blob: 642668b569368158cde1e72b3e7362980ef72f2c [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* the following is needed on Linux to define ptsname() in stdlib.h */
26#if defined(__linux__)
27#define _GNU_SOURCE 1
28#endif
29
30#include "qemu-common.h"
31#include "hw/hw.h"
32#include "hw/boards.h"
33#include "hw/usb.h"
34#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010035#include "hw/i386/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070036#include "hw/audiodev.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010037#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070038#include "hw/baum.h"
David 'Digit' Turnerf0665422013-12-17 10:47:09 +010039#include "hw/android/goldfish/nand.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010040#include "net/net.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +010041#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010042#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +010043#include "exec/gdbstub.h"
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +010044#include "qemu/log.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +010045#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +010046#include "sysemu/char.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010047#include "sysemu/blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070048#include "audio/audio.h"
49
David 'Digit' Turner28a09b62013-12-15 00:16:00 +010050#include "migration/qemu-file.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070051#include "android/android.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +010052#include "android/charpipe.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070053#include "modem_driver.h"
54#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070055#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020056#include "android/hw-pipe-net.h"
57#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070058#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080059#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070060#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070061#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070062#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010063#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010064#include "android/utils/filelock.h"
65#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010066#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010067#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080068#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080069#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010070#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020071#include "android/opengles.h"
Vladimir Chtchetkine1a820e92012-04-11 13:22:48 -070072#include "android/multitouch-screen.h"
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +010073#include "exec/hwaddr.h"
David 'Digit' Turner6e2eb782013-12-15 00:54:21 +010074#include "android/tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070075
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070076#ifdef CONFIG_MEMCHECK
77#include "memcheck/memcheck.h"
78#endif // CONFIG_MEMCHECK
79
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070080#include <unistd.h>
81#include <fcntl.h>
82#include <signal.h>
83#include <time.h>
84#include <errno.h>
85#include <sys/time.h>
86#include <zlib.h>
87
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070088/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070089#include "config-host.h"
90
91#ifndef _WIN32
92#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070093#include <sys/times.h>
94#include <sys/wait.h>
95#include <termios.h>
96#include <sys/mman.h>
97#include <sys/ioctl.h>
98#include <sys/resource.h>
99#include <sys/socket.h>
100#include <netinet/in.h>
101#include <net/if.h>
102#if defined(__NetBSD__)
103#include <net/if_tap.h>
104#endif
105#ifdef __linux__
106#include <linux/if_tun.h>
107#endif
108#include <arpa/inet.h>
109#include <dirent.h>
110#include <netdb.h>
111#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700112#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700113#include <sys/stat.h>
114#if defined(__FreeBSD__) || defined(__DragonFly__)
115#include <libutil.h>
116#else
117#include <util.h>
118#endif
119#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
120#include <freebsd/stdlib.h>
121#else
122#ifdef __linux__
123#include <pty.h>
124#include <malloc.h>
125#include <linux/rtc.h>
126
127/* For the benefit of older linux systems which don't supply it,
128 we use a local copy of hpet.h. */
129/* #include <linux/hpet.h> */
David 'Digit' Turner2dbdad52013-12-17 09:24:18 +0100130#include "hw/timer/hpet.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700131
132#include <linux/ppdev.h>
133#include <linux/parport.h>
134#endif
135#ifdef __sun__
136#include <sys/stat.h>
137#include <sys/ethernet.h>
138#include <sys/sockio.h>
139#include <netinet/arp.h>
140#include <netinet/in.h>
141#include <netinet/in_systm.h>
142#include <netinet/ip.h>
143#include <netinet/ip_icmp.h> // must come after ip.h
144#include <netinet/udp.h>
145#include <netinet/tcp.h>
146#include <net/if.h>
147#include <syslog.h>
148#include <stropts.h>
149#endif
150#endif
151#endif
152
153#if defined(__OpenBSD__)
154#include <util.h>
155#endif
156
157#if defined(CONFIG_VDE)
158#include <libvdeplug.h>
159#endif
160
161#ifdef _WIN32
162#include <windows.h>
163#include <malloc.h>
164#include <sys/timeb.h>
165#include <mmsystem.h>
166#define getopt_long_only getopt_long
167#define memalign(align, size) malloc(size)
168#endif
169
David 'Digit' Turner11822842013-12-17 09:16:47 +0100170#include "sysemu/cpus.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100171#include "sysemu/arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700172
173#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200174int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700175#undef main
176#define main qemu_main
177#endif /* CONFIG_COCOA */
178
179#include "hw/hw.h"
180#include "hw/boards.h"
181#include "hw/usb.h"
182#include "hw/pcmcia.h"
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +0100183#include "hw/i386/pc.h"
184#include "hw/isa/isa.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700185#include "hw/baum.h"
186#include "hw/bt.h"
David 'Digit' Turner7821d632013-12-17 11:27:50 +0100187#include "sysemu/watchdog.h"
David 'Digit' Turner73a93a12013-12-17 11:26:01 +0100188#include "hw/i386/smbios.h"
David 'Digit' Turner50319182013-12-17 11:28:54 +0100189#include "hw/xen/xen.h"
David 'Digit' Turner6800f0a2013-12-17 09:12:54 +0100190#include "sysemu/bt.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +0100191#include "net/net.h"
David 'Digit' Turner6af67652013-12-14 23:49:32 +0100192#include "monitor/monitor.h"
David 'Digit' Turner1c31e3e2013-12-14 20:07:17 +0100193#include "ui/console.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100194#include "sysemu/sysemu.h"
David 'Digit' Turner852088c2013-12-14 23:04:12 +0100195#include "exec/gdbstub.h"
David 'Digit' Turner7a78db72013-12-14 11:46:01 +0100196#include "qemu/timer.h"
David 'Digit' Turnere7216d82013-12-15 00:51:13 +0100197#include "sysemu/char.h"
David 'Digit' Turner37dc41a2013-12-14 14:45:51 +0100198#include "qemu/cache-utils.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100199#include "block/block.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100200#include "sysemu/dma.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700201#include "audio/audio.h"
David 'Digit' Turner28a09b62013-12-15 00:16:00 +0100202#include "migration/migration.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100203#include "sysemu/kvm.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +0100204#include "exec/hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200205#ifdef CONFIG_KVM
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100206#include "android/kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200207#endif
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +0100208#include "sysemu/balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700209#include "android/hw-lcd.h"
210#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200211#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700212#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100213#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700214
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800215#include "android/snaphost-android.h"
216
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800217#if !defined(CONFIG_STANDALONE_CORE)
218/* in android/qemulator.c */
219extern void android_emulator_set_base_port(int port);
220#endif
221
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700222#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700223#undef main
224#define main qemu_main
225#endif
226
David 'Digit' Turnercc33b2d2013-12-15 00:09:42 +0100227#include "disas/disas.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700228
David 'Digit' Turnerd0edecb2013-12-17 09:32:26 +0100229#include "qemu/sockets.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700230
231#if defined(CONFIG_SLIRP)
232#include "libslirp.h"
233#endif
234
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700235#define DEFAULT_RAM_SIZE 128
236
237/* Max number of USB devices that can be specified on the commandline. */
238#define MAX_USB_CMDLINE 8
239
240/* Max number of bluetooth switches on the commandline. */
241#define MAX_BT_CMDLINE 10
242
243/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700244
245static const char *data_dir;
246const char *bios_name = NULL;
247static void *ioport_opaque[MAX_IOPORTS];
248static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
249static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100250#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700251/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
252 to store the VM snapshots */
253DriveInfo drives_table[MAX_DRIVES+1];
254int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100255#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700256enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700257DisplayType display_type = DT_DEFAULT;
258const char* keyboard_layout = NULL;
259int64_t ticks_per_sec;
260ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200261const char *mem_path = NULL;
262#ifdef MAP_POPULATE
263int mem_prealloc = 0; /* force preallocation of physical target memory */
264#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700265int nb_nics;
266NICInfo nd_table[MAX_NICS];
267int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100268int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700269static int rtc_utc = 1;
270static int rtc_date_offset = -1; /* -1 means no change */
271int cirrus_vga_enabled = 1;
272int std_vga_enabled = 0;
273int vmsvga_enabled = 0;
274int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700275QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700276static int full_screen = 0;
277#ifdef CONFIG_SDL
278static int no_frame = 0;
279#endif
280int no_quit = 0;
281CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100282int serial_hds_count;
283
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700284CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
285CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
286#ifdef TARGET_I386
287int win2k_install_hack = 0;
288int rtc_td_hack = 0;
289#endif
290int usb_enabled = 0;
291int singlestep = 0;
292int smp_cpus = 1;
293const char *vnc_display;
294int acpi_enabled = 1;
295int no_hpet = 0;
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800296int hax_disabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700297int no_virtio_balloon = 0;
298int fd_bootchk = 1;
299int no_reboot = 0;
300int no_shutdown = 0;
301int cursor_hide = 1;
302int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700303WatchdogTimerModel *watchdog = NULL;
304int watchdog_action = WDT_RESET;
305const char *option_rom[MAX_OPTION_ROMS];
306int nb_option_roms;
307int semihosting_enabled = 0;
308#ifdef TARGET_ARM
309int old_param = 0;
310#endif
311const char *qemu_name;
312int alt_grab = 0;
313#if defined(TARGET_SPARC) || defined(TARGET_PPC)
314unsigned int nb_prom_envs = 0;
315const char *prom_envs[MAX_PROM_ENVS];
316#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100317#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700318int nb_drives_opt;
319struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100320#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700321int nb_numa_nodes;
322uint64_t node_mem[MAX_NODES];
323uint64_t node_cpumask[MAX_NODES];
324
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325static QEMUTimer *nographic_timer;
326
327uint8_t qemu_uuid[16];
328
329
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700330int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700331extern char* audio_input_source;
332
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700333extern char* android_op_ports;
334extern char* android_op_port;
335extern char* android_op_report_console;
336extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700337// Path to the file containing specific key character map.
338char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700339
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700340/* Path to hardware initialization file passed with -android-hw option. */
341char* android_op_hwini = NULL;
342
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700343/* Memory checker options. */
344char* android_op_memcheck = NULL;
345
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700346/* -dns-server option value. */
347char* android_op_dns_server = NULL;
348
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700349/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700350char* android_op_radio = NULL;
351
352/* -gps option value. */
353char* android_op_gps = NULL;
354
355/* -audio option value. */
356char* android_op_audio = NULL;
357
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700358/* -cpu-delay option value. */
359char* android_op_cpu_delay = NULL;
360
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700361#ifdef CONFIG_NAND_LIMITS
362/* -nand-limits option value. */
363char* android_op_nand_limits = NULL;
364#endif // CONFIG_NAND_LIMITS
365
366/* -netspeed option value. */
367char* android_op_netspeed = NULL;
368
369/* -netdelay option value. */
370char* android_op_netdelay = NULL;
371
372/* -netfast option value. */
373int android_op_netfast = 0;
374
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700375/* -tcpdump option value. */
376char* android_op_tcpdump = NULL;
377
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700378/* -lcd-density option value. */
379char* android_op_lcd_density = NULL;
380
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700381/* -ui-port option value. This port will be used to report the core
382 * initialization completion.
383 */
384char* android_op_ui_port = NULL;
385
386/* -ui-settings option value. This value will be passed to the UI when new UI
387 * process is attaching to the core.
388 */
389char* android_op_ui_settings = NULL;
390
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800391/* -android-avdname option value. */
392char* android_op_avd_name = "unknown";
393
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700394extern int android_display_width;
395extern int android_display_height;
396extern int android_display_bpp;
397
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700398extern void dprint( const char* format, ... );
399
rich canningsd952f282011-03-01 15:40:09 -0800400const char* dns_log_filename = NULL;
401const char* drop_log_filename = NULL;
402static int rotate_logs_requested = 0;
403
Tim Baverstock24204cc2010-11-25 11:37:43 +0000404const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000405
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700406#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
407
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700408/* Reports the core initialization failure to the error stdout and to the UI
409 * socket before exiting the application.
410 * Parameters that are passed to this macro are used to format the error
411 * mesage using sprintf routine.
412 */
413#ifdef CONFIG_ANDROID
414#define PANIC(...) android_core_init_failure(__VA_ARGS__)
415#else
416#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
417 exit(1); \
418 } while (0)
419#endif // CONFIG_ANDROID
420
421/* Exits the core during initialization. */
422#ifdef CONFIG_ANDROID
423#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
424#else
425#define QEMU_EXIT(exit_code) exit(exit_code)
426#endif // CONFIG_ANDROID
427
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700428/***********************************************************/
429/* x86 ISA bus support */
430
David 'Digit' Turnerbcde1092014-01-09 23:19:19 +0100431hwaddr isa_mem_base = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700432PicState2 *isa_pic;
433
434static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
435static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
436
437static uint32_t ioport_read(int index, uint32_t address)
438{
439 static IOPortReadFunc *default_func[3] = {
440 default_ioport_readb,
441 default_ioport_readw,
442 default_ioport_readl
443 };
444 IOPortReadFunc *func = ioport_read_table[index][address];
445 if (!func)
446 func = default_func[index];
447 return func(ioport_opaque[address], address);
448}
449
450static void ioport_write(int index, uint32_t address, uint32_t data)
451{
452 static IOPortWriteFunc *default_func[3] = {
453 default_ioport_writeb,
454 default_ioport_writew,
455 default_ioport_writel
456 };
457 IOPortWriteFunc *func = ioport_write_table[index][address];
458 if (!func)
459 func = default_func[index];
460 func(ioport_opaque[address], address, data);
461}
462
463static uint32_t default_ioport_readb(void *opaque, uint32_t address)
464{
465#ifdef DEBUG_UNUSED_IOPORT
466 fprintf(stderr, "unused inb: port=0x%04x\n", address);
467#endif
468 return 0xff;
469}
470
471static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
472{
473#ifdef DEBUG_UNUSED_IOPORT
474 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
475#endif
476}
477
478/* default is to make two byte accesses */
479static uint32_t default_ioport_readw(void *opaque, uint32_t address)
480{
481 uint32_t data;
482 data = ioport_read(0, address);
483 address = (address + 1) & (MAX_IOPORTS - 1);
484 data |= ioport_read(0, address) << 8;
485 return data;
486}
487
488static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
489{
490 ioport_write(0, address, data & 0xff);
491 address = (address + 1) & (MAX_IOPORTS - 1);
492 ioport_write(0, address, (data >> 8) & 0xff);
493}
494
495static uint32_t default_ioport_readl(void *opaque, uint32_t address)
496{
497#ifdef DEBUG_UNUSED_IOPORT
498 fprintf(stderr, "unused inl: port=0x%04x\n", address);
499#endif
500 return 0xffffffff;
501}
502
503static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
504{
505#ifdef DEBUG_UNUSED_IOPORT
506 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
507#endif
508}
509
rich canningsd952f282011-03-01 15:40:09 -0800510/*
511 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
512 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
513 * between the tasks that do not require restarting Qemu.
514 */
515void rotate_qemu_logs_handler(int signum) {
516 rotate_logs_requested = 1;
517}
518
519/*
520 * Resets the rotate_log_requested_flag. Normally called after qemu
521 * logs has been rotated.
522 */
523void reset_rotate_qemu_logs_request(void) {
524 rotate_logs_requested = 0;
525}
526
527/*
528 * Clears the passed qemu log when the rotate_logs_requested
529 * is set. We need to clear the logs between the tasks that do not
530 * require restarting Qemu.
531 */
532FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
533 FILE* new_log_fd = NULL;
534 if (old_log_fd) {
535 if (fclose(old_log_fd) == -1) {
536 fprintf(stderr, "Cannot close old_log fd\n");
537 exit(errno);
538 }
539 }
540
541 if (!filename) {
542 fprintf(stderr, "The log filename to be rotated is not provided");
543 exit(-1);
544 }
545
546 new_log_fd = fopen(filename , "wb+");
547 if (new_log_fd == NULL) {
548 fprintf(stderr, "Cannot open the log file: %s for write.\n",
549 filename);
550 exit(1);
551 }
552
553 return new_log_fd;
554}
555
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700556/***************/
557/* ballooning */
558
559static QEMUBalloonEvent *qemu_balloon_event;
560void *qemu_balloon_event_opaque;
561
562void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
563{
564 qemu_balloon_event = func;
565 qemu_balloon_event_opaque = opaque;
566}
567
568void qemu_balloon(ram_addr_t target)
569{
570 if (qemu_balloon_event)
571 qemu_balloon_event(qemu_balloon_event_opaque, target);
572}
573
574ram_addr_t qemu_balloon_status(void)
575{
576 if (qemu_balloon_event)
577 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
578 return 0;
579}
580
581/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700582/* host time/date access */
583void qemu_get_timedate(struct tm *tm, int offset)
584{
585 time_t ti;
586 struct tm *ret;
587
588 time(&ti);
589 ti += offset;
590 if (rtc_date_offset == -1) {
591 if (rtc_utc)
592 ret = gmtime(&ti);
593 else
594 ret = localtime(&ti);
595 } else {
596 ti -= rtc_date_offset;
597 ret = gmtime(&ti);
598 }
599
600 memcpy(tm, ret, sizeof(struct tm));
601}
602
603int qemu_timedate_diff(struct tm *tm)
604{
605 time_t seconds;
606
607 if (rtc_date_offset == -1)
608 if (rtc_utc)
609 seconds = mktimegm(tm);
610 else
611 seconds = mktime(tm);
612 else
613 seconds = mktimegm(tm) + rtc_date_offset;
614
615 return seconds - time(NULL);
616}
617
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700618/***********************************************************/
619/* Bluetooth support */
620static int nb_hcis;
621static int cur_hci;
622static struct HCIInfo *hci_table[MAX_NICS];
623
624static struct bt_vlan_s {
625 struct bt_scatternet_s net;
626 int id;
627 struct bt_vlan_s *next;
628} *first_bt_vlan;
629
630/* find or alloc a new bluetooth "VLAN" */
631static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
632{
633 struct bt_vlan_s **pvlan, *vlan;
634 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
635 if (vlan->id == id)
636 return &vlan->net;
637 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100638 vlan = g_malloc0(sizeof(struct bt_vlan_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700639 vlan->id = id;
640 pvlan = &first_bt_vlan;
641 while (*pvlan != NULL)
642 pvlan = &(*pvlan)->next;
643 *pvlan = vlan;
644 return &vlan->net;
645}
646
647static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
648{
649}
650
651static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
652{
653 return -ENOTSUP;
654}
655
656static struct HCIInfo null_hci = {
657 .cmd_send = null_hci_send,
658 .sco_send = null_hci_send,
659 .acl_send = null_hci_send,
660 .bdaddr_set = null_hci_addr_set,
661};
662
663struct HCIInfo *qemu_next_hci(void)
664{
665 if (cur_hci == nb_hcis)
666 return &null_hci;
667
668 return hci_table[cur_hci++];
669}
670
671static struct HCIInfo *hci_init(const char *str)
672{
673 char *endp;
674 struct bt_scatternet_s *vlan = 0;
675
676 if (!strcmp(str, "null"))
677 /* null */
678 return &null_hci;
679 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
680 /* host[:hciN] */
681 return bt_host_hci(str[4] ? str + 5 : "hci0");
682 else if (!strncmp(str, "hci", 3)) {
683 /* hci[,vlan=n] */
684 if (str[3]) {
685 if (!strncmp(str + 3, ",vlan=", 6)) {
686 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
687 if (*endp)
688 vlan = 0;
689 }
690 } else
691 vlan = qemu_find_bt_vlan(0);
692 if (vlan)
693 return bt_new_hci(vlan);
694 }
695
696 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
697
698 return 0;
699}
700
701static int bt_hci_parse(const char *str)
702{
703 struct HCIInfo *hci;
704 bdaddr_t bdaddr;
705
706 if (nb_hcis >= MAX_NICS) {
707 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
708 return -1;
709 }
710
711 hci = hci_init(str);
712 if (!hci)
713 return -1;
714
715 bdaddr.b[0] = 0x52;
716 bdaddr.b[1] = 0x54;
717 bdaddr.b[2] = 0x00;
718 bdaddr.b[3] = 0x12;
719 bdaddr.b[4] = 0x34;
720 bdaddr.b[5] = 0x56 + nb_hcis;
721 hci->bdaddr_set(hci, bdaddr.b);
722
723 hci_table[nb_hcis++] = hci;
724
725 return 0;
726}
727
728static void bt_vhci_add(int vlan_id)
729{
730 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
731
732 if (!vlan->slave)
733 fprintf(stderr, "qemu: warning: adding a VHCI to "
734 "an empty scatternet %i\n", vlan_id);
735
736 bt_vhci_init(bt_new_hci(vlan));
737}
738
739static struct bt_device_s *bt_device_add(const char *opt)
740{
741 struct bt_scatternet_s *vlan;
742 int vlan_id = 0;
743 char *endp = strstr(opt, ",vlan=");
744 int len = (endp ? endp - opt : strlen(opt)) + 1;
745 char devname[10];
746
747 pstrcpy(devname, MIN(sizeof(devname), len), opt);
748
749 if (endp) {
750 vlan_id = strtol(endp + 6, &endp, 0);
751 if (*endp) {
752 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
753 return 0;
754 }
755 }
756
757 vlan = qemu_find_bt_vlan(vlan_id);
758
759 if (!vlan->slave)
760 fprintf(stderr, "qemu: warning: adding a slave device to "
761 "an empty scatternet %i\n", vlan_id);
762
763 if (!strcmp(devname, "keyboard"))
764 return bt_keyboard_init(vlan);
765
766 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
767 return 0;
768}
769
770static int bt_parse(const char *opt)
771{
772 const char *endp, *p;
773 int vlan;
774
775 if (strstart(opt, "hci", &endp)) {
776 if (!*endp || *endp == ',') {
777 if (*endp)
778 if (!strstart(endp, ",vlan=", 0))
779 opt = endp + 1;
780
781 return bt_hci_parse(opt);
782 }
783 } else if (strstart(opt, "vhci", &endp)) {
784 if (!*endp || *endp == ',') {
785 if (*endp) {
786 if (strstart(endp, ",vlan=", &p)) {
787 vlan = strtol(p, (char **) &endp, 0);
788 if (*endp) {
789 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
790 return 1;
791 }
792 } else {
793 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
794 return 1;
795 }
796 } else
797 vlan = 0;
798
799 bt_vhci_add(vlan);
800 return 0;
801 }
802 } else if (strstart(opt, "device:", &endp))
803 return !bt_device_add(endp);
804
805 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
806 return 1;
807}
808
809/***********************************************************/
810/* QEMU Block devices */
811
812#define HD_ALIAS "index=%d,media=disk"
813#define CDROM_ALIAS "index=2,media=cdrom"
814#define FD_ALIAS "index=%d,if=floppy"
815#define PFLASH_ALIAS "if=pflash"
816#define MTD_ALIAS "if=mtd"
817#define SD_ALIAS "index=0,if=sd"
818
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100819static int drive_init_func(QemuOpts *opts, void *opaque)
820{
821 int *use_scsi = opaque;
822 int fatal_error = 0;
823
824 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
825 if (fatal_error)
826 return 1;
827 }
828 return 0;
829}
830
831static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
832{
833 if (NULL == qemu_opt_get(opts, "snapshot")) {
834 qemu_opt_set(opts, "snapshot", "on");
835 }
836 return 0;
837}
838
839#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700840static int drive_opt_get_free_idx(void)
841{
842 int index;
843
844 for (index = 0; index < MAX_DRIVES; index++)
845 if (!drives_opt[index].used) {
846 drives_opt[index].used = 1;
847 return index;
848 }
849
850 return -1;
851}
852
853static int drive_get_free_idx(void)
854{
855 int index;
856
857 for (index = 0; index < MAX_DRIVES; index++)
858 if (!drives_table[index].used) {
859 drives_table[index].used = 1;
860 return index;
861 }
862
863 return -1;
864}
865
866int drive_add(const char *file, const char *fmt, ...)
867{
868 va_list ap;
869 int index = drive_opt_get_free_idx();
870
871 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
872 fprintf(stderr, "qemu: too many drives\n");
873 return -1;
874 }
875
876 drives_opt[index].file = file;
877 va_start(ap, fmt);
878 vsnprintf(drives_opt[index].opt,
879 sizeof(drives_opt[0].opt), fmt, ap);
880 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700881
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700882 nb_drives_opt++;
883 return index;
884}
885
886void drive_remove(int index)
887{
888 drives_opt[index].used = 0;
889 nb_drives_opt--;
890}
891
892int drive_get_index(BlockInterfaceType type, int bus, int unit)
893{
894 int index;
895
896 /* seek interface, bus and unit */
897
898 for (index = 0; index < MAX_DRIVES; index++)
899 if (drives_table[index].type == type &&
900 drives_table[index].bus == bus &&
901 drives_table[index].unit == unit &&
902 drives_table[index].used)
903 return index;
904
905 return -1;
906}
907
908int drive_get_max_bus(BlockInterfaceType type)
909{
910 int max_bus;
911 int index;
912
913 max_bus = -1;
914 for (index = 0; index < nb_drives; index++) {
915 if(drives_table[index].type == type &&
916 drives_table[index].bus > max_bus)
917 max_bus = drives_table[index].bus;
918 }
919 return max_bus;
920}
921
922const char *drive_get_serial(BlockDriverState *bdrv)
923{
924 int index;
925
926 for (index = 0; index < nb_drives; index++)
927 if (drives_table[index].bdrv == bdrv)
928 return drives_table[index].serial;
929
930 return "\0";
931}
932
933BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
934{
935 int index;
936
937 for (index = 0; index < nb_drives; index++)
938 if (drives_table[index].bdrv == bdrv)
939 return drives_table[index].onerror;
940
941 return BLOCK_ERR_STOP_ENOSPC;
942}
943
944static void bdrv_format_print(void *opaque, const char *name)
945{
946 fprintf(stderr, " %s", name);
947}
948
949void drive_uninit(BlockDriverState *bdrv)
950{
951 int i;
952
953 for (i = 0; i < MAX_DRIVES; i++)
954 if (drives_table[i].bdrv == bdrv) {
955 drives_table[i].bdrv = NULL;
956 drives_table[i].used = 0;
957 drive_remove(drives_table[i].drive_opt_idx);
958 nb_drives--;
959 break;
960 }
961}
962
963int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
964{
965 char buf[128];
966 char file[1024];
967 char devname[128];
968 char serial[21];
969 const char *mediastr = "";
970 BlockInterfaceType type;
971 enum { MEDIA_DISK, MEDIA_CDROM } media;
972 int bus_id, unit_id;
973 int cyls, heads, secs, translation;
974 BlockDriverState *bdrv;
975 BlockDriver *drv = NULL;
976 QEMUMachine *machine = opaque;
977 int max_devs;
978 int index;
979 int cache;
980 int bdrv_flags, onerror;
981 int drives_table_idx;
982 char *str = arg->opt;
983 static const char * const params[] = { "bus", "unit", "if", "index",
984 "cyls", "heads", "secs", "trans",
985 "media", "snapshot", "file",
986 "cache", "format", "serial", "werror",
987 NULL };
988
989 if (check_params(buf, sizeof(buf), params, str) < 0) {
990 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
991 buf, str);
992 return -1;
993 }
994
995 file[0] = 0;
996 cyls = heads = secs = 0;
997 bus_id = 0;
998 unit_id = -1;
999 translation = BIOS_ATA_TRANSLATION_AUTO;
1000 index = -1;
1001 cache = 3;
1002
1003 if (machine->use_scsi) {
1004 type = IF_SCSI;
1005 max_devs = MAX_SCSI_DEVS;
1006 pstrcpy(devname, sizeof(devname), "scsi");
1007 } else {
1008 type = IF_IDE;
1009 max_devs = MAX_IDE_DEVS;
1010 pstrcpy(devname, sizeof(devname), "ide");
1011 }
1012 media = MEDIA_DISK;
1013
1014 /* extract parameters */
1015
1016 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1017 bus_id = strtol(buf, NULL, 0);
1018 if (bus_id < 0) {
1019 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1020 return -1;
1021 }
1022 }
1023
1024 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1025 unit_id = strtol(buf, NULL, 0);
1026 if (unit_id < 0) {
1027 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1028 return -1;
1029 }
1030 }
1031
1032 if (get_param_value(buf, sizeof(buf), "if", str)) {
1033 pstrcpy(devname, sizeof(devname), buf);
1034 if (!strcmp(buf, "ide")) {
1035 type = IF_IDE;
1036 max_devs = MAX_IDE_DEVS;
1037 } else if (!strcmp(buf, "scsi")) {
1038 type = IF_SCSI;
1039 max_devs = MAX_SCSI_DEVS;
1040 } else if (!strcmp(buf, "floppy")) {
1041 type = IF_FLOPPY;
1042 max_devs = 0;
1043 } else if (!strcmp(buf, "pflash")) {
1044 type = IF_PFLASH;
1045 max_devs = 0;
1046 } else if (!strcmp(buf, "mtd")) {
1047 type = IF_MTD;
1048 max_devs = 0;
1049 } else if (!strcmp(buf, "sd")) {
1050 type = IF_SD;
1051 max_devs = 0;
1052 } else if (!strcmp(buf, "virtio")) {
1053 type = IF_VIRTIO;
1054 max_devs = 0;
1055 } else if (!strcmp(buf, "xen")) {
1056 type = IF_XEN;
1057 max_devs = 0;
1058 } else {
1059 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1060 return -1;
1061 }
1062 }
1063
1064 if (get_param_value(buf, sizeof(buf), "index", str)) {
1065 index = strtol(buf, NULL, 0);
1066 if (index < 0) {
1067 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1068 return -1;
1069 }
1070 }
1071
1072 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1073 cyls = strtol(buf, NULL, 0);
1074 }
1075
1076 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1077 heads = strtol(buf, NULL, 0);
1078 }
1079
1080 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1081 secs = strtol(buf, NULL, 0);
1082 }
1083
1084 if (cyls || heads || secs) {
1085 if (cyls < 1 || cyls > 16383) {
1086 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1087 return -1;
1088 }
1089 if (heads < 1 || heads > 16) {
1090 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1091 return -1;
1092 }
1093 if (secs < 1 || secs > 63) {
1094 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1095 return -1;
1096 }
1097 }
1098
1099 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1100 if (!cyls) {
1101 fprintf(stderr,
1102 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1103 str);
1104 return -1;
1105 }
1106 if (!strcmp(buf, "none"))
1107 translation = BIOS_ATA_TRANSLATION_NONE;
1108 else if (!strcmp(buf, "lba"))
1109 translation = BIOS_ATA_TRANSLATION_LBA;
1110 else if (!strcmp(buf, "auto"))
1111 translation = BIOS_ATA_TRANSLATION_AUTO;
1112 else {
1113 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1114 return -1;
1115 }
1116 }
1117
1118 if (get_param_value(buf, sizeof(buf), "media", str)) {
1119 if (!strcmp(buf, "disk")) {
1120 media = MEDIA_DISK;
1121 } else if (!strcmp(buf, "cdrom")) {
1122 if (cyls || secs || heads) {
1123 fprintf(stderr,
1124 "qemu: '%s' invalid physical CHS format\n", str);
1125 return -1;
1126 }
1127 media = MEDIA_CDROM;
1128 } else {
1129 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1130 return -1;
1131 }
1132 }
1133
1134 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1135 if (!strcmp(buf, "on"))
1136 snapshot = 1;
1137 else if (!strcmp(buf, "off"))
1138 snapshot = 0;
1139 else {
1140 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1141 return -1;
1142 }
1143 }
1144
1145 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1146 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1147 cache = 0;
1148 else if (!strcmp(buf, "writethrough"))
1149 cache = 1;
1150 else if (!strcmp(buf, "writeback"))
1151 cache = 2;
1152 else {
1153 fprintf(stderr, "qemu: invalid cache option\n");
1154 return -1;
1155 }
1156 }
1157
1158 if (get_param_value(buf, sizeof(buf), "format", str)) {
1159 if (strcmp(buf, "?") == 0) {
1160 fprintf(stderr, "qemu: Supported formats:");
1161 bdrv_iterate_format(bdrv_format_print, NULL);
1162 fprintf(stderr, "\n");
1163 return -1;
1164 }
1165 drv = bdrv_find_format(buf);
1166 if (!drv) {
1167 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1168 return -1;
1169 }
1170 }
1171
1172 if (arg->file == NULL)
1173 get_param_value(file, sizeof(file), "file", str);
1174 else
1175 pstrcpy(file, sizeof(file), arg->file);
1176
1177 if (!get_param_value(serial, sizeof(serial), "serial", str))
1178 memset(serial, 0, sizeof(serial));
1179
1180 onerror = BLOCK_ERR_STOP_ENOSPC;
1181 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1182 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1183 fprintf(stderr, "werror is no supported by this format\n");
1184 return -1;
1185 }
1186 if (!strcmp(buf, "ignore"))
1187 onerror = BLOCK_ERR_IGNORE;
1188 else if (!strcmp(buf, "enospc"))
1189 onerror = BLOCK_ERR_STOP_ENOSPC;
1190 else if (!strcmp(buf, "stop"))
1191 onerror = BLOCK_ERR_STOP_ANY;
1192 else if (!strcmp(buf, "report"))
1193 onerror = BLOCK_ERR_REPORT;
1194 else {
1195 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1196 return -1;
1197 }
1198 }
1199
1200 /* compute bus and unit according index */
1201
1202 if (index != -1) {
1203 if (bus_id != 0 || unit_id != -1) {
1204 fprintf(stderr,
1205 "qemu: '%s' index cannot be used with bus and unit\n", str);
1206 return -1;
1207 }
1208 if (max_devs == 0)
1209 {
1210 unit_id = index;
1211 bus_id = 0;
1212 } else {
1213 unit_id = index % max_devs;
1214 bus_id = index / max_devs;
1215 }
1216 }
1217
1218 /* if user doesn't specify a unit_id,
1219 * try to find the first free
1220 */
1221
1222 if (unit_id == -1) {
1223 unit_id = 0;
1224 while (drive_get_index(type, bus_id, unit_id) != -1) {
1225 unit_id++;
1226 if (max_devs && unit_id >= max_devs) {
1227 unit_id -= max_devs;
1228 bus_id++;
1229 }
1230 }
1231 }
1232
1233 /* check unit id */
1234
1235 if (max_devs && unit_id >= max_devs) {
1236 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1237 str, unit_id, max_devs - 1);
1238 return -1;
1239 }
1240
1241 /*
1242 * ignore multiple definitions
1243 */
1244
1245 if (drive_get_index(type, bus_id, unit_id) != -1)
1246 return -2;
1247
1248 /* init */
1249
1250 if (type == IF_IDE || type == IF_SCSI)
1251 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1252 if (max_devs)
1253 snprintf(buf, sizeof(buf), "%s%i%s%i",
1254 devname, bus_id, mediastr, unit_id);
1255 else
1256 snprintf(buf, sizeof(buf), "%s%s%i",
1257 devname, mediastr, unit_id);
1258 bdrv = bdrv_new(buf);
1259 drives_table_idx = drive_get_free_idx();
1260 drives_table[drives_table_idx].bdrv = bdrv;
1261 drives_table[drives_table_idx].type = type;
1262 drives_table[drives_table_idx].bus = bus_id;
1263 drives_table[drives_table_idx].unit = unit_id;
1264 drives_table[drives_table_idx].onerror = onerror;
1265 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1266 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1267 nb_drives++;
1268
1269 switch(type) {
1270 case IF_IDE:
1271 case IF_SCSI:
1272 case IF_XEN:
1273 switch(media) {
1274 case MEDIA_DISK:
1275 if (cyls != 0) {
1276 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1277 bdrv_set_translation_hint(bdrv, translation);
1278 }
1279 break;
1280 case MEDIA_CDROM:
1281 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1282 break;
1283 }
1284 break;
1285 case IF_SD:
1286 /* FIXME: This isn't really a floppy, but it's a reasonable
1287 approximation. */
1288 case IF_FLOPPY:
1289 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1290 break;
1291 case IF_PFLASH:
1292 case IF_MTD:
1293 case IF_VIRTIO:
1294 break;
1295 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001296 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001297 abort();
1298 }
1299 if (!file[0])
1300 return -2;
1301 bdrv_flags = 0;
1302 if (snapshot) {
1303 bdrv_flags |= BDRV_O_SNAPSHOT;
1304 cache = 2; /* always use write-back with snapshot */
1305 }
1306 if (cache == 0) /* no caching */
1307 bdrv_flags |= BDRV_O_NOCACHE;
1308 else if (cache == 2) /* write-back */
1309 bdrv_flags |= BDRV_O_CACHE_WB;
1310 else if (cache == 3) /* not specified */
1311 bdrv_flags |= BDRV_O_CACHE_DEF;
1312 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1313 fprintf(stderr, "qemu: could not open disk image %s\n",
1314 file);
1315 return -1;
1316 }
1317 if (bdrv_key_required(bdrv))
1318 autostart = 0;
1319 return drives_table_idx;
1320}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001321#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001322
1323static void numa_add(const char *optarg)
1324{
1325 char option[128];
1326 char *endptr;
1327 unsigned long long value, endvalue;
1328 int nodenr;
1329
1330 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1331 if (!strcmp(option, "node")) {
1332 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1333 nodenr = nb_numa_nodes;
1334 } else {
1335 nodenr = strtoull(option, NULL, 10);
1336 }
1337
1338 if (get_param_value(option, 128, "mem", optarg) == 0) {
1339 node_mem[nodenr] = 0;
1340 } else {
1341 value = strtoull(option, &endptr, 0);
1342 switch (*endptr) {
1343 case 0: case 'M': case 'm':
1344 value <<= 20;
1345 break;
1346 case 'G': case 'g':
1347 value <<= 30;
1348 break;
1349 }
1350 node_mem[nodenr] = value;
1351 }
1352 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1353 node_cpumask[nodenr] = 0;
1354 } else {
1355 value = strtoull(option, &endptr, 10);
1356 if (value >= 64) {
1357 value = 63;
1358 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1359 } else {
1360 if (*endptr == '-') {
1361 endvalue = strtoull(endptr+1, &endptr, 10);
1362 if (endvalue >= 63) {
1363 endvalue = 62;
1364 fprintf(stderr,
1365 "only 63 CPUs in NUMA mode supported.\n");
1366 }
1367 value = (1 << (endvalue + 1)) - (1 << value);
1368 } else {
1369 value = 1 << value;
1370 }
1371 }
1372 node_cpumask[nodenr] = value;
1373 }
1374 nb_numa_nodes++;
1375 }
1376 return;
1377}
1378
1379/***********************************************************/
1380/* USB devices */
1381
1382static USBPort *used_usb_ports;
1383static USBPort *free_usb_ports;
1384
1385/* ??? Maybe change this to register a hub to keep track of the topology. */
1386void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1387 usb_attachfn attach)
1388{
1389 port->opaque = opaque;
1390 port->index = index;
1391 port->attach = attach;
1392 port->next = free_usb_ports;
1393 free_usb_ports = port;
1394}
1395
1396int usb_device_add_dev(USBDevice *dev)
1397{
1398 USBPort *port;
1399
1400 /* Find a USB port to add the device to. */
1401 port = free_usb_ports;
Manuel Francisco Naranjo58ecd742012-07-18 11:22:09 -03001402 if (!port){
1403 USBDevice *hub = usb_hub_init(VM_USB_HUB_SIZE);
1404 port = free_usb_ports;
1405 usb_attach(port, hub);
1406 }
1407 else if (!port->next){
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001408 USBDevice *hub;
1409
1410 /* Create a new hub and chain it on. */
1411 free_usb_ports = NULL;
1412 port->next = used_usb_ports;
1413 used_usb_ports = port;
1414
1415 hub = usb_hub_init(VM_USB_HUB_SIZE);
1416 usb_attach(port, hub);
1417 port = free_usb_ports;
1418 }
1419
1420 free_usb_ports = port->next;
1421 port->next = used_usb_ports;
1422 used_usb_ports = port;
1423 usb_attach(port, dev);
1424 return 0;
1425}
1426
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001427#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001428static void usb_msd_password_cb(void *opaque, int err)
1429{
1430 USBDevice *dev = opaque;
1431
1432 if (!err)
1433 usb_device_add_dev(dev);
1434 else
1435 dev->handle_destroy(dev);
1436}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001437#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001438
1439static int usb_device_add(const char *devname, int is_hotplug)
1440{
1441 const char *p;
1442 USBDevice *dev;
1443
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001444 if (strstart(devname, "host:", &p)) {
1445 dev = usb_host_device_open(p);
1446 } else if (!strcmp(devname, "mouse")) {
1447 dev = usb_mouse_init();
1448 } else if (!strcmp(devname, "tablet")) {
1449 dev = usb_tablet_init();
1450 } else if (!strcmp(devname, "keyboard")) {
1451 dev = usb_keyboard_init();
1452 } else if (strstart(devname, "disk:", &p)) {
1453#if 0
1454 BlockDriverState *bs;
1455#endif
1456 dev = usb_msd_init(p);
1457 if (!dev)
1458 return -1;
1459#if 0
1460 bs = usb_msd_get_bdrv(dev);
1461 if (bdrv_key_required(bs)) {
1462 autostart = 0;
1463 if (is_hotplug) {
1464 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1465 dev);
1466 return 0;
1467 }
1468 }
1469 } else if (!strcmp(devname, "wacom-tablet")) {
1470 dev = usb_wacom_init();
1471 } else if (strstart(devname, "serial:", &p)) {
1472 dev = usb_serial_init(p);
1473#ifdef CONFIG_BRLAPI
1474 } else if (!strcmp(devname, "braille")) {
1475 dev = usb_baum_init();
1476#endif
1477 } else if (strstart(devname, "net:", &p)) {
1478 int nic = nb_nics;
1479
1480 if (net_client_init("nic", p) < 0)
1481 return -1;
1482 nd_table[nic].model = "usb";
1483 dev = usb_net_init(&nd_table[nic]);
1484 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1485 dev = usb_bt_init(devname[2] ? hci_init(p) :
1486 bt_new_hci(qemu_find_bt_vlan(0)));
1487#endif
1488 } else {
1489 return -1;
1490 }
1491 if (!dev)
1492 return -1;
1493
1494 return usb_device_add_dev(dev);
1495}
1496
1497int usb_device_del_addr(int bus_num, int addr)
1498{
1499 USBPort *port;
1500 USBPort **lastp;
1501 USBDevice *dev;
1502
1503 if (!used_usb_ports)
1504 return -1;
1505
1506 if (bus_num != 0)
1507 return -1;
1508
1509 lastp = &used_usb_ports;
1510 port = used_usb_ports;
1511 while (port && port->dev->addr != addr) {
1512 lastp = &port->next;
1513 port = port->next;
1514 }
1515
1516 if (!port)
1517 return -1;
1518
1519 dev = port->dev;
1520 *lastp = port->next;
1521 usb_attach(port, NULL);
1522 dev->handle_destroy(dev);
1523 port->next = free_usb_ports;
1524 free_usb_ports = port;
1525 return 0;
1526}
1527
1528static int usb_device_del(const char *devname)
1529{
1530 int bus_num, addr;
1531 const char *p;
1532
1533 if (strstart(devname, "host:", &p))
1534 return usb_host_device_close(p);
1535
1536 if (!used_usb_ports)
1537 return -1;
1538
1539 p = strchr(devname, '.');
1540 if (!p)
1541 return -1;
1542 bus_num = strtoul(devname, NULL, 0);
1543 addr = strtoul(p + 1, NULL, 0);
1544
1545 return usb_device_del_addr(bus_num, addr);
1546}
1547
1548void do_usb_add(Monitor *mon, const char *devname)
1549{
1550 usb_device_add(devname, 1);
1551}
1552
1553void do_usb_del(Monitor *mon, const char *devname)
1554{
1555 usb_device_del(devname);
1556}
1557
1558void usb_info(Monitor *mon)
1559{
1560 USBDevice *dev;
1561 USBPort *port;
1562 const char *speed_str;
1563
1564 if (!usb_enabled) {
1565 monitor_printf(mon, "USB support not enabled\n");
1566 return;
1567 }
1568
1569 for (port = used_usb_ports; port; port = port->next) {
1570 dev = port->dev;
1571 if (!dev)
1572 continue;
1573 switch(dev->speed) {
1574 case USB_SPEED_LOW:
1575 speed_str = "1.5";
1576 break;
1577 case USB_SPEED_FULL:
1578 speed_str = "12";
1579 break;
1580 case USB_SPEED_HIGH:
1581 speed_str = "480";
1582 break;
1583 default:
1584 speed_str = "?";
1585 break;
1586 }
1587 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1588 0, dev->addr, speed_str, dev->devname);
1589 }
1590}
1591
1592/***********************************************************/
1593/* PCMCIA/Cardbus */
1594
1595static struct pcmcia_socket_entry_s {
1596 PCMCIASocket *socket;
1597 struct pcmcia_socket_entry_s *next;
1598} *pcmcia_sockets = 0;
1599
1600void pcmcia_socket_register(PCMCIASocket *socket)
1601{
1602 struct pcmcia_socket_entry_s *entry;
1603
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001604 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001605 entry->socket = socket;
1606 entry->next = pcmcia_sockets;
1607 pcmcia_sockets = entry;
1608}
1609
1610void pcmcia_socket_unregister(PCMCIASocket *socket)
1611{
1612 struct pcmcia_socket_entry_s *entry, **ptr;
1613
1614 ptr = &pcmcia_sockets;
1615 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1616 if (entry->socket == socket) {
1617 *ptr = entry->next;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001618 g_free(entry);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001619 }
1620}
1621
1622void pcmcia_info(Monitor *mon)
1623{
1624 struct pcmcia_socket_entry_s *iter;
1625
1626 if (!pcmcia_sockets)
1627 monitor_printf(mon, "No PCMCIA sockets\n");
1628
1629 for (iter = pcmcia_sockets; iter; iter = iter->next)
1630 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1631 iter->socket->attached ? iter->socket->card_string :
1632 "Empty");
1633}
1634
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001635/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001636/* machine registration */
1637
1638static QEMUMachine *first_machine = NULL;
1639QEMUMachine *current_machine = NULL;
1640
1641int qemu_register_machine(QEMUMachine *m)
1642{
1643 QEMUMachine **pm;
1644 pm = &first_machine;
1645 while (*pm != NULL)
1646 pm = &(*pm)->next;
1647 m->next = NULL;
1648 *pm = m;
1649 return 0;
1650}
1651
1652static QEMUMachine *find_machine(const char *name)
1653{
1654 QEMUMachine *m;
1655
1656 for(m = first_machine; m != NULL; m = m->next) {
1657 if (!strcmp(m->name, name))
1658 return m;
1659 }
1660 return NULL;
1661}
1662
1663static QEMUMachine *find_default_machine(void)
1664{
1665 QEMUMachine *m;
1666
1667 for(m = first_machine; m != NULL; m = m->next) {
1668 if (m->is_default) {
1669 return m;
1670 }
1671 }
1672 return NULL;
1673}
1674
1675/***********************************************************/
1676/* main execution loop */
1677
1678static void gui_update(void *opaque)
1679{
1680 uint64_t interval = GUI_REFRESH_INTERVAL;
1681 DisplayState *ds = opaque;
1682 DisplayChangeListener *dcl = ds->listeners;
1683
1684 dpy_refresh(ds);
1685
1686 while (dcl != NULL) {
1687 if (dcl->gui_timer_interval &&
1688 dcl->gui_timer_interval < interval)
1689 interval = dcl->gui_timer_interval;
1690 dcl = dcl->next;
1691 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001692 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001693}
1694
1695static void nographic_update(void *opaque)
1696{
1697 uint64_t interval = GUI_REFRESH_INTERVAL;
1698
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001699 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001700}
1701
1702struct vm_change_state_entry {
1703 VMChangeStateHandler *cb;
1704 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001705 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001706};
1707
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001708static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001709
1710VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1711 void *opaque)
1712{
1713 VMChangeStateEntry *e;
1714
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001715 e = g_malloc0(sizeof (*e));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001716
1717 e->cb = cb;
1718 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001719 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001720 return e;
1721}
1722
1723void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1724{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001725 QLIST_REMOVE (e, entries);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001726 g_free (e);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001727}
1728
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001729void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001730{
1731 VMChangeStateEntry *e;
1732
1733 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1734 e->cb(e->opaque, running, reason);
1735 }
1736}
1737
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001738void vm_start(void)
1739{
1740 if (!vm_running) {
1741 cpu_enable_ticks();
1742 vm_running = 1;
1743 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001744 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001745 resume_all_vcpus();
1746 }
1747}
1748
1749/* reset/shutdown handler */
1750
1751typedef struct QEMUResetEntry {
1752 QEMUResetHandler *func;
1753 void *opaque;
1754 int order;
1755 struct QEMUResetEntry *next;
1756} QEMUResetEntry;
1757
1758static QEMUResetEntry *first_reset_entry;
1759static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001760static int shutdown_requested, shutdown_signal = -1;
1761static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001762static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001763int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001764static int vmstop_requested;
1765
1766int qemu_shutdown_requested(void)
1767{
1768 int r = shutdown_requested;
1769 shutdown_requested = 0;
1770 return r;
1771}
1772
1773int qemu_reset_requested(void)
1774{
1775 int r = reset_requested;
1776 reset_requested = 0;
1777 return r;
1778}
1779
1780int qemu_powerdown_requested(void)
1781{
1782 int r = powerdown_requested;
1783 powerdown_requested = 0;
1784 return r;
1785}
1786
1787static int qemu_debug_requested(void)
1788{
1789 int r = debug_requested;
1790 debug_requested = 0;
1791 return r;
1792}
1793
1794static int qemu_vmstop_requested(void)
1795{
1796 int r = vmstop_requested;
1797 vmstop_requested = 0;
1798 return r;
1799}
1800
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001801void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1802{
1803 QEMUResetEntry **pre, *re;
1804
1805 pre = &first_reset_entry;
1806 while (*pre != NULL && (*pre)->order >= order) {
1807 pre = &(*pre)->next;
1808 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01001809 re = g_malloc0(sizeof(QEMUResetEntry));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001810 re->func = func;
1811 re->opaque = opaque;
1812 re->order = order;
1813 re->next = NULL;
1814 *pre = re;
1815}
1816
1817void qemu_system_reset(void)
1818{
1819 QEMUResetEntry *re;
1820
1821 /* reset all devices */
1822 for(re = first_reset_entry; re != NULL; re = re->next) {
1823 re->func(re->opaque);
1824 }
1825}
1826
1827void qemu_system_reset_request(void)
1828{
1829 if (no_reboot) {
1830 shutdown_requested = 1;
1831 } else {
1832 reset_requested = 1;
1833 }
1834 qemu_notify_event();
1835}
1836
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001837void qemu_system_killed(int signal, pid_t pid)
1838{
1839 shutdown_signal = signal;
1840 shutdown_pid = pid;
1841 qemu_system_shutdown_request();
1842}
1843
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001844void qemu_system_shutdown_request(void)
1845{
1846 shutdown_requested = 1;
1847 qemu_notify_event();
1848}
1849
1850void qemu_system_powerdown_request(void)
1851{
1852 powerdown_requested = 1;
1853 qemu_notify_event();
1854}
1855
1856#ifdef CONFIG_IOTHREAD
1857static void qemu_system_vmstop_request(int reason)
1858{
1859 vmstop_requested = reason;
1860 qemu_notify_event();
1861}
1862#endif
1863
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001864void main_loop_wait(int timeout)
1865{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001866 fd_set rfds, wfds, xfds;
1867 int ret, nfds;
1868 struct timeval tv;
1869
1870 qemu_bh_update_timeout(&timeout);
1871
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001872 os_host_main_loop_wait(&timeout);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001873
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001874
1875 tv.tv_sec = timeout / 1000;
1876 tv.tv_usec = (timeout % 1000) * 1000;
1877
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001878 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001879
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001880 /* XXX: separate device handlers from system ones */
1881 nfds = -1;
1882 FD_ZERO(&rfds);
1883 FD_ZERO(&wfds);
1884 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001885 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001886 if (slirp_is_inited()) {
1887 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1888 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001889
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001890 qemu_mutex_unlock_iothread();
1891 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1892 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001893 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001894 if (slirp_is_inited()) {
1895 if (ret < 0) {
1896 FD_ZERO(&rfds);
1897 FD_ZERO(&wfds);
1898 FD_ZERO(&xfds);
1899 }
1900 slirp_select_poll(&rfds, &wfds, &xfds);
1901 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001902 charpipe_poll();
1903
David Turner6a9ef172010-09-09 22:54:36 +02001904 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07001905
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001906 /* Check bottom-halves last in case any of the earlier events triggered
1907 them. */
1908 qemu_bh_poll();
1909
1910}
1911
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001912static int vm_can_run(void)
1913{
1914 if (powerdown_requested)
1915 return 0;
1916 if (reset_requested)
1917 return 0;
1918 if (shutdown_requested)
1919 return 0;
1920 if (debug_requested)
1921 return 0;
1922 return 1;
1923}
1924
1925static void main_loop(void)
1926{
1927 int r;
1928
1929#ifdef CONFIG_IOTHREAD
1930 qemu_system_ready = 1;
1931 qemu_cond_broadcast(&qemu_system_cond);
1932#endif
1933
Jun Nakajimaa381ef02011-12-17 19:13:25 -08001934#ifdef CONFIG_HAX
1935 if (hax_enabled())
1936 hax_sync_vcpus();
1937#endif
1938
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001939 for (;;) {
1940 do {
1941#ifdef CONFIG_PROFILER
1942 int64_t ti;
1943#endif
1944#ifndef CONFIG_IOTHREAD
1945 tcg_cpu_exec();
1946#endif
1947#ifdef CONFIG_PROFILER
1948 ti = profile_getclock();
1949#endif
1950 main_loop_wait(qemu_calculate_timeout());
1951#ifdef CONFIG_PROFILER
1952 dev_time += profile_getclock() - ti;
1953#endif
rich canningsd952f282011-03-01 15:40:09 -08001954
1955 if (rotate_logs_requested) {
1956 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
1957 dns_log_filename);
1958 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
1959 drop_log_filename);
1960 slirp_dns_log_fd(new_dns_log_fd);
1961 slirp_drop_log_fd(new_drop_log_fd);
1962 reset_rotate_qemu_logs_request();
1963 }
1964
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001965 } while (vm_can_run());
1966
1967 if (qemu_debug_requested())
1968 vm_stop(EXCP_DEBUG);
1969 if (qemu_shutdown_requested()) {
1970 if (no_shutdown) {
1971 vm_stop(0);
1972 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00001973 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00001974 if (savevm_on_exit != NULL) {
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08001975 /* Prior to saving VM to the snapshot file, save HW config
1976 * settings for that VM, so we can match them when VM gets
1977 * loaded from the snapshot. */
1978 snaphost_save_config(savevm_on_exit);
Tim Baverstock24204cc2010-11-25 11:37:43 +00001979 do_savevm(cur_mon, savevm_on_exit);
1980 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001981 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00001982 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001983 }
1984 if (qemu_reset_requested()) {
1985 pause_all_vcpus();
1986 qemu_system_reset();
1987 resume_all_vcpus();
1988 }
1989 if (qemu_powerdown_requested())
1990 qemu_system_powerdown();
1991 if ((r = qemu_vmstop_requested()))
1992 vm_stop(r);
1993 }
1994 pause_all_vcpus();
1995}
1996
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07001997void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001998{
1999 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2000}
2001
2002void qemu_help(int exitcode)
2003{
2004 version();
2005 printf("usage: %s [options] [disk_image]\n"
2006 "\n"
2007 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2008 "\n"
2009#define DEF(option, opt_arg, opt_enum, opt_help) \
2010 opt_help
2011#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002012#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002013#undef DEF
2014#undef DEFHEADING
2015#undef GEN_DOCS
2016 "\n"
2017 "During emulation, the following keys are useful:\n"
2018 "ctrl-alt-f toggle full screen\n"
2019 "ctrl-alt-n switch to virtual console 'n'\n"
2020 "ctrl-alt toggle mouse and keyboard grab\n"
2021 "\n"
2022 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2023 ,
2024 "qemu",
2025 DEFAULT_RAM_SIZE,
2026#ifndef _WIN32
2027 DEFAULT_NETWORK_SCRIPT,
2028 DEFAULT_NETWORK_DOWN_SCRIPT,
2029#endif
2030 DEFAULT_GDBSTUB_PORT,
2031 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002032 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002033}
2034
2035#define HAS_ARG 0x0001
2036
2037enum {
2038#define DEF(option, opt_arg, opt_enum, opt_help) \
2039 opt_enum,
2040#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002041#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002042#undef DEF
2043#undef DEFHEADING
2044#undef GEN_DOCS
2045};
2046
2047typedef struct QEMUOption {
2048 const char *name;
2049 int flags;
2050 int index;
2051} QEMUOption;
2052
2053static const QEMUOption qemu_options[] = {
2054 { "h", 0, QEMU_OPTION_h },
2055#define DEF(option, opt_arg, opt_enum, opt_help) \
2056 { option, opt_arg, opt_enum },
2057#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002058#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002059#undef DEF
2060#undef DEFHEADING
2061#undef GEN_DOCS
2062 { NULL, 0, 0 },
2063};
2064
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002065static void select_vgahw (const char *p)
2066{
2067 const char *opts;
2068
2069 cirrus_vga_enabled = 0;
2070 std_vga_enabled = 0;
2071 vmsvga_enabled = 0;
2072 xenfb_enabled = 0;
2073 if (strstart(p, "std", &opts)) {
2074 std_vga_enabled = 1;
2075 } else if (strstart(p, "cirrus", &opts)) {
2076 cirrus_vga_enabled = 1;
2077 } else if (strstart(p, "vmware", &opts)) {
2078 vmsvga_enabled = 1;
2079 } else if (strstart(p, "xenfb", &opts)) {
2080 xenfb_enabled = 1;
2081 } else if (!strstart(p, "none", &opts)) {
2082 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002083 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002084 }
2085 while (*opts) {
2086 const char *nextopt;
2087
2088 if (strstart(opts, ",retrace=", &nextopt)) {
2089 opts = nextopt;
2090 if (strstart(opts, "dumb", &nextopt))
2091 vga_retrace_method = VGA_RETRACE_DUMB;
2092 else if (strstart(opts, "precise", &nextopt))
2093 vga_retrace_method = VGA_RETRACE_PRECISE;
2094 else goto invalid_vga;
2095 } else goto invalid_vga;
2096 opts = nextopt;
2097 }
2098}
2099
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002100#define MAX_NET_CLIENTS 32
2101
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002102#ifdef _WIN32
2103/* Look for support files in the same directory as the executable. */
2104static char *find_datadir(const char *argv0)
2105{
2106 char *p;
2107 char buf[MAX_PATH];
2108 DWORD len;
2109
2110 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2111 if (len == 0) {
2112 return NULL;
2113 }
2114
2115 buf[len] = 0;
2116 p = buf + len - 1;
2117 while (p != buf && *p != '\\')
2118 p--;
2119 *p = 0;
2120 if (access(buf, R_OK) == 0) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002121 return g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002122 }
2123 return NULL;
2124}
2125#else /* !_WIN32 */
2126
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002127/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002128static char *find_datadir(const char *argv0)
2129{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002130 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002131 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002132
2133#if defined(__linux__)
2134 {
2135 int len;
2136 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2137 if (len > 0) {
2138 buf[len] = 0;
2139 p = buf;
2140 }
2141 }
2142#elif defined(__FreeBSD__)
2143 {
2144 int len;
2145 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2146 if (len > 0) {
2147 buf[len] = 0;
2148 p = buf;
2149 }
2150 }
2151#endif
2152 /* If we don't have any way of figuring out the actual executable
2153 location then try argv[0]. */
2154 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002155 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002156 if (!p) {
2157 return NULL;
2158 }
2159 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002160
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002161 return g_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002162}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002163#endif
2164
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002165static char*
2166qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
2167{
2168 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002169 char* buf = g_malloc0(len);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002170
2171 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2172 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2173 if (access(buf, R_OK)) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01002174 g_free(buf);
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002175 return NULL;
2176 }
2177 return buf;
2178}
2179
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002180char *qemu_find_file(int type, const char *name)
2181{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002182 const char *subdir;
2183 char *buf;
2184
2185 /* If name contains path separators then try it as a straight path. */
2186 if ((strchr(name, '/') || strchr(name, '\\'))
2187 && access(name, R_OK) == 0) {
2188 return strdup(name);
2189 }
2190 switch (type) {
2191 case QEMU_FILE_TYPE_BIOS:
2192 subdir = "";
2193 break;
2194 case QEMU_FILE_TYPE_KEYMAP:
2195 subdir = "keymaps/";
2196 break;
2197 default:
2198 abort();
2199 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002200 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2201#ifdef CONFIG_ANDROID
2202 if (type == QEMU_FILE_TYPE_BIOS) {
2203 /* This case corresponds to the emulator being used as part of an
2204 * SDK installation. NOTE: data_dir is really $bindir. */
2205 if (buf == NULL)
2206 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2207 /* This case corresponds to platform builds. */
2208 if (buf == NULL)
2209 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2210 /* Finally, try this for standalone builds under external/qemu */
2211 if (buf == NULL)
Andrew Hsieh3d894282012-05-02 12:06:12 +08002212 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilts/qemu-kernel/x86/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002213 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002214#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002215 return buf;
2216}
2217
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002218static int
2219add_dns_server( const char* server_name )
2220{
2221 SockAddress addr;
2222
2223 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2224 fprintf(stdout,
2225 "### WARNING: can't resolve DNS server name '%s'\n",
2226 server_name );
2227 return -1;
2228 }
2229
2230 fprintf(stderr,
2231 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2232
2233 if ( slirp_add_dns_server( &addr ) < 0 ) {
2234 fprintf(stderr,
2235 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2236 return -1;
2237 }
2238 return 0;
2239}
2240
rich cannings7339b552011-02-16 13:43:44 -08002241/* Parses an integer
2242 * Pararm:
2243 * str String containing a number to be parsed.
2244 * result Passes the parsed integer in this argument
2245 * returns 0 if ok, -1 if failed
2246 */
2247int
2248parse_int(const char *str, int *result)
2249{
2250 char* r;
2251 *result = strtol(str, &r, 0);
2252 if (r == NULL || *r != '\0')
2253 return -1;
2254
2255 return 0;
2256}
2257
rich canningsd952f282011-03-01 15:40:09 -08002258#ifndef _WIN32
2259/*
2260 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
2261 */
2262void init_qemu_clear_logs_sig() {
2263 struct sigaction act;
2264 sigfillset(&act.sa_mask);
2265 act.sa_flags = 0;
2266 act.sa_handler = rotate_qemu_logs_handler;
2267 if (sigaction(SIGUSR1, &act, NULL) == -1) {
2268 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
2269 exit(-1);
2270 }
2271}
2272#endif
2273
2274
rich cannings7339b552011-02-16 13:43:44 -08002275
2276/* parses a null-terminated string specifying a network port (e.g., "80") or
2277 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2278 * are the same. Returns 0 on success, -1 on error. */
2279
2280int parse_port_range(const char *str, unsigned short *lport,
2281 unsigned short *hport) {
2282
2283 unsigned int low = 0, high = 0;
2284 char *p, *arg = strdup(str);
2285
2286 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2287 p = arg + 1; /* skip '[' */
2288 low = atoi(strtok(p, "-"));
2289 high = atoi(strtok(NULL, "-"));
2290 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2291 *lport = low;
2292 *hport = high;
2293 }
2294 }
2295 else {
2296 low = atoi(arg);
2297 if ((0 < low) && (low < 65535)) {
2298 *lport = low;
2299 *hport = low;
2300 }
2301 }
2302 free(arg);
2303 if (low != 0)
2304 return 0;
2305 return -1;
2306}
2307
2308/*
2309 * Implements the generic port forwarding option
2310 */
2311void
2312net_slirp_forward(const char *optarg)
2313{
2314 /*
2315 * we expect the following format:
2316 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2317 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2318 */
2319 char *argument = strdup(optarg), *p = argument;
2320 char *dst_net, *dst_mask, *dst_port;
2321 char *redirect_ip, *redirect_port;
2322 uint32_t dnet, dmask, rip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002323 unsigned short dlport = 0, dhport = 0, rport;
rich cannings7339b552011-02-16 13:43:44 -08002324
2325
2326 dst_net = strtok(p, ":");
2327 dst_mask = strtok(NULL, ":");
2328 dst_port = strtok(NULL, ":");
2329 redirect_ip = strtok(NULL, ":");
2330 redirect_port = strtok(NULL, ":");
2331
2332 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2333 redirect_ip == NULL || redirect_port == NULL) {
2334 fprintf(stderr,
2335 "Invalid argument for -net-forward, we expect "
2336 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2337 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2338 ":redirect_ip:redirect_port: %s\n",
2339 optarg);
2340 exit(1);
2341 }
2342
2343 /* inet_strtoip converts dotted address to host byte order */
2344 if (inet_strtoip(dst_net, &dnet) == -1) {
2345 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2346 exit(1);
2347 }
2348 if (inet_strtoip(dst_mask, &dmask) == -1) {
2349 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2350 exit(1);
2351 }
2352 if (inet_strtoip(redirect_ip, &rip) == -1) {
2353 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2354 exit(1);
2355 }
2356
2357 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2358 fprintf(stderr, "Invalid destination port or port range\n");
2359 exit(1);
2360 }
2361
2362 rport = atoi(redirect_port);
2363 if (!rport) {
2364 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2365 exit(1);
2366 }
2367
2368 dnet &= dmask;
2369
2370 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2371 rip, rport);
2372
2373 free(argument);
2374}
2375
2376
2377/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2378 * entry in the allows list */
2379void
2380slirp_allow(const char *optarg, u_int8_t proto)
2381{
2382 /*
2383 * we expect the following format:
2384 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2385 */
2386 char *argument = strdup(optarg), *p = argument;
2387 char *dst_ip_str, *dst_port_str;
2388 uint32_t dst_ip;
Vladimir Chtchetkineb557e9f2012-03-22 15:38:08 -07002389 unsigned short dst_lport = 0, dst_hport = 0;
rich cannings7339b552011-02-16 13:43:44 -08002390
2391 dst_ip_str = strtok(p, ":");
2392 dst_port_str = strtok(NULL, ":");
2393
2394 if (dst_ip_str == NULL || dst_port_str == NULL) {
2395 fprintf(stderr,
2396 "Invalid argument %s for -allow. We expect "
2397 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2398 optarg);
2399 exit(1);
2400 }
2401
2402 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2403 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2404 exit(1);
2405 }
2406 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2407 fprintf(stderr, "Invalid destination port or port range\n");
2408 exit(1);
2409 }
2410
2411 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2412
2413 free(argument);
2414}
2415
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002416/* Add a serial device at a given location in the emulated hardware table.
2417 * On failure, this function aborts the program with an error message.
2418 */
2419static void
2420serial_hds_add_at(int index, const char* devname)
2421{
2422 char label[32];
2423
2424 if (!devname || !strcmp(devname,"none"))
2425 return;
2426
2427 if (index >= MAX_SERIAL_PORTS) {
2428 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2429 devname, index, MAX_SERIAL_PORTS);
2430 }
2431 if (serial_hds[index] != NULL) {
2432 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2433 devname, index);
2434 }
2435 snprintf(label, sizeof(label), "serial%d", index);
2436 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2437 if (!serial_hds[index]) {
2438 PANIC("qemu: could not open serial device '%s'", devname);
2439 }
2440}
2441
2442
2443/* Find a free slot in the emulated serial device table, and register
2444 * it. Return the allocated table index.
2445 */
2446static int
2447serial_hds_add(const char* devname)
2448{
2449 int index;
2450
2451 /* Find first free slot */
2452 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2453 if (serial_hds[index] == NULL) {
2454 serial_hds_add_at(index, devname);
2455 return index;
2456 }
2457 }
2458
2459 PANIC("qemu: too many serial devices registered (%d)", index);
2460 return -1; /* shouldn't happen */
2461}
2462
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002463int main(int argc, char **argv, char **envp)
2464{
2465 const char *gdbstub_dev = NULL;
2466 uint32_t boot_devices_bitmap = 0;
2467 int i;
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +01002468 int snapshot, linux_boot, __attribute__((unused)) net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002469 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002470 const char *initrd_filename;
2471 const char *kernel_filename, *kernel_cmdline;
2472 const char *boot_devices = "";
2473 DisplayState *ds;
2474 DisplayChangeListener *dcl;
2475 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002476 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002477 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002478 const char *net_clients[MAX_NET_CLIENTS];
2479 int nb_net_clients;
2480 const char *bt_opts[MAX_BT_CMDLINE];
2481 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002482 int optind;
2483 const char *r, *optarg;
2484 CharDriverState *monitor_hd = NULL;
2485 const char *monitor_device;
2486 const char *serial_devices[MAX_SERIAL_PORTS];
2487 int serial_device_index;
2488 const char *parallel_devices[MAX_PARALLEL_PORTS];
2489 int parallel_device_index;
2490 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2491 int virtio_console_index;
2492 const char *loadvm = NULL;
2493 QEMUMachine *machine;
2494 const char *cpu_model;
2495 const char *usb_devices[MAX_USB_CMDLINE];
2496 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002497 int tb_size;
2498 const char *pid_file = NULL;
2499 const char *incoming = NULL;
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002500 const char* log_mask = NULL;
2501 const char* log_file = NULL;
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01002502 CPUOldState *env;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002503 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002504 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002505 STRALLOC_DEFINE(kernel_params);
2506 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002507 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002508
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002509 /* Initialize sockets before anything else, so we can properly report
2510 * initialization failures back to the UI. */
2511#ifdef _WIN32
2512 socket_init();
2513#endif
2514
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002515 init_clocks();
2516
David 'Digit' Turner8848f632014-01-16 18:10:21 +01002517 qemu_cache_utils_init();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002518
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002519 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002520 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002521
2522 module_call_init(MODULE_INIT_MACHINE);
2523 machine = find_default_machine();
2524 cpu_model = NULL;
2525 initrd_filename = NULL;
2526 ram_size = 0;
2527 snapshot = 0;
2528 kernel_filename = NULL;
2529 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002530
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002531 cyls = heads = secs = 0;
2532 translation = BIOS_ATA_TRANSLATION_AUTO;
2533 monitor_device = "vc:80Cx24C";
2534
2535 serial_devices[0] = "vc:80Cx24C";
2536 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2537 serial_devices[i] = NULL;
2538 serial_device_index = 0;
2539
2540 parallel_devices[0] = "vc:80Cx24C";
2541 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2542 parallel_devices[i] = NULL;
2543 parallel_device_index = 0;
2544
2545 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2546 virtio_consoles[i] = NULL;
2547 virtio_console_index = 0;
2548
2549 for (i = 0; i < MAX_NODES; i++) {
2550 node_mem[i] = 0;
2551 node_cpumask[i] = 0;
2552 }
2553
2554 usb_devices_index = 0;
2555
2556 nb_net_clients = 0;
2557 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002558#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002559 nb_drives = 0;
2560 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002561#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002562 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002563
2564 nb_nics = 0;
2565
2566 tb_size = 0;
2567 autostart= 1;
2568
2569 register_watchdogs();
2570
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002571 /* Initialize boot properties. */
2572 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002573 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002574 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002575
David 'Digit' Turner36597752011-05-20 01:18:01 +02002576#ifdef CONFIG_KVM
2577 /* By default, force auto-detection for kvm */
2578 kvm_allowed = -1;
2579#endif
2580
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002581 optind = 1;
2582 for(;;) {
2583 if (optind >= argc)
2584 break;
2585 r = argv[optind];
2586 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002587 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002588 } else {
2589 const QEMUOption *popt;
2590
2591 optind++;
2592 /* Treat --foo the same as -foo. */
2593 if (r[1] == '-')
2594 r++;
2595 popt = qemu_options;
2596 for(;;) {
2597 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002598 PANIC("%s: invalid option -- '%s'",
2599 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002600 }
2601 if (!strcmp(popt->name, r + 1))
2602 break;
2603 popt++;
2604 }
2605 if (popt->flags & HAS_ARG) {
2606 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002607 PANIC("%s: option '%s' requires an argument",
2608 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002609 }
2610 optarg = argv[optind++];
2611 } else {
2612 optarg = NULL;
2613 }
2614
2615 switch(popt->index) {
2616 case QEMU_OPTION_M:
2617 machine = find_machine(optarg);
2618 if (!machine) {
2619 QEMUMachine *m;
2620 printf("Supported machines are:\n");
2621 for(m = first_machine; m != NULL; m = m->next) {
2622 printf("%-10s %s%s\n",
2623 m->name, m->desc,
2624 m->is_default ? " (default)" : "");
2625 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002626 if (*optarg != '?') {
2627 PANIC("Invalid machine parameter: %s",
2628 optarg);
2629 } else {
2630 QEMU_EXIT(0);
2631 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002632 }
2633 break;
2634 case QEMU_OPTION_cpu:
2635 /* hw initialization will check this */
2636 if (*optarg == '?') {
2637/* XXX: implement xxx_cpu_list for targets that still miss it */
2638#if defined(cpu_list)
2639 cpu_list(stdout, &fprintf);
2640#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002641 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002642 } else {
2643 cpu_model = optarg;
2644 }
2645 break;
2646 case QEMU_OPTION_initrd:
2647 initrd_filename = optarg;
2648 break;
2649 case QEMU_OPTION_hda:
2650 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002651 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002652 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002653 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002654 ",cyls=%d,heads=%d,secs=%d%s",
2655 0, cyls, heads, secs,
2656 translation == BIOS_ATA_TRANSLATION_LBA ?
2657 ",trans=lba" :
2658 translation == BIOS_ATA_TRANSLATION_NONE ?
2659 ",trans=none" : "");
2660 break;
2661 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002662 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2663 break;
2664
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002665 case QEMU_OPTION_hdc:
2666 case QEMU_OPTION_hdd:
2667 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2668 break;
2669 case QEMU_OPTION_drive:
2670 drive_add(NULL, "%s", optarg);
2671 break;
2672 case QEMU_OPTION_mtdblock:
2673 drive_add(optarg, MTD_ALIAS);
2674 break;
2675 case QEMU_OPTION_sd:
2676 drive_add(optarg, SD_ALIAS);
2677 break;
2678 case QEMU_OPTION_pflash:
2679 drive_add(optarg, PFLASH_ALIAS);
2680 break;
2681 case QEMU_OPTION_snapshot:
2682 snapshot = 1;
2683 break;
2684 case QEMU_OPTION_hdachs:
2685 {
2686 const char *p;
2687 p = optarg;
2688 cyls = strtol(p, (char **)&p, 0);
2689 if (cyls < 1 || cyls > 16383)
2690 goto chs_fail;
2691 if (*p != ',')
2692 goto chs_fail;
2693 p++;
2694 heads = strtol(p, (char **)&p, 0);
2695 if (heads < 1 || heads > 16)
2696 goto chs_fail;
2697 if (*p != ',')
2698 goto chs_fail;
2699 p++;
2700 secs = strtol(p, (char **)&p, 0);
2701 if (secs < 1 || secs > 63)
2702 goto chs_fail;
2703 if (*p == ',') {
2704 p++;
2705 if (!strcmp(p, "none"))
2706 translation = BIOS_ATA_TRANSLATION_NONE;
2707 else if (!strcmp(p, "lba"))
2708 translation = BIOS_ATA_TRANSLATION_LBA;
2709 else if (!strcmp(p, "auto"))
2710 translation = BIOS_ATA_TRANSLATION_AUTO;
2711 else
2712 goto chs_fail;
2713 } else if (*p != '\0') {
2714 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002715 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002716 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002717 if (hda_opts != NULL) {
2718 char num[16];
2719 snprintf(num, sizeof(num), "%d", cyls);
2720 qemu_opt_set(hda_opts, "cyls", num);
2721 snprintf(num, sizeof(num), "%d", heads);
2722 qemu_opt_set(hda_opts, "heads", num);
2723 snprintf(num, sizeof(num), "%d", secs);
2724 qemu_opt_set(hda_opts, "secs", num);
2725 if (translation == BIOS_ATA_TRANSLATION_LBA)
2726 qemu_opt_set(hda_opts, "trans", "lba");
2727 if (translation == BIOS_ATA_TRANSLATION_NONE)
2728 qemu_opt_set(hda_opts, "trans", "none");
2729 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002730 }
2731 break;
2732 case QEMU_OPTION_numa:
2733 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002734 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002735 }
2736 numa_add(optarg);
2737 break;
2738 case QEMU_OPTION_nographic:
2739 display_type = DT_NOGRAPHIC;
2740 break;
2741#ifdef CONFIG_CURSES
2742 case QEMU_OPTION_curses:
2743 display_type = DT_CURSES;
2744 break;
2745#endif
2746 case QEMU_OPTION_portrait:
2747 graphic_rotate = 1;
2748 break;
2749 case QEMU_OPTION_kernel:
2750 kernel_filename = optarg;
2751 break;
2752 case QEMU_OPTION_append:
2753 kernel_cmdline = optarg;
2754 break;
2755 case QEMU_OPTION_cdrom:
2756 drive_add(optarg, CDROM_ALIAS);
2757 break;
2758 case QEMU_OPTION_boot:
2759 boot_devices = optarg;
2760 /* We just do some generic consistency checks */
2761 {
2762 /* Could easily be extended to 64 devices if needed */
2763 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002764
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002765 boot_devices_bitmap = 0;
2766 for (p = boot_devices; *p != '\0'; p++) {
2767 /* Allowed boot devices are:
2768 * a b : floppy disk drives
2769 * c ... f : IDE disk drives
2770 * g ... m : machine implementation dependant drives
2771 * n ... p : network devices
2772 * It's up to each machine implementation to check
2773 * if the given boot devices match the actual hardware
2774 * implementation and firmware features.
2775 */
2776 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002777 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002778 }
2779 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002780 PANIC(
2781 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002782 }
2783 boot_devices_bitmap |= 1 << (*p - 'a');
2784 }
2785 }
2786 break;
2787 case QEMU_OPTION_fda:
2788 case QEMU_OPTION_fdb:
2789 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2790 break;
2791#ifdef TARGET_I386
2792 case QEMU_OPTION_no_fd_bootchk:
2793 fd_bootchk = 0;
2794 break;
2795#endif
2796 case QEMU_OPTION_net:
2797 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002798 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002799 }
2800 net_clients[nb_net_clients] = optarg;
2801 nb_net_clients++;
2802 break;
2803#ifdef CONFIG_SLIRP
2804 case QEMU_OPTION_tftp:
2805 tftp_prefix = optarg;
2806 break;
2807 case QEMU_OPTION_bootp:
2808 bootp_filename = optarg;
2809 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002810 case QEMU_OPTION_redir:
2811 net_slirp_redir(NULL, optarg, NULL);
2812 break;
2813#endif
2814 case QEMU_OPTION_bt:
2815 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002816 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002817 }
2818 bt_opts[nb_bt_opts++] = optarg;
2819 break;
2820#ifdef HAS_AUDIO
2821 case QEMU_OPTION_audio_help:
2822 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002823 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002824 break;
2825 case QEMU_OPTION_soundhw:
2826 select_soundhw (optarg);
2827 break;
2828#endif
2829 case QEMU_OPTION_h:
2830 qemu_help(0);
2831 break;
2832 case QEMU_OPTION_version:
2833 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002834 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002835 break;
2836 case QEMU_OPTION_m: {
2837 uint64_t value;
2838 char *ptr;
2839
2840 value = strtoul(optarg, &ptr, 10);
2841 switch (*ptr) {
2842 case 0: case 'M': case 'm':
2843 value <<= 20;
2844 break;
2845 case 'G': case 'g':
2846 value <<= 30;
2847 break;
2848 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002849 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002850 }
2851
2852 /* On 32-bit hosts, QEMU is limited by virtual address space */
David 'Digit' Turner81911b02014-02-10 17:53:45 +01002853 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002854 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002855 }
2856 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002857 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002858 }
2859 ram_size = value;
2860 break;
2861 }
2862 case QEMU_OPTION_d:
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01002863 log_mask = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002864 break;
2865 case QEMU_OPTION_s:
2866 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2867 break;
2868 case QEMU_OPTION_gdb:
2869 gdbstub_dev = optarg;
2870 break;
2871 case QEMU_OPTION_L:
2872 data_dir = optarg;
2873 break;
2874 case QEMU_OPTION_bios:
2875 bios_name = optarg;
2876 break;
2877 case QEMU_OPTION_singlestep:
2878 singlestep = 1;
2879 break;
2880 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002881 autostart = 0;
2882 break;
2883#ifndef _WIN32
2884 case QEMU_OPTION_k:
2885 keyboard_layout = optarg;
2886 break;
2887#endif
2888 case QEMU_OPTION_localtime:
2889 rtc_utc = 0;
2890 break;
2891 case QEMU_OPTION_vga:
2892 select_vgahw (optarg);
2893 break;
2894#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2895 case QEMU_OPTION_g:
2896 {
2897 const char *p;
2898 int w, h, depth;
2899 p = optarg;
2900 w = strtol(p, (char **)&p, 10);
2901 if (w <= 0) {
2902 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002903 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002904 }
2905 if (*p != 'x')
2906 goto graphic_error;
2907 p++;
2908 h = strtol(p, (char **)&p, 10);
2909 if (h <= 0)
2910 goto graphic_error;
2911 if (*p == 'x') {
2912 p++;
2913 depth = strtol(p, (char **)&p, 10);
2914 if (depth != 8 && depth != 15 && depth != 16 &&
2915 depth != 24 && depth != 32)
2916 goto graphic_error;
2917 } else if (*p == '\0') {
2918 depth = graphic_depth;
2919 } else {
2920 goto graphic_error;
2921 }
2922
2923 graphic_width = w;
2924 graphic_height = h;
2925 graphic_depth = depth;
2926 }
2927 break;
2928#endif
2929 case QEMU_OPTION_echr:
2930 {
2931 char *r;
2932 term_escape_char = strtol(optarg, &r, 0);
2933 if (r == optarg)
2934 printf("Bad argument to echr\n");
2935 break;
2936 }
2937 case QEMU_OPTION_monitor:
2938 monitor_device = optarg;
2939 break;
2940 case QEMU_OPTION_serial:
2941 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002942 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002943 }
2944 serial_devices[serial_device_index] = optarg;
2945 serial_device_index++;
2946 break;
2947 case QEMU_OPTION_watchdog:
2948 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002949 if (i > 0) {
2950 if (i == 1) {
2951 PANIC("Invalid watchdog parameter: %s",
2952 optarg);
2953 } else {
2954 QEMU_EXIT(0);
2955 }
2956 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002957 break;
2958 case QEMU_OPTION_watchdog_action:
2959 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002960 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002961 }
2962 break;
2963 case QEMU_OPTION_virtiocon:
2964 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002965 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002966 }
2967 virtio_consoles[virtio_console_index] = optarg;
2968 virtio_console_index++;
2969 break;
2970 case QEMU_OPTION_parallel:
2971 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002972 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002973 }
2974 parallel_devices[parallel_device_index] = optarg;
2975 parallel_device_index++;
2976 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002977 case QEMU_OPTION_loadvm:
2978 loadvm = optarg;
2979 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002980 case QEMU_OPTION_savevm_on_exit:
2981 savevm_on_exit = optarg;
2982 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002983 case QEMU_OPTION_full_screen:
2984 full_screen = 1;
2985 break;
2986#ifdef CONFIG_SDL
2987 case QEMU_OPTION_no_frame:
2988 no_frame = 1;
2989 break;
2990 case QEMU_OPTION_alt_grab:
2991 alt_grab = 1;
2992 break;
2993 case QEMU_OPTION_no_quit:
2994 no_quit = 1;
2995 break;
2996 case QEMU_OPTION_sdl:
2997 display_type = DT_SDL;
2998 break;
2999#endif
3000 case QEMU_OPTION_pidfile:
3001 pid_file = optarg;
3002 break;
3003#ifdef TARGET_I386
3004 case QEMU_OPTION_win2k_hack:
3005 win2k_install_hack = 1;
3006 break;
3007 case QEMU_OPTION_rtc_td_hack:
3008 rtc_td_hack = 1;
3009 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003010#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003011 case QEMU_OPTION_acpitable:
3012 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003013 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003014 }
3015 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003016#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003017 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02003018 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003019 break;
3020#endif
3021#ifdef CONFIG_KVM
3022 case QEMU_OPTION_enable_kvm:
3023 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003024 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02003025 case QEMU_OPTION_disable_kvm:
3026 kvm_allowed = 0;
3027 break;
3028#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003029 case QEMU_OPTION_usb:
3030 usb_enabled = 1;
3031 break;
3032 case QEMU_OPTION_usbdevice:
3033 usb_enabled = 1;
3034 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003035 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003036 }
3037 usb_devices[usb_devices_index] = optarg;
3038 usb_devices_index++;
3039 break;
3040 case QEMU_OPTION_smp:
3041 smp_cpus = atoi(optarg);
3042 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003043 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003044 }
3045 break;
3046 case QEMU_OPTION_vnc:
3047 display_type = DT_VNC;
3048 vnc_display = optarg;
3049 break;
3050#ifdef TARGET_I386
3051 case QEMU_OPTION_no_acpi:
3052 acpi_enabled = 0;
3053 break;
3054 case QEMU_OPTION_no_hpet:
3055 no_hpet = 1;
3056 break;
3057 case QEMU_OPTION_no_virtio_balloon:
3058 no_virtio_balloon = 1;
3059 break;
3060#endif
3061 case QEMU_OPTION_no_reboot:
3062 no_reboot = 1;
3063 break;
3064 case QEMU_OPTION_no_shutdown:
3065 no_shutdown = 1;
3066 break;
3067 case QEMU_OPTION_show_cursor:
3068 cursor_hide = 0;
3069 break;
3070 case QEMU_OPTION_uuid:
3071 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003072 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003073 }
3074 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003075 case QEMU_OPTION_option_rom:
3076 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003077 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003078 }
3079 option_rom[nb_option_roms] = optarg;
3080 nb_option_roms++;
3081 break;
3082#if defined(TARGET_ARM) || defined(TARGET_M68K)
3083 case QEMU_OPTION_semihosting:
3084 semihosting_enabled = 1;
3085 break;
3086#endif
3087 case QEMU_OPTION_name:
3088 qemu_name = optarg;
3089 break;
3090#if defined(TARGET_SPARC) || defined(TARGET_PPC)
3091 case QEMU_OPTION_prom_env:
3092 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003093 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003094 }
3095 prom_envs[nb_prom_envs] = optarg;
3096 nb_prom_envs++;
3097 break;
3098#endif
3099#ifdef TARGET_ARM
3100 case QEMU_OPTION_old_param:
3101 old_param = 1;
3102 break;
3103#endif
3104 case QEMU_OPTION_clock:
3105 configure_alarms(optarg);
3106 break;
3107 case QEMU_OPTION_startdate:
3108 {
3109 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02003110 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003111 if (!strcmp(optarg, "now")) {
3112 rtc_date_offset = -1;
3113 } else {
3114 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
3115 &tm.tm_year,
3116 &tm.tm_mon,
3117 &tm.tm_mday,
3118 &tm.tm_hour,
3119 &tm.tm_min,
3120 &tm.tm_sec) == 6) {
3121 /* OK */
3122 } else if (sscanf(optarg, "%d-%d-%d",
3123 &tm.tm_year,
3124 &tm.tm_mon,
3125 &tm.tm_mday) == 3) {
3126 tm.tm_hour = 0;
3127 tm.tm_min = 0;
3128 tm.tm_sec = 0;
3129 } else {
3130 goto date_fail;
3131 }
3132 tm.tm_year -= 1900;
3133 tm.tm_mon--;
3134 rtc_start_date = mktimegm(&tm);
3135 if (rtc_start_date == -1) {
3136 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003137 PANIC("Invalid date format. Valid format are:\n"
3138 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003139 }
3140 rtc_date_offset = time(NULL) - rtc_start_date;
3141 }
3142 }
3143 break;
rich cannings7339b552011-02-16 13:43:44 -08003144
3145 /* -------------------------------------------------------*/
3146 /* User mode network stack restrictions */
3147 case QEMU_OPTION_drop_udp:
3148 slirp_drop_udp();
3149 break;
3150 case QEMU_OPTION_drop_tcp:
3151 slirp_drop_tcp();
3152 break;
3153 case QEMU_OPTION_allow_tcp:
3154 slirp_allow(optarg, IPPROTO_TCP);
3155 break;
3156 case QEMU_OPTION_allow_udp:
3157 slirp_allow(optarg, IPPROTO_UDP);
3158 break;
3159 case QEMU_OPTION_drop_log:
3160 {
3161 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003162 drop_log_filename = optarg;
3163 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08003164
3165 if (!drop_log_fd) {
3166 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
3167 exit(1);
3168 }
3169
3170 slirp_drop_log_fd(drop_log_fd);
3171 }
3172 break;
3173
3174 case QEMU_OPTION_dns_log:
3175 {
3176 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003177 dns_log_filename = optarg;
3178 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08003179
3180 if (dns_log_fd == NULL) {
3181 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
3182 exit(1);
3183 }
3184
3185 slirp_dns_log_fd(dns_log_fd);
3186 }
3187 break;
3188
3189
3190 case QEMU_OPTION_max_dns_conns:
3191 {
3192 int max_dns_conns = 0;
3193 if (parse_int(optarg, &max_dns_conns)) {
3194 fprintf(stderr,
3195 "qemu: syntax: -max-dns-conns max_connections\n");
3196 exit(1);
3197 }
3198 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3199 fprintf(stderr,
3200 "Invalid arg for max dns connections: %s\n",
3201 optarg);
3202 exit(1);
3203 }
3204 slirp_set_max_dns_conns(max_dns_conns);
3205 }
3206 break;
3207
3208 case QEMU_OPTION_net_forward:
3209 net_slirp_forward(optarg);
3210 break;
3211 case QEMU_OPTION_net_forward_tcp2sink:
3212 {
3213 SockAddress saddr;
3214
3215 if (parse_host_port(&saddr, optarg)) {
3216 fprintf(stderr,
3217 "Invalid ip/port %s for "
3218 "-forward-dropped-tcp2sink. "
3219 "We expect 'sink_ip:sink_port'\n",
3220 optarg);
3221 exit(1);
3222 }
3223 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3224 saddr.u.inet.port);
3225 }
3226 break;
3227 /* -------------------------------------------------------*/
3228
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003229 case QEMU_OPTION_tb_size:
3230 tb_size = strtol(optarg, NULL, 0);
3231 if (tb_size < 0)
3232 tb_size = 0;
3233 break;
3234 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003235 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003236 break;
3237 case QEMU_OPTION_incoming:
3238 incoming = optarg;
3239 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003240#ifdef CONFIG_XEN
3241 case QEMU_OPTION_xen_domid:
3242 xen_domid = atoi(optarg);
3243 break;
3244 case QEMU_OPTION_xen_create:
3245 xen_mode = XEN_CREATE;
3246 break;
3247 case QEMU_OPTION_xen_attach:
3248 xen_mode = XEN_ATTACH;
3249 break;
3250#endif
3251
3252
3253 case QEMU_OPTION_mic:
3254 audio_input_source = (char*)optarg;
3255 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003256#ifdef CONFIG_NAND
3257 case QEMU_OPTION_nand:
3258 nand_add_dev(optarg);
3259 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003260
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003261#endif
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003262 case QEMU_OPTION_disable_hax:
3263 hax_disabled = 1;
3264 break;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003265 case QEMU_OPTION_android_ports:
3266 android_op_ports = (char*)optarg;
3267 break;
3268
3269 case QEMU_OPTION_android_port:
3270 android_op_port = (char*)optarg;
3271 break;
3272
3273 case QEMU_OPTION_android_report_console:
3274 android_op_report_console = (char*)optarg;
3275 break;
3276
3277 case QEMU_OPTION_http_proxy:
3278 op_http_proxy = (char*)optarg;
3279 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003280
3281 case QEMU_OPTION_charmap:
3282 op_charmap_file = (char*)optarg;
3283 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003284
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003285 case QEMU_OPTION_android_hw:
3286 android_op_hwini = (char*)optarg;
3287 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003288
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003289 case QEMU_OPTION_dns_server:
3290 android_op_dns_server = (char*)optarg;
3291 break;
3292
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003293 case QEMU_OPTION_radio:
3294 android_op_radio = (char*)optarg;
3295 break;
3296
3297 case QEMU_OPTION_gps:
3298 android_op_gps = (char*)optarg;
3299 break;
3300
3301 case QEMU_OPTION_audio:
3302 android_op_audio = (char*)optarg;
3303 break;
3304
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003305 case QEMU_OPTION_cpu_delay:
3306 android_op_cpu_delay = (char*)optarg;
3307 break;
3308
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003309 case QEMU_OPTION_show_kernel:
3310 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3311 break;
3312
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003313#ifdef CONFIG_NAND_LIMITS
3314 case QEMU_OPTION_nand_limits:
3315 android_op_nand_limits = (char*)optarg;
3316 break;
3317#endif // CONFIG_NAND_LIMITS
3318
3319 case QEMU_OPTION_netspeed:
3320 android_op_netspeed = (char*)optarg;
3321 break;
3322
3323 case QEMU_OPTION_netdelay:
3324 android_op_netdelay = (char*)optarg;
3325 break;
3326
3327 case QEMU_OPTION_netfast:
3328 android_op_netfast = 1;
3329 break;
3330
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003331 case QEMU_OPTION_tcpdump:
3332 android_op_tcpdump = (char*)optarg;
3333 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003334
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003335 case QEMU_OPTION_boot_property:
3336 boot_property_parse_option((char*)optarg);
3337 break;
3338
3339 case QEMU_OPTION_lcd_density:
3340 android_op_lcd_density = (char*)optarg;
3341 break;
3342
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003343 case QEMU_OPTION_ui_port:
3344 android_op_ui_port = (char*)optarg;
3345 break;
3346
3347 case QEMU_OPTION_ui_settings:
3348 android_op_ui_settings = (char*)optarg;
3349 break;
3350
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003351 case QEMU_OPTION_audio_test_out:
3352 android_audio_test_start_out();
3353 break;
3354
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003355 case QEMU_OPTION_android_avdname:
3356 android_op_avd_name = (char*)optarg;
3357 break;
3358
3359 case QEMU_OPTION_timezone:
3360 if (timezone_set((char*)optarg)) {
3361 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3362 (char*)optarg);
3363 }
3364 break;
3365
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003366#ifdef CONFIG_MEMCHECK
3367 case QEMU_OPTION_android_memcheck:
3368 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003369 /* This will set ro.kernel.memcheck system property
3370 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003371 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003372 break;
3373#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003374
3375 case QEMU_OPTION_snapshot_no_time_update:
3376 android_snapshot_update_time = 0;
3377 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003378
3379 case QEMU_OPTION_list_webcam:
3380 android_list_web_cameras();
3381 exit(0);
3382
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003383 default:
3384 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003385 }
3386 }
3387 }
3388
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003389 /* Initialize character map. */
3390 if (android_charmap_setup(op_charmap_file)) {
3391 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003392 PANIC(
3393 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003394 op_charmap_file);
3395 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003396 PANIC(
3397 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003398 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003399 }
3400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003401 /* If no data_dir is specified then try to find it relative to the
3402 executable path. */
3403 if (!data_dir) {
3404 data_dir = find_datadir(argv[0]);
3405 }
3406 /* If all else fails use the install patch specified when building. */
3407 if (!data_dir) {
3408 data_dir = CONFIG_QEMU_SHAREDIR;
3409 }
3410
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003411 if (!android_op_hwini) {
3412 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003413 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003414 hw_ini = iniFile_newFromFile(android_op_hwini);
3415 if (hw_ini == NULL) {
3416 PANIC("Could not find %s file.", android_op_hwini);
3417 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003418
3419 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003420 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003421
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003422 /* If we're loading VM from a snapshot, make sure that the current HW config
3423 * matches the one with which the VM has been saved. */
3424 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3425 exit(0);
3426 }
3427
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003428 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003429
3430 {
3431 int width = android_hw->hw_lcd_width;
3432 int height = android_hw->hw_lcd_height;
3433 int depth = android_hw->hw_lcd_depth;
3434
3435 /* A bit of sanity checking */
3436 if (width <= 0 || height <= 0 ||
3437 (depth != 16 && depth != 32) ||
3438 (((width|height) & 3) != 0) )
3439 {
3440 PANIC("Invalid display configuration (%d,%d,%d)",
3441 width, height, depth);
3442 }
3443 android_display_width = width;
3444 android_display_height = height;
3445 android_display_bpp = depth;
3446 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003447
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003448#ifdef CONFIG_NAND_LIMITS
3449 /* Init nand stuff. */
3450 if (android_op_nand_limits) {
3451 parse_nand_limits(android_op_nand_limits);
3452 }
3453#endif // CONFIG_NAND_LIMITS
3454
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003455 /* Initialize AVD name from hardware configuration if needed */
3456 if (!android_op_avd_name) {
3457 if (android_hw->avd_name && *android_hw->avd_name) {
3458 android_op_avd_name = android_hw->avd_name;
3459 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3460 }
3461 }
3462
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003463 /* Initialize system partition image */
3464 {
3465 char tmp[PATH_MAX+32];
3466 const char* sysImage = android_hw->disk_systemPartition_path;
3467 const char* initImage = android_hw->disk_systemPartition_initPath;
3468 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3469
3470 if (sysBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003471 PANIC("Invalid system partition size: %" PRIu64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003472 }
3473
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003474 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRIx64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003475
3476 if (sysImage && *sysImage) {
3477 if (filelock_create(sysImage) == NULL) {
3478 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3479 /* If there is no file= parameters, nand_add_dev will create
3480 * a temporary file to back the partition image. */
3481 } else {
3482 pstrcat(tmp,sizeof(tmp),",file=");
3483 pstrcat(tmp,sizeof(tmp),sysImage);
3484 }
3485 }
3486 if (initImage && *initImage) {
3487 if (!path_exists(initImage)) {
3488 PANIC("Invalid initial system image path: %s", initImage);
3489 }
3490 pstrcat(tmp,sizeof(tmp),",initfile=");
3491 pstrcat(tmp,sizeof(tmp),initImage);
3492 } else {
3493 PANIC("Missing initial system image path!");
3494 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003495 if (android_hw->hw_useext4) {
3496 /* Using a nand device to approximate a block device until full
3497 * support is added */
3498 pstrcat(tmp,sizeof(tmp),",pagesize=512,extrasize=0");
3499 }
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003500 nand_add_dev(tmp);
3501 }
3502
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003503 /* Initialize data partition image */
3504 {
3505 char tmp[PATH_MAX+32];
3506 const char* dataImage = android_hw->disk_dataPartition_path;
3507 const char* initImage = android_hw->disk_dataPartition_initPath;
3508 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3509
3510 if (dataBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003511 PANIC("Invalid data partition size: %" PRIu64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003512 }
3513
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003514 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRIx64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003515
3516 if (dataImage && *dataImage) {
3517 if (filelock_create(dataImage) == NULL) {
3518 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3519 /* Note: if there is no file= parameters, nand_add_dev() will
3520 * create a temporary file to back the partition image. */
3521 } else {
3522 /* Create the file if needed */
3523 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003524 if (path_empty_file(dataImage) < 0) {
3525 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3526 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003527 }
3528 pstrcat(tmp, sizeof(tmp), ",file=");
3529 pstrcat(tmp, sizeof(tmp), dataImage);
3530 }
3531 }
3532 if (initImage && *initImage) {
3533 pstrcat(tmp, sizeof(tmp), ",initfile=");
3534 pstrcat(tmp, sizeof(tmp), initImage);
3535 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003536 if (android_hw->hw_useext4) {
3537 /* Using a nand device to approximate a block device until full
3538 * support is added */
3539 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3540 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003541 nand_add_dev(tmp);
3542 }
3543
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003544 /* Init SD-Card stuff. For Android, it is always hda */
3545 /* If the -hda option was used, ignore the Android-provided one */
3546 if (hda_opts == NULL) {
3547 const char* sdPath = android_hw->hw_sdCard_path;
3548 if (sdPath && *sdPath) {
3549 if (!path_exists(sdPath)) {
3550 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3551 } else if (filelock_create(sdPath) == NULL) {
3552 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3553 } else {
3554 /* Successful locking */
3555 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003556 /* Set this property of any operation involving the SD Card
3557 * will be x100 slower, due to the corresponding file being
3558 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3559 * the context of an emulator crash. The data is already
3560 * synced properly when the emulator exits (either normally or through ^C).
3561 */
3562 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003563 }
3564 }
3565 }
3566
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003567 if (hdb_opts == NULL) {
3568 const char* spath = android_hw->disk_snapStorage_path;
3569 if (spath && *spath) {
3570 if (!path_exists(spath)) {
3571 PANIC("Snapshot storage file does not exist: %s", spath);
3572 }
3573 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003574 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003575 }
3576 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003577 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003578 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003579 }
3580 }
3581
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003582 /* Set the VM's max heap size, passed as a boot property */
3583 if (android_hw->vm_heapSize > 0) {
3584 char tmp[64];
3585 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3586 boot_property_add("dalvik.vm.heapsize",tmp);
3587 }
3588
Siva Velusamy0faeb3a2013-10-29 14:23:35 -07003589 /* From API 19 and above, the platform provides an explicit property for low memory devices. */
3590 if (android_hw->hw_ramSize <= 512) {
3591 boot_property_add("ro.config.low_ram", "true");
3592 }
3593
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003594 /* Initialize net speed and delays stuff. */
3595 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003596 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003597 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003598 }
3599
3600 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003601 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003602 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003603 }
3604
3605 if (android_op_netfast) {
3606 qemu_net_download_speed = 0;
3607 qemu_net_upload_speed = 0;
3608 qemu_net_min_latency = 0;
3609 qemu_net_max_latency = 0;
3610 }
3611
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003612 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003613 if (android_hw->hw_lcd_density) {
3614 long density = android_hw->hw_lcd_density;
3615 if (density <= 0) {
3616 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003617 }
3618 hwLcd_setBootProperty(density);
3619 }
3620
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003621 /* Initialize presence of hardware nav button */
3622 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3623
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003624 /* Initialize TCP dump */
3625 if (android_op_tcpdump) {
3626 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3627 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3628 }
3629 }
3630
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003631 /* Initialize modem */
3632 if (android_op_radio) {
3633 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3634 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003635 PANIC("unsupported character device specification: %s\n"
3636 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003637 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003638 }
3639 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3640 } else if (android_hw->hw_gsmModem != 0 ) {
3641 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003642 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003643 }
3644 }
3645
3646 /* Initialize GPS */
3647 if (android_op_gps) {
3648 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3649 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003650 PANIC("unsupported character device specification: %s\n"
3651 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003652 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003653 }
3654 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3655 } else if (android_hw->hw_gps != 0) {
3656 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003657 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003658 }
3659 }
3660
3661 /* Initialize audio. */
3662 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003663 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003664 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003665 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003666 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003667 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003668 }
3669
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003670 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003671 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003672
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003673 /* Initialize fake camera */
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003674 if (strcmp(android_hw->hw_camera_back, "emulated") &&
3675 strcmp(android_hw->hw_camera_front, "emulated")) {
3676 /* Fake camera is not used for camera emulation. */
3677 boot_property_add("qemu.sf.fake_camera", "none");
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003678 } else {
Vladimir Chtchetkine7485c292012-03-19 11:35:29 -07003679 /* Fake camera is used for at least one camera emulation. */
3680 if (!strcmp(android_hw->hw_camera_back, "emulated") &&
3681 !strcmp(android_hw->hw_camera_front, "emulated")) {
3682 /* Fake camera is used for both, front and back camera emulation. */
3683 boot_property_add("qemu.sf.fake_camera", "both");
3684 } else if (!strcmp(android_hw->hw_camera_back, "emulated")) {
3685 boot_property_add("qemu.sf.fake_camera", "back");
3686 } else {
3687 boot_property_add("qemu.sf.fake_camera", "front");
3688 }
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003689 }
3690
Vladimir Chtchetkine035f8052012-05-18 12:19:32 -07003691 /* Set LCD density (if required by -qemu, and AVD is missing it. */
3692 if (android_op_lcd_density && !android_hw->hw_lcd_density) {
3693 int density;
3694 if (parse_int(android_op_lcd_density, &density) || density <= 0) {
3695 PANIC("-lcd-density : %d", density);
3696 }
3697 hwLcd_setBootProperty(density);
3698 }
3699
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003700 /* Initialize camera emulation. */
3701 android_camera_service_init();
3702
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003703 if (android_op_cpu_delay) {
3704 char* end;
3705 long delay = strtol(android_op_cpu_delay, &end, 0);
3706 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003707 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003708 }
3709 if (delay > 0)
3710 delay = (1000-delay);
3711
3712 qemu_cpu_delay = (int) delay;
3713 }
3714
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003715 if (android_op_dns_server) {
3716 char* x = strchr(android_op_dns_server, ',');
3717 dns_count = 0;
3718 if (x == NULL)
3719 {
3720 if ( add_dns_server( android_op_dns_server ) == 0 )
3721 dns_count = 1;
3722 }
3723 else
3724 {
3725 x = android_op_dns_server;
3726 while (*x) {
3727 char* y = strchr(x, ',');
3728
3729 if (y != NULL) {
3730 *y = 0;
3731 y++;
3732 } else {
3733 y = x + strlen(x);
3734 }
3735
3736 if (y > x && add_dns_server( x ) == 0) {
3737 dns_count += 1;
3738 }
3739 x = y;
3740 }
3741 }
3742 if (dns_count == 0)
3743 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3744 }
3745
3746 if (dns_count == 0)
3747 dns_count = slirp_get_system_dns_servers();
3748 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003749 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003750 }
3751
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003752#ifdef CONFIG_MEMCHECK
3753 if (android_op_memcheck) {
3754 memcheck_init(android_op_memcheck);
3755 }
3756#endif // CONFIG_MEMCHECK
3757
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003758 /* Initialize cache partition, if any */
3759 if (android_hw->disk_cachePartition != 0) {
3760 char tmp[PATH_MAX+32];
3761 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003762 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003763
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003764 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRIx64, partSize);
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003765
3766 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3767 if (filelock_create(partPath) == NULL) {
3768 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3769 /* Note: if there is no file= parameters, nand_add_dev() will
3770 * create a temporary file to back the partition image. */
3771 } else {
3772 /* Create the file if needed */
3773 if (!path_exists(partPath)) {
3774 if (path_empty_file(partPath) < 0) {
3775 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3776 }
3777 }
3778 pstrcat(tmp, sizeof(tmp), ",file=");
3779 pstrcat(tmp, sizeof(tmp), partPath);
3780 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003781 }
Deepanshu Gupta04b20f42013-09-11 12:04:27 -07003782 if (android_hw->hw_useext4) {
3783 /* Using a nand device to approximate a block device until full
3784 * support is added */
3785 pstrcat(tmp, sizeof(tmp), ",pagesize=512,extrasize=0");
3786 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003787 nand_add_dev(tmp);
3788 }
3789
Jesse Hall733fffa2012-04-26 11:07:32 -07003790 /* qemu.gles will be read by the OpenGL ES emulation libraries.
3791 * If set to 0, the software GL ES renderer will be used as a fallback.
3792 * If the parameter is undefined, this means the system image runs
3793 * inside an emulator that doesn't support GPU emulation at all.
3794 *
3795 * We always start the GL ES renderer so we can gather stats on the
3796 * underlying GL implementation. If GL ES acceleration is disabled,
3797 * we just shut it down again once we have the strings. */
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003798 {
Jesse Hall733fffa2012-04-26 11:07:32 -07003799 int qemu_gles = 0;
Jesse Hall733fffa2012-04-26 11:07:32 -07003800 if (android_initOpenglesEmulation() == 0 &&
Jesse Hallba5c1f62012-05-08 15:44:35 -07003801 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height) == 0)
Jesse Hall733fffa2012-04-26 11:07:32 -07003802 {
3803 android_getOpenglesHardwareStrings(
3804 android_gl_vendor, sizeof(android_gl_vendor),
3805 android_gl_renderer, sizeof(android_gl_renderer),
3806 android_gl_version, sizeof(android_gl_version));
3807 if (android_hw->hw_gpu_enabled) {
3808 qemu_gles = 1;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003809 } else {
Jesse Hall733fffa2012-04-26 11:07:32 -07003810 android_stopOpenglesRenderer();
3811 qemu_gles = 0;
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003812 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003813 } else {
3814 dwarning("Could not initialize OpenglES emulation, using software renderer.");
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003815 }
Jesse Hall733fffa2012-04-26 11:07:32 -07003816 if (qemu_gles) {
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003817 stralloc_add_str(kernel_params, " qemu.gles=1");
3818 } else {
3819 stralloc_add_str(kernel_params, " qemu.gles=0");
3820 }
3821 }
3822
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003823 /* We always force qemu=1 when running inside QEMU */
3824 stralloc_add_str(kernel_params, " qemu=1");
3825
3826 /* We always initialize the first serial port for the android-kmsg
3827 * character device (used to send kernel messages) */
3828 serial_hds_add_at(0, "android-kmsg");
3829 stralloc_add_str(kernel_params, " console=ttyS0");
3830
3831 /* We always initialize the second serial port for the android-qemud
3832 * character device as well */
3833 serial_hds_add_at(1, "android-qemud");
3834 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3835
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003836 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3837 os_pidfile_error();
3838 exit(1);
3839 }
3840
David 'Digit' Turner7e9f6c12014-01-22 18:42:40 +01003841 /* Open the logfile at this point, if necessary. We can't open the logfile
3842 * when encountering either of the logging options (-d or -D) because the
3843 * other one may be encountered later on the command line, changing the
3844 * location or level of logging.
3845 */
3846 if (log_mask) {
3847 int mask;
3848 if (log_file) {
3849 qemu_set_log_filename(log_file);
3850 }
3851
3852 mask = qemu_str_to_log_mask(log_mask);
3853 if (!mask) {
3854 qemu_print_log_usage(stdout);
3855 exit(1);
3856 }
3857 qemu_set_log(mask);
3858 }
3859
David 'Digit' Turner36597752011-05-20 01:18:01 +02003860#if defined(CONFIG_KVM)
3861 if (kvm_allowed < 0) {
3862 kvm_allowed = kvm_check_allowed();
3863 }
3864#endif
3865
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003866 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3867 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003868 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3869 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003870 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003871 }
3872
3873 if (display_type == DT_NOGRAPHIC) {
3874 if (serial_device_index == 0)
3875 serial_devices[0] = "stdio";
3876 if (parallel_device_index == 0)
3877 parallel_devices[0] = "null";
3878 if (strncmp(monitor_device, "vc", 2) == 0)
3879 monitor_device = "stdio";
3880 }
3881
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003882 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003883 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003884 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003885
3886 if (kernel_filename == NULL) {
3887 kernel_filename = android_hw->kernel_path;
3888 }
3889 if (initrd_filename == NULL) {
3890 initrd_filename = android_hw->disk_ramdisk_path;
3891 }
3892
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003893 linux_boot = (kernel_filename != NULL);
3894 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3895
3896 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003897 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003898 }
3899
3900 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003901 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003902 }
3903
3904 /* boot to floppy or the default cd if no hard disk defined yet */
3905 if (!boot_devices[0]) {
3906 boot_devices = "cad";
3907 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003908 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003909
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003910 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003911 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003912 }
David Turner6a9ef172010-09-09 22:54:36 +02003913 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003914
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003915 /* init network clients */
3916 if (nb_net_clients == 0) {
3917 /* if no clients, we use a default config */
3918 net_clients[nb_net_clients++] = "nic";
3919#ifdef CONFIG_SLIRP
3920 net_clients[nb_net_clients++] = "user";
3921#endif
3922 }
3923
3924 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003925 if (net_client_parse(net_clients[i]) < 0) {
3926 PANIC("Unable to parse net clients");
3927 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003928 }
3929 net_client_check();
3930
3931#ifdef TARGET_I386
3932 /* XXX: this should be moved in the PC machine instantiation code */
3933 if (net_boot != 0) {
3934 int netroms = 0;
3935 for (i = 0; i < nb_nics && i < 4; i++) {
3936 const char *model = nd_table[i].model;
3937 char buf[1024];
3938 char *filename;
3939 if (net_boot & (1 << i)) {
3940 if (model == NULL)
3941 model = "ne2k_pci";
3942 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3943 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3944 if (filename && get_image_size(filename) > 0) {
3945 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003946 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003947 }
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003948 option_rom[nb_option_roms] = g_strdup(buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003949 nb_option_roms++;
3950 netroms++;
3951 }
3952 if (filename) {
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +01003953 g_free(filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003954 }
3955 }
3956 }
3957 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003958 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003959 }
3960 }
3961#endif
3962
3963 /* init the bluetooth world */
3964 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003965 if (bt_parse(bt_opts[i])) {
3966 PANIC("Unable to parse bluetooth options");
3967 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003968
3969 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003970 if (ram_size == 0) {
3971 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3972 if (ram_size == 0) {
3973 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3974 }
3975 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003976
Vladimir Chtchetkinecf289fb2012-05-14 08:04:29 -07003977 /* Quite often (especially on older XP machines) attempts to allocate large
3978 * VM RAM is going to fail, and crash the emulator. Since it's failing deep
3979 * inside QEMU, it's not really possible to provide the user with a
3980 * meaningful explanation for the crash. So, lets see if QEMU is going to be
3981 * able to allocate requested amount of RAM, and if not, lets try to come up
3982 * with a recomendation. */
3983 {
3984 ram_addr_t r_ram = ram_size;
3985 void* alloc_check = malloc(r_ram);
3986 while (alloc_check == NULL && r_ram > 1024 * 1024) {
3987 /* Make it 25% less */
3988 r_ram -= r_ram / 4;
3989 alloc_check = malloc(r_ram);
3990 }
3991 if (alloc_check != NULL) {
3992 free(alloc_check);
3993 }
3994 if (r_ram != ram_size) {
3995 /* Requested RAM is too large. Report this, as well as calculated
3996 * recomendation. */
3997 dwarning("Requested RAM size of %dMB is too large for your environment, and is reduced to %dMB.",
3998 (int)(ram_size / 1024 / 1024), (int)(r_ram / 1024 / 1024));
3999 ram_size = r_ram;
4000 }
4001 }
4002
rich canningsd952f282011-03-01 15:40:09 -08004003#ifndef _WIN32
4004 init_qemu_clear_logs_sig();
4005#endif
4006
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004007 /* init the dynamic translator */
4008 cpu_exec_init_all(tb_size * 1024 * 1024);
4009
4010 bdrv_init();
4011
4012 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004013#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004014 if (nb_drives_opt < MAX_DRIVES)
4015 drive_add(NULL, CDROM_ALIAS);
4016
4017 /* we always create at least one floppy */
4018
4019 if (nb_drives_opt < MAX_DRIVES)
4020 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004021 /* we always create one sd slot, even if no card is in it */
4022
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004023 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004024 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004025 }
4026#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004027
4028 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004029 if (snapshot)
4030 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4031 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
4032 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004033
David Turner6a9ef172010-09-09 22:54:36 +02004034 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004035 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
4036
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004037 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004038 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004039
4040 /* Maintain compatibility with multiple stdio monitors */
4041 if (!strcmp(monitor_device,"stdio")) {
4042 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4043 const char *devname = serial_devices[i];
4044 if (devname && !strcmp(devname,"mon:stdio")) {
4045 monitor_device = NULL;
4046 break;
4047 } else if (devname && !strcmp(devname,"stdio")) {
4048 monitor_device = NULL;
4049 serial_devices[i] = "mon:stdio";
4050 break;
4051 }
4052 }
4053 }
4054
4055 if (nb_numa_nodes > 0) {
4056 int i;
4057
4058 if (nb_numa_nodes > smp_cpus) {
4059 nb_numa_nodes = smp_cpus;
4060 }
4061
4062 /* If no memory size if given for any node, assume the default case
4063 * and distribute the available memory equally across all nodes
4064 */
4065 for (i = 0; i < nb_numa_nodes; i++) {
4066 if (node_mem[i] != 0)
4067 break;
4068 }
4069 if (i == nb_numa_nodes) {
4070 uint64_t usedmem = 0;
4071
4072 /* On Linux, the each node's border has to be 8MB aligned,
4073 * the final node gets the rest.
4074 */
4075 for (i = 0; i < nb_numa_nodes - 1; i++) {
4076 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4077 usedmem += node_mem[i];
4078 }
4079 node_mem[i] = ram_size - usedmem;
4080 }
4081
4082 for (i = 0; i < nb_numa_nodes; i++) {
4083 if (node_cpumask[i] != 0)
4084 break;
4085 }
4086 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4087 * must cope with this anyway, because there are BIOSes out there in
4088 * real machines which also use this scheme.
4089 */
4090 if (i == nb_numa_nodes) {
4091 for (i = 0; i < smp_cpus; i++) {
4092 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4093 }
4094 }
4095 }
4096
4097 if (kvm_enabled()) {
4098 int ret;
4099
4100 ret = kvm_init(smp_cpus);
4101 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004102 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004103 }
4104 }
4105
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004106#ifdef CONFIG_HAX
4107 if (!hax_disabled)
4108 {
4109 int ret;
4110
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08004111 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004112 ret = hax_init(smp_cpus);
4113 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
4114 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
4115 }
4116#endif
4117
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004118 if (monitor_device) {
4119 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
4120 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004121 PANIC("qemu: could not open monitor device '%s'",
4122 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004123 }
4124 }
4125
4126 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004127 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004128 }
4129
4130 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4131 const char *devname = parallel_devices[i];
4132 if (devname && strcmp(devname, "none")) {
4133 char label[32];
4134 snprintf(label, sizeof(label), "parallel%d", i);
4135 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
4136 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004137 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004138 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004139 }
4140 }
4141 }
4142
4143 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4144 const char *devname = virtio_consoles[i];
4145 if (devname && strcmp(devname, "none")) {
4146 char label[32];
4147 snprintf(label, sizeof(label), "virtcon%d", i);
4148 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
4149 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004150 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004151 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004152 }
4153 }
4154 }
4155
4156 module_call_init(MODULE_INIT_DEVICE);
4157
4158
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004159 /* Check the CPU Architecture value */
4160#if defined(TARGET_ARM)
4161 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4162 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4163 android_hw->hw_cpu_arch);
4164 exit(1);
4165 }
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -07004166#elif defined(TARGET_I386)
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004167 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4168 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4169 android_hw->hw_cpu_arch);
4170 exit(1);
4171 }
4172#endif
4173
4174 /* Grab CPU model if provided in hardware.ini */
4175 if ( !cpu_model
4176 && android_hw->hw_cpu_model
4177 && android_hw->hw_cpu_model[0] != '\0')
4178 {
4179 cpu_model = android_hw->hw_cpu_model;
4180 }
4181
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004182 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004183 * collected during initialization.
4184 *
4185 * The order is the following:
4186 * - parameters from the hw configuration (kernel.parameters)
4187 * - additionnal parameters from options (e.g. -memcheck)
4188 * - the -append parameters.
4189 */
4190 {
4191 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004192
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004193 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004194 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004195 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4196 }
4197
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004198 /* If not empty, kernel_config always contains a leading space */
4199 stralloc_append(kernel_params, kernel_config);
4200
4201 if (*kernel_cmdline) {
4202 stralloc_add_c(kernel_params, ' ');
4203 stralloc_add_str(kernel_params, kernel_cmdline);
4204 }
4205
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004206 /* Remove any leading/trailing spaces */
4207 stralloc_strip(kernel_params);
4208
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004209 kernel_parameters = stralloc_cstr(kernel_params);
4210 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4211
4212 machine->init(ram_size,
4213 boot_devices,
4214 kernel_filename,
4215 kernel_parameters,
4216 initrd_filename,
4217 cpu_model);
4218
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004219 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07004220 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004221 mts_port_create(NULL);
4222 }
4223
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004224 stralloc_reset(kernel_params);
4225 stralloc_reset(kernel_config);
4226 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004227
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004228 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4229 for (i = 0; i < nb_numa_nodes; i++) {
4230 if (node_cpumask[i] & (1 << env->cpu_index)) {
4231 env->numa_node = i;
4232 }
4233 }
4234 }
4235
4236 current_machine = machine;
4237
David 'Digit' Turnere2678e12014-01-16 15:56:43 +01004238 /* Set KVM's vcpu state to qemu's initial CPUOldState. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004239 if (kvm_enabled()) {
4240 int ret;
4241
4242 ret = kvm_sync_vcpus();
4243 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004244 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004245 }
4246 }
4247
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004248#ifdef CONFIG_HAX
4249 if (hax_enabled())
4250 hax_sync_vcpus();
4251#endif
4252
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004253 /* init USB devices */
4254 if (usb_enabled) {
4255 for(i = 0; i < usb_devices_index; i++) {
4256 if (usb_device_add(usb_devices[i], 0) < 0) {
4257 fprintf(stderr, "Warning: could not add USB device %s\n",
4258 usb_devices[i]);
4259 }
4260 }
4261 }
4262
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004263 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004264 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004265
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004266 /* Initialize display from the command line parameters. */
4267 android_display_reset(ds,
4268 android_display_width,
4269 android_display_height,
4270 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004271
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004272 if (display_type == DT_DEFAULT) {
4273#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4274 display_type = DT_SDL;
4275#else
4276 display_type = DT_VNC;
4277 vnc_display = "localhost:0,to=99";
4278 show_vnc_port = 1;
4279#endif
4280 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004281
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004282
4283 switch (display_type) {
4284 case DT_NOGRAPHIC:
4285 break;
4286#if defined(CONFIG_CURSES)
4287 case DT_CURSES:
4288 curses_display_init(ds, full_screen);
4289 break;
4290#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004291#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004292 case DT_SDL:
4293 sdl_display_init(ds, full_screen, no_frame);
4294 break;
4295#elif defined(CONFIG_COCOA)
4296 case DT_SDL:
4297 cocoa_display_init(ds, full_screen);
4298 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004299#elif defined(CONFIG_STANDALONE_CORE)
4300 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004301 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004302 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004303#endif
4304 case DT_VNC:
4305 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004306 if (vnc_display_open(ds, vnc_display) < 0) {
4307 PANIC("Unable to initialize VNC display");
4308 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004309
4310 if (show_vnc_port) {
4311 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4312 }
4313 break;
4314 default:
4315 break;
4316 }
4317 dpy_resize(ds);
4318
4319 dcl = ds->listeners;
4320 while (dcl != NULL) {
4321 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004322 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
4323 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004324 }
4325 dcl = dcl->next;
4326 }
4327
4328 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004329 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
4330 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004331 }
4332
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004333 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004334 qemu_chr_initial_reset();
4335
4336 if (monitor_device && monitor_hd)
4337 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4338
4339 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4340 const char *devname = serial_devices[i];
4341 if (devname && strcmp(devname, "none")) {
4342 if (strstart(devname, "vc", 0))
4343 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4344 }
4345 }
4346
4347 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4348 const char *devname = parallel_devices[i];
4349 if (devname && strcmp(devname, "none")) {
4350 if (strstart(devname, "vc", 0))
4351 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4352 }
4353 }
4354
4355 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4356 const char *devname = virtio_consoles[i];
4357 if (virtcon_hds[i] && devname) {
4358 if (strstart(devname, "vc", 0))
4359 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4360 }
4361 }
4362
4363 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004364 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004365 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004366 }
4367
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004368 /* call android-specific setup function */
4369 android_emulation_setup();
4370
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004371#if !defined(CONFIG_STANDALONE_CORE)
4372 // For the standalone emulator (UI+core in one executable) we need to
4373 // set the window title here.
4374 android_emulator_set_base_port(android_base_port);
4375#endif
4376
Ot ten Thije871da2a2010-09-20 10:29:22 +01004377 if (loadvm)
4378 do_loadvm(cur_mon, loadvm);
4379
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004380 if (incoming) {
4381 autostart = 0; /* fixme how to deal with -daemonize */
4382 qemu_start_incoming_migration(incoming);
4383 }
4384
4385 if (autostart)
4386 vm_start();
4387
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004388 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004389
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004390#ifdef CONFIG_ANDROID
4391 // This will notify the UI that the core is successfuly initialized
4392 android_core_init_completed();
4393#endif // CONFIG_ANDROID
4394
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004395 main_loop();
4396 quit_timers();
4397 net_cleanup();
4398 android_emulation_teardown();
4399 return 0;
4400}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004401
4402void
4403android_emulation_teardown(void)
4404{
4405 android_charmap_done();
4406}