~ubuntu-branches/ubuntu/wily/qemu-kvm-spice/wily

« back to all changes in this revision

Viewing changes to vl.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-10-19 10:44:56 UTC
  • Revision ID: james.westby@ubuntu.com-20111019104456-xgvskumk3sxi97f4
Tags: upstream-0.15.0+noroms
ImportĀ upstreamĀ versionĀ 0.15.0+noroms

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <sys/times.h>
 
38
#include <sys/wait.h>
 
39
#include <termios.h>
 
40
#include <sys/mman.h>
 
41
#include <sys/ioctl.h>
 
42
#include <sys/resource.h>
 
43
#include <sys/socket.h>
 
44
#include <netinet/in.h>
 
45
#include <net/if.h>
 
46
#include <arpa/inet.h>
 
47
#include <dirent.h>
 
48
#include <netdb.h>
 
49
#include <sys/select.h>
 
50
 
 
51
#ifdef CONFIG_BSD
 
52
#include <sys/stat.h>
 
53
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
 
54
#include <libutil.h>
 
55
#include <sys/sysctl.h>
 
56
#else
 
57
#include <util.h>
 
58
#endif
 
59
#else
 
60
#ifdef __linux__
 
61
#include <pty.h>
 
62
#include <malloc.h>
 
63
 
 
64
#include <linux/ppdev.h>
 
65
#include <linux/parport.h>
 
66
#endif
 
67
#ifdef __sun__
 
68
#include <sys/stat.h>
 
69
#include <sys/ethernet.h>
 
70
#include <sys/sockio.h>
 
71
#include <netinet/arp.h>
 
72
#include <netinet/in_systm.h>
 
73
#include <netinet/ip.h>
 
74
#include <netinet/ip_icmp.h> // must come after ip.h
 
75
#include <netinet/udp.h>
 
76
#include <netinet/tcp.h>
 
77
#include <net/if.h>
 
78
#include <syslog.h>
 
79
#include <stropts.h>
 
80
#endif
 
81
#endif
 
82
#endif
 
83
 
 
84
#if defined(__OpenBSD__)
 
85
#include <util.h>
 
86
#endif
 
87
 
 
88
#if defined(CONFIG_VDE)
 
89
#include <libvdeplug.h>
 
90
#endif
 
91
 
 
92
#ifdef _WIN32
 
93
#include <windows.h>
 
94
#endif
 
95
 
 
96
#ifdef CONFIG_SDL
 
97
#if defined(__APPLE__) || defined(main)
 
98
#include <SDL.h>
 
99
int qemu_main(int argc, char **argv, char **envp);
 
100
int main(int argc, char **argv)
 
101
{
 
102
    return qemu_main(argc, argv, NULL);
 
103
}
 
104
#undef main
 
105
#define main qemu_main
 
106
#endif
 
107
#endif /* CONFIG_SDL */
 
108
 
 
109
#ifdef CONFIG_COCOA
 
110
#undef main
 
111
#define main qemu_main
 
112
#endif /* CONFIG_COCOA */
 
113
 
 
114
#include "hw/hw.h"
 
115
#include "hw/boards.h"
 
116
#include "hw/usb.h"
 
117
#include "hw/pcmcia.h"
 
118
#include "hw/pc.h"
 
119
#include "hw/isa.h"
 
120
#include "hw/baum.h"
 
121
#include "hw/bt.h"
 
122
#include "hw/watchdog.h"
 
123
#include "hw/smbios.h"
 
124
#include "hw/xen.h"
 
125
#include "hw/qdev.h"
 
126
#include "hw/loader.h"
 
127
#include "bt-host.h"
 
128
#include "net.h"
 
129
#include "net/slirp.h"
 
130
#include "monitor.h"
 
131
#include "console.h"
 
132
#include "sysemu.h"
 
133
#include "gdbstub.h"
 
134
#include "qemu-timer.h"
 
135
#include "qemu-char.h"
 
136
#include "cache-utils.h"
 
137
#include "block.h"
 
138
#include "blockdev.h"
 
139
#include "block-migration.h"
 
140
#include "dma.h"
 
141
#include "audio/audio.h"
 
142
#include "migration.h"
 
143
#include "kvm.h"
 
144
#include "qemu-option.h"
 
145
#include "qemu-config.h"
 
146
#include "qemu-objects.h"
 
147
#include "qemu-options.h"
 
148
#include "kvm.h"
 
149
#ifdef CONFIG_VIRTFS
 
150
#include "fsdev/qemu-fsdev.h"
 
151
#endif
 
152
 
 
153
#include "disas.h"
 
154
 
 
155
#include "qemu_socket.h"
 
156
 
 
157
#include "slirp/libslirp.h"
 
158
 
 
159
#include "trace.h"
 
160
#include "simpletrace.h"
 
161
#include "qemu-queue.h"
 
162
#include "cpus.h"
 
163
#include "arch_init.h"
 
164
 
 
165
#include "ui/qemu-spice.h"
 
166
 
 
167
//#define DEBUG_NET
 
168
//#define DEBUG_SLIRP
 
169
 
 
170
#define DEFAULT_RAM_SIZE 128
 
171
 
 
172
#define MAX_VIRTIO_CONSOLES 1
 
173
 
 
174
static const char *data_dir;
 
175
const char *bios_name = NULL;
 
176
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
 
177
DisplayType display_type = DT_DEFAULT;
 
178
int display_remote = 0;
 
179
const char* keyboard_layout = NULL;
 
180
ram_addr_t ram_size;
 
181
const char *mem_path = NULL;
 
182
#ifdef MAP_POPULATE
 
183
int mem_prealloc = 0; /* force preallocation of physical target memory */
 
184
#endif
 
185
int nb_nics;
 
186
NICInfo nd_table[MAX_NICS];
 
187
int vm_running;
 
188
int autostart;
 
189
int incoming_expected; /* Started with -incoming and waiting for incoming */
 
190
static int rtc_utc = 1;
 
191
static int rtc_date_offset = -1; /* -1 means no change */
 
192
QEMUClock *rtc_clock;
 
193
int vga_interface_type = VGA_NONE;
 
194
static int full_screen = 0;
 
195
#ifdef CONFIG_SDL
 
196
static int no_frame = 0;
 
197
#endif
 
198
int no_quit = 0;
 
199
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 
200
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 
201
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 
202
int win2k_install_hack = 0;
 
203
int rtc_td_hack = 0;
 
204
int usb_enabled = 0;
 
205
int singlestep = 0;
 
206
int smp_cpus = 1;
 
207
int max_cpus = 0;
 
208
int smp_cores = 1;
 
209
int smp_threads = 1;
 
210
#ifdef CONFIG_VNC
 
211
const char *vnc_display;
 
212
#endif
 
213
int acpi_enabled = 1;
 
214
int no_hpet = 0;
 
215
int fd_bootchk = 1;
 
216
int no_reboot = 0;
 
217
int no_shutdown = 0;
 
218
int cursor_hide = 1;
 
219
int graphic_rotate = 0;
 
220
const char *watchdog;
 
221
QEMUOptionRom option_rom[MAX_OPTION_ROMS];
 
222
int nb_option_roms;
 
223
int semihosting_enabled = 0;
 
224
int time_drift_fix = 0;
 
225
unsigned int kvm_shadow_memory = 0;
 
226
int old_param = 0;
 
227
const char *qemu_name;
 
228
int alt_grab = 0;
 
229
int ctrl_grab = 0;
 
230
unsigned int nb_prom_envs = 0;
 
231
const char *prom_envs[MAX_PROM_ENVS];
 
232
const char *nvram = NULL;
 
233
int boot_menu;
 
234
 
 
235
typedef struct FWBootEntry FWBootEntry;
 
236
 
 
237
struct FWBootEntry {
 
238
    QTAILQ_ENTRY(FWBootEntry) link;
 
239
    int32_t bootindex;
 
240
    DeviceState *dev;
 
241
    char *suffix;
 
242
};
 
243
 
 
244
QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
 
245
 
 
246
int nb_numa_nodes;
 
247
uint64_t node_mem[MAX_NODES];
 
248
uint64_t node_cpumask[MAX_NODES];
 
249
 
 
250
static QEMUTimer *nographic_timer;
 
251
 
 
252
uint8_t qemu_uuid[16];
 
253
 
 
254
static QEMUBootSetHandler *boot_set_handler;
 
255
static void *boot_set_opaque;
 
256
 
 
257
static NotifierList exit_notifiers =
 
258
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);
 
259
 
 
260
static NotifierList machine_init_done_notifiers =
 
261
    NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
 
262
 
 
263
static int tcg_allowed = 1;
 
264
int kvm_allowed = 0;
 
265
int xen_allowed = 0;
 
266
uint32_t xen_domid;
 
267
enum xen_mode xen_mode = XEN_EMULATE;
 
268
 
 
269
static int default_serial = 1;
 
270
static int default_parallel = 1;
 
271
static int default_virtcon = 1;
 
272
static int default_monitor = 1;
 
273
static int default_vga = 1;
 
274
static int default_floppy = 1;
 
275
static int default_cdrom = 1;
 
276
static int default_sdcard = 1;
 
277
 
 
278
static struct {
 
279
    const char *driver;
 
280
    int *flag;
 
281
} default_list[] = {
 
282
    { .driver = "isa-serial",           .flag = &default_serial    },
 
283
    { .driver = "isa-parallel",         .flag = &default_parallel  },
 
284
    { .driver = "isa-fdc",              .flag = &default_floppy    },
 
285
    { .driver = "ide-cd",               .flag = &default_cdrom     },
 
286
    { .driver = "ide-hd",               .flag = &default_cdrom     },
 
287
    { .driver = "ide-drive",            .flag = &default_cdrom     },
 
288
    { .driver = "scsi-cd",              .flag = &default_cdrom     },
 
289
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
 
290
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
 
291
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
 
292
    { .driver = "VGA",                  .flag = &default_vga       },
 
293
    { .driver = "cirrus-vga",           .flag = &default_vga       },
 
294
    { .driver = "vmware-svga",          .flag = &default_vga       },
 
295
    { .driver = "isa-vga",              .flag = &default_vga       },
 
296
    { .driver = "qxl-vga",              .flag = &default_vga       },
 
297
};
 
298
 
 
299
static int default_driver_check(QemuOpts *opts, void *opaque)
 
300
{
 
301
    const char *driver = qemu_opt_get(opts, "driver");
 
302
    int i;
 
303
 
 
304
    if (!driver)
 
305
        return 0;
 
306
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
 
307
        if (strcmp(default_list[i].driver, driver) != 0)
 
308
            continue;
 
309
        *(default_list[i].flag) = 0;
 
310
    }
 
311
    return 0;
 
312
}
 
313
 
 
314
/***********************************************************/
 
315
/* real time host monotonic timer */
 
316
 
 
317
/***********************************************************/
 
318
/* host time/date access */
 
319
void qemu_get_timedate(struct tm *tm, int offset)
 
320
{
 
321
    time_t ti;
 
322
    struct tm *ret;
 
323
 
 
324
    time(&ti);
 
325
    ti += offset;
 
326
    if (rtc_date_offset == -1) {
 
327
        if (rtc_utc)
 
328
            ret = gmtime(&ti);
 
329
        else
 
330
            ret = localtime(&ti);
 
331
    } else {
 
332
        ti -= rtc_date_offset;
 
333
        ret = gmtime(&ti);
 
334
    }
 
335
 
 
336
    memcpy(tm, ret, sizeof(struct tm));
 
337
}
 
338
 
 
339
int qemu_timedate_diff(struct tm *tm)
 
340
{
 
341
    time_t seconds;
 
342
 
 
343
    if (rtc_date_offset == -1)
 
344
        if (rtc_utc)
 
345
            seconds = mktimegm(tm);
 
346
        else
 
347
            seconds = mktime(tm);
 
348
    else
 
349
        seconds = mktimegm(tm) + rtc_date_offset;
 
350
 
 
351
    return seconds - time(NULL);
 
352
}
 
353
 
 
354
void rtc_change_mon_event(struct tm *tm)
 
355
{
 
356
    QObject *data;
 
357
 
 
358
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
 
359
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
 
360
    qobject_decref(data);
 
361
}
 
362
 
 
363
static void configure_rtc_date_offset(const char *startdate, int legacy)
 
364
{
 
365
    time_t rtc_start_date;
 
366
    struct tm tm;
 
367
 
 
368
    if (!strcmp(startdate, "now") && legacy) {
 
369
        rtc_date_offset = -1;
 
370
    } else {
 
371
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
 
372
                   &tm.tm_year,
 
373
                   &tm.tm_mon,
 
374
                   &tm.tm_mday,
 
375
                   &tm.tm_hour,
 
376
                   &tm.tm_min,
 
377
                   &tm.tm_sec) == 6) {
 
378
            /* OK */
 
379
        } else if (sscanf(startdate, "%d-%d-%d",
 
380
                          &tm.tm_year,
 
381
                          &tm.tm_mon,
 
382
                          &tm.tm_mday) == 3) {
 
383
            tm.tm_hour = 0;
 
384
            tm.tm_min = 0;
 
385
            tm.tm_sec = 0;
 
386
        } else {
 
387
            goto date_fail;
 
388
        }
 
389
        tm.tm_year -= 1900;
 
390
        tm.tm_mon--;
 
391
        rtc_start_date = mktimegm(&tm);
 
392
        if (rtc_start_date == -1) {
 
393
        date_fail:
 
394
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
 
395
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
 
396
            exit(1);
 
397
        }
 
398
        rtc_date_offset = time(NULL) - rtc_start_date;
 
399
    }
 
400
}
 
401
 
 
402
static void configure_rtc(QemuOpts *opts)
 
403
{
 
404
    const char *value;
 
405
 
 
406
    value = qemu_opt_get(opts, "base");
 
407
    if (value) {
 
408
        if (!strcmp(value, "utc")) {
 
409
            rtc_utc = 1;
 
410
        } else if (!strcmp(value, "localtime")) {
 
411
            rtc_utc = 0;
 
412
        } else {
 
413
            configure_rtc_date_offset(value, 0);
 
414
        }
 
415
    }
 
416
    value = qemu_opt_get(opts, "clock");
 
417
    if (value) {
 
418
        if (!strcmp(value, "host")) {
 
419
            rtc_clock = host_clock;
 
420
        } else if (!strcmp(value, "vm")) {
 
421
            rtc_clock = vm_clock;
 
422
        } else {
 
423
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
 
424
            exit(1);
 
425
        }
 
426
    }
 
427
    value = qemu_opt_get(opts, "driftfix");
 
428
    if (value) {
 
429
        if (!strcmp(value, "slew")) {
 
430
            rtc_td_hack = 1;
 
431
        } else if (!strcmp(value, "none")) {
 
432
            rtc_td_hack = 0;
 
433
        } else {
 
434
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
 
435
            exit(1);
 
436
        }
 
437
    }
 
438
}
 
439
 
 
440
/***********************************************************/
 
441
/* Bluetooth support */
 
442
static int nb_hcis;
 
443
static int cur_hci;
 
444
static struct HCIInfo *hci_table[MAX_NICS];
 
445
 
 
446
static struct bt_vlan_s {
 
447
    struct bt_scatternet_s net;
 
448
    int id;
 
449
    struct bt_vlan_s *next;
 
450
} *first_bt_vlan;
 
451
 
 
452
/* find or alloc a new bluetooth "VLAN" */
 
453
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
 
