blob: 7023b443db8561bd26ed41c8f39a03e8ced5d089 [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' Turner1365eb22014-02-16 22:23:26 +010038#include "hw/loader.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070039#include "hw/baum.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010040#include "hw/android/goldfish/nand.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010041#include "net/net.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010042#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010043#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010044#include "exec/gdbstub.h"
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +010045#include "qemu/log.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +010046#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +010047#include "sysemu/char.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010048#include "sysemu/blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070049#include "audio/audio.h"
50
David 'Digit' Turner28a09b62013-12-15 00:16:00 +010051#include "migration/qemu-file.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "android/android.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010053#include "android/charpipe.h"
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +010054#include "android/log-rotate.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070055#include "modem_driver.h"
David 'Digit' Turner890f4642014-02-24 11:53:03 +010056#include "android/filesystems/ext4_utils.h"
David 'Digit' Turner965a6e02014-06-23 18:29:20 +020057#include "android/filesystems/fstab_parser.h"
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +020058#include "android/filesystems/partition_types.h"
David 'Digit' Turner965a6e02014-06-23 18:29:20 +020059#include "android/filesystems/ramdisk_extractor.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070060#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070061#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020062#include "android/hw-pipe-net.h"
63#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070064#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080065#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070066#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070067#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070068#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010069#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010070#include "android/utils/filelock.h"
71#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010072#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010073#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080074#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080075#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010076#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020077#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070078#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010079#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010080#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070081
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +010082#ifdef CONFIG_ANDROID_MEMCHECK
David 'Digit' Turner96e493a2014-03-14 17:17:26 +010083#include "android/qemu/memcheck/memcheck.h"
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +010084#endif // CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070085
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070086#include <unistd.h>
87#include <fcntl.h>
88#include <signal.h>
89#include <time.h>
90#include <errno.h>
91#include <sys/time.h>
92#include <zlib.h>
93
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070094/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070095#include "config-host.h"
96
97#ifndef _WIN32
98#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070099#include <sys/times.h>
100#include <sys/wait.h>
101#include <termios.h>
102#include <sys/mman.h>
103#include <sys/ioctl.h>
104#include <sys/resource.h>
105#include <sys/socket.h>
106#include <netinet/in.h>
107#include <net/if.h>
108#if defined(__NetBSD__)
109#include <net/if_tap.h>
110#endif
111#ifdef __linux__
112#include <linux/if_tun.h>
113#endif
114#include <arpa/inet.h>
115#include <dirent.h>
116#include <netdb.h>
117#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700118#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700119#include <sys/stat.h>
120#if defined(__FreeBSD__) || defined(__DragonFly__)
121#include <libutil.h>
122#else
123#include <util.h>
124#endif
125#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
126#include <freebsd/stdlib.h>
127#else
128#ifdef __linux__
129#include <pty.h>
130#include <malloc.h>
131#include <linux/rtc.h>
132
133/* For the benefit of older linux systems which don't supply it,
134 we use a local copy of hpet.h. */
135/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100136#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700137
138#include <linux/ppdev.h>
139#include <linux/parport.h>
140#endif
141#ifdef __sun__
142#include <sys/stat.h>
143#include <sys/ethernet.h>
144#include <sys/sockio.h>
145#include <netinet/arp.h>
146#include <netinet/in.h>
147#include <netinet/in_systm.h>
148#include <netinet/ip.h>
149#include <netinet/ip_icmp.h> // must come after ip.h
150#include <netinet/udp.h>
151#include <netinet/tcp.h>
152#include <net/if.h>
153#include <syslog.h>
154#include <stropts.h>
155#endif
156#endif
157#endif
158
159#if defined(__OpenBSD__)
160#include <util.h>
161#endif
162
163#if defined(CONFIG_VDE)
164#include <libvdeplug.h>
165#endif
166
167#ifdef _WIN32
168#include <windows.h>
169#include <malloc.h>
170#include <sys/timeb.h>
171#include <mmsystem.h>
172#define getopt_long_only getopt_long
173#define memalign(align, size) malloc(size)
174#endif
175
David 'Digit' Turner11822842013-12-17 09:16:47 +0100176#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100177#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700178
179#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200180int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700181#undef main
182#define main qemu_main
183#endif /* CONFIG_COCOA */
184
185#include "hw/hw.h"
186#include "hw/boards.h"
187#include "hw/usb.h"
188#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100189#include "hw/i386/pc.h"
190#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700191#include "hw/baum.h"
192#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100193#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100194#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100195#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100196#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100197#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100198#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100199#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100200#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100201#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100202#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100203#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100204#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100205#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100206#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700207#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100208#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100209#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100210#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200211#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100212#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200213#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100214#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700215#include "android/hw-lcd.h"
216#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200217#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700218#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100219#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700220
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800221#include "android/snaphost-android.h"
222
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800223#if !defined(CONFIG_STANDALONE_CORE)
224/* in android/qemulator.c */
225extern void android_emulator_set_base_port(int port);
226#endif
227
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700228#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700229#undef main
230#define main qemu_main
231#endif
232
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100233#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700234
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100235#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700236
237#if defined(CONFIG_SLIRP)
238#include "libslirp.h"
239#endif
240
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700241#define DEFAULT_RAM_SIZE 128
242
243/* Max number of USB devices that can be specified on the commandline. */
244#define MAX_USB_CMDLINE 8
245
246/* Max number of bluetooth switches on the commandline. */
247#define MAX_BT_CMDLINE 10
248
249/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700250
251static const char *data_dir;
252const char *bios_name = NULL;
253static void *ioport_opaque[MAX_IOPORTS];
254static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
255static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100256#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700257/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
258 to store the VM snapshots */
259DriveInfo drives_table[MAX_DRIVES+1];
260int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100261#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700262enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700263DisplayType display_type = DT_DEFAULT;
264const char* keyboard_layout = NULL;
265int64_t ticks_per_sec;
266ram_addr_t ram_size;
David 'Digit' Turner0e8e7482014-02-17 20:25:52 +0100267bool xen_allowed;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200268const char *mem_path = NULL;
269#ifdef MAP_POPULATE
270int mem_prealloc = 0; /* force preallocation of physical target memory */
271#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700272int nb_nics;
273NICInfo nd_table[MAX_NICS];
274int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100275int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700276static int rtc_utc = 1;
277static int rtc_date_offset = -1; /* -1 means no change */
278int cirrus_vga_enabled = 1;
279int std_vga_enabled = 0;
280int vmsvga_enabled = 0;
281int xenfb_enabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700282static int full_screen = 0;
283#ifdef CONFIG_SDL
284static int no_frame = 0;
285#endif
286int no_quit = 0;
287CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100288int serial_hds_count;
289
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700290CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
291CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
292#ifdef TARGET_I386
293int win2k_install_hack = 0;
294int rtc_td_hack = 0;
295#endif
296int usb_enabled = 0;
297int singlestep = 0;
298int smp_cpus = 1;
299const char *vnc_display;
300int acpi_enabled = 1;
301int no_hpet = 0;
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +0200302int hax_disabled = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700303int no_virtio_balloon = 0;
304int fd_bootchk = 1;
305int no_reboot = 0;
306int no_shutdown = 0;
307int cursor_hide = 1;
308int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700309WatchdogTimerModel *watchdog = NULL;
310int watchdog_action = WDT_RESET;
311const char *option_rom[MAX_OPTION_ROMS];
312int nb_option_roms;
313int semihosting_enabled = 0;
314#ifdef TARGET_ARM
315int old_param = 0;
316#endif
317const char *qemu_name;
318int alt_grab = 0;
319#if defined(TARGET_SPARC) || defined(TARGET_PPC)
320unsigned int nb_prom_envs = 0;
321const char *prom_envs[MAX_PROM_ENVS];
322#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100323#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700324int nb_drives_opt;
325struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100326#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700327int nb_numa_nodes;
328uint64_t node_mem[MAX_NODES];
329uint64_t node_cpumask[MAX_NODES];
330
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700331static QEMUTimer *nographic_timer;
332
333uint8_t qemu_uuid[16];
334
335
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700336int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700337extern char* audio_input_source;
338
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700339extern char* android_op_ports;
340extern char* android_op_port;
341extern char* android_op_report_console;
342extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700343// Path to the file containing specific key character map.
344char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700345
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700346/* Path to hardware initialization file passed with -android-hw option. */
347char* android_op_hwini = NULL;
348
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700349/* Memory checker options. */
350char* android_op_memcheck = NULL;
351
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700352/* -dns-server option value. */
353char* android_op_dns_server = NULL;
354
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700355/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700356char* android_op_radio = NULL;
357
358/* -gps option value. */
359char* android_op_gps = NULL;
360
361/* -audio option value. */
362char* android_op_audio = NULL;
363
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700364/* -cpu-delay option value. */
365char* android_op_cpu_delay = NULL;
366
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700367#ifdef CONFIG_NAND_LIMITS
368/* -nand-limits option value. */
369char* android_op_nand_limits = NULL;
370#endif // CONFIG_NAND_LIMITS
371
372/* -netspeed option value. */
373char* android_op_netspeed = NULL;
374
375/* -netdelay option value. */
376char* android_op_netdelay = NULL;
377
378/* -netfast option value. */
379int android_op_netfast = 0;
380
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700381/* -tcpdump option value. */
382char* android_op_tcpdump = NULL;
383
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700384/* -lcd-density option value. */
385char* android_op_lcd_density = NULL;
386
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700387/* -ui-port option value. This port will be used to report the core
388 * initialization completion.
389 */
390char* android_op_ui_port = NULL;
391
392/* -ui-settings option value. This value will be passed to the UI when new UI
393 * process is attaching to the core.
394 */
395char* android_op_ui_settings = NULL;
396
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800397/* -android-avdname option value. */
398char* android_op_avd_name = "unknown";
399
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +0200400bool android_op_wipe_data = false;
401
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700402extern int android_display_width;
403extern int android_display_height;
404extern int android_display_bpp;
405
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700406extern void dprint( const char* format, ... );
407
rich canningsd952f282011-03-01 15:40:09 -0800408const char* dns_log_filename = NULL;
409const char* drop_log_filename = NULL;
rich canningsd952f282011-03-01 15:40:09 -0800410
Tim Baverstock24204cc2010-11-25 11:37:43 +0000411const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000412
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700413#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
414
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700415/* Reports the core initialization failure to the error stdout and to the UI
416 * socket before exiting the application.
417 * Parameters that are passed to this macro are used to format the error
418 * mesage using sprintf routine.
419 */
420#ifdef CONFIG_ANDROID
421#define PANIC(...) android_core_init_failure(__VA_ARGS__)
422#else
423#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
424 exit(1); \
425 } while (0)
426#endif // CONFIG_ANDROID
427
428/* Exits the core during initialization. */
429#ifdef CONFIG_ANDROID
430#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
431#else
432#define QEMU_EXIT(exit_code) exit(exit_code)
433#endif // CONFIG_ANDROID
434
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700435/***********************************************************/
436/* x86 ISA bus support */
437
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100438hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700439PicState2 *isa_pic;
440
441static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
442static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
443
444static uint32_t ioport_read(int index, uint32_t address)
445{
446 static IOPortReadFunc *default_func[3] = {
447 default_ioport_readb,
448 default_ioport_readw,
449 default_ioport_readl
450 };
451 IOPortReadFunc *func = ioport_read_table[index][address];
452 if (!func)
453 func = default_func[index];
454 return func(ioport_opaque[address], address);
455}
456
457static void ioport_write(int index, uint32_t address, uint32_t data)
458{
459 static IOPortWriteFunc *default_func[3] = {
460 default_ioport_writeb,
461 default_ioport_writew,
462 default_ioport_writel
463 };
464 IOPortWriteFunc *func = ioport_write_table[index][address];
465 if (!func)
466 func = default_func[index];
467 func(ioport_opaque[address], address, data);
468}
469
470static uint32_t default_ioport_readb(void *opaque, uint32_t address)
471{
472#ifdef DEBUG_UNUSED_IOPORT
473 fprintf(stderr, "unused inb: port=0x%04x\n", address);
474#endif
475 return 0xff;
476}
477
478static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
479{
480#ifdef DEBUG_UNUSED_IOPORT
481 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
482#endif
483}
484
485/* default is to make two byte accesses */
486static uint32_t default_ioport_readw(void *opaque, uint32_t address)
487{
488 uint32_t data;
489 data = ioport_read(0, address);
490 address = (address + 1) & (MAX_IOPORTS - 1);
491 data |= ioport_read(0, address) << 8;
492 return data;
493}
494
495static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
496{
497 ioport_write(0, address, data & 0xff);
498 address = (address + 1) & (MAX_IOPORTS - 1);
499 ioport_write(0, address, (data >> 8) & 0xff);
500}
501
502static uint32_t default_ioport_readl(void *opaque, uint32_t address)
503{
504#ifdef DEBUG_UNUSED_IOPORT
505 fprintf(stderr, "unused inl: port=0x%04x\n", address);
506#endif
507 return 0xffffffff;
508}
509
510static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
511{
512#ifdef DEBUG_UNUSED_IOPORT
513 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
514#endif
515}
516
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700517/***************/
518/* ballooning */
519
520static QEMUBalloonEvent *qemu_balloon_event;
521void *qemu_balloon_event_opaque;
522
523void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
524{
525 qemu_balloon_event = func;
526 qemu_balloon_event_opaque = opaque;
527}
528
529void qemu_balloon(ram_addr_t target)
530{
531 if (qemu_balloon_event)
532 qemu_balloon_event(qemu_balloon_event_opaque, target);
533}
534
535ram_addr_t qemu_balloon_status(void)
536{
537 if (qemu_balloon_event)
538 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
539 return 0;
540}
541
542/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700543/* host time/date access */
544void qemu_get_timedate(struct tm *tm, int offset)
545{
546 time_t ti;
547 struct tm *ret;
548
549 time(&ti);
550 ti += offset;
551 if (rtc_date_offset == -1) {
552 if (rtc_utc)
553 ret = gmtime(&ti);
554 else
555 ret = localtime(&ti);
556 } else {
557 ti -= rtc_date_offset;
558 ret = gmtime(&ti);
559 }
560
561 memcpy(tm, ret, sizeof(struct tm));
562}
563
564int qemu_timedate_diff(struct tm *tm)
565{
566 time_t seconds;
567
568 if (rtc_date_offset == -1)
569 if (rtc_utc)
570 seconds = mktimegm(tm);
571 else
572 seconds = mktime(tm);
573 else
574 seconds = mktimegm(tm) + rtc_date_offset;
575
576 return seconds - time(NULL);
577}
578
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700579/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700580/* QEMU Block devices */
581
582#define HD_ALIAS "index=%d,media=disk"
583#define CDROM_ALIAS "index=2,media=cdrom"
584#define FD_ALIAS "index=%d,if=floppy"
585#define PFLASH_ALIAS "if=pflash"
586#define MTD_ALIAS "if=mtd"
587#define SD_ALIAS "index=0,if=sd"
588
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100589static int drive_init_func(QemuOpts *opts, void *opaque)
590{
591 int *use_scsi = opaque;
592 int fatal_error = 0;
593
594 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
595 if (fatal_error)
596 return 1;
597 }
598 return 0;
599}
600
601static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
602{
603 if (NULL == qemu_opt_get(opts, "snapshot")) {
604 qemu_opt_set(opts, "snapshot", "on");
605 }
606 return 0;
607}
608
609#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700610static int drive_opt_get_free_idx(void)
611{
612 int index;
613
614 for (index = 0; index < MAX_DRIVES; index++)
615 if (!drives_opt[index].used) {
616 drives_opt[index].used = 1;
617 return index;
618 }
619
620 return -1;
621}
622
623static int drive_get_free_idx(void)
624{
625 int index;
626
627 for (index = 0; index < MAX_DRIVES; index++)
628 if (!drives_table[index].used) {
629 drives_table[index].used = 1;
630 return index;
631 }
632
633 return -1;
634}
635
636int drive_add(const char *file, const char *fmt, ...)
637{
638 va_list ap;
639 int index = drive_opt_get_free_idx();
640
641 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
642 fprintf(stderr, "qemu: too many drives\n");
643 return -1;
644 }
645
646 drives_opt[index].file = file;
647 va_start(ap, fmt);
648 vsnprintf(drives_opt[index].opt,
649 sizeof(drives_opt[0].opt), fmt, ap);
650 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700651
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700652 nb_drives_opt++;
653 return index;
654}
655
656void drive_remove(int index)
657{
658 drives_opt[index].used = 0;
659 nb_drives_opt--;
660}
661
662int drive_get_index(BlockInterfaceType type, int bus, int unit)
663{
664 int index;
665
666 /* seek interface, bus and unit */
667
668 for (index = 0; index < MAX_DRIVES; index++)
669 if (drives_table[index].type == type &&
670 drives_table[index].bus == bus &&
671 drives_table[index].unit == unit &&
672 drives_table[index].used)
673 return index;
674
675 return -1;
676}
677
678int drive_get_max_bus(BlockInterfaceType type)
679{
680 int max_bus;
681 int index;
682
683 max_bus = -1;
684 for (index = 0; index < nb_drives; index++) {
685 if(drives_table[index].type == type &&
686 drives_table[index].bus > max_bus)
687 max_bus = drives_table[index].bus;
688 }
689 return max_bus;
690}
691
692const char *drive_get_serial(BlockDriverState *bdrv)
693{
694 int index;
695
696 for (index = 0; index < nb_drives; index++)
697 if (drives_table[index].bdrv == bdrv)
698 return drives_table[index].serial;
699
700 return "\0";
701}
702
703BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
704{
705 int index;
706
707 for (index = 0; index < nb_drives; index++)
708 if (drives_table[index].bdrv == bdrv)
709 return drives_table[index].onerror;
710
711 return BLOCK_ERR_STOP_ENOSPC;
712}
713
714static void bdrv_format_print(void *opaque, const char *name)
715{
716 fprintf(stderr, " %s", name);
717}
718
719void drive_uninit(BlockDriverState *bdrv)
720{
721 int i;
722
723 for (i = 0; i < MAX_DRIVES; i++)
724 if (drives_table[i].bdrv == bdrv) {
725 drives_table[i].bdrv = NULL;
726 drives_table[i].used = 0;
727 drive_remove(drives_table[i].drive_opt_idx);
728 nb_drives--;
729 break;
730 }
731}
732
733int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
734{
735 char buf[128];
736 char file[1024];
737 char devname[128];
738 char serial[21];
739 const char *mediastr = "";
740 BlockInterfaceType type;
741 enum { MEDIA_DISK, MEDIA_CDROM } media;
742 int bus_id, unit_id;
743 int cyls, heads, secs, translation;
744 BlockDriverState *bdrv;
745 BlockDriver *drv = NULL;
746 QEMUMachine *machine = opaque;
747 int max_devs;
748 int index;
749 int cache;
750 int bdrv_flags, onerror;
751 int drives_table_idx;
752 char *str = arg->opt;
753 static const char * const params[] = { "bus", "unit", "if", "index",
754 "cyls", "heads", "secs", "trans",
755 "media", "snapshot", "file",
756 "cache", "format", "serial", "werror",
757 NULL };
758
759 if (check_params(buf, sizeof(buf), params, str) < 0) {
760 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
761 buf, str);
762 return -1;
763 }
764
765 file[0] = 0;
766 cyls = heads = secs = 0;
767 bus_id = 0;
768 unit_id = -1;
769 translation = BIOS_ATA_TRANSLATION_AUTO;
770 index = -1;
771 cache = 3;
772
773 if (machine->use_scsi) {
774 type = IF_SCSI;
775 max_devs = MAX_SCSI_DEVS;
776 pstrcpy(devname, sizeof(devname), "scsi");
777 } else {
778 type = IF_IDE;
779 max_devs = MAX_IDE_DEVS;
780 pstrcpy(devname, sizeof(devname), "ide");
781 }
782 media = MEDIA_DISK;
783
784 /* extract parameters */
785
786 if (get_param_value(buf, sizeof(buf), "bus", str)) {
787 bus_id = strtol(buf, NULL, 0);
788 if (bus_id < 0) {
789 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
790 return -1;
791 }
792 }
793
794 if (get_param_value(buf, sizeof(buf), "unit", str)) {
795 unit_id = strtol(buf, NULL, 0);
796 if (unit_id < 0) {
797 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
798 return -1;
799 }
800 }
801
802 if (get_param_value(buf, sizeof(buf), "if", str)) {
803 pstrcpy(devname, sizeof(devname), buf);
804 if (!strcmp(buf, "ide")) {
805 type = IF_IDE;
806 max_devs = MAX_IDE_DEVS;
807 } else if (!strcmp(buf, "scsi")) {
808 type = IF_SCSI;
809 max_devs = MAX_SCSI_DEVS;
810 } else if (!strcmp(buf, "floppy")) {
811 type = IF_FLOPPY;
812 max_devs = 0;
813 } else if (!strcmp(buf, "pflash")) {
814 type = IF_PFLASH;
815 max_devs = 0;
816 } else if (!strcmp(buf, "mtd")) {
817 type = IF_MTD;
818 max_devs = 0;
819 } else if (!strcmp(buf, "sd")) {
820 type = IF_SD;
821 max_devs = 0;
822 } else if (!strcmp(buf, "virtio")) {
823 type = IF_VIRTIO;
824 max_devs = 0;
825 } else if (!strcmp(buf, "xen")) {
826 type = IF_XEN;
827 max_devs = 0;
828 } else {
829 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
830 return -1;
831 }
832 }
833
834 if (get_param_value(buf, sizeof(buf), "index", str)) {
835 index = strtol(buf, NULL, 0);
836 if (index < 0) {
837 fprintf(stderr, "qemu: '%s' invalid index\n", str);
838 return -1;
839 }
840 }
841
842 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
843 cyls = strtol(buf, NULL, 0);
844 }
845
846 if (get_param_value(buf, sizeof(buf), "heads", str)) {
847 heads = strtol(buf, NULL, 0);
848 }
849
850 if (get_param_value(buf, sizeof(buf), "secs", str)) {
851 secs = strtol(buf, NULL, 0);
852 }
853
854 if (cyls || heads || secs) {
855 if (cyls < 1 || cyls > 16383) {
856 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
857 return -1;
858 }
859 if (heads < 1 || heads > 16) {
860 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
861 return -1;
862 }
863 if (secs < 1 || secs > 63) {
864 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
865 return -1;
866 }
867 }
868
869 if (get_param_value(buf, sizeof(buf), "trans", str)) {
870 if (!cyls) {
871 fprintf(stderr,
872 "qemu: '%s' trans must be used with cyls,heads and secs\n",
873 str);
874 return -1;
875 }
876 if (!strcmp(buf, "none"))
877 translation = BIOS_ATA_TRANSLATION_NONE;
878 else if (!strcmp(buf, "lba"))
879 translation = BIOS_ATA_TRANSLATION_LBA;
880 else if (!strcmp(buf, "auto"))
881 translation = BIOS_ATA_TRANSLATION_AUTO;
882 else {
883 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
884 return -1;
885 }
886 }
887
888 if (get_param_value(buf, sizeof(buf), "media", str)) {
889 if (!strcmp(buf, "disk")) {
890 media = MEDIA_DISK;
891 } else if (!strcmp(buf, "cdrom")) {
892 if (cyls || secs || heads) {
893 fprintf(stderr,
894 "qemu: '%s' invalid physical CHS format\n", str);
895 return -1;
896 }
897 media = MEDIA_CDROM;
898 } else {
899 fprintf(stderr, "qemu: '%s' invalid media\n", str);
900 return -1;
901 }
902 }
903
904 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
905 if (!strcmp(buf, "on"))
906 snapshot = 1;
907 else if (!strcmp(buf, "off"))
908 snapshot = 0;
909 else {
910 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
911 return -1;
912 }
913 }
914
915 if (get_param_value(buf, sizeof(buf), "cache", str)) {
916 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
917 cache = 0;
918 else if (!strcmp(buf, "writethrough"))
919 cache = 1;
920 else if (!strcmp(buf, "writeback"))
921 cache = 2;
922 else {
923 fprintf(stderr, "qemu: invalid cache option\n");
924 return -1;
925 }
926 }
927
928 if (get_param_value(buf, sizeof(buf), "format", str)) {
929 if (strcmp(buf, "?") == 0) {
930 fprintf(stderr, "qemu: Supported formats:");
931 bdrv_iterate_format(bdrv_format_print, NULL);
932 fprintf(stderr, "\n");
933 return -1;
934 }
935 drv = bdrv_find_format(buf);
936 if (!drv) {
937 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
938 return -1;
939 }
940 }
941
942 if (arg->file == NULL)
943 get_param_value(file, sizeof(file), "file", str);
944 else
945 pstrcpy(file, sizeof(file), arg->file);
946
947 if (!get_param_value(serial, sizeof(serial), "serial", str))
948 memset(serial, 0, sizeof(serial));
949
950 onerror = BLOCK_ERR_STOP_ENOSPC;
951 if (get_param_value(buf, sizeof(serial), "werror", str)) {
952 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
953 fprintf(stderr, "werror is no supported by this format\n");
954 return -1;
955 }
956 if (!strcmp(buf, "ignore"))
957 onerror = BLOCK_ERR_IGNORE;
958 else if (!strcmp(buf, "enospc"))
959 onerror = BLOCK_ERR_STOP_ENOSPC;
960 else if (!strcmp(buf, "stop"))
961 onerror = BLOCK_ERR_STOP_ANY;
962 else if (!strcmp(buf, "report"))
963 onerror = BLOCK_ERR_REPORT;
964 else {
965 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
966 return -1;
967 }
968 }
969
970 /* compute bus and unit according index */
971
972 if (index != -1) {
973 if (bus_id != 0 || unit_id != -1) {
974 fprintf(stderr,
975 "qemu: '%s' index cannot be used with bus and unit\n", str);
976 return -1;
977 }
978 if (max_devs == 0)
979 {
980 unit_id = index;
981 bus_id = 0;
982 } else {
983 unit_id = index % max_devs;
984 bus_id = index / max_devs;
985 }
986 }
987
988 /* if user doesn't specify a unit_id,
989 * try to find the first free
990 */
991
992 if (unit_id == -1) {
993 unit_id = 0;
994 while (drive_get_index(type, bus_id, unit_id) != -1) {
995 unit_id++;
996 if (max_devs && unit_id >= max_devs) {
997 unit_id -= max_devs;
998 bus_id++;
999 }
1000 }
1001 }
1002
1003 /* check unit id */
1004
1005 if (max_devs && unit_id >= max_devs) {
1006 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1007 str, unit_id, max_devs - 1);
1008 return -1;
1009 }
1010
1011 /*
1012 * ignore multiple definitions
1013 */
1014
1015 if (drive_get_index(type, bus_id, unit_id) != -1)
1016 return -2;
1017
1018 /* init */
1019
1020 if (type == IF_IDE || type == IF_SCSI)
1021 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1022 if (max_devs)
1023 snprintf(buf, sizeof(buf), "%s%i%s%i",
1024 devname, bus_id, mediastr, unit_id);
1025 else
1026 snprintf(buf, sizeof(buf), "%s%s%i",
1027 devname, mediastr, unit_id);
1028 bdrv = bdrv_new(buf);
1029 drives_table_idx = drive_get_free_idx();
1030 drives_table[drives_table_idx].bdrv = bdrv;
1031 drives_table[drives_table_idx].type = type;
1032 drives_table[drives_table_idx].bus = bus_id;
1033 drives_table[drives_table_idx].unit = unit_id;
1034 drives_table[drives_table_idx].onerror = onerror;
1035 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1036 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1037 nb_drives++;
1038
1039 switch(type) {
1040 case IF_IDE:
1041 case IF_SCSI:
1042 case IF_XEN:
1043 switch(media) {
1044 case MEDIA_DISK:
1045 if (cyls != 0) {
1046 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1047 bdrv_set_translation_hint(bdrv, translation);
1048 }
1049 break;
1050 case MEDIA_CDROM:
1051 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1052 break;
1053 }
1054 break;
1055 case IF_SD:
1056 /* FIXME: This isn't really a floppy, but it's a reasonable
1057 approximation. */
1058 case IF_FLOPPY:
1059 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1060 break;
1061 case IF_PFLASH:
1062 case IF_MTD:
1063 case IF_VIRTIO:
1064 break;
1065 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001066 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001067 abort();
1068 }
1069 if (!file[0])
1070 return -2;
1071 bdrv_flags = 0;
1072 if (snapshot) {
1073 bdrv_flags |= BDRV_O_SNAPSHOT;
1074 cache = 2; /* always use write-back with snapshot */
1075 }
1076 if (cache == 0) /* no caching */
1077 bdrv_flags |= BDRV_O_NOCACHE;
1078 else if (cache == 2) /* write-back */
1079 bdrv_flags |= BDRV_O_CACHE_WB;
1080 else if (cache == 3) /* not specified */
1081 bdrv_flags |= BDRV_O_CACHE_DEF;
1082 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1083 fprintf(stderr, "qemu: could not open disk image %s\n",
1084 file);
1085 return -1;
1086 }
1087 if (bdrv_key_required(bdrv))
1088 autostart = 0;
1089 return drives_table_idx;
1090}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001091#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001092
1093static void numa_add(const char *optarg)
1094{
1095 char option[128];
1096 char *endptr;
1097 unsigned long long value, endvalue;
1098 int nodenr;
1099
1100 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1101 if (!strcmp(option, "node")) {
1102 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1103 nodenr = nb_numa_nodes;
1104 } else {
1105 nodenr = strtoull(option, NULL, 10);
1106 }
1107
1108 if (get_param_value(option, 128, "mem", optarg) == 0) {
1109 node_mem[nodenr] = 0;
1110 } else {
1111 value = strtoull(option, &endptr, 0);
1112 switch (*endptr) {
1113 case 0: case 'M': case 'm':
1114 value <<= 20;
1115 break;
1116 case 'G': case 'g':
1117 value <<= 30;
1118 break;
1119 }
1120 node_mem[nodenr] = value;
1121 }
1122 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1123 node_cpumask[nodenr] = 0;
1124 } else {
1125 value = strtoull(option, &endptr, 10);
1126 if (value >= 64) {
1127 value = 63;
1128 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1129 } else {
1130 if (*endptr == '-') {
1131 endvalue = strtoull(endptr+1, &endptr, 10);
1132 if (endvalue >= 63) {
1133 endvalue = 62;
1134 fprintf(stderr,
1135 "only 63 CPUs in NUMA mode supported.\n");
1136 }
1137 value = (1 << (endvalue + 1)) - (1 << value);
1138 } else {
1139 value = 1 << value;
1140 }
1141 }
1142 node_cpumask[nodenr] = value;
1143 }
1144 nb_numa_nodes++;
1145 }
1146 return;
1147}
1148
1149/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001150/* PCMCIA/Cardbus */
1151
1152static struct pcmcia_socket_entry_s {
1153 PCMCIASocket *socket;
1154 struct pcmcia_socket_entry_s *next;
1155} *pcmcia_sockets = 0;
1156
1157void pcmcia_socket_register(PCMCIASocket *socket)
1158{
1159 struct pcmcia_socket_entry_s *entry;
1160
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001161 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001162 entry->socket = socket;
1163 entry->next = pcmcia_sockets;
1164 pcmcia_sockets = entry;
1165}
1166
1167void pcmcia_socket_unregister(PCMCIASocket *socket)
1168{
1169 struct pcmcia_socket_entry_s *entry, **ptr;
1170
1171 ptr = &pcmcia_sockets;
1172 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1173 if (entry->socket == socket) {
1174 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001175 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001176 }
1177}
1178
1179void pcmcia_info(Monitor *mon)
1180{
1181 struct pcmcia_socket_entry_s *iter;
1182
1183 if (!pcmcia_sockets)
1184 monitor_printf(mon, "No PCMCIA sockets\n");
1185
1186 for (iter = pcmcia_sockets; iter; iter = iter->next)
1187 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1188 iter->socket->attached ? iter->socket->card_string :
1189 "Empty");
1190}
1191
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001192/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001193/* machine registration */
1194
1195static QEMUMachine *first_machine = NULL;
1196QEMUMachine *current_machine = NULL;
1197
1198int qemu_register_machine(QEMUMachine *m)
1199{
1200 QEMUMachine **pm;
1201 pm = &first_machine;
1202 while (*pm != NULL)
1203 pm = &(*pm)->next;
1204 m->next = NULL;
1205 *pm = m;
1206 return 0;
1207}
1208
1209static QEMUMachine *find_machine(const char *name)
1210{
1211 QEMUMachine *m;
1212
1213 for(m = first_machine; m != NULL; m = m->next) {
1214 if (!strcmp(m->name, name))
1215 return m;
1216 }
1217 return NULL;
1218}
1219
1220static QEMUMachine *find_default_machine(void)
1221{
1222 QEMUMachine *m;
1223
1224 for(m = first_machine; m != NULL; m = m->next) {
1225 if (m->is_default) {
1226 return m;
1227 }
1228 }
1229 return NULL;
1230}
1231
1232/***********************************************************/
1233/* main execution loop */
1234
1235static void gui_update(void *opaque)
1236{
1237 uint64_t interval = GUI_REFRESH_INTERVAL;
1238 DisplayState *ds = opaque;
1239 DisplayChangeListener *dcl = ds->listeners;
1240
1241 dpy_refresh(ds);
1242
1243 while (dcl != NULL) {
1244 if (dcl->gui_timer_interval &&
1245 dcl->gui_timer_interval < interval)
1246 interval = dcl->gui_timer_interval;
1247 dcl = dcl->next;
1248 }
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001249 timer_mod(ds->gui_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001250}
1251
1252static void nographic_update(void *opaque)
1253{
1254 uint64_t interval = GUI_REFRESH_INTERVAL;
1255
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01001256 timer_mod(nographic_timer, interval + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001257}
1258
1259struct vm_change_state_entry {
1260 VMChangeStateHandler *cb;
1261 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001262 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001263};
1264
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001265static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001266
1267VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1268 void *opaque)
1269{
1270 VMChangeStateEntry *e;
1271
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001272 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001273
1274 e->cb = cb;
1275 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001276 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001277 return e;
1278}
1279
1280void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1281{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001282 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001283 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001284}
1285
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001286void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001287{
1288 VMChangeStateEntry *e;
1289
1290 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1291 e->cb(e->opaque, running, reason);
1292 }
1293}
1294
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001295void vm_start(void)
1296{
1297 if (!vm_running) {
1298 cpu_enable_ticks();
1299 vm_running = 1;
1300 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001301 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001302 resume_all_vcpus();
1303 }
1304}
1305
1306/* reset/shutdown handler */
1307
1308typedef struct QEMUResetEntry {
1309 QEMUResetHandler *func;
1310 void *opaque;
1311 int order;
1312 struct QEMUResetEntry *next;
1313} QEMUResetEntry;
1314
1315static QEMUResetEntry *first_reset_entry;
1316static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001317static int shutdown_requested, shutdown_signal = -1;
1318static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001319static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001320int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001321static int vmstop_requested;
1322
1323int qemu_shutdown_requested(void)
1324{
1325 int r = shutdown_requested;
1326 shutdown_requested = 0;
1327 return r;
1328}
1329
1330int qemu_reset_requested(void)
1331{
1332 int r = reset_requested;
1333 reset_requested = 0;
1334 return r;
1335}
1336
1337int qemu_powerdown_requested(void)
1338{
1339 int r = powerdown_requested;
1340 powerdown_requested = 0;
1341 return r;
1342}
1343
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001344int qemu_debug_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001345{
1346 int r = debug_requested;
1347 debug_requested = 0;
1348 return r;
1349}
1350
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001351int qemu_vmstop_requested(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001352{
1353 int r = vmstop_requested;
1354 vmstop_requested = 0;
1355 return r;
1356}
1357
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001358void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1359{
1360 QEMUResetEntry **pre, *re;
1361
1362 pre = &first_reset_entry;
1363 while (*pre != NULL && (*pre)->order >= order) {
1364 pre = &(*pre)->next;
1365 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001366 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001367 re->func = func;
1368 re->opaque = opaque;
1369 re->order = order;
1370 re->next = NULL;
1371 *pre = re;
1372}
1373
1374void qemu_system_reset(void)
1375{
1376 QEMUResetEntry *re;
1377
1378 /* reset all devices */
1379 for(re = first_reset_entry; re != NULL; re = re->next) {
1380 re->func(re->opaque);
1381 }
1382}
1383
1384void qemu_system_reset_request(void)
1385{
1386 if (no_reboot) {
1387 shutdown_requested = 1;
1388 } else {
1389 reset_requested = 1;
1390 }
1391 qemu_notify_event();
1392}
1393
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001394void qemu_system_killed(int signal, pid_t pid)
1395{
1396 shutdown_signal = signal;
1397 shutdown_pid = pid;
1398 qemu_system_shutdown_request();
1399}
1400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001401void qemu_system_shutdown_request(void)
1402{
1403 shutdown_requested = 1;
1404 qemu_notify_event();
1405}
1406
1407void qemu_system_powerdown_request(void)
1408{
1409 powerdown_requested = 1;
1410 qemu_notify_event();
1411}
1412
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01001413int vm_can_run(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001414{
1415 if (powerdown_requested)
1416 return 0;
1417 if (reset_requested)
1418 return 0;
1419 if (shutdown_requested)
1420 return 0;
1421 if (debug_requested)
1422 return 0;
1423 return 1;
1424}
1425
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001426void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001427{
1428 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1429}
1430
1431void qemu_help(int exitcode)
1432{
1433 version();
1434 printf("usage: %s [options] [disk_image]\n"
1435 "\n"
1436 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1437 "\n"
1438#define DEF(option, opt_arg, opt_enum, opt_help) \
1439 opt_help
1440#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001441#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001442#undef DEF
1443#undef DEFHEADING
1444#undef GEN_DOCS
1445 "\n"
1446 "During emulation, the following keys are useful:\n"
1447 "ctrl-alt-f toggle full screen\n"
1448 "ctrl-alt-n switch to virtual console 'n'\n"
1449 "ctrl-alt toggle mouse and keyboard grab\n"
1450 "\n"
1451 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1452 ,
1453 "qemu",
1454 DEFAULT_RAM_SIZE,
1455#ifndef _WIN32
1456 DEFAULT_NETWORK_SCRIPT,
1457 DEFAULT_NETWORK_DOWN_SCRIPT,
1458#endif
1459 DEFAULT_GDBSTUB_PORT,
1460 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001461 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001462}
1463
1464#define HAS_ARG 0x0001
1465
1466enum {
1467#define DEF(option, opt_arg, opt_enum, opt_help) \
1468 opt_enum,
1469#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001470#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001471#undef DEF
1472#undef DEFHEADING
1473#undef GEN_DOCS
1474};
1475
1476typedef struct QEMUOption {
1477 const char *name;
1478 int flags;
1479 int index;
1480} QEMUOption;
1481
1482static const QEMUOption qemu_options[] = {
1483 { "h", 0, QEMU_OPTION_h },
1484#define DEF(option, opt_arg, opt_enum, opt_help) \
1485 { option, opt_arg, opt_enum },
1486#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001487#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001488#undef DEF
1489#undef DEFHEADING
1490#undef GEN_DOCS
1491 { NULL, 0, 0 },
1492};
1493
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001494static void select_vgahw (const char *p)
1495{
1496 const char *opts;
1497
1498 cirrus_vga_enabled = 0;
1499 std_vga_enabled = 0;
1500 vmsvga_enabled = 0;
1501 xenfb_enabled = 0;
1502 if (strstart(p, "std", &opts)) {
1503 std_vga_enabled = 1;
1504 } else if (strstart(p, "cirrus", &opts)) {
1505 cirrus_vga_enabled = 1;
1506 } else if (strstart(p, "vmware", &opts)) {
1507 vmsvga_enabled = 1;
1508 } else if (strstart(p, "xenfb", &opts)) {
1509 xenfb_enabled = 1;
1510 } else if (!strstart(p, "none", &opts)) {
1511 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07001512 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001513 }
1514 while (*opts) {
1515 const char *nextopt;
1516
1517 if (strstart(opts, ",retrace=", &nextopt)) {
1518 opts = nextopt;
1519 if (strstart(opts, "dumb", &nextopt))
1520 vga_retrace_method = VGA_RETRACE_DUMB;
1521 else if (strstart(opts, "precise", &nextopt))
1522 vga_retrace_method = VGA_RETRACE_PRECISE;
1523 else goto invalid_vga;
1524 } else goto invalid_vga;
1525 opts = nextopt;
1526 }
1527}
1528
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001529#define MAX_NET_CLIENTS 32
1530
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001531#ifdef _WIN32
1532/* Look for support files in the same directory as the executable. */
1533static char *find_datadir(const char *argv0)
1534{
1535 char *p;
1536 char buf[MAX_PATH];
1537 DWORD len;
1538
1539 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
1540 if (len == 0) {
1541 return NULL;
1542 }
1543
1544 buf[len] = 0;
1545 p = buf + len - 1;
1546 while (p != buf && *p != '\\')
1547 p--;
1548 *p = 0;
1549 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001550 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001551 }
1552 return NULL;
1553}
1554#else /* !_WIN32 */
1555
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001556/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001557static char *find_datadir(const char *argv0)
1558{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001559 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001560 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001561
1562#if defined(__linux__)
1563 {
1564 int len;
1565 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
1566 if (len > 0) {
1567 buf[len] = 0;
1568 p = buf;
1569 }
1570 }
1571#elif defined(__FreeBSD__)
1572 {
1573 int len;
1574 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
1575 if (len > 0) {
1576 buf[len] = 0;
1577 p = buf;
1578 }
1579 }
1580#endif
1581 /* If we don't have any way of figuring out the actual executable
1582 location then try argv[0]. */
1583 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001584 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001585 if (!p) {
1586 return NULL;
1587 }
1588 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001589
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001590 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001591}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001592#endif
1593
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001594static char*
1595qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
1596{
1597 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001598 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001599
1600 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1601 VERBOSE_PRINT(init," trying to find: %s\n", buf);
1602 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001603 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001604 return NULL;
1605 }
1606 return buf;
1607}
1608
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001609char *qemu_find_file(int type, const char *name)
1610{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001611 const char *subdir;
1612 char *buf;
1613
1614 /* If name contains path separators then try it as a straight path. */
1615 if ((strchr(name, '/') || strchr(name, '\\'))
1616 && access(name, R_OK) == 0) {
1617 return strdup(name);
1618 }
1619 switch (type) {
1620 case QEMU_FILE_TYPE_BIOS:
1621 subdir = "";
1622 break;
1623 case QEMU_FILE_TYPE_KEYMAP:
1624 subdir = "keymaps/";
1625 break;
1626 default:
1627 abort();
1628 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001629 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
1630#ifdef CONFIG_ANDROID
1631 if (type == QEMU_FILE_TYPE_BIOS) {
1632 /* This case corresponds to the emulator being used as part of an
1633 * SDK installation. NOTE: data_dir is really $bindir. */
1634 if (buf == NULL)
1635 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
1636 /* This case corresponds to platform builds. */
1637 if (buf == NULL)
1638 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
1639 /* Finally, try this for standalone builds under external/qemu */
1640 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08001641 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001642 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02001643#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001644 return buf;
1645}
1646
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001647static int
1648add_dns_server( const char* server_name )
1649{
1650 SockAddress addr;
1651
1652 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
1653 fprintf(stdout,
1654 "### WARNING: can't resolve DNS server name '%s'\n",
1655 server_name );
1656 return -1;
1657 }
1658
1659 fprintf(stderr,
1660 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
1661
1662 if ( slirp_add_dns_server( &addr ) < 0 ) {
1663 fprintf(stderr,
1664 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
1665 return -1;
1666 }
1667 return 0;
1668}
1669
rich cannings7339b552011-02-16 13:43:44 -08001670/* Parses an integer
1671 * Pararm:
1672 * str String containing a number to be parsed.
1673 * result Passes the parsed integer in this argument
1674 * returns 0 if ok, -1 if failed
1675 */
1676int
1677parse_int(const char *str, int *result)
1678{
1679 char* r;
1680 *result = strtol(str, &r, 0);
1681 if (r == NULL || *r != '\0')
1682 return -1;
1683
1684 return 0;
1685}
1686
rich cannings7339b552011-02-16 13:43:44 -08001687/* parses a null-terminated string specifying a network port (e.g., "80") or
1688 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
1689 * are the same. Returns 0 on success, -1 on error. */
1690
1691int parse_port_range(const char *str, unsigned short *lport,
1692 unsigned short *hport) {
1693
1694 unsigned int low = 0, high = 0;
1695 char *p, *arg = strdup(str);
1696
1697 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
1698 p = arg + 1; /* skip '[' */
1699 low = atoi(strtok(p, "-"));
1700 high = atoi(strtok(NULL, "-"));
1701 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
1702 *lport = low;
1703 *hport = high;
1704 }
1705 }
1706 else {
1707 low = atoi(arg);
1708 if ((0 < low) && (low < 65535)) {
1709 *lport = low;
1710 *hport = low;
1711 }
1712 }
1713 free(arg);
1714 if (low != 0)
1715 return 0;
1716 return -1;
1717}
1718
1719/*
1720 * Implements the generic port forwarding option
1721 */
1722void
1723net_slirp_forward(const char *optarg)
1724{
1725 /*
1726 * we expect the following format:
1727 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
1728 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
1729 */
1730 char *argument = strdup(optarg), *p = argument;
1731 char *dst_net, *dst_mask, *dst_port;
1732 char *redirect_ip, *redirect_port;
1733 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07001734 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08001735
1736
1737 dst_net = strtok(p, ":");
1738 dst_mask = strtok(NULL, ":");
1739 dst_port = strtok(NULL, ":");
1740 redirect_ip = strtok(NULL, ":");
1741 redirect_port = strtok(NULL, ":");
1742
1743 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
1744 redirect_ip == NULL || redirect_port == NULL) {
1745 fprintf(stderr,
1746 "Invalid argument for -net-forward, we expect "
1747 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
1748 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
1749 ":redirect_ip:redirect_port: %s\n",
1750 optarg);
1751 exit(1);
1752 }
1753
1754 /* inet_strtoip converts dotted address to host byte order */
1755 if (inet_strtoip(dst_net, &dnet) == -1) {
1756 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
1757 exit(1);
1758 }
1759 if (inet_strtoip(dst_mask, &dmask) == -1) {
1760 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
1761 exit(1);
1762 }
1763 if (inet_strtoip(redirect_ip, &rip) == -1) {
1764 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
1765 exit(1);
1766 }
1767
1768 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
1769 fprintf(stderr, "Invalid destination port or port range\n");
1770 exit(1);
1771 }
1772
1773 rport = atoi(redirect_port);
1774 if (!rport) {
1775 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
1776 exit(1);
1777 }
1778
1779 dnet &= dmask;
1780
1781 slirp_add_net_forward(dnet, dmask, dlport, dhport,
1782 rip, rport);
1783
1784 free(argument);
1785}
1786
1787
1788/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
1789 * entry in the allows list */
1790void
1791slirp_allow(const char *optarg, u_int8_t proto)
1792{
1793 /*
1794 * we expect the following format:
1795 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
1796 */
1797 char *argument = strdup(optarg), *p = argument;
1798 char *dst_ip_str, *dst_port_str;
1799 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07001800 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08001801
1802 dst_ip_str = strtok(p, ":");
1803 dst_port_str = strtok(NULL, ":");
1804
1805 if (dst_ip_str == NULL || dst_port_str == NULL) {
1806 fprintf(stderr,
1807 "Invalid argument %s for -allow. We expect "
1808 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
1809 optarg);
1810 exit(1);
1811 }
1812
1813 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
1814 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
1815 exit(1);
1816 }
1817 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
1818 fprintf(stderr, "Invalid destination port or port range\n");
1819 exit(1);
1820 }
1821
1822 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
1823
1824 free(argument);
1825}
1826
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01001827/* Add a serial device at a given location in the emulated hardware table.
1828 * On failure, this function aborts the program with an error message.
1829 */
1830static void
1831serial_hds_add_at(int index, const char* devname)
1832{
1833 char label[32];
1834
1835 if (!devname || !strcmp(devname,"none"))
1836 return;
1837
1838 if (index >= MAX_SERIAL_PORTS) {
1839 PANIC("qemu: invalid serial index for %s (%d >= %d)",
1840 devname, index, MAX_SERIAL_PORTS);
1841 }
1842 if (serial_hds[index] != NULL) {
1843 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
1844 devname, index);
1845 }
1846 snprintf(label, sizeof(label), "serial%d", index);
1847 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1848 if (!serial_hds[index]) {
1849 PANIC("qemu: could not open serial device '%s'", devname);
1850 }
1851}
1852
1853
1854/* Find a free slot in the emulated serial device table, and register
1855 * it. Return the allocated table index.
1856 */
1857static int
1858serial_hds_add(const char* devname)
1859{
1860 int index;
1861
1862 /* Find first free slot */
1863 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
1864 if (serial_hds[index] == NULL) {
1865 serial_hds_add_at(index, devname);
1866 return index;
1867 }
1868 }
1869
1870 PANIC("qemu: too many serial devices registered (%d)", index);
1871 return -1; /* shouldn't happen */
1872}
1873
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02001874
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02001875// Extract the partition type/format of a given partition image
1876// from the content of fstab.goldfish.
1877// |fstab| is the address of the fstab.goldfish data in memory.
1878// |fstabSize| is its size in bytes.
1879// |partitionName| is the name of the partition for debugging
1880// purposes (e.g. 'userdata').
1881// |partitionPath| is the partition path as it appears in the
1882// fstab file (e.g. '/data').
1883// On success, sets |*partitionType| to an appropriate value,
1884// on failure (i.e. |partitionPath| does not appear in the fstab
1885// file), leave the value untouched.
1886void android_extractPartitionFormat(const char* fstab,
1887 size_t fstabSize,
1888 const char* partitionName,
1889 const char* partitionPath,
1890 AndroidPartitionType* partitionType) {
1891 char* partFormat = NULL;
1892 if (!android_parseFstabPartitionFormat(fstab, fstabSize, partitionPath,
1893 &partFormat)) {
1894 VERBOSE_PRINT(init, "Could not extract format of %s partition!",
1895 partitionName);
1896 return;
1897 }
1898 VERBOSE_PRINT(init, "Found format of %s partition: '%s'",
1899 partitionName, partFormat);
1900 *partitionType = androidPartitionType_fromString(partFormat);
1901 free(partFormat);
1902}
1903
1904
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02001905// List of value describing how to handle partition images in
1906// android_nand_add_image() below, when no initiali partition image
1907// file is provided.
1908//
1909// MUST_EXIST means that the partition image must exist, otherwise
1910// dump an error message and exit.
1911//
1912// CREATE_IF_NEEDED means that if the partition image doesn't exist, an
1913// empty partition file should be created on demand.
1914//
1915// MUST_WIPE means that the partition image should be wiped cleaned,
1916// even if it exists. This is useful to implement the -wipe-data option.
1917typedef enum {
1918 ANDROID_PARTITION_OPEN_MODE_MUST_EXIST,
1919 ANDROID_PARTITION_OPEN_MODE_CREATE_IF_NEEDED,
1920 ANDROID_PARTITION_OPEN_MODE_MUST_WIPE,
1921} AndroidPartitionOpenMode;
1922
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02001923// Add a NAND partition image to the hardware configuration.
1924// |part_name| is a string indicating the type of partition, i.e. "system",
1925// "userdata" or "cache".
1926// |part_type| is an enum describing the type of partition. If it is
1927// DISK_PARTITION_TYPE_PROBE, then try to auto-detect the type directly
1928// from the content of |part_file| or |part_init_file|.
1929// |part_size| is the partition size in bytes.
1930// |part_file| is the partition file path, can be NULL if |path_init_file|
1931// is not NULL.
1932// |part_init_file| is an optional path to the initialization partition file.
1933// |is_ext4| is true if the partition is formatted as EXT4, false for YAFFS2.
1934//
1935// The NAND partition will be backed by |path_file|, except in the following
1936// cases:
1937// - |part_file| is NULL, or its value is "<temp>", indicating that a
1938// new temporary image file must be used instead.
1939//
1940// - |part_file| is not NULL, but the function fails to lock the file,
1941// indicating it's already used by another instance. A warning should
1942// be printed to warn the user, and a new temporary image should be
1943// used.
1944//
1945// If |part_file| is not NULL and can be locked, if the partition image does
1946// not exit, then the file must be created as an empty partition.
1947//
1948// When a new partition image is created, what happens depends on the
1949// value of |is_ext4|:
1950//
1951// - If |is_ext4| is false, a simple empty file is created, since that's
1952// enough to create an empty YAFFS2 partition.
1953//
1954// - If |is_ext4| is true, an "empty ext4" partition image is created
1955// instead, which will _not_ be backed by an empty file.
1956//
1957// If |part_init_file| is not NULL, its content will be used to erase
1958// the content of the main partition image. This is automatically handled
1959// by the NAND code though.
1960//
1961void android_nand_add_image(const char* part_name,
1962 AndroidPartitionType part_type,
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02001963 AndroidPartitionOpenMode part_mode,
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02001964 uint64_t part_size,
1965 const char* part_file,
1966 const char* part_init_file)
1967{
1968 char tmp[PATH_MAX * 2 + 32];
1969
1970 // Sanitize parameters, an empty string must be the same as NULL.
1971 if (part_file && !*part_file) {
1972 part_file = NULL;
1973 }
1974 if (part_init_file && !*part_init_file) {
1975 part_init_file = NULL;
1976 }
1977
1978 // Sanity checks.
1979 if (part_size == 0) {
1980 PANIC("Invalid %s partition size 0x%" PRIx64, part_size);
1981 }
1982
1983 if (part_init_file && !path_exists(part_init_file)) {
1984 PANIC("Missing initial %s image: %s", part_name, part_init_file);
1985 }
1986
1987 // As a special case, a |part_file| of '<temp>' means a temporary
1988 // partition is needed.
1989 if (part_file && !strcmp(part_file, "<temp>")) {
1990 part_file = NULL;
1991 }
1992
1993 // Verify partition type, or probe it if needed.
1994 {
1995 const char* image_file = NULL;
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02001996 if (part_file && path_exists(part_file)) {
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02001997 image_file = part_file;
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02001998 } else if (part_init_file) {
1999 image_file = part_init_file;
2000 } else if (part_type == ANDROID_PARTITION_TYPE_UNKNOWN) {
2001 PANIC("Cannot determine type of %s partition: no image files!",
2002 part_name);
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002003 }
2004
2005 if (part_type == ANDROID_PARTITION_TYPE_UNKNOWN) {
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002006 VERBOSE_PRINT(init, "Probing %s image file for partition type: %s",
2007 part_name, image_file);
2008
2009 part_type = androidPartitionType_probeFile(image_file);
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002010 } else {
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002011 // Probe the current image file to check that it is of the
2012 // right partition format.
2013 if (image_file) {
2014 AndroidPartitionType image_type =
2015 androidPartitionType_probeFile(image_file);
2016 if (image_type == ANDROID_PARTITION_TYPE_UNKNOWN) {
2017 PANIC("Cannot determine %s partition type of: %s",
2018 part_name,
2019 image_file);
2020 }
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002021
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002022 if (image_type != part_type) {
2023 PANIC("Invalid %s partition image type: %s (expected %s)",
2024 part_name,
2025 androidPartitionType_toString(image_type),
2026 androidPartitionType_toString(part_type));
2027 }
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002028 }
2029 }
2030 }
2031
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002032 VERBOSE_PRINT(init, "%s partition format: %s", part_name,
2033 androidPartitionType_toString(part_type));
2034
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002035 snprintf(tmp, sizeof tmp, "%s,size=0x%" PRIx64, part_name, part_size);
2036
2037 bool need_temp_partition = true;
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002038 bool need_make_empty =
2039 (part_mode == ANDROID_PARTITION_OPEN_MODE_MUST_WIPE);
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002040
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002041 if (part_file) {
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002042 if (filelock_create(part_file) == NULL) {
2043 fprintf(stderr,
2044 "WARNING: %s image already in use, changes will not persist!\n",
2045 part_name);
2046 } else {
2047 need_temp_partition = false;
2048
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002049 // If the partition image is missing, create it.
2050 if (!path_exists(part_file)) {
2051 if (part_mode == ANDROID_PARTITION_OPEN_MODE_MUST_EXIST) {
2052 PANIC("Missing %s partition image: %s", part_name,
2053 part_file);
2054 }
2055 if (path_empty_file(part_file) < 0) {
2056 PANIC("Cannot create %s image file at %s: %s",
2057 part_name,
2058 part_file,
2059 strerror(errno));
2060 }
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002061 need_make_empty = true;
2062 }
2063 }
2064 }
2065
2066 // Do we need a temporary partition image ?
2067 if (need_temp_partition) {
2068 TempFile* temp_file = tempfile_create();
2069 if (temp_file == NULL) {
2070 PANIC("Could not create temp file for %s partition image: %s\n",
2071 part_name);
2072 }
2073 part_file = tempfile_path(temp_file);
2074 VERBOSE_PRINT(init,
2075 "Mapping '%s' partition image to %s",
2076 part_name,
2077 part_file);
2078
2079 need_make_empty = true;
2080 }
2081
2082 pstrcat(tmp, sizeof tmp, ",file=");
2083 pstrcat(tmp, sizeof tmp, part_file);
2084
2085 // Do we need to make the partition image empty?
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002086 // Do not do it if there is an initial file though since it will
2087 // get copied directly by the NAND code into the image.
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002088 if (need_make_empty && !part_init_file) {
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02002089 VERBOSE_PRINT(init,
2090 "Creating empty %s partition image at: %s",
2091 part_name,
2092 part_file);
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002093 int ret = androidPartitionType_makeEmptyFile(part_type,
2094 part_size,
2095 part_file);
2096 if (ret < 0) {
2097 PANIC("Could not create %s image file at %s: %s",
2098 part_name,
2099 part_file,
2100 strerror(-ret));
2101 }
2102 }
2103
2104 if (part_init_file) {
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002105 pstrcat(tmp, sizeof tmp, ",initfile=");
2106 pstrcat(tmp, sizeof tmp, part_init_file);
2107 }
2108
2109 if (part_type == ANDROID_PARTITION_TYPE_EXT4) {
2110 // Using a nand device to approximate a block device until full
2111 // support is added.
2112 pstrcat(tmp, sizeof tmp,",pagesize=512,extrasize=0");
2113 }
2114
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02002115 nand_add_dev(tmp);
2116}
2117
2118
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002119int main(int argc, char **argv, char **envp)
2120{
2121 const char *gdbstub_dev = NULL;
2122 uint32_t boot_devices_bitmap = 0;
2123 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002124 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002125 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002126 const char *initrd_filename;
2127 const char *kernel_filename, *kernel_cmdline;
2128 const char *boot_devices = "";
2129 DisplayState *ds;
2130 DisplayChangeListener *dcl;
2131 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002132 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002133 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002134 const char *net_clients[MAX_NET_CLIENTS];
2135 int nb_net_clients;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002136 int optind;
2137 const char *r, *optarg;
2138 CharDriverState *monitor_hd = NULL;
2139 const char *monitor_device;
2140 const char *serial_devices[MAX_SERIAL_PORTS];
2141 int serial_device_index;
2142 const char *parallel_devices[MAX_PARALLEL_PORTS];
2143 int parallel_device_index;
2144 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2145 int virtio_console_index;
2146 const char *loadvm = NULL;
2147 QEMUMachine *machine;
2148 const char *cpu_model;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002149 int tb_size;
2150 const char *pid_file = NULL;
2151 const char *incoming = NULL;
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002152 const char* log_mask = NULL;
2153 const char* log_file = NULL;
David 'Digit' Turner66576782014-03-24 16:57:57 +01002154 CPUState *cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002155 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002156 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002157 STRALLOC_DEFINE(kernel_params);
2158 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002159 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002160
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002161 /* Initialize sockets before anything else, so we can properly report
2162 * initialization failures back to the UI. */
2163#ifdef _WIN32
2164 socket_init();
2165#endif
2166
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002167 init_clocks();
2168
David 'Digit' Turner8848f632014-01-16 18:10:21 +01002169 qemu_cache_utils_init();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002170
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002171 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002172 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002173
2174 module_call_init(MODULE_INIT_MACHINE);
2175 machine = find_default_machine();
2176 cpu_model = NULL;
2177 initrd_filename = NULL;
2178 ram_size = 0;
2179 snapshot = 0;
2180 kernel_filename = NULL;
2181 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002182
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002183 cyls = heads = secs = 0;
2184 translation = BIOS_ATA_TRANSLATION_AUTO;
2185 monitor_device = "vc:80Cx24C";
2186
2187 serial_devices[0] = "vc:80Cx24C";
2188 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2189 serial_devices[i] = NULL;
2190 serial_device_index = 0;
2191
2192 parallel_devices[0] = "vc:80Cx24C";
2193 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2194 parallel_devices[i] = NULL;
2195 parallel_device_index = 0;
2196
2197 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2198 virtio_consoles[i] = NULL;
2199 virtio_console_index = 0;
2200
2201 for (i = 0; i < MAX_NODES; i++) {
2202 node_mem[i] = 0;
2203 node_cpumask[i] = 0;
2204 }
2205
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002206 nb_net_clients = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002207#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002208 nb_drives = 0;
2209 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002210#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002211 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002212
2213 nb_nics = 0;
2214
2215 tb_size = 0;
2216 autostart= 1;
2217
2218 register_watchdogs();
2219
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002220 /* Initialize boot properties. */
2221 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002222 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002223 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002224
David 'Digit' Turner36597752011-05-20 01:18:01 +02002225#ifdef CONFIG_KVM
2226 /* By default, force auto-detection for kvm */
2227 kvm_allowed = -1;
2228#endif
2229
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002230 optind = 1;
2231 for(;;) {
2232 if (optind >= argc)
2233 break;
2234 r = argv[optind];
2235 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002236 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002237 } else {
2238 const QEMUOption *popt;
2239
2240 optind++;
2241 /* Treat --foo the same as -foo. */
2242 if (r[1] == '-')
2243 r++;
2244 popt = qemu_options;
2245 for(;;) {
2246 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002247 PANIC("%s: invalid option -- '%s'",
2248 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002249 }
2250 if (!strcmp(popt->name, r + 1))
2251 break;
2252 popt++;
2253 }
2254 if (popt->flags & HAS_ARG) {
2255 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002256 PANIC("%s: option '%s' requires an argument",
2257 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002258 }
2259 optarg = argv[optind++];
2260 } else {
2261 optarg = NULL;
2262 }
2263
2264 switch(popt->index) {
2265 case QEMU_OPTION_M:
2266 machine = find_machine(optarg);
2267 if (!machine) {
2268 QEMUMachine *m;
2269 printf("Supported machines are:\n");
2270 for(m = first_machine; m != NULL; m = m->next) {
2271 printf("%-10s %s%s\n",
2272 m->name, m->desc,
2273 m->is_default ? " (default)" : "");
2274 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002275 if (*optarg != '?') {
2276 PANIC("Invalid machine parameter: %s",
2277 optarg);
2278 } else {
2279 QEMU_EXIT(0);
2280 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002281 }
2282 break;
2283 case QEMU_OPTION_cpu:
2284 /* hw initialization will check this */
2285 if (*optarg == '?') {
2286/* XXX: implement xxx_cpu_list for targets that still miss it */
2287#if defined(cpu_list)
2288 cpu_list(stdout, &fprintf);
2289#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002290 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002291 } else {
2292 cpu_model = optarg;
2293 }
2294 break;
2295 case QEMU_OPTION_initrd:
2296 initrd_filename = optarg;
2297 break;
2298 case QEMU_OPTION_hda:
2299 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002300 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002301 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002302 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002303 ",cyls=%d,heads=%d,secs=%d%s",
2304 0, cyls, heads, secs,
2305 translation == BIOS_ATA_TRANSLATION_LBA ?
2306 ",trans=lba" :
2307 translation == BIOS_ATA_TRANSLATION_NONE ?
2308 ",trans=none" : "");
2309 break;
2310 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002311 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2312 break;
2313
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002314 case QEMU_OPTION_hdc:
2315 case QEMU_OPTION_hdd:
2316 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2317 break;
2318 case QEMU_OPTION_drive:
2319 drive_add(NULL, "%s", optarg);
2320 break;
2321 case QEMU_OPTION_mtdblock:
2322 drive_add(optarg, MTD_ALIAS);
2323 break;
2324 case QEMU_OPTION_sd:
2325 drive_add(optarg, SD_ALIAS);
2326 break;
2327 case QEMU_OPTION_pflash:
2328 drive_add(optarg, PFLASH_ALIAS);
2329 break;
2330 case QEMU_OPTION_snapshot:
2331 snapshot = 1;
2332 break;
2333 case QEMU_OPTION_hdachs:
2334 {
2335 const char *p;
2336 p = optarg;
2337 cyls = strtol(p, (char **)&p, 0);
2338 if (cyls < 1 || cyls > 16383)
2339 goto chs_fail;
2340 if (*p != ',')
2341 goto chs_fail;
2342 p++;
2343 heads = strtol(p, (char **)&p, 0);
2344 if (heads < 1 || heads > 16)
2345 goto chs_fail;
2346 if (*p != ',')
2347 goto chs_fail;
2348 p++;
2349 secs = strtol(p, (char **)&p, 0);
2350 if (secs < 1 || secs > 63)
2351 goto chs_fail;
2352 if (*p == ',') {
2353 p++;
2354 if (!strcmp(p, "none"))
2355 translation = BIOS_ATA_TRANSLATION_NONE;
2356 else if (!strcmp(p, "lba"))
2357 translation = BIOS_ATA_TRANSLATION_LBA;
2358 else if (!strcmp(p, "auto"))
2359 translation = BIOS_ATA_TRANSLATION_AUTO;
2360 else
2361 goto chs_fail;
2362 } else if (*p != '\0') {
2363 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002364 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002365 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002366 if (hda_opts != NULL) {
2367 char num[16];
2368 snprintf(num, sizeof(num), "%d", cyls);
2369 qemu_opt_set(hda_opts, "cyls", num);
2370 snprintf(num, sizeof(num), "%d", heads);
2371 qemu_opt_set(hda_opts, "heads", num);
2372 snprintf(num, sizeof(num), "%d", secs);
2373 qemu_opt_set(hda_opts, "secs", num);
2374 if (translation == BIOS_ATA_TRANSLATION_LBA)
2375 qemu_opt_set(hda_opts, "trans", "lba");
2376 if (translation == BIOS_ATA_TRANSLATION_NONE)
2377 qemu_opt_set(hda_opts, "trans", "none");
2378 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002379 }
2380 break;
2381 case QEMU_OPTION_numa:
2382 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002383 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002384 }
2385 numa_add(optarg);
2386 break;
2387 case QEMU_OPTION_nographic:
2388 display_type = DT_NOGRAPHIC;
2389 break;
2390#ifdef CONFIG_CURSES
2391 case QEMU_OPTION_curses:
2392 display_type = DT_CURSES;
2393 break;
2394#endif
2395 case QEMU_OPTION_portrait:
2396 graphic_rotate = 1;
2397 break;
2398 case QEMU_OPTION_kernel:
2399 kernel_filename = optarg;
2400 break;
2401 case QEMU_OPTION_append:
2402 kernel_cmdline = optarg;
2403 break;
2404 case QEMU_OPTION_cdrom:
2405 drive_add(optarg, CDROM_ALIAS);
2406 break;
2407 case QEMU_OPTION_boot:
2408 boot_devices = optarg;
2409 /* We just do some generic consistency checks */
2410 {
2411 /* Could easily be extended to 64 devices if needed */
2412 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002413
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002414 boot_devices_bitmap = 0;
2415 for (p = boot_devices; *p != '\0'; p++) {
2416 /* Allowed boot devices are:
2417 * a b : floppy disk drives
2418 * c ... f : IDE disk drives
2419 * g ... m : machine implementation dependant drives
2420 * n ... p : network devices
2421 * It's up to each machine implementation to check
2422 * if the given boot devices match the actual hardware
2423 * implementation and firmware features.
2424 */
2425 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002426 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002427 }
2428 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002429 PANIC(
2430 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002431 }
2432 boot_devices_bitmap |= 1 << (*p - 'a');
2433 }
2434 }
2435 break;
2436 case QEMU_OPTION_fda:
2437 case QEMU_OPTION_fdb:
2438 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2439 break;
2440#ifdef TARGET_I386
2441 case QEMU_OPTION_no_fd_bootchk:
2442 fd_bootchk = 0;
2443 break;
2444#endif
2445 case QEMU_OPTION_net:
2446 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002447 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002448 }
2449 net_clients[nb_net_clients] = optarg;
2450 nb_net_clients++;
2451 break;
2452#ifdef CONFIG_SLIRP
2453 case QEMU_OPTION_tftp:
2454 tftp_prefix = optarg;
2455 break;
2456 case QEMU_OPTION_bootp:
2457 bootp_filename = optarg;
2458 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002459 case QEMU_OPTION_redir:
2460 net_slirp_redir(NULL, optarg, NULL);
2461 break;
2462#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002463#ifdef HAS_AUDIO
2464 case QEMU_OPTION_audio_help:
2465 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002466 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002467 break;
2468 case QEMU_OPTION_soundhw:
2469 select_soundhw (optarg);
2470 break;
2471#endif
2472 case QEMU_OPTION_h:
2473 qemu_help(0);
2474 break;
2475 case QEMU_OPTION_version:
2476 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002477 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002478 break;
2479 case QEMU_OPTION_m: {
2480 uint64_t value;
2481 char *ptr;
2482
2483 value = strtoul(optarg, &ptr, 10);
2484 switch (*ptr) {
2485 case 0: case 'M': case 'm':
2486 value <<= 20;
2487 break;
2488 case 'G': case 'g':
2489 value <<= 30;
2490 break;
2491 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002492 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002493 }
2494
2495 /* On 32-bit hosts, QEMU is limited by virtual address space */
David 'Digit' Turner81911b02014-02-10 17:53:45 +01002496 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002497 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002498 }
2499 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002500 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002501 }
2502 ram_size = value;
2503 break;
2504 }
2505 case QEMU_OPTION_d:
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002506 log_mask = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002507 break;
2508 case QEMU_OPTION_s:
2509 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2510 break;
2511 case QEMU_OPTION_gdb:
2512 gdbstub_dev = optarg;
2513 break;
2514 case QEMU_OPTION_L:
2515 data_dir = optarg;
2516 break;
2517 case QEMU_OPTION_bios:
2518 bios_name = optarg;
2519 break;
2520 case QEMU_OPTION_singlestep:
2521 singlestep = 1;
2522 break;
2523 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002524 autostart = 0;
2525 break;
2526#ifndef _WIN32
2527 case QEMU_OPTION_k:
2528 keyboard_layout = optarg;
2529 break;
2530#endif
2531 case QEMU_OPTION_localtime:
2532 rtc_utc = 0;
2533 break;
2534 case QEMU_OPTION_vga:
2535 select_vgahw (optarg);
2536 break;
2537#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2538 case QEMU_OPTION_g:
2539 {
2540 const char *p;
2541 int w, h, depth;
2542 p = optarg;
2543 w = strtol(p, (char **)&p, 10);
2544 if (w <= 0) {
2545 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002546 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002547 }
2548 if (*p != 'x')
2549 goto graphic_error;
2550 p++;
2551 h = strtol(p, (char **)&p, 10);
2552 if (h <= 0)
2553 goto graphic_error;
2554 if (*p == 'x') {
2555 p++;
2556 depth = strtol(p, (char **)&p, 10);
2557 if (depth != 8 && depth != 15 && depth != 16 &&
2558 depth != 24 && depth != 32)
2559 goto graphic_error;
2560 } else if (*p == '\0') {
2561 depth = graphic_depth;
2562 } else {
2563 goto graphic_error;
2564 }
2565
2566 graphic_width = w;
2567 graphic_height = h;
2568 graphic_depth = depth;
2569 }
2570 break;
2571#endif
2572 case QEMU_OPTION_echr:
2573 {
2574 char *r;
2575 term_escape_char = strtol(optarg, &r, 0);
2576 if (r == optarg)
2577 printf("Bad argument to echr\n");
2578 break;
2579 }
2580 case QEMU_OPTION_monitor:
2581 monitor_device = optarg;
2582 break;
2583 case QEMU_OPTION_serial:
2584 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002585 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002586 }
2587 serial_devices[serial_device_index] = optarg;
2588 serial_device_index++;
2589 break;
2590 case QEMU_OPTION_watchdog:
2591 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002592 if (i > 0) {
2593 if (i == 1) {
2594 PANIC("Invalid watchdog parameter: %s",
2595 optarg);
2596 } else {
2597 QEMU_EXIT(0);
2598 }
2599 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002600 break;
2601 case QEMU_OPTION_watchdog_action:
2602 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002603 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002604 }
2605 break;
2606 case QEMU_OPTION_virtiocon:
2607 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002608 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002609 }
2610 virtio_consoles[virtio_console_index] = optarg;
2611 virtio_console_index++;
2612 break;
2613 case QEMU_OPTION_parallel:
2614 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002615 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002616 }
2617 parallel_devices[parallel_device_index] = optarg;
2618 parallel_device_index++;
2619 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002620 case QEMU_OPTION_loadvm:
2621 loadvm = optarg;
2622 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002623 case QEMU_OPTION_savevm_on_exit:
2624 savevm_on_exit = optarg;
2625 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002626 case QEMU_OPTION_full_screen:
2627 full_screen = 1;
2628 break;
2629#ifdef CONFIG_SDL
2630 case QEMU_OPTION_no_frame:
2631 no_frame = 1;
2632 break;
2633 case QEMU_OPTION_alt_grab:
2634 alt_grab = 1;
2635 break;
2636 case QEMU_OPTION_no_quit:
2637 no_quit = 1;
2638 break;
2639 case QEMU_OPTION_sdl:
2640 display_type = DT_SDL;
2641 break;
2642#endif
2643 case QEMU_OPTION_pidfile:
2644 pid_file = optarg;
2645 break;
2646#ifdef TARGET_I386
2647 case QEMU_OPTION_win2k_hack:
2648 win2k_install_hack = 1;
2649 break;
2650 case QEMU_OPTION_rtc_td_hack:
2651 rtc_td_hack = 1;
2652 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002653#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002654 case QEMU_OPTION_acpitable:
2655 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002656 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002657 }
2658 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08002659#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002660 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02002661 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002662 break;
2663#endif
2664#ifdef CONFIG_KVM
2665 case QEMU_OPTION_enable_kvm:
2666 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002667 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02002668 case QEMU_OPTION_disable_kvm:
2669 kvm_allowed = 0;
2670 break;
2671#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002672 case QEMU_OPTION_smp:
2673 smp_cpus = atoi(optarg);
2674 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002675 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002676 }
2677 break;
2678 case QEMU_OPTION_vnc:
2679 display_type = DT_VNC;
2680 vnc_display = optarg;
2681 break;
2682#ifdef TARGET_I386
2683 case QEMU_OPTION_no_acpi:
2684 acpi_enabled = 0;
2685 break;
2686 case QEMU_OPTION_no_hpet:
2687 no_hpet = 1;
2688 break;
2689 case QEMU_OPTION_no_virtio_balloon:
2690 no_virtio_balloon = 1;
2691 break;
2692#endif
2693 case QEMU_OPTION_no_reboot:
2694 no_reboot = 1;
2695 break;
2696 case QEMU_OPTION_no_shutdown:
2697 no_shutdown = 1;
2698 break;
2699 case QEMU_OPTION_show_cursor:
2700 cursor_hide = 0;
2701 break;
2702 case QEMU_OPTION_uuid:
2703 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002704 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002705 }
2706 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002707 case QEMU_OPTION_option_rom:
2708 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002709 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002710 }
2711 option_rom[nb_option_roms] = optarg;
2712 nb_option_roms++;
2713 break;
2714#if defined(TARGET_ARM) || defined(TARGET_M68K)
2715 case QEMU_OPTION_semihosting:
2716 semihosting_enabled = 1;
2717 break;
2718#endif
2719 case QEMU_OPTION_name:
2720 qemu_name = optarg;
2721 break;
2722#if defined(TARGET_SPARC) || defined(TARGET_PPC)
2723 case QEMU_OPTION_prom_env:
2724 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002725 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002726 }
2727 prom_envs[nb_prom_envs] = optarg;
2728 nb_prom_envs++;
2729 break;
2730#endif
2731#ifdef TARGET_ARM
2732 case QEMU_OPTION_old_param:
2733 old_param = 1;
2734 break;
2735#endif
2736 case QEMU_OPTION_clock:
2737 configure_alarms(optarg);
2738 break;
2739 case QEMU_OPTION_startdate:
2740 {
2741 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02002742 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002743 if (!strcmp(optarg, "now")) {
2744 rtc_date_offset = -1;
2745 } else {
2746 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
2747 &tm.tm_year,
2748 &tm.tm_mon,
2749 &tm.tm_mday,
2750 &tm.tm_hour,
2751 &tm.tm_min,
2752 &tm.tm_sec) == 6) {
2753 /* OK */
2754 } else if (sscanf(optarg, "%d-%d-%d",
2755 &tm.tm_year,
2756 &tm.tm_mon,
2757 &tm.tm_mday) == 3) {
2758 tm.tm_hour = 0;
2759 tm.tm_min = 0;
2760 tm.tm_sec = 0;
2761 } else {
2762 goto date_fail;
2763 }
2764 tm.tm_year -= 1900;
2765 tm.tm_mon--;
2766 rtc_start_date = mktimegm(&tm);
2767 if (rtc_start_date == -1) {
2768 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002769 PANIC("Invalid date format. Valid format are:\n"
2770 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002771 }
2772 rtc_date_offset = time(NULL) - rtc_start_date;
2773 }
2774 }
2775 break;
rich cannings7339b552011-02-16 13:43:44 -08002776
2777 /* -------------------------------------------------------*/
2778 /* User mode network stack restrictions */
2779 case QEMU_OPTION_drop_udp:
2780 slirp_drop_udp();
2781 break;
2782 case QEMU_OPTION_drop_tcp:
2783 slirp_drop_tcp();
2784 break;
2785 case QEMU_OPTION_allow_tcp:
2786 slirp_allow(optarg, IPPROTO_TCP);
2787 break;
2788 case QEMU_OPTION_allow_udp:
2789 slirp_allow(optarg, IPPROTO_UDP);
2790 break;
2791 case QEMU_OPTION_drop_log:
2792 {
2793 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002794 drop_log_filename = optarg;
2795 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08002796
2797 if (!drop_log_fd) {
2798 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
2799 exit(1);
2800 }
2801
2802 slirp_drop_log_fd(drop_log_fd);
2803 }
2804 break;
2805
2806 case QEMU_OPTION_dns_log:
2807 {
2808 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08002809 dns_log_filename = optarg;
2810 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08002811
2812 if (dns_log_fd == NULL) {
2813 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
2814 exit(1);
2815 }
2816
2817 slirp_dns_log_fd(dns_log_fd);
2818 }
2819 break;
2820
2821
2822 case QEMU_OPTION_max_dns_conns:
2823 {
2824 int max_dns_conns = 0;
2825 if (parse_int(optarg, &max_dns_conns)) {
2826 fprintf(stderr,
2827 "qemu: syntax: -max-dns-conns max_connections\n");
2828 exit(1);
2829 }
2830 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
2831 fprintf(stderr,
2832 "Invalid arg for max dns connections: %s\n",
2833 optarg);
2834 exit(1);
2835 }
2836 slirp_set_max_dns_conns(max_dns_conns);
2837 }
2838 break;
2839
2840 case QEMU_OPTION_net_forward:
2841 net_slirp_forward(optarg);
2842 break;
2843 case QEMU_OPTION_net_forward_tcp2sink:
2844 {
2845 SockAddress saddr;
2846
2847 if (parse_host_port(&saddr, optarg)) {
2848 fprintf(stderr,
2849 "Invalid ip/port %s for "
2850 "-forward-dropped-tcp2sink. "
2851 "We expect 'sink_ip:sink_port'\n",
2852 optarg);
2853 exit(1);
2854 }
2855 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
2856 saddr.u.inet.port);
2857 }
2858 break;
2859 /* -------------------------------------------------------*/
2860
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002861 case QEMU_OPTION_tb_size:
2862 tb_size = strtol(optarg, NULL, 0);
2863 if (tb_size < 0)
2864 tb_size = 0;
2865 break;
2866 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02002867 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002868 break;
2869 case QEMU_OPTION_incoming:
2870 incoming = optarg;
2871 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002872#ifdef CONFIG_XEN
2873 case QEMU_OPTION_xen_domid:
2874 xen_domid = atoi(optarg);
2875 break;
2876 case QEMU_OPTION_xen_create:
2877 xen_mode = XEN_CREATE;
2878 break;
2879 case QEMU_OPTION_xen_attach:
2880 xen_mode = XEN_ATTACH;
2881 break;
2882#endif
2883
2884
2885 case QEMU_OPTION_mic:
2886 audio_input_source = (char*)optarg;
2887 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002888#ifdef CONFIG_NAND
2889 case QEMU_OPTION_nand:
2890 nand_add_dev(optarg);
2891 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08002892
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002893#endif
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +02002894#ifdef CONFIG_HAX
2895 case QEMU_OPTION_enable_hax:
2896 hax_disabled = 0;
2897 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08002898 case QEMU_OPTION_disable_hax:
2899 hax_disabled = 1;
2900 break;
David 'Digit' Turner045bdbe2014-04-28 22:26:52 +02002901#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002902 case QEMU_OPTION_android_ports:
2903 android_op_ports = (char*)optarg;
2904 break;
2905
2906 case QEMU_OPTION_android_port:
2907 android_op_port = (char*)optarg;
2908 break;
2909
2910 case QEMU_OPTION_android_report_console:
2911 android_op_report_console = (char*)optarg;
2912 break;
2913
2914 case QEMU_OPTION_http_proxy:
2915 op_http_proxy = (char*)optarg;
2916 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07002917
2918 case QEMU_OPTION_charmap:
2919 op_charmap_file = (char*)optarg;
2920 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07002921
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002922 case QEMU_OPTION_android_hw:
2923 android_op_hwini = (char*)optarg;
2924 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07002925
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002926 case QEMU_OPTION_dns_server:
2927 android_op_dns_server = (char*)optarg;
2928 break;
2929
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07002930 case QEMU_OPTION_radio:
2931 android_op_radio = (char*)optarg;
2932 break;
2933
2934 case QEMU_OPTION_gps:
2935 android_op_gps = (char*)optarg;
2936 break;
2937
2938 case QEMU_OPTION_audio:
2939 android_op_audio = (char*)optarg;
2940 break;
2941
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07002942 case QEMU_OPTION_cpu_delay:
2943 android_op_cpu_delay = (char*)optarg;
2944 break;
2945
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07002946 case QEMU_OPTION_show_kernel:
2947 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
2948 break;
2949
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07002950#ifdef CONFIG_NAND_LIMITS
2951 case QEMU_OPTION_nand_limits:
2952 android_op_nand_limits = (char*)optarg;
2953 break;
2954#endif // CONFIG_NAND_LIMITS
2955
2956 case QEMU_OPTION_netspeed:
2957 android_op_netspeed = (char*)optarg;
2958 break;
2959
2960 case QEMU_OPTION_netdelay:
2961 android_op_netdelay = (char*)optarg;
2962 break;
2963
2964 case QEMU_OPTION_netfast:
2965 android_op_netfast = 1;
2966 break;
2967
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07002968 case QEMU_OPTION_tcpdump:
2969 android_op_tcpdump = (char*)optarg;
2970 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07002971
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002972 case QEMU_OPTION_boot_property:
2973 boot_property_parse_option((char*)optarg);
2974 break;
2975
2976 case QEMU_OPTION_lcd_density:
2977 android_op_lcd_density = (char*)optarg;
2978 break;
2979
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002980 case QEMU_OPTION_ui_port:
2981 android_op_ui_port = (char*)optarg;
2982 break;
2983
2984 case QEMU_OPTION_ui_settings:
2985 android_op_ui_settings = (char*)optarg;
2986 break;
2987
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01002988 case QEMU_OPTION_audio_test_out:
2989 android_audio_test_start_out();
2990 break;
2991
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08002992 case QEMU_OPTION_android_avdname:
2993 android_op_avd_name = (char*)optarg;
2994 break;
2995
2996 case QEMU_OPTION_timezone:
2997 if (timezone_set((char*)optarg)) {
2998 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
2999 (char*)optarg);
3000 }
3001 break;
3002
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01003003#ifdef CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003004 case QEMU_OPTION_android_memcheck:
3005 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003006 /* This will set ro.kernel.memcheck system property
3007 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003008 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003009 break;
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01003010#endif // CONFIG_ANDROID_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003011
3012 case QEMU_OPTION_snapshot_no_time_update:
3013 android_snapshot_update_time = 0;
3014 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003015
3016 case QEMU_OPTION_list_webcam:
3017 android_list_web_cameras();
3018 exit(0);
3019
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003020 default:
3021 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003022 }
3023 }
3024 }
3025
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003026 /* Initialize character map. */
3027 if (android_charmap_setup(op_charmap_file)) {
3028 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003029 PANIC(
3030 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003031 op_charmap_file);
3032 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003033 PANIC(
3034 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003035 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003036 }
3037
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003038 /* If no data_dir is specified then try to find it relative to the
3039 executable path. */
3040 if (!data_dir) {
3041 data_dir = find_datadir(argv[0]);
3042 }
3043 /* If all else fails use the install patch specified when building. */
3044 if (!data_dir) {
3045 data_dir = CONFIG_QEMU_SHAREDIR;
3046 }
3047
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003048 if (!android_op_hwini) {
3049 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003050 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003051 hw_ini = iniFile_newFromFile(android_op_hwini);
3052 if (hw_ini == NULL) {
3053 PANIC("Could not find %s file.", android_op_hwini);
3054 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003055
3056 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003057 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003058
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003059 /* If we're loading VM from a snapshot, make sure that the current HW config
3060 * matches the one with which the VM has been saved. */
3061 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3062 exit(0);
3063 }
3064
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003065 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003066
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003067 const char* kernelSerialDevicePrefix =
3068 androidHwConfig_getKernelSerialPrefix(android_hw);
3069 VERBOSE_PRINT(init, "Using kernel serial device prefix: %s",
3070 kernelSerialDevicePrefix);
3071
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003072 {
3073 int width = android_hw->hw_lcd_width;
3074 int height = android_hw->hw_lcd_height;
3075 int depth = android_hw->hw_lcd_depth;
3076
3077 /* A bit of sanity checking */
3078 if (width <= 0 || height <= 0 ||
3079 (depth != 16 && depth != 32) ||
3080 (((width|height) & 3) != 0) )
3081 {
3082 PANIC("Invalid display configuration (%d,%d,%d)",
3083 width, height, depth);
3084 }
3085 android_display_width = width;
3086 android_display_height = height;
3087 android_display_bpp = depth;
3088 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003089
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003090#ifdef CONFIG_NAND_LIMITS
3091 /* Init nand stuff. */
3092 if (android_op_nand_limits) {
3093 parse_nand_limits(android_op_nand_limits);
3094 }
3095#endif // CONFIG_NAND_LIMITS
3096
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003097 /* Initialize AVD name from hardware configuration if needed */
3098 if (!android_op_avd_name) {
3099 if (android_hw->avd_name && *android_hw->avd_name) {
3100 android_op_avd_name = android_hw->avd_name;
3101 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3102 }
3103 }
3104
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003105 // Determine format of all partition images, if possible.
3106 // Note that _UNKNOWN means the file, if it exists, will be probed.
3107 AndroidPartitionType system_partition_type =
3108 ANDROID_PARTITION_TYPE_UNKNOWN;
3109 AndroidPartitionType userdata_partition_type =
3110 ANDROID_PARTITION_TYPE_UNKNOWN;
3111 AndroidPartitionType cache_partition_type =
3112 ANDROID_PARTITION_TYPE_UNKNOWN;
3113
3114 {
3115 // Starting with Android 4.4.x, the ramdisk.img contains
3116 // an fstab.goldfish file that lists the format of each partition.
3117 // If the file exists, parse it to get the appropriate values.
3118 char* fstab = NULL;
3119 size_t fstabSize = 0;
3120
3121 if (android_extractRamdiskFile(android_hw->disk_ramdisk_path,
3122 "fstab.goldfish",
3123 &fstab,
3124 &fstabSize)) {
3125 VERBOSE_PRINT(init, "Ramdisk image contains fstab.goldfish file");
3126
3127 android_extractPartitionFormat(fstab,
3128 fstabSize,
3129 "system",
3130 "/system",
3131 &system_partition_type);
3132
3133 android_extractPartitionFormat(fstab,
3134 fstabSize,
3135 "userdata",
3136 "/data",
3137 &userdata_partition_type);
3138
3139 android_extractPartitionFormat(fstab,
3140 fstabSize,
3141 "cache",
3142 "/cache",
3143 &cache_partition_type);
3144
3145 free(fstab);
3146 } else {
3147 VERBOSE_PRINT(init, "No fstab.goldfish file in ramdisk image");
3148 }
3149 }
3150
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003151 /* Initialize system partition image */
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003152 android_nand_add_image("system",
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003153 system_partition_type,
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02003154 ANDROID_PARTITION_OPEN_MODE_MUST_EXIST,
3155 android_hw->disk_systemPartition_size,
3156 android_hw->disk_systemPartition_path,
3157 android_hw->disk_systemPartition_initPath);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003158
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003159 /* Initialize data partition image */
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003160 android_nand_add_image("userdata",
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003161 userdata_partition_type,
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02003162 ANDROID_PARTITION_OPEN_MODE_CREATE_IF_NEEDED,
3163 android_hw->disk_dataPartition_size,
3164 android_hw->disk_dataPartition_path,
3165 android_hw->disk_dataPartition_initPath);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003166
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003167 /* Initialize cache partition image, if any. Its type depends on the
3168 * kernel version. For anything >= 3.10, it must be EXT4, or
3169 * YAFFS2 otherwise.
3170 */
3171 if (android_hw->disk_cachePartition != 0) {
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003172 if (cache_partition_type == ANDROID_PARTITION_TYPE_UNKNOWN) {
3173 cache_partition_type =
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003174 (androidHwConfig_getKernelYaffs2Support(android_hw) >= 1) ?
3175 ANDROID_PARTITION_TYPE_YAFFS2 :
3176 ANDROID_PARTITION_TYPE_EXT4;
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003177 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003178
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003179 AndroidPartitionOpenMode cache_partition_mode =
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02003180 (android_op_wipe_data ?
3181 ANDROID_PARTITION_OPEN_MODE_MUST_WIPE :
3182 ANDROID_PARTITION_OPEN_MODE_CREATE_IF_NEEDED);
3183
David 'Digit' Turnera7f4b192014-06-17 17:26:31 +02003184 android_nand_add_image("cache",
David 'Digit' Turner965a6e02014-06-23 18:29:20 +02003185 cache_partition_type,
3186 cache_partition_mode,
David 'Digit' Turnerb72ae032014-06-18 19:23:20 +02003187 android_hw->disk_cachePartition_size,
3188 android_hw->disk_cachePartition_path,
3189 NULL);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003190 }
3191
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003192 /* Init SD-Card stuff. For Android, it is always hda */
3193 /* If the -hda option was used, ignore the Android-provided one */
3194 if (hda_opts == NULL) {
3195 const char* sdPath = android_hw->hw_sdCard_path;
3196 if (sdPath && *sdPath) {
3197 if (!path_exists(sdPath)) {
3198 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3199 } else if (filelock_create(sdPath) == NULL) {
3200 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3201 } else {
3202 /* Successful locking */
3203 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003204 /* Set this property of any operation involving the SD Card
3205 * will be x100 slower, due to the corresponding file being
3206 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3207 * the context of an emulator crash. The data is already
3208 * synced properly when the emulator exits (either normally or through ^C).
3209 */
3210 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003211 }
3212 }
3213 }
3214
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003215 if (hdb_opts == NULL) {
3216 const char* spath = android_hw->disk_snapStorage_path;
3217 if (spath && *spath) {
3218 if (!path_exists(spath)) {
3219 PANIC("Snapshot storage file does not exist: %s", spath);
3220 }
3221 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003222 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003223 }
3224 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003225 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003226 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003227 }
3228 }
3229
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003230 /* Set the VM's max heap size, passed as a boot property */
3231 if (android_hw->vm_heapSize > 0) {
3232 char tmp[64];
3233 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3234 boot_property_add("dalvik.vm.heapsize",tmp);
3235 }
3236
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003237 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3238 if (android_hw->hw_ramSize <= 512) {
3239 boot_property_add("ro.config.low_ram", "true");
3240 }
3241
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003242 /* Initialize net speed and delays stuff. */
3243 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003244 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003245 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003246 }
3247
3248 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003249 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003250 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003251 }
3252
3253 if (android_op_netfast) {
3254 qemu_net_download_speed = 0;
3255 qemu_net_upload_speed = 0;
3256 qemu_net_min_latency = 0;
3257 qemu_net_max_latency = 0;
3258 }
3259
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003260 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003261 if (android_hw->hw_lcd_density) {
3262 long density = android_hw->hw_lcd_density;
3263 if (density <= 0) {
3264 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003265 }
3266 hwLcd_setBootProperty(density);
3267 }
3268
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003269 /* Initialize presence of hardware nav button */
3270 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3271
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003272 /* Initialize TCP dump */
3273 if (android_op_tcpdump) {
3274 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3275 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3276 }
3277 }
3278
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003279 /* Initialize modem */
3280 if (android_op_radio) {
3281 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3282 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003283 PANIC("unsupported character device specification: %s\n"
3284 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003285 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003286 }
3287 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3288 } else if (android_hw->hw_gsmModem != 0 ) {
3289 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003290 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003291 }
3292 }
3293
3294 /* Initialize GPS */
3295 if (android_op_gps) {
3296 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3297 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003298 PANIC("unsupported character device specification: %s\n"
3299 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003300 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003301 }
3302 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3303 } else if (android_hw->hw_gps != 0) {
3304 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003305 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003306 }
3307 }
3308
3309 /* Initialize audio. */
3310 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003311 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003312 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003313 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003314 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003315 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003316 }
3317
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003318 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003319 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003320
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003321 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003322 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3323 strcmp(android_hw->hw_camera_front, "emulated")) {
3324 /* Fake camera is not used for camera emulation. */
3325 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003326 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003327 /* Fake camera is used for at least one camera emulation. */
3328 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3329 !strcmp(android_hw->hw_camera_front, "emulated")) {
3330 /* Fake camera is used for both, front and back camera emulation. */
3331 boot_property_add("qemu.sf.fake_camera", "both");
3332 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3333 boot_property_add("qemu.sf.fake_camera", "back");
3334 } else {
3335 boot_property_add("qemu.sf.fake_camera", "front");
3336 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003337 }
3338
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003339 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3340 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3341 int density;
3342 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3343 PANIC("-lcd-density : %d", density);
3344 }
3345 hwLcd_setBootProperty(density);
3346 }
3347
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003348 /* Initialize camera emulation. */
3349 android_camera_service_init();
3350
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003351 if (android_op_cpu_delay) {
3352 char* end;
3353 long delay = strtol(android_op_cpu_delay, &end, 0);
3354 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003355 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003356 }
3357 if (delay > 0)
3358 delay = (1000-delay);
3359
3360 qemu_cpu_delay = (int) delay;
3361 }
3362
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003363 if (android_op_dns_server) {
3364 char* x = strchr(android_op_dns_server, ',');
3365 dns_count = 0;
3366 if (x == NULL)
3367 {
3368 if ( add_dns_server( android_op_dns_server ) == 0 )
3369 dns_count = 1;
3370 }
3371 else
3372 {
3373 x = android_op_dns_server;
3374 while (*x) {
3375 char* y = strchr(x, ',');
3376
3377 if (y != NULL) {
3378 *y = 0;
3379 y++;
3380 } else {
3381 y = x + strlen(x);
3382 }
3383
3384 if (y > x && add_dns_server( x ) == 0) {
3385 dns_count += 1;
3386 }
3387 x = y;
3388 }
3389 }
3390 if (dns_count == 0)
3391 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3392 }
3393
3394 if (dns_count == 0)
3395 dns_count = slirp_get_system_dns_servers();
3396 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003397 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003398 }
3399
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01003400#ifdef CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003401 if (android_op_memcheck) {
3402 memcheck_init(android_op_memcheck);
3403 }
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +01003404#endif // CONFIG_ANDROID_MEMCHECK
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003405
Jesse Hall733fffa2012-04-26 11:07:32 -07003406 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3407 * If set to 0, the software GL ES renderer will be used as a fallback.
3408 * If the parameter is undefined, this means the system image runs
3409 * inside an emulator that doesn't support GPU emulation at all.
3410 *
3411 * We always start the GL ES renderer so we can gather stats on the
3412 * underlying GL implementation. If GL ES acceleration is disabled,
3413 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003414 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003415 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003416 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003417 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003418 {
3419 android_getOpenglesHardwareStrings(
3420 android_gl_vendor, sizeof(android_gl_vendor),
3421 android_gl_renderer, sizeof(android_gl_renderer),
3422 android_gl_version, sizeof(android_gl_version));
3423 if (android_hw->hw_gpu_enabled) {
3424 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003425 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003426 android_stopOpenglesRenderer();
3427 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003428 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003429 } else {
3430 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003431 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003432 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003433 stralloc_add_str(kernel_params, " qemu.gles=1");
3434 } else {
3435 stralloc_add_str(kernel_params, " qemu.gles=0");
3436 }
3437 }
3438
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003439 /* We always force qemu=1 when running inside QEMU */
3440 stralloc_add_str(kernel_params, " qemu=1");
3441
3442 /* We always initialize the first serial port for the android-kmsg
3443 * character device (used to send kernel messages) */
3444 serial_hds_add_at(0, "android-kmsg");
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003445 stralloc_add_format(kernel_params,
3446 " console=%s0",
3447 kernelSerialDevicePrefix);
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003448
3449 /* We always initialize the second serial port for the android-qemud
3450 * character device as well */
3451 serial_hds_add_at(1, "android-qemud");
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +01003452 stralloc_add_format(kernel_params,
3453 " android.qemud=%s1",
3454 kernelSerialDevicePrefix);
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003455
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003456 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3457 os_pidfile_error();
3458 exit(1);
3459 }
3460
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01003461 /* Open the logfile at this point, if necessary. We can't open the logfile
3462 * when encountering either of the logging options (-d or -D) because the
3463 * other one may be encountered later on the command line, changing the
3464 * location or level of logging.
3465 */
3466 if (log_mask) {
3467 int mask;
3468 if (log_file) {
3469 qemu_set_log_filename(log_file);
3470 }
3471
3472 mask = qemu_str_to_log_mask(log_mask);
3473 if (!mask) {
3474 qemu_print_log_usage(stdout);
3475 exit(1);
3476 }
3477 qemu_set_log(mask);
3478 }
David 'Digit' Turnerc0052462014-02-25 18:39:29 +01003479
David 'Digit' Turner36597752011-05-20 01:18:01 +02003480#if defined(CONFIG_KVM)
3481 if (kvm_allowed < 0) {
3482 kvm_allowed = kvm_check_allowed();
3483 }
3484#endif
3485
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003486 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3487 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003488 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3489 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003490 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003491 }
3492
3493 if (display_type == DT_NOGRAPHIC) {
3494 if (serial_device_index == 0)
3495 serial_devices[0] = "stdio";
3496 if (parallel_device_index == 0)
3497 parallel_devices[0] = "null";
3498 if (strncmp(monitor_device, "vc", 2) == 0)
3499 monitor_device = "stdio";
3500 }
3501
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003502 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003503 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003504 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003505
3506 if (kernel_filename == NULL) {
3507 kernel_filename = android_hw->kernel_path;
3508 }
3509 if (initrd_filename == NULL) {
3510 initrd_filename = android_hw->disk_ramdisk_path;
3511 }
3512
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003513 linux_boot = (kernel_filename != NULL);
3514 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3515
3516 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003517 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003518 }
3519
3520 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003521 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003522 }
3523
3524 /* boot to floppy or the default cd if no hard disk defined yet */
3525 if (!boot_devices[0]) {
3526 boot_devices = "cad";
3527 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003528 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003529
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003530 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003531 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003532 }
David Turner6a9ef172010-09-09 22:54:36 +02003533 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003534
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003535 /* init network clients */
3536 if (nb_net_clients == 0) {
3537 /* if no clients, we use a default config */
3538 net_clients[nb_net_clients++] = "nic";
3539#ifdef CONFIG_SLIRP
3540 net_clients[nb_net_clients++] = "user";
3541#endif
3542 }
3543
3544 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003545 if (net_client_parse(net_clients[i]) < 0) {
3546 PANIC("Unable to parse net clients");
3547 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003548 }
3549 net_client_check();
3550
3551#ifdef TARGET_I386
3552 /* XXX: this should be moved in the PC machine instantiation code */
3553 if (net_boot != 0) {
3554 int netroms = 0;
3555 for (i = 0; i < nb_nics && i < 4; i++) {
3556 const char *model = nd_table[i].model;
3557 char buf[1024];
3558 char *filename;
3559 if (net_boot & (1 << i)) {
3560 if (model == NULL)
3561 model = "ne2k_pci";
3562 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3563 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3564 if (filename && get_image_size(filename) > 0) {
3565 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003566 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003567 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003568 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003569 nb_option_roms++;
3570 netroms++;
3571 }
3572 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003573 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003574 }
3575 }
3576 }
3577 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003578 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003579 }
3580 }
3581#endif
3582
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003583 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003584 if (ram_size == 0) {
3585 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3586 if (ram_size == 0) {
3587 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3588 }
3589 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003590
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07003591 /* Quite often (especially on older XP machines) attempts to allocate large
3592 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
3593 * inside QEMU, it's not really possible to provide the user with a
3594 * meaningful explanation for the crash. So, lets see if QEMU is going to be
3595 * able to allocate requested amount of RAM, and if not, lets try to come up
3596 * with a recomendation. */
3597 {
3598 ram_addr_t r_ram = ram_size;
3599 void* alloc_check = malloc(r_ram);
3600 while (alloc_check == NULL && r_ram > 1024 * 1024) {
3601 /* Make it 25% less */
3602 r_ram -= r_ram / 4;
3603 alloc_check = malloc(r_ram);
3604 }
3605 if (alloc_check != NULL) {
3606 free(alloc_check);
3607 }
3608 if (r_ram != ram_size) {
3609 /* Requested RAM is too large. Report this, as well as calculated
3610 * recomendation. */
3611 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
3612 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
3613 ram_size = r_ram;
3614 }
3615 }
3616
rich canningsd952f282011-03-01 15:40:09 -08003617#ifndef _WIN32
David 'Digit' Turnerf9077a82014-02-10 23:10:47 +01003618 qemu_log_rotation_init();
rich canningsd952f282011-03-01 15:40:09 -08003619#endif
3620
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003621 /* init the dynamic translator */
3622 cpu_exec_init_all(tb_size * 1024 * 1024);
3623
3624 bdrv_init();
3625
3626 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003627#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003628 if (nb_drives_opt < MAX_DRIVES)
3629 drive_add(NULL, CDROM_ALIAS);
3630
3631 /* we always create at least one floppy */
3632
3633 if (nb_drives_opt < MAX_DRIVES)
3634 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003635 /* we always create one sd slot, even if no card is in it */
3636
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003637 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003638 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003639 }
3640#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003641
3642 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003643 if (snapshot)
3644 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3645 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3646 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003647
David 'Digit' Turner5cb5c0b2014-02-17 16:04:03 +01003648 //register_savevm(NULL, "timer", 0, 2, timer_save, timer_load, &timers_state);
3649
3650 SaveVMHandlers* ops = g_malloc0(sizeof(*ops));
3651 ops->save_live_state = ram_save_live;
3652 ops->load_state = ram_load;
3653
3654 register_savevm_live(NULL,
3655 "ram",
3656 0,
3657 3,
3658 ops,
3659 NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003660
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003661 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003662 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003663
3664 /* Maintain compatibility with multiple stdio monitors */
3665 if (!strcmp(monitor_device,"stdio")) {
3666 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
3667 const char *devname = serial_devices[i];
3668 if (devname && !strcmp(devname,"mon:stdio")) {
3669 monitor_device = NULL;
3670 break;
3671 } else if (devname && !strcmp(devname,"stdio")) {
3672 monitor_device = NULL;
3673 serial_devices[i] = "mon:stdio";
3674 break;
3675 }
3676 }
3677 }
3678
3679 if (nb_numa_nodes > 0) {
3680 int i;
3681
3682 if (nb_numa_nodes > smp_cpus) {
3683 nb_numa_nodes = smp_cpus;
3684 }
3685
3686 /* If no memory size if given for any node, assume the default case
3687 * and distribute the available memory equally across all nodes
3688 */
3689 for (i = 0; i < nb_numa_nodes; i++) {
3690 if (node_mem[i] != 0)
3691 break;
3692 }
3693 if (i == nb_numa_nodes) {
3694 uint64_t usedmem = 0;
3695
3696 /* On Linux, the each node's border has to be 8MB aligned,
3697 * the final node gets the rest.
3698 */
3699 for (i = 0; i < nb_numa_nodes - 1; i++) {
3700 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3701 usedmem += node_mem[i];
3702 }
3703 node_mem[i] = ram_size - usedmem;
3704 }
3705
3706 for (i = 0; i < nb_numa_nodes; i++) {
3707 if (node_cpumask[i] != 0)
3708 break;
3709 }
3710 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3711 * must cope with this anyway, because there are BIOSes out there in
3712 * real machines which also use this scheme.
3713 */
3714 if (i == nb_numa_nodes) {
3715 for (i = 0; i < smp_cpus; i++) {
3716 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3717 }
3718 }
3719 }
3720
3721 if (kvm_enabled()) {
3722 int ret;
3723
3724 ret = kvm_init(smp_cpus);
3725 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003726 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003727 }
3728 }
3729
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003730#ifdef CONFIG_HAX
3731 if (!hax_disabled)
3732 {
3733 int ret;
3734
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08003735 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003736 ret = hax_init(smp_cpus);
3737 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
3738 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
3739 }
3740#endif
3741
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003742 if (monitor_device) {
3743 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
3744 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003745 PANIC("qemu: could not open monitor device '%s'",
3746 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003747 }
3748 }
3749
3750 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003751 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003752 }
3753
3754 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3755 const char *devname = parallel_devices[i];
3756 if (devname && strcmp(devname, "none")) {
3757 char label[32];
3758 snprintf(label, sizeof(label), "parallel%d", i);
3759 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
3760 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003761 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003762 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003763 }
3764 }
3765 }
3766
3767 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3768 const char *devname = virtio_consoles[i];
3769 if (devname && strcmp(devname, "none")) {
3770 char label[32];
3771 snprintf(label, sizeof(label), "virtcon%d", i);
3772 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
3773 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003774 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003775 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003776 }
3777 }
3778 }
3779
3780 module_call_init(MODULE_INIT_DEVICE);
3781
3782
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003783 /* Check the CPU Architecture value */
David 'Digit' Turner7c07e4e2014-06-14 00:07:04 +02003784 {
3785 static const char* kSupportedArchs[] = {
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003786#if defined(TARGET_ARM)
David 'Digit' Turner7c07e4e2014-06-14 00:07:04 +02003787 "arm",
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003788#endif
David 'Digit' Turner7c07e4e2014-06-14 00:07:04 +02003789#if defined(TARGET_I386)
3790 "x86",
3791#endif
3792#if defined(TARGET_X86_64)
3793 "x86_64",
3794#endif
3795#if defined(TARGET_MIPS)
3796 "mips",
3797#endif
3798 };
3799 const size_t kNumSupportedArchs =
3800 sizeof(kSupportedArchs) / sizeof(kSupportedArchs[0]);
3801 bool supported_arch = false;
3802 size_t n;
3803 for (n = 0; n < kNumSupportedArchs; ++n) {
3804 if (!strcmp(android_hw->hw_cpu_arch, kSupportedArchs[n])) {
3805 supported_arch = true;
3806 break;
3807 }
3808 }
3809 if (!supported_arch) {
3810 fprintf(stderr, "-- Invalid CPU architecture: %s, valid values:",
3811 android_hw->hw_cpu_arch);
3812 for (n = 0; n < kNumSupportedArchs; ++n) {
3813 fprintf(stderr, " %s", kSupportedArchs[n]);
3814 }
3815 fprintf(stderr, "\n");
3816 exit(1);
3817 }
3818 }
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01003819
3820 /* Grab CPU model if provided in hardware.ini */
3821 if ( !cpu_model
3822 && android_hw->hw_cpu_model
3823 && android_hw->hw_cpu_model[0] != '\0')
3824 {
3825 cpu_model = android_hw->hw_cpu_model;
3826 }
3827
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003828 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003829 * collected during initialization.
3830 *
3831 * The order is the following:
3832 * - parameters from the hw configuration (kernel.parameters)
3833 * - additionnal parameters from options (e.g. -memcheck)
3834 * - the -append parameters.
3835 */
3836 {
3837 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003838
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003839 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01003840 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003841 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
3842 }
3843
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003844 /* If not empty, kernel_config always contains a leading space */
3845 stralloc_append(kernel_params, kernel_config);
3846
3847 if (*kernel_cmdline) {
3848 stralloc_add_c(kernel_params, ' ');
3849 stralloc_add_str(kernel_params, kernel_cmdline);
3850 }
3851
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003852 /* Remove any leading/trailing spaces */
3853 stralloc_strip(kernel_params);
3854
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003855 kernel_parameters = stralloc_cstr(kernel_params);
3856 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
3857
3858 machine->init(ram_size,
3859 boot_devices,
3860 kernel_filename,
3861 kernel_parameters,
3862 initrd_filename,
3863 cpu_model);
3864
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08003865 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07003866 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08003867 mts_port_create(NULL);
3868 }
3869
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003870 stralloc_reset(kernel_params);
3871 stralloc_reset(kernel_config);
3872 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003873
David 'Digit' Turner66576782014-03-24 16:57:57 +01003874 CPU_FOREACH(cpu) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003875 for (i = 0; i < nb_numa_nodes; i++) {
David 'Digit' Turner66576782014-03-24 16:57:57 +01003876 if (node_cpumask[i] & (1 << cpu->cpu_index)) {
3877 cpu->numa_node = i;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003878 }
3879 }
3880 }
3881
3882 current_machine = machine;
3883
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01003884 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003885 if (kvm_enabled()) {
3886 int ret;
3887
3888 ret = kvm_sync_vcpus();
3889 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003890 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003891 }
3892 }
3893
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003894#ifdef CONFIG_HAX
3895 if (hax_enabled())
3896 hax_sync_vcpus();
3897#endif
3898
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08003899 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01003900 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08003901
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003902 /* Initialize display from the command line parameters. */
3903 android_display_reset(ds,
3904 android_display_width,
3905 android_display_height,
3906 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003907
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003908 if (display_type == DT_DEFAULT) {
3909#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3910 display_type = DT_SDL;
3911#else
3912 display_type = DT_VNC;
3913 vnc_display = "localhost:0,to=99";
3914 show_vnc_port = 1;
3915#endif
3916 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003917
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003918
3919 switch (display_type) {
3920 case DT_NOGRAPHIC:
3921 break;
3922#if defined(CONFIG_CURSES)
3923 case DT_CURSES:
3924 curses_display_init(ds, full_screen);
3925 break;
3926#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07003927#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003928 case DT_SDL:
3929 sdl_display_init(ds, full_screen, no_frame);
3930 break;
3931#elif defined(CONFIG_COCOA)
3932 case DT_SDL:
3933 cocoa_display_init(ds, full_screen);
3934 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08003935#elif defined(CONFIG_STANDALONE_CORE)
3936 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08003937 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08003938 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003939#endif
3940 case DT_VNC:
3941 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003942 if (vnc_display_open(ds, vnc_display) < 0) {
3943 PANIC("Unable to initialize VNC display");
3944 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003945
3946 if (show_vnc_port) {
3947 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3948 }
3949 break;
3950 default:
3951 break;
3952 }
3953 dpy_resize(ds);
3954
3955 dcl = ds->listeners;
3956 while (dcl != NULL) {
3957 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01003958 ds->gui_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, gui_update, ds);
3959 timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003960 }
3961 dcl = dcl->next;
3962 }
3963
3964 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turnerdcda9492014-02-16 15:13:55 +01003965 nographic_timer = timer_new(QEMU_CLOCK_REALTIME, SCALE_MS, nographic_update, NULL);
3966 timer_mod(nographic_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003967 }
3968
David 'Digit' Turner94702b02011-01-20 02:46:33 +01003969 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003970 qemu_chr_initial_reset();
3971
3972 if (monitor_device && monitor_hd)
3973 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
3974
3975 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3976 const char *devname = serial_devices[i];
3977 if (devname && strcmp(devname, "none")) {
3978 if (strstart(devname, "vc", 0))
3979 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
3980 }
3981 }
3982
3983 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3984 const char *devname = parallel_devices[i];
3985 if (devname && strcmp(devname, "none")) {
3986 if (strstart(devname, "vc", 0))
3987 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
3988 }
3989 }
3990
3991 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
3992 const char *devname = virtio_consoles[i];
3993 if (virtcon_hds[i] && devname) {
3994 if (strstart(devname, "vc", 0))
3995 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
3996 }
3997 }
3998
3999 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004000 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004001 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004002 }
4003
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004004 /* call android-specific setup function */
4005 android_emulation_setup();
4006
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004007#if !defined(CONFIG_STANDALONE_CORE)
4008 // For the standalone emulator (UI+core in one executable) we need to
4009 // set the window title here.
4010 android_emulator_set_base_port(android_base_port);
4011#endif
4012
Ot ten Thije871da2a2010-09-20 10:29:22 +01004013 if (loadvm)
4014 do_loadvm(cur_mon, loadvm);
4015
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004016 if (incoming) {
4017 autostart = 0; /* fixme how to deal with -daemonize */
4018 qemu_start_incoming_migration(incoming);
4019 }
4020
4021 if (autostart)
4022 vm_start();
4023
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004024 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004025
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004026#ifdef CONFIG_ANDROID
4027 // This will notify the UI that the core is successfuly initialized
4028 android_core_init_completed();
4029#endif // CONFIG_ANDROID
4030
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004031 main_loop();
4032 quit_timers();
4033 net_cleanup();
4034 android_emulation_teardown();
4035 return 0;
4036}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004037
4038void
4039android_emulation_teardown(void)
4040{
4041 android_charmap_done();
4042}