Statistics
| Branch: | Revision:

root / vl.c @ 5d8a4f8f

History | View | Annotate | Download (105.4 kB)

1 0824d6fc bellard
/*
2 80cabfad bellard
 * QEMU System Emulator
3 5fafdf24 ths
 *
4 68d0f70e bellard
 * Copyright (c) 2003-2008 Fabrice Bellard
5 5fafdf24 ths
 *
6 1df912cf bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1df912cf bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1df912cf bellard
 * in the Software without restriction, including without limitation the rights
9 1df912cf bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1df912cf bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1df912cf bellard
 * furnished to do so, subject to the following conditions:
12 1df912cf bellard
 *
13 1df912cf bellard
 * The above copyright notice and this permission notice shall be included in
14 1df912cf bellard
 * all copies or substantial portions of the Software.
15 1df912cf bellard
 *
16 1df912cf bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1df912cf bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1df912cf bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1df912cf bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1df912cf bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1df912cf bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1df912cf bellard
 * THE SOFTWARE.
23 0824d6fc bellard
 */
24 0824d6fc bellard
#include <unistd.h>
25 0824d6fc bellard
#include <fcntl.h>
26 0824d6fc bellard
#include <signal.h>
27 0824d6fc bellard
#include <time.h>
28 0824d6fc bellard
#include <errno.h>
29 67b915a5 bellard
#include <sys/time.h>
30 c88676f8 bellard
#include <zlib.h>
31 67b915a5 bellard
32 71e72a19 Juan Quintela
/* Needed early for CONFIG_BSD etc. */
33 d40cdb10 blueswir1
#include "config-host.h"
34 d40cdb10 blueswir1
35 67b915a5 bellard
#ifndef _WIN32
36 5cea8590 Paul Brook
#include <libgen.h>
37 0858532e aliguori
#include <pwd.h>
38 67b915a5 bellard
#include <sys/times.h>
39 f1510b2c bellard
#include <sys/wait.h>
40 67b915a5 bellard
#include <termios.h>
41 67b915a5 bellard
#include <sys/mman.h>
42 f1510b2c bellard
#include <sys/ioctl.h>
43 24646c7e blueswir1
#include <sys/resource.h>
44 f1510b2c bellard
#include <sys/socket.h>
45 c94c8d64 bellard
#include <netinet/in.h>
46 24646c7e blueswir1
#include <net/if.h>
47 24646c7e blueswir1
#include <arpa/inet.h>
48 9d728e8c bellard
#include <dirent.h>
49 7c9d8e07 bellard
#include <netdb.h>
50 cb4b976b ths
#include <sys/select.h>
51 71e72a19 Juan Quintela
#ifdef CONFIG_BSD
52 7d3505c5 bellard
#include <sys/stat.h>
53 a167ba50 Aurelien Jarno
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 7d3505c5 bellard
#include <libutil.h>
55 92c0e657 Juergen Lock
#include <sys/sysctl.h>
56 24646c7e blueswir1
#else
57 24646c7e blueswir1
#include <util.h>
58 128ab2ff blueswir1
#endif
59 bbe813a2 Aurelien Jarno
#else
60 223f0d72 blueswir1
#ifdef __linux__
61 7d3505c5 bellard
#include <pty.h>
62 7d3505c5 bellard
#include <malloc.h>
63 1889465a Andi Kleen
#include <sys/prctl.h>
64 bd494f4c ths
65 e57a8c0e bellard
#include <linux/ppdev.h>
66 5867c88a ths
#include <linux/parport.h>
67 223f0d72 blueswir1
#endif
68 223f0d72 blueswir1
#ifdef __sun__
69 d5d10bc3 ths
#include <sys/stat.h>
70 d5d10bc3 ths
#include <sys/ethernet.h>
71 d5d10bc3 ths
#include <sys/sockio.h>
72 d5d10bc3 ths
#include <netinet/arp.h>
73 d5d10bc3 ths
#include <netinet/in.h>
74 d5d10bc3 ths
#include <netinet/in_systm.h>
75 d5d10bc3 ths
#include <netinet/ip.h>
76 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
77 d5d10bc3 ths
#include <netinet/udp.h>
78 d5d10bc3 ths
#include <netinet/tcp.h>
79 d5d10bc3 ths
#include <net/if.h>
80 d5d10bc3 ths
#include <syslog.h>
81 d5d10bc3 ths
#include <stropts.h>
82 8d32cf0e Blue Swirl
/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 8d32cf0e Blue Swirl
   discussion about Solaris header problems */
84 8d32cf0e Blue Swirl
extern int madvise(caddr_t, size_t, int);
85 67b915a5 bellard
#endif
86 7d3505c5 bellard
#endif
87 ec530c81 bellard
#endif
88 67b915a5 bellard
89 9892fbfb blueswir1
#if defined(__OpenBSD__)
90 9892fbfb blueswir1
#include <util.h>
91 9892fbfb blueswir1
#endif
92 9892fbfb blueswir1
93 8a16d273 ths
#if defined(CONFIG_VDE)
94 8a16d273 ths
#include <libvdeplug.h>
95 8a16d273 ths
#endif
96 8a16d273 ths
97 67b915a5 bellard
#ifdef _WIN32
98 49dc768d aliguori
#include <windows.h>
99 67b915a5 bellard
#endif
100 67b915a5 bellard
101 73332e5c bellard
#ifdef CONFIG_SDL
102 59a36a2f Stefan Weil
#if defined(__APPLE__) || defined(main)
103 6693665a Stefan Weil
#include <SDL.h>
104 880fec5d malc
int qemu_main(int argc, char **argv, char **envp);
105 880fec5d malc
int main(int argc, char **argv)
106 880fec5d malc
{
107 59a36a2f Stefan Weil
    return qemu_main(argc, argv, NULL);
108 880fec5d malc
}
109 880fec5d malc
#undef main
110 880fec5d malc
#define main qemu_main
111 96bcd4f8 bellard
#endif
112 73332e5c bellard
#endif /* CONFIG_SDL */
113 0824d6fc bellard
114 5b0753e0 bellard
#ifdef CONFIG_COCOA
115 5b0753e0 bellard
#undef main
116 5b0753e0 bellard
#define main qemu_main
117 5b0753e0 bellard
#endif /* CONFIG_COCOA */
118 5b0753e0 bellard
119 511d2b14 blueswir1
#include "hw/hw.h"
120 511d2b14 blueswir1
#include "hw/boards.h"
121 511d2b14 blueswir1
#include "hw/usb.h"
122 511d2b14 blueswir1
#include "hw/pcmcia.h"
123 511d2b14 blueswir1
#include "hw/pc.h"
124 511d2b14 blueswir1
#include "hw/isa.h"
125 511d2b14 blueswir1
#include "hw/baum.h"
126 511d2b14 blueswir1
#include "hw/bt.h"
127 9dd986cc Richard W.M. Jones
#include "hw/watchdog.h"
128 b6f6e3d3 aliguori
#include "hw/smbios.h"
129 e37630ca aliguori
#include "hw/xen.h"
130 bd3c948d Gerd Hoffmann
#include "hw/qdev.h"
131 45a50b16 Gerd Hoffmann
#include "hw/loader.h"
132 5ef4efa4 aurel32
#include "bt-host.h"
133 511d2b14 blueswir1
#include "net.h"
134 68ac40d2 Mark McLoughlin
#include "net/slirp.h"
135 511d2b14 blueswir1
#include "monitor.h"
136 511d2b14 blueswir1
#include "console.h"
137 511d2b14 blueswir1
#include "sysemu.h"
138 511d2b14 blueswir1
#include "gdbstub.h"
139 511d2b14 blueswir1
#include "qemu-timer.h"
140 511d2b14 blueswir1
#include "qemu-char.h"
141 511d2b14 blueswir1
#include "cache-utils.h"
142 511d2b14 blueswir1
#include "block.h"
143 c163b5ca lirans@il.ibm.com
#include "block_int.h"
144 c163b5ca lirans@il.ibm.com
#include "block-migration.h"
145 a718acec blueswir1
#include "dma.h"
146 511d2b14 blueswir1
#include "audio/audio.h"
147 511d2b14 blueswir1
#include "migration.h"
148 511d2b14 blueswir1
#include "kvm.h"
149 d3f24367 Kevin Wolf
#include "qemu-option.h"
150 7282a033 Gerd Hoffmann
#include "qemu-config.h"
151 e78c48ec Luiz Capitulino
#include "qemu-objects.h"
152 74db920c Gautham R Shenoy
#ifdef CONFIG_LINUX
153 74db920c Gautham R Shenoy
#include "fsdev/qemu-fsdev.h"
154 74db920c Gautham R Shenoy
#endif
155 511d2b14 blueswir1
156 0824d6fc bellard
#include "disas.h"
157 fc01f7e7 bellard
158 511d2b14 blueswir1
#include "qemu_socket.h"
159 511d2b14 blueswir1
160 d918f23e Jan Kiszka
#include "slirp/libslirp.h"
161 511d2b14 blueswir1
162 72cf2d4f Blue Swirl
#include "qemu-queue.h"
163 296af7c9 Blue Swirl
#include "cpus.h"
164 ad96090a Blue Swirl
#include "arch_init.h"
165 72cf2d4f Blue Swirl
166 9dc63a1e blueswir1
//#define DEBUG_NET
167 9dc63a1e blueswir1
//#define DEBUG_SLIRP
168 330d0414 bellard
169 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
170 313aa567 bellard
171 98b19252 Amit Shah
#define MAX_VIRTIO_CONSOLES 1
172 98b19252 Amit Shah
173 5cea8590 Paul Brook
static const char *data_dir;
174 1192dad8 j_mayer
const char *bios_name = NULL;
175 e4bcb14c ths
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
176 faea38e7 bellard
   to store the VM snapshots */
177 72cf2d4f Blue Swirl
struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
178 72cf2d4f Blue Swirl
struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
179 cb5a7aa8 malc
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
180 993fbfdb Anthony Liguori
DisplayType display_type = DT_DEFAULT;
181 3d11d0eb bellard
const char* keyboard_layout = NULL;
182 c227f099 Anthony Liguori
ram_addr_t ram_size;
183 c902760f Marcelo Tosatti
const char *mem_path = NULL;
184 c902760f Marcelo Tosatti
#ifdef MAP_POPULATE
185 c902760f Marcelo Tosatti
int mem_prealloc = 0; /* force preallocation of physical target memory */
186 c902760f Marcelo Tosatti
#endif
187 c4b1fcc0 bellard
int nb_nics;
188 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
189 8a7ddc38 bellard
int vm_running;
190 d399f677 Paolo Bonzini
int autostart;
191 f6503059 balrog
static int rtc_utc = 1;
192 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
193 6875204c Jan Kiszka
QEMUClock *rtc_clock;
194 64465297 Gerd Hoffmann
int vga_interface_type = VGA_NONE;
195 dbed7e40 blueswir1
static int full_screen = 0;
196 634a21f6 blueswir1
#ifdef CONFIG_SDL
197 dbed7e40 blueswir1
static int no_frame = 0;
198 634a21f6 blueswir1
#endif
199 667accab ths
int no_quit = 0;
200 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
201 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
202 9ede2fde aliguori
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
203 a09db21f bellard
int win2k_install_hack = 0;
204 73822ec8 aliguori
int rtc_td_hack = 0;
205 bb36d470 bellard
int usb_enabled = 0;
206 1b530a6d aurel32
int singlestep = 0;
207 6a00d601 bellard
int smp_cpus = 1;
208 6be68d7e Jes Sorensen
int max_cpus = 0;
209 dc6b1c09 Andre Przywara
int smp_cores = 1;
210 dc6b1c09 Andre Przywara
int smp_threads = 1;
211 73fc9742 ths
const char *vnc_display;
212 6515b203 bellard
int acpi_enabled = 1;
213 16b29ae1 aliguori
int no_hpet = 0;
214 52ca8d6a bellard
int fd_bootchk = 1;
215 d1beab82 bellard
int no_reboot = 0;
216 b2f76161 aurel32
int no_shutdown = 0;
217 9467cd46 balrog
int cursor_hide = 1;
218 a171fe39 balrog
int graphic_rotate = 0;
219 6b35e7bf Jes Sorensen
uint8_t irq0override = 1;
220 b9e82a59 blueswir1
#ifndef _WIN32
221 71e3ceb8 ths
int daemonize = 0;
222 b9e82a59 blueswir1
#endif
223 09aaa160 Markus Armbruster
const char *watchdog;
224 9ae02555 ths
const char *option_rom[MAX_OPTION_ROMS];
225 9ae02555 ths
int nb_option_roms;
226 8e71621f pbrook
int semihosting_enabled = 0;
227 2b8f2d41 balrog
int old_param = 0;
228 c35734b2 ths
const char *qemu_name;
229 3780e197 ths
int alt_grab = 0;
230 0ca9f8a4 Dustin Kirkland
int ctrl_grab = 0;
231 66508601 blueswir1
unsigned int nb_prom_envs = 0;
232 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
233 95387491 Jan Kiszka
int boot_menu;
234 0824d6fc bellard
235 268a362c aliguori
int nb_numa_nodes;
236 268a362c aliguori
uint64_t node_mem[MAX_NODES];
237 268a362c aliguori
uint64_t node_cpumask[MAX_NODES];
238 268a362c aliguori
239 9043b62d blueswir1
static QEMUTimer *nographic_timer;
240 ee5605e5 balrog
241 8fcb1b90 blueswir1
uint8_t qemu_uuid[16];
242 8fcb1b90 blueswir1
243 76e30d0f Jan Kiszka
static QEMUBootSetHandler *boot_set_handler;
244 76e30d0f Jan Kiszka
static void *boot_set_opaque;
245 76e30d0f Jan Kiszka
246 d745bef8 Blue Swirl
int kvm_allowed = 0;
247 d745bef8 Blue Swirl
uint32_t xen_domid;
248 d745bef8 Blue Swirl
enum xen_mode xen_mode = XEN_EMULATE;
249 d745bef8 Blue Swirl
250 998bbd74 Gerd Hoffmann
static int default_serial = 1;
251 6a5e8b0e Gerd Hoffmann
static int default_parallel = 1;
252 986c5f78 Gerd Hoffmann
static int default_virtcon = 1;
253 abdeed06 Gerd Hoffmann
static int default_monitor = 1;
254 64465297 Gerd Hoffmann
static int default_vga = 1;
255 ac33f8fa Gerd Hoffmann
static int default_floppy = 1;
256 ac33f8fa Gerd Hoffmann
static int default_cdrom = 1;
257 ac33f8fa Gerd Hoffmann
static int default_sdcard = 1;
258 998bbd74 Gerd Hoffmann
259 998bbd74 Gerd Hoffmann
static struct {
260 998bbd74 Gerd Hoffmann
    const char *driver;
261 998bbd74 Gerd Hoffmann
    int *flag;
262 998bbd74 Gerd Hoffmann
} default_list[] = {
263 6a5e8b0e Gerd Hoffmann
    { .driver = "isa-serial",           .flag = &default_serial    },
264 6a5e8b0e Gerd Hoffmann
    { .driver = "isa-parallel",         .flag = &default_parallel  },
265 d8bcbabf Gerd Hoffmann
    { .driver = "isa-fdc",              .flag = &default_floppy    },
266 d8bcbabf Gerd Hoffmann
    { .driver = "ide-drive",            .flag = &default_cdrom     },
267 392ecf54 Amit Shah
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
268 392ecf54 Amit Shah
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
269 392ecf54 Amit Shah
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
270 64465297 Gerd Hoffmann
    { .driver = "VGA",                  .flag = &default_vga       },
271 69fd02ee Gerd Hoffmann
    { .driver = "cirrus-vga",           .flag = &default_vga       },
272 69fd02ee Gerd Hoffmann
    { .driver = "vmware-svga",          .flag = &default_vga       },
273 998bbd74 Gerd Hoffmann
};
274 998bbd74 Gerd Hoffmann
275 998bbd74 Gerd Hoffmann
static int default_driver_check(QemuOpts *opts, void *opaque)
276 998bbd74 Gerd Hoffmann
{
277 998bbd74 Gerd Hoffmann
    const char *driver = qemu_opt_get(opts, "driver");
278 998bbd74 Gerd Hoffmann
    int i;
279 998bbd74 Gerd Hoffmann
280 998bbd74 Gerd Hoffmann
    if (!driver)
281 998bbd74 Gerd Hoffmann
        return 0;
282 998bbd74 Gerd Hoffmann
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
283 998bbd74 Gerd Hoffmann
        if (strcmp(default_list[i].driver, driver) != 0)
284 998bbd74 Gerd Hoffmann
            continue;
285 998bbd74 Gerd Hoffmann
        *(default_list[i].flag) = 0;
286 998bbd74 Gerd Hoffmann
    }
287 998bbd74 Gerd Hoffmann
    return 0;
288 998bbd74 Gerd Hoffmann
}
289 998bbd74 Gerd Hoffmann
290 0824d6fc bellard
/***********************************************************/
291 0824d6fc bellard
292 1889465a Andi Kleen
static void set_proc_name(const char *s)
293 1889465a Andi Kleen
{
294 6ca8d0fd Nathan Froyd
#if defined(__linux__) && defined(PR_SET_NAME)
295 1889465a Andi Kleen
    char name[16];
296 1889465a Andi Kleen
    if (!s)
297 1889465a Andi Kleen
        return;
298 1889465a Andi Kleen
    name[sizeof(name) - 1] = 0;
299 1889465a Andi Kleen
    strncpy(name, s, sizeof(name));
300 1889465a Andi Kleen
    /* Could rewrite argv[0] too, but that's a bit more complicated.
301 1889465a Andi Kleen
       This simple way is enough for `top'. */
302 1889465a Andi Kleen
    prctl(PR_SET_NAME, name);
303 1889465a Andi Kleen
#endif            
304 1889465a Andi Kleen
}
305 df751fa8 aliguori
 
306 8f0056b7 Paolo Bonzini
/***********************************************************/
307 8f0056b7 Paolo Bonzini
/* real time host monotonic timer */
308 09b26c5e bellard
309 1dce7c3c bellard
/* compute with 96 bit intermediate result: (a*b)/c */
310 1dce7c3c bellard
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
311 0824d6fc bellard
{
312 1dce7c3c bellard
    union {
313 1dce7c3c bellard
        uint64_t ll;
314 1dce7c3c bellard
        struct {
315 e2542fe2 Juan Quintela
#ifdef HOST_WORDS_BIGENDIAN
316 1dce7c3c bellard
            uint32_t high, low;
317 1dce7c3c bellard
#else
318 1dce7c3c bellard
            uint32_t low, high;
319 3b46e624 ths
#endif
320 1dce7c3c bellard
        } l;
321 1dce7c3c bellard
    } u, res;
322 1dce7c3c bellard
    uint64_t rl, rh;
323 0824d6fc bellard
324 1dce7c3c bellard
    u.ll = a;
325 1dce7c3c bellard
    rl = (uint64_t)u.l.low * (uint64_t)b;
326 1dce7c3c bellard
    rh = (uint64_t)u.l.high * (uint64_t)b;
327 1dce7c3c bellard
    rh += (rl >> 32);
328 1dce7c3c bellard
    res.l.high = rh / c;
329 1dce7c3c bellard
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
330 1dce7c3c bellard
    return res.ll;
331 34865134 bellard
}
332 34865134 bellard
333 c4b1fcc0 bellard
/***********************************************************/
334 f6503059 balrog
/* host time/date access */
335 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
336 f6503059 balrog
{
337 f6503059 balrog
    time_t ti;
338 f6503059 balrog
    struct tm *ret;
339 f6503059 balrog
340 f6503059 balrog
    time(&ti);
341 f6503059 balrog
    ti += offset;
342 f6503059 balrog
    if (rtc_date_offset == -1) {
343 f6503059 balrog
        if (rtc_utc)
344 f6503059 balrog
            ret = gmtime(&ti);
345 f6503059 balrog
        else
346 f6503059 balrog
            ret = localtime(&ti);
347 f6503059 balrog
    } else {
348 f6503059 balrog
        ti -= rtc_date_offset;
349 f6503059 balrog
        ret = gmtime(&ti);
350 f6503059 balrog
    }
351 f6503059 balrog
352 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
353 f6503059 balrog
}
354 f6503059 balrog
355 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
356 f6503059 balrog
{
357 f6503059 balrog
    time_t seconds;
358 f6503059 balrog
359 f6503059 balrog
    if (rtc_date_offset == -1)
360 f6503059 balrog
        if (rtc_utc)
361 f6503059 balrog
            seconds = mktimegm(tm);
362 f6503059 balrog
        else
363 f6503059 balrog
            seconds = mktime(tm);
364 f6503059 balrog
    else
365 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
366 f6503059 balrog
367 f6503059 balrog
    return seconds - time(NULL);
368 f6503059 balrog
}
369 f6503059 balrog
370 80cd3478 Luiz Capitulino
void rtc_change_mon_event(struct tm *tm)
371 80cd3478 Luiz Capitulino
{
372 80cd3478 Luiz Capitulino
    QObject *data;
373 80cd3478 Luiz Capitulino
374 80cd3478 Luiz Capitulino
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
375 80cd3478 Luiz Capitulino
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
376 80cd3478 Luiz Capitulino
    qobject_decref(data);
377 80cd3478 Luiz Capitulino
}
378 80cd3478 Luiz Capitulino
379 1ed2fc1f Jan Kiszka
static void configure_rtc_date_offset(const char *startdate, int legacy)
380 1ed2fc1f Jan Kiszka
{
381 1ed2fc1f Jan Kiszka
    time_t rtc_start_date;
382 1ed2fc1f Jan Kiszka
    struct tm tm;
383 1ed2fc1f Jan Kiszka
384 1ed2fc1f Jan Kiszka
    if (!strcmp(startdate, "now") && legacy) {
385 1ed2fc1f Jan Kiszka
        rtc_date_offset = -1;
386 1ed2fc1f Jan Kiszka
    } else {
387 1ed2fc1f Jan Kiszka
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
388 1ed2fc1f Jan Kiszka
                   &tm.tm_year,
389 1ed2fc1f Jan Kiszka
                   &tm.tm_mon,
390 1ed2fc1f Jan Kiszka
                   &tm.tm_mday,
391 1ed2fc1f Jan Kiszka
                   &tm.tm_hour,
392 1ed2fc1f Jan Kiszka
                   &tm.tm_min,
393 1ed2fc1f Jan Kiszka
                   &tm.tm_sec) == 6) {
394 1ed2fc1f Jan Kiszka
            /* OK */
395 1ed2fc1f Jan Kiszka
        } else if (sscanf(startdate, "%d-%d-%d",
396 1ed2fc1f Jan Kiszka
                          &tm.tm_year,
397 1ed2fc1f Jan Kiszka
                          &tm.tm_mon,
398 1ed2fc1f Jan Kiszka
                          &tm.tm_mday) == 3) {
399 1ed2fc1f Jan Kiszka
            tm.tm_hour = 0;
400 1ed2fc1f Jan Kiszka
            tm.tm_min = 0;
401 1ed2fc1f Jan Kiszka
            tm.tm_sec = 0;
402 1ed2fc1f Jan Kiszka
        } else {
403 1ed2fc1f Jan Kiszka
            goto date_fail;
404 1ed2fc1f Jan Kiszka
        }
405 1ed2fc1f Jan Kiszka
        tm.tm_year -= 1900;
406 1ed2fc1f Jan Kiszka
        tm.tm_mon--;
407 1ed2fc1f Jan Kiszka
        rtc_start_date = mktimegm(&tm);
408 1ed2fc1f Jan Kiszka
        if (rtc_start_date == -1) {
409 1ed2fc1f Jan Kiszka
        date_fail:
410 1ed2fc1f Jan Kiszka
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
411 1ed2fc1f Jan Kiszka
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
412 1ed2fc1f Jan Kiszka
            exit(1);
413 1ed2fc1f Jan Kiszka
        }
414 1ed2fc1f Jan Kiszka
        rtc_date_offset = time(NULL) - rtc_start_date;
415 1ed2fc1f Jan Kiszka
    }
416 1ed2fc1f Jan Kiszka
}
417 1ed2fc1f Jan Kiszka
418 1ed2fc1f Jan Kiszka
static void configure_rtc(QemuOpts *opts)
419 1ed2fc1f Jan Kiszka
{
420 1ed2fc1f Jan Kiszka
    const char *value;
421 1ed2fc1f Jan Kiszka
422 1ed2fc1f Jan Kiszka
    value = qemu_opt_get(opts, "base");
423 1ed2fc1f Jan Kiszka
    if (value) {
424 1ed2fc1f Jan Kiszka
        if (!strcmp(value, "utc")) {
425 1ed2fc1f Jan Kiszka
            rtc_utc = 1;
426 1ed2fc1f Jan Kiszka
        } else if (!strcmp(value, "localtime")) {
427 1ed2fc1f Jan Kiszka
            rtc_utc = 0;
428 1ed2fc1f Jan Kiszka
        } else {
429 1ed2fc1f Jan Kiszka
            configure_rtc_date_offset(value, 0);
430 1ed2fc1f Jan Kiszka
        }
431 1ed2fc1f Jan Kiszka
    }
432 6875204c Jan Kiszka
    value = qemu_opt_get(opts, "clock");
433 6875204c Jan Kiszka
    if (value) {
434 6875204c Jan Kiszka
        if (!strcmp(value, "host")) {
435 6875204c Jan Kiszka
            rtc_clock = host_clock;
436 6875204c Jan Kiszka
        } else if (!strcmp(value, "vm")) {
437 6875204c Jan Kiszka
            rtc_clock = vm_clock;
438 6875204c Jan Kiszka
        } else {
439 6875204c Jan Kiszka
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
440 6875204c Jan Kiszka
            exit(1);
441 6875204c Jan Kiszka
        }
442 6875204c Jan Kiszka
    }
443 1ed2fc1f Jan Kiszka
    value = qemu_opt_get(opts, "driftfix");
444 1ed2fc1f Jan Kiszka
    if (value) {
445 7e4c0336 Blue Swirl
        if (!strcmp(value, "slew")) {
446 1ed2fc1f Jan Kiszka
            rtc_td_hack = 1;
447 7e4c0336 Blue Swirl
        } else if (!strcmp(value, "none")) {
448 1ed2fc1f Jan Kiszka
            rtc_td_hack = 0;
449 1ed2fc1f Jan Kiszka
        } else {
450 1ed2fc1f Jan Kiszka
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
451 1ed2fc1f Jan Kiszka
            exit(1);
452 1ed2fc1f Jan Kiszka
        }
453 1ed2fc1f Jan Kiszka
    }
454 1ed2fc1f Jan Kiszka
}
455 1ed2fc1f Jan Kiszka
456 1ae26a18 balrog
/***********************************************************/
457 1ae26a18 balrog
/* Bluetooth support */
458 1ae26a18 balrog
static int nb_hcis;
459 1ae26a18 balrog
static int cur_hci;
460 1ae26a18 balrog
static struct HCIInfo *hci_table[MAX_NICS];
461 dc72ac14 balrog
462 1ae26a18 balrog
static struct bt_vlan_s {
463 1ae26a18 balrog
    struct bt_scatternet_s net;
464 1ae26a18 balrog
    int id;
465 1ae26a18 balrog
    struct bt_vlan_s *next;
466 1ae26a18 balrog
} *first_bt_vlan;
467 1ae26a18 balrog
468 1ae26a18 balrog
/* find or alloc a new bluetooth "VLAN" */
469 674bb261 blueswir1
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
470 1ae26a18 balrog
{
471 1ae26a18 balrog
    struct bt_vlan_s **pvlan, *vlan;
472 1ae26a18 balrog
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
473 1ae26a18 balrog
        if (vlan->id == id)
474 1ae26a18 balrog
            return &vlan->net;
475 1ae26a18 balrog
    }
476 1ae26a18 balrog
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
477 1ae26a18 balrog
    vlan->id = id;
478 1ae26a18 balrog
    pvlan = &first_bt_vlan;
479 1ae26a18 balrog
    while (*pvlan != NULL)
480 1ae26a18 balrog
        pvlan = &(*pvlan)->next;
481 1ae26a18 balrog
    *pvlan = vlan;
482 1ae26a18 balrog
    return &vlan->net;