454
{
 
455
    struct bt_vlan_s **pvlan, *vlan;
 
456
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
 
457
        if (vlan->id == id)
 
458
            return &vlan->net;
 
459
    }
 
460
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
 
461
    vlan->id = id;
 
462
    pvlan = &first_bt_vlan;
 
463
    while (*pvlan != NULL)
 
464
        pvlan = &(*pvlan)->next;
 
465
    *pvlan = vlan;
 
466
    return &vlan->net;
 
467
}
 
468
 
 
469
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
 
470
{
 
471
}
 
472
 
 
473
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
 
474
{
 
475
    return -ENOTSUP;
 
476
}
 
477
 
 
478
static struct HCIInfo null_hci = {
 
479
    .cmd_send = null_hci_send,
 
480
    .sco_send = null_hci_send,
 
481
    .acl_send = null_hci_send,
 
482
    .bdaddr_set = null_hci_addr_set,
 
483
};
 
484
 
 
485
struct HCIInfo *qemu_next_hci(void)
 
486
{
 
487
    if (cur_hci == nb_hcis)
 
488
        return &null_hci;
 
489
 
 
490
    return hci_table[cur_hci++];
 
491
}
 
492
 
 
493
static struct HCIInfo *hci_init(const char *str)
 
494
{
 
495
    char *endp;
 
496
    struct bt_scatternet_s *vlan = 0;
 
497
 
 
498
    if (!strcmp(str, "null"))
 
499
        /* null */
 
500
        return &null_hci;
 
501
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
 
502
        /* host[:hciN] */
 
503
        return bt_host_hci(str[4] ? str + 5 : "hci0");
 
504
    else if (!strncmp(str, "hci", 3)) {
 
505
        /* hci[,vlan=n] */
 
506
        if (str[3]) {
 
507
            if (!strncmp(str + 3, ",vlan=", 6)) {
 
508
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
 
509
                if (*endp)
 
510
                    vlan = 0;
 
511
            }
 
512
        } else
 
513
            vlan = qemu_find_bt_vlan(0);
 
514
        if (vlan)
 
515
           return bt_new_hci(vlan);
 
516
    }
 
517
 
 
518
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
 
519
 
 
520
    return 0;
 
521
}
 
522
 
 
523
static int bt_hci_parse(const char *str)
 
524
{
 
525
    struct HCIInfo *hci;
 
526
    bdaddr_t bdaddr;
 
527
 
 
528
    if (nb_hcis >= MAX_NICS) {
 
529
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
 
530
        return -1;
 
531
    }
 
532
 
 
533
    hci = hci_init(str);
 
534
    if (!hci)
 
535
        return -1;
 
536
 
 
537
    bdaddr.b[0] = 0x52;
 
538
    bdaddr.b[1] = 0x54;
 
539
    bdaddr.b[2] = 0x00;
 
540
    bdaddr.b[3] = 0x12;
 
541
    bdaddr.b[4] = 0x34;
 
542
    bdaddr.b[5] = 0x56 + nb_hcis;
 
543
    hci->bdaddr_set(hci, bdaddr.b);
 
544
 
 
545
    hci_table[nb_hcis++] = hci;
 
546
 
 
547
    return 0;
 
548
}
 
549
 
 
550
static void bt_vhci_add(int vlan_id)
 
551
{
 
552
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
 
553
 
 
554
    if (!vlan->slave)
 
555
        fprintf(stderr, "qemu: warning: adding a VHCI to "
 
556
                        "an empty scatternet %i\n", vlan_id);
 
557
 
 
558
    bt_vhci_init(bt_new_hci(vlan));
 
559
}
 
560
 
 
561
static struct bt_device_s *bt_device_add(const char *opt)
 
562
{
 
563
    struct bt_scatternet_s *vlan;
 
564
    int vlan_id = 0;
 
565
    char *endp = strstr(opt, ",vlan=");
 
566
    int len = (endp ? endp - opt : strlen(opt)) + 1;
 
567
    char devname[10];
 
568
 
 
569
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
 
570
 
 
571
    if (endp) {
 
572
        vlan_id = strtol(endp + 6, &endp, 0);
 
573
        if (*endp) {
 
574
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
 
575
            return 0;
 
576
        }
 
577
    }
 
578
 
 
579
    vlan = qemu_find_bt_vlan(vlan_id);
 
580
 
 
581
    if (!vlan->slave)
 
582
        fprintf(stderr, "qemu: warning: adding a slave device to "
 
583
                        "an empty scatternet %i\n", vlan_id);
 
584
 
 
585
    if (!strcmp(devname, "keyboard"))
 
586
        return bt_keyboard_init(vlan);
 
587
 
 
588
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
 
589
    return 0;
 
590
}
 
591
 
 
592
static int bt_parse(const char *opt)
 
593
{
 
594
    const char *endp, *p;
 
595
    int vlan;
 
596
 
 
597
    if (strstart(opt, "hci", &endp)) {
 
598
        if (!*endp || *endp == ',') {
 
599
            if (*endp)
 
600
                if (!strstart(endp, ",vlan=", 0))
 
601
                    opt = endp + 1;
 
602
 
 
603
            return bt_hci_parse(opt);
 
604
       }
 
605
    } else if (strstart(opt, "vhci", &endp)) {
 
606
        if (!*endp || *endp == ',') {
 
607
            if (*endp) {
 
608
                if (strstart(endp, ",vlan=", &p)) {
 
609
                    vlan = strtol(p, (char **) &endp, 0);
 
610
                    if (*endp) {
 
611
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
 
612
                        return 1;
 
613
                    }
 
614
                } else {
 
615
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
 
616
                    return 1;
 
617
                }
 
618
            } else
 
619
                vlan = 0;
 
620
 
 
621
            bt_vhci_add(vlan);
 
622
            return 0;
 
623
        }
 
624
    } else if (strstart(opt, "device:", &endp))
 
625
        return !bt_device_add(endp);
 
626
 
 
627
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
 
628
    return 1;
 
629
}
 
630
 
 
631
/***********************************************************/
 
632
/* QEMU Block devices */
 
633
 
 
634
#define HD_OPTS "media=disk"
 
635
#define CDROM_OPTS "media=cdrom"
 
636
#define FD_OPTS ""
 
637
#define PFLASH_OPTS ""
 
638
#define MTD_OPTS ""
 
639
#define SD_OPTS ""
 
640
 
 
641
static int drive_init_func(QemuOpts *opts, void *opaque)
 
642
{
 
643
    int *use_scsi = opaque;
 
644
 
 
645
    return drive_init(opts, *use_scsi) == NULL;
 
646
}
 
647
 
 
648
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
 
649
{
 
650
    if (NULL == qemu_opt_get(opts, "snapshot")) {
 
651
        qemu_opt_set(opts, "snapshot", "on");
 
652
    }
 
653
    return 0;
 
654
}
 
655
 
 
656
static void default_drive(int enable, int snapshot, int use_scsi,
 
657
                          BlockInterfaceType type, int index,
 
658
                          const char *optstr)
 
659
{
 
660
    QemuOpts *opts;
 
661
 
 
662
    if (type == IF_DEFAULT) {
 
663
        type = use_scsi ? IF_SCSI : IF_IDE;
 
664
    }
 
665
 
 
666
    if (!enable || drive_get_by_index(type, index)) {
 
667
        return;
 
668
    }
 
669
 
 
670
    opts = drive_add(type, index, NULL, optstr);
 
671
    if (snapshot) {
 
672
        drive_enable_snapshot(opts, NULL);
 
673
    }
 
674
    if (!drive_init(opts, use_scsi)) {
 
675
        exit(1);
 
676
    }
 
677
}
 
678
 
 
679
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
 
680
{
 
681
    boot_set_handler = func;
 
682
    boot_set_opaque = opaque;
 
683
}
 
684
 
 
685
int qemu_boot_set(const char *boot_devices)
 
686
{
 
687
    if (!boot_set_handler) {
 
688
        return -EINVAL;
 
689
    }
 
690
    return boot_set_handler(boot_set_opaque, boot_devices);
 
691
}
 
692
 
 
693
static void validate_bootdevices(char *devices)
 
694
{
 
695
    /* We just do some generic consistency checks */
 
696
    const char *p;
 
697
    int bitmap = 0;
 
698
 
 
699
    for (p = devices; *p != '\0'; p++) {
 
700
        /* Allowed boot devices are:
 
701
         * a-b: floppy disk drives
 
702
         * c-f: IDE disk drives
 
703
         * g-m: machine implementation dependant drives
 
704
         * n-p: network devices
 
705
         * It's up to each machine implementation to check if the given boot
 
706
         * devices match the actual hardware implementation and firmware
 
707
         * features.
 
708
         */
 
709
        if (*p < 'a' || *p > 'p') {
 
710
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
 
711
            exit(1);
 
712
        }
 
713
        if (bitmap & (1 << (*p - 'a'))) {
 
714
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
 
715
            exit(1);
 
716
        }
 
717
        bitmap |= 1 << (*p - 'a');
 
718
    }
 
719
}
 
720
 
 
721
static void restore_boot_devices(void *opaque)
 
722
{
 
723
    char *standard_boot_devices = opaque;
 
724
    static int first = 1;
 
725
 
 
726
    /* Restore boot order and remove ourselves after the first boot */
 
727
    if (first) {
 
728
        first = 0;
 
729
        return;
 
730
    }
 
731
 
 
732
    qemu_boot_set(standard_boot_devices);
 
733
 
 
734
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
 
735
    qemu_free(standard_boot_devices);
 
736
}
 
737
 
 
738
void add_boot_device_path(int32_t bootindex, DeviceState *dev,
 
739
                          const char *suffix)
 
740
{
 
741
    FWBootEntry *node, *i;
 
742
 
 
743
    if (bootindex < 0) {
 
744
        return;
 
745
    }
 
746
 
 
747
    assert(dev != NULL || suffix != NULL);
 
748
 
 
749
    node = qemu_mallocz(sizeof(FWBootEntry));
 
750
    node->bootindex = bootindex;
 
751
    node->suffix = suffix ? qemu_strdup(suffix) : NULL;
 
752
    node->dev = dev;
 
753
 
 
754
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
 
755
        if (i->bootindex == bootindex) {
 
756
            fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
 
757
            exit(1);
 
758
        } else if (i->bootindex < bootindex) {
 
759
            continue;
 
760
        }
 
761
        QTAILQ_INSERT_BEFORE(i, node, link);
 
762
        return;
 
763
    }
 
764
    QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
 
765
}
 
766
 
 
767
/*
 
768
 * This function returns null terminated string that consist of new line
 
769
 * separated device paths.
 
770
 *
 
771
 * memory pointed by "size" is assigned total length of the array in bytes
 
772
 *
 
773
 */
 
774
char *get_boot_devices_list(uint32_t *size)
 
775
{
 
776
    FWBootEntry *i;
 
777
    uint32_t total = 0;
 
778
    char *list = NULL;
 
779
 
 
780
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
 
781
        char *devpath = NULL, *bootpath;
 
782
        int len;
 
783
 
 
784
        if (i->dev) {
 
785
            devpath = qdev_get_fw_dev_path(i->dev);
 
786
            assert(devpath);
 
787
        }
 
788
 
 
789
        if (i->suffix && devpath) {
 
790
            size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
 
791
 
 
792
            bootpath = qemu_malloc(bootpathlen);
 
793
            snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
 
794
            qemu_free(devpath);
 
795
        } else if (devpath) {
 
796
            bootpath = devpath;
 
797
        } else {
 
798
            bootpath = qemu_strdup(i->suffix);
 
799
            assert(bootpath);
 
800
        }
 
801
 
 
802
        if (total) {
 
803
            list[total-1] = '\n';
 
804
        }
 
805
        len = strlen(bootpath) + 1;
 
806
        list = qemu_realloc(list, total + len);
 
807
        memcpy(&list[total], bootpath, len);
 
808
        total += len;
 
809
        qemu_free(bootpath);
 
810
    }
 
811
 
 
812
    *size = total;
 
813
 
 
814
    return list;
 
815
}
 
816
 
 
817
static void numa_add(const char *optarg)
 
818
{
 
819
    char option[128];
 
820
    char *endptr;
 
821
    unsigned long long value, endvalue;
 
822
    int nodenr;
 
823
 
 
824
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
 
825
    if (!strcmp(option, "node")) {
 
826
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
 
827
            nodenr = nb_numa_nodes;
 
828
        } else {
 
829
            nodenr = strtoull(option, NULL, 10);
 
830
        }
 
831
 
 
832
        if (get_param_value(option, 128, "mem", optarg) == 0) {
 
833
            node_mem[nodenr] = 0;
 
834
        } else {
 
835
            int64_t sval;
 
836
            sval = strtosz(option, NULL);
 
837
            if (sval < 0) {
 
838
                fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
 
839
                exit(1);
 
840
            }
 
841
            node_mem[nodenr] = sval;
 
842
        }
 
843
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
 
844
            node_cpumask[nodenr] = 0;
 
845
        } else {
 
846
            value = strtoull(option, &endptr, 10);
 
847
            if (value >= 64) {
 
848
                value = 63;
 
849
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
 
850
            } else {
 
851
                if (*endptr == '-') {
 
852
                    endvalue = strtoull(endptr+1, &endptr, 10);
 
853
                    if (endvalue >= 63) {
 
854
                        endvalue = 62;
 
855
                        fprintf(stderr,
 
856
                            "only 63 CPUs in NUMA mode supported.\n");
 
857
                    }
 
858
                    value = (2ULL << endvalue) - (1ULL << value);
 
859
                } else {
 
860
                    value = 1ULL << value;
 
861
                }
 
862
            }
 
863
            node_cpumask[nodenr] = value;
 
864
        }
 
865
        nb_numa_nodes++;
 
866
    }
 
867
    return;
 
868
}
 
869
 
 
870
static void smp_parse(const char *optarg)
 
871
{
 
872
    int smp, sockets = 0, threads = 0, cores = 0;
 
873
    char *endptr;
 
874
    char option[128];
 
875
 
 
876
    smp = strtoul(optarg, &endptr, 10);
 
877
    if (endptr != optarg) {
 
878
        if (*endptr == ',') {
 
879
            endptr++;
 
880
        }
 
881
    }
 
882
    if (get_param_value(option, 128, "sockets", endptr) != 0)
 
883
        sockets = strtoull(option, NULL, 10);
 
884
    if (get_param_value(option, 128, "cores", endptr) != 0)
 
885
        cores = strtoull(option, NULL, 10);
 
886
    if (get_param_value(option, 128, "threads", endptr) != 0)
 
887
        threads = strtoull(option, NULL, 10);
 
888
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
 
889
        max_cpus = strtoull(option, NULL, 10);
 
890
 
 
891
    /* compute missing values, prefer sockets over cores over threads */
 
892
    if (smp == 0 || sockets == 0) {
 
893
        sockets = sockets > 0 ? sockets : 1;
 
894
        cores = cores > 0 ? cores : 1;
 
895
        threads = threads > 0 ? threads : 1;
 
896
        if (smp == 0) {
 
897
            smp = cores * threads * sockets;
 
898
        }
 
899
    } else {
 
900
        if (cores == 0) {
 
901
            threads = threads > 0 ? threads : 1;
 
902
            cores = smp / (sockets * threads);
 
903
        } else {
 
904
            threads = smp / (cores * sockets);
 
905
        }
 
906
    }
 
907
    smp_cpus = smp;
 
908
    smp_cores = cores > 0 ? cores : 1;
 
909
    smp_threads = threads > 0 ? threads : 1;
 
910
    if (max_cpus == 0)
 
911
        max_cpus = smp_cpus;
 
912
}
 
