blob: 4c2a43362aa3a69a605760a589cbaf16042eac42 [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' Turnercb88e792011-08-26 01:35:14 +020068#include "android/opengles.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070069#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070070#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070071
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070072#ifdef CONFIG_MEMCHECK
73#include "memcheck/memcheck.h"
74#endif // CONFIG_MEMCHECK
75
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070076#include <unistd.h>
77#include <fcntl.h>
78#include <signal.h>
79#include <time.h>
80#include <errno.h>
81#include <sys/time.h>
82#include <zlib.h>
83
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070084/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070085#include "config-host.h"
86
87#ifndef _WIN32
88#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070089#include <sys/times.h>
90#include <sys/wait.h>
91#include <termios.h>
92#include <sys/mman.h>
93#include <sys/ioctl.h>
94#include <sys/resource.h>
95#include <sys/socket.h>
96#include <netinet/in.h>
97#include <net/if.h>
98#if defined(__NetBSD__)
99#include <net/if_tap.h>
100#endif
101#ifdef __linux__
102#include <linux/if_tun.h>
103#endif
104#include <arpa/inet.h>
105#include <dirent.h>
106#include <netdb.h>
107#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700108#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700109#include <sys/stat.h>
110#if defined(__FreeBSD__) || defined(__DragonFly__)
111#include <libutil.h>
112#else
113#include <util.h>
114#endif
115#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
116#include <freebsd/stdlib.h>
117#else
118#ifdef __linux__
119#include <pty.h>
120#include <malloc.h>
121#include <linux/rtc.h>
122
123/* For the benefit of older linux systems which don't supply it,
124 we use a local copy of hpet.h. */
125/* #include <linux/hpet.h> */
126#include "hpet.h"
127
128#include <linux/ppdev.h>
129#include <linux/parport.h>
130#endif
131#ifdef __sun__
132#include <sys/stat.h>
133#include <sys/ethernet.h>
134#include <sys/sockio.h>
135#include <netinet/arp.h>
136#include <netinet/in.h>
137#include <netinet/in_systm.h>
138#include <netinet/ip.h>
139#include <netinet/ip_icmp.h> // must come after ip.h
140#include <netinet/udp.h>
141#include <netinet/tcp.h>
142#include <net/if.h>
143#include <syslog.h>
144#include <stropts.h>
145#endif
146#endif
147#endif
148
149#if defined(__OpenBSD__)
150#include <util.h>
151#endif
152
153#if defined(CONFIG_VDE)
154#include <libvdeplug.h>
155#endif
156
157#ifdef _WIN32
158#include <windows.h>
159#include <malloc.h>
160#include <sys/timeb.h>
161#include <mmsystem.h>
162#define getopt_long_only getopt_long
163#define memalign(align, size) malloc(size)
164#endif
165
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200166#include "cpus.h"
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200167#include "arch_init.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700168
169#ifdef CONFIG_COCOA
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +0200170int qemu_main(int argc, char **argv, char **envp);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700171#undef main
172#define main qemu_main
173#endif /* CONFIG_COCOA */
174
175#include "hw/hw.h"
176#include "hw/boards.h"
177#include "hw/usb.h"
178#include "hw/pcmcia.h"
179#include "hw/pc.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700180#include "hw/isa.h"
181#include "hw/baum.h"
182#include "hw/bt.h"
183#include "hw/watchdog.h"
184#include "hw/smbios.h"
185#include "hw/xen.h"
186#include "bt-host.h"
187#include "net.h"
188#include "monitor.h"
189#include "console.h"
190#include "sysemu.h"
191#include "gdbstub.h"
192#include "qemu-timer.h"
193#include "qemu-char.h"
194#include "cache-utils.h"
195#include "block.h"
196#include "dma.h"
197#include "audio/audio.h"
198#include "migration.h"
199#include "kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200200#ifdef CONFIG_KVM
201#include "kvm-android.h"
202#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700203#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700204#include "android/hw-lcd.h"
205#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200206#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700207#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100208#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700209
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700210#ifdef CONFIG_STANDALONE_CORE
211/* Verbose value used by the standalone emulator core (without UI) */
212unsigned long android_verbose;
213#endif // CONFIG_STANDALONE_CORE
214
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800215#if !defined(CONFIG_STANDALONE_CORE)
216/* in android/qemulator.c */
217extern void android_emulator_set_base_port(int port);
218#endif
219
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700220#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700221#undef main
222#define main qemu_main
223#endif
224
225#include "disas.h"
226
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700227#ifdef CONFIG_TRACE
David 'Digit' Turner406a59b2011-05-12 10:26:09 +0200228#include "android-trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700229#endif
230
231#include "qemu_socket.h"
232
233#if defined(CONFIG_SLIRP)
234#include "libslirp.h"
235#endif
236
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700237
238
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700239#define DEFAULT_RAM_SIZE 128
240
241/* Max number of USB devices that can be specified on the commandline. */
242#define MAX_USB_CMDLINE 8
243
244/* Max number of bluetooth switches on the commandline. */
245#define MAX_BT_CMDLINE 10
246
247/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700248
249static const char *data_dir;
250const char *bios_name = NULL;
251static void *ioport_opaque[MAX_IOPORTS];
252static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
253static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100254#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700255/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
256 to store the VM snapshots */
257DriveInfo drives_table[MAX_DRIVES+1];
258int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100259#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700261DisplayType display_type = DT_DEFAULT;
262const char* keyboard_layout = NULL;
263int64_t ticks_per_sec;
264ram_addr_t ram_size;
David 'Digit' Turner280afa02011-05-11 17:37:44 +0200265const char *mem_path = NULL;
266#ifdef MAP_POPULATE
267int mem_prealloc = 0; /* force preallocation of physical target memory */
268#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700269int nb_nics;
270NICInfo nd_table[MAX_NICS];
271int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100272int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700273static int rtc_utc = 1;
274static int rtc_date_offset = -1; /* -1 means no change */
275int cirrus_vga_enabled = 1;
276int std_vga_enabled = 0;
277int vmsvga_enabled = 0;
278int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700279QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700280static int full_screen = 0;
281#ifdef CONFIG_SDL
282static int no_frame = 0;
283#endif
284int no_quit = 0;
285CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100286int serial_hds_count;
287
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700288CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
289CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
290#ifdef TARGET_I386
291int win2k_install_hack = 0;
292int rtc_td_hack = 0;
293#endif
294int usb_enabled = 0;
295int singlestep = 0;
296int smp_cpus = 1;
297const char *vnc_display;
298int acpi_enabled = 1;
299int no_hpet = 0;
300int no_virtio_balloon = 0;
301int fd_bootchk = 1;
302int no_reboot = 0;
303int no_shutdown = 0;
304int cursor_hide = 1;
305int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700306WatchdogTimerModel *watchdog = NULL;
307int watchdog_action = WDT_RESET;
308const char *option_rom[MAX_OPTION_ROMS];
309int nb_option_roms;
310int semihosting_enabled = 0;
311#ifdef TARGET_ARM
312int old_param = 0;
313#endif
314const char *qemu_name;
315int alt_grab = 0;
316#if defined(TARGET_SPARC) || defined(TARGET_PPC)
317unsigned int nb_prom_envs = 0;
318const char *prom_envs[MAX_PROM_ENVS];
319#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100320#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700321int nb_drives_opt;
322struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100323#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700324int nb_numa_nodes;
325uint64_t node_mem[MAX_NODES];
326uint64_t node_cpumask[MAX_NODES];
327
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700328static QEMUTimer *nographic_timer;
329
330uint8_t qemu_uuid[16];
331
332
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700333int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700334extern char* audio_input_source;
335
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700336extern char* android_op_ports;
337extern char* android_op_port;
338extern char* android_op_report_console;
339extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700340// Path to the file containing specific key character map.
341char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700342
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700343/* Path to hardware initialization file passed with -android-hw option. */
344char* android_op_hwini = NULL;
345
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700346/* Memory checker options. */
347char* android_op_memcheck = NULL;
348
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700349/* -dns-server option value. */
350char* android_op_dns_server = NULL;
351
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700352/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700353char* android_op_radio = NULL;
354
355/* -gps option value. */
356char* android_op_gps = NULL;
357
358/* -audio option value. */
359char* android_op_audio = NULL;
360
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700361/* -cpu-delay option value. */
362char* android_op_cpu_delay = NULL;
363
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700364#ifdef CONFIG_NAND_LIMITS
365/* -nand-limits option value. */
366char* android_op_nand_limits = NULL;
367#endif // CONFIG_NAND_LIMITS
368
369/* -netspeed option value. */
370char* android_op_netspeed = NULL;
371
372/* -netdelay option value. */
373char* android_op_netdelay = NULL;
374
375/* -netfast option value. */
376int android_op_netfast = 0;
377
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700378/* -tcpdump option value. */
379char* android_op_tcpdump = NULL;
380
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700381/* -lcd-density option value. */
382char* android_op_lcd_density = NULL;
383
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700384/* -ui-port option value. This port will be used to report the core
385 * initialization completion.
386 */
387char* android_op_ui_port = NULL;
388
389/* -ui-settings option value. This value will be passed to the UI when new UI
390 * process is attaching to the core.
391 */
392char* android_op_ui_settings = NULL;
393
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800394/* -android-avdname option value. */
395char* android_op_avd_name = "unknown";
396
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700397extern int android_display_width;
398extern int android_display_height;
399extern int android_display_bpp;
400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700401extern void dprint( const char* format, ... );
402
rich canningsd952f282011-03-01 15:40:09 -0800403const char* dns_log_filename = NULL;
404const char* drop_log_filename = NULL;
405static int rotate_logs_requested = 0;
406
Tim Baverstock24204cc2010-11-25 11:37:43 +0000407const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000408
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700409#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
410
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700411/* Reports the core initialization failure to the error stdout and to the UI
412 * socket before exiting the application.
413 * Parameters that are passed to this macro are used to format the error
414 * mesage using sprintf routine.
415 */
416#ifdef CONFIG_ANDROID
417#define PANIC(...) android_core_init_failure(__VA_ARGS__)
418#else
419#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
420 exit(1); \
421 } while (0)
422#endif // CONFIG_ANDROID
423
424/* Exits the core during initialization. */
425#ifdef CONFIG_ANDROID
426#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
427#else
428#define QEMU_EXIT(exit_code) exit(exit_code)
429#endif // CONFIG_ANDROID
430
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700431/***********************************************************/
432/* x86 ISA bus support */
433
434target_phys_addr_t isa_mem_base = 0;
435PicState2 *isa_pic;
436
437static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
438static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
439
440static uint32_t ioport_read(int index, uint32_t address)
441{
442 static IOPortReadFunc *default_func[3] = {
443 default_ioport_readb,
444 default_ioport_readw,
445 default_ioport_readl
446 };
447 IOPortReadFunc *func = ioport_read_table[index][address];
448 if (!func)
449 func = default_func[index];
450 return func(ioport_opaque[address], address);
451}
452
453static void ioport_write(int index, uint32_t address, uint32_t data)
454{
455 static IOPortWriteFunc *default_func[3] = {
456 default_ioport_writeb,
457 default_ioport_writew,
458 default_ioport_writel
459 };
460 IOPortWriteFunc *func = ioport_write_table[index][address];
461 if (!func)
462 func = default_func[index];
463 func(ioport_opaque[address], address, data);
464}
465
466static uint32_t default_ioport_readb(void *opaque, uint32_t address)
467{
468#ifdef DEBUG_UNUSED_IOPORT
469 fprintf(stderr, "unused inb: port=0x%04x\n", address);
470#endif
471 return 0xff;
472}
473
474static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
475{
476#ifdef DEBUG_UNUSED_IOPORT
477 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
478#endif
479}
480
481/* default is to make two byte accesses */
482static uint32_t default_ioport_readw(void *opaque, uint32_t address)
483{
484 uint32_t data;
485 data = ioport_read(0, address);
486 address = (address + 1) & (MAX_IOPORTS - 1);
487 data |= ioport_read(0, address) << 8;
488 return data;
489}
490
491static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
492{
493 ioport_write(0, address, data & 0xff);
494 address = (address + 1) & (MAX_IOPORTS - 1);
495 ioport_write(0, address, (data >> 8) & 0xff);
496}
497
498static uint32_t default_ioport_readl(void *opaque, uint32_t address)
499{
500#ifdef DEBUG_UNUSED_IOPORT
501 fprintf(stderr, "unused inl: port=0x%04x\n", address);
502#endif
503 return 0xffffffff;
504}
505
506static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
507{
508#ifdef DEBUG_UNUSED_IOPORT
509 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
510#endif
511}
512
rich canningsd952f282011-03-01 15:40:09 -0800513/*
514 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
515 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
516 * between the tasks that do not require restarting Qemu.
517 */
518void rotate_qemu_logs_handler(int signum) {
519 rotate_logs_requested = 1;
520}
521
522/*
523 * Resets the rotate_log_requested_flag. Normally called after qemu
524 * logs has been rotated.
525 */
526void reset_rotate_qemu_logs_request(void) {
527 rotate_logs_requested = 0;
528}
529
530/*
531 * Clears the passed qemu log when the rotate_logs_requested
532 * is set. We need to clear the logs between the tasks that do not
533 * require restarting Qemu.
534 */
535FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
536 FILE* new_log_fd = NULL;
537 if (old_log_fd) {
538 if (fclose(old_log_fd) == -1) {
539 fprintf(stderr, "Cannot close old_log fd\n");
540 exit(errno);
541 }
542 }
543
544 if (!filename) {
545 fprintf(stderr, "The log filename to be rotated is not provided");
546 exit(-1);
547 }
548
549 new_log_fd = fopen(filename , "wb+");
550 if (new_log_fd == NULL) {
551 fprintf(stderr, "Cannot open the log file: %s for write.\n",
552 filename);
553 exit(1);
554 }
555
556 return new_log_fd;
557}
558
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700559/***************/
560/* ballooning */
561
562static QEMUBalloonEvent *qemu_balloon_event;
563void *qemu_balloon_event_opaque;
564
565void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
566{
567 qemu_balloon_event = func;
568 qemu_balloon_event_opaque = opaque;
569}
570
571void qemu_balloon(ram_addr_t target)
572{
573 if (qemu_balloon_event)
574 qemu_balloon_event(qemu_balloon_event_opaque, target);
575}
576
577ram_addr_t qemu_balloon_status(void)
578{
579 if (qemu_balloon_event)
580 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
581 return 0;
582}
583
584/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700585/* host time/date access */
586void qemu_get_timedate(struct tm *tm, int offset)
587{
588 time_t ti;
589 struct tm *ret;
590
591 time(&ti);
592 ti += offset;
593 if (rtc_date_offset == -1) {
594 if (rtc_utc)
595 ret = gmtime(&ti);
596 else
597 ret = localtime(&ti);
598 } else {
599 ti -= rtc_date_offset;
600 ret = gmtime(&ti);
601 }
602
603 memcpy(tm, ret, sizeof(struct tm));
604}
605
606int qemu_timedate_diff(struct tm *tm)
607{
608 time_t seconds;
609
610 if (rtc_date_offset == -1)
611 if (rtc_utc)
612 seconds = mktimegm(tm);
613 else
614 seconds = mktime(tm);
615 else
616 seconds = mktimegm(tm) + rtc_date_offset;
617
618 return seconds - time(NULL);
619}
620
621
622#ifdef CONFIG_TRACE
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +0200623int tbflush_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700624static int exit_requested;
625
626void start_tracing()
627{
628 if (trace_filename == NULL)
629 return;
630 if (!tracing) {
631 fprintf(stderr,"-- start tracing --\n");
632 start_time = Now();
633 }
634 tracing = 1;
635 tbflush_requested = 1;
636 qemu_notify_event();
637}
638
639void stop_tracing()
640{
641 if (trace_filename == NULL)
642 return;
643 if (tracing) {
644 end_time = Now();
645 elapsed_usecs += end_time - start_time;
646 fprintf(stderr,"-- stop tracing --\n");
647 }
648 tracing = 0;
649 tbflush_requested = 1;
650 qemu_notify_event();
651}
652
653#ifndef _WIN32
654/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
655 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
656 */
657void sigusr_handler(int sig)
658{
659 if (sig == SIGUSR1)
660 start_tracing();
661 else
662 stop_tracing();
663}
664#endif
665
666/* This is the handler to catch control-C so that we can exit cleanly.
667 * This is needed when tracing to flush the buffers to disk.
668 */
669void sigint_handler(int sig)
670{
671 exit_requested = 1;
672 qemu_notify_event();
673}
674#endif /* CONFIG_TRACE */
675
676
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700677/***********************************************************/
678/* Bluetooth support */
679static int nb_hcis;
680static int cur_hci;
681static struct HCIInfo *hci_table[MAX_NICS];
682
683static struct bt_vlan_s {
684 struct bt_scatternet_s net;
685 int id;
686 struct bt_vlan_s *next;
687} *first_bt_vlan;
688
689/* find or alloc a new bluetooth "VLAN" */
690static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
691{
692 struct bt_vlan_s **pvlan, *vlan;
693 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
694 if (vlan->id == id)
695 return &vlan->net;
696 }
697 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
698 vlan->id = id;
699 pvlan = &first_bt_vlan;
700 while (*pvlan != NULL)
701 pvlan = &(*pvlan)->next;
702 *pvlan = vlan;
703 return &vlan->net;
704}
705
706static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
707{
708}
709
710static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
711{
712 return -ENOTSUP;
713}
714
715static struct HCIInfo null_hci = {
716 .cmd_send = null_hci_send,
717 .sco_send = null_hci_send,
718 .acl_send = null_hci_send,
719 .bdaddr_set = null_hci_addr_set,
720};
721
722struct HCIInfo *qemu_next_hci(void)
723{
724 if (cur_hci == nb_hcis)
725 return &null_hci;
726
727 return hci_table[cur_hci++];
728}
729
730static struct HCIInfo *hci_init(const char *str)
731{
732 char *endp;
733 struct bt_scatternet_s *vlan = 0;
734
735 if (!strcmp(str, "null"))
736 /* null */
737 return &null_hci;
738 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
739 /* host[:hciN] */
740 return bt_host_hci(str[4] ? str + 5 : "hci0");
741 else if (!strncmp(str, "hci", 3)) {
742 /* hci[,vlan=n] */
743 if (str[3]) {
744 if (!strncmp(str + 3, ",vlan=", 6)) {
745 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
746 if (*endp)
747 vlan = 0;
748 }
749 } else
750 vlan = qemu_find_bt_vlan(0);
751 if (vlan)
752 return bt_new_hci(vlan);
753 }
754
755 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
756
757 return 0;
758}
759
760static int bt_hci_parse(const char *str)
761{
762 struct HCIInfo *hci;
763 bdaddr_t bdaddr;
764
765 if (nb_hcis >= MAX_NICS) {
766 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
767 return -1;
768 }
769
770 hci = hci_init(str);
771 if (!hci)
772 return -1;
773
774 bdaddr.b[0] = 0x52;
775 bdaddr.b[1] = 0x54;
776 bdaddr.b[2] = 0x00;
777 bdaddr.b[3] = 0x12;
778 bdaddr.b[4] = 0x34;
779 bdaddr.b[5] = 0x56 + nb_hcis;
780 hci->bdaddr_set(hci, bdaddr.b);
781
782 hci_table[nb_hcis++] = hci;
783
784 return 0;
785}
786
787static void bt_vhci_add(int vlan_id)
788{
789 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
790
791 if (!vlan->slave)
792 fprintf(stderr, "qemu: warning: adding a VHCI to "
793 "an empty scatternet %i\n", vlan_id);
794
795 bt_vhci_init(bt_new_hci(vlan));
796}
797
798static struct bt_device_s *bt_device_add(const char *opt)
799{
800 struct bt_scatternet_s *vlan;
801 int vlan_id = 0;
802 char *endp = strstr(opt, ",vlan=");
803 int len = (endp ? endp - opt : strlen(opt)) + 1;
804 char devname[10];
805
806 pstrcpy(devname, MIN(sizeof(devname), len), opt);
807
808 if (endp) {
809 vlan_id = strtol(endp + 6, &endp, 0);
810 if (*endp) {
811 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
812 return 0;
813 }
814 }
815
816 vlan = qemu_find_bt_vlan(vlan_id);
817
818 if (!vlan->slave)
819 fprintf(stderr, "qemu: warning: adding a slave device to "
820 "an empty scatternet %i\n", vlan_id);
821
822 if (!strcmp(devname, "keyboard"))
823 return bt_keyboard_init(vlan);
824
825 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
826 return 0;
827}
828
829static int bt_parse(const char *opt)
830{
831 const char *endp, *p;
832 int vlan;
833
834 if (strstart(opt, "hci", &endp)) {
835 if (!*endp || *endp == ',') {
836 if (*endp)
837 if (!strstart(endp, ",vlan=", 0))
838 opt = endp + 1;
839
840 return bt_hci_parse(opt);
841 }
842 } else if (strstart(opt, "vhci", &endp)) {
843 if (!*endp || *endp == ',') {
844 if (*endp) {
845 if (strstart(endp, ",vlan=", &p)) {
846 vlan = strtol(p, (char **) &endp, 0);
847 if (*endp) {
848 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
849 return 1;
850 }
851 } else {
852 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
853 return 1;
854 }
855 } else
856 vlan = 0;
857
858 bt_vhci_add(vlan);
859 return 0;
860 }
861 } else if (strstart(opt, "device:", &endp))
862 return !bt_device_add(endp);
863
864 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
865 return 1;
866}
867
868/***********************************************************/
869/* QEMU Block devices */
870
871#define HD_ALIAS "index=%d,media=disk"
872#define CDROM_ALIAS "index=2,media=cdrom"
873#define FD_ALIAS "index=%d,if=floppy"
874#define PFLASH_ALIAS "if=pflash"
875#define MTD_ALIAS "if=mtd"
876#define SD_ALIAS "index=0,if=sd"
877
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100878static int drive_init_func(QemuOpts *opts, void *opaque)
879{
880 int *use_scsi = opaque;
881 int fatal_error = 0;
882
883 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
884 if (fatal_error)
885 return 1;
886 }
887 return 0;
888}
889
890static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
891{
892 if (NULL == qemu_opt_get(opts, "snapshot")) {
893 qemu_opt_set(opts, "snapshot", "on");
894 }
895 return 0;
896}
897
898#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700899static int drive_opt_get_free_idx(void)
900{
901 int index;
902
903 for (index = 0; index < MAX_DRIVES; index++)
904 if (!drives_opt[index].used) {
905 drives_opt[index].used = 1;
906 return index;
907 }
908
909 return -1;
910}
911
912static int drive_get_free_idx(void)
913{
914 int index;
915
916 for (index = 0; index < MAX_DRIVES; index++)
917 if (!drives_table[index].used) {
918 drives_table[index].used = 1;
919 return index;
920 }
921
922 return -1;
923}
924
925int drive_add(const char *file, const char *fmt, ...)
926{
927 va_list ap;
928 int index = drive_opt_get_free_idx();
929
930 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
931 fprintf(stderr, "qemu: too many drives\n");
932 return -1;
933 }
934
935 drives_opt[index].file = file;
936 va_start(ap, fmt);
937 vsnprintf(drives_opt[index].opt,
938 sizeof(drives_opt[0].opt), fmt, ap);
939 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700940
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700941 nb_drives_opt++;
942 return index;
943}
944
945void drive_remove(int index)
946{
947 drives_opt[index].used = 0;
948 nb_drives_opt--;
949}
950
951int drive_get_index(BlockInterfaceType type, int bus, int unit)
952{
953 int index;
954
955 /* seek interface, bus and unit */
956
957 for (index = 0; index < MAX_DRIVES; index++)
958 if (drives_table[index].type == type &&
959 drives_table[index].bus == bus &&
960 drives_table[index].unit == unit &&
961 drives_table[index].used)
962 return index;
963
964 return -1;
965}
966
967int drive_get_max_bus(BlockInterfaceType type)
968{
969 int max_bus;
970 int index;
971
972 max_bus = -1;
973 for (index = 0; index < nb_drives; index++) {
974 if(drives_table[index].type == type &&
975 drives_table[index].bus > max_bus)
976 max_bus = drives_table[index].bus;
977 }
978 return max_bus;
979}
980
981const char *drive_get_serial(BlockDriverState *bdrv)
982{
983 int index;
984
985 for (index = 0; index < nb_drives; index++)
986 if (drives_table[index].bdrv == bdrv)
987 return drives_table[index].serial;
988
989 return "\0";
990}
991
992BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
993{
994 int index;
995
996 for (index = 0; index < nb_drives; index++)
997 if (drives_table[index].bdrv == bdrv)
998 return drives_table[index].onerror;
999
1000 return BLOCK_ERR_STOP_ENOSPC;
1001}
1002
1003static void bdrv_format_print(void *opaque, const char *name)
1004{
1005 fprintf(stderr, " %s", name);
1006}
1007
1008void drive_uninit(BlockDriverState *bdrv)
1009{
1010 int i;
1011
1012 for (i = 0; i < MAX_DRIVES; i++)
1013 if (drives_table[i].bdrv == bdrv) {
1014 drives_table[i].bdrv = NULL;
1015 drives_table[i].used = 0;
1016 drive_remove(drives_table[i].drive_opt_idx);
1017 nb_drives--;
1018 break;
1019 }
1020}
1021
1022int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1023{
1024 char buf[128];
1025 char file[1024];
1026 char devname[128];
1027 char serial[21];
1028 const char *mediastr = "";
1029 BlockInterfaceType type;
1030 enum { MEDIA_DISK, MEDIA_CDROM } media;
1031 int bus_id, unit_id;
1032 int cyls, heads, secs, translation;
1033 BlockDriverState *bdrv;
1034 BlockDriver *drv = NULL;
1035 QEMUMachine *machine = opaque;
1036 int max_devs;
1037 int index;
1038 int cache;
1039 int bdrv_flags, onerror;
1040 int drives_table_idx;
1041 char *str = arg->opt;
1042 static const char * const params[] = { "bus", "unit", "if", "index",
1043 "cyls", "heads", "secs", "trans",
1044 "media", "snapshot", "file",
1045 "cache", "format", "serial", "werror",
1046 NULL };
1047
1048 if (check_params(buf, sizeof(buf), params, str) < 0) {
1049 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1050 buf, str);
1051 return -1;
1052 }
1053
1054 file[0] = 0;
1055 cyls = heads = secs = 0;
1056 bus_id = 0;
1057 unit_id = -1;
1058 translation = BIOS_ATA_TRANSLATION_AUTO;
1059 index = -1;
1060 cache = 3;
1061
1062 if (machine->use_scsi) {
1063 type = IF_SCSI;
1064 max_devs = MAX_SCSI_DEVS;
1065 pstrcpy(devname, sizeof(devname), "scsi");
1066 } else {
1067 type = IF_IDE;
1068 max_devs = MAX_IDE_DEVS;
1069 pstrcpy(devname, sizeof(devname), "ide");
1070 }
1071 media = MEDIA_DISK;
1072
1073 /* extract parameters */
1074
1075 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1076 bus_id = strtol(buf, NULL, 0);
1077 if (bus_id < 0) {
1078 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1079 return -1;
1080 }
1081 }
1082
1083 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1084 unit_id = strtol(buf, NULL, 0);
1085 if (unit_id < 0) {
1086 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1087 return -1;
1088 }
1089 }
1090
1091 if (get_param_value(buf, sizeof(buf), "if", str)) {
1092 pstrcpy(devname, sizeof(devname), buf);
1093 if (!strcmp(buf, "ide")) {
1094 type = IF_IDE;
1095 max_devs = MAX_IDE_DEVS;
1096 } else if (!strcmp(buf, "scsi")) {
1097 type = IF_SCSI;
1098 max_devs = MAX_SCSI_DEVS;
1099 } else if (!strcmp(buf, "floppy")) {
1100 type = IF_FLOPPY;
1101 max_devs = 0;
1102 } else if (!strcmp(buf, "pflash")) {
1103 type = IF_PFLASH;
1104 max_devs = 0;
1105 } else if (!strcmp(buf, "mtd")) {
1106 type = IF_MTD;
1107 max_devs = 0;
1108 } else if (!strcmp(buf, "sd")) {
1109 type = IF_SD;
1110 max_devs = 0;
1111 } else if (!strcmp(buf, "virtio")) {
1112 type = IF_VIRTIO;
1113 max_devs = 0;
1114 } else if (!strcmp(buf, "xen")) {
1115 type = IF_XEN;
1116 max_devs = 0;
1117 } else {
1118 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1119 return -1;
1120 }
1121 }
1122
1123 if (get_param_value(buf, sizeof(buf), "index", str)) {
1124 index = strtol(buf, NULL, 0);
1125 if (index < 0) {
1126 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1127 return -1;
1128 }
1129 }
1130
1131 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1132 cyls = strtol(buf, NULL, 0);
1133 }
1134
1135 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1136 heads = strtol(buf, NULL, 0);
1137 }
1138
1139 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1140 secs = strtol(buf, NULL, 0);
1141 }
1142
1143 if (cyls || heads || secs) {
1144 if (cyls < 1 || cyls > 16383) {
1145 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1146 return -1;
1147 }
1148 if (heads < 1 || heads > 16) {
1149 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1150 return -1;
1151 }
1152 if (secs < 1 || secs > 63) {
1153 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1154 return -1;
1155 }
1156 }
1157
1158 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1159 if (!cyls) {
1160 fprintf(stderr,
1161 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1162 str);
1163 return -1;
1164 }
1165 if (!strcmp(buf, "none"))
1166 translation = BIOS_ATA_TRANSLATION_NONE;
1167 else if (!strcmp(buf, "lba"))
1168 translation = BIOS_ATA_TRANSLATION_LBA;
1169 else if (!strcmp(buf, "auto"))
1170 translation = BIOS_ATA_TRANSLATION_AUTO;
1171 else {
1172 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1173 return -1;
1174 }
1175 }
1176
1177 if (get_param_value(buf, sizeof(buf), "media", str)) {
1178 if (!strcmp(buf, "disk")) {
1179 media = MEDIA_DISK;
1180 } else if (!strcmp(buf, "cdrom")) {
1181 if (cyls || secs || heads) {
1182 fprintf(stderr,
1183 "qemu: '%s' invalid physical CHS format\n", str);
1184 return -1;
1185 }
1186 media = MEDIA_CDROM;
1187 } else {
1188 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1189 return -1;
1190 }
1191 }
1192
1193 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1194 if (!strcmp(buf, "on"))
1195 snapshot = 1;
1196 else if (!strcmp(buf, "off"))
1197 snapshot = 0;
1198 else {
1199 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1200 return -1;
1201 }
1202 }
1203
1204 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1205 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1206 cache = 0;
1207 else if (!strcmp(buf, "writethrough"))
1208 cache = 1;
1209 else if (!strcmp(buf, "writeback"))
1210 cache = 2;
1211 else {
1212 fprintf(stderr, "qemu: invalid cache option\n");
1213 return -1;
1214 }
1215 }
1216
1217 if (get_param_value(buf, sizeof(buf), "format", str)) {
1218 if (strcmp(buf, "?") == 0) {
1219 fprintf(stderr, "qemu: Supported formats:");
1220 bdrv_iterate_format(bdrv_format_print, NULL);
1221 fprintf(stderr, "\n");
1222 return -1;
1223 }
1224 drv = bdrv_find_format(buf);
1225 if (!drv) {
1226 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1227 return -1;
1228 }
1229 }
1230
1231 if (arg->file == NULL)
1232 get_param_value(file, sizeof(file), "file", str);
1233 else
1234 pstrcpy(file, sizeof(file), arg->file);
1235
1236 if (!get_param_value(serial, sizeof(serial), "serial", str))
1237 memset(serial, 0, sizeof(serial));
1238
1239 onerror = BLOCK_ERR_STOP_ENOSPC;
1240 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1241 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1242 fprintf(stderr, "werror is no supported by this format\n");
1243 return -1;
1244 }
1245 if (!strcmp(buf, "ignore"))
1246 onerror = BLOCK_ERR_IGNORE;
1247 else if (!strcmp(buf, "enospc"))
1248 onerror = BLOCK_ERR_STOP_ENOSPC;
1249 else if (!strcmp(buf, "stop"))
1250 onerror = BLOCK_ERR_STOP_ANY;
1251 else if (!strcmp(buf, "report"))
1252 onerror = BLOCK_ERR_REPORT;
1253 else {
1254 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1255 return -1;
1256 }
1257 }
1258
1259 /* compute bus and unit according index */
1260
1261 if (index != -1) {
1262 if (bus_id != 0 || unit_id != -1) {
1263 fprintf(stderr,
1264 "qemu: '%s' index cannot be used with bus and unit\n", str);
1265 return -1;
1266 }
1267 if (max_devs == 0)
1268 {
1269 unit_id = index;
1270 bus_id = 0;
1271 } else {
1272 unit_id = index % max_devs;
1273 bus_id = index / max_devs;
1274 }
1275 }
1276
1277 /* if user doesn't specify a unit_id,
1278 * try to find the first free
1279 */
1280
1281 if (unit_id == -1) {
1282 unit_id = 0;
1283 while (drive_get_index(type, bus_id, unit_id) != -1) {
1284 unit_id++;
1285 if (max_devs && unit_id >= max_devs) {
1286 unit_id -= max_devs;
1287 bus_id++;
1288 }
1289 }
1290 }
1291
1292 /* check unit id */
1293
1294 if (max_devs && unit_id >= max_devs) {
1295 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1296 str, unit_id, max_devs - 1);
1297 return -1;
1298 }
1299
1300 /*
1301 * ignore multiple definitions
1302 */
1303
1304 if (drive_get_index(type, bus_id, unit_id) != -1)
1305 return -2;
1306
1307 /* init */
1308
1309 if (type == IF_IDE || type == IF_SCSI)
1310 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1311 if (max_devs)
1312 snprintf(buf, sizeof(buf), "%s%i%s%i",
1313 devname, bus_id, mediastr, unit_id);
1314 else
1315 snprintf(buf, sizeof(buf), "%s%s%i",
1316 devname, mediastr, unit_id);
1317 bdrv = bdrv_new(buf);
1318 drives_table_idx = drive_get_free_idx();
1319 drives_table[drives_table_idx].bdrv = bdrv;
1320 drives_table[drives_table_idx].type = type;
1321 drives_table[drives_table_idx].bus = bus_id;
1322 drives_table[drives_table_idx].unit = unit_id;
1323 drives_table[drives_table_idx].onerror = onerror;
1324 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1325 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1326 nb_drives++;
1327
1328 switch(type) {
1329 case IF_IDE:
1330 case IF_SCSI:
1331 case IF_XEN:
1332 switch(media) {
1333 case MEDIA_DISK:
1334 if (cyls != 0) {
1335 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1336 bdrv_set_translation_hint(bdrv, translation);
1337 }
1338 break;
1339 case MEDIA_CDROM:
1340 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1341 break;
1342 }
1343 break;
1344 case IF_SD:
1345 /* FIXME: This isn't really a floppy, but it's a reasonable
1346 approximation. */
1347 case IF_FLOPPY:
1348 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1349 break;
1350 case IF_PFLASH:
1351 case IF_MTD:
1352 case IF_VIRTIO:
1353 break;
1354 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001355 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001356 abort();
1357 }
1358 if (!file[0])
1359 return -2;
1360 bdrv_flags = 0;
1361 if (snapshot) {
1362 bdrv_flags |= BDRV_O_SNAPSHOT;
1363 cache = 2; /* always use write-back with snapshot */
1364 }
1365 if (cache == 0) /* no caching */
1366 bdrv_flags |= BDRV_O_NOCACHE;
1367 else if (cache == 2) /* write-back */
1368 bdrv_flags |= BDRV_O_CACHE_WB;
1369 else if (cache == 3) /* not specified */
1370 bdrv_flags |= BDRV_O_CACHE_DEF;
1371 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1372 fprintf(stderr, "qemu: could not open disk image %s\n",
1373 file);
1374 return -1;
1375 }
1376 if (bdrv_key_required(bdrv))
1377 autostart = 0;
1378 return drives_table_idx;
1379}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001380#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001381
1382static void numa_add(const char *optarg)
1383{
1384 char option[128];
1385 char *endptr;
1386 unsigned long long value, endvalue;
1387 int nodenr;
1388
1389 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1390 if (!strcmp(option, "node")) {
1391 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1392 nodenr = nb_numa_nodes;
1393 } else {
1394 nodenr = strtoull(option, NULL, 10);
1395 }
1396
1397 if (get_param_value(option, 128, "mem", optarg) == 0) {
1398 node_mem[nodenr] = 0;
1399 } else {
1400 value = strtoull(option, &endptr, 0);
1401 switch (*endptr) {
1402 case 0: case 'M': case 'm':
1403 value <<= 20;
1404 break;
1405 case 'G': case 'g':
1406 value <<= 30;
1407 break;
1408 }
1409 node_mem[nodenr] = value;
1410 }
1411 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1412 node_cpumask[nodenr] = 0;
1413 } else {
1414 value = strtoull(option, &endptr, 10);
1415 if (value >= 64) {
1416 value = 63;
1417 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1418 } else {
1419 if (*endptr == '-') {
1420 endvalue = strtoull(endptr+1, &endptr, 10);
1421 if (endvalue >= 63) {
1422 endvalue = 62;
1423 fprintf(stderr,
1424 "only 63 CPUs in NUMA mode supported.\n");
1425 }
1426 value = (1 << (endvalue + 1)) - (1 << value);
1427 } else {
1428 value = 1 << value;
1429 }
1430 }
1431 node_cpumask[nodenr] = value;
1432 }
1433 nb_numa_nodes++;
1434 }
1435 return;
1436}
1437
1438/***********************************************************/
1439/* USB devices */
1440
1441static USBPort *used_usb_ports;
1442static USBPort *free_usb_ports;
1443
1444/* ??? Maybe change this to register a hub to keep track of the topology. */
1445void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1446 usb_attachfn attach)
1447{
1448 port->opaque = opaque;
1449 port->index = index;
1450 port->attach = attach;
1451 port->next = free_usb_ports;
1452 free_usb_ports = port;
1453}
1454
1455int usb_device_add_dev(USBDevice *dev)
1456{
1457 USBPort *port;
1458
1459 /* Find a USB port to add the device to. */
1460 port = free_usb_ports;
1461 if (!port->next) {
1462 USBDevice *hub;
1463
1464 /* Create a new hub and chain it on. */
1465 free_usb_ports = NULL;
1466 port->next = used_usb_ports;
1467 used_usb_ports = port;
1468
1469 hub = usb_hub_init(VM_USB_HUB_SIZE);
1470 usb_attach(port, hub);
1471 port = free_usb_ports;
1472 }
1473
1474 free_usb_ports = port->next;
1475 port->next = used_usb_ports;
1476 used_usb_ports = port;
1477 usb_attach(port, dev);
1478 return 0;
1479}
1480
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001481#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001482static void usb_msd_password_cb(void *opaque, int err)
1483{
1484 USBDevice *dev = opaque;
1485
1486 if (!err)
1487 usb_device_add_dev(dev);
1488 else
1489 dev->handle_destroy(dev);
1490}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001491#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001492
1493static int usb_device_add(const char *devname, int is_hotplug)
1494{
1495 const char *p;
1496 USBDevice *dev;
1497
1498 if (!free_usb_ports)
1499 return -1;
1500
1501 if (strstart(devname, "host:", &p)) {
1502 dev = usb_host_device_open(p);
1503 } else if (!strcmp(devname, "mouse")) {
1504 dev = usb_mouse_init();
1505 } else if (!strcmp(devname, "tablet")) {
1506 dev = usb_tablet_init();
1507 } else if (!strcmp(devname, "keyboard")) {
1508 dev = usb_keyboard_init();
1509 } else if (strstart(devname, "disk:", &p)) {
1510#if 0
1511 BlockDriverState *bs;
1512#endif
1513 dev = usb_msd_init(p);
1514 if (!dev)
1515 return -1;
1516#if 0
1517 bs = usb_msd_get_bdrv(dev);
1518 if (bdrv_key_required(bs)) {
1519 autostart = 0;
1520 if (is_hotplug) {
1521 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1522 dev);
1523 return 0;
1524 }
1525 }
1526 } else if (!strcmp(devname, "wacom-tablet")) {
1527 dev = usb_wacom_init();
1528 } else if (strstart(devname, "serial:", &p)) {
1529 dev = usb_serial_init(p);
1530#ifdef CONFIG_BRLAPI
1531 } else if (!strcmp(devname, "braille")) {
1532 dev = usb_baum_init();
1533#endif
1534 } else if (strstart(devname, "net:", &p)) {
1535 int nic = nb_nics;
1536
1537 if (net_client_init("nic", p) < 0)
1538 return -1;
1539 nd_table[nic].model = "usb";
1540 dev = usb_net_init(&nd_table[nic]);
1541 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1542 dev = usb_bt_init(devname[2] ? hci_init(p) :
1543 bt_new_hci(qemu_find_bt_vlan(0)));
1544#endif
1545 } else {
1546 return -1;
1547 }
1548 if (!dev)
1549 return -1;
1550
1551 return usb_device_add_dev(dev);
1552}
1553
1554int usb_device_del_addr(int bus_num, int addr)
1555{
1556 USBPort *port;
1557 USBPort **lastp;
1558 USBDevice *dev;
1559
1560 if (!used_usb_ports)
1561 return -1;
1562
1563 if (bus_num != 0)
1564 return -1;
1565
1566 lastp = &used_usb_ports;
1567 port = used_usb_ports;
1568 while (port && port->dev->addr != addr) {
1569 lastp = &port->next;
1570 port = port->next;
1571 }
1572
1573 if (!port)
1574 return -1;
1575
1576 dev = port->dev;
1577 *lastp = port->next;
1578 usb_attach(port, NULL);
1579 dev->handle_destroy(dev);
1580 port->next = free_usb_ports;
1581 free_usb_ports = port;
1582 return 0;
1583}
1584
1585static int usb_device_del(const char *devname)
1586{
1587 int bus_num, addr;
1588 const char *p;
1589
1590 if (strstart(devname, "host:", &p))
1591 return usb_host_device_close(p);
1592
1593 if (!used_usb_ports)
1594 return -1;
1595
1596 p = strchr(devname, '.');
1597 if (!p)
1598 return -1;
1599 bus_num = strtoul(devname, NULL, 0);
1600 addr = strtoul(p + 1, NULL, 0);
1601
1602 return usb_device_del_addr(bus_num, addr);
1603}
1604
1605void do_usb_add(Monitor *mon, const char *devname)
1606{
1607 usb_device_add(devname, 1);
1608}
1609
1610void do_usb_del(Monitor *mon, const char *devname)
1611{
1612 usb_device_del(devname);
1613}
1614
1615void usb_info(Monitor *mon)
1616{
1617 USBDevice *dev;
1618 USBPort *port;
1619 const char *speed_str;
1620
1621 if (!usb_enabled) {
1622 monitor_printf(mon, "USB support not enabled\n");
1623 return;
1624 }
1625
1626 for (port = used_usb_ports; port; port = port->next) {
1627 dev = port->dev;
1628 if (!dev)
1629 continue;
1630 switch(dev->speed) {
1631 case USB_SPEED_LOW:
1632 speed_str = "1.5";
1633 break;
1634 case USB_SPEED_FULL:
1635 speed_str = "12";
1636 break;
1637 case USB_SPEED_HIGH:
1638 speed_str = "480";
1639 break;
1640 default:
1641 speed_str = "?";
1642 break;
1643 }
1644 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1645 0, dev->addr, speed_str, dev->devname);
1646 }
1647}
1648
1649/***********************************************************/
1650/* PCMCIA/Cardbus */
1651
1652static struct pcmcia_socket_entry_s {
1653 PCMCIASocket *socket;
1654 struct pcmcia_socket_entry_s *next;
1655} *pcmcia_sockets = 0;
1656
1657void pcmcia_socket_register(PCMCIASocket *socket)
1658{
1659 struct pcmcia_socket_entry_s *entry;
1660
1661 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1662 entry->socket = socket;
1663 entry->next = pcmcia_sockets;
1664 pcmcia_sockets = entry;
1665}
1666
1667void pcmcia_socket_unregister(PCMCIASocket *socket)
1668{
1669 struct pcmcia_socket_entry_s *entry, **ptr;
1670
1671 ptr = &pcmcia_sockets;
1672 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1673 if (entry->socket == socket) {
1674 *ptr = entry->next;
1675 qemu_free(entry);
1676 }
1677}
1678
1679void pcmcia_info(Monitor *mon)
1680{
1681 struct pcmcia_socket_entry_s *iter;
1682
1683 if (!pcmcia_sockets)
1684 monitor_printf(mon, "No PCMCIA sockets\n");
1685
1686 for (iter = pcmcia_sockets; iter; iter = iter->next)
1687 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1688 iter->socket->attached ? iter->socket->card_string :
1689 "Empty");
1690}
1691
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001692/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001693/* machine registration */
1694
1695static QEMUMachine *first_machine = NULL;
1696QEMUMachine *current_machine = NULL;
1697
1698int qemu_register_machine(QEMUMachine *m)
1699{
1700 QEMUMachine **pm;
1701 pm = &first_machine;
1702 while (*pm != NULL)
1703 pm = &(*pm)->next;
1704 m->next = NULL;
1705 *pm = m;
1706 return 0;
1707}
1708
1709static QEMUMachine *find_machine(const char *name)
1710{
1711 QEMUMachine *m;
1712
1713 for(m = first_machine; m != NULL; m = m->next) {
1714 if (!strcmp(m->name, name))
1715 return m;
1716 }
1717 return NULL;
1718}
1719
1720static QEMUMachine *find_default_machine(void)
1721{
1722 QEMUMachine *m;
1723
1724 for(m = first_machine; m != NULL; m = m->next) {
1725 if (m->is_default) {
1726 return m;
1727 }
1728 }
1729 return NULL;
1730}
1731
1732/***********************************************************/
1733/* main execution loop */
1734
1735static void gui_update(void *opaque)
1736{
1737 uint64_t interval = GUI_REFRESH_INTERVAL;
1738 DisplayState *ds = opaque;
1739 DisplayChangeListener *dcl = ds->listeners;
1740
1741 dpy_refresh(ds);
1742
1743 while (dcl != NULL) {
1744 if (dcl->gui_timer_interval &&
1745 dcl->gui_timer_interval < interval)
1746 interval = dcl->gui_timer_interval;
1747 dcl = dcl->next;
1748 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001749 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001750}
1751
1752static void nographic_update(void *opaque)
1753{
1754 uint64_t interval = GUI_REFRESH_INTERVAL;
1755
David 'Digit' Turner5973c772011-05-10 07:06:00 +02001756 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001757}
1758
1759struct vm_change_state_entry {
1760 VMChangeStateHandler *cb;
1761 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001762 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001763};
1764
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001765static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001766
1767VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1768 void *opaque)
1769{
1770 VMChangeStateEntry *e;
1771
1772 e = qemu_mallocz(sizeof (*e));
1773
1774 e->cb = cb;
1775 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001776 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001777 return e;
1778}
1779
1780void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1781{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07001782 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001783 qemu_free (e);
1784}
1785
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001786void vm_state_notify(int running, int reason)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001787{
1788 VMChangeStateEntry *e;
1789
1790 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1791 e->cb(e->opaque, running, reason);
1792 }
1793}
1794
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001795void vm_start(void)
1796{
1797 if (!vm_running) {
1798 cpu_enable_ticks();
1799 vm_running = 1;
1800 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02001801 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001802 resume_all_vcpus();
1803 }
1804}
1805
1806/* reset/shutdown handler */
1807
1808typedef struct QEMUResetEntry {
1809 QEMUResetHandler *func;
1810 void *opaque;
1811 int order;
1812 struct QEMUResetEntry *next;
1813} QEMUResetEntry;
1814
1815static QEMUResetEntry *first_reset_entry;
1816static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001817static int shutdown_requested, shutdown_signal = -1;
1818static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001819static int powerdown_requested;
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001820int debug_requested;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001821static int vmstop_requested;
1822
1823int qemu_shutdown_requested(void)
1824{
1825 int r = shutdown_requested;
1826 shutdown_requested = 0;
1827 return r;
1828}
1829
1830int qemu_reset_requested(void)
1831{
1832 int r = reset_requested;
1833 reset_requested = 0;
1834 return r;
1835}
1836
1837int qemu_powerdown_requested(void)
1838{
1839 int r = powerdown_requested;
1840 powerdown_requested = 0;
1841 return r;
1842}
1843
1844static int qemu_debug_requested(void)
1845{
1846 int r = debug_requested;
1847 debug_requested = 0;
1848 return r;
1849}
1850
1851static int qemu_vmstop_requested(void)
1852{
1853 int r = vmstop_requested;
1854 vmstop_requested = 0;
1855 return r;
1856}
1857
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001858void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
1859{
1860 QEMUResetEntry **pre, *re;
1861
1862 pre = &first_reset_entry;
1863 while (*pre != NULL && (*pre)->order >= order) {
1864 pre = &(*pre)->next;
1865 }
1866 re = qemu_mallocz(sizeof(QEMUResetEntry));
1867 re->func = func;
1868 re->opaque = opaque;
1869 re->order = order;
1870 re->next = NULL;
1871 *pre = re;
1872}
1873
1874void qemu_system_reset(void)
1875{
1876 QEMUResetEntry *re;
1877
1878 /* reset all devices */
1879 for(re = first_reset_entry; re != NULL; re = re->next) {
1880 re->func(re->opaque);
1881 }
1882}
1883
1884void qemu_system_reset_request(void)
1885{
1886 if (no_reboot) {
1887 shutdown_requested = 1;
1888 } else {
1889 reset_requested = 1;
1890 }
1891 qemu_notify_event();
1892}
1893
David 'Digit' Turner088edf82011-05-09 15:59:28 +02001894void qemu_system_killed(int signal, pid_t pid)
1895{
1896 shutdown_signal = signal;
1897 shutdown_pid = pid;
1898 qemu_system_shutdown_request();
1899}
1900
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001901void qemu_system_shutdown_request(void)
1902{
1903 shutdown_requested = 1;
1904 qemu_notify_event();
1905}
1906
1907void qemu_system_powerdown_request(void)
1908{
1909 powerdown_requested = 1;
1910 qemu_notify_event();
1911}
1912
1913#ifdef CONFIG_IOTHREAD
1914static void qemu_system_vmstop_request(int reason)
1915{
1916 vmstop_requested = reason;
1917 qemu_notify_event();
1918}
1919#endif
1920
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001921void main_loop_wait(int timeout)
1922{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001923 fd_set rfds, wfds, xfds;
1924 int ret, nfds;
1925 struct timeval tv;
1926
1927 qemu_bh_update_timeout(&timeout);
1928
David 'Digit' Turner23ca2ae2011-06-01 16:14:53 +02001929 os_host_main_loop_wait(&timeout);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001930
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001931
1932 tv.tv_sec = timeout / 1000;
1933 tv.tv_usec = (timeout % 1000) * 1000;
1934
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001935 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001936
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001937 /* XXX: separate device handlers from system ones */
1938 nfds = -1;
1939 FD_ZERO(&rfds);
1940 FD_ZERO(&wfds);
1941 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001942 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001943 if (slirp_is_inited()) {
1944 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1945 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001946
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001947 qemu_mutex_unlock_iothread();
1948 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1949 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02001950 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001951 if (slirp_is_inited()) {
1952 if (ret < 0) {
1953 FD_ZERO(&rfds);
1954 FD_ZERO(&wfds);
1955 FD_ZERO(&xfds);
1956 }
1957 slirp_select_poll(&rfds, &wfds, &xfds);
1958 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001959 charpipe_poll();
1960
David Turner6a9ef172010-09-09 22:54:36 +02001961 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07001962
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001963 /* Check bottom-halves last in case any of the earlier events triggered
1964 them. */
1965 qemu_bh_poll();
1966
1967}
1968
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001969static int vm_can_run(void)
1970{
1971 if (powerdown_requested)
1972 return 0;
1973 if (reset_requested)
1974 return 0;
1975 if (shutdown_requested)
1976 return 0;
1977 if (debug_requested)
1978 return 0;
1979 return 1;
1980}
1981
1982static void main_loop(void)
1983{
1984 int r;
1985
1986#ifdef CONFIG_IOTHREAD
1987 qemu_system_ready = 1;
1988 qemu_cond_broadcast(&qemu_system_cond);
1989#endif
1990
1991 for (;;) {
1992 do {
1993#ifdef CONFIG_PROFILER
1994 int64_t ti;
1995#endif
1996#ifndef CONFIG_IOTHREAD
1997 tcg_cpu_exec();
1998#endif
1999#ifdef CONFIG_PROFILER
2000 ti = profile_getclock();
2001#endif
2002 main_loop_wait(qemu_calculate_timeout());
2003#ifdef CONFIG_PROFILER
2004 dev_time += profile_getclock() - ti;
2005#endif
rich canningsd952f282011-03-01 15:40:09 -08002006
2007 if (rotate_logs_requested) {
2008 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
2009 dns_log_filename);
2010 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
2011 drop_log_filename);
2012 slirp_dns_log_fd(new_dns_log_fd);
2013 slirp_drop_log_fd(new_drop_log_fd);
2014 reset_rotate_qemu_logs_request();
2015 }
2016
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002017 } while (vm_can_run());
2018
2019 if (qemu_debug_requested())
2020 vm_stop(EXCP_DEBUG);
2021 if (qemu_shutdown_requested()) {
2022 if (no_shutdown) {
2023 vm_stop(0);
2024 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002025 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00002026 if (savevm_on_exit != NULL) {
2027 do_savevm(cur_mon, savevm_on_exit);
2028 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002029 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00002030 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002031 }
2032 if (qemu_reset_requested()) {
2033 pause_all_vcpus();
2034 qemu_system_reset();
2035 resume_all_vcpus();
2036 }
2037 if (qemu_powerdown_requested())
2038 qemu_system_powerdown();
2039 if ((r = qemu_vmstop_requested()))
2040 vm_stop(r);
2041 }
2042 pause_all_vcpus();
2043}
2044
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002045void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002046{
2047 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2048}
2049
2050void qemu_help(int exitcode)
2051{
2052 version();
2053 printf("usage: %s [options] [disk_image]\n"
2054 "\n"
2055 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2056 "\n"
2057#define DEF(option, opt_arg, opt_enum, opt_help) \
2058 opt_help
2059#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002060#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002061#undef DEF
2062#undef DEFHEADING
2063#undef GEN_DOCS
2064 "\n"
2065 "During emulation, the following keys are useful:\n"
2066 "ctrl-alt-f toggle full screen\n"
2067 "ctrl-alt-n switch to virtual console 'n'\n"
2068 "ctrl-alt toggle mouse and keyboard grab\n"
2069 "\n"
2070 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2071 ,
2072 "qemu",
2073 DEFAULT_RAM_SIZE,
2074#ifndef _WIN32
2075 DEFAULT_NETWORK_SCRIPT,
2076 DEFAULT_NETWORK_DOWN_SCRIPT,
2077#endif
2078 DEFAULT_GDBSTUB_PORT,
2079 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002080 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002081}
2082
2083#define HAS_ARG 0x0001
2084
2085enum {
2086#define DEF(option, opt_arg, opt_enum, opt_help) \
2087 opt_enum,
2088#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002089#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002090#undef DEF
2091#undef DEFHEADING
2092#undef GEN_DOCS
2093};
2094
2095typedef struct QEMUOption {
2096 const char *name;
2097 int flags;
2098 int index;
2099} QEMUOption;
2100
2101static const QEMUOption qemu_options[] = {
2102 { "h", 0, QEMU_OPTION_h },
2103#define DEF(option, opt_arg, opt_enum, opt_help) \
2104 { option, opt_arg, opt_enum },
2105#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002106#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002107#undef DEF
2108#undef DEFHEADING
2109#undef GEN_DOCS
2110 { NULL, 0, 0 },
2111};
2112
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002113static void select_vgahw (const char *p)
2114{
2115 const char *opts;
2116
2117 cirrus_vga_enabled = 0;
2118 std_vga_enabled = 0;
2119 vmsvga_enabled = 0;
2120 xenfb_enabled = 0;
2121 if (strstart(p, "std", &opts)) {
2122 std_vga_enabled = 1;
2123 } else if (strstart(p, "cirrus", &opts)) {
2124 cirrus_vga_enabled = 1;
2125 } else if (strstart(p, "vmware", &opts)) {
2126 vmsvga_enabled = 1;
2127 } else if (strstart(p, "xenfb", &opts)) {
2128 xenfb_enabled = 1;
2129 } else if (!strstart(p, "none", &opts)) {
2130 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002131 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002132 }
2133 while (*opts) {
2134 const char *nextopt;
2135
2136 if (strstart(opts, ",retrace=", &nextopt)) {
2137 opts = nextopt;
2138 if (strstart(opts, "dumb", &nextopt))
2139 vga_retrace_method = VGA_RETRACE_DUMB;
2140 else if (strstart(opts, "precise", &nextopt))
2141 vga_retrace_method = VGA_RETRACE_PRECISE;
2142 else goto invalid_vga;
2143 } else goto invalid_vga;
2144 opts = nextopt;
2145 }
2146}
2147
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002148#define MAX_NET_CLIENTS 32
2149
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002150#ifdef _WIN32
2151/* Look for support files in the same directory as the executable. */
2152static char *find_datadir(const char *argv0)
2153{
2154 char *p;
2155 char buf[MAX_PATH];
2156 DWORD len;
2157
2158 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2159 if (len == 0) {
2160 return NULL;
2161 }
2162
2163 buf[len] = 0;
2164 p = buf + len - 1;
2165 while (p != buf && *p != '\\')
2166 p--;
2167 *p = 0;
2168 if (access(buf, R_OK) == 0) {
2169 return qemu_strdup(buf);
2170 }
2171 return NULL;
2172}
2173#else /* !_WIN32 */
2174
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002175/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002176static char *find_datadir(const char *argv0)
2177{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002178 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002179 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002180
2181#if defined(__linux__)
2182 {
2183 int len;
2184 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2185 if (len > 0) {
2186 buf[len] = 0;
2187 p = buf;
2188 }
2189 }
2190#elif defined(__FreeBSD__)
2191 {
2192 int len;
2193 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2194 if (len > 0) {
2195 buf[len] = 0;
2196 p = buf;
2197 }
2198 }
2199#endif
2200 /* If we don't have any way of figuring out the actual executable
2201 location then try argv[0]. */
2202 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002203 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002204 if (!p) {
2205 return NULL;
2206 }
2207 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002208
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002209 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002210}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002211#endif
2212
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002213static char*
2214qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
2215{
2216 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2217 char* buf = qemu_mallocz(len);
2218
2219 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2220 VERBOSE_PRINT(init," trying to find: %s\n", buf);
2221 if (access(buf, R_OK)) {
2222 qemu_free(buf);
2223 return NULL;
2224 }
2225 return buf;
2226}
2227
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002228char *qemu_find_file(int type, const char *name)
2229{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002230 const char *subdir;
2231 char *buf;
2232
2233 /* If name contains path separators then try it as a straight path. */
2234 if ((strchr(name, '/') || strchr(name, '\\'))
2235 && access(name, R_OK) == 0) {
2236 return strdup(name);
2237 }
2238 switch (type) {
2239 case QEMU_FILE_TYPE_BIOS:
2240 subdir = "";
2241 break;
2242 case QEMU_FILE_TYPE_KEYMAP:
2243 subdir = "keymaps/";
2244 break;
2245 default:
2246 abort();
2247 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002248 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
2249#ifdef CONFIG_ANDROID
2250 if (type == QEMU_FILE_TYPE_BIOS) {
2251 /* This case corresponds to the emulator being used as part of an
2252 * SDK installation. NOTE: data_dir is really $bindir. */
2253 if (buf == NULL)
2254 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
2255 /* This case corresponds to platform builds. */
2256 if (buf == NULL)
2257 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
2258 /* Finally, try this for standalone builds under external/qemu */
2259 if (buf == NULL)
2260 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002261 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02002262#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002263 return buf;
2264}
2265
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002266static int
2267add_dns_server( const char* server_name )
2268{
2269 SockAddress addr;
2270
2271 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
2272 fprintf(stdout,
2273 "### WARNING: can't resolve DNS server name '%s'\n",
2274 server_name );
2275 return -1;
2276 }
2277
2278 fprintf(stderr,
2279 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
2280
2281 if ( slirp_add_dns_server( &addr ) < 0 ) {
2282 fprintf(stderr,
2283 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
2284 return -1;
2285 }
2286 return 0;
2287}
2288
rich cannings7339b552011-02-16 13:43:44 -08002289/* Parses an integer
2290 * Pararm:
2291 * str String containing a number to be parsed.
2292 * result Passes the parsed integer in this argument
2293 * returns 0 if ok, -1 if failed
2294 */
2295int
2296parse_int(const char *str, int *result)
2297{
2298 char* r;
2299 *result = strtol(str, &r, 0);
2300 if (r == NULL || *r != '\0')
2301 return -1;
2302
2303 return 0;
2304}
2305
rich canningsd952f282011-03-01 15:40:09 -08002306#ifndef _WIN32
2307/*
2308 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
2309 */
2310void init_qemu_clear_logs_sig() {
2311 struct sigaction act;
2312 sigfillset(&act.sa_mask);
2313 act.sa_flags = 0;
2314 act.sa_handler = rotate_qemu_logs_handler;
2315 if (sigaction(SIGUSR1, &act, NULL) == -1) {
2316 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
2317 exit(-1);
2318 }
2319}
2320#endif
2321
2322
rich cannings7339b552011-02-16 13:43:44 -08002323
2324/* parses a null-terminated string specifying a network port (e.g., "80") or
2325 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
2326 * are the same. Returns 0 on success, -1 on error. */
2327
2328int parse_port_range(const char *str, unsigned short *lport,
2329 unsigned short *hport) {
2330
2331 unsigned int low = 0, high = 0;
2332 char *p, *arg = strdup(str);
2333
2334 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
2335 p = arg + 1; /* skip '[' */
2336 low = atoi(strtok(p, "-"));
2337 high = atoi(strtok(NULL, "-"));
2338 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
2339 *lport = low;
2340 *hport = high;
2341 }
2342 }
2343 else {
2344 low = atoi(arg);
2345 if ((0 < low) && (low < 65535)) {
2346 *lport = low;
2347 *hport = low;
2348 }
2349 }
2350 free(arg);
2351 if (low != 0)
2352 return 0;
2353 return -1;
2354}
2355
2356/*
2357 * Implements the generic port forwarding option
2358 */
2359void
2360net_slirp_forward(const char *optarg)
2361{
2362 /*
2363 * we expect the following format:
2364 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
2365 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
2366 */
2367 char *argument = strdup(optarg), *p = argument;
2368 char *dst_net, *dst_mask, *dst_port;
2369 char *redirect_ip, *redirect_port;
2370 uint32_t dnet, dmask, rip;
2371 unsigned short dlport, dhport, rport;
2372
2373
2374 dst_net = strtok(p, ":");
2375 dst_mask = strtok(NULL, ":");
2376 dst_port = strtok(NULL, ":");
2377 redirect_ip = strtok(NULL, ":");
2378 redirect_port = strtok(NULL, ":");
2379
2380 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
2381 redirect_ip == NULL || redirect_port == NULL) {
2382 fprintf(stderr,
2383 "Invalid argument for -net-forward, we expect "
2384 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
2385 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
2386 ":redirect_ip:redirect_port: %s\n",
2387 optarg);
2388 exit(1);
2389 }
2390
2391 /* inet_strtoip converts dotted address to host byte order */
2392 if (inet_strtoip(dst_net, &dnet) == -1) {
2393 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
2394 exit(1);
2395 }
2396 if (inet_strtoip(dst_mask, &dmask) == -1) {
2397 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
2398 exit(1);
2399 }
2400 if (inet_strtoip(redirect_ip, &rip) == -1) {
2401 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
2402 exit(1);
2403 }
2404
2405 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
2406 fprintf(stderr, "Invalid destination port or port range\n");
2407 exit(1);
2408 }
2409
2410 rport = atoi(redirect_port);
2411 if (!rport) {
2412 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
2413 exit(1);
2414 }
2415
2416 dnet &= dmask;
2417
2418 slirp_add_net_forward(dnet, dmask, dlport, dhport,
2419 rip, rport);
2420
2421 free(argument);
2422}
2423
2424
2425/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
2426 * entry in the allows list */
2427void
2428slirp_allow(const char *optarg, u_int8_t proto)
2429{
2430 /*
2431 * we expect the following format:
2432 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
2433 */
2434 char *argument = strdup(optarg), *p = argument;
2435 char *dst_ip_str, *dst_port_str;
2436 uint32_t dst_ip;
2437 unsigned short dst_lport, dst_hport;
2438
2439 dst_ip_str = strtok(p, ":");
2440 dst_port_str = strtok(NULL, ":");
2441
2442 if (dst_ip_str == NULL || dst_port_str == NULL) {
2443 fprintf(stderr,
2444 "Invalid argument %s for -allow. We expect "
2445 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
2446 optarg);
2447 exit(1);
2448 }
2449
2450 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
2451 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
2452 exit(1);
2453 }
2454 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
2455 fprintf(stderr, "Invalid destination port or port range\n");
2456 exit(1);
2457 }
2458
2459 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
2460
2461 free(argument);
2462}
2463
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01002464/* Add a serial device at a given location in the emulated hardware table.
2465 * On failure, this function aborts the program with an error message.
2466 */
2467static void
2468serial_hds_add_at(int index, const char* devname)
2469{
2470 char label[32];
2471
2472 if (!devname || !strcmp(devname,"none"))
2473 return;
2474
2475 if (index >= MAX_SERIAL_PORTS) {
2476 PANIC("qemu: invalid serial index for %s (%d >= %d)",
2477 devname, index, MAX_SERIAL_PORTS);
2478 }
2479 if (serial_hds[index] != NULL) {
2480 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
2481 devname, index);
2482 }
2483 snprintf(label, sizeof(label), "serial%d", index);
2484 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2485 if (!serial_hds[index]) {
2486 PANIC("qemu: could not open serial device '%s'", devname);
2487 }
2488}
2489
2490
2491/* Find a free slot in the emulated serial device table, and register
2492 * it. Return the allocated table index.
2493 */
2494static int
2495serial_hds_add(const char* devname)
2496{
2497 int index;
2498
2499 /* Find first free slot */
2500 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
2501 if (serial_hds[index] == NULL) {
2502 serial_hds_add_at(index, devname);
2503 return index;
2504 }
2505 }
2506
2507 PANIC("qemu: too many serial devices registered (%d)", index);
2508 return -1; /* shouldn't happen */
2509}
2510
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002511int main(int argc, char **argv, char **envp)
2512{
2513 const char *gdbstub_dev = NULL;
2514 uint32_t boot_devices_bitmap = 0;
2515 int i;
2516 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02002517 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002518 const char *initrd_filename;
2519 const char *kernel_filename, *kernel_cmdline;
2520 const char *boot_devices = "";
2521 DisplayState *ds;
2522 DisplayChangeListener *dcl;
2523 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002524 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002525 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002526 const char *net_clients[MAX_NET_CLIENTS];
2527 int nb_net_clients;
2528 const char *bt_opts[MAX_BT_CMDLINE];
2529 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002530 int optind;
2531 const char *r, *optarg;
2532 CharDriverState *monitor_hd = NULL;
2533 const char *monitor_device;
2534 const char *serial_devices[MAX_SERIAL_PORTS];
2535 int serial_device_index;
2536 const char *parallel_devices[MAX_PARALLEL_PORTS];
2537 int parallel_device_index;
2538 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
2539 int virtio_console_index;
2540 const char *loadvm = NULL;
2541 QEMUMachine *machine;
2542 const char *cpu_model;
2543 const char *usb_devices[MAX_USB_CMDLINE];
2544 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002545 int tb_size;
2546 const char *pid_file = NULL;
2547 const char *incoming = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002548 CPUState *env;
2549 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07002550 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002551 STRALLOC_DEFINE(kernel_params);
2552 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07002553 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002554
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002555 /* Initialize sockets before anything else, so we can properly report
2556 * initialization failures back to the UI. */
2557#ifdef _WIN32
2558 socket_init();
2559#endif
2560
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002561 init_clocks();
2562
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002563 qemu_cache_utils_init(envp);
2564
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002565 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02002566 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002567
2568 module_call_init(MODULE_INIT_MACHINE);
2569 machine = find_default_machine();
2570 cpu_model = NULL;
2571 initrd_filename = NULL;
2572 ram_size = 0;
2573 snapshot = 0;
2574 kernel_filename = NULL;
2575 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01002576
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002577 cyls = heads = secs = 0;
2578 translation = BIOS_ATA_TRANSLATION_AUTO;
2579 monitor_device = "vc:80Cx24C";
2580
2581 serial_devices[0] = "vc:80Cx24C";
2582 for(i = 1; i < MAX_SERIAL_PORTS; i++)
2583 serial_devices[i] = NULL;
2584 serial_device_index = 0;
2585
2586 parallel_devices[0] = "vc:80Cx24C";
2587 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
2588 parallel_devices[i] = NULL;
2589 parallel_device_index = 0;
2590
2591 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
2592 virtio_consoles[i] = NULL;
2593 virtio_console_index = 0;
2594
2595 for (i = 0; i < MAX_NODES; i++) {
2596 node_mem[i] = 0;
2597 node_cpumask[i] = 0;
2598 }
2599
2600 usb_devices_index = 0;
2601
2602 nb_net_clients = 0;
2603 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002604#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002605 nb_drives = 0;
2606 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002607#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002608 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002609
2610 nb_nics = 0;
2611
2612 tb_size = 0;
2613 autostart= 1;
2614
2615 register_watchdogs();
2616
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002617 /* Initialize boot properties. */
2618 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02002619 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02002620 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07002621
David 'Digit' Turner36597752011-05-20 01:18:01 +02002622#ifdef CONFIG_KVM
2623 /* By default, force auto-detection for kvm */
2624 kvm_allowed = -1;
2625#endif
2626
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002627 optind = 1;
2628 for(;;) {
2629 if (optind >= argc)
2630 break;
2631 r = argv[optind];
2632 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002633 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002634 } else {
2635 const QEMUOption *popt;
2636
2637 optind++;
2638 /* Treat --foo the same as -foo. */
2639 if (r[1] == '-')
2640 r++;
2641 popt = qemu_options;
2642 for(;;) {
2643 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002644 PANIC("%s: invalid option -- '%s'",
2645 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002646 }
2647 if (!strcmp(popt->name, r + 1))
2648 break;
2649 popt++;
2650 }
2651 if (popt->flags & HAS_ARG) {
2652 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002653 PANIC("%s: option '%s' requires an argument",
2654 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002655 }
2656 optarg = argv[optind++];
2657 } else {
2658 optarg = NULL;
2659 }
2660
2661 switch(popt->index) {
2662 case QEMU_OPTION_M:
2663 machine = find_machine(optarg);
2664 if (!machine) {
2665 QEMUMachine *m;
2666 printf("Supported machines are:\n");
2667 for(m = first_machine; m != NULL; m = m->next) {
2668 printf("%-10s %s%s\n",
2669 m->name, m->desc,
2670 m->is_default ? " (default)" : "");
2671 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002672 if (*optarg != '?') {
2673 PANIC("Invalid machine parameter: %s",
2674 optarg);
2675 } else {
2676 QEMU_EXIT(0);
2677 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002678 }
2679 break;
2680 case QEMU_OPTION_cpu:
2681 /* hw initialization will check this */
2682 if (*optarg == '?') {
2683/* XXX: implement xxx_cpu_list for targets that still miss it */
2684#if defined(cpu_list)
2685 cpu_list(stdout, &fprintf);
2686#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002687 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002688 } else {
2689 cpu_model = optarg;
2690 }
2691 break;
2692 case QEMU_OPTION_initrd:
2693 initrd_filename = optarg;
2694 break;
2695 case QEMU_OPTION_hda:
2696 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002697 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002698 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002699 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002700 ",cyls=%d,heads=%d,secs=%d%s",
2701 0, cyls, heads, secs,
2702 translation == BIOS_ATA_TRANSLATION_LBA ?
2703 ",trans=lba" :
2704 translation == BIOS_ATA_TRANSLATION_NONE ?
2705 ",trans=none" : "");
2706 break;
2707 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01002708 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
2709 break;
2710
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002711 case QEMU_OPTION_hdc:
2712 case QEMU_OPTION_hdd:
2713 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2714 break;
2715 case QEMU_OPTION_drive:
2716 drive_add(NULL, "%s", optarg);
2717 break;
2718 case QEMU_OPTION_mtdblock:
2719 drive_add(optarg, MTD_ALIAS);
2720 break;
2721 case QEMU_OPTION_sd:
2722 drive_add(optarg, SD_ALIAS);
2723 break;
2724 case QEMU_OPTION_pflash:
2725 drive_add(optarg, PFLASH_ALIAS);
2726 break;
2727 case QEMU_OPTION_snapshot:
2728 snapshot = 1;
2729 break;
2730 case QEMU_OPTION_hdachs:
2731 {
2732 const char *p;
2733 p = optarg;
2734 cyls = strtol(p, (char **)&p, 0);
2735 if (cyls < 1 || cyls > 16383)
2736 goto chs_fail;
2737 if (*p != ',')
2738 goto chs_fail;
2739 p++;
2740 heads = strtol(p, (char **)&p, 0);
2741 if (heads < 1 || heads > 16)
2742 goto chs_fail;
2743 if (*p != ',')
2744 goto chs_fail;
2745 p++;
2746 secs = strtol(p, (char **)&p, 0);
2747 if (secs < 1 || secs > 63)
2748 goto chs_fail;
2749 if (*p == ',') {
2750 p++;
2751 if (!strcmp(p, "none"))
2752 translation = BIOS_ATA_TRANSLATION_NONE;
2753 else if (!strcmp(p, "lba"))
2754 translation = BIOS_ATA_TRANSLATION_LBA;
2755 else if (!strcmp(p, "auto"))
2756 translation = BIOS_ATA_TRANSLATION_AUTO;
2757 else
2758 goto chs_fail;
2759 } else if (*p != '\0') {
2760 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002761 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002762 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01002763 if (hda_opts != NULL) {
2764 char num[16];
2765 snprintf(num, sizeof(num), "%d", cyls);
2766 qemu_opt_set(hda_opts, "cyls", num);
2767 snprintf(num, sizeof(num), "%d", heads);
2768 qemu_opt_set(hda_opts, "heads", num);
2769 snprintf(num, sizeof(num), "%d", secs);
2770 qemu_opt_set(hda_opts, "secs", num);
2771 if (translation == BIOS_ATA_TRANSLATION_LBA)
2772 qemu_opt_set(hda_opts, "trans", "lba");
2773 if (translation == BIOS_ATA_TRANSLATION_NONE)
2774 qemu_opt_set(hda_opts, "trans", "none");
2775 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002776 }
2777 break;
2778 case QEMU_OPTION_numa:
2779 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002780 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002781 }
2782 numa_add(optarg);
2783 break;
2784 case QEMU_OPTION_nographic:
2785 display_type = DT_NOGRAPHIC;
2786 break;
2787#ifdef CONFIG_CURSES
2788 case QEMU_OPTION_curses:
2789 display_type = DT_CURSES;
2790 break;
2791#endif
2792 case QEMU_OPTION_portrait:
2793 graphic_rotate = 1;
2794 break;
2795 case QEMU_OPTION_kernel:
2796 kernel_filename = optarg;
2797 break;
2798 case QEMU_OPTION_append:
2799 kernel_cmdline = optarg;
2800 break;
2801 case QEMU_OPTION_cdrom:
2802 drive_add(optarg, CDROM_ALIAS);
2803 break;
2804 case QEMU_OPTION_boot:
2805 boot_devices = optarg;
2806 /* We just do some generic consistency checks */
2807 {
2808 /* Could easily be extended to 64 devices if needed */
2809 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002810
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002811 boot_devices_bitmap = 0;
2812 for (p = boot_devices; *p != '\0'; p++) {
2813 /* Allowed boot devices are:
2814 * a b : floppy disk drives
2815 * c ... f : IDE disk drives
2816 * g ... m : machine implementation dependant drives
2817 * n ... p : network devices
2818 * It's up to each machine implementation to check
2819 * if the given boot devices match the actual hardware
2820 * implementation and firmware features.
2821 */
2822 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002823 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002824 }
2825 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002826 PANIC(
2827 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002828 }
2829 boot_devices_bitmap |= 1 << (*p - 'a');
2830 }
2831 }
2832 break;
2833 case QEMU_OPTION_fda:
2834 case QEMU_OPTION_fdb:
2835 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2836 break;
2837#ifdef TARGET_I386
2838 case QEMU_OPTION_no_fd_bootchk:
2839 fd_bootchk = 0;
2840 break;
2841#endif
2842 case QEMU_OPTION_net:
2843 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002844 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002845 }
2846 net_clients[nb_net_clients] = optarg;
2847 nb_net_clients++;
2848 break;
2849#ifdef CONFIG_SLIRP
2850 case QEMU_OPTION_tftp:
2851 tftp_prefix = optarg;
2852 break;
2853 case QEMU_OPTION_bootp:
2854 bootp_filename = optarg;
2855 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002856 case QEMU_OPTION_redir:
2857 net_slirp_redir(NULL, optarg, NULL);
2858 break;
2859#endif
2860 case QEMU_OPTION_bt:
2861 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002862 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002863 }
2864 bt_opts[nb_bt_opts++] = optarg;
2865 break;
2866#ifdef HAS_AUDIO
2867 case QEMU_OPTION_audio_help:
2868 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002869 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002870 break;
2871 case QEMU_OPTION_soundhw:
2872 select_soundhw (optarg);
2873 break;
2874#endif
2875 case QEMU_OPTION_h:
2876 qemu_help(0);
2877 break;
2878 case QEMU_OPTION_version:
2879 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002880 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002881 break;
2882 case QEMU_OPTION_m: {
2883 uint64_t value;
2884 char *ptr;
2885
2886 value = strtoul(optarg, &ptr, 10);
2887 switch (*ptr) {
2888 case 0: case 'M': case 'm':
2889 value <<= 20;
2890 break;
2891 case 'G': case 'g':
2892 value <<= 30;
2893 break;
2894 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002895 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002896 }
2897
2898 /* On 32-bit hosts, QEMU is limited by virtual address space */
2899 if (value > (2047 << 20)
2900#ifndef CONFIG_KQEMU
2901 && HOST_LONG_BITS == 32
2902#endif
2903 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002904 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002905 }
2906 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002907 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002908 }
2909 ram_size = value;
2910 break;
2911 }
2912 case QEMU_OPTION_d:
2913 {
2914 int mask;
2915 const CPULogItem *item;
2916
2917 mask = cpu_str_to_log_mask(optarg);
2918 if (!mask) {
2919 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002920 for(item = cpu_log_items; item->mask != 0; item++) {
2921 printf("%-10s %s\n", item->name, item->help);
2922 }
2923 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002924 }
2925 cpu_set_log(mask);
2926 }
2927 break;
2928 case QEMU_OPTION_s:
2929 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2930 break;
2931 case QEMU_OPTION_gdb:
2932 gdbstub_dev = optarg;
2933 break;
2934 case QEMU_OPTION_L:
2935 data_dir = optarg;
2936 break;
2937 case QEMU_OPTION_bios:
2938 bios_name = optarg;
2939 break;
2940 case QEMU_OPTION_singlestep:
2941 singlestep = 1;
2942 break;
2943 case QEMU_OPTION_S:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002944 autostart = 0;
2945 break;
2946#ifndef _WIN32
2947 case QEMU_OPTION_k:
2948 keyboard_layout = optarg;
2949 break;
2950#endif
2951 case QEMU_OPTION_localtime:
2952 rtc_utc = 0;
2953 break;
2954 case QEMU_OPTION_vga:
2955 select_vgahw (optarg);
2956 break;
2957#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2958 case QEMU_OPTION_g:
2959 {
2960 const char *p;
2961 int w, h, depth;
2962 p = optarg;
2963 w = strtol(p, (char **)&p, 10);
2964 if (w <= 0) {
2965 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07002966 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002967 }
2968 if (*p != 'x')
2969 goto graphic_error;
2970 p++;
2971 h = strtol(p, (char **)&p, 10);
2972 if (h <= 0)
2973 goto graphic_error;
2974 if (*p == 'x') {
2975 p++;
2976 depth = strtol(p, (char **)&p, 10);
2977 if (depth != 8 && depth != 15 && depth != 16 &&
2978 depth != 24 && depth != 32)
2979 goto graphic_error;
2980 } else if (*p == '\0') {
2981 depth = graphic_depth;
2982 } else {
2983 goto graphic_error;
2984 }
2985
2986 graphic_width = w;
2987 graphic_height = h;
2988 graphic_depth = depth;
2989 }
2990 break;
2991#endif
2992 case QEMU_OPTION_echr:
2993 {
2994 char *r;
2995 term_escape_char = strtol(optarg, &r, 0);
2996 if (r == optarg)
2997 printf("Bad argument to echr\n");
2998 break;
2999 }
3000 case QEMU_OPTION_monitor:
3001 monitor_device = optarg;
3002 break;
3003 case QEMU_OPTION_serial:
3004 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003005 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003006 }
3007 serial_devices[serial_device_index] = optarg;
3008 serial_device_index++;
3009 break;
3010 case QEMU_OPTION_watchdog:
3011 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003012 if (i > 0) {
3013 if (i == 1) {
3014 PANIC("Invalid watchdog parameter: %s",
3015 optarg);
3016 } else {
3017 QEMU_EXIT(0);
3018 }
3019 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003020 break;
3021 case QEMU_OPTION_watchdog_action:
3022 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003023 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003024 }
3025 break;
3026 case QEMU_OPTION_virtiocon:
3027 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003028 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003029 }
3030 virtio_consoles[virtio_console_index] = optarg;
3031 virtio_console_index++;
3032 break;
3033 case QEMU_OPTION_parallel:
3034 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003035 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003036 }
3037 parallel_devices[parallel_device_index] = optarg;
3038 parallel_device_index++;
3039 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003040 case QEMU_OPTION_loadvm:
3041 loadvm = optarg;
3042 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003043 case QEMU_OPTION_savevm_on_exit:
3044 savevm_on_exit = optarg;
3045 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003046 case QEMU_OPTION_full_screen:
3047 full_screen = 1;
3048 break;
3049#ifdef CONFIG_SDL
3050 case QEMU_OPTION_no_frame:
3051 no_frame = 1;
3052 break;
3053 case QEMU_OPTION_alt_grab:
3054 alt_grab = 1;
3055 break;
3056 case QEMU_OPTION_no_quit:
3057 no_quit = 1;
3058 break;
3059 case QEMU_OPTION_sdl:
3060 display_type = DT_SDL;
3061 break;
3062#endif
3063 case QEMU_OPTION_pidfile:
3064 pid_file = optarg;
3065 break;
3066#ifdef TARGET_I386
3067 case QEMU_OPTION_win2k_hack:
3068 win2k_install_hack = 1;
3069 break;
3070 case QEMU_OPTION_rtc_td_hack:
3071 rtc_td_hack = 1;
3072 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003073#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003074 case QEMU_OPTION_acpitable:
3075 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003076 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003077 }
3078 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08003079#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003080 case QEMU_OPTION_smbios:
David 'Digit' Turnerae3098a2011-05-11 16:01:57 +02003081 do_smbios_option(optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003082 break;
3083#endif
3084#ifdef CONFIG_KVM
3085 case QEMU_OPTION_enable_kvm:
3086 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003087 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02003088 case QEMU_OPTION_disable_kvm:
3089 kvm_allowed = 0;
3090 break;
3091#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003092 case QEMU_OPTION_usb:
3093 usb_enabled = 1;
3094 break;
3095 case QEMU_OPTION_usbdevice:
3096 usb_enabled = 1;
3097 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003098 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003099 }
3100 usb_devices[usb_devices_index] = optarg;
3101 usb_devices_index++;
3102 break;
3103 case QEMU_OPTION_smp:
3104 smp_cpus = atoi(optarg);
3105 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003106 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003107 }
3108 break;
3109 case QEMU_OPTION_vnc:
3110 display_type = DT_VNC;
3111 vnc_display = optarg;
3112 break;
3113#ifdef TARGET_I386
3114 case QEMU_OPTION_no_acpi:
3115 acpi_enabled = 0;
3116 break;
3117 case QEMU_OPTION_no_hpet:
3118 no_hpet = 1;
3119 break;
3120 case QEMU_OPTION_no_virtio_balloon:
3121 no_virtio_balloon = 1;
3122 break;
3123#endif
3124 case QEMU_OPTION_no_reboot:
3125 no_reboot = 1;
3126 break;
3127 case QEMU_OPTION_no_shutdown:
3128 no_shutdown = 1;
3129 break;
3130 case QEMU_OPTION_show_cursor:
3131 cursor_hide = 0;
3132 break;
3133 case QEMU_OPTION_uuid:
3134 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003135 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003136 }
3137 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003138 case QEMU_OPTION_option_rom:
3139 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003140 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003141 }
3142 option_rom[nb_option_roms] = optarg;
3143 nb_option_roms++;
3144 break;
3145#if defined(TARGET_ARM) || defined(TARGET_M68K)
3146 case QEMU_OPTION_semihosting:
3147 semihosting_enabled = 1;
3148 break;
3149#endif
3150 case QEMU_OPTION_name:
3151 qemu_name = optarg;
3152 break;
3153#if defined(TARGET_SPARC) || defined(TARGET_PPC)
3154 case QEMU_OPTION_prom_env:
3155 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003156 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003157 }
3158 prom_envs[nb_prom_envs] = optarg;
3159 nb_prom_envs++;
3160 break;
3161#endif
3162#ifdef TARGET_ARM
3163 case QEMU_OPTION_old_param:
3164 old_param = 1;
3165 break;
3166#endif
3167 case QEMU_OPTION_clock:
3168 configure_alarms(optarg);
3169 break;
3170 case QEMU_OPTION_startdate:
3171 {
3172 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02003173 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003174 if (!strcmp(optarg, "now")) {
3175 rtc_date_offset = -1;
3176 } else {
3177 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
3178 &tm.tm_year,
3179 &tm.tm_mon,
3180 &tm.tm_mday,
3181 &tm.tm_hour,
3182 &tm.tm_min,
3183 &tm.tm_sec) == 6) {
3184 /* OK */
3185 } else if (sscanf(optarg, "%d-%d-%d",
3186 &tm.tm_year,
3187 &tm.tm_mon,
3188 &tm.tm_mday) == 3) {
3189 tm.tm_hour = 0;
3190 tm.tm_min = 0;
3191 tm.tm_sec = 0;
3192 } else {
3193 goto date_fail;
3194 }
3195 tm.tm_year -= 1900;
3196 tm.tm_mon--;
3197 rtc_start_date = mktimegm(&tm);
3198 if (rtc_start_date == -1) {
3199 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003200 PANIC("Invalid date format. Valid format are:\n"
3201 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003202 }
3203 rtc_date_offset = time(NULL) - rtc_start_date;
3204 }
3205 }
3206 break;
rich cannings7339b552011-02-16 13:43:44 -08003207
3208 /* -------------------------------------------------------*/
3209 /* User mode network stack restrictions */
3210 case QEMU_OPTION_drop_udp:
3211 slirp_drop_udp();
3212 break;
3213 case QEMU_OPTION_drop_tcp:
3214 slirp_drop_tcp();
3215 break;
3216 case QEMU_OPTION_allow_tcp:
3217 slirp_allow(optarg, IPPROTO_TCP);
3218 break;
3219 case QEMU_OPTION_allow_udp:
3220 slirp_allow(optarg, IPPROTO_UDP);
3221 break;
3222 case QEMU_OPTION_drop_log:
3223 {
3224 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003225 drop_log_filename = optarg;
3226 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08003227
3228 if (!drop_log_fd) {
3229 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
3230 exit(1);
3231 }
3232
3233 slirp_drop_log_fd(drop_log_fd);
3234 }
3235 break;
3236
3237 case QEMU_OPTION_dns_log:
3238 {
3239 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08003240 dns_log_filename = optarg;
3241 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08003242
3243 if (dns_log_fd == NULL) {
3244 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
3245 exit(1);
3246 }
3247
3248 slirp_dns_log_fd(dns_log_fd);
3249 }
3250 break;
3251
3252
3253 case QEMU_OPTION_max_dns_conns:
3254 {
3255 int max_dns_conns = 0;
3256 if (parse_int(optarg, &max_dns_conns)) {
3257 fprintf(stderr,
3258 "qemu: syntax: -max-dns-conns max_connections\n");
3259 exit(1);
3260 }
3261 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
3262 fprintf(stderr,
3263 "Invalid arg for max dns connections: %s\n",
3264 optarg);
3265 exit(1);
3266 }
3267 slirp_set_max_dns_conns(max_dns_conns);
3268 }
3269 break;
3270
3271 case QEMU_OPTION_net_forward:
3272 net_slirp_forward(optarg);
3273 break;
3274 case QEMU_OPTION_net_forward_tcp2sink:
3275 {
3276 SockAddress saddr;
3277
3278 if (parse_host_port(&saddr, optarg)) {
3279 fprintf(stderr,
3280 "Invalid ip/port %s for "
3281 "-forward-dropped-tcp2sink. "
3282 "We expect 'sink_ip:sink_port'\n",
3283 optarg);
3284 exit(1);
3285 }
3286 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
3287 saddr.u.inet.port);
3288 }
3289 break;
3290 /* -------------------------------------------------------*/
3291
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003292 case QEMU_OPTION_tb_size:
3293 tb_size = strtol(optarg, NULL, 0);
3294 if (tb_size < 0)
3295 tb_size = 0;
3296 break;
3297 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02003298 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003299 break;
3300 case QEMU_OPTION_incoming:
3301 incoming = optarg;
3302 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003303#ifdef CONFIG_XEN
3304 case QEMU_OPTION_xen_domid:
3305 xen_domid = atoi(optarg);
3306 break;
3307 case QEMU_OPTION_xen_create:
3308 xen_mode = XEN_CREATE;
3309 break;
3310 case QEMU_OPTION_xen_attach:
3311 xen_mode = XEN_ATTACH;
3312 break;
3313#endif
3314
3315
3316 case QEMU_OPTION_mic:
3317 audio_input_source = (char*)optarg;
3318 break;
3319#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003320 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003321 trace_filename = optarg;
3322 tracing = 1;
3323 break;
3324#if 0
3325 case QEMU_OPTION_trace_miss:
3326 trace_cache_miss = 1;
3327 break;
3328 case QEMU_OPTION_trace_addr:
3329 trace_all_addr = 1;
3330 break;
3331#endif
3332 case QEMU_OPTION_tracing:
3333 if (strcmp(optarg, "off") == 0)
3334 tracing = 0;
3335 else if (strcmp(optarg, "on") == 0 && trace_filename)
3336 tracing = 1;
3337 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003338 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003339 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003340 }
3341 break;
3342#if 0
3343 case QEMU_OPTION_dcache_load_miss:
3344 dcache_load_miss_penalty = atoi(optarg);
3345 break;
3346 case QEMU_OPTION_dcache_store_miss:
3347 dcache_store_miss_penalty = atoi(optarg);
3348 break;
3349#endif
3350#endif
3351#ifdef CONFIG_NAND
3352 case QEMU_OPTION_nand:
3353 nand_add_dev(optarg);
3354 break;
3355#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07003356 case QEMU_OPTION_android_ports:
3357 android_op_ports = (char*)optarg;
3358 break;
3359
3360 case QEMU_OPTION_android_port:
3361 android_op_port = (char*)optarg;
3362 break;
3363
3364 case QEMU_OPTION_android_report_console:
3365 android_op_report_console = (char*)optarg;
3366 break;
3367
3368 case QEMU_OPTION_http_proxy:
3369 op_http_proxy = (char*)optarg;
3370 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003371
3372 case QEMU_OPTION_charmap:
3373 op_charmap_file = (char*)optarg;
3374 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07003375
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003376 case QEMU_OPTION_android_hw:
3377 android_op_hwini = (char*)optarg;
3378 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003379
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003380 case QEMU_OPTION_dns_server:
3381 android_op_dns_server = (char*)optarg;
3382 break;
3383
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003384 case QEMU_OPTION_radio:
3385 android_op_radio = (char*)optarg;
3386 break;
3387
3388 case QEMU_OPTION_gps:
3389 android_op_gps = (char*)optarg;
3390 break;
3391
3392 case QEMU_OPTION_audio:
3393 android_op_audio = (char*)optarg;
3394 break;
3395
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003396 case QEMU_OPTION_cpu_delay:
3397 android_op_cpu_delay = (char*)optarg;
3398 break;
3399
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07003400 case QEMU_OPTION_show_kernel:
3401 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
3402 break;
3403
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003404#ifdef CONFIG_NAND_LIMITS
3405 case QEMU_OPTION_nand_limits:
3406 android_op_nand_limits = (char*)optarg;
3407 break;
3408#endif // CONFIG_NAND_LIMITS
3409
3410 case QEMU_OPTION_netspeed:
3411 android_op_netspeed = (char*)optarg;
3412 break;
3413
3414 case QEMU_OPTION_netdelay:
3415 android_op_netdelay = (char*)optarg;
3416 break;
3417
3418 case QEMU_OPTION_netfast:
3419 android_op_netfast = 1;
3420 break;
3421
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003422 case QEMU_OPTION_tcpdump:
3423 android_op_tcpdump = (char*)optarg;
3424 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003425
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003426 case QEMU_OPTION_boot_property:
3427 boot_property_parse_option((char*)optarg);
3428 break;
3429
3430 case QEMU_OPTION_lcd_density:
3431 android_op_lcd_density = (char*)optarg;
3432 break;
3433
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003434 case QEMU_OPTION_ui_port:
3435 android_op_ui_port = (char*)optarg;
3436 break;
3437
3438 case QEMU_OPTION_ui_settings:
3439 android_op_ui_settings = (char*)optarg;
3440 break;
3441
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01003442 case QEMU_OPTION_audio_test_out:
3443 android_audio_test_start_out();
3444 break;
3445
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08003446 case QEMU_OPTION_android_avdname:
3447 android_op_avd_name = (char*)optarg;
3448 break;
3449
3450 case QEMU_OPTION_timezone:
3451 if (timezone_set((char*)optarg)) {
3452 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
3453 (char*)optarg);
3454 }
3455 break;
3456
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003457#ifdef CONFIG_MEMCHECK
3458 case QEMU_OPTION_android_memcheck:
3459 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003460 /* This will set ro.kernel.memcheck system property
3461 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003462 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003463 break;
3464#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01003465
3466 case QEMU_OPTION_snapshot_no_time_update:
3467 android_snapshot_update_time = 0;
3468 break;
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003469 default:
3470 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003471 }
3472 }
3473 }
3474
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003475 /* Initialize character map. */
3476 if (android_charmap_setup(op_charmap_file)) {
3477 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003478 PANIC(
3479 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003480 op_charmap_file);
3481 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003482 PANIC(
3483 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003484 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07003485 }
3486
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003487 /* If no data_dir is specified then try to find it relative to the
3488 executable path. */
3489 if (!data_dir) {
3490 data_dir = find_datadir(argv[0]);
3491 }
3492 /* If all else fails use the install patch specified when building. */
3493 if (!data_dir) {
3494 data_dir = CONFIG_QEMU_SHAREDIR;
3495 }
3496
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003497 if (!android_op_hwini) {
3498 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003499 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003500 hw_ini = iniFile_newFromFile(android_op_hwini);
3501 if (hw_ini == NULL) {
3502 PANIC("Could not find %s file.", android_op_hwini);
3503 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003504
3505 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003506 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01003507
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003508 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01003509
3510 {
3511 int width = android_hw->hw_lcd_width;
3512 int height = android_hw->hw_lcd_height;
3513 int depth = android_hw->hw_lcd_depth;
3514
3515 /* A bit of sanity checking */
3516 if (width <= 0 || height <= 0 ||
3517 (depth != 16 && depth != 32) ||
3518 (((width|height) & 3) != 0) )
3519 {
3520 PANIC("Invalid display configuration (%d,%d,%d)",
3521 width, height, depth);
3522 }
3523 android_display_width = width;
3524 android_display_height = height;
3525 android_display_bpp = depth;
3526 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003527
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003528#ifdef CONFIG_NAND_LIMITS
3529 /* Init nand stuff. */
3530 if (android_op_nand_limits) {
3531 parse_nand_limits(android_op_nand_limits);
3532 }
3533#endif // CONFIG_NAND_LIMITS
3534
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01003535 /* Initialize AVD name from hardware configuration if needed */
3536 if (!android_op_avd_name) {
3537 if (android_hw->avd_name && *android_hw->avd_name) {
3538 android_op_avd_name = android_hw->avd_name;
3539 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
3540 }
3541 }
3542
David 'Digit' Turner40841b22011-03-01 14:04:00 +01003543 /* Initialize system partition image */
3544 {
3545 char tmp[PATH_MAX+32];
3546 const char* sysImage = android_hw->disk_systemPartition_path;
3547 const char* initImage = android_hw->disk_systemPartition_initPath;
3548 uint64_t sysBytes = android_hw->disk_systemPartition_size;
3549
3550 if (sysBytes == 0) {
3551 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
3552 }
3553
3554 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
3555
3556 if (sysImage && *sysImage) {
3557 if (filelock_create(sysImage) == NULL) {
3558 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
3559 /* If there is no file= parameters, nand_add_dev will create
3560 * a temporary file to back the partition image. */
3561 } else {
3562 pstrcat(tmp,sizeof(tmp),",file=");
3563 pstrcat(tmp,sizeof(tmp),sysImage);
3564 }
3565 }
3566 if (initImage && *initImage) {
3567 if (!path_exists(initImage)) {
3568 PANIC("Invalid initial system image path: %s", initImage);
3569 }
3570 pstrcat(tmp,sizeof(tmp),",initfile=");
3571 pstrcat(tmp,sizeof(tmp),initImage);
3572 } else {
3573 PANIC("Missing initial system image path!");
3574 }
3575 nand_add_dev(tmp);
3576 }
3577
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003578 /* Initialize data partition image */
3579 {
3580 char tmp[PATH_MAX+32];
3581 const char* dataImage = android_hw->disk_dataPartition_path;
3582 const char* initImage = android_hw->disk_dataPartition_initPath;
3583 uint64_t dataBytes = android_hw->disk_dataPartition_size;
3584
3585 if (dataBytes == 0) {
3586 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
3587 }
3588
3589 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
3590
3591 if (dataImage && *dataImage) {
3592 if (filelock_create(dataImage) == NULL) {
3593 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
3594 /* Note: if there is no file= parameters, nand_add_dev() will
3595 * create a temporary file to back the partition image. */
3596 } else {
3597 /* Create the file if needed */
3598 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003599 if (path_empty_file(dataImage) < 0) {
3600 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
3601 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01003602 }
3603 pstrcat(tmp, sizeof(tmp), ",file=");
3604 pstrcat(tmp, sizeof(tmp), dataImage);
3605 }
3606 }
3607 if (initImage && *initImage) {
3608 pstrcat(tmp, sizeof(tmp), ",initfile=");
3609 pstrcat(tmp, sizeof(tmp), initImage);
3610 }
3611 nand_add_dev(tmp);
3612 }
3613
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003614 /* Init SD-Card stuff. For Android, it is always hda */
3615 /* If the -hda option was used, ignore the Android-provided one */
3616 if (hda_opts == NULL) {
3617 const char* sdPath = android_hw->hw_sdCard_path;
3618 if (sdPath && *sdPath) {
3619 if (!path_exists(sdPath)) {
3620 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
3621 } else if (filelock_create(sdPath) == NULL) {
3622 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
3623 } else {
3624 /* Successful locking */
3625 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003626 /* Set this property of any operation involving the SD Card
3627 * will be x100 slower, due to the corresponding file being
3628 * mounted as O_DIRECT. Note that this is only 'unsafe' in
3629 * the context of an emulator crash. The data is already
3630 * synced properly when the emulator exits (either normally or through ^C).
3631 */
3632 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01003633 }
3634 }
3635 }
3636
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003637 if (hdb_opts == NULL) {
3638 const char* spath = android_hw->disk_snapStorage_path;
3639 if (spath && *spath) {
3640 if (!path_exists(spath)) {
3641 PANIC("Snapshot storage file does not exist: %s", spath);
3642 }
3643 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02003644 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003645 }
3646 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02003647 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02003648 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003649 }
3650 }
3651
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003652 /* Set the VM's max heap size, passed as a boot property */
3653 if (android_hw->vm_heapSize > 0) {
3654 char tmp[64];
3655 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
3656 boot_property_add("dalvik.vm.heapsize",tmp);
3657 }
3658
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003659 /* Initialize net speed and delays stuff. */
3660 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003661 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003662 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003663 }
3664
3665 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003666 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003667 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07003668 }
3669
3670 if (android_op_netfast) {
3671 qemu_net_download_speed = 0;
3672 qemu_net_upload_speed = 0;
3673 qemu_net_min_latency = 0;
3674 qemu_net_max_latency = 0;
3675 }
3676
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003677 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003678 if (android_hw->hw_lcd_density) {
3679 long density = android_hw->hw_lcd_density;
3680 if (density <= 0) {
3681 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003682 }
3683 hwLcd_setBootProperty(density);
3684 }
3685
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07003686 /* Initialize TCP dump */
3687 if (android_op_tcpdump) {
3688 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
3689 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
3690 }
3691 }
3692
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003693 /* Initialize modem */
3694 if (android_op_radio) {
3695 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
3696 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003697 PANIC("unsupported character device specification: %s\n"
3698 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003699 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003700 }
3701 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
3702 } else if (android_hw->hw_gsmModem != 0 ) {
3703 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003704 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003705 }
3706 }
3707
3708 /* Initialize GPS */
3709 if (android_op_gps) {
3710 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
3711 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003712 PANIC("unsupported character device specification: %s\n"
3713 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003714 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003715 }
3716 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
3717 } else if (android_hw->hw_gps != 0) {
3718 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003719 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003720 }
3721 }
3722
3723 /* Initialize audio. */
3724 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003725 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003726 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003727 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003728 }
David 'Digit' Turnerf816a752011-06-23 18:40:11 +02003729 setenv("QEMU_AUDIO_DRV", android_op_audio, 1);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003730 }
3731
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003732 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003733 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02003734
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003735 if (android_op_cpu_delay) {
3736 char* end;
3737 long delay = strtol(android_op_cpu_delay, &end, 0);
3738 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003739 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07003740 }
3741 if (delay > 0)
3742 delay = (1000-delay);
3743
3744 qemu_cpu_delay = (int) delay;
3745 }
3746
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003747 if (android_op_dns_server) {
3748 char* x = strchr(android_op_dns_server, ',');
3749 dns_count = 0;
3750 if (x == NULL)
3751 {
3752 if ( add_dns_server( android_op_dns_server ) == 0 )
3753 dns_count = 1;
3754 }
3755 else
3756 {
3757 x = android_op_dns_server;
3758 while (*x) {
3759 char* y = strchr(x, ',');
3760
3761 if (y != NULL) {
3762 *y = 0;
3763 y++;
3764 } else {
3765 y = x + strlen(x);
3766 }
3767
3768 if (y > x && add_dns_server( x ) == 0) {
3769 dns_count += 1;
3770 }
3771 x = y;
3772 }
3773 }
3774 if (dns_count == 0)
3775 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
3776 }
3777
3778 if (dns_count == 0)
3779 dns_count = slirp_get_system_dns_servers();
3780 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003781 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003782 }
3783
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07003784#ifdef CONFIG_MEMCHECK
3785 if (android_op_memcheck) {
3786 memcheck_init(android_op_memcheck);
3787 }
3788#endif // CONFIG_MEMCHECK
3789
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003790 /* Initialize cache partition, if any */
3791 if (android_hw->disk_cachePartition != 0) {
3792 char tmp[PATH_MAX+32];
3793 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003794 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003795
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01003796 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
3797
3798 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
3799 if (filelock_create(partPath) == NULL) {
3800 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
3801 /* Note: if there is no file= parameters, nand_add_dev() will
3802 * create a temporary file to back the partition image. */
3803 } else {
3804 /* Create the file if needed */
3805 if (!path_exists(partPath)) {
3806 if (path_empty_file(partPath) < 0) {
3807 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
3808 }
3809 }
3810 pstrcat(tmp, sizeof(tmp), ",file=");
3811 pstrcat(tmp, sizeof(tmp), partPath);
3812 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01003813 }
3814 nand_add_dev(tmp);
3815 }
3816
David 'Digit' Turnercb88e792011-08-26 01:35:14 +02003817 /* qemu.gles will be read by the OpenGLES emulation libraries.
3818 * If set to 0, the software GLES renderer will be used as a fallback.
3819 * If the parameter is undefined, this means the system image runs
3820 * inside an emulator that doesn't support GPU emulation at all.
3821 */
3822 {
3823 int gles_emul = 0;
3824
3825 if (android_hw->hw_gpu_enabled) {
3826 if (android_initOpenglesEmulation() == 0) {
3827 gles_emul = 1;
3828 android_startOpenglesRenderer(android_hw->hw_lcd_width, android_hw->hw_lcd_height);
3829 } else {
3830 dwarning("Could not initialize OpenglES emulation, using software renderer.");
3831 }
3832 }
3833 if (gles_emul) {
3834 stralloc_add_str(kernel_params, " qemu.gles=1");
3835 } else {
3836 stralloc_add_str(kernel_params, " qemu.gles=0");
3837 }
3838 }
3839
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003840 /* We always force qemu=1 when running inside QEMU */
3841 stralloc_add_str(kernel_params, " qemu=1");
3842
3843 /* We always initialize the first serial port for the android-kmsg
3844 * character device (used to send kernel messages) */
3845 serial_hds_add_at(0, "android-kmsg");
3846 stralloc_add_str(kernel_params, " console=ttyS0");
3847
3848 /* We always initialize the second serial port for the android-qemud
3849 * character device as well */
3850 serial_hds_add_at(1, "android-qemud");
3851 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
3852
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003853 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3854 os_pidfile_error();
3855 exit(1);
3856 }
3857
David 'Digit' Turner36597752011-05-20 01:18:01 +02003858#if defined(CONFIG_KVM)
3859 if (kvm_allowed < 0) {
3860 kvm_allowed = kvm_check_allowed();
3861 }
3862#endif
3863
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003864#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
3865 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003866 PANIC(
3867 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003868 }
3869#endif
3870
3871 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3872 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003873 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
3874 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003875 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003876 }
3877
3878 if (display_type == DT_NOGRAPHIC) {
3879 if (serial_device_index == 0)
3880 serial_devices[0] = "stdio";
3881 if (parallel_device_index == 0)
3882 parallel_devices[0] = "null";
3883 if (strncmp(monitor_device, "vc", 2) == 0)
3884 monitor_device = "stdio";
3885 }
3886
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003887#ifdef CONFIG_KQEMU
3888 if (smp_cpus > 1)
3889 kqemu_allowed = 0;
3890#endif
3891 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003892 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003893 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01003894
3895 if (kernel_filename == NULL) {
3896 kernel_filename = android_hw->kernel_path;
3897 }
3898 if (initrd_filename == NULL) {
3899 initrd_filename = android_hw->disk_ramdisk_path;
3900 }
3901
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003902 linux_boot = (kernel_filename != NULL);
3903 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
3904
3905 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003906 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003907 }
3908
3909 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003910 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003911 }
3912
3913 /* boot to floppy or the default cd if no hard disk defined yet */
3914 if (!boot_devices[0]) {
3915 boot_devices = "cad";
3916 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003917 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003918
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003919 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003920 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003921 }
David Turner6a9ef172010-09-09 22:54:36 +02003922 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003923
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003924 /* init network clients */
3925 if (nb_net_clients == 0) {
3926 /* if no clients, we use a default config */
3927 net_clients[nb_net_clients++] = "nic";
3928#ifdef CONFIG_SLIRP
3929 net_clients[nb_net_clients++] = "user";
3930#endif
3931 }
3932
3933 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003934 if (net_client_parse(net_clients[i]) < 0) {
3935 PANIC("Unable to parse net clients");
3936 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003937 }
3938 net_client_check();
3939
3940#ifdef TARGET_I386
3941 /* XXX: this should be moved in the PC machine instantiation code */
3942 if (net_boot != 0) {
3943 int netroms = 0;
3944 for (i = 0; i < nb_nics && i < 4; i++) {
3945 const char *model = nd_table[i].model;
3946 char buf[1024];
3947 char *filename;
3948 if (net_boot & (1 << i)) {
3949 if (model == NULL)
3950 model = "ne2k_pci";
3951 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
3952 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
3953 if (filename && get_image_size(filename) > 0) {
3954 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003955 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003956 }
3957 option_rom[nb_option_roms] = qemu_strdup(buf);
3958 nb_option_roms++;
3959 netroms++;
3960 }
3961 if (filename) {
3962 qemu_free(filename);
3963 }
3964 }
3965 }
3966 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003967 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003968 }
3969 }
3970#endif
3971
3972 /* init the bluetooth world */
3973 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003974 if (bt_parse(bt_opts[i])) {
3975 PANIC("Unable to parse bluetooth options");
3976 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003977
3978 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01003979 if (ram_size == 0) {
3980 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
3981 if (ram_size == 0) {
3982 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3983 }
3984 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003985
3986#ifdef CONFIG_KQEMU
3987 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
3988 guest ram allocation. It needs to go away. */
3989 if (kqemu_allowed) {
3990 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
3991 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
3992 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003993 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003994 }
3995 }
3996#endif
3997
rich canningsd952f282011-03-01 15:40:09 -08003998#ifndef _WIN32
3999 init_qemu_clear_logs_sig();
4000#endif
4001
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004002 /* init the dynamic translator */
4003 cpu_exec_init_all(tb_size * 1024 * 1024);
4004
4005 bdrv_init();
4006
4007 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004008#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004009 if (nb_drives_opt < MAX_DRIVES)
4010 drive_add(NULL, CDROM_ALIAS);
4011
4012 /* we always create at least one floppy */
4013
4014 if (nb_drives_opt < MAX_DRIVES)
4015 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004016 /* we always create one sd slot, even if no card is in it */
4017
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004018 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004019 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004020 }
4021#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004022
4023 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004024 if (snapshot)
4025 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4026 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
4027 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004028
David Turner6a9ef172010-09-09 22:54:36 +02004029 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004030 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
4031
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004032 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004033 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004034
4035 /* Maintain compatibility with multiple stdio monitors */
4036 if (!strcmp(monitor_device,"stdio")) {
4037 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4038 const char *devname = serial_devices[i];
4039 if (devname && !strcmp(devname,"mon:stdio")) {
4040 monitor_device = NULL;
4041 break;
4042 } else if (devname && !strcmp(devname,"stdio")) {
4043 monitor_device = NULL;
4044 serial_devices[i] = "mon:stdio";
4045 break;
4046 }
4047 }
4048 }
4049
4050 if (nb_numa_nodes > 0) {
4051 int i;
4052
4053 if (nb_numa_nodes > smp_cpus) {
4054 nb_numa_nodes = smp_cpus;
4055 }
4056
4057 /* If no memory size if given for any node, assume the default case
4058 * and distribute the available memory equally across all nodes
4059 */
4060 for (i = 0; i < nb_numa_nodes; i++) {
4061 if (node_mem[i] != 0)
4062 break;
4063 }
4064 if (i == nb_numa_nodes) {
4065 uint64_t usedmem = 0;
4066
4067 /* On Linux, the each node's border has to be 8MB aligned,
4068 * the final node gets the rest.
4069 */
4070 for (i = 0; i < nb_numa_nodes - 1; i++) {
4071 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4072 usedmem += node_mem[i];
4073 }
4074 node_mem[i] = ram_size - usedmem;
4075 }
4076
4077 for (i = 0; i < nb_numa_nodes; i++) {
4078 if (node_cpumask[i] != 0)
4079 break;
4080 }
4081 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4082 * must cope with this anyway, because there are BIOSes out there in
4083 * real machines which also use this scheme.
4084 */
4085 if (i == nb_numa_nodes) {
4086 for (i = 0; i < smp_cpus; i++) {
4087 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4088 }
4089 }
4090 }
4091
4092 if (kvm_enabled()) {
4093 int ret;
4094
4095 ret = kvm_init(smp_cpus);
4096 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004097 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004098 }
4099 }
4100
4101 if (monitor_device) {
4102 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
4103 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004104 PANIC("qemu: could not open monitor device '%s'",
4105 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004106 }
4107 }
4108
4109 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004110 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004111 }
4112
4113 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4114 const char *devname = parallel_devices[i];
4115 if (devname && strcmp(devname, "none")) {
4116 char label[32];
4117 snprintf(label, sizeof(label), "parallel%d", i);
4118 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
4119 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004120 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004121 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004122 }
4123 }
4124 }
4125
4126 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4127 const char *devname = virtio_consoles[i];
4128 if (devname && strcmp(devname, "none")) {
4129 char label[32];
4130 snprintf(label, sizeof(label), "virtcon%d", i);
4131 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
4132 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004133 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004134 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004135 }
4136 }
4137 }
4138
4139 module_call_init(MODULE_INIT_DEVICE);
4140
4141
4142#ifdef CONFIG_TRACE
4143 if (trace_filename) {
4144 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004145 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
4146 }
4147#endif
4148
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01004149 /* Check the CPU Architecture value */
4150#if defined(TARGET_ARM)
4151 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
4152 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
4153 android_hw->hw_cpu_arch);
4154 exit(1);
4155 }
4156#elif defined(TARGET_X86)
4157 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
4158 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
4159 android_hw->hw_cpu_arch);
4160 exit(1);
4161 }
4162#endif
4163
4164 /* Grab CPU model if provided in hardware.ini */
4165 if ( !cpu_model
4166 && android_hw->hw_cpu_model
4167 && android_hw->hw_cpu_model[0] != '\0')
4168 {
4169 cpu_model = android_hw->hw_cpu_model;
4170 }
4171
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004172 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004173 * collected during initialization.
4174 *
4175 * The order is the following:
4176 * - parameters from the hw configuration (kernel.parameters)
4177 * - additionnal parameters from options (e.g. -memcheck)
4178 * - the -append parameters.
4179 */
4180 {
4181 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004182
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004183 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01004184 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01004185 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
4186 }
4187
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004188 /* If not empty, kernel_config always contains a leading space */
4189 stralloc_append(kernel_params, kernel_config);
4190
4191 if (*kernel_cmdline) {
4192 stralloc_add_c(kernel_params, ' ');
4193 stralloc_add_str(kernel_params, kernel_cmdline);
4194 }
4195
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01004196 /* Remove any leading/trailing spaces */
4197 stralloc_strip(kernel_params);
4198
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004199 kernel_parameters = stralloc_cstr(kernel_params);
4200 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
4201
4202 machine->init(ram_size,
4203 boot_devices,
4204 kernel_filename,
4205 kernel_parameters,
4206 initrd_filename,
4207 cpu_model);
4208
4209 stralloc_reset(kernel_params);
4210 stralloc_reset(kernel_config);
4211 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004212
4213
4214 for (env = first_cpu; env != NULL; env = env->next_cpu) {
4215 for (i = 0; i < nb_numa_nodes; i++) {
4216 if (node_cpumask[i] & (1 << env->cpu_index)) {
4217 env->numa_node = i;
4218 }
4219 }
4220 }
4221
4222 current_machine = machine;
4223
4224 /* Set KVM's vcpu state to qemu's initial CPUState. */
4225 if (kvm_enabled()) {
4226 int ret;
4227
4228 ret = kvm_sync_vcpus();
4229 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004230 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004231 }
4232 }
4233
4234 /* init USB devices */
4235 if (usb_enabled) {
4236 for(i = 0; i < usb_devices_index; i++) {
4237 if (usb_device_add(usb_devices[i], 0) < 0) {
4238 fprintf(stderr, "Warning: could not add USB device %s\n",
4239 usb_devices[i]);
4240 }
4241 }
4242 }
4243
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004244 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004245 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08004246
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004247 /* Initialize display from the command line parameters. */
4248 android_display_reset(ds,
4249 android_display_width,
4250 android_display_height,
4251 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004252
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004253 if (display_type == DT_DEFAULT) {
4254#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4255 display_type = DT_SDL;
4256#else
4257 display_type = DT_VNC;
4258 vnc_display = "localhost:0,to=99";
4259 show_vnc_port = 1;
4260#endif
4261 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004262
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004263
4264 switch (display_type) {
4265 case DT_NOGRAPHIC:
4266 break;
4267#if defined(CONFIG_CURSES)
4268 case DT_CURSES:
4269 curses_display_init(ds, full_screen);
4270 break;
4271#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004272#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004273 case DT_SDL:
4274 sdl_display_init(ds, full_screen, no_frame);
4275 break;
4276#elif defined(CONFIG_COCOA)
4277 case DT_SDL:
4278 cocoa_display_init(ds, full_screen);
4279 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004280#elif defined(CONFIG_STANDALONE_CORE)
4281 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08004282 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08004283 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004284#endif
4285 case DT_VNC:
4286 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004287 if (vnc_display_open(ds, vnc_display) < 0) {
4288 PANIC("Unable to initialize VNC display");
4289 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004290
4291 if (show_vnc_port) {
4292 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4293 }
4294 break;
4295 default:
4296 break;
4297 }
4298 dpy_resize(ds);
4299
4300 dcl = ds->listeners;
4301 while (dcl != NULL) {
4302 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004303 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
4304 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004305 }
4306 dcl = dcl->next;
4307 }
4308
4309 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02004310 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
4311 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004312 }
4313
David 'Digit' Turner94702b02011-01-20 02:46:33 +01004314 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004315 qemu_chr_initial_reset();
4316
4317 if (monitor_device && monitor_hd)
4318 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
4319
4320 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
4321 const char *devname = serial_devices[i];
4322 if (devname && strcmp(devname, "none")) {
4323 if (strstart(devname, "vc", 0))
4324 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
4325 }
4326 }
4327
4328 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
4329 const char *devname = parallel_devices[i];
4330 if (devname && strcmp(devname, "none")) {
4331 if (strstart(devname, "vc", 0))
4332 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
4333 }
4334 }
4335
4336 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
4337 const char *devname = virtio_consoles[i];
4338 if (virtcon_hds[i] && devname) {
4339 if (strstart(devname, "vc", 0))
4340 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
4341 }
4342 }
4343
4344 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004345 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004346 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004347 }
4348
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004349 /* call android-specific setup function */
4350 android_emulation_setup();
4351
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08004352#if !defined(CONFIG_STANDALONE_CORE)
4353 // For the standalone emulator (UI+core in one executable) we need to
4354 // set the window title here.
4355 android_emulator_set_base_port(android_base_port);
4356#endif
4357
Ot ten Thije871da2a2010-09-20 10:29:22 +01004358 if (loadvm)
4359 do_loadvm(cur_mon, loadvm);
4360
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004361 if (incoming) {
4362 autostart = 0; /* fixme how to deal with -daemonize */
4363 qemu_start_incoming_migration(incoming);
4364 }
4365
4366 if (autostart)
4367 vm_start();
4368
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004369 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004370
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004371#ifdef CONFIG_ANDROID
4372 // This will notify the UI that the core is successfuly initialized
4373 android_core_init_completed();
4374#endif // CONFIG_ANDROID
4375
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004376 main_loop();
4377 quit_timers();
4378 net_cleanup();
4379 android_emulation_teardown();
4380 return 0;
4381}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07004382
4383void
4384android_emulation_teardown(void)
4385{
4386 android_charmap_done();
4387}