483 1ae26a18 balrog
}
484 1ae26a18 balrog
485 1ae26a18 balrog
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
486 1ae26a18 balrog
{
487 1ae26a18 balrog
}
488 1ae26a18 balrog
489 1ae26a18 balrog
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
490 1ae26a18 balrog
{
491 1ae26a18 balrog
    return -ENOTSUP;
492 1ae26a18 balrog
}
493 1ae26a18 balrog
494 1ae26a18 balrog
static struct HCIInfo null_hci = {
495 1ae26a18 balrog
    .cmd_send = null_hci_send,
496 1ae26a18 balrog
    .sco_send = null_hci_send,
497 1ae26a18 balrog
    .acl_send = null_hci_send,
498 1ae26a18 balrog
    .bdaddr_set = null_hci_addr_set,
499 1ae26a18 balrog
};
500 1ae26a18 balrog
501 1ae26a18 balrog
struct HCIInfo *qemu_next_hci(void)
502 1ae26a18 balrog
{
503 1ae26a18 balrog
    if (cur_hci == nb_hcis)
504 1ae26a18 balrog
        return &null_hci;
505 1ae26a18 balrog
506 1ae26a18 balrog
    return hci_table[cur_hci++];
507 1ae26a18 balrog
}
508 1ae26a18 balrog
509 dc72ac14 balrog
static struct HCIInfo *hci_init(const char *str)
510 dc72ac14 balrog
{
511 dc72ac14 balrog
    char *endp;
512 dc72ac14 balrog
    struct bt_scatternet_s *vlan = 0;
513 dc72ac14 balrog
514 dc72ac14 balrog
    if (!strcmp(str, "null"))
515 dc72ac14 balrog
        /* null */
516 dc72ac14 balrog
        return &null_hci;
517 dc72ac14 balrog
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
518 dc72ac14 balrog
        /* host[:hciN] */
519 dc72ac14 balrog
        return bt_host_hci(str[4] ? str + 5 : "hci0");
520 dc72ac14 balrog
    else if (!strncmp(str, "hci", 3)) {
521 dc72ac14 balrog
        /* hci[,vlan=n] */
522 dc72ac14 balrog
        if (str[3]) {
523 dc72ac14 balrog
            if (!strncmp(str + 3, ",vlan=", 6)) {
524 dc72ac14 balrog
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
525 dc72ac14 balrog
                if (*endp)
526 dc72ac14 balrog
                    vlan = 0;
527 dc72ac14 balrog
            }
528 dc72ac14 balrog
        } else
529 dc72ac14 balrog
            vlan = qemu_find_bt_vlan(0);
530 dc72ac14 balrog
        if (vlan)
531 dc72ac14 balrog
           return bt_new_hci(vlan);
532 dc72ac14 balrog
    }
533 dc72ac14 balrog
534 dc72ac14 balrog
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
535 dc72ac14 balrog
536 dc72ac14 balrog
    return 0;
537 dc72ac14 balrog
}
538 dc72ac14 balrog
539 dc72ac14 balrog
static int bt_hci_parse(const char *str)
540 dc72ac14 balrog
{
541 dc72ac14 balrog
    struct HCIInfo *hci;
542 c227f099 Anthony Liguori
    bdaddr_t bdaddr;
543 dc72ac14 balrog
544 dc72ac14 balrog
    if (nb_hcis >= MAX_NICS) {
545 dc72ac14 balrog
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
546 dc72ac14 balrog
        return -1;
547 dc72ac14 balrog
    }
548 dc72ac14 balrog
549 dc72ac14 balrog
    hci = hci_init(str);
550 dc72ac14 balrog
    if (!hci)
551 dc72ac14 balrog
        return -1;
552 dc72ac14 balrog
553 dc72ac14 balrog
    bdaddr.b[0] = 0x52;
554 dc72ac14 balrog
    bdaddr.b[1] = 0x54;
555 dc72ac14 balrog
    bdaddr.b[2] = 0x00;
556 dc72ac14 balrog
    bdaddr.b[3] = 0x12;
557 dc72ac14 balrog
    bdaddr.b[4] = 0x34;
558 dc72ac14 balrog
    bdaddr.b[5] = 0x56 + nb_hcis;
559 dc72ac14 balrog
    hci->bdaddr_set(hci, bdaddr.b);
560 dc72ac14 balrog
561 dc72ac14 balrog
    hci_table[nb_hcis++] = hci;
562 dc72ac14 balrog
563 dc72ac14 balrog
    return 0;
564 dc72ac14 balrog
}
565 dc72ac14 balrog
566 dc72ac14 balrog
static void bt_vhci_add(int vlan_id)
567 dc72ac14 balrog
{
568 dc72ac14 balrog
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
569 dc72ac14 balrog
570 dc72ac14 balrog
    if (!vlan->slave)
571 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a VHCI to "
572 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
573 dc72ac14 balrog
574 dc72ac14 balrog
    bt_vhci_init(bt_new_hci(vlan));
575 dc72ac14 balrog
}
576 dc72ac14 balrog
577 dc72ac14 balrog
static struct bt_device_s *bt_device_add(const char *opt)
578 dc72ac14 balrog
{
579 dc72ac14 balrog
    struct bt_scatternet_s *vlan;
580 dc72ac14 balrog
    int vlan_id = 0;
581 dc72ac14 balrog
    char *endp = strstr(opt, ",vlan=");
582 dc72ac14 balrog
    int len = (endp ? endp - opt : strlen(opt)) + 1;
583 dc72ac14 balrog
    char devname[10];
584 dc72ac14 balrog
585 dc72ac14 balrog
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
586 dc72ac14 balrog
587 dc72ac14 balrog
    if (endp) {
588 dc72ac14 balrog
        vlan_id = strtol(endp + 6, &endp, 0);
589 dc72ac14 balrog
        if (*endp) {
590 dc72ac14 balrog
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
591 dc72ac14 balrog
            return 0;
592 dc72ac14 balrog
        }
593 dc72ac14 balrog
    }
594 dc72ac14 balrog
595 dc72ac14 balrog
    vlan = qemu_find_bt_vlan(vlan_id);
596 dc72ac14 balrog
597 dc72ac14 balrog
    if (!vlan->slave)
598 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a slave device to "
599 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
600 dc72ac14 balrog
601 dc72ac14 balrog
    if (!strcmp(devname, "keyboard"))
602 dc72ac14 balrog
        return bt_keyboard_init(vlan);
603 dc72ac14 balrog
604 dc72ac14 balrog
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
605 dc72ac14 balrog
    return 0;
606 dc72ac14 balrog
}
607 dc72ac14 balrog
608 dc72ac14 balrog
static int bt_parse(const char *opt)
609 dc72ac14 balrog
{
610 dc72ac14 balrog
    const char *endp, *p;
611 dc72ac14 balrog
    int vlan;
612 dc72ac14 balrog
613 dc72ac14 balrog
    if (strstart(opt, "hci", &endp)) {
614 dc72ac14 balrog
        if (!*endp || *endp == ',') {
615 dc72ac14 balrog
            if (*endp)
616 dc72ac14 balrog
                if (!strstart(endp, ",vlan=", 0))
617 dc72ac14 balrog
                    opt = endp + 1;
618 dc72ac14 balrog
619 dc72ac14 balrog
            return bt_hci_parse(opt);
620 dc72ac14 balrog
       }
621 dc72ac14 balrog
    } else if (strstart(opt, "vhci", &endp)) {
622 dc72ac14 balrog
        if (!*endp || *endp == ',') {
623 dc72ac14 balrog
            if (*endp) {
624 dc72ac14 balrog
                if (strstart(endp, ",vlan=", &p)) {
625 dc72ac14 balrog
                    vlan = strtol(p, (char **) &endp, 0);
626 dc72ac14 balrog
                    if (*endp) {
627 dc72ac14 balrog
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
628 dc72ac14 balrog
                        return 1;
629 dc72ac14 balrog
                    }
630 dc72ac14 balrog
                } else {
631 dc72ac14 balrog
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
632 dc72ac14 balrog
                    return 1;
633 dc72ac14 balrog
                }
634 dc72ac14 balrog
            } else
635 dc72ac14 balrog
                vlan = 0;
636 dc72ac14 balrog
637 dc72ac14 balrog
            bt_vhci_add(vlan);
638 dc72ac14 balrog
            return 0;
639 dc72ac14 balrog
        }
640 dc72ac14 balrog
    } else if (strstart(opt, "device:", &endp))
641 dc72ac14 balrog
        return !bt_device_add(endp);
642 dc72ac14 balrog
643 dc72ac14 balrog
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
644 dc72ac14 balrog
    return 1;
645 dc72ac14 balrog
}
646 dc72ac14 balrog
647 1ae26a18 balrog
/***********************************************************/
648 1ae26a18 balrog
/* QEMU Block devices */
649 1ae26a18 balrog
650 609497ab balrog
#define HD_ALIAS "index=%d,media=disk"
651 e4bcb14c ths
#define CDROM_ALIAS "index=2,media=cdrom"
652 e4bcb14c ths
#define FD_ALIAS "index=%d,if=floppy"
653 609497ab balrog
#define PFLASH_ALIAS "if=pflash"
654 609497ab balrog
#define MTD_ALIAS "if=mtd"
655 9d413d1d balrog
#define SD_ALIAS "index=0,if=sd"
656 e4bcb14c ths
657 9dfd7c7a Gerd Hoffmann
QemuOpts *drive_add(const char *file, const char *fmt, ...)
658 e4bcb14c ths
{
659 e4bcb14c ths
    va_list ap;
660 9dfd7c7a Gerd Hoffmann
    char optstr[1024];
661 9dfd7c7a Gerd Hoffmann
    QemuOpts *opts;
662 e4bcb14c ths
663 e4bcb14c ths
    va_start(ap, fmt);
664 9dfd7c7a Gerd Hoffmann
    vsnprintf(optstr, sizeof(optstr), fmt, ap);
665 e4bcb14c ths
    va_end(ap);
666 e4bcb14c ths
667 8212c64f Markus Armbruster
    opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
668 9dfd7c7a Gerd Hoffmann
    if (!opts) {
669 9dfd7c7a Gerd Hoffmann
        return NULL;
670 9dfd7c7a Gerd Hoffmann
    }
671 9dfd7c7a Gerd Hoffmann
    if (file)
672 9dfd7c7a Gerd Hoffmann
        qemu_opt_set(opts, "file", file);
673 9dfd7c7a Gerd Hoffmann
    return opts;
674 b01b1111 aliguori
}
675 b01b1111 aliguori
676 751c6a17 Gerd Hoffmann
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
677 e4bcb14c ths
{
678 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
679 e4bcb14c ths
680 e4bcb14c ths
    /* seek interface, bus and unit */
681 e4bcb14c ths
682 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
683 751c6a17 Gerd Hoffmann
        if (dinfo->type == type &&
684 751c6a17 Gerd Hoffmann
            dinfo->bus == bus &&
685 751c6a17 Gerd Hoffmann
            dinfo->unit == unit)
686 751c6a17 Gerd Hoffmann
            return dinfo;
687 751c6a17 Gerd Hoffmann
    }
688 e4bcb14c ths
689 751c6a17 Gerd Hoffmann
    return NULL;
690 e4bcb14c ths
}
691 e4bcb14c ths
692 2e810b36 Gerd Hoffmann
DriveInfo *drive_get_by_id(const char *id)
693 1dae12e6 Gerd Hoffmann
{
694 1dae12e6 Gerd Hoffmann
    DriveInfo *dinfo;
695 1dae12e6 Gerd Hoffmann
696 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
697 1dae12e6 Gerd Hoffmann
        if (strcmp(id, dinfo->id))
698 1dae12e6 Gerd Hoffmann
            continue;
699 1dae12e6 Gerd Hoffmann
        return dinfo;
700 1dae12e6 Gerd Hoffmann
    }
701 1dae12e6 Gerd Hoffmann
    return NULL;
702 1dae12e6 Gerd Hoffmann
}
703 1dae12e6 Gerd Hoffmann
704 f60d39bc ths
int drive_get_max_bus(BlockInterfaceType type)
705 e4bcb14c ths
{
706 e4bcb14c ths
    int max_bus;
707 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
708 e4bcb14c ths
709 e4bcb14c ths
    max_bus = -1;
710 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
711 751c6a17 Gerd Hoffmann
        if(dinfo->type == type &&
712 751c6a17 Gerd Hoffmann
           dinfo->bus > max_bus)
713 751c6a17 Gerd Hoffmann
            max_bus = dinfo->bus;
714 e4bcb14c ths
    }
715 e4bcb14c ths
    return max_bus;
716 e4bcb14c ths
}
717 e4bcb14c ths
718 fa879c64 aliguori
const char *drive_get_serial(BlockDriverState *bdrv)
719 fa879c64 aliguori
{
720 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
721 fa879c64 aliguori
722 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
723 751c6a17 Gerd Hoffmann
        if (dinfo->bdrv == bdrv)
724 751c6a17 Gerd Hoffmann
            return dinfo->serial;
725 751c6a17 Gerd Hoffmann
    }
726 fa879c64 aliguori
727 fa879c64 aliguori
    return "\0";
728 fa879c64 aliguori
}
729 fa879c64 aliguori
730 f7850099 Kevin Wolf
BlockInterfaceErrorAction drive_get_on_error(
731 f7850099 Kevin Wolf
    BlockDriverState *bdrv, int is_read)
732 428c5705 aliguori
{
733 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
734 428c5705 aliguori
735 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(dinfo, &drives, next) {
736 751c6a17 Gerd Hoffmann
        if (dinfo->bdrv == bdrv)
737 e9b2e818 Kevin Wolf
            return is_read ? dinfo->on_read_error : dinfo->on_write_error;
738 751c6a17 Gerd Hoffmann
    }
739 428c5705 aliguori
740 e9b2e818 Kevin Wolf
    return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
741 428c5705 aliguori
}
742 428c5705 aliguori
743 a1620fac aurel32
static void bdrv_format_print(void *opaque, const char *name)
744 a1620fac aurel32
{
745 a1620fac aurel32
    fprintf(stderr, " %s", name);
746 a1620fac aurel32
}
747 a1620fac aurel32
748 56a14938 Gerd Hoffmann
void drive_uninit(DriveInfo *dinfo)
749 b01b1111 aliguori
{
750 56a14938 Gerd Hoffmann
    qemu_opts_del(dinfo->opts);
751 56a14938 Gerd Hoffmann
    bdrv_delete(dinfo->bdrv);
752 56a14938 Gerd Hoffmann
    QTAILQ_REMOVE(&drives, dinfo, next);
753 56a14938 Gerd Hoffmann
    qemu_free(dinfo);
754 b01b1111 aliguori
}
755 b01b1111 aliguori
756 e9b2e818 Kevin Wolf
static int parse_block_error_action(const char *buf, int is_read)
757 e9b2e818 Kevin Wolf
{
758 e9b2e818 Kevin Wolf
    if (!strcmp(buf, "ignore")) {
759 e9b2e818 Kevin Wolf
        return BLOCK_ERR_IGNORE;
760 e9b2e818 Kevin Wolf
    } else if (!is_read && !strcmp(buf, "enospc")) {
761 e9b2e818 Kevin Wolf
        return BLOCK_ERR_STOP_ENOSPC;
762 e9b2e818 Kevin Wolf
    } else if (!strcmp(buf, "stop")) {
763 e9b2e818 Kevin Wolf
        return BLOCK_ERR_STOP_ANY;
764 e9b2e818 Kevin Wolf
    } else if (!strcmp(buf, "report")) {
765 e9b2e818 Kevin Wolf
        return BLOCK_ERR_REPORT;
766 e9b2e818 Kevin Wolf
    } else {
767 e9b2e818 Kevin Wolf
        fprintf(stderr, "qemu: '%s' invalid %s error action\n",
768 e9b2e818 Kevin Wolf
            buf, is_read ? "read" : "write");
769 e9b2e818 Kevin Wolf
        return -1;
770 e9b2e818 Kevin Wolf
    }
771 e9b2e818 Kevin Wolf
}
772 e9b2e818 Kevin Wolf
773 9dfd7c7a Gerd Hoffmann
DriveInfo *drive_init(QemuOpts *opts, void *opaque,
774 751c6a17 Gerd Hoffmann
                      int *fatal_error)
775 e4bcb14c ths
{
776 9dfd7c7a Gerd Hoffmann
    const char *buf;
777 9dfd7c7a Gerd Hoffmann
    const char *file = NULL;
778 c8522bdf balrog
    char devname[128];
779 9dfd7c7a Gerd Hoffmann
    const char *serial;
780 c8522bdf balrog
    const char *mediastr = "";
781 f60d39bc ths
    BlockInterfaceType type;
782 e4bcb14c ths
    enum { MEDIA_DISK, MEDIA_CDROM } media;
783 e4bcb14c ths
    int bus_id, unit_id;
784 e4bcb14c ths
    int cyls, heads, secs, translation;
785 1e72d3b7 aurel32
    BlockDriver *drv = NULL;
786 4d73cd3b aliguori
    QEMUMachine *machine = opaque;
787 e4bcb14c ths
    int max_devs;
788 e4bcb14c ths
    int index;
789 59f2689d Naphtali Sprei
    int ro = 0;
790 763b6084 Christoph Hellwig
    int bdrv_flags = 0;
791 e9b2e818 Kevin Wolf
    int on_read_error, on_write_error;
792 c2cc47a4 Markus Armbruster
    const char *devaddr;
793 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
794 9dfd7c7a Gerd Hoffmann
    int snapshot = 0;
795 e4bcb14c ths
796 9dfd7c7a Gerd Hoffmann
    *fatal_error = 1;
797 e4bcb14c ths
798 e4bcb14c ths
    translation = BIOS_ATA_TRANSLATION_AUTO;
799 e4bcb14c ths
800 4d007814 Gerd Hoffmann
    if (machine && machine->use_scsi) {
801 f60d39bc ths
        type = IF_SCSI;
802 e4bcb14c ths
        max_devs = MAX_SCSI_DEVS;
803 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "scsi");
804 e4bcb14c ths
    } else {
805 f60d39bc ths
        type = IF_IDE;
806 e4bcb14c ths
        max_devs = MAX_IDE_DEVS;
807 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "ide");
808 e4bcb14c ths
    }
809 e4bcb14c ths
    media = MEDIA_DISK;
810 e4bcb14c ths
811 e4bcb14c ths
    /* extract parameters */
812 9dfd7c7a Gerd Hoffmann
    bus_id  = qemu_opt_get_number(opts, "bus", 0);
813 9dfd7c7a Gerd Hoffmann
    unit_id = qemu_opt_get_number(opts, "unit", -1);
814 9dfd7c7a Gerd Hoffmann
    index   = qemu_opt_get_number(opts, "index", -1);
815 e4bcb14c ths
816 9dfd7c7a Gerd Hoffmann
    cyls  = qemu_opt_get_number(opts, "cyls", 0);
817 9dfd7c7a Gerd Hoffmann
    heads = qemu_opt_get_number(opts, "heads", 0);
818 9dfd7c7a Gerd Hoffmann
    secs  = qemu_opt_get_number(opts, "secs", 0);
819 e4bcb14c ths
820 9dfd7c7a Gerd Hoffmann
    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
821 59f2689d Naphtali Sprei
    ro = qemu_opt_get_bool(opts, "readonly", 0);
822 e4bcb14c ths
823 9dfd7c7a Gerd Hoffmann
    file = qemu_opt_get(opts, "file");
824 9dfd7c7a Gerd Hoffmann
    serial = qemu_opt_get(opts, "serial");
825 9dfd7c7a Gerd Hoffmann
826 9dfd7c7a Gerd Hoffmann
    if ((buf = qemu_opt_get(opts, "if")) != NULL) {
827 ae45d369 bellard
        pstrcpy(devname, sizeof(devname), buf);
828 e4bcb14c ths
        if (!strcmp(buf, "ide")) {
829 f60d39bc ths
            type = IF_IDE;
830 e4bcb14c ths
            max_devs = MAX_IDE_DEVS;
831 e4bcb14c ths
        } else if (!strcmp(buf, "scsi")) {
832 f60d39bc ths
            type = IF_SCSI;
833 e4bcb14c ths
            max_devs = MAX_SCSI_DEVS;
834 e4bcb14c ths
        } else if (!strcmp(buf, "floppy")) {
835 f60d39bc ths
            type = IF_FLOPPY;
836 e4bcb14c ths
            max_devs = 0;
837 e4bcb14c ths
        } else if (!strcmp(buf, "pflash")) {
838 f60d39bc ths
            type = IF_PFLASH;
839 e4bcb14c ths
            max_devs = 0;
840 e4bcb14c ths
        } else if (!strcmp(buf, "mtd")) {
841 f60d39bc ths
            type = IF_MTD;
842 e4bcb14c ths
            max_devs = 0;
843 e4bcb14c ths
        } else if (!strcmp(buf, "sd")) {
844 f60d39bc ths
            type = IF_SD;
845 e4bcb14c ths
            max_devs = 0;
846 6e02c38d aliguori
        } else if (!strcmp(buf, "virtio")) {
847 6e02c38d aliguori
            type = IF_VIRTIO;
848 6e02c38d aliguori
            max_devs = 0;
849 62d23efa aliguori
        } else if (!strcmp(buf, "xen")) {
850 62d23efa aliguori
            type = IF_XEN;
851 62d23efa aliguori
            max_devs = 0;
852 a8659e90 Gerd Hoffmann
        } else if (!strcmp(buf, "none")) {
853 a8659e90 Gerd Hoffmann
            type = IF_NONE;
854 a8659e90 Gerd Hoffmann
            max_devs = 0;
855 62d23efa aliguori
        } else {
856 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
857 751c6a17 Gerd Hoffmann
            return NULL;
858 e4bcb14c ths
        }
859 e4bcb14c ths
    }
860 e4bcb14c ths
861 e4bcb14c ths
    if (cyls || heads || secs) {
862 5afe3f04 Blue Swirl
        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
863 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
864 751c6a17 Gerd Hoffmann
            return NULL;
865 e4bcb14c ths
        }
866 5afe3f04 Blue Swirl
        if (heads < 1 || (type == IF_IDE && heads > 16)) {
867 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
868 751c6a17 Gerd Hoffmann
            return NULL;
869 e4bcb14c ths
        }
870 5afe3f04 Blue Swirl
        if (secs < 1 || (type == IF_IDE && secs > 63)) {
871 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
872 751c6a17 Gerd Hoffmann
            return NULL;
873 e4bcb14c ths
        }
874 e4bcb14c ths
    }
875 e4bcb14c ths
876 9dfd7c7a Gerd Hoffmann
    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
877 e4bcb14c ths
        if (!cyls) {
878 e4bcb14c ths
            fprintf(stderr,
879 e4bcb14c ths
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
880 9dfd7c7a Gerd Hoffmann
                    buf);
881 751c6a17 Gerd Hoffmann
            return NULL;
882 e4bcb14c ths
        }
883 e4bcb14c ths
        if (!strcmp(buf, "none"))
884 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_NONE;
885 e4bcb14c ths
        else if (!strcmp(buf, "lba"))
886 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_LBA;
887 e4bcb14c ths
        else if (!strcmp(buf, "auto"))
888 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_AUTO;
889 e4bcb14c ths
        else {
890 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
891 751c6a17 Gerd Hoffmann
            return NULL;
892 e4bcb14c ths
        }
893 e4bcb14c ths
    }
894 e4bcb14c ths
895 9dfd7c7a Gerd Hoffmann
    if ((buf = qemu_opt_get(opts, "media")) != NULL) {
896 e4bcb14c ths
        if (!strcmp(buf, "disk")) {
897 e4bcb14c ths
            media = MEDIA_DISK;
898 e4bcb14c ths
        } else if (!strcmp(buf, "cdrom")) {
899 e4bcb14c ths
            if (cyls || secs || heads) {
900 e4bcb14c ths
                fprintf(stderr,
901 9dfd7c7a Gerd Hoffmann
                        "qemu: '%s' invalid physical CHS format\n", buf);
902 751c6a17 Gerd Hoffmann
                return NULL;
903 e4bcb14c ths
            }
904 e4bcb14c ths
            media = MEDIA_CDROM;
905 e4bcb14c ths
        } else {
906 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "qemu: '%s' invalid media\n", buf);
907 751c6a17 Gerd Hoffmann
            return NULL;
908 e4bcb14c ths
        }
909 e4bcb14c ths
    }
910 e4bcb14c ths
911 9dfd7c7a Gerd Hoffmann
    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
912 763b6084 Christoph Hellwig
        if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
913 763b6084 Christoph Hellwig
            bdrv_flags |= BDRV_O_NOCACHE;
914 763b6084 Christoph Hellwig
        } else if (!strcmp(buf, "writeback")) {
915 763b6084 Christoph Hellwig
            bdrv_flags |= BDRV_O_CACHE_WB;
916 016f5cf6 Alexander Graf
        } else if (!strcmp(buf, "unsafe")) {
917 016f5cf6 Alexander Graf
            bdrv_flags |= BDRV_O_CACHE_WB;
918 016f5cf6 Alexander Graf
            bdrv_flags |= BDRV_O_NO_FLUSH;
919 763b6084 Christoph Hellwig
        } else if (!strcmp(buf, "writethrough")) {
920 763b6084 Christoph Hellwig
            /* this is the default */
921 763b6084 Christoph Hellwig
        } else {
922 33f00271 balrog
           fprintf(stderr, "qemu: invalid cache option\n");
923 751c6a17 Gerd Hoffmann
           return NULL;
924 33f00271 balrog
        }
925 33f00271 balrog
    }
926 33f00271 balrog
927 5c6c3a6c Christoph Hellwig
#ifdef CONFIG_LINUX_AIO
928 5c6c3a6c Christoph Hellwig
    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
929 763b6084 Christoph Hellwig
        if (!strcmp(buf, "native")) {
930 763b6084 Christoph Hellwig
            bdrv_flags |= BDRV_O_NATIVE_AIO;
931 763b6084 Christoph Hellwig
        } else if (!strcmp(buf, "threads")) {
932 763b6084 Christoph Hellwig
            /* this is the default */
933 763b6084 Christoph Hellwig
        } else {
934 5c6c3a6c Christoph Hellwig
           fprintf(stderr, "qemu: invalid aio option\n");
935 5c6c3a6c Christoph Hellwig
           return NULL;
936 5c6c3a6c Christoph Hellwig
        }
937 5c6c3a6c Christoph Hellwig
    }
938 5c6c3a6c Christoph Hellwig
#endif
939 5c6c3a6c Christoph Hellwig
940 9dfd7c7a Gerd Hoffmann
    if ((buf = qemu_opt_get(opts, "format")) != NULL) {
941 a1620fac aurel32
       if (strcmp(buf, "?") == 0) {
942 a1620fac aurel32
            fprintf(stderr, "qemu: Supported formats:");
943 a1620fac aurel32
            bdrv_iterate_format(bdrv_format_print, NULL);
944 a1620fac aurel32
            fprintf(stderr, "\n");
945 751c6a17 Gerd Hoffmann
            return NULL;
946 a1620fac aurel32
        }
947 eb852011 Markus Armbruster
        drv = bdrv_find_whitelisted_format(buf);
948 1e72d3b7 aurel32
        if (!drv) {
949 1e72d3b7 aurel32
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
950 751c6a17 Gerd Hoffmann
            return NULL;
951 1e72d3b7 aurel32
        }
952 1e72d3b7 aurel32
    }
953 1e72d3b7 aurel32
954 e9b2e818 Kevin Wolf
    on_write_error = BLOCK_ERR_STOP_ENOSPC;
955 9dfd7c7a Gerd Hoffmann
    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
956 dc33bb34 Gerd Hoffmann
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
957 ea8a5d7f aliguori
            fprintf(stderr, "werror is no supported by this format\n");
958 751c6a17 Gerd Hoffmann
            return NULL;
959 428c5705 aliguori
        }
960 e9b2e818 Kevin Wolf
961 e9b2e818 Kevin Wolf
        on_write_error = parse_block_error_action(buf, 0);
962 e9b2e818 Kevin Wolf
        if (on_write_error < 0) {
963 e9b2e818 Kevin Wolf
            return NULL;
964 e9b2e818 Kevin Wolf
        }
965 e9b2e818 Kevin Wolf
    }
966 e9b2e818 Kevin Wolf
967 e9b2e818 Kevin Wolf
    on_read_error = BLOCK_ERR_REPORT;
968 e9b2e818 Kevin Wolf
    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
969 dc33bb34 Gerd Hoffmann
        if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
970 e9b2e818 Kevin Wolf
            fprintf(stderr, "rerror is no supported by this format\n");
971 e9b2e818 Kevin Wolf
            return NULL;
972 e9b2e818 Kevin Wolf
        }
973 e9b2e818 Kevin Wolf
974 e9b2e818 Kevin Wolf
        on_read_error = parse_block_error_action(buf, 1);
975 e9b2e818 Kevin Wolf
        if (on_read_error < 0) {
976 751c6a17 Gerd Hoffmann
            return NULL;
977 428c5705 aliguori
        }
978 428c5705 aliguori
    }
979 428c5705 aliguori
980 9dfd7c7a Gerd Hoffmann
    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
981 c2cc47a4 Markus Armbruster
        if (type != IF_VIRTIO) {
982 9dfd7c7a Gerd Hoffmann
            fprintf(stderr, "addr is not supported\n");
983 751c6a17 Gerd Hoffmann
            return NULL;
984 c2cc47a4 Markus Armbruster
        }
985 c2cc47a4 Markus Armbruster
    }
986 c2cc47a4 Markus Armbruster
987 e4bcb14c ths
    /* compute bus and unit according index */
988 e4bcb14c ths
989 e4bcb14c ths
    if (index != -1) {
990 e4bcb14c ths
        if (bus_id != 0 || unit_id != -1) {
991 e4bcb14c ths
            fprintf(stderr,
992 9dfd7c7a Gerd Hoffmann
                    "qemu: index cannot be used with bus and unit\n");
993 751c6a17 Gerd Hoffmann
            return NULL;
994 e4bcb14c ths
        }
995 e4bcb14c ths
        if (max_devs == 0)
996 e4bcb14c ths
        {
997 e4bcb14c ths
            unit_id = index;
998 e4bcb14c ths
            bus_id = 0;
999 e4bcb14c ths
        } else {
1000 e4bcb14c ths
            unit_id = index % max_devs;
1001 e4bcb14c ths
            bus_id = index / max_devs;
1002 e4bcb14c ths
        }
1003 e4bcb14c ths
    }