913
 
 
914
/***********************************************************/
 
915
/* USB devices */
 
916
 
 
917
static int usb_device_add(const char *devname)
 
918
{
 
919
    const char *p;
 
920
    USBDevice *dev = NULL;
 
921
 
 
922
    if (!usb_enabled)
 
923
        return -1;
 
924
 
 
925
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
 
926
    dev = usbdevice_create(devname);
 
927
    if (dev)
 
928
        goto done;
 
929
 
 
930
    /* the other ones */
 
931
#ifndef CONFIG_LINUX
 
932
    /* only the linux version is qdev-ified, usb-bsd still needs this */
 
933
    if (strstart(devname, "host:", &p)) {
 
934
        dev = usb_host_device_open(p);
 
935
    } else
 
936
#endif
 
937
    if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
 
938
        dev = usb_bt_init(devname[2] ? hci_init(p) :
 
939
                        bt_new_hci(qemu_find_bt_vlan(0)));
 
940
    } else {
 
941
        return -1;
 
942
    }
 
943
    if (!dev)
 
944
        return -1;
 
945
 
 
946
done:
 
947
    return 0;
 
948
}
 
949
 
 
950
static int usb_device_del(const char *devname)
 
951
{
 
952
    int bus_num, addr;
 
953
    const char *p;
 
954
 
 
955
    if (strstart(devname, "host:", &p))
 
956
        return usb_host_device_close(p);
 
957
 
 
958
    if (!usb_enabled)
 
959
        return -1;
 
960
 
 
961
    p = strchr(devname, '.');
 
962
    if (!p)
 
963
        return -1;
 
964
    bus_num = strtoul(devname, NULL, 0);
 
965
    addr = strtoul(p + 1, NULL, 0);
 
966
 
 
967
    return usb_device_delete_addr(bus_num, addr);
 
968
}
 
969
 
 
970
static int usb_parse(const char *cmdline)
 
971
{
 
972
    int r;
 
973
    r = usb_device_add(cmdline);
 
974
    if (r < 0) {
 
975
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
 
976
    }
 
977
    return r;
 
978
}
 
979
 
 
980
void do_usb_add(Monitor *mon, const QDict *qdict)
 
981
{
 
982
    const char *devname = qdict_get_str(qdict, "devname");
 
983
    if (usb_device_add(devname) < 0) {
 
984
        error_report("could not add USB device '%s'", devname);
 
985
    }
 
986
}
 
987
 
 
988
void do_usb_del(Monitor *mon, const QDict *qdict)
 
989
{
 
990
    const char *devname = qdict_get_str(qdict, "devname");
 
991
    if (usb_device_del(devname) < 0) {
 
992
        error_report("could not delete USB device '%s'", devname);
 
993
    }
 
994
}
 
995
 
 
996
/***********************************************************/
 
997
/* PCMCIA/Cardbus */
 
998
 
 
999
static struct pcmcia_socket_entry_s {
 
1000
    PCMCIASocket *socket;
 
1001
    struct pcmcia_socket_entry_s *next;
 
1002
} *pcmcia_sockets = 0;
 
1003
 
 
1004
void pcmcia_socket_register(PCMCIASocket *socket)
 
1005
{
 
1006
    struct pcmcia_socket_entry_s *entry;
 
1007
 
 
1008
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
 
1009
    entry->socket = socket;
 
1010
    entry->next = pcmcia_sockets;
 
1011
    pcmcia_sockets = entry;
 
1012
}
 
1013
 
 
1014
void pcmcia_socket_unregister(PCMCIASocket *socket)
 
1015
{
 
1016
    struct pcmcia_socket_entry_s *entry, **ptr;
 
1017
 
 
1018
    ptr = &pcmcia_sockets;
 
1019
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
 
1020
        if (entry->socket == socket) {
 
1021
            *ptr = entry->next;
 
1022
            qemu_free(entry);
 
1023
        }
 
1024
}
 
1025
 
 
1026
void pcmcia_info(Monitor *mon)
 
1027
{
 
1028
    struct pcmcia_socket_entry_s *iter;
 
1029
 
 
1030
    if (!pcmcia_sockets)
 
1031
        monitor_printf(mon, "No PCMCIA sockets\n");
 
1032
 
 
1033
    for (iter = pcmcia_sockets; iter; iter = iter->next)
 
1034
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
 
1035
                       iter->socket->attached ? iter->socket->card_string :
 
1036
                       "Empty");
 
1037
}
 
1038
 
 
1039
/***********************************************************/
 
1040
/* machine registration */
 
1041
 
 
1042
static QEMUMachine *first_machine = NULL;
 
1043
QEMUMachine *current_machine = NULL;
 
1044
 
 
1045
int qemu_register_machine(QEMUMachine *m)
 
1046
{
 
1047
    QEMUMachine **pm;
 
1048
    pm = &first_machine;
 
1049
    while (*pm != NULL)
 
1050
        pm = &(*pm)->next;
 
1051
    m->next = NULL;
 
1052
    *pm = m;
 
1053
    return 0;
 
1054
}
 
1055
 
 
1056
static QEMUMachine *find_machine(const char *name)
 
1057
{
 
1058
    QEMUMachine *m;
 
1059
 
 
1060
    for(m = first_machine; m != NULL; m = m->next) {
 
1061
        if (!strcmp(m->name, name))
 
1062
            return m;
 
1063
        if (m->alias && !strcmp(m->alias, name))
 
1064
            return m;
 
1065
    }
 
1066
    return NULL;
 
1067
}
 
1068
 
 
1069
static QEMUMachine *find_default_machine(void)
 
1070
{
 
1071
    QEMUMachine *m;
 
1072
 
 
1073
    for(m = first_machine; m != NULL; m = m->next) {
 
1074
        if (m->is_default) {
 
1075
            return m;
 
1076
        }
 
1077
    }
 
1078
    return NULL;
 
1079
}
 
1080
 
 
1081
/***********************************************************/
 
1082
/* main execution loop */
 
1083
 
 
1084
static void gui_update(void *opaque)
 
1085
{
 
1086
    uint64_t interval = GUI_REFRESH_INTERVAL;
 
1087
    DisplayState *ds = opaque;
 
1088
    DisplayChangeListener *dcl = ds->listeners;
 
1089
 
 
1090
    qemu_flush_coalesced_mmio_buffer();
 
1091
    dpy_refresh(ds);
 
1092
 
 
1093
    while (dcl != NULL) {
 
1094
        if (dcl->gui_timer_interval &&
 
1095
            dcl->gui_timer_interval < interval)
 
1096
            interval = dcl->gui_timer_interval;
 
1097
        dcl = dcl->next;
 
1098
    }
 
1099
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
 
1100
}
 
1101
 
 
1102
static void nographic_update(void *opaque)
 
1103
{
 
1104
    uint64_t interval = GUI_REFRESH_INTERVAL;
 
1105
 
 
1106
    qemu_flush_coalesced_mmio_buffer();
 
1107
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
 
1108
}
 
1109
 
 
1110
struct vm_change_state_entry {
 
1111
    VMChangeStateHandler *cb;
 
1112
    void *opaque;
 
1113
    QLIST_ENTRY (vm_change_state_entry) entries;
 
1114
};
 
1115
 
 
1116
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
 
1117
 
 
1118
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
 
1119
                                                     void *opaque)
 
1120
{
 
1121
    VMChangeStateEntry *e;
 
1122
 
 
1123
    e = qemu_mallocz(sizeof (*e));
 
1124
 
 
1125
    e->cb = cb;
 
1126
    e->opaque = opaque;
 
1127
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
 
1128
    return e;
 
1129
}
 
1130
 
 
1131
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
 
1132
{
 
1133
    QLIST_REMOVE (e, entries);
 
1134
    qemu_free (e);
 
1135
}
 
1136
 
 
1137
void vm_state_notify(int running, int reason)
 
1138
{
 
1139
    VMChangeStateEntry *e;
 
1140
 
 
1141
    trace_vm_state_notify(running, reason);
 
1142
 
 
1143
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
 
1144
        e->cb(e->opaque, running, reason);
 
1145
    }
 
1146
}
 
1147
 
 
1148
void vm_start(void)
 
1149
{
 
1150
    if (!vm_running) {
 
1151
        cpu_enable_ticks();
 
1152
        vm_running = 1;
 
1153
        vm_state_notify(1, 0);
 
1154
        resume_all_vcpus();
 
1155
        monitor_protocol_event(QEVENT_RESUME, NULL);
 
1156
    }
 
1157
}
 
1158
 
 
1159
/* reset/shutdown handler */
 
1160
 
 
1161
typedef struct QEMUResetEntry {
 
1162
    QTAILQ_ENTRY(QEMUResetEntry) entry;
 
1163
    QEMUResetHandler *func;
 
1164
    void *opaque;
 
1165
} QEMUResetEntry;
 
1166
 
 
1167
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
 
1168
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
 
1169
static int reset_requested;
 
1170
static int shutdown_requested, shutdown_signal = -1;
 
1171
static pid_t shutdown_pid;
 
1172
static int powerdown_requested;
 
1173
static int debug_requested;
 
1174
static int vmstop_requested;
 
1175
 
 
1176
int qemu_shutdown_requested_get(void)
 
1177
{
 
1178
    return shutdown_requested;
 
1179
}
 
1180
 
 
1181
int qemu_reset_requested_get(void)
 
1182
{
 
1183
    return reset_requested;
 
1184
}
 
1185
 
 
1186
int qemu_shutdown_requested(void)
 
1187
{
 
1188
    int r = shutdown_requested;
 
1189
    shutdown_requested = 0;
 
1190
    return r;
 
1191
}
 
1192
 
 
1193
void qemu_kill_report(void)
 
1194
{
 
1195
    if (shutdown_signal != -1) {
 
1196
        fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
 
1197
        if (shutdown_pid == 0) {
 
1198
            /* This happens for eg ^C at the terminal, so it's worth
 
1199
             * avoiding printing an odd message in that case.
 
1200
             */
 
1201
            fputc('\n', stderr);
 
1202
        } else {
 
1203
            fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
 
1204
        }
 
1205
        shutdown_signal = -1;
 
1206
    }
 
1207
}
 
1208
 
 
1209
int qemu_reset_requested(void)
 
1210
{
 
1211
    int r = reset_requested;
 
1212
    reset_requested = 0;
 
1213
    return r;
 
1214
}
 
1215
 
 
1216
int qemu_powerdown_requested(void)
 
1217
{
 
1218
    int r = powerdown_requested;
 
1219
    powerdown_requested = 0;
 
1220
    return r;
 
1221
}
 
1222
 
 
1223
static int qemu_debug_requested(void)
 
1224
{
 
1225
    int r = debug_requested;
 
1226
    debug_requested = 0;
 
1227
    return r;
 
1228
}
 
1229
 
 
1230
static int qemu_vmstop_requested(void)
 
1231
{
 
1232
    int r = vmstop_requested;
 
1233
    vmstop_requested = 0;
 
1234
    return r;
 
1235
}
 
1236
 
 
1237
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 
1238
{
 
1239
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
 
1240
 
 
1241
    re->func = func;
 
1242
    re->opaque = opaque;
 
1243
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
 
1244
}
 
1245
 
 
1246
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
 
1247
{
 
1248
    QEMUResetEntry *re;
 
1249
 
 
1250
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
 
1251
        if (re->func == func && re->opaque == opaque) {
 
1252
            QTAILQ_REMOVE(&reset_handlers, re, entry);
 
1253
            qemu_free(re);
 
1254
            return;
 
1255
        }
 
1256
    }
 
1257
}
 
1258
 
 
1259
void qemu_system_reset(bool report)
 
1260
{
 
1261
    QEMUResetEntry *re, *nre;
 
1262
 
 
1263
    /* reset all devices */
 
1264
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
 
1265
        re->func(re->opaque);
 
1266
    }
 
1267
    if (report) {
 
1268
        monitor_protocol_event(QEVENT_RESET, NULL);
 
1269
    }
 
1270
    cpu_synchronize_all_post_reset();
 
1271
}
 
1272
 
 
1273
void qemu_system_reset_request(void)
 
1274
{
 
1275
    if (no_reboot) {
 
1276
        shutdown_requested = 1;
 
1277
    } else {
 
1278
        reset_requested = 1;
 
1279
    }
 
1280
    cpu_stop_current();
 
1281
    qemu_notify_event();
 
1282
}
 
1283
 
 
1284
void qemu_system_killed(int signal, pid_t pid)
 
1285
{
 
1286
    shutdown_signal = signal;
 
1287
    shutdown_pid = pid;
 
1288
    qemu_system_shutdown_request();
 
1289
}
 
1290
 
 
1291
void qemu_system_shutdown_request(void)
 
1292
{
 
1293
    shutdown_requested = 1;
 
1294
    qemu_notify_event();
 
1295
}
 
1296
 
 
1297
void qemu_system_powerdown_request(void)
 
1298
{
 
1299
    powerdown_requested = 1;
 
1300
    qemu_notify_event();
 
1301
}
 
1302
 
 
1303
void qemu_system_debug_request(void)
 
1304
{
 
1305
    debug_requested = 1;
 
1306
    qemu_notify_event();
 
1307
}
 
1308
 
 
1309
void qemu_system_vmstop_request(int reason)
 
1310
{
 
1311
    vmstop_requested = reason;
 
1312
    qemu_notify_event();
 
1313
}
 
1314
 
 
1315
void main_loop_wait(int nonblocking)
 
1316
{
 
1317
    fd_set rfds, wfds, xfds;
 
1318
    int ret, nfds;
 
1319
    struct timeval tv;
 
1320
    int timeout;
 
1321
 
 
1322
    if (nonblocking)
 
1323
        timeout = 0;
 
1324
    else {
 
1325
        timeout = qemu_calculate_timeout();
 
1326
        qemu_bh_update_timeout(&timeout);
 
1327
    }
 
1328
 
 
1329
    os_host_main_loop_wait(&timeout);
 
1330
 
 
1331
    tv.tv_sec = timeout / 1000;
 
1332
    tv.tv_usec = (timeout % 1000) * 1000;
 
1333
 
 
1334
    /* poll any events */
 
1335
    /* XXX: separate device handlers from system ones */
 
1336
    nfds = -1;
 
1337
    FD_ZERO(&rfds);
 
1338
    FD_ZERO(&wfds);
 
1339
    FD_ZERO(&xfds);
 
1340
    qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
 
1341
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
 
1342
 
 
1343
    qemu_mutex_unlock_iothread();
 
1344
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
 
1345
    qemu_mutex_lock_iothread();
 
1346
 
 
1347
    qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
 
1348
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
 
1349
 
 
1350
    qemu_run_all_timers();
 
1351
 
 
1352
    /* Check bottom-halves last in case any of the earlier events triggered
 
1353
       them.  */
 
1354
    qemu_bh_poll();
 
1355
 
 
1356
}
 
1357
 
 
1358
#ifndef CONFIG_IOTHREAD
 
1359
static int vm_request_pending(void)
 
1360
{
 
1361
    return powerdown_requested ||
 
1362
           reset_requested ||
 
1363
           shutdown_requested ||
 
1364
           debug_requested ||
 
1365
           vmstop_requested;
 
1366
}
 
1367
#endif
 
1368
 
 
1369
qemu_irq qemu_system_powerdown;
 
1370
 
 
1371
static void main_loop(void)
 
