blob: ebff1eb4fc9b6bc9f6d4df64b55fc4c16e5f2a84 [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* the following is needed on Linux to define ptsname() in stdlib.h */
26#if defined(__linux__)
27#define _GNU_SOURCE 1
28#endif
29
30#include "qemu-common.h"
31#include "hw/hw.h"
32#include "hw/boards.h"
33#include "hw/usb.h"
34#include "hw/pcmcia.h"
35#include "hw/pc.h"
36#include "hw/audiodev.h"
37#include "hw/isa.h"
38#include "hw/baum.h"
39#include "hw/goldfish_nand.h"
40#include "net.h"
41#include "console.h"
42#include "sysemu.h"
43#include "gdbstub.h"
44#include "qemu-timer.h"
45#include "qemu-char.h"
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +010046#include "blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070047#include "audio/audio.h"
48
49#include "qemu_file.h"
50#include "android/android.h"
51#include "charpipe.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "modem_driver.h"
53#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070054#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020055#include "android/hw-pipe-net.h"
56#include "android/hw-qemud.h"
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -070057#include "android/camera/camera-service.h"
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -080058#include "android/multitouch-port.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070059#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070060#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070061#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010062#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010063#include "android/utils/filelock.h"
64#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010065#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010066#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080067#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080068#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010069#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020070#include "android/opengles.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070071#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070072#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070073
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070074#ifdef CONFIG_MEMCHECK
75#include "memcheck/memcheck.h"
76#endif // CONFIG_MEMCHECK
77
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070078#include <unistd.h>
79#include <fcntl.h>
80#include <signal.h>
81#include <time.h>
82#include <errno.h>
83#include <sys/time.h>
84#include <zlib.h>
85
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070086/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070087#include "config-host.h"
88
89#ifndef _WIN32
90#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070091#include <sys/times.h>
92#include <sys/wait.h>
93#include <termios.h>
94#include <sys/mman.h>
95#include <sys/ioctl.h>
96#include <sys/resource.h>
97#include <sys/socket.h>
98#include <netinet/in.h>
99#include <net/if.h>
100#if defined(__NetBSD__)
101#include <net/if_tap.h>
102#endif
103#ifdef __linux__
104#include <linux/if_tun.h>
105#endif
106#include <arpa/inet.h>
107#include <dirent.h>
108#include <netdb.h>
109#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700110#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700111#include <sys/stat.h>
112#if defined(__FreeBSD__) || defined(__DragonFly__)
113#include <libutil.h>
114#else
115#include <util.h>
116#endif
117#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
118#include <freebsd/stdlib.h>
119#else
120#ifdef __linux__
121#include <pty.h>
122#include <malloc.h>
123#include <linux/rtc.h>
124
125/* For the benefit of older linux systems which don't supply it,
126 we use a local copy of hpet.h. */
127/* #include <linux/hpet.h> */
128#include "hpet.h"
129
130#include <linux/ppdev.h>
131#include <linux/parport.h>
132#endif
133#ifdef __sun__
134#include <sys/stat.h>
135#include <sys/ethernet.h>
136#include <sys/sockio.h>
137#include <netinet/arp.h>
138#include <netinet/in.h>
139#include <netinet/in_systm.h>
140#include <netinet/ip.h>
141#include <netinet/ip_icmp.h> // must come after ip.h
142#include <netinet/udp.h>
143#include <netinet/tcp.h>
144#include <net/if.h>
145#include <syslog.h>
146#include <stropts.h>
147#endif
148#endif
149#endif
150
151#if defined(__OpenBSD__)
152#include <util.h>
153#endif
154
155#if defined(CONFIG_VDE)
156#include <libvdeplug.h>
157#endif
158
159#ifdef _WIN32
160#include <windows.h>
161#include <malloc.h>
162#include <sys/timeb.h>
163#include <mmsystem.h>
164#define getopt_long_only getopt_long
165#define memalign(align, size) malloc(size)
166#endif
167
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200168#include "cpus.h"
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200169#include "arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700170
171#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200172int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700173#undef main
174#define main qemu_main
175#endif /* CONFIG_COCOA */
176
177#include "hw/hw.h"
178#include "hw/boards.h"
179#include "hw/usb.h"
180#include "hw/pcmcia.h"
181#include "hw/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700182#include "hw/isa.h"
183#include "hw/baum.h"
184#include "hw/bt.h"
185#include "hw/watchdog.h"
186#include "hw/smbios.h"
187#include "hw/xen.h"
188#include "bt-host.h"
189#include "net.h"
190#include "monitor.h"
191#include "console.h"
192#include "sysemu.h"
193#include "gdbstub.h"
194#include "qemu-timer.h"
195#include "qemu-char.h"
196#include "cache-utils.h"
197#include "block.h"
198#include "dma.h"
199#include "audio/audio.h"
200#include "migration.h"
201#include "kvm.h"
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800202#include "hax.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200203#ifdef CONFIG_KVM
204#include "kvm-android.h"
205#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700206#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700207#include "android/hw-lcd.h"
208#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200209#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700210#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100211#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700212
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -0800213#include "android/snaphost-android.h"
214
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700215#ifdef CONFIG_STANDALONE_CORE
216/* Verbose value used by the standalone emulator core (without UI) */
217unsigned long android_verbose;
218#endif // CONFIG_STANDALONE_CORE
219
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800220#if !defined(CONFIG_STANDALONE_CORE)
221/* in android/qemulator.c */
222extern void android_emulator_set_base_port(int port);
223#endif
224
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700225#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700226#undef main
227#define main qemu_main
228#endif
229
230#include "disas.h"
231
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700232#ifdef CONFIG_TRACE
David 'Digit' Turner406a59b2011-05-12 10:26:09 +0200233#include "android-trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700234#endif
235
236#include "qemu_socket.h"
237
238#if defined(CONFIG_SLIRP)
239#include "libslirp.h"
240#endif
241
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700242#define DEFAULT_RAM_SIZE 128
243
244/* Max number of USB devices that can be specified on the commandline. */
245#define MAX_USB_CMDLINE 8
246
247/* Max number of bluetooth switches on the commandline. */
248#define MAX_BT_CMDLINE 10
249
250/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700251
252static const char *data_dir;
253const char *bios_name = NULL;
254static void *ioport_opaque[MAX_IOPORTS];
255static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
256static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100257#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700258/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
259 to store the VM snapshots */
260DriveInfo drives_table[MAX_DRIVES+1];
261int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100262#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700263enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700264DisplayType display_type = DT_DEFAULT;
265const char* keyboard_layout = NULL;
266int64_t ticks_per_sec;
267ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200268const char *mem_path = NULL;
269#ifdef MAP_POPULATE
270int mem_prealloc = 0; /* force preallocation of physical target memory */
271#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700272int nb_nics;
273NICInfo nd_table[MAX_NICS];
274int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100275int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700276static int rtc_utc = 1;
277static int rtc_date_offset = -1; /* -1 means no change */
278int cirrus_vga_enabled = 1;
279int std_vga_enabled = 0;
280int vmsvga_enabled = 0;
281int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700282QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700283static int full_screen = 0;
284#ifdef CONFIG_SDL
285static int no_frame = 0;
286#endif
287int no_quit = 0;
288CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100289int serial_hds_count;
290
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700291CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
292CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
293#ifdef TARGET_I386
294int win2k_install_hack = 0;
295int rtc_td_hack = 0;
296#endif
297int usb_enabled = 0;
298int singlestep = 0;
299int smp_cpus = 1;
300const char *vnc_display;
301int acpi_enabled = 1;
302int no_hpet = 0;
Jun Nakajimaa381ef02011-12-17 19:13:25 -0800303int hax_disabled = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700304int no_virtio_balloon = 0;
305int fd_bootchk = 1;
306int no_reboot = 0;
307int no_shutdown = 0;
308int cursor_hide = 1;
309int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700310WatchdogTimerModel *watchdog = NULL;
311int watchdog_action = WDT_RESET;
312const char *option_rom[MAX_OPTION_ROMS];
313int nb_option_roms;
314int semihosting_enabled = 0;
315#ifdef TARGET_ARM
316int old_param = 0;
317#endif
318const char *qemu_name;
319int alt_grab = 0;
320#if defined(TARGET_SPARC) || defined(TARGET_PPC)
321unsigned int nb_prom_envs = 0;
322const char *prom_envs[MAX_PROM_ENVS];
323#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100324#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325int nb_drives_opt;
326struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100327#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700328int nb_numa_nodes;
329uint64_t node_mem[MAX_NODES];
330uint64_t node_cpumask[MAX_NODES];
331
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700332static QEMUTimer *nographic_timer;
333
334uint8_t qemu_uuid[16];
335
336
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700337int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700338extern char* audio_input_source;
339
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700340extern char* android_op_ports;
341extern char* android_op_port;
342extern char* android_op_report_console;
343extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700344// Path to the file containing specific key character map.
345char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700346
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700347/* Path to hardware initialization file passed with -android-hw option. */
348char* android_op_hwini = NULL;
349
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700350/* Memory checker options. */
351char* android_op_memcheck = NULL;
352
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700353/* -dns-server option value. */
354char* android_op_dns_server = NULL;
355
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700356/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700357char* android_op_radio = NULL;
358
359/* -gps option value. */
360char* android_op_gps = NULL;
361
362/* -audio option value. */
363char* android_op_audio = NULL;
364
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700365/* -cpu-delay option value. */
366char* android_op_cpu_delay = NULL;
367
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700368#ifdef CONFIG_NAND_LIMITS
369/* -nand-limits option value. */
370char* android_op_nand_limits = NULL;
371#endif // CONFIG_NAND_LIMITS
372
373/* -netspeed option value. */
374char* android_op_netspeed = NULL;
375
376/* -netdelay option value. */
377char* android_op_netdelay = NULL;
378
379/* -netfast option value. */
380int android_op_netfast = 0;
381
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700382/* -tcpdump option value. */
383char* android_op_tcpdump = NULL;
384
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700385/* -lcd-density option value. */
386char* android_op_lcd_density = NULL;
387
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700388/* -ui-port option value. This port will be used to report the core
389 * initialization completion.
390 */
391char* android_op_ui_port = NULL;
392
393/* -ui-settings option value. This value will be passed to the UI when new UI
394 * process is attaching to the core.
395 */
396char* android_op_ui_settings = NULL;
397
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800398/* -android-avdname option value. */
399char* android_op_avd_name = "unknown";
400
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700401extern int android_display_width;
402extern int android_display_height;
403extern int android_display_bpp;
404
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700405extern void dprint( const char* format, ... );
406
rich canningsd952f282011-03-01 15:40:09 -0800407const char* dns_log_filename = NULL;
408const char* drop_log_filename = NULL;
409static int rotate_logs_requested = 0;
410
Tim Baverstock24204cc2010-11-25 11:37:43 +0000411const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000412
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700413#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
414
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700415/* Reports the core initialization failure to the error stdout and to the UI
416 * socket before exiting the application.
417 * Parameters that are passed to this macro are used to format the error
418 * mesage using sprintf routine.
419 */
420#ifdef CONFIG_ANDROID
421#define PANIC(...) android_core_init_failure(__VA_ARGS__)
422#else
423#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
424 exit(1); \
425 } while (0)
426#endif // CONFIG_ANDROID
427
428/* Exits the core during initialization. */
429#ifdef CONFIG_ANDROID
430#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
431#else
432#define QEMU_EXIT(exit_code) exit(exit_code)
433#endif // CONFIG_ANDROID
434
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700435/***********************************************************/
436/* x86 ISA bus support */
437
438target_phys_addr_t isa_mem_base = 0;
439PicState2 *isa_pic;
440
441static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
442static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
443
444static uint32_t ioport_read(int index, uint32_t address)
445{
446 static IOPortReadFunc *default_func[3] = {
447 default_ioport_readb,
448 default_ioport_readw,
449 default_ioport_readl
450 };
451 IOPortReadFunc *func = ioport_read_table[index][address];
452 if (!func)
453 func = default_func[index];
454 return func(ioport_opaque[address], address);
455}
456
457static void ioport_write(int index, uint32_t address, uint32_t data)
458{
459 static IOPortWriteFunc *default_func[3] = {
460 default_ioport_writeb,
461 default_ioport_writew,
462 default_ioport_writel
463 };
464 IOPortWriteFunc *func = ioport_write_table[index][address];
465 if (!func)
466 func = default_func[index];
467 func(ioport_opaque[address], address, data);
468}
469
470static uint32_t default_ioport_readb(void *opaque, uint32_t address)
471{
472#ifdef DEBUG_UNUSED_IOPORT
473 fprintf(stderr, "unused inb: port=0x%04x\n", address);
474#endif
475 return 0xff;
476}
477
478static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
479{
480#ifdef DEBUG_UNUSED_IOPORT
481 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
482#endif
483}
484
485/* default is to make two byte accesses */
486static uint32_t default_ioport_readw(void *opaque, uint32_t address)
487{
488 uint32_t data;
489 data = ioport_read(0, address);
490 address = (address + 1) & (MAX_IOPORTS - 1);
491 data |= ioport_read(0, address) << 8;
492 return data;
493}
494
495static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
496{
497 ioport_write(0, address, data & 0xff);
498 address = (address + 1) & (MAX_IOPORTS - 1);
499 ioport_write(0, address, (data >> 8) & 0xff);
500}
501
502static uint32_t default_ioport_readl(void *opaque, uint32_t address)
503{
504#ifdef DEBUG_UNUSED_IOPORT
505 fprintf(stderr, "unused inl: port=0x%04x\n", address);
506#endif
507 return 0xffffffff;
508}
509
510static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
511{
512#ifdef DEBUG_UNUSED_IOPORT
513 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
514#endif
515}
516
rich canningsd952f282011-03-01 15:40:09 -0800517/*
518 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
519 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
520 * between the tasks that do not require restarting Qemu.
521 */
522void rotate_qemu_logs_handler(int signum) {
523 rotate_logs_requested = 1;
524}
525
526/*
527 * Resets the rotate_log_requested_flag. Normally called after qemu
528 * logs has been rotated.
529 */
530void reset_rotate_qemu_logs_request(void) {
531 rotate_logs_requested = 0;
532}
533
534/*
535 * Clears the passed qemu log when the rotate_logs_requested
536 * is set. We need to clear the logs between the tasks that do not
537 * require restarting Qemu.
538 */
539FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
540 FILE* new_log_fd = NULL;
541 if (old_log_fd) {
542 if (fclose(old_log_fd) == -1) {
543 fprintf(stderr, "Cannot close old_log fd\n");
544 exit(errno);
545 }
546 }
547
548 if (!filename) {
549 fprintf(stderr, "The log filename to be rotated is not provided");
550 exit(-1);
551 }
552
553 new_log_fd = fopen(filename , "wb+");
554 if (new_log_fd == NULL) {
555 fprintf(stderr, "Cannot open the log file: %s for write.\n",
556 filename);
557 exit(1);
558 }
559
560 return new_log_fd;
561}
562
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700563/***************/
564/* ballooning */
565
566static QEMUBalloonEvent *qemu_balloon_event;
567void *qemu_balloon_event_opaque;
568
569void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
570{
571 qemu_balloon_event = func;
572 qemu_balloon_event_opaque = opaque;
573}
574
575void qemu_balloon(ram_addr_t target)
576{
577 if (qemu_balloon_event)
578 qemu_balloon_event(qemu_balloon_event_opaque, target);
579}
580
581ram_addr_t qemu_balloon_status(void)
582{
583 if (qemu_balloon_event)
584 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
585 return 0;
586}
587
588/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700589/* host time/date access */
590void qemu_get_timedate(struct tm *tm, int offset)
591{
592 time_t ti;
593 struct tm *ret;
594
595 time(&ti);
596 ti += offset;
597 if (rtc_date_offset == -1) {
598 if (rtc_utc)
599 ret = gmtime(&ti);
600 else
601 ret = localtime(&ti);
602 } else {
603 ti -= rtc_date_offset;
604 ret = gmtime(&ti);
605 }
606
607 memcpy(tm, ret, sizeof(struct tm));
608}
609
610int qemu_timedate_diff(struct tm *tm)
611{
612 time_t seconds;
613
614 if (rtc_date_offset == -1)
615 if (rtc_utc)
616 seconds = mktimegm(tm);
617 else
618 seconds = mktime(tm);
619 else
620 seconds = mktimegm(tm) + rtc_date_offset;
621
622 return seconds - time(NULL);
623}
624
625
626#ifdef CONFIG_TRACE
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200627int tbflush_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700628static int exit_requested;
629
630void start_tracing()
631{
632 if (trace_filename == NULL)
633 return;
634 if (!tracing) {
635 fprintf(stderr,"-- start tracing --\n");
636 start_time = Now();
637 }
638 tracing = 1;
639 tbflush_requested = 1;
640 qemu_notify_event();
641}
642
643void stop_tracing()
644{
645 if (trace_filename == NULL)
646 return;
647 if (tracing) {
648 end_time = Now();
649 elapsed_usecs += end_time - start_time;
650 fprintf(stderr,"-- stop tracing --\n");
651 }
652 tracing = 0;
653 tbflush_requested = 1;
654 qemu_notify_event();
655}
656
657#ifndef _WIN32
658/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
659 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
660 */
661void sigusr_handler(int sig)
662{
663 if (sig == SIGUSR1)
664 start_tracing();
665 else
666 stop_tracing();
667}
668#endif
669
670/* This is the handler to catch control-C so that we can exit cleanly.
671 * This is needed when tracing to flush the buffers to disk.
672 */
673void sigint_handler(int sig)
674{
675 exit_requested = 1;
676 qemu_notify_event();
677}
678#endif /* CONFIG_TRACE */
679
680
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700681/***********************************************************/
682/* Bluetooth support */
683static int nb_hcis;
684static int cur_hci;
685static struct HCIInfo *hci_table[MAX_NICS];
686
687static struct bt_vlan_s {
688 struct bt_scatternet_s net;
689 int id;
690 struct bt_vlan_s *next;
691} *first_bt_vlan;
692
693/* find or alloc a new bluetooth "VLAN" */
694static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
695{
696 struct bt_vlan_s **pvlan, *vlan;
697 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
698 if (vlan->id == id)
699 return &vlan->net;
700 }
701 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
702 vlan->id = id;
703 pvlan = &first_bt_vlan;
704 while (*pvlan != NULL)
705 pvlan = &(*pvlan)->next;
706 *pvlan = vlan;
707 return &vlan->net;
708}
709
710static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
711{
712}
713
714static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
715{
716 return -ENOTSUP;
717}
718
719static struct HCIInfo null_hci = {
720 .cmd_send = null_hci_send,
721 .sco_send = null_hci_send,
722 .acl_send = null_hci_send,
723 .bdaddr_set = null_hci_addr_set,
724};
725
726struct HCIInfo *qemu_next_hci(void)
727{
728 if (cur_hci == nb_hcis)
729 return &null_hci;
730
731 return hci_table[cur_hci++];
732}
733
734static struct HCIInfo *hci_init(const char *str)
735{
736 char *endp;
737 struct bt_scatternet_s *vlan = 0;
738
739 if (!strcmp(str, "null"))
740 /* null */
741 return &null_hci;
742 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
743 /* host[:hciN] */
744 return bt_host_hci(str[4] ? str + 5 : "hci0");
745 else if (!strncmp(str, "hci", 3)) {
746 /* hci[,vlan=n] */
747 if (str[3]) {
748 if (!strncmp(str + 3, ",vlan=", 6)) {
749 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
750 if (*endp)
751 vlan = 0;
752 }
753 } else
754 vlan = qemu_find_bt_vlan(0);
755 if (vlan)
756 return bt_new_hci(vlan);
757 }
758
759 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
760
761 return 0;
762}
763
764static int bt_hci_parse(const char *str)
765{
766 struct HCIInfo *hci;
767 bdaddr_t bdaddr;
768
769 if (nb_hcis >= MAX_NICS) {
770 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
771 return -1;
772 }
773
774 hci = hci_init(str);
775 if (!hci)
776 return -1;
777
778 bdaddr.b[0] = 0x52;
779 bdaddr.b[1] = 0x54;
780 bdaddr.b[2] = 0x00;
781 bdaddr.b[3] = 0x12;
782 bdaddr.b[4] = 0x34;
783 bdaddr.b[5] = 0x56 + nb_hcis;
784 hci->bdaddr_set(hci, bdaddr.b);
785
786 hci_table[nb_hcis++] = hci;
787
788 return 0;
789}
790
791static void bt_vhci_add(int vlan_id)
792{
793 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
794
795 if (!vlan->slave)
796 fprintf(stderr, "qemu: warning: adding a VHCI to "
797 "an empty scatternet %i\n", vlan_id);
798
799 bt_vhci_init(bt_new_hci(vlan));
800}
801
802static struct bt_device_s *bt_device_add(const char *opt)
803{
804 struct bt_scatternet_s *vlan;
805 int vlan_id = 0;
806 char *endp = strstr(opt, ",vlan=");
807 int len = (endp ? endp - opt : strlen(opt)) + 1;
808 char devname[10];
809
810 pstrcpy(devname, MIN(sizeof(devname), len), opt);
811
812 if (endp) {
813 vlan_id = strtol(endp + 6, &endp, 0);
814 if (*endp) {
815 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
816 return 0;
817 }
818 }
819
820 vlan = qemu_find_bt_vlan(vlan_id);
821
822 if (!vlan->slave)
823 fprintf(stderr, "qemu: warning: adding a slave device to "
824 "an empty scatternet %i\n", vlan_id);
825
826 if (!strcmp(devname, "keyboard"))
827 return bt_keyboard_init(vlan);
828
829 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
830 return 0;
831}
832
833static int bt_parse(const char *opt)
834{
835 const char *endp, *p;
836 int vlan;
837
838 if (strstart(opt, "hci", &endp)) {
839 if (!*endp || *endp == ',') {
840 if (*endp)
841 if (!strstart(endp, ",vlan=", 0))
842 opt = endp + 1;
843
844 return bt_hci_parse(opt);
845 }
846 } else if (strstart(opt, "vhci", &endp)) {
847 if (!*endp || *endp == ',') {
848 if (*endp) {
849 if (strstart(endp, ",vlan=", &p)) {
850 vlan = strtol(p, (char **) &endp, 0);
851 if (*endp) {
852 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
853 return 1;
854 }
855 } else {
856 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
857 return 1;
858 }
859 } else
860 vlan = 0;
861
862 bt_vhci_add(vlan);
863 return 0;
864 }
865 } else if (strstart(opt, "device:", &endp))
866 return !bt_device_add(endp);
867
868 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
869 return 1;
870}
871
872/***********************************************************/
873/* QEMU Block devices */
874
875#define HD_ALIAS "index=%d,media=disk"
876#define CDROM_ALIAS "index=2,media=cdrom"
877#define FD_ALIAS "index=%d,if=floppy"
878#define PFLASH_ALIAS "if=pflash"
879#define MTD_ALIAS "if=mtd"
880#define SD_ALIAS "index=0,if=sd"
881
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100882static int drive_init_func(QemuOpts *opts, void *opaque)
883{
884 int *use_scsi = opaque;
885 int fatal_error = 0;
886
887 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
888 if (fatal_error)
889 return 1;
890 }
891 return 0;
892}
893
894static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
895{
896 if (NULL == qemu_opt_get(opts, "snapshot")) {
897 qemu_opt_set(opts, "snapshot", "on");
898 }
899 return 0;
900}
901
902#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700903static int drive_opt_get_free_idx(void)
904{
905 int index;
906
907 for (index = 0; index < MAX_DRIVES; index++)
908 if (!drives_opt[index].used) {
909 drives_opt[index].used = 1;
910 return index;
911 }
912
913 return -1;
914}
915
916static int drive_get_free_idx(void)
917{
918 int index;
919
920 for (index = 0; index < MAX_DRIVES; index++)
921 if (!drives_table[index].used) {
922 drives_table[index].used = 1;
923 return index;
924 }
925
926 return -1;
927}
928
929int drive_add(const char *file, const char *fmt, ...)
930{
931 va_list ap;
932 int index = drive_opt_get_free_idx();
933
934 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
935 fprintf(stderr, "qemu: too many drives\n");
936 return -1;
937 }
938
939 drives_opt[index].file = file;
940 va_start(ap, fmt);
941 vsnprintf(drives_opt[index].opt,
942 sizeof(drives_opt[0].opt), fmt, ap);
943 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700944
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700945 nb_drives_opt++;
946 return index;
947}
948
949void drive_remove(int index)
950{
951 drives_opt[index].used = 0;
952 nb_drives_opt--;
953}
954
955int drive_get_index(BlockInterfaceType type, int bus, int unit)
956{
957 int index;
958
959 /* seek interface, bus and unit */
960
961 for (index = 0; index < MAX_DRIVES; index++)
962 if (drives_table[index].type == type &&
963 drives_table[index].bus == bus &&
964 drives_table[index].unit == unit &&
965 drives_table[index].used)
966 return index;
967
968 return -1;
969}
970
971int drive_get_max_bus(BlockInterfaceType type)
972{
973 int max_bus;
974 int index;
975
976 max_bus = -1;
977 for (index = 0; index < nb_drives; index++) {
978 if(drives_table[index].type == type &&
979 drives_table[index].bus > max_bus)
980 max_bus = drives_table[index].bus;
981 }
982 return max_bus;
983}
984
985const char *drive_get_serial(BlockDriverState *bdrv)
986{
987 int index;
988
989 for (index = 0; index < nb_drives; index++)
990 if (drives_table[index].bdrv == bdrv)
991 return drives_table[index].serial;
992
993 return "\0";
994}
995
996BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
997{
998 int index;
999
1000 for (index = 0; index < nb_drives; index++)
1001 if (drives_table[index].bdrv == bdrv)
1002 return drives_table[index].onerror;
1003
1004 return BLOCK_ERR_STOP_ENOSPC;
1005}
1006
1007static void bdrv_format_print(void *opaque, const char *name)
1008{
1009 fprintf(stderr, " %s", name);
1010}
1011
1012void drive_uninit(BlockDriverState *bdrv)
1013{
1014 int i;
1015
1016 for (i = 0; i < MAX_DRIVES; i++)
1017 if (drives_table[i].bdrv == bdrv) {
1018 drives_table[i].bdrv = NULL;
1019 drives_table[i].used = 0;
1020 drive_remove(drives_table[i].drive_opt_idx);
1021 nb_drives--;
1022 break;
1023 }
1024}
1025
1026int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1027{
1028 char buf[128];
1029 char file[1024];
1030 char devname[128];
1031 char serial[21];
1032 const char *mediastr = "";
1033 BlockInterfaceType type;
1034 enum { MEDIA_DISK, MEDIA_CDROM } media;
1035 int bus_id, unit_id;
1036 int cyls, heads, secs, translation;
1037 BlockDriverState *bdrv;
1038 BlockDriver *drv = NULL;
1039 QEMUMachine *machine = opaque;
1040 int max_devs;
1041 int index;
1042 int cache;
1043 int bdrv_flags, onerror;
1044 int drives_table_idx;
1045 char *str = arg->opt;
1046 static const char * const params[] = { "bus", "unit", "if", "index",
1047 "cyls", "heads", "secs", "trans",
1048 "media", "snapshot", "file",
1049 "cache", "format", "serial", "werror",
1050 NULL };
1051
1052 if (check_params(buf, sizeof(buf), params, str) < 0) {
1053 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1054 buf, str);
1055 return -1;
1056 }
1057
1058 file[0] = 0;
1059 cyls = heads = secs = 0;
1060 bus_id = 0;
1061 unit_id = -1;
1062 translation = BIOS_ATA_TRANSLATION_AUTO;
1063 index = -1;
1064 cache = 3;
1065
1066 if (machine->use_scsi) {
1067 type = IF_SCSI;
1068 max_devs = MAX_SCSI_DEVS;
1069 pstrcpy(devname, sizeof(devname), "scsi");
1070 } else {
1071 type = IF_IDE;
1072 max_devs = MAX_IDE_DEVS;
1073 pstrcpy(devname, sizeof(devname), "ide");
1074 }
1075 media = MEDIA_DISK;
1076
1077 /* extract parameters */
1078
1079 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1080 bus_id = strtol(buf, NULL, 0);
1081 if (bus_id < 0) {
1082 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1083 return -1;
1084 }
1085 }
1086
1087 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1088 unit_id = strtol(buf, NULL, 0);
1089 if (unit_id < 0) {
1090 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1091 return -1;
1092 }
1093 }
1094
1095 if (get_param_value(buf, sizeof(buf), "if", str)) {
1096 pstrcpy(devname, sizeof(devname), buf);
1097 if (!strcmp(buf, "ide")) {
1098 type = IF_IDE;
1099 max_devs = MAX_IDE_DEVS;
1100 } else if (!strcmp(buf, "scsi")) {
1101 type = IF_SCSI;
1102 max_devs = MAX_SCSI_DEVS;
1103 } else if (!strcmp(buf, "floppy")) {
1104 type = IF_FLOPPY;
1105 max_devs = 0;
1106 } else if (!strcmp(buf, "pflash")) {
1107 type = IF_PFLASH;
1108 max_devs = 0;
1109 } else if (!strcmp(buf, "mtd")) {
1110 type = IF_MTD;
1111 max_devs = 0;
1112 } else if (!strcmp(buf, "sd")) {
1113 type = IF_SD;
1114 max_devs = 0;
1115 } else if (!strcmp(buf, "virtio")) {
1116 type = IF_VIRTIO;
1117 max_devs = 0;
1118 } else if (!strcmp(buf, "xen")) {
1119 type = IF_XEN;
1120 max_devs = 0;
1121 } else {
1122 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1123 return -1;
1124 }
1125 }
1126
1127 if (get_param_value(buf, sizeof(buf), "index", str)) {
1128 index = strtol(buf, NULL, 0);
1129 if (index < 0) {
1130 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1131 return -1;
1132 }
1133 }
1134
1135 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1136 cyls = strtol(buf, NULL, 0);
1137 }
1138
1139 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1140 heads = strtol(buf, NULL, 0);
1141 }
1142
1143 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1144 secs = strtol(buf, NULL, 0);
1145 }
1146
1147 if (cyls || heads || secs) {
1148 if (cyls < 1 || cyls > 16383) {
1149 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1150 return -1;
1151 }
1152 if (heads < 1 || heads > 16) {
1153 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1154 return -1;
1155 }
1156 if (secs < 1 || secs > 63) {
1157 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1158 return -1;
1159 }
1160 }
1161
1162 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1163 if (!cyls) {
1164 fprintf(stderr,
1165 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1166 str);
1167 return -1;
1168 }
1169 if (!strcmp(buf, "none"))
1170 translation = BIOS_ATA_TRANSLATION_NONE;
1171 else if (!strcmp(buf, "lba"))
1172 translation = BIOS_ATA_TRANSLATION_LBA;
1173 else if (!strcmp(buf, "auto"))
1174 translation = BIOS_ATA_TRANSLATION_AUTO;
1175 else {
1176 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1177 return -1;
1178 }
1179 }
1180
1181 if (get_param_value(buf, sizeof(buf), "media", str)) {
1182 if (!strcmp(buf, "disk")) {
1183 media = MEDIA_DISK;
1184 } else if (!strcmp(buf, "cdrom")) {
1185 if (cyls || secs || heads) {
1186 fprintf(stderr,
1187 "qemu: '%s' invalid physical CHS format\n", str);
1188 return -1;
1189 }
1190 media = MEDIA_CDROM;
1191 } else {
1192 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1193 return -1;
1194 }
1195 }
1196
1197 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1198 if (!strcmp(buf, "on"))
1199 snapshot = 1;
1200 else if (!strcmp(buf, "off"))
1201 snapshot = 0;
1202 else {
1203 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1204 return -1;
1205 }
1206 }
1207
1208 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1209 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1210 cache = 0;
1211 else if (!strcmp(buf, "writethrough"))
1212 cache = 1;
1213 else if (!strcmp(buf, "writeback"))
1214 cache = 2;
1215 else {
1216 fprintf(stderr, "qemu: invalid cache option\n");
1217 return -1;
1218 }
1219 }
1220
1221 if (get_param_value(buf, sizeof(buf), "format", str)) {
1222 if (strcmp(buf, "?") == 0) {
1223 fprintf(stderr, "qemu: Supported formats:");
1224 bdrv_iterate_format(bdrv_format_print, NULL);
1225 fprintf(stderr, "\n");
1226 return -1;
1227 }
1228 drv = bdrv_find_format(buf);
1229 if (!drv) {
1230 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1231 return -1;
1232 }
1233 }
1234
1235 if (arg->file == NULL)
1236 get_param_value(file, sizeof(file), "file", str);
1237 else
1238 pstrcpy(file, sizeof(file), arg->file);
1239
1240 if (!get_param_value(serial, sizeof(serial), "serial", str))
1241 memset(serial, 0, sizeof(serial));
1242
1243 onerror = BLOCK_ERR_STOP_ENOSPC;
1244 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1245 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1246 fprintf(stderr, "werror is no supported by this format\n");
1247 return -1;
1248 }
1249 if (!strcmp(buf, "ignore"))
1250 onerror = BLOCK_ERR_IGNORE;
1251 else if (!strcmp(buf, "enospc"))
1252 onerror = BLOCK_ERR_STOP_ENOSPC;
1253 else if (!strcmp(buf, "stop"))
1254 onerror = BLOCK_ERR_STOP_ANY;
1255 else if (!strcmp(buf, "report"))
1256 onerror = BLOCK_ERR_REPORT;
1257 else {
1258 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1259 return -1;
1260 }
1261 }
1262
1263 /* compute bus and unit according index */
1264
1265 if (index != -1) {
1266 if (bus_id != 0 || unit_id != -1) {
1267 fprintf(stderr,
1268 "qemu: '%s' index cannot be used with bus and unit\n", str);
1269 return -1;
1270 }
1271 if (max_devs == 0)
1272 {
1273 unit_id = index;
1274 bus_id = 0;
1275 } else {
1276 unit_id = index % max_devs;
1277 bus_id = index / max_devs;
1278 }
1279 }
1280
1281 /* if user doesn't specify a unit_id,
1282 * try to find the first free
1283 */
1284
1285 if (unit_id == -1) {
1286 unit_id = 0;
1287 while (drive_get_index(type, bus_id, unit_id) != -1) {
1288 unit_id++;
1289 if (max_devs && unit_id >= max_devs) {
1290 unit_id -= max_devs;
1291 bus_id++;
1292 }
1293 }
1294 }
1295
1296 /* check unit id */
1297
1298 if (max_devs && unit_id >= max_devs) {
1299 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1300 str, unit_id, max_devs - 1);
1301 return -1;
1302 }
1303
1304 /*
1305 * ignore multiple definitions
1306 */
1307
1308 if (drive_get_index(type, bus_id, unit_id) != -1)
1309 return -2;
1310
1311 /* init */
1312
1313 if (type == IF_IDE || type == IF_SCSI)
1314 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1315 if (max_devs)
1316 snprintf(buf, sizeof(buf), "%s%i%s%i",
1317 devname, bus_id, mediastr, unit_id);
1318 else
1319 snprintf(buf, sizeof(buf), "%s%s%i",
1320 devname, mediastr, unit_id);
1321 bdrv = bdrv_new(buf);
1322 drives_table_idx = drive_get_free_idx();
1323 drives_table[drives_table_idx].bdrv = bdrv;
1324 drives_table[drives_table_idx].type = type;
1325 drives_table[drives_table_idx].bus = bus_id;
1326 drives_table[drives_table_idx].unit = unit_id;
1327 drives_table[drives_table_idx].onerror = onerror;
1328 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1329 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1330 nb_drives++;
1331
1332 switch(type) {
1333 case IF_IDE:
1334 case IF_SCSI:
1335 case IF_XEN:
1336 switch(media) {
1337 case MEDIA_DISK:
1338 if (cyls != 0) {
1339 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1340 bdrv_set_translation_hint(bdrv, translation);
1341 }
1342 break;
1343 case MEDIA_CDROM:
1344 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1345 break;
1346 }
1347 break;
1348 case IF_SD:
1349 /* FIXME: This isn't really a floppy, but it's a reasonable
1350 approximation. */
1351 case IF_FLOPPY:
1352 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1353 break;
1354 case IF_PFLASH:
1355 case IF_MTD:
1356 case IF_VIRTIO:
1357 break;
1358 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001359 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001360 abort();
1361 }
1362 if (!file[0])
1363 return -2;
1364 bdrv_flags = 0;
1365 if (snapshot) {
1366 bdrv_flags |= BDRV_O_SNAPSHOT;
1367 cache = 2; /* always use write-back with snapshot */
1368 }
1369 if (cache == 0) /* no caching */
1370 bdrv_flags |= BDRV_O_NOCACHE;
1371 else if (cache == 2) /* write-back */
1372 bdrv_flags |= BDRV_O_CACHE_WB;
1373 else if (cache == 3) /* not specified */
1374 bdrv_flags |= BDRV_O_CACHE_DEF;
1375 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1376 fprintf(stderr, "qemu: could not open disk image %s\n",
1377 file);
1378 return -1;
1379 }
1380 if (bdrv_key_required(bdrv))
1381 autostart = 0;
1382 return drives_table_idx;
1383}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001384#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001385
1386static void numa_add(const char *optarg)
1387{
1388 char option[128];
1389 char *endptr;
1390 unsigned long long value, endvalue;
1391 int nodenr;
1392
1393 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1394 if (!strcmp(option, "node")) {
1395 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1396 nodenr = nb_numa_nodes;
1397 } else {
1398 nodenr = strtoull(option, NULL, 10);
1399 }
1400
1401 if (get_param_value(option, 128, "mem", optarg) == 0) {
1402 node_mem[nodenr] = 0;
1403 } else {
1404 value = strtoull(option, &endptr, 0);
1405 switch (*endptr) {
1406 case 0: case 'M': case 'm':
1407 value <<= 20;
1408 break;
1409 case 'G': case 'g':
1410 value <<= 30;
1411 break;
1412 }
1413 node_mem[nodenr] = value;
1414 }
1415 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1416 node_cpumask[nodenr] = 0;
1417 } else {
1418 value = strtoull(option, &endptr, 10);
1419 if (value >= 64) {
1420 value = 63;
1421 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1422 } else {
1423 if (*endptr == '-') {
1424 endvalue = strtoull(endptr+1, &endptr, 10);
1425 if (endvalue >= 63) {
1426 endvalue = 62;
1427 fprintf(stderr,
1428 "only 63 CPUs in NUMA mode supported.\n");
1429 }
1430 value = (1 << (endvalue + 1)) - (1 << value);
1431 } else {
1432 value = 1 << value;
1433 }
1434 }
1435 node_cpumask[nodenr] = value;
1436 }
1437 nb_numa_nodes++;
1438 }
1439 return;
1440}
1441
1442/***********************************************************/
1443/* USB devices */
1444
1445static USBPort *used_usb_ports;
1446static USBPort *free_usb_ports;
1447
1448/* ??? Maybe change this to register a hub to keep track of the topology. */
1449void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1450 usb_attachfn attach)
1451{
1452 port->opaque = opaque;
1453 port->index = index;
1454 port->attach = attach;
1455 port->next = free_usb_ports;
1456 free_usb_ports = port;
1457}
1458
1459int usb_device_add_dev(USBDevice *dev)
1460{
1461 USBPort *port;
1462
1463 /* Find a USB port to add the device to. */
1464 port = free_usb_ports;
1465 if (!port->next) {
1466 USBDevice *hub;
1467
1468 /* Create a new hub and chain it on. */
1469 free_usb_ports = NULL;
1470 port->next = used_usb_ports;
1471 used_usb_ports = port;
1472
1473 hub = usb_hub_init(VM_USB_HUB_SIZE);
1474 usb_attach(port, hub);
1475 port = free_usb_ports;
1476 }
1477
1478 free_usb_ports = port->next;
1479 port->next = used_usb_ports;
1480 used_usb_ports = port;
1481 usb_attach(port, dev);
1482 return 0;
1483}
1484
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001485#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001486static void usb_msd_password_cb(void *opaque, int err)
1487{
1488 USBDevice *dev = opaque;
1489
1490 if (!err)
1491 usb_device_add_dev(dev);
1492 else
1493 dev->handle_destroy(dev);
1494}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001495#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001496
1497static int usb_device_add(const char *devname, int is_hotplug)
1498{
1499 const char *p;
1500 USBDevice *dev;
1501
1502 if (!free_usb_ports)
1503 return -1;
1504
1505 if (strstart(devname, "host:", &p)) {
1506 dev = usb_host_device_open(p);
1507 } else if (!strcmp(devname, "mouse")) {
1508 dev = usb_mouse_init();
1509 } else if (!strcmp(devname, "tablet")) {
1510 dev = usb_tablet_init();
1511 } else if (!strcmp(devname, "keyboard")) {
1512 dev = usb_keyboard_init();
1513 } else if (strstart(devname, "disk:", &p)) {
1514#if 0
1515 BlockDriverState *bs;
1516#endif
1517 dev = usb_msd_init(p);
1518 if (!dev)
1519 return -1;
1520#if 0
1521 bs = usb_msd_get_bdrv(dev);
1522 if (bdrv_key_required(bs)) {
1523 autostart = 0;
1524 if (is_hotplug) {
1525 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1526 dev);
1527 return 0;
1528 }
1529 }
1530 } else if (!strcmp(devname, "wacom-tablet")) {
1531 dev = usb_wacom_init();
1532 } else if (strstart(devname, "serial:", &p)) {
1533 dev = usb_serial_init(p);
1534#ifdef CONFIG_BRLAPI
1535 } else if (!strcmp(devname, "braille")) {
1536 dev = usb_baum_init();
1537#endif
1538 } else if (strstart(devname, "net:", &p)) {
1539 int nic = nb_nics;
1540
1541 if (net_client_init("nic", p) < 0)
1542 return -1;
1543 nd_table[nic].model = "usb";
1544 dev = usb_net_init(&nd_table[nic]);
1545 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1546 dev = usb_bt_init(devname[2] ? hci_init(p) :
1547 bt_new_hci(qemu_find_bt_vlan(0)));
1548#endif
1549 } else {
1550 return -1;
1551 }
1552 if (!dev)
1553 return -1;
1554
1555 return usb_device_add_dev(dev);
1556}
1557
1558int usb_device_del_addr(int bus_num, int addr)
1559{
1560 USBPort *port;
1561 USBPort **lastp;
1562 USBDevice *dev;
1563
1564 if (!used_usb_ports)
1565 return -1;
1566
1567 if (bus_num != 0)
1568 return -1;
1569
1570 lastp = &used_usb_ports;
1571 port = used_usb_ports;
1572 while (port && port->dev->addr != addr) {
1573 lastp = &port->next;
1574 port = port->next;
1575 }
1576
1577 if (!port)
1578 return -1;
1579
1580 dev = port->dev;
1581 *lastp = port->next;
1582 usb_attach(port, NULL);
1583 dev->handle_destroy(dev);
1584 port->next = free_usb_ports;
1585 free_usb_ports = port;
1586 return 0;
1587}
1588
1589static int usb_device_del(const char *devname)
1590{
1591 int bus_num, addr;
1592 const char *p;
1593
1594 if (strstart(devname, "host:", &p))
1595 return usb_host_device_close(p);
1596
1597 if (!used_usb_ports)
1598 return -1;
1599
1600 p = strchr(devname, '.');
1601 if (!p)
1602 return -1;
1603 bus_num = strtoul(devname, NULL, 0);
1604 addr = strtoul(p + 1, NULL, 0);
1605
1606 return usb_device_del_addr(bus_num, addr);
1607}
1608
1609void do_usb_add(Monitor *mon, const char *devname)
1610{
1611 usb_device_add(devname, 1);
1612}
1613
1614void do_usb_del(Monitor *mon, const char *devname)
1615{
1616 usb_device_del(devname);
1617}
1618
1619void usb_info(Monitor *mon)
1620{
1621 USBDevice *dev;
1622 USBPort *port;
1623 const char *speed_str;
1624
1625 if (!usb_enabled) {
1626 monitor_printf(mon, "USB support not enabled\n");
1627 return;
1628 }
1629
1630 for (port = used_usb_ports; port; port = port->next) {
1631 dev = port->dev;
1632 if (!dev)
1633 continue;
1634 switch(dev->speed) {
1635 case USB_SPEED_LOW:
1636 speed_str = "1.5";
1637 break;
1638 case USB_SPEED_FULL:
1639 speed_str = "12";
1640 break;
1641 case USB_SPEED_HIGH:
1642 speed_str = "480";
1643 break;
1644 default:
1645 speed_str = "?";
1646 break;
1647 }
1648 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1649 0, dev->addr, speed_str, dev->devname);
1650 }
1651}
1652
1653/***********************************************************/
1654/* PCMCIA/Cardbus */
1655
1656static struct pcmcia_socket_entry_s {
1657 PCMCIASocket *socket;
1658 struct pcmcia_socket_entry_s *next;
1659} *pcmcia_sockets = 0;
1660
1661void pcmcia_socket_register(PCMCIASocket *socket)
1662{
1663 struct pcmcia_socket_entry_s *entry;
1664
1665 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1666 entry->socket = socket;
1667 entry->next = pcmcia_sockets;
1668 pcmcia_sockets = entry;
1669}
1670
1671void pcmcia_socket_unregister(PCMCIASocket *socket)
1672{
1673 struct pcmcia_socket_entry_s *entry, **ptr;
1674
1675 ptr = &pcmcia_sockets;
1676 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1677 if (entry->socket == socket) {
1678 *ptr = entry->next;
1679 qemu_free(entry);
1680 }
1681}
1682
1683void pcmcia_info(Monitor *mon)
1684{
1685 struct pcmcia_socket_entry_s *iter;
1686
1687 if (!pcmcia_sockets)
1688 monitor_printf(mon, "No PCMCIA sockets\n");
1689
1690 for (iter = pcmcia_sockets; iter; iter = iter->next)
1691 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1692 iter->socket->attached ? iter->socket->card_string :
1693 "Empty");
1694}
1695
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001696/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001697/* machine registration */
1698
1699static QEMUMachine *first_machine = NULL;
1700QEMUMachine *current_machine = NULL;
1701
1702int qemu_register_machine(QEMUMachine *m)
1703{
1704 QEMUMachine **pm;
1705 pm = &first_machine;
1706 while (*pm != NULL)
1707 pm = &(*pm)->next;
1708 m->next = NULL;
1709 *pm = m;
1710 return 0;
1711}
1712
1713static QEMUMachine *find_machine(const char *name)
1714{
1715 QEMUMachine *m;
1716
1717 for(m = first_machine; m != NULL; m = m->next) {
1718 if (!strcmp(m->name, name))
1719 return m;
1720 }
1721 return NULL;
1722}
1723
1724static QEMUMachine *find_default_machine(void)
1725{
1726 QEMUMachine *m;
1727
1728 for(m = first_machine; m != NULL; m = m->next) {
1729 if (m->is_default) {
1730 return m;
1731 }
1732 }
1733 return NULL;
1734}
1735
1736/***********************************************************/
1737/* main execution loop */
1738
1739static void gui_update(void *opaque)
1740{
1741 uint64_t interval = GUI_REFRESH_INTERVAL;
1742 DisplayState *ds = opaque;
1743 DisplayChangeListener *dcl = ds->listeners;
1744
1745 dpy_refresh(ds);
1746
1747 while (dcl != NULL) {
1748 if (dcl->gui_timer_interval &&
1749 dcl->gui_timer_interval < interval)
1750 interval = dcl->gui_timer_interval;
1751 dcl = dcl->next;
1752 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001753 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001754}
1755
1756static void nographic_update(void *opaque)
1757{
1758 uint64_t interval = GUI_REFRESH_INTERVAL;
1759
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001760 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001761}
1762
1763struct vm_change_state_entry {
1764 VMChangeStateHandler *cb;
1765 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001766 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001767};
1768
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001769static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001770
1771VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1772 void *opaque)
1773{
1774 VMChangeStateEntry *e;
1775
1776 e = qemu_mallocz(sizeof (*e));
1777
1778 e->cb = cb;
1779 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001780 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001781 return e;
1782}
1783
1784void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1785{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001786 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001787 qemu_free (e);
1788}
1789
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001790void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001791{
1792 VMChangeStateEntry *e;
1793
1794 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1795 e->cb(e->opaque, running, reason);
1796 }
1797}
1798
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001799void vm_start(void)
1800{
1801 if (!vm_running) {
1802 cpu_enable_ticks();
1803 vm_running = 1;
1804 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001805 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001806 resume_all_vcpus();
1807 }
1808}
1809
1810/* reset/shutdown handler */
1811
1812typedef struct QEMUResetEntry {
1813 QEMUResetHandler *func;
1814 void *opaque;
1815 int order;
1816 struct QEMUResetEntry *next;
1817} QEMUResetEntry;
1818
1819static QEMUResetEntry *first_reset_entry;
1820static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001821static int shutdown_requested, shutdown_signal = -1;
1822static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001823static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001824int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001825static int vmstop_requested;
1826
1827int qemu_shutdown_requested(void)
1828{
1829 int r = shutdown_requested;
1830 shutdown_requested = 0;
1831 return r;
1832}
1833
1834int qemu_reset_requested(void)
1835{
1836 int r = reset_requested;
1837 reset_requested = 0;
1838 return r;
1839}
1840
1841int qemu_powerdown_requested(void)
1842{
1843 int r = powerdown_requested;
1844 powerdown_requested = 0;
1845 return r;
1846}
1847
1848static int qemu_debug_requested(void)
1849{
1850 int r = debug_requested;
1851 debug_requested = 0;
1852 return r;
1853}
1854
1855static int qemu_vmstop_requested(void)
1856{
1857 int r = vmstop_requested;
1858 vmstop_requested = 0;
1859 return r;
1860}
1861
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001862void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1863{
1864 QEMUResetEntry **pre, *re;
1865
1866 pre = &first_reset_entry;
1867 while (*pre != NULL && (*pre)->order >= order) {
1868 pre = &(*pre)->next;
1869 }
1870 re = qemu_mallocz(sizeof(QEMUResetEntry));
1871 re->func = func;
1872 re->opaque = opaque;
1873 re->order = order;
1874 re->next = NULL;
1875 *pre = re;
1876}
1877
1878void qemu_system_reset(void)
1879{
1880 QEMUResetEntry *re;
1881
1882 /* reset all devices */
1883 for(re = first_reset_entry; re != NULL; re = re->next) {
1884 re->func(re->opaque);
1885 }
1886}
1887
1888void qemu_system_reset_request(void)
1889{
1890 if (no_reboot) {
1891 shutdown_requested = 1;
1892 } else {
1893 reset_requested = 1;
1894 }
1895 qemu_notify_event();
1896}
1897
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001898void qemu_system_killed(int signal, pid_t pid)
1899{
1900 shutdown_signal = signal;
1901 shutdown_pid = pid;
1902 qemu_system_shutdown_request();
1903}
1904
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001905void qemu_system_shutdown_request(void)
1906{
1907 shutdown_requested = 1;
1908 qemu_notify_event();
1909}
1910
1911void qemu_system_powerdown_request(void)
1912{
1913 powerdown_requested = 1;
1914 qemu_notify_event();
1915}
1916
1917#ifdef CONFIG_IOTHREAD
1918static void qemu_system_vmstop_request(int reason)
1919{
1920 vmstop_requested = reason;
1921 qemu_notify_event();
1922}
1923#endif
1924
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001925void main_loop_wait(int timeout)
1926{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001927 fd_set rfds, wfds, xfds;
1928 int ret, nfds;
1929 struct timeval tv;
1930
1931 qemu_bh_update_timeout(&timeout);
1932
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001933 os_host_main_loop_wait(&timeout);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001934
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001935
1936 tv.tv_sec = timeout / 1000;
1937 tv.tv_usec = (timeout % 1000) * 1000;
1938
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001939 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001940
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001941 /* XXX: separate device handlers from system ones */
1942 nfds = -1;
1943 FD_ZERO(&rfds);
1944 FD_ZERO(&wfds);
1945 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001946 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001947 if (slirp_is_inited()) {
1948 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1949 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001950
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001951 qemu_mutex_unlock_iothread();
1952 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1953 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001954 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001955 if (slirp_is_inited()) {
1956 if (ret < 0) {
1957 FD_ZERO(&rfds);
1958 FD_ZERO(&wfds);
1959 FD_ZERO(&xfds);
1960 }
1961 slirp_select_poll(&rfds, &wfds, &xfds);
1962 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001963 charpipe_poll();
1964
David Turner6a9ef172010-09-09 22:54:36 +02001965 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07001966
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001967 /* Check bottom-halves last in case any of the earlier events triggered
1968 them. */
1969 qemu_bh_poll();
1970
1971}
1972
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001973static int vm_can_run(void)
1974{
1975 if (powerdown_requested)
1976 return 0;
1977 if (reset_requested)
1978 return 0;
1979 if (shutdown_requested)
1980 return 0;
1981 if (debug_requested)
1982 return 0;
1983 return 1;
1984}
1985
1986static void main_loop(void)
1987{
1988 int r;
1989
1990#ifdef CONFIG_IOTHREAD
1991 qemu_system_ready = 1;
1992 qemu_cond_broadcast(&qemu_system_cond);
1993#endif
1994
Jun Nakajimaa381ef02011-12-17 19:13:25 -08001995#ifdef CONFIG_HAX
1996 if (hax_enabled())
1997 hax_sync_vcpus();
1998#endif
1999
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002000 for (;;) {
2001 do {
2002#ifdef CONFIG_PROFILER
2003 int64_t ti;
2004#endif
2005#ifndef CONFIG_IOTHREAD
2006 tcg_cpu_exec();
2007#endif
2008#ifdef CONFIG_PROFILER
2009 ti = profile_getclock();
2010#endif
2011 main_loop_wait(qemu_calculate_timeout());
2012#ifdef CONFIG_PROFILER
2013 dev_time += profile_getclock() - ti;
2014#endif
rich canningsd952f282011-03-01 15:40:09 -08002015
2016 if (rotate_logs_requested) {
2017 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
2018 dns_log_filename);
2019 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
2020 drop_log_filename);
2021 slirp_dns_log_fd(new_dns_log_fd);
2022 slirp_drop_log_fd(new_drop_log_fd);
2023 reset_rotate_qemu_logs_request();
2024 }
2025
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002026 } while (vm_can_run());
2027
2028 if (qemu_debug_requested())
2029 vm_stop(EXCP_DEBUG);
2030 if (qemu_shutdown_requested()) {
2031 if (no_shutdown) {
2032 vm_stop(0);
2033 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002034 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00002035 if (savevm_on_exit != NULL) {
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08002036 /* Prior to saving VM to the snapshot file, save HW config
2037 * settings for that VM, so we can match them when VM gets
2038 * loaded from the snapshot. */
2039 snaphost_save_config(savevm_on_exit);
Tim Baverstock24204cc2010-11-25 11:37:43 +00002040 do_savevm(cur_mon, savevm_on_exit);
2041 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002042 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002043 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002044 }
2045 if (qemu_reset_requested()) {
2046 pause_all_vcpus();
2047 qemu_system_reset();
2048 resume_all_vcpus();
2049 }
2050 if (qemu_powerdown_requested())
2051 qemu_system_powerdown();
2052 if ((r = qemu_vmstop_requested()))
2053 vm_stop(r);
2054 }
2055 pause_all_vcpus();
2056}
2057
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002058void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002059{
2060 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2061}
2062
2063void qemu_help(int exitcode)
2064{
2065 version();
2066 printf("usage: %s [options] [disk_image]\n"
2067 "\n"
2068 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2069 "\n"
2070#define DEF(option, opt_arg, opt_enum, opt_help) \
2071 opt_help
2072#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002073#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002074#undef DEF
2075#undef DEFHEADING
2076#undef GEN_DOCS
2077 "\n"
2078 "During emulation, the following keys are useful:\n"
2079 "ctrl-alt-f toggle full screen\n"
2080 "ctrl-alt-n switch to virtual console 'n'\n"
2081 "ctrl-alt toggle mouse and keyboard grab\n"
2082 "\n"
2083 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2084 ,
2085 "qemu",
2086 DEFAULT_RAM_SIZE,
2087#ifndef _WIN32
2088 DEFAULT_NETWORK_SCRIPT,
2089 DEFAULT_NETWORK_DOWN_SCRIPT,
2090#endif
2091 DEFAULT_GDBSTUB_PORT,
2092 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002093 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002094}
2095
2096#define HAS_ARG 0x0001
2097
2098enum {
2099#define DEF(option, opt_arg, opt_enum, opt_help) \
2100 opt_enum,
2101#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002102#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002103#undef DEF
2104#undef DEFHEADING
2105#undef GEN_DOCS
2106};
2107
2108typedef struct QEMUOption {
2109 const char *name;
2110 int flags;
2111 int index;
2112} QEMUOption;
2113
2114static const QEMUOption qemu_options[] = {
2115 { "h", 0, QEMU_OPTION_h },
2116#define DEF(option, opt_arg, opt_enum, opt_help) \
2117 { option, opt_arg, opt_enum },
2118#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002119#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002120#undef DEF
2121#undef DEFHEADING
2122#undef GEN_DOCS
2123 { NULL, 0, 0 },
2124};
2125
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002126static void select_vgahw (const char *p)
2127{
2128 const char *opts;
2129
2130 cirrus_vga_enabled = 0;
2131 std_vga_enabled = 0;
2132 vmsvga_enabled = 0;
2133 xenfb_enabled = 0;
2134 if (strstart(p, "std", &opts)) {
2135 std_vga_enabled = 1;
2136 } else if (strstart(p, "cirrus", &opts)) {
2137 cirrus_vga_enabled = 1;
2138 } else if (strstart(p, "vmware", &opts)) {
2139 vmsvga_enabled = 1;
2140 } else if (strstart(p, "xenfb", &opts)) {
2141 xenfb_enabled = 1;
2142 } else if (!strstart(p, "none", &opts)) {
2143 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002144 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002145 }
2146 while (*opts) {
2147 const char *nextopt;
2148
2149 if (strstart(opts, ",retrace=", &nextopt)) {
2150 opts = nextopt;
2151 if (strstart(opts, "dumb", &nextopt))
2152 vga_retrace_method = VGA_RETRACE_DUMB;
2153 else if (strstart(opts, "precise", &nextopt))
2154 vga_retrace_method = VGA_RETRACE_PRECISE;
2155 else goto invalid_vga;
2156 } else goto invalid_vga;
2157 opts = nextopt;
2158 }
2159}
2160
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002161#define MAX_NET_CLIENTS 32
2162
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002163#ifdef _WIN32
2164/* Look for support files in the same directory as the executable. */
2165static char *find_datadir(const char *argv0)
2166{
2167 char *p;
2168 char buf[MAX_PATH];
2169 DWORD len;
2170
2171 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2172 if (len == 0) {
2173 return NULL;
2174 }
2175
2176 buf[len] = 0;
2177 p = buf + len - 1;
2178 while (p != buf && *p != '\\')
2179 p--;
2180 *p = 0;
2181 if (access(buf, R_OK) == 0) {
2182 return qemu_strdup(buf);
2183 }
2184 return NULL;
2185}
2186#else /* !_WIN32 */
2187
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002188/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002189static char *find_datadir(const char *argv0)
2190{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002191 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002192 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002193
2194#if defined(__linux__)
2195 {
2196 int len;
2197 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2198 if (len > 0) {
2199 buf[len] = 0;
2200 p = buf;
2201 }
2202 }
2203#elif defined(__FreeBSD__)
2204 {
2205 int len;
2206 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2207 if (len > 0) {
2208 buf[len] = 0;
2209 p = buf;
2210 }
2211 }
2212#endif
2213 /* If we don't have any way of figuring out the actual executable
2214 location then try argv[0]. */
2215 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002216 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002217 if (!p) {
2218 return NULL;
2219 }
2220 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002221
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002222 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002223}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002224#endif
2225
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002226static char*
2227qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
2228{
2229 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2230 char* buf = qemu_mallocz(len);
2231
2232 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2233 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2234 if (access(buf, R_OK)) {
2235 qemu_free(buf);
2236 return NULL;
2237 }
2238 return buf;
2239}
2240
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002241char *qemu_find_file(int type, const char *name)
2242{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002243 const char *subdir;
2244 char *buf;
2245
2246 /* If name contains path separators then try it as a straight path. */
2247 if ((strchr(name, '/') || strchr(name, '\\'))
2248 && access(name, R_OK) == 0) {
2249 return strdup(name);
2250 }
2251 switch (type) {
2252 case QEMU_FILE_TYPE_BIOS:
2253 subdir = "";
2254 break;
2255 case QEMU_FILE_TYPE_KEYMAP:
2256 subdir = "keymaps/";
2257 break;
2258 default:
2259 abort();
2260 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002261 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2262#ifdef CONFIG_ANDROID
2263 if (type == QEMU_FILE_TYPE_BIOS) {
2264 /* This case corresponds to the emulator being used as part of an
2265 * SDK installation. NOTE: data_dir is really $bindir. */
2266 if (buf == NULL)
2267 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2268 /* This case corresponds to platform builds. */
2269 if (buf == NULL)
2270 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2271 /* Finally, try this for standalone builds under external/qemu */
2272 if (buf == NULL)
2273 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002274 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002275#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002276 return buf;
2277}
2278
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002279static int
2280add_dns_server( const char* server_name )
2281{
2282 SockAddress addr;
2283
2284 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2285 fprintf(stdout,
2286 "### WARNING: can't resolve DNS server name '%s'\n",
2287 server_name );
2288 return -1;
2289 }
2290
2291 fprintf(stderr,
2292 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2293
2294 if ( slirp_add_dns_server( &addr ) < 0 ) {
2295 fprintf(stderr,
2296 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2297 return -1;
2298 }
2299 return 0;
2300}
2301
rich cannings7339b552011-02-16 13:43:44 -08002302/* Parses an integer
2303 * Pararm:
2304 * str String containing a number to be parsed.
2305 * result Passes the parsed integer in this argument
2306 * returns 0 if ok, -1 if failed
2307 */
2308int
2309parse_int(const char *str, int *result)
2310{
2311 char* r;
2312 *result = strtol(str, &r, 0);
2313 if (r == NULL || *r != '\0')
2314 return -1;
2315
2316 return 0;
2317}
2318
rich canningsd952f282011-03-01 15:40:09 -08002319#ifndef _WIN32
2320/*
2321 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
2322 */
2323void init_qemu_clear_logs_sig() {
2324 struct sigaction act;
2325 sigfillset(&act.sa_mask);
2326 act.sa_flags = 0;
2327 act.sa_handler = rotate_qemu_logs_handler;
2328 if (sigaction(SIGUSR1, &act, NULL) == -1) {
2329 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
2330 exit(-1);
2331 }
2332}
2333#endif
2334
2335
rich cannings7339b552011-02-16 13:43:44 -08002336
2337/* parses a null-terminated string specifying a network port (e.g., "80") or
2338 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2339 * are the same. Returns 0 on success, -1 on error. */
2340
2341int parse_port_range(const char *str, unsigned short *lport,
2342 unsigned short *hport) {
2343
2344 unsigned int low = 0, high = 0;
2345 char *p, *arg = strdup(str);
2346
2347 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2348 p = arg + 1; /* skip '[' */
2349 low = atoi(strtok(p, "-"));
2350 high = atoi(strtok(NULL, "-"));
2351 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2352 *lport = low;
2353 *hport = high;
2354 }
2355 }
2356 else {
2357 low = atoi(arg);
2358 if ((0 < low) && (low < 65535)) {
2359 *lport = low;
2360 *hport = low;
2361 }
2362 }
2363 free(arg);
2364 if (low != 0)
2365 return 0;
2366 return -1;
2367}
2368
2369/*
2370 * Implements the generic port forwarding option
2371 */
2372void
2373net_slirp_forward(const char *optarg)
2374{
2375 /*
2376 * we expect the following format:
2377 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2378 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2379 */
2380 char *argument = strdup(optarg), *p = argument;
2381 char *dst_net, *dst_mask, *dst_port;
2382 char *redirect_ip, *redirect_port;
2383 uint32_t dnet, dmask, rip;
2384 unsigned short dlport, dhport, rport;
2385
2386
2387 dst_net = strtok(p, ":");
2388 dst_mask = strtok(NULL, ":");
2389 dst_port = strtok(NULL, ":");
2390 redirect_ip = strtok(NULL, ":");
2391 redirect_port = strtok(NULL, ":");
2392
2393 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2394 redirect_ip == NULL || redirect_port == NULL) {
2395 fprintf(stderr,
2396 "Invalid argument for -net-forward, we expect "
2397 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2398 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2399 ":redirect_ip:redirect_port: %s\n",
2400 optarg);
2401 exit(1);
2402 }
2403
2404 /* inet_strtoip converts dotted address to host byte order */
2405 if (inet_strtoip(dst_net, &dnet) == -1) {
2406 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2407 exit(1);
2408 }
2409 if (inet_strtoip(dst_mask, &dmask) == -1) {
2410 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2411 exit(1);
2412 }
2413 if (inet_strtoip(redirect_ip, &rip) == -1) {
2414 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2415 exit(1);
2416 }
2417
2418 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2419 fprintf(stderr, "Invalid destination port or port range\n");
2420 exit(1);
2421 }
2422
2423 rport = atoi(redirect_port);
2424 if (!rport) {
2425 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2426 exit(1);
2427 }
2428
2429 dnet &= dmask;
2430
2431 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2432 rip, rport);
2433
2434 free(argument);
2435}
2436
2437
2438/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2439 * entry in the allows list */
2440void
2441slirp_allow(const char *optarg, u_int8_t proto)
2442{
2443 /*
2444 * we expect the following format:
2445 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2446 */
2447 char *argument = strdup(optarg), *p = argument;
2448 char *dst_ip_str, *dst_port_str;
2449 uint32_t dst_ip;
2450 unsigned short dst_lport, dst_hport;
2451
2452 dst_ip_str = strtok(p, ":");
2453 dst_port_str = strtok(NULL, ":");
2454
2455 if (dst_ip_str == NULL || dst_port_str == NULL) {
2456 fprintf(stderr,
2457 "Invalid argument %s for -allow. We expect "
2458 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2459 optarg);
2460 exit(1);
2461 }
2462
2463 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2464 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2465 exit(1);
2466 }
2467 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2468 fprintf(stderr, "Invalid destination port or port range\n");
2469 exit(1);
2470 }
2471
2472 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2473
2474 free(argument);
2475}
2476
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002477/* Add a serial device at a given location in the emulated hardware table.
2478 * On failure, this function aborts the program with an error message.
2479 */
2480static void
2481serial_hds_add_at(int index, const char* devname)
2482{
2483 char label[32];
2484
2485 if (!devname || !strcmp(devname,"none"))
2486 return;
2487
2488 if (index >= MAX_SERIAL_PORTS) {
2489 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2490 devname, index, MAX_SERIAL_PORTS);
2491 }
2492 if (serial_hds[index] != NULL) {
2493 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2494 devname, index);
2495 }
2496 snprintf(label, sizeof(label), "serial%d", index);
2497 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2498 if (!serial_hds[index]) {
2499 PANIC("qemu: could not open serial device '%s'", devname);
2500 }
2501}
2502
2503
2504/* Find a free slot in the emulated serial device table, and register
2505 * it. Return the allocated table index.
2506 */
2507static int
2508serial_hds_add(const char* devname)
2509{
2510 int index;
2511
2512 /* Find first free slot */
2513 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2514 if (serial_hds[index] == NULL) {
2515 serial_hds_add_at(index, devname);
2516 return index;
2517 }
2518 }
2519
2520 PANIC("qemu: too many serial devices registered (%d)", index);
2521 return -1; /* shouldn't happen */
2522}
2523
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002524int main(int argc, char **argv, char **envp)
2525{
2526 const char *gdbstub_dev = NULL;
2527 uint32_t boot_devices_bitmap = 0;
2528 int i;
2529 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002530 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002531 const char *initrd_filename;
2532 const char *kernel_filename, *kernel_cmdline;
2533 const char *boot_devices = "";
2534 DisplayState *ds;
2535 DisplayChangeListener *dcl;
2536 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002537 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002538 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002539 const char *net_clients[MAX_NET_CLIENTS];
2540 int nb_net_clients;
2541 const char *bt_opts[MAX_BT_CMDLINE];
2542 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002543 int optind;
2544 const char *r, *optarg;
2545 CharDriverState *monitor_hd = NULL;
2546 const char *monitor_device;
2547 const char *serial_devices[MAX_SERIAL_PORTS];
2548 int serial_device_index;
2549 const char *parallel_devices[MAX_PARALLEL_PORTS];
2550 int parallel_device_index;
2551 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2552 int virtio_console_index;
2553 const char *loadvm = NULL;
2554 QEMUMachine *machine;
2555 const char *cpu_model;
2556 const char *usb_devices[MAX_USB_CMDLINE];
2557 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002558 int tb_size;
2559 const char *pid_file = NULL;
2560 const char *incoming = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002561 CPUState *env;
2562 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002563 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002564 STRALLOC_DEFINE(kernel_params);
2565 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002566 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002567
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002568 /* Initialize sockets before anything else, so we can properly report
2569 * initialization failures back to the UI. */
2570#ifdef _WIN32
2571 socket_init();
2572#endif
2573
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002574 init_clocks();
2575
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002576 qemu_cache_utils_init(envp);
2577
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002578 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002579 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002580
2581 module_call_init(MODULE_INIT_MACHINE);
2582 machine = find_default_machine();
2583 cpu_model = NULL;
2584 initrd_filename = NULL;
2585 ram_size = 0;
2586 snapshot = 0;
2587 kernel_filename = NULL;
2588 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002589
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002590 cyls = heads = secs = 0;
2591 translation = BIOS_ATA_TRANSLATION_AUTO;
2592 monitor_device = "vc:80Cx24C";
2593
2594 serial_devices[0] = "vc:80Cx24C";
2595 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2596 serial_devices[i] = NULL;
2597 serial_device_index = 0;
2598
2599 parallel_devices[0] = "vc:80Cx24C";
2600 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2601 parallel_devices[i] = NULL;
2602 parallel_device_index = 0;
2603
2604 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2605 virtio_consoles[i] = NULL;
2606 virtio_console_index = 0;
2607
2608 for (i = 0; i < MAX_NODES; i++) {
2609 node_mem[i] = 0;
2610 node_cpumask[i] = 0;
2611 }
2612
2613 usb_devices_index = 0;
2614
2615 nb_net_clients = 0;
2616 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002617#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002618 nb_drives = 0;
2619 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002620#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002621 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002622
2623 nb_nics = 0;
2624
2625 tb_size = 0;
2626 autostart= 1;
2627
2628 register_watchdogs();
2629
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002630 /* Initialize boot properties. */
2631 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002632 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002633 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002634
David 'Digit' Turner36597752011-05-20 01:18:01 +02002635#ifdef CONFIG_KVM
2636 /* By default, force auto-detection for kvm */
2637 kvm_allowed = -1;
2638#endif
2639
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002640 optind = 1;
2641 for(;;) {
2642 if (optind >= argc)
2643 break;
2644 r = argv[optind];
2645 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002646 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002647 } else {
2648 const QEMUOption *popt;
2649
2650 optind++;
2651 /* Treat --foo the same as -foo. */
2652 if (r[1] == '-')
2653 r++;
2654 popt = qemu_options;
2655 for(;;) {
2656 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002657 PANIC("%s: invalid option -- '%s'",
2658 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002659 }
2660 if (!strcmp(popt->name, r + 1))
2661 break;
2662 popt++;
2663 }
2664 if (popt->flags & HAS_ARG) {
2665 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002666 PANIC("%s: option '%s' requires an argument",
2667 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002668 }
2669 optarg = argv[optind++];
2670 } else {
2671 optarg = NULL;
2672 }
2673
2674 switch(popt->index) {
2675 case QEMU_OPTION_M:
2676 machine = find_machine(optarg);
2677 if (!machine) {
2678 QEMUMachine *m;
2679 printf("Supported machines are:\n");
2680 for(m = first_machine; m != NULL; m = m->next) {
2681 printf("%-10s %s%s\n",
2682 m->name, m->desc,
2683 m->is_default ? " (default)" : "");
2684 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002685 if (*optarg != '?') {
2686 PANIC("Invalid machine parameter: %s",
2687 optarg);
2688 } else {
2689 QEMU_EXIT(0);
2690 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002691 }
2692 break;
2693 case QEMU_OPTION_cpu:
2694 /* hw initialization will check this */
2695 if (*optarg == '?') {
2696/* XXX: implement xxx_cpu_list for targets that still miss it */
2697#if defined(cpu_list)
2698 cpu_list(stdout, &fprintf);
2699#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002700 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002701 } else {
2702 cpu_model = optarg;
2703 }
2704 break;
2705 case QEMU_OPTION_initrd:
2706 initrd_filename = optarg;
2707 break;
2708 case QEMU_OPTION_hda:
2709 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002710 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002711 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002712 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002713 ",cyls=%d,heads=%d,secs=%d%s",
2714 0, cyls, heads, secs,
2715 translation == BIOS_ATA_TRANSLATION_LBA ?
2716 ",trans=lba" :
2717 translation == BIOS_ATA_TRANSLATION_NONE ?
2718 ",trans=none" : "");
2719 break;
2720 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002721 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2722 break;
2723
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002724 case QEMU_OPTION_hdc:
2725 case QEMU_OPTION_hdd:
2726 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2727 break;
2728 case QEMU_OPTION_drive:
2729 drive_add(NULL, "%s", optarg);
2730 break;
2731 case QEMU_OPTION_mtdblock:
2732 drive_add(optarg, MTD_ALIAS);
2733 break;
2734 case QEMU_OPTION_sd:
2735 drive_add(optarg, SD_ALIAS);
2736 break;
2737 case QEMU_OPTION_pflash:
2738 drive_add(optarg, PFLASH_ALIAS);
2739 break;
2740 case QEMU_OPTION_snapshot:
2741 snapshot = 1;
2742 break;
2743 case QEMU_OPTION_hdachs:
2744 {
2745 const char *p;
2746 p = optarg;
2747 cyls = strtol(p, (char **)&p, 0);
2748 if (cyls < 1 || cyls > 16383)
2749 goto chs_fail;
2750 if (*p != ',')
2751 goto chs_fail;
2752 p++;
2753 heads = strtol(p, (char **)&p, 0);
2754 if (heads < 1 || heads > 16)
2755 goto chs_fail;
2756 if (*p != ',')
2757 goto chs_fail;
2758 p++;
2759 secs = strtol(p, (char **)&p, 0);
2760 if (secs < 1 || secs > 63)
2761 goto chs_fail;
2762 if (*p == ',') {
2763 p++;
2764 if (!strcmp(p, "none"))
2765 translation = BIOS_ATA_TRANSLATION_NONE;
2766 else if (!strcmp(p, "lba"))
2767 translation = BIOS_ATA_TRANSLATION_LBA;
2768 else if (!strcmp(p, "auto"))
2769 translation = BIOS_ATA_TRANSLATION_AUTO;
2770 else
2771 goto chs_fail;
2772 } else if (*p != '\0') {
2773 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002774 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002775 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002776 if (hda_opts != NULL) {
2777 char num[16];
2778 snprintf(num, sizeof(num), "%d", cyls);
2779 qemu_opt_set(hda_opts, "cyls", num);
2780 snprintf(num, sizeof(num), "%d", heads);
2781 qemu_opt_set(hda_opts, "heads", num);
2782 snprintf(num, sizeof(num), "%d", secs);
2783 qemu_opt_set(hda_opts, "secs", num);
2784 if (translation == BIOS_ATA_TRANSLATION_LBA)
2785 qemu_opt_set(hda_opts, "trans", "lba");
2786 if (translation == BIOS_ATA_TRANSLATION_NONE)
2787 qemu_opt_set(hda_opts, "trans", "none");
2788 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002789 }
2790 break;
2791 case QEMU_OPTION_numa:
2792 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002793 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002794 }
2795 numa_add(optarg);
2796 break;
2797 case QEMU_OPTION_nographic:
2798 display_type = DT_NOGRAPHIC;
2799 break;
2800#ifdef CONFIG_CURSES
2801 case QEMU_OPTION_curses:
2802 display_type = DT_CURSES;
2803 break;
2804#endif
2805 case QEMU_OPTION_portrait:
2806 graphic_rotate = 1;
2807 break;
2808 case QEMU_OPTION_kernel:
2809 kernel_filename = optarg;
2810 break;
2811 case QEMU_OPTION_append:
2812 kernel_cmdline = optarg;
2813 break;
2814 case QEMU_OPTION_cdrom:
2815 drive_add(optarg, CDROM_ALIAS);
2816 break;
2817 case QEMU_OPTION_boot:
2818 boot_devices = optarg;
2819 /* We just do some generic consistency checks */
2820 {
2821 /* Could easily be extended to 64 devices if needed */
2822 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002823
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002824 boot_devices_bitmap = 0;
2825 for (p = boot_devices; *p != '\0'; p++) {
2826 /* Allowed boot devices are:
2827 * a b : floppy disk drives
2828 * c ... f : IDE disk drives
2829 * g ... m : machine implementation dependant drives
2830 * n ... p : network devices
2831 * It's up to each machine implementation to check
2832 * if the given boot devices match the actual hardware
2833 * implementation and firmware features.
2834 */
2835 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002836 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002837 }
2838 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002839 PANIC(
2840 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002841 }
2842 boot_devices_bitmap |= 1 << (*p - 'a');
2843 }
2844 }
2845 break;
2846 case QEMU_OPTION_fda:
2847 case QEMU_OPTION_fdb:
2848 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2849 break;
2850#ifdef TARGET_I386
2851 case QEMU_OPTION_no_fd_bootchk:
2852 fd_bootchk = 0;
2853 break;
2854#endif
2855 case QEMU_OPTION_net:
2856 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002857 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002858 }
2859 net_clients[nb_net_clients] = optarg;
2860 nb_net_clients++;
2861 break;
2862#ifdef CONFIG_SLIRP
2863 case QEMU_OPTION_tftp:
2864 tftp_prefix = optarg;
2865 break;
2866 case QEMU_OPTION_bootp:
2867 bootp_filename = optarg;
2868 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002869 case QEMU_OPTION_redir:
2870 net_slirp_redir(NULL, optarg, NULL);
2871 break;
2872#endif
2873 case QEMU_OPTION_bt:
2874 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002875 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002876 }
2877 bt_opts[nb_bt_opts++] = optarg;
2878 break;
2879#ifdef HAS_AUDIO
2880 case QEMU_OPTION_audio_help:
2881 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002882 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002883 break;
2884 case QEMU_OPTION_soundhw:
2885 select_soundhw (optarg);
2886 break;
2887#endif
2888 case QEMU_OPTION_h:
2889 qemu_help(0);
2890 break;
2891 case QEMU_OPTION_version:
2892 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002893 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002894 break;
2895 case QEMU_OPTION_m: {
2896 uint64_t value;
2897 char *ptr;
2898
2899 value = strtoul(optarg, &ptr, 10);
2900 switch (*ptr) {
2901 case 0: case 'M': case 'm':
2902 value <<= 20;
2903 break;
2904 case 'G': case 'g':
2905 value <<= 30;
2906 break;
2907 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002908 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002909 }
2910
2911 /* On 32-bit hosts, QEMU is limited by virtual address space */
2912 if (value > (2047 << 20)
2913#ifndef CONFIG_KQEMU
2914 && HOST_LONG_BITS == 32
2915#endif
2916 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002917 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002918 }
2919 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002920 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002921 }
2922 ram_size = value;
2923 break;
2924 }
2925 case QEMU_OPTION_d:
2926 {
2927 int mask;
2928 const CPULogItem *item;
2929
2930 mask = cpu_str_to_log_mask(optarg);
2931 if (!mask) {
2932 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002933 for(item = cpu_log_items; item->mask != 0; item++) {
2934 printf("%-10s %s\n", item->name, item->help);
2935 }
2936 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002937 }
2938 cpu_set_log(mask);
2939 }
2940 break;
2941 case QEMU_OPTION_s:
2942 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2943 break;
2944 case QEMU_OPTION_gdb:
2945 gdbstub_dev = optarg;
2946 break;
2947 case QEMU_OPTION_L:
2948 data_dir = optarg;
2949 break;
2950 case QEMU_OPTION_bios:
2951 bios_name = optarg;
2952 break;
2953 case QEMU_OPTION_singlestep:
2954 singlestep = 1;
2955 break;
2956 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002957 autostart = 0;
2958 break;
2959#ifndef _WIN32
2960 case QEMU_OPTION_k:
2961 keyboard_layout = optarg;
2962 break;
2963#endif
2964 case QEMU_OPTION_localtime:
2965 rtc_utc = 0;
2966 break;
2967 case QEMU_OPTION_vga:
2968 select_vgahw (optarg);
2969 break;
2970#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2971 case QEMU_OPTION_g:
2972 {
2973 const char *p;
2974 int w, h, depth;
2975 p = optarg;
2976 w = strtol(p, (char **)&p, 10);
2977 if (w <= 0) {
2978 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002979 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002980 }
2981 if (*p != 'x')
2982 goto graphic_error;
2983 p++;
2984 h = strtol(p, (char **)&p, 10);
2985 if (h <= 0)
2986 goto graphic_error;
2987 if (*p == 'x') {
2988 p++;
2989 depth = strtol(p, (char **)&p, 10);
2990 if (depth != 8 && depth != 15 && depth != 16 &&
2991 depth != 24 && depth != 32)
2992 goto graphic_error;
2993 } else if (*p == '\0') {
2994 depth = graphic_depth;
2995 } else {
2996 goto graphic_error;
2997 }
2998
2999 graphic_width = w;
3000 graphic_height = h;
3001 graphic_depth = depth;
3002 }
3003 break;
3004#endif
3005 case QEMU_OPTION_echr:
3006 {
3007 char *r;
3008 term_escape_char = strtol(optarg, &r, 0);
3009 if (r == optarg)
3010 printf("Bad argument to echr\n");
3011 break;
3012 }
3013 case QEMU_OPTION_monitor:
3014 monitor_device = optarg;
3015 break;
3016 case QEMU_OPTION_serial:
3017 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003018 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003019 }
3020 serial_devices[serial_device_index] = optarg;
3021 serial_device_index++;
3022 break;
3023 case QEMU_OPTION_watchdog:
3024 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003025 if (i > 0) {
3026 if (i == 1) {
3027 PANIC("Invalid watchdog parameter: %s",
3028 optarg);
3029 } else {
3030 QEMU_EXIT(0);
3031 }
3032 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003033 break;
3034 case QEMU_OPTION_watchdog_action:
3035 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003036 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003037 }
3038 break;
3039 case QEMU_OPTION_virtiocon:
3040 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003041 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003042 }
3043 virtio_consoles[virtio_console_index] = optarg;
3044 virtio_console_index++;
3045 break;
3046 case QEMU_OPTION_parallel:
3047 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003048 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003049 }
3050 parallel_devices[parallel_device_index] = optarg;
3051 parallel_device_index++;
3052 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003053 case QEMU_OPTION_loadvm:
3054 loadvm = optarg;
3055 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003056 case QEMU_OPTION_savevm_on_exit:
3057 savevm_on_exit = optarg;
3058 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003059 case QEMU_OPTION_full_screen:
3060 full_screen = 1;
3061 break;
3062#ifdef CONFIG_SDL
3063 case QEMU_OPTION_no_frame:
3064 no_frame = 1;
3065 break;
3066 case QEMU_OPTION_alt_grab:
3067 alt_grab = 1;
3068 break;
3069 case QEMU_OPTION_no_quit:
3070 no_quit = 1;
3071 break;
3072 case QEMU_OPTION_sdl:
3073 display_type = DT_SDL;
3074 break;
3075#endif
3076 case QEMU_OPTION_pidfile:
3077 pid_file = optarg;
3078 break;
3079#ifdef TARGET_I386
3080 case QEMU_OPTION_win2k_hack:
3081 win2k_install_hack = 1;
3082 break;
3083 case QEMU_OPTION_rtc_td_hack:
3084 rtc_td_hack = 1;
3085 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003086#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003087 case QEMU_OPTION_acpitable:
3088 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003089 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003090 }
3091 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003092#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003093 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02003094 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003095 break;
3096#endif
3097#ifdef CONFIG_KVM
3098 case QEMU_OPTION_enable_kvm:
3099 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003100 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02003101 case QEMU_OPTION_disable_kvm:
3102 kvm_allowed = 0;
3103 break;
3104#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003105 case QEMU_OPTION_usb:
3106 usb_enabled = 1;
3107 break;
3108 case QEMU_OPTION_usbdevice:
3109 usb_enabled = 1;
3110 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003111 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003112 }
3113 usb_devices[usb_devices_index] = optarg;
3114 usb_devices_index++;
3115 break;
3116 case QEMU_OPTION_smp:
3117 smp_cpus = atoi(optarg);
3118 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003119 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003120 }
3121 break;
3122 case QEMU_OPTION_vnc:
3123 display_type = DT_VNC;
3124 vnc_display = optarg;
3125 break;
3126#ifdef TARGET_I386
3127 case QEMU_OPTION_no_acpi:
3128 acpi_enabled = 0;
3129 break;
3130 case QEMU_OPTION_no_hpet:
3131 no_hpet = 1;
3132 break;
3133 case QEMU_OPTION_no_virtio_balloon:
3134 no_virtio_balloon = 1;
3135 break;
3136#endif
3137 case QEMU_OPTION_no_reboot:
3138 no_reboot = 1;
3139 break;
3140 case QEMU_OPTION_no_shutdown:
3141 no_shutdown = 1;
3142 break;
3143 case QEMU_OPTION_show_cursor:
3144 cursor_hide = 0;
3145 break;
3146 case QEMU_OPTION_uuid:
3147 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003148 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003149 }
3150 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003151 case QEMU_OPTION_option_rom:
3152 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003153 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003154 }
3155 option_rom[nb_option_roms] = optarg;
3156 nb_option_roms++;
3157 break;
3158#if defined(TARGET_ARM) || defined(TARGET_M68K)
3159 case QEMU_OPTION_semihosting:
3160 semihosting_enabled = 1;
3161 break;
3162#endif
3163 case QEMU_OPTION_name:
3164 qemu_name = optarg;
3165 break;
3166#if defined(TARGET_SPARC) || defined(TARGET_PPC)
3167 case QEMU_OPTION_prom_env:
3168 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003169 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003170 }
3171 prom_envs[nb_prom_envs] = optarg;
3172 nb_prom_envs++;
3173 break;
3174#endif
3175#ifdef TARGET_ARM
3176 case QEMU_OPTION_old_param:
3177 old_param = 1;
3178 break;
3179#endif
3180 case QEMU_OPTION_clock:
3181 configure_alarms(optarg);
3182 break;
3183 case QEMU_OPTION_startdate:
3184 {
3185 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02003186 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003187 if (!strcmp(optarg, "now")) {
3188 rtc_date_offset = -1;
3189 } else {
3190 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
3191 &tm.tm_year,
3192 &tm.tm_mon,
3193 &tm.tm_mday,
3194 &tm.tm_hour,
3195 &tm.tm_min,
3196 &tm.tm_sec) == 6) {
3197 /* OK */
3198 } else if (sscanf(optarg, "%d-%d-%d",
3199 &tm.tm_year,
3200 &tm.tm_mon,
3201 &tm.tm_mday) == 3) {
3202 tm.tm_hour = 0;
3203 tm.tm_min = 0;
3204 tm.tm_sec = 0;
3205 } else {
3206 goto date_fail;
3207 }
3208 tm.tm_year -= 1900;
3209 tm.tm_mon--;
3210 rtc_start_date = mktimegm(&tm);
3211 if (rtc_start_date == -1) {
3212 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003213 PANIC("Invalid date format. Valid format are:\n"
3214 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003215 }
3216 rtc_date_offset = time(NULL) - rtc_start_date;
3217 }
3218 }
3219 break;
rich cannings7339b552011-02-16 13:43:44 -08003220
3221 /* -------------------------------------------------------*/
3222 /* User mode network stack restrictions */
3223 case QEMU_OPTION_drop_udp:
3224 slirp_drop_udp();
3225 break;
3226 case QEMU_OPTION_drop_tcp:
3227 slirp_drop_tcp();
3228 break;
3229 case QEMU_OPTION_allow_tcp:
3230 slirp_allow(optarg, IPPROTO_TCP);
3231 break;
3232 case QEMU_OPTION_allow_udp:
3233 slirp_allow(optarg, IPPROTO_UDP);
3234 break;
3235 case QEMU_OPTION_drop_log:
3236 {
3237 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003238 drop_log_filename = optarg;
3239 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08003240
3241 if (!drop_log_fd) {
3242 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
3243 exit(1);
3244 }
3245
3246 slirp_drop_log_fd(drop_log_fd);
3247 }
3248 break;
3249
3250 case QEMU_OPTION_dns_log:
3251 {
3252 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003253 dns_log_filename = optarg;
3254 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08003255
3256 if (dns_log_fd == NULL) {
3257 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
3258 exit(1);
3259 }
3260
3261 slirp_dns_log_fd(dns_log_fd);
3262 }
3263 break;
3264
3265
3266 case QEMU_OPTION_max_dns_conns:
3267 {
3268 int max_dns_conns = 0;
3269 if (parse_int(optarg, &max_dns_conns)) {
3270 fprintf(stderr,
3271 "qemu: syntax: -max-dns-conns max_connections\n");
3272 exit(1);
3273 }
3274 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3275 fprintf(stderr,
3276 "Invalid arg for max dns connections: %s\n",
3277 optarg);
3278 exit(1);
3279 }
3280 slirp_set_max_dns_conns(max_dns_conns);
3281 }
3282 break;
3283
3284 case QEMU_OPTION_net_forward:
3285 net_slirp_forward(optarg);
3286 break;
3287 case QEMU_OPTION_net_forward_tcp2sink:
3288 {
3289 SockAddress saddr;
3290
3291 if (parse_host_port(&saddr, optarg)) {
3292 fprintf(stderr,
3293 "Invalid ip/port %s for "
3294 "-forward-dropped-tcp2sink. "
3295 "We expect 'sink_ip:sink_port'\n",
3296 optarg);
3297 exit(1);
3298 }
3299 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3300 saddr.u.inet.port);
3301 }
3302 break;
3303 /* -------------------------------------------------------*/
3304
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003305 case QEMU_OPTION_tb_size:
3306 tb_size = strtol(optarg, NULL, 0);
3307 if (tb_size < 0)
3308 tb_size = 0;
3309 break;
3310 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003311 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003312 break;
3313 case QEMU_OPTION_incoming:
3314 incoming = optarg;
3315 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003316#ifdef CONFIG_XEN
3317 case QEMU_OPTION_xen_domid:
3318 xen_domid = atoi(optarg);
3319 break;
3320 case QEMU_OPTION_xen_create:
3321 xen_mode = XEN_CREATE;
3322 break;
3323 case QEMU_OPTION_xen_attach:
3324 xen_mode = XEN_ATTACH;
3325 break;
3326#endif
3327
3328
3329 case QEMU_OPTION_mic:
3330 audio_input_source = (char*)optarg;
3331 break;
3332#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003333 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003334 trace_filename = optarg;
3335 tracing = 1;
3336 break;
3337#if 0
3338 case QEMU_OPTION_trace_miss:
3339 trace_cache_miss = 1;
3340 break;
3341 case QEMU_OPTION_trace_addr:
3342 trace_all_addr = 1;
3343 break;
3344#endif
3345 case QEMU_OPTION_tracing:
3346 if (strcmp(optarg, "off") == 0)
3347 tracing = 0;
3348 else if (strcmp(optarg, "on") == 0 && trace_filename)
3349 tracing = 1;
3350 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003351 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003352 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003353 }
3354 break;
3355#if 0
3356 case QEMU_OPTION_dcache_load_miss:
3357 dcache_load_miss_penalty = atoi(optarg);
3358 break;
3359 case QEMU_OPTION_dcache_store_miss:
3360 dcache_store_miss_penalty = atoi(optarg);
3361 break;
3362#endif
3363#endif
3364#ifdef CONFIG_NAND
3365 case QEMU_OPTION_nand:
3366 nand_add_dev(optarg);
3367 break;
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003368
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003369#endif
Jun Nakajimaa381ef02011-12-17 19:13:25 -08003370 case QEMU_OPTION_disable_hax:
3371 hax_disabled = 1;
3372 break;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003373 case QEMU_OPTION_android_ports:
3374 android_op_ports = (char*)optarg;
3375 break;
3376
3377 case QEMU_OPTION_android_port:
3378 android_op_port = (char*)optarg;
3379 break;
3380
3381 case QEMU_OPTION_android_report_console:
3382 android_op_report_console = (char*)optarg;
3383 break;
3384
3385 case QEMU_OPTION_http_proxy:
3386 op_http_proxy = (char*)optarg;
3387 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003388
3389 case QEMU_OPTION_charmap:
3390 op_charmap_file = (char*)optarg;
3391 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003392
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003393 case QEMU_OPTION_android_hw:
3394 android_op_hwini = (char*)optarg;
3395 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003396
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003397 case QEMU_OPTION_dns_server:
3398 android_op_dns_server = (char*)optarg;
3399 break;
3400
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003401 case QEMU_OPTION_radio:
3402 android_op_radio = (char*)optarg;
3403 break;
3404
3405 case QEMU_OPTION_gps:
3406 android_op_gps = (char*)optarg;
3407 break;
3408
3409 case QEMU_OPTION_audio:
3410 android_op_audio = (char*)optarg;
3411 break;
3412
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003413 case QEMU_OPTION_cpu_delay:
3414 android_op_cpu_delay = (char*)optarg;
3415 break;
3416
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003417 case QEMU_OPTION_show_kernel:
3418 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3419 break;
3420
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003421#ifdef CONFIG_NAND_LIMITS
3422 case QEMU_OPTION_nand_limits:
3423 android_op_nand_limits = (char*)optarg;
3424 break;
3425#endif // CONFIG_NAND_LIMITS
3426
3427 case QEMU_OPTION_netspeed:
3428 android_op_netspeed = (char*)optarg;
3429 break;
3430
3431 case QEMU_OPTION_netdelay:
3432 android_op_netdelay = (char*)optarg;
3433 break;
3434
3435 case QEMU_OPTION_netfast:
3436 android_op_netfast = 1;
3437 break;
3438
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003439 case QEMU_OPTION_tcpdump:
3440 android_op_tcpdump = (char*)optarg;
3441 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003442
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003443 case QEMU_OPTION_boot_property:
3444 boot_property_parse_option((char*)optarg);
3445 break;
3446
3447 case QEMU_OPTION_lcd_density:
3448 android_op_lcd_density = (char*)optarg;
3449 break;
3450
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003451 case QEMU_OPTION_ui_port:
3452 android_op_ui_port = (char*)optarg;
3453 break;
3454
3455 case QEMU_OPTION_ui_settings:
3456 android_op_ui_settings = (char*)optarg;
3457 break;
3458
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003459 case QEMU_OPTION_audio_test_out:
3460 android_audio_test_start_out();
3461 break;
3462
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003463 case QEMU_OPTION_android_avdname:
3464 android_op_avd_name = (char*)optarg;
3465 break;
3466
3467 case QEMU_OPTION_timezone:
3468 if (timezone_set((char*)optarg)) {
3469 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3470 (char*)optarg);
3471 }
3472 break;
3473
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003474#ifdef CONFIG_MEMCHECK
3475 case QEMU_OPTION_android_memcheck:
3476 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003477 /* This will set ro.kernel.memcheck system property
3478 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003479 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003480 break;
3481#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003482
3483 case QEMU_OPTION_snapshot_no_time_update:
3484 android_snapshot_update_time = 0;
3485 break;
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003486
3487 case QEMU_OPTION_list_webcam:
3488 android_list_web_cameras();
3489 exit(0);
3490
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003491 default:
3492 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003493 }
3494 }
3495 }
3496
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003497 /* Initialize character map. */
3498 if (android_charmap_setup(op_charmap_file)) {
3499 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003500 PANIC(
3501 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003502 op_charmap_file);
3503 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003504 PANIC(
3505 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003506 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003507 }
3508
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003509 /* If no data_dir is specified then try to find it relative to the
3510 executable path. */
3511 if (!data_dir) {
3512 data_dir = find_datadir(argv[0]);
3513 }
3514 /* If all else fails use the install patch specified when building. */
3515 if (!data_dir) {
3516 data_dir = CONFIG_QEMU_SHAREDIR;
3517 }
3518
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003519 if (!android_op_hwini) {
3520 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003521 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003522 hw_ini = iniFile_newFromFile(android_op_hwini);
3523 if (hw_ini == NULL) {
3524 PANIC("Could not find %s file.", android_op_hwini);
3525 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003526
3527 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003528 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003529
Vladimir Chtchetkinedb450d72012-01-12 13:37:40 -08003530 /* If we're loading VM from a snapshot, make sure that the current HW config
3531 * matches the one with which the VM has been saved. */
3532 if (loadvm && *loadvm && !snaphost_match_configs(hw_ini, loadvm)) {
3533 exit(0);
3534 }
3535
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003536 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003537
3538 {
3539 int width = android_hw->hw_lcd_width;
3540 int height = android_hw->hw_lcd_height;
3541 int depth = android_hw->hw_lcd_depth;
3542
3543 /* A bit of sanity checking */
3544 if (width <= 0 || height <= 0 ||
3545 (depth != 16 && depth != 32) ||
3546 (((width|height) & 3) != 0) )
3547 {
3548 PANIC("Invalid display configuration (%d,%d,%d)",
3549 width, height, depth);
3550 }
3551 android_display_width = width;
3552 android_display_height = height;
3553 android_display_bpp = depth;
3554 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003555
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003556#ifdef CONFIG_NAND_LIMITS
3557 /* Init nand stuff. */
3558 if (android_op_nand_limits) {
3559 parse_nand_limits(android_op_nand_limits);
3560 }
3561#endif // CONFIG_NAND_LIMITS
3562
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003563 /* Initialize AVD name from hardware configuration if needed */
3564 if (!android_op_avd_name) {
3565 if (android_hw->avd_name && *android_hw->avd_name) {
3566 android_op_avd_name = android_hw->avd_name;
3567 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3568 }
3569 }
3570
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003571 /* Initialize system partition image */
3572 {
3573 char tmp[PATH_MAX+32];
3574 const char* sysImage = android_hw->disk_systemPartition_path;
3575 const char* initImage = android_hw->disk_systemPartition_initPath;
3576 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3577
3578 if (sysBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003579 PANIC("Invalid system partition size: %" PRIu64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003580 }
3581
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003582 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRIx64, sysBytes);
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003583
3584 if (sysImage && *sysImage) {
3585 if (filelock_create(sysImage) == NULL) {
3586 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3587 /* If there is no file= parameters, nand_add_dev will create
3588 * a temporary file to back the partition image. */
3589 } else {
3590 pstrcat(tmp,sizeof(tmp),",file=");
3591 pstrcat(tmp,sizeof(tmp),sysImage);
3592 }
3593 }
3594 if (initImage && *initImage) {
3595 if (!path_exists(initImage)) {
3596 PANIC("Invalid initial system image path: %s", initImage);
3597 }
3598 pstrcat(tmp,sizeof(tmp),",initfile=");
3599 pstrcat(tmp,sizeof(tmp),initImage);
3600 } else {
3601 PANIC("Missing initial system image path!");
3602 }
3603 nand_add_dev(tmp);
3604 }
3605
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003606 /* Initialize data partition image */
3607 {
3608 char tmp[PATH_MAX+32];
3609 const char* dataImage = android_hw->disk_dataPartition_path;
3610 const char* initImage = android_hw->disk_dataPartition_initPath;
3611 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3612
3613 if (dataBytes == 0) {
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003614 PANIC("Invalid data partition size: %" PRIu64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003615 }
3616
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003617 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRIx64, dataBytes);
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003618
3619 if (dataImage && *dataImage) {
3620 if (filelock_create(dataImage) == NULL) {
3621 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3622 /* Note: if there is no file= parameters, nand_add_dev() will
3623 * create a temporary file to back the partition image. */
3624 } else {
3625 /* Create the file if needed */
3626 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003627 if (path_empty_file(dataImage) < 0) {
3628 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3629 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003630 }
3631 pstrcat(tmp, sizeof(tmp), ",file=");
3632 pstrcat(tmp, sizeof(tmp), dataImage);
3633 }
3634 }
3635 if (initImage && *initImage) {
3636 pstrcat(tmp, sizeof(tmp), ",initfile=");
3637 pstrcat(tmp, sizeof(tmp), initImage);
3638 }
3639 nand_add_dev(tmp);
3640 }
3641
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003642 /* Init SD-Card stuff. For Android, it is always hda */
3643 /* If the -hda option was used, ignore the Android-provided one */
3644 if (hda_opts == NULL) {
3645 const char* sdPath = android_hw->hw_sdCard_path;
3646 if (sdPath && *sdPath) {
3647 if (!path_exists(sdPath)) {
3648 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3649 } else if (filelock_create(sdPath) == NULL) {
3650 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3651 } else {
3652 /* Successful locking */
3653 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003654 /* Set this property of any operation involving the SD Card
3655 * will be x100 slower, due to the corresponding file being
3656 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3657 * the context of an emulator crash. The data is already
3658 * synced properly when the emulator exits (either normally or through ^C).
3659 */
3660 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003661 }
3662 }
3663 }
3664
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003665 if (hdb_opts == NULL) {
3666 const char* spath = android_hw->disk_snapStorage_path;
3667 if (spath && *spath) {
3668 if (!path_exists(spath)) {
3669 PANIC("Snapshot storage file does not exist: %s", spath);
3670 }
3671 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003672 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003673 }
3674 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003675 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003676 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003677 }
3678 }
3679
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003680 /* Set the VM's max heap size, passed as a boot property */
3681 if (android_hw->vm_heapSize > 0) {
3682 char tmp[64];
3683 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3684 boot_property_add("dalvik.vm.heapsize",tmp);
3685 }
3686
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003687 /* Initialize net speed and delays stuff. */
3688 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003689 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003690 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003691 }
3692
3693 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003694 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003695 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003696 }
3697
3698 if (android_op_netfast) {
3699 qemu_net_download_speed = 0;
3700 qemu_net_upload_speed = 0;
3701 qemu_net_min_latency = 0;
3702 qemu_net_max_latency = 0;
3703 }
3704
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003705 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003706 if (android_hw->hw_lcd_density) {
3707 long density = android_hw->hw_lcd_density;
3708 if (density <= 0) {
3709 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003710 }
3711 hwLcd_setBootProperty(density);
3712 }
3713
Xavier Ducrohetfa0c8e22011-10-04 10:41:26 -07003714 /* Initialize presence of hardware nav button */
3715 boot_property_add("qemu.hw.mainkeys", android_hw->hw_mainKeys ? "1" : "0");
3716
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003717 /* Initialize TCP dump */
3718 if (android_op_tcpdump) {
3719 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3720 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3721 }
3722 }
3723
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003724 /* Initialize modem */
3725 if (android_op_radio) {
3726 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3727 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003728 PANIC("unsupported character device specification: %s\n"
3729 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003730 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003731 }
3732 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3733 } else if (android_hw->hw_gsmModem != 0 ) {
3734 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003735 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003736 }
3737 }
3738
3739 /* Initialize GPS */
3740 if (android_op_gps) {
3741 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3742 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003743 PANIC("unsupported character device specification: %s\n"
3744 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003745 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003746 }
3747 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3748 } else if (android_hw->hw_gps != 0) {
3749 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003750 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003751 }
3752 }
3753
3754 /* Initialize audio. */
3755 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003756 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003757 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003758 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003759 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003760 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003761 }
3762
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003763 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003764 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003765
Vladimir Chtchetkineae0d8132011-09-13 10:48:02 -07003766 /* Initialize fake camera */
3767 if (android_hw->hw_fakeCamera) {
3768 boot_property_add("qemu.sf.fake_camera", android_hw->hw_fakeCamera);
3769 } else {
3770 boot_property_add("qemu.sf.fake_camera", "back");
3771 }
3772
Vladimir Chtchetkineb8dcaff2011-09-17 11:15:47 -07003773 /* Initialize camera emulation. */
3774 android_camera_service_init();
3775
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003776 if (android_op_cpu_delay) {
3777 char* end;
3778 long delay = strtol(android_op_cpu_delay, &end, 0);
3779 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003780 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003781 }
3782 if (delay > 0)
3783 delay = (1000-delay);
3784
3785 qemu_cpu_delay = (int) delay;
3786 }
3787
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003788 if (android_op_dns_server) {
3789 char* x = strchr(android_op_dns_server, ',');
3790 dns_count = 0;
3791 if (x == NULL)
3792 {
3793 if ( add_dns_server( android_op_dns_server ) == 0 )
3794 dns_count = 1;
3795 }
3796 else
3797 {
3798 x = android_op_dns_server;
3799 while (*x) {
3800 char* y = strchr(x, ',');
3801
3802 if (y != NULL) {
3803 *y = 0;
3804 y++;
3805 } else {
3806 y = x + strlen(x);
3807 }
3808
3809 if (y > x && add_dns_server( x ) == 0) {
3810 dns_count += 1;
3811 }
3812 x = y;
3813 }
3814 }
3815 if (dns_count == 0)
3816 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3817 }
3818
3819 if (dns_count == 0)
3820 dns_count = slirp_get_system_dns_servers();
3821 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003822 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003823 }
3824
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003825#ifdef CONFIG_MEMCHECK
3826 if (android_op_memcheck) {
3827 memcheck_init(android_op_memcheck);
3828 }
3829#endif // CONFIG_MEMCHECK
3830
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003831 /* Initialize cache partition, if any */
3832 if (android_hw->disk_cachePartition != 0) {
3833 char tmp[PATH_MAX+32];
3834 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003835 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003836
Andrew Hsiehc7389bd2012-03-13 02:13:40 -07003837 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRIx64, partSize);
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003838
3839 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3840 if (filelock_create(partPath) == NULL) {
3841 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3842 /* Note: if there is no file= parameters, nand_add_dev() will
3843 * create a temporary file to back the partition image. */
3844 } else {
3845 /* Create the file if needed */
3846 if (!path_exists(partPath)) {
3847 if (path_empty_file(partPath) < 0) {
3848 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3849 }
3850 }
3851 pstrcat(tmp, sizeof(tmp), ",file=");
3852 pstrcat(tmp, sizeof(tmp), partPath);
3853 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003854 }
3855 nand_add_dev(tmp);
3856 }
3857
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003858 /* qemu.gles will be read by the OpenGLES emulation libraries.
3859 * If set to 0, the software GLES renderer will be used as a fallback.
3860 * If the parameter is undefined, this means the system image runs
3861 * inside an emulator that doesn't support GPU emulation at all.
3862 */
3863 {
3864 int gles_emul = 0;
3865
3866 if (android_hw->hw_gpu_enabled) {
3867 if (android_initOpenglesEmulation() == 0) {
3868 gles_emul = 1;
3869 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height);
3870 } else {
3871 dwarning("Could not initialize OpenglES emulation, using software renderer.");
3872 }
3873 }
3874 if (gles_emul) {
3875 stralloc_add_str(kernel_params, " qemu.gles=1");
3876 } else {
3877 stralloc_add_str(kernel_params, " qemu.gles=0");
3878 }
3879 }
3880
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003881 /* We always force qemu=1 when running inside QEMU */
3882 stralloc_add_str(kernel_params, " qemu=1");
3883
3884 /* We always initialize the first serial port for the android-kmsg
3885 * character device (used to send kernel messages) */
3886 serial_hds_add_at(0, "android-kmsg");
3887 stralloc_add_str(kernel_params, " console=ttyS0");
3888
3889 /* We always initialize the second serial port for the android-qemud
3890 * character device as well */
3891 serial_hds_add_at(1, "android-qemud");
3892 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3893
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003894 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3895 os_pidfile_error();
3896 exit(1);
3897 }
3898
David 'Digit' Turner36597752011-05-20 01:18:01 +02003899#if defined(CONFIG_KVM)
3900 if (kvm_allowed < 0) {
3901 kvm_allowed = kvm_check_allowed();
3902 }
3903#endif
3904
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003905#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
3906 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003907 PANIC(
3908 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003909 }
3910#endif
3911
3912 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3913 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003914 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3915 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003916 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003917 }
3918
3919 if (display_type == DT_NOGRAPHIC) {
3920 if (serial_device_index == 0)
3921 serial_devices[0] = "stdio";
3922 if (parallel_device_index == 0)
3923 parallel_devices[0] = "null";
3924 if (strncmp(monitor_device, "vc", 2) == 0)
3925 monitor_device = "stdio";
3926 }
3927
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003928#ifdef CONFIG_KQEMU
3929 if (smp_cpus > 1)
3930 kqemu_allowed = 0;
3931#endif
3932 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003933 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003934 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003935
3936 if (kernel_filename == NULL) {
3937 kernel_filename = android_hw->kernel_path;
3938 }
3939 if (initrd_filename == NULL) {
3940 initrd_filename = android_hw->disk_ramdisk_path;
3941 }
3942
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003943 linux_boot = (kernel_filename != NULL);
3944 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3945
3946 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003947 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003948 }
3949
3950 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003951 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003952 }
3953
3954 /* boot to floppy or the default cd if no hard disk defined yet */
3955 if (!boot_devices[0]) {
3956 boot_devices = "cad";
3957 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003958 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003959
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003960 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003961 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003962 }
David Turner6a9ef172010-09-09 22:54:36 +02003963 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003964
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003965 /* init network clients */
3966 if (nb_net_clients == 0) {
3967 /* if no clients, we use a default config */
3968 net_clients[nb_net_clients++] = "nic";
3969#ifdef CONFIG_SLIRP
3970 net_clients[nb_net_clients++] = "user";
3971#endif
3972 }
3973
3974 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003975 if (net_client_parse(net_clients[i]) < 0) {
3976 PANIC("Unable to parse net clients");
3977 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003978 }
3979 net_client_check();
3980
3981#ifdef TARGET_I386
3982 /* XXX: this should be moved in the PC machine instantiation code */
3983 if (net_boot != 0) {
3984 int netroms = 0;
3985 for (i = 0; i < nb_nics && i < 4; i++) {
3986 const char *model = nd_table[i].model;
3987 char buf[1024];
3988 char *filename;
3989 if (net_boot & (1 << i)) {
3990 if (model == NULL)
3991 model = "ne2k_pci";
3992 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3993 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3994 if (filename && get_image_size(filename) > 0) {
3995 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003996 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003997 }
3998 option_rom[nb_option_roms] = qemu_strdup(buf);
3999 nb_option_roms++;
4000 netroms++;
4001 }
4002 if (filename) {
4003 qemu_free(filename);
4004 }
4005 }
4006 }
4007 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004008 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004009 }
4010 }
4011#endif
4012
4013 /* init the bluetooth world */
4014 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004015 if (bt_parse(bt_opts[i])) {
4016 PANIC("Unable to parse bluetooth options");
4017 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004018
4019 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01004020 if (ram_size == 0) {
4021 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
4022 if (ram_size == 0) {
4023 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4024 }
4025 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004026
4027#ifdef CONFIG_KQEMU
4028 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
4029 guest ram allocation. It needs to go away. */
4030 if (kqemu_allowed) {
4031 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
4032 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
4033 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004034 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004035 }
4036 }
4037#endif
4038
rich canningsd952f282011-03-01 15:40:09 -08004039#ifndef _WIN32
4040 init_qemu_clear_logs_sig();
4041#endif
4042
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004043 /* init the dynamic translator */
4044 cpu_exec_init_all(tb_size * 1024 * 1024);
4045
4046 bdrv_init();
4047
4048 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004049#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004050 if (nb_drives_opt < MAX_DRIVES)
4051 drive_add(NULL, CDROM_ALIAS);
4052
4053 /* we always create at least one floppy */
4054
4055 if (nb_drives_opt < MAX_DRIVES)
4056 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004057 /* we always create one sd slot, even if no card is in it */
4058
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004059 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004060 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004061 }
4062#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004063
4064 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004065 if (snapshot)
4066 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4067 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
4068 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004069
David Turner6a9ef172010-09-09 22:54:36 +02004070 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004071 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
4072
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004073 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004074 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004075
4076 /* Maintain compatibility with multiple stdio monitors */
4077 if (!strcmp(monitor_device,"stdio")) {
4078 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4079 const char *devname = serial_devices[i];
4080 if (devname && !strcmp(devname,"mon:stdio")) {
4081 monitor_device = NULL;
4082 break;
4083 } else if (devname && !strcmp(devname,"stdio")) {
4084 monitor_device = NULL;
4085 serial_devices[i] = "mon:stdio";
4086 break;
4087 }
4088 }
4089 }
4090
4091 if (nb_numa_nodes > 0) {
4092 int i;
4093
4094 if (nb_numa_nodes > smp_cpus) {
4095 nb_numa_nodes = smp_cpus;
4096 }
4097
4098 /* If no memory size if given for any node, assume the default case
4099 * and distribute the available memory equally across all nodes
4100 */
4101 for (i = 0; i < nb_numa_nodes; i++) {
4102 if (node_mem[i] != 0)
4103 break;
4104 }
4105 if (i == nb_numa_nodes) {
4106 uint64_t usedmem = 0;
4107
4108 /* On Linux, the each node's border has to be 8MB aligned,
4109 * the final node gets the rest.
4110 */
4111 for (i = 0; i < nb_numa_nodes - 1; i++) {
4112 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4113 usedmem += node_mem[i];
4114 }
4115 node_mem[i] = ram_size - usedmem;
4116 }
4117
4118 for (i = 0; i < nb_numa_nodes; i++) {
4119 if (node_cpumask[i] != 0)
4120 break;
4121 }
4122 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4123 * must cope with this anyway, because there are BIOSes out there in
4124 * real machines which also use this scheme.
4125 */
4126 if (i == nb_numa_nodes) {
4127 for (i = 0; i < smp_cpus; i++) {
4128 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4129 }
4130 }
4131 }
4132
4133 if (kvm_enabled()) {
4134 int ret;
4135
4136 ret = kvm_init(smp_cpus);
4137 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004138 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004139 }
4140 }
4141
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004142#ifdef CONFIG_HAX
4143 if (!hax_disabled)
4144 {
4145 int ret;
4146
Jiang, Yunhong4a5a0ef2012-02-23 06:31:12 +08004147 hax_set_ramsize(ram_size);
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004148 ret = hax_init(smp_cpus);
4149 fprintf(stderr, "HAX is %s and emulator runs in %s mode\n",
4150 !ret ? "working" :"not working", !ret ? "fast virt" : "emulation");
4151 }
4152#endif
4153
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004154 if (monitor_device) {
4155 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
4156 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004157 PANIC("qemu: could not open monitor device '%s'",
4158 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004159 }
4160 }
4161
4162 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004163 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004164 }
4165
4166 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4167 const char *devname = parallel_devices[i];
4168 if (devname && strcmp(devname, "none")) {
4169 char label[32];
4170 snprintf(label, sizeof(label), "parallel%d", i);
4171 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
4172 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004173 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004174 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004175 }
4176 }
4177 }
4178
4179 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4180 const char *devname = virtio_consoles[i];
4181 if (devname && strcmp(devname, "none")) {
4182 char label[32];
4183 snprintf(label, sizeof(label), "virtcon%d", i);
4184 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
4185 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004186 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004187 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004188 }
4189 }
4190 }
4191
4192 module_call_init(MODULE_INIT_DEVICE);
4193
4194
4195#ifdef CONFIG_TRACE
4196 if (trace_filename) {
4197 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004198 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
4199 }
4200#endif
4201
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004202 /* Check the CPU Architecture value */
4203#if defined(TARGET_ARM)
4204 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4205 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4206 android_hw->hw_cpu_arch);
4207 exit(1);
4208 }
Vladimir Chtchetkine33f89d02011-09-28 09:19:09 -07004209#elif defined(TARGET_I386)
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004210 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4211 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4212 android_hw->hw_cpu_arch);
4213 exit(1);
4214 }
4215#endif
4216
4217 /* Grab CPU model if provided in hardware.ini */
4218 if ( !cpu_model
4219 && android_hw->hw_cpu_model
4220 && android_hw->hw_cpu_model[0] != '\0')
4221 {
4222 cpu_model = android_hw->hw_cpu_model;
4223 }
4224
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004225 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004226 * collected during initialization.
4227 *
4228 * The order is the following:
4229 * - parameters from the hw configuration (kernel.parameters)
4230 * - additionnal parameters from options (e.g. -memcheck)
4231 * - the -append parameters.
4232 */
4233 {
4234 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004235
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004236 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004237 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004238 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4239 }
4240
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004241 /* If not empty, kernel_config always contains a leading space */
4242 stralloc_append(kernel_params, kernel_config);
4243
4244 if (*kernel_cmdline) {
4245 stralloc_add_c(kernel_params, ' ');
4246 stralloc_add_str(kernel_params, kernel_cmdline);
4247 }
4248
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004249 /* Remove any leading/trailing spaces */
4250 stralloc_strip(kernel_params);
4251
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004252 kernel_parameters = stralloc_cstr(kernel_params);
4253 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4254
4255 machine->init(ram_size,
4256 boot_devices,
4257 kernel_filename,
4258 kernel_parameters,
4259 initrd_filename,
4260 cpu_model);
4261
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004262 /* Initialize multi-touch emulation. */
Vladimir Chtchetkine863d1012012-03-16 12:25:23 -07004263 if (androidHwConfig_isScreenMultiTouch(android_hw)) {
Vladimir Chtchetkine8dd31e82012-02-15 17:16:04 -08004264 mts_port_create(NULL);
4265 }
4266
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004267 stralloc_reset(kernel_params);
4268 stralloc_reset(kernel_config);
4269 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004270
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004271 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4272 for (i = 0; i < nb_numa_nodes; i++) {
4273 if (node_cpumask[i] & (1 << env->cpu_index)) {
4274 env->numa_node = i;
4275 }
4276 }
4277 }
4278
4279 current_machine = machine;
4280
4281 /* Set KVM's vcpu state to qemu's initial CPUState. */
4282 if (kvm_enabled()) {
4283 int ret;
4284
4285 ret = kvm_sync_vcpus();
4286 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004287 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004288 }
4289 }
4290
Jun Nakajimaa381ef02011-12-17 19:13:25 -08004291#ifdef CONFIG_HAX
4292 if (hax_enabled())
4293 hax_sync_vcpus();
4294#endif
4295
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004296 /* init USB devices */
4297 if (usb_enabled) {
4298 for(i = 0; i < usb_devices_index; i++) {
4299 if (usb_device_add(usb_devices[i], 0) < 0) {
4300 fprintf(stderr, "Warning: could not add USB device %s\n",
4301 usb_devices[i]);
4302 }
4303 }
4304 }
4305
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004306 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004307 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004308
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004309 /* Initialize display from the command line parameters. */
4310 android_display_reset(ds,
4311 android_display_width,
4312 android_display_height,
4313 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004314
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004315 if (display_type == DT_DEFAULT) {
4316#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4317 display_type = DT_SDL;
4318#else
4319 display_type = DT_VNC;
4320 vnc_display = "localhost:0,to=99";
4321 show_vnc_port = 1;
4322#endif
4323 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004324
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004325
4326 switch (display_type) {
4327 case DT_NOGRAPHIC:
4328 break;
4329#if defined(CONFIG_CURSES)
4330 case DT_CURSES:
4331 curses_display_init(ds, full_screen);
4332 break;
4333#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004334#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004335 case DT_SDL:
4336 sdl_display_init(ds, full_screen, no_frame);
4337 break;
4338#elif defined(CONFIG_COCOA)
4339 case DT_SDL:
4340 cocoa_display_init(ds, full_screen);
4341 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004342#elif defined(CONFIG_STANDALONE_CORE)
4343 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004344 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004345 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004346#endif
4347 case DT_VNC:
4348 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004349 if (vnc_display_open(ds, vnc_display) < 0) {
4350 PANIC("Unable to initialize VNC display");
4351 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004352
4353 if (show_vnc_port) {
4354 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4355 }
4356 break;
4357 default:
4358 break;
4359 }
4360 dpy_resize(ds);
4361
4362 dcl = ds->listeners;
4363 while (dcl != NULL) {
4364 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004365 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
4366 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004367 }
4368 dcl = dcl->next;
4369 }
4370
4371 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004372 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
4373 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004374 }
4375
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004376 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004377 qemu_chr_initial_reset();
4378
4379 if (monitor_device && monitor_hd)
4380 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4381
4382 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4383 const char *devname = serial_devices[i];
4384 if (devname && strcmp(devname, "none")) {
4385 if (strstart(devname, "vc", 0))
4386 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4387 }
4388 }
4389
4390 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4391 const char *devname = parallel_devices[i];
4392 if (devname && strcmp(devname, "none")) {
4393 if (strstart(devname, "vc", 0))
4394 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4395 }
4396 }
4397
4398 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4399 const char *devname = virtio_consoles[i];
4400 if (virtcon_hds[i] && devname) {
4401 if (strstart(devname, "vc", 0))
4402 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4403 }
4404 }
4405
4406 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004407 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004408 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004409 }
4410
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004411 /* call android-specific setup function */
4412 android_emulation_setup();
4413
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004414#if !defined(CONFIG_STANDALONE_CORE)
4415 // For the standalone emulator (UI+core in one executable) we need to
4416 // set the window title here.
4417 android_emulator_set_base_port(android_base_port);
4418#endif
4419
Ot ten Thije871da2a2010-09-20 10:29:22 +01004420 if (loadvm)
4421 do_loadvm(cur_mon, loadvm);
4422
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004423 if (incoming) {
4424 autostart = 0; /* fixme how to deal with -daemonize */
4425 qemu_start_incoming_migration(incoming);
4426 }
4427
4428 if (autostart)
4429 vm_start();
4430
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004431 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004432
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004433#ifdef CONFIG_ANDROID
4434 // This will notify the UI that the core is successfuly initialized
4435 android_core_init_completed();
4436#endif // CONFIG_ANDROID
4437
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004438 main_loop();
4439 quit_timers();
4440 net_cleanup();
4441 android_emulation_teardown();
4442 return 0;
4443}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004444
4445void
4446android_emulation_teardown(void)
4447{
4448 android_charmap_done();
4449}