1004 e4bcb14c ths
1005 e4bcb14c ths
    /* if user doesn't specify a unit_id,
1006 e4bcb14c ths
     * try to find the first free
1007 e4bcb14c ths
     */
1008 e4bcb14c ths
1009 e4bcb14c ths
    if (unit_id == -1) {
1010 e4bcb14c ths
       unit_id = 0;
1011 751c6a17 Gerd Hoffmann
       while (drive_get(type, bus_id, unit_id) != NULL) {
1012 e4bcb14c ths
           unit_id++;
1013 e4bcb14c ths
           if (max_devs && unit_id >= max_devs) {
1014 e4bcb14c ths
               unit_id -= max_devs;
1015 e4bcb14c ths
               bus_id++;
1016 e4bcb14c ths
           }
1017 e4bcb14c ths
       }
1018 e4bcb14c ths
    }
1019 e4bcb14c ths
1020 e4bcb14c ths
    /* check unit id */
1021 e4bcb14c ths
1022 e4bcb14c ths
    if (max_devs && unit_id >= max_devs) {
1023 9dfd7c7a Gerd Hoffmann
        fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1024 9dfd7c7a Gerd Hoffmann
                unit_id, max_devs - 1);
1025 751c6a17 Gerd Hoffmann
        return NULL;
1026 e4bcb14c ths
    }
1027 e4bcb14c ths
1028 e4bcb14c ths
    /*
1029 e4bcb14c ths
     * ignore multiple definitions
1030 e4bcb14c ths
     */
1031 e4bcb14c ths
1032 751c6a17 Gerd Hoffmann
    if (drive_get(type, bus_id, unit_id) != NULL) {
1033 751c6a17 Gerd Hoffmann
        *fatal_error = 0;
1034 751c6a17 Gerd Hoffmann
        return NULL;
1035 751c6a17 Gerd Hoffmann
    }
1036 e4bcb14c ths
1037 e4bcb14c ths
    /* init */
1038 e4bcb14c ths
1039 751c6a17 Gerd Hoffmann
    dinfo = qemu_mallocz(sizeof(*dinfo));
1040 e23d9c4d Gerd Hoffmann
    if ((buf = qemu_opts_id(opts)) != NULL) {
1041 9dfd7c7a Gerd Hoffmann
        dinfo->id = qemu_strdup(buf);
1042 9dfd7c7a Gerd Hoffmann
    } else {
1043 1dae12e6 Gerd Hoffmann
        /* no id supplied -> create one */
1044 9dfd7c7a Gerd Hoffmann
        dinfo->id = qemu_mallocz(32);
1045 1dae12e6 Gerd Hoffmann
        if (type == IF_IDE || type == IF_SCSI)
1046 1dae12e6 Gerd Hoffmann
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1047 1dae12e6 Gerd Hoffmann
        if (max_devs)
1048 9dfd7c7a Gerd Hoffmann
            snprintf(dinfo->id, 32, "%s%i%s%i",
1049 1dae12e6 Gerd Hoffmann
                     devname, bus_id, mediastr, unit_id);
1050 1dae12e6 Gerd Hoffmann
        else
1051 9dfd7c7a Gerd Hoffmann
            snprintf(dinfo->id, 32, "%s%s%i",
1052 1dae12e6 Gerd Hoffmann
                     devname, mediastr, unit_id);
1053 1dae12e6 Gerd Hoffmann
    }
1054 1dae12e6 Gerd Hoffmann
    dinfo->bdrv = bdrv_new(dinfo->id);
1055 751c6a17 Gerd Hoffmann
    dinfo->devaddr = devaddr;
1056 751c6a17 Gerd Hoffmann
    dinfo->type = type;
1057 751c6a17 Gerd Hoffmann
    dinfo->bus = bus_id;
1058 751c6a17 Gerd Hoffmann
    dinfo->unit = unit_id;
1059 e9b2e818 Kevin Wolf
    dinfo->on_read_error = on_read_error;
1060 e9b2e818 Kevin Wolf
    dinfo->on_write_error = on_write_error;
1061 9dfd7c7a Gerd Hoffmann
    dinfo->opts = opts;
1062 9dfd7c7a Gerd Hoffmann
    if (serial)
1063 9dfd7c7a Gerd Hoffmann
        strncpy(dinfo->serial, serial, sizeof(serial));
1064 72cf2d4f Blue Swirl
    QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1065 e4bcb14c ths
1066 f60d39bc ths
    switch(type) {
1067 e4bcb14c ths
    case IF_IDE:
1068 e4bcb14c ths
    case IF_SCSI:
1069 62d23efa aliguori
    case IF_XEN:
1070 c219331e Gerd Hoffmann
    case IF_NONE:
1071 e4bcb14c ths
        switch(media) {
1072 e4bcb14c ths
        case MEDIA_DISK:
1073 e4bcb14c ths
            if (cyls != 0) {
1074 1dae12e6 Gerd Hoffmann
                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1075 1dae12e6 Gerd Hoffmann
                bdrv_set_translation_hint(dinfo->bdrv, translation);
1076 e4bcb14c ths
            }
1077 e4bcb14c ths
            break;
1078 e4bcb14c ths
        case MEDIA_CDROM:
1079 1dae12e6 Gerd Hoffmann
            bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1080 e4bcb14c ths
            break;
1081 e4bcb14c ths
        }
1082 e4bcb14c ths
        break;
1083 e4bcb14c ths
    case IF_SD:
1084 e4bcb14c ths
        /* FIXME: This isn't really a floppy, but it's a reasonable
1085 e4bcb14c ths
           approximation.  */
1086 e4bcb14c ths
    case IF_FLOPPY:
1087 1dae12e6 Gerd Hoffmann
        bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1088 e4bcb14c ths
        break;
1089 e4bcb14c ths
    case IF_PFLASH:
1090 e4bcb14c ths
    case IF_MTD:
1091 e4bcb14c ths
        break;
1092 d176c495 Gerd Hoffmann
    case IF_VIRTIO:
1093 d176c495 Gerd Hoffmann
        /* add virtio block device */
1094 d176c495 Gerd Hoffmann
        opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1095 d176c495 Gerd Hoffmann
        qemu_opt_set(opts, "driver", "virtio-blk-pci");
1096 d176c495 Gerd Hoffmann
        qemu_opt_set(opts, "drive", dinfo->id);
1097 d176c495 Gerd Hoffmann
        if (devaddr)
1098 d176c495 Gerd Hoffmann
            qemu_opt_set(opts, "addr", devaddr);
1099 d176c495 Gerd Hoffmann
        break;
1100 aae9460e Paul Brook
    case IF_COUNT:
1101 aae9460e Paul Brook
        abort();
1102 e4bcb14c ths
    }
1103 9dfd7c7a Gerd Hoffmann
    if (!file) {
1104 751c6a17 Gerd Hoffmann
        *fatal_error = 0;
1105 751c6a17 Gerd Hoffmann
        return NULL;
1106 751c6a17 Gerd Hoffmann
    }
1107 9f7965c7 aliguori
    if (snapshot) {
1108 c3177288 Alexander Graf
        /* always use cache=unsafe with snapshot */
1109 763b6084 Christoph Hellwig
        bdrv_flags &= ~BDRV_O_CACHE_MASK;
1110 c3177288 Alexander Graf
        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
1111 5c6c3a6c Christoph Hellwig
    }
1112 5c6c3a6c Christoph Hellwig
1113 2db7ad59 Naphtali Sprei
    if (media == MEDIA_CDROM) {
1114 2db7ad59 Naphtali Sprei
        /* CDROM is fine for any interface, don't check.  */
1115 2db7ad59 Naphtali Sprei
        ro = 1;
1116 2db7ad59 Naphtali Sprei
    } else if (ro == 1) {
1117 dc33bb34 Gerd Hoffmann
        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1118 f5edb014 Naphtali Sprei
            fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1119 59f2689d Naphtali Sprei
            return NULL;
1120 59f2689d Naphtali Sprei
        }
1121 b196b153 Naphtali Sprei
    }
1122 2db7ad59 Naphtali Sprei
1123 f5edb014 Naphtali Sprei
    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1124 59f2689d Naphtali Sprei
1125 d6e9098e Kevin Wolf
    if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
1126 850810d0 Justin M. Forbes
        fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1127 850810d0 Justin M. Forbes
                        file, strerror(errno));
1128 751c6a17 Gerd Hoffmann
        return NULL;
1129 e4bcb14c ths
    }
1130 5c6c3a6c Christoph Hellwig
1131 1dae12e6 Gerd Hoffmann
    if (bdrv_key_required(dinfo->bdrv))
1132 c0f4ce77 aliguori
        autostart = 0;
1133 751c6a17 Gerd Hoffmann
    *fatal_error = 0;
1134 751c6a17 Gerd Hoffmann
    return dinfo;
1135 e4bcb14c ths
}
1136 e4bcb14c ths
1137 9dfd7c7a Gerd Hoffmann
static int drive_init_func(QemuOpts *opts, void *opaque)
1138 9dfd7c7a Gerd Hoffmann
{
1139 9dfd7c7a Gerd Hoffmann
    QEMUMachine *machine = opaque;
1140 9dfd7c7a Gerd Hoffmann
    int fatal_error = 0;
1141 9dfd7c7a Gerd Hoffmann
1142 9dfd7c7a Gerd Hoffmann
    if (drive_init(opts, machine, &fatal_error) == NULL) {
1143 9dfd7c7a Gerd Hoffmann
        if (fatal_error)
1144 9dfd7c7a Gerd Hoffmann
            return 1;
1145 9dfd7c7a Gerd Hoffmann
    }
1146 9dfd7c7a Gerd Hoffmann
    return 0;
1147 9dfd7c7a Gerd Hoffmann
}
1148 9dfd7c7a Gerd Hoffmann
1149 9dfd7c7a Gerd Hoffmann
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1150 9dfd7c7a Gerd Hoffmann
{
1151 9dfd7c7a Gerd Hoffmann
    if (NULL == qemu_opt_get(opts, "snapshot")) {
1152 9dfd7c7a Gerd Hoffmann
        qemu_opt_set(opts, "snapshot", "on");
1153 9dfd7c7a Gerd Hoffmann
    }
1154 9dfd7c7a Gerd Hoffmann
    return 0;
1155 9dfd7c7a Gerd Hoffmann
}
1156 9dfd7c7a Gerd Hoffmann
1157 76e30d0f Jan Kiszka
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1158 76e30d0f Jan Kiszka
{
1159 76e30d0f Jan Kiszka
    boot_set_handler = func;
1160 76e30d0f Jan Kiszka
    boot_set_opaque = opaque;
1161 76e30d0f Jan Kiszka
}
1162 76e30d0f Jan Kiszka
1163 76e30d0f Jan Kiszka
int qemu_boot_set(const char *boot_devices)
1164 76e30d0f Jan Kiszka
{
1165 76e30d0f Jan Kiszka
    if (!boot_set_handler) {
1166 76e30d0f Jan Kiszka
        return -EINVAL;
1167 76e30d0f Jan Kiszka
    }
1168 76e30d0f Jan Kiszka
    return boot_set_handler(boot_set_opaque, boot_devices);
1169 76e30d0f Jan Kiszka
}
1170 76e30d0f Jan Kiszka
1171 4e9e9d6e Eduardo Habkost
static void validate_bootdevices(char *devices)
1172 ef3adf68 Jan Kiszka
{
1173 ef3adf68 Jan Kiszka
    /* We just do some generic consistency checks */
1174 ef3adf68 Jan Kiszka
    const char *p;
1175 ef3adf68 Jan Kiszka
    int bitmap = 0;
1176 ef3adf68 Jan Kiszka
1177 ef3adf68 Jan Kiszka
    for (p = devices; *p != '\0'; p++) {
1178 ef3adf68 Jan Kiszka
        /* Allowed boot devices are:
1179 ef3adf68 Jan Kiszka
         * a-b: floppy disk drives
1180 ef3adf68 Jan Kiszka
         * c-f: IDE disk drives
1181 ef3adf68 Jan Kiszka
         * g-m: machine implementation dependant drives
1182 ef3adf68 Jan Kiszka
         * n-p: network devices
1183 ef3adf68 Jan Kiszka
         * It's up to each machine implementation to check if the given boot
1184 ef3adf68 Jan Kiszka
         * devices match the actual hardware implementation and firmware
1185 ef3adf68 Jan Kiszka
         * features.
1186 ef3adf68 Jan Kiszka
         */
1187 ef3adf68 Jan Kiszka
        if (*p < 'a' || *p > 'p') {
1188 ef3adf68 Jan Kiszka
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
1189 ef3adf68 Jan Kiszka
            exit(1);
1190 ef3adf68 Jan Kiszka
        }
1191 ef3adf68 Jan Kiszka
        if (bitmap & (1 << (*p - 'a'))) {
1192 ef3adf68 Jan Kiszka
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1193 ef3adf68 Jan Kiszka
            exit(1);
1194 ef3adf68 Jan Kiszka
        }
1195 ef3adf68 Jan Kiszka
        bitmap |= 1 << (*p - 'a');
1196 ef3adf68 Jan Kiszka
    }
1197 ef3adf68 Jan Kiszka
}
1198 ef3adf68 Jan Kiszka
1199 e0f084bf Jan Kiszka
static void restore_boot_devices(void *opaque)
1200 e0f084bf Jan Kiszka
{
1201 e0f084bf Jan Kiszka
    char *standard_boot_devices = opaque;
1202 37905d6a Alex Williamson
    static int first = 1;
1203 37905d6a Alex Williamson
1204 37905d6a Alex Williamson
    /* Restore boot order and remove ourselves after the first boot */
1205 37905d6a Alex Williamson
    if (first) {
1206 37905d6a Alex Williamson
        first = 0;
1207 37905d6a Alex Williamson
        return;
1208 37905d6a Alex Williamson
    }
1209 e0f084bf Jan Kiszka
1210 e0f084bf Jan Kiszka
    qemu_boot_set(standard_boot_devices);
1211 e0f084bf Jan Kiszka
1212 e0f084bf Jan Kiszka
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1213 e0f084bf Jan Kiszka
    qemu_free(standard_boot_devices);
1214 e0f084bf Jan Kiszka
}
1215 e0f084bf Jan Kiszka
1216 268a362c aliguori
static void numa_add(const char *optarg)
1217 268a362c aliguori
{
1218 268a362c aliguori
    char option[128];
1219 268a362c aliguori
    char *endptr;
1220 268a362c aliguori
    unsigned long long value, endvalue;
1221 268a362c aliguori
    int nodenr;
1222 268a362c aliguori
1223 268a362c aliguori
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
1224 268a362c aliguori
    if (!strcmp(option, "node")) {
1225 268a362c aliguori
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1226 268a362c aliguori
            nodenr = nb_numa_nodes;
1227 268a362c aliguori
        } else {
1228 268a362c aliguori
            nodenr = strtoull(option, NULL, 10);
1229 268a362c aliguori
        }
1230 268a362c aliguori
1231 268a362c aliguori
        if (get_param_value(option, 128, "mem", optarg) == 0) {
1232 268a362c aliguori
            node_mem[nodenr] = 0;
1233 268a362c aliguori
        } else {
1234 268a362c aliguori
            value = strtoull(option, &endptr, 0);
1235 268a362c aliguori
            switch (*endptr) {
1236 268a362c aliguori
            case 0: case 'M': case 'm':
1237 268a362c aliguori
                value <<= 20;
1238 268a362c aliguori
                break;
1239 268a362c aliguori
            case 'G': case 'g':
1240 268a362c aliguori
                value <<= 30;
1241 268a362c aliguori
                break;
1242 268a362c aliguori
            }
1243 268a362c aliguori
            node_mem[nodenr] = value;
1244 268a362c aliguori
        }
1245 268a362c aliguori
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
1246 268a362c aliguori
            node_cpumask[nodenr] = 0;
1247 268a362c aliguori
        } else {
1248 268a362c aliguori
            value = strtoull(option, &endptr, 10);
1249 268a362c aliguori
            if (value >= 64) {
1250 268a362c aliguori
                value = 63;
1251 268a362c aliguori
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1252 268a362c aliguori
            } else {
1253 268a362c aliguori
                if (*endptr == '-') {
1254 268a362c aliguori
                    endvalue = strtoull(endptr+1, &endptr, 10);
1255 268a362c aliguori
                    if (endvalue >= 63) {
1256 268a362c aliguori
                        endvalue = 62;
1257 268a362c aliguori
                        fprintf(stderr,
1258 268a362c aliguori
                            "only 63 CPUs in NUMA mode supported.\n");
1259 268a362c aliguori
                    }
1260 0dfbd514 Paolo Bonzini
                    value = (2ULL << endvalue) - (1ULL << value);
1261 268a362c aliguori
                } else {
1262 0dfbd514 Paolo Bonzini
                    value = 1ULL << value;
1263 268a362c aliguori
                }
1264 268a362c aliguori
            }
1265 268a362c aliguori
            node_cpumask[nodenr] = value;
1266 268a362c aliguori
        }
1267 268a362c aliguori
        nb_numa_nodes++;
1268 268a362c aliguori
    }
1269 268a362c aliguori
    return;
1270 268a362c aliguori
}
1271 268a362c aliguori
1272 dc6b1c09 Andre Przywara
static void smp_parse(const char *optarg)
1273 dc6b1c09 Andre Przywara
{
1274 dc6b1c09 Andre Przywara
    int smp, sockets = 0, threads = 0, cores = 0;
1275 dc6b1c09 Andre Przywara
    char *endptr;
1276 dc6b1c09 Andre Przywara
    char option[128];
1277 dc6b1c09 Andre Przywara
1278 dc6b1c09 Andre Przywara
    smp = strtoul(optarg, &endptr, 10);
1279 dc6b1c09 Andre Przywara
    if (endptr != optarg) {
1280 dc6b1c09 Andre Przywara
        if (*endptr == ',') {
1281 dc6b1c09 Andre Przywara
            endptr++;
1282 dc6b1c09 Andre Przywara
        }
1283 dc6b1c09 Andre Przywara
    }
1284 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "sockets", endptr) != 0)
1285 dc6b1c09 Andre Przywara
        sockets = strtoull(option, NULL, 10);
1286 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "cores", endptr) != 0)
1287 dc6b1c09 Andre Przywara
        cores = strtoull(option, NULL, 10);
1288 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "threads", endptr) != 0)
1289 dc6b1c09 Andre Przywara
        threads = strtoull(option, NULL, 10);
1290 dc6b1c09 Andre Przywara
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1291 dc6b1c09 Andre Przywara
        max_cpus = strtoull(option, NULL, 10);
1292 dc6b1c09 Andre Przywara
1293 dc6b1c09 Andre Przywara
    /* compute missing values, prefer sockets over cores over threads */
1294 dc6b1c09 Andre Przywara
    if (smp == 0 || sockets == 0) {
1295 dc6b1c09 Andre Przywara
        sockets = sockets > 0 ? sockets : 1;
1296 dc6b1c09 Andre Przywara
        cores = cores > 0 ? cores : 1;
1297 dc6b1c09 Andre Przywara
        threads = threads > 0 ? threads : 1;
1298 dc6b1c09 Andre Przywara
        if (smp == 0) {
1299 dc6b1c09 Andre Przywara
            smp = cores * threads * sockets;
1300 dc6b1c09 Andre Przywara
        }
1301 dc6b1c09 Andre Przywara
    } else {
1302 dc6b1c09 Andre Przywara
        if (cores == 0) {
1303 dc6b1c09 Andre Przywara
            threads = threads > 0 ? threads : 1;
1304 dc6b1c09 Andre Przywara
            cores = smp / (sockets * threads);
1305 dc6b1c09 Andre Przywara
        } else {
1306 5cdc9b76 Amit Shah
            if (sockets) {
1307 dc6b1c09 Andre Przywara
                threads = smp / (cores * sockets);
1308 dc6b1c09 Andre Przywara
            }
1309 dc6b1c09 Andre Przywara
        }
1310 dc6b1c09 Andre Przywara
    }
1311 dc6b1c09 Andre Przywara
    smp_cpus = smp;
1312 dc6b1c09 Andre Przywara
    smp_cores = cores > 0 ? cores : 1;
1313 dc6b1c09 Andre Przywara
    smp_threads = threads > 0 ? threads : 1;
1314 dc6b1c09 Andre Przywara
    if (max_cpus == 0)
1315 dc6b1c09 Andre Przywara
        max_cpus = smp_cpus;
1316 dc6b1c09 Andre Przywara
}
1317 dc6b1c09 Andre Przywara
1318 330d0414 bellard
/***********************************************************/
1319 a594cfbf bellard
/* USB devices */
1320 a594cfbf bellard
1321 c0f4ce77 aliguori
static int usb_device_add(const char *devname, int is_hotplug)
1322 a594cfbf bellard
{
1323 a594cfbf bellard
    const char *p;
1324 a5d2f727 Gerd Hoffmann
    USBDevice *dev = NULL;
1325 a594cfbf bellard
1326 a5d2f727 Gerd Hoffmann
    if (!usb_enabled)
1327 a594cfbf bellard
        return -1;
1328 a594cfbf bellard
1329 0958b4cc Gerd Hoffmann
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
1330 0958b4cc Gerd Hoffmann
    dev = usbdevice_create(devname);
1331 0958b4cc Gerd Hoffmann
    if (dev)
1332 0958b4cc Gerd Hoffmann
        goto done;
1333 0958b4cc Gerd Hoffmann
1334 a5d2f727 Gerd Hoffmann
    /* the other ones */
1335 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
1336 a594cfbf bellard
        dev = usb_host_device_open(p);
1337 dc72ac14 balrog
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1338 dc72ac14 balrog
        dev = usb_bt_init(devname[2] ? hci_init(p) :
1339 dc72ac14 balrog
                        bt_new_hci(qemu_find_bt_vlan(0)));
1340 a594cfbf bellard
    } else {
1341 a594cfbf bellard
        return -1;
1342 a594cfbf bellard
    }
1343 0d92ed30 pbrook
    if (!dev)
1344 0d92ed30 pbrook
        return -1;
1345 0d92ed30 pbrook
1346 a5d2f727 Gerd Hoffmann
done:
1347 a594cfbf bellard
    return 0;
1348 a594cfbf bellard
}
1349 a594cfbf bellard
1350 1f3870ab aliguori
static int usb_device_del(const char *devname)
1351 1f3870ab aliguori
{
1352 1f3870ab aliguori
    int bus_num, addr;
1353 1f3870ab aliguori
    const char *p;
1354 1f3870ab aliguori
1355 5d0c5750 aliguori
    if (strstart(devname, "host:", &p))
1356 5d0c5750 aliguori
        return usb_host_device_close(p);
1357 5d0c5750 aliguori
1358 a5d2f727 Gerd Hoffmann
    if (!usb_enabled)
1359 1f3870ab aliguori
        return -1;
1360 1f3870ab aliguori
1361 1f3870ab aliguori
    p = strchr(devname, '.');
1362 1f3870ab aliguori
    if (!p)
1363 1f3870ab aliguori
        return -1;
1364 1f3870ab aliguori
    bus_num = strtoul(devname, NULL, 0);
1365 1f3870ab aliguori
    addr = strtoul(p + 1, NULL, 0);
1366 1f3870ab aliguori
1367 a5d2f727 Gerd Hoffmann
    return usb_device_delete_addr(bus_num, addr);
1368 1f3870ab aliguori
}
1369 1f3870ab aliguori
1370 bd3c948d Gerd Hoffmann
static int usb_parse(const char *cmdline)
1371 bd3c948d Gerd Hoffmann
{
1372 59d1c1c2 Scott Tsai
    int r;
1373 59d1c1c2 Scott Tsai
    r = usb_device_add(cmdline, 0);
1374 59d1c1c2 Scott Tsai
    if (r < 0) {
1375 59d1c1c2 Scott Tsai
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1376 59d1c1c2 Scott Tsai
    }
1377 59d1c1c2 Scott Tsai
    return r;
1378 bd3c948d Gerd Hoffmann
}
1379 bd3c948d Gerd Hoffmann
1380 d54908a5 Luiz Capitulino
void do_usb_add(Monitor *mon, const QDict *qdict)
1381 a594cfbf bellard
{
1382 59d1c1c2 Scott Tsai
    const char *devname = qdict_get_str(qdict, "devname");
1383 59d1c1c2 Scott Tsai
    if (usb_device_add(devname, 1) < 0) {
1384 1ecda02b Markus Armbruster
        error_report("could not add USB device '%s'", devname);
1385 59d1c1c2 Scott Tsai
    }
1386 a594cfbf bellard
}
1387 a594cfbf bellard
1388 d54908a5 Luiz Capitulino
void do_usb_del(Monitor *mon, const QDict *qdict)
1389 a594cfbf bellard
{
1390 59d1c1c2 Scott Tsai
    const char *devname = qdict_get_str(qdict, "devname");
1391 59d1c1c2 Scott Tsai
    if (usb_device_del(devname) < 0) {
1392 1ecda02b Markus Armbruster
        error_report("could not delete USB device '%s'", devname);
1393 59d1c1c2 Scott Tsai
    }
1394 a594cfbf bellard
}
1395 a594cfbf bellard
1396 f7cce898 bellard
/***********************************************************/
1397 201a51fc balrog
/* PCMCIA/Cardbus */
1398 201a51fc balrog
1399 201a51fc balrog
static struct pcmcia_socket_entry_s {
1400 bc24a225 Paul Brook
    PCMCIASocket *socket;
1401 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
1402 201a51fc balrog
} *pcmcia_sockets = 0;
1403 201a51fc balrog
1404 bc24a225 Paul Brook
void pcmcia_socket_register(PCMCIASocket *socket)
1405 201a51fc balrog
{
1406 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
1407 201a51fc balrog
1408 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1409 201a51fc balrog
    entry->socket = socket;
1410 201a51fc balrog
    entry->next = pcmcia_sockets;
1411 201a51fc balrog
    pcmcia_sockets = entry;
1412 201a51fc balrog
}
1413 201a51fc balrog
1414 bc24a225 Paul Brook
void pcmcia_socket_unregister(PCMCIASocket *socket)
1415 201a51fc balrog
{
1416 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
1417 201a51fc balrog
1418 201a51fc balrog
    ptr = &pcmcia_sockets;
1419 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1420 201a51fc balrog
        if (entry->socket == socket) {
1421 201a51fc balrog
            *ptr = entry->next;
1422 201a51fc balrog
            qemu_free(entry);
1423 201a51fc balrog
        }
1424 201a51fc balrog
}
1425 201a51fc balrog
1426 376253ec aliguori
void pcmcia_info(Monitor *mon)
1427 201a51fc balrog
{
1428 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
1429 376253ec aliguori
1430 201a51fc balrog
    if (!pcmcia_sockets)
1431 376253ec aliguori
        monitor_printf(mon, "No PCMCIA sockets\n");
1432 201a51fc balrog
1433 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
1434 376253ec aliguori
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1435 376253ec aliguori
                       iter->socket->attached ? iter->socket->card_string :
1436 376253ec aliguori
                       "Empty");
1437 201a51fc balrog
}
1438 201a51fc balrog
1439 201a51fc balrog
/***********************************************************/
1440 8a7ddc38 bellard
/* I/O handling */
1441 0824d6fc bellard
1442 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
1443 c4b1fcc0 bellard
    int fd;
1444 7b27a769 Juan Quintela
    IOCanReadHandler *fd_read_poll;
1445 7c9d8e07 bellard
    IOHandler *fd_read;
1446 7c9d8e07 bellard
    IOHandler *fd_write;
1447 cafffd40 ths
    int deleted;
1448 c4b1fcc0 bellard
    void *opaque;
1449 c4b1fcc0 bellard
    /* temporary data */
1450 c4b1fcc0 bellard
    struct pollfd *ufd;
1451 31d4ee6c Juan Quintela
    QLIST_ENTRY(IOHandlerRecord) next;
1452 c4b1fcc0 bellard
} IOHandlerRecord;
1453 c4b1fcc0 bellard
1454 31d4ee6c Juan Quintela
static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1455 31d4ee6c Juan Quintela
    QLIST_HEAD_INITIALIZER(io_handlers);
1456 31d4ee6c Juan Quintela
1457 c4b1fcc0 bellard
1458 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
1459 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
1460 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
1461 7b27a769 Juan Quintela
                         IOCanReadHandler *fd_read_poll,
1462 5fafdf24 ths
                         IOHandler *fd_read,
1463 5fafdf24 ths
                         IOHandler *fd_write,
1464 7c9d8e07 bellard
                         void *opaque)
1465 c4b1fcc0 bellard
{
1466 31d4ee6c Juan Quintela
    IOHandlerRecord *ioh;
1467 c4b1fcc0 bellard
1468 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
1469 31d4ee6c Juan Quintela
        QLIST_FOREACH(ioh, &io_handlers, next) {
1470 7c9d8e07 bellard
            if (ioh->fd == fd) {
1471 cafffd40 ths
                ioh->deleted = 1;
1472 7c9d8e07 bellard
                break;
1473 7c9d8e07 bellard
            }
1474 7c9d8e07 bellard
        }
1475 7c9d8e07 bellard
    } else {
1476 31d4ee6c Juan Quintela
        QLIST_FOREACH(ioh, &io_handlers, next) {
1477 7c9d8e07 bellard
            if (ioh->fd == fd)
1478 7c9d8e07 bellard
                goto found;
1479 7c9d8e07 bellard
        }
1480 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1481 31d4ee6c Juan Quintela
        QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1482 7c9d8e07 bellard
    found:
1483 7c9d8e07 bellard
        ioh->fd = fd;
1484 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
1485 7c9d8e07 bellard
        ioh->fd_read = fd_read;
1486 7c9d8e07 bellard
        ioh->fd_write = fd_write;
1487 7c9d8e07 bellard
        ioh->opaque = opaque;
1488 cafffd40 ths
        ioh->deleted = 0;
1489 7c9d8e07 bellard
    }