1372
{
 
1373
    bool nonblocking = false;
 
1374
#ifdef CONFIG_PROFILER
 
1375
    int64_t ti;
 
1376
#endif
 
1377
    int r;
 
1378
 
 
1379
    qemu_main_loop_start();
 
1380
 
 
1381
    for (;;) {
 
1382
#ifndef CONFIG_IOTHREAD
 
1383
        nonblocking = cpu_exec_all();
 
1384
        if (vm_request_pending()) {
 
1385
            nonblocking = true;
 
1386
        }
 
1387
#endif
 
1388
#ifdef CONFIG_PROFILER
 
1389
        ti = profile_getclock();
 
1390
#endif
 
1391
        main_loop_wait(nonblocking);
 
1392
#ifdef CONFIG_PROFILER
 
1393
        dev_time += profile_getclock() - ti;
 
1394
#endif
 
1395
 
 
1396
        if (qemu_debug_requested()) {
 
1397
            vm_stop(VMSTOP_DEBUG);
 
1398
        }
 
1399
        if (qemu_shutdown_requested()) {
 
1400
            qemu_kill_report();
 
1401
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
 
1402
            if (no_shutdown) {
 
1403
                vm_stop(VMSTOP_SHUTDOWN);
 
1404
            } else
 
1405
                break;
 
1406
        }
 
1407
        if (qemu_reset_requested()) {
 
1408
            pause_all_vcpus();
 
1409
            cpu_synchronize_all_states();
 
1410
            qemu_system_reset(VMRESET_REPORT);
 
1411
            resume_all_vcpus();
 
1412
        }
 
1413
        if (qemu_powerdown_requested()) {
 
1414
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
 
1415
            qemu_irq_raise(qemu_system_powerdown);
 
1416
        }
 
1417
        if ((r = qemu_vmstop_requested())) {
 
1418
            vm_stop(r);
 
1419
        }
 
1420
    }
 
1421
    bdrv_close_all();
 
1422
    pause_all_vcpus();
 
1423
}
 
1424
 
 
1425
static void version(void)
 
1426
{
 
1427
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
 
1428
}
 
1429
 
 
1430
static void help(int exitcode)
 
1431
{
 
1432
    const char *options_help =
 
1433
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
 
1434
        opt_help
 
1435
#define DEFHEADING(text) stringify(text) "\n"
 
1436
#include "qemu-options.def"
 
1437
#undef DEF
 
1438
#undef DEFHEADING
 
1439
#undef GEN_DOCS
 
1440
        ;
 
1441
    version();
 
1442
    printf("usage: %s [options] [disk_image]\n"
 
1443
           "\n"
 
1444
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
 
1445
           "\n"
 
1446
           "%s\n"
 
1447
           "During emulation, the following keys are useful:\n"
 
1448
           "ctrl-alt-f      toggle full screen\n"
 
1449
           "ctrl-alt-n      switch to virtual console 'n'\n"
 
1450
           "ctrl-alt        toggle mouse and keyboard grab\n"
 
1451
           "\n"
 
1452
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
 
1453
           "qemu",
 
1454
           options_help);
 
1455
    exit(exitcode);
 
1456
}
 
1457
 
 
1458
#define HAS_ARG 0x0001
 
1459
 
 
1460
typedef struct QEMUOption {
 
1461
    const char *name;
 
1462
    int flags;
 
1463
    int index;
 
1464
    uint32_t arch_mask;
 
1465
} QEMUOption;
 
1466
 
 
1467
static const QEMUOption qemu_options[] = {
 
1468
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
 
1469
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
 
1470
    { option, opt_arg, opt_enum, arch_mask },
 
1471
#define DEFHEADING(text)
 
1472
#include "qemu-options.def"
 
1473
#undef DEF
 
1474
#undef DEFHEADING
 
1475
#undef GEN_DOCS
 
1476
    { NULL },
 
1477
};
 
1478
static void select_vgahw (const char *p)
 
1479
{
 
1480
    const char *opts;
 
1481
 
 
1482
    default_vga = 0;
 
1483
    vga_interface_type = VGA_NONE;
 
1484
    if (strstart(p, "std", &opts)) {
 
1485
        vga_interface_type = VGA_STD;
 
1486
    } else if (strstart(p, "cirrus", &opts)) {
 
1487
        vga_interface_type = VGA_CIRRUS;
 
1488
    } else if (strstart(p, "vmware", &opts)) {
 
1489
        vga_interface_type = VGA_VMWARE;
 
1490
    } else if (strstart(p, "xenfb", &opts)) {
 
1491
        vga_interface_type = VGA_XENFB;
 
1492
    } else if (strstart(p, "qxl", &opts)) {
 
1493
        vga_interface_type = VGA_QXL;
 
1494
    } else if (!strstart(p, "none", &opts)) {
 
1495
    invalid_vga:
 
1496
        fprintf(stderr, "Unknown vga type: %s\n", p);
 
1497
        exit(1);
 
1498
    }
 
1499
    while (*opts) {
 
1500
        const char *nextopt;
 
1501
 
 
1502
        if (strstart(opts, ",retrace=", &nextopt)) {
 
1503
            opts = nextopt;
 
1504
            if (strstart(opts, "dumb", &nextopt))
 
1505
                vga_retrace_method = VGA_RETRACE_DUMB;
 
1506
            else if (strstart(opts, "precise", &nextopt))
 
1507
                vga_retrace_method = VGA_RETRACE_PRECISE;
 
1508
            else goto invalid_vga;
 
1509
        } else goto invalid_vga;
 
1510
        opts = nextopt;
 
1511
    }
 
1512
}
 
1513
 
 
1514
static DisplayType select_display(const char *p)
 
1515
{
 
1516
    const char *opts;
 
1517
    DisplayType display = DT_DEFAULT;
 
1518
 
 
1519
    if (strstart(p, "sdl", &opts)) {
 
1520
#ifdef CONFIG_SDL
 
1521
        display = DT_SDL;
 
1522
        while (*opts) {
 
1523
            const char *nextopt;
 
1524
 
 
1525
            if (strstart(opts, ",frame=", &nextopt)) {
 
1526
                opts = nextopt;
 
1527
                if (strstart(opts, "on", &nextopt)) {
 
1528
                    no_frame = 0;
 
1529
                } else if (strstart(opts, "off", &nextopt)) {
 
1530
                    no_frame = 1;
 
1531
                } else {
 
1532
                    goto invalid_sdl_args;
 
1533
                }
 
1534
            } else if (strstart(opts, ",alt_grab=", &nextopt)) {
 
1535
                opts = nextopt;
 
1536
                if (strstart(opts, "on", &nextopt)) {
 
1537
                    alt_grab = 1;
 
1538
                } else if (strstart(opts, "off", &nextopt)) {
 
1539
                    alt_grab = 0;
 
1540
                } else {
 
1541
                    goto invalid_sdl_args;
 
1542
                }
 
1543
            } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
 
1544
                opts = nextopt;
 
1545
                if (strstart(opts, "on", &nextopt)) {
 
1546
                    ctrl_grab = 1;
 
1547
                } else if (strstart(opts, "off", &nextopt)) {
 
1548
                    ctrl_grab = 0;
 
1549
                } else {
 
1550
                    goto invalid_sdl_args;
 
1551
                }
 
1552
            } else if (strstart(opts, ",window_close=", &nextopt)) {
 
1553
                opts = nextopt;
 
1554
                if (strstart(opts, "on", &nextopt)) {
 
1555
                    no_quit = 0;
 
1556
                } else if (strstart(opts, "off", &nextopt)) {
 
1557
                    no_quit = 1;
 
1558
                } else {
 
1559
                    goto invalid_sdl_args;
 
1560
                }
 
1561
            } else {
 
1562
            invalid_sdl_args:
 
1563
                fprintf(stderr, "Invalid SDL option string: %s\n", p);
 
1564
                exit(1);
 
1565
            }
 
1566
            opts = nextopt;
 
1567
        }
 
1568
#else
 
1569
        fprintf(stderr, "SDL support is disabled\n");
 
1570
        exit(1);
 
1571
#endif
 
1572
    } else if (strstart(p, "vnc", &opts)) {
 
1573
#ifdef CONFIG_VNC
 
1574
        display_remote++;
 
1575
 
 
1576
        if (*opts) {
 
1577
            const char *nextopt;
 
1578
 
 
1579
            if (strstart(opts, "=", &nextopt)) {
 
1580
                vnc_display = nextopt;
 
1581
            }
 
1582
        }
 
1583
        if (!vnc_display) {
 
1584
            fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
 
1585
            exit(1);
 
1586
        }
 
1587
#else
 
1588
        fprintf(stderr, "VNC support is disabled\n");
 
1589
        exit(1);
 
1590
#endif
 
1591
    } else if (strstart(p, "curses", &opts)) {
 
1592
#ifdef CONFIG_CURSES
 
1593
        display = DT_CURSES;
 
1594
#else
 
1595
        fprintf(stderr, "Curses support is disabled\n");
 
1596
        exit(1);
 
1597
#endif
 
1598
    } else if (strstart(p, "none", &opts)) {
 
1599
        display = DT_NONE;
 
1600
    } else {
 
1601
        fprintf(stderr, "Unknown display type: %s\n", p);
 
1602
        exit(1);
 
1603
    }
 
1604
 
 
1605
    return display;
 
1606
}
 
1607
 
 
1608
static int balloon_parse(const char *arg)
 
1609
{
 
1610
    QemuOpts *opts;
 
1611
 
 
1612
    if (strcmp(arg, "none") == 0) {
 
1613
        return 0;
 
1614
    }
 
1615
 
 
1616
    if (!strncmp(arg, "virtio", 6)) {
 
1617
        if (arg[6] == ',') {
 
1618
            /* have params -> parse them */
 
1619
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
 
1620
            if (!opts)
 
1621
                return  -1;
 
1622
        } else {
 
1623
            /* create empty opts */
 
1624
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
 
1625
        }
 
1626
        qemu_opt_set(opts, "driver", "virtio-balloon");
 
1627
        return 0;
 
1628
    }
 
1629
 
 
1630
    return -1;
 
1631
}
 
1632
 
 
1633
char *qemu_find_file(int type, const char *name)
 
1634
{
 
1635
    int len;
 
1636
    const char *subdir;
 
1637
    char *buf;
 
1638
 
 
1639
    /* If name contains path separators then try it as a straight path.  */
 
1640
    if ((strchr(name, '/') || strchr(name, '\\'))
 
1641
        && access(name, R_OK) == 0) {
 
1642
        return qemu_strdup(name);
 
1643
    }
 
1644
    switch (type) {
 
1645
    case QEMU_FILE_TYPE_BIOS:
 
1646
        subdir = "";
 
1647
        break;
 
1648
    case QEMU_FILE_TYPE_KEYMAP:
 
1649
        subdir = "keymaps/";
 
1650
        break;
 
1651
    default:
 
1652
        abort();
 
1653
    }
 
1654
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
 
1655
    buf = qemu_mallocz(len);
 
1656
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
 
1657
    if (access(buf, R_OK)) {
 
1658
        qemu_free(buf);
 
1659
        return NULL;
 
1660
    }
 
1661
    return buf;
 
1662
}
 
1663
 
 
1664
static int device_help_func(QemuOpts *opts, void *opaque)
 
1665
{
 
1666
    return qdev_device_help(opts);
 
1667
}
 
1668
 
 
1669
static int device_init_func(QemuOpts *opts, void *opaque)
 
1670
{
 
1671
    DeviceState *dev;
 
1672
 
 
1673
    dev = qdev_device_add(opts);
 
1674
    if (!dev)
 
1675
        return -1;
 
1676
    return 0;
 
1677
}
 
1678
 
 
1679
static int chardev_init_func(QemuOpts *opts, void *opaque)
 
1680
{
 
1681
    CharDriverState *chr;
 
1682
 
 
1683
    chr = qemu_chr_open_opts(opts, NULL);
 
1684
    if (!chr)
 
1685
        return -1;
 
1686
    return 0;
 
1687
}
 
1688
 
 
1689
#ifdef CONFIG_VIRTFS
 
1690
static int fsdev_init_func(QemuOpts *opts, void *opaque)
 
1691
{
 
1692
    int ret;
 
1693
    ret = qemu_fsdev_add(opts);
 
1694
 
 
1695
    return ret;
 
1696
}
 
1697
#endif
 
1698
 
 
1699
static int mon_init_func(QemuOpts *opts, void *opaque)
 
1700
{
 
1701
    CharDriverState *chr;
 
1702
    const char *chardev;
 
1703
    const char *mode;
 
1704
    int flags;
 
1705
 
 
1706
    mode = qemu_opt_get(opts, "mode");
 
1707
    if (mode == NULL) {
 
1708
        mode = "readline";
 
1709
    }
 
1710
    if (strcmp(mode, "readline") == 0) {
 
1711
        flags = MONITOR_USE_READLINE;
 
1712
    } else if (strcmp(mode, "control") == 0) {
 
1713
        flags = MONITOR_USE_CONTROL;
 
1714
    } else {
 
1715
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
 
1716
        exit(1);
 
1717
    }
 
1718
 
 
1719
    if (qemu_opt_get_bool(opts, "pretty", 0))
 
1720
        flags |= MONITOR_USE_PRETTY;
 
1721
 
 
1722
    if (qemu_opt_get_bool(opts, "default", 0))
 
1723
        flags |= MONITOR_IS_DEFAULT;
 
1724
 
 
1725
    chardev = qemu_opt_get(opts, "chardev");
 
1726
    chr = qemu_chr_find(chardev);
 
1727
    if (chr == NULL) {
 
1728
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
 
1729
        exit(1);
 
1730
    }
 
1731
 
 
1732
    monitor_init(chr, flags);
 
1733
    return 0;
 
1734
}
 
1735
 
 
1736
static void monitor_parse(const char *optarg, const char *mode)
 
1737
{
 
1738
    static int monitor_device_index = 0;
 
1739
    QemuOpts *opts;
 
1740
    const char *p;
 
1741
    char label[32];
 
1742
    int def = 0;
 
1743
 
 
1744
    if (strstart(optarg, "chardev:", &p)) {
 
1745
        snprintf(label, sizeof(label), "%s", p);
 
1746
    } else {
 
1747
        snprintf(label, sizeof(label), "compat_monitor%d",
 
1748
                 monitor_device_index);
 
1749
        if (monitor_device_index == 0) {
 
1750
            def = 1;
 
1751
        }
 
1752
        opts = qemu_chr_parse_compat(label, optarg);
 
1753
        if (!opts) {
 
1754
            fprintf(stderr, "parse error: %s\n", optarg);
 
1755
            exit(1);
 
1756
        }
 
1757
    }
 
1758
 
 
1759
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
 
1760
    if (!opts) {
 
1761
        fprintf(stderr, "duplicate chardev: %s\n", label);
 
1762
        exit(1);
 
1763
    }
 
1764
    qemu_opt_set(opts, "mode", mode);
 
1765
    qemu_opt_set(opts, "chardev", label);
 
1766
    if (def)
 
1767
        qemu_opt_set(opts, "default", "on");
 
1768
    monitor_device_index++;
 
1769
}
 
1770
 
 
1771
struct device_config {
 
1772
    enum {
 
1773
        DEV_USB,       /* -usbdevice     */
 
1774
        DEV_BT,        /* -bt            */
 
1775
        DEV_SERIAL,    /* -serial        */
 
1776
        DEV_PARALLEL,  /* -parallel      */
 
1777
        DEV_VIRTCON,   /* -virtioconsole */
 
1778
        DEV_DEBUGCON,  /* -debugcon */
 
1779
    } type;
 
1780
    const char *cmdline;
 
1781
    QTAILQ_ENTRY(device_config) next;
 
1782
};
 
