blob: 32b5eacb59afcd22aca17748acdb91a64eb79d91 [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 Chtchetkineeb838252010-07-15 12:27:56 -070057#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070058#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070059#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010060#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010061#include "android/utils/filelock.h"
62#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010063#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010064#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080065#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080066#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010067#include "android/snapshot.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070068#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070069#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070070
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070071#ifdef CONFIG_MEMCHECK
72#include "memcheck/memcheck.h"
73#endif // CONFIG_MEMCHECK
74
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070075#include <unistd.h>
76#include <fcntl.h>
77#include <signal.h>
78#include <time.h>
79#include <errno.h>
80#include <sys/time.h>
81#include <zlib.h>
82
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070083/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070084#include "config-host.h"
85
86#ifndef _WIN32
87#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070088#include <sys/times.h>
89#include <sys/wait.h>
90#include <termios.h>
91#include <sys/mman.h>
92#include <sys/ioctl.h>
93#include <sys/resource.h>
94#include <sys/socket.h>
95#include <netinet/in.h>
96#include <net/if.h>
97#if defined(__NetBSD__)
98#include <net/if_tap.h>
99#endif
100#ifdef __linux__
101#include <linux/if_tun.h>
102#endif
103#include <arpa/inet.h>
104#include <dirent.h>
105#include <netdb.h>
106#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700107#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700108#include <sys/stat.h>
109#if defined(__FreeBSD__) || defined(__DragonFly__)
110#include <libutil.h>
111#else
112#include <util.h>
113#endif
114#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
115#include <freebsd/stdlib.h>
116#else
117#ifdef __linux__
118#include <pty.h>
119#include <malloc.h>
120#include <linux/rtc.h>
121
122/* For the benefit of older linux systems which don't supply it,
123 we use a local copy of hpet.h. */
124/* #include <linux/hpet.h> */
125#include "hpet.h"
126
127#include <linux/ppdev.h>
128#include <linux/parport.h>
129#endif
130#ifdef __sun__
131#include <sys/stat.h>
132#include <sys/ethernet.h>
133#include <sys/sockio.h>
134#include <netinet/arp.h>
135#include <netinet/in.h>
136#include <netinet/in_systm.h>
137#include <netinet/ip.h>
138#include <netinet/ip_icmp.h> // must come after ip.h
139#include <netinet/udp.h>
140#include <netinet/tcp.h>
141#include <net/if.h>
142#include <syslog.h>
143#include <stropts.h>
144#endif
145#endif
146#endif
147
148#if defined(__OpenBSD__)
149#include <util.h>
150#endif
151
152#if defined(CONFIG_VDE)
153#include <libvdeplug.h>
154#endif
155
156#ifdef _WIN32
157#include <windows.h>
158#include <malloc.h>
159#include <sys/timeb.h>
160#include <mmsystem.h>
161#define getopt_long_only getopt_long
162#define memalign(align, size) malloc(size)
163#endif
164
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200165#include "cpus.h"
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200166#include "arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700167
168#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200169int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700170#undef main
171#define main qemu_main
172#endif /* CONFIG_COCOA */
173
174#include "hw/hw.h"
175#include "hw/boards.h"
176#include "hw/usb.h"
177#include "hw/pcmcia.h"
178#include "hw/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700179#include "hw/isa.h"
180#include "hw/baum.h"
181#include "hw/bt.h"
182#include "hw/watchdog.h"
183#include "hw/smbios.h"
184#include "hw/xen.h"
185#include "bt-host.h"
186#include "net.h"
187#include "monitor.h"
188#include "console.h"
189#include "sysemu.h"
190#include "gdbstub.h"
191#include "qemu-timer.h"
192#include "qemu-char.h"
193#include "cache-utils.h"
194#include "block.h"
195#include "dma.h"
196#include "audio/audio.h"
197#include "migration.h"
198#include "kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200199#ifdef CONFIG_KVM
200#include "kvm-android.h"
201#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700202#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700203#include "android/hw-lcd.h"
204#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200205#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700206#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100207#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700208
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700209#ifdef CONFIG_STANDALONE_CORE
210/* Verbose value used by the standalone emulator core (without UI) */
211unsigned long android_verbose;
212#endif // CONFIG_STANDALONE_CORE
213
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800214#if !defined(CONFIG_STANDALONE_CORE)
215/* in android/qemulator.c */
216extern void android_emulator_set_base_port(int port);
217#endif
218
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700219#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700220#undef main
221#define main qemu_main
222#endif
223
224#include "disas.h"
225
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700226#ifdef CONFIG_TRACE
David 'Digit' Turner406a59b2011-05-12 10:26:09 +0200227#include "android-trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700228#endif
229
230#include "qemu_socket.h"
231
232#if defined(CONFIG_SLIRP)
233#include "libslirp.h"
234#endif
235
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700236
237
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238#define DEFAULT_RAM_SIZE 128
239
240/* Max number of USB devices that can be specified on the commandline. */
241#define MAX_USB_CMDLINE 8
242
243/* Max number of bluetooth switches on the commandline. */
244#define MAX_BT_CMDLINE 10
245
246/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700247
248static const char *data_dir;
249const char *bios_name = NULL;
250static void *ioport_opaque[MAX_IOPORTS];
251static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
252static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100253#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700254/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
255 to store the VM snapshots */
256DriveInfo drives_table[MAX_DRIVES+1];
257int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100258#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260DisplayType display_type = DT_DEFAULT;
261const char* keyboard_layout = NULL;
262int64_t ticks_per_sec;
263ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200264const char *mem_path = NULL;
265#ifdef MAP_POPULATE
266int mem_prealloc = 0; /* force preallocation of physical target memory */
267#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700268int nb_nics;
269NICInfo nd_table[MAX_NICS];
270int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100271int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700272static int rtc_utc = 1;
273static int rtc_date_offset = -1; /* -1 means no change */
274int cirrus_vga_enabled = 1;
275int std_vga_enabled = 0;
276int vmsvga_enabled = 0;
277int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700278QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700279static int full_screen = 0;
280#ifdef CONFIG_SDL
281static int no_frame = 0;
282#endif
283int no_quit = 0;
284CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100285int serial_hds_count;
286
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700287CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
288CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
289#ifdef TARGET_I386
290int win2k_install_hack = 0;
291int rtc_td_hack = 0;
292#endif
293int usb_enabled = 0;
294int singlestep = 0;
295int smp_cpus = 1;
296const char *vnc_display;
297int acpi_enabled = 1;
298int no_hpet = 0;
299int no_virtio_balloon = 0;
300int fd_bootchk = 1;
301int no_reboot = 0;
302int no_shutdown = 0;
303int cursor_hide = 1;
304int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700305WatchdogTimerModel *watchdog = NULL;
306int watchdog_action = WDT_RESET;
307const char *option_rom[MAX_OPTION_ROMS];
308int nb_option_roms;
309int semihosting_enabled = 0;
310#ifdef TARGET_ARM
311int old_param = 0;
312#endif
313const char *qemu_name;
314int alt_grab = 0;
315#if defined(TARGET_SPARC) || defined(TARGET_PPC)
316unsigned int nb_prom_envs = 0;
317const char *prom_envs[MAX_PROM_ENVS];
318#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100319#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700320int nb_drives_opt;
321struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100322#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700323int nb_numa_nodes;
324uint64_t node_mem[MAX_NODES];
325uint64_t node_cpumask[MAX_NODES];
326
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700327static QEMUTimer *nographic_timer;
328
329uint8_t qemu_uuid[16];
330
331
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700332int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700333extern char* audio_input_source;
334
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700335extern char* android_op_ports;
336extern char* android_op_port;
337extern char* android_op_report_console;
338extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700339// Path to the file containing specific key character map.
340char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700341
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700342/* Path to hardware initialization file passed with -android-hw option. */
343char* android_op_hwini = NULL;
344
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700345/* Memory checker options. */
346char* android_op_memcheck = NULL;
347
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700348/* -dns-server option value. */
349char* android_op_dns_server = NULL;
350
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700351/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700352char* android_op_radio = NULL;
353
354/* -gps option value. */
355char* android_op_gps = NULL;
356
357/* -audio option value. */
358char* android_op_audio = NULL;
359
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700360/* -cpu-delay option value. */
361char* android_op_cpu_delay = NULL;
362
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700363#ifdef CONFIG_NAND_LIMITS
364/* -nand-limits option value. */
365char* android_op_nand_limits = NULL;
366#endif // CONFIG_NAND_LIMITS
367
368/* -netspeed option value. */
369char* android_op_netspeed = NULL;
370
371/* -netdelay option value. */
372char* android_op_netdelay = NULL;
373
374/* -netfast option value. */
375int android_op_netfast = 0;
376
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700377/* -tcpdump option value. */
378char* android_op_tcpdump = NULL;
379
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700380/* -lcd-density option value. */
381char* android_op_lcd_density = NULL;
382
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700383/* -ui-port option value. This port will be used to report the core
384 * initialization completion.
385 */
386char* android_op_ui_port = NULL;
387
388/* -ui-settings option value. This value will be passed to the UI when new UI
389 * process is attaching to the core.
390 */
391char* android_op_ui_settings = NULL;
392
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800393/* -android-avdname option value. */
394char* android_op_avd_name = "unknown";
395
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700396extern int android_display_width;
397extern int android_display_height;
398extern int android_display_bpp;
399
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700400extern void dprint( const char* format, ... );
401
rich canningsd952f282011-03-01 15:40:09 -0800402const char* dns_log_filename = NULL;
403const char* drop_log_filename = NULL;
404static int rotate_logs_requested = 0;
405
Tim Baverstock24204cc2010-11-25 11:37:43 +0000406const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000407
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700408#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
409
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700410/* Reports the core initialization failure to the error stdout and to the UI
411 * socket before exiting the application.
412 * Parameters that are passed to this macro are used to format the error
413 * mesage using sprintf routine.
414 */
415#ifdef CONFIG_ANDROID
416#define PANIC(...) android_core_init_failure(__VA_ARGS__)
417#else
418#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
419 exit(1); \
420 } while (0)
421#endif // CONFIG_ANDROID
422
423/* Exits the core during initialization. */
424#ifdef CONFIG_ANDROID
425#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
426#else
427#define QEMU_EXIT(exit_code) exit(exit_code)
428#endif // CONFIG_ANDROID
429
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700430/***********************************************************/
431/* x86 ISA bus support */
432
433target_phys_addr_t isa_mem_base = 0;
434PicState2 *isa_pic;
435
436static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
437static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
438
439static uint32_t ioport_read(int index, uint32_t address)
440{
441 static IOPortReadFunc *default_func[3] = {
442 default_ioport_readb,
443 default_ioport_readw,
444 default_ioport_readl
445 };
446 IOPortReadFunc *func = ioport_read_table[index][address];
447 if (!func)
448 func = default_func[index];
449 return func(ioport_opaque[address], address);
450}
451
452static void ioport_write(int index, uint32_t address, uint32_t data)
453{
454 static IOPortWriteFunc *default_func[3] = {
455 default_ioport_writeb,
456 default_ioport_writew,
457 default_ioport_writel
458 };
459 IOPortWriteFunc *func = ioport_write_table[index][address];
460 if (!func)
461 func = default_func[index];
462 func(ioport_opaque[address], address, data);
463}
464
465static uint32_t default_ioport_readb(void *opaque, uint32_t address)
466{
467#ifdef DEBUG_UNUSED_IOPORT
468 fprintf(stderr, "unused inb: port=0x%04x\n", address);
469#endif
470 return 0xff;
471}
472
473static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
474{
475#ifdef DEBUG_UNUSED_IOPORT
476 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
477#endif
478}
479
480/* default is to make two byte accesses */
481static uint32_t default_ioport_readw(void *opaque, uint32_t address)
482{
483 uint32_t data;
484 data = ioport_read(0, address);
485 address = (address + 1) & (MAX_IOPORTS - 1);
486 data |= ioport_read(0, address) << 8;
487 return data;
488}
489
490static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
491{
492 ioport_write(0, address, data & 0xff);
493 address = (address + 1) & (MAX_IOPORTS - 1);
494 ioport_write(0, address, (data >> 8) & 0xff);
495}
496
497static uint32_t default_ioport_readl(void *opaque, uint32_t address)
498{
499#ifdef DEBUG_UNUSED_IOPORT
500 fprintf(stderr, "unused inl: port=0x%04x\n", address);
501#endif
502 return 0xffffffff;
503}
504
505static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
506{
507#ifdef DEBUG_UNUSED_IOPORT
508 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
509#endif
510}
511
rich canningsd952f282011-03-01 15:40:09 -0800512/*
513 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
514 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
515 * between the tasks that do not require restarting Qemu.
516 */
517void rotate_qemu_logs_handler(int signum) {
518 rotate_logs_requested = 1;
519}
520
521/*
522 * Resets the rotate_log_requested_flag. Normally called after qemu
523 * logs has been rotated.
524 */
525void reset_rotate_qemu_logs_request(void) {
526 rotate_logs_requested = 0;
527}
528
529/*
530 * Clears the passed qemu log when the rotate_logs_requested
531 * is set. We need to clear the logs between the tasks that do not
532 * require restarting Qemu.
533 */
534FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
535 FILE* new_log_fd = NULL;
536 if (old_log_fd) {
537 if (fclose(old_log_fd) == -1) {
538 fprintf(stderr, "Cannot close old_log fd\n");
539 exit(errno);
540 }
541 }
542
543 if (!filename) {
544 fprintf(stderr, "The log filename to be rotated is not provided");
545 exit(-1);
546 }
547
548 new_log_fd = fopen(filename , "wb+");
549 if (new_log_fd == NULL) {
550 fprintf(stderr, "Cannot open the log file: %s for write.\n",
551 filename);
552 exit(1);
553 }
554
555 return new_log_fd;
556}
557
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700558/***************/
559/* ballooning */
560
561static QEMUBalloonEvent *qemu_balloon_event;
562void *qemu_balloon_event_opaque;
563
564void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
565{
566 qemu_balloon_event = func;
567 qemu_balloon_event_opaque = opaque;
568}
569
570void qemu_balloon(ram_addr_t target)
571{
572 if (qemu_balloon_event)
573 qemu_balloon_event(qemu_balloon_event_opaque, target);
574}
575
576ram_addr_t qemu_balloon_status(void)
577{
578 if (qemu_balloon_event)
579 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
580 return 0;
581}
582
583/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700584/* host time/date access */
585void qemu_get_timedate(struct tm *tm, int offset)
586{
587 time_t ti;
588 struct tm *ret;
589
590 time(&ti);
591 ti += offset;
592 if (rtc_date_offset == -1) {
593 if (rtc_utc)
594 ret = gmtime(&ti);
595 else
596 ret = localtime(&ti);
597 } else {
598 ti -= rtc_date_offset;
599 ret = gmtime(&ti);
600 }
601
602 memcpy(tm, ret, sizeof(struct tm));
603}
604
605int qemu_timedate_diff(struct tm *tm)
606{
607 time_t seconds;
608
609 if (rtc_date_offset == -1)
610 if (rtc_utc)
611 seconds = mktimegm(tm);
612 else
613 seconds = mktime(tm);
614 else
615 seconds = mktimegm(tm) + rtc_date_offset;
616
617 return seconds - time(NULL);
618}
619
620
621#ifdef CONFIG_TRACE
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200622int tbflush_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700623static int exit_requested;
624
625void start_tracing()
626{
627 if (trace_filename == NULL)
628 return;
629 if (!tracing) {
630 fprintf(stderr,"-- start tracing --\n");
631 start_time = Now();
632 }
633 tracing = 1;
634 tbflush_requested = 1;
635 qemu_notify_event();
636}
637
638void stop_tracing()
639{
640 if (trace_filename == NULL)
641 return;
642 if (tracing) {
643 end_time = Now();
644 elapsed_usecs += end_time - start_time;
645 fprintf(stderr,"-- stop tracing --\n");
646 }
647 tracing = 0;
648 tbflush_requested = 1;
649 qemu_notify_event();
650}
651
652#ifndef _WIN32
653/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
654 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
655 */
656void sigusr_handler(int sig)
657{
658 if (sig == SIGUSR1)
659 start_tracing();
660 else
661 stop_tracing();
662}
663#endif
664
665/* This is the handler to catch control-C so that we can exit cleanly.
666 * This is needed when tracing to flush the buffers to disk.
667 */
668void sigint_handler(int sig)
669{
670 exit_requested = 1;
671 qemu_notify_event();
672}
673#endif /* CONFIG_TRACE */
674
675
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700676/***********************************************************/
677/* Bluetooth support */
678static int nb_hcis;
679static int cur_hci;
680static struct HCIInfo *hci_table[MAX_NICS];
681
682static struct bt_vlan_s {
683 struct bt_scatternet_s net;
684 int id;
685 struct bt_vlan_s *next;
686} *first_bt_vlan;
687
688/* find or alloc a new bluetooth "VLAN" */
689static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
690{
691 struct bt_vlan_s **pvlan, *vlan;
692 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
693 if (vlan->id == id)
694 return &vlan->net;
695 }
696 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
697 vlan->id = id;
698 pvlan = &first_bt_vlan;
699 while (*pvlan != NULL)
700 pvlan = &(*pvlan)->next;
701 *pvlan = vlan;
702 return &vlan->net;
703}
704
705static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
706{
707}
708
709static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
710{
711 return -ENOTSUP;
712}
713
714static struct HCIInfo null_hci = {
715 .cmd_send = null_hci_send,
716 .sco_send = null_hci_send,
717 .acl_send = null_hci_send,
718 .bdaddr_set = null_hci_addr_set,
719};
720
721struct HCIInfo *qemu_next_hci(void)
722{
723 if (cur_hci == nb_hcis)
724 return &null_hci;
725
726 return hci_table[cur_hci++];
727}
728
729static struct HCIInfo *hci_init(const char *str)
730{
731 char *endp;
732 struct bt_scatternet_s *vlan = 0;
733
734 if (!strcmp(str, "null"))
735 /* null */
736 return &null_hci;
737 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
738 /* host[:hciN] */
739 return bt_host_hci(str[4] ? str + 5 : "hci0");
740 else if (!strncmp(str, "hci", 3)) {
741 /* hci[,vlan=n] */
742 if (str[3]) {
743 if (!strncmp(str + 3, ",vlan=", 6)) {
744 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
745 if (*endp)
746 vlan = 0;
747 }
748 } else
749 vlan = qemu_find_bt_vlan(0);
750 if (vlan)
751 return bt_new_hci(vlan);
752 }
753
754 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
755
756 return 0;
757}
758
759static int bt_hci_parse(const char *str)
760{
761 struct HCIInfo *hci;
762 bdaddr_t bdaddr;
763
764 if (nb_hcis >= MAX_NICS) {
765 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
766 return -1;
767 }
768
769 hci = hci_init(str);
770 if (!hci)
771 return -1;
772
773 bdaddr.b[0] = 0x52;
774 bdaddr.b[1] = 0x54;
775 bdaddr.b[2] = 0x00;
776 bdaddr.b[3] = 0x12;
777 bdaddr.b[4] = 0x34;
778 bdaddr.b[5] = 0x56 + nb_hcis;
779 hci->bdaddr_set(hci, bdaddr.b);
780
781 hci_table[nb_hcis++] = hci;
782
783 return 0;
784}
785
786static void bt_vhci_add(int vlan_id)
787{
788 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
789
790 if (!vlan->slave)
791 fprintf(stderr, "qemu: warning: adding a VHCI to "
792 "an empty scatternet %i\n", vlan_id);
793
794 bt_vhci_init(bt_new_hci(vlan));
795}
796
797static struct bt_device_s *bt_device_add(const char *opt)
798{
799 struct bt_scatternet_s *vlan;
800 int vlan_id = 0;
801 char *endp = strstr(opt, ",vlan=");
802 int len = (endp ? endp - opt : strlen(opt)) + 1;
803 char devname[10];
804
805 pstrcpy(devname, MIN(sizeof(devname), len), opt);
806
807 if (endp) {
808 vlan_id = strtol(endp + 6, &endp, 0);
809 if (*endp) {
810 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
811 return 0;
812 }
813 }
814
815 vlan = qemu_find_bt_vlan(vlan_id);
816
817 if (!vlan->slave)
818 fprintf(stderr, "qemu: warning: adding a slave device to "
819 "an empty scatternet %i\n", vlan_id);
820
821 if (!strcmp(devname, "keyboard"))
822 return bt_keyboard_init(vlan);
823
824 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
825 return 0;
826}
827
828static int bt_parse(const char *opt)
829{
830 const char *endp, *p;
831 int vlan;
832
833 if (strstart(opt, "hci", &endp)) {
834 if (!*endp || *endp == ',') {
835 if (*endp)
836 if (!strstart(endp, ",vlan=", 0))
837 opt = endp + 1;
838
839 return bt_hci_parse(opt);
840 }
841 } else if (strstart(opt, "vhci", &endp)) {
842 if (!*endp || *endp == ',') {
843 if (*endp) {
844 if (strstart(endp, ",vlan=", &p)) {
845 vlan = strtol(p, (char **) &endp, 0);
846 if (*endp) {
847 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
848 return 1;
849 }
850 } else {
851 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
852 return 1;
853 }
854 } else
855 vlan = 0;
856
857 bt_vhci_add(vlan);
858 return 0;
859 }
860 } else if (strstart(opt, "device:", &endp))
861 return !bt_device_add(endp);
862
863 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
864 return 1;
865}
866
867/***********************************************************/
868/* QEMU Block devices */
869
870#define HD_ALIAS "index=%d,media=disk"
871#define CDROM_ALIAS "index=2,media=cdrom"
872#define FD_ALIAS "index=%d,if=floppy"
873#define PFLASH_ALIAS "if=pflash"
874#define MTD_ALIAS "if=mtd"
875#define SD_ALIAS "index=0,if=sd"
876
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100877static int drive_init_func(QemuOpts *opts, void *opaque)
878{
879 int *use_scsi = opaque;
880 int fatal_error = 0;
881
882 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
883 if (fatal_error)
884 return 1;
885 }
886 return 0;
887}
888
889static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
890{
891 if (NULL == qemu_opt_get(opts, "snapshot")) {
892 qemu_opt_set(opts, "snapshot", "on");
893 }
894 return 0;
895}
896
897#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700898static int drive_opt_get_free_idx(void)
899{
900 int index;
901
902 for (index = 0; index < MAX_DRIVES; index++)
903 if (!drives_opt[index].used) {
904 drives_opt[index].used = 1;
905 return index;
906 }
907
908 return -1;
909}
910
911static int drive_get_free_idx(void)
912{
913 int index;
914
915 for (index = 0; index < MAX_DRIVES; index++)
916 if (!drives_table[index].used) {
917 drives_table[index].used = 1;
918 return index;
919 }
920
921 return -1;
922}
923
924int drive_add(const char *file, const char *fmt, ...)
925{
926 va_list ap;
927 int index = drive_opt_get_free_idx();
928
929 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
930 fprintf(stderr, "qemu: too many drives\n");
931 return -1;
932 }
933
934 drives_opt[index].file = file;
935 va_start(ap, fmt);
936 vsnprintf(drives_opt[index].opt,
937 sizeof(drives_opt[0].opt), fmt, ap);
938 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700939
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700940 nb_drives_opt++;
941 return index;
942}
943
944void drive_remove(int index)
945{
946 drives_opt[index].used = 0;
947 nb_drives_opt--;
948}
949
950int drive_get_index(BlockInterfaceType type, int bus, int unit)
951{
952 int index;
953
954 /* seek interface, bus and unit */
955
956 for (index = 0; index < MAX_DRIVES; index++)
957 if (drives_table[index].type == type &&
958 drives_table[index].bus == bus &&
959 drives_table[index].unit == unit &&
960 drives_table[index].used)
961 return index;
962
963 return -1;
964}
965
966int drive_get_max_bus(BlockInterfaceType type)
967{
968 int max_bus;
969 int index;
970
971 max_bus = -1;
972 for (index = 0; index < nb_drives; index++) {
973 if(drives_table[index].type == type &&
974 drives_table[index].bus > max_bus)
975 max_bus = drives_table[index].bus;
976 }
977 return max_bus;
978}
979
980const char *drive_get_serial(BlockDriverState *bdrv)
981{
982 int index;
983
984 for (index = 0; index < nb_drives; index++)
985 if (drives_table[index].bdrv == bdrv)
986 return drives_table[index].serial;
987
988 return "\0";
989}
990
991BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
992{
993 int index;
994
995 for (index = 0; index < nb_drives; index++)
996 if (drives_table[index].bdrv == bdrv)
997 return drives_table[index].onerror;
998
999 return BLOCK_ERR_STOP_ENOSPC;
1000}
1001
1002static void bdrv_format_print(void *opaque, const char *name)
1003{
1004 fprintf(stderr, " %s", name);
1005}
1006
1007void drive_uninit(BlockDriverState *bdrv)
1008{
1009 int i;
1010
1011 for (i = 0; i < MAX_DRIVES; i++)
1012 if (drives_table[i].bdrv == bdrv) {
1013 drives_table[i].bdrv = NULL;
1014 drives_table[i].used = 0;
1015 drive_remove(drives_table[i].drive_opt_idx);
1016 nb_drives--;
1017 break;
1018 }
1019}
1020
1021int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1022{
1023 char buf[128];
1024 char file[1024];
1025 char devname[128];
1026 char serial[21];
1027 const char *mediastr = "";
1028 BlockInterfaceType type;
1029 enum { MEDIA_DISK, MEDIA_CDROM } media;
1030 int bus_id, unit_id;
1031 int cyls, heads, secs, translation;
1032 BlockDriverState *bdrv;
1033 BlockDriver *drv = NULL;
1034 QEMUMachine *machine = opaque;
1035 int max_devs;
1036 int index;
1037 int cache;
1038 int bdrv_flags, onerror;
1039 int drives_table_idx;
1040 char *str = arg->opt;
1041 static const char * const params[] = { "bus", "unit", "if", "index",
1042 "cyls", "heads", "secs", "trans",
1043 "media", "snapshot", "file",
1044 "cache", "format", "serial", "werror",
1045 NULL };
1046
1047 if (check_params(buf, sizeof(buf), params, str) < 0) {
1048 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1049 buf, str);
1050 return -1;
1051 }
1052
1053 file[0] = 0;
1054 cyls = heads = secs = 0;
1055 bus_id = 0;
1056 unit_id = -1;
1057 translation = BIOS_ATA_TRANSLATION_AUTO;
1058 index = -1;
1059 cache = 3;
1060
1061 if (machine->use_scsi) {
1062 type = IF_SCSI;
1063 max_devs = MAX_SCSI_DEVS;
1064 pstrcpy(devname, sizeof(devname), "scsi");
1065 } else {
1066 type = IF_IDE;
1067 max_devs = MAX_IDE_DEVS;
1068 pstrcpy(devname, sizeof(devname), "ide");
1069 }
1070 media = MEDIA_DISK;
1071
1072 /* extract parameters */
1073
1074 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1075 bus_id = strtol(buf, NULL, 0);
1076 if (bus_id < 0) {
1077 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1078 return -1;
1079 }
1080 }
1081
1082 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1083 unit_id = strtol(buf, NULL, 0);
1084 if (unit_id < 0) {
1085 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1086 return -1;
1087 }
1088 }
1089
1090 if (get_param_value(buf, sizeof(buf), "if", str)) {
1091 pstrcpy(devname, sizeof(devname), buf);
1092 if (!strcmp(buf, "ide")) {
1093 type = IF_IDE;
1094 max_devs = MAX_IDE_DEVS;
1095 } else if (!strcmp(buf, "scsi")) {
1096 type = IF_SCSI;
1097 max_devs = MAX_SCSI_DEVS;
1098 } else if (!strcmp(buf, "floppy")) {
1099 type = IF_FLOPPY;
1100 max_devs = 0;
1101 } else if (!strcmp(buf, "pflash")) {
1102 type = IF_PFLASH;
1103 max_devs = 0;
1104 } else if (!strcmp(buf, "mtd")) {
1105 type = IF_MTD;
1106 max_devs = 0;
1107 } else if (!strcmp(buf, "sd")) {
1108 type = IF_SD;
1109 max_devs = 0;
1110 } else if (!strcmp(buf, "virtio")) {
1111 type = IF_VIRTIO;
1112 max_devs = 0;
1113 } else if (!strcmp(buf, "xen")) {
1114 type = IF_XEN;
1115 max_devs = 0;
1116 } else {
1117 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1118 return -1;
1119 }
1120 }
1121
1122 if (get_param_value(buf, sizeof(buf), "index", str)) {
1123 index = strtol(buf, NULL, 0);
1124 if (index < 0) {
1125 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1126 return -1;
1127 }
1128 }
1129
1130 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1131 cyls = strtol(buf, NULL, 0);
1132 }
1133
1134 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1135 heads = strtol(buf, NULL, 0);
1136 }
1137
1138 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1139 secs = strtol(buf, NULL, 0);
1140 }
1141
1142 if (cyls || heads || secs) {
1143 if (cyls < 1 || cyls > 16383) {
1144 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1145 return -1;
1146 }
1147 if (heads < 1 || heads > 16) {
1148 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1149 return -1;
1150 }
1151 if (secs < 1 || secs > 63) {
1152 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1153 return -1;
1154 }
1155 }
1156
1157 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1158 if (!cyls) {
1159 fprintf(stderr,
1160 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1161 str);
1162 return -1;
1163 }
1164 if (!strcmp(buf, "none"))
1165 translation = BIOS_ATA_TRANSLATION_NONE;
1166 else if (!strcmp(buf, "lba"))
1167 translation = BIOS_ATA_TRANSLATION_LBA;
1168 else if (!strcmp(buf, "auto"))
1169 translation = BIOS_ATA_TRANSLATION_AUTO;
1170 else {
1171 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1172 return -1;
1173 }
1174 }
1175
1176 if (get_param_value(buf, sizeof(buf), "media", str)) {
1177 if (!strcmp(buf, "disk")) {
1178 media = MEDIA_DISK;
1179 } else if (!strcmp(buf, "cdrom")) {
1180 if (cyls || secs || heads) {
1181 fprintf(stderr,
1182 "qemu: '%s' invalid physical CHS format\n", str);
1183 return -1;
1184 }
1185 media = MEDIA_CDROM;
1186 } else {
1187 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1188 return -1;
1189 }
1190 }
1191
1192 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1193 if (!strcmp(buf, "on"))
1194 snapshot = 1;
1195 else if (!strcmp(buf, "off"))
1196 snapshot = 0;
1197 else {
1198 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1199 return -1;
1200 }
1201 }
1202
1203 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1204 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1205 cache = 0;
1206 else if (!strcmp(buf, "writethrough"))
1207 cache = 1;
1208 else if (!strcmp(buf, "writeback"))
1209 cache = 2;
1210 else {
1211 fprintf(stderr, "qemu: invalid cache option\n");
1212 return -1;
1213 }
1214 }
1215
1216 if (get_param_value(buf, sizeof(buf), "format", str)) {
1217 if (strcmp(buf, "?") == 0) {
1218 fprintf(stderr, "qemu: Supported formats:");
1219 bdrv_iterate_format(bdrv_format_print, NULL);
1220 fprintf(stderr, "\n");
1221 return -1;
1222 }
1223 drv = bdrv_find_format(buf);
1224 if (!drv) {
1225 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1226 return -1;
1227 }
1228 }
1229
1230 if (arg->file == NULL)
1231 get_param_value(file, sizeof(file), "file", str);
1232 else
1233 pstrcpy(file, sizeof(file), arg->file);
1234
1235 if (!get_param_value(serial, sizeof(serial), "serial", str))
1236 memset(serial, 0, sizeof(serial));
1237
1238 onerror = BLOCK_ERR_STOP_ENOSPC;
1239 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1240 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1241 fprintf(stderr, "werror is no supported by this format\n");
1242 return -1;
1243 }
1244 if (!strcmp(buf, "ignore"))
1245 onerror = BLOCK_ERR_IGNORE;
1246 else if (!strcmp(buf, "enospc"))
1247 onerror = BLOCK_ERR_STOP_ENOSPC;
1248 else if (!strcmp(buf, "stop"))
1249 onerror = BLOCK_ERR_STOP_ANY;
1250 else if (!strcmp(buf, "report"))
1251 onerror = BLOCK_ERR_REPORT;
1252 else {
1253 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1254 return -1;
1255 }
1256 }
1257
1258 /* compute bus and unit according index */
1259
1260 if (index != -1) {
1261 if (bus_id != 0 || unit_id != -1) {
1262 fprintf(stderr,
1263 "qemu: '%s' index cannot be used with bus and unit\n", str);
1264 return -1;
1265 }
1266 if (max_devs == 0)
1267 {
1268 unit_id = index;
1269 bus_id = 0;
1270 } else {
1271 unit_id = index % max_devs;
1272 bus_id = index / max_devs;
1273 }
1274 }
1275
1276 /* if user doesn't specify a unit_id,
1277 * try to find the first free
1278 */
1279
1280 if (unit_id == -1) {
1281 unit_id = 0;
1282 while (drive_get_index(type, bus_id, unit_id) != -1) {
1283 unit_id++;
1284 if (max_devs && unit_id >= max_devs) {
1285 unit_id -= max_devs;
1286 bus_id++;
1287 }
1288 }
1289 }
1290
1291 /* check unit id */
1292
1293 if (max_devs && unit_id >= max_devs) {
1294 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1295 str, unit_id, max_devs - 1);
1296 return -1;
1297 }
1298
1299 /*
1300 * ignore multiple definitions
1301 */
1302
1303 if (drive_get_index(type, bus_id, unit_id) != -1)
1304 return -2;
1305
1306 /* init */
1307
1308 if (type == IF_IDE || type == IF_SCSI)
1309 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1310 if (max_devs)
1311 snprintf(buf, sizeof(buf), "%s%i%s%i",
1312 devname, bus_id, mediastr, unit_id);
1313 else
1314 snprintf(buf, sizeof(buf), "%s%s%i",
1315 devname, mediastr, unit_id);
1316 bdrv = bdrv_new(buf);
1317 drives_table_idx = drive_get_free_idx();
1318 drives_table[drives_table_idx].bdrv = bdrv;
1319 drives_table[drives_table_idx].type = type;
1320 drives_table[drives_table_idx].bus = bus_id;
1321 drives_table[drives_table_idx].unit = unit_id;
1322 drives_table[drives_table_idx].onerror = onerror;
1323 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1324 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1325 nb_drives++;
1326
1327 switch(type) {
1328 case IF_IDE:
1329 case IF_SCSI:
1330 case IF_XEN:
1331 switch(media) {
1332 case MEDIA_DISK:
1333 if (cyls != 0) {
1334 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1335 bdrv_set_translation_hint(bdrv, translation);
1336 }
1337 break;
1338 case MEDIA_CDROM:
1339 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1340 break;
1341 }
1342 break;
1343 case IF_SD:
1344 /* FIXME: This isn't really a floppy, but it's a reasonable
1345 approximation. */
1346 case IF_FLOPPY:
1347 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1348 break;
1349 case IF_PFLASH:
1350 case IF_MTD:
1351 case IF_VIRTIO:
1352 break;
1353 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001354 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001355 abort();
1356 }
1357 if (!file[0])
1358 return -2;
1359 bdrv_flags = 0;
1360 if (snapshot) {
1361 bdrv_flags |= BDRV_O_SNAPSHOT;
1362 cache = 2; /* always use write-back with snapshot */
1363 }
1364 if (cache == 0) /* no caching */
1365 bdrv_flags |= BDRV_O_NOCACHE;
1366 else if (cache == 2) /* write-back */
1367 bdrv_flags |= BDRV_O_CACHE_WB;
1368 else if (cache == 3) /* not specified */
1369 bdrv_flags |= BDRV_O_CACHE_DEF;
1370 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1371 fprintf(stderr, "qemu: could not open disk image %s\n",
1372 file);
1373 return -1;
1374 }
1375 if (bdrv_key_required(bdrv))
1376 autostart = 0;
1377 return drives_table_idx;
1378}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001379#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001380
1381static void numa_add(const char *optarg)
1382{
1383 char option[128];
1384 char *endptr;
1385 unsigned long long value, endvalue;
1386 int nodenr;
1387
1388 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1389 if (!strcmp(option, "node")) {
1390 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1391 nodenr = nb_numa_nodes;
1392 } else {
1393 nodenr = strtoull(option, NULL, 10);
1394 }
1395
1396 if (get_param_value(option, 128, "mem", optarg) == 0) {
1397 node_mem[nodenr] = 0;
1398 } else {
1399 value = strtoull(option, &endptr, 0);
1400 switch (*endptr) {
1401 case 0: case 'M': case 'm':
1402 value <<= 20;
1403 break;
1404 case 'G': case 'g':
1405 value <<= 30;
1406 break;
1407 }
1408 node_mem[nodenr] = value;
1409 }
1410 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1411 node_cpumask[nodenr] = 0;
1412 } else {
1413 value = strtoull(option, &endptr, 10);
1414 if (value >= 64) {
1415 value = 63;
1416 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1417 } else {
1418 if (*endptr == '-') {
1419 endvalue = strtoull(endptr+1, &endptr, 10);
1420 if (endvalue >= 63) {
1421 endvalue = 62;
1422 fprintf(stderr,
1423 "only 63 CPUs in NUMA mode supported.\n");
1424 }
1425 value = (1 << (endvalue + 1)) - (1 << value);
1426 } else {
1427 value = 1 << value;
1428 }
1429 }
1430 node_cpumask[nodenr] = value;
1431 }
1432 nb_numa_nodes++;
1433 }
1434 return;
1435}
1436
1437/***********************************************************/
1438/* USB devices */
1439
1440static USBPort *used_usb_ports;
1441static USBPort *free_usb_ports;
1442
1443/* ??? Maybe change this to register a hub to keep track of the topology. */
1444void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1445 usb_attachfn attach)
1446{
1447 port->opaque = opaque;
1448 port->index = index;
1449 port->attach = attach;
1450 port->next = free_usb_ports;
1451 free_usb_ports = port;
1452}
1453
1454int usb_device_add_dev(USBDevice *dev)
1455{
1456 USBPort *port;
1457
1458 /* Find a USB port to add the device to. */
1459 port = free_usb_ports;
1460 if (!port->next) {
1461 USBDevice *hub;
1462
1463 /* Create a new hub and chain it on. */
1464 free_usb_ports = NULL;
1465 port->next = used_usb_ports;
1466 used_usb_ports = port;
1467
1468 hub = usb_hub_init(VM_USB_HUB_SIZE);
1469 usb_attach(port, hub);
1470 port = free_usb_ports;
1471 }
1472
1473 free_usb_ports = port->next;
1474 port->next = used_usb_ports;
1475 used_usb_ports = port;
1476 usb_attach(port, dev);
1477 return 0;
1478}
1479
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001480#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001481static void usb_msd_password_cb(void *opaque, int err)
1482{
1483 USBDevice *dev = opaque;
1484
1485 if (!err)
1486 usb_device_add_dev(dev);
1487 else
1488 dev->handle_destroy(dev);
1489}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001490#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001491
1492static int usb_device_add(const char *devname, int is_hotplug)
1493{
1494 const char *p;
1495 USBDevice *dev;
1496
1497 if (!free_usb_ports)
1498 return -1;
1499
1500 if (strstart(devname, "host:", &p)) {
1501 dev = usb_host_device_open(p);
1502 } else if (!strcmp(devname, "mouse")) {
1503 dev = usb_mouse_init();
1504 } else if (!strcmp(devname, "tablet")) {
1505 dev = usb_tablet_init();
1506 } else if (!strcmp(devname, "keyboard")) {
1507 dev = usb_keyboard_init();
1508 } else if (strstart(devname, "disk:", &p)) {
1509#if 0
1510 BlockDriverState *bs;
1511#endif
1512 dev = usb_msd_init(p);
1513 if (!dev)
1514 return -1;
1515#if 0
1516 bs = usb_msd_get_bdrv(dev);
1517 if (bdrv_key_required(bs)) {
1518 autostart = 0;
1519 if (is_hotplug) {
1520 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1521 dev);
1522 return 0;
1523 }
1524 }
1525 } else if (!strcmp(devname, "wacom-tablet")) {
1526 dev = usb_wacom_init();
1527 } else if (strstart(devname, "serial:", &p)) {
1528 dev = usb_serial_init(p);
1529#ifdef CONFIG_BRLAPI
1530 } else if (!strcmp(devname, "braille")) {
1531 dev = usb_baum_init();
1532#endif
1533 } else if (strstart(devname, "net:", &p)) {
1534 int nic = nb_nics;
1535
1536 if (net_client_init("nic", p) < 0)
1537 return -1;
1538 nd_table[nic].model = "usb";
1539 dev = usb_net_init(&nd_table[nic]);
1540 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1541 dev = usb_bt_init(devname[2] ? hci_init(p) :
1542 bt_new_hci(qemu_find_bt_vlan(0)));
1543#endif
1544 } else {
1545 return -1;
1546 }
1547 if (!dev)
1548 return -1;
1549
1550 return usb_device_add_dev(dev);
1551}
1552
1553int usb_device_del_addr(int bus_num, int addr)
1554{
1555 USBPort *port;
1556 USBPort **lastp;
1557 USBDevice *dev;
1558
1559 if (!used_usb_ports)
1560 return -1;
1561
1562 if (bus_num != 0)
1563 return -1;
1564
1565 lastp = &used_usb_ports;
1566 port = used_usb_ports;
1567 while (port && port->dev->addr != addr) {
1568 lastp = &port->next;
1569 port = port->next;
1570 }
1571
1572 if (!port)
1573 return -1;
1574
1575 dev = port->dev;
1576 *lastp = port->next;
1577 usb_attach(port, NULL);
1578 dev->handle_destroy(dev);
1579 port->next = free_usb_ports;
1580 free_usb_ports = port;
1581 return 0;
1582}
1583
1584static int usb_device_del(const char *devname)
1585{
1586 int bus_num, addr;
1587 const char *p;
1588
1589 if (strstart(devname, "host:", &p))
1590 return usb_host_device_close(p);
1591
1592 if (!used_usb_ports)
1593 return -1;
1594
1595 p = strchr(devname, '.');
1596 if (!p)
1597 return -1;
1598 bus_num = strtoul(devname, NULL, 0);
1599 addr = strtoul(p + 1, NULL, 0);
1600
1601 return usb_device_del_addr(bus_num, addr);
1602}
1603
1604void do_usb_add(Monitor *mon, const char *devname)
1605{
1606 usb_device_add(devname, 1);
1607}
1608
1609void do_usb_del(Monitor *mon, const char *devname)
1610{
1611 usb_device_del(devname);
1612}
1613
1614void usb_info(Monitor *mon)
1615{
1616 USBDevice *dev;
1617 USBPort *port;
1618 const char *speed_str;
1619
1620 if (!usb_enabled) {
1621 monitor_printf(mon, "USB support not enabled\n");
1622 return;
1623 }
1624
1625 for (port = used_usb_ports; port; port = port->next) {
1626 dev = port->dev;
1627 if (!dev)
1628 continue;
1629 switch(dev->speed) {
1630 case USB_SPEED_LOW:
1631 speed_str = "1.5";
1632 break;
1633 case USB_SPEED_FULL:
1634 speed_str = "12";
1635 break;
1636 case USB_SPEED_HIGH:
1637 speed_str = "480";
1638 break;
1639 default:
1640 speed_str = "?";
1641 break;
1642 }
1643 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1644 0, dev->addr, speed_str, dev->devname);
1645 }
1646}
1647
1648/***********************************************************/
1649/* PCMCIA/Cardbus */
1650
1651static struct pcmcia_socket_entry_s {
1652 PCMCIASocket *socket;
1653 struct pcmcia_socket_entry_s *next;
1654} *pcmcia_sockets = 0;
1655
1656void pcmcia_socket_register(PCMCIASocket *socket)
1657{
1658 struct pcmcia_socket_entry_s *entry;
1659
1660 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1661 entry->socket = socket;
1662 entry->next = pcmcia_sockets;
1663 pcmcia_sockets = entry;
1664}
1665
1666void pcmcia_socket_unregister(PCMCIASocket *socket)
1667{
1668 struct pcmcia_socket_entry_s *entry, **ptr;
1669
1670 ptr = &pcmcia_sockets;
1671 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1672 if (entry->socket == socket) {
1673 *ptr = entry->next;
1674 qemu_free(entry);
1675 }
1676}
1677
1678void pcmcia_info(Monitor *mon)
1679{
1680 struct pcmcia_socket_entry_s *iter;
1681
1682 if (!pcmcia_sockets)
1683 monitor_printf(mon, "No PCMCIA sockets\n");
1684
1685 for (iter = pcmcia_sockets; iter; iter = iter->next)
1686 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1687 iter->socket->attached ? iter->socket->card_string :
1688 "Empty");
1689}
1690
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001691/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001692/* machine registration */
1693
1694static QEMUMachine *first_machine = NULL;
1695QEMUMachine *current_machine = NULL;
1696
1697int qemu_register_machine(QEMUMachine *m)
1698{
1699 QEMUMachine **pm;
1700 pm = &first_machine;
1701 while (*pm != NULL)
1702 pm = &(*pm)->next;
1703 m->next = NULL;
1704 *pm = m;
1705 return 0;
1706}
1707
1708static QEMUMachine *find_machine(const char *name)
1709{
1710 QEMUMachine *m;
1711
1712 for(m = first_machine; m != NULL; m = m->next) {
1713 if (!strcmp(m->name, name))
1714 return m;
1715 }
1716 return NULL;
1717}
1718
1719static QEMUMachine *find_default_machine(void)
1720{
1721 QEMUMachine *m;
1722
1723 for(m = first_machine; m != NULL; m = m->next) {
1724 if (m->is_default) {
1725 return m;
1726 }
1727 }
1728 return NULL;
1729}
1730
1731/***********************************************************/
1732/* main execution loop */
1733
1734static void gui_update(void *opaque)
1735{
1736 uint64_t interval = GUI_REFRESH_INTERVAL;
1737 DisplayState *ds = opaque;
1738 DisplayChangeListener *dcl = ds->listeners;
1739
1740 dpy_refresh(ds);
1741
1742 while (dcl != NULL) {
1743 if (dcl->gui_timer_interval &&
1744 dcl->gui_timer_interval < interval)
1745 interval = dcl->gui_timer_interval;
1746 dcl = dcl->next;
1747 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001748 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001749}
1750
1751static void nographic_update(void *opaque)
1752{
1753 uint64_t interval = GUI_REFRESH_INTERVAL;
1754
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001755 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001756}
1757
1758struct vm_change_state_entry {
1759 VMChangeStateHandler *cb;
1760 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001761 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001762};
1763
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001764static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001765
1766VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1767 void *opaque)
1768{
1769 VMChangeStateEntry *e;
1770
1771 e = qemu_mallocz(sizeof (*e));
1772
1773 e->cb = cb;
1774 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001775 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001776 return e;
1777}
1778
1779void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1780{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001781 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001782 qemu_free (e);
1783}
1784
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001785void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001786{
1787 VMChangeStateEntry *e;
1788
1789 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1790 e->cb(e->opaque, running, reason);
1791 }
1792}
1793
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001794void vm_start(void)
1795{
1796 if (!vm_running) {
1797 cpu_enable_ticks();
1798 vm_running = 1;
1799 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001800 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001801 resume_all_vcpus();
1802 }
1803}
1804
1805/* reset/shutdown handler */
1806
1807typedef struct QEMUResetEntry {
1808 QEMUResetHandler *func;
1809 void *opaque;
1810 int order;
1811 struct QEMUResetEntry *next;
1812} QEMUResetEntry;
1813
1814static QEMUResetEntry *first_reset_entry;
1815static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001816static int shutdown_requested, shutdown_signal = -1;
1817static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001818static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001819int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001820static int vmstop_requested;
1821
1822int qemu_shutdown_requested(void)
1823{
1824 int r = shutdown_requested;
1825 shutdown_requested = 0;
1826 return r;
1827}
1828
1829int qemu_reset_requested(void)
1830{
1831 int r = reset_requested;
1832 reset_requested = 0;
1833 return r;
1834}
1835
1836int qemu_powerdown_requested(void)
1837{
1838 int r = powerdown_requested;
1839 powerdown_requested = 0;
1840 return r;
1841}
1842
1843static int qemu_debug_requested(void)
1844{
1845 int r = debug_requested;
1846 debug_requested = 0;
1847 return r;
1848}
1849
1850static int qemu_vmstop_requested(void)
1851{
1852 int r = vmstop_requested;
1853 vmstop_requested = 0;
1854 return r;
1855}
1856
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001857void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1858{
1859 QEMUResetEntry **pre, *re;
1860
1861 pre = &first_reset_entry;
1862 while (*pre != NULL && (*pre)->order >= order) {
1863 pre = &(*pre)->next;
1864 }
1865 re = qemu_mallocz(sizeof(QEMUResetEntry));
1866 re->func = func;
1867 re->opaque = opaque;
1868 re->order = order;
1869 re->next = NULL;
1870 *pre = re;
1871}
1872
1873void qemu_system_reset(void)
1874{
1875 QEMUResetEntry *re;
1876
1877 /* reset all devices */
1878 for(re = first_reset_entry; re != NULL; re = re->next) {
1879 re->func(re->opaque);
1880 }
1881}
1882
1883void qemu_system_reset_request(void)
1884{
1885 if (no_reboot) {
1886 shutdown_requested = 1;
1887 } else {
1888 reset_requested = 1;
1889 }
1890 qemu_notify_event();
1891}
1892
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001893void qemu_system_killed(int signal, pid_t pid)
1894{
1895 shutdown_signal = signal;
1896 shutdown_pid = pid;
1897 qemu_system_shutdown_request();
1898}
1899
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001900void qemu_system_shutdown_request(void)
1901{
1902 shutdown_requested = 1;
1903 qemu_notify_event();
1904}
1905
1906void qemu_system_powerdown_request(void)
1907{
1908 powerdown_requested = 1;
1909 qemu_notify_event();
1910}
1911
1912#ifdef CONFIG_IOTHREAD
1913static void qemu_system_vmstop_request(int reason)
1914{
1915 vmstop_requested = reason;
1916 qemu_notify_event();
1917}
1918#endif
1919
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001920void main_loop_wait(int timeout)
1921{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001922 fd_set rfds, wfds, xfds;
1923 int ret, nfds;
1924 struct timeval tv;
1925
1926 qemu_bh_update_timeout(&timeout);
1927
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001928 os_host_main_loop_wait(&timeout);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001929
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001930
1931 tv.tv_sec = timeout / 1000;
1932 tv.tv_usec = (timeout % 1000) * 1000;
1933
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001934 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001935
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001936 /* XXX: separate device handlers from system ones */
1937 nfds = -1;
1938 FD_ZERO(&rfds);
1939 FD_ZERO(&wfds);
1940 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001941 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001942 if (slirp_is_inited()) {
1943 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1944 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001945
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001946 qemu_mutex_unlock_iothread();
1947 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1948 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001949 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001950 if (slirp_is_inited()) {
1951 if (ret < 0) {
1952 FD_ZERO(&rfds);
1953 FD_ZERO(&wfds);
1954 FD_ZERO(&xfds);
1955 }
1956 slirp_select_poll(&rfds, &wfds, &xfds);
1957 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001958 charpipe_poll();
1959
David Turner6a9ef172010-09-09 22:54:36 +02001960 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07001961
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001962 /* Check bottom-halves last in case any of the earlier events triggered
1963 them. */
1964 qemu_bh_poll();
1965
1966}
1967
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001968static int vm_can_run(void)
1969{
1970 if (powerdown_requested)
1971 return 0;
1972 if (reset_requested)
1973 return 0;
1974 if (shutdown_requested)
1975 return 0;
1976 if (debug_requested)
1977 return 0;
1978 return 1;
1979}
1980
1981static void main_loop(void)
1982{
1983 int r;
1984
1985#ifdef CONFIG_IOTHREAD
1986 qemu_system_ready = 1;
1987 qemu_cond_broadcast(&qemu_system_cond);
1988#endif
1989
1990 for (;;) {
1991 do {
1992#ifdef CONFIG_PROFILER
1993 int64_t ti;
1994#endif
1995#ifndef CONFIG_IOTHREAD
1996 tcg_cpu_exec();
1997#endif
1998#ifdef CONFIG_PROFILER
1999 ti = profile_getclock();
2000#endif
2001 main_loop_wait(qemu_calculate_timeout());
2002#ifdef CONFIG_PROFILER
2003 dev_time += profile_getclock() - ti;
2004#endif
rich canningsd952f282011-03-01 15:40:09 -08002005
2006 if (rotate_logs_requested) {
2007 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
2008 dns_log_filename);
2009 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
2010 drop_log_filename);
2011 slirp_dns_log_fd(new_dns_log_fd);
2012 slirp_drop_log_fd(new_drop_log_fd);
2013 reset_rotate_qemu_logs_request();
2014 }
2015
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002016 } while (vm_can_run());
2017
2018 if (qemu_debug_requested())
2019 vm_stop(EXCP_DEBUG);
2020 if (qemu_shutdown_requested()) {
2021 if (no_shutdown) {
2022 vm_stop(0);
2023 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002024 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00002025 if (savevm_on_exit != NULL) {
2026 do_savevm(cur_mon, savevm_on_exit);
2027 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002028 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002029 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002030 }
2031 if (qemu_reset_requested()) {
2032 pause_all_vcpus();
2033 qemu_system_reset();
2034 resume_all_vcpus();
2035 }
2036 if (qemu_powerdown_requested())
2037 qemu_system_powerdown();
2038 if ((r = qemu_vmstop_requested()))
2039 vm_stop(r);
2040 }
2041 pause_all_vcpus();
2042}
2043
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002044void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002045{
2046 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2047}
2048
2049void qemu_help(int exitcode)
2050{
2051 version();
2052 printf("usage: %s [options] [disk_image]\n"
2053 "\n"
2054 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2055 "\n"
2056#define DEF(option, opt_arg, opt_enum, opt_help) \
2057 opt_help
2058#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002059#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002060#undef DEF
2061#undef DEFHEADING
2062#undef GEN_DOCS
2063 "\n"
2064 "During emulation, the following keys are useful:\n"
2065 "ctrl-alt-f toggle full screen\n"
2066 "ctrl-alt-n switch to virtual console 'n'\n"
2067 "ctrl-alt toggle mouse and keyboard grab\n"
2068 "\n"
2069 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2070 ,
2071 "qemu",
2072 DEFAULT_RAM_SIZE,
2073#ifndef _WIN32
2074 DEFAULT_NETWORK_SCRIPT,
2075 DEFAULT_NETWORK_DOWN_SCRIPT,
2076#endif
2077 DEFAULT_GDBSTUB_PORT,
2078 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002079 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002080}
2081
2082#define HAS_ARG 0x0001
2083
2084enum {
2085#define DEF(option, opt_arg, opt_enum, opt_help) \
2086 opt_enum,
2087#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002088#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002089#undef DEF
2090#undef DEFHEADING
2091#undef GEN_DOCS
2092};
2093
2094typedef struct QEMUOption {
2095 const char *name;
2096 int flags;
2097 int index;
2098} QEMUOption;
2099
2100static const QEMUOption qemu_options[] = {
2101 { "h", 0, QEMU_OPTION_h },
2102#define DEF(option, opt_arg, opt_enum, opt_help) \
2103 { option, opt_arg, opt_enum },
2104#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002105#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002106#undef DEF
2107#undef DEFHEADING
2108#undef GEN_DOCS
2109 { NULL, 0, 0 },
2110};
2111
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002112static void select_vgahw (const char *p)
2113{
2114 const char *opts;
2115
2116 cirrus_vga_enabled = 0;
2117 std_vga_enabled = 0;
2118 vmsvga_enabled = 0;
2119 xenfb_enabled = 0;
2120 if (strstart(p, "std", &opts)) {
2121 std_vga_enabled = 1;
2122 } else if (strstart(p, "cirrus", &opts)) {
2123 cirrus_vga_enabled = 1;
2124 } else if (strstart(p, "vmware", &opts)) {
2125 vmsvga_enabled = 1;
2126 } else if (strstart(p, "xenfb", &opts)) {
2127 xenfb_enabled = 1;
2128 } else if (!strstart(p, "none", &opts)) {
2129 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002130 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002131 }
2132 while (*opts) {
2133 const char *nextopt;
2134
2135 if (strstart(opts, ",retrace=", &nextopt)) {
2136 opts = nextopt;
2137 if (strstart(opts, "dumb", &nextopt))
2138 vga_retrace_method = VGA_RETRACE_DUMB;
2139 else if (strstart(opts, "precise", &nextopt))
2140 vga_retrace_method = VGA_RETRACE_PRECISE;
2141 else goto invalid_vga;
2142 } else goto invalid_vga;
2143 opts = nextopt;
2144 }
2145}
2146
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002147#define MAX_NET_CLIENTS 32
2148
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002149#ifdef _WIN32
2150/* Look for support files in the same directory as the executable. */
2151static char *find_datadir(const char *argv0)
2152{
2153 char *p;
2154 char buf[MAX_PATH];
2155 DWORD len;
2156
2157 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2158 if (len == 0) {
2159 return NULL;
2160 }
2161
2162 buf[len] = 0;
2163 p = buf + len - 1;
2164 while (p != buf && *p != '\\')
2165 p--;
2166 *p = 0;
2167 if (access(buf, R_OK) == 0) {
2168 return qemu_strdup(buf);
2169 }
2170 return NULL;
2171}
2172#else /* !_WIN32 */
2173
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002174/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002175static char *find_datadir(const char *argv0)
2176{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002177 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002178 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002179
2180#if defined(__linux__)
2181 {
2182 int len;
2183 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2184 if (len > 0) {
2185 buf[len] = 0;
2186 p = buf;
2187 }
2188 }
2189#elif defined(__FreeBSD__)
2190 {
2191 int len;
2192 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2193 if (len > 0) {
2194 buf[len] = 0;
2195 p = buf;
2196 }
2197 }
2198#endif
2199 /* If we don't have any way of figuring out the actual executable
2200 location then try argv[0]. */
2201 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002202 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002203 if (!p) {
2204 return NULL;
2205 }
2206 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002207
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002208 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002209}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002210#endif
2211
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002212static char*
2213qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
2214{
2215 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2216 char* buf = qemu_mallocz(len);
2217
2218 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2219 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2220 if (access(buf, R_OK)) {
2221 qemu_free(buf);
2222 return NULL;
2223 }
2224 return buf;
2225}
2226
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002227char *qemu_find_file(int type, const char *name)
2228{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002229 const char *subdir;
2230 char *buf;
2231
2232 /* If name contains path separators then try it as a straight path. */
2233 if ((strchr(name, '/') || strchr(name, '\\'))
2234 && access(name, R_OK) == 0) {
2235 return strdup(name);
2236 }
2237 switch (type) {
2238 case QEMU_FILE_TYPE_BIOS:
2239 subdir = "";
2240 break;
2241 case QEMU_FILE_TYPE_KEYMAP:
2242 subdir = "keymaps/";
2243 break;
2244 default:
2245 abort();
2246 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002247 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2248#ifdef CONFIG_ANDROID
2249 if (type == QEMU_FILE_TYPE_BIOS) {
2250 /* This case corresponds to the emulator being used as part of an
2251 * SDK installation. NOTE: data_dir is really $bindir. */
2252 if (buf == NULL)
2253 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2254 /* This case corresponds to platform builds. */
2255 if (buf == NULL)
2256 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2257 /* Finally, try this for standalone builds under external/qemu */
2258 if (buf == NULL)
2259 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002260 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002261#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002262 return buf;
2263}
2264
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002265static int
2266add_dns_server( const char* server_name )
2267{
2268 SockAddress addr;
2269
2270 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2271 fprintf(stdout,
2272 "### WARNING: can't resolve DNS server name '%s'\n",
2273 server_name );
2274 return -1;
2275 }
2276
2277 fprintf(stderr,
2278 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2279
2280 if ( slirp_add_dns_server( &addr ) < 0 ) {
2281 fprintf(stderr,
2282 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2283 return -1;
2284 }
2285 return 0;
2286}
2287
rich cannings7339b552011-02-16 13:43:44 -08002288/* Parses an integer
2289 * Pararm:
2290 * str String containing a number to be parsed.
2291 * result Passes the parsed integer in this argument
2292 * returns 0 if ok, -1 if failed
2293 */
2294int
2295parse_int(const char *str, int *result)
2296{
2297 char* r;
2298 *result = strtol(str, &r, 0);
2299 if (r == NULL || *r != '\0')
2300 return -1;
2301
2302 return 0;
2303}
2304
rich canningsd952f282011-03-01 15:40:09 -08002305#ifndef _WIN32
2306/*
2307 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
2308 */
2309void init_qemu_clear_logs_sig() {
2310 struct sigaction act;
2311 sigfillset(&act.sa_mask);
2312 act.sa_flags = 0;
2313 act.sa_handler = rotate_qemu_logs_handler;
2314 if (sigaction(SIGUSR1, &act, NULL) == -1) {
2315 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
2316 exit(-1);
2317 }
2318}
2319#endif
2320
2321
rich cannings7339b552011-02-16 13:43:44 -08002322
2323/* parses a null-terminated string specifying a network port (e.g., "80") or
2324 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2325 * are the same. Returns 0 on success, -1 on error. */
2326
2327int parse_port_range(const char *str, unsigned short *lport,
2328 unsigned short *hport) {
2329
2330 unsigned int low = 0, high = 0;
2331 char *p, *arg = strdup(str);
2332
2333 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2334 p = arg + 1; /* skip '[' */
2335 low = atoi(strtok(p, "-"));
2336 high = atoi(strtok(NULL, "-"));
2337 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2338 *lport = low;
2339 *hport = high;
2340 }
2341 }
2342 else {
2343 low = atoi(arg);
2344 if ((0 < low) && (low < 65535)) {
2345 *lport = low;
2346 *hport = low;
2347 }
2348 }
2349 free(arg);
2350 if (low != 0)
2351 return 0;
2352 return -1;
2353}
2354
2355/*
2356 * Implements the generic port forwarding option
2357 */
2358void
2359net_slirp_forward(const char *optarg)
2360{
2361 /*
2362 * we expect the following format:
2363 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2364 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2365 */
2366 char *argument = strdup(optarg), *p = argument;
2367 char *dst_net, *dst_mask, *dst_port;
2368 char *redirect_ip, *redirect_port;
2369 uint32_t dnet, dmask, rip;
2370 unsigned short dlport, dhport, rport;
2371
2372
2373 dst_net = strtok(p, ":");
2374 dst_mask = strtok(NULL, ":");
2375 dst_port = strtok(NULL, ":");
2376 redirect_ip = strtok(NULL, ":");
2377 redirect_port = strtok(NULL, ":");
2378
2379 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2380 redirect_ip == NULL || redirect_port == NULL) {
2381 fprintf(stderr,
2382 "Invalid argument for -net-forward, we expect "
2383 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2384 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2385 ":redirect_ip:redirect_port: %s\n",
2386 optarg);
2387 exit(1);
2388 }
2389
2390 /* inet_strtoip converts dotted address to host byte order */
2391 if (inet_strtoip(dst_net, &dnet) == -1) {
2392 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2393 exit(1);
2394 }
2395 if (inet_strtoip(dst_mask, &dmask) == -1) {
2396 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2397 exit(1);
2398 }
2399 if (inet_strtoip(redirect_ip, &rip) == -1) {
2400 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2401 exit(1);
2402 }
2403
2404 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2405 fprintf(stderr, "Invalid destination port or port range\n");
2406 exit(1);
2407 }
2408
2409 rport = atoi(redirect_port);
2410 if (!rport) {
2411 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2412 exit(1);
2413 }
2414
2415 dnet &= dmask;
2416
2417 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2418 rip, rport);
2419
2420 free(argument);
2421}
2422
2423
2424/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2425 * entry in the allows list */
2426void
2427slirp_allow(const char *optarg, u_int8_t proto)
2428{
2429 /*
2430 * we expect the following format:
2431 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2432 */
2433 char *argument = strdup(optarg), *p = argument;
2434 char *dst_ip_str, *dst_port_str;
2435 uint32_t dst_ip;
2436 unsigned short dst_lport, dst_hport;
2437
2438 dst_ip_str = strtok(p, ":");
2439 dst_port_str = strtok(NULL, ":");
2440
2441 if (dst_ip_str == NULL || dst_port_str == NULL) {
2442 fprintf(stderr,
2443 "Invalid argument %s for -allow. We expect "
2444 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2445 optarg);
2446 exit(1);
2447 }
2448
2449 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2450 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2451 exit(1);
2452 }
2453 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2454 fprintf(stderr, "Invalid destination port or port range\n");
2455 exit(1);
2456 }
2457
2458 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2459
2460 free(argument);
2461}
2462
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002463/* Add a serial device at a given location in the emulated hardware table.
2464 * On failure, this function aborts the program with an error message.
2465 */
2466static void
2467serial_hds_add_at(int index, const char* devname)
2468{
2469 char label[32];
2470
2471 if (!devname || !strcmp(devname,"none"))
2472 return;
2473
2474 if (index >= MAX_SERIAL_PORTS) {
2475 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2476 devname, index, MAX_SERIAL_PORTS);
2477 }
2478 if (serial_hds[index] != NULL) {
2479 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2480 devname, index);
2481 }
2482 snprintf(label, sizeof(label), "serial%d", index);
2483 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2484 if (!serial_hds[index]) {
2485 PANIC("qemu: could not open serial device '%s'", devname);
2486 }
2487}
2488
2489
2490/* Find a free slot in the emulated serial device table, and register
2491 * it. Return the allocated table index.
2492 */
2493static int
2494serial_hds_add(const char* devname)
2495{
2496 int index;
2497
2498 /* Find first free slot */
2499 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2500 if (serial_hds[index] == NULL) {
2501 serial_hds_add_at(index, devname);
2502 return index;
2503 }
2504 }
2505
2506 PANIC("qemu: too many serial devices registered (%d)", index);
2507 return -1; /* shouldn't happen */
2508}
2509
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002510int main(int argc, char **argv, char **envp)
2511{
2512 const char *gdbstub_dev = NULL;
2513 uint32_t boot_devices_bitmap = 0;
2514 int i;
2515 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002516 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002517 const char *initrd_filename;
2518 const char *kernel_filename, *kernel_cmdline;
2519 const char *boot_devices = "";
2520 DisplayState *ds;
2521 DisplayChangeListener *dcl;
2522 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002523 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002524 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002525 const char *net_clients[MAX_NET_CLIENTS];
2526 int nb_net_clients;
2527 const char *bt_opts[MAX_BT_CMDLINE];
2528 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002529 int optind;
2530 const char *r, *optarg;
2531 CharDriverState *monitor_hd = NULL;
2532 const char *monitor_device;
2533 const char *serial_devices[MAX_SERIAL_PORTS];
2534 int serial_device_index;
2535 const char *parallel_devices[MAX_PARALLEL_PORTS];
2536 int parallel_device_index;
2537 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2538 int virtio_console_index;
2539 const char *loadvm = NULL;
2540 QEMUMachine *machine;
2541 const char *cpu_model;
2542 const char *usb_devices[MAX_USB_CMDLINE];
2543 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002544 int tb_size;
2545 const char *pid_file = NULL;
2546 const char *incoming = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002547 CPUState *env;
2548 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002549 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002550 STRALLOC_DEFINE(kernel_params);
2551 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002552 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002553
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002554 /* Initialize sockets before anything else, so we can properly report
2555 * initialization failures back to the UI. */
2556#ifdef _WIN32
2557 socket_init();
2558#endif
2559
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002560 init_clocks();
2561
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002562 qemu_cache_utils_init(envp);
2563
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002564 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002565 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002566
2567 module_call_init(MODULE_INIT_MACHINE);
2568 machine = find_default_machine();
2569 cpu_model = NULL;
2570 initrd_filename = NULL;
2571 ram_size = 0;
2572 snapshot = 0;
2573 kernel_filename = NULL;
2574 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002575
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002576 cyls = heads = secs = 0;
2577 translation = BIOS_ATA_TRANSLATION_AUTO;
2578 monitor_device = "vc:80Cx24C";
2579
2580 serial_devices[0] = "vc:80Cx24C";
2581 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2582 serial_devices[i] = NULL;
2583 serial_device_index = 0;
2584
2585 parallel_devices[0] = "vc:80Cx24C";
2586 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2587 parallel_devices[i] = NULL;
2588 parallel_device_index = 0;
2589
2590 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2591 virtio_consoles[i] = NULL;
2592 virtio_console_index = 0;
2593
2594 for (i = 0; i < MAX_NODES; i++) {
2595 node_mem[i] = 0;
2596 node_cpumask[i] = 0;
2597 }
2598
2599 usb_devices_index = 0;
2600
2601 nb_net_clients = 0;
2602 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002603#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002604 nb_drives = 0;
2605 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002606#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002607 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002608
2609 nb_nics = 0;
2610
2611 tb_size = 0;
2612 autostart= 1;
2613
2614 register_watchdogs();
2615
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002616 /* Initialize boot properties. */
2617 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002618 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002619 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002620
David 'Digit' Turner36597752011-05-20 01:18:01 +02002621#ifdef CONFIG_KVM
2622 /* By default, force auto-detection for kvm */
2623 kvm_allowed = -1;
2624#endif
2625
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002626 optind = 1;
2627 for(;;) {
2628 if (optind >= argc)
2629 break;
2630 r = argv[optind];
2631 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002632 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002633 } else {
2634 const QEMUOption *popt;
2635
2636 optind++;
2637 /* Treat --foo the same as -foo. */
2638 if (r[1] == '-')
2639 r++;
2640 popt = qemu_options;
2641 for(;;) {
2642 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002643 PANIC("%s: invalid option -- '%s'",
2644 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002645 }
2646 if (!strcmp(popt->name, r + 1))
2647 break;
2648 popt++;
2649 }
2650 if (popt->flags & HAS_ARG) {
2651 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002652 PANIC("%s: option '%s' requires an argument",
2653 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002654 }
2655 optarg = argv[optind++];
2656 } else {
2657 optarg = NULL;
2658 }
2659
2660 switch(popt->index) {
2661 case QEMU_OPTION_M:
2662 machine = find_machine(optarg);
2663 if (!machine) {
2664 QEMUMachine *m;
2665 printf("Supported machines are:\n");
2666 for(m = first_machine; m != NULL; m = m->next) {
2667 printf("%-10s %s%s\n",
2668 m->name, m->desc,
2669 m->is_default ? " (default)" : "");
2670 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002671 if (*optarg != '?') {
2672 PANIC("Invalid machine parameter: %s",
2673 optarg);
2674 } else {
2675 QEMU_EXIT(0);
2676 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002677 }
2678 break;
2679 case QEMU_OPTION_cpu:
2680 /* hw initialization will check this */
2681 if (*optarg == '?') {
2682/* XXX: implement xxx_cpu_list for targets that still miss it */
2683#if defined(cpu_list)
2684 cpu_list(stdout, &fprintf);
2685#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002686 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002687 } else {
2688 cpu_model = optarg;
2689 }
2690 break;
2691 case QEMU_OPTION_initrd:
2692 initrd_filename = optarg;
2693 break;
2694 case QEMU_OPTION_hda:
2695 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002696 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002697 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002698 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002699 ",cyls=%d,heads=%d,secs=%d%s",
2700 0, cyls, heads, secs,
2701 translation == BIOS_ATA_TRANSLATION_LBA ?
2702 ",trans=lba" :
2703 translation == BIOS_ATA_TRANSLATION_NONE ?
2704 ",trans=none" : "");
2705 break;
2706 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002707 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2708 break;
2709
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002710 case QEMU_OPTION_hdc:
2711 case QEMU_OPTION_hdd:
2712 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2713 break;
2714 case QEMU_OPTION_drive:
2715 drive_add(NULL, "%s", optarg);
2716 break;
2717 case QEMU_OPTION_mtdblock:
2718 drive_add(optarg, MTD_ALIAS);
2719 break;
2720 case QEMU_OPTION_sd:
2721 drive_add(optarg, SD_ALIAS);
2722 break;
2723 case QEMU_OPTION_pflash:
2724 drive_add(optarg, PFLASH_ALIAS);
2725 break;
2726 case QEMU_OPTION_snapshot:
2727 snapshot = 1;
2728 break;
2729 case QEMU_OPTION_hdachs:
2730 {
2731 const char *p;
2732 p = optarg;
2733 cyls = strtol(p, (char **)&p, 0);
2734 if (cyls < 1 || cyls > 16383)
2735 goto chs_fail;
2736 if (*p != ',')
2737 goto chs_fail;
2738 p++;
2739 heads = strtol(p, (char **)&p, 0);
2740 if (heads < 1 || heads > 16)
2741 goto chs_fail;
2742 if (*p != ',')
2743 goto chs_fail;
2744 p++;
2745 secs = strtol(p, (char **)&p, 0);
2746 if (secs < 1 || secs > 63)
2747 goto chs_fail;
2748 if (*p == ',') {
2749 p++;
2750 if (!strcmp(p, "none"))
2751 translation = BIOS_ATA_TRANSLATION_NONE;
2752 else if (!strcmp(p, "lba"))
2753 translation = BIOS_ATA_TRANSLATION_LBA;
2754 else if (!strcmp(p, "auto"))
2755 translation = BIOS_ATA_TRANSLATION_AUTO;
2756 else
2757 goto chs_fail;
2758 } else if (*p != '\0') {
2759 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002760 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002761 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002762 if (hda_opts != NULL) {
2763 char num[16];
2764 snprintf(num, sizeof(num), "%d", cyls);
2765 qemu_opt_set(hda_opts, "cyls", num);
2766 snprintf(num, sizeof(num), "%d", heads);
2767 qemu_opt_set(hda_opts, "heads", num);
2768 snprintf(num, sizeof(num), "%d", secs);
2769 qemu_opt_set(hda_opts, "secs", num);
2770 if (translation == BIOS_ATA_TRANSLATION_LBA)
2771 qemu_opt_set(hda_opts, "trans", "lba");
2772 if (translation == BIOS_ATA_TRANSLATION_NONE)
2773 qemu_opt_set(hda_opts, "trans", "none");
2774 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002775 }
2776 break;
2777 case QEMU_OPTION_numa:
2778 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002779 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002780 }
2781 numa_add(optarg);
2782 break;
2783 case QEMU_OPTION_nographic:
2784 display_type = DT_NOGRAPHIC;
2785 break;
2786#ifdef CONFIG_CURSES
2787 case QEMU_OPTION_curses:
2788 display_type = DT_CURSES;
2789 break;
2790#endif
2791 case QEMU_OPTION_portrait:
2792 graphic_rotate = 1;
2793 break;
2794 case QEMU_OPTION_kernel:
2795 kernel_filename = optarg;
2796 break;
2797 case QEMU_OPTION_append:
2798 kernel_cmdline = optarg;
2799 break;
2800 case QEMU_OPTION_cdrom:
2801 drive_add(optarg, CDROM_ALIAS);
2802 break;
2803 case QEMU_OPTION_boot:
2804 boot_devices = optarg;
2805 /* We just do some generic consistency checks */
2806 {
2807 /* Could easily be extended to 64 devices if needed */
2808 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002809
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002810 boot_devices_bitmap = 0;
2811 for (p = boot_devices; *p != '\0'; p++) {
2812 /* Allowed boot devices are:
2813 * a b : floppy disk drives
2814 * c ... f : IDE disk drives
2815 * g ... m : machine implementation dependant drives
2816 * n ... p : network devices
2817 * It's up to each machine implementation to check
2818 * if the given boot devices match the actual hardware
2819 * implementation and firmware features.
2820 */
2821 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002822 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002823 }
2824 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002825 PANIC(
2826 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002827 }
2828 boot_devices_bitmap |= 1 << (*p - 'a');
2829 }
2830 }
2831 break;
2832 case QEMU_OPTION_fda:
2833 case QEMU_OPTION_fdb:
2834 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2835 break;
2836#ifdef TARGET_I386
2837 case QEMU_OPTION_no_fd_bootchk:
2838 fd_bootchk = 0;
2839 break;
2840#endif
2841 case QEMU_OPTION_net:
2842 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002843 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002844 }
2845 net_clients[nb_net_clients] = optarg;
2846 nb_net_clients++;
2847 break;
2848#ifdef CONFIG_SLIRP
2849 case QEMU_OPTION_tftp:
2850 tftp_prefix = optarg;
2851 break;
2852 case QEMU_OPTION_bootp:
2853 bootp_filename = optarg;
2854 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002855 case QEMU_OPTION_redir:
2856 net_slirp_redir(NULL, optarg, NULL);
2857 break;
2858#endif
2859 case QEMU_OPTION_bt:
2860 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002861 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002862 }
2863 bt_opts[nb_bt_opts++] = optarg;
2864 break;
2865#ifdef HAS_AUDIO
2866 case QEMU_OPTION_audio_help:
2867 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002868 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002869 break;
2870 case QEMU_OPTION_soundhw:
2871 select_soundhw (optarg);
2872 break;
2873#endif
2874 case QEMU_OPTION_h:
2875 qemu_help(0);
2876 break;
2877 case QEMU_OPTION_version:
2878 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002879 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002880 break;
2881 case QEMU_OPTION_m: {
2882 uint64_t value;
2883 char *ptr;
2884
2885 value = strtoul(optarg, &ptr, 10);
2886 switch (*ptr) {
2887 case 0: case 'M': case 'm':
2888 value <<= 20;
2889 break;
2890 case 'G': case 'g':
2891 value <<= 30;
2892 break;
2893 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002894 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002895 }
2896
2897 /* On 32-bit hosts, QEMU is limited by virtual address space */
2898 if (value > (2047 << 20)
2899#ifndef CONFIG_KQEMU
2900 && HOST_LONG_BITS == 32
2901#endif
2902 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002903 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002904 }
2905 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002906 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002907 }
2908 ram_size = value;
2909 break;
2910 }
2911 case QEMU_OPTION_d:
2912 {
2913 int mask;
2914 const CPULogItem *item;
2915
2916 mask = cpu_str_to_log_mask(optarg);
2917 if (!mask) {
2918 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002919 for(item = cpu_log_items; item->mask != 0; item++) {
2920 printf("%-10s %s\n", item->name, item->help);
2921 }
2922 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002923 }
2924 cpu_set_log(mask);
2925 }
2926 break;
2927 case QEMU_OPTION_s:
2928 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2929 break;
2930 case QEMU_OPTION_gdb:
2931 gdbstub_dev = optarg;
2932 break;
2933 case QEMU_OPTION_L:
2934 data_dir = optarg;
2935 break;
2936 case QEMU_OPTION_bios:
2937 bios_name = optarg;
2938 break;
2939 case QEMU_OPTION_singlestep:
2940 singlestep = 1;
2941 break;
2942 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002943 autostart = 0;
2944 break;
2945#ifndef _WIN32
2946 case QEMU_OPTION_k:
2947 keyboard_layout = optarg;
2948 break;
2949#endif
2950 case QEMU_OPTION_localtime:
2951 rtc_utc = 0;
2952 break;
2953 case QEMU_OPTION_vga:
2954 select_vgahw (optarg);
2955 break;
2956#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2957 case QEMU_OPTION_g:
2958 {
2959 const char *p;
2960 int w, h, depth;
2961 p = optarg;
2962 w = strtol(p, (char **)&p, 10);
2963 if (w <= 0) {
2964 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002965 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002966 }
2967 if (*p != 'x')
2968 goto graphic_error;
2969 p++;
2970 h = strtol(p, (char **)&p, 10);
2971 if (h <= 0)
2972 goto graphic_error;
2973 if (*p == 'x') {
2974 p++;
2975 depth = strtol(p, (char **)&p, 10);
2976 if (depth != 8 && depth != 15 && depth != 16 &&
2977 depth != 24 && depth != 32)
2978 goto graphic_error;
2979 } else if (*p == '\0') {
2980 depth = graphic_depth;
2981 } else {
2982 goto graphic_error;
2983 }
2984
2985 graphic_width = w;
2986 graphic_height = h;
2987 graphic_depth = depth;
2988 }
2989 break;
2990#endif
2991 case QEMU_OPTION_echr:
2992 {
2993 char *r;
2994 term_escape_char = strtol(optarg, &r, 0);
2995 if (r == optarg)
2996 printf("Bad argument to echr\n");
2997 break;
2998 }
2999 case QEMU_OPTION_monitor:
3000 monitor_device = optarg;
3001 break;
3002 case QEMU_OPTION_serial:
3003 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003004 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003005 }
3006 serial_devices[serial_device_index] = optarg;
3007 serial_device_index++;
3008 break;
3009 case QEMU_OPTION_watchdog:
3010 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003011 if (i > 0) {
3012 if (i == 1) {
3013 PANIC("Invalid watchdog parameter: %s",
3014 optarg);
3015 } else {
3016 QEMU_EXIT(0);
3017 }
3018 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003019 break;
3020 case QEMU_OPTION_watchdog_action:
3021 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003022 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003023 }
3024 break;
3025 case QEMU_OPTION_virtiocon:
3026 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003027 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003028 }
3029 virtio_consoles[virtio_console_index] = optarg;
3030 virtio_console_index++;
3031 break;
3032 case QEMU_OPTION_parallel:
3033 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003034 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003035 }
3036 parallel_devices[parallel_device_index] = optarg;
3037 parallel_device_index++;
3038 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003039 case QEMU_OPTION_loadvm:
3040 loadvm = optarg;
3041 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003042 case QEMU_OPTION_savevm_on_exit:
3043 savevm_on_exit = optarg;
3044 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003045 case QEMU_OPTION_full_screen:
3046 full_screen = 1;
3047 break;
3048#ifdef CONFIG_SDL
3049 case QEMU_OPTION_no_frame:
3050 no_frame = 1;
3051 break;
3052 case QEMU_OPTION_alt_grab:
3053 alt_grab = 1;
3054 break;
3055 case QEMU_OPTION_no_quit:
3056 no_quit = 1;
3057 break;
3058 case QEMU_OPTION_sdl:
3059 display_type = DT_SDL;
3060 break;
3061#endif
3062 case QEMU_OPTION_pidfile:
3063 pid_file = optarg;
3064 break;
3065#ifdef TARGET_I386
3066 case QEMU_OPTION_win2k_hack:
3067 win2k_install_hack = 1;
3068 break;
3069 case QEMU_OPTION_rtc_td_hack:
3070 rtc_td_hack = 1;
3071 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003072#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003073 case QEMU_OPTION_acpitable:
3074 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003075 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003076 }
3077 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003078#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003079 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02003080 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003081 break;
3082#endif
3083#ifdef CONFIG_KVM
3084 case QEMU_OPTION_enable_kvm:
3085 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003086 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02003087 case QEMU_OPTION_disable_kvm:
3088 kvm_allowed = 0;
3089 break;
3090#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003091 case QEMU_OPTION_usb:
3092 usb_enabled = 1;
3093 break;
3094 case QEMU_OPTION_usbdevice:
3095 usb_enabled = 1;
3096 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003097 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003098 }
3099 usb_devices[usb_devices_index] = optarg;
3100 usb_devices_index++;
3101 break;
3102 case QEMU_OPTION_smp:
3103 smp_cpus = atoi(optarg);
3104 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003105 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003106 }
3107 break;
3108 case QEMU_OPTION_vnc:
3109 display_type = DT_VNC;
3110 vnc_display = optarg;
3111 break;
3112#ifdef TARGET_I386
3113 case QEMU_OPTION_no_acpi:
3114 acpi_enabled = 0;
3115 break;
3116 case QEMU_OPTION_no_hpet:
3117 no_hpet = 1;
3118 break;
3119 case QEMU_OPTION_no_virtio_balloon:
3120 no_virtio_balloon = 1;
3121 break;
3122#endif
3123 case QEMU_OPTION_no_reboot:
3124 no_reboot = 1;
3125 break;
3126 case QEMU_OPTION_no_shutdown:
3127 no_shutdown = 1;
3128 break;
3129 case QEMU_OPTION_show_cursor:
3130 cursor_hide = 0;
3131 break;
3132 case QEMU_OPTION_uuid:
3133 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003134 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003135 }
3136 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003137 case QEMU_OPTION_option_rom:
3138 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003139 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003140 }
3141 option_rom[nb_option_roms] = optarg;
3142 nb_option_roms++;
3143 break;
3144#if defined(TARGET_ARM) || defined(TARGET_M68K)
3145 case QEMU_OPTION_semihosting:
3146 semihosting_enabled = 1;
3147 break;
3148#endif
3149 case QEMU_OPTION_name:
3150 qemu_name = optarg;
3151 break;
3152#if defined(TARGET_SPARC) || defined(TARGET_PPC)
3153 case QEMU_OPTION_prom_env:
3154 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003155 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003156 }
3157 prom_envs[nb_prom_envs] = optarg;
3158 nb_prom_envs++;
3159 break;
3160#endif
3161#ifdef TARGET_ARM
3162 case QEMU_OPTION_old_param:
3163 old_param = 1;
3164 break;
3165#endif
3166 case QEMU_OPTION_clock:
3167 configure_alarms(optarg);
3168 break;
3169 case QEMU_OPTION_startdate:
3170 {
3171 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02003172 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003173 if (!strcmp(optarg, "now")) {
3174 rtc_date_offset = -1;
3175 } else {
3176 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
3177 &tm.tm_year,
3178 &tm.tm_mon,
3179 &tm.tm_mday,
3180 &tm.tm_hour,
3181 &tm.tm_min,
3182 &tm.tm_sec) == 6) {
3183 /* OK */
3184 } else if (sscanf(optarg, "%d-%d-%d",
3185 &tm.tm_year,
3186 &tm.tm_mon,
3187 &tm.tm_mday) == 3) {
3188 tm.tm_hour = 0;
3189 tm.tm_min = 0;
3190 tm.tm_sec = 0;
3191 } else {
3192 goto date_fail;
3193 }
3194 tm.tm_year -= 1900;
3195 tm.tm_mon--;
3196 rtc_start_date = mktimegm(&tm);
3197 if (rtc_start_date == -1) {
3198 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003199 PANIC("Invalid date format. Valid format are:\n"
3200 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003201 }
3202 rtc_date_offset = time(NULL) - rtc_start_date;
3203 }
3204 }
3205 break;
rich cannings7339b552011-02-16 13:43:44 -08003206
3207 /* -------------------------------------------------------*/
3208 /* User mode network stack restrictions */
3209 case QEMU_OPTION_drop_udp:
3210 slirp_drop_udp();
3211 break;
3212 case QEMU_OPTION_drop_tcp:
3213 slirp_drop_tcp();
3214 break;
3215 case QEMU_OPTION_allow_tcp:
3216 slirp_allow(optarg, IPPROTO_TCP);
3217 break;
3218 case QEMU_OPTION_allow_udp:
3219 slirp_allow(optarg, IPPROTO_UDP);
3220 break;
3221 case QEMU_OPTION_drop_log:
3222 {
3223 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003224 drop_log_filename = optarg;
3225 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08003226
3227 if (!drop_log_fd) {
3228 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
3229 exit(1);
3230 }
3231
3232 slirp_drop_log_fd(drop_log_fd);
3233 }
3234 break;
3235
3236 case QEMU_OPTION_dns_log:
3237 {
3238 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003239 dns_log_filename = optarg;
3240 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08003241
3242 if (dns_log_fd == NULL) {
3243 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
3244 exit(1);
3245 }
3246
3247 slirp_dns_log_fd(dns_log_fd);
3248 }
3249 break;
3250
3251
3252 case QEMU_OPTION_max_dns_conns:
3253 {
3254 int max_dns_conns = 0;
3255 if (parse_int(optarg, &max_dns_conns)) {
3256 fprintf(stderr,
3257 "qemu: syntax: -max-dns-conns max_connections\n");
3258 exit(1);
3259 }
3260 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3261 fprintf(stderr,
3262 "Invalid arg for max dns connections: %s\n",
3263 optarg);
3264 exit(1);
3265 }
3266 slirp_set_max_dns_conns(max_dns_conns);
3267 }
3268 break;
3269
3270 case QEMU_OPTION_net_forward:
3271 net_slirp_forward(optarg);
3272 break;
3273 case QEMU_OPTION_net_forward_tcp2sink:
3274 {
3275 SockAddress saddr;
3276
3277 if (parse_host_port(&saddr, optarg)) {
3278 fprintf(stderr,
3279 "Invalid ip/port %s for "
3280 "-forward-dropped-tcp2sink. "
3281 "We expect 'sink_ip:sink_port'\n",
3282 optarg);
3283 exit(1);
3284 }
3285 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3286 saddr.u.inet.port);
3287 }
3288 break;
3289 /* -------------------------------------------------------*/
3290
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003291 case QEMU_OPTION_tb_size:
3292 tb_size = strtol(optarg, NULL, 0);
3293 if (tb_size < 0)
3294 tb_size = 0;
3295 break;
3296 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003297 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003298 break;
3299 case QEMU_OPTION_incoming:
3300 incoming = optarg;
3301 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003302#ifdef CONFIG_XEN
3303 case QEMU_OPTION_xen_domid:
3304 xen_domid = atoi(optarg);
3305 break;
3306 case QEMU_OPTION_xen_create:
3307 xen_mode = XEN_CREATE;
3308 break;
3309 case QEMU_OPTION_xen_attach:
3310 xen_mode = XEN_ATTACH;
3311 break;
3312#endif
3313
3314
3315 case QEMU_OPTION_mic:
3316 audio_input_source = (char*)optarg;
3317 break;
3318#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003319 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003320 trace_filename = optarg;
3321 tracing = 1;
3322 break;
3323#if 0
3324 case QEMU_OPTION_trace_miss:
3325 trace_cache_miss = 1;
3326 break;
3327 case QEMU_OPTION_trace_addr:
3328 trace_all_addr = 1;
3329 break;
3330#endif
3331 case QEMU_OPTION_tracing:
3332 if (strcmp(optarg, "off") == 0)
3333 tracing = 0;
3334 else if (strcmp(optarg, "on") == 0 && trace_filename)
3335 tracing = 1;
3336 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003337 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003338 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003339 }
3340 break;
3341#if 0
3342 case QEMU_OPTION_dcache_load_miss:
3343 dcache_load_miss_penalty = atoi(optarg);
3344 break;
3345 case QEMU_OPTION_dcache_store_miss:
3346 dcache_store_miss_penalty = atoi(optarg);
3347 break;
3348#endif
3349#endif
3350#ifdef CONFIG_NAND
3351 case QEMU_OPTION_nand:
3352 nand_add_dev(optarg);
3353 break;
3354#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003355 case QEMU_OPTION_android_ports:
3356 android_op_ports = (char*)optarg;
3357 break;
3358
3359 case QEMU_OPTION_android_port:
3360 android_op_port = (char*)optarg;
3361 break;
3362
3363 case QEMU_OPTION_android_report_console:
3364 android_op_report_console = (char*)optarg;
3365 break;
3366
3367 case QEMU_OPTION_http_proxy:
3368 op_http_proxy = (char*)optarg;
3369 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003370
3371 case QEMU_OPTION_charmap:
3372 op_charmap_file = (char*)optarg;
3373 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003374
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003375 case QEMU_OPTION_android_hw:
3376 android_op_hwini = (char*)optarg;
3377 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003378
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003379 case QEMU_OPTION_dns_server:
3380 android_op_dns_server = (char*)optarg;
3381 break;
3382
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003383 case QEMU_OPTION_radio:
3384 android_op_radio = (char*)optarg;
3385 break;
3386
3387 case QEMU_OPTION_gps:
3388 android_op_gps = (char*)optarg;
3389 break;
3390
3391 case QEMU_OPTION_audio:
3392 android_op_audio = (char*)optarg;
3393 break;
3394
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003395 case QEMU_OPTION_cpu_delay:
3396 android_op_cpu_delay = (char*)optarg;
3397 break;
3398
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003399 case QEMU_OPTION_show_kernel:
3400 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3401 break;
3402
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003403#ifdef CONFIG_NAND_LIMITS
3404 case QEMU_OPTION_nand_limits:
3405 android_op_nand_limits = (char*)optarg;
3406 break;
3407#endif // CONFIG_NAND_LIMITS
3408
3409 case QEMU_OPTION_netspeed:
3410 android_op_netspeed = (char*)optarg;
3411 break;
3412
3413 case QEMU_OPTION_netdelay:
3414 android_op_netdelay = (char*)optarg;
3415 break;
3416
3417 case QEMU_OPTION_netfast:
3418 android_op_netfast = 1;
3419 break;
3420
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003421 case QEMU_OPTION_tcpdump:
3422 android_op_tcpdump = (char*)optarg;
3423 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003424
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003425 case QEMU_OPTION_boot_property:
3426 boot_property_parse_option((char*)optarg);
3427 break;
3428
3429 case QEMU_OPTION_lcd_density:
3430 android_op_lcd_density = (char*)optarg;
3431 break;
3432
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003433 case QEMU_OPTION_ui_port:
3434 android_op_ui_port = (char*)optarg;
3435 break;
3436
3437 case QEMU_OPTION_ui_settings:
3438 android_op_ui_settings = (char*)optarg;
3439 break;
3440
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003441 case QEMU_OPTION_audio_test_out:
3442 android_audio_test_start_out();
3443 break;
3444
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003445 case QEMU_OPTION_android_avdname:
3446 android_op_avd_name = (char*)optarg;
3447 break;
3448
3449 case QEMU_OPTION_timezone:
3450 if (timezone_set((char*)optarg)) {
3451 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3452 (char*)optarg);
3453 }
3454 break;
3455
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003456#ifdef CONFIG_MEMCHECK
3457 case QEMU_OPTION_android_memcheck:
3458 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003459 /* This will set ro.kernel.memcheck system property
3460 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003461 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003462 break;
3463#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003464
3465 case QEMU_OPTION_snapshot_no_time_update:
3466 android_snapshot_update_time = 0;
3467 break;
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003468 default:
3469 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003470 }
3471 }
3472 }
3473
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003474 /* Initialize character map. */
3475 if (android_charmap_setup(op_charmap_file)) {
3476 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003477 PANIC(
3478 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003479 op_charmap_file);
3480 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003481 PANIC(
3482 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003483 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003484 }
3485
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003486 /* If no data_dir is specified then try to find it relative to the
3487 executable path. */
3488 if (!data_dir) {
3489 data_dir = find_datadir(argv[0]);
3490 }
3491 /* If all else fails use the install patch specified when building. */
3492 if (!data_dir) {
3493 data_dir = CONFIG_QEMU_SHAREDIR;
3494 }
3495
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003496 if (!android_op_hwini) {
3497 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003498 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003499 hw_ini = iniFile_newFromFile(android_op_hwini);
3500 if (hw_ini == NULL) {
3501 PANIC("Could not find %s file.", android_op_hwini);
3502 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003503
3504 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003505 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003506
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003507 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003508
3509 {
3510 int width = android_hw->hw_lcd_width;
3511 int height = android_hw->hw_lcd_height;
3512 int depth = android_hw->hw_lcd_depth;
3513
3514 /* A bit of sanity checking */
3515 if (width <= 0 || height <= 0 ||
3516 (depth != 16 && depth != 32) ||
3517 (((width|height) & 3) != 0) )
3518 {
3519 PANIC("Invalid display configuration (%d,%d,%d)",
3520 width, height, depth);
3521 }
3522 android_display_width = width;
3523 android_display_height = height;
3524 android_display_bpp = depth;
3525 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003526
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003527#ifdef CONFIG_NAND_LIMITS
3528 /* Init nand stuff. */
3529 if (android_op_nand_limits) {
3530 parse_nand_limits(android_op_nand_limits);
3531 }
3532#endif // CONFIG_NAND_LIMITS
3533
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003534 /* Initialize AVD name from hardware configuration if needed */
3535 if (!android_op_avd_name) {
3536 if (android_hw->avd_name && *android_hw->avd_name) {
3537 android_op_avd_name = android_hw->avd_name;
3538 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3539 }
3540 }
3541
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003542 /* Initialize system partition image */
3543 {
3544 char tmp[PATH_MAX+32];
3545 const char* sysImage = android_hw->disk_systemPartition_path;
3546 const char* initImage = android_hw->disk_systemPartition_initPath;
3547 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3548
3549 if (sysBytes == 0) {
3550 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
3551 }
3552
3553 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
3554
3555 if (sysImage && *sysImage) {
3556 if (filelock_create(sysImage) == NULL) {
3557 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3558 /* If there is no file= parameters, nand_add_dev will create
3559 * a temporary file to back the partition image. */
3560 } else {
3561 pstrcat(tmp,sizeof(tmp),",file=");
3562 pstrcat(tmp,sizeof(tmp),sysImage);
3563 }
3564 }
3565 if (initImage && *initImage) {
3566 if (!path_exists(initImage)) {
3567 PANIC("Invalid initial system image path: %s", initImage);
3568 }
3569 pstrcat(tmp,sizeof(tmp),",initfile=");
3570 pstrcat(tmp,sizeof(tmp),initImage);
3571 } else {
3572 PANIC("Missing initial system image path!");
3573 }
3574 nand_add_dev(tmp);
3575 }
3576
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003577 /* Initialize data partition image */
3578 {
3579 char tmp[PATH_MAX+32];
3580 const char* dataImage = android_hw->disk_dataPartition_path;
3581 const char* initImage = android_hw->disk_dataPartition_initPath;
3582 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3583
3584 if (dataBytes == 0) {
3585 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
3586 }
3587
3588 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
3589
3590 if (dataImage && *dataImage) {
3591 if (filelock_create(dataImage) == NULL) {
3592 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3593 /* Note: if there is no file= parameters, nand_add_dev() will
3594 * create a temporary file to back the partition image. */
3595 } else {
3596 /* Create the file if needed */
3597 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003598 if (path_empty_file(dataImage) < 0) {
3599 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3600 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003601 }
3602 pstrcat(tmp, sizeof(tmp), ",file=");
3603 pstrcat(tmp, sizeof(tmp), dataImage);
3604 }
3605 }
3606 if (initImage && *initImage) {
3607 pstrcat(tmp, sizeof(tmp), ",initfile=");
3608 pstrcat(tmp, sizeof(tmp), initImage);
3609 }
3610 nand_add_dev(tmp);
3611 }
3612
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003613 /* Init SD-Card stuff. For Android, it is always hda */
3614 /* If the -hda option was used, ignore the Android-provided one */
3615 if (hda_opts == NULL) {
3616 const char* sdPath = android_hw->hw_sdCard_path;
3617 if (sdPath && *sdPath) {
3618 if (!path_exists(sdPath)) {
3619 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3620 } else if (filelock_create(sdPath) == NULL) {
3621 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3622 } else {
3623 /* Successful locking */
3624 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003625 /* Set this property of any operation involving the SD Card
3626 * will be x100 slower, due to the corresponding file being
3627 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3628 * the context of an emulator crash. The data is already
3629 * synced properly when the emulator exits (either normally or through ^C).
3630 */
3631 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003632 }
3633 }
3634 }
3635
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003636 if (hdb_opts == NULL) {
3637 const char* spath = android_hw->disk_snapStorage_path;
3638 if (spath && *spath) {
3639 if (!path_exists(spath)) {
3640 PANIC("Snapshot storage file does not exist: %s", spath);
3641 }
3642 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003643 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003644 }
3645 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003646 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003647 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003648 }
3649 }
3650
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003651 /* Set the VM's max heap size, passed as a boot property */
3652 if (android_hw->vm_heapSize > 0) {
3653 char tmp[64];
3654 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3655 boot_property_add("dalvik.vm.heapsize",tmp);
3656 }
3657
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003658 /* Initialize net speed and delays stuff. */
3659 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003660 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003661 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003662 }
3663
3664 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003665 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003666 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003667 }
3668
3669 if (android_op_netfast) {
3670 qemu_net_download_speed = 0;
3671 qemu_net_upload_speed = 0;
3672 qemu_net_min_latency = 0;
3673 qemu_net_max_latency = 0;
3674 }
3675
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003676 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003677 if (android_hw->hw_lcd_density) {
3678 long density = android_hw->hw_lcd_density;
3679 if (density <= 0) {
3680 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003681 }
3682 hwLcd_setBootProperty(density);
3683 }
3684
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003685 /* Initialize TCP dump */
3686 if (android_op_tcpdump) {
3687 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3688 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3689 }
3690 }
3691
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003692 /* Initialize modem */
3693 if (android_op_radio) {
3694 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3695 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003696 PANIC("unsupported character device specification: %s\n"
3697 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003698 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003699 }
3700 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3701 } else if (android_hw->hw_gsmModem != 0 ) {
3702 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003703 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003704 }
3705 }
3706
3707 /* Initialize GPS */
3708 if (android_op_gps) {
3709 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3710 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003711 PANIC("unsupported character device specification: %s\n"
3712 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003713 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003714 }
3715 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3716 } else if (android_hw->hw_gps != 0) {
3717 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003718 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003719 }
3720 }
3721
3722 /* Initialize audio. */
3723 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003724 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003725 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003726 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003727 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003728 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003729 }
3730
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003731 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003732 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003733
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003734 if (android_op_cpu_delay) {
3735 char* end;
3736 long delay = strtol(android_op_cpu_delay, &end, 0);
3737 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003738 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003739 }
3740 if (delay > 0)
3741 delay = (1000-delay);
3742
3743 qemu_cpu_delay = (int) delay;
3744 }
3745
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003746 if (android_op_dns_server) {
3747 char* x = strchr(android_op_dns_server, ',');
3748 dns_count = 0;
3749 if (x == NULL)
3750 {
3751 if ( add_dns_server( android_op_dns_server ) == 0 )
3752 dns_count = 1;
3753 }
3754 else
3755 {
3756 x = android_op_dns_server;
3757 while (*x) {
3758 char* y = strchr(x, ',');
3759
3760 if (y != NULL) {
3761 *y = 0;
3762 y++;
3763 } else {
3764 y = x + strlen(x);
3765 }
3766
3767 if (y > x && add_dns_server( x ) == 0) {
3768 dns_count += 1;
3769 }
3770 x = y;
3771 }
3772 }
3773 if (dns_count == 0)
3774 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3775 }
3776
3777 if (dns_count == 0)
3778 dns_count = slirp_get_system_dns_servers();
3779 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003780 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003781 }
3782
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003783#ifdef CONFIG_MEMCHECK
3784 if (android_op_memcheck) {
3785 memcheck_init(android_op_memcheck);
3786 }
3787#endif // CONFIG_MEMCHECK
3788
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003789 /* Initialize cache partition, if any */
3790 if (android_hw->disk_cachePartition != 0) {
3791 char tmp[PATH_MAX+32];
3792 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003793 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003794
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003795 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
3796
3797 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3798 if (filelock_create(partPath) == NULL) {
3799 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3800 /* Note: if there is no file= parameters, nand_add_dev() will
3801 * create a temporary file to back the partition image. */
3802 } else {
3803 /* Create the file if needed */
3804 if (!path_exists(partPath)) {
3805 if (path_empty_file(partPath) < 0) {
3806 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3807 }
3808 }
3809 pstrcat(tmp, sizeof(tmp), ",file=");
3810 pstrcat(tmp, sizeof(tmp), partPath);
3811 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003812 }
3813 nand_add_dev(tmp);
3814 }
3815
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003816 /* We always force qemu=1 when running inside QEMU */
3817 stralloc_add_str(kernel_params, " qemu=1");
3818
3819 /* We always initialize the first serial port for the android-kmsg
3820 * character device (used to send kernel messages) */
3821 serial_hds_add_at(0, "android-kmsg");
3822 stralloc_add_str(kernel_params, " console=ttyS0");
3823
3824 /* We always initialize the second serial port for the android-qemud
3825 * character device as well */
3826 serial_hds_add_at(1, "android-qemud");
3827 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3828
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003829 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3830 os_pidfile_error();
3831 exit(1);
3832 }
3833
David 'Digit' Turner36597752011-05-20 01:18:01 +02003834#if defined(CONFIG_KVM)
3835 if (kvm_allowed < 0) {
3836 kvm_allowed = kvm_check_allowed();
3837 }
3838#endif
3839
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003840#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
3841 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003842 PANIC(
3843 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003844 }
3845#endif
3846
3847 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3848 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003849 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3850 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003851 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003852 }
3853
3854 if (display_type == DT_NOGRAPHIC) {
3855 if (serial_device_index == 0)
3856 serial_devices[0] = "stdio";
3857 if (parallel_device_index == 0)
3858 parallel_devices[0] = "null";
3859 if (strncmp(monitor_device, "vc", 2) == 0)
3860 monitor_device = "stdio";
3861 }
3862
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003863#ifdef CONFIG_KQEMU
3864 if (smp_cpus > 1)
3865 kqemu_allowed = 0;
3866#endif
3867 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003868 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003869 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003870
3871 if (kernel_filename == NULL) {
3872 kernel_filename = android_hw->kernel_path;
3873 }
3874 if (initrd_filename == NULL) {
3875 initrd_filename = android_hw->disk_ramdisk_path;
3876 }
3877
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003878 linux_boot = (kernel_filename != NULL);
3879 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3880
3881 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003882 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003883 }
3884
3885 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003886 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003887 }
3888
3889 /* boot to floppy or the default cd if no hard disk defined yet */
3890 if (!boot_devices[0]) {
3891 boot_devices = "cad";
3892 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003893 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003894
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003895 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003896 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003897 }
David Turner6a9ef172010-09-09 22:54:36 +02003898 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003899
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003900 /* init network clients */
3901 if (nb_net_clients == 0) {
3902 /* if no clients, we use a default config */
3903 net_clients[nb_net_clients++] = "nic";
3904#ifdef CONFIG_SLIRP
3905 net_clients[nb_net_clients++] = "user";
3906#endif
3907 }
3908
3909 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003910 if (net_client_parse(net_clients[i]) < 0) {
3911 PANIC("Unable to parse net clients");
3912 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003913 }
3914 net_client_check();
3915
3916#ifdef TARGET_I386
3917 /* XXX: this should be moved in the PC machine instantiation code */
3918 if (net_boot != 0) {
3919 int netroms = 0;
3920 for (i = 0; i < nb_nics && i < 4; i++) {
3921 const char *model = nd_table[i].model;
3922 char buf[1024];
3923 char *filename;
3924 if (net_boot & (1 << i)) {
3925 if (model == NULL)
3926 model = "ne2k_pci";
3927 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3928 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3929 if (filename && get_image_size(filename) > 0) {
3930 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003931 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003932 }
3933 option_rom[nb_option_roms] = qemu_strdup(buf);
3934 nb_option_roms++;
3935 netroms++;
3936 }
3937 if (filename) {
3938 qemu_free(filename);
3939 }
3940 }
3941 }
3942 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003943 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003944 }
3945 }
3946#endif
3947
3948 /* init the bluetooth world */
3949 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003950 if (bt_parse(bt_opts[i])) {
3951 PANIC("Unable to parse bluetooth options");
3952 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003953
3954 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003955 if (ram_size == 0) {
3956 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3957 if (ram_size == 0) {
3958 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3959 }
3960 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003961
3962#ifdef CONFIG_KQEMU
3963 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
3964 guest ram allocation. It needs to go away. */
3965 if (kqemu_allowed) {
3966 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
3967 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
3968 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003969 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003970 }
3971 }
3972#endif
3973
rich canningsd952f282011-03-01 15:40:09 -08003974#ifndef _WIN32
3975 init_qemu_clear_logs_sig();
3976#endif
3977
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003978 /* init the dynamic translator */
3979 cpu_exec_init_all(tb_size * 1024 * 1024);
3980
3981 bdrv_init();
3982
3983 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003984#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003985 if (nb_drives_opt < MAX_DRIVES)
3986 drive_add(NULL, CDROM_ALIAS);
3987
3988 /* we always create at least one floppy */
3989
3990 if (nb_drives_opt < MAX_DRIVES)
3991 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003992 /* we always create one sd slot, even if no card is in it */
3993
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003994 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003995 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003996 }
3997#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003998
3999 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004000 if (snapshot)
4001 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4002 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
4003 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004004
David Turner6a9ef172010-09-09 22:54:36 +02004005 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004006 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
4007
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004008 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004009 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004010
4011 /* Maintain compatibility with multiple stdio monitors */
4012 if (!strcmp(monitor_device,"stdio")) {
4013 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4014 const char *devname = serial_devices[i];
4015 if (devname && !strcmp(devname,"mon:stdio")) {
4016 monitor_device = NULL;
4017 break;
4018 } else if (devname && !strcmp(devname,"stdio")) {
4019 monitor_device = NULL;
4020 serial_devices[i] = "mon:stdio";
4021 break;
4022 }
4023 }
4024 }
4025
4026 if (nb_numa_nodes > 0) {
4027 int i;
4028
4029 if (nb_numa_nodes > smp_cpus) {
4030 nb_numa_nodes = smp_cpus;
4031 }
4032
4033 /* If no memory size if given for any node, assume the default case
4034 * and distribute the available memory equally across all nodes
4035 */
4036 for (i = 0; i < nb_numa_nodes; i++) {
4037 if (node_mem[i] != 0)
4038 break;
4039 }
4040 if (i == nb_numa_nodes) {
4041 uint64_t usedmem = 0;
4042
4043 /* On Linux, the each node's border has to be 8MB aligned,
4044 * the final node gets the rest.
4045 */
4046 for (i = 0; i < nb_numa_nodes - 1; i++) {
4047 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4048 usedmem += node_mem[i];
4049 }
4050 node_mem[i] = ram_size - usedmem;
4051 }
4052
4053 for (i = 0; i < nb_numa_nodes; i++) {
4054 if (node_cpumask[i] != 0)
4055 break;
4056 }
4057 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4058 * must cope with this anyway, because there are BIOSes out there in
4059 * real machines which also use this scheme.
4060 */
4061 if (i == nb_numa_nodes) {
4062 for (i = 0; i < smp_cpus; i++) {
4063 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4064 }
4065 }
4066 }
4067
4068 if (kvm_enabled()) {
4069 int ret;
4070
4071 ret = kvm_init(smp_cpus);
4072 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004073 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004074 }
4075 }
4076
4077 if (monitor_device) {
4078 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
4079 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004080 PANIC("qemu: could not open monitor device '%s'",
4081 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004082 }
4083 }
4084
4085 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004086 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004087 }
4088
4089 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4090 const char *devname = parallel_devices[i];
4091 if (devname && strcmp(devname, "none")) {
4092 char label[32];
4093 snprintf(label, sizeof(label), "parallel%d", i);
4094 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
4095 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004096 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004097 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004098 }
4099 }
4100 }
4101
4102 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4103 const char *devname = virtio_consoles[i];
4104 if (devname && strcmp(devname, "none")) {
4105 char label[32];
4106 snprintf(label, sizeof(label), "virtcon%d", i);
4107 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
4108 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004109 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004110 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004111 }
4112 }
4113 }
4114
4115 module_call_init(MODULE_INIT_DEVICE);
4116
4117
4118#ifdef CONFIG_TRACE
4119 if (trace_filename) {
4120 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004121 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
4122 }
4123#endif
4124
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004125 /* Check the CPU Architecture value */
4126#if defined(TARGET_ARM)
4127 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4128 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4129 android_hw->hw_cpu_arch);
4130 exit(1);
4131 }
4132#elif defined(TARGET_X86)
4133 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4134 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4135 android_hw->hw_cpu_arch);
4136 exit(1);
4137 }
4138#endif
4139
4140 /* Grab CPU model if provided in hardware.ini */
4141 if ( !cpu_model
4142 && android_hw->hw_cpu_model
4143 && android_hw->hw_cpu_model[0] != '\0')
4144 {
4145 cpu_model = android_hw->hw_cpu_model;
4146 }
4147
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004148 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004149 * collected during initialization.
4150 *
4151 * The order is the following:
4152 * - parameters from the hw configuration (kernel.parameters)
4153 * - additionnal parameters from options (e.g. -memcheck)
4154 * - the -append parameters.
4155 */
4156 {
4157 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004158
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004159 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004160 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004161 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4162 }
4163
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004164 /* If not empty, kernel_config always contains a leading space */
4165 stralloc_append(kernel_params, kernel_config);
4166
4167 if (*kernel_cmdline) {
4168 stralloc_add_c(kernel_params, ' ');
4169 stralloc_add_str(kernel_params, kernel_cmdline);
4170 }
4171
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004172 /* Remove any leading/trailing spaces */
4173 stralloc_strip(kernel_params);
4174
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004175 kernel_parameters = stralloc_cstr(kernel_params);
4176 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4177
4178 machine->init(ram_size,
4179 boot_devices,
4180 kernel_filename,
4181 kernel_parameters,
4182 initrd_filename,
4183 cpu_model);
4184
4185 stralloc_reset(kernel_params);
4186 stralloc_reset(kernel_config);
4187 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004188
4189
4190 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4191 for (i = 0; i < nb_numa_nodes; i++) {
4192 if (node_cpumask[i] & (1 << env->cpu_index)) {
4193 env->numa_node = i;
4194 }
4195 }
4196 }
4197
4198 current_machine = machine;
4199
4200 /* Set KVM's vcpu state to qemu's initial CPUState. */
4201 if (kvm_enabled()) {
4202 int ret;
4203
4204 ret = kvm_sync_vcpus();
4205 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004206 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004207 }
4208 }
4209
4210 /* init USB devices */
4211 if (usb_enabled) {
4212 for(i = 0; i < usb_devices_index; i++) {
4213 if (usb_device_add(usb_devices[i], 0) < 0) {
4214 fprintf(stderr, "Warning: could not add USB device %s\n",
4215 usb_devices[i]);
4216 }
4217 }
4218 }
4219
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004220 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004221 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004222
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004223 /* Initialize display from the command line parameters. */
4224 android_display_reset(ds,
4225 android_display_width,
4226 android_display_height,
4227 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004228
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004229 if (display_type == DT_DEFAULT) {
4230#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4231 display_type = DT_SDL;
4232#else
4233 display_type = DT_VNC;
4234 vnc_display = "localhost:0,to=99";
4235 show_vnc_port = 1;
4236#endif
4237 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004238
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004239
4240 switch (display_type) {
4241 case DT_NOGRAPHIC:
4242 break;
4243#if defined(CONFIG_CURSES)
4244 case DT_CURSES:
4245 curses_display_init(ds, full_screen);
4246 break;
4247#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004248#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004249 case DT_SDL:
4250 sdl_display_init(ds, full_screen, no_frame);
4251 break;
4252#elif defined(CONFIG_COCOA)
4253 case DT_SDL:
4254 cocoa_display_init(ds, full_screen);
4255 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004256#elif defined(CONFIG_STANDALONE_CORE)
4257 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004258 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004259 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004260#endif
4261 case DT_VNC:
4262 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004263 if (vnc_display_open(ds, vnc_display) < 0) {
4264 PANIC("Unable to initialize VNC display");
4265 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004266
4267 if (show_vnc_port) {
4268 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4269 }
4270 break;
4271 default:
4272 break;
4273 }
4274 dpy_resize(ds);
4275
4276 dcl = ds->listeners;
4277 while (dcl != NULL) {
4278 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004279 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
4280 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004281 }
4282 dcl = dcl->next;
4283 }
4284
4285 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004286 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
4287 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004288 }
4289
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004290 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004291 qemu_chr_initial_reset();
4292
4293 if (monitor_device && monitor_hd)
4294 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4295
4296 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4297 const char *devname = serial_devices[i];
4298 if (devname && strcmp(devname, "none")) {
4299 if (strstart(devname, "vc", 0))
4300 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4301 }
4302 }
4303
4304 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4305 const char *devname = parallel_devices[i];
4306 if (devname && strcmp(devname, "none")) {
4307 if (strstart(devname, "vc", 0))
4308 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4309 }
4310 }
4311
4312 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4313 const char *devname = virtio_consoles[i];
4314 if (virtcon_hds[i] && devname) {
4315 if (strstart(devname, "vc", 0))
4316 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4317 }
4318 }
4319
4320 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004321 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004322 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004323 }
4324
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004325 /* call android-specific setup function */
4326 android_emulation_setup();
4327
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004328#if !defined(CONFIG_STANDALONE_CORE)
4329 // For the standalone emulator (UI+core in one executable) we need to
4330 // set the window title here.
4331 android_emulator_set_base_port(android_base_port);
4332#endif
4333
Ot ten Thije871da2a2010-09-20 10:29:22 +01004334 if (loadvm)
4335 do_loadvm(cur_mon, loadvm);
4336
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004337 if (incoming) {
4338 autostart = 0; /* fixme how to deal with -daemonize */
4339 qemu_start_incoming_migration(incoming);
4340 }
4341
4342 if (autostart)
4343 vm_start();
4344
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004345 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004346
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004347#ifdef CONFIG_ANDROID
4348 // This will notify the UI that the core is successfuly initialized
4349 android_core_init_completed();
4350#endif // CONFIG_ANDROID
4351
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004352 main_loop();
4353 quit_timers();
4354 net_cleanup();
4355 android_emulation_teardown();
4356 return 0;
4357}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004358
4359void
4360android_emulation_teardown(void)
4361{
4362 android_charmap_done();
4363}