1490 c4b1fcc0 bellard
    return 0;
1491 c4b1fcc0 bellard
}
1492 c4b1fcc0 bellard
1493 5fafdf24 ths
int qemu_set_fd_handler(int fd,
1494 5fafdf24 ths
                        IOHandler *fd_read,
1495 5fafdf24 ths
                        IOHandler *fd_write,
1496 7c9d8e07 bellard
                        void *opaque)
1497 8a7ddc38 bellard
{
1498 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1499 8a7ddc38 bellard
}
1500 8a7ddc38 bellard
1501 56f3a5d0 aliguori
#ifdef _WIN32
1502 8a7ddc38 bellard
/***********************************************************/
1503 f331110f bellard
/* Polling handling */
1504 f331110f bellard
1505 f331110f bellard
typedef struct PollingEntry {
1506 f331110f bellard
    PollingFunc *func;
1507 f331110f bellard
    void *opaque;
1508 f331110f bellard
    struct PollingEntry *next;
1509 f331110f bellard
} PollingEntry;
1510 f331110f bellard
1511 f331110f bellard
static PollingEntry *first_polling_entry;
1512 f331110f bellard
1513 f331110f bellard
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1514 f331110f bellard
{
1515 f331110f bellard
    PollingEntry **ppe, *pe;
1516 f331110f bellard
    pe = qemu_mallocz(sizeof(PollingEntry));
1517 f331110f bellard
    pe->func = func;
1518 f331110f bellard
    pe->opaque = opaque;
1519 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1520 f331110f bellard
    *ppe = pe;
1521 f331110f bellard
    return 0;
1522 f331110f bellard
}
1523 f331110f bellard
1524 f331110f bellard
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1525 f331110f bellard
{
1526 f331110f bellard
    PollingEntry **ppe, *pe;
1527 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1528 f331110f bellard
        pe = *ppe;
1529 f331110f bellard
        if (pe->func == func && pe->opaque == opaque) {
1530 f331110f bellard
            *ppe = pe->next;
1531 f331110f bellard
            qemu_free(pe);
1532 f331110f bellard
            break;
1533 f331110f bellard
        }
1534 f331110f bellard
    }
1535 f331110f bellard
}
1536 f331110f bellard
1537 a18e524a bellard
/***********************************************************/
1538 a18e524a bellard
/* Wait objects support */
1539 a18e524a bellard
typedef struct WaitObjects {
1540 a18e524a bellard
    int num;
1541 a18e524a bellard
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1542 a18e524a bellard
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1543 a18e524a bellard
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1544 a18e524a bellard
} WaitObjects;
1545 a18e524a bellard
1546 a18e524a bellard
static WaitObjects wait_objects = {0};
1547 3b46e624 ths
1548 a18e524a bellard
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1549 a18e524a bellard
{
1550 a18e524a bellard
    WaitObjects *w = &wait_objects;
1551 a18e524a bellard
1552 a18e524a bellard
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
1553 a18e524a bellard
        return -1;
1554 a18e524a bellard
    w->events[w->num] = handle;
1555 a18e524a bellard
    w->func[w->num] = func;
1556 a18e524a bellard
    w->opaque[w->num] = opaque;
1557 a18e524a bellard
    w->num++;
1558 a18e524a bellard
    return 0;
1559 a18e524a bellard
}
1560 a18e524a bellard
1561 a18e524a bellard
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1562 a18e524a bellard
{
1563 a18e524a bellard
    int i, found;
1564 a18e524a bellard
    WaitObjects *w = &wait_objects;
1565 a18e524a bellard
1566 a18e524a bellard
    found = 0;
1567 a18e524a bellard
    for (i = 0; i < w->num; i++) {
1568 a18e524a bellard
        if (w->events[i] == handle)
1569 a18e524a bellard
            found = 1;
1570 a18e524a bellard
        if (found) {
1571 a18e524a bellard
            w->events[i] = w->events[i + 1];
1572 a18e524a bellard
            w->func[i] = w->func[i + 1];
1573 a18e524a bellard
            w->opaque[i] = w->opaque[i + 1];
1574 3b46e624 ths
        }
1575 a18e524a bellard
    }
1576 a18e524a bellard
    if (found)
1577 a18e524a bellard
        w->num--;
1578 a18e524a bellard
}
1579 a18e524a bellard
#endif
1580 a18e524a bellard
1581 8a7ddc38 bellard
/***********************************************************/
1582 cc1daa40 bellard
/* machine registration */
1583 cc1daa40 bellard
1584 bdaf78e0 blueswir1
static QEMUMachine *first_machine = NULL;
1585 6f338c34 aliguori
QEMUMachine *current_machine = NULL;
1586 cc1daa40 bellard
1587 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
1588 cc1daa40 bellard
{
1589 cc1daa40 bellard
    QEMUMachine **pm;
1590 cc1daa40 bellard
    pm = &first_machine;
1591 cc1daa40 bellard
    while (*pm != NULL)
1592 cc1daa40 bellard
        pm = &(*pm)->next;
1593 cc1daa40 bellard
    m->next = NULL;
1594 cc1daa40 bellard
    *pm = m;
1595 cc1daa40 bellard
    return 0;
1596 cc1daa40 bellard
}
1597 cc1daa40 bellard
1598 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
1599 cc1daa40 bellard
{
1600 cc1daa40 bellard
    QEMUMachine *m;
1601 cc1daa40 bellard
1602 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
1603 cc1daa40 bellard
        if (!strcmp(m->name, name))
1604 cc1daa40 bellard
            return m;
1605 3f6599e6 Mark McLoughlin
        if (m->alias && !strcmp(m->alias, name))
1606 3f6599e6 Mark McLoughlin
            return m;
1607 cc1daa40 bellard
    }
1608 cc1daa40 bellard
    return NULL;
1609 cc1daa40 bellard
}
1610 cc1daa40 bellard
1611 0c257437 Anthony Liguori
static QEMUMachine *find_default_machine(void)
1612 0c257437 Anthony Liguori
{
1613 0c257437 Anthony Liguori
    QEMUMachine *m;
1614 0c257437 Anthony Liguori
1615 0c257437 Anthony Liguori
    for(m = first_machine; m != NULL; m = m->next) {
1616 0c257437 Anthony Liguori
        if (m->is_default) {
1617 0c257437 Anthony Liguori
            return m;
1618 0c257437 Anthony Liguori
        }
1619 0c257437 Anthony Liguori
    }
1620 0c257437 Anthony Liguori
    return NULL;
1621 0c257437 Anthony Liguori
}
1622 0c257437 Anthony Liguori
1623 cc1daa40 bellard
/***********************************************************/
1624 8a7ddc38 bellard
/* main execution loop */
1625 8a7ddc38 bellard
1626 9596ebb7 pbrook
static void gui_update(void *opaque)
1627 8a7ddc38 bellard
{
1628 7d957bd8 aliguori
    uint64_t interval = GUI_REFRESH_INTERVAL;
1629 740733bb ths
    DisplayState *ds = opaque;
1630 7d957bd8 aliguori
    DisplayChangeListener *dcl = ds->listeners;
1631 7d957bd8 aliguori
1632 62a2744c Sheng Yang
    qemu_flush_coalesced_mmio_buffer();
1633 7d957bd8 aliguori
    dpy_refresh(ds);
1634 7d957bd8 aliguori
1635 7d957bd8 aliguori
    while (dcl != NULL) {
1636 7d957bd8 aliguori
        if (dcl->gui_timer_interval &&
1637 7d957bd8 aliguori
            dcl->gui_timer_interval < interval)
1638 7d957bd8 aliguori
            interval = dcl->gui_timer_interval;
1639 7d957bd8 aliguori
        dcl = dcl->next;
1640 7d957bd8 aliguori
    }
1641 7d957bd8 aliguori
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1642 8a7ddc38 bellard
}
1643 8a7ddc38 bellard
1644 9043b62d blueswir1
static void nographic_update(void *opaque)
1645 9043b62d blueswir1
{
1646 9043b62d blueswir1
    uint64_t interval = GUI_REFRESH_INTERVAL;
1647 9043b62d blueswir1
1648 62a2744c Sheng Yang
    qemu_flush_coalesced_mmio_buffer();
1649 9043b62d blueswir1
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1650 9043b62d blueswir1
}
1651 9043b62d blueswir1
1652 0bd48850 bellard
struct vm_change_state_entry {
1653 0bd48850 bellard
    VMChangeStateHandler *cb;
1654 0bd48850 bellard
    void *opaque;
1655 72cf2d4f Blue Swirl
    QLIST_ENTRY (vm_change_state_entry) entries;
1656 0bd48850 bellard
};
1657 0bd48850 bellard
1658 72cf2d4f Blue Swirl
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1659 0bd48850 bellard
1660 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1661 0bd48850 bellard
                                                     void *opaque)
1662 0bd48850 bellard
{
1663 0bd48850 bellard
    VMChangeStateEntry *e;
1664 0bd48850 bellard
1665 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
1666 0bd48850 bellard
1667 0bd48850 bellard
    e->cb = cb;
1668 0bd48850 bellard
    e->opaque = opaque;
1669 72cf2d4f Blue Swirl
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1670 0bd48850 bellard
    return e;
1671 0bd48850 bellard
}
1672 0bd48850 bellard
1673 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1674 0bd48850 bellard
{
1675 72cf2d4f Blue Swirl
    QLIST_REMOVE (e, entries);
1676 0bd48850 bellard
    qemu_free (e);
1677 0bd48850 bellard
}
1678 0bd48850 bellard
1679 296af7c9 Blue Swirl
void vm_state_notify(int running, int reason)
1680 0bd48850 bellard
{
1681 0bd48850 bellard
    VMChangeStateEntry *e;
1682 0bd48850 bellard
1683 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1684 9781e040 aliguori
        e->cb(e->opaque, running, reason);
1685 0bd48850 bellard
    }
1686 0bd48850 bellard
}
1687 0bd48850 bellard
1688 8a7ddc38 bellard
void vm_start(void)
1689 8a7ddc38 bellard
{
1690 8a7ddc38 bellard
    if (!vm_running) {
1691 8a7ddc38 bellard
        cpu_enable_ticks();
1692 8a7ddc38 bellard
        vm_running = 1;
1693 9781e040 aliguori
        vm_state_notify(1, 0);
1694 d6dc3d42 aliguori
        resume_all_vcpus();
1695 6ed2c484 Luiz Capitulino
        monitor_protocol_event(QEVENT_RESUME, NULL);
1696 8a7ddc38 bellard
    }
1697 8a7ddc38 bellard
}
1698 8a7ddc38 bellard
1699 bb0c6722 bellard
/* reset/shutdown handler */
1700 bb0c6722 bellard
1701 bb0c6722 bellard
typedef struct QEMUResetEntry {
1702 72cf2d4f Blue Swirl
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1703 bb0c6722 bellard
    QEMUResetHandler *func;
1704 bb0c6722 bellard
    void *opaque;
1705 bb0c6722 bellard
} QEMUResetEntry;
1706 bb0c6722 bellard
1707 72cf2d4f Blue Swirl
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1708 72cf2d4f Blue Swirl
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1709 bb0c6722 bellard
static int reset_requested;
1710 bb0c6722 bellard
static int shutdown_requested;
1711 3475187d bellard
static int powerdown_requested;
1712 296af7c9 Blue Swirl
int debug_requested;
1713 7277e027 Blue Swirl
int vmstop_requested;
1714 bb0c6722 bellard
1715 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
1716 cf7a2fe2 aurel32
{
1717 cf7a2fe2 aurel32
    int r = shutdown_requested;
1718 cf7a2fe2 aurel32
    shutdown_requested = 0;
1719 cf7a2fe2 aurel32
    return r;
1720 cf7a2fe2 aurel32
}
1721 cf7a2fe2 aurel32
1722 cf7a2fe2 aurel32
int qemu_reset_requested(void)
1723 cf7a2fe2 aurel32
{
1724 cf7a2fe2 aurel32
    int r = reset_requested;
1725 cf7a2fe2 aurel32
    reset_requested = 0;
1726 cf7a2fe2 aurel32
    return r;
1727 cf7a2fe2 aurel32
}
1728 cf7a2fe2 aurel32
1729 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
1730 cf7a2fe2 aurel32
{
1731 cf7a2fe2 aurel32
    int r = powerdown_requested;
1732 cf7a2fe2 aurel32
    powerdown_requested = 0;
1733 cf7a2fe2 aurel32
    return r;
1734 cf7a2fe2 aurel32
}
1735 cf7a2fe2 aurel32
1736 e568902a aliguori
static int qemu_debug_requested(void)
1737 e568902a aliguori
{
1738 e568902a aliguori
    int r = debug_requested;
1739 e568902a aliguori
    debug_requested = 0;
1740 e568902a aliguori
    return r;
1741 e568902a aliguori
}
1742 e568902a aliguori
1743 6e29f5da aliguori
static int qemu_vmstop_requested(void)
1744 6e29f5da aliguori
{
1745 6e29f5da aliguori
    int r = vmstop_requested;
1746 6e29f5da aliguori
    vmstop_requested = 0;
1747 6e29f5da aliguori
    return r;
1748 6e29f5da aliguori
}
1749 6e29f5da aliguori
1750 a08d4367 Jan Kiszka
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1751 bb0c6722 bellard
{
1752 55ddfe8e Jan Kiszka
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1753 bb0c6722 bellard
1754 bb0c6722 bellard
    re->func = func;
1755 bb0c6722 bellard
    re->opaque = opaque;
1756 72cf2d4f Blue Swirl
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1757 bb0c6722 bellard
}
1758 bb0c6722 bellard
1759 dda9b29f Jan Kiszka
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1760 bb0c6722 bellard
{
1761 bb0c6722 bellard
    QEMUResetEntry *re;
1762 bb0c6722 bellard
1763 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1764 dda9b29f Jan Kiszka
        if (re->func == func && re->opaque == opaque) {
1765 72cf2d4f Blue Swirl
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1766 dda9b29f Jan Kiszka
            qemu_free(re);
1767 dda9b29f Jan Kiszka
            return;
1768 dda9b29f Jan Kiszka
        }
1769 dda9b29f Jan Kiszka
    }
1770 dda9b29f Jan Kiszka
}
1771 dda9b29f Jan Kiszka
1772 dda9b29f Jan Kiszka
void qemu_system_reset(void)
1773 dda9b29f Jan Kiszka
{
1774 dda9b29f Jan Kiszka
    QEMUResetEntry *re, *nre;
1775 dda9b29f Jan Kiszka
1776 dda9b29f Jan Kiszka
    /* reset all devices */
1777 72cf2d4f Blue Swirl
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1778 bb0c6722 bellard
        re->func(re->opaque);
1779 bb0c6722 bellard
    }
1780 81d9b784 Luiz Capitulino
    monitor_protocol_event(QEVENT_RESET, NULL);
1781 ea375f9a Jan Kiszka
    cpu_synchronize_all_post_reset();
1782 bb0c6722 bellard
}
1783 bb0c6722 bellard
1784 bb0c6722 bellard
void qemu_system_reset_request(void)
1785 bb0c6722 bellard
{
1786 d1beab82 bellard
    if (no_reboot) {
1787 d1beab82 bellard
        shutdown_requested = 1;
1788 d1beab82 bellard
    } else {
1789 d1beab82 bellard
        reset_requested = 1;
1790 d1beab82 bellard
    }
1791 d9f75a4e aliguori
    qemu_notify_event();
1792 bb0c6722 bellard
}
1793 bb0c6722 bellard
1794 bb0c6722 bellard
void qemu_system_shutdown_request(void)
1795 bb0c6722 bellard
{
1796 bb0c6722 bellard
    shutdown_requested = 1;
1797 d9f75a4e aliguori
    qemu_notify_event();
1798 bb0c6722 bellard
}
1799 bb0c6722 bellard
1800 3475187d bellard
void qemu_system_powerdown_request(void)
1801 3475187d bellard
{
1802 3475187d bellard
    powerdown_requested = 1;
1803 d9f75a4e aliguori
    qemu_notify_event();
1804 d9f75a4e aliguori
}
1805 d9f75a4e aliguori
1806 877cf882 ths
#ifdef _WIN32
1807 69d6451c blueswir1
static void host_main_loop_wait(int *timeout)
1808 56f3a5d0 aliguori
{
1809 56f3a5d0 aliguori
    int ret, ret2, i;
1810 f331110f bellard
    PollingEntry *pe;
1811 f331110f bellard
1812 c4b1fcc0 bellard
1813 f331110f bellard
    /* XXX: need to suppress polling by better using win32 events */
1814 f331110f bellard
    ret = 0;
1815 f331110f bellard
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
1816 f331110f bellard
        ret |= pe->func(pe->opaque);
1817 f331110f bellard
    }
1818 e6b1e558 ths
    if (ret == 0) {
1819 a18e524a bellard
        int err;
1820 a18e524a bellard
        WaitObjects *w = &wait_objects;
1821 3b46e624 ths
1822 56f3a5d0 aliguori
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
1823 a18e524a bellard
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
1824 a18e524a bellard
            if (w->func[ret - WAIT_OBJECT_0])
1825 a18e524a bellard
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
1826 3b46e624 ths
1827 5fafdf24 ths
            /* Check for additional signaled events */
1828 e6b1e558 ths
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
1829 3b46e624 ths
1830 e6b1e558 ths
                /* Check if event is signaled */
1831 e6b1e558 ths
                ret2 = WaitForSingleObject(w->events[i], 0);
1832 e6b1e558 ths
                if(ret2 == WAIT_OBJECT_0) {
1833 e6b1e558 ths
                    if (w->func[i])
1834 e6b1e558 ths
                        w->func[i](w->opaque[i]);
1835 e6b1e558 ths
                } else if (ret2 == WAIT_TIMEOUT) {
1836 e6b1e558 ths
                } else {
1837 e6b1e558 ths
                    err = GetLastError();
1838 e6b1e558 ths
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
1839 3b46e624 ths
                }
1840 3b46e624 ths
            }
1841 a18e524a bellard
        } else if (ret == WAIT_TIMEOUT) {
1842 a18e524a bellard
        } else {
1843 a18e524a bellard
            err = GetLastError();
1844 e6b1e558 ths
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
1845 a18e524a bellard
        }
1846 f331110f bellard
    }
1847 56f3a5d0 aliguori
1848 56f3a5d0 aliguori
    *timeout = 0;
1849 56f3a5d0 aliguori
}
1850 56f3a5d0 aliguori
#else
1851 69d6451c blueswir1
static void host_main_loop_wait(int *timeout)
1852 56f3a5d0 aliguori
{
1853 56f3a5d0 aliguori
}
1854 fd1dff4b bellard
#endif
1855 56f3a5d0 aliguori
1856 d6f4ade2 Paolo Bonzini
void main_loop_wait(int nonblocking)
1857 56f3a5d0 aliguori
{
1858 56f3a5d0 aliguori
    IOHandlerRecord *ioh;
1859 56f3a5d0 aliguori
    fd_set rfds, wfds, xfds;
1860 56f3a5d0 aliguori
    int ret, nfds;
1861 56f3a5d0 aliguori
    struct timeval tv;
1862 d6f4ade2 Paolo Bonzini
    int timeout;
1863 56f3a5d0 aliguori
1864 d6f4ade2 Paolo Bonzini
    if (nonblocking)
1865 d6f4ade2 Paolo Bonzini
        timeout = 0;
1866 d6f4ade2 Paolo Bonzini
    else {
1867 d6f4ade2 Paolo Bonzini
        timeout = qemu_calculate_timeout();
1868 d6f4ade2 Paolo Bonzini
        qemu_bh_update_timeout(&timeout);
1869 d6f4ade2 Paolo Bonzini
    }
1870 56f3a5d0 aliguori
1871 56f3a5d0 aliguori
    host_main_loop_wait(&timeout);
1872 56f3a5d0 aliguori
1873 fd1dff4b bellard
    /* poll any events */
1874 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
1875 6abfbd79 aliguori
    nfds = -1;
1876 fd1dff4b bellard
    FD_ZERO(&rfds);
1877 fd1dff4b bellard
    FD_ZERO(&wfds);
1878 e035649e bellard
    FD_ZERO(&xfds);
1879 31d4ee6c Juan Quintela
    QLIST_FOREACH(ioh, &io_handlers, next) {
1880 cafffd40 ths
        if (ioh->deleted)
1881 cafffd40 ths
            continue;
1882 fd1dff4b bellard
        if (ioh->fd_read &&
1883 fd1dff4b bellard
            (!ioh->fd_read_poll ||
1884 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
1885 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
1886 fd1dff4b bellard
            if (ioh->fd > nfds)
1887 fd1dff4b bellard
                nfds = ioh->fd;
1888 fd1dff4b bellard
        }
1889 fd1dff4b bellard
        if (ioh->fd_write) {
1890 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
1891 fd1dff4b bellard
            if (ioh->fd > nfds)
1892 fd1dff4b bellard
                nfds = ioh->fd;
1893 fd1dff4b bellard
        }
1894 fd1dff4b bellard
    }
1895 3b46e624 ths
1896 56f3a5d0 aliguori
    tv.tv_sec = timeout / 1000;
1897 56f3a5d0 aliguori
    tv.tv_usec = (timeout % 1000) * 1000;
1898 56f3a5d0 aliguori
1899 d918f23e Jan Kiszka
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1900 d918f23e Jan Kiszka
1901 4870852c aliguori
    qemu_mutex_unlock_iothread();
1902 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1903 4870852c aliguori
    qemu_mutex_lock_iothread();
1904 fd1dff4b bellard
    if (ret > 0) {
1905 31d4ee6c Juan Quintela
        IOHandlerRecord *pioh;
1906 cafffd40 ths
1907 31d4ee6c Juan Quintela
        QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1908 cafffd40 ths
            if (ioh->deleted) {
1909 31d4ee6c Juan Quintela
                QLIST_REMOVE(ioh, next);
1910 cafffd40 ths
                qemu_free(ioh);
1911 4bed9837 Juan Quintela
                continue;
1912 4bed9837 Juan Quintela
            }
1913 4bed9837 Juan Quintela
            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1914 4bed9837 Juan Quintela
                ioh->fd_read(ioh->opaque);
1915 4bed9837 Juan Quintela
            }
1916 4bed9837 Juan Quintela
            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1917 4bed9837 Juan Quintela
                ioh->fd_write(ioh->opaque);
1918 31d4ee6c Juan Quintela
            }
1919 cafffd40 ths
        }
1920 fd1dff4b bellard
    }
1921 d918f23e Jan Kiszka
1922 d918f23e Jan Kiszka
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1923 c20709aa bellard
1924 b6964827 Paolo Bonzini
    qemu_run_all_timers();
1925 21d5d12b Jan Kiszka
1926 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
1927 423f0742 pbrook
       them.  */
1928 423f0742 pbrook
    qemu_bh_poll();