1783
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
 
1784
 
 
1785
static void add_device_config(int type, const char *cmdline)
 
1786
{
 
1787
    struct device_config *conf;
 
1788
 
 
1789
    conf = qemu_mallocz(sizeof(*conf));
 
1790
    conf->type = type;
 
1791
    conf->cmdline = cmdline;
 
1792
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
 
1793
}
 
1794
 
 
1795
static int foreach_device_config(int type, int (*func)(const char *cmdline))
 
1796
{
 
1797
    struct device_config *conf;
 
1798
    int rc;
 
1799
 
 
1800
    QTAILQ_FOREACH(conf, &device_configs, next) {
 
1801
        if (conf->type != type)
 
1802
            continue;
 
1803
        rc = func(conf->cmdline);
 
1804
        if (0 != rc)
 
1805
            return rc;
 
1806
    }
 
1807
    return 0;
 
1808
}
 
1809
 
 
1810
static int serial_parse(const char *devname)
 
1811
{
 
1812
    static int index = 0;
 
1813
    char label[32];
 
1814
 
 
1815
    if (strcmp(devname, "none") == 0)
 
1816
        return 0;
 
1817
    if (index == MAX_SERIAL_PORTS) {
 
1818
        fprintf(stderr, "qemu: too many serial ports\n");
 
1819
        exit(1);
 
1820
    }
 
1821
    snprintf(label, sizeof(label), "serial%d", index);
 
1822
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
 
1823
    if (!serial_hds[index]) {
 
1824
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
 
1825
                devname, strerror(errno));
 
1826
        return -1;
 
1827
    }
 
1828
    index++;
 
1829
    return 0;
 
1830
}
 
1831
 
 
1832
static int parallel_parse(const char *devname)
 
1833
{
 
1834
    static int index = 0;
 
1835
    char label[32];
 
1836
 
 
1837
    if (strcmp(devname, "none") == 0)
 
1838
        return 0;
 
1839
    if (index == MAX_PARALLEL_PORTS) {
 
1840
        fprintf(stderr, "qemu: too many parallel ports\n");
 
1841
        exit(1);
 
1842
    }
 
1843
    snprintf(label, sizeof(label), "parallel%d", index);
 
1844
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
 
1845
    if (!parallel_hds[index]) {
 
1846
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
 
1847
                devname, strerror(errno));
 
1848
        return -1;
 
1849
    }
 
1850
    index++;
 
1851
    return 0;
 
1852
}
 
1853
 
 
1854
static int virtcon_parse(const char *devname)
 
1855
{
 
1856
    QemuOptsList *device = qemu_find_opts("device");
 
1857
    static int index = 0;
 
1858
    char label[32];
 
1859
    QemuOpts *bus_opts, *dev_opts;
 
1860
 
 
1861
    if (strcmp(devname, "none") == 0)
 
1862
        return 0;
 
1863
    if (index == MAX_VIRTIO_CONSOLES) {
 
1864
        fprintf(stderr, "qemu: too many virtio consoles\n");
 
1865
        exit(1);
 
1866
    }
 
1867
 
 
1868
    bus_opts = qemu_opts_create(device, NULL, 0);
 
1869
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
 
1870
 
 
1871
    dev_opts = qemu_opts_create(device, NULL, 0);
 
1872
    qemu_opt_set(dev_opts, "driver", "virtconsole");
 
1873
 
 
1874
    snprintf(label, sizeof(label), "virtcon%d", index);
 
1875
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
 
1876
    if (!virtcon_hds[index]) {
 
1877
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
 
1878
                devname, strerror(errno));
 
1879
        return -1;
 
1880
    }
 
1881
    qemu_opt_set(dev_opts, "chardev", label);
 
1882
 
 
1883
    index++;
 
1884
    return 0;
 
1885
}
 
1886
 
 
1887
static int debugcon_parse(const char *devname)
 
1888
{   
 
1889
    QemuOpts *opts;
 
1890
 
 
1891
    if (!qemu_chr_open("debugcon", devname, NULL)) {
 
1892
        exit(1);
 
1893
    }
 
1894
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
 
1895
    if (!opts) {
 
1896
        fprintf(stderr, "qemu: already have a debugcon device\n");
 
1897
        exit(1);
 
1898
    }
 
1899
    qemu_opt_set(opts, "driver", "isa-debugcon");
 
1900
    qemu_opt_set(opts, "chardev", "debugcon");
 
1901
    return 0;
 
1902
}
 
1903
 
 
1904
static QEMUMachine *machine_parse(const char *name)
 
1905
{
 
1906
    QEMUMachine *m, *machine = NULL;
 
1907
 
 
1908
    if (name) {
 
1909
        machine = find_machine(name);
 
1910
    }
 
1911
    if (machine) {
 
1912
        return machine;
 
1913
    }
 
1914
    printf("Supported machines are:\n");
 
1915
    for (m = first_machine; m != NULL; m = m->next) {
 
1916
        if (m->alias) {
 
1917
            printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
 
1918
        }
 
1919
        printf("%-10s %s%s\n", m->name, m->desc,
 
1920
               m->is_default ? " (default)" : "");
 
1921
    }
 
1922
    exit(!name || *name != '?');
 
1923
}
 
1924
 
 
1925
static int tcg_init(void)
 
1926
{
 
1927
    return 0;
 
1928
}
 
