blob: ff8ecb97cf456f0f03f7364e9c5e53760d0e2dc5 [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"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010035#include "hw/i386/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070036#include "hw/audiodev.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010037#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070038#include "hw/baum.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010039#include "hw/android/goldfish/nand.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010040#include "net/net.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010041#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010042#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010043#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +010044#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +010045#include "sysemu/char.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010046#include "sysemu/blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070047#include "audio/audio.h"
48
David 'Digit' Turner28a09b62013-12-15 00:16:00 +010049#include "migration/qemu-file.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070050#include "android/android.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010051#include "android/charpipe.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "modem_driver.h"
53#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070054#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020055#include "android/hw-pipe-net.h"
56#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070057#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080058#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070059#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070060#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070061#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010062#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010063#include "android/utils/filelock.h"
64#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010065#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010066#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080067#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080068#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010069#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020070#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070071#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010072#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010073#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070074
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070075#ifdef CONFIG_MEMCHECK
76#include "memcheck/memcheck.h"
77#endif // CONFIG_MEMCHECK
78
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070079#include <unistd.h>
80#include <fcntl.h>
81#include <signal.h>
82#include <time.h>
83#include <errno.h>
84#include <sys/time.h>
85#include <zlib.h>
86
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070087/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070088#include "config-host.h"
89
90#ifndef _WIN32
91#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070092#include <sys/times.h>
93#include <sys/wait.h>
94#include <termios.h>
95#include <sys/mman.h>
96#include <sys/ioctl.h>
97#include <sys/resource.h>
98#include <sys/socket.h>
99#include <netinet/in.h>
100#include <net/if.h>
101#if defined(__NetBSD__)
102#include <net/if_tap.h>
103#endif
104#ifdef __linux__
105#include <linux/if_tun.h>
106#endif
107#include <arpa/inet.h>
108#include <dirent.h>
109#include <netdb.h>
110#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700111#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700112#include <sys/stat.h>
113#if defined(__FreeBSD__) || defined(__DragonFly__)
114#include <libutil.h>
115#else
116#include <util.h>
117#endif
118#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
119#include <freebsd/stdlib.h>
120#else
121#ifdef __linux__
122#include <pty.h>
123#include <malloc.h>
124#include <linux/rtc.h>
125
126/* For the benefit of older linux systems which don't supply it,
127 we use a local copy of hpet.h. */
128/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100129#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700130
131#include <linux/ppdev.h>
132#include <linux/parport.h>
133#endif
134#ifdef __sun__
135#include <sys/stat.h>
136#include <sys/ethernet.h>
137#include <sys/sockio.h>
138#include <netinet/arp.h>
139#include <netinet/in.h>
140#include <netinet/in_systm.h>
141#include <netinet/ip.h>
142#include <netinet/ip_icmp.h> // must come after ip.h
143#include <netinet/udp.h>
144#include <netinet/tcp.h>
145#include <net/if.h>
146#include <syslog.h>
147#include <stropts.h>
148#endif
149#endif
150#endif
151
152#if defined(__OpenBSD__)
153#include <util.h>
154#endif
155
156#if defined(CONFIG_VDE)
157#include <libvdeplug.h>
158#endif
159
160#ifdef _WIN32
161#include <windows.h>
162#include <malloc.h>
163#include <sys/timeb.h>
164#include <mmsystem.h>
165#define getopt_long_only getopt_long
166#define memalign(align, size) malloc(size)
167#endif
168
David 'Digit' Turner11822842013-12-17 09:16:47 +0100169#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100170#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700171
172#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200173int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700174#undef main
175#define main qemu_main
176#endif /* CONFIG_COCOA */
177
178#include "hw/hw.h"
179#include "hw/boards.h"
180#include "hw/usb.h"
181#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100182#include "hw/i386/pc.h"
183#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700184#include "hw/baum.h"
185#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100186#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100187#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100188#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100189#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100190#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100191#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100192#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100193#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100194#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100195#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100196#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100197#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100198#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100199#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700200#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100201#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100202#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100203#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200204#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100205#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200206#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100207#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700208#include "android/hw-lcd.h"
209#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200210#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700211#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100212#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700213
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800214#include "android/snaphost-android.h"
215
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800216#if !defined(CONFIG_STANDALONE_CORE)
217/* in android/qemulator.c */
218extern void android_emulator_set_base_port(int port);
219#endif
220
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700221#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700222#undef main
223#define main qemu_main
224#endif
225
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100226#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700227
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700228#ifdef CONFIG_TRACE
David 'Digit' Turnerd4e803c2013-12-14 23:45:50 +0100229#include "android/trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700230#endif
231
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100232#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700233
234#if defined(CONFIG_SLIRP)
235#include "libslirp.h"
236#endif
237
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238#define DEFAULT_RAM_SIZE 128
239
240/* Max number of USB devices that can be specified on the commandline. */
241#define MAX_USB_CMDLINE 8
242
243/* Max number of bluetooth switches on the commandline. */
244#define MAX_BT_CMDLINE 10
245
246/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700247
248static const char *data_dir;
249const char *bios_name = NULL;
250static void *ioport_opaque[MAX_IOPORTS];
251static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
252static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100253#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700254/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
255 to store the VM snapshots */
256DriveInfo drives_table[MAX_DRIVES+1];
257int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100258#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260DisplayType display_type = DT_DEFAULT;
261const char* keyboard_layout = NULL;
262int64_t ticks_per_sec;
263ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200264const char *mem_path = NULL;
265#ifdef MAP_POPULATE
266int mem_prealloc = 0; /* force preallocation of physical target memory */
267#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700268int nb_nics;
269NICInfo nd_table[MAX_NICS];
270int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100271int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700272static int rtc_utc = 1;
273static int rtc_date_offset = -1; /* -1 means no change */
274int cirrus_vga_enabled = 1;
275int std_vga_enabled = 0;
276int vmsvga_enabled = 0;
277int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700278QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700279static int full_screen = 0;
280#ifdef CONFIG_SDL
281static int no_frame = 0;
282#endif
283int no_quit = 0;
284CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100285int serial_hds_count;
286
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700287CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
288CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
289#ifdef TARGET_I386
290int win2k_install_hack = 0;
291int rtc_td_hack = 0;
292#endif
293int usb_enabled = 0;
294int singlestep = 0;
295int smp_cpus = 1;
296const char *vnc_display;
297int acpi_enabled = 1;
298int no_hpet = 0;
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800299int hax_disabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700300int no_virtio_balloon = 0;
301int fd_bootchk = 1;
302int no_reboot = 0;
303int no_shutdown = 0;
304int cursor_hide = 1;
305int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700306WatchdogTimerModel *watchdog = NULL;
307int watchdog_action = WDT_RESET;
308const char *option_rom[MAX_OPTION_ROMS];
309int nb_option_roms;
310int semihosting_enabled = 0;
311#ifdef TARGET_ARM
312int old_param = 0;
313#endif
314const char *qemu_name;
315int alt_grab = 0;
316#if defined(TARGET_SPARC) || defined(TARGET_PPC)
317unsigned int nb_prom_envs = 0;
318const char *prom_envs[MAX_PROM_ENVS];
319#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100320#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700321int nb_drives_opt;
322struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100323#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700324int nb_numa_nodes;
325uint64_t node_mem[MAX_NODES];
326uint64_t node_cpumask[MAX_NODES];
327
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700328static QEMUTimer *nographic_timer;
329
330uint8_t qemu_uuid[16];
331
332
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700333int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700334extern char* audio_input_source;
335
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700336extern char* android_op_ports;
337extern char* android_op_port;
338extern char* android_op_report_console;
339extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700340// Path to the file containing specific key character map.
341char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700342
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700343/* Path to hardware initialization file passed with -android-hw option. */
344char* android_op_hwini = NULL;
345
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700346/* Memory checker options. */
347char* android_op_memcheck = NULL;
348
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700349/* -dns-server option value. */
350char* android_op_dns_server = NULL;
351
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700352/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700353char* android_op_radio = NULL;
354
355/* -gps option value. */
356char* android_op_gps = NULL;
357
358/* -audio option value. */
359char* android_op_audio = NULL;
360
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700361/* -cpu-delay option value. */
362char* android_op_cpu_delay = NULL;
363
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700364#ifdef CONFIG_NAND_LIMITS
365/* -nand-limits option value. */
366char* android_op_nand_limits = NULL;
367#endif // CONFIG_NAND_LIMITS
368
369/* -netspeed option value. */
370char* android_op_netspeed = NULL;
371
372/* -netdelay option value. */
373char* android_op_netdelay = NULL;
374
375/* -netfast option value. */
376int android_op_netfast = 0;
377
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700378/* -tcpdump option value. */
379char* android_op_tcpdump = NULL;
380
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700381/* -lcd-density option value. */
382char* android_op_lcd_density = NULL;
383
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700384/* -ui-port option value. This port will be used to report the core
385 * initialization completion.
386 */
387char* android_op_ui_port = NULL;
388
389/* -ui-settings option value. This value will be passed to the UI when new UI
390 * process is attaching to the core.
391 */
392char* android_op_ui_settings = NULL;
393
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800394/* -android-avdname option value. */
395char* android_op_avd_name = "unknown";
396
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700397extern int android_display_width;
398extern int android_display_height;
399extern int android_display_bpp;
400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700401extern void dprint( const char* format, ... );
402
rich canningsd952f282011-03-01 15:40:09 -0800403const char* dns_log_filename = NULL;
404const char* drop_log_filename = NULL;
405static int rotate_logs_requested = 0;
406
Tim Baverstock24204cc2010-11-25 11:37:43 +0000407const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000408
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700409#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
410
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700411/* Reports the core initialization failure to the error stdout and to the UI
412 * socket before exiting the application.
413 * Parameters that are passed to this macro are used to format the error
414 * mesage using sprintf routine.
415 */
416#ifdef CONFIG_ANDROID
417#define PANIC(...) android_core_init_failure(__VA_ARGS__)
418#else
419#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
420 exit(1); \
421 } while (0)
422#endif // CONFIG_ANDROID
423
424/* Exits the core during initialization. */
425#ifdef CONFIG_ANDROID
426#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
427#else
428#define QEMU_EXIT(exit_code) exit(exit_code)
429#endif // CONFIG_ANDROID
430
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700431/***********************************************************/
432/* x86 ISA bus support */
433
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100434hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700435PicState2 *isa_pic;
436
437static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
438static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
439
440static uint32_t ioport_read(int index, uint32_t address)
441{
442 static IOPortReadFunc *default_func[3] = {
443 default_ioport_readb,
444 default_ioport_readw,
445 default_ioport_readl
446 };
447 IOPortReadFunc *func = ioport_read_table[index][address];
448 if (!func)
449 func = default_func[index];
450 return func(ioport_opaque[address], address);
451}
452
453static void ioport_write(int index, uint32_t address, uint32_t data)
454{
455 static IOPortWriteFunc *default_func[3] = {
456 default_ioport_writeb,
457 default_ioport_writew,
458 default_ioport_writel
459 };
460 IOPortWriteFunc *func = ioport_write_table[index][address];
461 if (!func)
462 func = default_func[index];
463 func(ioport_opaque[address], address, data);
464}
465
466static uint32_t default_ioport_readb(void *opaque, uint32_t address)
467{
468#ifdef DEBUG_UNUSED_IOPORT
469 fprintf(stderr, "unused inb: port=0x%04x\n", address);
470#endif
471 return 0xff;
472}
473
474static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
475{
476#ifdef DEBUG_UNUSED_IOPORT
477 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
478#endif
479}
480
481/* default is to make two byte accesses */
482static uint32_t default_ioport_readw(void *opaque, uint32_t address)
483{
484 uint32_t data;
485 data = ioport_read(0, address);
486 address = (address + 1) & (MAX_IOPORTS - 1);
487 data |= ioport_read(0, address) << 8;
488 return data;
489}
490
491static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
492{
493 ioport_write(0, address, data & 0xff);
494 address = (address + 1) & (MAX_IOPORTS - 1);
495 ioport_write(0, address, (data >> 8) & 0xff);
496}
497
498static uint32_t default_ioport_readl(void *opaque, uint32_t address)
499{
500#ifdef DEBUG_UNUSED_IOPORT
501 fprintf(stderr, "unused inl: port=0x%04x\n", address);
502#endif
503 return 0xffffffff;
504}
505
506static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
507{
508#ifdef DEBUG_UNUSED_IOPORT
509 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
510#endif
511}
512
rich canningsd952f282011-03-01 15:40:09 -0800513/*
514 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
515 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
516 * between the tasks that do not require restarting Qemu.
517 */
518void rotate_qemu_logs_handler(int signum) {
519 rotate_logs_requested = 1;
520}
521
522/*
523 * Resets the rotate_log_requested_flag. Normally called after qemu
524 * logs has been rotated.
525 */
526void reset_rotate_qemu_logs_request(void) {
527 rotate_logs_requested = 0;
528}
529
530/*
531 * Clears the passed qemu log when the rotate_logs_requested
532 * is set. We need to clear the logs between the tasks that do not
533 * require restarting Qemu.
534 */
535FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
536 FILE* new_log_fd = NULL;
537 if (old_log_fd) {
538 if (fclose(old_log_fd) == -1) {
539 fprintf(stderr, "Cannot close old_log fd\n");
540 exit(errno);
541 }
542 }
543
544 if (!filename) {
545 fprintf(stderr, "The log filename to be rotated is not provided");
546 exit(-1);
547 }
548
549 new_log_fd = fopen(filename , "wb+");
550 if (new_log_fd == NULL) {
551 fprintf(stderr, "Cannot open the log file: %s for write.\n",
552 filename);
553 exit(1);
554 }
555
556 return new_log_fd;
557}
558
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700559/***************/
560/* ballooning */
561
562static QEMUBalloonEvent *qemu_balloon_event;
563void *qemu_balloon_event_opaque;
564
565void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
566{
567 qemu_balloon_event = func;
568 qemu_balloon_event_opaque = opaque;
569}
570
571void qemu_balloon(ram_addr_t target)
572{
573 if (qemu_balloon_event)
574 qemu_balloon_event(qemu_balloon_event_opaque, target);
575}
576
577ram_addr_t qemu_balloon_status(void)
578{
579 if (qemu_balloon_event)
580 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
581 return 0;
582}
583
584/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700585/* 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
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200623int tbflush_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700624static 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 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100697 vlan = g_malloc0(sizeof(struct bt_vlan_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700698 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;
Manuel Francisco Naranjo58ecd742012-07-18 11:22:09 -03001461 if (!port){
1462 USBDevice *hub = usb_hub_init(VM_USB_HUB_SIZE);
1463 port = free_usb_ports;
1464 usb_attach(port, hub);
1465 }
1466 else if (!port->next){
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001467 USBDevice *hub;
1468
1469 /* Create a new hub and chain it on. */
1470 free_usb_ports = NULL;
1471 port->next = used_usb_ports;
1472 used_usb_ports = port;
1473
1474 hub = usb_hub_init(VM_USB_HUB_SIZE);
1475 usb_attach(port, hub);
1476 port = free_usb_ports;
1477 }
1478
1479 free_usb_ports = port->next;
1480 port->next = used_usb_ports;
1481 used_usb_ports = port;
1482 usb_attach(port, dev);
1483 return 0;
1484}
1485
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001486#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001487static void usb_msd_password_cb(void *opaque, int err)
1488{
1489 USBDevice *dev = opaque;
1490
1491 if (!err)
1492 usb_device_add_dev(dev);
1493 else
1494 dev->handle_destroy(dev);
1495}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001496#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001497
1498static int usb_device_add(const char *devname, int is_hotplug)
1499{
1500 const char *p;
1501 USBDevice *dev;
1502
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001503 if (strstart(devname, "host:", &p)) {
1504 dev = usb_host_device_open(p);
1505 } else if (!strcmp(devname, "mouse")) {
1506 dev = usb_mouse_init();
1507 } else if (!strcmp(devname, "tablet")) {
1508 dev = usb_tablet_init();
1509 } else if (!strcmp(devname, "keyboard")) {
1510 dev = usb_keyboard_init();
1511 } else if (strstart(devname, "disk:", &p)) {
1512#if 0
1513 BlockDriverState *bs;
1514#endif
1515 dev = usb_msd_init(p);
1516 if (!dev)
1517 return -1;
1518#if 0
1519 bs = usb_msd_get_bdrv(dev);
1520 if (bdrv_key_required(bs)) {
1521 autostart = 0;
1522 if (is_hotplug) {
1523 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1524 dev);
1525 return 0;
1526 }
1527 }
1528 } else if (!strcmp(devname, "wacom-tablet")) {
1529 dev = usb_wacom_init();
1530 } else if (strstart(devname, "serial:", &p)) {
1531 dev = usb_serial_init(p);
1532#ifdef CONFIG_BRLAPI
1533 } else if (!strcmp(devname, "braille")) {
1534 dev = usb_baum_init();
1535#endif
1536 } else if (strstart(devname, "net:", &p)) {
1537 int nic = nb_nics;
1538
1539 if (net_client_init("nic", p) < 0)
1540 return -1;
1541 nd_table[nic].model = "usb";
1542 dev = usb_net_init(&nd_table[nic]);
1543 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1544 dev = usb_bt_init(devname[2] ? hci_init(p) :
1545 bt_new_hci(qemu_find_bt_vlan(0)));
1546#endif
1547 } else {
1548 return -1;
1549 }
1550 if (!dev)
1551 return -1;
1552
1553 return usb_device_add_dev(dev);
1554}
1555
1556int usb_device_del_addr(int bus_num, int addr)
1557{
1558 USBPort *port;
1559 USBPort **lastp;
1560 USBDevice *dev;
1561
1562 if (!used_usb_ports)
1563 return -1;
1564
1565 if (bus_num != 0)
1566 return -1;
1567
1568 lastp = &used_usb_ports;
1569 port = used_usb_ports;
1570 while (port && port->dev->addr != addr) {
1571 lastp = &port->next;
1572 port = port->next;
1573 }
1574
1575 if (!port)
1576 return -1;
1577
1578 dev = port->dev;
1579 *lastp = port->next;
1580 usb_attach(port, NULL);
1581 dev->handle_destroy(dev);
1582 port->next = free_usb_ports;
1583 free_usb_ports = port;
1584 return 0;
1585}
1586
1587static int usb_device_del(const char *devname)
1588{
1589 int bus_num, addr;
1590 const char *p;
1591
1592 if (strstart(devname, "host:", &p))
1593 return usb_host_device_close(p);
1594
1595 if (!used_usb_ports)
1596 return -1;
1597
1598 p = strchr(devname, '.');
1599 if (!p)
1600 return -1;
1601 bus_num = strtoul(devname, NULL, 0);
1602 addr = strtoul(p + 1, NULL, 0);
1603
1604 return usb_device_del_addr(bus_num, addr);
1605}
1606
1607void do_usb_add(Monitor *mon, const char *devname)
1608{
1609 usb_device_add(devname, 1);
1610}
1611
1612void do_usb_del(Monitor *mon, const char *devname)
1613{
1614 usb_device_del(devname);
1615}
1616
1617void usb_info(Monitor *mon)
1618{
1619 USBDevice *dev;
1620 USBPort *port;
1621 const char *speed_str;
1622
1623 if (!usb_enabled) {
1624 monitor_printf(mon, "USB support not enabled\n");
1625 return;
1626 }
1627
1628 for (port = used_usb_ports; port; port = port->next) {
1629 dev = port->dev;
1630 if (!dev)
1631 continue;
1632 switch(dev->speed) {
1633 case USB_SPEED_LOW:
1634 speed_str = "1.5";
1635 break;
1636 case USB_SPEED_FULL:
1637 speed_str = "12";
1638 break;
1639 case USB_SPEED_HIGH:
1640 speed_str = "480";
1641 break;
1642 default:
1643 speed_str = "?";
1644 break;
1645 }
1646 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1647 0, dev->addr, speed_str, dev->devname);
1648 }
1649}
1650
1651/***********************************************************/
1652/* PCMCIA/Cardbus */
1653
1654static struct pcmcia_socket_entry_s {
1655 PCMCIASocket *socket;
1656 struct pcmcia_socket_entry_s *next;
1657} *pcmcia_sockets = 0;
1658
1659void pcmcia_socket_register(PCMCIASocket *socket)
1660{
1661 struct pcmcia_socket_entry_s *entry;
1662
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001663 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001664 entry->socket = socket;
1665 entry->next = pcmcia_sockets;
1666 pcmcia_sockets = entry;
1667}
1668
1669void pcmcia_socket_unregister(PCMCIASocket *socket)
1670{
1671 struct pcmcia_socket_entry_s *entry, **ptr;
1672
1673 ptr = &pcmcia_sockets;
1674 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1675 if (entry->socket == socket) {
1676 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001677 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001678 }
1679}
1680
1681void pcmcia_info(Monitor *mon)
1682{
1683 struct pcmcia_socket_entry_s *iter;
1684
1685 if (!pcmcia_sockets)
1686 monitor_printf(mon, "No PCMCIA sockets\n");
1687
1688 for (iter = pcmcia_sockets; iter; iter = iter->next)
1689 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1690 iter->socket->attached ? iter->socket->card_string :
1691 "Empty");
1692}
1693
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001694/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001695/* machine registration */
1696
1697static QEMUMachine *first_machine = NULL;
1698QEMUMachine *current_machine = NULL;
1699
1700int qemu_register_machine(QEMUMachine *m)
1701{
1702 QEMUMachine **pm;
1703 pm = &first_machine;
1704 while (*pm != NULL)
1705 pm = &(*pm)->next;
1706 m->next = NULL;
1707 *pm = m;
1708 return 0;
1709}
1710
1711static QEMUMachine *find_machine(const char *name)
1712{
1713 QEMUMachine *m;
1714
1715 for(m = first_machine; m != NULL; m = m->next) {
1716 if (!strcmp(m->name, name))
1717 return m;
1718 }
1719 return NULL;
1720}
1721
1722static QEMUMachine *find_default_machine(void)
1723{
1724 QEMUMachine *m;
1725
1726 for(m = first_machine; m != NULL; m = m->next) {
1727 if (m->is_default) {
1728 return m;
1729 }
1730 }
1731 return NULL;
1732}
1733
1734/***********************************************************/
1735/* main execution loop */
1736
1737static void gui_update(void *opaque)
1738{
1739 uint64_t interval = GUI_REFRESH_INTERVAL;
1740 DisplayState *ds = opaque;
1741 DisplayChangeListener *dcl = ds->listeners;
1742
1743 dpy_refresh(ds);
1744
1745 while (dcl != NULL) {
1746 if (dcl->gui_timer_interval &&
1747 dcl->gui_timer_interval < interval)
1748 interval = dcl->gui_timer_interval;
1749 dcl = dcl->next;
1750 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001751 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001752}
1753
1754static void nographic_update(void *opaque)
1755{
1756 uint64_t interval = GUI_REFRESH_INTERVAL;
1757
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001758 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001759}
1760
1761struct vm_change_state_entry {
1762 VMChangeStateHandler *cb;
1763 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001764 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001765};
1766
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001767static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001768
1769VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1770 void *opaque)
1771{
1772 VMChangeStateEntry *e;
1773
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001774 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001775
1776 e->cb = cb;
1777 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001778 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001779 return e;
1780}
1781
1782void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1783{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001784 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001785 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001786}
1787
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001788void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001789{
1790 VMChangeStateEntry *e;
1791
1792 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1793 e->cb(e->opaque, running, reason);
1794 }
1795}
1796
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001797void vm_start(void)
1798{
1799 if (!vm_running) {
1800 cpu_enable_ticks();
1801 vm_running = 1;
1802 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001803 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001804 resume_all_vcpus();
1805 }
1806}
1807
1808/* reset/shutdown handler */
1809
1810typedef struct QEMUResetEntry {
1811 QEMUResetHandler *func;
1812 void *opaque;
1813 int order;
1814 struct QEMUResetEntry *next;
1815} QEMUResetEntry;
1816
1817static QEMUResetEntry *first_reset_entry;
1818static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001819static int shutdown_requested, shutdown_signal = -1;
1820static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001821static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001822int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001823static int vmstop_requested;
1824
1825int qemu_shutdown_requested(void)
1826{
1827 int r = shutdown_requested;
1828 shutdown_requested = 0;
1829 return r;
1830}
1831
1832int qemu_reset_requested(void)
1833{
1834 int r = reset_requested;
1835 reset_requested = 0;
1836 return r;
1837}
1838
1839int qemu_powerdown_requested(void)
1840{
1841 int r = powerdown_requested;
1842 powerdown_requested = 0;
1843 return r;
1844}
1845
1846static int qemu_debug_requested(void)
1847{
1848 int r = debug_requested;
1849 debug_requested = 0;
1850 return r;
1851}
1852
1853static int qemu_vmstop_requested(void)
1854{
1855 int r = vmstop_requested;
1856 vmstop_requested = 0;
1857 return r;
1858}
1859
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001860void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1861{
1862 QEMUResetEntry **pre, *re;
1863
1864 pre = &first_reset_entry;
1865 while (*pre != NULL && (*pre)->order >= order) {
1866 pre = &(*pre)->next;
1867 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001868 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001869 re->func = func;
1870 re->opaque = opaque;
1871 re->order = order;
1872 re->next = NULL;
1873 *pre = re;
1874}
1875
1876void qemu_system_reset(void)
1877{
1878 QEMUResetEntry *re;
1879
1880 /* reset all devices */
1881 for(re = first_reset_entry; re != NULL; re = re->next) {
1882 re->func(re->opaque);
1883 }
1884}
1885
1886void qemu_system_reset_request(void)
1887{
1888 if (no_reboot) {
1889 shutdown_requested = 1;
1890 } else {
1891 reset_requested = 1;
1892 }
1893 qemu_notify_event();
1894}
1895
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001896void qemu_system_killed(int signal, pid_t pid)
1897{
1898 shutdown_signal = signal;
1899 shutdown_pid = pid;
1900 qemu_system_shutdown_request();
1901}
1902
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001903void qemu_system_shutdown_request(void)
1904{
1905 shutdown_requested = 1;
1906 qemu_notify_event();
1907}
1908
1909void qemu_system_powerdown_request(void)
1910{
1911 powerdown_requested = 1;
1912 qemu_notify_event();
1913}
1914
1915#ifdef CONFIG_IOTHREAD
1916static void qemu_system_vmstop_request(int reason)
1917{
1918 vmstop_requested = reason;
1919 qemu_notify_event();
1920}
1921#endif
1922
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001923void main_loop_wait(int timeout)
1924{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001925 fd_set rfds, wfds, xfds;
1926 int ret, nfds;
1927 struct timeval tv;
1928
1929 qemu_bh_update_timeout(&timeout);
1930
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001931 os_host_main_loop_wait(&timeout);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001932
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001933
1934 tv.tv_sec = timeout / 1000;
1935 tv.tv_usec = (timeout % 1000) * 1000;
1936
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001937 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001938
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001939 /* XXX: separate device handlers from system ones */
1940 nfds = -1;
1941 FD_ZERO(&rfds);
1942 FD_ZERO(&wfds);
1943 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001944 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001945 if (slirp_is_inited()) {
1946 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1947 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001948
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001949 qemu_mutex_unlock_iothread();
1950 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1951 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001952 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001953 if (slirp_is_inited()) {
1954 if (ret < 0) {
1955 FD_ZERO(&rfds);
1956 FD_ZERO(&wfds);
1957 FD_ZERO(&xfds);
1958 }
1959 slirp_select_poll(&rfds, &wfds, &xfds);
1960 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001961 charpipe_poll();
1962
David Turner6a9ef172010-09-09 22:54:36 +02001963 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07001964
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001965 /* Check bottom-halves last in case any of the earlier events triggered
1966 them. */
1967 qemu_bh_poll();
1968
1969}
1970
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001971static int vm_can_run(void)
1972{
1973 if (powerdown_requested)
1974 return 0;
1975 if (reset_requested)
1976 return 0;
1977 if (shutdown_requested)
1978 return 0;
1979 if (debug_requested)
1980 return 0;
1981 return 1;
1982}
1983
1984static void main_loop(void)
1985{
1986 int r;
1987
1988#ifdef CONFIG_IOTHREAD
1989 qemu_system_ready = 1;
1990 qemu_cond_broadcast(&qemu_system_cond);
1991#endif
1992
Jun Nakajimaa381ef02011-12-17 19:13:25 -08001993#ifdef CONFIG_HAX
1994 if (hax_enabled())
1995 hax_sync_vcpus();
1996#endif
1997
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001998 for (;;) {
1999 do {
2000#ifdef CONFIG_PROFILER
2001 int64_t ti;
2002#endif
2003#ifndef CONFIG_IOTHREAD
2004 tcg_cpu_exec();
2005#endif
2006#ifdef CONFIG_PROFILER
2007 ti = profile_getclock();
2008#endif
2009 main_loop_wait(qemu_calculate_timeout());
2010#ifdef CONFIG_PROFILER
2011 dev_time += profile_getclock() - ti;
2012#endif
rich canningsd952f282011-03-01 15:40:09 -08002013
2014 if (rotate_logs_requested) {
2015 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
2016 dns_log_filename);
2017 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
2018 drop_log_filename);
2019 slirp_dns_log_fd(new_dns_log_fd);
2020 slirp_drop_log_fd(new_drop_log_fd);
2021 reset_rotate_qemu_logs_request();
2022 }
2023
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002024 } while (vm_can_run());
2025
2026 if (qemu_debug_requested())
2027 vm_stop(EXCP_DEBUG);
2028 if (qemu_shutdown_requested()) {
2029 if (no_shutdown) {
2030 vm_stop(0);
2031 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002032 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00002033 if (savevm_on_exit != NULL) {
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08002034 /* Prior to saving VM to the snapshot file, save HW config
2035 * settings for that VM, so we can match them when VM gets
2036 * loaded from the snapshot. */
2037 snaphost_save_config(savevm_on_exit);
Tim Baverstock24204cc2010-11-25 11:37:43 +00002038 do_savevm(cur_mon, savevm_on_exit);
2039 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002040 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002041 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002042 }
2043 if (qemu_reset_requested()) {
2044 pause_all_vcpus();
2045 qemu_system_reset();
2046 resume_all_vcpus();
2047 }
2048 if (qemu_powerdown_requested())
2049 qemu_system_powerdown();
2050 if ((r = qemu_vmstop_requested()))
2051 vm_stop(r);
2052 }
2053 pause_all_vcpus();
2054}
2055
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002056void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002057{
2058 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2059}
2060
2061void qemu_help(int exitcode)
2062{
2063 version();
2064 printf("usage: %s [options] [disk_image]\n"
2065 "\n"
2066 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2067 "\n"
2068#define DEF(option, opt_arg, opt_enum, opt_help) \
2069 opt_help
2070#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002071#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002072#undef DEF
2073#undef DEFHEADING
2074#undef GEN_DOCS
2075 "\n"
2076 "During emulation, the following keys are useful:\n"
2077 "ctrl-alt-f toggle full screen\n"
2078 "ctrl-alt-n switch to virtual console 'n'\n"
2079 "ctrl-alt toggle mouse and keyboard grab\n"
2080 "\n"
2081 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2082 ,
2083 "qemu",
2084 DEFAULT_RAM_SIZE,
2085#ifndef _WIN32
2086 DEFAULT_NETWORK_SCRIPT,
2087 DEFAULT_NETWORK_DOWN_SCRIPT,
2088#endif
2089 DEFAULT_GDBSTUB_PORT,
2090 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002091 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002092}
2093
2094#define HAS_ARG 0x0001
2095
2096enum {
2097#define DEF(option, opt_arg, opt_enum, opt_help) \
2098 opt_enum,
2099#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002100#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002101#undef DEF
2102#undef DEFHEADING
2103#undef GEN_DOCS
2104};
2105
2106typedef struct QEMUOption {
2107 const char *name;
2108 int flags;
2109 int index;
2110} QEMUOption;
2111
2112static const QEMUOption qemu_options[] = {
2113 { "h", 0, QEMU_OPTION_h },
2114#define DEF(option, opt_arg, opt_enum, opt_help) \
2115 { option, opt_arg, opt_enum },
2116#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002117#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002118#undef DEF
2119#undef DEFHEADING
2120#undef GEN_DOCS
2121 { NULL, 0, 0 },
2122};
2123
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002124static void select_vgahw (const char *p)
2125{
2126 const char *opts;
2127
2128 cirrus_vga_enabled = 0;
2129 std_vga_enabled = 0;
2130 vmsvga_enabled = 0;
2131 xenfb_enabled = 0;
2132 if (strstart(p, "std", &opts)) {
2133 std_vga_enabled = 1;
2134 } else if (strstart(p, "cirrus", &opts)) {
2135 cirrus_vga_enabled = 1;
2136 } else if (strstart(p, "vmware", &opts)) {
2137 vmsvga_enabled = 1;
2138 } else if (strstart(p, "xenfb", &opts)) {
2139 xenfb_enabled = 1;
2140 } else if (!strstart(p, "none", &opts)) {
2141 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002142 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002143 }
2144 while (*opts) {
2145 const char *nextopt;
2146
2147 if (strstart(opts, ",retrace=", &nextopt)) {
2148 opts = nextopt;
2149 if (strstart(opts, "dumb", &nextopt))
2150 vga_retrace_method = VGA_RETRACE_DUMB;
2151 else if (strstart(opts, "precise", &nextopt))
2152 vga_retrace_method = VGA_RETRACE_PRECISE;
2153 else goto invalid_vga;
2154 } else goto invalid_vga;
2155 opts = nextopt;
2156 }
2157}
2158
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002159#define MAX_NET_CLIENTS 32
2160
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002161#ifdef _WIN32
2162/* Look for support files in the same directory as the executable. */
2163static char *find_datadir(const char *argv0)
2164{
2165 char *p;
2166 char buf[MAX_PATH];
2167 DWORD len;
2168
2169 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2170 if (len == 0) {
2171 return NULL;
2172 }
2173
2174 buf[len] = 0;
2175 p = buf + len - 1;
2176 while (p != buf && *p != '\\')
2177 p--;
2178 *p = 0;
2179 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002180 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002181 }
2182 return NULL;
2183}
2184#else /* !_WIN32 */
2185
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002186/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002187static char *find_datadir(const char *argv0)
2188{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002189 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002190 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002191
2192#if defined(__linux__)
2193 {
2194 int len;
2195 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2196 if (len > 0) {
2197 buf[len] = 0;
2198 p = buf;
2199 }
2200 }
2201#elif defined(__FreeBSD__)
2202 {
2203 int len;
2204 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2205 if (len > 0) {
2206 buf[len] = 0;
2207 p = buf;
2208 }
2209 }
2210#endif
2211 /* If we don't have any way of figuring out the actual executable
2212 location then try argv[0]. */
2213 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002214 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002215 if (!p) {
2216 return NULL;
2217 }
2218 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002219
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002220 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002221}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002222#endif
2223
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002224static char*
2225qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
2226{
2227 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002228 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002229
2230 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2231 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2232 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002233 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002234 return NULL;
2235 }
2236 return buf;
2237}
2238
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002239char *qemu_find_file(int type, const char *name)
2240{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002241 const char *subdir;
2242 char *buf;
2243
2244 /* If name contains path separators then try it as a straight path. */
2245 if ((strchr(name, '/') || strchr(name, '\\'))
2246 && access(name, R_OK) == 0) {
2247 return strdup(name);
2248 }
2249 switch (type) {
2250 case QEMU_FILE_TYPE_BIOS:
2251 subdir = "";
2252 break;
2253 case QEMU_FILE_TYPE_KEYMAP:
2254 subdir = "keymaps/";
2255 break;
2256 default:
2257 abort();
2258 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002259 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2260#ifdef CONFIG_ANDROID
2261 if (type == QEMU_FILE_TYPE_BIOS) {
2262 /* This case corresponds to the emulator being used as part of an
2263 * SDK installation. NOTE: data_dir is really $bindir. */
2264 if (buf == NULL)
2265 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2266 /* This case corresponds to platform builds. */
2267 if (buf == NULL)
2268 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2269 /* Finally, try this for standalone builds under external/qemu */
2270 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08002271 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002272 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002273#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002274 return buf;
2275}
2276
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002277static int
2278add_dns_server( const char* server_name )
2279{
2280 SockAddress addr;
2281
2282 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2283 fprintf(stdout,
2284 "### WARNING: can't resolve DNS server name '%s'\n",
2285 server_name );
2286 return -1;
2287 }
2288
2289 fprintf(stderr,
2290 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2291
2292 if ( slirp_add_dns_server( &addr ) < 0 ) {
2293 fprintf(stderr,
2294 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2295 return -1;
2296 }
2297 return 0;
2298}
2299
rich cannings7339b552011-02-16 13:43:44 -08002300/* Parses an integer
2301 * Pararm:
2302 * str String containing a number to be parsed.
2303 * result Passes the parsed integer in this argument
2304 * returns 0 if ok, -1 if failed
2305 */
2306int
2307parse_int(const char *str, int *result)
2308{
2309 char* r;
2310 *result = strtol(str, &r, 0);
2311 if (r == NULL || *r != '\0')
2312 return -1;
2313
2314 return 0;
2315}
2316
rich canningsd952f282011-03-01 15:40:09 -08002317#ifndef _WIN32
2318/*
2319 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
2320 */
2321void init_qemu_clear_logs_sig() {
2322 struct sigaction act;
2323 sigfillset(&act.sa_mask);
2324 act.sa_flags = 0;
2325 act.sa_handler = rotate_qemu_logs_handler;
2326 if (sigaction(SIGUSR1, &act, NULL) == -1) {
2327 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
2328 exit(-1);
2329 }
2330}
2331#endif
2332
2333
rich cannings7339b552011-02-16 13:43:44 -08002334
2335/* parses a null-terminated string specifying a network port (e.g., "80") or
2336 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2337 * are the same. Returns 0 on success, -1 on error. */
2338
2339int parse_port_range(const char *str, unsigned short *lport,
2340 unsigned short *hport) {
2341
2342 unsigned int low = 0, high = 0;
2343 char *p, *arg = strdup(str);
2344
2345 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2346 p = arg + 1; /* skip '[' */
2347 low = atoi(strtok(p, "-"));
2348 high = atoi(strtok(NULL, "-"));
2349 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2350 *lport = low;
2351 *hport = high;
2352 }
2353 }
2354 else {
2355 low = atoi(arg);
2356 if ((0 < low) && (low < 65535)) {
2357 *lport = low;
2358 *hport = low;
2359 }
2360 }
2361 free(arg);
2362 if (low != 0)
2363 return 0;
2364 return -1;
2365}
2366
2367/*
2368 * Implements the generic port forwarding option
2369 */
2370void
2371net_slirp_forward(const char *optarg)
2372{
2373 /*
2374 * we expect the following format:
2375 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2376 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2377 */
2378 char *argument = strdup(optarg), *p = argument;
2379 char *dst_net, *dst_mask, *dst_port;
2380 char *redirect_ip, *redirect_port;
2381 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002382 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08002383
2384
2385 dst_net = strtok(p, ":");
2386 dst_mask = strtok(NULL, ":");
2387 dst_port = strtok(NULL, ":");
2388 redirect_ip = strtok(NULL, ":");
2389 redirect_port = strtok(NULL, ":");
2390
2391 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2392 redirect_ip == NULL || redirect_port == NULL) {
2393 fprintf(stderr,
2394 "Invalid argument for -net-forward, we expect "
2395 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2396 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2397 ":redirect_ip:redirect_port: %s\n",
2398 optarg);
2399 exit(1);
2400 }
2401
2402 /* inet_strtoip converts dotted address to host byte order */
2403 if (inet_strtoip(dst_net, &dnet) == -1) {
2404 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2405 exit(1);
2406 }
2407 if (inet_strtoip(dst_mask, &dmask) == -1) {
2408 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2409 exit(1);
2410 }
2411 if (inet_strtoip(redirect_ip, &rip) == -1) {
2412 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2413 exit(1);
2414 }
2415
2416 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2417 fprintf(stderr, "Invalid destination port or port range\n");
2418 exit(1);
2419 }
2420
2421 rport = atoi(redirect_port);
2422 if (!rport) {
2423 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2424 exit(1);
2425 }
2426
2427 dnet &= dmask;
2428
2429 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2430 rip, rport);
2431
2432 free(argument);
2433}
2434
2435
2436/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2437 * entry in the allows list */
2438void
2439slirp_allow(const char *optarg, u_int8_t proto)
2440{
2441 /*
2442 * we expect the following format:
2443 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2444 */
2445 char *argument = strdup(optarg), *p = argument;
2446 char *dst_ip_str, *dst_port_str;
2447 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002448 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08002449
2450 dst_ip_str = strtok(p, ":");
2451 dst_port_str = strtok(NULL, ":");
2452
2453 if (dst_ip_str == NULL || dst_port_str == NULL) {
2454 fprintf(stderr,
2455 "Invalid argument %s for -allow. We expect "
2456 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2457 optarg);
2458 exit(1);
2459 }
2460
2461 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2462 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2463 exit(1);
2464 }
2465 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2466 fprintf(stderr, "Invalid destination port or port range\n");
2467 exit(1);
2468 }
2469
2470 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2471
2472 free(argument);
2473}
2474
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002475/* Add a serial device at a given location in the emulated hardware table.
2476 * On failure, this function aborts the program with an error message.
2477 */
2478static void
2479serial_hds_add_at(int index, const char* devname)
2480{
2481 char label[32];
2482
2483 if (!devname || !strcmp(devname,"none"))
2484 return;
2485
2486 if (index >= MAX_SERIAL_PORTS) {
2487 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2488 devname, index, MAX_SERIAL_PORTS);
2489 }
2490 if (serial_hds[index] != NULL) {
2491 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2492 devname, index);
2493 }
2494 snprintf(label, sizeof(label), "serial%d", index);
2495 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2496 if (!serial_hds[index]) {
2497 PANIC("qemu: could not open serial device '%s'", devname);
2498 }
2499}
2500
2501
2502/* Find a free slot in the emulated serial device table, and register
2503 * it. Return the allocated table index.
2504 */
2505static int
2506serial_hds_add(const char* devname)
2507{
2508 int index;
2509
2510 /* Find first free slot */
2511 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2512 if (serial_hds[index] == NULL) {
2513 serial_hds_add_at(index, devname);
2514 return index;
2515 }
2516 }
2517
2518 PANIC("qemu: too many serial devices registered (%d)", index);
2519 return -1; /* shouldn't happen */
2520}
2521
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002522int main(int argc, char **argv, char **envp)
2523{
2524 const char *gdbstub_dev = NULL;
2525 uint32_t boot_devices_bitmap = 0;
2526 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002527 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002528 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002529 const char *initrd_filename;
2530 const char *kernel_filename, *kernel_cmdline;
2531 const char *boot_devices = "";
2532 DisplayState *ds;
2533 DisplayChangeListener *dcl;
2534 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002535 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002536 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002537 const char *net_clients[MAX_NET_CLIENTS];
2538 int nb_net_clients;
2539 const char *bt_opts[MAX_BT_CMDLINE];
2540 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002541 int optind;
2542 const char *r, *optarg;
2543 CharDriverState *monitor_hd = NULL;
2544 const char *monitor_device;
2545 const char *serial_devices[MAX_SERIAL_PORTS];
2546 int serial_device_index;
2547 const char *parallel_devices[MAX_PARALLEL_PORTS];
2548 int parallel_device_index;
2549 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2550 int virtio_console_index;
2551 const char *loadvm = NULL;
2552 QEMUMachine *machine;
2553 const char *cpu_model;
2554 const char *usb_devices[MAX_USB_CMDLINE];
2555 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002556 int tb_size;
2557 const char *pid_file = NULL;
2558 const char *incoming = NULL;
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01002559 CPUOldState *env;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002560 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002561 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002562 STRALLOC_DEFINE(kernel_params);
2563 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002564 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002565
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002566 /* Initialize sockets before anything else, so we can properly report
2567 * initialization failures back to the UI. */
2568#ifdef _WIN32
2569 socket_init();
2570#endif
2571
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002572 init_clocks();
2573
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002574 qemu_cache_utils_init(envp);
2575
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002576 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002577 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002578
2579 module_call_init(MODULE_INIT_MACHINE);
2580 machine = find_default_machine();
2581 cpu_model = NULL;
2582 initrd_filename = NULL;
2583 ram_size = 0;
2584 snapshot = 0;
2585 kernel_filename = NULL;
2586 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002587
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002588 cyls = heads = secs = 0;
2589 translation = BIOS_ATA_TRANSLATION_AUTO;
2590 monitor_device = "vc:80Cx24C";
2591
2592 serial_devices[0] = "vc:80Cx24C";
2593 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2594 serial_devices[i] = NULL;
2595 serial_device_index = 0;
2596
2597 parallel_devices[0] = "vc:80Cx24C";
2598 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2599 parallel_devices[i] = NULL;
2600 parallel_device_index = 0;
2601
2602 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2603 virtio_consoles[i] = NULL;
2604 virtio_console_index = 0;
2605
2606 for (i = 0; i < MAX_NODES; i++) {
2607 node_mem[i] = 0;
2608 node_cpumask[i] = 0;
2609 }
2610
2611 usb_devices_index = 0;
2612
2613 nb_net_clients = 0;
2614 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002615#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002616 nb_drives = 0;
2617 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002618#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002619 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002620
2621 nb_nics = 0;
2622
2623 tb_size = 0;
2624 autostart= 1;
2625
2626 register_watchdogs();
2627
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002628 /* Initialize boot properties. */
2629 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002630 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002631 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002632
David 'Digit' Turner36597752011-05-20 01:18:01 +02002633#ifdef CONFIG_KVM
2634 /* By default, force auto-detection for kvm */
2635 kvm_allowed = -1;
2636#endif
2637
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002638 optind = 1;
2639 for(;;) {
2640 if (optind >= argc)
2641 break;
2642 r = argv[optind];
2643 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002644 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002645 } else {
2646 const QEMUOption *popt;
2647
2648 optind++;
2649 /* Treat --foo the same as -foo. */
2650 if (r[1] == '-')
2651 r++;
2652 popt = qemu_options;
2653 for(;;) {
2654 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002655 PANIC("%s: invalid option -- '%s'",
2656 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002657 }
2658 if (!strcmp(popt->name, r + 1))
2659 break;
2660 popt++;
2661 }
2662 if (popt->flags & HAS_ARG) {
2663 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002664 PANIC("%s: option '%s' requires an argument",
2665 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002666 }
2667 optarg = argv[optind++];
2668 } else {
2669 optarg = NULL;
2670 }
2671
2672 switch(popt->index) {
2673 case QEMU_OPTION_M:
2674 machine = find_machine(optarg);
2675 if (!machine) {
2676 QEMUMachine *m;
2677 printf("Supported machines are:\n");
2678 for(m = first_machine; m != NULL; m = m->next) {
2679 printf("%-10s %s%s\n",
2680 m->name, m->desc,
2681 m->is_default ? " (default)" : "");
2682 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002683 if (*optarg != '?') {
2684 PANIC("Invalid machine parameter: %s",
2685 optarg);
2686 } else {
2687 QEMU_EXIT(0);
2688 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002689 }
2690 break;
2691 case QEMU_OPTION_cpu:
2692 /* hw initialization will check this */
2693 if (*optarg == '?') {
2694/* XXX: implement xxx_cpu_list for targets that still miss it */
2695#if defined(cpu_list)
2696 cpu_list(stdout, &fprintf);
2697#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002698 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002699 } else {
2700 cpu_model = optarg;
2701 }
2702 break;
2703 case QEMU_OPTION_initrd:
2704 initrd_filename = optarg;
2705 break;
2706 case QEMU_OPTION_hda:
2707 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002708 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002709 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002710 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002711 ",cyls=%d,heads=%d,secs=%d%s",
2712 0, cyls, heads, secs,
2713 translation == BIOS_ATA_TRANSLATION_LBA ?
2714 ",trans=lba" :
2715 translation == BIOS_ATA_TRANSLATION_NONE ?
2716 ",trans=none" : "");
2717 break;
2718 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002719 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2720 break;
2721
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002722 case QEMU_OPTION_hdc:
2723 case QEMU_OPTION_hdd:
2724 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2725 break;
2726 case QEMU_OPTION_drive:
2727 drive_add(NULL, "%s", optarg);
2728 break;
2729 case QEMU_OPTION_mtdblock:
2730 drive_add(optarg, MTD_ALIAS);
2731 break;
2732 case QEMU_OPTION_sd:
2733 drive_add(optarg, SD_ALIAS);
2734 break;
2735 case QEMU_OPTION_pflash:
2736 drive_add(optarg, PFLASH_ALIAS);
2737 break;
2738 case QEMU_OPTION_snapshot:
2739 snapshot = 1;
2740 break;
2741 case QEMU_OPTION_hdachs:
2742 {
2743 const char *p;
2744 p = optarg;
2745 cyls = strtol(p, (char **)&p, 0);
2746 if (cyls < 1 || cyls > 16383)
2747 goto chs_fail;
2748 if (*p != ',')
2749 goto chs_fail;
2750 p++;
2751 heads = strtol(p, (char **)&p, 0);
2752 if (heads < 1 || heads > 16)
2753 goto chs_fail;
2754 if (*p != ',')
2755 goto chs_fail;
2756 p++;
2757 secs = strtol(p, (char **)&p, 0);
2758 if (secs < 1 || secs > 63)
2759 goto chs_fail;
2760 if (*p == ',') {
2761 p++;
2762 if (!strcmp(p, "none"))
2763 translation = BIOS_ATA_TRANSLATION_NONE;
2764 else if (!strcmp(p, "lba"))
2765 translation = BIOS_ATA_TRANSLATION_LBA;
2766 else if (!strcmp(p, "auto"))
2767 translation = BIOS_ATA_TRANSLATION_AUTO;
2768 else
2769 goto chs_fail;
2770 } else if (*p != '\0') {
2771 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002772 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002773 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002774 if (hda_opts != NULL) {
2775 char num[16];
2776 snprintf(num, sizeof(num), "%d", cyls);
2777 qemu_opt_set(hda_opts, "cyls", num);
2778 snprintf(num, sizeof(num), "%d", heads);
2779 qemu_opt_set(hda_opts, "heads", num);
2780 snprintf(num, sizeof(num), "%d", secs);
2781 qemu_opt_set(hda_opts, "secs", num);
2782 if (translation == BIOS_ATA_TRANSLATION_LBA)
2783 qemu_opt_set(hda_opts, "trans", "lba");
2784 if (translation == BIOS_ATA_TRANSLATION_NONE)
2785 qemu_opt_set(hda_opts, "trans", "none");
2786 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002787 }
2788 break;
2789 case QEMU_OPTION_numa:
2790 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002791 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002792 }
2793 numa_add(optarg);
2794 break;
2795 case QEMU_OPTION_nographic:
2796 display_type = DT_NOGRAPHIC;
2797 break;
2798#ifdef CONFIG_CURSES
2799 case QEMU_OPTION_curses:
2800 display_type = DT_CURSES;
2801 break;
2802#endif
2803 case QEMU_OPTION_portrait:
2804 graphic_rotate = 1;
2805 break;
2806 case QEMU_OPTION_kernel:
2807 kernel_filename = optarg;
2808 break;
2809 case QEMU_OPTION_append:
2810 kernel_cmdline = optarg;
2811 break;
2812 case QEMU_OPTION_cdrom:
2813 drive_add(optarg, CDROM_ALIAS);
2814 break;
2815 case QEMU_OPTION_boot:
2816 boot_devices = optarg;
2817 /* We just do some generic consistency checks */
2818 {
2819 /* Could easily be extended to 64 devices if needed */
2820 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002821
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002822 boot_devices_bitmap = 0;
2823 for (p = boot_devices; *p != '\0'; p++) {
2824 /* Allowed boot devices are:
2825 * a b : floppy disk drives
2826 * c ... f : IDE disk drives
2827 * g ... m : machine implementation dependant drives
2828 * n ... p : network devices
2829 * It's up to each machine implementation to check
2830 * if the given boot devices match the actual hardware
2831 * implementation and firmware features.
2832 */
2833 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002834 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002835 }
2836 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002837 PANIC(
2838 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002839 }
2840 boot_devices_bitmap |= 1 << (*p - 'a');
2841 }
2842 }
2843 break;
2844 case QEMU_OPTION_fda:
2845 case QEMU_OPTION_fdb:
2846 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2847 break;
2848#ifdef TARGET_I386
2849 case QEMU_OPTION_no_fd_bootchk:
2850 fd_bootchk = 0;
2851 break;
2852#endif
2853 case QEMU_OPTION_net:
2854 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002855 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002856 }
2857 net_clients[nb_net_clients] = optarg;
2858 nb_net_clients++;
2859 break;
2860#ifdef CONFIG_SLIRP
2861 case QEMU_OPTION_tftp:
2862 tftp_prefix = optarg;
2863 break;
2864 case QEMU_OPTION_bootp:
2865 bootp_filename = optarg;
2866 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002867 case QEMU_OPTION_redir:
2868 net_slirp_redir(NULL, optarg, NULL);
2869 break;
2870#endif
2871 case QEMU_OPTION_bt:
2872 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002873 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002874 }
2875 bt_opts[nb_bt_opts++] = optarg;
2876 break;
2877#ifdef HAS_AUDIO
2878 case QEMU_OPTION_audio_help:
2879 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002880 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002881 break;
2882 case QEMU_OPTION_soundhw:
2883 select_soundhw (optarg);
2884 break;
2885#endif
2886 case QEMU_OPTION_h:
2887 qemu_help(0);
2888 break;
2889 case QEMU_OPTION_version:
2890 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002891 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002892 break;
2893 case QEMU_OPTION_m: {
2894 uint64_t value;
2895 char *ptr;
2896
2897 value = strtoul(optarg, &ptr, 10);
2898 switch (*ptr) {
2899 case 0: case 'M': case 'm':
2900 value <<= 20;
2901 break;
2902 case 'G': case 'g':
2903 value <<= 30;
2904 break;
2905 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002906 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002907 }
2908
2909 /* On 32-bit hosts, QEMU is limited by virtual address space */
2910 if (value > (2047 << 20)
2911#ifndef CONFIG_KQEMU
2912 && HOST_LONG_BITS == 32
2913#endif
2914 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002915 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002916 }
2917 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002918 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002919 }
2920 ram_size = value;
2921 break;
2922 }
2923 case QEMU_OPTION_d:
2924 {
2925 int mask;
2926 const CPULogItem *item;
2927
2928 mask = cpu_str_to_log_mask(optarg);
2929 if (!mask) {
2930 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002931 for(item = cpu_log_items; item->mask != 0; item++) {
2932 printf("%-10s %s\n", item->name, item->help);
2933 }
2934 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002935 }
2936 cpu_set_log(mask);
2937 }
2938 break;
2939 case QEMU_OPTION_s:
2940 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2941 break;
2942 case QEMU_OPTION_gdb:
2943 gdbstub_dev = optarg;
2944 break;
2945 case QEMU_OPTION_L:
2946 data_dir = optarg;
2947 break;
2948 case QEMU_OPTION_bios:
2949 bios_name = optarg;
2950 break;
2951 case QEMU_OPTION_singlestep:
2952 singlestep = 1;
2953 break;
2954 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002955 autostart = 0;
2956 break;
2957#ifndef _WIN32
2958 case QEMU_OPTION_k:
2959 keyboard_layout = optarg;
2960 break;
2961#endif
2962 case QEMU_OPTION_localtime:
2963 rtc_utc = 0;
2964 break;
2965 case QEMU_OPTION_vga:
2966 select_vgahw (optarg);
2967 break;
2968#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2969 case QEMU_OPTION_g:
2970 {
2971 const char *p;
2972 int w, h, depth;
2973 p = optarg;
2974 w = strtol(p, (char **)&p, 10);
2975 if (w <= 0) {
2976 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002977 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002978 }
2979 if (*p != 'x')
2980 goto graphic_error;
2981 p++;
2982 h = strtol(p, (char **)&p, 10);
2983 if (h <= 0)
2984 goto graphic_error;
2985 if (*p == 'x') {
2986 p++;
2987 depth = strtol(p, (char **)&p, 10);
2988 if (depth != 8 && depth != 15 && depth != 16 &&
2989 depth != 24 && depth != 32)
2990 goto graphic_error;
2991 } else if (*p == '\0') {
2992 depth = graphic_depth;
2993 } else {
2994 goto graphic_error;
2995 }
2996
2997 graphic_width = w;
2998 graphic_height = h;
2999 graphic_depth = depth;
3000 }
3001 break;
3002#endif
3003 case QEMU_OPTION_echr:
3004 {
3005 char *r;
3006 term_escape_char = strtol(optarg, &r, 0);
3007 if (r == optarg)
3008 printf("Bad argument to echr\n");
3009 break;
3010 }
3011 case QEMU_OPTION_monitor:
3012 monitor_device = optarg;
3013 break;
3014 case QEMU_OPTION_serial:
3015 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003016 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003017 }
3018 serial_devices[serial_device_index] = optarg;
3019 serial_device_index++;
3020 break;
3021 case QEMU_OPTION_watchdog:
3022 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003023 if (i > 0) {
3024 if (i == 1) {
3025 PANIC("Invalid watchdog parameter: %s",
3026 optarg);
3027 } else {
3028 QEMU_EXIT(0);
3029 }
3030 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003031 break;
3032 case QEMU_OPTION_watchdog_action:
3033 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003034 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003035 }
3036 break;
3037 case QEMU_OPTION_virtiocon:
3038 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003039 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003040 }
3041 virtio_consoles[virtio_console_index] = optarg;
3042 virtio_console_index++;
3043 break;
3044 case QEMU_OPTION_parallel:
3045 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003046 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003047 }
3048 parallel_devices[parallel_device_index] = optarg;
3049 parallel_device_index++;
3050 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003051 case QEMU_OPTION_loadvm:
3052 loadvm = optarg;
3053 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003054 case QEMU_OPTION_savevm_on_exit:
3055 savevm_on_exit = optarg;
3056 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003057 case QEMU_OPTION_full_screen:
3058 full_screen = 1;
3059 break;
3060#ifdef CONFIG_SDL
3061 case QEMU_OPTION_no_frame:
3062 no_frame = 1;
3063 break;
3064 case QEMU_OPTION_alt_grab:
3065 alt_grab = 1;
3066 break;
3067 case QEMU_OPTION_no_quit:
3068 no_quit = 1;
3069 break;
3070 case QEMU_OPTION_sdl:
3071 display_type = DT_SDL;
3072 break;
3073#endif
3074 case QEMU_OPTION_pidfile:
3075 pid_file = optarg;
3076 break;
3077#ifdef TARGET_I386
3078 case QEMU_OPTION_win2k_hack:
3079 win2k_install_hack = 1;
3080 break;
3081 case QEMU_OPTION_rtc_td_hack:
3082 rtc_td_hack = 1;
3083 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003084#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003085 case QEMU_OPTION_acpitable:
3086 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003087 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003088 }
3089 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003090#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003091 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02003092 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003093 break;
3094#endif
3095#ifdef CONFIG_KVM
3096 case QEMU_OPTION_enable_kvm:
3097 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003098 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02003099 case QEMU_OPTION_disable_kvm:
3100 kvm_allowed = 0;
3101 break;
3102#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003103 case QEMU_OPTION_usb:
3104 usb_enabled = 1;
3105 break;
3106 case QEMU_OPTION_usbdevice:
3107 usb_enabled = 1;
3108 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003109 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003110 }
3111 usb_devices[usb_devices_index] = optarg;
3112 usb_devices_index++;
3113 break;
3114 case QEMU_OPTION_smp:
3115 smp_cpus = atoi(optarg);
3116 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003117 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003118 }
3119 break;
3120 case QEMU_OPTION_vnc:
3121 display_type = DT_VNC;
3122 vnc_display = optarg;
3123 break;
3124#ifdef TARGET_I386
3125 case QEMU_OPTION_no_acpi:
3126 acpi_enabled = 0;
3127 break;
3128 case QEMU_OPTION_no_hpet:
3129 no_hpet = 1;
3130 break;
3131 case QEMU_OPTION_no_virtio_balloon:
3132 no_virtio_balloon = 1;
3133 break;
3134#endif
3135 case QEMU_OPTION_no_reboot:
3136 no_reboot = 1;
3137 break;
3138 case QEMU_OPTION_no_shutdown:
3139 no_shutdown = 1;
3140 break;
3141 case QEMU_OPTION_show_cursor:
3142 cursor_hide = 0;
3143 break;
3144 case QEMU_OPTION_uuid:
3145 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003146 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003147 }
3148 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003149 case QEMU_OPTION_option_rom:
3150 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003151 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003152 }
3153 option_rom[nb_option_roms] = optarg;
3154 nb_option_roms++;
3155 break;
3156#if defined(TARGET_ARM) || defined(TARGET_M68K)
3157 case QEMU_OPTION_semihosting:
3158 semihosting_enabled = 1;
3159 break;
3160#endif
3161 case QEMU_OPTION_name:
3162 qemu_name = optarg;
3163 break;
3164#if defined(TARGET_SPARC) || defined(TARGET_PPC)
3165 case QEMU_OPTION_prom_env:
3166 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003167 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003168 }
3169 prom_envs[nb_prom_envs] = optarg;
3170 nb_prom_envs++;
3171 break;
3172#endif
3173#ifdef TARGET_ARM
3174 case QEMU_OPTION_old_param:
3175 old_param = 1;
3176 break;
3177#endif
3178 case QEMU_OPTION_clock:
3179 configure_alarms(optarg);
3180 break;
3181 case QEMU_OPTION_startdate:
3182 {
3183 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02003184 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003185 if (!strcmp(optarg, "now")) {
3186 rtc_date_offset = -1;
3187 } else {
3188 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
3189 &tm.tm_year,
3190 &tm.tm_mon,
3191 &tm.tm_mday,
3192 &tm.tm_hour,
3193 &tm.tm_min,
3194 &tm.tm_sec) == 6) {
3195 /* OK */
3196 } else if (sscanf(optarg, "%d-%d-%d",
3197 &tm.tm_year,
3198 &tm.tm_mon,
3199 &tm.tm_mday) == 3) {
3200 tm.tm_hour = 0;
3201 tm.tm_min = 0;
3202 tm.tm_sec = 0;
3203 } else {
3204 goto date_fail;
3205 }
3206 tm.tm_year -= 1900;
3207 tm.tm_mon--;
3208 rtc_start_date = mktimegm(&tm);
3209 if (rtc_start_date == -1) {
3210 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003211 PANIC("Invalid date format. Valid format are:\n"
3212 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003213 }
3214 rtc_date_offset = time(NULL) - rtc_start_date;
3215 }
3216 }
3217 break;
rich cannings7339b552011-02-16 13:43:44 -08003218
3219 /* -------------------------------------------------------*/
3220 /* User mode network stack restrictions */
3221 case QEMU_OPTION_drop_udp:
3222 slirp_drop_udp();
3223 break;
3224 case QEMU_OPTION_drop_tcp:
3225 slirp_drop_tcp();
3226 break;
3227 case QEMU_OPTION_allow_tcp:
3228 slirp_allow(optarg, IPPROTO_TCP);
3229 break;
3230 case QEMU_OPTION_allow_udp:
3231 slirp_allow(optarg, IPPROTO_UDP);
3232 break;
3233 case QEMU_OPTION_drop_log:
3234 {
3235 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003236 drop_log_filename = optarg;
3237 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08003238
3239 if (!drop_log_fd) {
3240 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
3241 exit(1);
3242 }
3243
3244 slirp_drop_log_fd(drop_log_fd);
3245 }
3246 break;
3247
3248 case QEMU_OPTION_dns_log:
3249 {
3250 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003251 dns_log_filename = optarg;
3252 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08003253
3254 if (dns_log_fd == NULL) {
3255 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
3256 exit(1);
3257 }
3258
3259 slirp_dns_log_fd(dns_log_fd);
3260 }
3261 break;
3262
3263
3264 case QEMU_OPTION_max_dns_conns:
3265 {
3266 int max_dns_conns = 0;
3267 if (parse_int(optarg, &max_dns_conns)) {
3268 fprintf(stderr,
3269 "qemu: syntax: -max-dns-conns max_connections\n");
3270 exit(1);
3271 }
3272 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3273 fprintf(stderr,
3274 "Invalid arg for max dns connections: %s\n",
3275 optarg);
3276 exit(1);
3277 }
3278 slirp_set_max_dns_conns(max_dns_conns);
3279 }
3280 break;
3281
3282 case QEMU_OPTION_net_forward:
3283 net_slirp_forward(optarg);
3284 break;
3285 case QEMU_OPTION_net_forward_tcp2sink:
3286 {
3287 SockAddress saddr;
3288
3289 if (parse_host_port(&saddr, optarg)) {
3290 fprintf(stderr,
3291 "Invalid ip/port %s for "
3292 "-forward-dropped-tcp2sink. "
3293 "We expect 'sink_ip:sink_port'\n",
3294 optarg);
3295 exit(1);
3296 }
3297 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3298 saddr.u.inet.port);
3299 }
3300 break;
3301 /* -------------------------------------------------------*/
3302
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003303 case QEMU_OPTION_tb_size:
3304 tb_size = strtol(optarg, NULL, 0);
3305 if (tb_size < 0)
3306 tb_size = 0;
3307 break;
3308 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003309 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003310 break;
3311 case QEMU_OPTION_incoming:
3312 incoming = optarg;
3313 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003314#ifdef CONFIG_XEN
3315 case QEMU_OPTION_xen_domid:
3316 xen_domid = atoi(optarg);
3317 break;
3318 case QEMU_OPTION_xen_create:
3319 xen_mode = XEN_CREATE;
3320 break;
3321 case QEMU_OPTION_xen_attach:
3322 xen_mode = XEN_ATTACH;
3323 break;
3324#endif
3325
3326
3327 case QEMU_OPTION_mic:
3328 audio_input_source = (char*)optarg;
3329 break;
3330#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003331 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003332 trace_filename = optarg;
3333 tracing = 1;
3334 break;
3335#if 0
3336 case QEMU_OPTION_trace_miss:
3337 trace_cache_miss = 1;
3338 break;
3339 case QEMU_OPTION_trace_addr:
3340 trace_all_addr = 1;
3341 break;
3342#endif
3343 case QEMU_OPTION_tracing:
3344 if (strcmp(optarg, "off") == 0)
3345 tracing = 0;
3346 else if (strcmp(optarg, "on") == 0 && trace_filename)
3347 tracing = 1;
3348 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003349 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003350 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003351 }
3352 break;
3353#if 0
3354 case QEMU_OPTION_dcache_load_miss:
3355 dcache_load_miss_penalty = atoi(optarg);
3356 break;
3357 case QEMU_OPTION_dcache_store_miss:
3358 dcache_store_miss_penalty = atoi(optarg);
3359 break;
3360#endif
3361#endif
3362#ifdef CONFIG_NAND
3363 case QEMU_OPTION_nand:
3364 nand_add_dev(optarg);
3365 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003366
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003367#endif
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003368 case QEMU_OPTION_disable_hax:
3369 hax_disabled = 1;
3370 break;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003371 case QEMU_OPTION_android_ports:
3372 android_op_ports = (char*)optarg;
3373 break;
3374
3375 case QEMU_OPTION_android_port:
3376 android_op_port = (char*)optarg;
3377 break;
3378
3379 case QEMU_OPTION_android_report_console:
3380 android_op_report_console = (char*)optarg;
3381 break;
3382
3383 case QEMU_OPTION_http_proxy:
3384 op_http_proxy = (char*)optarg;
3385 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003386
3387 case QEMU_OPTION_charmap:
3388 op_charmap_file = (char*)optarg;
3389 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003390
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003391 case QEMU_OPTION_android_hw:
3392 android_op_hwini = (char*)optarg;
3393 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003394
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003395 case QEMU_OPTION_dns_server:
3396 android_op_dns_server = (char*)optarg;
3397 break;
3398
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003399 case QEMU_OPTION_radio:
3400 android_op_radio = (char*)optarg;
3401 break;
3402
3403 case QEMU_OPTION_gps:
3404 android_op_gps = (char*)optarg;
3405 break;
3406
3407 case QEMU_OPTION_audio:
3408 android_op_audio = (char*)optarg;
3409 break;
3410
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003411 case QEMU_OPTION_cpu_delay:
3412 android_op_cpu_delay = (char*)optarg;
3413 break;
3414
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003415 case QEMU_OPTION_show_kernel:
3416 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3417 break;
3418
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003419#ifdef CONFIG_NAND_LIMITS
3420 case QEMU_OPTION_nand_limits:
3421 android_op_nand_limits = (char*)optarg;
3422 break;
3423#endif // CONFIG_NAND_LIMITS
3424
3425 case QEMU_OPTION_netspeed:
3426 android_op_netspeed = (char*)optarg;
3427 break;
3428
3429 case QEMU_OPTION_netdelay:
3430 android_op_netdelay = (char*)optarg;
3431 break;
3432
3433 case QEMU_OPTION_netfast:
3434 android_op_netfast = 1;
3435 break;
3436
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003437 case QEMU_OPTION_tcpdump:
3438 android_op_tcpdump = (char*)optarg;
3439 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003440
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003441 case QEMU_OPTION_boot_property:
3442 boot_property_parse_option((char*)optarg);
3443 break;
3444
3445 case QEMU_OPTION_lcd_density:
3446 android_op_lcd_density = (char*)optarg;
3447 break;
3448
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003449 case QEMU_OPTION_ui_port:
3450 android_op_ui_port = (char*)optarg;
3451 break;
3452
3453 case QEMU_OPTION_ui_settings:
3454 android_op_ui_settings = (char*)optarg;
3455 break;
3456
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003457 case QEMU_OPTION_audio_test_out:
3458 android_audio_test_start_out();
3459 break;
3460
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003461 case QEMU_OPTION_android_avdname:
3462 android_op_avd_name = (char*)optarg;
3463 break;
3464
3465 case QEMU_OPTION_timezone:
3466 if (timezone_set((char*)optarg)) {
3467 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3468 (char*)optarg);
3469 }
3470 break;
3471
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003472#ifdef CONFIG_MEMCHECK
3473 case QEMU_OPTION_android_memcheck:
3474 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003475 /* This will set ro.kernel.memcheck system property
3476 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003477 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003478 break;
3479#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003480
3481 case QEMU_OPTION_snapshot_no_time_update:
3482 android_snapshot_update_time = 0;
3483 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003484
3485 case QEMU_OPTION_list_webcam:
3486 android_list_web_cameras();
3487 exit(0);
3488
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003489 default:
3490 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003491 }
3492 }
3493 }
3494
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003495 /* Initialize character map. */
3496 if (android_charmap_setup(op_charmap_file)) {
3497 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003498 PANIC(
3499 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003500 op_charmap_file);
3501 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003502 PANIC(
3503 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003504 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003505 }
3506
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003507 /* If no data_dir is specified then try to find it relative to the
3508 executable path. */
3509 if (!data_dir) {
3510 data_dir = find_datadir(argv[0]);
3511 }
3512 /* If all else fails use the install patch specified when building. */
3513 if (!data_dir) {
3514 data_dir = CONFIG_QEMU_SHAREDIR;
3515 }
3516
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003517 if (!android_op_hwini) {
3518 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003519 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003520 hw_ini = iniFile_newFromFile(android_op_hwini);
3521 if (hw_ini == NULL) {
3522 PANIC("Could not find %s file.", android_op_hwini);
3523 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003524
3525 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003526 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003527
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003528 /* If we're loading VM from a snapshot, make sure that the current HW config
3529 * matches the one with which the VM has been saved. */
3530 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3531 exit(0);
3532 }
3533
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003534 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003535
3536 {
3537 int width = android_hw->hw_lcd_width;
3538 int height = android_hw->hw_lcd_height;
3539 int depth = android_hw->hw_lcd_depth;
3540
3541 /* A bit of sanity checking */
3542 if (width <= 0 || height <= 0 ||
3543 (depth != 16 && depth != 32) ||
3544 (((width|height) & 3) != 0) )
3545 {
3546 PANIC("Invalid display configuration (%d,%d,%d)",
3547 width, height, depth);
3548 }
3549 android_display_width = width;
3550 android_display_height = height;
3551 android_display_bpp = depth;
3552 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003553
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003554#ifdef CONFIG_NAND_LIMITS
3555 /* Init nand stuff. */
3556 if (android_op_nand_limits) {
3557 parse_nand_limits(android_op_nand_limits);
3558 }
3559#endif // CONFIG_NAND_LIMITS
3560
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003561 /* Initialize AVD name from hardware configuration if needed */
3562 if (!android_op_avd_name) {
3563 if (android_hw->avd_name && *android_hw->avd_name) {
3564 android_op_avd_name = android_hw->avd_name;
3565 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3566 }
3567 }
3568
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003569 /* Initialize system partition image */
3570 {
3571 char tmp[PATH_MAX+32];
3572 const char* sysImage = android_hw->disk_systemPartition_path;
3573 const char* initImage = android_hw->disk_systemPartition_initPath;
3574 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3575
3576 if (sysBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003577 PANIC("Invalid system partition size: %" PRIu64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003578 }
3579
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003580 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRIx64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003581
3582 if (sysImage && *sysImage) {
3583 if (filelock_create(sysImage) == NULL) {
3584 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3585 /* If there is no file= parameters, nand_add_dev will create
3586 * a temporary file to back the partition image. */
3587 } else {
3588 pstrcat(tmp,sizeof(tmp),",file=");
3589 pstrcat(tmp,sizeof(tmp),sysImage);
3590 }
3591 }
3592 if (initImage && *initImage) {
3593 if (!path_exists(initImage)) {
3594 PANIC("Invalid initial system image path: %s", initImage);
3595 }
3596 pstrcat(tmp,sizeof(tmp),",initfile=");
3597 pstrcat(tmp,sizeof(tmp),initImage);
3598 } else {
3599 PANIC("Missing initial system image path!");
3600 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003601 if (android_hw->hw_useext4) {
3602 /* Using a nand device to approximate a block device until full
3603 * support is added */
3604 pstrcat(tmp,sizeof(tmp),",pagesize=512,extrasize=0");
3605 }
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003606 nand_add_dev(tmp);
3607 }
3608
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003609 /* Initialize data partition image */
3610 {
3611 char tmp[PATH_MAX+32];
3612 const char* dataImage = android_hw->disk_dataPartition_path;
3613 const char* initImage = android_hw->disk_dataPartition_initPath;
3614 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3615
3616 if (dataBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003617 PANIC("Invalid data partition size: %" PRIu64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003618 }
3619
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003620 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRIx64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003621
3622 if (dataImage && *dataImage) {
3623 if (filelock_create(dataImage) == NULL) {
3624 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3625 /* Note: if there is no file= parameters, nand_add_dev() will
3626 * create a temporary file to back the partition image. */
3627 } else {
3628 /* Create the file if needed */
3629 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003630 if (path_empty_file(dataImage) < 0) {
3631 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3632 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003633 }
3634 pstrcat(tmp, sizeof(tmp), ",file=");
3635 pstrcat(tmp, sizeof(tmp), dataImage);
3636 }
3637 }
3638 if (initImage && *initImage) {
3639 pstrcat(tmp, sizeof(tmp), ",initfile=");
3640 pstrcat(tmp, sizeof(tmp), initImage);
3641 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003642 if (android_hw->hw_useext4) {
3643 /* Using a nand device to approximate a block device until full
3644 * support is added */
3645 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3646 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003647 nand_add_dev(tmp);
3648 }
3649
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003650 /* Init SD-Card stuff. For Android, it is always hda */
3651 /* If the -hda option was used, ignore the Android-provided one */
3652 if (hda_opts == NULL) {
3653 const char* sdPath = android_hw->hw_sdCard_path;
3654 if (sdPath && *sdPath) {
3655 if (!path_exists(sdPath)) {
3656 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3657 } else if (filelock_create(sdPath) == NULL) {
3658 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3659 } else {
3660 /* Successful locking */
3661 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003662 /* Set this property of any operation involving the SD Card
3663 * will be x100 slower, due to the corresponding file being
3664 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3665 * the context of an emulator crash. The data is already
3666 * synced properly when the emulator exits (either normally or through ^C).
3667 */
3668 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003669 }
3670 }
3671 }
3672
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003673 if (hdb_opts == NULL) {
3674 const char* spath = android_hw->disk_snapStorage_path;
3675 if (spath && *spath) {
3676 if (!path_exists(spath)) {
3677 PANIC("Snapshot storage file does not exist: %s", spath);
3678 }
3679 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003680 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003681 }
3682 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003683 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003684 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003685 }
3686 }
3687
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003688 /* Set the VM's max heap size, passed as a boot property */
3689 if (android_hw->vm_heapSize > 0) {
3690 char tmp[64];
3691 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3692 boot_property_add("dalvik.vm.heapsize",tmp);
3693 }
3694
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003695 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3696 if (android_hw->hw_ramSize <= 512) {
3697 boot_property_add("ro.config.low_ram", "true");
3698 }
3699
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003700 /* Initialize net speed and delays stuff. */
3701 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003702 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003703 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003704 }
3705
3706 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003707 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003708 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003709 }
3710
3711 if (android_op_netfast) {
3712 qemu_net_download_speed = 0;
3713 qemu_net_upload_speed = 0;
3714 qemu_net_min_latency = 0;
3715 qemu_net_max_latency = 0;
3716 }
3717
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003718 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003719 if (android_hw->hw_lcd_density) {
3720 long density = android_hw->hw_lcd_density;
3721 if (density <= 0) {
3722 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003723 }
3724 hwLcd_setBootProperty(density);
3725 }
3726
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003727 /* Initialize presence of hardware nav button */
3728 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3729
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003730 /* Initialize TCP dump */
3731 if (android_op_tcpdump) {
3732 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3733 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3734 }
3735 }
3736
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003737 /* Initialize modem */
3738 if (android_op_radio) {
3739 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3740 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003741 PANIC("unsupported character device specification: %s\n"
3742 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003743 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003744 }
3745 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3746 } else if (android_hw->hw_gsmModem != 0 ) {
3747 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003748 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003749 }
3750 }
3751
3752 /* Initialize GPS */
3753 if (android_op_gps) {
3754 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3755 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003756 PANIC("unsupported character device specification: %s\n"
3757 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003758 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003759 }
3760 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3761 } else if (android_hw->hw_gps != 0) {
3762 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003763 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003764 }
3765 }
3766
3767 /* Initialize audio. */
3768 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003769 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003770 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003771 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003772 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003773 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003774 }
3775
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003776 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003777 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003778
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003779 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003780 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3781 strcmp(android_hw->hw_camera_front, "emulated")) {
3782 /* Fake camera is not used for camera emulation. */
3783 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003784 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003785 /* Fake camera is used for at least one camera emulation. */
3786 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3787 !strcmp(android_hw->hw_camera_front, "emulated")) {
3788 /* Fake camera is used for both, front and back camera emulation. */
3789 boot_property_add("qemu.sf.fake_camera", "both");
3790 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3791 boot_property_add("qemu.sf.fake_camera", "back");
3792 } else {
3793 boot_property_add("qemu.sf.fake_camera", "front");
3794 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003795 }
3796
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003797 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3798 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3799 int density;
3800 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3801 PANIC("-lcd-density : %d", density);
3802 }
3803 hwLcd_setBootProperty(density);
3804 }
3805
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003806 /* Initialize camera emulation. */
3807 android_camera_service_init();
3808
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003809 if (android_op_cpu_delay) {
3810 char* end;
3811 long delay = strtol(android_op_cpu_delay, &end, 0);
3812 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003813 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003814 }
3815 if (delay > 0)
3816 delay = (1000-delay);
3817
3818 qemu_cpu_delay = (int) delay;
3819 }
3820
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003821 if (android_op_dns_server) {
3822 char* x = strchr(android_op_dns_server, ',');
3823 dns_count = 0;
3824 if (x == NULL)
3825 {
3826 if ( add_dns_server( android_op_dns_server ) == 0 )
3827 dns_count = 1;
3828 }
3829 else
3830 {
3831 x = android_op_dns_server;
3832 while (*x) {
3833 char* y = strchr(x, ',');
3834
3835 if (y != NULL) {
3836 *y = 0;
3837 y++;
3838 } else {
3839 y = x + strlen(x);
3840 }
3841
3842 if (y > x && add_dns_server( x ) == 0) {
3843 dns_count += 1;
3844 }
3845 x = y;
3846 }
3847 }
3848 if (dns_count == 0)
3849 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3850 }
3851
3852 if (dns_count == 0)
3853 dns_count = slirp_get_system_dns_servers();
3854 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003855 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003856 }
3857
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003858#ifdef CONFIG_MEMCHECK
3859 if (android_op_memcheck) {
3860 memcheck_init(android_op_memcheck);
3861 }
3862#endif // CONFIG_MEMCHECK
3863
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003864 /* Initialize cache partition, if any */
3865 if (android_hw->disk_cachePartition != 0) {
3866 char tmp[PATH_MAX+32];
3867 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003868 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003869
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003870 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRIx64, partSize);
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003871
3872 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3873 if (filelock_create(partPath) == NULL) {
3874 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3875 /* Note: if there is no file= parameters, nand_add_dev() will
3876 * create a temporary file to back the partition image. */
3877 } else {
3878 /* Create the file if needed */
3879 if (!path_exists(partPath)) {
3880 if (path_empty_file(partPath) < 0) {
3881 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3882 }
3883 }
3884 pstrcat(tmp, sizeof(tmp), ",file=");
3885 pstrcat(tmp, sizeof(tmp), partPath);
3886 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003887 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003888 if (android_hw->hw_useext4) {
3889 /* Using a nand device to approximate a block device until full
3890 * support is added */
3891 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3892 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003893 nand_add_dev(tmp);
3894 }
3895
Jesse Hall733fffa2012-04-26 11:07:32 -07003896 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3897 * If set to 0, the software GL ES renderer will be used as a fallback.
3898 * If the parameter is undefined, this means the system image runs
3899 * inside an emulator that doesn't support GPU emulation at all.
3900 *
3901 * We always start the GL ES renderer so we can gather stats on the
3902 * underlying GL implementation. If GL ES acceleration is disabled,
3903 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003904 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003905 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003906 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003907 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003908 {
3909 android_getOpenglesHardwareStrings(
3910 android_gl_vendor, sizeof(android_gl_vendor),
3911 android_gl_renderer, sizeof(android_gl_renderer),
3912 android_gl_version, sizeof(android_gl_version));
3913 if (android_hw->hw_gpu_enabled) {
3914 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003915 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003916 android_stopOpenglesRenderer();
3917 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003918 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003919 } else {
3920 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003921 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003922 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003923 stralloc_add_str(kernel_params, " qemu.gles=1");
3924 } else {
3925 stralloc_add_str(kernel_params, " qemu.gles=0");
3926 }
3927 }
3928
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003929 /* We always force qemu=1 when running inside QEMU */
3930 stralloc_add_str(kernel_params, " qemu=1");
3931
3932 /* We always initialize the first serial port for the android-kmsg
3933 * character device (used to send kernel messages) */
3934 serial_hds_add_at(0, "android-kmsg");
3935 stralloc_add_str(kernel_params, " console=ttyS0");
3936
3937 /* We always initialize the second serial port for the android-qemud
3938 * character device as well */
3939 serial_hds_add_at(1, "android-qemud");
3940 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3941
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003942 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3943 os_pidfile_error();
3944 exit(1);
3945 }
3946
David 'Digit' Turner36597752011-05-20 01:18:01 +02003947#if defined(CONFIG_KVM)
3948 if (kvm_allowed < 0) {
3949 kvm_allowed = kvm_check_allowed();
3950 }
3951#endif
3952
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003953#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
3954 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003955 PANIC(
3956 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003957 }
3958#endif
3959
3960 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3961 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003962 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3963 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003964 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003965 }
3966
3967 if (display_type == DT_NOGRAPHIC) {
3968 if (serial_device_index == 0)
3969 serial_devices[0] = "stdio";
3970 if (parallel_device_index == 0)
3971 parallel_devices[0] = "null";
3972 if (strncmp(monitor_device, "vc", 2) == 0)
3973 monitor_device = "stdio";
3974 }
3975
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003976#ifdef CONFIG_KQEMU
3977 if (smp_cpus > 1)
3978 kqemu_allowed = 0;
3979#endif
3980 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003981 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003982 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003983
3984 if (kernel_filename == NULL) {
3985 kernel_filename = android_hw->kernel_path;
3986 }
3987 if (initrd_filename == NULL) {
3988 initrd_filename = android_hw->disk_ramdisk_path;
3989 }
3990
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003991 linux_boot = (kernel_filename != NULL);
3992 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3993
3994 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003995 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003996 }
3997
3998 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003999 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004000 }
4001
4002 /* boot to floppy or the default cd if no hard disk defined yet */
4003 if (!boot_devices[0]) {
4004 boot_devices = "cad";
4005 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004006 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004007
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004008 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004009 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004010 }
David Turner6a9ef172010-09-09 22:54:36 +02004011 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004012
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004013 /* init network clients */
4014 if (nb_net_clients == 0) {
4015 /* if no clients, we use a default config */
4016 net_clients[nb_net_clients++] = "nic";
4017#ifdef CONFIG_SLIRP
4018 net_clients[nb_net_clients++] = "user";
4019#endif
4020 }
4021
4022 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004023 if (net_client_parse(net_clients[i]) < 0) {
4024 PANIC("Unable to parse net clients");
4025 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004026 }
4027 net_client_check();
4028
4029#ifdef TARGET_I386
4030 /* XXX: this should be moved in the PC machine instantiation code */
4031 if (net_boot != 0) {
4032 int netroms = 0;
4033 for (i = 0; i < nb_nics && i < 4; i++) {
4034 const char *model = nd_table[i].model;
4035 char buf[1024];
4036 char *filename;
4037 if (net_boot & (1 << i)) {
4038 if (model == NULL)
4039 model = "ne2k_pci";
4040 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
4041 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
4042 if (filename && get_image_size(filename) > 0) {
4043 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004044 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004045 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01004046 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004047 nb_option_roms++;
4048 netroms++;
4049 }
4050 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01004051 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004052 }
4053 }
4054 }
4055 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004056 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004057 }
4058 }
4059#endif
4060
4061 /* init the bluetooth world */
4062 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004063 if (bt_parse(bt_opts[i])) {
4064 PANIC("Unable to parse bluetooth options");
4065 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004066
4067 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01004068 if (ram_size == 0) {
4069 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
4070 if (ram_size == 0) {
4071 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4072 }
4073 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004074
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07004075 /* Quite often (especially on older XP machines) attempts to allocate large
4076 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
4077 * inside QEMU, it's not really possible to provide the user with a
4078 * meaningful explanation for the crash. So, lets see if QEMU is going to be
4079 * able to allocate requested amount of RAM, and if not, lets try to come up
4080 * with a recomendation. */
4081 {
4082 ram_addr_t r_ram = ram_size;
4083 void* alloc_check = malloc(r_ram);
4084 while (alloc_check == NULL && r_ram > 1024 * 1024) {
4085 /* Make it 25% less */
4086 r_ram -= r_ram / 4;
4087 alloc_check = malloc(r_ram);
4088 }
4089 if (alloc_check != NULL) {
4090 free(alloc_check);
4091 }
4092 if (r_ram != ram_size) {
4093 /* Requested RAM is too large. Report this, as well as calculated
4094 * recomendation. */
4095 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
4096 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
4097 ram_size = r_ram;
4098 }
4099 }
4100
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004101#ifdef CONFIG_KQEMU
4102 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
4103 guest ram allocation. It needs to go away. */
4104 if (kqemu_allowed) {
4105 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
4106 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
4107 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004108 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004109 }
4110 }
4111#endif
4112
rich canningsd952f282011-03-01 15:40:09 -08004113#ifndef _WIN32
4114 init_qemu_clear_logs_sig();
4115#endif
4116
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004117 /* init the dynamic translator */
4118 cpu_exec_init_all(tb_size * 1024 * 1024);
4119
4120 bdrv_init();
4121
4122 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004123#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004124 if (nb_drives_opt < MAX_DRIVES)
4125 drive_add(NULL, CDROM_ALIAS);
4126
4127 /* we always create at least one floppy */
4128
4129 if (nb_drives_opt < MAX_DRIVES)
4130 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004131 /* we always create one sd slot, even if no card is in it */
4132
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004133 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004134 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004135 }
4136#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004137
4138 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004139 if (snapshot)
4140 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4141 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
4142 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004143
David Turner6a9ef172010-09-09 22:54:36 +02004144 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004145 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
4146
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004147 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004148 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004149
4150 /* Maintain compatibility with multiple stdio monitors */
4151 if (!strcmp(monitor_device,"stdio")) {
4152 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4153 const char *devname = serial_devices[i];
4154 if (devname && !strcmp(devname,"mon:stdio")) {
4155 monitor_device = NULL;
4156 break;
4157 } else if (devname && !strcmp(devname,"stdio")) {
4158 monitor_device = NULL;
4159 serial_devices[i] = "mon:stdio";
4160 break;
4161 }
4162 }
4163 }
4164
4165 if (nb_numa_nodes > 0) {
4166 int i;
4167
4168 if (nb_numa_nodes > smp_cpus) {
4169 nb_numa_nodes = smp_cpus;
4170 }
4171
4172 /* If no memory size if given for any node, assume the default case
4173 * and distribute the available memory equally across all nodes
4174 */
4175 for (i = 0; i < nb_numa_nodes; i++) {
4176 if (node_mem[i] != 0)
4177 break;
4178 }
4179 if (i == nb_numa_nodes) {
4180 uint64_t usedmem = 0;
4181
4182 /* On Linux, the each node's border has to be 8MB aligned,
4183 * the final node gets the rest.
4184 */
4185 for (i = 0; i < nb_numa_nodes - 1; i++) {
4186 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4187 usedmem += node_mem[i];
4188 }
4189 node_mem[i] = ram_size - usedmem;
4190 }
4191
4192 for (i = 0; i < nb_numa_nodes; i++) {
4193 if (node_cpumask[i] != 0)
4194 break;
4195 }
4196 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4197 * must cope with this anyway, because there are BIOSes out there in
4198 * real machines which also use this scheme.
4199 */
4200 if (i == nb_numa_nodes) {
4201 for (i = 0; i < smp_cpus; i++) {
4202 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4203 }
4204 }
4205 }
4206
4207 if (kvm_enabled()) {
4208 int ret;
4209
4210 ret = kvm_init(smp_cpus);
4211 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004212 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004213 }
4214 }
4215
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004216#ifdef CONFIG_HAX
4217 if (!hax_disabled)
4218 {
4219 int ret;
4220
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08004221 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004222 ret = hax_init(smp_cpus);
4223 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
4224 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
4225 }
4226#endif
4227
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004228 if (monitor_device) {
4229 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
4230 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004231 PANIC("qemu: could not open monitor device '%s'",
4232 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004233 }
4234 }
4235
4236 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004237 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004238 }
4239
4240 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4241 const char *devname = parallel_devices[i];
4242 if (devname && strcmp(devname, "none")) {
4243 char label[32];
4244 snprintf(label, sizeof(label), "parallel%d", i);
4245 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
4246 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004247 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004248 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004249 }
4250 }
4251 }
4252
4253 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4254 const char *devname = virtio_consoles[i];
4255 if (devname && strcmp(devname, "none")) {
4256 char label[32];
4257 snprintf(label, sizeof(label), "virtcon%d", i);
4258 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
4259 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004260 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004261 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004262 }
4263 }
4264 }
4265
4266 module_call_init(MODULE_INIT_DEVICE);
4267
4268
4269#ifdef CONFIG_TRACE
4270 if (trace_filename) {
4271 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004272 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
4273 }
4274#endif
4275
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004276 /* Check the CPU Architecture value */
4277#if defined(TARGET_ARM)
4278 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4279 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4280 android_hw->hw_cpu_arch);
4281 exit(1);
4282 }
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -07004283#elif defined(TARGET_I386)
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004284 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4285 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4286 android_hw->hw_cpu_arch);
4287 exit(1);
4288 }
4289#endif
4290
4291 /* Grab CPU model if provided in hardware.ini */
4292 if ( !cpu_model
4293 && android_hw->hw_cpu_model
4294 && android_hw->hw_cpu_model[0] != '\0')
4295 {
4296 cpu_model = android_hw->hw_cpu_model;
4297 }
4298
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004299 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004300 * collected during initialization.
4301 *
4302 * The order is the following:
4303 * - parameters from the hw configuration (kernel.parameters)
4304 * - additionnal parameters from options (e.g. -memcheck)
4305 * - the -append parameters.
4306 */
4307 {
4308 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004309
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004310 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004311 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004312 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4313 }
4314
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004315 /* If not empty, kernel_config always contains a leading space */
4316 stralloc_append(kernel_params, kernel_config);
4317
4318 if (*kernel_cmdline) {
4319 stralloc_add_c(kernel_params, ' ');
4320 stralloc_add_str(kernel_params, kernel_cmdline);
4321 }
4322
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004323 /* Remove any leading/trailing spaces */
4324 stralloc_strip(kernel_params);
4325
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004326 kernel_parameters = stralloc_cstr(kernel_params);
4327 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4328
4329 machine->init(ram_size,
4330 boot_devices,
4331 kernel_filename,
4332 kernel_parameters,
4333 initrd_filename,
4334 cpu_model);
4335
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004336 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07004337 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004338 mts_port_create(NULL);
4339 }
4340
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004341 stralloc_reset(kernel_params);
4342 stralloc_reset(kernel_config);
4343 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004344
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004345 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4346 for (i = 0; i < nb_numa_nodes; i++) {
4347 if (node_cpumask[i] & (1 << env->cpu_index)) {
4348 env->numa_node = i;
4349 }
4350 }
4351 }
4352
4353 current_machine = machine;
4354
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01004355 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004356 if (kvm_enabled()) {
4357 int ret;
4358
4359 ret = kvm_sync_vcpus();
4360 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004361 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004362 }
4363 }
4364
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004365#ifdef CONFIG_HAX
4366 if (hax_enabled())
4367 hax_sync_vcpus();
4368#endif
4369
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004370 /* init USB devices */
4371 if (usb_enabled) {
4372 for(i = 0; i < usb_devices_index; i++) {
4373 if (usb_device_add(usb_devices[i], 0) < 0) {
4374 fprintf(stderr, "Warning: could not add USB device %s\n",
4375 usb_devices[i]);
4376 }
4377 }
4378 }
4379
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004380 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004381 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004382
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004383 /* Initialize display from the command line parameters. */
4384 android_display_reset(ds,
4385 android_display_width,
4386 android_display_height,
4387 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004388
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004389 if (display_type == DT_DEFAULT) {
4390#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4391 display_type = DT_SDL;
4392#else
4393 display_type = DT_VNC;
4394 vnc_display = "localhost:0,to=99";
4395 show_vnc_port = 1;
4396#endif
4397 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004398
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004399
4400 switch (display_type) {
4401 case DT_NOGRAPHIC:
4402 break;
4403#if defined(CONFIG_CURSES)
4404 case DT_CURSES:
4405 curses_display_init(ds, full_screen);
4406 break;
4407#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004408#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004409 case DT_SDL:
4410 sdl_display_init(ds, full_screen, no_frame);
4411 break;
4412#elif defined(CONFIG_COCOA)
4413 case DT_SDL:
4414 cocoa_display_init(ds, full_screen);
4415 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004416#elif defined(CONFIG_STANDALONE_CORE)
4417 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004418 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004419 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004420#endif
4421 case DT_VNC:
4422 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004423 if (vnc_display_open(ds, vnc_display) < 0) {
4424 PANIC("Unable to initialize VNC display");
4425 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004426
4427 if (show_vnc_port) {
4428 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4429 }
4430 break;
4431 default:
4432 break;
4433 }
4434 dpy_resize(ds);
4435
4436 dcl = ds->listeners;
4437 while (dcl != NULL) {
4438 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004439 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
4440 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004441 }
4442 dcl = dcl->next;
4443 }
4444
4445 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004446 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
4447 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004448 }
4449
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004450 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004451 qemu_chr_initial_reset();
4452
4453 if (monitor_device && monitor_hd)
4454 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4455
4456 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4457 const char *devname = serial_devices[i];
4458 if (devname && strcmp(devname, "none")) {
4459 if (strstart(devname, "vc", 0))
4460 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4461 }
4462 }
4463
4464 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4465 const char *devname = parallel_devices[i];
4466 if (devname && strcmp(devname, "none")) {
4467 if (strstart(devname, "vc", 0))
4468 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4469 }
4470 }
4471
4472 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4473 const char *devname = virtio_consoles[i];
4474 if (virtcon_hds[i] && devname) {
4475 if (strstart(devname, "vc", 0))
4476 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4477 }
4478 }
4479
4480 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004481 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004482 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004483 }
4484
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004485 /* call android-specific setup function */
4486 android_emulation_setup();
4487
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004488#if !defined(CONFIG_STANDALONE_CORE)
4489 // For the standalone emulator (UI+core in one executable) we need to
4490 // set the window title here.
4491 android_emulator_set_base_port(android_base_port);
4492#endif
4493
Ot ten Thije871da2a2010-09-20 10:29:22 +01004494 if (loadvm)
4495 do_loadvm(cur_mon, loadvm);
4496
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004497 if (incoming) {
4498 autostart = 0; /* fixme how to deal with -daemonize */
4499 qemu_start_incoming_migration(incoming);
4500 }
4501
4502 if (autostart)
4503 vm_start();
4504
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004505 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004506
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004507#ifdef CONFIG_ANDROID
4508 // This will notify the UI that the core is successfuly initialized
4509 android_core_init_completed();
4510#endif // CONFIG_ANDROID
4511
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004512 main_loop();
4513 quit_timers();
4514 net_cleanup();
4515 android_emulation_teardown();
4516 return 0;
4517}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004518
4519void
4520android_emulation_teardown(void)
4521{
4522 android_charmap_done();
4523}