1929 3b46e624 ths
1930 5905b2e5 bellard
}
1931 5905b2e5 bellard
1932 43b96858 aliguori
static int vm_can_run(void)
1933 43b96858 aliguori
{
1934 43b96858 aliguori
    if (powerdown_requested)
1935 43b96858 aliguori
        return 0;
1936 43b96858 aliguori
    if (reset_requested)
1937 43b96858 aliguori
        return 0;
1938 43b96858 aliguori
    if (shutdown_requested)
1939 43b96858 aliguori
        return 0;
1940 e568902a aliguori
    if (debug_requested)
1941 e568902a aliguori
        return 0;
1942 43b96858 aliguori
    return 1;
1943 43b96858 aliguori
}
1944 43b96858 aliguori
1945 d9c32310 Blue Swirl
qemu_irq qemu_system_powerdown;
1946 d9c32310 Blue Swirl
1947 43b96858 aliguori
static void main_loop(void)
1948 43b96858 aliguori
{
1949 6e29f5da aliguori
    int r;
1950 e6e35b1e aliguori
1951 7277e027 Blue Swirl
    qemu_main_loop_start();
1952 d6dc3d42 aliguori
1953 6e29f5da aliguori
    for (;;) {
1954 43b96858 aliguori
        do {
1955 d6f4ade2 Paolo Bonzini
            bool nonblocking = false;
1956 e6e35b1e aliguori
#ifdef CONFIG_PROFILER
1957 e6e35b1e aliguori
            int64_t ti;
1958 e6e35b1e aliguori
#endif
1959 d6dc3d42 aliguori
#ifndef CONFIG_IOTHREAD
1960 d6f4ade2 Paolo Bonzini
            nonblocking = tcg_cpu_exec();
1961 d6dc3d42 aliguori
#endif
1962 89bfc105 bellard
#ifdef CONFIG_PROFILER
1963 43b96858 aliguori
            ti = profile_getclock();
1964 89bfc105 bellard
#endif
1965 d6f4ade2 Paolo Bonzini
            main_loop_wait(nonblocking);
1966 89bfc105 bellard
#ifdef CONFIG_PROFILER
1967 43b96858 aliguori
            dev_time += profile_getclock() - ti;
1968 89bfc105 bellard
#endif
1969 e568902a aliguori
        } while (vm_can_run());
1970 43b96858 aliguori
1971 54fc6ea9 Blue Swirl
        if ((r = qemu_debug_requested())) {
1972 54fc6ea9 Blue Swirl
            vm_stop(r);
1973 b1a15e7e Luiz Capitulino
        }
1974 43b96858 aliguori
        if (qemu_shutdown_requested()) {
1975 242cd003 Blue Swirl
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1976 43b96858 aliguori
            if (no_shutdown) {
1977 43b96858 aliguori
                vm_stop(0);
1978 43b96858 aliguori
                no_shutdown = 0;
1979 43b96858 aliguori
            } else
1980 43b96858 aliguori
                break;
1981 43b96858 aliguori
        }
1982 d6dc3d42 aliguori
        if (qemu_reset_requested()) {
1983 d6dc3d42 aliguori
            pause_all_vcpus();
1984 43b96858 aliguori
            qemu_system_reset();
1985 d6dc3d42 aliguori
            resume_all_vcpus();
1986 d6dc3d42 aliguori
        }
1987 d9c32310 Blue Swirl
        if (qemu_powerdown_requested()) {
1988 242cd003 Blue Swirl
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1989 d9c32310 Blue Swirl
            qemu_irq_raise(qemu_system_powerdown);
1990 d9c32310 Blue Swirl
        }
1991 b1a15e7e Luiz Capitulino
        if ((r = qemu_vmstop_requested())) {
1992 6e29f5da aliguori
            vm_stop(r);
1993 b1a15e7e Luiz Capitulino
        }
1994 b4608c04 bellard
    }
1995 d6dc3d42 aliguori
    pause_all_vcpus();
1996 b4608c04 bellard
}
1997 b4608c04 bellard
1998 9bd7e6d9 pbrook
static void version(void)
1999 9bd7e6d9 pbrook
{
2000 f75ca1ae Thomas Monjalon
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2001 9bd7e6d9 pbrook
}
2002 9bd7e6d9 pbrook
2003 15f82208 ths
static void help(int exitcode)
2004 0824d6fc bellard
{
2005 e8105ebb Paolo Bonzini
    const char *options_help =
2006 ad96090a Blue Swirl
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
2007 ad96090a Blue Swirl
        opt_help
2008 5824d651 blueswir1
#define DEFHEADING(text) stringify(text) "\n"
2009 5824d651 blueswir1
#include "qemu-options.h"
2010 5824d651 blueswir1
#undef DEF
2011 5824d651 blueswir1
#undef DEFHEADING
2012 5824d651 blueswir1
#undef GEN_DOCS
2013 e8105ebb Paolo Bonzini
        ;
2014 e8105ebb Paolo Bonzini
    version();
2015 e8105ebb Paolo Bonzini
    printf("usage: %s [options] [disk_image]\n"
2016 e8105ebb Paolo Bonzini
           "\n"
2017 f75ca1ae Thomas Monjalon
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
2018 3f020d70 malc
           "\n"
2019 e8105ebb Paolo Bonzini
           "%s\n"
2020 3f020d70 malc
           "During emulation, the following keys are useful:\n"
2021 3f020d70 malc
           "ctrl-alt-f      toggle full screen\n"
2022 3f020d70 malc
           "ctrl-alt-n      switch to virtual console 'n'\n"
2023 3f020d70 malc
           "ctrl-alt        toggle mouse and keyboard grab\n"
2024 3f020d70 malc
           "\n"
2025 e8105ebb Paolo Bonzini
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
2026 e8105ebb Paolo Bonzini
           "qemu",
2027 e8105ebb Paolo Bonzini
           options_help);
2028 15f82208 ths
    exit(exitcode);
2029 0824d6fc bellard
}
2030 0824d6fc bellard
2031 cd6f1169 bellard
#define HAS_ARG 0x0001
2032 cd6f1169 bellard
2033 cd6f1169 bellard
enum {
2034 ad96090a Blue Swirl
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
2035 5824d651 blueswir1
    opt_enum,
2036 5824d651 blueswir1
#define DEFHEADING(text)
2037 5824d651 blueswir1
#include "qemu-options.h"
2038 5824d651 blueswir1
#undef DEF
2039 5824d651 blueswir1
#undef DEFHEADING
2040 5824d651 blueswir1
#undef GEN_DOCS
2041 cd6f1169 bellard
};
2042 cd6f1169 bellard
2043 cd6f1169 bellard
typedef struct QEMUOption {
2044 cd6f1169 bellard
    const char *name;
2045 cd6f1169 bellard
    int flags;
2046 cd6f1169 bellard
    int index;
2047 ad96090a Blue Swirl
    uint32_t arch_mask;
2048 cd6f1169 bellard
} QEMUOption;
2049 cd6f1169 bellard
2050 dbed7e40 blueswir1
static const QEMUOption qemu_options[] = {
2051 ad96090a Blue Swirl
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2052 ad96090a Blue Swirl
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
2053 ad96090a Blue Swirl
    { option, opt_arg, opt_enum, arch_mask },
2054 5824d651 blueswir1
#define DEFHEADING(text)
2055 5824d651 blueswir1
#include "qemu-options.h"
2056 5824d651 blueswir1
#undef DEF
2057 5824d651 blueswir1
#undef DEFHEADING
2058 5824d651 blueswir1
#undef GEN_DOCS
2059 cd6f1169 bellard
    { NULL },
2060 fc01f7e7 bellard
};
2061 3893c124 malc
static void select_vgahw (const char *p)
2062 3893c124 malc
{
2063 3893c124 malc
    const char *opts;
2064 3893c124 malc
2065 64465297 Gerd Hoffmann
    default_vga = 0;
2066 86176759 Zachary Amsden
    vga_interface_type = VGA_NONE;
2067 3893c124 malc
    if (strstart(p, "std", &opts)) {
2068 86176759 Zachary Amsden
        vga_interface_type = VGA_STD;
2069 3893c124 malc
    } else if (strstart(p, "cirrus", &opts)) {
2070 86176759 Zachary Amsden
        vga_interface_type = VGA_CIRRUS;
2071 3893c124 malc
    } else if (strstart(p, "vmware", &opts)) {
2072 86176759 Zachary Amsden
        vga_interface_type = VGA_VMWARE;
2073 94909d9f aliguori
    } else if (strstart(p, "xenfb", &opts)) {
2074 86176759 Zachary Amsden
        vga_interface_type = VGA_XENFB;
2075 28b85ed8 aliguori
    } else if (!strstart(p, "none", &opts)) {
2076 3893c124 malc
    invalid_vga:
2077 3893c124 malc
        fprintf(stderr, "Unknown vga type: %s\n", p);
2078 3893c124 malc
        exit(1);
2079 3893c124 malc
    }
2080 cb5a7aa8 malc
    while (*opts) {
2081 cb5a7aa8 malc
        const char *nextopt;
2082 cb5a7aa8 malc
2083 cb5a7aa8 malc
        if (strstart(opts, ",retrace=", &nextopt)) {
2084 cb5a7aa8 malc
            opts = nextopt;
2085 cb5a7aa8 malc
            if (strstart(opts, "dumb", &nextopt))
2086 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_DUMB;
2087 cb5a7aa8 malc
            else if (strstart(opts, "precise", &nextopt))
2088 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_PRECISE;
2089 cb5a7aa8 malc
            else goto invalid_vga;
2090 cb5a7aa8 malc
        } else goto invalid_vga;
2091 cb5a7aa8 malc
        opts = nextopt;
2092 cb5a7aa8 malc
    }
2093 3893c124 malc
}
2094 3893c124 malc
2095 7d4c3d53 Markus Armbruster
static int balloon_parse(const char *arg)
2096 7d4c3d53 Markus Armbruster
{
2097 382f0743 Gerd Hoffmann
    QemuOpts *opts;
2098 7d4c3d53 Markus Armbruster
2099 382f0743 Gerd Hoffmann
    if (strcmp(arg, "none") == 0) {
2100 382f0743 Gerd Hoffmann
        return 0;
2101 382f0743 Gerd Hoffmann
    }
2102 382f0743 Gerd Hoffmann
2103 382f0743 Gerd Hoffmann
    if (!strncmp(arg, "virtio", 6)) {
2104 382f0743 Gerd Hoffmann
        if (arg[6] == ',') {
2105 382f0743 Gerd Hoffmann
            /* have params -> parse them */
2106 8212c64f Markus Armbruster
            opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
2107 382f0743 Gerd Hoffmann
            if (!opts)
2108 382f0743 Gerd Hoffmann
                return  -1;
2109 382f0743 Gerd Hoffmann
        } else {
2110 382f0743 Gerd Hoffmann
            /* create empty opts */
2111 382f0743 Gerd Hoffmann
            opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2112 7d4c3d53 Markus Armbruster
        }
2113 382f0743 Gerd Hoffmann
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
2114 382f0743 Gerd Hoffmann
        return 0;
2115 7d4c3d53 Markus Armbruster
    }
2116 382f0743 Gerd Hoffmann
2117 382f0743 Gerd Hoffmann
    return -1;
2118 7d4c3d53 Markus Armbruster
}
2119 7d4c3d53 Markus Armbruster
2120 3587d7e6 bellard
#ifdef _WIN32
2121 3587d7e6 bellard
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
2122 3587d7e6 bellard
{
2123 3587d7e6 bellard
    exit(STATUS_CONTROL_C_EXIT);
2124 3587d7e6 bellard
    return TRUE;
2125 3587d7e6 bellard
}
2126 3587d7e6 bellard
#endif
2127 3587d7e6 bellard
2128 5b08fc10 aliguori
#ifndef _WIN32
2129 5b08fc10 aliguori
2130 5b08fc10 aliguori
static void termsig_handler(int signal)
2131 5b08fc10 aliguori
{
2132 5b08fc10 aliguori
    qemu_system_shutdown_request();
2133 5b08fc10 aliguori
}
2134 5b08fc10 aliguori
2135 7c3370d4 Jan Kiszka
static void sigchld_handler(int signal)
2136 7c3370d4 Jan Kiszka
{
2137 7c3370d4 Jan Kiszka
    waitpid(-1, NULL, WNOHANG);
2138 7c3370d4 Jan Kiszka
}
2139 7c3370d4 Jan Kiszka
2140 7c3370d4 Jan Kiszka
static void sighandler_setup(void)
2141 5b08fc10 aliguori
{
2142 5b08fc10 aliguori
    struct sigaction act;
2143 5b08fc10 aliguori
2144 5b08fc10 aliguori
    memset(&act, 0, sizeof(act));
2145 5b08fc10 aliguori
    act.sa_handler = termsig_handler;
2146 5b08fc10 aliguori
    sigaction(SIGINT,  &act, NULL);
2147 5b08fc10 aliguori
    sigaction(SIGHUP,  &act, NULL);
2148 5b08fc10 aliguori
    sigaction(SIGTERM, &act, NULL);
2149 7c3370d4 Jan Kiszka
2150 7c3370d4 Jan Kiszka
    act.sa_handler = sigchld_handler;
2151 7c3370d4 Jan Kiszka
    act.sa_flags = SA_NOCLDSTOP;
2152 7c3370d4 Jan Kiszka
    sigaction(SIGCHLD, &act, NULL);
2153 5b08fc10 aliguori
}
2154 5b08fc10 aliguori
2155 5b08fc10 aliguori
#endif
2156 5b08fc10 aliguori
2157 5cea8590 Paul Brook
#ifdef _WIN32
2158 5cea8590 Paul Brook
/* Look for support files in the same directory as the executable.  */
2159 5cea8590 Paul Brook
static char *find_datadir(const char *argv0)
2160 5cea8590 Paul Brook
{
2161 5cea8590 Paul Brook
    char *p;
2162 5cea8590 Paul Brook
    char buf[MAX_PATH];
2163 5cea8590 Paul Brook
    DWORD len;
2164 5cea8590 Paul Brook
2165 5cea8590 Paul Brook
    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2166 5cea8590 Paul Brook
    if (len == 0) {
2167 c5947808 Blue Swirl
        return NULL;
2168 5cea8590 Paul Brook
    }
2169 5cea8590 Paul Brook
2170 5cea8590 Paul Brook
    buf[len] = 0;
2171 5cea8590 Paul Brook
    p = buf + len - 1;
2172 5cea8590 Paul Brook
    while (p != buf && *p != '\\')
2173 5cea8590 Paul Brook
        p--;
2174 5cea8590 Paul Brook
    *p = 0;
2175 5cea8590 Paul Brook
    if (access(buf, R_OK) == 0) {
2176 5cea8590 Paul Brook
        return qemu_strdup(buf);
2177 5cea8590 Paul Brook
    }
2178 5cea8590 Paul Brook
    return NULL;
2179 5cea8590 Paul Brook
}
2180 5cea8590 Paul Brook
#else /* !_WIN32 */
2181 5cea8590 Paul Brook
2182 5cea8590 Paul Brook
/* Find a likely location for support files using the location of the binary.
2183 5cea8590 Paul Brook
   For installed binaries this will be "$bindir/../share/qemu".  When
2184 5cea8590 Paul Brook
   running from the build tree this will be "$bindir/../pc-bios".  */
2185 5cea8590 Paul Brook
#define SHARE_SUFFIX "/share/qemu"
2186 5cea8590 Paul Brook
#define BUILD_SUFFIX "/pc-bios"
2187 5cea8590 Paul Brook
static char *find_datadir(const char *argv0)
2188 5cea8590 Paul Brook
{
2189 5cea8590 Paul Brook
    char *dir;
2190 5cea8590 Paul Brook
    char *p = NULL;
2191 5cea8590 Paul Brook
    char *res;
2192 5cea8590 Paul Brook
    char buf[PATH_MAX];
2193 3a41759d Blue Swirl
    size_t max_len;
2194 5cea8590 Paul Brook
2195 5cea8590 Paul Brook
#if defined(__linux__)
2196 5cea8590 Paul Brook
    {
2197 5cea8590 Paul Brook
        int len;
2198 5cea8590 Paul Brook
        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2199 5cea8590 Paul Brook
        if (len > 0) {
2200 5cea8590 Paul Brook
            buf[len] = 0;
2201 5cea8590 Paul Brook
            p = buf;
2202 5cea8590 Paul Brook
        }
2203 5cea8590 Paul Brook
    }
2204 5cea8590 Paul Brook
#elif defined(__FreeBSD__)
2205 5cea8590 Paul Brook
    {
2206 92c0e657 Juergen Lock
        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2207 92c0e657 Juergen Lock
        size_t len = sizeof(buf) - 1;
2208 92c0e657 Juergen Lock
2209 92c0e657 Juergen Lock
        *buf = '\0';
2210 92c0e657 Juergen Lock
        if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
2211 92c0e657 Juergen Lock
            *buf) {
2212 92c0e657 Juergen Lock
            buf[sizeof(buf) - 1] = '\0';
2213 5cea8590 Paul Brook
            p = buf;
2214 5cea8590 Paul Brook
        }
2215 5cea8590 Paul Brook
    }
2216 5cea8590 Paul Brook
#endif
2217 5cea8590 Paul Brook
    /* If we don't have any way of figuring out the actual executable
2218 5cea8590 Paul Brook
       location then try argv[0].  */
2219 5cea8590 Paul Brook
    if (!p) {
2220 4d224196 Jean-Christophe DUBOIS
        p = realpath(argv0, buf);
2221 5cea8590 Paul Brook
        if (!p) {
2222 5cea8590 Paul Brook
            return NULL;
2223 5cea8590 Paul Brook
        }
2224 5cea8590 Paul Brook
    }
2225 5cea8590 Paul Brook
    dir = dirname(p);
2226 5cea8590 Paul Brook
    dir = dirname(dir);
2227 5cea8590 Paul Brook
2228 3a41759d Blue Swirl
    max_len = strlen(dir) +
2229 3a41759d Blue Swirl
        MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
2230 3a41759d Blue Swirl
    res = qemu_mallocz(max_len);
2231 3a41759d Blue Swirl
    snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
2232 5cea8590 Paul Brook
    if (access(res, R_OK)) {
2233 3a41759d Blue Swirl
        snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
2234 5cea8590 Paul Brook
        if (access(res, R_OK)) {
2235 5cea8590 Paul Brook
            qemu_free(res);
2236 5cea8590 Paul Brook
            res = NULL;
2237 5cea8590 Paul Brook
        }
2238 5cea8590 Paul Brook
    }
2239 4d224196 Jean-Christophe DUBOIS
2240 5cea8590 Paul Brook
    return res;
2241 5cea8590 Paul Brook
}
2242 5cea8590 Paul Brook
#undef SHARE_SUFFIX
2243 5cea8590 Paul Brook
#undef BUILD_SUFFIX
2244 5cea8590 Paul Brook
#endif
2245 5cea8590 Paul Brook
2246 5cea8590 Paul Brook
char *qemu_find_file(int type, const char *name)
2247 5cea8590 Paul Brook
{
2248 5cea8590 Paul Brook
    int len;
2249 5cea8590 Paul Brook
    const char *subdir;
2250 5cea8590 Paul Brook
    char *buf;
2251 5cea8590 Paul Brook
2252 5cea8590 Paul Brook
    /* If name contains path separators then try it as a straight path.  */
2253 5cea8590 Paul Brook
    if ((strchr(name, '/') || strchr(name, '\\'))
2254 5cea8590 Paul Brook
        && access(name, R_OK) == 0) {
2255 73ffc805 Jean-Christophe DUBOIS
        return qemu_strdup(name);
2256 5cea8590 Paul Brook
    }
2257 5cea8590 Paul Brook
    switch (type) {
2258 5cea8590 Paul Brook
    case QEMU_FILE_TYPE_BIOS:
2259 5cea8590 Paul Brook
        subdir = "";
2260 5cea8590 Paul Brook
        break;
2261 5cea8590 Paul Brook
    case QEMU_FILE_TYPE_KEYMAP:
2262 5cea8590 Paul Brook
        subdir = "keymaps/";
2263 5cea8590 Paul Brook
        break;
2264 5cea8590 Paul Brook
    default:
2265 5cea8590 Paul Brook
        abort();
2266 5cea8590 Paul Brook
    }
2267 5cea8590 Paul Brook
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2268 5cea8590 Paul Brook
    buf = qemu_mallocz(len);
2269 3a41759d Blue Swirl
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2270 5cea8590 Paul Brook
    if (access(buf, R_OK)) {
2271 5cea8590 Paul Brook
        qemu_free(buf);
2272 5cea8590 Paul Brook
        return NULL;
2273 5cea8590 Paul Brook
    }
2274 5cea8590 Paul Brook
    return buf;
2275 5cea8590 Paul Brook
}
2276 5cea8590 Paul Brook
2277 ff952ba2 Markus Armbruster
static int device_help_func(QemuOpts *opts, void *opaque)
2278 ff952ba2 Markus Armbruster
{
2279 ff952ba2 Markus Armbruster
    return qdev_device_help(opts);
2280 ff952ba2 Markus Armbruster
}
2281 ff952ba2 Markus Armbruster
2282 f31d07d1 Gerd Hoffmann
static int device_init_func(QemuOpts *opts, void *opaque)
2283 f31d07d1 Gerd Hoffmann
{
2284 f31d07d1 Gerd Hoffmann
    DeviceState *dev;
2285 f31d07d1 Gerd Hoffmann
2286 f31d07d1 Gerd Hoffmann
    dev = qdev_device_add(opts);
2287 f31d07d1 Gerd Hoffmann
    if (!dev)
2288 f31d07d1 Gerd Hoffmann
        return -1;
2289 f31d07d1 Gerd Hoffmann
    return 0;
2290 f31d07d1 Gerd Hoffmann
}
2291 f31d07d1 Gerd Hoffmann
2292 1a688d3b Gerd Hoffmann
static int chardev_init_func(QemuOpts *opts, void *opaque)
2293 1a688d3b Gerd Hoffmann
{
2294 1a688d3b Gerd Hoffmann
    CharDriverState *chr;
2295 1a688d3b Gerd Hoffmann
2296 1a688d3b Gerd Hoffmann
    chr = qemu_chr_open_opts(opts, NULL);
2297 1a688d3b Gerd Hoffmann
    if (!chr)
2298 1a688d3b Gerd Hoffmann
        return -1;
2299 1a688d3b Gerd Hoffmann
    return 0;
2300 1a688d3b Gerd Hoffmann
}
2301 1a688d3b Gerd Hoffmann
2302 74db920c Gautham R Shenoy
#ifdef CONFIG_LINUX
2303 74db920c Gautham R Shenoy
static int fsdev_init_func(QemuOpts *opts, void *opaque)
2304 74db920c Gautham R Shenoy
{
2305 74db920c Gautham R Shenoy
    int ret;
2306 74db920c Gautham R Shenoy
    ret = qemu_fsdev_add(opts);
2307 74db920c Gautham R Shenoy
2308 74db920c Gautham R Shenoy
    return ret;
2309 74db920c Gautham R Shenoy
}
2310 74db920c Gautham R Shenoy
#endif
2311 74db920c Gautham R Shenoy
2312 88589343 Gerd Hoffmann
static int mon_init_func(QemuOpts *opts, void *opaque)
2313 88589343 Gerd Hoffmann
{
2314 88589343 Gerd Hoffmann
    CharDriverState *chr;
2315 88589343 Gerd Hoffmann
    const char *chardev;
2316 88589343 Gerd Hoffmann
    const char *mode;
2317 88589343 Gerd Hoffmann
    int flags;
2318 88589343 Gerd Hoffmann
2319 88589343 Gerd Hoffmann
    mode = qemu_opt_get(opts, "mode");
2320 88589343 Gerd Hoffmann
    if (mode == NULL) {
2321 88589343 Gerd Hoffmann
        mode = "readline";
2322 88589343 Gerd Hoffmann
    }
2323 88589343 Gerd Hoffmann
    if (strcmp(mode, "readline") == 0) {
2324 88589343 Gerd Hoffmann
        flags = MONITOR_USE_READLINE;
2325 88589343 Gerd Hoffmann
    } else if (strcmp(mode, "control") == 0) {
2326 88589343 Gerd Hoffmann
        flags = MONITOR_USE_CONTROL;
2327 88589343 Gerd Hoffmann
    } else {
2328 88589343 Gerd Hoffmann
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2329 88589343 Gerd Hoffmann
        exit(1);
2330 88589343 Gerd Hoffmann
    }
2331 88589343 Gerd Hoffmann
2332 88589343 Gerd Hoffmann
    if (qemu_opt_get_bool(opts, "default", 0))
2333 88589343 Gerd Hoffmann
        flags |= MONITOR_IS_DEFAULT;
2334 88589343 Gerd Hoffmann
2335 88589343 Gerd Hoffmann
    chardev = qemu_opt_get(opts, "chardev");
2336 88589343 Gerd Hoffmann
    chr = qemu_chr_find(chardev);
2337 88589343 Gerd Hoffmann
    if (chr == NULL) {
2338 88589343 Gerd Hoffmann
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2339 88589343 Gerd Hoffmann
        exit(1);
2340 88589343 Gerd Hoffmann
    }
2341 88589343 Gerd Hoffmann
2342 88589343 Gerd Hoffmann
    monitor_init(chr, flags);
2343 88589343 Gerd Hoffmann
    return 0;
2344 88589343 Gerd Hoffmann
}
2345 88589343 Gerd Hoffmann
2346 6ca5582d Gerd Hoffmann
static void monitor_parse(const char *optarg, const char *mode)
2347 88589343 Gerd Hoffmann
{
2348 88589343 Gerd Hoffmann
    static int monitor_device_index = 0;
2349 88589343 Gerd Hoffmann
    QemuOpts *opts;
2350 88589343 Gerd Hoffmann
    const char *p;
2351 88589343 Gerd Hoffmann
    char label[32];
2352 88589343 Gerd Hoffmann
    int def = 0;
2353 88589343 Gerd Hoffmann
2354 88589343 Gerd Hoffmann
    if (strstart(optarg, "chardev:", &p)) {
2355 88589343 Gerd Hoffmann
        snprintf(label, sizeof(label), "%s", p);
2356 88589343 Gerd Hoffmann
    } else {
2357 140e065d Jan Kiszka
        snprintf(label, sizeof(label), "compat_monitor%d",
2358 140e065d Jan Kiszka
                 monitor_device_index);
2359 140e065d Jan Kiszka
        if (monitor_device_index == 0) {
2360 88589343 Gerd Hoffmann
            def = 1;
2361 88589343 Gerd Hoffmann
        }
2362 88589343 Gerd Hoffmann
        opts = qemu_chr_parse_compat(label, optarg);
2363 88589343 Gerd Hoffmann
        if (!opts) {
2364 88589343 Gerd Hoffmann
            fprintf(stderr, "parse error: %s\n", optarg);
2365 88589343 Gerd Hoffmann
            exit(1);
2366 88589343 Gerd Hoffmann
        }
2367 88589343 Gerd Hoffmann
    }
2368 88589343 Gerd Hoffmann
2369 88589343 Gerd Hoffmann
    opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2370 88589343 Gerd Hoffmann
    if (!opts) {
2371 88589343 Gerd Hoffmann
        fprintf(stderr, "duplicate chardev: %s\n", label);
2372 88589343 Gerd Hoffmann
        exit(1);
2373 88589343 Gerd Hoffmann
    }
2374 6ca5582d Gerd Hoffmann
    qemu_opt_set(opts, "mode", mode);
2375 88589343 Gerd Hoffmann
    qemu_opt_set(opts, "chardev", label);
2376 88589343 Gerd Hoffmann
    if (def)
2377 88589343 Gerd Hoffmann
        qemu_opt_set(opts, "default", "on");
2378 88589343 Gerd Hoffmann
    monitor_device_index++;
2379 88589343 Gerd Hoffmann
}
2380 88589343 Gerd Hoffmann
2381 bd3c948d Gerd Hoffmann
struct device_config {
2382 bd3c948d Gerd Hoffmann
    enum {
2383 aee1b935 Gerd Hoffmann
        DEV_USB,       /* -usbdevice     */
2384 aee1b935 Gerd Hoffmann
        DEV_BT,        /* -bt            */
2385 aee1b935 Gerd Hoffmann
        DEV_SERIAL,    /* -serial        */
2386 aee1b935 Gerd Hoffmann
        DEV_PARALLEL,  /* -parallel      */
2387 aee1b935 Gerd Hoffmann
        DEV_VIRTCON,   /* -virtioconsole */
2388 c9f398e5 H. Peter Anvin
        DEV_DEBUGCON,  /* -debugcon */
2389 bd3c948d Gerd Hoffmann
    } type;
2390 bd3c948d Gerd Hoffmann
    const char *cmdline;
2391 72cf2d4f Blue Swirl
    QTAILQ_ENTRY(device_config) next;
2392 bd3c948d Gerd Hoffmann
};
2393 72cf2d4f Blue Swirl
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2394 bd3c948d Gerd Hoffmann
2395 bd3c948d Gerd Hoffmann
static void add_device_config(int type, const char *cmdline)
2396 bd3c948d Gerd Hoffmann
{
2397 bd3c948d Gerd Hoffmann
    struct device_config *conf;
2398 bd3c948d Gerd Hoffmann
2399 bd3c948d Gerd Hoffmann
    conf = qemu_mallocz(sizeof(*conf));
2400 bd3c948d Gerd Hoffmann
    conf->type = type;
2401 bd3c948d Gerd Hoffmann
    conf->cmdline = cmdline;
2402 72cf2d4f Blue Swirl
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2403 bd3c948d Gerd Hoffmann
}
2404 bd3c948d Gerd Hoffmann
2405 bd3c948d Gerd Hoffmann
static int foreach_device_config(int type, int (*func)(const char *cmdline))
2406 bd3c948d Gerd Hoffmann
{
2407 bd3c948d Gerd Hoffmann
    struct device_config *conf;
2408 bd3c948d Gerd Hoffmann
    int rc;
2409 bd3c948d Gerd Hoffmann
2410 72cf2d4f Blue Swirl
    QTAILQ_FOREACH(conf, &device_configs, next) {
2411 bd3c948d Gerd Hoffmann
        if (conf->type != type)
2412 bd3c948d Gerd Hoffmann
            continue;
2413 bd3c948d Gerd Hoffmann
        rc = func(conf->cmdline);
2414 bd3c948d Gerd Hoffmann
        if (0 != rc)
2415 bd3c948d Gerd Hoffmann
            return rc;
2416 bd3c948d Gerd Hoffmann
    }
2417 bd3c948d Gerd Hoffmann
    return 0;
2418 bd3c948d Gerd Hoffmann
}
2419 bd3c948d Gerd Hoffmann
2420 998bbd74 Gerd Hoffmann
static int serial_parse(const char *devname)
2421 998bbd74 Gerd Hoffmann
{
2422 998bbd74 Gerd Hoffmann
    static int index = 0;
2423 998bbd74 Gerd Hoffmann
    char label[32];
2424 998bbd74 Gerd Hoffmann
2425 998bbd74 Gerd Hoffmann
    if (strcmp(devname, "none") == 0)
2426 998bbd74 Gerd Hoffmann
        return 0;
2427 998bbd74 Gerd Hoffmann
    if (index == MAX_SERIAL_PORTS) {
2428 998bbd74 Gerd Hoffmann
        fprintf(stderr, "qemu: too many serial ports\n");
2429 998bbd74 Gerd Hoffmann
        exit(1);
2430 998bbd74 Gerd Hoffmann
    }
2431 998bbd74 Gerd Hoffmann
    snprintf(label, sizeof(label), "serial%d", index);
2432 998bbd74 Gerd Hoffmann
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
2433 998bbd74 Gerd Hoffmann
    if (!serial_hds[index]) {
2434 998bbd74 Gerd Hoffmann
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2435 998bbd74 Gerd Hoffmann
                devname, strerror(errno));
2436 998bbd74 Gerd Hoffmann
        return -1;
2437 998bbd74 Gerd Hoffmann
    }
2438 998bbd74 Gerd Hoffmann
    index++;
2439 998bbd74 Gerd Hoffmann
    return 0;
2440 998bbd74 Gerd Hoffmann
}
2441 998bbd74 Gerd Hoffmann
2442 6a5e8b0e Gerd Hoffmann
static int parallel_parse(const char *devname)
2443 6a5e8b0e Gerd Hoffmann
{
2444 6a5e8b0e Gerd Hoffmann
    static int index = 0;
2445 6a5e8b0e Gerd Hoffmann
    char label[32];
2446 6a5e8b0e Gerd Hoffmann
2447 6a5e8b0e Gerd Hoffmann
    if (strcmp(devname, "none") == 0)
2448 6a5e8b0e Gerd Hoffmann
        return 0;
2449 6a5e8b0e Gerd Hoffmann
    if (index == MAX_PARALLEL_PORTS) {
2450 6a5e8b0e Gerd Hoffmann
        fprintf(stderr, "qemu: too many parallel ports\n");
2451 6a5e8b0e Gerd Hoffmann
        exit(1);
2452 6a5e8b0e Gerd Hoffmann
    }
2453 6a5e8b0e Gerd Hoffmann
    snprintf(label, sizeof(label), "parallel%d", index);
2454 6a5e8b0e Gerd Hoffmann
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2455 6a5e8b0e Gerd Hoffmann
    if (!parallel_hds[index]) {
2456 6a5e8b0e Gerd Hoffmann
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2457 6a5e8b0e Gerd Hoffmann
                devname, strerror(errno));
2458 6a5e8b0e Gerd Hoffmann
        return -1;
2459 6a5e8b0e Gerd Hoffmann
    }
2460 6a5e8b0e Gerd Hoffmann
    index++;
2461 6a5e8b0e Gerd Hoffmann
    return 0;
2462 6a5e8b0e Gerd Hoffmann
}
2463 6a5e8b0e Gerd Hoffmann
2464 aee1b935 Gerd Hoffmann
static int virtcon_parse(const char *devname)
2465 aee1b935 Gerd Hoffmann
{
2466 aee1b935 Gerd Hoffmann
    static int index = 0;
2467 aee1b935 Gerd Hoffmann
    char label[32];
2468 392ecf54 Amit Shah
    QemuOpts *bus_opts, *dev_opts;
2469 aee1b935 Gerd Hoffmann
2470 aee1b935 Gerd Hoffmann
    if (strcmp(devname, "none") == 0)
2471 aee1b935 Gerd Hoffmann
        return 0;
2472 aee1b935 Gerd Hoffmann
    if (index == MAX_VIRTIO_CONSOLES) {
2473 aee1b935 Gerd Hoffmann
        fprintf(stderr, "qemu: too many virtio consoles\n");
2474 aee1b935 Gerd Hoffmann
        exit(1);
2475 aee1b935 Gerd Hoffmann
    }
2476 392ecf54 Amit Shah
2477 392ecf54 Amit Shah
    bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2478 392ecf54 Amit Shah
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
2479 392ecf54 Amit Shah
2480 392ecf54 Amit Shah
    dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2481 392ecf54 Amit Shah
    qemu_opt_set(dev_opts, "driver", "virtconsole");
2482 392ecf54 Amit Shah
2483 aee1b935 Gerd Hoffmann
    snprintf(label, sizeof(label), "virtcon%d", index);
2484 aee1b935 Gerd Hoffmann
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2485 aee1b935 Gerd Hoffmann
    if (!virtcon_hds[index]) {
2486 aee1b935 Gerd Hoffmann
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2487 aee1b935 Gerd Hoffmann
                devname, strerror(errno));
2488 aee1b935 Gerd Hoffmann
        return -1;
2489 aee1b935 Gerd Hoffmann
    }