1929
 
 
1930
static struct {
 
1931
    const char *opt_name;
 
1932
    const char *name;
 
1933
    int (*available)(void);
 
1934
    int (*init)(void);
 
1935
    int *allowed;
 
1936
} accel_list[] = {
 
1937
    { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
 
1938
    { "xen", "Xen", xen_available, xen_init, &xen_allowed },
 
1939
    { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
 
1940
};
 
1941
 
 
1942
static int configure_accelerator(void)
 
1943
{
 
1944
    const char *p = NULL;
 
1945
    char buf[10];
 
1946
    int i, ret;
 
1947
    bool accel_initalised = 0;
 
1948
    bool init_failed = 0;
 
1949
 
 
1950
    QemuOptsList *list = qemu_find_opts("machine");
 
1951
    if (!QTAILQ_EMPTY(&list->head)) {
 
1952
        p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
 
1953
    }
 
1954
 
 
1955
    if (p == NULL) {
 
1956
#ifdef CONFIG_KVM
 
1957
        /* Use the default "accelerator", kvm */
 
1958
        p = "kvm";
 
1959
#else
 
1960
        /* Use the default "accelerator", tcg */
 
1961
        p = "tcg";
 
1962
#endif
 
1963
    }
 
1964
 
 
1965
    while (!accel_initalised && *p != '\0') {
 
1966
        if (*p == ':') {
 
1967
            p++;
 
1968
        }
 
1969
        p = get_opt_name(buf, sizeof (buf), p, ':');
 
1970
        for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
 
1971
            if (strcmp(accel_list[i].opt_name, buf) == 0) {
 
1972
                *(accel_list[i].allowed) = 1;
 
1973
                ret = accel_list[i].init();
 
1974
                if (ret < 0) {
 
1975
                    init_failed = 1;
 
1976
                    if (!accel_list[i].available()) {
 
1977
                        printf("%s not supported for this target\n",
 
1978
                               accel_list[i].name);
 
1979
                    } else {
 
1980
                        fprintf(stderr, "failed to initialize %s: %s\n",
 
1981
                                accel_list[i].name,
 
1982
                                strerror(-ret));
 
1983
                    }
 
1984
                    *(accel_list[i].allowed) = 0;
 
1985
                } else {
 
1986
                    accel_initalised = 1;
 
1987
                }
 
1988
                break;
 
1989
            }
 
1990
        }
 
1991
        if (i == ARRAY_SIZE(accel_list)) {
 
1992
            fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
 
1993
        }
 
1994
    }
 
1995
 
 
1996
    if (!accel_initalised) {
 
1997
        fprintf(stderr, "No accelerator found!\n");
 
1998
        exit(1);
 
1999
    }
 
2000
 
 
2001
    if (init_failed) {
 
2002
        fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
 
2003
    }
 
2004
 
 
2005
    return !accel_initalised;
 
2006
}
 
2007
 
 
2008
void qemu_add_exit_notifier(Notifier *notify)
 
2009
{
 
2010
    notifier_list_add(&exit_notifiers, notify);
 
2011
}
 
2012
 
 
2013
void qemu_remove_exit_notifier(Notifier *notify)
 
2014
{
 
2015
    notifier_list_remove(&exit_notifiers, notify);
 
2016
}
 
2017
 
 
2018
static void qemu_run_exit_notifiers(void)
 
2019
{
 
2020
    notifier_list_notify(&exit_notifiers, NULL);
 
2021
}
 
2022
 
 
2023
void qemu_add_machine_init_done_notifier(Notifier *notify)
 
2024
{
 
2025
    notifier_list_add(&machine_init_done_notifiers, notify);
 
2026
}
 
2027
 
 
2028
static void qemu_run_machine_init_done_notifiers(void)
 
2029
{
 
2030
    notifier_list_notify(&machine_init_done_notifiers, NULL);
 
2031
}
 
2032
 
 
2033
static const QEMUOption *lookup_opt(int argc, char **argv,
 
2034
                                    const char **poptarg, int *poptind)
 
2035
{
 
2036
    const QEMUOption *popt;
 
2037
    int optind = *poptind;
 
2038
    char *r = argv[optind];
 
2039
    const char *optarg;
 
2040
 
 
2041
    loc_set_cmdline(argv, optind, 1);
 
2042
    optind++;
 
2043
    /* Treat --foo the same as -foo.  */
 
2044
    if (r[1] == '-')
 
2045
        r++;
 
2046
    popt = qemu_options;
 
2047
    for(;;) {
 
2048
        if (!popt->name) {
 
2049
            error_report("invalid option");
 
2050
            exit(1);
 
2051
        }
 
2052
        if (!strcmp(popt->name, r + 1))
 
2053
            break;
 
2054
        popt++;
 
2055
    }
 
2056
    if (popt->flags & HAS_ARG) {
 
2057
        if (optind >= argc) {
 
2058
            error_report("requires an argument");
 
2059
            exit(1);
 
2060
        }
 
2061
        optarg = argv[optind++];
 
2062
        loc_set_cmdline(argv, optind - 2, 2);
 
2063
    } else {
 
2064
        optarg = NULL;
 
2065
    }
 
2066
 
 
2067
    *poptarg = optarg;
 
2068
    *poptind = optind;
 
2069
 
 
2070
    return popt;
 
2071
}
 
2072
 
 
2073
int main(int argc, char **argv, char **envp)
 
2074
{
 
2075
    const char *gdbstub_dev = NULL;
 
2076
    int i;
 
2077
    int snapshot, linux_boot;
 
2078
    const char *icount_option = NULL;
 
2079
    const char *initrd_filename;
 
2080
    const char *kernel_filename, *kernel_cmdline;
 
2081
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
 
2082
    DisplayState *ds;
 
2083
    DisplayChangeListener *dcl;
 
2084
    int cyls, heads, secs, translation;
 
2085
    QemuOpts *hda_opts = NULL, *opts;
 
2086
    QemuOptsList *olist;
 
2087
    int optind;
 
2088
    const char *optarg;
 
2089
    const char *loadvm = NULL;
 
2090
    QEMUMachine *machine;
 
2091
    const char *cpu_model;
 
2092
    int tb_size;
 
2093
    const char *pid_file = NULL;
 
2094
    const char *incoming = NULL;
 
2095
#ifdef CONFIG_VNC
 
2096
    int show_vnc_port = 0;
 
2097
#endif
 
2098
    int defconfig = 1;
 
2099
    const char *trace_file = NULL;
 
2100
    const char *log_mask = NULL;
 
2101
    const char *log_file = NULL;
 
2102
 
 
2103
    atexit(qemu_run_exit_notifiers);
 
2104
    error_set_progname(argv[0]);
 
2105
 
 
2106
    init_clocks();
 
2107
 
 
2108
    qemu_cache_utils_init(envp);
 
2109
 
 
2110
    QLIST_INIT (&vm_change_state_head);
 
2111
    os_setup_early_signal_handling();
 
2112
 
 
2113
    module_call_init(MODULE_INIT_MACHINE);
 
2114
    machine = find_default_machine();
 
2115
    cpu_model = NULL;
 
2116
    initrd_filename = NULL;
 
2117
    ram_size = 0;
 
2118
    snapshot = 0;
 
2119
    kernel_filename = NULL;
 
2120
    kernel_cmdline = "";
 
2121
    cyls = heads = secs = 0;
 
2122
    translation = BIOS_ATA_TRANSLATION_AUTO;
 
2123
 
 
2124
    for (i = 0; i < MAX_NODES; i++) {
 
2125
        node_mem[i] = 0;
 
2126
        node_cpumask[i] = 0;
 
2127
    }
 
2128
 
 
2129
    nb_numa_nodes = 0;
 
2130
    nb_nics = 0;
 
2131
 
 
2132
    tb_size = 0;
 
2133
    autostart= 1;
 
2134
 
 
2135
    /* first pass of option parsing */
 
2136
    optind = 1;
 
2137
    while (optind < argc) {
 
2138
        if (argv[optind][0] != '-') {
 
2139
            /* disk image */
 
2140
            optind++;
 
2141
            continue;
 
2142
        } else {
 
2143
            const QEMUOption *popt;
 
2144
 
 
2145
            popt = lookup_opt(argc, argv, &optarg, &optind);
 
2146
            switch (popt->index) {
 
2147
            case QEMU_OPTION_nodefconfig:
 
2148
                defconfig=0;
 
2149
                break;
 
2150
            }
 
2151
        }
 
2152
    }
 
2153
 
 
2154
    if (defconfig) {
 
2155
        int ret;
 
2156
 
 
2157
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
 
2158
        if (ret < 0 && ret != -ENOENT) {
 
2159
            exit(1);
 
2160
        }
 
2161
 
 
2162
        ret = qemu_read_config_file(arch_config_name);
 
2163
        if (ret < 0 && ret != -ENOENT) {
 
2164
            exit(1);
 
2165
        }
 
2166
    }
 
2167
    cpudef_init();
 
2168
 
 
2169
    /* second pass of option parsing */
 
2170
    optind = 1;
 
2171
    for(;;) {
 
2172
        if (optind >= argc)
 
2173
            break;
 
2174
        if (argv[optind][0] != '-') {
 
2175
            hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
 
2176
        } else {
 
2177
            const QEMUOption *popt;
 
2178
 
 
2179
            popt = lookup_opt(argc, argv, &optarg, &optind);
 
2180
            if (!(popt->arch_mask & arch_type)) {
 
2181
                printf("Option %s not supported for this target\n", popt->name);
 
2182
                exit(1);
 
2183
            }
 
2184
            switch(popt->index) {
 
2185
            case QEMU_OPTION_M:
 
2186
                machine = machine_parse(optarg);
 
2187
                break;
 
2188
            case QEMU_OPTION_cpu:
 
2189
                /* hw initialization will check this */
 
2190
                if (*optarg == '?') {
 
2191
                    list_cpus(stdout, &fprintf, optarg);
 
2192
                    exit(0);
 
2193
                } else {
 
2194
                    cpu_model = optarg;
 
2195
                }
 
2196
                break;
 
2197
            case QEMU_OPTION_initrd:
 
2198
                initrd_filename = optarg;
 
2199
                break;
 
2200
            case QEMU_OPTION_hda:
 
2201
                {
 
2202
                    char buf[256];
 
2203
                    if (cyls == 0)
 
2204
                        snprintf(buf, sizeof(buf), "%s", HD_OPTS);
 
2205
                    else
 
2206
                        snprintf(buf, sizeof(buf),
 
2207
                                 "%s,cyls=%d,heads=%d,secs=%d%s",
 
2208
                                 HD_OPTS , cyls, heads, secs,
 
2209
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
 
2210
                                 ",trans=lba" :
 
2211
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
 
2212
                                 ",trans=none" : "");
 
2213
                    drive_add(IF_DEFAULT, 0, optarg, buf);
 
2214
                    break;
 
2215
                }
 
2216
            case QEMU_OPTION_hdb:
 
2217
            case QEMU_OPTION_hdc:
 
2218
            case QEMU_OPTION_hdd:
 
2219
                drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
 
2220
                          HD_OPTS);
 
2221
                break;
 
2222
            case QEMU_OPTION_drive:
 
2223
                if (drive_def(optarg) == NULL) {
 
2224
                    exit(1);
 
2225
                }
 
2226
                break;
 
2227
            case QEMU_OPTION_set:
 
2228
                if (qemu_set_option(optarg) != 0)
 
2229
                    exit(1);
 
2230
                break;
 
2231
            case QEMU_OPTION_global:
 
2232
                if (qemu_global_option(optarg) != 0)
 
2233
                    exit(1);
 
2234
                break;
 
2235
            case QEMU_OPTION_mtdblock:
 
2236
                drive_add(IF_MTD, -1, optarg, MTD_OPTS);
 
2237
                break;
 
2238
            case QEMU_OPTION_sd:
 
2239
                drive_add(IF_SD, 0, optarg, SD_OPTS);
 
2240
                break;
 
2241
            case QEMU_OPTION_pflash:
 
2242
                drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
 
2243
                break;
 
2244
            case QEMU_OPTION_snapshot:
 
2245
                snapshot = 1;
 
2246
                break;
 
2247
            case QEMU_OPTION_hdachs:
 
2248
                {
 
2249
                    const char *p;
 
2250
                    p = optarg;
 
2251
                    cyls = strtol(p, (char **)&p, 0);
 
2252
                    if (cyls < 1 || cyls > 16383)
 
2253
                        goto chs_fail;
 
2254
                    if (*p != ',')
 
2255
                        goto chs_fail;
 
2256
                    p++;
 
2257
                    heads = strtol(p, (char **)&p, 0);
 
2258
                    if (heads < 1 || heads > 16)
 
2259
                        goto chs_fail;
 
2260
                    if (*p != ',')
 
2261
                        goto chs_fail;
 
2262
                    p++;
 
2263
                    secs = strtol(p, (char **)&p, 0);
 
2264
                    if (secs < 1 || secs > 63)
 
2265
                        goto chs_fail;
 
2266
                    if (*p == ',') {
 
2267
                        p++;
 
2268
                        if (!strcmp(p, "none"))
 
2269
                            translation = BIOS_ATA_TRANSLATION_NONE;
 
2270
                        else if (!strcmp(p, "lba"))
 
2271
                            translation = BIOS_ATA_TRANSLATION_LBA;
 
2272
                        else if (!strcmp(p, "auto"))
 
2273
                            translation = BIOS_ATA_TRANSLATION_AUTO;
 
2274
                        else
 
2275
                            goto chs_fail;
 
2276
                    } else if (*p != '\0') {
 
2277
                    chs_fail:
 
2278
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
 
2279
                        exit(1);
 
2280
                    }
 
2281
                    if (hda_opts != NULL) {
 
2282
                        char num[16];
 
2283
                        snprintf(num, sizeof(num), "%d", cyls);
 
2284
                        qemu_opt_set(hda_opts, "cyls", num);
 
2285
                        snprintf(num, sizeof(num), "%d", heads);
 
2286
                        qemu_opt_set(hda_opts, "heads", num);
 
2287
                        snprintf(num, sizeof(num), "%d", secs);
 
2288
                        qemu_opt_set(hda_opts, "secs", num);
 
2289
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
 
2290
                            qemu_opt_set(hda_opts, "trans", "lba");
 
2291
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
 
2292
                            qemu_opt_set(hda_opts, "trans", "none");
 
2293
                    }
 
2294
                }
 
2295
                break;
 
2296
            case QEMU_OPTION_numa:
 
2297
                if (nb_numa_nodes >= MAX_NODES) {
 
2298
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
 
2299
                    exit(1);
 
2300
                }
 
2301
                numa_add(optarg);
 
2302
                break;
 
2303
            case QEMU_OPTION_display:
 
2304
                display_type = select_display(optarg);
 
2305
                break;
 
2306
            case QEMU_OPTION_nographic:
 
2307
                display_type = DT_NOGRAPHIC;
 
2308
                break;
 
2309
            case QEMU_OPTION_curses:
 
2310
#ifdef CONFIG_CURSES
 
2311
                display_type = DT_CURSES;
 
2312
#else
 
2313
                fprintf(stderr, "Curses support is disabled\n");
 
2314
                exit(1);
 
2315
#endif
 
2316
                break;
 
2317
            case QEMU_OPTION_portrait:
 
2318
                graphic_rotate = 90;
 
2319
                break;
 
2320
            case QEMU_OPTION_rotate:
 
2321
                graphic_rotate = strtol(optarg, (char **) &optarg, 10);
 
2322
                if (graphic_rotate != 0 && graphic_rotate != 90 &&
 
2323
                    graphic_rotate != 180 && graphic_rotate != 270) {
 
2324
                    fprintf(stderr,
 
2325
                        "qemu: only 90, 180, 270 deg rotation is available\n");
 
2326
                    exit(1);
 
2327
                }
 
2328
                break;
 
2329
            case QEMU_OPTION_kernel:
 
2330
                kernel_filename = optarg;
 
2331
                break;
 
2332
            case QEMU_OPTION_append:
 
2333
                kernel_cmdline = optarg;
 
2334
                break;
 
2335
            case QEMU_OPTION_cdrom:
 
2336
                drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
 
2337
                break;
 
2338
            case QEMU_OPTION_boot:
 
2339
                {
 
2340
                    static const char * const params[] = {
 
2341
                        "order", "once", "menu", NULL
 
2342
                    };
 
2343
                    char buf[sizeof(boot_devices)];
 
2344
                    char *standard_boot_devices;
 
2345
                    int legacy = 0;
 
2346
 
 
2347
                    if (!strchr(optarg, '=')) {
 
2348
                        legacy = 1;
 
2349
                        pstrcpy(buf, sizeof(buf), optarg);
 
2350
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
 
2351
                        fprintf(stderr,
 
2352
                                "qemu: unknown boot parameter '%s' in '%s'\n",
 
2353
                                buf, optarg);
 
2354
                        exit(1);
 
2355
                    }
 
2356
 
 
2357
                    if (legacy ||
 
2358
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
 
2359
                        validate_bootdevices(buf);
 
2360
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
 
2361
                    }
 
2362
                    if (!legacy) {
 
2363
                        if (get_param_value(buf, sizeof(buf),
 
2364
                                            "once", optarg)) {
 
2365
                            validate_bootdevices(buf);
 
2366
                            standard_boot_devices = qemu_strdup(boot_devices);
 
2367
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
 
2368
                            qemu_register_reset(restore_boot_devices,
 
2369
                                                standard_boot_devices);
 
2370
                        }
 
2371
                        if (get_param_value(buf, sizeof(buf),
 
2372
                                            "menu", optarg)) {
 
2373
                            if (!strcmp(buf, "on")) {
 
2374
                                boot_menu = 1;
 
2375
                            } else if (!strcmp(buf, "off")) {
 
2376
                                boot_menu = 0;
 
2377
                            } else {
 
2378
                                fprintf(stderr,
 
2379
                                        "qemu: invalid option value '%s'\n",
 
2380
                                        buf);
 
2381
                                exit(1);
 
2382
                            }
 
2383
                        }
 
2384
                    }
 
2385
                }
 
2386
                break;
 
2387
            case QEMU_OPTION_fda:
 
2388
            case QEMU_OPTION_fdb:
 
2389
                drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
 
2390
                          optarg, FD_OPTS);
 
2391
                break;
 
2392
            case QEMU_OPTION_no_fd_bootchk:
 
2393
                fd_bootchk = 0;
 
2394
                break;
 
2395
            case QEMU_OPTION_netdev:
 
2396
                if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
 
2397
                    exit(1);
 
2398
                }
 
2399
                break;
 
2400
            case QEMU_OPTION_net:
 
2401
                if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
 
2402
                    exit(1);
 
2403
                }
 
2404
                break;
 
2405
#ifdef CONFIG_SLIRP
 
2406
            case QEMU_OPTION_tftp:
 
2407
                legacy_tftp_prefix = optarg;
 
2408
                break;
 
2409
            case QEMU_OPTION_bootp:
 
2410
                legacy_bootp_filename = optarg;
 
2411
                break;
 
2412
            case QEMU_OPTION_redir:
 
2413
                if (net_slirp_redir(optarg) < 0)
 
2414
                    exit(1);
 
2415
                break;
 
2416
#endif
 
2417
            case QEMU_OPTION_bt:
 
2418
                add_device_config(DEV_BT, optarg);
 
2419
                break;
 
2420
            case QEMU_OPTION_audio_help:
 
2421
                if (!(audio_available())) {
 
2422
                    printf("Option %s not supported for this target\n", popt->name);
 
2423
                    exit(1);
 
2424
                }
 
2425
                AUD_help ();
 
2426
                exit (0);
 
2427
                break;
 
2428
            case QEMU_OPTION_soundhw:
 
2429
                if (!(audio_available())) {
 
2430
                    printf("Option %s not supported for this target\n", popt->name);
 
2431
                    exit(1);
 
2432
                }
 
2433
                select_soundhw (optarg);
 
2434
                break;
 
2435
            case QEMU_OPTION_h:
 
2436
                help(0);
 
2437
                break;
 
2438
            case QEMU_OPTION_version:
 
2439
                version();
 
2440
                exit(0);
 
2441
                break;
 
2442
            case QEMU_OPTION_m: {
 
2443
                int64_t value;
 
2444
 
 
2445
                value = strtosz(optarg, NULL);
 
2446
                if (value < 0) {
 
2447
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
 
2448
                    exit(1);
 
2449
                }
 
2450
 
 
2451
                /* On 32-bit hosts, QEMU is limited by virtual address space */
 
2452
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
 
2453
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
 
2454
                    exit(1);
 
2455
                }
 
2456
                if (value != (uint64_t)(ram_addr_t)value) {
 
2457
                    fprintf(stderr, "qemu: ram size too large\n");
 
2458
                    exit(1);
 
2459
                }
 
2460
                ram_size = value;
 
2461
                break;
 
2462
            }
 
2463
            case QEMU_OPTION_mempath:
 
2464
                mem_path = optarg;
 
2465
                break;
 
2466
#ifdef MAP_POPULATE
 
2467
            case QEMU_OPTION_mem_prealloc:
 
2468
                mem_prealloc = 1;
 
2469
                break;
 
2470
#endif
 
2471
            case QEMU_OPTION_d:
 
2472
                log_mask = optarg;
 
2473
                break;
 
2474
            case QEMU_OPTION_D:
 
2475
                log_file = optarg;
 
2476
                break;
 
2477
            case QEMU_OPTION_s:
 
2478
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
 
2479
                break;
 
2480
            case QEMU_OPTION_gdb:
 
2481
                gdbstub_dev = optarg;
 
2482
                break;
 
2483
            case QEMU_OPTION_L:
 
2484
                data_dir = optarg;
 
2485
                break;
 
2486
            case QEMU_OPTION_bios:
 
2487
                bios_name = optarg;
 
2488
                break;
 
2489
            case QEMU_OPTION_singlestep:
 
2490
                singlestep = 1;
 
2491
                break;
 
2492
            case QEMU_OPTION_S:
 
2493
                autostart = 0;
 
2494
                break;
 
2495
            case QEMU_OPTION_k:
 
2496
                keyboard_layout = optarg;
 
2497
                break;
 
2498
            case QEMU_OPTION_localtime:
 
2499
                rtc_utc = 0;
 
2500
                break;
 
2501
            case QEMU_OPTION_vga:
 
2502
                select_vgahw (optarg);
 
2503
                break;
 
2504
            case QEMU_OPTION_g:
 
2505
                {
 
2506
                    const char *p;
 
2507
                    int w, h, depth;
 
2508
                    p = optarg;
 
2509
                    w = strtol(p, (char **)&p, 10);
 
2510
                    if (w <= 0) {
 
2511
                    graphic_error:
 
2512
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
 
2513
                        exit(1);
 
2514
                    }
 
2515
                    if (*p != 'x')
 
2516
                        goto graphic_error;
 
2517
                    p++;
 
2518
                    h = strtol(p, (char **)&p, 10);
 
2519
                    if (h <= 0)
 
2520
                        goto graphic_error;
 
2521
                    if (*p == 'x') {
 
2522
                        p++;
 
2523
                        depth = strtol(p, (char **)&p, 10);
 
2524
                        if (depth != 8 && depth != 15 && depth != 16 &&
 
2525
                            depth != 24 && depth != 32)
 
2526
                            goto graphic_error;
 
2527
                    } else if (*p == '\0') {
 
2528
                        depth = graphic_depth;
 
2529
                    } else {
 
2530
                        goto graphic_error;
 
2531
                    }
 
2532
 
 
2533
                    graphic_width = w;
 
2534
                    graphic_height = h;
 
2535
                    graphic_depth = depth;
 
2536
                }
 
2537
                break;
 
2538
            case QEMU_OPTION_echr:
 
2539
                {
 
2540
                    char *r;
 
2541
                    term_escape_char = strtol(optarg, &r, 0);
 
2542
                    if (r == optarg)
 
2543
                        printf("Bad argument to echr\n");
 
2544
                    break;
 
2545
                }
 
2546
            case QEMU_OPTION_monitor:
 
2547
                monitor_parse(optarg, "readline");
 
2548
                default_monitor = 0;
 
2549
                break;
 
2550
            case QEMU_OPTION_qmp:
 
2551
                monitor_parse(optarg, "control");
 
2552
                default_monitor = 0;
 
2553
                break;
 
2554
            case QEMU_OPTION_mon:
 
2555
                opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
 
2556
                if (!opts) {
 
2557
                    exit(1);
 
2558
                }
 
2559
                default_monitor = 0;
 
2560
                break;
 
2561
            case QEMU_OPTION_chardev:
 
2562
                opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
 
2563
                if (!opts) {
 
2564
                    exit(1);
 
2565
                }
 
2566
                break;
 
2567
            case QEMU_OPTION_fsdev:
 
2568
                olist = qemu_find_opts("fsdev");
 
2569
                if (!olist) {
 
2570
                    fprintf(stderr, "fsdev is not supported by this qemu build.\n");
 
2571
                    exit(1);
 
2572
                }
 
2573
                opts = qemu_opts_parse(olist, optarg, 1);
 
2574
                if (!opts) {
 
2575
                    fprintf(stderr, "parse error: %s\n", optarg);
 
2576
                    exit(1);
 
2577
                }
 
2578
                break;
 
