blob: c02abd51a1c263e5ffd2baaea9170bb88ca742f4 [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 Chtchetkineeb838252010-07-15 12:27:56 -070058#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070059#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070060#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010061#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010062#include "android/utils/filelock.h"
63#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010064#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010065#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080066#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080067#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010068#include "android/snapshot.h"
David 'Digit' Turnercb88e792011-08-26 01:35:14 +020069#include "android/opengles.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070070#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070071#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070072
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070073#ifdef CONFIG_MEMCHECK
74#include "memcheck/memcheck.h"
75#endif // CONFIG_MEMCHECK
76
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070077#include <unistd.h>
78#include <fcntl.h>
79#include <signal.h>
80#include <time.h>
81#include <errno.h>
82#include <sys/time.h>
83#include <zlib.h>
84
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070085/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070086#include "config-host.h"
87
88#ifndef _WIN32
89#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070090#include <sys/times.h>
91#include <sys/wait.h>
92#include <termios.h>
93#include <sys/mman.h>
94#include <sys/ioctl.h>
95#include <sys/resource.h>
96#include <sys/socket.h>
97#include <netinet/in.h>
98#include <net/if.h>
99#if defined(__NetBSD__)
100#include <net/if_tap.h>
101#endif
102#ifdef __linux__
103#include <linux/if_tun.h>
104#endif
105#include <arpa/inet.h>
106#include <dirent.h>
107#include <netdb.h>
108#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700109#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700110#include <sys/stat.h>
111#if defined(__FreeBSD__) || defined(__DragonFly__)
112#include <libutil.h>
113#else
114#include <util.h>
115#endif
116#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
117#include <freebsd/stdlib.h>
118#else
119#ifdef __linux__
120#include <pty.h>
121#include <malloc.h>
122#include <linux/rtc.h>
123
124/* For the benefit of older linux systems which don't supply it,
125 we use a local copy of hpet.h. */
126/* #include <linux/hpet.h> */
127#include "hpet.h"
128
129#include <linux/ppdev.h>
130#include <linux/parport.h>
131#endif
132#ifdef __sun__
133#include <sys/stat.h>
134#include <sys/ethernet.h>
135#include <sys/sockio.h>
136#include <netinet/arp.h>
137#include <netinet/in.h>
138#include <netinet/in_systm.h>
139#include <netinet/ip.h>
140#include <netinet/ip_icmp.h> // must come after ip.h
141#include <netinet/udp.h>
142#include <netinet/tcp.h>
143#include <net/if.h>
144#include <syslog.h>
145#include <stropts.h>
146#endif
147#endif
148#endif
149
150#if defined(__OpenBSD__)
151#include <util.h>
152#endif
153
154#if defined(CONFIG_VDE)
155#include <libvdeplug.h>
156#endif
157
158#ifdef _WIN32
159#include <windows.h>
160#include <malloc.h>
161#include <sys/timeb.h>
162#include <mmsystem.h>
163#define getopt_long_only getopt_long
164#define memalign(align, size) malloc(size)
165#endif
166
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200167#include "cpus.h"
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200168#include "arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700169
170#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200171int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700172#undef main
173#define main qemu_main
174#endif /* CONFIG_COCOA */
175
176#include "hw/hw.h"
177#include "hw/boards.h"
178#include "hw/usb.h"
179#include "hw/pcmcia.h"
180#include "hw/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700181#include "hw/isa.h"
182#include "hw/baum.h"
183#include "hw/bt.h"
184#include "hw/watchdog.h"
185#include "hw/smbios.h"
186#include "hw/xen.h"
187#include "bt-host.h"
188#include "net.h"
189#include "monitor.h"
190#include "console.h"
191#include "sysemu.h"
192#include "gdbstub.h"
193#include "qemu-timer.h"
194#include "qemu-char.h"
195#include "cache-utils.h"
196#include "block.h"
197#include "dma.h"
198#include "audio/audio.h"
199#include "migration.h"
200#include "kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200201#ifdef CONFIG_KVM
202#include "kvm-android.h"
203#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700204#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700205#include "android/hw-lcd.h"
206#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200207#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700208#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100209#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700210
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700211#ifdef CONFIG_STANDALONE_CORE
212/* Verbose value used by the standalone emulator core (without UI) */
213unsigned long android_verbose;
214#endif // CONFIG_STANDALONE_CORE
215
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800216#if !defined(CONFIG_STANDALONE_CORE)
217/* in android/qemulator.c */
218extern void android_emulator_set_base_port(int port);
219#endif
220
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700221#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700222#undef main
223#define main qemu_main
224#endif
225
226#include "disas.h"
227
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700228#ifdef CONFIG_TRACE
David 'Digit' Turner406a59b2011-05-12 10:26:09 +0200229#include "android-trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700230#endif
231
232#include "qemu_socket.h"
233
234#if defined(CONFIG_SLIRP)
235#include "libslirp.h"
236#endif
237
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238
239
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700240#define DEFAULT_RAM_SIZE 128
241
242/* Max number of USB devices that can be specified on the commandline. */
243#define MAX_USB_CMDLINE 8
244
245/* Max number of bluetooth switches on the commandline. */
246#define MAX_BT_CMDLINE 10
247
248/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700249
250static const char *data_dir;
251const char *bios_name = NULL;
252static void *ioport_opaque[MAX_IOPORTS];
253static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
254static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100255#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700256/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
257 to store the VM snapshots */
258DriveInfo drives_table[MAX_DRIVES+1];
259int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100260#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700261enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700262DisplayType display_type = DT_DEFAULT;
263const char* keyboard_layout = NULL;
264int64_t ticks_per_sec;
265ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200266const char *mem_path = NULL;
267#ifdef MAP_POPULATE
268int mem_prealloc = 0; /* force preallocation of physical target memory */
269#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700270int nb_nics;
271NICInfo nd_table[MAX_NICS];
272int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100273int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700274static int rtc_utc = 1;
275static int rtc_date_offset = -1; /* -1 means no change */
276int cirrus_vga_enabled = 1;
277int std_vga_enabled = 0;
278int vmsvga_enabled = 0;
279int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700280QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700281static int full_screen = 0;
282#ifdef CONFIG_SDL
283static int no_frame = 0;
284#endif
285int no_quit = 0;
286CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100287int serial_hds_count;
288
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700289CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
290CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
291#ifdef TARGET_I386
292int win2k_install_hack = 0;
293int rtc_td_hack = 0;
294#endif
295int usb_enabled = 0;
296int singlestep = 0;
297int smp_cpus = 1;
298const char *vnc_display;
299int acpi_enabled = 1;
300int no_hpet = 0;
301int no_virtio_balloon = 0;
302int fd_bootchk = 1;
303int no_reboot = 0;
304int no_shutdown = 0;
305int cursor_hide = 1;
306int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700307WatchdogTimerModel *watchdog = NULL;
308int watchdog_action = WDT_RESET;
309const char *option_rom[MAX_OPTION_ROMS];
310int nb_option_roms;
311int semihosting_enabled = 0;
312#ifdef TARGET_ARM
313int old_param = 0;
314#endif
315const char *qemu_name;
316int alt_grab = 0;
317#if defined(TARGET_SPARC) || defined(TARGET_PPC)
318unsigned int nb_prom_envs = 0;
319const char *prom_envs[MAX_PROM_ENVS];
320#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100321#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700322int nb_drives_opt;
323struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100324#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325int nb_numa_nodes;
326uint64_t node_mem[MAX_NODES];
327uint64_t node_cpumask[MAX_NODES];
328
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700329static QEMUTimer *nographic_timer;
330
331uint8_t qemu_uuid[16];
332
333
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700334int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700335extern char* audio_input_source;
336
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700337extern char* android_op_ports;
338extern char* android_op_port;
339extern char* android_op_report_console;
340extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700341// Path to the file containing specific key character map.
342char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700343
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700344/* Path to hardware initialization file passed with -android-hw option. */
345char* android_op_hwini = NULL;
346
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700347/* Memory checker options. */
348char* android_op_memcheck = NULL;
349
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700350/* -dns-server option value. */
351char* android_op_dns_server = NULL;
352
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700353/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700354char* android_op_radio = NULL;
355
356/* -gps option value. */
357char* android_op_gps = NULL;
358
359/* -audio option value. */
360char* android_op_audio = NULL;
361
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700362/* -cpu-delay option value. */
363char* android_op_cpu_delay = NULL;
364
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700365#ifdef CONFIG_NAND_LIMITS
366/* -nand-limits option value. */
367char* android_op_nand_limits = NULL;
368#endif // CONFIG_NAND_LIMITS
369
370/* -netspeed option value. */
371char* android_op_netspeed = NULL;
372
373/* -netdelay option value. */
374char* android_op_netdelay = NULL;
375
376/* -netfast option value. */
377int android_op_netfast = 0;
378
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700379/* -tcpdump option value. */
380char* android_op_tcpdump = NULL;
381
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700382/* -lcd-density option value. */
383char* android_op_lcd_density = NULL;
384
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700385/* -ui-port option value. This port will be used to report the core
386 * initialization completion.
387 */
388char* android_op_ui_port = NULL;
389
390/* -ui-settings option value. This value will be passed to the UI when new UI
391 * process is attaching to the core.
392 */
393char* android_op_ui_settings = NULL;
394
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800395/* -android-avdname option value. */
396char* android_op_avd_name = "unknown";
397
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700398extern int android_display_width;
399extern int android_display_height;
400extern int android_display_bpp;
401
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700402extern void dprint( const char* format, ... );
403
rich canningsd952f282011-03-01 15:40:09 -0800404const char* dns_log_filename = NULL;
405const char* drop_log_filename = NULL;
406static int rotate_logs_requested = 0;
407
Tim Baverstock24204cc2010-11-25 11:37:43 +0000408const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000409
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700410#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
411
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700412/* Reports the core initialization failure to the error stdout and to the UI
413 * socket before exiting the application.
414 * Parameters that are passed to this macro are used to format the error
415 * mesage using sprintf routine.
416 */
417#ifdef CONFIG_ANDROID
418#define PANIC(...) android_core_init_failure(__VA_ARGS__)
419#else
420#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
421 exit(1); \
422 } while (0)
423#endif // CONFIG_ANDROID
424
425/* Exits the core during initialization. */
426#ifdef CONFIG_ANDROID
427#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
428#else
429#define QEMU_EXIT(exit_code) exit(exit_code)
430#endif // CONFIG_ANDROID
431
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700432/***********************************************************/
433/* x86 ISA bus support */
434
435target_phys_addr_t isa_mem_base = 0;
436PicState2 *isa_pic;
437
438static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
439static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
440
441static uint32_t ioport_read(int index, uint32_t address)
442{
443 static IOPortReadFunc *default_func[3] = {
444 default_ioport_readb,
445 default_ioport_readw,
446 default_ioport_readl
447 };
448 IOPortReadFunc *func = ioport_read_table[index][address];
449 if (!func)
450 func = default_func[index];
451 return func(ioport_opaque[address], address);
452}
453
454static void ioport_write(int index, uint32_t address, uint32_t data)
455{
456 static IOPortWriteFunc *default_func[3] = {
457 default_ioport_writeb,
458 default_ioport_writew,
459 default_ioport_writel
460 };
461 IOPortWriteFunc *func = ioport_write_table[index][address];
462 if (!func)
463 func = default_func[index];
464 func(ioport_opaque[address], address, data);
465}
466
467static uint32_t default_ioport_readb(void *opaque, uint32_t address)
468{
469#ifdef DEBUG_UNUSED_IOPORT
470 fprintf(stderr, "unused inb: port=0x%04x\n", address);
471#endif
472 return 0xff;
473}
474
475static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
476{
477#ifdef DEBUG_UNUSED_IOPORT
478 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
479#endif
480}
481
482/* default is to make two byte accesses */
483static uint32_t default_ioport_readw(void *opaque, uint32_t address)
484{
485 uint32_t data;
486 data = ioport_read(0, address);
487 address = (address + 1) & (MAX_IOPORTS - 1);
488 data |= ioport_read(0, address) << 8;
489 return data;
490}
491
492static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
493{
494 ioport_write(0, address, data & 0xff);
495 address = (address + 1) & (MAX_IOPORTS - 1);
496 ioport_write(0, address, (data >> 8) & 0xff);
497}
498
499static uint32_t default_ioport_readl(void *opaque, uint32_t address)
500{
501#ifdef DEBUG_UNUSED_IOPORT
502 fprintf(stderr, "unused inl: port=0x%04x\n", address);
503#endif
504 return 0xffffffff;
505}
506
507static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
508{
509#ifdef DEBUG_UNUSED_IOPORT
510 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
511#endif
512}
513
rich canningsd952f282011-03-01 15:40:09 -0800514/*
515 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
516 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
517 * between the tasks that do not require restarting Qemu.
518 */
519void rotate_qemu_logs_handler(int signum) {
520 rotate_logs_requested = 1;
521}
522
523/*
524 * Resets the rotate_log_requested_flag. Normally called after qemu
525 * logs has been rotated.
526 */
527void reset_rotate_qemu_logs_request(void) {
528 rotate_logs_requested = 0;
529}
530
531/*
532 * Clears the passed qemu log when the rotate_logs_requested
533 * is set. We need to clear the logs between the tasks that do not
534 * require restarting Qemu.
535 */
536FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
537 FILE* new_log_fd = NULL;
538 if (old_log_fd) {
539 if (fclose(old_log_fd) == -1) {
540 fprintf(stderr, "Cannot close old_log fd\n");
541 exit(errno);
542 }
543 }
544
545 if (!filename) {
546 fprintf(stderr, "The log filename to be rotated is not provided");
547 exit(-1);
548 }
549
550 new_log_fd = fopen(filename , "wb+");
551 if (new_log_fd == NULL) {
552 fprintf(stderr, "Cannot open the log file: %s for write.\n",
553 filename);
554 exit(1);
555 }
556
557 return new_log_fd;
558}
559
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700560/***************/
561/* ballooning */
562
563static QEMUBalloonEvent *qemu_balloon_event;
564void *qemu_balloon_event_opaque;
565
566void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
567{
568 qemu_balloon_event = func;
569 qemu_balloon_event_opaque = opaque;
570}
571
572void qemu_balloon(ram_addr_t target)
573{
574 if (qemu_balloon_event)
575 qemu_balloon_event(qemu_balloon_event_opaque, target);
576}
577
578ram_addr_t qemu_balloon_status(void)
579{
580 if (qemu_balloon_event)
581 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
582 return 0;
583}
584
585/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700586/* host time/date access */
587void qemu_get_timedate(struct tm *tm, int offset)
588{
589 time_t ti;
590 struct tm *ret;
591
592 time(&ti);
593 ti += offset;
594 if (rtc_date_offset == -1) {
595 if (rtc_utc)
596 ret = gmtime(&ti);
597 else
598 ret = localtime(&ti);
599 } else {
600 ti -= rtc_date_offset;
601 ret = gmtime(&ti);
602 }
603
604 memcpy(tm, ret, sizeof(struct tm));
605}
606
607int qemu_timedate_diff(struct tm *tm)
608{
609 time_t seconds;
610
611 if (rtc_date_offset == -1)
612 if (rtc_utc)
613 seconds = mktimegm(tm);
614 else
615 seconds = mktime(tm);
616 else
617 seconds = mktimegm(tm) + rtc_date_offset;
618
619 return seconds - time(NULL);
620}
621
622
623#ifdef CONFIG_TRACE
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200624int tbflush_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700625static int exit_requested;
626
627void start_tracing()
628{
629 if (trace_filename == NULL)
630 return;
631 if (!tracing) {
632 fprintf(stderr,"-- start tracing --\n");
633 start_time = Now();
634 }
635 tracing = 1;
636 tbflush_requested = 1;
637 qemu_notify_event();
638}
639
640void stop_tracing()
641{
642 if (trace_filename == NULL)
643 return;
644 if (tracing) {
645 end_time = Now();
646 elapsed_usecs += end_time - start_time;
647 fprintf(stderr,"-- stop tracing --\n");
648 }
649 tracing = 0;
650 tbflush_requested = 1;
651 qemu_notify_event();
652}
653
654#ifndef _WIN32
655/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
656 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
657 */
658void sigusr_handler(int sig)
659{
660 if (sig == SIGUSR1)
661 start_tracing();
662 else
663 stop_tracing();
664}
665#endif
666
667/* This is the handler to catch control-C so that we can exit cleanly.
668 * This is needed when tracing to flush the buffers to disk.
669 */
670void sigint_handler(int sig)
671{
672 exit_requested = 1;
673 qemu_notify_event();
674}
675#endif /* CONFIG_TRACE */
676
677
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700678/***********************************************************/
679/* Bluetooth support */
680static int nb_hcis;
681static int cur_hci;
682static struct HCIInfo *hci_table[MAX_NICS];
683
684static struct bt_vlan_s {
685 struct bt_scatternet_s net;
686 int id;
687 struct bt_vlan_s *next;
688} *first_bt_vlan;
689
690/* find or alloc a new bluetooth "VLAN" */
691static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
692{
693 struct bt_vlan_s **pvlan, *vlan;
694 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
695 if (vlan->id == id)
696 return &vlan->net;
697 }
698 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
699 vlan->id = id;
700 pvlan = &first_bt_vlan;
701 while (*pvlan != NULL)
702 pvlan = &(*pvlan)->next;
703 *pvlan = vlan;
704 return &vlan->net;
705}
706
707static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
708{
709}
710
711static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
712{
713 return -ENOTSUP;
714}
715
716static struct HCIInfo null_hci = {
717 .cmd_send = null_hci_send,
718 .sco_send = null_hci_send,
719 .acl_send = null_hci_send,
720 .bdaddr_set = null_hci_addr_set,
721};
722
723struct HCIInfo *qemu_next_hci(void)
724{
725 if (cur_hci == nb_hcis)
726 return &null_hci;
727
728 return hci_table[cur_hci++];
729}
730
731static struct HCIInfo *hci_init(const char *str)
732{
733 char *endp;
734 struct bt_scatternet_s *vlan = 0;
735
736 if (!strcmp(str, "null"))
737 /* null */
738 return &null_hci;
739 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
740 /* host[:hciN] */
741 return bt_host_hci(str[4] ? str + 5 : "hci0");
742 else if (!strncmp(str, "hci", 3)) {
743 /* hci[,vlan=n] */
744 if (str[3]) {
745 if (!strncmp(str + 3, ",vlan=", 6)) {
746 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
747 if (*endp)
748 vlan = 0;
749 }
750 } else
751 vlan = qemu_find_bt_vlan(0);
752 if (vlan)
753 return bt_new_hci(vlan);
754 }
755
756 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
757
758 return 0;
759}
760
761static int bt_hci_parse(const char *str)
762{
763 struct HCIInfo *hci;
764 bdaddr_t bdaddr;
765
766 if (nb_hcis >= MAX_NICS) {
767 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
768 return -1;
769 }
770
771 hci = hci_init(str);
772 if (!hci)
773 return -1;
774
775 bdaddr.b[0] = 0x52;
776 bdaddr.b[1] = 0x54;
777 bdaddr.b[2] = 0x00;
778 bdaddr.b[3] = 0x12;
779 bdaddr.b[4] = 0x34;
780 bdaddr.b[5] = 0x56 + nb_hcis;
781 hci->bdaddr_set(hci, bdaddr.b);
782
783 hci_table[nb_hcis++] = hci;
784
785 return 0;
786}
787
788static void bt_vhci_add(int vlan_id)
789{
790 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
791
792 if (!vlan->slave)
793 fprintf(stderr, "qemu: warning: adding a VHCI to "
794 "an empty scatternet %i\n", vlan_id);
795
796 bt_vhci_init(bt_new_hci(vlan));
797}
798
799static struct bt_device_s *bt_device_add(const char *opt)
800{
801 struct bt_scatternet_s *vlan;
802 int vlan_id = 0;
803 char *endp = strstr(opt, ",vlan=");
804 int len = (endp ? endp - opt : strlen(opt)) + 1;
805 char devname[10];
806
807 pstrcpy(devname, MIN(sizeof(devname), len), opt);
808
809 if (endp) {
810 vlan_id = strtol(endp + 6, &endp, 0);
811 if (*endp) {
812 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
813 return 0;
814 }
815 }
816
817 vlan = qemu_find_bt_vlan(vlan_id);
818
819 if (!vlan->slave)
820 fprintf(stderr, "qemu: warning: adding a slave device to "
821 "an empty scatternet %i\n", vlan_id);
822
823 if (!strcmp(devname, "keyboard"))
824 return bt_keyboard_init(vlan);
825
826 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
827 return 0;
828}
829
830static int bt_parse(const char *opt)
831{
832 const char *endp, *p;
833 int vlan;
834
835 if (strstart(opt, "hci", &endp)) {
836 if (!*endp || *endp == ',') {
837 if (*endp)
838 if (!strstart(endp, ",vlan=", 0))
839 opt = endp + 1;
840
841 return bt_hci_parse(opt);
842 }
843 } else if (strstart(opt, "vhci", &endp)) {
844 if (!*endp || *endp == ',') {
845 if (*endp) {
846 if (strstart(endp, ",vlan=", &p)) {
847 vlan = strtol(p, (char **) &endp, 0);
848 if (*endp) {
849 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
850 return 1;
851 }
852 } else {
853 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
854 return 1;
855 }
856 } else
857 vlan = 0;
858
859 bt_vhci_add(vlan);
860 return 0;
861 }
862 } else if (strstart(opt, "device:", &endp))
863 return !bt_device_add(endp);
864
865 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
866 return 1;
867}
868
869/***********************************************************/
870/* QEMU Block devices */
871
872#define HD_ALIAS "index=%d,media=disk"
873#define CDROM_ALIAS "index=2,media=cdrom"
874#define FD_ALIAS "index=%d,if=floppy"
875#define PFLASH_ALIAS "if=pflash"
876#define MTD_ALIAS "if=mtd"
877#define SD_ALIAS "index=0,if=sd"
878
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100879static int drive_init_func(QemuOpts *opts, void *opaque)
880{
881 int *use_scsi = opaque;
882 int fatal_error = 0;
883
884 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
885 if (fatal_error)
886 return 1;
887 }
888 return 0;
889}
890
891static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
892{
893 if (NULL == qemu_opt_get(opts, "snapshot")) {
894 qemu_opt_set(opts, "snapshot", "on");
895 }
896 return 0;
897}
898
899#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700900static int drive_opt_get_free_idx(void)
901{
902 int index;
903
904 for (index = 0; index < MAX_DRIVES; index++)
905 if (!drives_opt[index].used) {
906 drives_opt[index].used = 1;
907 return index;
908 }
909
910 return -1;
911}
912
913static int drive_get_free_idx(void)
914{
915 int index;
916
917 for (index = 0; index < MAX_DRIVES; index++)
918 if (!drives_table[index].used) {
919 drives_table[index].used = 1;
920 return index;
921 }
922
923 return -1;
924}
925
926int drive_add(const char *file, const char *fmt, ...)
927{
928 va_list ap;
929 int index = drive_opt_get_free_idx();
930
931 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
932 fprintf(stderr, "qemu: too many drives\n");
933 return -1;
934 }
935
936 drives_opt[index].file = file;
937 va_start(ap, fmt);
938 vsnprintf(drives_opt[index].opt,
939 sizeof(drives_opt[0].opt), fmt, ap);
940 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700941
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700942 nb_drives_opt++;
943 return index;
944}
945
946void drive_remove(int index)
947{
948 drives_opt[index].used = 0;
949 nb_drives_opt--;
950}
951
952int drive_get_index(BlockInterfaceType type, int bus, int unit)
953{
954 int index;
955
956 /* seek interface, bus and unit */
957
958 for (index = 0; index < MAX_DRIVES; index++)
959 if (drives_table[index].type == type &&
960 drives_table[index].bus == bus &&
961 drives_table[index].unit == unit &&
962 drives_table[index].used)
963 return index;
964
965 return -1;
966}
967
968int drive_get_max_bus(BlockInterfaceType type)
969{
970 int max_bus;
971 int index;
972
973 max_bus = -1;
974 for (index = 0; index < nb_drives; index++) {
975 if(drives_table[index].type == type &&
976 drives_table[index].bus > max_bus)
977 max_bus = drives_table[index].bus;
978 }
979 return max_bus;
980}
981
982const char *drive_get_serial(BlockDriverState *bdrv)
983{
984 int index;
985
986 for (index = 0; index < nb_drives; index++)
987 if (drives_table[index].bdrv == bdrv)
988 return drives_table[index].serial;
989
990 return "\0";
991}
992
993BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
994{
995 int index;
996
997 for (index = 0; index < nb_drives; index++)
998 if (drives_table[index].bdrv == bdrv)
999 return drives_table[index].onerror;
1000
1001 return BLOCK_ERR_STOP_ENOSPC;
1002}
1003
1004static void bdrv_format_print(void *opaque, const char *name)
1005{
1006 fprintf(stderr, " %s", name);
1007}
1008
1009void drive_uninit(BlockDriverState *bdrv)
1010{
1011 int i;
1012
1013 for (i = 0; i < MAX_DRIVES; i++)
1014 if (drives_table[i].bdrv == bdrv) {
1015 drives_table[i].bdrv = NULL;
1016 drives_table[i].used = 0;
1017 drive_remove(drives_table[i].drive_opt_idx);
1018 nb_drives--;
1019 break;
1020 }
1021}
1022
1023int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1024{
1025 char buf[128];
1026 char file[1024];
1027 char devname[128];
1028 char serial[21];
1029 const char *mediastr = "";
1030 BlockInterfaceType type;
1031 enum { MEDIA_DISK, MEDIA_CDROM } media;
1032 int bus_id, unit_id;
1033 int cyls, heads, secs, translation;
1034 BlockDriverState *bdrv;
1035 BlockDriver *drv = NULL;
1036 QEMUMachine *machine = opaque;
1037 int max_devs;
1038 int index;
1039 int cache;
1040 int bdrv_flags, onerror;
1041 int drives_table_idx;
1042 char *str = arg->opt;
1043 static const char * const params[] = { "bus", "unit", "if", "index",
1044 "cyls", "heads", "secs", "trans",
1045 "media", "snapshot", "file",
1046 "cache", "format", "serial", "werror",
1047 NULL };
1048
1049 if (check_params(buf, sizeof(buf), params, str) < 0) {
1050 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1051 buf, str);
1052 return -1;
1053 }
1054
1055 file[0] = 0;
1056 cyls = heads = secs = 0;
1057 bus_id = 0;
1058 unit_id = -1;
1059 translation = BIOS_ATA_TRANSLATION_AUTO;
1060 index = -1;
1061 cache = 3;
1062
1063 if (machine->use_scsi) {
1064 type = IF_SCSI;
1065 max_devs = MAX_SCSI_DEVS;
1066 pstrcpy(devname, sizeof(devname), "scsi");
1067 } else {
1068 type = IF_IDE;
1069 max_devs = MAX_IDE_DEVS;
1070 pstrcpy(devname, sizeof(devname), "ide");
1071 }
1072 media = MEDIA_DISK;
1073
1074 /* extract parameters */
1075
1076 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1077 bus_id = strtol(buf, NULL, 0);
1078 if (bus_id < 0) {
1079 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1080 return -1;
1081 }
1082 }
1083
1084 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1085 unit_id = strtol(buf, NULL, 0);
1086 if (unit_id < 0) {
1087 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1088 return -1;
1089 }
1090 }
1091
1092 if (get_param_value(buf, sizeof(buf), "if", str)) {
1093 pstrcpy(devname, sizeof(devname), buf);
1094 if (!strcmp(buf, "ide")) {
1095 type = IF_IDE;
1096 max_devs = MAX_IDE_DEVS;
1097 } else if (!strcmp(buf, "scsi")) {
1098 type = IF_SCSI;
1099 max_devs = MAX_SCSI_DEVS;
1100 } else if (!strcmp(buf, "floppy")) {
1101 type = IF_FLOPPY;
1102 max_devs = 0;
1103 } else if (!strcmp(buf, "pflash")) {
1104 type = IF_PFLASH;
1105 max_devs = 0;
1106 } else if (!strcmp(buf, "mtd")) {
1107 type = IF_MTD;
1108 max_devs = 0;
1109 } else if (!strcmp(buf, "sd")) {
1110 type = IF_SD;
1111 max_devs = 0;
1112 } else if (!strcmp(buf, "virtio")) {
1113 type = IF_VIRTIO;
1114 max_devs = 0;
1115 } else if (!strcmp(buf, "xen")) {
1116 type = IF_XEN;
1117 max_devs = 0;
1118 } else {
1119 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1120 return -1;
1121 }
1122 }
1123
1124 if (get_param_value(buf, sizeof(buf), "index", str)) {
1125 index = strtol(buf, NULL, 0);
1126 if (index < 0) {
1127 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1128 return -1;
1129 }
1130 }
1131
1132 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1133 cyls = strtol(buf, NULL, 0);
1134 }
1135
1136 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1137 heads = strtol(buf, NULL, 0);
1138 }
1139
1140 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1141 secs = strtol(buf, NULL, 0);
1142 }
1143
1144 if (cyls || heads || secs) {
1145 if (cyls < 1 || cyls > 16383) {
1146 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1147 return -1;
1148 }
1149 if (heads < 1 || heads > 16) {
1150 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1151 return -1;
1152 }
1153 if (secs < 1 || secs > 63) {
1154 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1155 return -1;
1156 }
1157 }
1158
1159 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1160 if (!cyls) {
1161 fprintf(stderr,
1162 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1163 str);
1164 return -1;
1165 }
1166 if (!strcmp(buf, "none"))
1167 translation = BIOS_ATA_TRANSLATION_NONE;
1168 else if (!strcmp(buf, "lba"))
1169 translation = BIOS_ATA_TRANSLATION_LBA;
1170 else if (!strcmp(buf, "auto"))
1171 translation = BIOS_ATA_TRANSLATION_AUTO;
1172 else {
1173 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1174 return -1;
1175 }
1176 }
1177
1178 if (get_param_value(buf, sizeof(buf), "media", str)) {
1179 if (!strcmp(buf, "disk")) {
1180 media = MEDIA_DISK;
1181 } else if (!strcmp(buf, "cdrom")) {
1182 if (cyls || secs || heads) {
1183 fprintf(stderr,
1184 "qemu: '%s' invalid physical CHS format\n", str);
1185 return -1;
1186 }
1187 media = MEDIA_CDROM;
1188 } else {
1189 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1190 return -1;
1191 }
1192 }
1193
1194 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1195 if (!strcmp(buf, "on"))
1196 snapshot = 1;
1197 else if (!strcmp(buf, "off"))
1198 snapshot = 0;
1199 else {
1200 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1201 return -1;
1202 }
1203 }
1204
1205 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1206 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1207 cache = 0;
1208 else if (!strcmp(buf, "writethrough"))
1209 cache = 1;
1210 else if (!strcmp(buf, "writeback"))
1211 cache = 2;
1212 else {
1213 fprintf(stderr, "qemu: invalid cache option\n");
1214 return -1;
1215 }
1216 }
1217
1218 if (get_param_value(buf, sizeof(buf), "format", str)) {
1219 if (strcmp(buf, "?") == 0) {
1220 fprintf(stderr, "qemu: Supported formats:");
1221 bdrv_iterate_format(bdrv_format_print, NULL);
1222 fprintf(stderr, "\n");
1223 return -1;
1224 }
1225 drv = bdrv_find_format(buf);
1226 if (!drv) {
1227 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1228 return -1;
1229 }
1230 }
1231
1232 if (arg->file == NULL)
1233 get_param_value(file, sizeof(file), "file", str);
1234 else
1235 pstrcpy(file, sizeof(file), arg->file);
1236
1237 if (!get_param_value(serial, sizeof(serial), "serial", str))
1238 memset(serial, 0, sizeof(serial));
1239
1240 onerror = BLOCK_ERR_STOP_ENOSPC;
1241 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1242 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1243 fprintf(stderr, "werror is no supported by this format\n");
1244 return -1;
1245 }
1246 if (!strcmp(buf, "ignore"))
1247 onerror = BLOCK_ERR_IGNORE;
1248 else if (!strcmp(buf, "enospc"))
1249 onerror = BLOCK_ERR_STOP_ENOSPC;
1250 else if (!strcmp(buf, "stop"))
1251 onerror = BLOCK_ERR_STOP_ANY;
1252 else if (!strcmp(buf, "report"))
1253 onerror = BLOCK_ERR_REPORT;
1254 else {
1255 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1256 return -1;
1257 }
1258 }
1259
1260 /* compute bus and unit according index */
1261
1262 if (index != -1) {
1263 if (bus_id != 0 || unit_id != -1) {
1264 fprintf(stderr,
1265 "qemu: '%s' index cannot be used with bus and unit\n", str);
1266 return -1;
1267 }
1268 if (max_devs == 0)
1269 {
1270 unit_id = index;
1271 bus_id = 0;
1272 } else {
1273 unit_id = index % max_devs;
1274 bus_id = index / max_devs;
1275 }
1276 }
1277
1278 /* if user doesn't specify a unit_id,
1279 * try to find the first free
1280 */
1281
1282 if (unit_id == -1) {
1283 unit_id = 0;
1284 while (drive_get_index(type, bus_id, unit_id) != -1) {
1285 unit_id++;
1286 if (max_devs && unit_id >= max_devs) {
1287 unit_id -= max_devs;
1288 bus_id++;
1289 }
1290 }
1291 }
1292
1293 /* check unit id */
1294
1295 if (max_devs && unit_id >= max_devs) {
1296 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1297 str, unit_id, max_devs - 1);
1298 return -1;
1299 }
1300
1301 /*
1302 * ignore multiple definitions
1303 */
1304
1305 if (drive_get_index(type, bus_id, unit_id) != -1)
1306 return -2;
1307
1308 /* init */
1309
1310 if (type == IF_IDE || type == IF_SCSI)
1311 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1312 if (max_devs)
1313 snprintf(buf, sizeof(buf), "%s%i%s%i",
1314 devname, bus_id, mediastr, unit_id);
1315 else
1316 snprintf(buf, sizeof(buf), "%s%s%i",
1317 devname, mediastr, unit_id);
1318 bdrv = bdrv_new(buf);
1319 drives_table_idx = drive_get_free_idx();
1320 drives_table[drives_table_idx].bdrv = bdrv;
1321 drives_table[drives_table_idx].type = type;
1322 drives_table[drives_table_idx].bus = bus_id;
1323 drives_table[drives_table_idx].unit = unit_id;
1324 drives_table[drives_table_idx].onerror = onerror;
1325 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1326 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1327 nb_drives++;
1328
1329 switch(type) {
1330 case IF_IDE:
1331 case IF_SCSI:
1332 case IF_XEN:
1333 switch(media) {
1334 case MEDIA_DISK:
1335 if (cyls != 0) {
1336 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1337 bdrv_set_translation_hint(bdrv, translation);
1338 }
1339 break;
1340 case MEDIA_CDROM:
1341 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1342 break;
1343 }
1344 break;
1345 case IF_SD:
1346 /* FIXME: This isn't really a floppy, but it's a reasonable
1347 approximation. */
1348 case IF_FLOPPY:
1349 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1350 break;
1351 case IF_PFLASH:
1352 case IF_MTD:
1353 case IF_VIRTIO:
1354 break;
1355 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001356 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001357 abort();
1358 }
1359 if (!file[0])
1360 return -2;
1361 bdrv_flags = 0;
1362 if (snapshot) {
1363 bdrv_flags |= BDRV_O_SNAPSHOT;
1364 cache = 2; /* always use write-back with snapshot */
1365 }
1366 if (cache == 0) /* no caching */
1367 bdrv_flags |= BDRV_O_NOCACHE;
1368 else if (cache == 2) /* write-back */
1369 bdrv_flags |= BDRV_O_CACHE_WB;
1370 else if (cache == 3) /* not specified */
1371 bdrv_flags |= BDRV_O_CACHE_DEF;
1372 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1373 fprintf(stderr, "qemu: could not open disk image %s\n",
1374 file);
1375 return -1;
1376 }
1377 if (bdrv_key_required(bdrv))
1378 autostart = 0;
1379 return drives_table_idx;
1380}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001381#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001382
1383static void numa_add(const char *optarg)
1384{
1385 char option[128];
1386 char *endptr;
1387 unsigned long long value, endvalue;
1388 int nodenr;
1389
1390 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1391 if (!strcmp(option, "node")) {
1392 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1393 nodenr = nb_numa_nodes;
1394 } else {
1395 nodenr = strtoull(option, NULL, 10);
1396 }
1397
1398 if (get_param_value(option, 128, "mem", optarg) == 0) {
1399 node_mem[nodenr] = 0;
1400 } else {
1401 value = strtoull(option, &endptr, 0);
1402 switch (*endptr) {
1403 case 0: case 'M': case 'm':
1404 value <<= 20;
1405 break;
1406 case 'G': case 'g':
1407 value <<= 30;
1408 break;
1409 }
1410 node_mem[nodenr] = value;
1411 }
1412 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1413 node_cpumask[nodenr] = 0;
1414 } else {
1415 value = strtoull(option, &endptr, 10);
1416 if (value >= 64) {
1417 value = 63;
1418 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1419 } else {
1420 if (*endptr == '-') {
1421 endvalue = strtoull(endptr+1, &endptr, 10);
1422 if (endvalue >= 63) {
1423 endvalue = 62;
1424 fprintf(stderr,
1425 "only 63 CPUs in NUMA mode supported.\n");
1426 }
1427 value = (1 << (endvalue + 1)) - (1 << value);
1428 } else {
1429 value = 1 << value;
1430 }
1431 }
1432 node_cpumask[nodenr] = value;
1433 }
1434 nb_numa_nodes++;
1435 }
1436 return;
1437}
1438
1439/***********************************************************/
1440/* USB devices */
1441
1442static USBPort *used_usb_ports;
1443static USBPort *free_usb_ports;
1444
1445/* ??? Maybe change this to register a hub to keep track of the topology. */
1446void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1447 usb_attachfn attach)
1448{
1449 port->opaque = opaque;
1450 port->index = index;
1451 port->attach = attach;
1452 port->next = free_usb_ports;
1453 free_usb_ports = port;
1454}
1455
1456int usb_device_add_dev(USBDevice *dev)
1457{
1458 USBPort *port;
1459
1460 /* Find a USB port to add the device to. */
1461 port = free_usb_ports;
1462 if (!port->next) {
1463 USBDevice *hub;
1464
1465 /* Create a new hub and chain it on. */
1466 free_usb_ports = NULL;
1467 port->next = used_usb_ports;
1468 used_usb_ports = port;
1469
1470 hub = usb_hub_init(VM_USB_HUB_SIZE);
1471 usb_attach(port, hub);
1472 port = free_usb_ports;
1473 }
1474
1475 free_usb_ports = port->next;
1476 port->next = used_usb_ports;
1477 used_usb_ports = port;
1478 usb_attach(port, dev);
1479 return 0;
1480}
1481
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001482#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001483static void usb_msd_password_cb(void *opaque, int err)
1484{
1485 USBDevice *dev = opaque;
1486
1487 if (!err)
1488 usb_device_add_dev(dev);
1489 else
1490 dev->handle_destroy(dev);
1491}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001492#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001493
1494static int usb_device_add(const char *devname, int is_hotplug)
1495{
1496 const char *p;
1497 USBDevice *dev;
1498
1499 if (!free_usb_ports)
1500 return -1;
1501
1502 if (strstart(devname, "host:", &p)) {
1503 dev = usb_host_device_open(p);
1504 } else if (!strcmp(devname, "mouse")) {
1505 dev = usb_mouse_init();
1506 } else if (!strcmp(devname, "tablet")) {
1507 dev = usb_tablet_init();
1508 } else if (!strcmp(devname, "keyboard")) {
1509 dev = usb_keyboard_init();
1510 } else if (strstart(devname, "disk:", &p)) {
1511#if 0
1512 BlockDriverState *bs;
1513#endif
1514 dev = usb_msd_init(p);
1515 if (!dev)
1516 return -1;
1517#if 0
1518 bs = usb_msd_get_bdrv(dev);
1519 if (bdrv_key_required(bs)) {
1520 autostart = 0;
1521 if (is_hotplug) {
1522 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1523 dev);
1524 return 0;
1525 }
1526 }
1527 } else if (!strcmp(devname, "wacom-tablet")) {
1528 dev = usb_wacom_init();
1529 } else if (strstart(devname, "serial:", &p)) {
1530 dev = usb_serial_init(p);
1531#ifdef CONFIG_BRLAPI
1532 } else if (!strcmp(devname, "braille")) {
1533 dev = usb_baum_init();
1534#endif
1535 } else if (strstart(devname, "net:", &p)) {
1536 int nic = nb_nics;
1537
1538 if (net_client_init("nic", p) < 0)
1539 return -1;
1540 nd_table[nic].model = "usb";
1541 dev = usb_net_init(&nd_table[nic]);
1542 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1543 dev = usb_bt_init(devname[2] ? hci_init(p) :
1544 bt_new_hci(qemu_find_bt_vlan(0)));
1545#endif
1546 } else {
1547 return -1;
1548 }
1549 if (!dev)
1550 return -1;
1551
1552 return usb_device_add_dev(dev);
1553}
1554
1555int usb_device_del_addr(int bus_num, int addr)
1556{
1557 USBPort *port;
1558 USBPort **lastp;
1559 USBDevice *dev;
1560
1561 if (!used_usb_ports)
1562 return -1;
1563
1564 if (bus_num != 0)
1565 return -1;
1566
1567 lastp = &used_usb_ports;
1568 port = used_usb_ports;
1569 while (port && port->dev->addr != addr) {
1570 lastp = &port->next;
1571 port = port->next;
1572 }
1573
1574 if (!port)
1575 return -1;
1576
1577 dev = port->dev;
1578 *lastp = port->next;
1579 usb_attach(port, NULL);
1580 dev->handle_destroy(dev);
1581 port->next = free_usb_ports;
1582 free_usb_ports = port;
1583 return 0;
1584}
1585
1586static int usb_device_del(const char *devname)
1587{
1588 int bus_num, addr;
1589 const char *p;
1590
1591 if (strstart(devname, "host:", &p))
1592 return usb_host_device_close(p);
1593
1594 if (!used_usb_ports)
1595 return -1;
1596
1597 p = strchr(devname, '.');
1598 if (!p)
1599 return -1;
1600 bus_num = strtoul(devname, NULL, 0);
1601 addr = strtoul(p + 1, NULL, 0);
1602
1603 return usb_device_del_addr(bus_num, addr);
1604}
1605
1606void do_usb_add(Monitor *mon, const char *devname)
1607{
1608 usb_device_add(devname, 1);
1609}
1610
1611void do_usb_del(Monitor *mon, const char *devname)
1612{
1613 usb_device_del(devname);
1614}
1615
1616void usb_info(Monitor *mon)
1617{
1618 USBDevice *dev;
1619 USBPort *port;
1620 const char *speed_str;
1621
1622 if (!usb_enabled) {
1623 monitor_printf(mon, "USB support not enabled\n");
1624 return;
1625 }
1626
1627 for (port = used_usb_ports; port; port = port->next) {
1628 dev = port->dev;
1629 if (!dev)
1630 continue;
1631 switch(dev->speed) {
1632 case USB_SPEED_LOW:
1633 speed_str = "1.5";
1634 break;
1635 case USB_SPEED_FULL:
1636 speed_str = "12";
1637 break;
1638 case USB_SPEED_HIGH:
1639 speed_str = "480";
1640 break;
1641 default:
1642 speed_str = "?";
1643 break;
1644 }
1645 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1646 0, dev->addr, speed_str, dev->devname);
1647 }
1648}
1649
1650/***********************************************************/
1651/* PCMCIA/Cardbus */
1652
1653static struct pcmcia_socket_entry_s {
1654 PCMCIASocket *socket;
1655 struct pcmcia_socket_entry_s *next;
1656} *pcmcia_sockets = 0;
1657
1658void pcmcia_socket_register(PCMCIASocket *socket)
1659{
1660 struct pcmcia_socket_entry_s *entry;
1661
1662 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1663 entry->socket = socket;
1664 entry->next = pcmcia_sockets;
1665 pcmcia_sockets = entry;
1666}
1667
1668void pcmcia_socket_unregister(PCMCIASocket *socket)
1669{
1670 struct pcmcia_socket_entry_s *entry, **ptr;
1671
1672 ptr = &pcmcia_sockets;
1673 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1674 if (entry->socket == socket) {
1675 *ptr = entry->next;
1676 qemu_free(entry);
1677 }
1678}
1679
1680void pcmcia_info(Monitor *mon)
1681{
1682 struct pcmcia_socket_entry_s *iter;
1683
1684 if (!pcmcia_sockets)
1685 monitor_printf(mon, "No PCMCIA sockets\n");
1686
1687 for (iter = pcmcia_sockets; iter; iter = iter->next)
1688 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1689 iter->socket->attached ? iter->socket->card_string :
1690 "Empty");
1691}
1692
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001693/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001694/* machine registration */
1695
1696static QEMUMachine *first_machine = NULL;
1697QEMUMachine *current_machine = NULL;
1698
1699int qemu_register_machine(QEMUMachine *m)
1700{
1701 QEMUMachine **pm;
1702 pm = &first_machine;
1703 while (*pm != NULL)
1704 pm = &(*pm)->next;
1705 m->next = NULL;
1706 *pm = m;
1707 return 0;
1708}
1709
1710static QEMUMachine *find_machine(const char *name)
1711{
1712 QEMUMachine *m;
1713
1714 for(m = first_machine; m != NULL; m = m->next) {
1715 if (!strcmp(m->name, name))
1716 return m;
1717 }
1718 return NULL;
1719}
1720
1721static QEMUMachine *find_default_machine(void)
1722{
1723 QEMUMachine *m;
1724
1725 for(m = first_machine; m != NULL; m = m->next) {
1726 if (m->is_default) {
1727 return m;
1728 }
1729 }
1730 return NULL;
1731}
1732
1733/***********************************************************/
1734/* main execution loop */
1735
1736static void gui_update(void *opaque)
1737{
1738 uint64_t interval = GUI_REFRESH_INTERVAL;
1739 DisplayState *ds = opaque;
1740 DisplayChangeListener *dcl = ds->listeners;
1741
1742 dpy_refresh(ds);
1743
1744 while (dcl != NULL) {
1745 if (dcl->gui_timer_interval &&
1746 dcl->gui_timer_interval < interval)
1747 interval = dcl->gui_timer_interval;
1748 dcl = dcl->next;
1749 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001750 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001751}
1752
1753static void nographic_update(void *opaque)
1754{
1755 uint64_t interval = GUI_REFRESH_INTERVAL;
1756
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001757 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001758}
1759
1760struct vm_change_state_entry {
1761 VMChangeStateHandler *cb;
1762 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001763 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001764};
1765
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001766static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001767
1768VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1769 void *opaque)
1770{
1771 VMChangeStateEntry *e;
1772
1773 e = qemu_mallocz(sizeof (*e));
1774
1775 e->cb = cb;
1776 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001777 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001778 return e;
1779}
1780
1781void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1782{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001783 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001784 qemu_free (e);
1785}
1786
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001787void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001788{
1789 VMChangeStateEntry *e;
1790
1791 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1792 e->cb(e->opaque, running, reason);
1793 }
1794}
1795
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001796void vm_start(void)
1797{
1798 if (!vm_running) {
1799 cpu_enable_ticks();
1800 vm_running = 1;
1801 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001802 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001803 resume_all_vcpus();
1804 }
1805}
1806
1807/* reset/shutdown handler */
1808
1809typedef struct QEMUResetEntry {
1810 QEMUResetHandler *func;
1811 void *opaque;
1812 int order;
1813 struct QEMUResetEntry *next;
1814} QEMUResetEntry;
1815
1816static QEMUResetEntry *first_reset_entry;
1817static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001818static int shutdown_requested, shutdown_signal = -1;
1819static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001820static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001821int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001822static int vmstop_requested;
1823
1824int qemu_shutdown_requested(void)
1825{
1826 int r = shutdown_requested;
1827 shutdown_requested = 0;
1828 return r;
1829}
1830
1831int qemu_reset_requested(void)
1832{
1833 int r = reset_requested;
1834 reset_requested = 0;
1835 return r;
1836}
1837
1838int qemu_powerdown_requested(void)
1839{
1840 int r = powerdown_requested;
1841 powerdown_requested = 0;
1842 return r;
1843}
1844
1845static int qemu_debug_requested(void)
1846{
1847 int r = debug_requested;
1848 debug_requested = 0;
1849 return r;
1850}
1851
1852static int qemu_vmstop_requested(void)
1853{
1854 int r = vmstop_requested;
1855 vmstop_requested = 0;
1856 return r;
1857}
1858
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001859void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1860{
1861 QEMUResetEntry **pre, *re;
1862
1863 pre = &first_reset_entry;
1864 while (*pre != NULL && (*pre)->order >= order) {
1865 pre = &(*pre)->next;
1866 }
1867 re = qemu_mallocz(sizeof(QEMUResetEntry));
1868 re->func = func;
1869 re->opaque = opaque;
1870 re->order = order;
1871 re->next = NULL;
1872 *pre = re;
1873}
1874
1875void qemu_system_reset(void)
1876{
1877 QEMUResetEntry *re;
1878
1879 /* reset all devices */
1880 for(re = first_reset_entry; re != NULL; re = re->next) {
1881 re->func(re->opaque);
1882 }
1883}
1884
1885void qemu_system_reset_request(void)
1886{
1887 if (no_reboot) {
1888 shutdown_requested = 1;
1889 } else {
1890 reset_requested = 1;
1891 }
1892 qemu_notify_event();
1893}
1894
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001895void qemu_system_killed(int signal, pid_t pid)
1896{
1897 shutdown_signal = signal;
1898 shutdown_pid = pid;
1899 qemu_system_shutdown_request();
1900}
1901
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001902void qemu_system_shutdown_request(void)
1903{
1904 shutdown_requested = 1;
1905 qemu_notify_event();
1906}
1907
1908void qemu_system_powerdown_request(void)
1909{
1910 powerdown_requested = 1;
1911 qemu_notify_event();
1912}
1913
1914#ifdef CONFIG_IOTHREAD
1915static void qemu_system_vmstop_request(int reason)
1916{
1917 vmstop_requested = reason;
1918 qemu_notify_event();
1919}
1920#endif
1921
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001922void main_loop_wait(int timeout)
1923{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001924 fd_set rfds, wfds, xfds;
1925 int ret, nfds;
1926 struct timeval tv;
1927
1928 qemu_bh_update_timeout(&timeout);
1929
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001930 os_host_main_loop_wait(&timeout);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001931
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001932
1933 tv.tv_sec = timeout / 1000;
1934 tv.tv_usec = (timeout % 1000) * 1000;
1935
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001936 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001937
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001938 /* XXX: separate device handlers from system ones */
1939 nfds = -1;
1940 FD_ZERO(&rfds);
1941 FD_ZERO(&wfds);
1942 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001943 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001944 if (slirp_is_inited()) {
1945 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1946 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001947
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001948 qemu_mutex_unlock_iothread();
1949 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1950 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001951 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001952 if (slirp_is_inited()) {
1953 if (ret < 0) {
1954 FD_ZERO(&rfds);
1955 FD_ZERO(&wfds);
1956 FD_ZERO(&xfds);
1957 }
1958 slirp_select_poll(&rfds, &wfds, &xfds);
1959 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001960 charpipe_poll();
1961
David Turner6a9ef172010-09-09 22:54:36 +02001962 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07001963
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001964 /* Check bottom-halves last in case any of the earlier events triggered
1965 them. */
1966 qemu_bh_poll();
1967
1968}
1969
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001970static int vm_can_run(void)
1971{
1972 if (powerdown_requested)
1973 return 0;
1974 if (reset_requested)
1975 return 0;
1976 if (shutdown_requested)
1977 return 0;
1978 if (debug_requested)
1979 return 0;
1980 return 1;
1981}
1982
1983static void main_loop(void)
1984{
1985 int r;
1986
1987#ifdef CONFIG_IOTHREAD
1988 qemu_system_ready = 1;
1989 qemu_cond_broadcast(&qemu_system_cond);
1990#endif
1991
1992 for (;;) {
1993 do {
1994#ifdef CONFIG_PROFILER
1995 int64_t ti;
1996#endif
1997#ifndef CONFIG_IOTHREAD
1998 tcg_cpu_exec();
1999#endif
2000#ifdef CONFIG_PROFILER
2001 ti = profile_getclock();
2002#endif
2003 main_loop_wait(qemu_calculate_timeout());
2004#ifdef CONFIG_PROFILER
2005 dev_time += profile_getclock() - ti;
2006#endif
rich canningsd952f282011-03-01 15:40:09 -08002007
2008 if (rotate_logs_requested) {
2009 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
2010 dns_log_filename);
2011 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
2012 drop_log_filename);
2013 slirp_dns_log_fd(new_dns_log_fd);
2014 slirp_drop_log_fd(new_drop_log_fd);
2015 reset_rotate_qemu_logs_request();
2016 }
2017
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002018 } while (vm_can_run());
2019
2020 if (qemu_debug_requested())
2021 vm_stop(EXCP_DEBUG);
2022 if (qemu_shutdown_requested()) {
2023 if (no_shutdown) {
2024 vm_stop(0);
2025 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002026 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00002027 if (savevm_on_exit != NULL) {
2028 do_savevm(cur_mon, savevm_on_exit);
2029 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002030 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002031 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002032 }
2033 if (qemu_reset_requested()) {
2034 pause_all_vcpus();
2035 qemu_system_reset();
2036 resume_all_vcpus();
2037 }
2038 if (qemu_powerdown_requested())
2039 qemu_system_powerdown();
2040 if ((r = qemu_vmstop_requested()))
2041 vm_stop(r);
2042 }
2043 pause_all_vcpus();
2044}
2045
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002046void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002047{
2048 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2049}
2050
2051void qemu_help(int exitcode)
2052{
2053 version();
2054 printf("usage: %s [options] [disk_image]\n"
2055 "\n"
2056 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2057 "\n"
2058#define DEF(option, opt_arg, opt_enum, opt_help) \
2059 opt_help
2060#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002061#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002062#undef DEF
2063#undef DEFHEADING
2064#undef GEN_DOCS
2065 "\n"
2066 "During emulation, the following keys are useful:\n"
2067 "ctrl-alt-f toggle full screen\n"
2068 "ctrl-alt-n switch to virtual console 'n'\n"
2069 "ctrl-alt toggle mouse and keyboard grab\n"
2070 "\n"
2071 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2072 ,
2073 "qemu",
2074 DEFAULT_RAM_SIZE,
2075#ifndef _WIN32
2076 DEFAULT_NETWORK_SCRIPT,
2077 DEFAULT_NETWORK_DOWN_SCRIPT,
2078#endif
2079 DEFAULT_GDBSTUB_PORT,
2080 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002081 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002082}
2083
2084#define HAS_ARG 0x0001
2085
2086enum {
2087#define DEF(option, opt_arg, opt_enum, opt_help) \
2088 opt_enum,
2089#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002090#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002091#undef DEF
2092#undef DEFHEADING
2093#undef GEN_DOCS
2094};
2095
2096typedef struct QEMUOption {
2097 const char *name;
2098 int flags;
2099 int index;
2100} QEMUOption;
2101
2102static const QEMUOption qemu_options[] = {
2103 { "h", 0, QEMU_OPTION_h },
2104#define DEF(option, opt_arg, opt_enum, opt_help) \
2105 { option, opt_arg, opt_enum },
2106#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002107#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002108#undef DEF
2109#undef DEFHEADING
2110#undef GEN_DOCS
2111 { NULL, 0, 0 },
2112};
2113
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002114static void select_vgahw (const char *p)
2115{
2116 const char *opts;
2117
2118 cirrus_vga_enabled = 0;
2119 std_vga_enabled = 0;
2120 vmsvga_enabled = 0;
2121 xenfb_enabled = 0;
2122 if (strstart(p, "std", &opts)) {
2123 std_vga_enabled = 1;
2124 } else if (strstart(p, "cirrus", &opts)) {
2125 cirrus_vga_enabled = 1;
2126 } else if (strstart(p, "vmware", &opts)) {
2127 vmsvga_enabled = 1;
2128 } else if (strstart(p, "xenfb", &opts)) {
2129 xenfb_enabled = 1;
2130 } else if (!strstart(p, "none", &opts)) {
2131 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002132 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002133 }
2134 while (*opts) {
2135 const char *nextopt;
2136
2137 if (strstart(opts, ",retrace=", &nextopt)) {
2138 opts = nextopt;
2139 if (strstart(opts, "dumb", &nextopt))
2140 vga_retrace_method = VGA_RETRACE_DUMB;
2141 else if (strstart(opts, "precise", &nextopt))
2142 vga_retrace_method = VGA_RETRACE_PRECISE;
2143 else goto invalid_vga;
2144 } else goto invalid_vga;
2145 opts = nextopt;
2146 }
2147}
2148
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002149#define MAX_NET_CLIENTS 32
2150
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002151#ifdef _WIN32
2152/* Look for support files in the same directory as the executable. */
2153static char *find_datadir(const char *argv0)
2154{
2155 char *p;
2156 char buf[MAX_PATH];
2157 DWORD len;
2158
2159 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2160 if (len == 0) {
2161 return NULL;
2162 }
2163
2164 buf[len] = 0;
2165 p = buf + len - 1;
2166 while (p != buf && *p != '\\')
2167 p--;
2168 *p = 0;
2169 if (access(buf, R_OK) == 0) {
2170 return qemu_strdup(buf);
2171 }
2172 return NULL;
2173}
2174#else /* !_WIN32 */
2175
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002176/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002177static char *find_datadir(const char *argv0)
2178{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002179 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002180 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002181
2182#if defined(__linux__)
2183 {
2184 int len;
2185 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2186 if (len > 0) {
2187 buf[len] = 0;
2188 p = buf;
2189 }
2190 }
2191#elif defined(__FreeBSD__)
2192 {
2193 int len;
2194 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2195 if (len > 0) {
2196 buf[len] = 0;
2197 p = buf;
2198 }
2199 }
2200#endif
2201 /* If we don't have any way of figuring out the actual executable
2202 location then try argv[0]. */
2203 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002204 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002205 if (!p) {
2206 return NULL;
2207 }
2208 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002209
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002210 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002211}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002212#endif
2213
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002214static char*
2215qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
2216{
2217 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2218 char* buf = qemu_mallocz(len);
2219
2220 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2221 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2222 if (access(buf, R_OK)) {
2223 qemu_free(buf);
2224 return NULL;
2225 }
2226 return buf;
2227}
2228
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002229char *qemu_find_file(int type, const char *name)
2230{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002231 const char *subdir;
2232 char *buf;
2233
2234 /* If name contains path separators then try it as a straight path. */
2235 if ((strchr(name, '/') || strchr(name, '\\'))
2236 && access(name, R_OK) == 0) {
2237 return strdup(name);
2238 }
2239 switch (type) {
2240 case QEMU_FILE_TYPE_BIOS:
2241 subdir = "";
2242 break;
2243 case QEMU_FILE_TYPE_KEYMAP:
2244 subdir = "keymaps/";
2245 break;
2246 default:
2247 abort();
2248 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002249 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2250#ifdef CONFIG_ANDROID
2251 if (type == QEMU_FILE_TYPE_BIOS) {
2252 /* This case corresponds to the emulator being used as part of an
2253 * SDK installation. NOTE: data_dir is really $bindir. */
2254 if (buf == NULL)
2255 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2256 /* This case corresponds to platform builds. */
2257 if (buf == NULL)
2258 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2259 /* Finally, try this for standalone builds under external/qemu */
2260 if (buf == NULL)
2261 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002262 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002263#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002264 return buf;
2265}
2266
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002267static int
2268add_dns_server( const char* server_name )
2269{
2270 SockAddress addr;
2271
2272 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2273 fprintf(stdout,
2274 "### WARNING: can't resolve DNS server name '%s'\n",
2275 server_name );
2276 return -1;
2277 }
2278
2279 fprintf(stderr,
2280 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2281
2282 if ( slirp_add_dns_server( &addr ) < 0 ) {
2283 fprintf(stderr,
2284 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2285 return -1;
2286 }
2287 return 0;
2288}
2289
rich cannings7339b552011-02-16 13:43:44 -08002290/* Parses an integer
2291 * Pararm:
2292 * str String containing a number to be parsed.
2293 * result Passes the parsed integer in this argument
2294 * returns 0 if ok, -1 if failed
2295 */
2296int
2297parse_int(const char *str, int *result)
2298{
2299 char* r;
2300 *result = strtol(str, &r, 0);
2301 if (r == NULL || *r != '\0')
2302 return -1;
2303
2304 return 0;
2305}
2306
rich canningsd952f282011-03-01 15:40:09 -08002307#ifndef _WIN32
2308/*
2309 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
2310 */
2311void init_qemu_clear_logs_sig() {
2312 struct sigaction act;
2313 sigfillset(&act.sa_mask);
2314 act.sa_flags = 0;
2315 act.sa_handler = rotate_qemu_logs_handler;
2316 if (sigaction(SIGUSR1, &act, NULL) == -1) {
2317 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
2318 exit(-1);
2319 }
2320}
2321#endif
2322
2323
rich cannings7339b552011-02-16 13:43:44 -08002324
2325/* parses a null-terminated string specifying a network port (e.g., "80") or
2326 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2327 * are the same. Returns 0 on success, -1 on error. */
2328
2329int parse_port_range(const char *str, unsigned short *lport,
2330 unsigned short *hport) {
2331
2332 unsigned int low = 0, high = 0;
2333 char *p, *arg = strdup(str);
2334
2335 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2336 p = arg + 1; /* skip '[' */
2337 low = atoi(strtok(p, "-"));
2338 high = atoi(strtok(NULL, "-"));
2339 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2340 *lport = low;
2341 *hport = high;
2342 }
2343 }
2344 else {
2345 low = atoi(arg);
2346 if ((0 < low) && (low < 65535)) {
2347 *lport = low;
2348 *hport = low;
2349 }
2350 }
2351 free(arg);
2352 if (low != 0)
2353 return 0;
2354 return -1;
2355}
2356
2357/*
2358 * Implements the generic port forwarding option
2359 */
2360void
2361net_slirp_forward(const char *optarg)
2362{
2363 /*
2364 * we expect the following format:
2365 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2366 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2367 */
2368 char *argument = strdup(optarg), *p = argument;
2369 char *dst_net, *dst_mask, *dst_port;
2370 char *redirect_ip, *redirect_port;
2371 uint32_t dnet, dmask, rip;
2372 unsigned short dlport, dhport, rport;
2373
2374
2375 dst_net = strtok(p, ":");
2376 dst_mask = strtok(NULL, ":");
2377 dst_port = strtok(NULL, ":");
2378 redirect_ip = strtok(NULL, ":");
2379 redirect_port = strtok(NULL, ":");
2380
2381 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2382 redirect_ip == NULL || redirect_port == NULL) {
2383 fprintf(stderr,
2384 "Invalid argument for -net-forward, we expect "
2385 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2386 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2387 ":redirect_ip:redirect_port: %s\n",
2388 optarg);
2389 exit(1);
2390 }
2391
2392 /* inet_strtoip converts dotted address to host byte order */
2393 if (inet_strtoip(dst_net, &dnet) == -1) {
2394 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2395 exit(1);
2396 }
2397 if (inet_strtoip(dst_mask, &dmask) == -1) {
2398 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2399 exit(1);
2400 }
2401 if (inet_strtoip(redirect_ip, &rip) == -1) {
2402 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2403 exit(1);
2404 }
2405
2406 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2407 fprintf(stderr, "Invalid destination port or port range\n");
2408 exit(1);
2409 }
2410
2411 rport = atoi(redirect_port);
2412 if (!rport) {
2413 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2414 exit(1);
2415 }
2416
2417 dnet &= dmask;
2418
2419 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2420 rip, rport);
2421
2422 free(argument);
2423}
2424
2425
2426/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2427 * entry in the allows list */
2428void
2429slirp_allow(const char *optarg, u_int8_t proto)
2430{
2431 /*
2432 * we expect the following format:
2433 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2434 */
2435 char *argument = strdup(optarg), *p = argument;
2436 char *dst_ip_str, *dst_port_str;
2437 uint32_t dst_ip;
2438 unsigned short dst_lport, dst_hport;
2439
2440 dst_ip_str = strtok(p, ":");
2441 dst_port_str = strtok(NULL, ":");
2442
2443 if (dst_ip_str == NULL || dst_port_str == NULL) {
2444 fprintf(stderr,
2445 "Invalid argument %s for -allow. We expect "
2446 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2447 optarg);
2448 exit(1);
2449 }
2450
2451 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2452 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2453 exit(1);
2454 }
2455 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2456 fprintf(stderr, "Invalid destination port or port range\n");
2457 exit(1);
2458 }
2459
2460 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2461
2462 free(argument);
2463}
2464
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002465/* Add a serial device at a given location in the emulated hardware table.
2466 * On failure, this function aborts the program with an error message.
2467 */
2468static void
2469serial_hds_add_at(int index, const char* devname)
2470{
2471 char label[32];
2472
2473 if (!devname || !strcmp(devname,"none"))
2474 return;
2475
2476 if (index >= MAX_SERIAL_PORTS) {
2477 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2478 devname, index, MAX_SERIAL_PORTS);
2479 }
2480 if (serial_hds[index] != NULL) {
2481 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2482 devname, index);
2483 }
2484 snprintf(label, sizeof(label), "serial%d", index);
2485 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2486 if (!serial_hds[index]) {
2487 PANIC("qemu: could not open serial device '%s'", devname);
2488 }
2489}
2490
2491
2492/* Find a free slot in the emulated serial device table, and register
2493 * it. Return the allocated table index.
2494 */
2495static int
2496serial_hds_add(const char* devname)
2497{
2498 int index;
2499
2500 /* Find first free slot */
2501 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2502 if (serial_hds[index] == NULL) {
2503 serial_hds_add_at(index, devname);
2504 return index;
2505 }
2506 }
2507
2508 PANIC("qemu: too many serial devices registered (%d)", index);
2509 return -1; /* shouldn't happen */
2510}
2511
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002512int main(int argc, char **argv, char **envp)
2513{
2514 const char *gdbstub_dev = NULL;
2515 uint32_t boot_devices_bitmap = 0;
2516 int i;
2517 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002518 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002519 const char *initrd_filename;
2520 const char *kernel_filename, *kernel_cmdline;
2521 const char *boot_devices = "";
2522 DisplayState *ds;
2523 DisplayChangeListener *dcl;
2524 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002525 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002526 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002527 const char *net_clients[MAX_NET_CLIENTS];
2528 int nb_net_clients;
2529 const char *bt_opts[MAX_BT_CMDLINE];
2530 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002531 int optind;
2532 const char *r, *optarg;
2533 CharDriverState *monitor_hd = NULL;
2534 const char *monitor_device;
2535 const char *serial_devices[MAX_SERIAL_PORTS];
2536 int serial_device_index;
2537 const char *parallel_devices[MAX_PARALLEL_PORTS];
2538 int parallel_device_index;
2539 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2540 int virtio_console_index;
2541 const char *loadvm = NULL;
2542 QEMUMachine *machine;
2543 const char *cpu_model;
2544 const char *usb_devices[MAX_USB_CMDLINE];
2545 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002546 int tb_size;
2547 const char *pid_file = NULL;
2548 const char *incoming = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002549 CPUState *env;
2550 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002551 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002552 STRALLOC_DEFINE(kernel_params);
2553 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002554 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002555
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002556 /* Initialize sockets before anything else, so we can properly report
2557 * initialization failures back to the UI. */
2558#ifdef _WIN32
2559 socket_init();
2560#endif
2561
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002562 init_clocks();
2563
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002564 qemu_cache_utils_init(envp);
2565
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002566 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002567 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002568
2569 module_call_init(MODULE_INIT_MACHINE);
2570 machine = find_default_machine();
2571 cpu_model = NULL;
2572 initrd_filename = NULL;
2573 ram_size = 0;
2574 snapshot = 0;
2575 kernel_filename = NULL;
2576 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002577
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002578 cyls = heads = secs = 0;
2579 translation = BIOS_ATA_TRANSLATION_AUTO;
2580 monitor_device = "vc:80Cx24C";
2581
2582 serial_devices[0] = "vc:80Cx24C";
2583 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2584 serial_devices[i] = NULL;
2585 serial_device_index = 0;
2586
2587 parallel_devices[0] = "vc:80Cx24C";
2588 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2589 parallel_devices[i] = NULL;
2590 parallel_device_index = 0;
2591
2592 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2593 virtio_consoles[i] = NULL;
2594 virtio_console_index = 0;
2595
2596 for (i = 0; i < MAX_NODES; i++) {
2597 node_mem[i] = 0;
2598 node_cpumask[i] = 0;
2599 }
2600
2601 usb_devices_index = 0;
2602
2603 nb_net_clients = 0;
2604 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002605#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002606 nb_drives = 0;
2607 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002608#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002609 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002610
2611 nb_nics = 0;
2612
2613 tb_size = 0;
2614 autostart= 1;
2615
2616 register_watchdogs();
2617
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002618 /* Initialize boot properties. */
2619 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002620 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002621 android_net_pipes_init();
Vladimir Chtchetkinec646f5e2011-09-03 15:17:13 -07002622 android_camera_service_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002623
David 'Digit' Turner36597752011-05-20 01:18:01 +02002624#ifdef CONFIG_KVM
2625 /* By default, force auto-detection for kvm */
2626 kvm_allowed = -1;
2627#endif
2628
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002629 optind = 1;
2630 for(;;) {
2631 if (optind >= argc)
2632 break;
2633 r = argv[optind];
2634 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002635 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002636 } else {
2637 const QEMUOption *popt;
2638
2639 optind++;
2640 /* Treat --foo the same as -foo. */
2641 if (r[1] == '-')
2642 r++;
2643 popt = qemu_options;
2644 for(;;) {
2645 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002646 PANIC("%s: invalid option -- '%s'",
2647 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002648 }
2649 if (!strcmp(popt->name, r + 1))
2650 break;
2651 popt++;
2652 }
2653 if (popt->flags & HAS_ARG) {
2654 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002655 PANIC("%s: option '%s' requires an argument",
2656 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002657 }
2658 optarg = argv[optind++];
2659 } else {
2660 optarg = NULL;
2661 }
2662
2663 switch(popt->index) {
2664 case QEMU_OPTION_M:
2665 machine = find_machine(optarg);
2666 if (!machine) {
2667 QEMUMachine *m;
2668 printf("Supported machines are:\n");
2669 for(m = first_machine; m != NULL; m = m->next) {
2670 printf("%-10s %s%s\n",
2671 m->name, m->desc,
2672 m->is_default ? " (default)" : "");
2673 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002674 if (*optarg != '?') {
2675 PANIC("Invalid machine parameter: %s",
2676 optarg);
2677 } else {
2678 QEMU_EXIT(0);
2679 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002680 }
2681 break;
2682 case QEMU_OPTION_cpu:
2683 /* hw initialization will check this */
2684 if (*optarg == '?') {
2685/* XXX: implement xxx_cpu_list for targets that still miss it */
2686#if defined(cpu_list)
2687 cpu_list(stdout, &fprintf);
2688#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002689 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002690 } else {
2691 cpu_model = optarg;
2692 }
2693 break;
2694 case QEMU_OPTION_initrd:
2695 initrd_filename = optarg;
2696 break;
2697 case QEMU_OPTION_hda:
2698 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002699 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002700 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002701 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002702 ",cyls=%d,heads=%d,secs=%d%s",
2703 0, cyls, heads, secs,
2704 translation == BIOS_ATA_TRANSLATION_LBA ?
2705 ",trans=lba" :
2706 translation == BIOS_ATA_TRANSLATION_NONE ?
2707 ",trans=none" : "");
2708 break;
2709 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002710 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2711 break;
2712
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002713 case QEMU_OPTION_hdc:
2714 case QEMU_OPTION_hdd:
2715 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2716 break;
2717 case QEMU_OPTION_drive:
2718 drive_add(NULL, "%s", optarg);
2719 break;
2720 case QEMU_OPTION_mtdblock:
2721 drive_add(optarg, MTD_ALIAS);
2722 break;
2723 case QEMU_OPTION_sd:
2724 drive_add(optarg, SD_ALIAS);
2725 break;
2726 case QEMU_OPTION_pflash:
2727 drive_add(optarg, PFLASH_ALIAS);
2728 break;
2729 case QEMU_OPTION_snapshot:
2730 snapshot = 1;
2731 break;
2732 case QEMU_OPTION_hdachs:
2733 {
2734 const char *p;
2735 p = optarg;
2736 cyls = strtol(p, (char **)&p, 0);
2737 if (cyls < 1 || cyls > 16383)
2738 goto chs_fail;
2739 if (*p != ',')
2740 goto chs_fail;
2741 p++;
2742 heads = strtol(p, (char **)&p, 0);
2743 if (heads < 1 || heads > 16)
2744 goto chs_fail;
2745 if (*p != ',')
2746 goto chs_fail;
2747 p++;
2748 secs = strtol(p, (char **)&p, 0);
2749 if (secs < 1 || secs > 63)
2750 goto chs_fail;
2751 if (*p == ',') {
2752 p++;
2753 if (!strcmp(p, "none"))
2754 translation = BIOS_ATA_TRANSLATION_NONE;
2755 else if (!strcmp(p, "lba"))
2756 translation = BIOS_ATA_TRANSLATION_LBA;
2757 else if (!strcmp(p, "auto"))
2758 translation = BIOS_ATA_TRANSLATION_AUTO;
2759 else
2760 goto chs_fail;
2761 } else if (*p != '\0') {
2762 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002763 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002764 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002765 if (hda_opts != NULL) {
2766 char num[16];
2767 snprintf(num, sizeof(num), "%d", cyls);
2768 qemu_opt_set(hda_opts, "cyls", num);
2769 snprintf(num, sizeof(num), "%d", heads);
2770 qemu_opt_set(hda_opts, "heads", num);
2771 snprintf(num, sizeof(num), "%d", secs);
2772 qemu_opt_set(hda_opts, "secs", num);
2773 if (translation == BIOS_ATA_TRANSLATION_LBA)
2774 qemu_opt_set(hda_opts, "trans", "lba");
2775 if (translation == BIOS_ATA_TRANSLATION_NONE)
2776 qemu_opt_set(hda_opts, "trans", "none");
2777 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002778 }
2779 break;
2780 case QEMU_OPTION_numa:
2781 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002782 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002783 }
2784 numa_add(optarg);
2785 break;
2786 case QEMU_OPTION_nographic:
2787 display_type = DT_NOGRAPHIC;
2788 break;
2789#ifdef CONFIG_CURSES
2790 case QEMU_OPTION_curses:
2791 display_type = DT_CURSES;
2792 break;
2793#endif
2794 case QEMU_OPTION_portrait:
2795 graphic_rotate = 1;
2796 break;
2797 case QEMU_OPTION_kernel:
2798 kernel_filename = optarg;
2799 break;
2800 case QEMU_OPTION_append:
2801 kernel_cmdline = optarg;
2802 break;
2803 case QEMU_OPTION_cdrom:
2804 drive_add(optarg, CDROM_ALIAS);
2805 break;
2806 case QEMU_OPTION_boot:
2807 boot_devices = optarg;
2808 /* We just do some generic consistency checks */
2809 {
2810 /* Could easily be extended to 64 devices if needed */
2811 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002812
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002813 boot_devices_bitmap = 0;
2814 for (p = boot_devices; *p != '\0'; p++) {
2815 /* Allowed boot devices are:
2816 * a b : floppy disk drives
2817 * c ... f : IDE disk drives
2818 * g ... m : machine implementation dependant drives
2819 * n ... p : network devices
2820 * It's up to each machine implementation to check
2821 * if the given boot devices match the actual hardware
2822 * implementation and firmware features.
2823 */
2824 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002825 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002826 }
2827 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002828 PANIC(
2829 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002830 }
2831 boot_devices_bitmap |= 1 << (*p - 'a');
2832 }
2833 }
2834 break;
2835 case QEMU_OPTION_fda:
2836 case QEMU_OPTION_fdb:
2837 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2838 break;
2839#ifdef TARGET_I386
2840 case QEMU_OPTION_no_fd_bootchk:
2841 fd_bootchk = 0;
2842 break;
2843#endif
2844 case QEMU_OPTION_net:
2845 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002846 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002847 }
2848 net_clients[nb_net_clients] = optarg;
2849 nb_net_clients++;
2850 break;
2851#ifdef CONFIG_SLIRP
2852 case QEMU_OPTION_tftp:
2853 tftp_prefix = optarg;
2854 break;
2855 case QEMU_OPTION_bootp:
2856 bootp_filename = optarg;
2857 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002858 case QEMU_OPTION_redir:
2859 net_slirp_redir(NULL, optarg, NULL);
2860 break;
2861#endif
2862 case QEMU_OPTION_bt:
2863 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002864 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002865 }
2866 bt_opts[nb_bt_opts++] = optarg;
2867 break;
2868#ifdef HAS_AUDIO
2869 case QEMU_OPTION_audio_help:
2870 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002871 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002872 break;
2873 case QEMU_OPTION_soundhw:
2874 select_soundhw (optarg);
2875 break;
2876#endif
2877 case QEMU_OPTION_h:
2878 qemu_help(0);
2879 break;
2880 case QEMU_OPTION_version:
2881 version();
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_m: {
2885 uint64_t value;
2886 char *ptr;
2887
2888 value = strtoul(optarg, &ptr, 10);
2889 switch (*ptr) {
2890 case 0: case 'M': case 'm':
2891 value <<= 20;
2892 break;
2893 case 'G': case 'g':
2894 value <<= 30;
2895 break;
2896 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002897 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002898 }
2899
2900 /* On 32-bit hosts, QEMU is limited by virtual address space */
2901 if (value > (2047 << 20)
2902#ifndef CONFIG_KQEMU
2903 && HOST_LONG_BITS == 32
2904#endif
2905 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002906 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002907 }
2908 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002909 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002910 }
2911 ram_size = value;
2912 break;
2913 }
2914 case QEMU_OPTION_d:
2915 {
2916 int mask;
2917 const CPULogItem *item;
2918
2919 mask = cpu_str_to_log_mask(optarg);
2920 if (!mask) {
2921 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002922 for(item = cpu_log_items; item->mask != 0; item++) {
2923 printf("%-10s %s\n", item->name, item->help);
2924 }
2925 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002926 }
2927 cpu_set_log(mask);
2928 }
2929 break;
2930 case QEMU_OPTION_s:
2931 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2932 break;
2933 case QEMU_OPTION_gdb:
2934 gdbstub_dev = optarg;
2935 break;
2936 case QEMU_OPTION_L:
2937 data_dir = optarg;
2938 break;
2939 case QEMU_OPTION_bios:
2940 bios_name = optarg;
2941 break;
2942 case QEMU_OPTION_singlestep:
2943 singlestep = 1;
2944 break;
2945 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002946 autostart = 0;
2947 break;
2948#ifndef _WIN32
2949 case QEMU_OPTION_k:
2950 keyboard_layout = optarg;
2951 break;
2952#endif
2953 case QEMU_OPTION_localtime:
2954 rtc_utc = 0;
2955 break;
2956 case QEMU_OPTION_vga:
2957 select_vgahw (optarg);
2958 break;
2959#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2960 case QEMU_OPTION_g:
2961 {
2962 const char *p;
2963 int w, h, depth;
2964 p = optarg;
2965 w = strtol(p, (char **)&p, 10);
2966 if (w <= 0) {
2967 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002968 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002969 }
2970 if (*p != 'x')
2971 goto graphic_error;
2972 p++;
2973 h = strtol(p, (char **)&p, 10);
2974 if (h <= 0)
2975 goto graphic_error;
2976 if (*p == 'x') {
2977 p++;
2978 depth = strtol(p, (char **)&p, 10);
2979 if (depth != 8 && depth != 15 && depth != 16 &&
2980 depth != 24 && depth != 32)
2981 goto graphic_error;
2982 } else if (*p == '\0') {
2983 depth = graphic_depth;
2984 } else {
2985 goto graphic_error;
2986 }
2987
2988 graphic_width = w;
2989 graphic_height = h;
2990 graphic_depth = depth;
2991 }
2992 break;
2993#endif
2994 case QEMU_OPTION_echr:
2995 {
2996 char *r;
2997 term_escape_char = strtol(optarg, &r, 0);
2998 if (r == optarg)
2999 printf("Bad argument to echr\n");
3000 break;
3001 }
3002 case QEMU_OPTION_monitor:
3003 monitor_device = optarg;
3004 break;
3005 case QEMU_OPTION_serial:
3006 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003007 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003008 }
3009 serial_devices[serial_device_index] = optarg;
3010 serial_device_index++;
3011 break;
3012 case QEMU_OPTION_watchdog:
3013 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003014 if (i > 0) {
3015 if (i == 1) {
3016 PANIC("Invalid watchdog parameter: %s",
3017 optarg);
3018 } else {
3019 QEMU_EXIT(0);
3020 }
3021 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003022 break;
3023 case QEMU_OPTION_watchdog_action:
3024 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003025 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003026 }
3027 break;
3028 case QEMU_OPTION_virtiocon:
3029 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003030 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003031 }
3032 virtio_consoles[virtio_console_index] = optarg;
3033 virtio_console_index++;
3034 break;
3035 case QEMU_OPTION_parallel:
3036 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003037 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003038 }
3039 parallel_devices[parallel_device_index] = optarg;
3040 parallel_device_index++;
3041 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003042 case QEMU_OPTION_loadvm:
3043 loadvm = optarg;
3044 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003045 case QEMU_OPTION_savevm_on_exit:
3046 savevm_on_exit = optarg;
3047 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003048 case QEMU_OPTION_full_screen:
3049 full_screen = 1;
3050 break;
3051#ifdef CONFIG_SDL
3052 case QEMU_OPTION_no_frame:
3053 no_frame = 1;
3054 break;
3055 case QEMU_OPTION_alt_grab:
3056 alt_grab = 1;
3057 break;
3058 case QEMU_OPTION_no_quit:
3059 no_quit = 1;
3060 break;
3061 case QEMU_OPTION_sdl:
3062 display_type = DT_SDL;
3063 break;
3064#endif
3065 case QEMU_OPTION_pidfile:
3066 pid_file = optarg;
3067 break;
3068#ifdef TARGET_I386
3069 case QEMU_OPTION_win2k_hack:
3070 win2k_install_hack = 1;
3071 break;
3072 case QEMU_OPTION_rtc_td_hack:
3073 rtc_td_hack = 1;
3074 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003075#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003076 case QEMU_OPTION_acpitable:
3077 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003078 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003079 }
3080 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003081#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003082 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02003083 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003084 break;
3085#endif
3086#ifdef CONFIG_KVM
3087 case QEMU_OPTION_enable_kvm:
3088 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003089 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02003090 case QEMU_OPTION_disable_kvm:
3091 kvm_allowed = 0;
3092 break;
3093#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003094 case QEMU_OPTION_usb:
3095 usb_enabled = 1;
3096 break;
3097 case QEMU_OPTION_usbdevice:
3098 usb_enabled = 1;
3099 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003100 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003101 }
3102 usb_devices[usb_devices_index] = optarg;
3103 usb_devices_index++;
3104 break;
3105 case QEMU_OPTION_smp:
3106 smp_cpus = atoi(optarg);
3107 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003108 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003109 }
3110 break;
3111 case QEMU_OPTION_vnc:
3112 display_type = DT_VNC;
3113 vnc_display = optarg;
3114 break;
3115#ifdef TARGET_I386
3116 case QEMU_OPTION_no_acpi:
3117 acpi_enabled = 0;
3118 break;
3119 case QEMU_OPTION_no_hpet:
3120 no_hpet = 1;
3121 break;
3122 case QEMU_OPTION_no_virtio_balloon:
3123 no_virtio_balloon = 1;
3124 break;
3125#endif
3126 case QEMU_OPTION_no_reboot:
3127 no_reboot = 1;
3128 break;
3129 case QEMU_OPTION_no_shutdown:
3130 no_shutdown = 1;
3131 break;
3132 case QEMU_OPTION_show_cursor:
3133 cursor_hide = 0;
3134 break;
3135 case QEMU_OPTION_uuid:
3136 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003137 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003138 }
3139 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003140 case QEMU_OPTION_option_rom:
3141 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003142 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003143 }
3144 option_rom[nb_option_roms] = optarg;
3145 nb_option_roms++;
3146 break;
3147#if defined(TARGET_ARM) || defined(TARGET_M68K)
3148 case QEMU_OPTION_semihosting:
3149 semihosting_enabled = 1;
3150 break;
3151#endif
3152 case QEMU_OPTION_name:
3153 qemu_name = optarg;
3154 break;
3155#if defined(TARGET_SPARC) || defined(TARGET_PPC)
3156 case QEMU_OPTION_prom_env:
3157 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003158 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003159 }
3160 prom_envs[nb_prom_envs] = optarg;
3161 nb_prom_envs++;
3162 break;
3163#endif
3164#ifdef TARGET_ARM
3165 case QEMU_OPTION_old_param:
3166 old_param = 1;
3167 break;
3168#endif
3169 case QEMU_OPTION_clock:
3170 configure_alarms(optarg);
3171 break;
3172 case QEMU_OPTION_startdate:
3173 {
3174 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02003175 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003176 if (!strcmp(optarg, "now")) {
3177 rtc_date_offset = -1;
3178 } else {
3179 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
3180 &tm.tm_year,
3181 &tm.tm_mon,
3182 &tm.tm_mday,
3183 &tm.tm_hour,
3184 &tm.tm_min,
3185 &tm.tm_sec) == 6) {
3186 /* OK */
3187 } else if (sscanf(optarg, "%d-%d-%d",
3188 &tm.tm_year,
3189 &tm.tm_mon,
3190 &tm.tm_mday) == 3) {
3191 tm.tm_hour = 0;
3192 tm.tm_min = 0;
3193 tm.tm_sec = 0;
3194 } else {
3195 goto date_fail;
3196 }
3197 tm.tm_year -= 1900;
3198 tm.tm_mon--;
3199 rtc_start_date = mktimegm(&tm);
3200 if (rtc_start_date == -1) {
3201 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003202 PANIC("Invalid date format. Valid format are:\n"
3203 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003204 }
3205 rtc_date_offset = time(NULL) - rtc_start_date;
3206 }
3207 }
3208 break;
rich cannings7339b552011-02-16 13:43:44 -08003209
3210 /* -------------------------------------------------------*/
3211 /* User mode network stack restrictions */
3212 case QEMU_OPTION_drop_udp:
3213 slirp_drop_udp();
3214 break;
3215 case QEMU_OPTION_drop_tcp:
3216 slirp_drop_tcp();
3217 break;
3218 case QEMU_OPTION_allow_tcp:
3219 slirp_allow(optarg, IPPROTO_TCP);
3220 break;
3221 case QEMU_OPTION_allow_udp:
3222 slirp_allow(optarg, IPPROTO_UDP);
3223 break;
3224 case QEMU_OPTION_drop_log:
3225 {
3226 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003227 drop_log_filename = optarg;
3228 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08003229
3230 if (!drop_log_fd) {
3231 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
3232 exit(1);
3233 }
3234
3235 slirp_drop_log_fd(drop_log_fd);
3236 }
3237 break;
3238
3239 case QEMU_OPTION_dns_log:
3240 {
3241 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003242 dns_log_filename = optarg;
3243 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08003244
3245 if (dns_log_fd == NULL) {
3246 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
3247 exit(1);
3248 }
3249
3250 slirp_dns_log_fd(dns_log_fd);
3251 }
3252 break;
3253
3254
3255 case QEMU_OPTION_max_dns_conns:
3256 {
3257 int max_dns_conns = 0;
3258 if (parse_int(optarg, &max_dns_conns)) {
3259 fprintf(stderr,
3260 "qemu: syntax: -max-dns-conns max_connections\n");
3261 exit(1);
3262 }
3263 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3264 fprintf(stderr,
3265 "Invalid arg for max dns connections: %s\n",
3266 optarg);
3267 exit(1);
3268 }
3269 slirp_set_max_dns_conns(max_dns_conns);
3270 }
3271 break;
3272
3273 case QEMU_OPTION_net_forward:
3274 net_slirp_forward(optarg);
3275 break;
3276 case QEMU_OPTION_net_forward_tcp2sink:
3277 {
3278 SockAddress saddr;
3279
3280 if (parse_host_port(&saddr, optarg)) {
3281 fprintf(stderr,
3282 "Invalid ip/port %s for "
3283 "-forward-dropped-tcp2sink. "
3284 "We expect 'sink_ip:sink_port'\n",
3285 optarg);
3286 exit(1);
3287 }
3288 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3289 saddr.u.inet.port);
3290 }
3291 break;
3292 /* -------------------------------------------------------*/
3293
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003294 case QEMU_OPTION_tb_size:
3295 tb_size = strtol(optarg, NULL, 0);
3296 if (tb_size < 0)
3297 tb_size = 0;
3298 break;
3299 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003300 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003301 break;
3302 case QEMU_OPTION_incoming:
3303 incoming = optarg;
3304 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003305#ifdef CONFIG_XEN
3306 case QEMU_OPTION_xen_domid:
3307 xen_domid = atoi(optarg);
3308 break;
3309 case QEMU_OPTION_xen_create:
3310 xen_mode = XEN_CREATE;
3311 break;
3312 case QEMU_OPTION_xen_attach:
3313 xen_mode = XEN_ATTACH;
3314 break;
3315#endif
3316
3317
3318 case QEMU_OPTION_mic:
3319 audio_input_source = (char*)optarg;
3320 break;
3321#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003322 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003323 trace_filename = optarg;
3324 tracing = 1;
3325 break;
3326#if 0
3327 case QEMU_OPTION_trace_miss:
3328 trace_cache_miss = 1;
3329 break;
3330 case QEMU_OPTION_trace_addr:
3331 trace_all_addr = 1;
3332 break;
3333#endif
3334 case QEMU_OPTION_tracing:
3335 if (strcmp(optarg, "off") == 0)
3336 tracing = 0;
3337 else if (strcmp(optarg, "on") == 0 && trace_filename)
3338 tracing = 1;
3339 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003340 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003341 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003342 }
3343 break;
3344#if 0
3345 case QEMU_OPTION_dcache_load_miss:
3346 dcache_load_miss_penalty = atoi(optarg);
3347 break;
3348 case QEMU_OPTION_dcache_store_miss:
3349 dcache_store_miss_penalty = atoi(optarg);
3350 break;
3351#endif
3352#endif
3353#ifdef CONFIG_NAND
3354 case QEMU_OPTION_nand:
3355 nand_add_dev(optarg);
3356 break;
3357#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003358 case QEMU_OPTION_android_ports:
3359 android_op_ports = (char*)optarg;
3360 break;
3361
3362 case QEMU_OPTION_android_port:
3363 android_op_port = (char*)optarg;
3364 break;
3365
3366 case QEMU_OPTION_android_report_console:
3367 android_op_report_console = (char*)optarg;
3368 break;
3369
3370 case QEMU_OPTION_http_proxy:
3371 op_http_proxy = (char*)optarg;
3372 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003373
3374 case QEMU_OPTION_charmap:
3375 op_charmap_file = (char*)optarg;
3376 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003377
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003378 case QEMU_OPTION_android_hw:
3379 android_op_hwini = (char*)optarg;
3380 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003381
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003382 case QEMU_OPTION_dns_server:
3383 android_op_dns_server = (char*)optarg;
3384 break;
3385
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003386 case QEMU_OPTION_radio:
3387 android_op_radio = (char*)optarg;
3388 break;
3389
3390 case QEMU_OPTION_gps:
3391 android_op_gps = (char*)optarg;
3392 break;
3393
3394 case QEMU_OPTION_audio:
3395 android_op_audio = (char*)optarg;
3396 break;
3397
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003398 case QEMU_OPTION_cpu_delay:
3399 android_op_cpu_delay = (char*)optarg;
3400 break;
3401
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003402 case QEMU_OPTION_show_kernel:
3403 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3404 break;
3405
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003406#ifdef CONFIG_NAND_LIMITS
3407 case QEMU_OPTION_nand_limits:
3408 android_op_nand_limits = (char*)optarg;
3409 break;
3410#endif // CONFIG_NAND_LIMITS
3411
3412 case QEMU_OPTION_netspeed:
3413 android_op_netspeed = (char*)optarg;
3414 break;
3415
3416 case QEMU_OPTION_netdelay:
3417 android_op_netdelay = (char*)optarg;
3418 break;
3419
3420 case QEMU_OPTION_netfast:
3421 android_op_netfast = 1;
3422 break;
3423
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003424 case QEMU_OPTION_tcpdump:
3425 android_op_tcpdump = (char*)optarg;
3426 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003427
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003428 case QEMU_OPTION_boot_property:
3429 boot_property_parse_option((char*)optarg);
3430 break;
3431
3432 case QEMU_OPTION_lcd_density:
3433 android_op_lcd_density = (char*)optarg;
3434 break;
3435
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003436 case QEMU_OPTION_ui_port:
3437 android_op_ui_port = (char*)optarg;
3438 break;
3439
3440 case QEMU_OPTION_ui_settings:
3441 android_op_ui_settings = (char*)optarg;
3442 break;
3443
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003444 case QEMU_OPTION_audio_test_out:
3445 android_audio_test_start_out();
3446 break;
3447
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003448 case QEMU_OPTION_android_avdname:
3449 android_op_avd_name = (char*)optarg;
3450 break;
3451
3452 case QEMU_OPTION_timezone:
3453 if (timezone_set((char*)optarg)) {
3454 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3455 (char*)optarg);
3456 }
3457 break;
3458
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003459#ifdef CONFIG_MEMCHECK
3460 case QEMU_OPTION_android_memcheck:
3461 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003462 /* This will set ro.kernel.memcheck system property
3463 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003464 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003465 break;
3466#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003467
3468 case QEMU_OPTION_snapshot_no_time_update:
3469 android_snapshot_update_time = 0;
3470 break;
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003471 default:
3472 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003473 }
3474 }
3475 }
3476
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003477 /* Initialize character map. */
3478 if (android_charmap_setup(op_charmap_file)) {
3479 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003480 PANIC(
3481 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003482 op_charmap_file);
3483 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003484 PANIC(
3485 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003486 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003487 }
3488
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003489 /* If no data_dir is specified then try to find it relative to the
3490 executable path. */
3491 if (!data_dir) {
3492 data_dir = find_datadir(argv[0]);
3493 }
3494 /* If all else fails use the install patch specified when building. */
3495 if (!data_dir) {
3496 data_dir = CONFIG_QEMU_SHAREDIR;
3497 }
3498
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003499 if (!android_op_hwini) {
3500 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003501 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003502 hw_ini = iniFile_newFromFile(android_op_hwini);
3503 if (hw_ini == NULL) {
3504 PANIC("Could not find %s file.", android_op_hwini);
3505 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003506
3507 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003508 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003509
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003510 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003511
3512 {
3513 int width = android_hw->hw_lcd_width;
3514 int height = android_hw->hw_lcd_height;
3515 int depth = android_hw->hw_lcd_depth;
3516
3517 /* A bit of sanity checking */
3518 if (width <= 0 || height <= 0 ||
3519 (depth != 16 && depth != 32) ||
3520 (((width|height) & 3) != 0) )
3521 {
3522 PANIC("Invalid display configuration (%d,%d,%d)",
3523 width, height, depth);
3524 }
3525 android_display_width = width;
3526 android_display_height = height;
3527 android_display_bpp = depth;
3528 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003529
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003530#ifdef CONFIG_NAND_LIMITS
3531 /* Init nand stuff. */
3532 if (android_op_nand_limits) {
3533 parse_nand_limits(android_op_nand_limits);
3534 }
3535#endif // CONFIG_NAND_LIMITS
3536
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003537 /* Initialize AVD name from hardware configuration if needed */
3538 if (!android_op_avd_name) {
3539 if (android_hw->avd_name && *android_hw->avd_name) {
3540 android_op_avd_name = android_hw->avd_name;
3541 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3542 }
3543 }
3544
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003545 /* Initialize system partition image */
3546 {
3547 char tmp[PATH_MAX+32];
3548 const char* sysImage = android_hw->disk_systemPartition_path;
3549 const char* initImage = android_hw->disk_systemPartition_initPath;
3550 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3551
3552 if (sysBytes == 0) {
3553 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
3554 }
3555
3556 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
3557
3558 if (sysImage && *sysImage) {
3559 if (filelock_create(sysImage) == NULL) {
3560 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3561 /* If there is no file= parameters, nand_add_dev will create
3562 * a temporary file to back the partition image. */
3563 } else {
3564 pstrcat(tmp,sizeof(tmp),",file=");
3565 pstrcat(tmp,sizeof(tmp),sysImage);
3566 }
3567 }
3568 if (initImage && *initImage) {
3569 if (!path_exists(initImage)) {
3570 PANIC("Invalid initial system image path: %s", initImage);
3571 }
3572 pstrcat(tmp,sizeof(tmp),",initfile=");
3573 pstrcat(tmp,sizeof(tmp),initImage);
3574 } else {
3575 PANIC("Missing initial system image path!");
3576 }
3577 nand_add_dev(tmp);
3578 }
3579
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003580 /* Initialize data partition image */
3581 {
3582 char tmp[PATH_MAX+32];
3583 const char* dataImage = android_hw->disk_dataPartition_path;
3584 const char* initImage = android_hw->disk_dataPartition_initPath;
3585 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3586
3587 if (dataBytes == 0) {
3588 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
3589 }
3590
3591 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
3592
3593 if (dataImage && *dataImage) {
3594 if (filelock_create(dataImage) == NULL) {
3595 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3596 /* Note: if there is no file= parameters, nand_add_dev() will
3597 * create a temporary file to back the partition image. */
3598 } else {
3599 /* Create the file if needed */
3600 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003601 if (path_empty_file(dataImage) < 0) {
3602 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3603 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003604 }
3605 pstrcat(tmp, sizeof(tmp), ",file=");
3606 pstrcat(tmp, sizeof(tmp), dataImage);
3607 }
3608 }
3609 if (initImage && *initImage) {
3610 pstrcat(tmp, sizeof(tmp), ",initfile=");
3611 pstrcat(tmp, sizeof(tmp), initImage);
3612 }
3613 nand_add_dev(tmp);
3614 }
3615
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003616 /* Init SD-Card stuff. For Android, it is always hda */
3617 /* If the -hda option was used, ignore the Android-provided one */
3618 if (hda_opts == NULL) {
3619 const char* sdPath = android_hw->hw_sdCard_path;
3620 if (sdPath && *sdPath) {
3621 if (!path_exists(sdPath)) {
3622 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3623 } else if (filelock_create(sdPath) == NULL) {
3624 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3625 } else {
3626 /* Successful locking */
3627 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003628 /* Set this property of any operation involving the SD Card
3629 * will be x100 slower, due to the corresponding file being
3630 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3631 * the context of an emulator crash. The data is already
3632 * synced properly when the emulator exits (either normally or through ^C).
3633 */
3634 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003635 }
3636 }
3637 }
3638
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003639 if (hdb_opts == NULL) {
3640 const char* spath = android_hw->disk_snapStorage_path;
3641 if (spath && *spath) {
3642 if (!path_exists(spath)) {
3643 PANIC("Snapshot storage file does not exist: %s", spath);
3644 }
3645 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003646 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003647 }
3648 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003649 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003650 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003651 }
3652 }
3653
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003654 /* Set the VM's max heap size, passed as a boot property */
3655 if (android_hw->vm_heapSize > 0) {
3656 char tmp[64];
3657 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3658 boot_property_add("dalvik.vm.heapsize",tmp);
3659 }
3660
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003661 /* Initialize net speed and delays stuff. */
3662 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003663 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003664 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003665 }
3666
3667 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003668 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003669 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003670 }
3671
3672 if (android_op_netfast) {
3673 qemu_net_download_speed = 0;
3674 qemu_net_upload_speed = 0;
3675 qemu_net_min_latency = 0;
3676 qemu_net_max_latency = 0;
3677 }
3678
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003679 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003680 if (android_hw->hw_lcd_density) {
3681 long density = android_hw->hw_lcd_density;
3682 if (density <= 0) {
3683 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003684 }
3685 hwLcd_setBootProperty(density);
3686 }
3687
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003688 /* Initialize TCP dump */
3689 if (android_op_tcpdump) {
3690 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3691 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3692 }
3693 }
3694
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003695 /* Initialize modem */
3696 if (android_op_radio) {
3697 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3698 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003699 PANIC("unsupported character device specification: %s\n"
3700 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003701 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003702 }
3703 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3704 } else if (android_hw->hw_gsmModem != 0 ) {
3705 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003706 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003707 }
3708 }
3709
3710 /* Initialize GPS */
3711 if (android_op_gps) {
3712 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3713 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003714 PANIC("unsupported character device specification: %s\n"
3715 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003716 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003717 }
3718 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3719 } else if (android_hw->hw_gps != 0) {
3720 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003721 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003722 }
3723 }
3724
3725 /* Initialize audio. */
3726 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003727 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003728 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003729 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003730 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003731 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003732 }
3733
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003734 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003735 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003736
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003737 if (android_op_cpu_delay) {
3738 char* end;
3739 long delay = strtol(android_op_cpu_delay, &end, 0);
3740 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003741 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003742 }
3743 if (delay > 0)
3744 delay = (1000-delay);
3745
3746 qemu_cpu_delay = (int) delay;
3747 }
3748
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003749 if (android_op_dns_server) {
3750 char* x = strchr(android_op_dns_server, ',');
3751 dns_count = 0;
3752 if (x == NULL)
3753 {
3754 if ( add_dns_server( android_op_dns_server ) == 0 )
3755 dns_count = 1;
3756 }
3757 else
3758 {
3759 x = android_op_dns_server;
3760 while (*x) {
3761 char* y = strchr(x, ',');
3762
3763 if (y != NULL) {
3764 *y = 0;
3765 y++;
3766 } else {
3767 y = x + strlen(x);
3768 }
3769
3770 if (y > x && add_dns_server( x ) == 0) {
3771 dns_count += 1;
3772 }
3773 x = y;
3774 }
3775 }
3776 if (dns_count == 0)
3777 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3778 }
3779
3780 if (dns_count == 0)
3781 dns_count = slirp_get_system_dns_servers();
3782 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003783 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003784 }
3785
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003786#ifdef CONFIG_MEMCHECK
3787 if (android_op_memcheck) {
3788 memcheck_init(android_op_memcheck);
3789 }
3790#endif // CONFIG_MEMCHECK
3791
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003792 /* Initialize cache partition, if any */
3793 if (android_hw->disk_cachePartition != 0) {
3794 char tmp[PATH_MAX+32];
3795 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003796 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003797
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003798 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
3799
3800 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3801 if (filelock_create(partPath) == NULL) {
3802 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3803 /* Note: if there is no file= parameters, nand_add_dev() will
3804 * create a temporary file to back the partition image. */
3805 } else {
3806 /* Create the file if needed */
3807 if (!path_exists(partPath)) {
3808 if (path_empty_file(partPath) < 0) {
3809 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3810 }
3811 }
3812 pstrcat(tmp, sizeof(tmp), ",file=");
3813 pstrcat(tmp, sizeof(tmp), partPath);
3814 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003815 }
3816 nand_add_dev(tmp);
3817 }
3818
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003819 /* qemu.gles will be read by the OpenGLES emulation libraries.
3820 * If set to 0, the software GLES renderer will be used as a fallback.
3821 * If the parameter is undefined, this means the system image runs
3822 * inside an emulator that doesn't support GPU emulation at all.
3823 */
3824 {
3825 int gles_emul = 0;
3826
3827 if (android_hw->hw_gpu_enabled) {
3828 if (android_initOpenglesEmulation() == 0) {
3829 gles_emul = 1;
3830 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height);
3831 } else {
3832 dwarning("Could not initialize OpenglES emulation, using software renderer.");
3833 }
3834 }
3835 if (gles_emul) {
3836 stralloc_add_str(kernel_params, " qemu.gles=1");
3837 } else {
3838 stralloc_add_str(kernel_params, " qemu.gles=0");
3839 }
3840 }
3841
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003842 /* We always force qemu=1 when running inside QEMU */
3843 stralloc_add_str(kernel_params, " qemu=1");
3844
3845 /* We always initialize the first serial port for the android-kmsg
3846 * character device (used to send kernel messages) */
3847 serial_hds_add_at(0, "android-kmsg");
3848 stralloc_add_str(kernel_params, " console=ttyS0");
3849
3850 /* We always initialize the second serial port for the android-qemud
3851 * character device as well */
3852 serial_hds_add_at(1, "android-qemud");
3853 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3854
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003855 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3856 os_pidfile_error();
3857 exit(1);
3858 }
3859
David 'Digit' Turner36597752011-05-20 01:18:01 +02003860#if defined(CONFIG_KVM)
3861 if (kvm_allowed < 0) {
3862 kvm_allowed = kvm_check_allowed();
3863 }
3864#endif
3865
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003866#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
3867 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003868 PANIC(
3869 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003870 }
3871#endif
3872
3873 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3874 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003875 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3876 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003877 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003878 }
3879
3880 if (display_type == DT_NOGRAPHIC) {
3881 if (serial_device_index == 0)
3882 serial_devices[0] = "stdio";
3883 if (parallel_device_index == 0)
3884 parallel_devices[0] = "null";
3885 if (strncmp(monitor_device, "vc", 2) == 0)
3886 monitor_device = "stdio";
3887 }
3888
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003889#ifdef CONFIG_KQEMU
3890 if (smp_cpus > 1)
3891 kqemu_allowed = 0;
3892#endif
3893 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003894 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003895 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003896
3897 if (kernel_filename == NULL) {
3898 kernel_filename = android_hw->kernel_path;
3899 }
3900 if (initrd_filename == NULL) {
3901 initrd_filename = android_hw->disk_ramdisk_path;
3902 }
3903
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003904 linux_boot = (kernel_filename != NULL);
3905 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3906
3907 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003908 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003909 }
3910
3911 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003912 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003913 }
3914
3915 /* boot to floppy or the default cd if no hard disk defined yet */
3916 if (!boot_devices[0]) {
3917 boot_devices = "cad";
3918 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003919 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003920
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003921 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003922 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003923 }
David Turner6a9ef172010-09-09 22:54:36 +02003924 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003925
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003926 /* init network clients */
3927 if (nb_net_clients == 0) {
3928 /* if no clients, we use a default config */
3929 net_clients[nb_net_clients++] = "nic";
3930#ifdef CONFIG_SLIRP
3931 net_clients[nb_net_clients++] = "user";
3932#endif
3933 }
3934
3935 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003936 if (net_client_parse(net_clients[i]) < 0) {
3937 PANIC("Unable to parse net clients");
3938 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003939 }
3940 net_client_check();
3941
3942#ifdef TARGET_I386
3943 /* XXX: this should be moved in the PC machine instantiation code */
3944 if (net_boot != 0) {
3945 int netroms = 0;
3946 for (i = 0; i < nb_nics && i < 4; i++) {
3947 const char *model = nd_table[i].model;
3948 char buf[1024];
3949 char *filename;
3950 if (net_boot & (1 << i)) {
3951 if (model == NULL)
3952 model = "ne2k_pci";
3953 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3954 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3955 if (filename && get_image_size(filename) > 0) {
3956 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003957 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003958 }
3959 option_rom[nb_option_roms] = qemu_strdup(buf);
3960 nb_option_roms++;
3961 netroms++;
3962 }
3963 if (filename) {
3964 qemu_free(filename);
3965 }
3966 }
3967 }
3968 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003969 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003970 }
3971 }
3972#endif
3973
3974 /* init the bluetooth world */
3975 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003976 if (bt_parse(bt_opts[i])) {
3977 PANIC("Unable to parse bluetooth options");
3978 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003979
3980 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003981 if (ram_size == 0) {
3982 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3983 if (ram_size == 0) {
3984 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3985 }
3986 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003987
3988#ifdef CONFIG_KQEMU
3989 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
3990 guest ram allocation. It needs to go away. */
3991 if (kqemu_allowed) {
3992 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
3993 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
3994 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003995 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003996 }
3997 }
3998#endif
3999
rich canningsd952f282011-03-01 15:40:09 -08004000#ifndef _WIN32
4001 init_qemu_clear_logs_sig();
4002#endif
4003
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004004 /* init the dynamic translator */
4005 cpu_exec_init_all(tb_size * 1024 * 1024);
4006
4007 bdrv_init();
4008
4009 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004010#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004011 if (nb_drives_opt < MAX_DRIVES)
4012 drive_add(NULL, CDROM_ALIAS);
4013
4014 /* we always create at least one floppy */
4015
4016 if (nb_drives_opt < MAX_DRIVES)
4017 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004018 /* we always create one sd slot, even if no card is in it */
4019
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004020 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004021 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004022 }
4023#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004024
4025 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004026 if (snapshot)
4027 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4028 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
4029 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004030
David Turner6a9ef172010-09-09 22:54:36 +02004031 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004032 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
4033
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004034 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004035 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004036
4037 /* Maintain compatibility with multiple stdio monitors */
4038 if (!strcmp(monitor_device,"stdio")) {
4039 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4040 const char *devname = serial_devices[i];
4041 if (devname && !strcmp(devname,"mon:stdio")) {
4042 monitor_device = NULL;
4043 break;
4044 } else if (devname && !strcmp(devname,"stdio")) {
4045 monitor_device = NULL;
4046 serial_devices[i] = "mon:stdio";
4047 break;
4048 }
4049 }
4050 }
4051
4052 if (nb_numa_nodes > 0) {
4053 int i;
4054
4055 if (nb_numa_nodes > smp_cpus) {
4056 nb_numa_nodes = smp_cpus;
4057 }
4058
4059 /* If no memory size if given for any node, assume the default case
4060 * and distribute the available memory equally across all nodes
4061 */
4062 for (i = 0; i < nb_numa_nodes; i++) {
4063 if (node_mem[i] != 0)
4064 break;
4065 }
4066 if (i == nb_numa_nodes) {
4067 uint64_t usedmem = 0;
4068
4069 /* On Linux, the each node's border has to be 8MB aligned,
4070 * the final node gets the rest.
4071 */
4072 for (i = 0; i < nb_numa_nodes - 1; i++) {
4073 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4074 usedmem += node_mem[i];
4075 }
4076 node_mem[i] = ram_size - usedmem;
4077 }
4078
4079 for (i = 0; i < nb_numa_nodes; i++) {
4080 if (node_cpumask[i] != 0)
4081 break;
4082 }
4083 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4084 * must cope with this anyway, because there are BIOSes out there in
4085 * real machines which also use this scheme.
4086 */
4087 if (i == nb_numa_nodes) {
4088 for (i = 0; i < smp_cpus; i++) {
4089 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4090 }
4091 }
4092 }
4093
4094 if (kvm_enabled()) {
4095 int ret;
4096
4097 ret = kvm_init(smp_cpus);
4098 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004099 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004100 }
4101 }
4102
4103 if (monitor_device) {
4104 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
4105 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004106 PANIC("qemu: could not open monitor device '%s'",
4107 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004108 }
4109 }
4110
4111 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004112 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004113 }
4114
4115 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4116 const char *devname = parallel_devices[i];
4117 if (devname && strcmp(devname, "none")) {
4118 char label[32];
4119 snprintf(label, sizeof(label), "parallel%d", i);
4120 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
4121 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004122 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004123 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004124 }
4125 }
4126 }
4127
4128 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4129 const char *devname = virtio_consoles[i];
4130 if (devname && strcmp(devname, "none")) {
4131 char label[32];
4132 snprintf(label, sizeof(label), "virtcon%d", i);
4133 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
4134 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004135 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004136 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004137 }
4138 }
4139 }
4140
4141 module_call_init(MODULE_INIT_DEVICE);
4142
4143
4144#ifdef CONFIG_TRACE
4145 if (trace_filename) {
4146 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004147 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
4148 }
4149#endif
4150
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004151 /* Check the CPU Architecture value */
4152#if defined(TARGET_ARM)
4153 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4154 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4155 android_hw->hw_cpu_arch);
4156 exit(1);
4157 }
4158#elif defined(TARGET_X86)
4159 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4160 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4161 android_hw->hw_cpu_arch);
4162 exit(1);
4163 }
4164#endif
4165
4166 /* Grab CPU model if provided in hardware.ini */
4167 if ( !cpu_model
4168 && android_hw->hw_cpu_model
4169 && android_hw->hw_cpu_model[0] != '\0')
4170 {
4171 cpu_model = android_hw->hw_cpu_model;
4172 }
4173
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004174 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004175 * collected during initialization.
4176 *
4177 * The order is the following:
4178 * - parameters from the hw configuration (kernel.parameters)
4179 * - additionnal parameters from options (e.g. -memcheck)
4180 * - the -append parameters.
4181 */
4182 {
4183 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004184
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004185 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004186 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004187 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4188 }
4189
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004190 /* If not empty, kernel_config always contains a leading space */
4191 stralloc_append(kernel_params, kernel_config);
4192
4193 if (*kernel_cmdline) {
4194 stralloc_add_c(kernel_params, ' ');
4195 stralloc_add_str(kernel_params, kernel_cmdline);
4196 }
4197
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004198 /* Remove any leading/trailing spaces */
4199 stralloc_strip(kernel_params);
4200
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004201 kernel_parameters = stralloc_cstr(kernel_params);
4202 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4203
4204 machine->init(ram_size,
4205 boot_devices,
4206 kernel_filename,
4207 kernel_parameters,
4208 initrd_filename,
4209 cpu_model);
4210
4211 stralloc_reset(kernel_params);
4212 stralloc_reset(kernel_config);
4213 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004214
4215
4216 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4217 for (i = 0; i < nb_numa_nodes; i++) {
4218 if (node_cpumask[i] & (1 << env->cpu_index)) {
4219 env->numa_node = i;
4220 }
4221 }
4222 }
4223
4224 current_machine = machine;
4225
4226 /* Set KVM's vcpu state to qemu's initial CPUState. */
4227 if (kvm_enabled()) {
4228 int ret;
4229
4230 ret = kvm_sync_vcpus();
4231 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004232 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004233 }
4234 }
4235
4236 /* init USB devices */
4237 if (usb_enabled) {
4238 for(i = 0; i < usb_devices_index; i++) {
4239 if (usb_device_add(usb_devices[i], 0) < 0) {
4240 fprintf(stderr, "Warning: could not add USB device %s\n",
4241 usb_devices[i]);
4242 }
4243 }
4244 }
4245
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004246 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004247 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004248
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004249 /* Initialize display from the command line parameters. */
4250 android_display_reset(ds,
4251 android_display_width,
4252 android_display_height,
4253 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004254
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004255 if (display_type == DT_DEFAULT) {
4256#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4257 display_type = DT_SDL;
4258#else
4259 display_type = DT_VNC;
4260 vnc_display = "localhost:0,to=99";
4261 show_vnc_port = 1;
4262#endif
4263 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004264
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004265
4266 switch (display_type) {
4267 case DT_NOGRAPHIC:
4268 break;
4269#if defined(CONFIG_CURSES)
4270 case DT_CURSES:
4271 curses_display_init(ds, full_screen);
4272 break;
4273#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004274#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004275 case DT_SDL:
4276 sdl_display_init(ds, full_screen, no_frame);
4277 break;
4278#elif defined(CONFIG_COCOA)
4279 case DT_SDL:
4280 cocoa_display_init(ds, full_screen);
4281 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004282#elif defined(CONFIG_STANDALONE_CORE)
4283 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004284 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004285 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004286#endif
4287 case DT_VNC:
4288 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004289 if (vnc_display_open(ds, vnc_display) < 0) {
4290 PANIC("Unable to initialize VNC display");
4291 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004292
4293 if (show_vnc_port) {
4294 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4295 }
4296 break;
4297 default:
4298 break;
4299 }
4300 dpy_resize(ds);
4301
4302 dcl = ds->listeners;
4303 while (dcl != NULL) {
4304 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004305 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
4306 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004307 }
4308 dcl = dcl->next;
4309 }
4310
4311 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004312 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
4313 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004314 }
4315
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004316 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004317 qemu_chr_initial_reset();
4318
4319 if (monitor_device && monitor_hd)
4320 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4321
4322 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4323 const char *devname = serial_devices[i];
4324 if (devname && strcmp(devname, "none")) {
4325 if (strstart(devname, "vc", 0))
4326 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4327 }
4328 }
4329
4330 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4331 const char *devname = parallel_devices[i];
4332 if (devname && strcmp(devname, "none")) {
4333 if (strstart(devname, "vc", 0))
4334 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4335 }
4336 }
4337
4338 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4339 const char *devname = virtio_consoles[i];
4340 if (virtcon_hds[i] && devname) {
4341 if (strstart(devname, "vc", 0))
4342 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4343 }
4344 }
4345
4346 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004347 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004348 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004349 }
4350
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004351 /* call android-specific setup function */
4352 android_emulation_setup();
4353
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004354#if !defined(CONFIG_STANDALONE_CORE)
4355 // For the standalone emulator (UI+core in one executable) we need to
4356 // set the window title here.
4357 android_emulator_set_base_port(android_base_port);
4358#endif
4359
Ot ten Thije871da2a2010-09-20 10:29:22 +01004360 if (loadvm)
4361 do_loadvm(cur_mon, loadvm);
4362
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004363 if (incoming) {
4364 autostart = 0; /* fixme how to deal with -daemonize */
4365 qemu_start_incoming_migration(incoming);
4366 }
4367
4368 if (autostart)
4369 vm_start();
4370
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004371 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004372
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004373#ifdef CONFIG_ANDROID
4374 // This will notify the UI that the core is successfuly initialized
4375 android_core_init_completed();
4376#endif // CONFIG_ANDROID
4377
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004378 main_loop();
4379 quit_timers();
4380 net_cleanup();
4381 android_emulation_teardown();
4382 return 0;
4383}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004384
4385void
4386android_emulation_teardown(void)
4387{
4388 android_charmap_done();
4389}