2490 392ecf54 Amit Shah
    qemu_opt_set(dev_opts, "chardev", label);
2491 392ecf54 Amit Shah
2492 aee1b935 Gerd Hoffmann
    index++;
2493 aee1b935 Gerd Hoffmann
    return 0;
2494 aee1b935 Gerd Hoffmann
}
2495 aee1b935 Gerd Hoffmann
2496 c9f398e5 H. Peter Anvin
static int debugcon_parse(const char *devname)
2497 c9f398e5 H. Peter Anvin
{   
2498 c9f398e5 H. Peter Anvin
    QemuOpts *opts;
2499 c9f398e5 H. Peter Anvin
2500 c9f398e5 H. Peter Anvin
    if (!qemu_chr_open("debugcon", devname, NULL)) {
2501 c9f398e5 H. Peter Anvin
        exit(1);
2502 c9f398e5 H. Peter Anvin
    }
2503 c9f398e5 H. Peter Anvin
    opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2504 c9f398e5 H. Peter Anvin
    if (!opts) {
2505 c9f398e5 H. Peter Anvin
        fprintf(stderr, "qemu: already have a debugcon device\n");
2506 c9f398e5 H. Peter Anvin
        exit(1);
2507 c9f398e5 H. Peter Anvin
    }
2508 c9f398e5 H. Peter Anvin
    qemu_opt_set(opts, "driver", "isa-debugcon");
2509 c9f398e5 H. Peter Anvin
    qemu_opt_set(opts, "chardev", "debugcon");
2510 c9f398e5 H. Peter Anvin
    return 0;
2511 c9f398e5 H. Peter Anvin
}
2512 c9f398e5 H. Peter Anvin
2513 6530a97b Anthony Liguori
static const QEMUOption *lookup_opt(int argc, char **argv,
2514 6530a97b Anthony Liguori
                                    const char **poptarg, int *poptind)
2515 6530a97b Anthony Liguori
{
2516 6530a97b Anthony Liguori
    const QEMUOption *popt;
2517 6530a97b Anthony Liguori
    int optind = *poptind;
2518 6530a97b Anthony Liguori
    char *r = argv[optind];
2519 6530a97b Anthony Liguori
    const char *optarg;
2520 6530a97b Anthony Liguori
2521 0f0bc3f1 Markus Armbruster
    loc_set_cmdline(argv, optind, 1);
2522 6530a97b Anthony Liguori
    optind++;
2523 6530a97b Anthony Liguori
    /* Treat --foo the same as -foo.  */
2524 6530a97b Anthony Liguori
    if (r[1] == '-')
2525 6530a97b Anthony Liguori
        r++;
2526 6530a97b Anthony Liguori
    popt = qemu_options;
2527 6530a97b Anthony Liguori
    for(;;) {
2528 6530a97b Anthony Liguori
        if (!popt->name) {
2529 0f0bc3f1 Markus Armbruster
            error_report("invalid option");
2530 6530a97b Anthony Liguori
            exit(1);
2531 6530a97b Anthony Liguori
        }
2532 6530a97b Anthony Liguori
        if (!strcmp(popt->name, r + 1))
2533 6530a97b Anthony Liguori
            break;
2534 6530a97b Anthony Liguori
        popt++;
2535 6530a97b Anthony Liguori
    }
2536 6530a97b Anthony Liguori
    if (popt->flags & HAS_ARG) {
2537 6530a97b Anthony Liguori
        if (optind >= argc) {
2538 0f0bc3f1 Markus Armbruster
            error_report("requires an argument");
2539 6530a97b Anthony Liguori
            exit(1);
2540 6530a97b Anthony Liguori
        }
2541 6530a97b Anthony Liguori
        optarg = argv[optind++];
2542 0f0bc3f1 Markus Armbruster
        loc_set_cmdline(argv, optind - 2, 2);
2543 6530a97b Anthony Liguori
    } else {
2544 6530a97b Anthony Liguori
        optarg = NULL;
2545 6530a97b Anthony Liguori
    }
2546 6530a97b Anthony Liguori
2547 6530a97b Anthony Liguori
    *poptarg = optarg;
2548 6530a97b Anthony Liguori
    *poptind = optind;
2549 6530a97b Anthony Liguori
2550 6530a97b Anthony Liguori
    return popt;
2551 6530a97b Anthony Liguori
}
2552 6530a97b Anthony Liguori
2553 902b3d5c malc
int main(int argc, char **argv, char **envp)
2554 0824d6fc bellard
{
2555 59030a8c aliguori
    const char *gdbstub_dev = NULL;
2556 e4bcb14c ths
    int i;
2557 da1fcfda Eduardo Habkost
    int snapshot, linux_boot;
2558 4e3de9e9 Paolo Bonzini
    const char *icount_option = NULL;
2559 7f7f9873 bellard
    const char *initrd_filename;
2560 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
2561 195325a4 Anthony Liguori
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2562 3023f332 aliguori
    DisplayState *ds;
2563 7d957bd8 aliguori
    DisplayChangeListener *dcl;
2564 46d4767d bellard
    int cyls, heads, secs, translation;
2565 f31d07d1 Gerd Hoffmann
    QemuOpts *hda_opts = NULL, *opts;
2566 cd6f1169 bellard
    int optind;
2567 6530a97b Anthony Liguori
    const char *optarg;
2568 d63d307f bellard
    const char *loadvm = NULL;
2569 cc1daa40 bellard
    QEMUMachine *machine;
2570 94fc95cd j_mayer
    const char *cpu_model;
2571 b9e82a59 blueswir1
#ifndef _WIN32
2572 71e3ceb8 ths
    int fds[2];
2573 b9e82a59 blueswir1
#endif
2574 26a5f13b bellard
    int tb_size;
2575 93815bc2 ths
    const char *pid_file = NULL;
2576 5bb7910a aliguori
    const char *incoming = NULL;
2577 b9e82a59 blueswir1
#ifndef _WIN32
2578 54042bcf aliguori
    int fd = 0;
2579 54042bcf aliguori
    struct passwd *pwd = NULL;
2580 0858532e aliguori
    const char *chroot_dir = NULL;
2581 0858532e aliguori
    const char *run_as = NULL;
2582 b9e82a59 blueswir1
#endif
2583 993fbfdb Anthony Liguori
    int show_vnc_port = 0;
2584 292444cb Anthony Liguori
    int defconfig = 1;
2585 0bd48850 bellard
2586 65abca0a Markus Armbruster
    error_set_progname(argv[0]);
2587 65abca0a Markus Armbruster
2588 6875204c Jan Kiszka
    init_clocks();
2589 6875204c Jan Kiszka
2590 902b3d5c malc
    qemu_cache_utils_init(envp);
2591 902b3d5c malc
2592 72cf2d4f Blue Swirl
    QLIST_INIT (&vm_change_state_head);
2593 be995c27 bellard
#ifndef _WIN32
2594 be995c27 bellard
    {
2595 be995c27 bellard
        struct sigaction act;
2596 be995c27 bellard
        sigfillset(&act.sa_mask);
2597 be995c27 bellard
        act.sa_flags = 0;
2598 be995c27 bellard
        act.sa_handler = SIG_IGN;
2599 be995c27 bellard
        sigaction(SIGPIPE, &act, NULL);
2600 be995c27 bellard
    }
2601 3587d7e6 bellard
#else
2602 3587d7e6 bellard
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
2603 a8e5ac33 bellard
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
2604 a8e5ac33 bellard
       QEMU to run on a single CPU */
2605 a8e5ac33 bellard
    {
2606 a8e5ac33 bellard
        HANDLE h;
2607 a8e5ac33 bellard
        DWORD mask, smask;
2608 a8e5ac33 bellard
        int i;
2609 a8e5ac33 bellard
        h = GetCurrentProcess();
2610 a8e5ac33 bellard
        if (GetProcessAffinityMask(h, &mask, &smask)) {
2611 a8e5ac33 bellard
            for(i = 0; i < 32; i++) {
2612 a8e5ac33 bellard
                if (mask & (1 << i))
2613 a8e5ac33 bellard
                    break;
2614 a8e5ac33 bellard
            }
2615 a8e5ac33 bellard
            if (i != 32) {
2616 a8e5ac33 bellard
                mask = 1 << i;
2617 a8e5ac33 bellard
                SetProcessAffinityMask(h, mask);
2618 a8e5ac33 bellard
            }
2619 a8e5ac33 bellard
        }
2620 a8e5ac33 bellard
    }
2621 67b915a5 bellard
#endif
2622 be995c27 bellard
2623 f80f9ec9 Anthony Liguori
    module_call_init(MODULE_INIT_MACHINE);
2624 0c257437 Anthony Liguori
    machine = find_default_machine();
2625 94fc95cd j_mayer
    cpu_model = NULL;
2626 fc01f7e7 bellard
    initrd_filename = NULL;
2627 4fc5d071 aurel32
    ram_size = 0;
2628 33e3963e bellard
    snapshot = 0;
2629 a20dd508 bellard
    kernel_filename = NULL;
2630 a20dd508 bellard
    kernel_cmdline = "";
2631 c4b1fcc0 bellard
    cyls = heads = secs = 0;
2632 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
2633 c4b1fcc0 bellard
2634 268a362c aliguori
    for (i = 0; i < MAX_NODES; i++) {
2635 268a362c aliguori
        node_mem[i] = 0;
2636 268a362c aliguori
        node_cpumask[i] = 0;
2637 268a362c aliguori
    }
2638 268a362c aliguori
2639 268a362c aliguori
    nb_numa_nodes = 0;
2640 7c9d8e07 bellard
    nb_nics = 0;
2641 3b46e624 ths
2642 26a5f13b bellard
    tb_size = 0;
2643 41bd639b blueswir1
    autostart= 1;
2644 41bd639b blueswir1
2645 292444cb Anthony Liguori
    /* first pass of option parsing */
2646 292444cb Anthony Liguori
    optind = 1;
2647 292444cb Anthony Liguori
    while (optind < argc) {
2648 292444cb Anthony Liguori
        if (argv[optind][0] != '-') {
2649 292444cb Anthony Liguori
            /* disk image */
2650 28e68d68 Anthony Liguori
            optind++;
2651 292444cb Anthony Liguori
            continue;
2652 292444cb Anthony Liguori
        } else {
2653 292444cb Anthony Liguori
            const QEMUOption *popt;
2654 292444cb Anthony Liguori
2655 292444cb Anthony Liguori
            popt = lookup_opt(argc, argv, &optarg, &optind);
2656 292444cb Anthony Liguori
            switch (popt->index) {
2657 292444cb Anthony Liguori
            case QEMU_OPTION_nodefconfig:
2658 292444cb Anthony Liguori
                defconfig=0;
2659 292444cb Anthony Liguori
                break;
2660 292444cb Anthony Liguori
            }
2661 292444cb Anthony Liguori
        }
2662 292444cb Anthony Liguori
    }
2663 292444cb Anthony Liguori
2664 292444cb Anthony Liguori
    if (defconfig) {
2665 dcfb0939 Kevin Wolf
        int ret;
2666 cf5a65aa Markus Armbruster
2667 dcfb0939 Kevin Wolf
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2668 019e78ba Kevin Wolf
        if (ret < 0 && ret != -ENOENT) {
2669 dcfb0939 Kevin Wolf
            exit(1);
2670 292444cb Anthony Liguori
        }
2671 292444cb Anthony Liguori
2672 dcfb0939 Kevin Wolf
        ret = qemu_read_config_file(arch_config_name);
2673 019e78ba Kevin Wolf
        if (ret < 0 && ret != -ENOENT) {
2674 dcfb0939 Kevin Wolf
            exit(1);
2675 292444cb Anthony Liguori
        }
2676 292444cb Anthony Liguori
    }
2677 de06f8d1 Blue Swirl
    cpudef_init();
2678 292444cb Anthony Liguori
2679 292444cb Anthony Liguori
    /* second pass of option parsing */
2680 cd6f1169 bellard
    optind = 1;
2681 0824d6fc bellard
    for(;;) {
2682 cd6f1169 bellard
        if (optind >= argc)
2683 0824d6fc bellard
            break;
2684 6530a97b Anthony Liguori
        if (argv[optind][0] != '-') {
2685 9dfd7c7a Gerd Hoffmann
            hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
2686 cd6f1169 bellard
        } else {
2687 cd6f1169 bellard
            const QEMUOption *popt;
2688 cd6f1169 bellard
2689 6530a97b Anthony Liguori
            popt = lookup_opt(argc, argv, &optarg, &optind);
2690 ad96090a Blue Swirl
            if (!(popt->arch_mask & arch_type)) {
2691 ad96090a Blue Swirl
                printf("Option %s not supported for this target\n", popt->name);
2692 ad96090a Blue Swirl
                exit(1);
2693 ad96090a Blue Swirl
            }
2694 cd6f1169 bellard
            switch(popt->index) {
2695 cc1daa40 bellard
            case QEMU_OPTION_M:
2696 cc1daa40 bellard
                machine = find_machine(optarg);
2697 cc1daa40 bellard
                if (!machine) {
2698 cc1daa40 bellard
                    QEMUMachine *m;
2699 cc1daa40 bellard
                    printf("Supported machines are:\n");
2700 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
2701 3f6599e6 Mark McLoughlin
                        if (m->alias)
2702 3f6599e6 Mark McLoughlin
                            printf("%-10s %s (alias of %s)\n",
2703 3f6599e6 Mark McLoughlin
                                   m->alias, m->desc, m->name);
2704 cc1daa40 bellard
                        printf("%-10s %s%s\n",
2705 5fafdf24 ths
                               m->name, m->desc,
2706 0c257437 Anthony Liguori
                               m->is_default ? " (default)" : "");
2707 cc1daa40 bellard
                    }
2708 15f82208 ths
                    exit(*optarg != '?');
2709 cc1daa40 bellard
                }
2710 cc1daa40 bellard
                break;
2711 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
2712 94fc95cd j_mayer
                /* hw initialization will check this */
2713 15f82208 ths
                if (*optarg == '?') {
2714 262353cb Blue Swirl
                    list_cpus(stdout, &fprintf, optarg);
2715 15f82208 ths
                    exit(0);
2716 94fc95cd j_mayer
                } else {
2717 94fc95cd j_mayer
                    cpu_model = optarg;
2718 94fc95cd j_mayer
                }
2719 94fc95cd j_mayer
                break;
2720 cd6f1169 bellard
            case QEMU_OPTION_initrd:
2721 fc01f7e7 bellard
                initrd_filename = optarg;
2722 fc01f7e7 bellard
                break;
2723 cd6f1169 bellard
            case QEMU_OPTION_hda:
2724 e4bcb14c ths
                if (cyls == 0)
2725 9dfd7c7a Gerd Hoffmann
                    hda_opts = drive_add(optarg, HD_ALIAS, 0);
2726 e4bcb14c ths
                else
2727 9dfd7c7a Gerd Hoffmann
                    hda_opts = drive_add(optarg, HD_ALIAS
2728 e4bcb14c ths
                             ",cyls=%d,heads=%d,secs=%d%s",
2729 609497ab balrog
                             0, cyls, heads, secs,
2730 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_LBA ?
2731 e4bcb14c ths
                                 ",trans=lba" :
2732 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_NONE ?
2733 e4bcb14c ths
                                 ",trans=none" : "");
2734 e4bcb14c ths
                 break;
2735 cd6f1169 bellard
            case QEMU_OPTION_hdb:
2736 cc1daa40 bellard
            case QEMU_OPTION_hdc:
2737 cc1daa40 bellard
            case QEMU_OPTION_hdd:
2738 609497ab balrog
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2739 fc01f7e7 bellard
                break;
2740 e4bcb14c ths
            case QEMU_OPTION_drive:
2741 609497ab balrog
                drive_add(NULL, "%s", optarg);
2742 e4bcb14c ths
                break;
2743 d058fe03 Gerd Hoffmann
            case QEMU_OPTION_set:
2744 d058fe03 Gerd Hoffmann
                if (qemu_set_option(optarg) != 0)
2745 d058fe03 Gerd Hoffmann
                    exit(1);
2746 d058fe03 Gerd Hoffmann
                break;
2747 d0fef6fb Gerd Hoffmann
            case QEMU_OPTION_global:
2748 d0fef6fb Gerd Hoffmann
                if (qemu_global_option(optarg) != 0)
2749 d0fef6fb Gerd Hoffmann
                    exit(1);
2750 d0fef6fb Gerd Hoffmann
                break;
2751 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
2752 609497ab balrog
                drive_add(optarg, MTD_ALIAS);
2753 3e3d5815 balrog
                break;
2754 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
2755 609497ab balrog
                drive_add(optarg, SD_ALIAS);
2756 a1bb27b1 pbrook
                break;
2757 86f55663 j_mayer
            case QEMU_OPTION_pflash:
2758 609497ab balrog
                drive_add(optarg, PFLASH_ALIAS);
2759 86f55663 j_mayer
                break;
2760 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
2761 33e3963e bellard
                snapshot = 1;
2762 33e3963e bellard
                break;
2763 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
2764 330d0414 bellard
                {
2765 330d0414 bellard
                    const char *p;
2766 330d0414 bellard
                    p = optarg;
2767 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
2768 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
2769 46d4767d bellard
                        goto chs_fail;
2770 330d0414 bellard
                    if (*p != ',')
2771 330d0414 bellard
                        goto chs_fail;
2772 330d0414 bellard
                    p++;
2773 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
2774 46d4767d bellard
                    if (heads < 1 || heads > 16)
2775 46d4767d bellard
                        goto chs_fail;
2776 330d0414 bellard
                    if (*p != ',')
2777 330d0414 bellard
                        goto chs_fail;
2778 330d0414 bellard
                    p++;
2779 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
2780 46d4767d bellard
                    if (secs < 1 || secs > 63)
2781 46d4767d bellard
                        goto chs_fail;
2782 46d4767d bellard
                    if (*p == ',') {
2783 46d4767d bellard
                        p++;
2784 46d4767d bellard
                        if (!strcmp(p, "none"))
2785 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
2786 46d4767d bellard
                        else if (!strcmp(p, "lba"))
2787 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
2788 46d4767d bellard
                        else if (!strcmp(p, "auto"))
2789 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
2790 46d4767d bellard
                        else
2791 46d4767d bellard
                            goto chs_fail;
2792 46d4767d bellard
                    } else if (*p != '\0') {
2793 c4b1fcc0 bellard
                    chs_fail:
2794 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
2795 46d4767d bellard
                        exit(1);
2796 c4b1fcc0 bellard
                    }
2797 9dfd7c7a Gerd Hoffmann
                    if (hda_opts != NULL) {
2798 9dfd7c7a Gerd Hoffmann
                        char num[16];
2799 9dfd7c7a Gerd Hoffmann
                        snprintf(num, sizeof(num), "%d", cyls);
2800 9dfd7c7a Gerd Hoffmann
                        qemu_opt_set(hda_opts, "cyls", num);
2801 9dfd7c7a Gerd Hoffmann
                        snprintf(num, sizeof(num), "%d", heads);
2802 9dfd7c7a Gerd Hoffmann
                        qemu_opt_set(hda_opts, "heads", num);
2803 9dfd7c7a Gerd Hoffmann
                        snprintf(num, sizeof(num), "%d", secs);
2804 9dfd7c7a Gerd Hoffmann
                        qemu_opt_set(hda_opts, "secs", num);
2805 9dfd7c7a Gerd Hoffmann
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
2806 9dfd7c7a Gerd Hoffmann
                            qemu_opt_set(hda_opts, "trans", "lba");
2807 9dfd7c7a Gerd Hoffmann
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
2808 9dfd7c7a Gerd Hoffmann
                            qemu_opt_set(hda_opts, "trans", "none");
2809 9dfd7c7a Gerd Hoffmann
                    }
2810 330d0414 bellard
                }
2811 330d0414 bellard
                break;
2812 268a362c aliguori
            case QEMU_OPTION_numa:
2813 268a362c aliguori
                if (nb_numa_nodes >= MAX_NODES) {
2814 268a362c aliguori
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
2815 268a362c aliguori
                    exit(1);
2816 268a362c aliguori
                }
2817 268a362c aliguori
                numa_add(optarg);
2818 268a362c aliguori
                break;
2819 cd6f1169 bellard
            case QEMU_OPTION_nographic:
2820 993fbfdb Anthony Liguori
                display_type = DT_NOGRAPHIC;
2821 a20dd508 bellard
                break;
2822 4d3b6f6e balrog
#ifdef CONFIG_CURSES
2823 4d3b6f6e balrog
            case QEMU_OPTION_curses:
2824 993fbfdb Anthony Liguori
                display_type = DT_CURSES;
2825 4d3b6f6e balrog
                break;
2826 4d3b6f6e balrog
#endif
2827 a171fe39 balrog
            case QEMU_OPTION_portrait:
2828 a171fe39 balrog
                graphic_rotate = 1;
2829 a171fe39 balrog
                break;
2830 cd6f1169 bellard
            case QEMU_OPTION_kernel:
2831 a20dd508 bellard
                kernel_filename = optarg;
2832 a20dd508 bellard
                break;
2833 cd6f1169 bellard
            case QEMU_OPTION_append:
2834 a20dd508 bellard
                kernel_cmdline = optarg;
2835 313aa567 bellard
                break;
2836 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
2837 609497ab balrog
                drive_add(optarg, CDROM_ALIAS);
2838 36b486bb bellard
                break;
2839 cd6f1169 bellard
            case QEMU_OPTION_boot:
2840 28c5af54 j_mayer
                {
2841 ef3adf68 Jan Kiszka
                    static const char * const params[] = {
2842 95387491 Jan Kiszka
                        "order", "once", "menu", NULL
2843 ef3adf68 Jan Kiszka
                    };
2844 ef3adf68 Jan Kiszka
                    char buf[sizeof(boot_devices)];
2845 e0f084bf Jan Kiszka
                    char *standard_boot_devices;
2846 ef3adf68 Jan Kiszka
                    int legacy = 0;
2847 ef3adf68 Jan Kiszka
2848 ef3adf68 Jan Kiszka
                    if (!strchr(optarg, '=')) {
2849 ef3adf68 Jan Kiszka
                        legacy = 1;
2850 ef3adf68 Jan Kiszka
                        pstrcpy(buf, sizeof(buf), optarg);
2851 ef3adf68 Jan Kiszka
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2852 ef3adf68 Jan Kiszka
                        fprintf(stderr,
2853 ef3adf68 Jan Kiszka
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2854 ef3adf68 Jan Kiszka
                                buf, optarg);
2855 ef3adf68 Jan Kiszka
                        exit(1);
2856 ef3adf68 Jan Kiszka
                    }
2857 ef3adf68 Jan Kiszka
2858 ef3adf68 Jan Kiszka
                    if (legacy ||
2859 ef3adf68 Jan Kiszka
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2860 4e9e9d6e Eduardo Habkost
                        validate_bootdevices(buf);
2861 ef3adf68 Jan Kiszka
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2862 28c5af54 j_mayer
                    }
2863 e0f084bf Jan Kiszka
                    if (!legacy) {
2864 e0f084bf Jan Kiszka
                        if (get_param_value(buf, sizeof(buf),
2865 e0f084bf Jan Kiszka
                                            "once", optarg)) {
2866 4e9e9d6e Eduardo Habkost
                            validate_bootdevices(buf);
2867 e0f084bf Jan Kiszka
                            standard_boot_devices = qemu_strdup(boot_devices);
2868 e0f084bf Jan Kiszka
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2869 e0f084bf Jan Kiszka
                            qemu_register_reset(restore_boot_devices,
2870 e0f084bf Jan Kiszka
                                                standard_boot_devices);
2871 e0f084bf Jan Kiszka
                        }
2872 95387491 Jan Kiszka
                        if (get_param_value(buf, sizeof(buf),
2873 95387491 Jan Kiszka
                                            "menu", optarg)) {
2874 95387491 Jan Kiszka
                            if (!strcmp(buf, "on")) {
2875 95387491 Jan Kiszka
                                boot_menu = 1;
2876 95387491 Jan Kiszka
                            } else if (!strcmp(buf, "off")) {
2877 95387491 Jan Kiszka
                                boot_menu = 0;
2878 95387491 Jan Kiszka
                            } else {
2879 95387491 Jan Kiszka
                                fprintf(stderr,
2880 95387491 Jan Kiszka
                                        "qemu: invalid option value '%s'\n",
2881 95387491 Jan Kiszka
                                        buf);
2882 95387491 Jan Kiszka
                                exit(1);
2883 95387491 Jan Kiszka
                            }
2884 95387491 Jan Kiszka
                        }
2885 e0f084bf Jan Kiszka
                    }
2886 36b486bb bellard
                }
2887 36b486bb bellard
                break;
2888 cd6f1169 bellard
            case QEMU_OPTION_fda:
2889 cd6f1169 bellard
            case QEMU_OPTION_fdb:
2890 609497ab balrog
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2891 c45886db bellard
                break;
2892 52ca8d6a bellard
            case QEMU_OPTION_no_fd_bootchk:
2893 52ca8d6a bellard
                fd_bootchk = 0;
2894 52ca8d6a bellard
                break;
2895 a1ea458f Mark McLoughlin
            case QEMU_OPTION_netdev:
2896 a1ea458f Mark McLoughlin
                if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2897 a1ea458f Mark McLoughlin
                    exit(1);
2898 a1ea458f Mark McLoughlin
                }
2899 a1ea458f Mark McLoughlin
                break;
2900 7c9d8e07 bellard
            case QEMU_OPTION_net:
2901 7f161aae Mark McLoughlin
                if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2902 c4b1fcc0 bellard
                    exit(1);
2903 c4b1fcc0 bellard
                }
2904 702c651c bellard
                break;
2905 c7f74643 bellard
#ifdef CONFIG_SLIRP
2906 c7f74643 bellard
            case QEMU_OPTION_tftp:
2907 ad196a9d Jan Kiszka
                legacy_tftp_prefix = optarg;
2908 9bf05444 bellard
                break;
2909 47d5d01a ths
            case QEMU_OPTION_bootp:
2910 ad196a9d Jan Kiszka
                legacy_bootp_filename = optarg;
2911 47d5d01a ths
                break;
2912 c94c8d64 bellard
#ifndef _WIN32
2913 9d728e8c bellard
            case QEMU_OPTION_smb:
2914 0752706d Markus Armbruster
                if (net_slirp_smb(optarg) < 0)
2915 0752706d Markus Armbruster
                    exit(1);
2916 9d728e8c bellard
                break;
2917 c94c8d64 bellard
#endif
2918 9bf05444 bellard
            case QEMU_OPTION_redir:
2919 0752706d Markus Armbruster
                if (net_slirp_redir(optarg) < 0)
2920 0752706d Markus Armbruster
                    exit(1);
2921 9bf05444 bellard
                break;
2922 c7f74643 bellard
#endif
2923 dc72ac14 balrog
            case QEMU_OPTION_bt:
2924 bd3c948d Gerd Hoffmann
                add_device_config(DEV_BT, optarg);
2925 dc72ac14 balrog
                break;
2926 1d14ffa9 bellard
            case QEMU_OPTION_audio_help:
2927 ad96090a Blue Swirl
                if (!(audio_available())) {
2928 ad96090a Blue Swirl
                    printf("Option %s not supported for this target\n", popt->name);
2929 ad96090a Blue Swirl
                    exit(1);
2930 ad96090a Blue Swirl
                }
2931 1d14ffa9 bellard
                AUD_help ();
2932 1d14ffa9 bellard
                exit (0);
2933 1d14ffa9 bellard
                break;
2934 1d14ffa9 bellard
            case QEMU_OPTION_soundhw:
2935 ad96090a Blue Swirl
                if (!(audio_available())) {
2936 ad96090a Blue Swirl
                    printf("Option %s not supported for this target\n", popt->name);
2937 ad96090a Blue Swirl
                    exit(1);
2938 ad96090a Blue Swirl
                }
2939 1d14ffa9 bellard
                select_soundhw (optarg);
2940 1d14ffa9 bellard
                break;
2941 cd6f1169 bellard
            case QEMU_OPTION_h:
2942 15f82208 ths
                help(0);
2943 cd6f1169 bellard
                break;
2944 9bd7e6d9 pbrook
            case QEMU_OPTION_version:
2945 9bd7e6d9 pbrook
                version();
2946 9bd7e6d9 pbrook
                exit(0);
2947 9bd7e6d9 pbrook
                break;
2948 00f82b8a aurel32
            case QEMU_OPTION_m: {
2949 00f82b8a aurel32
                uint64_t value;
2950 00f82b8a aurel32
                char *ptr;
2951 00f82b8a aurel32
2952 00f82b8a aurel32
                value = strtoul(optarg, &ptr, 10);
2953 00f82b8a aurel32
                switch (*ptr) {
2954 00f82b8a aurel32
                case 0: case 'M': case 'm':
2955 00f82b8a aurel32
                    value <<= 20;
2956 00f82b8a aurel32
                    break;
2957 00f82b8a aurel32
                case 'G': case 'g':
2958 00f82b8a aurel32
                    value <<= 30;
2959 00f82b8a aurel32
                    break;
2960 00f82b8a aurel32
                default:
2961 00f82b8a aurel32
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2962 cd6f1169 bellard
                    exit(1);
2963 cd6f1169 bellard
                }
2964 00f82b8a aurel32
2965 00f82b8a aurel32
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2966 4a1418e0 Anthony Liguori
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2967 00f82b8a aurel32
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2968 00f82b8a aurel32
                    exit(1);
2969 00f82b8a aurel32
                }
2970 c227f099 Anthony Liguori
                if (value != (uint64_t)(ram_addr_t)value) {
2971 00f82b8a aurel32
                    fprintf(stderr, "qemu: ram size too large\n");
2972 00f82b8a aurel32
                    exit(1);
2973 00f82b8a aurel32
                }
2974 00f82b8a aurel32
                ram_size = value;
2975 cd6f1169 bellard
                break;
2976 00f82b8a aurel32
            }