2579
            case QEMU_OPTION_virtfs: {
 
2580
                QemuOpts *fsdev;
 
2581
                QemuOpts *device;
 
2582
 
 
2583
                olist = qemu_find_opts("virtfs");
 
2584
                if (!olist) {
 
2585
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
 
2586
                    exit(1);
 
2587
                }
 
2588
                opts = qemu_opts_parse(olist, optarg, 1);
 
2589
                if (!opts) {
 
2590
                    fprintf(stderr, "parse error: %s\n", optarg);
 
2591
                    exit(1);
 
2592
                }
 
2593
 
 
2594
                if (qemu_opt_get(opts, "fstype") == NULL ||
 
2595
                        qemu_opt_get(opts, "mount_tag") == NULL ||
 
2596
                        qemu_opt_get(opts, "path") == NULL ||
 
2597
                        qemu_opt_get(opts, "security_model") == NULL) {
 
2598
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
 
2599
                            "security_model=[mapped|passthrough|none],"
 
2600
                            "mount_tag=tag.\n");
 
2601
                    exit(1);
 
2602
                }
 
2603
 
 
2604
                fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
 
2605
                                         qemu_opt_get(opts, "mount_tag"), 1);
 
2606
                if (!fsdev) {
 
2607
                    fprintf(stderr, "duplicate fsdev id: %s\n",
 
2608
                            qemu_opt_get(opts, "mount_tag"));
 
2609
                    exit(1);
 
2610
                }
 
2611
                qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
 
2612
                qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
 
2613
                qemu_opt_set(fsdev, "security_model",
 
2614
                             qemu_opt_get(opts, "security_model"));
 
2615
 
 
2616
                device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
 
2617
                qemu_opt_set(device, "driver", "virtio-9p-pci");
 
2618
                qemu_opt_set(device, "fsdev",
 
2619
                             qemu_opt_get(opts, "mount_tag"));
 
2620
                qemu_opt_set(device, "mount_tag",
 
2621
                             qemu_opt_get(opts, "mount_tag"));
 
2622
                break;
 
2623
            }
 
2624
            case QEMU_OPTION_serial:
 
2625
                add_device_config(DEV_SERIAL, optarg);
 
2626
                default_serial = 0;
 
2627
                if (strncmp(optarg, "mon:", 4) == 0) {
 
2628
                    default_monitor = 0;
 
2629
                }
 
2630
                break;
 
2631
            case QEMU_OPTION_watchdog:
 
2632
                if (watchdog) {
 
2633
                    fprintf(stderr,
 
2634
                            "qemu: only one watchdog option may be given\n");
 
2635
                    return 1;
 
2636
                }
 
2637
                watchdog = optarg;
 
2638
                break;
 
2639
            case QEMU_OPTION_watchdog_action:
 
2640
                if (select_watchdog_action(optarg) == -1) {
 
2641
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
 
2642
                    exit(1);
 
2643
                }
 
2644
                break;
 
2645
            case QEMU_OPTION_virtiocon:
 
2646
                add_device_config(DEV_VIRTCON, optarg);
 
2647
                default_virtcon = 0;
 
2648
                if (strncmp(optarg, "mon:", 4) == 0) {
 
2649
                    default_monitor = 0;
 
2650
                }
 
2651
                break;
 
2652
            case QEMU_OPTION_parallel:
 
2653
                add_device_config(DEV_PARALLEL, optarg);
 
2654
                default_parallel = 0;
 
2655
                if (strncmp(optarg, "mon:", 4) == 0) {
 
2656
                    default_monitor = 0;
 
2657
                }
 
2658
                break;
 
2659
            case QEMU_OPTION_debugcon:
 
2660
                add_device_config(DEV_DEBUGCON, optarg);
 
2661
                break;
 
2662
            case QEMU_OPTION_loadvm:
 
2663
                loadvm = optarg;
 
2664
                break;
 
2665
            case QEMU_OPTION_full_screen:
 
2666
                full_screen = 1;
 
2667
                break;
 
2668
#ifdef CONFIG_SDL
 
2669
            case QEMU_OPTION_no_frame:
 
2670
                no_frame = 1;
 
2671
                break;
 
2672
            case QEMU_OPTION_alt_grab:
 
2673
                alt_grab = 1;
 
2674
                break;
 
2675
            case QEMU_OPTION_ctrl_grab:
 
2676
                ctrl_grab = 1;
 
2677
                break;
 
2678
            case QEMU_OPTION_no_quit:
 
2679
                no_quit = 1;
 
2680
                break;
 
2681
            case QEMU_OPTION_sdl:
 
2682
                display_type = DT_SDL;
 
2683
                break;
 
2684
#else
 
2685
            case QEMU_OPTION_no_frame:
 
2686
            case QEMU_OPTION_alt_grab:
 
2687
            case QEMU_OPTION_ctrl_grab:
 
2688
            case QEMU_OPTION_no_quit:
 
2689
            case QEMU_OPTION_sdl:
 
2690
                fprintf(stderr, "SDL support is disabled\n");
 
2691
                exit(1);
 
2692
#endif
 
2693
            case QEMU_OPTION_pidfile:
 
2694
                pid_file = optarg;
 
2695
                break;
 
2696
            case QEMU_OPTION_win2k_hack:
 
2697
                win2k_install_hack = 1;
 
2698
                break;
 
2699
            case QEMU_OPTION_rtc_td_hack:
 
2700
                rtc_td_hack = 1;
 
2701
                break;
 
2702
            case QEMU_OPTION_acpitable:
 
2703
                do_acpitable_option(optarg);
 
2704
                break;
 
2705
            case QEMU_OPTION_smbios:
 
2706
                do_smbios_option(optarg);
 
2707
                break;
 
2708
            case QEMU_OPTION_enable_kvm:
 
2709
                olist = qemu_find_opts("machine");
 
2710
                qemu_opts_reset(olist);
 
2711
                qemu_opts_parse(olist, "accel=kvm", 0);
 
2712
                break;
 
2713
            case QEMU_OPTION_machine:
 
2714
                olist = qemu_find_opts("machine");
 
2715
                qemu_opts_reset(olist);
 
2716
                opts = qemu_opts_parse(olist, optarg, 1);
 
2717
                if (!opts) {
 
2718
                    fprintf(stderr, "parse error: %s\n", optarg);
 
2719
                    exit(1);
 
2720
                }
 
2721
                machine = machine_parse(qemu_opt_get(opts, "type"));
 
2722
                break;
 
2723
            case QEMU_OPTION_no_kvm:
 
2724
                olist = qemu_find_opts("machine");
 
2725
                qemu_opts_reset(olist);
 
2726
                qemu_opts_parse(olist, "accel=tcg", 0);
 
2727
                break;
 
2728
#ifdef CONFIG_KVM
 
2729
            case QEMU_OPTION_no_kvm_irqchip: {
 
2730
                kvm_irqchip = 0;
 
2731
                kvm_pit = 0;
 
2732
                break;
 
2733
            }
 
2734
            case QEMU_OPTION_no_kvm_pit: {
 
2735
                kvm_pit = 0;
 
2736
                break;
 
2737
            }
 
2738
            case QEMU_OPTION_no_kvm_pit_reinjection: {
 
2739
                kvm_pit_reinject = 0;
 
2740
                break;
 
2741
            }
 
2742
#endif
 
2743
            case QEMU_OPTION_usb:
 
2744
                usb_enabled = 1;
 
2745
                break;
 
2746
            case QEMU_OPTION_usbdevice:
 
2747
                usb_enabled = 1;
 
2748
                add_device_config(DEV_USB, optarg);
 
2749
                break;
 
2750
            case QEMU_OPTION_device:
 
2751
                if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
 
2752
                    exit(1);
 
2753
                }
 
2754
                break;
 
2755
            case QEMU_OPTION_smp:
 
2756
                smp_parse(optarg);
 
2757
                if (smp_cpus < 1) {
 
2758
                    fprintf(stderr, "Invalid number of CPUs\n");
 
2759
                    exit(1);
 
2760
                }
 
2761
                if (max_cpus < smp_cpus) {
 
2762
                    fprintf(stderr, "maxcpus must be equal to or greater than "
 
2763
                            "smp\n");
 
2764
                    exit(1);
 
2765
                }
 
2766
                if (max_cpus > 255) {
 
2767
                    fprintf(stderr, "Unsupported number of maxcpus\n");
 
2768
                    exit(1);
 
2769
                }
 
2770
                break;
 
2771
            case QEMU_OPTION_vnc:
 
2772
#ifdef CONFIG_VNC
 
2773
                display_remote++;
 
2774
                vnc_display = optarg;
 
2775
#else
 
2776
                fprintf(stderr, "VNC support is disabled\n");
 
2777
                exit(1);
 
2778
#endif
 
2779
                break;
 
2780
            case QEMU_OPTION_no_acpi:
 
2781
                acpi_enabled = 0;
 
2782
                break;
 
2783
            case QEMU_OPTION_no_hpet:
 
2784
                no_hpet = 1;
 
2785
                break;
 
2786
            case QEMU_OPTION_balloon:
 
2787
                if (balloon_parse(optarg) < 0) {
 
2788
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
 
2789
                    exit(1);
 
2790
                }
 
2791
                break;
 
2792
            case QEMU_OPTION_no_reboot:
 
2793
                no_reboot = 1;
 
2794
                break;
 
2795
            case QEMU_OPTION_no_shutdown:
 
2796
                no_shutdown = 1;
 
2797
                break;
 
2798
            case QEMU_OPTION_show_cursor:
 
2799
                cursor_hide = 0;
 
2800
                break;
 
2801
            case QEMU_OPTION_uuid:
 
2802
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
 
2803
                    fprintf(stderr, "Fail to parse UUID string."
 
2804
                            " Wrong format.\n");
 
2805
                    exit(1);
 
2806
                }
 
2807
                break;
 
2808
            case QEMU_OPTION_option_rom:
 
2809
                if (nb_option_roms >= MAX_OPTION_ROMS) {
 
2810
                    fprintf(stderr, "Too many option ROMs\n");
 
2811
                    exit(1);
 
2812
                }
 
2813
                opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
 
2814
                option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
 
2815
                option_rom[nb_option_roms].bootindex =
 
2816
                    qemu_opt_get_number(opts, "bootindex", -1);
 
2817
                if (!option_rom[nb_option_roms].name) {
 
2818
                    fprintf(stderr, "Option ROM file is not specified\n");
 
2819
                    exit(1);
 
2820
                }
 
2821
                nb_option_roms++;
 
2822
                break;
 
2823
            case QEMU_OPTION_semihosting:
 
2824
                semihosting_enabled = 1;
 
2825
                break;
 
2826
            case QEMU_OPTION_tdf:
 
2827
                time_drift_fix = 1;
 
2828
                break;
 
2829
            case QEMU_OPTION_kvm_shadow_memory:
 
2830
                kvm_shadow_memory = (int64_t)atoi(optarg) * 1024 * 1024 / 4096;
 
2831
                break;
 
2832
            case QEMU_OPTION_name:
 
2833
                qemu_name = qemu_strdup(optarg);
 
2834
                 {
 
2835
                     char *p = strchr(qemu_name, ',');
 
2836
                     if (p != NULL) {
 
2837
                        *p++ = 0;
 
2838
                        if (strncmp(p, "process=", 8)) {
 
2839
                            fprintf(stderr, "Unknown subargument %s to -name\n", p);
 
2840
                            exit(1);
 
2841
                        }
 
2842
                        p += 8;
 
2843
                        os_set_proc_name(p);
 
2844
                     }  
 
2845
                 }      
 
2846
                break;
 
2847
            case QEMU_OPTION_prom_env:
 
2848
                if (nb_prom_envs >= MAX_PROM_ENVS) {
 
2849
                    fprintf(stderr, "Too many prom variables\n");
 
2850
                    exit(1);
 
2851
                }
 
2852
                prom_envs[nb_prom_envs] = optarg;
 
2853
                nb_prom_envs++;
 
2854
                break;
 
2855
            case QEMU_OPTION_old_param:
 
2856
                old_param = 1;
 
2857
                break;
 
2858
            case QEMU_OPTION_clock:
 
2859
                configure_alarms(optarg);
 
2860
                break;
 
2861
            case QEMU_OPTION_startdate:
 
2862
                configure_rtc_date_offset(optarg, 1);
 
2863
                break;
 
2864
            case QEMU_OPTION_rtc:
 
2865
                opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
 
2866
                if (!opts) {
 
2867
                    exit(1);
 
2868
                }
 
2869
                configure_rtc(opts);
 
2870
                break;
 
2871
            case QEMU_OPTION_tb_size:
 
2872
                tb_size = strtol(optarg, NULL, 0);
 
2873
                if (tb_size < 0)
 
2874
                    tb_size = 0;
 
2875
                break;
 
2876
            case QEMU_OPTION_icount:
 
2877
                icount_option = optarg;
 
2878
                break;
 
2879
            case QEMU_OPTION_incoming:
 
2880
                incoming = optarg;
 
2881
                incoming_expected = true;
 
2882
                break;
 
2883
            case QEMU_OPTION_nodefaults:
 
2884
                default_serial = 0;
 
2885
                default_parallel = 0;
 
2886
                default_virtcon = 0;
 
2887
                default_monitor = 0;
 
2888
                default_vga = 0;
 
2889
                default_net = 0;
 
2890
                default_floppy = 0;
 
2891
                default_cdrom = 0;
 
2892
                default_sdcard = 0;
 
2893
                break;
 
2894
#ifndef _WIN32
 
2895
            case QEMU_OPTION_nvram:
 
2896
                nvram = optarg;
 
2897
                break;
 
2898
#endif
 
2899
            case QEMU_OPTION_xen_domid:
 
2900
                if (!(xen_available())) {
 
2901
                    printf("Option %s not supported for this target\n", popt->name);
 
2902
                    exit(1);
 
2903
                }
 
2904
                xen_domid = atoi(optarg);
 
2905
                break;
 
2906
            case QEMU_OPTION_xen_create:
 
2907
                if (!(xen_available())) {
 
2908
                    printf("Option %s not supported for this target\n", popt->name);
 
2909
                    exit(1);
 
2910
                }
 
2911
                xen_mode = XEN_CREATE;
 
2912
                break;
 
2913
            case QEMU_OPTION_xen_attach:
 
2914
                if (!(xen_available())) {
 
2915
                    printf("Option %s not supported for this target\n", popt->name);
 
2916
                    exit(1);
 
2917
                }
 
2918
                xen_mode = XEN_ATTACH;
 
2919
                break;
 
2920
#ifdef CONFIG_SIMPLE_TRACE
 
2921
            case QEMU_OPTION_trace:
 
2922
                opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
 
2923
                if (opts) {
 
2924
                    trace_file = qemu_opt_get(opts, "file");
 
2925
                }
 
2926
                break;
 
2927
#endif
 
2928
            case QEMU_OPTION_readconfig:
 
2929
                {
 
2930
                    int ret = qemu_read_config_file(optarg);
 
2931
                    if (ret < 0) {
 
2932
                        fprintf(stderr, "read config %s: %s\n", optarg,
 
2933
                            strerror(-ret));
 
2934
                        exit(1);
 
2935
                    }
 
2936
                    break;
 
2937
                }
 
2938
            case QEMU_OPTION_spice:
 
2939
                olist = qemu_find_opts("spice");
 
2940
                if (!olist) {
 
2941
                    fprintf(stderr, "spice is not supported by this qemu build.\n");
 
2942
                    exit(1);
 
2943
                }
 
