blob: 85971b93cad33a75714b246916661890405cadcc [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* the following is needed on Linux to define ptsname() in stdlib.h */
26#if defined(__linux__)
27#define _GNU_SOURCE 1
28#endif
29
30#include "qemu-common.h"
31#include "hw/hw.h"
32#include "hw/boards.h"
33#include "hw/usb.h"
34#include "hw/pcmcia.h"
35#include "hw/pc.h"
36#include "hw/audiodev.h"
37#include "hw/isa.h"
38#include "hw/baum.h"
39#include "hw/goldfish_nand.h"
40#include "net.h"
41#include "console.h"
42#include "sysemu.h"
43#include "gdbstub.h"
44#include "qemu-timer.h"
45#include "qemu-char.h"
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +010046#include "blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070047#include "audio/audio.h"
48
49#include "qemu_file.h"
50#include "android/android.h"
51#include "charpipe.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "modem_driver.h"
53#include "android/gps.h"
54#include "android/hw-qemud.h"
55#include "android/hw-kmsg.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070056#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070057#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070058#include "android/utils/bufprint.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080059#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080060#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010061#include "android/snapshot.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070062#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070063#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070064
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070065#ifdef CONFIG_MEMCHECK
66#include "memcheck/memcheck.h"
67#endif // CONFIG_MEMCHECK
68
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070069#include <unistd.h>
70#include <fcntl.h>
71#include <signal.h>
72#include <time.h>
73#include <errno.h>
74#include <sys/time.h>
75#include <zlib.h>
76
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070077/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070078#include "config-host.h"
79
80#ifndef _WIN32
81#include <libgen.h>
82#include <pwd.h>
83#include <sys/times.h>
84#include <sys/wait.h>
85#include <termios.h>
86#include <sys/mman.h>
87#include <sys/ioctl.h>
88#include <sys/resource.h>
89#include <sys/socket.h>
90#include <netinet/in.h>
91#include <net/if.h>
92#if defined(__NetBSD__)
93#include <net/if_tap.h>
94#endif
95#ifdef __linux__
96#include <linux/if_tun.h>
97#endif
98#include <arpa/inet.h>
99#include <dirent.h>
100#include <netdb.h>
101#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700102#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700103#include <sys/stat.h>
104#if defined(__FreeBSD__) || defined(__DragonFly__)
105#include <libutil.h>
106#else
107#include <util.h>
108#endif
109#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
110#include <freebsd/stdlib.h>
111#else
112#ifdef __linux__
113#include <pty.h>
114#include <malloc.h>
115#include <linux/rtc.h>
116
117/* For the benefit of older linux systems which don't supply it,
118 we use a local copy of hpet.h. */
119/* #include <linux/hpet.h> */
120#include "hpet.h"
121
122#include <linux/ppdev.h>
123#include <linux/parport.h>
124#endif
125#ifdef __sun__
126#include <sys/stat.h>
127#include <sys/ethernet.h>
128#include <sys/sockio.h>
129#include <netinet/arp.h>
130#include <netinet/in.h>
131#include <netinet/in_systm.h>
132#include <netinet/ip.h>
133#include <netinet/ip_icmp.h> // must come after ip.h
134#include <netinet/udp.h>
135#include <netinet/tcp.h>
136#include <net/if.h>
137#include <syslog.h>
138#include <stropts.h>
139#endif
140#endif
141#endif
142
143#if defined(__OpenBSD__)
144#include <util.h>
145#endif
146
147#if defined(CONFIG_VDE)
148#include <libvdeplug.h>
149#endif
150
151#ifdef _WIN32
152#include <windows.h>
153#include <malloc.h>
154#include <sys/timeb.h>
155#include <mmsystem.h>
156#define getopt_long_only getopt_long
157#define memalign(align, size) malloc(size)
158#endif
159
160
161#ifdef CONFIG_COCOA
162#undef main
163#define main qemu_main
164#endif /* CONFIG_COCOA */
165
166#include "hw/hw.h"
167#include "hw/boards.h"
168#include "hw/usb.h"
169#include "hw/pcmcia.h"
170#include "hw/pc.h"
171#include "hw/audiodev.h"
172#include "hw/isa.h"
173#include "hw/baum.h"
174#include "hw/bt.h"
175#include "hw/watchdog.h"
176#include "hw/smbios.h"
177#include "hw/xen.h"
178#include "bt-host.h"
179#include "net.h"
180#include "monitor.h"
181#include "console.h"
182#include "sysemu.h"
183#include "gdbstub.h"
184#include "qemu-timer.h"
185#include "qemu-char.h"
186#include "cache-utils.h"
187#include "block.h"
188#include "dma.h"
189#include "audio/audio.h"
190#include "migration.h"
191#include "kvm.h"
192#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700193#include "android/hw-lcd.h"
194#include "android/boot-properties.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700195#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100196#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700197
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700198#ifdef CONFIG_STANDALONE_CORE
199/* Verbose value used by the standalone emulator core (without UI) */
200unsigned long android_verbose;
201#endif // CONFIG_STANDALONE_CORE
202
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800203#if !defined(CONFIG_STANDALONE_CORE)
204/* in android/qemulator.c */
205extern void android_emulator_set_base_port(int port);
206#endif
207
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700208#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700209#undef main
210#define main qemu_main
211#endif
212
213#include "disas.h"
214
215#include "exec-all.h"
216
217#ifdef CONFIG_TRACE
218#include "trace.h"
219#include "dcache.h"
220#endif
221
222#include "qemu_socket.h"
223
224#if defined(CONFIG_SLIRP)
225#include "libslirp.h"
226#endif
227
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700228
229
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700230#define DEFAULT_RAM_SIZE 128
231
232/* Max number of USB devices that can be specified on the commandline. */
233#define MAX_USB_CMDLINE 8
234
235/* Max number of bluetooth switches on the commandline. */
236#define MAX_BT_CMDLINE 10
237
238/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700239
240static const char *data_dir;
241const char *bios_name = NULL;
242static void *ioport_opaque[MAX_IOPORTS];
243static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
244static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100245#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700246/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
247 to store the VM snapshots */
248DriveInfo drives_table[MAX_DRIVES+1];
249int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100250#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700251enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700252DisplayType display_type = DT_DEFAULT;
253const char* keyboard_layout = NULL;
254int64_t ticks_per_sec;
255ram_addr_t ram_size;
256int nb_nics;
257NICInfo nd_table[MAX_NICS];
258int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100259int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260static int rtc_utc = 1;
261static int rtc_date_offset = -1; /* -1 means no change */
262int cirrus_vga_enabled = 1;
263int std_vga_enabled = 0;
264int vmsvga_enabled = 0;
265int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700266QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700267#ifdef TARGET_SPARC
268int graphic_width = 1024;
269int graphic_height = 768;
270int graphic_depth = 8;
271#else
272int graphic_width = 800;
273int graphic_height = 600;
274int graphic_depth = 15;
275#endif
276static int full_screen = 0;
277#ifdef CONFIG_SDL
278static int no_frame = 0;
279#endif
280int no_quit = 0;
281CharDriverState *serial_hds[MAX_SERIAL_PORTS];
282CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
283CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
284#ifdef TARGET_I386
285int win2k_install_hack = 0;
286int rtc_td_hack = 0;
287#endif
288int usb_enabled = 0;
289int singlestep = 0;
290int smp_cpus = 1;
291const char *vnc_display;
292int acpi_enabled = 1;
293int no_hpet = 0;
294int no_virtio_balloon = 0;
295int fd_bootchk = 1;
296int no_reboot = 0;
297int no_shutdown = 0;
298int cursor_hide = 1;
299int graphic_rotate = 0;
300#ifndef _WIN32
301int daemonize = 0;
302#endif
303WatchdogTimerModel *watchdog = NULL;
304int watchdog_action = WDT_RESET;
305const char *option_rom[MAX_OPTION_ROMS];
306int nb_option_roms;
307int semihosting_enabled = 0;
308#ifdef TARGET_ARM
309int old_param = 0;
310#endif
311const char *qemu_name;
312int alt_grab = 0;
313#if defined(TARGET_SPARC) || defined(TARGET_PPC)
314unsigned int nb_prom_envs = 0;
315const char *prom_envs[MAX_PROM_ENVS];
316#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100317#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700318int nb_drives_opt;
319struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100320#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700321int nb_numa_nodes;
322uint64_t node_mem[MAX_NODES];
323uint64_t node_cpumask[MAX_NODES];
324
325static CPUState *cur_cpu;
326static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700327static QEMUTimer *nographic_timer;
328
329uint8_t qemu_uuid[16];
330
331
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700332int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700333extern char* audio_input_source;
334
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700335extern char* android_op_ports;
336extern char* android_op_port;
337extern char* android_op_report_console;
338extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700339// Path to the file containing specific key character map.
340char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700341
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700342/* Path to hardware initialization file passed with -android-hw option. */
343char* android_op_hwini = NULL;
344
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700345/* Memory checker options. */
346char* android_op_memcheck = NULL;
347
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700348/* -dns-server option value. */
349char* android_op_dns_server = NULL;
350
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700351/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700352char* android_op_radio = NULL;
353
354/* -gps option value. */
355char* android_op_gps = NULL;
356
357/* -audio option value. */
358char* android_op_audio = NULL;
359
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700360/* -cpu-delay option value. */
361char* android_op_cpu_delay = NULL;
362
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700363#ifdef CONFIG_NAND_LIMITS
364/* -nand-limits option value. */
365char* android_op_nand_limits = NULL;
366#endif // CONFIG_NAND_LIMITS
367
368/* -netspeed option value. */
369char* android_op_netspeed = NULL;
370
371/* -netdelay option value. */
372char* android_op_netdelay = NULL;
373
374/* -netfast option value. */
375int android_op_netfast = 0;
376
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700377/* -tcpdump option value. */
378char* android_op_tcpdump = NULL;
379
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700380/* -lcd-density option value. */
381char* android_op_lcd_density = NULL;
382
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700383/* -ui-port option value. This port will be used to report the core
384 * initialization completion.
385 */
386char* android_op_ui_port = NULL;
387
388/* -ui-settings option value. This value will be passed to the UI when new UI
389 * process is attaching to the core.
390 */
391char* android_op_ui_settings = NULL;
392
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800393/* -android-avdname option value. */
394char* android_op_avd_name = "unknown";
395
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700396extern int android_display_width;
397extern int android_display_height;
398extern int android_display_bpp;
399
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700400extern void dprint( const char* format, ... );
401
Tim Baverstock24204cc2010-11-25 11:37:43 +0000402#if CONFIG_ANDROID_SNAPSHOTS
403const char* savevm_on_exit = NULL;
404#endif
405
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700406#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
407
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700408/* Reports the core initialization failure to the error stdout and to the UI
409 * socket before exiting the application.
410 * Parameters that are passed to this macro are used to format the error
411 * mesage using sprintf routine.
412 */
413#ifdef CONFIG_ANDROID
414#define PANIC(...) android_core_init_failure(__VA_ARGS__)
415#else
416#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
417 exit(1); \
418 } while (0)
419#endif // CONFIG_ANDROID
420
421/* Exits the core during initialization. */
422#ifdef CONFIG_ANDROID
423#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
424#else
425#define QEMU_EXIT(exit_code) exit(exit_code)
426#endif // CONFIG_ANDROID
427
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700428/***********************************************************/
429/* x86 ISA bus support */
430
431target_phys_addr_t isa_mem_base = 0;
432PicState2 *isa_pic;
433
434static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
435static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
436
437static uint32_t ioport_read(int index, uint32_t address)
438{
439 static IOPortReadFunc *default_func[3] = {
440 default_ioport_readb,
441 default_ioport_readw,
442 default_ioport_readl
443 };
444 IOPortReadFunc *func = ioport_read_table[index][address];
445 if (!func)
446 func = default_func[index];
447 return func(ioport_opaque[address], address);
448}
449
450static void ioport_write(int index, uint32_t address, uint32_t data)
451{
452 static IOPortWriteFunc *default_func[3] = {
453 default_ioport_writeb,
454 default_ioport_writew,
455 default_ioport_writel
456 };
457 IOPortWriteFunc *func = ioport_write_table[index][address];
458 if (!func)
459 func = default_func[index];
460 func(ioport_opaque[address], address, data);
461}
462
463static uint32_t default_ioport_readb(void *opaque, uint32_t address)
464{
465#ifdef DEBUG_UNUSED_IOPORT
466 fprintf(stderr, "unused inb: port=0x%04x\n", address);
467#endif
468 return 0xff;
469}
470
471static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
472{
473#ifdef DEBUG_UNUSED_IOPORT
474 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
475#endif
476}
477
478/* default is to make two byte accesses */
479static uint32_t default_ioport_readw(void *opaque, uint32_t address)
480{
481 uint32_t data;
482 data = ioport_read(0, address);
483 address = (address + 1) & (MAX_IOPORTS - 1);
484 data |= ioport_read(0, address) << 8;
485 return data;
486}
487
488static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
489{
490 ioport_write(0, address, data & 0xff);
491 address = (address + 1) & (MAX_IOPORTS - 1);
492 ioport_write(0, address, (data >> 8) & 0xff);
493}
494
495static uint32_t default_ioport_readl(void *opaque, uint32_t address)
496{
497#ifdef DEBUG_UNUSED_IOPORT
498 fprintf(stderr, "unused inl: port=0x%04x\n", address);
499#endif
500 return 0xffffffff;
501}
502
503static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
504{
505#ifdef DEBUG_UNUSED_IOPORT
506 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
507#endif
508}
509
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700510/***********************************************************/
511void hw_error(const char *fmt, ...)
512{
513 va_list ap;
514 CPUState *env;
515
516 va_start(ap, fmt);
517 fprintf(stderr, "qemu: hardware error: ");
518 vfprintf(stderr, fmt, ap);
519 fprintf(stderr, "\n");
520 for(env = first_cpu; env != NULL; env = env->next_cpu) {
521 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
522#ifdef TARGET_I386
523 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
524#else
525 cpu_dump_state(env, stderr, fprintf, 0);
526#endif
527 }
528 va_end(ap);
529 abort();
530}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200531
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700532/***************/
533/* ballooning */
534
535static QEMUBalloonEvent *qemu_balloon_event;
536void *qemu_balloon_event_opaque;
537
538void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
539{
540 qemu_balloon_event = func;
541 qemu_balloon_event_opaque = opaque;
542}
543
544void qemu_balloon(ram_addr_t target)
545{
546 if (qemu_balloon_event)
547 qemu_balloon_event(qemu_balloon_event_opaque, target);
548}
549
550ram_addr_t qemu_balloon_status(void)
551{
552 if (qemu_balloon_event)
553 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
554 return 0;
555}
556
557/***********************************************************/
David Turner025c32f2010-09-10 14:52:42 +0200558/* real time host monotonic timer */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700559
560/* compute with 96 bit intermediate result: (a*b)/c */
561uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
562{
563 union {
564 uint64_t ll;
565 struct {
David 'Digit' Turner20894ae2010-05-10 17:07:36 -0700566#ifdef HOST_WORDS_BIGENDIAN
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700567 uint32_t high, low;
568#else
569 uint32_t low, high;
570#endif
571 } l;
572 } u, res;
573 uint64_t rl, rh;
574
575 u.ll = a;
576 rl = (uint64_t)u.l.low * (uint64_t)b;
577 rh = (uint64_t)u.l.high * (uint64_t)b;
578 rh += (rl >> 32);
579 res.l.high = rh / c;
580 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
581 return res.ll;
582}
583
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700584/***********************************************************/
585/* host time/date access */
586void qemu_get_timedate(struct tm *tm, int offset)
587{
588 time_t ti;
589 struct tm *ret;
590
591 time(&ti);
592 ti += offset;
593 if (rtc_date_offset == -1) {
594 if (rtc_utc)
595 ret = gmtime(&ti);
596 else
597 ret = localtime(&ti);
598 } else {
599 ti -= rtc_date_offset;
600 ret = gmtime(&ti);
601 }
602
603 memcpy(tm, ret, sizeof(struct tm));
604}
605
606int qemu_timedate_diff(struct tm *tm)
607{
608 time_t seconds;
609
610 if (rtc_date_offset == -1)
611 if (rtc_utc)
612 seconds = mktimegm(tm);
613 else
614 seconds = mktime(tm);
615 else
616 seconds = mktimegm(tm) + rtc_date_offset;
617
618 return seconds - time(NULL);
619}
620
621
622#ifdef CONFIG_TRACE
623static int tbflush_requested;
624static int exit_requested;
625
626void start_tracing()
627{
628 if (trace_filename == NULL)
629 return;
630 if (!tracing) {
631 fprintf(stderr,"-- start tracing --\n");
632 start_time = Now();
633 }
634 tracing = 1;
635 tbflush_requested = 1;
636 qemu_notify_event();
637}
638
639void stop_tracing()
640{
641 if (trace_filename == NULL)
642 return;
643 if (tracing) {
644 end_time = Now();
645 elapsed_usecs += end_time - start_time;
646 fprintf(stderr,"-- stop tracing --\n");
647 }
648 tracing = 0;
649 tbflush_requested = 1;
650 qemu_notify_event();
651}
652
653#ifndef _WIN32
654/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
655 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
656 */
657void sigusr_handler(int sig)
658{
659 if (sig == SIGUSR1)
660 start_tracing();
661 else
662 stop_tracing();
663}
664#endif
665
666/* This is the handler to catch control-C so that we can exit cleanly.
667 * This is needed when tracing to flush the buffers to disk.
668 */
669void sigint_handler(int sig)
670{
671 exit_requested = 1;
672 qemu_notify_event();
673}
674#endif /* CONFIG_TRACE */
675
676
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700677/***********************************************************/
678/* Bluetooth support */
679static int nb_hcis;
680static int cur_hci;
681static struct HCIInfo *hci_table[MAX_NICS];
682
683static struct bt_vlan_s {
684 struct bt_scatternet_s net;
685 int id;
686 struct bt_vlan_s *next;
687} *first_bt_vlan;
688
689/* find or alloc a new bluetooth "VLAN" */
690static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
691{
692 struct bt_vlan_s **pvlan, *vlan;
693 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
694 if (vlan->id == id)
695 return &vlan->net;
696 }
697 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
698 vlan->id = id;
699 pvlan = &first_bt_vlan;
700 while (*pvlan != NULL)
701 pvlan = &(*pvlan)->next;
702 *pvlan = vlan;
703 return &vlan->net;
704}
705
706static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
707{
708}
709
710static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
711{
712 return -ENOTSUP;
713}
714
715static struct HCIInfo null_hci = {
716 .cmd_send = null_hci_send,
717 .sco_send = null_hci_send,
718 .acl_send = null_hci_send,
719 .bdaddr_set = null_hci_addr_set,
720};
721
722struct HCIInfo *qemu_next_hci(void)
723{
724 if (cur_hci == nb_hcis)
725 return &null_hci;
726
727 return hci_table[cur_hci++];
728}
729
730static struct HCIInfo *hci_init(const char *str)
731{
732 char *endp;
733 struct bt_scatternet_s *vlan = 0;
734
735 if (!strcmp(str, "null"))
736 /* null */
737 return &null_hci;
738 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
739 /* host[:hciN] */
740 return bt_host_hci(str[4] ? str + 5 : "hci0");
741 else if (!strncmp(str, "hci", 3)) {
742 /* hci[,vlan=n] */
743 if (str[3]) {
744 if (!strncmp(str + 3, ",vlan=", 6)) {
745 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
746 if (*endp)
747 vlan = 0;
748 }
749 } else
750 vlan = qemu_find_bt_vlan(0);
751 if (vlan)
752 return bt_new_hci(vlan);
753 }
754
755 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
756
757 return 0;
758}
759
760static int bt_hci_parse(const char *str)
761{
762 struct HCIInfo *hci;
763 bdaddr_t bdaddr;
764
765 if (nb_hcis >= MAX_NICS) {
766 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
767 return -1;
768 }
769
770 hci = hci_init(str);
771 if (!hci)
772 return -1;
773
774 bdaddr.b[0] = 0x52;
775 bdaddr.b[1] = 0x54;
776 bdaddr.b[2] = 0x00;
777 bdaddr.b[3] = 0x12;
778 bdaddr.b[4] = 0x34;
779 bdaddr.b[5] = 0x56 + nb_hcis;
780 hci->bdaddr_set(hci, bdaddr.b);
781
782 hci_table[nb_hcis++] = hci;
783
784 return 0;
785}
786
787static void bt_vhci_add(int vlan_id)
788{
789 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
790
791 if (!vlan->slave)
792 fprintf(stderr, "qemu: warning: adding a VHCI to "
793 "an empty scatternet %i\n", vlan_id);
794
795 bt_vhci_init(bt_new_hci(vlan));
796}
797
798static struct bt_device_s *bt_device_add(const char *opt)
799{
800 struct bt_scatternet_s *vlan;
801 int vlan_id = 0;
802 char *endp = strstr(opt, ",vlan=");
803 int len = (endp ? endp - opt : strlen(opt)) + 1;
804 char devname[10];
805
806 pstrcpy(devname, MIN(sizeof(devname), len), opt);
807
808 if (endp) {
809 vlan_id = strtol(endp + 6, &endp, 0);
810 if (*endp) {
811 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
812 return 0;
813 }
814 }
815
816 vlan = qemu_find_bt_vlan(vlan_id);
817
818 if (!vlan->slave)
819 fprintf(stderr, "qemu: warning: adding a slave device to "
820 "an empty scatternet %i\n", vlan_id);
821
822 if (!strcmp(devname, "keyboard"))
823 return bt_keyboard_init(vlan);
824
825 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
826 return 0;
827}
828
829static int bt_parse(const char *opt)
830{
831 const char *endp, *p;
832 int vlan;
833
834 if (strstart(opt, "hci", &endp)) {
835 if (!*endp || *endp == ',') {
836 if (*endp)
837 if (!strstart(endp, ",vlan=", 0))
838 opt = endp + 1;
839
840 return bt_hci_parse(opt);
841 }
842 } else if (strstart(opt, "vhci", &endp)) {
843 if (!*endp || *endp == ',') {
844 if (*endp) {
845 if (strstart(endp, ",vlan=", &p)) {
846 vlan = strtol(p, (char **) &endp, 0);
847 if (*endp) {
848 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
849 return 1;
850 }
851 } else {
852 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
853 return 1;
854 }
855 } else
856 vlan = 0;
857
858 bt_vhci_add(vlan);
859 return 0;
860 }
861 } else if (strstart(opt, "device:", &endp))
862 return !bt_device_add(endp);
863
864 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
865 return 1;
866}
867
868/***********************************************************/
869/* QEMU Block devices */
870
871#define HD_ALIAS "index=%d,media=disk"
872#define CDROM_ALIAS "index=2,media=cdrom"
873#define FD_ALIAS "index=%d,if=floppy"
874#define PFLASH_ALIAS "if=pflash"
875#define MTD_ALIAS "if=mtd"
876#define SD_ALIAS "index=0,if=sd"
877
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100878static int drive_init_func(QemuOpts *opts, void *opaque)
879{
880 int *use_scsi = opaque;
881 int fatal_error = 0;
882
883 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
884 if (fatal_error)
885 return 1;
886 }
887 return 0;
888}
889
890static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
891{
892 if (NULL == qemu_opt_get(opts, "snapshot")) {
893 qemu_opt_set(opts, "snapshot", "on");
894 }
895 return 0;
896}
897
898#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700899static int drive_opt_get_free_idx(void)
900{
901 int index;
902
903 for (index = 0; index < MAX_DRIVES; index++)
904 if (!drives_opt[index].used) {
905 drives_opt[index].used = 1;
906 return index;
907 }
908
909 return -1;
910}
911
912static int drive_get_free_idx(void)
913{
914 int index;
915
916 for (index = 0; index < MAX_DRIVES; index++)
917 if (!drives_table[index].used) {
918 drives_table[index].used = 1;
919 return index;
920 }
921
922 return -1;
923}
924
925int drive_add(const char *file, const char *fmt, ...)
926{
927 va_list ap;
928 int index = drive_opt_get_free_idx();
929
930 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
931 fprintf(stderr, "qemu: too many drives\n");
932 return -1;
933 }
934
935 drives_opt[index].file = file;
936 va_start(ap, fmt);
937 vsnprintf(drives_opt[index].opt,
938 sizeof(drives_opt[0].opt), fmt, ap);
939 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700940
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700941 nb_drives_opt++;
942 return index;
943}
944
945void drive_remove(int index)
946{
947 drives_opt[index].used = 0;
948 nb_drives_opt--;
949}
950
951int drive_get_index(BlockInterfaceType type, int bus, int unit)
952{
953 int index;
954
955 /* seek interface, bus and unit */
956
957 for (index = 0; index < MAX_DRIVES; index++)
958 if (drives_table[index].type == type &&
959 drives_table[index].bus == bus &&
960 drives_table[index].unit == unit &&
961 drives_table[index].used)
962 return index;
963
964 return -1;
965}
966
967int drive_get_max_bus(BlockInterfaceType type)
968{
969 int max_bus;
970 int index;
971
972 max_bus = -1;
973 for (index = 0; index < nb_drives; index++) {
974 if(drives_table[index].type == type &&
975 drives_table[index].bus > max_bus)
976 max_bus = drives_table[index].bus;
977 }
978 return max_bus;
979}
980
981const char *drive_get_serial(BlockDriverState *bdrv)
982{
983 int index;
984
985 for (index = 0; index < nb_drives; index++)
986 if (drives_table[index].bdrv == bdrv)
987 return drives_table[index].serial;
988
989 return "\0";
990}
991
992BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
993{
994 int index;
995
996 for (index = 0; index < nb_drives; index++)
997 if (drives_table[index].bdrv == bdrv)
998 return drives_table[index].onerror;
999
1000 return BLOCK_ERR_STOP_ENOSPC;
1001}
1002
1003static void bdrv_format_print(void *opaque, const char *name)
1004{
1005 fprintf(stderr, " %s", name);
1006}
1007
1008void drive_uninit(BlockDriverState *bdrv)
1009{
1010 int i;
1011
1012 for (i = 0; i < MAX_DRIVES; i++)
1013 if (drives_table[i].bdrv == bdrv) {
1014 drives_table[i].bdrv = NULL;
1015 drives_table[i].used = 0;
1016 drive_remove(drives_table[i].drive_opt_idx);
1017 nb_drives--;
1018 break;
1019 }
1020}
1021
1022int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1023{
1024 char buf[128];
1025 char file[1024];
1026 char devname[128];
1027 char serial[21];
1028 const char *mediastr = "";
1029 BlockInterfaceType type;
1030 enum { MEDIA_DISK, MEDIA_CDROM } media;
1031 int bus_id, unit_id;
1032 int cyls, heads, secs, translation;
1033 BlockDriverState *bdrv;
1034 BlockDriver *drv = NULL;
1035 QEMUMachine *machine = opaque;
1036 int max_devs;
1037 int index;
1038 int cache;
1039 int bdrv_flags, onerror;
1040 int drives_table_idx;
1041 char *str = arg->opt;
1042 static const char * const params[] = { "bus", "unit", "if", "index",
1043 "cyls", "heads", "secs", "trans",
1044 "media", "snapshot", "file",
1045 "cache", "format", "serial", "werror",
1046 NULL };
1047
1048 if (check_params(buf, sizeof(buf), params, str) < 0) {
1049 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1050 buf, str);
1051 return -1;
1052 }
1053
1054 file[0] = 0;
1055 cyls = heads = secs = 0;
1056 bus_id = 0;
1057 unit_id = -1;
1058 translation = BIOS_ATA_TRANSLATION_AUTO;
1059 index = -1;
1060 cache = 3;
1061
1062 if (machine->use_scsi) {
1063 type = IF_SCSI;
1064 max_devs = MAX_SCSI_DEVS;
1065 pstrcpy(devname, sizeof(devname), "scsi");
1066 } else {
1067 type = IF_IDE;
1068 max_devs = MAX_IDE_DEVS;
1069 pstrcpy(devname, sizeof(devname), "ide");
1070 }
1071 media = MEDIA_DISK;
1072
1073 /* extract parameters */
1074
1075 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1076 bus_id = strtol(buf, NULL, 0);
1077 if (bus_id < 0) {
1078 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1079 return -1;
1080 }
1081 }
1082
1083 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1084 unit_id = strtol(buf, NULL, 0);
1085 if (unit_id < 0) {
1086 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1087 return -1;
1088 }
1089 }
1090
1091 if (get_param_value(buf, sizeof(buf), "if", str)) {
1092 pstrcpy(devname, sizeof(devname), buf);
1093 if (!strcmp(buf, "ide")) {
1094 type = IF_IDE;
1095 max_devs = MAX_IDE_DEVS;
1096 } else if (!strcmp(buf, "scsi")) {
1097 type = IF_SCSI;
1098 max_devs = MAX_SCSI_DEVS;
1099 } else if (!strcmp(buf, "floppy")) {
1100 type = IF_FLOPPY;
1101 max_devs = 0;
1102 } else if (!strcmp(buf, "pflash")) {
1103 type = IF_PFLASH;
1104 max_devs = 0;
1105 } else if (!strcmp(buf, "mtd")) {
1106 type = IF_MTD;
1107 max_devs = 0;
1108 } else if (!strcmp(buf, "sd")) {
1109 type = IF_SD;
1110 max_devs = 0;
1111 } else if (!strcmp(buf, "virtio")) {
1112 type = IF_VIRTIO;
1113 max_devs = 0;
1114 } else if (!strcmp(buf, "xen")) {
1115 type = IF_XEN;
1116 max_devs = 0;
1117 } else {
1118 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1119 return -1;
1120 }
1121 }
1122
1123 if (get_param_value(buf, sizeof(buf), "index", str)) {
1124 index = strtol(buf, NULL, 0);
1125 if (index < 0) {
1126 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1127 return -1;
1128 }
1129 }
1130
1131 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1132 cyls = strtol(buf, NULL, 0);
1133 }
1134
1135 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1136 heads = strtol(buf, NULL, 0);
1137 }
1138
1139 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1140 secs = strtol(buf, NULL, 0);
1141 }
1142
1143 if (cyls || heads || secs) {
1144 if (cyls < 1 || cyls > 16383) {
1145 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1146 return -1;
1147 }
1148 if (heads < 1 || heads > 16) {
1149 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1150 return -1;
1151 }
1152 if (secs < 1 || secs > 63) {
1153 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1154 return -1;
1155 }
1156 }
1157
1158 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1159 if (!cyls) {
1160 fprintf(stderr,
1161 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1162 str);
1163 return -1;
1164 }
1165 if (!strcmp(buf, "none"))
1166 translation = BIOS_ATA_TRANSLATION_NONE;
1167 else if (!strcmp(buf, "lba"))
1168 translation = BIOS_ATA_TRANSLATION_LBA;
1169 else if (!strcmp(buf, "auto"))
1170 translation = BIOS_ATA_TRANSLATION_AUTO;
1171 else {
1172 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1173 return -1;
1174 }
1175 }
1176
1177 if (get_param_value(buf, sizeof(buf), "media", str)) {
1178 if (!strcmp(buf, "disk")) {
1179 media = MEDIA_DISK;
1180 } else if (!strcmp(buf, "cdrom")) {
1181 if (cyls || secs || heads) {
1182 fprintf(stderr,
1183 "qemu: '%s' invalid physical CHS format\n", str);
1184 return -1;
1185 }
1186 media = MEDIA_CDROM;
1187 } else {
1188 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1189 return -1;
1190 }
1191 }
1192
1193 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1194 if (!strcmp(buf, "on"))
1195 snapshot = 1;
1196 else if (!strcmp(buf, "off"))
1197 snapshot = 0;
1198 else {
1199 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1200 return -1;
1201 }
1202 }
1203
1204 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1205 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1206 cache = 0;
1207 else if (!strcmp(buf, "writethrough"))
1208 cache = 1;
1209 else if (!strcmp(buf, "writeback"))
1210 cache = 2;
1211 else {
1212 fprintf(stderr, "qemu: invalid cache option\n");
1213 return -1;
1214 }
1215 }
1216
1217 if (get_param_value(buf, sizeof(buf), "format", str)) {
1218 if (strcmp(buf, "?") == 0) {
1219 fprintf(stderr, "qemu: Supported formats:");
1220 bdrv_iterate_format(bdrv_format_print, NULL);
1221 fprintf(stderr, "\n");
1222 return -1;
1223 }
1224 drv = bdrv_find_format(buf);
1225 if (!drv) {
1226 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1227 return -1;
1228 }
1229 }
1230
1231 if (arg->file == NULL)
1232 get_param_value(file, sizeof(file), "file", str);
1233 else
1234 pstrcpy(file, sizeof(file), arg->file);
1235
1236 if (!get_param_value(serial, sizeof(serial), "serial", str))
1237 memset(serial, 0, sizeof(serial));
1238
1239 onerror = BLOCK_ERR_STOP_ENOSPC;
1240 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1241 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1242 fprintf(stderr, "werror is no supported by this format\n");
1243 return -1;
1244 }
1245 if (!strcmp(buf, "ignore"))
1246 onerror = BLOCK_ERR_IGNORE;
1247 else if (!strcmp(buf, "enospc"))
1248 onerror = BLOCK_ERR_STOP_ENOSPC;
1249 else if (!strcmp(buf, "stop"))
1250 onerror = BLOCK_ERR_STOP_ANY;
1251 else if (!strcmp(buf, "report"))
1252 onerror = BLOCK_ERR_REPORT;
1253 else {
1254 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1255 return -1;
1256 }
1257 }
1258
1259 /* compute bus and unit according index */
1260
1261 if (index != -1) {
1262 if (bus_id != 0 || unit_id != -1) {
1263 fprintf(stderr,
1264 "qemu: '%s' index cannot be used with bus and unit\n", str);
1265 return -1;
1266 }
1267 if (max_devs == 0)
1268 {
1269 unit_id = index;
1270 bus_id = 0;
1271 } else {
1272 unit_id = index % max_devs;
1273 bus_id = index / max_devs;
1274 }
1275 }
1276
1277 /* if user doesn't specify a unit_id,
1278 * try to find the first free
1279 */
1280
1281 if (unit_id == -1) {
1282 unit_id = 0;
1283 while (drive_get_index(type, bus_id, unit_id) != -1) {
1284 unit_id++;
1285 if (max_devs && unit_id >= max_devs) {
1286 unit_id -= max_devs;
1287 bus_id++;
1288 }
1289 }
1290 }
1291
1292 /* check unit id */
1293
1294 if (max_devs && unit_id >= max_devs) {
1295 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1296 str, unit_id, max_devs - 1);
1297 return -1;
1298 }
1299
1300 /*
1301 * ignore multiple definitions
1302 */
1303
1304 if (drive_get_index(type, bus_id, unit_id) != -1)
1305 return -2;
1306
1307 /* init */
1308
1309 if (type == IF_IDE || type == IF_SCSI)
1310 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1311 if (max_devs)
1312 snprintf(buf, sizeof(buf), "%s%i%s%i",
1313 devname, bus_id, mediastr, unit_id);
1314 else
1315 snprintf(buf, sizeof(buf), "%s%s%i",
1316 devname, mediastr, unit_id);
1317 bdrv = bdrv_new(buf);
1318 drives_table_idx = drive_get_free_idx();
1319 drives_table[drives_table_idx].bdrv = bdrv;
1320 drives_table[drives_table_idx].type = type;
1321 drives_table[drives_table_idx].bus = bus_id;
1322 drives_table[drives_table_idx].unit = unit_id;
1323 drives_table[drives_table_idx].onerror = onerror;
1324 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1325 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1326 nb_drives++;
1327
1328 switch(type) {
1329 case IF_IDE:
1330 case IF_SCSI:
1331 case IF_XEN:
1332 switch(media) {
1333 case MEDIA_DISK:
1334 if (cyls != 0) {
1335 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1336 bdrv_set_translation_hint(bdrv, translation);
1337 }
1338 break;
1339 case MEDIA_CDROM:
1340 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1341 break;
1342 }
1343 break;
1344 case IF_SD:
1345 /* FIXME: This isn't really a floppy, but it's a reasonable
1346 approximation. */
1347 case IF_FLOPPY:
1348 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1349 break;
1350 case IF_PFLASH:
1351 case IF_MTD:
1352 case IF_VIRTIO:
1353 break;
1354 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001355 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001356 abort();
1357 }
1358 if (!file[0])
1359 return -2;
1360 bdrv_flags = 0;
1361 if (snapshot) {
1362 bdrv_flags |= BDRV_O_SNAPSHOT;
1363 cache = 2; /* always use write-back with snapshot */
1364 }
1365 if (cache == 0) /* no caching */
1366 bdrv_flags |= BDRV_O_NOCACHE;
1367 else if (cache == 2) /* write-back */
1368 bdrv_flags |= BDRV_O_CACHE_WB;
1369 else if (cache == 3) /* not specified */
1370 bdrv_flags |= BDRV_O_CACHE_DEF;
1371 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1372 fprintf(stderr, "qemu: could not open disk image %s\n",
1373 file);
1374 return -1;
1375 }
1376 if (bdrv_key_required(bdrv))
1377 autostart = 0;
1378 return drives_table_idx;
1379}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001380#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001381
1382static void numa_add(const char *optarg)
1383{
1384 char option[128];
1385 char *endptr;
1386 unsigned long long value, endvalue;
1387 int nodenr;
1388
1389 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1390 if (!strcmp(option, "node")) {
1391 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1392 nodenr = nb_numa_nodes;
1393 } else {
1394 nodenr = strtoull(option, NULL, 10);
1395 }
1396
1397 if (get_param_value(option, 128, "mem", optarg) == 0) {
1398 node_mem[nodenr] = 0;
1399 } else {
1400 value = strtoull(option, &endptr, 0);
1401 switch (*endptr) {
1402 case 0: case 'M': case 'm':
1403 value <<= 20;
1404 break;
1405 case 'G': case 'g':
1406 value <<= 30;
1407 break;
1408 }
1409 node_mem[nodenr] = value;
1410 }
1411 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1412 node_cpumask[nodenr] = 0;
1413 } else {
1414 value = strtoull(option, &endptr, 10);
1415 if (value >= 64) {
1416 value = 63;
1417 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1418 } else {
1419 if (*endptr == '-') {
1420 endvalue = strtoull(endptr+1, &endptr, 10);
1421 if (endvalue >= 63) {
1422 endvalue = 62;
1423 fprintf(stderr,
1424 "only 63 CPUs in NUMA mode supported.\n");
1425 }
1426 value = (1 << (endvalue + 1)) - (1 << value);
1427 } else {
1428 value = 1 << value;
1429 }
1430 }
1431 node_cpumask[nodenr] = value;
1432 }
1433 nb_numa_nodes++;
1434 }
1435 return;
1436}
1437
1438/***********************************************************/
1439/* USB devices */
1440
1441static USBPort *used_usb_ports;
1442static USBPort *free_usb_ports;
1443
1444/* ??? Maybe change this to register a hub to keep track of the topology. */
1445void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1446 usb_attachfn attach)
1447{
1448 port->opaque = opaque;
1449 port->index = index;
1450 port->attach = attach;
1451 port->next = free_usb_ports;
1452 free_usb_ports = port;
1453}
1454
1455int usb_device_add_dev(USBDevice *dev)
1456{
1457 USBPort *port;
1458
1459 /* Find a USB port to add the device to. */
1460 port = free_usb_ports;
1461 if (!port->next) {
1462 USBDevice *hub;
1463
1464 /* Create a new hub and chain it on. */
1465 free_usb_ports = NULL;
1466 port->next = used_usb_ports;
1467 used_usb_ports = port;
1468
1469 hub = usb_hub_init(VM_USB_HUB_SIZE);
1470 usb_attach(port, hub);
1471 port = free_usb_ports;
1472 }
1473
1474 free_usb_ports = port->next;
1475 port->next = used_usb_ports;
1476 used_usb_ports = port;
1477 usb_attach(port, dev);
1478 return 0;
1479}
1480
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001481#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001482static void usb_msd_password_cb(void *opaque, int err)
1483{
1484 USBDevice *dev = opaque;
1485
1486 if (!err)
1487 usb_device_add_dev(dev);
1488 else
1489 dev->handle_destroy(dev);
1490}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001491#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001492
1493static int usb_device_add(const char *devname, int is_hotplug)
1494{
1495 const char *p;
1496 USBDevice *dev;
1497
1498 if (!free_usb_ports)
1499 return -1;
1500
1501 if (strstart(devname, "host:", &p)) {
1502 dev = usb_host_device_open(p);
1503 } else if (!strcmp(devname, "mouse")) {
1504 dev = usb_mouse_init();
1505 } else if (!strcmp(devname, "tablet")) {
1506 dev = usb_tablet_init();
1507 } else if (!strcmp(devname, "keyboard")) {
1508 dev = usb_keyboard_init();
1509 } else if (strstart(devname, "disk:", &p)) {
1510#if 0
1511 BlockDriverState *bs;
1512#endif
1513 dev = usb_msd_init(p);
1514 if (!dev)
1515 return -1;
1516#if 0
1517 bs = usb_msd_get_bdrv(dev);
1518 if (bdrv_key_required(bs)) {
1519 autostart = 0;
1520 if (is_hotplug) {
1521 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1522 dev);
1523 return 0;
1524 }
1525 }
1526 } else if (!strcmp(devname, "wacom-tablet")) {
1527 dev = usb_wacom_init();
1528 } else if (strstart(devname, "serial:", &p)) {
1529 dev = usb_serial_init(p);
1530#ifdef CONFIG_BRLAPI
1531 } else if (!strcmp(devname, "braille")) {
1532 dev = usb_baum_init();
1533#endif
1534 } else if (strstart(devname, "net:", &p)) {
1535 int nic = nb_nics;
1536
1537 if (net_client_init("nic", p) < 0)
1538 return -1;
1539 nd_table[nic].model = "usb";
1540 dev = usb_net_init(&nd_table[nic]);
1541 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1542 dev = usb_bt_init(devname[2] ? hci_init(p) :
1543 bt_new_hci(qemu_find_bt_vlan(0)));
1544#endif
1545 } else {
1546 return -1;
1547 }
1548 if (!dev)
1549 return -1;
1550
1551 return usb_device_add_dev(dev);
1552}
1553
1554int usb_device_del_addr(int bus_num, int addr)
1555{
1556 USBPort *port;
1557 USBPort **lastp;
1558 USBDevice *dev;
1559
1560 if (!used_usb_ports)
1561 return -1;
1562
1563 if (bus_num != 0)
1564 return -1;
1565
1566 lastp = &used_usb_ports;
1567 port = used_usb_ports;
1568 while (port && port->dev->addr != addr) {
1569 lastp = &port->next;
1570 port = port->next;
1571 }
1572
1573 if (!port)
1574 return -1;
1575
1576 dev = port->dev;
1577 *lastp = port->next;
1578 usb_attach(port, NULL);
1579 dev->handle_destroy(dev);
1580 port->next = free_usb_ports;
1581 free_usb_ports = port;
1582 return 0;
1583}
1584
1585static int usb_device_del(const char *devname)
1586{
1587 int bus_num, addr;
1588 const char *p;
1589
1590 if (strstart(devname, "host:", &p))
1591 return usb_host_device_close(p);
1592
1593 if (!used_usb_ports)
1594 return -1;
1595
1596 p = strchr(devname, '.');
1597 if (!p)
1598 return -1;
1599 bus_num = strtoul(devname, NULL, 0);
1600 addr = strtoul(p + 1, NULL, 0);
1601
1602 return usb_device_del_addr(bus_num, addr);
1603}
1604
1605void do_usb_add(Monitor *mon, const char *devname)
1606{
1607 usb_device_add(devname, 1);
1608}
1609
1610void do_usb_del(Monitor *mon, const char *devname)
1611{
1612 usb_device_del(devname);
1613}
1614
1615void usb_info(Monitor *mon)
1616{
1617 USBDevice *dev;
1618 USBPort *port;
1619 const char *speed_str;
1620
1621 if (!usb_enabled) {
1622 monitor_printf(mon, "USB support not enabled\n");
1623 return;
1624 }
1625
1626 for (port = used_usb_ports; port; port = port->next) {
1627 dev = port->dev;
1628 if (!dev)
1629 continue;
1630 switch(dev->speed) {
1631 case USB_SPEED_LOW:
1632 speed_str = "1.5";
1633 break;
1634 case USB_SPEED_FULL:
1635 speed_str = "12";
1636 break;
1637 case USB_SPEED_HIGH:
1638 speed_str = "480";
1639 break;
1640 default:
1641 speed_str = "?";
1642 break;
1643 }
1644 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1645 0, dev->addr, speed_str, dev->devname);
1646 }
1647}
1648
1649/***********************************************************/
1650/* PCMCIA/Cardbus */
1651
1652static struct pcmcia_socket_entry_s {
1653 PCMCIASocket *socket;
1654 struct pcmcia_socket_entry_s *next;
1655} *pcmcia_sockets = 0;
1656
1657void pcmcia_socket_register(PCMCIASocket *socket)
1658{
1659 struct pcmcia_socket_entry_s *entry;
1660
1661 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1662 entry->socket = socket;
1663 entry->next = pcmcia_sockets;
1664 pcmcia_sockets = entry;
1665}
1666
1667void pcmcia_socket_unregister(PCMCIASocket *socket)
1668{
1669 struct pcmcia_socket_entry_s *entry, **ptr;
1670
1671 ptr = &pcmcia_sockets;
1672 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1673 if (entry->socket == socket) {
1674 *ptr = entry->next;
1675 qemu_free(entry);
1676 }
1677}
1678
1679void pcmcia_info(Monitor *mon)
1680{
1681 struct pcmcia_socket_entry_s *iter;
1682
1683 if (!pcmcia_sockets)
1684 monitor_printf(mon, "No PCMCIA sockets\n");
1685
1686 for (iter = pcmcia_sockets; iter; iter = iter->next)
1687 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1688 iter->socket->attached ? iter->socket->card_string :
1689 "Empty");
1690}
1691
1692/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001693/* I/O handling */
1694
1695typedef struct IOHandlerRecord {
1696 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001697 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001698 IOHandler *fd_read;
1699 IOHandler *fd_write;
1700 int deleted;
1701 void *opaque;
1702 /* temporary data */
1703 struct pollfd *ufd;
1704 struct IOHandlerRecord *next;
1705} IOHandlerRecord;
1706
1707static IOHandlerRecord *first_io_handler;
1708
1709/* XXX: fd_read_poll should be suppressed, but an API change is
1710 necessary in the character devices to suppress fd_can_read(). */
1711int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001712 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001713 IOHandler *fd_read,
1714 IOHandler *fd_write,
1715 void *opaque)
1716{
1717 IOHandlerRecord **pioh, *ioh;
1718
1719 if (!fd_read && !fd_write) {
1720 pioh = &first_io_handler;
1721 for(;;) {
1722 ioh = *pioh;
1723 if (ioh == NULL)
1724 break;
1725 if (ioh->fd == fd) {
1726 ioh->deleted = 1;
1727 break;
1728 }
1729 pioh = &ioh->next;
1730 }
1731 } else {
1732 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1733 if (ioh->fd == fd)
1734 goto found;
1735 }
1736 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1737 ioh->next = first_io_handler;
1738 first_io_handler = ioh;
1739 found:
1740 ioh->fd = fd;
1741 ioh->fd_read_poll = fd_read_poll;
1742 ioh->fd_read = fd_read;
1743 ioh->fd_write = fd_write;
1744 ioh->opaque = opaque;
1745 ioh->deleted = 0;
1746 }
1747 return 0;
1748}
1749
1750int qemu_set_fd_handler(int fd,
1751 IOHandler *fd_read,
1752 IOHandler *fd_write,
1753 void *opaque)
1754{
1755 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1756}
1757
1758#ifdef _WIN32
1759/***********************************************************/
1760/* Polling handling */
1761
1762typedef struct PollingEntry {
1763 PollingFunc *func;
1764 void *opaque;
1765 struct PollingEntry *next;
1766} PollingEntry;
1767
1768static PollingEntry *first_polling_entry;
1769
1770int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1771{
1772 PollingEntry **ppe, *pe;
1773 pe = qemu_mallocz(sizeof(PollingEntry));
1774 pe->func = func;
1775 pe->opaque = opaque;
1776 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1777 *ppe = pe;
1778 return 0;
1779}
1780
1781void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1782{
1783 PollingEntry **ppe, *pe;
1784 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1785 pe = *ppe;
1786 if (pe->func == func && pe->opaque == opaque) {
1787 *ppe = pe->next;
1788 qemu_free(pe);
1789 break;
1790 }
1791 }
1792}
1793
1794/***********************************************************/
1795/* Wait objects support */
1796typedef struct WaitObjects {
1797 int num;
1798 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1799 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1800 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1801} WaitObjects;
1802
1803static WaitObjects wait_objects = {0};
1804
1805int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1806{
1807 WaitObjects *w = &wait_objects;
1808
1809 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1810 return -1;
1811 w->events[w->num] = handle;
1812 w->func[w->num] = func;
1813 w->opaque[w->num] = opaque;
1814 w->num++;
1815 return 0;
1816}
1817
1818void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1819{
1820 int i, found;
1821 WaitObjects *w = &wait_objects;
1822
1823 found = 0;
1824 for (i = 0; i < w->num; i++) {
1825 if (w->events[i] == handle)
1826 found = 1;
1827 if (found) {
1828 w->events[i] = w->events[i + 1];
1829 w->func[i] = w->func[i + 1];
1830 w->opaque[i] = w->opaque[i + 1];
1831 }
1832 }
1833 if (found)
1834 w->num--;
1835}
1836#endif
1837
1838/***********************************************************/
1839/* ram save/restore */
1840
1841static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1842{
1843 int v;
1844
1845 v = qemu_get_byte(f);
1846 switch(v) {
1847 case 0:
1848 if (qemu_get_buffer(f, buf, len) != len)
1849 return -EIO;
1850 break;
1851 case 1:
1852 v = qemu_get_byte(f);
1853 memset(buf, v, len);
1854 break;
1855 default:
1856 return -EINVAL;
1857 }
1858
1859 if (qemu_file_has_error(f))
1860 return -EIO;
1861
1862 return 0;
1863}
1864
1865static int ram_load_v1(QEMUFile *f, void *opaque)
1866{
1867 int ret;
1868 ram_addr_t i;
1869
1870 if (qemu_get_be32(f) != last_ram_offset)
1871 return -EINVAL;
1872 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1873 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1874 if (ret)
1875 return ret;
1876 }
1877 return 0;
1878}
1879
1880#define BDRV_HASH_BLOCK_SIZE 1024
1881#define IOBUF_SIZE 4096
1882#define RAM_CBLOCK_MAGIC 0xfabe
1883
1884typedef struct RamDecompressState {
1885 z_stream zstream;
1886 QEMUFile *f;
1887 uint8_t buf[IOBUF_SIZE];
1888} RamDecompressState;
1889
1890static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1891{
1892 int ret;
1893 memset(s, 0, sizeof(*s));
1894 s->f = f;
1895 ret = inflateInit(&s->zstream);
1896 if (ret != Z_OK)
1897 return -1;
1898 return 0;
1899}
1900
1901static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1902{
1903 int ret, clen;
1904
1905 s->zstream.avail_out = len;
1906 s->zstream.next_out = buf;
1907 while (s->zstream.avail_out > 0) {
1908 if (s->zstream.avail_in == 0) {
1909 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1910 return -1;
1911 clen = qemu_get_be16(s->f);
1912 if (clen > IOBUF_SIZE)
1913 return -1;
1914 qemu_get_buffer(s->f, s->buf, clen);
1915 s->zstream.avail_in = clen;
1916 s->zstream.next_in = s->buf;
1917 }
1918 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1919 if (ret != Z_OK && ret != Z_STREAM_END) {
1920 return -1;
1921 }
1922 }
1923 return 0;
1924}
1925
1926static void ram_decompress_close(RamDecompressState *s)
1927{
1928 inflateEnd(&s->zstream);
1929}
1930
1931#define RAM_SAVE_FLAG_FULL 0x01
1932#define RAM_SAVE_FLAG_COMPRESS 0x02
1933#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1934#define RAM_SAVE_FLAG_PAGE 0x08
1935#define RAM_SAVE_FLAG_EOS 0x10
1936
1937static int is_dup_page(uint8_t *page, uint8_t ch)
1938{
1939 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1940 uint32_t *array = (uint32_t *)page;
1941 int i;
1942
1943 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1944 if (array[i] != val)
1945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951static int ram_save_block(QEMUFile *f)
1952{
1953 static ram_addr_t current_addr = 0;
1954 ram_addr_t saved_addr = current_addr;
1955 ram_addr_t addr = 0;
1956 int found = 0;
1957
1958 while (addr < last_ram_offset) {
1959 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1960 uint8_t *p;
1961
1962 cpu_physical_memory_reset_dirty(current_addr,
1963 current_addr + TARGET_PAGE_SIZE,
1964 MIGRATION_DIRTY_FLAG);
1965
1966 p = qemu_get_ram_ptr(current_addr);
1967
1968 if (is_dup_page(p, *p)) {
1969 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
1970 qemu_put_byte(f, *p);
1971 } else {
1972 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
1973 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
1974 }
1975
1976 found = 1;
1977 break;
1978 }
1979 addr += TARGET_PAGE_SIZE;
1980 current_addr = (saved_addr + addr) % last_ram_offset;
1981 }
1982
1983 return found;
1984}
1985
1986static uint64_t bytes_transferred = 0;
1987
1988static ram_addr_t ram_save_remaining(void)
1989{
1990 ram_addr_t addr;
1991 ram_addr_t count = 0;
1992
1993 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
1994 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1995 count++;
1996 }
1997
1998 return count;
1999}
2000
2001uint64_t ram_bytes_remaining(void)
2002{
2003 return ram_save_remaining() * TARGET_PAGE_SIZE;
2004}
2005
2006uint64_t ram_bytes_transferred(void)
2007{
2008 return bytes_transferred;
2009}
2010
2011uint64_t ram_bytes_total(void)
2012{
2013 return last_ram_offset;
2014}
2015
2016static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2017{
2018 ram_addr_t addr;
2019 uint64_t bytes_transferred_last;
2020 double bwidth = 0;
2021 uint64_t expected_time = 0;
2022
2023 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2024
2025 if (stage == 1) {
2026 /* Make sure all dirty bits are set */
2027 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2028 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2029 cpu_physical_memory_set_dirty(addr);
2030 }
2031
2032 /* Enable dirty memory tracking */
2033 cpu_physical_memory_set_dirty_tracking(1);
2034
2035 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2036 }
2037
2038 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002039 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002040
2041 while (!qemu_file_rate_limit(f)) {
2042 int ret;
2043
2044 ret = ram_save_block(f);
2045 bytes_transferred += ret * TARGET_PAGE_SIZE;
2046 if (ret == 0) /* no more blocks */
2047 break;
2048 }
2049
David Turner6a9ef172010-09-09 22:54:36 +02002050 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002051 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2052
2053 /* if we haven't transferred anything this round, force expected_time to a
2054 * a very high value, but without crashing */
2055 if (bwidth == 0)
2056 bwidth = 0.000001;
2057
2058 /* try transferring iterative blocks of memory */
2059
2060 if (stage == 3) {
2061
2062 /* flush all remaining blocks regardless of rate limiting */
2063 while (ram_save_block(f) != 0) {
2064 bytes_transferred += TARGET_PAGE_SIZE;
2065 }
2066 cpu_physical_memory_set_dirty_tracking(0);
2067 }
2068
2069 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2070
2071 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2072
2073 return (stage == 2) && (expected_time <= migrate_max_downtime());
2074}
2075
2076static int ram_load_dead(QEMUFile *f, void *opaque)
2077{
2078 RamDecompressState s1, *s = &s1;
2079 uint8_t buf[10];
2080 ram_addr_t i;
2081
2082 if (ram_decompress_open(s, f) < 0)
2083 return -EINVAL;
2084 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2085 if (ram_decompress_buf(s, buf, 1) < 0) {
2086 fprintf(stderr, "Error while reading ram block header\n");
2087 goto error;
2088 }
2089 if (buf[0] == 0) {
2090 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2091 BDRV_HASH_BLOCK_SIZE) < 0) {
2092 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2093 goto error;
2094 }
2095 } else {
2096 error:
2097 printf("Error block header\n");
2098 return -EINVAL;
2099 }
2100 }
2101 ram_decompress_close(s);
2102
2103 return 0;
2104}
2105
2106static int ram_load(QEMUFile *f, void *opaque, int version_id)
2107{
2108 ram_addr_t addr;
2109 int flags;
2110
2111 if (version_id == 1)
2112 return ram_load_v1(f, opaque);
2113
2114 if (version_id == 2) {
2115 if (qemu_get_be32(f) != last_ram_offset)
2116 return -EINVAL;
2117 return ram_load_dead(f, opaque);
2118 }
2119
2120 if (version_id != 3)
2121 return -EINVAL;
2122
2123 do {
2124 addr = qemu_get_be64(f);
2125
2126 flags = addr & ~TARGET_PAGE_MASK;
2127 addr &= TARGET_PAGE_MASK;
2128
2129 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2130 if (addr != last_ram_offset)
2131 return -EINVAL;
2132 }
2133
2134 if (flags & RAM_SAVE_FLAG_FULL) {
2135 if (ram_load_dead(f, opaque) < 0)
2136 return -EINVAL;
2137 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002138
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002139 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2140 uint8_t ch = qemu_get_byte(f);
2141 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2142 } else if (flags & RAM_SAVE_FLAG_PAGE)
2143 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2144 } while (!(flags & RAM_SAVE_FLAG_EOS));
2145
2146 return 0;
2147}
2148
2149void qemu_service_io(void)
2150{
2151 qemu_notify_event();
2152}
2153
2154/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002155/* machine registration */
2156
2157static QEMUMachine *first_machine = NULL;
2158QEMUMachine *current_machine = NULL;
2159
2160int qemu_register_machine(QEMUMachine *m)
2161{
2162 QEMUMachine **pm;
2163 pm = &first_machine;
2164 while (*pm != NULL)
2165 pm = &(*pm)->next;
2166 m->next = NULL;
2167 *pm = m;
2168 return 0;
2169}
2170
2171static QEMUMachine *find_machine(const char *name)
2172{
2173 QEMUMachine *m;
2174
2175 for(m = first_machine; m != NULL; m = m->next) {
2176 if (!strcmp(m->name, name))
2177 return m;
2178 }
2179 return NULL;
2180}
2181
2182static QEMUMachine *find_default_machine(void)
2183{
2184 QEMUMachine *m;
2185
2186 for(m = first_machine; m != NULL; m = m->next) {
2187 if (m->is_default) {
2188 return m;
2189 }
2190 }
2191 return NULL;
2192}
2193
2194/***********************************************************/
2195/* main execution loop */
2196
2197static void gui_update(void *opaque)
2198{
2199 uint64_t interval = GUI_REFRESH_INTERVAL;
2200 DisplayState *ds = opaque;
2201 DisplayChangeListener *dcl = ds->listeners;
2202
2203 dpy_refresh(ds);
2204
2205 while (dcl != NULL) {
2206 if (dcl->gui_timer_interval &&
2207 dcl->gui_timer_interval < interval)
2208 interval = dcl->gui_timer_interval;
2209 dcl = dcl->next;
2210 }
2211 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
2212}
2213
2214static void nographic_update(void *opaque)
2215{
2216 uint64_t interval = GUI_REFRESH_INTERVAL;
2217
2218 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
2219}
2220
2221struct vm_change_state_entry {
2222 VMChangeStateHandler *cb;
2223 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002224 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002225};
2226
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002227static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002228
2229VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2230 void *opaque)
2231{
2232 VMChangeStateEntry *e;
2233
2234 e = qemu_mallocz(sizeof (*e));
2235
2236 e->cb = cb;
2237 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002238 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002239 return e;
2240}
2241
2242void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2243{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002244 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002245 qemu_free (e);
2246}
2247
2248static void vm_state_notify(int running, int reason)
2249{
2250 VMChangeStateEntry *e;
2251
2252 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2253 e->cb(e->opaque, running, reason);
2254 }
2255}
2256
2257static void resume_all_vcpus(void);
2258static void pause_all_vcpus(void);
2259
2260void vm_start(void)
2261{
2262 if (!vm_running) {
2263 cpu_enable_ticks();
2264 vm_running = 1;
2265 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002266 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002267 resume_all_vcpus();
2268 }
2269}
2270
2271/* reset/shutdown handler */
2272
2273typedef struct QEMUResetEntry {
2274 QEMUResetHandler *func;
2275 void *opaque;
2276 int order;
2277 struct QEMUResetEntry *next;
2278} QEMUResetEntry;
2279
2280static QEMUResetEntry *first_reset_entry;
2281static int reset_requested;
2282static int shutdown_requested;
2283static int powerdown_requested;
2284static int debug_requested;
2285static int vmstop_requested;
2286
2287int qemu_shutdown_requested(void)
2288{
2289 int r = shutdown_requested;
2290 shutdown_requested = 0;
2291 return r;
2292}
2293
2294int qemu_reset_requested(void)
2295{
2296 int r = reset_requested;
2297 reset_requested = 0;
2298 return r;
2299}
2300
2301int qemu_powerdown_requested(void)
2302{
2303 int r = powerdown_requested;
2304 powerdown_requested = 0;
2305 return r;
2306}
2307
2308static int qemu_debug_requested(void)
2309{
2310 int r = debug_requested;
2311 debug_requested = 0;
2312 return r;
2313}
2314
2315static int qemu_vmstop_requested(void)
2316{
2317 int r = vmstop_requested;
2318 vmstop_requested = 0;
2319 return r;
2320}
2321
2322static void do_vm_stop(int reason)
2323{
2324 if (vm_running) {
2325 cpu_disable_ticks();
2326 vm_running = 0;
2327 pause_all_vcpus();
2328 vm_state_notify(0, reason);
2329 }
2330}
2331
2332void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2333{
2334 QEMUResetEntry **pre, *re;
2335
2336 pre = &first_reset_entry;
2337 while (*pre != NULL && (*pre)->order >= order) {
2338 pre = &(*pre)->next;
2339 }
2340 re = qemu_mallocz(sizeof(QEMUResetEntry));
2341 re->func = func;
2342 re->opaque = opaque;
2343 re->order = order;
2344 re->next = NULL;
2345 *pre = re;
2346}
2347
2348void qemu_system_reset(void)
2349{
2350 QEMUResetEntry *re;
2351
2352 /* reset all devices */
2353 for(re = first_reset_entry; re != NULL; re = re->next) {
2354 re->func(re->opaque);
2355 }
2356}
2357
2358void qemu_system_reset_request(void)
2359{
2360 if (no_reboot) {
2361 shutdown_requested = 1;
2362 } else {
2363 reset_requested = 1;
2364 }
2365 qemu_notify_event();
2366}
2367
2368void qemu_system_shutdown_request(void)
2369{
2370 shutdown_requested = 1;
2371 qemu_notify_event();
2372}
2373
2374void qemu_system_powerdown_request(void)
2375{
2376 powerdown_requested = 1;
2377 qemu_notify_event();
2378}
2379
2380#ifdef CONFIG_IOTHREAD
2381static void qemu_system_vmstop_request(int reason)
2382{
2383 vmstop_requested = reason;
2384 qemu_notify_event();
2385}
2386#endif
2387
2388#ifndef _WIN32
2389static int io_thread_fd = -1;
2390
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002391#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002392static void qemu_event_increment(void)
2393{
2394 static const char byte = 0;
2395
2396 if (io_thread_fd == -1)
2397 return;
2398
2399 write(io_thread_fd, &byte, sizeof(byte));
2400}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002401#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002402
2403static void qemu_event_read(void *opaque)
2404{
2405 int fd = (unsigned long)opaque;
2406 ssize_t len;
2407
2408 /* Drain the notify pipe */
2409 do {
2410 char buffer[512];
2411 len = read(fd, buffer, sizeof(buffer));
2412 } while ((len == -1 && errno == EINTR) || len > 0);
2413}
2414
2415static int qemu_event_init(void)
2416{
2417 int err;
2418 int fds[2];
2419
2420 err = pipe(fds);
2421 if (err == -1)
2422 return -errno;
2423
2424 err = fcntl_setfl(fds[0], O_NONBLOCK);
2425 if (err < 0)
2426 goto fail;
2427
2428 err = fcntl_setfl(fds[1], O_NONBLOCK);
2429 if (err < 0)
2430 goto fail;
2431
2432 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2433 (void *)(unsigned long)fds[0]);
2434
2435 io_thread_fd = fds[1];
2436 return 0;
2437
2438fail:
2439 close(fds[0]);
2440 close(fds[1]);
2441 return err;
2442}
2443#else
2444HANDLE qemu_event_handle;
2445
2446static void dummy_event_handler(void *opaque)
2447{
2448}
2449
2450static int qemu_event_init(void)
2451{
2452 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2453 if (!qemu_event_handle) {
2454 perror("Failed CreateEvent");
2455 return -1;
2456 }
2457 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2458 return 0;
2459}
2460
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002461#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002462static void qemu_event_increment(void)
2463{
2464 SetEvent(qemu_event_handle);
2465}
2466#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002467#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002468
2469static int cpu_can_run(CPUState *env)
2470{
2471 if (env->stop)
2472 return 0;
2473 if (env->stopped)
2474 return 0;
2475 return 1;
2476}
2477
2478#ifndef CONFIG_IOTHREAD
2479static int qemu_init_main_loop(void)
2480{
2481 return qemu_event_init();
2482}
2483
2484void qemu_init_vcpu(void *_env)
2485{
2486 CPUState *env = _env;
2487
2488 if (kvm_enabled())
2489 kvm_init_vcpu(env);
2490 return;
2491}
2492
2493int qemu_cpu_self(void *env)
2494{
2495 return 1;
2496}
2497
2498static void resume_all_vcpus(void)
2499{
2500}
2501
2502static void pause_all_vcpus(void)
2503{
2504}
2505
2506void qemu_cpu_kick(void *env)
2507{
2508 return;
2509}
2510
2511void qemu_notify_event(void)
2512{
2513 CPUState *env = cpu_single_env;
2514
2515 if (env) {
2516 cpu_exit(env);
2517#ifdef USE_KQEMU
2518 if (env->kqemu_enabled)
2519 kqemu_cpu_interrupt(env);
2520#endif
2521 }
2522}
2523
2524#define qemu_mutex_lock_iothread() do { } while (0)
2525#define qemu_mutex_unlock_iothread() do { } while (0)
2526
2527void vm_stop(int reason)
2528{
2529 do_vm_stop(reason);
2530}
2531
2532#else /* CONFIG_IOTHREAD */
2533
2534#include "qemu-thread.h"
2535
2536QemuMutex qemu_global_mutex;
2537static QemuMutex qemu_fair_mutex;
2538
2539static QemuThread io_thread;
2540
2541static QemuThread *tcg_cpu_thread;
2542static QemuCond *tcg_halt_cond;
2543
2544static int qemu_system_ready;
2545/* cpu creation */
2546static QemuCond qemu_cpu_cond;
2547/* system init */
2548static QemuCond qemu_system_cond;
2549static QemuCond qemu_pause_cond;
2550
2551static void block_io_signals(void);
2552static void unblock_io_signals(void);
2553static int tcg_has_work(void);
2554
2555static int qemu_init_main_loop(void)
2556{
2557 int ret;
2558
2559 ret = qemu_event_init();
2560 if (ret)
2561 return ret;
2562
2563 qemu_cond_init(&qemu_pause_cond);
2564 qemu_mutex_init(&qemu_fair_mutex);
2565 qemu_mutex_init(&qemu_global_mutex);
2566 qemu_mutex_lock(&qemu_global_mutex);
2567
2568 unblock_io_signals();
2569 qemu_thread_self(&io_thread);
2570
2571 return 0;
2572}
2573
2574static void qemu_wait_io_event(CPUState *env)
2575{
2576 while (!tcg_has_work())
2577 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2578
2579 qemu_mutex_unlock(&qemu_global_mutex);
2580
2581 /*
2582 * Users of qemu_global_mutex can be starved, having no chance
2583 * to acquire it since this path will get to it first.
2584 * So use another lock to provide fairness.
2585 */
2586 qemu_mutex_lock(&qemu_fair_mutex);
2587 qemu_mutex_unlock(&qemu_fair_mutex);
2588
2589 qemu_mutex_lock(&qemu_global_mutex);
2590 if (env->stop) {
2591 env->stop = 0;
2592 env->stopped = 1;
2593 qemu_cond_signal(&qemu_pause_cond);
2594 }
2595}
2596
2597static int qemu_cpu_exec(CPUState *env);
2598
2599static void *kvm_cpu_thread_fn(void *arg)
2600{
2601 CPUState *env = arg;
2602
2603 block_io_signals();
2604 qemu_thread_self(env->thread);
2605
2606 /* signal CPU creation */
2607 qemu_mutex_lock(&qemu_global_mutex);
2608 env->created = 1;
2609 qemu_cond_signal(&qemu_cpu_cond);
2610
2611 /* and wait for machine initialization */
2612 while (!qemu_system_ready)
2613 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2614
2615 while (1) {
2616 if (cpu_can_run(env))
2617 qemu_cpu_exec(env);
2618 qemu_wait_io_event(env);
2619 }
2620
2621 return NULL;
2622}
2623
2624static void tcg_cpu_exec(void);
2625
2626static void *tcg_cpu_thread_fn(void *arg)
2627{
2628 CPUState *env = arg;
2629
2630 block_io_signals();
2631 qemu_thread_self(env->thread);
2632
2633 /* signal CPU creation */
2634 qemu_mutex_lock(&qemu_global_mutex);
2635 for (env = first_cpu; env != NULL; env = env->next_cpu)
2636 env->created = 1;
2637 qemu_cond_signal(&qemu_cpu_cond);
2638
2639 /* and wait for machine initialization */
2640 while (!qemu_system_ready)
2641 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2642
2643 while (1) {
2644 tcg_cpu_exec();
2645 qemu_wait_io_event(cur_cpu);
2646 }
2647
2648 return NULL;
2649}
2650
2651void qemu_cpu_kick(void *_env)
2652{
2653 CPUState *env = _env;
2654 qemu_cond_broadcast(env->halt_cond);
2655 if (kvm_enabled())
2656 qemu_thread_signal(env->thread, SIGUSR1);
2657}
2658
2659int qemu_cpu_self(void *env)
2660{
2661 return (cpu_single_env != NULL);
2662}
2663
2664static void cpu_signal(int sig)
2665{
2666 if (cpu_single_env)
2667 cpu_exit(cpu_single_env);
2668}
2669
2670static void block_io_signals(void)
2671{
2672 sigset_t set;
2673 struct sigaction sigact;
2674
2675 sigemptyset(&set);
2676 sigaddset(&set, SIGUSR2);
2677 sigaddset(&set, SIGIO);
2678 sigaddset(&set, SIGALRM);
2679 pthread_sigmask(SIG_BLOCK, &set, NULL);
2680
2681 sigemptyset(&set);
2682 sigaddset(&set, SIGUSR1);
2683 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2684
2685 memset(&sigact, 0, sizeof(sigact));
2686 sigact.sa_handler = cpu_signal;
2687 sigaction(SIGUSR1, &sigact, NULL);
2688}
2689
2690static void unblock_io_signals(void)
2691{
2692 sigset_t set;
2693
2694 sigemptyset(&set);
2695 sigaddset(&set, SIGUSR2);
2696 sigaddset(&set, SIGIO);
2697 sigaddset(&set, SIGALRM);
2698 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2699
2700 sigemptyset(&set);
2701 sigaddset(&set, SIGUSR1);
2702 pthread_sigmask(SIG_BLOCK, &set, NULL);
2703}
2704
2705static void qemu_signal_lock(unsigned int msecs)
2706{
2707 qemu_mutex_lock(&qemu_fair_mutex);
2708
2709 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2710 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2711 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2712 break;
2713 }
2714 qemu_mutex_unlock(&qemu_fair_mutex);
2715}
2716
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002717void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002718{
2719 if (kvm_enabled()) {
2720 qemu_mutex_lock(&qemu_fair_mutex);
2721 qemu_mutex_lock(&qemu_global_mutex);
2722 qemu_mutex_unlock(&qemu_fair_mutex);
2723 } else
2724 qemu_signal_lock(100);
2725}
2726
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002727void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002728{
2729 qemu_mutex_unlock(&qemu_global_mutex);
2730}
2731
2732static int all_vcpus_paused(void)
2733{
2734 CPUState *penv = first_cpu;
2735
2736 while (penv) {
2737 if (!penv->stopped)
2738 return 0;
2739 penv = (CPUState *)penv->next_cpu;
2740 }
2741
2742 return 1;
2743}
2744
2745static void pause_all_vcpus(void)
2746{
2747 CPUState *penv = first_cpu;
2748
2749 while (penv) {
2750 penv->stop = 1;
2751 qemu_thread_signal(penv->thread, SIGUSR1);
2752 qemu_cpu_kick(penv);
2753 penv = (CPUState *)penv->next_cpu;
2754 }
2755
2756 while (!all_vcpus_paused()) {
2757 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2758 penv = first_cpu;
2759 while (penv) {
2760 qemu_thread_signal(penv->thread, SIGUSR1);
2761 penv = (CPUState *)penv->next_cpu;
2762 }
2763 }
2764}
2765
2766static void resume_all_vcpus(void)
2767{
2768 CPUState *penv = first_cpu;
2769
2770 while (penv) {
2771 penv->stop = 0;
2772 penv->stopped = 0;
2773 qemu_thread_signal(penv->thread, SIGUSR1);
2774 qemu_cpu_kick(penv);
2775 penv = (CPUState *)penv->next_cpu;
2776 }
2777}
2778
2779static void tcg_init_vcpu(void *_env)
2780{
2781 CPUState *env = _env;
2782 /* share a single thread for all cpus with TCG */
2783 if (!tcg_cpu_thread) {
2784 env->thread = qemu_mallocz(sizeof(QemuThread));
2785 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2786 qemu_cond_init(env->halt_cond);
2787 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2788 while (env->created == 0)
2789 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2790 tcg_cpu_thread = env->thread;
2791 tcg_halt_cond = env->halt_cond;
2792 } else {
2793 env->thread = tcg_cpu_thread;
2794 env->halt_cond = tcg_halt_cond;
2795 }
2796}
2797
2798static void kvm_start_vcpu(CPUState *env)
2799{
2800#if 0
2801 kvm_init_vcpu(env);
2802 env->thread = qemu_mallocz(sizeof(QemuThread));
2803 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2804 qemu_cond_init(env->halt_cond);
2805 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2806 while (env->created == 0)
2807 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2808#endif
2809}
2810
2811void qemu_init_vcpu(void *_env)
2812{
2813 CPUState *env = _env;
2814
2815 if (kvm_enabled())
2816 kvm_start_vcpu(env);
2817 else
2818 tcg_init_vcpu(env);
2819}
2820
2821void qemu_notify_event(void)
2822{
2823 qemu_event_increment();
2824}
2825
2826void vm_stop(int reason)
2827{
2828 QemuThread me;
2829 qemu_thread_self(&me);
2830
2831 if (!qemu_thread_equal(&me, &io_thread)) {
2832 qemu_system_vmstop_request(reason);
2833 /*
2834 * FIXME: should not return to device code in case
2835 * vm_stop() has been requested.
2836 */
2837 if (cpu_single_env) {
2838 cpu_exit(cpu_single_env);
2839 cpu_single_env->stop = 1;
2840 }
2841 return;
2842 }
2843 do_vm_stop(reason);
2844}
2845
2846#endif
2847
2848
2849#ifdef _WIN32
2850static void host_main_loop_wait(int *timeout)
2851{
2852 int ret, ret2, i;
2853 PollingEntry *pe;
2854
2855
2856 /* XXX: need to suppress polling by better using win32 events */
2857 ret = 0;
2858 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2859 ret |= pe->func(pe->opaque);
2860 }
2861 if (ret == 0) {
2862 int err;
2863 WaitObjects *w = &wait_objects;
2864
2865 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2866 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2867 if (w->func[ret - WAIT_OBJECT_0])
2868 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2869
2870 /* Check for additional signaled events */
2871 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2872
2873 /* Check if event is signaled */
2874 ret2 = WaitForSingleObject(w->events[i], 0);
2875 if(ret2 == WAIT_OBJECT_0) {
2876 if (w->func[i])
2877 w->func[i](w->opaque[i]);
2878 } else if (ret2 == WAIT_TIMEOUT) {
2879 } else {
2880 err = GetLastError();
2881 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2882 }
2883 }
2884 } else if (ret == WAIT_TIMEOUT) {
2885 } else {
2886 err = GetLastError();
2887 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2888 }
2889 }
2890
2891 *timeout = 0;
2892}
2893#else
2894static void host_main_loop_wait(int *timeout)
2895{
2896}
2897#endif
2898
2899void main_loop_wait(int timeout)
2900{
2901 IOHandlerRecord *ioh;
2902 fd_set rfds, wfds, xfds;
2903 int ret, nfds;
2904 struct timeval tv;
2905
2906 qemu_bh_update_timeout(&timeout);
2907
2908 host_main_loop_wait(&timeout);
2909
2910 /* poll any events */
2911 /* XXX: separate device handlers from system ones */
2912 nfds = -1;
2913 FD_ZERO(&rfds);
2914 FD_ZERO(&wfds);
2915 FD_ZERO(&xfds);
2916 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2917 if (ioh->deleted)
2918 continue;
2919 if (ioh->fd_read &&
2920 (!ioh->fd_read_poll ||
2921 ioh->fd_read_poll(ioh->opaque) != 0)) {
2922 FD_SET(ioh->fd, &rfds);
2923 if (ioh->fd > nfds)
2924 nfds = ioh->fd;
2925 }
2926 if (ioh->fd_write) {
2927 FD_SET(ioh->fd, &wfds);
2928 if (ioh->fd > nfds)
2929 nfds = ioh->fd;
2930 }
2931 }
2932
2933 tv.tv_sec = timeout / 1000;
2934 tv.tv_usec = (timeout % 1000) * 1000;
2935
2936#if defined(CONFIG_SLIRP)
2937 if (slirp_is_inited()) {
2938 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2939 }
2940#endif
2941 qemu_mutex_unlock_iothread();
2942 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2943 qemu_mutex_lock_iothread();
2944 if (ret > 0) {
2945 IOHandlerRecord **pioh;
2946
2947 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2948 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2949 ioh->fd_read(ioh->opaque);
2950 }
2951 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2952 ioh->fd_write(ioh->opaque);
2953 }
2954 }
2955
David 'Digit' Turner6b512812010-10-15 15:05:04 +02002956 /* remove deleted IO handlers */
2957 pioh = &first_io_handler;
2958 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002959 ioh = *pioh;
2960 if (ioh->deleted) {
2961 *pioh = ioh->next;
2962 qemu_free(ioh);
2963 } else
2964 pioh = &ioh->next;
2965 }
2966 }
2967#if defined(CONFIG_SLIRP)
2968 if (slirp_is_inited()) {
2969 if (ret < 0) {
2970 FD_ZERO(&rfds);
2971 FD_ZERO(&wfds);
2972 FD_ZERO(&xfds);
2973 }
2974 slirp_select_poll(&rfds, &wfds, &xfds);
2975 }
2976#endif
2977 charpipe_poll();
2978
David Turner6a9ef172010-09-09 22:54:36 +02002979 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002980
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002981 /* Check bottom-halves last in case any of the earlier events triggered
2982 them. */
2983 qemu_bh_poll();
2984
2985}
2986
2987static int qemu_cpu_exec(CPUState *env)
2988{
2989 int ret;
2990#ifdef CONFIG_PROFILER
2991 int64_t ti;
2992#endif
2993
2994#ifdef CONFIG_PROFILER
2995 ti = profile_getclock();
2996#endif
2997 if (use_icount) {
2998 int64_t count;
2999 int decr;
3000 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3001 env->icount_decr.u16.low = 0;
3002 env->icount_extra = 0;
3003 count = qemu_next_deadline();
3004 count = (count + (1 << icount_time_shift) - 1)
3005 >> icount_time_shift;
3006 qemu_icount += count;
3007 decr = (count > 0xffff) ? 0xffff : count;
3008 count -= decr;
3009 env->icount_decr.u16.low = decr;
3010 env->icount_extra = count;
3011 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003012#ifdef CONFIG_TRACE
3013 if (tbflush_requested) {
3014 tbflush_requested = 0;
3015 tb_flush(env);
3016 return EXCP_INTERRUPT;
3017 }
3018#endif
3019
3020
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003021 ret = cpu_exec(env);
3022#ifdef CONFIG_PROFILER
3023 qemu_time += profile_getclock() - ti;
3024#endif
3025 if (use_icount) {
3026 /* Fold pending instructions back into the
3027 instruction counter, and clear the interrupt flag. */
3028 qemu_icount -= (env->icount_decr.u16.low
3029 + env->icount_extra);
3030 env->icount_decr.u32 = 0;
3031 env->icount_extra = 0;
3032 }
3033 return ret;
3034}
3035
3036static void tcg_cpu_exec(void)
3037{
3038 int ret = 0;
3039
3040 if (next_cpu == NULL)
3041 next_cpu = first_cpu;
3042 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3043 CPUState *env = cur_cpu = next_cpu;
3044
3045 if (!vm_running)
3046 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003047 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003048 break;
3049 }
3050 if (cpu_can_run(env))
3051 ret = qemu_cpu_exec(env);
3052 if (ret == EXCP_DEBUG) {
3053 gdb_set_stop_cpu(env);
3054 debug_requested = 1;
3055 break;
3056 }
3057 }
3058}
3059
3060static int cpu_has_work(CPUState *env)
3061{
3062 if (env->stop)
3063 return 1;
3064 if (env->stopped)
3065 return 0;
3066 if (!env->halted)
3067 return 1;
3068 if (qemu_cpu_has_work(env))
3069 return 1;
3070 return 0;
3071}
3072
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003073int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003074{
3075 CPUState *env;
3076
3077 for (env = first_cpu; env != NULL; env = env->next_cpu)
3078 if (cpu_has_work(env))
3079 return 1;
3080 return 0;
3081}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003082
3083static int vm_can_run(void)
3084{
3085 if (powerdown_requested)
3086 return 0;
3087 if (reset_requested)
3088 return 0;
3089 if (shutdown_requested)
3090 return 0;
3091 if (debug_requested)
3092 return 0;
3093 return 1;
3094}
3095
3096static void main_loop(void)
3097{
3098 int r;
3099
3100#ifdef CONFIG_IOTHREAD
3101 qemu_system_ready = 1;
3102 qemu_cond_broadcast(&qemu_system_cond);
3103#endif
3104
3105 for (;;) {
3106 do {
3107#ifdef CONFIG_PROFILER
3108 int64_t ti;
3109#endif
3110#ifndef CONFIG_IOTHREAD
3111 tcg_cpu_exec();
3112#endif
3113#ifdef CONFIG_PROFILER
3114 ti = profile_getclock();
3115#endif
3116 main_loop_wait(qemu_calculate_timeout());
3117#ifdef CONFIG_PROFILER
3118 dev_time += profile_getclock() - ti;
3119#endif
3120 } while (vm_can_run());
3121
3122 if (qemu_debug_requested())
3123 vm_stop(EXCP_DEBUG);
3124 if (qemu_shutdown_requested()) {
3125 if (no_shutdown) {
3126 vm_stop(0);
3127 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003128 } else {
3129#if CONFIG_ANDROID_SNAPSHOTS
3130 if (savevm_on_exit != NULL) {
3131 do_savevm(cur_mon, savevm_on_exit);
3132 }
3133#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003134 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003135 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003136 }
3137 if (qemu_reset_requested()) {
3138 pause_all_vcpus();
3139 qemu_system_reset();
3140 resume_all_vcpus();
3141 }
3142 if (qemu_powerdown_requested())
3143 qemu_system_powerdown();
3144 if ((r = qemu_vmstop_requested()))
3145 vm_stop(r);
3146 }
3147 pause_all_vcpus();
3148}
3149
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003150void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003151{
3152 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3153}
3154
3155void qemu_help(int exitcode)
3156{
3157 version();
3158 printf("usage: %s [options] [disk_image]\n"
3159 "\n"
3160 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3161 "\n"
3162#define DEF(option, opt_arg, opt_enum, opt_help) \
3163 opt_help
3164#define DEFHEADING(text) stringify(text) "\n"
3165#include "qemu-options.h"
3166#undef DEF
3167#undef DEFHEADING
3168#undef GEN_DOCS
3169 "\n"
3170 "During emulation, the following keys are useful:\n"
3171 "ctrl-alt-f toggle full screen\n"
3172 "ctrl-alt-n switch to virtual console 'n'\n"
3173 "ctrl-alt toggle mouse and keyboard grab\n"
3174 "\n"
3175 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3176 ,
3177 "qemu",
3178 DEFAULT_RAM_SIZE,
3179#ifndef _WIN32
3180 DEFAULT_NETWORK_SCRIPT,
3181 DEFAULT_NETWORK_DOWN_SCRIPT,
3182#endif
3183 DEFAULT_GDBSTUB_PORT,
3184 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003185 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003186}
3187
3188#define HAS_ARG 0x0001
3189
3190enum {
3191#define DEF(option, opt_arg, opt_enum, opt_help) \
3192 opt_enum,
3193#define DEFHEADING(text)
3194#include "qemu-options.h"
3195#undef DEF
3196#undef DEFHEADING
3197#undef GEN_DOCS
3198};
3199
3200typedef struct QEMUOption {
3201 const char *name;
3202 int flags;
3203 int index;
3204} QEMUOption;
3205
3206static const QEMUOption qemu_options[] = {
3207 { "h", 0, QEMU_OPTION_h },
3208#define DEF(option, opt_arg, opt_enum, opt_help) \
3209 { option, opt_arg, opt_enum },
3210#define DEFHEADING(text)
3211#include "qemu-options.h"
3212#undef DEF
3213#undef DEFHEADING
3214#undef GEN_DOCS
3215 { NULL, 0, 0 },
3216};
3217
3218#ifdef HAS_AUDIO
3219struct soundhw soundhw[] = {
3220#ifdef HAS_AUDIO_CHOICE
3221#if defined(TARGET_I386) || defined(TARGET_MIPS)
3222 {
3223 "pcspk",
3224 "PC speaker",
3225 0,
3226 1,
3227 { .init_isa = pcspk_audio_init }
3228 },
3229#endif
3230
3231#ifdef CONFIG_SB16
3232 {
3233 "sb16",
3234 "Creative Sound Blaster 16",
3235 0,
3236 1,
3237 { .init_isa = SB16_init }
3238 },
3239#endif
3240
3241#ifdef CONFIG_CS4231A
3242 {
3243 "cs4231a",
3244 "CS4231A",
3245 0,
3246 1,
3247 { .init_isa = cs4231a_init }
3248 },
3249#endif
3250
3251#ifdef CONFIG_ADLIB
3252 {
3253 "adlib",
3254#ifdef HAS_YMF262
3255 "Yamaha YMF262 (OPL3)",
3256#else
3257 "Yamaha YM3812 (OPL2)",
3258#endif
3259 0,
3260 1,
3261 { .init_isa = Adlib_init }
3262 },
3263#endif
3264
3265#ifdef CONFIG_GUS
3266 {
3267 "gus",
3268 "Gravis Ultrasound GF1",
3269 0,
3270 1,
3271 { .init_isa = GUS_init }
3272 },
3273#endif
3274
3275#ifdef CONFIG_AC97
3276 {
3277 "ac97",
3278 "Intel 82801AA AC97 Audio",
3279 0,
3280 0,
3281 { .init_pci = ac97_init }
3282 },
3283#endif
3284
3285#ifdef CONFIG_ES1370
3286 {
3287 "es1370",
3288 "ENSONIQ AudioPCI ES1370",
3289 0,
3290 0,
3291 { .init_pci = es1370_init }
3292 },
3293#endif
3294
3295#endif /* HAS_AUDIO_CHOICE */
3296
3297 { NULL, NULL, 0, 0, { NULL } }
3298};
3299
3300static void select_soundhw (const char *optarg)
3301{
3302 struct soundhw *c;
3303
3304 if (*optarg == '?') {
3305 show_valid_cards:
3306
3307 printf ("Valid sound card names (comma separated):\n");
3308 for (c = soundhw; c->name; ++c) {
3309 printf ("%-11s %s\n", c->name, c->descr);
3310 }
3311 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003312 if (*optarg != '?') {
3313 PANIC("Unknown sound card name: %s", optarg);
3314 } else {
3315 QEMU_EXIT(0);
3316 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003317 }
3318 else {
3319 size_t l;
3320 const char *p;
3321 char *e;
3322 int bad_card = 0;
3323
3324 if (!strcmp (optarg, "all")) {
3325 for (c = soundhw; c->name; ++c) {
3326 c->enabled = 1;
3327 }
3328 return;
3329 }
3330
3331 p = optarg;
3332 while (*p) {
3333 e = strchr (p, ',');
3334 l = !e ? strlen (p) : (size_t) (e - p);
3335
3336 for (c = soundhw; c->name; ++c) {
3337 if (!strncmp (c->name, p, l)) {
3338 c->enabled = 1;
3339 break;
3340 }
3341 }
3342
3343 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003344#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003345 if (l > 80) {
3346 fprintf (stderr,
3347 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003348 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003349 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3350 (int) l, p);
3351 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003352#endif // !CONFIG_ANDROID
3353 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003354 }
3355 p += l + (e != NULL);
3356 }
3357
3358 if (bad_card)
3359 goto show_valid_cards;
3360 }
3361}
3362#endif
3363
3364static void select_vgahw (const char *p)
3365{
3366 const char *opts;
3367
3368 cirrus_vga_enabled = 0;
3369 std_vga_enabled = 0;
3370 vmsvga_enabled = 0;
3371 xenfb_enabled = 0;
3372 if (strstart(p, "std", &opts)) {
3373 std_vga_enabled = 1;
3374 } else if (strstart(p, "cirrus", &opts)) {
3375 cirrus_vga_enabled = 1;
3376 } else if (strstart(p, "vmware", &opts)) {
3377 vmsvga_enabled = 1;
3378 } else if (strstart(p, "xenfb", &opts)) {
3379 xenfb_enabled = 1;
3380 } else if (!strstart(p, "none", &opts)) {
3381 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003382 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003383 }
3384 while (*opts) {
3385 const char *nextopt;
3386
3387 if (strstart(opts, ",retrace=", &nextopt)) {
3388 opts = nextopt;
3389 if (strstart(opts, "dumb", &nextopt))
3390 vga_retrace_method = VGA_RETRACE_DUMB;
3391 else if (strstart(opts, "precise", &nextopt))
3392 vga_retrace_method = VGA_RETRACE_PRECISE;
3393 else goto invalid_vga;
3394 } else goto invalid_vga;
3395 opts = nextopt;
3396 }
3397}
3398
3399#ifdef _WIN32
3400static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3401{
3402 exit(STATUS_CONTROL_C_EXIT);
3403 return TRUE;
3404}
3405#endif
3406
3407int qemu_uuid_parse(const char *str, uint8_t *uuid)
3408{
3409 int ret;
3410
3411 if(strlen(str) != 36)
3412 return -1;
3413
3414 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3415 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3416 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3417
3418 if(ret != 16)
3419 return -1;
3420
3421#ifdef TARGET_I386
3422 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3423#endif
3424
3425 return 0;
3426}
3427
3428#define MAX_NET_CLIENTS 32
3429
3430#ifndef _WIN32
3431
3432static void termsig_handler(int signal)
3433{
3434 qemu_system_shutdown_request();
3435}
3436
3437static void sigchld_handler(int signal)
3438{
3439 waitpid(-1, NULL, WNOHANG);
3440}
3441
3442static void sighandler_setup(void)
3443{
3444 struct sigaction act;
3445
3446 memset(&act, 0, sizeof(act));
3447 act.sa_handler = termsig_handler;
3448 sigaction(SIGINT, &act, NULL);
3449 sigaction(SIGHUP, &act, NULL);
3450 sigaction(SIGTERM, &act, NULL);
3451
3452 act.sa_handler = sigchld_handler;
3453 act.sa_flags = SA_NOCLDSTOP;
3454 sigaction(SIGCHLD, &act, NULL);
3455}
3456
3457#endif
3458
3459#ifdef _WIN32
3460/* Look for support files in the same directory as the executable. */
3461static char *find_datadir(const char *argv0)
3462{
3463 char *p;
3464 char buf[MAX_PATH];
3465 DWORD len;
3466
3467 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3468 if (len == 0) {
3469 return NULL;
3470 }
3471
3472 buf[len] = 0;
3473 p = buf + len - 1;
3474 while (p != buf && *p != '\\')
3475 p--;
3476 *p = 0;
3477 if (access(buf, R_OK) == 0) {
3478 return qemu_strdup(buf);
3479 }
3480 return NULL;
3481}
3482#else /* !_WIN32 */
3483
3484/* Find a likely location for support files using the location of the binary.
3485 For installed binaries this will be "$bindir/../share/qemu". When
3486 running from the build tree this will be "$bindir/../pc-bios". */
3487#define SHARE_SUFFIX "/share/qemu"
3488#define BUILD_SUFFIX "/pc-bios"
3489static char *find_datadir(const char *argv0)
3490{
3491 char *dir;
3492 char *p = NULL;
3493 char *res;
3494#ifdef PATH_MAX
3495 char buf[PATH_MAX];
3496#endif
3497 size_t max_len;
3498
3499#if defined(__linux__)
3500 {
3501 int len;
3502 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3503 if (len > 0) {
3504 buf[len] = 0;
3505 p = buf;
3506 }
3507 }
3508#elif defined(__FreeBSD__)
3509 {
3510 int len;
3511 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3512 if (len > 0) {
3513 buf[len] = 0;
3514 p = buf;
3515 }
3516 }
3517#endif
3518 /* If we don't have any way of figuring out the actual executable
3519 location then try argv[0]. */
3520 if (!p) {
3521#ifdef PATH_MAX
3522 p = buf;
3523#endif
3524 p = realpath(argv0, p);
3525 if (!p) {
3526 return NULL;
3527 }
3528 }
3529 dir = dirname(p);
3530 dir = dirname(dir);
3531
3532 max_len = strlen(dir) +
3533 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
3534 res = qemu_mallocz(max_len);
3535 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
3536 if (access(res, R_OK)) {
3537 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
3538 if (access(res, R_OK)) {
3539 qemu_free(res);
3540 res = NULL;
3541 }
3542 }
3543#ifndef PATH_MAX
3544 free(p);
3545#endif
3546 return res;
3547}
3548#undef SHARE_SUFFIX
3549#undef BUILD_SUFFIX
3550#endif
3551
3552char *qemu_find_file(int type, const char *name)
3553{
3554 int len;
3555 const char *subdir;
3556 char *buf;
3557
3558 /* If name contains path separators then try it as a straight path. */
3559 if ((strchr(name, '/') || strchr(name, '\\'))
3560 && access(name, R_OK) == 0) {
3561 return strdup(name);
3562 }
3563 switch (type) {
3564 case QEMU_FILE_TYPE_BIOS:
3565 subdir = "";
3566 break;
3567 case QEMU_FILE_TYPE_KEYMAP:
3568 subdir = "keymaps/";
3569 break;
3570 default:
3571 abort();
3572 }
3573 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3574 buf = qemu_mallocz(len);
3575 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3576 if (access(buf, R_OK)) {
3577 qemu_free(buf);
3578 return NULL;
3579 }
3580 return buf;
3581}
3582
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003583static int
3584add_dns_server( const char* server_name )
3585{
3586 SockAddress addr;
3587
3588 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3589 fprintf(stdout,
3590 "### WARNING: can't resolve DNS server name '%s'\n",
3591 server_name );
3592 return -1;
3593 }
3594
3595 fprintf(stderr,
3596 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3597
3598 if ( slirp_add_dns_server( &addr ) < 0 ) {
3599 fprintf(stderr,
3600 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3601 return -1;
3602 }
3603 return 0;
3604}
3605
3606/* Appends a parameter to a string of parameters separated with space.
3607 * Pararm:
3608 * param_str String containing parameters separated with space.
3609 * param Parameter to append to the string.
3610 * size - Size (in characters) of the buffer addressed by param_str.
3611 */
3612static void
3613append_param(char* param_str, const char* arg, int size)
3614{
3615 if (*param_str) {
3616 strncat(param_str, " ", size);
3617 strncat(param_str, arg, size);
3618 } else {
3619 strncpy(param_str, arg, size);
3620 param_str[size - 1] = '\0';
3621 }
3622}
3623
rich cannings7339b552011-02-16 13:43:44 -08003624/* Parses an integer
3625 * Pararm:
3626 * str String containing a number to be parsed.
3627 * result Passes the parsed integer in this argument
3628 * returns 0 if ok, -1 if failed
3629 */
3630int
3631parse_int(const char *str, int *result)
3632{
3633 char* r;
3634 *result = strtol(str, &r, 0);
3635 if (r == NULL || *r != '\0')
3636 return -1;
3637
3638 return 0;
3639}
3640
3641
3642/* parses a null-terminated string specifying a network port (e.g., "80") or
3643 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3644 * are the same. Returns 0 on success, -1 on error. */
3645
3646int parse_port_range(const char *str, unsigned short *lport,
3647 unsigned short *hport) {
3648
3649 unsigned int low = 0, high = 0;
3650 char *p, *arg = strdup(str);
3651
3652 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3653 p = arg + 1; /* skip '[' */
3654 low = atoi(strtok(p, "-"));
3655 high = atoi(strtok(NULL, "-"));
3656 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3657 *lport = low;
3658 *hport = high;
3659 }
3660 }
3661 else {
3662 low = atoi(arg);
3663 if ((0 < low) && (low < 65535)) {
3664 *lport = low;
3665 *hport = low;
3666 }
3667 }
3668 free(arg);
3669 if (low != 0)
3670 return 0;
3671 return -1;
3672}
3673
3674/*
3675 * Implements the generic port forwarding option
3676 */
3677void
3678net_slirp_forward(const char *optarg)
3679{
3680 /*
3681 * we expect the following format:
3682 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3683 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3684 */
3685 char *argument = strdup(optarg), *p = argument;
3686 char *dst_net, *dst_mask, *dst_port;
3687 char *redirect_ip, *redirect_port;
3688 uint32_t dnet, dmask, rip;
3689 unsigned short dlport, dhport, rport;
3690
3691
3692 dst_net = strtok(p, ":");
3693 dst_mask = strtok(NULL, ":");
3694 dst_port = strtok(NULL, ":");
3695 redirect_ip = strtok(NULL, ":");
3696 redirect_port = strtok(NULL, ":");
3697
3698 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3699 redirect_ip == NULL || redirect_port == NULL) {
3700 fprintf(stderr,
3701 "Invalid argument for -net-forward, we expect "
3702 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3703 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3704 ":redirect_ip:redirect_port: %s\n",
3705 optarg);
3706 exit(1);
3707 }
3708
3709 /* inet_strtoip converts dotted address to host byte order */
3710 if (inet_strtoip(dst_net, &dnet) == -1) {
3711 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3712 exit(1);
3713 }
3714 if (inet_strtoip(dst_mask, &dmask) == -1) {
3715 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3716 exit(1);
3717 }
3718 if (inet_strtoip(redirect_ip, &rip) == -1) {
3719 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3720 exit(1);
3721 }
3722
3723 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3724 fprintf(stderr, "Invalid destination port or port range\n");
3725 exit(1);
3726 }
3727
3728 rport = atoi(redirect_port);
3729 if (!rport) {
3730 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3731 exit(1);
3732 }
3733
3734 dnet &= dmask;
3735
3736 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3737 rip, rport);
3738
3739 free(argument);
3740}
3741
3742
3743/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3744 * entry in the allows list */
3745void
3746slirp_allow(const char *optarg, u_int8_t proto)
3747{
3748 /*
3749 * we expect the following format:
3750 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3751 */
3752 char *argument = strdup(optarg), *p = argument;
3753 char *dst_ip_str, *dst_port_str;
3754 uint32_t dst_ip;
3755 unsigned short dst_lport, dst_hport;
3756
3757 dst_ip_str = strtok(p, ":");
3758 dst_port_str = strtok(NULL, ":");
3759
3760 if (dst_ip_str == NULL || dst_port_str == NULL) {
3761 fprintf(stderr,
3762 "Invalid argument %s for -allow. We expect "
3763 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3764 optarg);
3765 exit(1);
3766 }
3767
3768 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3769 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3770 exit(1);
3771 }
3772 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3773 fprintf(stderr, "Invalid destination port or port range\n");
3774 exit(1);
3775 }
3776
3777 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3778
3779 free(argument);
3780}
3781
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003782int main(int argc, char **argv, char **envp)
3783{
3784 const char *gdbstub_dev = NULL;
3785 uint32_t boot_devices_bitmap = 0;
3786 int i;
3787 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003788 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003789 const char *initrd_filename;
3790 const char *kernel_filename, *kernel_cmdline;
3791 const char *boot_devices = "";
3792 DisplayState *ds;
3793 DisplayChangeListener *dcl;
3794 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003795 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003796 const char *net_clients[MAX_NET_CLIENTS];
3797 int nb_net_clients;
3798 const char *bt_opts[MAX_BT_CMDLINE];
3799 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003800 int optind;
3801 const char *r, *optarg;
3802 CharDriverState *monitor_hd = NULL;
3803 const char *monitor_device;
3804 const char *serial_devices[MAX_SERIAL_PORTS];
3805 int serial_device_index;
3806 const char *parallel_devices[MAX_PARALLEL_PORTS];
3807 int parallel_device_index;
3808 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3809 int virtio_console_index;
3810 const char *loadvm = NULL;
3811 QEMUMachine *machine;
3812 const char *cpu_model;
3813 const char *usb_devices[MAX_USB_CMDLINE];
3814 int usb_devices_index;
3815#ifndef _WIN32
3816 int fds[2];
3817#endif
3818 int tb_size;
3819 const char *pid_file = NULL;
3820 const char *incoming = NULL;
3821#ifndef _WIN32
3822 int fd = 0;
3823 struct passwd *pwd = NULL;
3824 const char *chroot_dir = NULL;
3825 const char *run_as = NULL;
3826#endif
3827 CPUState *env;
3828 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003829 IniFile* hw_ini = NULL;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003830 /* Container for the kernel initialization parameters collected in this
3831 * routine. */
3832 char kernel_cmdline_append[1024];
3833 /* Combines kernel initialization parameters passed from the UI with
3834 * the parameters collected in this routine. */
3835 char kernel_cmdline_full[1024];
3836 char tmp_str[1024];
3837 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003838
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003839 /* Initialize sockets before anything else, so we can properly report
3840 * initialization failures back to the UI. */
3841#ifdef _WIN32
3842 socket_init();
3843#endif
3844
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003845 init_clocks();
3846
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003847 qemu_cache_utils_init(envp);
3848
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003849 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003850#ifndef _WIN32
3851 {
3852 struct sigaction act;
3853 sigfillset(&act.sa_mask);
3854 act.sa_flags = 0;
3855 act.sa_handler = SIG_IGN;
3856 sigaction(SIGPIPE, &act, NULL);
3857 }
3858#else
3859 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3860 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3861 QEMU to run on a single CPU */
3862 {
3863 HANDLE h;
3864 DWORD mask, smask;
3865 int i;
3866 h = GetCurrentProcess();
3867 if (GetProcessAffinityMask(h, &mask, &smask)) {
3868 for(i = 0; i < 32; i++) {
3869 if (mask & (1 << i))
3870 break;
3871 }
3872 if (i != 32) {
3873 mask = 1 << i;
3874 SetProcessAffinityMask(h, mask);
3875 }
3876 }
3877 }
3878#endif
3879
3880 module_call_init(MODULE_INIT_MACHINE);
3881 machine = find_default_machine();
3882 cpu_model = NULL;
3883 initrd_filename = NULL;
3884 ram_size = 0;
3885 snapshot = 0;
3886 kernel_filename = NULL;
3887 kernel_cmdline = "";
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003888 kernel_cmdline_append[0] = '\0';
3889 kernel_cmdline_full[0] = '\0';
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003890 cyls = heads = secs = 0;
3891 translation = BIOS_ATA_TRANSLATION_AUTO;
3892 monitor_device = "vc:80Cx24C";
3893
3894 serial_devices[0] = "vc:80Cx24C";
3895 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3896 serial_devices[i] = NULL;
3897 serial_device_index = 0;
3898
3899 parallel_devices[0] = "vc:80Cx24C";
3900 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3901 parallel_devices[i] = NULL;
3902 parallel_device_index = 0;
3903
3904 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3905 virtio_consoles[i] = NULL;
3906 virtio_console_index = 0;
3907
3908 for (i = 0; i < MAX_NODES; i++) {
3909 node_mem[i] = 0;
3910 node_cpumask[i] = 0;
3911 }
3912
3913 usb_devices_index = 0;
3914
3915 nb_net_clients = 0;
3916 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003917#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003918 nb_drives = 0;
3919 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003920#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003921 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003922
3923 nb_nics = 0;
3924
3925 tb_size = 0;
3926 autostart= 1;
3927
3928 register_watchdogs();
3929
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003930 /* Initialize boot properties. */
3931 boot_property_init_service();
3932
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003933 optind = 1;
3934 for(;;) {
3935 if (optind >= argc)
3936 break;
3937 r = argv[optind];
3938 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003939 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003940 } else {
3941 const QEMUOption *popt;
3942
3943 optind++;
3944 /* Treat --foo the same as -foo. */
3945 if (r[1] == '-')
3946 r++;
3947 popt = qemu_options;
3948 for(;;) {
3949 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003950 PANIC("%s: invalid option -- '%s'",
3951 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003952 }
3953 if (!strcmp(popt->name, r + 1))
3954 break;
3955 popt++;
3956 }
3957 if (popt->flags & HAS_ARG) {
3958 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003959 PANIC("%s: option '%s' requires an argument",
3960 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003961 }
3962 optarg = argv[optind++];
3963 } else {
3964 optarg = NULL;
3965 }
3966
3967 switch(popt->index) {
3968 case QEMU_OPTION_M:
3969 machine = find_machine(optarg);
3970 if (!machine) {
3971 QEMUMachine *m;
3972 printf("Supported machines are:\n");
3973 for(m = first_machine; m != NULL; m = m->next) {
3974 printf("%-10s %s%s\n",
3975 m->name, m->desc,
3976 m->is_default ? " (default)" : "");
3977 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003978 if (*optarg != '?') {
3979 PANIC("Invalid machine parameter: %s",
3980 optarg);
3981 } else {
3982 QEMU_EXIT(0);
3983 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003984 }
3985 break;
3986 case QEMU_OPTION_cpu:
3987 /* hw initialization will check this */
3988 if (*optarg == '?') {
3989/* XXX: implement xxx_cpu_list for targets that still miss it */
3990#if defined(cpu_list)
3991 cpu_list(stdout, &fprintf);
3992#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003993 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003994 } else {
3995 cpu_model = optarg;
3996 }
3997 break;
3998 case QEMU_OPTION_initrd:
3999 initrd_filename = optarg;
4000 break;
4001 case QEMU_OPTION_hda:
4002 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004003 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004004 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004005 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004006 ",cyls=%d,heads=%d,secs=%d%s",
4007 0, cyls, heads, secs,
4008 translation == BIOS_ATA_TRANSLATION_LBA ?
4009 ",trans=lba" :
4010 translation == BIOS_ATA_TRANSLATION_NONE ?
4011 ",trans=none" : "");
4012 break;
4013 case QEMU_OPTION_hdb:
4014 case QEMU_OPTION_hdc:
4015 case QEMU_OPTION_hdd:
4016 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4017 break;
4018 case QEMU_OPTION_drive:
4019 drive_add(NULL, "%s", optarg);
4020 break;
4021 case QEMU_OPTION_mtdblock:
4022 drive_add(optarg, MTD_ALIAS);
4023 break;
4024 case QEMU_OPTION_sd:
4025 drive_add(optarg, SD_ALIAS);
4026 break;
4027 case QEMU_OPTION_pflash:
4028 drive_add(optarg, PFLASH_ALIAS);
4029 break;
4030 case QEMU_OPTION_snapshot:
4031 snapshot = 1;
4032 break;
4033 case QEMU_OPTION_hdachs:
4034 {
4035 const char *p;
4036 p = optarg;
4037 cyls = strtol(p, (char **)&p, 0);
4038 if (cyls < 1 || cyls > 16383)
4039 goto chs_fail;
4040 if (*p != ',')
4041 goto chs_fail;
4042 p++;
4043 heads = strtol(p, (char **)&p, 0);
4044 if (heads < 1 || heads > 16)
4045 goto chs_fail;
4046 if (*p != ',')
4047 goto chs_fail;
4048 p++;
4049 secs = strtol(p, (char **)&p, 0);
4050 if (secs < 1 || secs > 63)
4051 goto chs_fail;
4052 if (*p == ',') {
4053 p++;
4054 if (!strcmp(p, "none"))
4055 translation = BIOS_ATA_TRANSLATION_NONE;
4056 else if (!strcmp(p, "lba"))
4057 translation = BIOS_ATA_TRANSLATION_LBA;
4058 else if (!strcmp(p, "auto"))
4059 translation = BIOS_ATA_TRANSLATION_AUTO;
4060 else
4061 goto chs_fail;
4062 } else if (*p != '\0') {
4063 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004064 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004065 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004066 if (hda_opts != NULL) {
4067 char num[16];
4068 snprintf(num, sizeof(num), "%d", cyls);
4069 qemu_opt_set(hda_opts, "cyls", num);
4070 snprintf(num, sizeof(num), "%d", heads);
4071 qemu_opt_set(hda_opts, "heads", num);
4072 snprintf(num, sizeof(num), "%d", secs);
4073 qemu_opt_set(hda_opts, "secs", num);
4074 if (translation == BIOS_ATA_TRANSLATION_LBA)
4075 qemu_opt_set(hda_opts, "trans", "lba");
4076 if (translation == BIOS_ATA_TRANSLATION_NONE)
4077 qemu_opt_set(hda_opts, "trans", "none");
4078 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004079 }
4080 break;
4081 case QEMU_OPTION_numa:
4082 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004083 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004084 }
4085 numa_add(optarg);
4086 break;
4087 case QEMU_OPTION_nographic:
4088 display_type = DT_NOGRAPHIC;
4089 break;
4090#ifdef CONFIG_CURSES
4091 case QEMU_OPTION_curses:
4092 display_type = DT_CURSES;
4093 break;
4094#endif
4095 case QEMU_OPTION_portrait:
4096 graphic_rotate = 1;
4097 break;
4098 case QEMU_OPTION_kernel:
4099 kernel_filename = optarg;
4100 break;
4101 case QEMU_OPTION_append:
4102 kernel_cmdline = optarg;
4103 break;
4104 case QEMU_OPTION_cdrom:
4105 drive_add(optarg, CDROM_ALIAS);
4106 break;
4107 case QEMU_OPTION_boot:
4108 boot_devices = optarg;
4109 /* We just do some generic consistency checks */
4110 {
4111 /* Could easily be extended to 64 devices if needed */
4112 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004113
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004114 boot_devices_bitmap = 0;
4115 for (p = boot_devices; *p != '\0'; p++) {
4116 /* Allowed boot devices are:
4117 * a b : floppy disk drives
4118 * c ... f : IDE disk drives
4119 * g ... m : machine implementation dependant drives
4120 * n ... p : network devices
4121 * It's up to each machine implementation to check
4122 * if the given boot devices match the actual hardware
4123 * implementation and firmware features.
4124 */
4125 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004126 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004127 }
4128 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004129 PANIC(
4130 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004131 }
4132 boot_devices_bitmap |= 1 << (*p - 'a');
4133 }
4134 }
4135 break;
4136 case QEMU_OPTION_fda:
4137 case QEMU_OPTION_fdb:
4138 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4139 break;
4140#ifdef TARGET_I386
4141 case QEMU_OPTION_no_fd_bootchk:
4142 fd_bootchk = 0;
4143 break;
4144#endif
4145 case QEMU_OPTION_net:
4146 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004147 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004148 }
4149 net_clients[nb_net_clients] = optarg;
4150 nb_net_clients++;
4151 break;
4152#ifdef CONFIG_SLIRP
4153 case QEMU_OPTION_tftp:
4154 tftp_prefix = optarg;
4155 break;
4156 case QEMU_OPTION_bootp:
4157 bootp_filename = optarg;
4158 break;
4159#if 0 /* ANDROID disabled */
4160#ifndef _WIN32
4161 case QEMU_OPTION_smb:
4162 net_slirp_smb(optarg);
4163 break;
4164#endif
4165#endif /* ANDROID */
4166 case QEMU_OPTION_redir:
4167 net_slirp_redir(NULL, optarg, NULL);
4168 break;
4169#endif
4170 case QEMU_OPTION_bt:
4171 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004172 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004173 }
4174 bt_opts[nb_bt_opts++] = optarg;
4175 break;
4176#ifdef HAS_AUDIO
4177 case QEMU_OPTION_audio_help:
4178 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004179 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004180 break;
4181 case QEMU_OPTION_soundhw:
4182 select_soundhw (optarg);
4183 break;
4184#endif
4185 case QEMU_OPTION_h:
4186 qemu_help(0);
4187 break;
4188 case QEMU_OPTION_version:
4189 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004190 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004191 break;
4192 case QEMU_OPTION_m: {
4193 uint64_t value;
4194 char *ptr;
4195
4196 value = strtoul(optarg, &ptr, 10);
4197 switch (*ptr) {
4198 case 0: case 'M': case 'm':
4199 value <<= 20;
4200 break;
4201 case 'G': case 'g':
4202 value <<= 30;
4203 break;
4204 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004205 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004206 }
4207
4208 /* On 32-bit hosts, QEMU is limited by virtual address space */
4209 if (value > (2047 << 20)
4210#ifndef CONFIG_KQEMU
4211 && HOST_LONG_BITS == 32
4212#endif
4213 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004214 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004215 }
4216 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004217 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004218 }
4219 ram_size = value;
4220 break;
4221 }
4222 case QEMU_OPTION_d:
4223 {
4224 int mask;
4225 const CPULogItem *item;
4226
4227 mask = cpu_str_to_log_mask(optarg);
4228 if (!mask) {
4229 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004230 for(item = cpu_log_items; item->mask != 0; item++) {
4231 printf("%-10s %s\n", item->name, item->help);
4232 }
4233 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004234 }
4235 cpu_set_log(mask);
4236 }
4237 break;
4238 case QEMU_OPTION_s:
4239 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4240 break;
4241 case QEMU_OPTION_gdb:
4242 gdbstub_dev = optarg;
4243 break;
4244 case QEMU_OPTION_L:
4245 data_dir = optarg;
4246 break;
4247 case QEMU_OPTION_bios:
4248 bios_name = optarg;
4249 break;
4250 case QEMU_OPTION_singlestep:
4251 singlestep = 1;
4252 break;
4253 case QEMU_OPTION_S:
4254#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004255 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004256#endif
4257 autostart = 0;
4258 break;
4259#ifndef _WIN32
4260 case QEMU_OPTION_k:
4261 keyboard_layout = optarg;
4262 break;
4263#endif
4264 case QEMU_OPTION_localtime:
4265 rtc_utc = 0;
4266 break;
4267 case QEMU_OPTION_vga:
4268 select_vgahw (optarg);
4269 break;
4270#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4271 case QEMU_OPTION_g:
4272 {
4273 const char *p;
4274 int w, h, depth;
4275 p = optarg;
4276 w = strtol(p, (char **)&p, 10);
4277 if (w <= 0) {
4278 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004279 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004280 }
4281 if (*p != 'x')
4282 goto graphic_error;
4283 p++;
4284 h = strtol(p, (char **)&p, 10);
4285 if (h <= 0)
4286 goto graphic_error;
4287 if (*p == 'x') {
4288 p++;
4289 depth = strtol(p, (char **)&p, 10);
4290 if (depth != 8 && depth != 15 && depth != 16 &&
4291 depth != 24 && depth != 32)
4292 goto graphic_error;
4293 } else if (*p == '\0') {
4294 depth = graphic_depth;
4295 } else {
4296 goto graphic_error;
4297 }
4298
4299 graphic_width = w;
4300 graphic_height = h;
4301 graphic_depth = depth;
4302 }
4303 break;
4304#endif
4305 case QEMU_OPTION_echr:
4306 {
4307 char *r;
4308 term_escape_char = strtol(optarg, &r, 0);
4309 if (r == optarg)
4310 printf("Bad argument to echr\n");
4311 break;
4312 }
4313 case QEMU_OPTION_monitor:
4314 monitor_device = optarg;
4315 break;
4316 case QEMU_OPTION_serial:
4317 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004318 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004319 }
4320 serial_devices[serial_device_index] = optarg;
4321 serial_device_index++;
4322 break;
4323 case QEMU_OPTION_watchdog:
4324 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004325 if (i > 0) {
4326 if (i == 1) {
4327 PANIC("Invalid watchdog parameter: %s",
4328 optarg);
4329 } else {
4330 QEMU_EXIT(0);
4331 }
4332 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004333 break;
4334 case QEMU_OPTION_watchdog_action:
4335 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004336 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004337 }
4338 break;
4339 case QEMU_OPTION_virtiocon:
4340 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004341 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004342 }
4343 virtio_consoles[virtio_console_index] = optarg;
4344 virtio_console_index++;
4345 break;
4346 case QEMU_OPTION_parallel:
4347 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004348 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004349 }
4350 parallel_devices[parallel_device_index] = optarg;
4351 parallel_device_index++;
4352 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004353 case QEMU_OPTION_loadvm:
4354 loadvm = optarg;
4355 break;
4356#if CONFIG_ANDROID_SNAPSHOTS
4357 case QEMU_OPTION_savevm_on_exit:
4358 savevm_on_exit = optarg;
4359 break;
4360#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004361 case QEMU_OPTION_full_screen:
4362 full_screen = 1;
4363 break;
4364#ifdef CONFIG_SDL
4365 case QEMU_OPTION_no_frame:
4366 no_frame = 1;
4367 break;
4368 case QEMU_OPTION_alt_grab:
4369 alt_grab = 1;
4370 break;
4371 case QEMU_OPTION_no_quit:
4372 no_quit = 1;
4373 break;
4374 case QEMU_OPTION_sdl:
4375 display_type = DT_SDL;
4376 break;
4377#endif
4378 case QEMU_OPTION_pidfile:
4379 pid_file = optarg;
4380 break;
4381#ifdef TARGET_I386
4382 case QEMU_OPTION_win2k_hack:
4383 win2k_install_hack = 1;
4384 break;
4385 case QEMU_OPTION_rtc_td_hack:
4386 rtc_td_hack = 1;
4387 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004388#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004389 case QEMU_OPTION_acpitable:
4390 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004391 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004392 }
4393 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004394#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004395 case QEMU_OPTION_smbios:
4396 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004397 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004398 }
4399 break;
4400#endif
4401#ifdef CONFIG_KQEMU
4402 case QEMU_OPTION_no_kqemu:
4403 kqemu_allowed = 0;
4404 break;
4405 case QEMU_OPTION_kernel_kqemu:
4406 kqemu_allowed = 2;
4407 break;
4408#endif
4409#ifdef CONFIG_KVM
4410 case QEMU_OPTION_enable_kvm:
4411 kvm_allowed = 1;
4412#ifdef CONFIG_KQEMU
4413 kqemu_allowed = 0;
4414#endif
4415 break;
4416#endif
4417 case QEMU_OPTION_usb:
4418 usb_enabled = 1;
4419 break;
4420 case QEMU_OPTION_usbdevice:
4421 usb_enabled = 1;
4422 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004423 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004424 }
4425 usb_devices[usb_devices_index] = optarg;
4426 usb_devices_index++;
4427 break;
4428 case QEMU_OPTION_smp:
4429 smp_cpus = atoi(optarg);
4430 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004431 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004432 }
4433 break;
4434 case QEMU_OPTION_vnc:
4435 display_type = DT_VNC;
4436 vnc_display = optarg;
4437 break;
4438#ifdef TARGET_I386
4439 case QEMU_OPTION_no_acpi:
4440 acpi_enabled = 0;
4441 break;
4442 case QEMU_OPTION_no_hpet:
4443 no_hpet = 1;
4444 break;
4445 case QEMU_OPTION_no_virtio_balloon:
4446 no_virtio_balloon = 1;
4447 break;
4448#endif
4449 case QEMU_OPTION_no_reboot:
4450 no_reboot = 1;
4451 break;
4452 case QEMU_OPTION_no_shutdown:
4453 no_shutdown = 1;
4454 break;
4455 case QEMU_OPTION_show_cursor:
4456 cursor_hide = 0;
4457 break;
4458 case QEMU_OPTION_uuid:
4459 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004460 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004461 }
4462 break;
4463#ifndef _WIN32
4464 case QEMU_OPTION_daemonize:
4465 daemonize = 1;
4466 break;
4467#endif
4468 case QEMU_OPTION_option_rom:
4469 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004470 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004471 }
4472 option_rom[nb_option_roms] = optarg;
4473 nb_option_roms++;
4474 break;
4475#if defined(TARGET_ARM) || defined(TARGET_M68K)
4476 case QEMU_OPTION_semihosting:
4477 semihosting_enabled = 1;
4478 break;
4479#endif
4480 case QEMU_OPTION_name:
4481 qemu_name = optarg;
4482 break;
4483#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4484 case QEMU_OPTION_prom_env:
4485 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004486 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004487 }
4488 prom_envs[nb_prom_envs] = optarg;
4489 nb_prom_envs++;
4490 break;
4491#endif
4492#ifdef TARGET_ARM
4493 case QEMU_OPTION_old_param:
4494 old_param = 1;
4495 break;
4496#endif
4497 case QEMU_OPTION_clock:
4498 configure_alarms(optarg);
4499 break;
4500 case QEMU_OPTION_startdate:
4501 {
4502 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004503 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004504 if (!strcmp(optarg, "now")) {
4505 rtc_date_offset = -1;
4506 } else {
4507 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4508 &tm.tm_year,
4509 &tm.tm_mon,
4510 &tm.tm_mday,
4511 &tm.tm_hour,
4512 &tm.tm_min,
4513 &tm.tm_sec) == 6) {
4514 /* OK */
4515 } else if (sscanf(optarg, "%d-%d-%d",
4516 &tm.tm_year,
4517 &tm.tm_mon,
4518 &tm.tm_mday) == 3) {
4519 tm.tm_hour = 0;
4520 tm.tm_min = 0;
4521 tm.tm_sec = 0;
4522 } else {
4523 goto date_fail;
4524 }
4525 tm.tm_year -= 1900;
4526 tm.tm_mon--;
4527 rtc_start_date = mktimegm(&tm);
4528 if (rtc_start_date == -1) {
4529 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004530 PANIC("Invalid date format. Valid format are:\n"
4531 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004532 }
4533 rtc_date_offset = time(NULL) - rtc_start_date;
4534 }
4535 }
4536 break;
rich cannings7339b552011-02-16 13:43:44 -08004537
4538 /* -------------------------------------------------------*/
4539 /* User mode network stack restrictions */
4540 case QEMU_OPTION_drop_udp:
4541 slirp_drop_udp();
4542 break;
4543 case QEMU_OPTION_drop_tcp:
4544 slirp_drop_tcp();
4545 break;
4546 case QEMU_OPTION_allow_tcp:
4547 slirp_allow(optarg, IPPROTO_TCP);
4548 break;
4549 case QEMU_OPTION_allow_udp:
4550 slirp_allow(optarg, IPPROTO_UDP);
4551 break;
4552 case QEMU_OPTION_drop_log:
4553 {
4554 FILE* drop_log_fd;
4555 drop_log_fd = fopen(optarg, "w");
4556
4557 if (!drop_log_fd) {
4558 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4559 exit(1);
4560 }
4561
4562 slirp_drop_log_fd(drop_log_fd);
4563 }
4564 break;
4565
4566 case QEMU_OPTION_dns_log:
4567 {
4568 FILE* dns_log_fd;
4569 dns_log_fd = fopen(optarg, "wb");
4570
4571 if (dns_log_fd == NULL) {
4572 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4573 exit(1);
4574 }
4575
4576 slirp_dns_log_fd(dns_log_fd);
4577 }
4578 break;
4579
4580
4581 case QEMU_OPTION_max_dns_conns:
4582 {
4583 int max_dns_conns = 0;
4584 if (parse_int(optarg, &max_dns_conns)) {
4585 fprintf(stderr,
4586 "qemu: syntax: -max-dns-conns max_connections\n");
4587 exit(1);
4588 }
4589 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4590 fprintf(stderr,
4591 "Invalid arg for max dns connections: %s\n",
4592 optarg);
4593 exit(1);
4594 }
4595 slirp_set_max_dns_conns(max_dns_conns);
4596 }
4597 break;
4598
4599 case QEMU_OPTION_net_forward:
4600 net_slirp_forward(optarg);
4601 break;
4602 case QEMU_OPTION_net_forward_tcp2sink:
4603 {
4604 SockAddress saddr;
4605
4606 if (parse_host_port(&saddr, optarg)) {
4607 fprintf(stderr,
4608 "Invalid ip/port %s for "
4609 "-forward-dropped-tcp2sink. "
4610 "We expect 'sink_ip:sink_port'\n",
4611 optarg);
4612 exit(1);
4613 }
4614 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4615 saddr.u.inet.port);
4616 }
4617 break;
4618 /* -------------------------------------------------------*/
4619
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004620 case QEMU_OPTION_tb_size:
4621 tb_size = strtol(optarg, NULL, 0);
4622 if (tb_size < 0)
4623 tb_size = 0;
4624 break;
4625 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004626 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004627 break;
4628 case QEMU_OPTION_incoming:
4629 incoming = optarg;
4630 break;
4631#ifndef _WIN32
4632 case QEMU_OPTION_chroot:
4633 chroot_dir = optarg;
4634 break;
4635 case QEMU_OPTION_runas:
4636 run_as = optarg;
4637 break;
4638#endif
4639#ifdef CONFIG_XEN
4640 case QEMU_OPTION_xen_domid:
4641 xen_domid = atoi(optarg);
4642 break;
4643 case QEMU_OPTION_xen_create:
4644 xen_mode = XEN_CREATE;
4645 break;
4646 case QEMU_OPTION_xen_attach:
4647 xen_mode = XEN_ATTACH;
4648 break;
4649#endif
4650
4651
4652 case QEMU_OPTION_mic:
4653 audio_input_source = (char*)optarg;
4654 break;
4655#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004656 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004657 trace_filename = optarg;
4658 tracing = 1;
4659 break;
4660#if 0
4661 case QEMU_OPTION_trace_miss:
4662 trace_cache_miss = 1;
4663 break;
4664 case QEMU_OPTION_trace_addr:
4665 trace_all_addr = 1;
4666 break;
4667#endif
4668 case QEMU_OPTION_tracing:
4669 if (strcmp(optarg, "off") == 0)
4670 tracing = 0;
4671 else if (strcmp(optarg, "on") == 0 && trace_filename)
4672 tracing = 1;
4673 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004674 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004675 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004676 }
4677 break;
4678#if 0
4679 case QEMU_OPTION_dcache_load_miss:
4680 dcache_load_miss_penalty = atoi(optarg);
4681 break;
4682 case QEMU_OPTION_dcache_store_miss:
4683 dcache_store_miss_penalty = atoi(optarg);
4684 break;
4685#endif
4686#endif
4687#ifdef CONFIG_NAND
4688 case QEMU_OPTION_nand:
4689 nand_add_dev(optarg);
4690 break;
4691#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004692 case QEMU_OPTION_android_ports:
4693 android_op_ports = (char*)optarg;
4694 break;
4695
4696 case QEMU_OPTION_android_port:
4697 android_op_port = (char*)optarg;
4698 break;
4699
4700 case QEMU_OPTION_android_report_console:
4701 android_op_report_console = (char*)optarg;
4702 break;
4703
4704 case QEMU_OPTION_http_proxy:
4705 op_http_proxy = (char*)optarg;
4706 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004707
4708 case QEMU_OPTION_charmap:
4709 op_charmap_file = (char*)optarg;
4710 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004711
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004712 case QEMU_OPTION_android_hw:
4713 android_op_hwini = (char*)optarg;
4714 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004715
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004716 case QEMU_OPTION_dns_server:
4717 android_op_dns_server = (char*)optarg;
4718 break;
4719
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004720 case QEMU_OPTION_radio:
4721 android_op_radio = (char*)optarg;
4722 break;
4723
4724 case QEMU_OPTION_gps:
4725 android_op_gps = (char*)optarg;
4726 break;
4727
4728 case QEMU_OPTION_audio:
4729 android_op_audio = (char*)optarg;
4730 break;
4731
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004732 case QEMU_OPTION_cpu_delay:
4733 android_op_cpu_delay = (char*)optarg;
4734 break;
4735
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004736 case QEMU_OPTION_show_kernel:
4737 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4738 break;
4739
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004740#ifdef CONFIG_NAND_LIMITS
4741 case QEMU_OPTION_nand_limits:
4742 android_op_nand_limits = (char*)optarg;
4743 break;
4744#endif // CONFIG_NAND_LIMITS
4745
4746 case QEMU_OPTION_netspeed:
4747 android_op_netspeed = (char*)optarg;
4748 break;
4749
4750 case QEMU_OPTION_netdelay:
4751 android_op_netdelay = (char*)optarg;
4752 break;
4753
4754 case QEMU_OPTION_netfast:
4755 android_op_netfast = 1;
4756 break;
4757
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004758 case QEMU_OPTION_tcpdump:
4759 android_op_tcpdump = (char*)optarg;
4760 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004761
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004762 case QEMU_OPTION_boot_property:
4763 boot_property_parse_option((char*)optarg);
4764 break;
4765
4766 case QEMU_OPTION_lcd_density:
4767 android_op_lcd_density = (char*)optarg;
4768 break;
4769
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004770 case QEMU_OPTION_ui_port:
4771 android_op_ui_port = (char*)optarg;
4772 break;
4773
4774 case QEMU_OPTION_ui_settings:
4775 android_op_ui_settings = (char*)optarg;
4776 break;
4777
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004778 case QEMU_OPTION_audio_test_out:
4779 android_audio_test_start_out();
4780 break;
4781
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004782 case QEMU_OPTION_android_avdname:
4783 android_op_avd_name = (char*)optarg;
4784 break;
4785
4786 case QEMU_OPTION_timezone:
4787 if (timezone_set((char*)optarg)) {
4788 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4789 (char*)optarg);
4790 }
4791 break;
4792
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004793#ifdef CONFIG_MEMCHECK
4794 case QEMU_OPTION_android_memcheck:
4795 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004796 snprintf(tmp_str, sizeof(tmp_str), "memcheck=%s",
4797 android_op_memcheck);
4798 tmp_str[sizeof(tmp_str) - 1] = '\0';
4799 /* This will set ro.kernel.memcheck system property
4800 * to memcheck's tracing flags. */
4801 append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004802 break;
4803#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004804
4805 case QEMU_OPTION_snapshot_no_time_update:
4806 android_snapshot_update_time = 0;
4807 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004808 }
4809 }
4810 }
4811
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004812 /* Initialize character map. */
4813 if (android_charmap_setup(op_charmap_file)) {
4814 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004815 PANIC(
4816 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004817 op_charmap_file);
4818 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004819 PANIC(
4820 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004821 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004822 }
4823
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004824 /* If no data_dir is specified then try to find it relative to the
4825 executable path. */
4826 if (!data_dir) {
4827 data_dir = find_datadir(argv[0]);
4828 }
4829 /* If all else fails use the install patch specified when building. */
4830 if (!data_dir) {
4831 data_dir = CONFIG_QEMU_SHAREDIR;
4832 }
4833
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004834 if (!android_op_hwini) {
4835 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004836 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004837 hw_ini = iniFile_newFromFile(android_op_hwini);
4838 if (hw_ini == NULL) {
4839 PANIC("Could not find %s file.", android_op_hwini);
4840 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004841 androidHwConfig_read(android_hw, hw_ini);
4842 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004843
4844 {
4845 int width = android_hw->hw_lcd_width;
4846 int height = android_hw->hw_lcd_height;
4847 int depth = android_hw->hw_lcd_depth;
4848
4849 /* A bit of sanity checking */
4850 if (width <= 0 || height <= 0 ||
4851 (depth != 16 && depth != 32) ||
4852 (((width|height) & 3) != 0) )
4853 {
4854 PANIC("Invalid display configuration (%d,%d,%d)",
4855 width, height, depth);
4856 }
4857 android_display_width = width;
4858 android_display_height = height;
4859 android_display_bpp = depth;
4860 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004861
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004862#ifdef CONFIG_NAND_LIMITS
4863 /* Init nand stuff. */
4864 if (android_op_nand_limits) {
4865 parse_nand_limits(android_op_nand_limits);
4866 }
4867#endif // CONFIG_NAND_LIMITS
4868
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004869 /* Set the VM's max heap size, passed as a boot property */
4870 if (android_hw->vm_heapSize > 0) {
4871 char tmp[64];
4872 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
4873 boot_property_add("dalvik.vm.heapsize",tmp);
4874 }
4875
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004876 /* Initialize net speed and delays stuff. */
4877 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004878 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004879 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004880 }
4881
4882 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004883 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004884 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004885 }
4886
4887 if (android_op_netfast) {
4888 qemu_net_download_speed = 0;
4889 qemu_net_upload_speed = 0;
4890 qemu_net_min_latency = 0;
4891 qemu_net_max_latency = 0;
4892 }
4893
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004894 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01004895 if (android_hw->hw_lcd_density) {
4896 long density = android_hw->hw_lcd_density;
4897 if (density <= 0) {
4898 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004899 }
4900 hwLcd_setBootProperty(density);
4901 }
4902
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004903 /* Initialize TCP dump */
4904 if (android_op_tcpdump) {
4905 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
4906 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
4907 }
4908 }
4909
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004910 /* Initialize modem */
4911 if (android_op_radio) {
4912 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
4913 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004914 PANIC("unsupported character device specification: %s\n"
4915 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004916 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004917 }
4918 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
4919 } else if (android_hw->hw_gsmModem != 0 ) {
4920 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004921 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004922 }
4923 }
4924
4925 /* Initialize GPS */
4926 if (android_op_gps) {
4927 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
4928 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004929 PANIC("unsupported character device specification: %s\n"
4930 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004931 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004932 }
4933 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
4934 } else if (android_hw->hw_gps != 0) {
4935 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004936 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004937 }
4938 }
4939
4940 /* Initialize audio. */
4941 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004942 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004943 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004944 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004945 }
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004946 }
4947
4948 if (android_op_cpu_delay) {
4949 char* end;
4950 long delay = strtol(android_op_cpu_delay, &end, 0);
4951 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004952 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004953 }
4954 if (delay > 0)
4955 delay = (1000-delay);
4956
4957 qemu_cpu_delay = (int) delay;
4958 }
4959
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004960 if (android_op_dns_server) {
4961 char* x = strchr(android_op_dns_server, ',');
4962 dns_count = 0;
4963 if (x == NULL)
4964 {
4965 if ( add_dns_server( android_op_dns_server ) == 0 )
4966 dns_count = 1;
4967 }
4968 else
4969 {
4970 x = android_op_dns_server;
4971 while (*x) {
4972 char* y = strchr(x, ',');
4973
4974 if (y != NULL) {
4975 *y = 0;
4976 y++;
4977 } else {
4978 y = x + strlen(x);
4979 }
4980
4981 if (y > x && add_dns_server( x ) == 0) {
4982 dns_count += 1;
4983 }
4984 x = y;
4985 }
4986 }
4987 if (dns_count == 0)
4988 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
4989 }
4990
4991 if (dns_count == 0)
4992 dns_count = slirp_get_system_dns_servers();
4993 if (dns_count) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004994 snprintf(tmp_str, sizeof(tmp_str), "ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004995 append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
4996 }
4997
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004998#ifdef CONFIG_MEMCHECK
4999 if (android_op_memcheck) {
5000 memcheck_init(android_op_memcheck);
5001 }
5002#endif // CONFIG_MEMCHECK
5003
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005004#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5005 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005006 PANIC(
5007 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005008 }
5009#endif
5010
5011 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5012 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005013 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5014 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005015 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005016 }
5017
5018 if (display_type == DT_NOGRAPHIC) {
5019 if (serial_device_index == 0)
5020 serial_devices[0] = "stdio";
5021 if (parallel_device_index == 0)
5022 parallel_devices[0] = "null";
5023 if (strncmp(monitor_device, "vc", 2) == 0)
5024 monitor_device = "stdio";
5025 }
5026
5027#ifndef _WIN32
5028 if (daemonize) {
5029 pid_t pid;
5030
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005031 if (pipe(fds) == -1) {
5032 PANIC("Unable to aquire pidfile");
5033 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005034
5035 pid = fork();
5036 if (pid > 0) {
5037 uint8_t status;
5038 ssize_t len;
5039
5040 close(fds[1]);
5041
5042 again:
5043 len = read(fds[0], &status, 1);
5044 if (len == -1 && (errno == EINTR))
5045 goto again;
5046
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005047 if (len != 1) {
5048 PANIC("Error when aquiring pidfile");
5049 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005050 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005051 PANIC("Could not acquire pidfile");
5052 } else {
5053 QEMU_EXIT(0);
5054 }
5055 } else if (pid < 0) {
5056 PANIC("Unable to daemonize");
5057 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005058
5059 setsid();
5060
5061 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005062 if (pid > 0) {
5063 QEMU_EXIT(0);
5064 } else if (pid < 0) {
5065 PANIC("Could not acquire pid file");
5066 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005067
5068 umask(027);
5069
5070 signal(SIGTSTP, SIG_IGN);
5071 signal(SIGTTOU, SIG_IGN);
5072 signal(SIGTTIN, SIG_IGN);
5073 }
5074
5075 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5076 if (daemonize) {
5077 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005078 int ret;
5079 do {
5080 ret = write(fds[1], &status, 1);
5081 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005082 PANIC("Could not acquire pid file");
5083 } else {
5084 PANIC("Could not acquire pid file");
5085 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005086 }
5087#endif
5088
5089#ifdef CONFIG_KQEMU
5090 if (smp_cpus > 1)
5091 kqemu_allowed = 0;
5092#endif
5093 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005094 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005095 }
5096 linux_boot = (kernel_filename != NULL);
5097 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5098
5099 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005100 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005101 }
5102
5103 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005104 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005105 }
5106
5107 /* boot to floppy or the default cd if no hard disk defined yet */
5108 if (!boot_devices[0]) {
5109 boot_devices = "cad";
5110 }
5111 setvbuf(stdout, NULL, _IOLBF, 0);
5112
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005113 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005114 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005115 }
David Turner6a9ef172010-09-09 22:54:36 +02005116 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005117
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005118 /* init network clients */
5119 if (nb_net_clients == 0) {
5120 /* if no clients, we use a default config */
5121 net_clients[nb_net_clients++] = "nic";
5122#ifdef CONFIG_SLIRP
5123 net_clients[nb_net_clients++] = "user";
5124#endif
5125 }
5126
5127 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005128 if (net_client_parse(net_clients[i]) < 0) {
5129 PANIC("Unable to parse net clients");
5130 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005131 }
5132 net_client_check();
5133
5134#ifdef TARGET_I386
5135 /* XXX: this should be moved in the PC machine instantiation code */
5136 if (net_boot != 0) {
5137 int netroms = 0;
5138 for (i = 0; i < nb_nics && i < 4; i++) {
5139 const char *model = nd_table[i].model;
5140 char buf[1024];
5141 char *filename;
5142 if (net_boot & (1 << i)) {
5143 if (model == NULL)
5144 model = "ne2k_pci";
5145 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5146 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5147 if (filename && get_image_size(filename) > 0) {
5148 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005149 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005150 }
5151 option_rom[nb_option_roms] = qemu_strdup(buf);
5152 nb_option_roms++;
5153 netroms++;
5154 }
5155 if (filename) {
5156 qemu_free(filename);
5157 }
5158 }
5159 }
5160 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005161 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005162 }
5163 }
5164#endif
5165
5166 /* init the bluetooth world */
5167 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005168 if (bt_parse(bt_opts[i])) {
5169 PANIC("Unable to parse bluetooth options");
5170 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005171
5172 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005173 if (ram_size == 0) {
5174 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5175 if (ram_size == 0) {
5176 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5177 }
5178 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005179
5180#ifdef CONFIG_KQEMU
5181 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5182 guest ram allocation. It needs to go away. */
5183 if (kqemu_allowed) {
5184 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5185 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5186 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005187 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005188 }
5189 }
5190#endif
5191
5192 /* init the dynamic translator */
5193 cpu_exec_init_all(tb_size * 1024 * 1024);
5194
5195 bdrv_init();
5196
5197 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005198#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005199 if (nb_drives_opt < MAX_DRIVES)
5200 drive_add(NULL, CDROM_ALIAS);
5201
5202 /* we always create at least one floppy */
5203
5204 if (nb_drives_opt < MAX_DRIVES)
5205 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005206 /* we always create one sd slot, even if no card is in it */
5207
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005208 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005209 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005210 }
5211#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005212
5213 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005214 if (snapshot)
5215 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5216 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5217 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005218
David Turner6a9ef172010-09-09 22:54:36 +02005219 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005220 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5221
5222#ifndef _WIN32
5223 /* must be after terminal init, SDL library changes signal handlers */
5224 sighandler_setup();
5225#endif
5226
5227 /* Maintain compatibility with multiple stdio monitors */
5228 if (!strcmp(monitor_device,"stdio")) {
5229 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5230 const char *devname = serial_devices[i];
5231 if (devname && !strcmp(devname,"mon:stdio")) {
5232 monitor_device = NULL;
5233 break;
5234 } else if (devname && !strcmp(devname,"stdio")) {
5235 monitor_device = NULL;
5236 serial_devices[i] = "mon:stdio";
5237 break;
5238 }
5239 }
5240 }
5241
5242 if (nb_numa_nodes > 0) {
5243 int i;
5244
5245 if (nb_numa_nodes > smp_cpus) {
5246 nb_numa_nodes = smp_cpus;
5247 }
5248
5249 /* If no memory size if given for any node, assume the default case
5250 * and distribute the available memory equally across all nodes
5251 */
5252 for (i = 0; i < nb_numa_nodes; i++) {
5253 if (node_mem[i] != 0)
5254 break;
5255 }
5256 if (i == nb_numa_nodes) {
5257 uint64_t usedmem = 0;
5258
5259 /* On Linux, the each node's border has to be 8MB aligned,
5260 * the final node gets the rest.
5261 */
5262 for (i = 0; i < nb_numa_nodes - 1; i++) {
5263 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5264 usedmem += node_mem[i];
5265 }
5266 node_mem[i] = ram_size - usedmem;
5267 }
5268
5269 for (i = 0; i < nb_numa_nodes; i++) {
5270 if (node_cpumask[i] != 0)
5271 break;
5272 }
5273 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5274 * must cope with this anyway, because there are BIOSes out there in
5275 * real machines which also use this scheme.
5276 */
5277 if (i == nb_numa_nodes) {
5278 for (i = 0; i < smp_cpus; i++) {
5279 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5280 }
5281 }
5282 }
5283
5284 if (kvm_enabled()) {
5285 int ret;
5286
5287 ret = kvm_init(smp_cpus);
5288 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005289 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005290 }
5291 }
5292
5293 if (monitor_device) {
5294 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5295 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005296 PANIC("qemu: could not open monitor device '%s'",
5297 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005298 }
5299 }
5300
5301 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5302 const char *devname = serial_devices[i];
5303 if (devname && strcmp(devname, "none")) {
5304 char label[32];
5305 snprintf(label, sizeof(label), "serial%d", i);
5306 serial_hds[i] = qemu_chr_open(label, devname, NULL);
5307 if (!serial_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005308 PANIC("qemu: could not open serial device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005309 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005310 }
5311 }
5312 }
5313
5314 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5315 const char *devname = parallel_devices[i];
5316 if (devname && strcmp(devname, "none")) {
5317 char label[32];
5318 snprintf(label, sizeof(label), "parallel%d", i);
5319 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5320 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005321 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005322 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005323 }
5324 }
5325 }
5326
5327 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5328 const char *devname = virtio_consoles[i];
5329 if (devname && strcmp(devname, "none")) {
5330 char label[32];
5331 snprintf(label, sizeof(label), "virtcon%d", i);
5332 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5333 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005334 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005335 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005336 }
5337 }
5338 }
5339
5340 module_call_init(MODULE_INIT_DEVICE);
5341
5342
5343#ifdef CONFIG_TRACE
5344 if (trace_filename) {
5345 trace_init(trace_filename);
5346#if 0
5347 // We don't need the dcache code until we can get load and store tracing
5348 // working again.
5349 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5350 dcache_replace_policy, dcache_load_miss_penalty,
5351 dcache_store_miss_penalty);
5352#endif
5353 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5354 }
5355#endif
5356
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005357 /* Combine kernel command line passed from the UI with parameters
5358 * collected during initialization. */
5359 if (*kernel_cmdline) {
5360 if (kernel_cmdline_append[0]) {
5361 snprintf(kernel_cmdline_full, sizeof(kernel_cmdline_full), "%s %s",
5362 kernel_cmdline, kernel_cmdline_append);
5363 } else {
5364 strncpy(kernel_cmdline_full, kernel_cmdline, sizeof(kernel_cmdline_full));
5365 kernel_cmdline_full[sizeof(kernel_cmdline_full) - 1] = '\0';
5366 }
5367 } else if (kernel_cmdline_append[0]) {
5368 strncpy(kernel_cmdline_full, kernel_cmdline_append, sizeof(kernel_cmdline_full));
5369 }
5370
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005371 machine->init(ram_size, boot_devices,
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005372 kernel_filename, kernel_cmdline_full, initrd_filename, cpu_model);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005373
5374
5375 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5376 for (i = 0; i < nb_numa_nodes; i++) {
5377 if (node_cpumask[i] & (1 << env->cpu_index)) {
5378 env->numa_node = i;
5379 }
5380 }
5381 }
5382
5383 current_machine = machine;
5384
5385 /* Set KVM's vcpu state to qemu's initial CPUState. */
5386 if (kvm_enabled()) {
5387 int ret;
5388
5389 ret = kvm_sync_vcpus();
5390 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005391 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005392 }
5393 }
5394
5395 /* init USB devices */
5396 if (usb_enabled) {
5397 for(i = 0; i < usb_devices_index; i++) {
5398 if (usb_device_add(usb_devices[i], 0) < 0) {
5399 fprintf(stderr, "Warning: could not add USB device %s\n",
5400 usb_devices[i]);
5401 }
5402 }
5403 }
5404
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005405 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005406 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005407
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005408 /* Initialize display from the command line parameters. */
5409 android_display_reset(ds,
5410 android_display_width,
5411 android_display_height,
5412 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005413
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005414 if (display_type == DT_DEFAULT) {
5415#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5416 display_type = DT_SDL;
5417#else
5418 display_type = DT_VNC;
5419 vnc_display = "localhost:0,to=99";
5420 show_vnc_port = 1;
5421#endif
5422 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005423
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005424
5425 switch (display_type) {
5426 case DT_NOGRAPHIC:
5427 break;
5428#if defined(CONFIG_CURSES)
5429 case DT_CURSES:
5430 curses_display_init(ds, full_screen);
5431 break;
5432#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005433#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005434 case DT_SDL:
5435 sdl_display_init(ds, full_screen, no_frame);
5436 break;
5437#elif defined(CONFIG_COCOA)
5438 case DT_SDL:
5439 cocoa_display_init(ds, full_screen);
5440 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005441#elif defined(CONFIG_STANDALONE_CORE)
5442 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005443 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005444 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005445#endif
5446 case DT_VNC:
5447 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005448 if (vnc_display_open(ds, vnc_display) < 0) {
5449 PANIC("Unable to initialize VNC display");
5450 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005451
5452 if (show_vnc_port) {
5453 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5454 }
5455 break;
5456 default:
5457 break;
5458 }
5459 dpy_resize(ds);
5460
5461 dcl = ds->listeners;
5462 while (dcl != NULL) {
5463 if (dcl->dpy_refresh != NULL) {
5464 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5465 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5466 }
5467 dcl = dcl->next;
5468 }
5469
5470 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5471 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5472 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5473 }
5474
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005475 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005476 qemu_chr_initial_reset();
5477
5478 if (monitor_device && monitor_hd)
5479 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5480
5481 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5482 const char *devname = serial_devices[i];
5483 if (devname && strcmp(devname, "none")) {
5484 if (strstart(devname, "vc", 0))
5485 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5486 }
5487 }
5488
5489 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5490 const char *devname = parallel_devices[i];
5491 if (devname && strcmp(devname, "none")) {
5492 if (strstart(devname, "vc", 0))
5493 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5494 }
5495 }
5496
5497 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5498 const char *devname = virtio_consoles[i];
5499 if (virtcon_hds[i] && devname) {
5500 if (strstart(devname, "vc", 0))
5501 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5502 }
5503 }
5504
5505 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005506 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005507 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005508 }
5509
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005510 /* call android-specific setup function */
5511 android_emulation_setup();
5512
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005513#if !defined(CONFIG_STANDALONE_CORE)
5514 // For the standalone emulator (UI+core in one executable) we need to
5515 // set the window title here.
5516 android_emulator_set_base_port(android_base_port);
5517#endif
5518
Ot ten Thije871da2a2010-09-20 10:29:22 +01005519 if (loadvm)
5520 do_loadvm(cur_mon, loadvm);
5521
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005522 if (incoming) {
5523 autostart = 0; /* fixme how to deal with -daemonize */
5524 qemu_start_incoming_migration(incoming);
5525 }
5526
5527 if (autostart)
5528 vm_start();
5529
5530#ifndef _WIN32
5531 if (daemonize) {
5532 uint8_t status = 0;
5533 ssize_t len;
5534
5535 again1:
5536 len = write(fds[1], &status, 1);
5537 if (len == -1 && (errno == EINTR))
5538 goto again1;
5539
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005540 if (len != 1) {
5541 PANIC("Unable to daemonize");
5542 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005543
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005544 if (chdir("/")) {
5545 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005546 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005547 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005548 TFR(fd = open("/dev/null", O_RDWR));
5549 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005550 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005551 }
5552
5553 if (run_as) {
5554 pwd = getpwnam(run_as);
5555 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005556 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005557 }
5558 }
5559
5560 if (chroot_dir) {
5561 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005562 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005563 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005564 if (chdir("/")) {
5565 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005566 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005567 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005568 }
5569
5570 if (run_as) {
5571 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005572 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005573 }
5574 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005575 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005576 }
5577 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005578 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005579 }
5580 }
5581
5582 if (daemonize) {
5583 dup2(fd, 0);
5584 dup2(fd, 1);
5585 dup2(fd, 2);
5586
5587 close(fd);
5588 }
5589#endif
5590
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005591#ifdef CONFIG_ANDROID
5592 // This will notify the UI that the core is successfuly initialized
5593 android_core_init_completed();
5594#endif // CONFIG_ANDROID
5595
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005596 main_loop();
5597 quit_timers();
5598 net_cleanup();
5599 android_emulation_teardown();
5600 return 0;
5601}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005602
5603void
5604android_emulation_teardown(void)
5605{
5606 android_charmap_done();
5607}