2977 c902760f Marcelo Tosatti
            case QEMU_OPTION_mempath:
2978 c902760f Marcelo Tosatti
                mem_path = optarg;
2979 c902760f Marcelo Tosatti
                break;
2980 c902760f Marcelo Tosatti
#ifdef MAP_POPULATE
2981 c902760f Marcelo Tosatti
            case QEMU_OPTION_mem_prealloc:
2982 c902760f Marcelo Tosatti
                mem_prealloc = 1;
2983 c902760f Marcelo Tosatti
                break;
2984 c902760f Marcelo Tosatti
#endif
2985 cd6f1169 bellard
            case QEMU_OPTION_d:
2986 de06f8d1 Blue Swirl
                set_cpu_log(optarg);
2987 cd6f1169 bellard
                break;
2988 cd6f1169 bellard
            case QEMU_OPTION_s:
2989 59030a8c aliguori
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2990 cd6f1169 bellard
                break;
2991 59030a8c aliguori
            case QEMU_OPTION_gdb:
2992 59030a8c aliguori
                gdbstub_dev = optarg;
2993 cd6f1169 bellard
                break;
2994 cd6f1169 bellard
            case QEMU_OPTION_L:
2995 5cea8590 Paul Brook
                data_dir = optarg;
2996 cd6f1169 bellard
                break;
2997 1192dad8 j_mayer
            case QEMU_OPTION_bios:
2998 1192dad8 j_mayer
                bios_name = optarg;
2999 1192dad8 j_mayer
                break;
3000 1b530a6d aurel32
            case QEMU_OPTION_singlestep:
3001 1b530a6d aurel32
                singlestep = 1;
3002 1b530a6d aurel32
                break;
3003 cd6f1169 bellard
            case QEMU_OPTION_S:
3004 3c07f8e8 pbrook
                autostart = 0;
3005 cd6f1169 bellard
                break;
3006 3d11d0eb bellard
            case QEMU_OPTION_k:
3007 3d11d0eb bellard
                keyboard_layout = optarg;
3008 3d11d0eb bellard
                break;
3009 ee22c2f7 bellard
            case QEMU_OPTION_localtime:
3010 ee22c2f7 bellard
                rtc_utc = 0;
3011 ee22c2f7 bellard
                break;
3012 3893c124 malc
            case QEMU_OPTION_vga:
3013 3893c124 malc
                select_vgahw (optarg);
3014 1bfe856e bellard
                break;
3015 e9b137c2 bellard
            case QEMU_OPTION_g:
3016 e9b137c2 bellard
                {
3017 e9b137c2 bellard
                    const char *p;
3018 e9b137c2 bellard
                    int w, h, depth;
3019 e9b137c2 bellard
                    p = optarg;
3020 e9b137c2 bellard
                    w = strtol(p, (char **)&p, 10);
3021 e9b137c2 bellard
                    if (w <= 0) {
3022 e9b137c2 bellard
                    graphic_error:
3023 e9b137c2 bellard
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
3024 e9b137c2 bellard
                        exit(1);
3025 e9b137c2 bellard
                    }
3026 e9b137c2 bellard
                    if (*p != 'x')
3027 e9b137c2 bellard
                        goto graphic_error;
3028 e9b137c2 bellard
                    p++;
3029 e9b137c2 bellard
                    h = strtol(p, (char **)&p, 10);
3030 e9b137c2 bellard
                    if (h <= 0)
3031 e9b137c2 bellard
                        goto graphic_error;
3032 e9b137c2 bellard
                    if (*p == 'x') {
3033 e9b137c2 bellard
                        p++;
3034 e9b137c2 bellard
                        depth = strtol(p, (char **)&p, 10);
3035 5fafdf24 ths
                        if (depth != 8 && depth != 15 && depth != 16 &&
3036 e9b137c2 bellard
                            depth != 24 && depth != 32)
3037 e9b137c2 bellard
                            goto graphic_error;
3038 e9b137c2 bellard
                    } else if (*p == '\0') {
3039 e9b137c2 bellard
                        depth = graphic_depth;
3040 e9b137c2 bellard
                    } else {
3041 e9b137c2 bellard
                        goto graphic_error;
3042 e9b137c2 bellard
                    }
3043 3b46e624 ths
3044 e9b137c2 bellard
                    graphic_width = w;
3045 e9b137c2 bellard
                    graphic_height = h;
3046 e9b137c2 bellard
                    graphic_depth = depth;
3047 e9b137c2 bellard
                }
3048 e9b137c2 bellard
                break;
3049 20d8a3ed ths
            case QEMU_OPTION_echr:
3050 20d8a3ed ths
                {
3051 20d8a3ed ths
                    char *r;
3052 20d8a3ed ths
                    term_escape_char = strtol(optarg, &r, 0);
3053 20d8a3ed ths
                    if (r == optarg)
3054 20d8a3ed ths
                        printf("Bad argument to echr\n");
3055 20d8a3ed ths
                    break;
3056 20d8a3ed ths
                }
3057 82c643ff bellard
            case QEMU_OPTION_monitor:
3058 6ca5582d Gerd Hoffmann
                monitor_parse(optarg, "readline");
3059 6ca5582d Gerd Hoffmann
                default_monitor = 0;
3060 6ca5582d Gerd Hoffmann
                break;
3061 6ca5582d Gerd Hoffmann
            case QEMU_OPTION_qmp:
3062 6ca5582d Gerd Hoffmann
                monitor_parse(optarg, "control");
3063 2d114dc1 Anthony Liguori
                default_monitor = 0;
3064 82c643ff bellard
                break;
3065 22a0e04b Gerd Hoffmann
            case QEMU_OPTION_mon:
3066 8212c64f Markus Armbruster
                opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
3067 22a0e04b Gerd Hoffmann
                if (!opts) {
3068 22a0e04b Gerd Hoffmann
                    exit(1);
3069 22a0e04b Gerd Hoffmann
                }
3070 2d114dc1 Anthony Liguori
                default_monitor = 0;
3071 22a0e04b Gerd Hoffmann
                break;
3072 191bc01b Gerd Hoffmann
            case QEMU_OPTION_chardev:
3073 8212c64f Markus Armbruster
                opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
3074 191bc01b Gerd Hoffmann
                if (!opts) {
3075 191bc01b Gerd Hoffmann
                    exit(1);
3076 191bc01b Gerd Hoffmann
                }
3077 191bc01b Gerd Hoffmann
                break;
3078 74db920c Gautham R Shenoy
#ifdef CONFIG_LINUX
3079 74db920c Gautham R Shenoy
            case QEMU_OPTION_fsdev:
3080 74db920c Gautham R Shenoy
                opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
3081 74db920c Gautham R Shenoy
                if (!opts) {
3082 74db920c Gautham R Shenoy
                    fprintf(stderr, "parse error: %s\n", optarg);
3083 74db920c Gautham R Shenoy
                    exit(1);
3084 74db920c Gautham R Shenoy
                }
3085 74db920c Gautham R Shenoy
                break;
3086 3d54abc7 Gautham R Shenoy
            case QEMU_OPTION_virtfs: {
3087 3d54abc7 Gautham R Shenoy
                char *arg_fsdev = NULL;
3088 3d54abc7 Gautham R Shenoy
                char *arg_9p = NULL;
3089 3d54abc7 Gautham R Shenoy
                int len = 0;
3090 3d54abc7 Gautham R Shenoy
3091 3d54abc7 Gautham R Shenoy
                opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
3092 3d54abc7 Gautham R Shenoy
                if (!opts) {
3093 3d54abc7 Gautham R Shenoy
                    fprintf(stderr, "parse error: %s\n", optarg);
3094 3d54abc7 Gautham R Shenoy
                    exit(1);
3095 3d54abc7 Gautham R Shenoy
                }
3096 3d54abc7 Gautham R Shenoy
3097 3d54abc7 Gautham R Shenoy
                len = strlen(",id=,path=");
3098 3d54abc7 Gautham R Shenoy
                len += strlen(qemu_opt_get(opts, "fstype"));
3099 3d54abc7 Gautham R Shenoy
                len += strlen(qemu_opt_get(opts, "mount_tag"));
3100 3d54abc7 Gautham R Shenoy
                len += strlen(qemu_opt_get(opts, "path"));
3101 3d54abc7 Gautham R Shenoy
                arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
3102 3d54abc7 Gautham R Shenoy
3103 3d54abc7 Gautham R Shenoy
                if (!arg_fsdev) {
3104 3d54abc7 Gautham R Shenoy
                    fprintf(stderr, "No memory to parse -fsdev for %s\n",
3105 3d54abc7 Gautham R Shenoy
                            optarg);
3106 3d54abc7 Gautham R Shenoy
                    exit(1);
3107 3d54abc7 Gautham R Shenoy
                }
3108 3d54abc7 Gautham R Shenoy
3109 3d54abc7 Gautham R Shenoy
                sprintf(arg_fsdev, "%s,id=%s,path=%s",
3110 3d54abc7 Gautham R Shenoy
                                qemu_opt_get(opts, "fstype"),
3111 3d54abc7 Gautham R Shenoy
                                qemu_opt_get(opts, "mount_tag"),
3112 3d54abc7 Gautham R Shenoy
                                qemu_opt_get(opts, "path"));
3113 3d54abc7 Gautham R Shenoy
3114 3d54abc7 Gautham R Shenoy
                len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
3115 3d54abc7 Gautham R Shenoy
                len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
3116 3d54abc7 Gautham R Shenoy
                arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
3117 3d54abc7 Gautham R Shenoy
3118 3d54abc7 Gautham R Shenoy
                if (!arg_9p) {
3119 3d54abc7 Gautham R Shenoy
                    fprintf(stderr, "No memory to parse -device for %s\n",
3120 3d54abc7 Gautham R Shenoy
                            optarg);
3121 3d54abc7 Gautham R Shenoy
                    exit(1);
3122 3d54abc7 Gautham R Shenoy
                }
3123 3d54abc7 Gautham R Shenoy
3124 3d54abc7 Gautham R Shenoy
                sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
3125 3d54abc7 Gautham R Shenoy
                                qemu_opt_get(opts, "mount_tag"),
3126 3d54abc7 Gautham R Shenoy
                                qemu_opt_get(opts, "mount_tag"));
3127 3d54abc7 Gautham R Shenoy
3128 3d54abc7 Gautham R Shenoy
                if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
3129 3d54abc7 Gautham R Shenoy
                    fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
3130 3d54abc7 Gautham R Shenoy
                    exit(1);
3131 3d54abc7 Gautham R Shenoy
                }
3132 3d54abc7 Gautham R Shenoy
3133 3d54abc7 Gautham R Shenoy
                if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
3134 3d54abc7 Gautham R Shenoy
                    fprintf(stderr, "parse error [device]: %s\n", optarg);
3135 3d54abc7 Gautham R Shenoy
                    exit(1);
3136 3d54abc7 Gautham R Shenoy
                }
3137 3d54abc7 Gautham R Shenoy
3138 3d54abc7 Gautham R Shenoy
                qemu_free(arg_fsdev);
3139 3d54abc7 Gautham R Shenoy
                qemu_free(arg_9p);
3140 3d54abc7 Gautham R Shenoy
                break;
3141 3d54abc7 Gautham R Shenoy
            }
3142 74db920c Gautham R Shenoy
#endif
3143 82c643ff bellard
            case QEMU_OPTION_serial:
3144 998bbd74 Gerd Hoffmann
                add_device_config(DEV_SERIAL, optarg);
3145 998bbd74 Gerd Hoffmann
                default_serial = 0;
3146 18141ed6 Jan Kiszka
                if (strncmp(optarg, "mon:", 4) == 0) {
3147 18141ed6 Jan Kiszka
                    default_monitor = 0;
3148 18141ed6 Jan Kiszka
                }
3149 82c643ff bellard
                break;
3150 9dd986cc Richard W.M. Jones
            case QEMU_OPTION_watchdog:
3151 09aaa160 Markus Armbruster
                if (watchdog) {
3152 09aaa160 Markus Armbruster
                    fprintf(stderr,
3153 09aaa160 Markus Armbruster
                            "qemu: only one watchdog option may be given\n");
3154 09aaa160 Markus Armbruster
                    return 1;
3155 09aaa160 Markus Armbruster
                }
3156 09aaa160 Markus Armbruster
                watchdog = optarg;
3157 9dd986cc Richard W.M. Jones
                break;
3158 9dd986cc Richard W.M. Jones
            case QEMU_OPTION_watchdog_action:
3159 9dd986cc Richard W.M. Jones
                if (select_watchdog_action(optarg) == -1) {
3160 9dd986cc Richard W.M. Jones
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
3161 9dd986cc Richard W.M. Jones
                    exit(1);
3162 9dd986cc Richard W.M. Jones
                }
3163 9dd986cc Richard W.M. Jones
                break;
3164 51ecf136 aliguori
            case QEMU_OPTION_virtiocon:
3165 aee1b935 Gerd Hoffmann
                add_device_config(DEV_VIRTCON, optarg);
3166 aee1b935 Gerd Hoffmann
                default_virtcon = 0;
3167 18141ed6 Jan Kiszka
                if (strncmp(optarg, "mon:", 4) == 0) {
3168 18141ed6 Jan Kiszka
                    default_monitor = 0;
3169 18141ed6 Jan Kiszka
                }
3170 51ecf136 aliguori
                break;
3171 6508fe59 bellard
            case QEMU_OPTION_parallel:
3172 6a5e8b0e Gerd Hoffmann
                add_device_config(DEV_PARALLEL, optarg);
3173 6a5e8b0e Gerd Hoffmann
                default_parallel = 0;
3174 18141ed6 Jan Kiszka
                if (strncmp(optarg, "mon:", 4) == 0) {
3175 18141ed6 Jan Kiszka
                    default_monitor = 0;
3176 18141ed6 Jan Kiszka
                }
3177 6508fe59 bellard
                break;
3178 c9f398e5 H. Peter Anvin
            case QEMU_OPTION_debugcon:
3179 c9f398e5 H. Peter Anvin
                add_device_config(DEV_DEBUGCON, optarg);
3180 c9f398e5 H. Peter Anvin
                break;
3181 d63d307f bellard
            case QEMU_OPTION_loadvm:
3182 d63d307f bellard
                loadvm = optarg;
3183 d63d307f bellard
                break;
3184 d63d307f bellard
            case QEMU_OPTION_full_screen:
3185 d63d307f bellard
                full_screen = 1;
3186 d63d307f bellard
                break;
3187 667accab ths
#ifdef CONFIG_SDL
3188 43523e93 ths
            case QEMU_OPTION_no_frame:
3189 43523e93 ths
                no_frame = 1;
3190 43523e93 ths
                break;
3191 3780e197 ths
            case QEMU_OPTION_alt_grab:
3192 3780e197 ths
                alt_grab = 1;
3193 3780e197 ths
                break;
3194 0ca9f8a4 Dustin Kirkland
            case QEMU_OPTION_ctrl_grab:
3195 0ca9f8a4 Dustin Kirkland
                ctrl_grab = 1;
3196 0ca9f8a4 Dustin Kirkland
                break;
3197 667accab ths
            case QEMU_OPTION_no_quit:
3198 667accab ths
                no_quit = 1;
3199 667accab ths
                break;
3200 7d957bd8 aliguori
            case QEMU_OPTION_sdl:
3201 993fbfdb Anthony Liguori
                display_type = DT_SDL;
3202 7d957bd8 aliguori
                break;
3203 667accab ths
#endif
3204 f7cce898 bellard
            case QEMU_OPTION_pidfile:
3205 93815bc2 ths
                pid_file = optarg;
3206 f7cce898 bellard
                break;
3207 a09db21f bellard
            case QEMU_OPTION_win2k_hack:
3208 a09db21f bellard
                win2k_install_hack = 1;
3209 a09db21f bellard
                break;
3210 73822ec8 aliguori
            case QEMU_OPTION_rtc_td_hack:
3211 73822ec8 aliguori
                rtc_td_hack = 1;
3212 73822ec8 aliguori
                break;
3213 8a92ea2f aliguori
            case QEMU_OPTION_acpitable:
3214 de06f8d1 Blue Swirl
                do_acpitable_option(optarg);
3215 8a92ea2f aliguori
                break;
3216 b6f6e3d3 aliguori
            case QEMU_OPTION_smbios:
3217 de06f8d1 Blue Swirl
                do_smbios_option(optarg);
3218 b6f6e3d3 aliguori
                break;
3219 7ba1e619 aliguori
            case QEMU_OPTION_enable_kvm:
3220 7ba1e619 aliguori
                kvm_allowed = 1;
3221 7ba1e619 aliguori
                break;
3222 bb36d470 bellard
            case QEMU_OPTION_usb:
3223 bb36d470 bellard
                usb_enabled = 1;
3224 bb36d470 bellard
                break;
3225 a594cfbf bellard
            case QEMU_OPTION_usbdevice:
3226 a594cfbf bellard
                usb_enabled = 1;
3227 bd3c948d Gerd Hoffmann
                add_device_config(DEV_USB, optarg);
3228 bd3c948d Gerd Hoffmann
                break;
3229 bd3c948d Gerd Hoffmann
            case QEMU_OPTION_device:
3230 8212c64f Markus Armbruster
                if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
3231 f31d07d1 Gerd Hoffmann
                    exit(1);
3232 f31d07d1 Gerd Hoffmann
                }
3233 a594cfbf bellard
                break;
3234 6a00d601 bellard
            case QEMU_OPTION_smp:
3235 dc6b1c09 Andre Przywara
                smp_parse(optarg);
3236 b2097003 aliguori
                if (smp_cpus < 1) {
3237 6a00d601 bellard
                    fprintf(stderr, "Invalid number of CPUs\n");
3238 6a00d601 bellard
                    exit(1);
3239 6a00d601 bellard
                }
3240 6be68d7e Jes Sorensen
                if (max_cpus < smp_cpus) {
3241 6be68d7e Jes Sorensen
                    fprintf(stderr, "maxcpus must be equal to or greater than "
3242 6be68d7e Jes Sorensen
                            "smp\n");
3243 6be68d7e Jes Sorensen
                    exit(1);
3244 6be68d7e Jes Sorensen
                }
3245 6be68d7e Jes Sorensen
                if (max_cpus > 255) {
3246 6be68d7e Jes Sorensen
                    fprintf(stderr, "Unsupported number of maxcpus\n");
3247 6be68d7e Jes Sorensen
                    exit(1);
3248 6be68d7e Jes Sorensen
                }
3249 6a00d601 bellard
                break;
3250 24236869 bellard
            case QEMU_OPTION_vnc:
3251 993fbfdb Anthony Liguori
                display_type = DT_VNC;
3252 73fc9742 ths
                vnc_display = optarg;
3253 24236869 bellard
                break;
3254 6515b203 bellard
            case QEMU_OPTION_no_acpi:
3255 6515b203 bellard
                acpi_enabled = 0;
3256 6515b203 bellard
                break;
3257 16b29ae1 aliguori
            case QEMU_OPTION_no_hpet:
3258 16b29ae1 aliguori
                no_hpet = 1;
3259 16b29ae1 aliguori
                break;
3260 7d4c3d53 Markus Armbruster
            case QEMU_OPTION_balloon:
3261 7d4c3d53 Markus Armbruster
                if (balloon_parse(optarg) < 0) {
3262 7d4c3d53 Markus Armbruster
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3263 7d4c3d53 Markus Armbruster
                    exit(1);
3264 7d4c3d53 Markus Armbruster
                }
3265 df97b920 Eduardo Habkost
                break;
3266 d1beab82 bellard
            case QEMU_OPTION_no_reboot:
3267 d1beab82 bellard
                no_reboot = 1;
3268 d1beab82 bellard
                break;
3269 b2f76161 aurel32
            case QEMU_OPTION_no_shutdown:
3270 b2f76161 aurel32
                no_shutdown = 1;
3271 b2f76161 aurel32
                break;
3272 9467cd46 balrog
            case QEMU_OPTION_show_cursor:
3273 9467cd46 balrog
                cursor_hide = 0;
3274 9467cd46 balrog
                break;
3275 8fcb1b90 blueswir1
            case QEMU_OPTION_uuid:
3276 8fcb1b90 blueswir1
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3277 8fcb1b90 blueswir1
                    fprintf(stderr, "Fail to parse UUID string."
3278 8fcb1b90 blueswir1
                            " Wrong format.\n");
3279 8fcb1b90 blueswir1
                    exit(1);
3280 8fcb1b90 blueswir1
                }
3281 8fcb1b90 blueswir1
                break;
3282 5824d651 blueswir1
#ifndef _WIN32
3283 71e3ceb8 ths
            case QEMU_OPTION_daemonize:
3284 71e3ceb8 ths
                daemonize = 1;
3285 71e3ceb8 ths
                break;
3286 5824d651 blueswir1
#endif
3287 9ae02555 ths
            case QEMU_OPTION_option_rom:
3288 9ae02555 ths
                if (nb_option_roms >= MAX_OPTION_ROMS) {
3289 9ae02555 ths
                    fprintf(stderr, "Too many option ROMs\n");
3290 9ae02555 ths
                    exit(1);
3291 9ae02555 ths
                }
3292 9ae02555 ths
                option_rom[nb_option_roms] = optarg;
3293 9ae02555 ths
                nb_option_roms++;
3294 9ae02555 ths
                break;
3295 8e71621f pbrook
            case QEMU_OPTION_semihosting:
3296 8e71621f pbrook
                semihosting_enabled = 1;
3297 8e71621f pbrook
                break;
3298 c35734b2 ths
            case QEMU_OPTION_name:
3299 1889465a Andi Kleen
                qemu_name = qemu_strdup(optarg);
3300 1889465a Andi Kleen
                 {
3301 1889465a Andi Kleen
                     char *p = strchr(qemu_name, ',');
3302 1889465a Andi Kleen
                     if (p != NULL) {
3303 1889465a Andi Kleen
                        *p++ = 0;
3304 1889465a Andi Kleen
                        if (strncmp(p, "process=", 8)) {
3305 1889465a Andi Kleen
                            fprintf(stderr, "Unknown subargument %s to -name", p);
3306 1889465a Andi Kleen
                            exit(1);
3307 1889465a Andi Kleen
                        }
3308 1889465a Andi Kleen
                        p += 8;
3309 1889465a Andi Kleen
                        set_proc_name(p);
3310 1889465a Andi Kleen
                     }        
3311 1889465a Andi Kleen
                 }        
3312 c35734b2 ths
                break;
3313 66508601 blueswir1
            case QEMU_OPTION_prom_env:
3314 66508601 blueswir1
                if (nb_prom_envs >= MAX_PROM_ENVS) {
3315 66508601 blueswir1
                    fprintf(stderr, "Too many prom variables\n");
3316 66508601 blueswir1
                    exit(1);
3317 66508601 blueswir1
                }
3318 66508601 blueswir1
                prom_envs[nb_prom_envs] = optarg;
3319 66508601 blueswir1
                nb_prom_envs++;
3320 66508601 blueswir1
                break;
3321 2b8f2d41 balrog
            case QEMU_OPTION_old_param:
3322 2b8f2d41 balrog
                old_param = 1;
3323 05ebd537 ths
                break;
3324 f3dcfada ths
            case QEMU_OPTION_clock:
3325 f3dcfada ths
                configure_alarms(optarg);
3326 f3dcfada ths
                break;
3327 7e0af5d0 bellard
            case QEMU_OPTION_startdate:
3328 1ed2fc1f Jan Kiszka
                configure_rtc_date_offset(optarg, 1);
3329 1ed2fc1f Jan Kiszka
                break;
3330 1ed2fc1f Jan Kiszka
            case QEMU_OPTION_rtc:
3331 8212c64f Markus Armbruster
                opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
3332 1ed2fc1f Jan Kiszka
                if (!opts) {
3333 1ed2fc1f Jan Kiszka
                    exit(1);
3334 7e0af5d0 bellard
                }
3335 1ed2fc1f Jan Kiszka
                configure_rtc(opts);
3336 7e0af5d0 bellard
                break;
3337 26a5f13b bellard
            case QEMU_OPTION_tb_size:
3338 26a5f13b bellard
                tb_size = strtol(optarg, NULL, 0);
3339 26a5f13b bellard
                if (tb_size < 0)
3340 26a5f13b bellard
                    tb_size = 0;
3341 26a5f13b bellard
                break;
3342 2e70f6ef pbrook
            case QEMU_OPTION_icount:
3343 4e3de9e9 Paolo Bonzini
                icount_option = optarg;
3344 2e70f6ef pbrook
                break;
3345 5bb7910a aliguori
            case QEMU_OPTION_incoming:
3346 5bb7910a aliguori
                incoming = optarg;
3347 5bb7910a aliguori
                break;
3348 d8c208dd Gerd Hoffmann
            case QEMU_OPTION_nodefaults:
3349 d8c208dd Gerd Hoffmann
                default_serial = 0;
3350 d8c208dd Gerd Hoffmann
                default_parallel = 0;
3351 aee1b935 Gerd Hoffmann
                default_virtcon = 0;
3352 d8c208dd Gerd Hoffmann
                default_monitor = 0;
3353 d8c208dd Gerd Hoffmann
                default_vga = 0;
3354 cb4522cc Gerd Hoffmann
                default_net = 0;
3355 ac33f8fa Gerd Hoffmann
                default_floppy = 0;
3356 ac33f8fa Gerd Hoffmann
                default_cdrom = 0;
3357 ac33f8fa Gerd Hoffmann
                default_sdcard = 0;
3358 d8c208dd Gerd Hoffmann
                break;
3359 5824d651 blueswir1
#ifndef _WIN32
3360 0858532e aliguori
            case QEMU_OPTION_chroot:
3361 0858532e aliguori
                chroot_dir = optarg;
3362 0858532e aliguori
                break;
3363 0858532e aliguori
            case QEMU_OPTION_runas:
3364 0858532e aliguori
                run_as = optarg;
3365 0858532e aliguori
                break;
3366 5824d651 blueswir1
#endif
3367 e37630ca aliguori
            case QEMU_OPTION_xen_domid:
3368 ad96090a Blue Swirl
                if (!(xen_available())) {
3369 ad96090a Blue Swirl
                    printf("Option %s not supported for this target\n", popt->name);
3370 ad96090a Blue Swirl
                    exit(1);
3371 ad96090a Blue Swirl
                }
3372 e37630ca aliguori
                xen_domid = atoi(optarg);
3373 e37630ca aliguori
                break;
3374 e37630ca aliguori
            case QEMU_OPTION_xen_create:
3375 ad96090a Blue Swirl
                if (!(xen_available())) {
3376 ad96090a Blue Swirl
                    printf("Option %s not supported for this target\n", popt->name);
3377 ad96090a Blue Swirl
                    exit(1);
3378 ad96090a Blue Swirl
                }
3379 e37630ca aliguori
                xen_mode = XEN_CREATE;
3380 e37630ca aliguori
                break;
3381 e37630ca aliguori
            case QEMU_OPTION_xen_attach:
3382 ad96090a Blue Swirl
                if (!(xen_available())) {
3383 ad96090a Blue Swirl
                    printf("Option %s not supported for this target\n", popt->name);
3384 ad96090a Blue Swirl
                    exit(1);
3385 ad96090a Blue Swirl
                }
3386 e37630ca aliguori
                xen_mode = XEN_ATTACH;
3387 e37630ca aliguori
                break;
3388 715a664a Gerd Hoffmann
            case QEMU_OPTION_readconfig:
3389 715a664a Gerd Hoffmann
                {
3390 dcfb0939 Kevin Wolf
                    int ret = qemu_read_config_file(optarg);
3391 dcfb0939 Kevin Wolf
                    if (ret < 0) {
3392 dcfb0939 Kevin Wolf
                        fprintf(stderr, "read config %s: %s\n", optarg,
3393 dcfb0939 Kevin Wolf
                            strerror(-ret));
3394 715a664a Gerd Hoffmann
                        exit(1);
3395 715a664a Gerd Hoffmann
                    }
3396 715a664a Gerd Hoffmann
                    break;
3397 715a664a Gerd Hoffmann
                }
3398 715a664a Gerd Hoffmann
            case QEMU_OPTION_writeconfig:
3399 715a664a Gerd Hoffmann
                {
3400 715a664a Gerd Hoffmann
                    FILE *fp;
3401 715a664a Gerd Hoffmann
                    if (strcmp(optarg, "-") == 0) {
3402 715a664a Gerd Hoffmann
                        fp = stdout;
3403 715a664a Gerd Hoffmann
                    } else {
3404 715a664a Gerd Hoffmann
                        fp = fopen(optarg, "w");
3405 715a664a Gerd Hoffmann
                        if (fp == NULL) {
3406 715a664a Gerd Hoffmann
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3407 715a664a Gerd Hoffmann
                            exit(1);
3408 715a664a Gerd Hoffmann
                        }
3409 715a664a Gerd Hoffmann
                    }
3410 715a664a Gerd Hoffmann
                    qemu_config_write(fp);
3411 715a664a Gerd Hoffmann
                    fclose(fp);
3412 715a664a Gerd Hoffmann
                    break;
3413 715a664a Gerd Hoffmann
                }