2944
                opts = qemu_opts_parse(olist, optarg, 0);
 
2945
                if (!opts) {
 
2946
                    fprintf(stderr, "parse error: %s\n", optarg);
 
2947
                    exit(1);
 
2948
                }
 
2949
                break;
 
2950
            case QEMU_OPTION_writeconfig:
 
2951
                {
 
2952
                    FILE *fp;
 
2953
                    if (strcmp(optarg, "-") == 0) {
 
2954
                        fp = stdout;
 
2955
                    } else {
 
2956
                        fp = fopen(optarg, "w");
 
2957
                        if (fp == NULL) {
 
2958
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
 
2959
                            exit(1);
 
2960
                        }
 
2961
                    }
 
2962
                    qemu_config_write(fp);
 
2963
                    fclose(fp);
 
2964
                    break;
 
2965
                }
 
2966
            default:
 
2967
                os_parse_cmd_args(popt->index, optarg);
 
2968
            }
 
2969
        }
 
2970
    }
 
2971
    loc_set_none();
 
2972
 
 
2973
    /* Open the logfile at this point, if necessary. We can't open the logfile
 
2974
     * when encountering either of the logging options (-d or -D) because the
 
2975
     * other one may be encountered later on the command line, changing the
 
2976
     * location or level of logging.
 
2977
     */
 
2978
    if (log_mask) {
 
2979
        if (log_file) {
 
2980
            set_cpu_log_filename(log_file);
 
2981
        }
 
2982
        set_cpu_log(log_mask);
 
2983
    }
 
2984
 
 
2985
    if (!st_init(trace_file)) {
 
2986
        fprintf(stderr, "warning: unable to initialize simple trace backend\n");
 
2987
    }
 
2988
 
 
2989
    /* If no data_dir is specified then try to find it relative to the
 
2990
       executable path.  */
 
2991
    if (!data_dir) {
 
2992
        data_dir = os_find_datadir(argv[0]);
 
2993
    }
 
2994
    /* If all else fails use the install patch specified when building.  */
 
2995
    if (!data_dir) {
 
2996
        data_dir = CONFIG_QEMU_DATADIR;
 
2997
    }
 
2998
 
 
2999
    /*
 
3000
     * Default to max_cpus = smp_cpus, in case the user doesn't
 
3001
     * specify a max_cpus value.
 
3002
     */
 
3003
    if (!max_cpus)
 
3004
        max_cpus = smp_cpus;
 
3005
 
 
3006
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
 
3007
    if (smp_cpus > machine->max_cpus) {
 
3008
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
 
3009
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
 
3010
                machine->max_cpus);
 
3011
        exit(1);
 
3012
    }
 
3013
 
 
3014
    /*
 
3015
     * Get the default machine options from the machine if it is not already
 
3016
     * specified either by the configuration file or by the command line.
 
3017
     */
 
3018
    if (machine->default_machine_opts) {
 
3019
        QemuOptsList *list = qemu_find_opts("machine");
 
3020
        const char *p = NULL;
 
3021
 
 
3022
        if (!QTAILQ_EMPTY(&list->head)) {
 
3023
            p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
 
3024
        }
 
3025
        if (p == NULL) {
 
3026
            qemu_opts_reset(list);
 
3027
            opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
 
3028
            if (!opts) {
 
3029
                fprintf(stderr, "parse error for machine %s: %s\n",
 
3030
                        machine->name, machine->default_machine_opts);
 
3031
                exit(1);
 
3032
            }
 
3033
        }
 
3034
    }
 
3035
 
 
3036
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
 
3037
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
 
3038
 
 
3039
    if (machine->no_serial) {
 
3040
        default_serial = 0;
 
3041
    }
 
3042
    if (machine->no_parallel) {
 
3043
        default_parallel = 0;
 
3044
    }
 
3045
    if (!machine->use_virtcon) {
 
3046
        default_virtcon = 0;
 
3047
    }
 
3048
    if (machine->no_vga) {
 
3049
        default_vga = 0;
 
3050
    }
 
3051
    if (machine->no_floppy) {
 
3052
        default_floppy = 0;
 
3053
    }
 
3054
    if (machine->no_cdrom) {
 
3055
        default_cdrom = 0;
 
3056
    }
 
3057
    if (machine->no_sdcard) {
 
3058
        default_sdcard = 0;
 
3059
    }
 
3060
 
 
3061
    if (display_type == DT_NOGRAPHIC) {
 
3062
        if (default_parallel)
 
3063
            add_device_config(DEV_PARALLEL, "null");
 
3064
        if (default_serial && default_monitor) {
 
3065
            add_device_config(DEV_SERIAL, "mon:stdio");
 
3066
        } else if (default_virtcon && default_monitor) {
 
3067
            add_device_config(DEV_VIRTCON, "mon:stdio");
 
3068
        } else {
 
3069
            if (default_serial)
 
3070
                add_device_config(DEV_SERIAL, "stdio");
 
3071
            if (default_virtcon)
 
3072
                add_device_config(DEV_VIRTCON, "stdio");
 
3073
            if (default_monitor)
 
3074
                monitor_parse("stdio", "readline");
 
3075
        }
 
3076
    } else {
 
3077
        if (default_serial)
 
3078
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
 
3079
        if (default_parallel)
 
3080
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
 
3081
        if (default_monitor)
 
3082
            monitor_parse("vc:80Cx24C", "readline");
 
3083
        if (default_virtcon)
 
3084
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
 
3085
    }
 
3086
    if (default_vga)
 
3087
        vga_interface_type = VGA_CIRRUS;
 
3088
 
 
3089
    socket_init();
 
3090
 
 
3091
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
 
3092
        exit(1);
 
3093
#ifdef CONFIG_VIRTFS
 
3094
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
 
3095
        exit(1);
 
3096
    }
 
3097
#endif
 
3098
 
 
3099
    os_daemonize();
 
3100
 
 
3101
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
 
3102
        os_pidfile_error();
 
3103
        exit(1);
 
3104
    }
 
3105
 
 
3106
    configure_accelerator();
 
3107
 
 
3108
    if (qemu_init_main_loop()) {
 
3109
        fprintf(stderr, "qemu_init_main_loop failed\n");
 
3110
        exit(1);
 
3111
    }
 
3112
    linux_boot = (kernel_filename != NULL);
 
3113
 
 
3114
    if (!linux_boot && *kernel_cmdline != '\0') {
 
3115
        fprintf(stderr, "-append only allowed with -kernel option\n");
 
3116
        exit(1);
 
3117
    }
 
3118
 
 
3119
    if (!linux_boot && initrd_filename != NULL) {
 
3120
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
 
3121
        exit(1);
 
3122
    }
 
3123
 
 
3124
    os_set_line_buffering();
 
3125
 
 
3126
    if (init_timer_alarm() < 0) {
 
3127
        fprintf(stderr, "could not initialize alarm timer\n");
 
3128
        exit(1);
 
3129
    }
 
3130
    configure_icount(icount_option);
 
3131
 
 
3132
    if (net_init_clients() < 0) {
 
3133
        exit(1);
 
3134
    }
 
3135
 
 
3136
    /* init the bluetooth world */
 
3137
    if (foreach_device_config(DEV_BT, bt_parse))
 
3138
        exit(1);
 
3139
 
 
3140
    /* init the memory */
 
3141
    if (ram_size == 0)
 
3142
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
 
3143
 
 
3144
    /* init the dynamic translator */
 
3145
    cpu_exec_init_all(tb_size * 1024 * 1024);
 
3146
 
 
3147
    bdrv_init_with_whitelist();
 
3148
 
 
3149
    blk_mig_init();
 
3150
 
 
3151
    /* open the virtual block devices */
 
3152
    if (snapshot)
 
3153
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
 
3154
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
 
3155
        exit(1);
 
3156
 
 
3157
    default_drive(default_cdrom, snapshot, machine->use_scsi,
 
3158
                  IF_DEFAULT, 2, CDROM_OPTS);
 
3159
    default_drive(default_floppy, snapshot, machine->use_scsi,
 
3160
                  IF_FLOPPY, 0, FD_OPTS);
 
3161
    default_drive(default_sdcard, snapshot, machine->use_scsi,
 
3162
                  IF_SD, 0, SD_OPTS);
 
3163
 
 
3164
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
 
3165
                         ram_load, NULL);
 
3166
 
 
3167
    if (nb_numa_nodes > 0) {
 
3168
        int i;
 
3169
 
 
3170
        if (nb_numa_nodes > MAX_NODES) {
 
3171
            nb_numa_nodes = MAX_NODES;
 
3172
        }
 
3173
 
 
3174
        /* If no memory size if given for any node, assume the default case
 
3175
         * and distribute the available memory equally across all nodes
 
3176
         */
 
3177
        for (i = 0; i < nb_numa_nodes; i++) {
 
3178
            if (node_mem[i] != 0)
 
3179
                break;
 
3180
        }
 
3181
        if (i == nb_numa_nodes) {
 
3182
            uint64_t usedmem = 0;
 
3183
 
 
3184
            /* On Linux, the each node's border has to be 8MB aligned,
 
3185
             * the final node gets the rest.
 
3186
             */
 
3187
            for (i = 0; i < nb_numa_nodes - 1; i++) {
 
3188
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
 
3189
                usedmem += node_mem[i];
 
3190
            }
 
3191
            node_mem[i] = ram_size - usedmem;
 
3192
        }
 
3193
 
 
3194
        for (i = 0; i < nb_numa_nodes; i++) {
 
3195
            if (node_cpumask[i] != 0)
 
3196
                break;
 
3197
        }
 
3198
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
 
3199
         * must cope with this anyway, because there are BIOSes out there in
 
3200
         * real machines which also use this scheme.
 
3201
         */
 
3202
        if (i == nb_numa_nodes) {
 
3203
            for (i = 0; i < smp_cpus; i++) {
 
3204
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
 
3205
            }
 
3206
        }
 
3207
    }
 
3208
 
 
3209
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
 
3210
        exit(1);
 
3211
    }
 
3212
 
 
3213
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
 
3214
        exit(1);
 
3215
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
 
3216
        exit(1);
 
3217
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
 
3218
        exit(1);
 
3219
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
 
3220
        exit(1);
 
3221
 
 
3222
    module_call_init(MODULE_INIT_DEVICE);
 
3223
 
 
3224
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
 
3225
        exit(0);
 
3226
 
 
3227
    if (watchdog) {
 
3228
        i = select_watchdog(watchdog);
 
3229
        if (i > 0)
 
3230
            exit (i == 1 ? 1 : 0);
 
3231
    }
 
3232
 
 
3233
    if (machine->compat_props) {
 
3234
        qdev_prop_register_global_list(machine->compat_props);
 
3235
    }
 
3236
    qemu_add_globals();
 
3237
 
 
3238
    machine->init(ram_size, boot_devices,
 
3239
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
 
3240
 
 
3241
    cpu_synchronize_all_post_init();
 
3242
 
 
3243
    set_numa_modes();
 
3244
 
 
3245
    current_machine = machine;
 
3246
 
 
3247
    /* init USB devices */
 
3248
    if (usb_enabled) {
 
3249
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
 
3250
            exit(1);
 
3251
    }
 
3252
 
 
3253
    /* init generic devices */
 
3254
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
 
3255
        exit(1);
 
3256
 
 
3257
    net_check_clients();
 
3258
 
 
3259
    /* just use the first displaystate for the moment */
 
3260
    ds = get_displaystate();
 
3261
 
 
3262
    if (using_spice)
 
3263
        display_remote++;
 
3264
    if (display_type == DT_DEFAULT && !display_remote) {
 
3265
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
 
3266
        display_type = DT_SDL;
 
3267
#elif defined(CONFIG_VNC)
 
3268
        vnc_display = "localhost:0,to=99";
 
3269
        show_vnc_port = 1;
 
3270
#else
 
3271
        display_type = DT_NONE;
 
3272
#endif
 
3273
    }
 
3274
 
 
3275
 
 
3276
    /* init local displays */
 
3277
    switch (display_type) {
 
3278
    case DT_NOGRAPHIC:
 
3279
        break;
 
3280
#if defined(CONFIG_CURSES)
 
3281
    case DT_CURSES:
 
3282
        curses_display_init(ds, full_screen);
 
3283
        break;
 
3284
#endif
 
3285
#if defined(CONFIG_SDL)
 
3286
    case DT_SDL:
 
3287
        sdl_display_init(ds, full_screen, no_frame);
 
3288
        break;
 
3289
#elif defined(CONFIG_COCOA)
 
3290
    case DT_SDL:
 
3291
        cocoa_display_init(ds, full_screen);
 
3292
        break;
 
3293
#endif
 
3294
    default:
 
3295
        break;
 
3296
    }
 
3297
 
 
3298
    /* must be after terminal init, SDL library changes signal handlers */
 
3299
    os_setup_signal_handling();
 
3300
 
 
3301
#ifdef CONFIG_VNC
 
3302
    /* init remote displays */
 
3303
    if (vnc_display) {
 
3304
        vnc_display_init(ds);
 
3305
        if (vnc_display_open(ds, vnc_display) < 0)
 
3306
            exit(1);
 
3307
 
 
3308
        if (show_vnc_port) {
 
3309
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
 
3310
        }
 
3311
    }
 
3312
#endif
 
3313
#ifdef CONFIG_SPICE
 
3314
    if (using_spice && !qxl_enabled) {
 
3315
        qemu_spice_display_init(ds);
 
3316
    }
 
3317
#endif
 
3318
 
 
3319
    /* display setup */
 
3320
    dpy_resize(ds);
 
3321
    dcl = ds->listeners;
 
3322
    while (dcl != NULL) {
 
3323
        if (dcl->dpy_refresh != NULL) {
 
3324
            ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
 
3325
            qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
 
3326
            break;
 
3327
        }
 
3328
        dcl = dcl->next;
 
3329
    }
 
3330
    if (ds->gui_timer == NULL) {
 
3331
        nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
 
3332
        qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
 
3333
    }
 
3334
    text_consoles_set_display(ds);
 
3335
 
 
3336
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
 
3337
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
 
3338
                gdbstub_dev);
 
3339
        exit(1);
 
3340
    }
 
3341
 
 
3342
    qdev_machine_creation_done();
 
3343
 
 
3344
    if (rom_load_all() != 0) {
 
3345
        fprintf(stderr, "rom loading failed\n");
 
3346
        exit(1);
 
3347
    }
 
3348
 
 
3349
    /* TODO: once all bus devices are qdevified, this should be done
 
3350
     * when bus is created by qdev.c */
 
3351
    qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
 
3352
    qemu_run_machine_init_done_notifiers();
 
3353
 
 
3354
    qemu_system_reset(VMRESET_SILENT);
 
3355
    if (loadvm) {
 
3356
        if (load_vmstate(loadvm) < 0) {
 
3357
            autostart = 0;
 
3358
        }
 
3359
    }
 
3360
 
 
3361
    if (incoming) {
 
3362
        int ret = qemu_start_incoming_migration(incoming);
 
3363
        if (ret < 0) {
 
3364
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
 
3365
                    incoming, ret);
 
3366
            exit(ret);
 
3367
        }
 
3368
    } else if (autostart) {
 
3369
        vm_start();
 
3370
    }
 
3371
 
 
3372
    os_setup_post();
 
3373
 
 
3374
    main_loop();
 
3375
    quit_timers();
 
3376
    net_cleanup();
 
3377
 
 
3378
    return 0;
 
3379
}