3414 cd6f1169 bellard
            }
3415 0824d6fc bellard
        }
3416 0824d6fc bellard
    }
3417 0f0bc3f1 Markus Armbruster
    loc_set_none();
3418 330d0414 bellard
3419 5cea8590 Paul Brook
    /* If no data_dir is specified then try to find it relative to the
3420 5cea8590 Paul Brook
       executable path.  */
3421 5cea8590 Paul Brook
    if (!data_dir) {
3422 5cea8590 Paul Brook
        data_dir = find_datadir(argv[0]);
3423 5cea8590 Paul Brook
    }
3424 5cea8590 Paul Brook
    /* If all else fails use the install patch specified when building.  */
3425 5cea8590 Paul Brook
    if (!data_dir) {
3426 1dabe05c Paolo Bonzini
        data_dir = CONFIG_QEMU_DATADIR;
3427 5cea8590 Paul Brook
    }
3428 5cea8590 Paul Brook
3429 6be68d7e Jes Sorensen
    /*
3430 6be68d7e Jes Sorensen
     * Default to max_cpus = smp_cpus, in case the user doesn't
3431 6be68d7e Jes Sorensen
     * specify a max_cpus value.
3432 6be68d7e Jes Sorensen
     */
3433 6be68d7e Jes Sorensen
    if (!max_cpus)
3434 6be68d7e Jes Sorensen
        max_cpus = smp_cpus;
3435 6be68d7e Jes Sorensen
3436 3d878caa balrog
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3437 b2097003 aliguori
    if (smp_cpus > machine->max_cpus) {
3438 b2097003 aliguori
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3439 b2097003 aliguori
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
3440 b2097003 aliguori
                machine->max_cpus);
3441 b2097003 aliguori
        exit(1);
3442 b2097003 aliguori
    }
3443 b2097003 aliguori
3444 998bbd74 Gerd Hoffmann
    qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
3445 d8bcbabf Gerd Hoffmann
    qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
3446 998bbd74 Gerd Hoffmann
3447 986c5f78 Gerd Hoffmann
    if (machine->no_serial) {
3448 986c5f78 Gerd Hoffmann
        default_serial = 0;
3449 986c5f78 Gerd Hoffmann
    }
3450 986c5f78 Gerd Hoffmann
    if (machine->no_parallel) {
3451 986c5f78 Gerd Hoffmann
        default_parallel = 0;
3452 986c5f78 Gerd Hoffmann
    }
3453 986c5f78 Gerd Hoffmann
    if (!machine->use_virtcon) {
3454 986c5f78 Gerd Hoffmann
        default_virtcon = 0;
3455 986c5f78 Gerd Hoffmann
    }
3456 986c5f78 Gerd Hoffmann
    if (machine->no_vga) {
3457 986c5f78 Gerd Hoffmann
        default_vga = 0;
3458 986c5f78 Gerd Hoffmann
    }
3459 ac33f8fa Gerd Hoffmann
    if (machine->no_floppy) {
3460 ac33f8fa Gerd Hoffmann
        default_floppy = 0;
3461 ac33f8fa Gerd Hoffmann
    }
3462 ac33f8fa Gerd Hoffmann
    if (machine->no_cdrom) {
3463 ac33f8fa Gerd Hoffmann
        default_cdrom = 0;
3464 ac33f8fa Gerd Hoffmann
    }
3465 ac33f8fa Gerd Hoffmann
    if (machine->no_sdcard) {
3466 ac33f8fa Gerd Hoffmann
        default_sdcard = 0;
3467 ac33f8fa Gerd Hoffmann
    }
3468 986c5f78 Gerd Hoffmann
3469 993fbfdb Anthony Liguori
    if (display_type == DT_NOGRAPHIC) {
3470 6a5e8b0e Gerd Hoffmann
        if (default_parallel)
3471 6a5e8b0e Gerd Hoffmann
            add_device_config(DEV_PARALLEL, "null");
3472 e1c09175 Gerd Hoffmann
        if (default_serial && default_monitor) {
3473 e1c09175 Gerd Hoffmann
            add_device_config(DEV_SERIAL, "mon:stdio");
3474 986c5f78 Gerd Hoffmann
        } else if (default_virtcon && default_monitor) {
3475 986c5f78 Gerd Hoffmann
            add_device_config(DEV_VIRTCON, "mon:stdio");
3476 e1c09175 Gerd Hoffmann
        } else {
3477 e1c09175 Gerd Hoffmann
            if (default_serial)
3478 e1c09175 Gerd Hoffmann
                add_device_config(DEV_SERIAL, "stdio");
3479 986c5f78 Gerd Hoffmann
            if (default_virtcon)
3480 986c5f78 Gerd Hoffmann
                add_device_config(DEV_VIRTCON, "stdio");
3481 e1c09175 Gerd Hoffmann
            if (default_monitor)
3482 6ca5582d Gerd Hoffmann
                monitor_parse("stdio", "readline");
3483 e1c09175 Gerd Hoffmann
        }
3484 998bbd74 Gerd Hoffmann
    } else {
3485 998bbd74 Gerd Hoffmann
        if (default_serial)
3486 998bbd74 Gerd Hoffmann
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
3487 6a5e8b0e Gerd Hoffmann
        if (default_parallel)
3488 6a5e8b0e Gerd Hoffmann
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3489 abdeed06 Gerd Hoffmann
        if (default_monitor)
3490 6ca5582d Gerd Hoffmann
            monitor_parse("vc:80Cx24C", "readline");
3491 38536da1 Alexander Graf
        if (default_virtcon)
3492 38536da1 Alexander Graf
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3493 bc0129d9 aliguori
    }
3494 64465297 Gerd Hoffmann
    if (default_vga)
3495 64465297 Gerd Hoffmann
        vga_interface_type = VGA_CIRRUS;
3496 bc0129d9 aliguori
3497 a5829fd9 TeLeMan
    socket_init();
3498 a5829fd9 TeLeMan
3499 1a688d3b Gerd Hoffmann
    if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3500 1a688d3b Gerd Hoffmann
        exit(1);
3501 74db920c Gautham R Shenoy
#ifdef CONFIG_LINUX
3502 74db920c Gautham R Shenoy
    if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3503 74db920c Gautham R Shenoy
        exit(1);
3504 74db920c Gautham R Shenoy
    }
3505 74db920c Gautham R Shenoy
#endif
3506 1a688d3b Gerd Hoffmann
3507 71e3ceb8 ths
#ifndef _WIN32
3508 71e3ceb8 ths
    if (daemonize) {
3509 71e3ceb8 ths
        pid_t pid;
3510 71e3ceb8 ths
3511 71e3ceb8 ths
        if (pipe(fds) == -1)
3512 71e3ceb8 ths
            exit(1);
3513 71e3ceb8 ths
3514 71e3ceb8 ths
        pid = fork();
3515 71e3ceb8 ths
        if (pid > 0) {
3516 71e3ceb8 ths
            uint8_t status;
3517 71e3ceb8 ths
            ssize_t len;
3518 71e3ceb8 ths
3519 71e3ceb8 ths
            close(fds[1]);
3520 71e3ceb8 ths
3521 71e3ceb8 ths
        again:
3522 93815bc2 ths
            len = read(fds[0], &status, 1);
3523 93815bc2 ths
            if (len == -1 && (errno == EINTR))
3524 93815bc2 ths
                goto again;
3525 93815bc2 ths
3526 93815bc2 ths
            if (len != 1)
3527 93815bc2 ths
                exit(1);
3528 93815bc2 ths
            else if (status == 1) {
3529 850810d0 Justin M. Forbes
                fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
3530 93815bc2 ths
                exit(1);
3531 93815bc2 ths
            } else
3532 93815bc2 ths
                exit(0);
3533 71e3ceb8 ths
        } else if (pid < 0)
3534 93815bc2 ths
            exit(1);
3535 71e3ceb8 ths
3536 40ff6d7e Kevin Wolf
        close(fds[0]);
3537 40ff6d7e Kevin Wolf
        qemu_set_cloexec(fds[1]);
3538 40ff6d7e Kevin Wolf
3539 71e3ceb8 ths
        setsid();
3540 71e3ceb8 ths
3541 71e3ceb8 ths
        pid = fork();
3542 71e3ceb8 ths
        if (pid > 0)
3543 71e3ceb8 ths
            exit(0);
3544 71e3ceb8 ths
        else if (pid < 0)
3545 71e3ceb8 ths
            exit(1);
3546 71e3ceb8 ths
3547 71e3ceb8 ths
        umask(027);
3548 71e3ceb8 ths
3549 71e3ceb8 ths
        signal(SIGTSTP, SIG_IGN);
3550 71e3ceb8 ths
        signal(SIGTTOU, SIG_IGN);
3551 71e3ceb8 ths
        signal(SIGTTIN, SIG_IGN);
3552 71e3ceb8 ths
    }
3553 099fe236 Juha Riihimรคki
#endif
3554 71e3ceb8 ths
3555 aa26bb2d ths
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3556 099fe236 Juha Riihimรคki
#ifndef _WIN32
3557 93815bc2 ths
        if (daemonize) {
3558 93815bc2 ths
            uint8_t status = 1;
3559 dc330e28 Kirill A. Shutemov
            if (write(fds[1], &status, 1) != 1) {
3560 dc330e28 Kirill A. Shutemov
                perror("daemonize. Writing to pipe\n");
3561 dc330e28 Kirill A. Shutemov
            }
3562 93815bc2 ths
        } else
3563 099fe236 Juha Riihimรคki
#endif
3564 850810d0 Justin M. Forbes
            fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
3565 93815bc2 ths
        exit(1);
3566 93815bc2 ths
    }
3567 93815bc2 ths
3568 98c8573e Paolo Bonzini
    if (kvm_allowed) {
3569 98c8573e Paolo Bonzini
        int ret = kvm_init(smp_cpus);
3570 214910a7 Marcelo Tosatti
        if (ret < 0) {
3571 98c8573e Paolo Bonzini
            if (!kvm_available()) {
3572 98c8573e Paolo Bonzini
                printf("KVM not supported for this target\n");
3573 98c8573e Paolo Bonzini
            } else {
3574 98c8573e Paolo Bonzini
                fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3575 98c8573e Paolo Bonzini
            }
3576 214910a7 Marcelo Tosatti
            exit(1);
3577 214910a7 Marcelo Tosatti
        }
3578 214910a7 Marcelo Tosatti
    }
3579 214910a7 Marcelo Tosatti
3580 3fcf7b6b aliguori
    if (qemu_init_main_loop()) {
3581 3fcf7b6b aliguori
        fprintf(stderr, "qemu_init_main_loop failed\n");
3582 3fcf7b6b aliguori
        exit(1);
3583 3fcf7b6b aliguori
    }
3584 a20dd508 bellard
    linux_boot = (kernel_filename != NULL);
3585 6c41b272 balrog
3586 f8d39c01 ths
    if (!linux_boot && *kernel_cmdline != '\0') {
3587 f8d39c01 ths
        fprintf(stderr, "-append only allowed with -kernel option\n");
3588 f8d39c01 ths
        exit(1);
3589 f8d39c01 ths
    }
3590 f8d39c01 ths
3591 f8d39c01 ths
    if (!linux_boot && initrd_filename != NULL) {
3592 f8d39c01 ths
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
3593 f8d39c01 ths
        exit(1);
3594 f8d39c01 ths
    }
3595 f8d39c01 ths
3596 bf65f53f Filip Navara
#ifndef _WIN32
3597 bf65f53f Filip Navara
    /* Win32 doesn't support line-buffering and requires size >= 2 */
3598 b118d61e bellard
    setvbuf(stdout, NULL, _IOLBF, 0);
3599 bf65f53f Filip Navara
#endif
3600 3b46e624 ths
3601 7183b4b4 aliguori
    if (init_timer_alarm() < 0) {
3602 7183b4b4 aliguori
        fprintf(stderr, "could not initialize alarm timer\n");
3603 7183b4b4 aliguori
        exit(1);
3604 7183b4b4 aliguori
    }
3605 4e3de9e9 Paolo Bonzini
    configure_icount(icount_option);
3606 634fce96 pbrook
3607 dc1c9fe8 Mark McLoughlin
    if (net_init_clients() < 0) {
3608 dc1c9fe8 Mark McLoughlin
        exit(1);
3609 702c651c bellard
    }
3610 f1510b2c bellard
3611 dc72ac14 balrog
    /* init the bluetooth world */
3612 bd3c948d Gerd Hoffmann
    if (foreach_device_config(DEV_BT, bt_parse))
3613 bd3c948d Gerd Hoffmann
        exit(1);
3614 dc72ac14 balrog
3615 0824d6fc bellard
    /* init the memory */
3616 94a6b54f pbrook
    if (ram_size == 0)
3617 94a6b54f pbrook
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3618 9ae02555 ths
3619 26a5f13b bellard
    /* init the dynamic translator */
3620 26a5f13b bellard
    cpu_exec_init_all(tb_size * 1024 * 1024);
3621 26a5f13b bellard
3622 eb852011 Markus Armbruster
    bdrv_init_with_whitelist();
3623 c4b1fcc0 bellard
3624 c163b5ca lirans@il.ibm.com
    blk_mig_init();
3625 c163b5ca lirans@il.ibm.com
3626 ac33f8fa Gerd Hoffmann
    if (default_cdrom) {
3627 aa40fc9c Gerd Hoffmann
        /* we always create the cdrom drive, even if no disk is there */
3628 aa40fc9c Gerd Hoffmann
        drive_add(NULL, CDROM_ALIAS);
3629 ac33f8fa Gerd Hoffmann
    }
3630 c4b1fcc0 bellard
3631 ac33f8fa Gerd Hoffmann
    if (default_floppy) {
3632 aa40fc9c Gerd Hoffmann
        /* we always create at least one floppy */
3633 aa40fc9c Gerd Hoffmann
        drive_add(NULL, FD_ALIAS, 0);
3634 ac33f8fa Gerd Hoffmann
    }
3635 86f55663 j_mayer
3636 ac33f8fa Gerd Hoffmann
    if (default_sdcard) {
3637 aa40fc9c Gerd Hoffmann
        /* we always create one sd slot, even if no card is in it */
3638 aa40fc9c Gerd Hoffmann
        drive_add(NULL, SD_ALIAS);
3639 aa40fc9c Gerd Hoffmann
    }
3640 9d413d1d balrog
3641 e4bcb14c ths
    /* open the virtual block devices */
3642 9dfd7c7a Gerd Hoffmann
    if (snapshot)
3643 7282a033 Gerd Hoffmann
        qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3644 7282a033 Gerd Hoffmann
    if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
3645 9dfd7c7a Gerd Hoffmann
        exit(1);
3646 3e3d5815 balrog
3647 c163b5ca lirans@il.ibm.com
    register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, 
3648 c163b5ca lirans@il.ibm.com
                         ram_load, NULL);
3649 8a7ddc38 bellard
3650 268a362c aliguori
    if (nb_numa_nodes > 0) {
3651 268a362c aliguori
        int i;
3652 268a362c aliguori
3653 268a362c aliguori
        if (nb_numa_nodes > smp_cpus) {
3654 268a362c aliguori
            nb_numa_nodes = smp_cpus;
3655 268a362c aliguori
        }
3656 268a362c aliguori
3657 268a362c aliguori
        /* If no memory size if given for any node, assume the default case
3658 268a362c aliguori
         * and distribute the available memory equally across all nodes
3659 268a362c aliguori
         */
3660 268a362c aliguori
        for (i = 0; i < nb_numa_nodes; i++) {
3661 268a362c aliguori
            if (node_mem[i] != 0)
3662 268a362c aliguori
                break;
3663 268a362c aliguori
        }
3664 268a362c aliguori
        if (i == nb_numa_nodes) {
3665 268a362c aliguori
            uint64_t usedmem = 0;
3666 268a362c aliguori
3667 268a362c aliguori
            /* On Linux, the each node's border has to be 8MB aligned,
3668 268a362c aliguori
             * the final node gets the rest.
3669 268a362c aliguori
             */
3670 268a362c aliguori
            for (i = 0; i < nb_numa_nodes - 1; i++) {
3671 268a362c aliguori
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3672 268a362c aliguori
                usedmem += node_mem[i];
3673 268a362c aliguori
            }
3674 268a362c aliguori
            node_mem[i] = ram_size - usedmem;
3675 268a362c aliguori
        }
3676 268a362c aliguori
3677 268a362c aliguori
        for (i = 0; i < nb_numa_nodes; i++) {
3678 268a362c aliguori
            if (node_cpumask[i] != 0)
3679 268a362c aliguori
                break;
3680 268a362c aliguori
        }
3681 268a362c aliguori
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
3682 268a362c aliguori
         * must cope with this anyway, because there are BIOSes out there in
3683 268a362c aliguori
         * real machines which also use this scheme.
3684 268a362c aliguori
         */
3685 268a362c aliguori
        if (i == nb_numa_nodes) {
3686 268a362c aliguori
            for (i = 0; i < smp_cpus; i++) {
3687 268a362c aliguori
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
3688 268a362c aliguori
            }
3689 268a362c aliguori
        }
3690 268a362c aliguori
    }
3691 268a362c aliguori
3692 157b9319 Jan Kiszka
    if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3693 157b9319 Jan Kiszka
        exit(1);
3694 157b9319 Jan Kiszka
    }
3695 157b9319 Jan Kiszka
3696 998bbd74 Gerd Hoffmann
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3697 998bbd74 Gerd Hoffmann
        exit(1);
3698 6a5e8b0e Gerd Hoffmann
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3699 6a5e8b0e Gerd Hoffmann
        exit(1);
3700 aee1b935 Gerd Hoffmann
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3701 aee1b935 Gerd Hoffmann
        exit(1);
3702 c9f398e5 H. Peter Anvin
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3703 c9f398e5 H. Peter Anvin
        exit(1);
3704 2796dae0 aliguori
3705 aae9460e Paul Brook
    module_call_init(MODULE_INIT_DEVICE);
3706 aae9460e Paul Brook
3707 ff952ba2 Markus Armbruster
    if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3708 ff952ba2 Markus Armbruster
        exit(0);
3709 ff952ba2 Markus Armbruster
3710 09aaa160 Markus Armbruster
    if (watchdog) {
3711 09aaa160 Markus Armbruster
        i = select_watchdog(watchdog);
3712 09aaa160 Markus Armbruster
        if (i > 0)
3713 09aaa160 Markus Armbruster
            exit (i == 1 ? 1 : 0);
3714 09aaa160 Markus Armbruster
    }
3715 09aaa160 Markus Armbruster
3716 b6b61144 Gerd Hoffmann
    if (machine->compat_props) {
3717 458fb679 Gerd Hoffmann
        qdev_prop_register_global_list(machine->compat_props);
3718 b6b61144 Gerd Hoffmann
    }
3719 d0fef6fb Gerd Hoffmann
    qemu_add_globals();
3720 d0fef6fb Gerd Hoffmann
3721 fbe1b595 Paul Brook
    machine->init(ram_size, boot_devices,
3722 3023f332 aliguori
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3723 3023f332 aliguori
3724 ea375f9a Jan Kiszka
    cpu_synchronize_all_post_init();
3725 268a362c aliguori
3726 67b3b71d Juan Quintela
#ifndef _WIN32
3727 67b3b71d Juan Quintela
    /* must be after terminal init, SDL library changes signal handlers */
3728 67b3b71d Juan Quintela
    sighandler_setup();
3729 67b3b71d Juan Quintela
#endif
3730 67b3b71d Juan Quintela
3731 87d0a28e Blue Swirl
    set_numa_modes();
3732 268a362c aliguori
3733 6f338c34 aliguori
    current_machine = machine;
3734 6f338c34 aliguori
3735 3023f332 aliguori
    /* init USB devices */
3736 3023f332 aliguori
    if (usb_enabled) {
3737 0752706d Markus Armbruster
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
3738 0752706d Markus Armbruster
            exit(1);
3739 3023f332 aliguori
    }
3740 3023f332 aliguori
3741 bd3c948d Gerd Hoffmann
    /* init generic devices */
3742 f31d07d1 Gerd Hoffmann
    if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
3743 bd3c948d Gerd Hoffmann
        exit(1);
3744 bd3c948d Gerd Hoffmann
3745 668680f7 Markus Armbruster
    net_check_clients();
3746 668680f7 Markus Armbruster
3747 3023f332 aliguori
    /* just use the first displaystate for the moment */
3748 b473df6e Paolo Bonzini
    ds = get_displaystate();
3749 993fbfdb Anthony Liguori
3750 993fbfdb Anthony Liguori
    if (display_type == DT_DEFAULT) {
3751 993fbfdb Anthony Liguori
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3752 993fbfdb Anthony Liguori
        display_type = DT_SDL;
3753 993fbfdb Anthony Liguori
#else
3754 993fbfdb Anthony Liguori
        display_type = DT_VNC;
3755 993fbfdb Anthony Liguori
        vnc_display = "localhost:0,to=99";
3756 993fbfdb Anthony Liguori
        show_vnc_port = 1;
3757 993fbfdb Anthony Liguori
#endif
3758 993fbfdb Anthony Liguori
    }
3759 993fbfdb Anthony Liguori
        
3760 993fbfdb Anthony Liguori
3761 993fbfdb Anthony Liguori
    switch (display_type) {
3762 993fbfdb Anthony Liguori
    case DT_NOGRAPHIC:
3763 993fbfdb Anthony Liguori
        break;
3764 4d3b6f6e balrog
#if defined(CONFIG_CURSES)
3765 993fbfdb Anthony Liguori
    case DT_CURSES:
3766 993fbfdb Anthony Liguori
        curses_display_init(ds, full_screen);
3767 993fbfdb Anthony Liguori
        break;
3768 4d3b6f6e balrog
#endif
3769 5b0753e0 bellard
#if defined(CONFIG_SDL)
3770 993fbfdb Anthony Liguori
    case DT_SDL:
3771 993fbfdb Anthony Liguori
        sdl_display_init(ds, full_screen, no_frame);
3772 993fbfdb Anthony Liguori
        break;
3773 5b0753e0 bellard
#elif defined(CONFIG_COCOA)
3774 993fbfdb Anthony Liguori
    case DT_SDL:
3775 993fbfdb Anthony Liguori
        cocoa_display_init(ds, full_screen);
3776 993fbfdb Anthony Liguori
        break;
3777 313aa567 bellard
#endif
3778 993fbfdb Anthony Liguori
    case DT_VNC:
3779 993fbfdb Anthony Liguori
        vnc_display_init(ds);
3780 993fbfdb Anthony Liguori
        if (vnc_display_open(ds, vnc_display) < 0)
3781 993fbfdb Anthony Liguori
            exit(1);
3782 f92f8afe Anthony Liguori
3783 993fbfdb Anthony Liguori
        if (show_vnc_port) {
3784 993fbfdb Anthony Liguori
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3785 f92f8afe Anthony Liguori
        }
3786 993fbfdb Anthony Liguori
        break;
3787 993fbfdb Anthony Liguori
    default:
3788 993fbfdb Anthony Liguori
        break;
3789 313aa567 bellard
    }
3790 7d957bd8 aliguori
    dpy_resize(ds);
3791 5b08fc10 aliguori
3792 3023f332 aliguori
    dcl = ds->listeners;
3793 3023f332 aliguori
    while (dcl != NULL) {
3794 3023f332 aliguori
        if (dcl->dpy_refresh != NULL) {
3795 3023f332 aliguori
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3796 3023f332 aliguori
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3797 20d8a3ed ths
        }
3798 3023f332 aliguori
        dcl = dcl->next;
3799 20d8a3ed ths
    }
3800 3023f332 aliguori
3801 993fbfdb Anthony Liguori
    if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3802 9043b62d blueswir1
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3803 9043b62d blueswir1
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3804 9043b62d blueswir1
    }
3805 9043b62d blueswir1
3806 b473df6e Paolo Bonzini
    text_consoles_set_display(ds);
3807 2796dae0 aliguori
3808 59030a8c aliguori
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3809 59030a8c aliguori
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3810 59030a8c aliguori
                gdbstub_dev);
3811 59030a8c aliguori
        exit(1);
3812 45669e00 balrog
    }
3813 45669e00 balrog
3814 3418bd25 Gerd Hoffmann
    qdev_machine_creation_done();
3815 3418bd25 Gerd Hoffmann
3816 15ff7705 Gerd Hoffmann
    if (rom_load_all() != 0) {
3817 15ff7705 Gerd Hoffmann
        fprintf(stderr, "rom loading failed\n");
3818 15ff7705 Gerd Hoffmann
        exit(1);
3819 15ff7705 Gerd Hoffmann
    }
3820 45a50b16 Gerd Hoffmann
3821 504c2948 Juan Quintela
    qemu_system_reset();
3822 05f2401e Juan Quintela
    if (loadvm) {
3823 03cd4655 Markus Armbruster
        if (load_vmstate(loadvm) < 0) {
3824 05f2401e Juan Quintela
            autostart = 0;
3825 05f2401e Juan Quintela
        }
3826 05f2401e Juan Quintela
    }
3827 d63d307f bellard
3828 2bb8c10c Glauber Costa
    if (incoming) {
3829 5bb7910a aliguori
        qemu_start_incoming_migration(incoming);
3830 6b99dadc Avi Kivity
    } else if (autostart) {
3831 c0f4ce77 aliguori
        vm_start();
3832 6b99dadc Avi Kivity
    }
3833 ffd843bc ths
3834 b9e82a59 blueswir1
#ifndef _WIN32
3835 71e3ceb8 ths
    if (daemonize) {
3836 71e3ceb8 ths
        uint8_t status = 0;
3837 71e3ceb8 ths
        ssize_t len;
3838 71e3ceb8 ths
3839 71e3ceb8 ths
    again1:
3840 71e3ceb8 ths
        len = write(fds[1], &status, 1);
3841 71e3ceb8 ths
        if (len == -1 && (errno == EINTR))
3842 71e3ceb8 ths
            goto again1;
3843 71e3ceb8 ths
3844 71e3ceb8 ths
        if (len != 1)
3845 71e3ceb8 ths
            exit(1);
3846 71e3ceb8 ths
3847 dc330e28 Kirill A. Shutemov
        if (chdir("/")) {
3848 dc330e28 Kirill A. Shutemov
            perror("not able to chdir to /");
3849 dc330e28 Kirill A. Shutemov
            exit(1);
3850 dc330e28 Kirill A. Shutemov
        }
3851 40ff6d7e Kevin Wolf
        TFR(fd = qemu_open("/dev/null", O_RDWR));
3852 71e3ceb8 ths
        if (fd == -1)
3853 71e3ceb8 ths
            exit(1);
3854 0858532e aliguori
    }
3855 71e3ceb8 ths
3856 0858532e aliguori
    if (run_as) {
3857 0858532e aliguori
        pwd = getpwnam(run_as);
3858 0858532e aliguori
        if (!pwd) {
3859 0858532e aliguori
            fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
3860 0858532e aliguori
            exit(1);
3861 0858532e aliguori
        }
3862 0858532e aliguori
    }
3863 0858532e aliguori
3864 0858532e aliguori
    if (chroot_dir) {
3865 0858532e aliguori
        if (chroot(chroot_dir) < 0) {
3866 0858532e aliguori
            fprintf(stderr, "chroot failed\n");
3867 0858532e aliguori
            exit(1);
3868 0858532e aliguori
        }
3869 dc330e28 Kirill A. Shutemov
        if (chdir("/")) {
3870 dc330e28 Kirill A. Shutemov
            perror("not able to chdir to /");
3871 dc330e28 Kirill A. Shutemov
            exit(1);
3872 dc330e28 Kirill A. Shutemov
        }
3873 0858532e aliguori
    }
3874 0858532e aliguori
3875 0858532e aliguori
    if (run_as) {
3876 0858532e aliguori
        if (setgid(pwd->pw_gid) < 0) {
3877 0858532e aliguori
            fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
3878 0858532e aliguori
            exit(1);
3879 0858532e aliguori
        }
3880 0858532e aliguori
        if (setuid(pwd->pw_uid) < 0) {
3881 0858532e aliguori
            fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
3882 0858532e aliguori
            exit(1);
3883 0858532e aliguori
        }
3884 0858532e aliguori
        if (setuid(0) != -1) {
3885 0858532e aliguori
            fprintf(stderr, "Dropping privileges failed\n");
3886 0858532e aliguori
            exit(1);
3887 0858532e aliguori
        }
3888 0858532e aliguori
    }
3889 0858532e aliguori
3890 0858532e aliguori
    if (daemonize) {
3891 0858532e aliguori
        dup2(fd, 0);
3892 0858532e aliguori
        dup2(fd, 1);
3893 0858532e aliguori
        dup2(fd, 2);
3894 71e3ceb8 ths
3895 0858532e aliguori
        close(fd);
3896 71e3ceb8 ths
    }
3897 b9e82a59 blueswir1
#endif
3898 71e3ceb8 ths
3899 8a7ddc38 bellard
    main_loop();
3900 40c3bac3 bellard
    quit_timers();
3901 63a01ef8 aliguori
    net_cleanup();
3902 b46a8906 ths
3903 0824d6fc bellard
    return 0;
3904 0824d6fc bellard
}