blob: 18fdb4037e368bd47a4d1285e1a37e58f59096fa [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* the following is needed on Linux to define ptsname() in stdlib.h */
26#if defined(__linux__)
27#define _GNU_SOURCE 1
28#endif
29
30#include "qemu-common.h"
31#include "hw/hw.h"
32#include "hw/boards.h"
33#include "hw/usb.h"
34#include "hw/pcmcia.h"
35#include "hw/pc.h"
36#include "hw/audiodev.h"
37#include "hw/isa.h"
38#include "hw/baum.h"
39#include "hw/goldfish_nand.h"
40#include "net.h"
41#include "console.h"
42#include "sysemu.h"
43#include "gdbstub.h"
44#include "qemu-timer.h"
45#include "qemu-char.h"
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +010046#include "blockdev.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070047#include "audio/audio.h"
48
49#include "qemu_file.h"
50#include "android/android.h"
51#include "charpipe.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "modem_driver.h"
53#include "android/gps.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070054#include "android/hw-kmsg.h"
David 'Digit' Turnerc0ac7332011-05-02 15:05:35 +020055#include "android/hw-pipe-net.h"
56#include "android/hw-qemud.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070057#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070058#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070059#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010060#include "android/utils/debug.h"
David 'Digit' Turner48a3c662011-03-01 14:03:20 +010061#include "android/utils/filelock.h"
62#include "android/utils/path.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010063#include "android/utils/stralloc.h"
David 'Digit' Turner40841b22011-03-01 14:04:00 +010064#include "android/utils/tempfile.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080065#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080066#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010067#include "android/snapshot.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070068#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070069#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070070
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070071#ifdef CONFIG_MEMCHECK
72#include "memcheck/memcheck.h"
73#endif // CONFIG_MEMCHECK
74
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070075#include <unistd.h>
76#include <fcntl.h>
77#include <signal.h>
78#include <time.h>
79#include <errno.h>
80#include <sys/time.h>
81#include <zlib.h>
82
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070083/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070084#include "config-host.h"
85
86#ifndef _WIN32
87#include <libgen.h>
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070088#include <sys/times.h>
89#include <sys/wait.h>
90#include <termios.h>
91#include <sys/mman.h>
92#include <sys/ioctl.h>
93#include <sys/resource.h>
94#include <sys/socket.h>
95#include <netinet/in.h>
96#include <net/if.h>
97#if defined(__NetBSD__)
98#include <net/if_tap.h>
99#endif
100#ifdef __linux__
101#include <linux/if_tun.h>
102#endif
103#include <arpa/inet.h>
104#include <dirent.h>
105#include <netdb.h>
106#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700107#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700108#include <sys/stat.h>
109#if defined(__FreeBSD__) || defined(__DragonFly__)
110#include <libutil.h>
111#else
112#include <util.h>
113#endif
114#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
115#include <freebsd/stdlib.h>
116#else
117#ifdef __linux__
118#include <pty.h>
119#include <malloc.h>
120#include <linux/rtc.h>
121
122/* For the benefit of older linux systems which don't supply it,
123 we use a local copy of hpet.h. */
124/* #include <linux/hpet.h> */
125#include "hpet.h"
126
127#include <linux/ppdev.h>
128#include <linux/parport.h>
129#endif
130#ifdef __sun__
131#include <sys/stat.h>
132#include <sys/ethernet.h>
133#include <sys/sockio.h>
134#include <netinet/arp.h>
135#include <netinet/in.h>
136#include <netinet/in_systm.h>
137#include <netinet/ip.h>
138#include <netinet/ip_icmp.h> // must come after ip.h
139#include <netinet/udp.h>
140#include <netinet/tcp.h>
141#include <net/if.h>
142#include <syslog.h>
143#include <stropts.h>
144#endif
145#endif
146#endif
147
148#if defined(__OpenBSD__)
149#include <util.h>
150#endif
151
152#if defined(CONFIG_VDE)
153#include <libvdeplug.h>
154#endif
155
156#ifdef _WIN32
157#include <windows.h>
158#include <malloc.h>
159#include <sys/timeb.h>
160#include <mmsystem.h>
161#define getopt_long_only getopt_long
162#define memalign(align, size) malloc(size)
163#endif
164
165
166#ifdef CONFIG_COCOA
167#undef main
168#define main qemu_main
169#endif /* CONFIG_COCOA */
170
171#include "hw/hw.h"
172#include "hw/boards.h"
173#include "hw/usb.h"
174#include "hw/pcmcia.h"
175#include "hw/pc.h"
176#include "hw/audiodev.h"
177#include "hw/isa.h"
178#include "hw/baum.h"
179#include "hw/bt.h"
180#include "hw/watchdog.h"
181#include "hw/smbios.h"
182#include "hw/xen.h"
183#include "bt-host.h"
184#include "net.h"
185#include "monitor.h"
186#include "console.h"
187#include "sysemu.h"
188#include "gdbstub.h"
189#include "qemu-timer.h"
190#include "qemu-char.h"
191#include "cache-utils.h"
192#include "block.h"
193#include "dma.h"
194#include "audio/audio.h"
195#include "migration.h"
196#include "kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200197#ifdef CONFIG_KVM
198#include "kvm-android.h"
199#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700200#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700201#include "android/hw-lcd.h"
202#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200203#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700204#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100205#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700206
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700207#ifdef CONFIG_STANDALONE_CORE
208/* Verbose value used by the standalone emulator core (without UI) */
209unsigned long android_verbose;
210#endif // CONFIG_STANDALONE_CORE
211
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800212#if !defined(CONFIG_STANDALONE_CORE)
213/* in android/qemulator.c */
214extern void android_emulator_set_base_port(int port);
215#endif
216
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700217#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700218#undef main
219#define main qemu_main
220#endif
221
222#include "disas.h"
223
224#include "exec-all.h"
225
226#ifdef CONFIG_TRACE
227#include "trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700228#endif
229
230#include "qemu_socket.h"
231
232#if defined(CONFIG_SLIRP)
233#include "libslirp.h"
234#endif
235
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700236
237
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238#define DEFAULT_RAM_SIZE 128
239
240/* Max number of USB devices that can be specified on the commandline. */
241#define MAX_USB_CMDLINE 8
242
243/* Max number of bluetooth switches on the commandline. */
244#define MAX_BT_CMDLINE 10
245
246/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700247
248static const char *data_dir;
249const char *bios_name = NULL;
250static void *ioport_opaque[MAX_IOPORTS];
251static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
252static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100253#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700254/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
255 to store the VM snapshots */
256DriveInfo drives_table[MAX_DRIVES+1];
257int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100258#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260DisplayType display_type = DT_DEFAULT;
261const char* keyboard_layout = NULL;
262int64_t ticks_per_sec;
263ram_addr_t ram_size;
264int nb_nics;
265NICInfo nd_table[MAX_NICS];
266int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100267int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700268static int rtc_utc = 1;
269static int rtc_date_offset = -1; /* -1 means no change */
270int cirrus_vga_enabled = 1;
271int std_vga_enabled = 0;
272int vmsvga_enabled = 0;
273int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700274QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700275#ifdef TARGET_SPARC
276int graphic_width = 1024;
277int graphic_height = 768;
278int graphic_depth = 8;
279#else
280int graphic_width = 800;
281int graphic_height = 600;
282int graphic_depth = 15;
283#endif
284static int full_screen = 0;
285#ifdef CONFIG_SDL
286static int no_frame = 0;
287#endif
288int no_quit = 0;
289CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100290int serial_hds_count;
291
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700292CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
293CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
294#ifdef TARGET_I386
295int win2k_install_hack = 0;
296int rtc_td_hack = 0;
297#endif
298int usb_enabled = 0;
299int singlestep = 0;
300int smp_cpus = 1;
301const char *vnc_display;
302int acpi_enabled = 1;
303int no_hpet = 0;
304int no_virtio_balloon = 0;
305int fd_bootchk = 1;
306int no_reboot = 0;
307int no_shutdown = 0;
308int cursor_hide = 1;
309int graphic_rotate = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700310WatchdogTimerModel *watchdog = NULL;
311int watchdog_action = WDT_RESET;
312const char *option_rom[MAX_OPTION_ROMS];
313int nb_option_roms;
314int semihosting_enabled = 0;
315#ifdef TARGET_ARM
316int old_param = 0;
317#endif
318const char *qemu_name;
319int alt_grab = 0;
320#if defined(TARGET_SPARC) || defined(TARGET_PPC)
321unsigned int nb_prom_envs = 0;
322const char *prom_envs[MAX_PROM_ENVS];
323#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100324#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700325int nb_drives_opt;
326struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100327#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700328int nb_numa_nodes;
329uint64_t node_mem[MAX_NODES];
330uint64_t node_cpumask[MAX_NODES];
331
332static CPUState *cur_cpu;
333static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700334static QEMUTimer *nographic_timer;
335
336uint8_t qemu_uuid[16];
337
338
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700339int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700340extern char* audio_input_source;
341
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700342extern char* android_op_ports;
343extern char* android_op_port;
344extern char* android_op_report_console;
345extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700346// Path to the file containing specific key character map.
347char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700348
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700349/* Path to hardware initialization file passed with -android-hw option. */
350char* android_op_hwini = NULL;
351
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700352/* Memory checker options. */
353char* android_op_memcheck = NULL;
354
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700355/* -dns-server option value. */
356char* android_op_dns_server = NULL;
357
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700358/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700359char* android_op_radio = NULL;
360
361/* -gps option value. */
362char* android_op_gps = NULL;
363
364/* -audio option value. */
365char* android_op_audio = NULL;
366
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700367/* -cpu-delay option value. */
368char* android_op_cpu_delay = NULL;
369
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700370#ifdef CONFIG_NAND_LIMITS
371/* -nand-limits option value. */
372char* android_op_nand_limits = NULL;
373#endif // CONFIG_NAND_LIMITS
374
375/* -netspeed option value. */
376char* android_op_netspeed = NULL;
377
378/* -netdelay option value. */
379char* android_op_netdelay = NULL;
380
381/* -netfast option value. */
382int android_op_netfast = 0;
383
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700384/* -tcpdump option value. */
385char* android_op_tcpdump = NULL;
386
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700387/* -lcd-density option value. */
388char* android_op_lcd_density = NULL;
389
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700390/* -ui-port option value. This port will be used to report the core
391 * initialization completion.
392 */
393char* android_op_ui_port = NULL;
394
395/* -ui-settings option value. This value will be passed to the UI when new UI
396 * process is attaching to the core.
397 */
398char* android_op_ui_settings = NULL;
399
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800400/* -android-avdname option value. */
401char* android_op_avd_name = "unknown";
402
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700403extern int android_display_width;
404extern int android_display_height;
405extern int android_display_bpp;
406
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700407extern void dprint( const char* format, ... );
408
rich canningsd952f282011-03-01 15:40:09 -0800409const char* dns_log_filename = NULL;
410const char* drop_log_filename = NULL;
411static int rotate_logs_requested = 0;
412
Tim Baverstock24204cc2010-11-25 11:37:43 +0000413const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000414
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700415#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
416
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700417/* Reports the core initialization failure to the error stdout and to the UI
418 * socket before exiting the application.
419 * Parameters that are passed to this macro are used to format the error
420 * mesage using sprintf routine.
421 */
422#ifdef CONFIG_ANDROID
423#define PANIC(...) android_core_init_failure(__VA_ARGS__)
424#else
425#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
426 exit(1); \
427 } while (0)
428#endif // CONFIG_ANDROID
429
430/* Exits the core during initialization. */
431#ifdef CONFIG_ANDROID
432#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
433#else
434#define QEMU_EXIT(exit_code) exit(exit_code)
435#endif // CONFIG_ANDROID
436
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700437/***********************************************************/
438/* x86 ISA bus support */
439
440target_phys_addr_t isa_mem_base = 0;
441PicState2 *isa_pic;
442
443static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
444static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
445
446static uint32_t ioport_read(int index, uint32_t address)
447{
448 static IOPortReadFunc *default_func[3] = {
449 default_ioport_readb,
450 default_ioport_readw,
451 default_ioport_readl
452 };
453 IOPortReadFunc *func = ioport_read_table[index][address];
454 if (!func)
455 func = default_func[index];
456 return func(ioport_opaque[address], address);
457}
458
459static void ioport_write(int index, uint32_t address, uint32_t data)
460{
461 static IOPortWriteFunc *default_func[3] = {
462 default_ioport_writeb,
463 default_ioport_writew,
464 default_ioport_writel
465 };
466 IOPortWriteFunc *func = ioport_write_table[index][address];
467 if (!func)
468 func = default_func[index];
469 func(ioport_opaque[address], address, data);
470}
471
472static uint32_t default_ioport_readb(void *opaque, uint32_t address)
473{
474#ifdef DEBUG_UNUSED_IOPORT
475 fprintf(stderr, "unused inb: port=0x%04x\n", address);
476#endif
477 return 0xff;
478}
479
480static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
481{
482#ifdef DEBUG_UNUSED_IOPORT
483 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
484#endif
485}
486
487/* default is to make two byte accesses */
488static uint32_t default_ioport_readw(void *opaque, uint32_t address)
489{
490 uint32_t data;
491 data = ioport_read(0, address);
492 address = (address + 1) & (MAX_IOPORTS - 1);
493 data |= ioport_read(0, address) << 8;
494 return data;
495}
496
497static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
498{
499 ioport_write(0, address, data & 0xff);
500 address = (address + 1) & (MAX_IOPORTS - 1);
501 ioport_write(0, address, (data >> 8) & 0xff);
502}
503
504static uint32_t default_ioport_readl(void *opaque, uint32_t address)
505{
506#ifdef DEBUG_UNUSED_IOPORT
507 fprintf(stderr, "unused inl: port=0x%04x\n", address);
508#endif
509 return 0xffffffff;
510}
511
512static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
513{
514#ifdef DEBUG_UNUSED_IOPORT
515 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
516#endif
517}
518
rich canningsd952f282011-03-01 15:40:09 -0800519/*
520 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
521 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
522 * between the tasks that do not require restarting Qemu.
523 */
524void rotate_qemu_logs_handler(int signum) {
525 rotate_logs_requested = 1;
526}
527
528/*
529 * Resets the rotate_log_requested_flag. Normally called after qemu
530 * logs has been rotated.
531 */
532void reset_rotate_qemu_logs_request(void) {
533 rotate_logs_requested = 0;
534}
535
536/*
537 * Clears the passed qemu log when the rotate_logs_requested
538 * is set. We need to clear the logs between the tasks that do not
539 * require restarting Qemu.
540 */
541FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
542 FILE* new_log_fd = NULL;
543 if (old_log_fd) {
544 if (fclose(old_log_fd) == -1) {
545 fprintf(stderr, "Cannot close old_log fd\n");
546 exit(errno);
547 }
548 }
549
550 if (!filename) {
551 fprintf(stderr, "The log filename to be rotated is not provided");
552 exit(-1);
553 }
554
555 new_log_fd = fopen(filename , "wb+");
556 if (new_log_fd == NULL) {
557 fprintf(stderr, "Cannot open the log file: %s for write.\n",
558 filename);
559 exit(1);
560 }
561
562 return new_log_fd;
563}
564
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700565/***********************************************************/
566void hw_error(const char *fmt, ...)
567{
568 va_list ap;
569 CPUState *env;
570
571 va_start(ap, fmt);
572 fprintf(stderr, "qemu: hardware error: ");
573 vfprintf(stderr, fmt, ap);
574 fprintf(stderr, "\n");
575 for(env = first_cpu; env != NULL; env = env->next_cpu) {
576 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
577#ifdef TARGET_I386
578 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
579#else
580 cpu_dump_state(env, stderr, fprintf, 0);
581#endif
582 }
583 va_end(ap);
584 abort();
585}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200586
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700587/***************/
588/* ballooning */
589
590static QEMUBalloonEvent *qemu_balloon_event;
591void *qemu_balloon_event_opaque;
592
593void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
594{
595 qemu_balloon_event = func;
596 qemu_balloon_event_opaque = opaque;
597}
598
599void qemu_balloon(ram_addr_t target)
600{
601 if (qemu_balloon_event)
602 qemu_balloon_event(qemu_balloon_event_opaque, target);
603}
604
605ram_addr_t qemu_balloon_status(void)
606{
607 if (qemu_balloon_event)
608 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
609 return 0;
610}
611
612/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700613/* host time/date access */
614void qemu_get_timedate(struct tm *tm, int offset)
615{
616 time_t ti;
617 struct tm *ret;
618
619 time(&ti);
620 ti += offset;
621 if (rtc_date_offset == -1) {
622 if (rtc_utc)
623 ret = gmtime(&ti);
624 else
625 ret = localtime(&ti);
626 } else {
627 ti -= rtc_date_offset;
628 ret = gmtime(&ti);
629 }
630
631 memcpy(tm, ret, sizeof(struct tm));
632}
633
634int qemu_timedate_diff(struct tm *tm)
635{
636 time_t seconds;
637
638 if (rtc_date_offset == -1)
639 if (rtc_utc)
640 seconds = mktimegm(tm);
641 else
642 seconds = mktime(tm);
643 else
644 seconds = mktimegm(tm) + rtc_date_offset;
645
646 return seconds - time(NULL);
647}
648
649
650#ifdef CONFIG_TRACE
651static int tbflush_requested;
652static int exit_requested;
653
654void start_tracing()
655{
656 if (trace_filename == NULL)
657 return;
658 if (!tracing) {
659 fprintf(stderr,"-- start tracing --\n");
660 start_time = Now();
661 }
662 tracing = 1;
663 tbflush_requested = 1;
664 qemu_notify_event();
665}
666
667void stop_tracing()
668{
669 if (trace_filename == NULL)
670 return;
671 if (tracing) {
672 end_time = Now();
673 elapsed_usecs += end_time - start_time;
674 fprintf(stderr,"-- stop tracing --\n");
675 }
676 tracing = 0;
677 tbflush_requested = 1;
678 qemu_notify_event();
679}
680
681#ifndef _WIN32
682/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
683 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
684 */
685void sigusr_handler(int sig)
686{
687 if (sig == SIGUSR1)
688 start_tracing();
689 else
690 stop_tracing();
691}
692#endif
693
694/* This is the handler to catch control-C so that we can exit cleanly.
695 * This is needed when tracing to flush the buffers to disk.
696 */
697void sigint_handler(int sig)
698{
699 exit_requested = 1;
700 qemu_notify_event();
701}
702#endif /* CONFIG_TRACE */
703
704
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700705/***********************************************************/
706/* Bluetooth support */
707static int nb_hcis;
708static int cur_hci;
709static struct HCIInfo *hci_table[MAX_NICS];
710
711static struct bt_vlan_s {
712 struct bt_scatternet_s net;
713 int id;
714 struct bt_vlan_s *next;
715} *first_bt_vlan;
716
717/* find or alloc a new bluetooth "VLAN" */
718static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
719{
720 struct bt_vlan_s **pvlan, *vlan;
721 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
722 if (vlan->id == id)
723 return &vlan->net;
724 }
725 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
726 vlan->id = id;
727 pvlan = &first_bt_vlan;
728 while (*pvlan != NULL)
729 pvlan = &(*pvlan)->next;
730 *pvlan = vlan;
731 return &vlan->net;
732}
733
734static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
735{
736}
737
738static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
739{
740 return -ENOTSUP;
741}
742
743static struct HCIInfo null_hci = {
744 .cmd_send = null_hci_send,
745 .sco_send = null_hci_send,
746 .acl_send = null_hci_send,
747 .bdaddr_set = null_hci_addr_set,
748};
749
750struct HCIInfo *qemu_next_hci(void)
751{
752 if (cur_hci == nb_hcis)
753 return &null_hci;
754
755 return hci_table[cur_hci++];
756}
757
758static struct HCIInfo *hci_init(const char *str)
759{
760 char *endp;
761 struct bt_scatternet_s *vlan = 0;
762
763 if (!strcmp(str, "null"))
764 /* null */
765 return &null_hci;
766 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
767 /* host[:hciN] */
768 return bt_host_hci(str[4] ? str + 5 : "hci0");
769 else if (!strncmp(str, "hci", 3)) {
770 /* hci[,vlan=n] */
771 if (str[3]) {
772 if (!strncmp(str + 3, ",vlan=", 6)) {
773 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
774 if (*endp)
775 vlan = 0;
776 }
777 } else
778 vlan = qemu_find_bt_vlan(0);
779 if (vlan)
780 return bt_new_hci(vlan);
781 }
782
783 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
784
785 return 0;
786}
787
788static int bt_hci_parse(const char *str)
789{
790 struct HCIInfo *hci;
791 bdaddr_t bdaddr;
792
793 if (nb_hcis >= MAX_NICS) {
794 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
795 return -1;
796 }
797
798 hci = hci_init(str);
799 if (!hci)
800 return -1;
801
802 bdaddr.b[0] = 0x52;
803 bdaddr.b[1] = 0x54;
804 bdaddr.b[2] = 0x00;
805 bdaddr.b[3] = 0x12;
806 bdaddr.b[4] = 0x34;
807 bdaddr.b[5] = 0x56 + nb_hcis;
808 hci->bdaddr_set(hci, bdaddr.b);
809
810 hci_table[nb_hcis++] = hci;
811
812 return 0;
813}
814
815static void bt_vhci_add(int vlan_id)
816{
817 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
818
819 if (!vlan->slave)
820 fprintf(stderr, "qemu: warning: adding a VHCI to "
821 "an empty scatternet %i\n", vlan_id);
822
823 bt_vhci_init(bt_new_hci(vlan));
824}
825
826static struct bt_device_s *bt_device_add(const char *opt)
827{
828 struct bt_scatternet_s *vlan;
829 int vlan_id = 0;
830 char *endp = strstr(opt, ",vlan=");
831 int len = (endp ? endp - opt : strlen(opt)) + 1;
832 char devname[10];
833
834 pstrcpy(devname, MIN(sizeof(devname), len), opt);
835
836 if (endp) {
837 vlan_id = strtol(endp + 6, &endp, 0);
838 if (*endp) {
839 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
840 return 0;
841 }
842 }
843
844 vlan = qemu_find_bt_vlan(vlan_id);
845
846 if (!vlan->slave)
847 fprintf(stderr, "qemu: warning: adding a slave device to "
848 "an empty scatternet %i\n", vlan_id);
849
850 if (!strcmp(devname, "keyboard"))
851 return bt_keyboard_init(vlan);
852
853 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
854 return 0;
855}
856
857static int bt_parse(const char *opt)
858{
859 const char *endp, *p;
860 int vlan;
861
862 if (strstart(opt, "hci", &endp)) {
863 if (!*endp || *endp == ',') {
864 if (*endp)
865 if (!strstart(endp, ",vlan=", 0))
866 opt = endp + 1;
867
868 return bt_hci_parse(opt);
869 }
870 } else if (strstart(opt, "vhci", &endp)) {
871 if (!*endp || *endp == ',') {
872 if (*endp) {
873 if (strstart(endp, ",vlan=", &p)) {
874 vlan = strtol(p, (char **) &endp, 0);
875 if (*endp) {
876 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
877 return 1;
878 }
879 } else {
880 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
881 return 1;
882 }
883 } else
884 vlan = 0;
885
886 bt_vhci_add(vlan);
887 return 0;
888 }
889 } else if (strstart(opt, "device:", &endp))
890 return !bt_device_add(endp);
891
892 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
893 return 1;
894}
895
896/***********************************************************/
897/* QEMU Block devices */
898
899#define HD_ALIAS "index=%d,media=disk"
900#define CDROM_ALIAS "index=2,media=cdrom"
901#define FD_ALIAS "index=%d,if=floppy"
902#define PFLASH_ALIAS "if=pflash"
903#define MTD_ALIAS "if=mtd"
904#define SD_ALIAS "index=0,if=sd"
905
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100906static int drive_init_func(QemuOpts *opts, void *opaque)
907{
908 int *use_scsi = opaque;
909 int fatal_error = 0;
910
911 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
912 if (fatal_error)
913 return 1;
914 }
915 return 0;
916}
917
918static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
919{
920 if (NULL == qemu_opt_get(opts, "snapshot")) {
921 qemu_opt_set(opts, "snapshot", "on");
922 }
923 return 0;
924}
925
926#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700927static int drive_opt_get_free_idx(void)
928{
929 int index;
930
931 for (index = 0; index < MAX_DRIVES; index++)
932 if (!drives_opt[index].used) {
933 drives_opt[index].used = 1;
934 return index;
935 }
936
937 return -1;
938}
939
940static int drive_get_free_idx(void)
941{
942 int index;
943
944 for (index = 0; index < MAX_DRIVES; index++)
945 if (!drives_table[index].used) {
946 drives_table[index].used = 1;
947 return index;
948 }
949
950 return -1;
951}
952
953int drive_add(const char *file, const char *fmt, ...)
954{
955 va_list ap;
956 int index = drive_opt_get_free_idx();
957
958 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
959 fprintf(stderr, "qemu: too many drives\n");
960 return -1;
961 }
962
963 drives_opt[index].file = file;
964 va_start(ap, fmt);
965 vsnprintf(drives_opt[index].opt,
966 sizeof(drives_opt[0].opt), fmt, ap);
967 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700968
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700969 nb_drives_opt++;
970 return index;
971}
972
973void drive_remove(int index)
974{
975 drives_opt[index].used = 0;
976 nb_drives_opt--;
977}
978
979int drive_get_index(BlockInterfaceType type, int bus, int unit)
980{
981 int index;
982
983 /* seek interface, bus and unit */
984
985 for (index = 0; index < MAX_DRIVES; index++)
986 if (drives_table[index].type == type &&
987 drives_table[index].bus == bus &&
988 drives_table[index].unit == unit &&
989 drives_table[index].used)
990 return index;
991
992 return -1;
993}
994
995int drive_get_max_bus(BlockInterfaceType type)
996{
997 int max_bus;
998 int index;
999
1000 max_bus = -1;
1001 for (index = 0; index < nb_drives; index++) {
1002 if(drives_table[index].type == type &&
1003 drives_table[index].bus > max_bus)
1004 max_bus = drives_table[index].bus;
1005 }
1006 return max_bus;
1007}
1008
1009const char *drive_get_serial(BlockDriverState *bdrv)
1010{
1011 int index;
1012
1013 for (index = 0; index < nb_drives; index++)
1014 if (drives_table[index].bdrv == bdrv)
1015 return drives_table[index].serial;
1016
1017 return "\0";
1018}
1019
1020BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1021{
1022 int index;
1023
1024 for (index = 0; index < nb_drives; index++)
1025 if (drives_table[index].bdrv == bdrv)
1026 return drives_table[index].onerror;
1027
1028 return BLOCK_ERR_STOP_ENOSPC;
1029}
1030
1031static void bdrv_format_print(void *opaque, const char *name)
1032{
1033 fprintf(stderr, " %s", name);
1034}
1035
1036void drive_uninit(BlockDriverState *bdrv)
1037{
1038 int i;
1039
1040 for (i = 0; i < MAX_DRIVES; i++)
1041 if (drives_table[i].bdrv == bdrv) {
1042 drives_table[i].bdrv = NULL;
1043 drives_table[i].used = 0;
1044 drive_remove(drives_table[i].drive_opt_idx);
1045 nb_drives--;
1046 break;
1047 }
1048}
1049
1050int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1051{
1052 char buf[128];
1053 char file[1024];
1054 char devname[128];
1055 char serial[21];
1056 const char *mediastr = "";
1057 BlockInterfaceType type;
1058 enum { MEDIA_DISK, MEDIA_CDROM } media;
1059 int bus_id, unit_id;
1060 int cyls, heads, secs, translation;
1061 BlockDriverState *bdrv;
1062 BlockDriver *drv = NULL;
1063 QEMUMachine *machine = opaque;
1064 int max_devs;
1065 int index;
1066 int cache;
1067 int bdrv_flags, onerror;
1068 int drives_table_idx;
1069 char *str = arg->opt;
1070 static const char * const params[] = { "bus", "unit", "if", "index",
1071 "cyls", "heads", "secs", "trans",
1072 "media", "snapshot", "file",
1073 "cache", "format", "serial", "werror",
1074 NULL };
1075
1076 if (check_params(buf, sizeof(buf), params, str) < 0) {
1077 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1078 buf, str);
1079 return -1;
1080 }
1081
1082 file[0] = 0;
1083 cyls = heads = secs = 0;
1084 bus_id = 0;
1085 unit_id = -1;
1086 translation = BIOS_ATA_TRANSLATION_AUTO;
1087 index = -1;
1088 cache = 3;
1089
1090 if (machine->use_scsi) {
1091 type = IF_SCSI;
1092 max_devs = MAX_SCSI_DEVS;
1093 pstrcpy(devname, sizeof(devname), "scsi");
1094 } else {
1095 type = IF_IDE;
1096 max_devs = MAX_IDE_DEVS;
1097 pstrcpy(devname, sizeof(devname), "ide");
1098 }
1099 media = MEDIA_DISK;
1100
1101 /* extract parameters */
1102
1103 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1104 bus_id = strtol(buf, NULL, 0);
1105 if (bus_id < 0) {
1106 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1107 return -1;
1108 }
1109 }
1110
1111 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1112 unit_id = strtol(buf, NULL, 0);
1113 if (unit_id < 0) {
1114 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1115 return -1;
1116 }
1117 }
1118
1119 if (get_param_value(buf, sizeof(buf), "if", str)) {
1120 pstrcpy(devname, sizeof(devname), buf);
1121 if (!strcmp(buf, "ide")) {
1122 type = IF_IDE;
1123 max_devs = MAX_IDE_DEVS;
1124 } else if (!strcmp(buf, "scsi")) {
1125 type = IF_SCSI;
1126 max_devs = MAX_SCSI_DEVS;
1127 } else if (!strcmp(buf, "floppy")) {
1128 type = IF_FLOPPY;
1129 max_devs = 0;
1130 } else if (!strcmp(buf, "pflash")) {
1131 type = IF_PFLASH;
1132 max_devs = 0;
1133 } else if (!strcmp(buf, "mtd")) {
1134 type = IF_MTD;
1135 max_devs = 0;
1136 } else if (!strcmp(buf, "sd")) {
1137 type = IF_SD;
1138 max_devs = 0;
1139 } else if (!strcmp(buf, "virtio")) {
1140 type = IF_VIRTIO;
1141 max_devs = 0;
1142 } else if (!strcmp(buf, "xen")) {
1143 type = IF_XEN;
1144 max_devs = 0;
1145 } else {
1146 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1147 return -1;
1148 }
1149 }
1150
1151 if (get_param_value(buf, sizeof(buf), "index", str)) {
1152 index = strtol(buf, NULL, 0);
1153 if (index < 0) {
1154 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1155 return -1;
1156 }
1157 }
1158
1159 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1160 cyls = strtol(buf, NULL, 0);
1161 }
1162
1163 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1164 heads = strtol(buf, NULL, 0);
1165 }
1166
1167 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1168 secs = strtol(buf, NULL, 0);
1169 }
1170
1171 if (cyls || heads || secs) {
1172 if (cyls < 1 || cyls > 16383) {
1173 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1174 return -1;
1175 }
1176 if (heads < 1 || heads > 16) {
1177 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1178 return -1;
1179 }
1180 if (secs < 1 || secs > 63) {
1181 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1182 return -1;
1183 }
1184 }
1185
1186 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1187 if (!cyls) {
1188 fprintf(stderr,
1189 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1190 str);
1191 return -1;
1192 }
1193 if (!strcmp(buf, "none"))
1194 translation = BIOS_ATA_TRANSLATION_NONE;
1195 else if (!strcmp(buf, "lba"))
1196 translation = BIOS_ATA_TRANSLATION_LBA;
1197 else if (!strcmp(buf, "auto"))
1198 translation = BIOS_ATA_TRANSLATION_AUTO;
1199 else {
1200 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1201 return -1;
1202 }
1203 }
1204
1205 if (get_param_value(buf, sizeof(buf), "media", str)) {
1206 if (!strcmp(buf, "disk")) {
1207 media = MEDIA_DISK;
1208 } else if (!strcmp(buf, "cdrom")) {
1209 if (cyls || secs || heads) {
1210 fprintf(stderr,
1211 "qemu: '%s' invalid physical CHS format\n", str);
1212 return -1;
1213 }
1214 media = MEDIA_CDROM;
1215 } else {
1216 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1217 return -1;
1218 }
1219 }
1220
1221 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1222 if (!strcmp(buf, "on"))
1223 snapshot = 1;
1224 else if (!strcmp(buf, "off"))
1225 snapshot = 0;
1226 else {
1227 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1228 return -1;
1229 }
1230 }
1231
1232 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1233 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1234 cache = 0;
1235 else if (!strcmp(buf, "writethrough"))
1236 cache = 1;
1237 else if (!strcmp(buf, "writeback"))
1238 cache = 2;
1239 else {
1240 fprintf(stderr, "qemu: invalid cache option\n");
1241 return -1;
1242 }
1243 }
1244
1245 if (get_param_value(buf, sizeof(buf), "format", str)) {
1246 if (strcmp(buf, "?") == 0) {
1247 fprintf(stderr, "qemu: Supported formats:");
1248 bdrv_iterate_format(bdrv_format_print, NULL);
1249 fprintf(stderr, "\n");
1250 return -1;
1251 }
1252 drv = bdrv_find_format(buf);
1253 if (!drv) {
1254 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1255 return -1;
1256 }
1257 }
1258
1259 if (arg->file == NULL)
1260 get_param_value(file, sizeof(file), "file", str);
1261 else
1262 pstrcpy(file, sizeof(file), arg->file);
1263
1264 if (!get_param_value(serial, sizeof(serial), "serial", str))
1265 memset(serial, 0, sizeof(serial));
1266
1267 onerror = BLOCK_ERR_STOP_ENOSPC;
1268 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1269 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1270 fprintf(stderr, "werror is no supported by this format\n");
1271 return -1;
1272 }
1273 if (!strcmp(buf, "ignore"))
1274 onerror = BLOCK_ERR_IGNORE;
1275 else if (!strcmp(buf, "enospc"))
1276 onerror = BLOCK_ERR_STOP_ENOSPC;
1277 else if (!strcmp(buf, "stop"))
1278 onerror = BLOCK_ERR_STOP_ANY;
1279 else if (!strcmp(buf, "report"))
1280 onerror = BLOCK_ERR_REPORT;
1281 else {
1282 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1283 return -1;
1284 }
1285 }
1286
1287 /* compute bus and unit according index */
1288
1289 if (index != -1) {
1290 if (bus_id != 0 || unit_id != -1) {
1291 fprintf(stderr,
1292 "qemu: '%s' index cannot be used with bus and unit\n", str);
1293 return -1;
1294 }
1295 if (max_devs == 0)
1296 {
1297 unit_id = index;
1298 bus_id = 0;
1299 } else {
1300 unit_id = index % max_devs;
1301 bus_id = index / max_devs;
1302 }
1303 }
1304
1305 /* if user doesn't specify a unit_id,
1306 * try to find the first free
1307 */
1308
1309 if (unit_id == -1) {
1310 unit_id = 0;
1311 while (drive_get_index(type, bus_id, unit_id) != -1) {
1312 unit_id++;
1313 if (max_devs && unit_id >= max_devs) {
1314 unit_id -= max_devs;
1315 bus_id++;
1316 }
1317 }
1318 }
1319
1320 /* check unit id */
1321
1322 if (max_devs && unit_id >= max_devs) {
1323 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1324 str, unit_id, max_devs - 1);
1325 return -1;
1326 }
1327
1328 /*
1329 * ignore multiple definitions
1330 */
1331
1332 if (drive_get_index(type, bus_id, unit_id) != -1)
1333 return -2;
1334
1335 /* init */
1336
1337 if (type == IF_IDE || type == IF_SCSI)
1338 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1339 if (max_devs)
1340 snprintf(buf, sizeof(buf), "%s%i%s%i",
1341 devname, bus_id, mediastr, unit_id);
1342 else
1343 snprintf(buf, sizeof(buf), "%s%s%i",
1344 devname, mediastr, unit_id);
1345 bdrv = bdrv_new(buf);
1346 drives_table_idx = drive_get_free_idx();
1347 drives_table[drives_table_idx].bdrv = bdrv;
1348 drives_table[drives_table_idx].type = type;
1349 drives_table[drives_table_idx].bus = bus_id;
1350 drives_table[drives_table_idx].unit = unit_id;
1351 drives_table[drives_table_idx].onerror = onerror;
1352 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1353 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1354 nb_drives++;
1355
1356 switch(type) {
1357 case IF_IDE:
1358 case IF_SCSI:
1359 case IF_XEN:
1360 switch(media) {
1361 case MEDIA_DISK:
1362 if (cyls != 0) {
1363 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1364 bdrv_set_translation_hint(bdrv, translation);
1365 }
1366 break;
1367 case MEDIA_CDROM:
1368 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1369 break;
1370 }
1371 break;
1372 case IF_SD:
1373 /* FIXME: This isn't really a floppy, but it's a reasonable
1374 approximation. */
1375 case IF_FLOPPY:
1376 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1377 break;
1378 case IF_PFLASH:
1379 case IF_MTD:
1380 case IF_VIRTIO:
1381 break;
1382 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001383 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001384 abort();
1385 }
1386 if (!file[0])
1387 return -2;
1388 bdrv_flags = 0;
1389 if (snapshot) {
1390 bdrv_flags |= BDRV_O_SNAPSHOT;
1391 cache = 2; /* always use write-back with snapshot */
1392 }
1393 if (cache == 0) /* no caching */
1394 bdrv_flags |= BDRV_O_NOCACHE;
1395 else if (cache == 2) /* write-back */
1396 bdrv_flags |= BDRV_O_CACHE_WB;
1397 else if (cache == 3) /* not specified */
1398 bdrv_flags |= BDRV_O_CACHE_DEF;
1399 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1400 fprintf(stderr, "qemu: could not open disk image %s\n",
1401 file);
1402 return -1;
1403 }
1404 if (bdrv_key_required(bdrv))
1405 autostart = 0;
1406 return drives_table_idx;
1407}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001408#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001409
1410static void numa_add(const char *optarg)
1411{
1412 char option[128];
1413 char *endptr;
1414 unsigned long long value, endvalue;
1415 int nodenr;
1416
1417 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1418 if (!strcmp(option, "node")) {
1419 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1420 nodenr = nb_numa_nodes;
1421 } else {
1422 nodenr = strtoull(option, NULL, 10);
1423 }
1424
1425 if (get_param_value(option, 128, "mem", optarg) == 0) {
1426 node_mem[nodenr] = 0;
1427 } else {
1428 value = strtoull(option, &endptr, 0);
1429 switch (*endptr) {
1430 case 0: case 'M': case 'm':
1431 value <<= 20;
1432 break;
1433 case 'G': case 'g':
1434 value <<= 30;
1435 break;
1436 }
1437 node_mem[nodenr] = value;
1438 }
1439 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1440 node_cpumask[nodenr] = 0;
1441 } else {
1442 value = strtoull(option, &endptr, 10);
1443 if (value >= 64) {
1444 value = 63;
1445 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1446 } else {
1447 if (*endptr == '-') {
1448 endvalue = strtoull(endptr+1, &endptr, 10);
1449 if (endvalue >= 63) {
1450 endvalue = 62;
1451 fprintf(stderr,
1452 "only 63 CPUs in NUMA mode supported.\n");
1453 }
1454 value = (1 << (endvalue + 1)) - (1 << value);
1455 } else {
1456 value = 1 << value;
1457 }
1458 }
1459 node_cpumask[nodenr] = value;
1460 }
1461 nb_numa_nodes++;
1462 }
1463 return;
1464}
1465
1466/***********************************************************/
1467/* USB devices */
1468
1469static USBPort *used_usb_ports;
1470static USBPort *free_usb_ports;
1471
1472/* ??? Maybe change this to register a hub to keep track of the topology. */
1473void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1474 usb_attachfn attach)
1475{
1476 port->opaque = opaque;
1477 port->index = index;
1478 port->attach = attach;
1479 port->next = free_usb_ports;
1480 free_usb_ports = port;
1481}
1482
1483int usb_device_add_dev(USBDevice *dev)
1484{
1485 USBPort *port;
1486
1487 /* Find a USB port to add the device to. */
1488 port = free_usb_ports;
1489 if (!port->next) {
1490 USBDevice *hub;
1491
1492 /* Create a new hub and chain it on. */
1493 free_usb_ports = NULL;
1494 port->next = used_usb_ports;
1495 used_usb_ports = port;
1496
1497 hub = usb_hub_init(VM_USB_HUB_SIZE);
1498 usb_attach(port, hub);
1499 port = free_usb_ports;
1500 }
1501
1502 free_usb_ports = port->next;
1503 port->next = used_usb_ports;
1504 used_usb_ports = port;
1505 usb_attach(port, dev);
1506 return 0;
1507}
1508
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001509#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001510static void usb_msd_password_cb(void *opaque, int err)
1511{
1512 USBDevice *dev = opaque;
1513
1514 if (!err)
1515 usb_device_add_dev(dev);
1516 else
1517 dev->handle_destroy(dev);
1518}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001519#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001520
1521static int usb_device_add(const char *devname, int is_hotplug)
1522{
1523 const char *p;
1524 USBDevice *dev;
1525
1526 if (!free_usb_ports)
1527 return -1;
1528
1529 if (strstart(devname, "host:", &p)) {
1530 dev = usb_host_device_open(p);
1531 } else if (!strcmp(devname, "mouse")) {
1532 dev = usb_mouse_init();
1533 } else if (!strcmp(devname, "tablet")) {
1534 dev = usb_tablet_init();
1535 } else if (!strcmp(devname, "keyboard")) {
1536 dev = usb_keyboard_init();
1537 } else if (strstart(devname, "disk:", &p)) {
1538#if 0
1539 BlockDriverState *bs;
1540#endif
1541 dev = usb_msd_init(p);
1542 if (!dev)
1543 return -1;
1544#if 0
1545 bs = usb_msd_get_bdrv(dev);
1546 if (bdrv_key_required(bs)) {
1547 autostart = 0;
1548 if (is_hotplug) {
1549 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1550 dev);
1551 return 0;
1552 }
1553 }
1554 } else if (!strcmp(devname, "wacom-tablet")) {
1555 dev = usb_wacom_init();
1556 } else if (strstart(devname, "serial:", &p)) {
1557 dev = usb_serial_init(p);
1558#ifdef CONFIG_BRLAPI
1559 } else if (!strcmp(devname, "braille")) {
1560 dev = usb_baum_init();
1561#endif
1562 } else if (strstart(devname, "net:", &p)) {
1563 int nic = nb_nics;
1564
1565 if (net_client_init("nic", p) < 0)
1566 return -1;
1567 nd_table[nic].model = "usb";
1568 dev = usb_net_init(&nd_table[nic]);
1569 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1570 dev = usb_bt_init(devname[2] ? hci_init(p) :
1571 bt_new_hci(qemu_find_bt_vlan(0)));
1572#endif
1573 } else {
1574 return -1;
1575 }
1576 if (!dev)
1577 return -1;
1578
1579 return usb_device_add_dev(dev);
1580}
1581
1582int usb_device_del_addr(int bus_num, int addr)
1583{
1584 USBPort *port;
1585 USBPort **lastp;
1586 USBDevice *dev;
1587
1588 if (!used_usb_ports)
1589 return -1;
1590
1591 if (bus_num != 0)
1592 return -1;
1593
1594 lastp = &used_usb_ports;
1595 port = used_usb_ports;
1596 while (port && port->dev->addr != addr) {
1597 lastp = &port->next;
1598 port = port->next;
1599 }
1600
1601 if (!port)
1602 return -1;
1603
1604 dev = port->dev;
1605 *lastp = port->next;
1606 usb_attach(port, NULL);
1607 dev->handle_destroy(dev);
1608 port->next = free_usb_ports;
1609 free_usb_ports = port;
1610 return 0;
1611}
1612
1613static int usb_device_del(const char *devname)
1614{
1615 int bus_num, addr;
1616 const char *p;
1617
1618 if (strstart(devname, "host:", &p))
1619 return usb_host_device_close(p);
1620
1621 if (!used_usb_ports)
1622 return -1;
1623
1624 p = strchr(devname, '.');
1625 if (!p)
1626 return -1;
1627 bus_num = strtoul(devname, NULL, 0);
1628 addr = strtoul(p + 1, NULL, 0);
1629
1630 return usb_device_del_addr(bus_num, addr);
1631}
1632
1633void do_usb_add(Monitor *mon, const char *devname)
1634{
1635 usb_device_add(devname, 1);
1636}
1637
1638void do_usb_del(Monitor *mon, const char *devname)
1639{
1640 usb_device_del(devname);
1641}
1642
1643void usb_info(Monitor *mon)
1644{
1645 USBDevice *dev;
1646 USBPort *port;
1647 const char *speed_str;
1648
1649 if (!usb_enabled) {
1650 monitor_printf(mon, "USB support not enabled\n");
1651 return;
1652 }
1653
1654 for (port = used_usb_ports; port; port = port->next) {
1655 dev = port->dev;
1656 if (!dev)
1657 continue;
1658 switch(dev->speed) {
1659 case USB_SPEED_LOW:
1660 speed_str = "1.5";
1661 break;
1662 case USB_SPEED_FULL:
1663 speed_str = "12";
1664 break;
1665 case USB_SPEED_HIGH:
1666 speed_str = "480";
1667 break;
1668 default:
1669 speed_str = "?";
1670 break;
1671 }
1672 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1673 0, dev->addr, speed_str, dev->devname);
1674 }
1675}
1676
1677/***********************************************************/
1678/* PCMCIA/Cardbus */
1679
1680static struct pcmcia_socket_entry_s {
1681 PCMCIASocket *socket;
1682 struct pcmcia_socket_entry_s *next;
1683} *pcmcia_sockets = 0;
1684
1685void pcmcia_socket_register(PCMCIASocket *socket)
1686{
1687 struct pcmcia_socket_entry_s *entry;
1688
1689 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1690 entry->socket = socket;
1691 entry->next = pcmcia_sockets;
1692 pcmcia_sockets = entry;
1693}
1694
1695void pcmcia_socket_unregister(PCMCIASocket *socket)
1696{
1697 struct pcmcia_socket_entry_s *entry, **ptr;
1698
1699 ptr = &pcmcia_sockets;
1700 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1701 if (entry->socket == socket) {
1702 *ptr = entry->next;
1703 qemu_free(entry);
1704 }
1705}
1706
1707void pcmcia_info(Monitor *mon)
1708{
1709 struct pcmcia_socket_entry_s *iter;
1710
1711 if (!pcmcia_sockets)
1712 monitor_printf(mon, "No PCMCIA sockets\n");
1713
1714 for (iter = pcmcia_sockets; iter; iter = iter->next)
1715 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1716 iter->socket->attached ? iter->socket->card_string :
1717 "Empty");
1718}
1719
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001720#ifdef _WIN32
1721/***********************************************************/
1722/* Polling handling */
1723
1724typedef struct PollingEntry {
1725 PollingFunc *func;
1726 void *opaque;
1727 struct PollingEntry *next;
1728} PollingEntry;
1729
1730static PollingEntry *first_polling_entry;
1731
1732int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1733{
1734 PollingEntry **ppe, *pe;
1735 pe = qemu_mallocz(sizeof(PollingEntry));
1736 pe->func = func;
1737 pe->opaque = opaque;
1738 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1739 *ppe = pe;
1740 return 0;
1741}
1742
1743void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1744{
1745 PollingEntry **ppe, *pe;
1746 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1747 pe = *ppe;
1748 if (pe->func == func && pe->opaque == opaque) {
1749 *ppe = pe->next;
1750 qemu_free(pe);
1751 break;
1752 }
1753 }
1754}
1755
1756/***********************************************************/
1757/* Wait objects support */
1758typedef struct WaitObjects {
1759 int num;
1760 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1761 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1762 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1763} WaitObjects;
1764
1765static WaitObjects wait_objects = {0};
1766
1767int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1768{
1769 WaitObjects *w = &wait_objects;
1770
1771 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1772 return -1;
1773 w->events[w->num] = handle;
1774 w->func[w->num] = func;
1775 w->opaque[w->num] = opaque;
1776 w->num++;
1777 return 0;
1778}
1779
1780void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1781{
1782 int i, found;
1783 WaitObjects *w = &wait_objects;
1784
1785 found = 0;
1786 for (i = 0; i < w->num; i++) {
1787 if (w->events[i] == handle)
1788 found = 1;
1789 if (found) {
1790 w->events[i] = w->events[i + 1];
1791 w->func[i] = w->func[i + 1];
1792 w->opaque[i] = w->opaque[i + 1];
1793 }
1794 }
1795 if (found)
1796 w->num--;
1797}
1798#endif
1799
1800/***********************************************************/
1801/* ram save/restore */
1802
1803static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1804{
1805 int v;
1806
1807 v = qemu_get_byte(f);
1808 switch(v) {
1809 case 0:
1810 if (qemu_get_buffer(f, buf, len) != len)
1811 return -EIO;
1812 break;
1813 case 1:
1814 v = qemu_get_byte(f);
1815 memset(buf, v, len);
1816 break;
1817 default:
1818 return -EINVAL;
1819 }
1820
1821 if (qemu_file_has_error(f))
1822 return -EIO;
1823
1824 return 0;
1825}
1826
1827static int ram_load_v1(QEMUFile *f, void *opaque)
1828{
1829 int ret;
1830 ram_addr_t i;
1831
1832 if (qemu_get_be32(f) != last_ram_offset)
1833 return -EINVAL;
1834 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1835 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1836 if (ret)
1837 return ret;
1838 }
1839 return 0;
1840}
1841
1842#define BDRV_HASH_BLOCK_SIZE 1024
1843#define IOBUF_SIZE 4096
1844#define RAM_CBLOCK_MAGIC 0xfabe
1845
1846typedef struct RamDecompressState {
1847 z_stream zstream;
1848 QEMUFile *f;
1849 uint8_t buf[IOBUF_SIZE];
1850} RamDecompressState;
1851
1852static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1853{
1854 int ret;
1855 memset(s, 0, sizeof(*s));
1856 s->f = f;
1857 ret = inflateInit(&s->zstream);
1858 if (ret != Z_OK)
1859 return -1;
1860 return 0;
1861}
1862
1863static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1864{
1865 int ret, clen;
1866
1867 s->zstream.avail_out = len;
1868 s->zstream.next_out = buf;
1869 while (s->zstream.avail_out > 0) {
1870 if (s->zstream.avail_in == 0) {
1871 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1872 return -1;
1873 clen = qemu_get_be16(s->f);
1874 if (clen > IOBUF_SIZE)
1875 return -1;
1876 qemu_get_buffer(s->f, s->buf, clen);
1877 s->zstream.avail_in = clen;
1878 s->zstream.next_in = s->buf;
1879 }
1880 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1881 if (ret != Z_OK && ret != Z_STREAM_END) {
1882 return -1;
1883 }
1884 }
1885 return 0;
1886}
1887
1888static void ram_decompress_close(RamDecompressState *s)
1889{
1890 inflateEnd(&s->zstream);
1891}
1892
1893#define RAM_SAVE_FLAG_FULL 0x01
1894#define RAM_SAVE_FLAG_COMPRESS 0x02
1895#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1896#define RAM_SAVE_FLAG_PAGE 0x08
1897#define RAM_SAVE_FLAG_EOS 0x10
1898
1899static int is_dup_page(uint8_t *page, uint8_t ch)
1900{
1901 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1902 uint32_t *array = (uint32_t *)page;
1903 int i;
1904
1905 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1906 if (array[i] != val)
1907 return 0;
1908 }
1909
1910 return 1;
1911}
1912
1913static int ram_save_block(QEMUFile *f)
1914{
1915 static ram_addr_t current_addr = 0;
1916 ram_addr_t saved_addr = current_addr;
1917 ram_addr_t addr = 0;
1918 int found = 0;
1919
1920 while (addr < last_ram_offset) {
1921 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1922 uint8_t *p;
1923
1924 cpu_physical_memory_reset_dirty(current_addr,
1925 current_addr + TARGET_PAGE_SIZE,
1926 MIGRATION_DIRTY_FLAG);
1927
1928 p = qemu_get_ram_ptr(current_addr);
1929
1930 if (is_dup_page(p, *p)) {
1931 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
1932 qemu_put_byte(f, *p);
1933 } else {
1934 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
1935 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
1936 }
1937
1938 found = 1;
1939 break;
1940 }
1941 addr += TARGET_PAGE_SIZE;
1942 current_addr = (saved_addr + addr) % last_ram_offset;
1943 }
1944
1945 return found;
1946}
1947
1948static uint64_t bytes_transferred = 0;
1949
1950static ram_addr_t ram_save_remaining(void)
1951{
1952 ram_addr_t addr;
1953 ram_addr_t count = 0;
1954
1955 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
1956 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1957 count++;
1958 }
1959
1960 return count;
1961}
1962
1963uint64_t ram_bytes_remaining(void)
1964{
1965 return ram_save_remaining() * TARGET_PAGE_SIZE;
1966}
1967
1968uint64_t ram_bytes_transferred(void)
1969{
1970 return bytes_transferred;
1971}
1972
1973uint64_t ram_bytes_total(void)
1974{
1975 return last_ram_offset;
1976}
1977
1978static int ram_save_live(QEMUFile *f, int stage, void *opaque)
1979{
1980 ram_addr_t addr;
1981 uint64_t bytes_transferred_last;
1982 double bwidth = 0;
1983 uint64_t expected_time = 0;
1984
1985 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
1986
1987 if (stage == 1) {
1988 /* Make sure all dirty bits are set */
1989 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
1990 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1991 cpu_physical_memory_set_dirty(addr);
1992 }
1993
1994 /* Enable dirty memory tracking */
1995 cpu_physical_memory_set_dirty_tracking(1);
1996
1997 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
1998 }
1999
2000 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002001 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002002
2003 while (!qemu_file_rate_limit(f)) {
2004 int ret;
2005
2006 ret = ram_save_block(f);
2007 bytes_transferred += ret * TARGET_PAGE_SIZE;
2008 if (ret == 0) /* no more blocks */
2009 break;
2010 }
2011
David Turner6a9ef172010-09-09 22:54:36 +02002012 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002013 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2014
2015 /* if we haven't transferred anything this round, force expected_time to a
2016 * a very high value, but without crashing */
2017 if (bwidth == 0)
2018 bwidth = 0.000001;
2019
2020 /* try transferring iterative blocks of memory */
2021
2022 if (stage == 3) {
2023
2024 /* flush all remaining blocks regardless of rate limiting */
2025 while (ram_save_block(f) != 0) {
2026 bytes_transferred += TARGET_PAGE_SIZE;
2027 }
2028 cpu_physical_memory_set_dirty_tracking(0);
2029 }
2030
2031 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2032
2033 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2034
2035 return (stage == 2) && (expected_time <= migrate_max_downtime());
2036}
2037
2038static int ram_load_dead(QEMUFile *f, void *opaque)
2039{
2040 RamDecompressState s1, *s = &s1;
2041 uint8_t buf[10];
2042 ram_addr_t i;
2043
2044 if (ram_decompress_open(s, f) < 0)
2045 return -EINVAL;
2046 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2047 if (ram_decompress_buf(s, buf, 1) < 0) {
2048 fprintf(stderr, "Error while reading ram block header\n");
2049 goto error;
2050 }
2051 if (buf[0] == 0) {
2052 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2053 BDRV_HASH_BLOCK_SIZE) < 0) {
2054 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2055 goto error;
2056 }
2057 } else {
2058 error:
2059 printf("Error block header\n");
2060 return -EINVAL;
2061 }
2062 }
2063 ram_decompress_close(s);
2064
2065 return 0;
2066}
2067
2068static int ram_load(QEMUFile *f, void *opaque, int version_id)
2069{
2070 ram_addr_t addr;
2071 int flags;
2072
2073 if (version_id == 1)
2074 return ram_load_v1(f, opaque);
2075
2076 if (version_id == 2) {
2077 if (qemu_get_be32(f) != last_ram_offset)
2078 return -EINVAL;
2079 return ram_load_dead(f, opaque);
2080 }
2081
2082 if (version_id != 3)
2083 return -EINVAL;
2084
2085 do {
2086 addr = qemu_get_be64(f);
2087
2088 flags = addr & ~TARGET_PAGE_MASK;
2089 addr &= TARGET_PAGE_MASK;
2090
2091 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2092 if (addr != last_ram_offset)
2093 return -EINVAL;
2094 }
2095
2096 if (flags & RAM_SAVE_FLAG_FULL) {
2097 if (ram_load_dead(f, opaque) < 0)
2098 return -EINVAL;
2099 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002100
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002101 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2102 uint8_t ch = qemu_get_byte(f);
2103 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2104 } else if (flags & RAM_SAVE_FLAG_PAGE)
2105 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2106 } while (!(flags & RAM_SAVE_FLAG_EOS));
2107
2108 return 0;
2109}
2110
2111void qemu_service_io(void)
2112{
2113 qemu_notify_event();
2114}
2115
2116/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002117/* machine registration */
2118
2119static QEMUMachine *first_machine = NULL;
2120QEMUMachine *current_machine = NULL;
2121
2122int qemu_register_machine(QEMUMachine *m)
2123{
2124 QEMUMachine **pm;
2125 pm = &first_machine;
2126 while (*pm != NULL)
2127 pm = &(*pm)->next;
2128 m->next = NULL;
2129 *pm = m;
2130 return 0;
2131}
2132
2133static QEMUMachine *find_machine(const char *name)
2134{
2135 QEMUMachine *m;
2136
2137 for(m = first_machine; m != NULL; m = m->next) {
2138 if (!strcmp(m->name, name))
2139 return m;
2140 }
2141 return NULL;
2142}
2143
2144static QEMUMachine *find_default_machine(void)
2145{
2146 QEMUMachine *m;
2147
2148 for(m = first_machine; m != NULL; m = m->next) {
2149 if (m->is_default) {
2150 return m;
2151 }
2152 }
2153 return NULL;
2154}
2155
2156/***********************************************************/
2157/* main execution loop */
2158
2159static void gui_update(void *opaque)
2160{
2161 uint64_t interval = GUI_REFRESH_INTERVAL;
2162 DisplayState *ds = opaque;
2163 DisplayChangeListener *dcl = ds->listeners;
2164
2165 dpy_refresh(ds);
2166
2167 while (dcl != NULL) {
2168 if (dcl->gui_timer_interval &&
2169 dcl->gui_timer_interval < interval)
2170 interval = dcl->gui_timer_interval;
2171 dcl = dcl->next;
2172 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002173 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002174}
2175
2176static void nographic_update(void *opaque)
2177{
2178 uint64_t interval = GUI_REFRESH_INTERVAL;
2179
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002180 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002181}
2182
2183struct vm_change_state_entry {
2184 VMChangeStateHandler *cb;
2185 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002186 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002187};
2188
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002189static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002190
2191VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2192 void *opaque)
2193{
2194 VMChangeStateEntry *e;
2195
2196 e = qemu_mallocz(sizeof (*e));
2197
2198 e->cb = cb;
2199 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002200 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002201 return e;
2202}
2203
2204void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2205{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002206 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002207 qemu_free (e);
2208}
2209
2210static void vm_state_notify(int running, int reason)
2211{
2212 VMChangeStateEntry *e;
2213
2214 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2215 e->cb(e->opaque, running, reason);
2216 }
2217}
2218
2219static void resume_all_vcpus(void);
2220static void pause_all_vcpus(void);
2221
2222void vm_start(void)
2223{
2224 if (!vm_running) {
2225 cpu_enable_ticks();
2226 vm_running = 1;
2227 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002228 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002229 resume_all_vcpus();
2230 }
2231}
2232
2233/* reset/shutdown handler */
2234
2235typedef struct QEMUResetEntry {
2236 QEMUResetHandler *func;
2237 void *opaque;
2238 int order;
2239 struct QEMUResetEntry *next;
2240} QEMUResetEntry;
2241
2242static QEMUResetEntry *first_reset_entry;
2243static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002244static int shutdown_requested, shutdown_signal = -1;
2245static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002246static int powerdown_requested;
2247static int debug_requested;
2248static int vmstop_requested;
2249
2250int qemu_shutdown_requested(void)
2251{
2252 int r = shutdown_requested;
2253 shutdown_requested = 0;
2254 return r;
2255}
2256
2257int qemu_reset_requested(void)
2258{
2259 int r = reset_requested;
2260 reset_requested = 0;
2261 return r;
2262}
2263
2264int qemu_powerdown_requested(void)
2265{
2266 int r = powerdown_requested;
2267 powerdown_requested = 0;
2268 return r;
2269}
2270
2271static int qemu_debug_requested(void)
2272{
2273 int r = debug_requested;
2274 debug_requested = 0;
2275 return r;
2276}
2277
2278static int qemu_vmstop_requested(void)
2279{
2280 int r = vmstop_requested;
2281 vmstop_requested = 0;
2282 return r;
2283}
2284
2285static void do_vm_stop(int reason)
2286{
2287 if (vm_running) {
2288 cpu_disable_ticks();
2289 vm_running = 0;
2290 pause_all_vcpus();
2291 vm_state_notify(0, reason);
2292 }
2293}
2294
2295void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2296{
2297 QEMUResetEntry **pre, *re;
2298
2299 pre = &first_reset_entry;
2300 while (*pre != NULL && (*pre)->order >= order) {
2301 pre = &(*pre)->next;
2302 }
2303 re = qemu_mallocz(sizeof(QEMUResetEntry));
2304 re->func = func;
2305 re->opaque = opaque;
2306 re->order = order;
2307 re->next = NULL;
2308 *pre = re;
2309}
2310
2311void qemu_system_reset(void)
2312{
2313 QEMUResetEntry *re;
2314
2315 /* reset all devices */
2316 for(re = first_reset_entry; re != NULL; re = re->next) {
2317 re->func(re->opaque);
2318 }
2319}
2320
2321void qemu_system_reset_request(void)
2322{
2323 if (no_reboot) {
2324 shutdown_requested = 1;
2325 } else {
2326 reset_requested = 1;
2327 }
2328 qemu_notify_event();
2329}
2330
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002331void qemu_system_killed(int signal, pid_t pid)
2332{
2333 shutdown_signal = signal;
2334 shutdown_pid = pid;
2335 qemu_system_shutdown_request();
2336}
2337
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002338void qemu_system_shutdown_request(void)
2339{
2340 shutdown_requested = 1;
2341 qemu_notify_event();
2342}
2343
2344void qemu_system_powerdown_request(void)
2345{
2346 powerdown_requested = 1;
2347 qemu_notify_event();
2348}
2349
2350#ifdef CONFIG_IOTHREAD
2351static void qemu_system_vmstop_request(int reason)
2352{
2353 vmstop_requested = reason;
2354 qemu_notify_event();
2355}
2356#endif
2357
2358#ifndef _WIN32
2359static int io_thread_fd = -1;
2360
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002361#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002362static void qemu_event_increment(void)
2363{
2364 static const char byte = 0;
2365
2366 if (io_thread_fd == -1)
2367 return;
2368
2369 write(io_thread_fd, &byte, sizeof(byte));
2370}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002371#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002372
2373static void qemu_event_read(void *opaque)
2374{
2375 int fd = (unsigned long)opaque;
2376 ssize_t len;
2377
2378 /* Drain the notify pipe */
2379 do {
2380 char buffer[512];
2381 len = read(fd, buffer, sizeof(buffer));
2382 } while ((len == -1 && errno == EINTR) || len > 0);
2383}
2384
2385static int qemu_event_init(void)
2386{
2387 int err;
2388 int fds[2];
2389
2390 err = pipe(fds);
2391 if (err == -1)
2392 return -errno;
2393
2394 err = fcntl_setfl(fds[0], O_NONBLOCK);
2395 if (err < 0)
2396 goto fail;
2397
2398 err = fcntl_setfl(fds[1], O_NONBLOCK);
2399 if (err < 0)
2400 goto fail;
2401
2402 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2403 (void *)(unsigned long)fds[0]);
2404
2405 io_thread_fd = fds[1];
2406 return 0;
2407
2408fail:
2409 close(fds[0]);
2410 close(fds[1]);
2411 return err;
2412}
2413#else
2414HANDLE qemu_event_handle;
2415
2416static void dummy_event_handler(void *opaque)
2417{
2418}
2419
2420static int qemu_event_init(void)
2421{
2422 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2423 if (!qemu_event_handle) {
2424 perror("Failed CreateEvent");
2425 return -1;
2426 }
2427 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2428 return 0;
2429}
2430
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002431#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002432static void qemu_event_increment(void)
2433{
2434 SetEvent(qemu_event_handle);
2435}
2436#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002437#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002438
2439static int cpu_can_run(CPUState *env)
2440{
2441 if (env->stop)
2442 return 0;
2443 if (env->stopped)
2444 return 0;
2445 return 1;
2446}
2447
2448#ifndef CONFIG_IOTHREAD
2449static int qemu_init_main_loop(void)
2450{
2451 return qemu_event_init();
2452}
2453
2454void qemu_init_vcpu(void *_env)
2455{
2456 CPUState *env = _env;
2457
2458 if (kvm_enabled())
2459 kvm_init_vcpu(env);
2460 return;
2461}
2462
2463int qemu_cpu_self(void *env)
2464{
2465 return 1;
2466}
2467
2468static void resume_all_vcpus(void)
2469{
2470}
2471
2472static void pause_all_vcpus(void)
2473{
2474}
2475
2476void qemu_cpu_kick(void *env)
2477{
2478 return;
2479}
2480
2481void qemu_notify_event(void)
2482{
2483 CPUState *env = cpu_single_env;
2484
2485 if (env) {
2486 cpu_exit(env);
2487#ifdef USE_KQEMU
2488 if (env->kqemu_enabled)
2489 kqemu_cpu_interrupt(env);
2490#endif
2491 }
2492}
2493
2494#define qemu_mutex_lock_iothread() do { } while (0)
2495#define qemu_mutex_unlock_iothread() do { } while (0)
2496
2497void vm_stop(int reason)
2498{
2499 do_vm_stop(reason);
2500}
2501
2502#else /* CONFIG_IOTHREAD */
2503
2504#include "qemu-thread.h"
2505
2506QemuMutex qemu_global_mutex;
2507static QemuMutex qemu_fair_mutex;
2508
2509static QemuThread io_thread;
2510
2511static QemuThread *tcg_cpu_thread;
2512static QemuCond *tcg_halt_cond;
2513
2514static int qemu_system_ready;
2515/* cpu creation */
2516static QemuCond qemu_cpu_cond;
2517/* system init */
2518static QemuCond qemu_system_cond;
2519static QemuCond qemu_pause_cond;
2520
2521static void block_io_signals(void);
2522static void unblock_io_signals(void);
2523static int tcg_has_work(void);
2524
2525static int qemu_init_main_loop(void)
2526{
2527 int ret;
2528
2529 ret = qemu_event_init();
2530 if (ret)
2531 return ret;
2532
2533 qemu_cond_init(&qemu_pause_cond);
2534 qemu_mutex_init(&qemu_fair_mutex);
2535 qemu_mutex_init(&qemu_global_mutex);
2536 qemu_mutex_lock(&qemu_global_mutex);
2537
2538 unblock_io_signals();
2539 qemu_thread_self(&io_thread);
2540
2541 return 0;
2542}
2543
2544static void qemu_wait_io_event(CPUState *env)
2545{
2546 while (!tcg_has_work())
2547 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2548
2549 qemu_mutex_unlock(&qemu_global_mutex);
2550
2551 /*
2552 * Users of qemu_global_mutex can be starved, having no chance
2553 * to acquire it since this path will get to it first.
2554 * So use another lock to provide fairness.
2555 */
2556 qemu_mutex_lock(&qemu_fair_mutex);
2557 qemu_mutex_unlock(&qemu_fair_mutex);
2558
2559 qemu_mutex_lock(&qemu_global_mutex);
2560 if (env->stop) {
2561 env->stop = 0;
2562 env->stopped = 1;
2563 qemu_cond_signal(&qemu_pause_cond);
2564 }
2565}
2566
2567static int qemu_cpu_exec(CPUState *env);
2568
2569static void *kvm_cpu_thread_fn(void *arg)
2570{
2571 CPUState *env = arg;
2572
2573 block_io_signals();
2574 qemu_thread_self(env->thread);
2575
2576 /* signal CPU creation */
2577 qemu_mutex_lock(&qemu_global_mutex);
2578 env->created = 1;
2579 qemu_cond_signal(&qemu_cpu_cond);
2580
2581 /* and wait for machine initialization */
2582 while (!qemu_system_ready)
2583 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2584
2585 while (1) {
2586 if (cpu_can_run(env))
2587 qemu_cpu_exec(env);
2588 qemu_wait_io_event(env);
2589 }
2590
2591 return NULL;
2592}
2593
2594static void tcg_cpu_exec(void);
2595
2596static void *tcg_cpu_thread_fn(void *arg)
2597{
2598 CPUState *env = arg;
2599
2600 block_io_signals();
2601 qemu_thread_self(env->thread);
2602
2603 /* signal CPU creation */
2604 qemu_mutex_lock(&qemu_global_mutex);
2605 for (env = first_cpu; env != NULL; env = env->next_cpu)
2606 env->created = 1;
2607 qemu_cond_signal(&qemu_cpu_cond);
2608
2609 /* and wait for machine initialization */
2610 while (!qemu_system_ready)
2611 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2612
2613 while (1) {
2614 tcg_cpu_exec();
2615 qemu_wait_io_event(cur_cpu);
2616 }
2617
2618 return NULL;
2619}
2620
2621void qemu_cpu_kick(void *_env)
2622{
2623 CPUState *env = _env;
2624 qemu_cond_broadcast(env->halt_cond);
2625 if (kvm_enabled())
2626 qemu_thread_signal(env->thread, SIGUSR1);
2627}
2628
2629int qemu_cpu_self(void *env)
2630{
2631 return (cpu_single_env != NULL);
2632}
2633
2634static void cpu_signal(int sig)
2635{
2636 if (cpu_single_env)
2637 cpu_exit(cpu_single_env);
2638}
2639
2640static void block_io_signals(void)
2641{
2642 sigset_t set;
2643 struct sigaction sigact;
2644
2645 sigemptyset(&set);
2646 sigaddset(&set, SIGUSR2);
2647 sigaddset(&set, SIGIO);
2648 sigaddset(&set, SIGALRM);
2649 pthread_sigmask(SIG_BLOCK, &set, NULL);
2650
2651 sigemptyset(&set);
2652 sigaddset(&set, SIGUSR1);
2653 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2654
2655 memset(&sigact, 0, sizeof(sigact));
2656 sigact.sa_handler = cpu_signal;
2657 sigaction(SIGUSR1, &sigact, NULL);
2658}
2659
2660static void unblock_io_signals(void)
2661{
2662 sigset_t set;
2663
2664 sigemptyset(&set);
2665 sigaddset(&set, SIGUSR2);
2666 sigaddset(&set, SIGIO);
2667 sigaddset(&set, SIGALRM);
2668 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2669
2670 sigemptyset(&set);
2671 sigaddset(&set, SIGUSR1);
2672 pthread_sigmask(SIG_BLOCK, &set, NULL);
2673}
2674
2675static void qemu_signal_lock(unsigned int msecs)
2676{
2677 qemu_mutex_lock(&qemu_fair_mutex);
2678
2679 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2680 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2681 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2682 break;
2683 }
2684 qemu_mutex_unlock(&qemu_fair_mutex);
2685}
2686
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002687void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002688{
2689 if (kvm_enabled()) {
2690 qemu_mutex_lock(&qemu_fair_mutex);
2691 qemu_mutex_lock(&qemu_global_mutex);
2692 qemu_mutex_unlock(&qemu_fair_mutex);
2693 } else
2694 qemu_signal_lock(100);
2695}
2696
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002697void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002698{
2699 qemu_mutex_unlock(&qemu_global_mutex);
2700}
2701
2702static int all_vcpus_paused(void)
2703{
2704 CPUState *penv = first_cpu;
2705
2706 while (penv) {
2707 if (!penv->stopped)
2708 return 0;
2709 penv = (CPUState *)penv->next_cpu;
2710 }
2711
2712 return 1;
2713}
2714
2715static void pause_all_vcpus(void)
2716{
2717 CPUState *penv = first_cpu;
2718
2719 while (penv) {
2720 penv->stop = 1;
2721 qemu_thread_signal(penv->thread, SIGUSR1);
2722 qemu_cpu_kick(penv);
2723 penv = (CPUState *)penv->next_cpu;
2724 }
2725
2726 while (!all_vcpus_paused()) {
2727 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2728 penv = first_cpu;
2729 while (penv) {
2730 qemu_thread_signal(penv->thread, SIGUSR1);
2731 penv = (CPUState *)penv->next_cpu;
2732 }
2733 }
2734}
2735
2736static void resume_all_vcpus(void)
2737{
2738 CPUState *penv = first_cpu;
2739
2740 while (penv) {
2741 penv->stop = 0;
2742 penv->stopped = 0;
2743 qemu_thread_signal(penv->thread, SIGUSR1);
2744 qemu_cpu_kick(penv);
2745 penv = (CPUState *)penv->next_cpu;
2746 }
2747}
2748
2749static void tcg_init_vcpu(void *_env)
2750{
2751 CPUState *env = _env;
2752 /* share a single thread for all cpus with TCG */
2753 if (!tcg_cpu_thread) {
2754 env->thread = qemu_mallocz(sizeof(QemuThread));
2755 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2756 qemu_cond_init(env->halt_cond);
2757 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2758 while (env->created == 0)
2759 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2760 tcg_cpu_thread = env->thread;
2761 tcg_halt_cond = env->halt_cond;
2762 } else {
2763 env->thread = tcg_cpu_thread;
2764 env->halt_cond = tcg_halt_cond;
2765 }
2766}
2767
2768static void kvm_start_vcpu(CPUState *env)
2769{
2770#if 0
2771 kvm_init_vcpu(env);
2772 env->thread = qemu_mallocz(sizeof(QemuThread));
2773 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2774 qemu_cond_init(env->halt_cond);
2775 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2776 while (env->created == 0)
2777 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2778#endif
2779}
2780
2781void qemu_init_vcpu(void *_env)
2782{
2783 CPUState *env = _env;
2784
2785 if (kvm_enabled())
2786 kvm_start_vcpu(env);
2787 else
2788 tcg_init_vcpu(env);
2789}
2790
2791void qemu_notify_event(void)
2792{
2793 qemu_event_increment();
2794}
2795
2796void vm_stop(int reason)
2797{
2798 QemuThread me;
2799 qemu_thread_self(&me);
2800
2801 if (!qemu_thread_equal(&me, &io_thread)) {
2802 qemu_system_vmstop_request(reason);
2803 /*
2804 * FIXME: should not return to device code in case
2805 * vm_stop() has been requested.
2806 */
2807 if (cpu_single_env) {
2808 cpu_exit(cpu_single_env);
2809 cpu_single_env->stop = 1;
2810 }
2811 return;
2812 }
2813 do_vm_stop(reason);
2814}
2815
2816#endif
2817
2818
2819#ifdef _WIN32
2820static void host_main_loop_wait(int *timeout)
2821{
2822 int ret, ret2, i;
2823 PollingEntry *pe;
2824
2825
2826 /* XXX: need to suppress polling by better using win32 events */
2827 ret = 0;
2828 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2829 ret |= pe->func(pe->opaque);
2830 }
2831 if (ret == 0) {
2832 int err;
2833 WaitObjects *w = &wait_objects;
2834
2835 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2836 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2837 if (w->func[ret - WAIT_OBJECT_0])
2838 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2839
2840 /* Check for additional signaled events */
2841 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2842
2843 /* Check if event is signaled */
2844 ret2 = WaitForSingleObject(w->events[i], 0);
2845 if(ret2 == WAIT_OBJECT_0) {
2846 if (w->func[i])
2847 w->func[i](w->opaque[i]);
2848 } else if (ret2 == WAIT_TIMEOUT) {
2849 } else {
2850 err = GetLastError();
2851 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2852 }
2853 }
2854 } else if (ret == WAIT_TIMEOUT) {
2855 } else {
2856 err = GetLastError();
2857 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2858 }
2859 }
2860
2861 *timeout = 0;
2862}
2863#else
2864static void host_main_loop_wait(int *timeout)
2865{
2866}
2867#endif
2868
2869void main_loop_wait(int timeout)
2870{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002871 fd_set rfds, wfds, xfds;
2872 int ret, nfds;
2873 struct timeval tv;
2874
2875 qemu_bh_update_timeout(&timeout);
2876
2877 host_main_loop_wait(&timeout);
2878
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002879
2880 tv.tv_sec = timeout / 1000;
2881 tv.tv_usec = (timeout % 1000) * 1000;
2882
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002883 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002884
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002885 /* XXX: separate device handlers from system ones */
2886 nfds = -1;
2887 FD_ZERO(&rfds);
2888 FD_ZERO(&wfds);
2889 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002890 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002891 if (slirp_is_inited()) {
2892 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2893 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002894
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002895 qemu_mutex_unlock_iothread();
2896 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2897 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002898 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002899 if (slirp_is_inited()) {
2900 if (ret < 0) {
2901 FD_ZERO(&rfds);
2902 FD_ZERO(&wfds);
2903 FD_ZERO(&xfds);
2904 }
2905 slirp_select_poll(&rfds, &wfds, &xfds);
2906 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002907 charpipe_poll();
2908
David Turner6a9ef172010-09-09 22:54:36 +02002909 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002910
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002911 /* Check bottom-halves last in case any of the earlier events triggered
2912 them. */
2913 qemu_bh_poll();
2914
2915}
2916
2917static int qemu_cpu_exec(CPUState *env)
2918{
2919 int ret;
2920#ifdef CONFIG_PROFILER
2921 int64_t ti;
2922#endif
2923
2924#ifdef CONFIG_PROFILER
2925 ti = profile_getclock();
2926#endif
2927 if (use_icount) {
2928 int64_t count;
2929 int decr;
2930 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
2931 env->icount_decr.u16.low = 0;
2932 env->icount_extra = 0;
David 'Digit' Turner317c9d52011-05-10 06:38:21 +02002933 count = qemu_next_icount_deadline();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002934 count = (count + (1 << icount_time_shift) - 1)
2935 >> icount_time_shift;
2936 qemu_icount += count;
2937 decr = (count > 0xffff) ? 0xffff : count;
2938 count -= decr;
2939 env->icount_decr.u16.low = decr;
2940 env->icount_extra = count;
2941 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07002942#ifdef CONFIG_TRACE
2943 if (tbflush_requested) {
2944 tbflush_requested = 0;
2945 tb_flush(env);
2946 return EXCP_INTERRUPT;
2947 }
2948#endif
2949
2950
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002951 ret = cpu_exec(env);
2952#ifdef CONFIG_PROFILER
2953 qemu_time += profile_getclock() - ti;
2954#endif
2955 if (use_icount) {
2956 /* Fold pending instructions back into the
2957 instruction counter, and clear the interrupt flag. */
2958 qemu_icount -= (env->icount_decr.u16.low
2959 + env->icount_extra);
2960 env->icount_decr.u32 = 0;
2961 env->icount_extra = 0;
2962 }
2963 return ret;
2964}
2965
2966static void tcg_cpu_exec(void)
2967{
2968 int ret = 0;
2969
2970 if (next_cpu == NULL)
2971 next_cpu = first_cpu;
2972 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
2973 CPUState *env = cur_cpu = next_cpu;
2974
2975 if (!vm_running)
2976 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02002977 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002978 break;
2979 }
2980 if (cpu_can_run(env))
2981 ret = qemu_cpu_exec(env);
2982 if (ret == EXCP_DEBUG) {
2983 gdb_set_stop_cpu(env);
2984 debug_requested = 1;
2985 break;
2986 }
2987 }
2988}
2989
2990static int cpu_has_work(CPUState *env)
2991{
2992 if (env->stop)
2993 return 1;
2994 if (env->stopped)
2995 return 0;
2996 if (!env->halted)
2997 return 1;
2998 if (qemu_cpu_has_work(env))
2999 return 1;
3000 return 0;
3001}
3002
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003003int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003004{
3005 CPUState *env;
3006
3007 for (env = first_cpu; env != NULL; env = env->next_cpu)
3008 if (cpu_has_work(env))
3009 return 1;
3010 return 0;
3011}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003012
3013static int vm_can_run(void)
3014{
3015 if (powerdown_requested)
3016 return 0;
3017 if (reset_requested)
3018 return 0;
3019 if (shutdown_requested)
3020 return 0;
3021 if (debug_requested)
3022 return 0;
3023 return 1;
3024}
3025
3026static void main_loop(void)
3027{
3028 int r;
3029
3030#ifdef CONFIG_IOTHREAD
3031 qemu_system_ready = 1;
3032 qemu_cond_broadcast(&qemu_system_cond);
3033#endif
3034
3035 for (;;) {
3036 do {
3037#ifdef CONFIG_PROFILER
3038 int64_t ti;
3039#endif
3040#ifndef CONFIG_IOTHREAD
3041 tcg_cpu_exec();
3042#endif
3043#ifdef CONFIG_PROFILER
3044 ti = profile_getclock();
3045#endif
3046 main_loop_wait(qemu_calculate_timeout());
3047#ifdef CONFIG_PROFILER
3048 dev_time += profile_getclock() - ti;
3049#endif
rich canningsd952f282011-03-01 15:40:09 -08003050
3051 if (rotate_logs_requested) {
3052 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
3053 dns_log_filename);
3054 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
3055 drop_log_filename);
3056 slirp_dns_log_fd(new_dns_log_fd);
3057 slirp_drop_log_fd(new_drop_log_fd);
3058 reset_rotate_qemu_logs_request();
3059 }
3060
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003061 } while (vm_can_run());
3062
3063 if (qemu_debug_requested())
3064 vm_stop(EXCP_DEBUG);
3065 if (qemu_shutdown_requested()) {
3066 if (no_shutdown) {
3067 vm_stop(0);
3068 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003069 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003070 if (savevm_on_exit != NULL) {
3071 do_savevm(cur_mon, savevm_on_exit);
3072 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003073 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003074 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003075 }
3076 if (qemu_reset_requested()) {
3077 pause_all_vcpus();
3078 qemu_system_reset();
3079 resume_all_vcpus();
3080 }
3081 if (qemu_powerdown_requested())
3082 qemu_system_powerdown();
3083 if ((r = qemu_vmstop_requested()))
3084 vm_stop(r);
3085 }
3086 pause_all_vcpus();
3087}
3088
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003089void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003090{
3091 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3092}
3093
3094void qemu_help(int exitcode)
3095{
3096 version();
3097 printf("usage: %s [options] [disk_image]\n"
3098 "\n"
3099 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3100 "\n"
3101#define DEF(option, opt_arg, opt_enum, opt_help) \
3102 opt_help
3103#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003104#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003105#undef DEF
3106#undef DEFHEADING
3107#undef GEN_DOCS
3108 "\n"
3109 "During emulation, the following keys are useful:\n"
3110 "ctrl-alt-f toggle full screen\n"
3111 "ctrl-alt-n switch to virtual console 'n'\n"
3112 "ctrl-alt toggle mouse and keyboard grab\n"
3113 "\n"
3114 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3115 ,
3116 "qemu",
3117 DEFAULT_RAM_SIZE,
3118#ifndef _WIN32
3119 DEFAULT_NETWORK_SCRIPT,
3120 DEFAULT_NETWORK_DOWN_SCRIPT,
3121#endif
3122 DEFAULT_GDBSTUB_PORT,
3123 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003124 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003125}
3126
3127#define HAS_ARG 0x0001
3128
3129enum {
3130#define DEF(option, opt_arg, opt_enum, opt_help) \
3131 opt_enum,
3132#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003133#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003134#undef DEF
3135#undef DEFHEADING
3136#undef GEN_DOCS
3137};
3138
3139typedef struct QEMUOption {
3140 const char *name;
3141 int flags;
3142 int index;
3143} QEMUOption;
3144
3145static const QEMUOption qemu_options[] = {
3146 { "h", 0, QEMU_OPTION_h },
3147#define DEF(option, opt_arg, opt_enum, opt_help) \
3148 { option, opt_arg, opt_enum },
3149#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003150#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003151#undef DEF
3152#undef DEFHEADING
3153#undef GEN_DOCS
3154 { NULL, 0, 0 },
3155};
3156
3157#ifdef HAS_AUDIO
3158struct soundhw soundhw[] = {
3159#ifdef HAS_AUDIO_CHOICE
3160#if defined(TARGET_I386) || defined(TARGET_MIPS)
3161 {
3162 "pcspk",
3163 "PC speaker",
3164 0,
3165 1,
3166 { .init_isa = pcspk_audio_init }
3167 },
3168#endif
3169
3170#ifdef CONFIG_SB16
3171 {
3172 "sb16",
3173 "Creative Sound Blaster 16",
3174 0,
3175 1,
3176 { .init_isa = SB16_init }
3177 },
3178#endif
3179
3180#ifdef CONFIG_CS4231A
3181 {
3182 "cs4231a",
3183 "CS4231A",
3184 0,
3185 1,
3186 { .init_isa = cs4231a_init }
3187 },
3188#endif
3189
3190#ifdef CONFIG_ADLIB
3191 {
3192 "adlib",
3193#ifdef HAS_YMF262
3194 "Yamaha YMF262 (OPL3)",
3195#else
3196 "Yamaha YM3812 (OPL2)",
3197#endif
3198 0,
3199 1,
3200 { .init_isa = Adlib_init }
3201 },
3202#endif
3203
3204#ifdef CONFIG_GUS
3205 {
3206 "gus",
3207 "Gravis Ultrasound GF1",
3208 0,
3209 1,
3210 { .init_isa = GUS_init }
3211 },
3212#endif
3213
3214#ifdef CONFIG_AC97
3215 {
3216 "ac97",
3217 "Intel 82801AA AC97 Audio",
3218 0,
3219 0,
3220 { .init_pci = ac97_init }
3221 },
3222#endif
3223
3224#ifdef CONFIG_ES1370
3225 {
3226 "es1370",
3227 "ENSONIQ AudioPCI ES1370",
3228 0,
3229 0,
3230 { .init_pci = es1370_init }
3231 },
3232#endif
3233
3234#endif /* HAS_AUDIO_CHOICE */
3235
3236 { NULL, NULL, 0, 0, { NULL } }
3237};
3238
3239static void select_soundhw (const char *optarg)
3240{
3241 struct soundhw *c;
3242
3243 if (*optarg == '?') {
3244 show_valid_cards:
3245
3246 printf ("Valid sound card names (comma separated):\n");
3247 for (c = soundhw; c->name; ++c) {
3248 printf ("%-11s %s\n", c->name, c->descr);
3249 }
3250 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003251 if (*optarg != '?') {
3252 PANIC("Unknown sound card name: %s", optarg);
3253 } else {
3254 QEMU_EXIT(0);
3255 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003256 }
3257 else {
3258 size_t l;
3259 const char *p;
3260 char *e;
3261 int bad_card = 0;
3262
3263 if (!strcmp (optarg, "all")) {
3264 for (c = soundhw; c->name; ++c) {
3265 c->enabled = 1;
3266 }
3267 return;
3268 }
3269
3270 p = optarg;
3271 while (*p) {
3272 e = strchr (p, ',');
3273 l = !e ? strlen (p) : (size_t) (e - p);
3274
3275 for (c = soundhw; c->name; ++c) {
3276 if (!strncmp (c->name, p, l)) {
3277 c->enabled = 1;
3278 break;
3279 }
3280 }
3281
3282 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003283#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003284 if (l > 80) {
3285 fprintf (stderr,
3286 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003287 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003288 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3289 (int) l, p);
3290 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003291#endif // !CONFIG_ANDROID
3292 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003293 }
3294 p += l + (e != NULL);
3295 }
3296
3297 if (bad_card)
3298 goto show_valid_cards;
3299 }
3300}
3301#endif
3302
3303static void select_vgahw (const char *p)
3304{
3305 const char *opts;
3306
3307 cirrus_vga_enabled = 0;
3308 std_vga_enabled = 0;
3309 vmsvga_enabled = 0;
3310 xenfb_enabled = 0;
3311 if (strstart(p, "std", &opts)) {
3312 std_vga_enabled = 1;
3313 } else if (strstart(p, "cirrus", &opts)) {
3314 cirrus_vga_enabled = 1;
3315 } else if (strstart(p, "vmware", &opts)) {
3316 vmsvga_enabled = 1;
3317 } else if (strstart(p, "xenfb", &opts)) {
3318 xenfb_enabled = 1;
3319 } else if (!strstart(p, "none", &opts)) {
3320 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003321 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003322 }
3323 while (*opts) {
3324 const char *nextopt;
3325
3326 if (strstart(opts, ",retrace=", &nextopt)) {
3327 opts = nextopt;
3328 if (strstart(opts, "dumb", &nextopt))
3329 vga_retrace_method = VGA_RETRACE_DUMB;
3330 else if (strstart(opts, "precise", &nextopt))
3331 vga_retrace_method = VGA_RETRACE_PRECISE;
3332 else goto invalid_vga;
3333 } else goto invalid_vga;
3334 opts = nextopt;
3335 }
3336}
3337
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003338int qemu_uuid_parse(const char *str, uint8_t *uuid)
3339{
3340 int ret;
3341
3342 if(strlen(str) != 36)
3343 return -1;
3344
3345 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3346 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3347 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3348
3349 if(ret != 16)
3350 return -1;
3351
3352#ifdef TARGET_I386
3353 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3354#endif
3355
3356 return 0;
3357}
3358
3359#define MAX_NET_CLIENTS 32
3360
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003361#ifdef _WIN32
3362/* Look for support files in the same directory as the executable. */
3363static char *find_datadir(const char *argv0)
3364{
3365 char *p;
3366 char buf[MAX_PATH];
3367 DWORD len;
3368
3369 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3370 if (len == 0) {
3371 return NULL;
3372 }
3373
3374 buf[len] = 0;
3375 p = buf + len - 1;
3376 while (p != buf && *p != '\\')
3377 p--;
3378 *p = 0;
3379 if (access(buf, R_OK) == 0) {
3380 return qemu_strdup(buf);
3381 }
3382 return NULL;
3383}
3384#else /* !_WIN32 */
3385
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003386/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003387static char *find_datadir(const char *argv0)
3388{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003389 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003390 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003391
3392#if defined(__linux__)
3393 {
3394 int len;
3395 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3396 if (len > 0) {
3397 buf[len] = 0;
3398 p = buf;
3399 }
3400 }
3401#elif defined(__FreeBSD__)
3402 {
3403 int len;
3404 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3405 if (len > 0) {
3406 buf[len] = 0;
3407 p = buf;
3408 }
3409 }
3410#endif
3411 /* If we don't have any way of figuring out the actual executable
3412 location then try argv[0]. */
3413 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003414 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003415 if (!p) {
3416 return NULL;
3417 }
3418 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003419
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003420 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003421}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003422#endif
3423
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003424static char*
3425qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
3426{
3427 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3428 char* buf = qemu_mallocz(len);
3429
3430 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3431 VERBOSE_PRINT(init," trying to find: %s\n", buf);
3432 if (access(buf, R_OK)) {
3433 qemu_free(buf);
3434 return NULL;
3435 }
3436 return buf;
3437}
3438
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003439char *qemu_find_file(int type, const char *name)
3440{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003441 const char *subdir;
3442 char *buf;
3443
3444 /* If name contains path separators then try it as a straight path. */
3445 if ((strchr(name, '/') || strchr(name, '\\'))
3446 && access(name, R_OK) == 0) {
3447 return strdup(name);
3448 }
3449 switch (type) {
3450 case QEMU_FILE_TYPE_BIOS:
3451 subdir = "";
3452 break;
3453 case QEMU_FILE_TYPE_KEYMAP:
3454 subdir = "keymaps/";
3455 break;
3456 default:
3457 abort();
3458 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003459 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
3460#ifdef CONFIG_ANDROID
3461 if (type == QEMU_FILE_TYPE_BIOS) {
3462 /* This case corresponds to the emulator being used as part of an
3463 * SDK installation. NOTE: data_dir is really $bindir. */
3464 if (buf == NULL)
3465 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
3466 /* This case corresponds to platform builds. */
3467 if (buf == NULL)
3468 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
3469 /* Finally, try this for standalone builds under external/qemu */
3470 if (buf == NULL)
3471 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003472 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003473#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003474 return buf;
3475}
3476
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003477static int
3478add_dns_server( const char* server_name )
3479{
3480 SockAddress addr;
3481
3482 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3483 fprintf(stdout,
3484 "### WARNING: can't resolve DNS server name '%s'\n",
3485 server_name );
3486 return -1;
3487 }
3488
3489 fprintf(stderr,
3490 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3491
3492 if ( slirp_add_dns_server( &addr ) < 0 ) {
3493 fprintf(stderr,
3494 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3495 return -1;
3496 }
3497 return 0;
3498}
3499
rich cannings7339b552011-02-16 13:43:44 -08003500/* Parses an integer
3501 * Pararm:
3502 * str String containing a number to be parsed.
3503 * result Passes the parsed integer in this argument
3504 * returns 0 if ok, -1 if failed
3505 */
3506int
3507parse_int(const char *str, int *result)
3508{
3509 char* r;
3510 *result = strtol(str, &r, 0);
3511 if (r == NULL || *r != '\0')
3512 return -1;
3513
3514 return 0;
3515}
3516
rich canningsd952f282011-03-01 15:40:09 -08003517#ifndef _WIN32
3518/*
3519 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3520 */
3521void init_qemu_clear_logs_sig() {
3522 struct sigaction act;
3523 sigfillset(&act.sa_mask);
3524 act.sa_flags = 0;
3525 act.sa_handler = rotate_qemu_logs_handler;
3526 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3527 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3528 exit(-1);
3529 }
3530}
3531#endif
3532
3533
rich cannings7339b552011-02-16 13:43:44 -08003534
3535/* parses a null-terminated string specifying a network port (e.g., "80") or
3536 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3537 * are the same. Returns 0 on success, -1 on error. */
3538
3539int parse_port_range(const char *str, unsigned short *lport,
3540 unsigned short *hport) {
3541
3542 unsigned int low = 0, high = 0;
3543 char *p, *arg = strdup(str);
3544
3545 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3546 p = arg + 1; /* skip '[' */
3547 low = atoi(strtok(p, "-"));
3548 high = atoi(strtok(NULL, "-"));
3549 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3550 *lport = low;
3551 *hport = high;
3552 }
3553 }
3554 else {
3555 low = atoi(arg);
3556 if ((0 < low) && (low < 65535)) {
3557 *lport = low;
3558 *hport = low;
3559 }
3560 }
3561 free(arg);
3562 if (low != 0)
3563 return 0;
3564 return -1;
3565}
3566
3567/*
3568 * Implements the generic port forwarding option
3569 */
3570void
3571net_slirp_forward(const char *optarg)
3572{
3573 /*
3574 * we expect the following format:
3575 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3576 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3577 */
3578 char *argument = strdup(optarg), *p = argument;
3579 char *dst_net, *dst_mask, *dst_port;
3580 char *redirect_ip, *redirect_port;
3581 uint32_t dnet, dmask, rip;
3582 unsigned short dlport, dhport, rport;
3583
3584
3585 dst_net = strtok(p, ":");
3586 dst_mask = strtok(NULL, ":");
3587 dst_port = strtok(NULL, ":");
3588 redirect_ip = strtok(NULL, ":");
3589 redirect_port = strtok(NULL, ":");
3590
3591 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3592 redirect_ip == NULL || redirect_port == NULL) {
3593 fprintf(stderr,
3594 "Invalid argument for -net-forward, we expect "
3595 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3596 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3597 ":redirect_ip:redirect_port: %s\n",
3598 optarg);
3599 exit(1);
3600 }
3601
3602 /* inet_strtoip converts dotted address to host byte order */
3603 if (inet_strtoip(dst_net, &dnet) == -1) {
3604 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3605 exit(1);
3606 }
3607 if (inet_strtoip(dst_mask, &dmask) == -1) {
3608 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3609 exit(1);
3610 }
3611 if (inet_strtoip(redirect_ip, &rip) == -1) {
3612 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3613 exit(1);
3614 }
3615
3616 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3617 fprintf(stderr, "Invalid destination port or port range\n");
3618 exit(1);
3619 }
3620
3621 rport = atoi(redirect_port);
3622 if (!rport) {
3623 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3624 exit(1);
3625 }
3626
3627 dnet &= dmask;
3628
3629 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3630 rip, rport);
3631
3632 free(argument);
3633}
3634
3635
3636/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3637 * entry in the allows list */
3638void
3639slirp_allow(const char *optarg, u_int8_t proto)
3640{
3641 /*
3642 * we expect the following format:
3643 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3644 */
3645 char *argument = strdup(optarg), *p = argument;
3646 char *dst_ip_str, *dst_port_str;
3647 uint32_t dst_ip;
3648 unsigned short dst_lport, dst_hport;
3649
3650 dst_ip_str = strtok(p, ":");
3651 dst_port_str = strtok(NULL, ":");
3652
3653 if (dst_ip_str == NULL || dst_port_str == NULL) {
3654 fprintf(stderr,
3655 "Invalid argument %s for -allow. We expect "
3656 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3657 optarg);
3658 exit(1);
3659 }
3660
3661 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3662 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3663 exit(1);
3664 }
3665 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3666 fprintf(stderr, "Invalid destination port or port range\n");
3667 exit(1);
3668 }
3669
3670 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3671
3672 free(argument);
3673}
3674
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003675/* Add a serial device at a given location in the emulated hardware table.
3676 * On failure, this function aborts the program with an error message.
3677 */
3678static void
3679serial_hds_add_at(int index, const char* devname)
3680{
3681 char label[32];
3682
3683 if (!devname || !strcmp(devname,"none"))
3684 return;
3685
3686 if (index >= MAX_SERIAL_PORTS) {
3687 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3688 devname, index, MAX_SERIAL_PORTS);
3689 }
3690 if (serial_hds[index] != NULL) {
3691 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3692 devname, index);
3693 }
3694 snprintf(label, sizeof(label), "serial%d", index);
3695 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3696 if (!serial_hds[index]) {
3697 PANIC("qemu: could not open serial device '%s'", devname);
3698 }
3699}
3700
3701
3702/* Find a free slot in the emulated serial device table, and register
3703 * it. Return the allocated table index.
3704 */
3705static int
3706serial_hds_add(const char* devname)
3707{
3708 int index;
3709
3710 /* Find first free slot */
3711 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3712 if (serial_hds[index] == NULL) {
3713 serial_hds_add_at(index, devname);
3714 return index;
3715 }
3716 }
3717
3718 PANIC("qemu: too many serial devices registered (%d)", index);
3719 return -1; /* shouldn't happen */
3720}
3721
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003722int main(int argc, char **argv, char **envp)
3723{
3724 const char *gdbstub_dev = NULL;
3725 uint32_t boot_devices_bitmap = 0;
3726 int i;
3727 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003728 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003729 const char *initrd_filename;
3730 const char *kernel_filename, *kernel_cmdline;
3731 const char *boot_devices = "";
3732 DisplayState *ds;
3733 DisplayChangeListener *dcl;
3734 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003735 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003736 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003737 const char *net_clients[MAX_NET_CLIENTS];
3738 int nb_net_clients;
3739 const char *bt_opts[MAX_BT_CMDLINE];
3740 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003741 int optind;
3742 const char *r, *optarg;
3743 CharDriverState *monitor_hd = NULL;
3744 const char *monitor_device;
3745 const char *serial_devices[MAX_SERIAL_PORTS];
3746 int serial_device_index;
3747 const char *parallel_devices[MAX_PARALLEL_PORTS];
3748 int parallel_device_index;
3749 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3750 int virtio_console_index;
3751 const char *loadvm = NULL;
3752 QEMUMachine *machine;
3753 const char *cpu_model;
3754 const char *usb_devices[MAX_USB_CMDLINE];
3755 int usb_devices_index;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003756 int tb_size;
3757 const char *pid_file = NULL;
3758 const char *incoming = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003759 CPUState *env;
3760 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003761 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003762 STRALLOC_DEFINE(kernel_params);
3763 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003764 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003765
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003766 /* Initialize sockets before anything else, so we can properly report
3767 * initialization failures back to the UI. */
3768#ifdef _WIN32
3769 socket_init();
3770#endif
3771
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003772 init_clocks();
3773
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003774 qemu_cache_utils_init(envp);
3775
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003776 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02003777 os_setup_early_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003778
3779 module_call_init(MODULE_INIT_MACHINE);
3780 machine = find_default_machine();
3781 cpu_model = NULL;
3782 initrd_filename = NULL;
3783 ram_size = 0;
3784 snapshot = 0;
3785 kernel_filename = NULL;
3786 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003787
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003788 cyls = heads = secs = 0;
3789 translation = BIOS_ATA_TRANSLATION_AUTO;
3790 monitor_device = "vc:80Cx24C";
3791
3792 serial_devices[0] = "vc:80Cx24C";
3793 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3794 serial_devices[i] = NULL;
3795 serial_device_index = 0;
3796
3797 parallel_devices[0] = "vc:80Cx24C";
3798 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3799 parallel_devices[i] = NULL;
3800 parallel_device_index = 0;
3801
3802 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3803 virtio_consoles[i] = NULL;
3804 virtio_console_index = 0;
3805
3806 for (i = 0; i < MAX_NODES; i++) {
3807 node_mem[i] = 0;
3808 node_cpumask[i] = 0;
3809 }
3810
3811 usb_devices_index = 0;
3812
3813 nb_net_clients = 0;
3814 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003815#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003816 nb_drives = 0;
3817 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003818#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003819 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003820
3821 nb_nics = 0;
3822
3823 tb_size = 0;
3824 autostart= 1;
3825
3826 register_watchdogs();
3827
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003828 /* Initialize boot properties. */
3829 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02003830 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003831 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003832
David 'Digit' Turner36597752011-05-20 01:18:01 +02003833#ifdef CONFIG_KVM
3834 /* By default, force auto-detection for kvm */
3835 kvm_allowed = -1;
3836#endif
3837
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003838 optind = 1;
3839 for(;;) {
3840 if (optind >= argc)
3841 break;
3842 r = argv[optind];
3843 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003844 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003845 } else {
3846 const QEMUOption *popt;
3847
3848 optind++;
3849 /* Treat --foo the same as -foo. */
3850 if (r[1] == '-')
3851 r++;
3852 popt = qemu_options;
3853 for(;;) {
3854 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003855 PANIC("%s: invalid option -- '%s'",
3856 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003857 }
3858 if (!strcmp(popt->name, r + 1))
3859 break;
3860 popt++;
3861 }
3862 if (popt->flags & HAS_ARG) {
3863 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003864 PANIC("%s: option '%s' requires an argument",
3865 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003866 }
3867 optarg = argv[optind++];
3868 } else {
3869 optarg = NULL;
3870 }
3871
3872 switch(popt->index) {
3873 case QEMU_OPTION_M:
3874 machine = find_machine(optarg);
3875 if (!machine) {
3876 QEMUMachine *m;
3877 printf("Supported machines are:\n");
3878 for(m = first_machine; m != NULL; m = m->next) {
3879 printf("%-10s %s%s\n",
3880 m->name, m->desc,
3881 m->is_default ? " (default)" : "");
3882 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003883 if (*optarg != '?') {
3884 PANIC("Invalid machine parameter: %s",
3885 optarg);
3886 } else {
3887 QEMU_EXIT(0);
3888 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003889 }
3890 break;
3891 case QEMU_OPTION_cpu:
3892 /* hw initialization will check this */
3893 if (*optarg == '?') {
3894/* XXX: implement xxx_cpu_list for targets that still miss it */
3895#if defined(cpu_list)
3896 cpu_list(stdout, &fprintf);
3897#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003898 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003899 } else {
3900 cpu_model = optarg;
3901 }
3902 break;
3903 case QEMU_OPTION_initrd:
3904 initrd_filename = optarg;
3905 break;
3906 case QEMU_OPTION_hda:
3907 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003908 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003909 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003910 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003911 ",cyls=%d,heads=%d,secs=%d%s",
3912 0, cyls, heads, secs,
3913 translation == BIOS_ATA_TRANSLATION_LBA ?
3914 ",trans=lba" :
3915 translation == BIOS_ATA_TRANSLATION_NONE ?
3916 ",trans=none" : "");
3917 break;
3918 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003919 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
3920 break;
3921
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003922 case QEMU_OPTION_hdc:
3923 case QEMU_OPTION_hdd:
3924 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
3925 break;
3926 case QEMU_OPTION_drive:
3927 drive_add(NULL, "%s", optarg);
3928 break;
3929 case QEMU_OPTION_mtdblock:
3930 drive_add(optarg, MTD_ALIAS);
3931 break;
3932 case QEMU_OPTION_sd:
3933 drive_add(optarg, SD_ALIAS);
3934 break;
3935 case QEMU_OPTION_pflash:
3936 drive_add(optarg, PFLASH_ALIAS);
3937 break;
3938 case QEMU_OPTION_snapshot:
3939 snapshot = 1;
3940 break;
3941 case QEMU_OPTION_hdachs:
3942 {
3943 const char *p;
3944 p = optarg;
3945 cyls = strtol(p, (char **)&p, 0);
3946 if (cyls < 1 || cyls > 16383)
3947 goto chs_fail;
3948 if (*p != ',')
3949 goto chs_fail;
3950 p++;
3951 heads = strtol(p, (char **)&p, 0);
3952 if (heads < 1 || heads > 16)
3953 goto chs_fail;
3954 if (*p != ',')
3955 goto chs_fail;
3956 p++;
3957 secs = strtol(p, (char **)&p, 0);
3958 if (secs < 1 || secs > 63)
3959 goto chs_fail;
3960 if (*p == ',') {
3961 p++;
3962 if (!strcmp(p, "none"))
3963 translation = BIOS_ATA_TRANSLATION_NONE;
3964 else if (!strcmp(p, "lba"))
3965 translation = BIOS_ATA_TRANSLATION_LBA;
3966 else if (!strcmp(p, "auto"))
3967 translation = BIOS_ATA_TRANSLATION_AUTO;
3968 else
3969 goto chs_fail;
3970 } else if (*p != '\0') {
3971 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003972 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003973 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003974 if (hda_opts != NULL) {
3975 char num[16];
3976 snprintf(num, sizeof(num), "%d", cyls);
3977 qemu_opt_set(hda_opts, "cyls", num);
3978 snprintf(num, sizeof(num), "%d", heads);
3979 qemu_opt_set(hda_opts, "heads", num);
3980 snprintf(num, sizeof(num), "%d", secs);
3981 qemu_opt_set(hda_opts, "secs", num);
3982 if (translation == BIOS_ATA_TRANSLATION_LBA)
3983 qemu_opt_set(hda_opts, "trans", "lba");
3984 if (translation == BIOS_ATA_TRANSLATION_NONE)
3985 qemu_opt_set(hda_opts, "trans", "none");
3986 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003987 }
3988 break;
3989 case QEMU_OPTION_numa:
3990 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003991 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003992 }
3993 numa_add(optarg);
3994 break;
3995 case QEMU_OPTION_nographic:
3996 display_type = DT_NOGRAPHIC;
3997 break;
3998#ifdef CONFIG_CURSES
3999 case QEMU_OPTION_curses:
4000 display_type = DT_CURSES;
4001 break;
4002#endif
4003 case QEMU_OPTION_portrait:
4004 graphic_rotate = 1;
4005 break;
4006 case QEMU_OPTION_kernel:
4007 kernel_filename = optarg;
4008 break;
4009 case QEMU_OPTION_append:
4010 kernel_cmdline = optarg;
4011 break;
4012 case QEMU_OPTION_cdrom:
4013 drive_add(optarg, CDROM_ALIAS);
4014 break;
4015 case QEMU_OPTION_boot:
4016 boot_devices = optarg;
4017 /* We just do some generic consistency checks */
4018 {
4019 /* Could easily be extended to 64 devices if needed */
4020 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004021
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004022 boot_devices_bitmap = 0;
4023 for (p = boot_devices; *p != '\0'; p++) {
4024 /* Allowed boot devices are:
4025 * a b : floppy disk drives
4026 * c ... f : IDE disk drives
4027 * g ... m : machine implementation dependant drives
4028 * n ... p : network devices
4029 * It's up to each machine implementation to check
4030 * if the given boot devices match the actual hardware
4031 * implementation and firmware features.
4032 */
4033 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004034 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004035 }
4036 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004037 PANIC(
4038 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004039 }
4040 boot_devices_bitmap |= 1 << (*p - 'a');
4041 }
4042 }
4043 break;
4044 case QEMU_OPTION_fda:
4045 case QEMU_OPTION_fdb:
4046 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4047 break;
4048#ifdef TARGET_I386
4049 case QEMU_OPTION_no_fd_bootchk:
4050 fd_bootchk = 0;
4051 break;
4052#endif
4053 case QEMU_OPTION_net:
4054 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004055 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004056 }
4057 net_clients[nb_net_clients] = optarg;
4058 nb_net_clients++;
4059 break;
4060#ifdef CONFIG_SLIRP
4061 case QEMU_OPTION_tftp:
4062 tftp_prefix = optarg;
4063 break;
4064 case QEMU_OPTION_bootp:
4065 bootp_filename = optarg;
4066 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004067 case QEMU_OPTION_redir:
4068 net_slirp_redir(NULL, optarg, NULL);
4069 break;
4070#endif
4071 case QEMU_OPTION_bt:
4072 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004073 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004074 }
4075 bt_opts[nb_bt_opts++] = optarg;
4076 break;
4077#ifdef HAS_AUDIO
4078 case QEMU_OPTION_audio_help:
4079 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004080 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004081 break;
4082 case QEMU_OPTION_soundhw:
4083 select_soundhw (optarg);
4084 break;
4085#endif
4086 case QEMU_OPTION_h:
4087 qemu_help(0);
4088 break;
4089 case QEMU_OPTION_version:
4090 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004091 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004092 break;
4093 case QEMU_OPTION_m: {
4094 uint64_t value;
4095 char *ptr;
4096
4097 value = strtoul(optarg, &ptr, 10);
4098 switch (*ptr) {
4099 case 0: case 'M': case 'm':
4100 value <<= 20;
4101 break;
4102 case 'G': case 'g':
4103 value <<= 30;
4104 break;
4105 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004106 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004107 }
4108
4109 /* On 32-bit hosts, QEMU is limited by virtual address space */
4110 if (value > (2047 << 20)
4111#ifndef CONFIG_KQEMU
4112 && HOST_LONG_BITS == 32
4113#endif
4114 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004115 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004116 }
4117 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004118 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004119 }
4120 ram_size = value;
4121 break;
4122 }
4123 case QEMU_OPTION_d:
4124 {
4125 int mask;
4126 const CPULogItem *item;
4127
4128 mask = cpu_str_to_log_mask(optarg);
4129 if (!mask) {
4130 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004131 for(item = cpu_log_items; item->mask != 0; item++) {
4132 printf("%-10s %s\n", item->name, item->help);
4133 }
4134 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004135 }
4136 cpu_set_log(mask);
4137 }
4138 break;
4139 case QEMU_OPTION_s:
4140 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4141 break;
4142 case QEMU_OPTION_gdb:
4143 gdbstub_dev = optarg;
4144 break;
4145 case QEMU_OPTION_L:
4146 data_dir = optarg;
4147 break;
4148 case QEMU_OPTION_bios:
4149 bios_name = optarg;
4150 break;
4151 case QEMU_OPTION_singlestep:
4152 singlestep = 1;
4153 break;
4154 case QEMU_OPTION_S:
4155#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004156 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004157#endif
4158 autostart = 0;
4159 break;
4160#ifndef _WIN32
4161 case QEMU_OPTION_k:
4162 keyboard_layout = optarg;
4163 break;
4164#endif
4165 case QEMU_OPTION_localtime:
4166 rtc_utc = 0;
4167 break;
4168 case QEMU_OPTION_vga:
4169 select_vgahw (optarg);
4170 break;
4171#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4172 case QEMU_OPTION_g:
4173 {
4174 const char *p;
4175 int w, h, depth;
4176 p = optarg;
4177 w = strtol(p, (char **)&p, 10);
4178 if (w <= 0) {
4179 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004180 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004181 }
4182 if (*p != 'x')
4183 goto graphic_error;
4184 p++;
4185 h = strtol(p, (char **)&p, 10);
4186 if (h <= 0)
4187 goto graphic_error;
4188 if (*p == 'x') {
4189 p++;
4190 depth = strtol(p, (char **)&p, 10);
4191 if (depth != 8 && depth != 15 && depth != 16 &&
4192 depth != 24 && depth != 32)
4193 goto graphic_error;
4194 } else if (*p == '\0') {
4195 depth = graphic_depth;
4196 } else {
4197 goto graphic_error;
4198 }
4199
4200 graphic_width = w;
4201 graphic_height = h;
4202 graphic_depth = depth;
4203 }
4204 break;
4205#endif
4206 case QEMU_OPTION_echr:
4207 {
4208 char *r;
4209 term_escape_char = strtol(optarg, &r, 0);
4210 if (r == optarg)
4211 printf("Bad argument to echr\n");
4212 break;
4213 }
4214 case QEMU_OPTION_monitor:
4215 monitor_device = optarg;
4216 break;
4217 case QEMU_OPTION_serial:
4218 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004219 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004220 }
4221 serial_devices[serial_device_index] = optarg;
4222 serial_device_index++;
4223 break;
4224 case QEMU_OPTION_watchdog:
4225 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004226 if (i > 0) {
4227 if (i == 1) {
4228 PANIC("Invalid watchdog parameter: %s",
4229 optarg);
4230 } else {
4231 QEMU_EXIT(0);
4232 }
4233 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004234 break;
4235 case QEMU_OPTION_watchdog_action:
4236 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004237 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004238 }
4239 break;
4240 case QEMU_OPTION_virtiocon:
4241 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004242 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004243 }
4244 virtio_consoles[virtio_console_index] = optarg;
4245 virtio_console_index++;
4246 break;
4247 case QEMU_OPTION_parallel:
4248 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004249 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004250 }
4251 parallel_devices[parallel_device_index] = optarg;
4252 parallel_device_index++;
4253 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004254 case QEMU_OPTION_loadvm:
4255 loadvm = optarg;
4256 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004257 case QEMU_OPTION_savevm_on_exit:
4258 savevm_on_exit = optarg;
4259 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004260 case QEMU_OPTION_full_screen:
4261 full_screen = 1;
4262 break;
4263#ifdef CONFIG_SDL
4264 case QEMU_OPTION_no_frame:
4265 no_frame = 1;
4266 break;
4267 case QEMU_OPTION_alt_grab:
4268 alt_grab = 1;
4269 break;
4270 case QEMU_OPTION_no_quit:
4271 no_quit = 1;
4272 break;
4273 case QEMU_OPTION_sdl:
4274 display_type = DT_SDL;
4275 break;
4276#endif
4277 case QEMU_OPTION_pidfile:
4278 pid_file = optarg;
4279 break;
4280#ifdef TARGET_I386
4281 case QEMU_OPTION_win2k_hack:
4282 win2k_install_hack = 1;
4283 break;
4284 case QEMU_OPTION_rtc_td_hack:
4285 rtc_td_hack = 1;
4286 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004287#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004288 case QEMU_OPTION_acpitable:
4289 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004290 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004291 }
4292 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004293#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004294 case QEMU_OPTION_smbios:
4295 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004296 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004297 }
4298 break;
4299#endif
4300#ifdef CONFIG_KQEMU
4301 case QEMU_OPTION_no_kqemu:
4302 kqemu_allowed = 0;
4303 break;
4304 case QEMU_OPTION_kernel_kqemu:
4305 kqemu_allowed = 2;
4306 break;
4307#endif
4308#ifdef CONFIG_KVM
4309 case QEMU_OPTION_enable_kvm:
4310 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004311 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02004312 case QEMU_OPTION_disable_kvm:
4313 kvm_allowed = 0;
4314 break;
4315#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004316 case QEMU_OPTION_usb:
4317 usb_enabled = 1;
4318 break;
4319 case QEMU_OPTION_usbdevice:
4320 usb_enabled = 1;
4321 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004322 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004323 }
4324 usb_devices[usb_devices_index] = optarg;
4325 usb_devices_index++;
4326 break;
4327 case QEMU_OPTION_smp:
4328 smp_cpus = atoi(optarg);
4329 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004330 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004331 }
4332 break;
4333 case QEMU_OPTION_vnc:
4334 display_type = DT_VNC;
4335 vnc_display = optarg;
4336 break;
4337#ifdef TARGET_I386
4338 case QEMU_OPTION_no_acpi:
4339 acpi_enabled = 0;
4340 break;
4341 case QEMU_OPTION_no_hpet:
4342 no_hpet = 1;
4343 break;
4344 case QEMU_OPTION_no_virtio_balloon:
4345 no_virtio_balloon = 1;
4346 break;
4347#endif
4348 case QEMU_OPTION_no_reboot:
4349 no_reboot = 1;
4350 break;
4351 case QEMU_OPTION_no_shutdown:
4352 no_shutdown = 1;
4353 break;
4354 case QEMU_OPTION_show_cursor:
4355 cursor_hide = 0;
4356 break;
4357 case QEMU_OPTION_uuid:
4358 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004359 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004360 }
4361 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004362 case QEMU_OPTION_option_rom:
4363 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004364 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004365 }
4366 option_rom[nb_option_roms] = optarg;
4367 nb_option_roms++;
4368 break;
4369#if defined(TARGET_ARM) || defined(TARGET_M68K)
4370 case QEMU_OPTION_semihosting:
4371 semihosting_enabled = 1;
4372 break;
4373#endif
4374 case QEMU_OPTION_name:
4375 qemu_name = optarg;
4376 break;
4377#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4378 case QEMU_OPTION_prom_env:
4379 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004380 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004381 }
4382 prom_envs[nb_prom_envs] = optarg;
4383 nb_prom_envs++;
4384 break;
4385#endif
4386#ifdef TARGET_ARM
4387 case QEMU_OPTION_old_param:
4388 old_param = 1;
4389 break;
4390#endif
4391 case QEMU_OPTION_clock:
4392 configure_alarms(optarg);
4393 break;
4394 case QEMU_OPTION_startdate:
4395 {
4396 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004397 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004398 if (!strcmp(optarg, "now")) {
4399 rtc_date_offset = -1;
4400 } else {
4401 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4402 &tm.tm_year,
4403 &tm.tm_mon,
4404 &tm.tm_mday,
4405 &tm.tm_hour,
4406 &tm.tm_min,
4407 &tm.tm_sec) == 6) {
4408 /* OK */
4409 } else if (sscanf(optarg, "%d-%d-%d",
4410 &tm.tm_year,
4411 &tm.tm_mon,
4412 &tm.tm_mday) == 3) {
4413 tm.tm_hour = 0;
4414 tm.tm_min = 0;
4415 tm.tm_sec = 0;
4416 } else {
4417 goto date_fail;
4418 }
4419 tm.tm_year -= 1900;
4420 tm.tm_mon--;
4421 rtc_start_date = mktimegm(&tm);
4422 if (rtc_start_date == -1) {
4423 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004424 PANIC("Invalid date format. Valid format are:\n"
4425 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004426 }
4427 rtc_date_offset = time(NULL) - rtc_start_date;
4428 }
4429 }
4430 break;
rich cannings7339b552011-02-16 13:43:44 -08004431
4432 /* -------------------------------------------------------*/
4433 /* User mode network stack restrictions */
4434 case QEMU_OPTION_drop_udp:
4435 slirp_drop_udp();
4436 break;
4437 case QEMU_OPTION_drop_tcp:
4438 slirp_drop_tcp();
4439 break;
4440 case QEMU_OPTION_allow_tcp:
4441 slirp_allow(optarg, IPPROTO_TCP);
4442 break;
4443 case QEMU_OPTION_allow_udp:
4444 slirp_allow(optarg, IPPROTO_UDP);
4445 break;
4446 case QEMU_OPTION_drop_log:
4447 {
4448 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004449 drop_log_filename = optarg;
4450 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004451
4452 if (!drop_log_fd) {
4453 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4454 exit(1);
4455 }
4456
4457 slirp_drop_log_fd(drop_log_fd);
4458 }
4459 break;
4460
4461 case QEMU_OPTION_dns_log:
4462 {
4463 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004464 dns_log_filename = optarg;
4465 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004466
4467 if (dns_log_fd == NULL) {
4468 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4469 exit(1);
4470 }
4471
4472 slirp_dns_log_fd(dns_log_fd);
4473 }
4474 break;
4475
4476
4477 case QEMU_OPTION_max_dns_conns:
4478 {
4479 int max_dns_conns = 0;
4480 if (parse_int(optarg, &max_dns_conns)) {
4481 fprintf(stderr,
4482 "qemu: syntax: -max-dns-conns max_connections\n");
4483 exit(1);
4484 }
4485 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4486 fprintf(stderr,
4487 "Invalid arg for max dns connections: %s\n",
4488 optarg);
4489 exit(1);
4490 }
4491 slirp_set_max_dns_conns(max_dns_conns);
4492 }
4493 break;
4494
4495 case QEMU_OPTION_net_forward:
4496 net_slirp_forward(optarg);
4497 break;
4498 case QEMU_OPTION_net_forward_tcp2sink:
4499 {
4500 SockAddress saddr;
4501
4502 if (parse_host_port(&saddr, optarg)) {
4503 fprintf(stderr,
4504 "Invalid ip/port %s for "
4505 "-forward-dropped-tcp2sink. "
4506 "We expect 'sink_ip:sink_port'\n",
4507 optarg);
4508 exit(1);
4509 }
4510 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4511 saddr.u.inet.port);
4512 }
4513 break;
4514 /* -------------------------------------------------------*/
4515
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004516 case QEMU_OPTION_tb_size:
4517 tb_size = strtol(optarg, NULL, 0);
4518 if (tb_size < 0)
4519 tb_size = 0;
4520 break;
4521 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004522 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004523 break;
4524 case QEMU_OPTION_incoming:
4525 incoming = optarg;
4526 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004527#ifdef CONFIG_XEN
4528 case QEMU_OPTION_xen_domid:
4529 xen_domid = atoi(optarg);
4530 break;
4531 case QEMU_OPTION_xen_create:
4532 xen_mode = XEN_CREATE;
4533 break;
4534 case QEMU_OPTION_xen_attach:
4535 xen_mode = XEN_ATTACH;
4536 break;
4537#endif
4538
4539
4540 case QEMU_OPTION_mic:
4541 audio_input_source = (char*)optarg;
4542 break;
4543#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004544 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004545 trace_filename = optarg;
4546 tracing = 1;
4547 break;
4548#if 0
4549 case QEMU_OPTION_trace_miss:
4550 trace_cache_miss = 1;
4551 break;
4552 case QEMU_OPTION_trace_addr:
4553 trace_all_addr = 1;
4554 break;
4555#endif
4556 case QEMU_OPTION_tracing:
4557 if (strcmp(optarg, "off") == 0)
4558 tracing = 0;
4559 else if (strcmp(optarg, "on") == 0 && trace_filename)
4560 tracing = 1;
4561 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004562 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004563 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004564 }
4565 break;
4566#if 0
4567 case QEMU_OPTION_dcache_load_miss:
4568 dcache_load_miss_penalty = atoi(optarg);
4569 break;
4570 case QEMU_OPTION_dcache_store_miss:
4571 dcache_store_miss_penalty = atoi(optarg);
4572 break;
4573#endif
4574#endif
4575#ifdef CONFIG_NAND
4576 case QEMU_OPTION_nand:
4577 nand_add_dev(optarg);
4578 break;
4579#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004580 case QEMU_OPTION_android_ports:
4581 android_op_ports = (char*)optarg;
4582 break;
4583
4584 case QEMU_OPTION_android_port:
4585 android_op_port = (char*)optarg;
4586 break;
4587
4588 case QEMU_OPTION_android_report_console:
4589 android_op_report_console = (char*)optarg;
4590 break;
4591
4592 case QEMU_OPTION_http_proxy:
4593 op_http_proxy = (char*)optarg;
4594 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004595
4596 case QEMU_OPTION_charmap:
4597 op_charmap_file = (char*)optarg;
4598 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004599
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004600 case QEMU_OPTION_android_hw:
4601 android_op_hwini = (char*)optarg;
4602 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004603
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004604 case QEMU_OPTION_dns_server:
4605 android_op_dns_server = (char*)optarg;
4606 break;
4607
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004608 case QEMU_OPTION_radio:
4609 android_op_radio = (char*)optarg;
4610 break;
4611
4612 case QEMU_OPTION_gps:
4613 android_op_gps = (char*)optarg;
4614 break;
4615
4616 case QEMU_OPTION_audio:
4617 android_op_audio = (char*)optarg;
4618 break;
4619
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004620 case QEMU_OPTION_cpu_delay:
4621 android_op_cpu_delay = (char*)optarg;
4622 break;
4623
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004624 case QEMU_OPTION_show_kernel:
4625 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4626 break;
4627
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004628#ifdef CONFIG_NAND_LIMITS
4629 case QEMU_OPTION_nand_limits:
4630 android_op_nand_limits = (char*)optarg;
4631 break;
4632#endif // CONFIG_NAND_LIMITS
4633
4634 case QEMU_OPTION_netspeed:
4635 android_op_netspeed = (char*)optarg;
4636 break;
4637
4638 case QEMU_OPTION_netdelay:
4639 android_op_netdelay = (char*)optarg;
4640 break;
4641
4642 case QEMU_OPTION_netfast:
4643 android_op_netfast = 1;
4644 break;
4645
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004646 case QEMU_OPTION_tcpdump:
4647 android_op_tcpdump = (char*)optarg;
4648 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004649
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004650 case QEMU_OPTION_boot_property:
4651 boot_property_parse_option((char*)optarg);
4652 break;
4653
4654 case QEMU_OPTION_lcd_density:
4655 android_op_lcd_density = (char*)optarg;
4656 break;
4657
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004658 case QEMU_OPTION_ui_port:
4659 android_op_ui_port = (char*)optarg;
4660 break;
4661
4662 case QEMU_OPTION_ui_settings:
4663 android_op_ui_settings = (char*)optarg;
4664 break;
4665
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004666 case QEMU_OPTION_audio_test_out:
4667 android_audio_test_start_out();
4668 break;
4669
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004670 case QEMU_OPTION_android_avdname:
4671 android_op_avd_name = (char*)optarg;
4672 break;
4673
4674 case QEMU_OPTION_timezone:
4675 if (timezone_set((char*)optarg)) {
4676 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4677 (char*)optarg);
4678 }
4679 break;
4680
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004681#ifdef CONFIG_MEMCHECK
4682 case QEMU_OPTION_android_memcheck:
4683 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004684 /* This will set ro.kernel.memcheck system property
4685 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004686 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004687 break;
4688#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004689
4690 case QEMU_OPTION_snapshot_no_time_update:
4691 android_snapshot_update_time = 0;
4692 break;
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02004693 default:
4694 os_parse_cmd_args(popt->index, optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004695 }
4696 }
4697 }
4698
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004699 /* Initialize character map. */
4700 if (android_charmap_setup(op_charmap_file)) {
4701 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004702 PANIC(
4703 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004704 op_charmap_file);
4705 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004706 PANIC(
4707 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004708 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004709 }
4710
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004711 /* If no data_dir is specified then try to find it relative to the
4712 executable path. */
4713 if (!data_dir) {
4714 data_dir = find_datadir(argv[0]);
4715 }
4716 /* If all else fails use the install patch specified when building. */
4717 if (!data_dir) {
4718 data_dir = CONFIG_QEMU_SHAREDIR;
4719 }
4720
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004721 if (!android_op_hwini) {
4722 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004723 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004724 hw_ini = iniFile_newFromFile(android_op_hwini);
4725 if (hw_ini == NULL) {
4726 PANIC("Could not find %s file.", android_op_hwini);
4727 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004728
4729 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004730 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004731
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004732 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004733
4734 {
4735 int width = android_hw->hw_lcd_width;
4736 int height = android_hw->hw_lcd_height;
4737 int depth = android_hw->hw_lcd_depth;
4738
4739 /* A bit of sanity checking */
4740 if (width <= 0 || height <= 0 ||
4741 (depth != 16 && depth != 32) ||
4742 (((width|height) & 3) != 0) )
4743 {
4744 PANIC("Invalid display configuration (%d,%d,%d)",
4745 width, height, depth);
4746 }
4747 android_display_width = width;
4748 android_display_height = height;
4749 android_display_bpp = depth;
4750 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004751
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004752#ifdef CONFIG_NAND_LIMITS
4753 /* Init nand stuff. */
4754 if (android_op_nand_limits) {
4755 parse_nand_limits(android_op_nand_limits);
4756 }
4757#endif // CONFIG_NAND_LIMITS
4758
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004759 /* Initialize AVD name from hardware configuration if needed */
4760 if (!android_op_avd_name) {
4761 if (android_hw->avd_name && *android_hw->avd_name) {
4762 android_op_avd_name = android_hw->avd_name;
4763 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
4764 }
4765 }
4766
David 'Digit' Turner40841b22011-03-01 14:04:00 +01004767 /* Initialize system partition image */
4768 {
4769 char tmp[PATH_MAX+32];
4770 const char* sysImage = android_hw->disk_systemPartition_path;
4771 const char* initImage = android_hw->disk_systemPartition_initPath;
4772 uint64_t sysBytes = android_hw->disk_systemPartition_size;
4773
4774 if (sysBytes == 0) {
4775 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
4776 }
4777
4778 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
4779
4780 if (sysImage && *sysImage) {
4781 if (filelock_create(sysImage) == NULL) {
4782 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
4783 /* If there is no file= parameters, nand_add_dev will create
4784 * a temporary file to back the partition image. */
4785 } else {
4786 pstrcat(tmp,sizeof(tmp),",file=");
4787 pstrcat(tmp,sizeof(tmp),sysImage);
4788 }
4789 }
4790 if (initImage && *initImage) {
4791 if (!path_exists(initImage)) {
4792 PANIC("Invalid initial system image path: %s", initImage);
4793 }
4794 pstrcat(tmp,sizeof(tmp),",initfile=");
4795 pstrcat(tmp,sizeof(tmp),initImage);
4796 } else {
4797 PANIC("Missing initial system image path!");
4798 }
4799 nand_add_dev(tmp);
4800 }
4801
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01004802 /* Initialize data partition image */
4803 {
4804 char tmp[PATH_MAX+32];
4805 const char* dataImage = android_hw->disk_dataPartition_path;
4806 const char* initImage = android_hw->disk_dataPartition_initPath;
4807 uint64_t dataBytes = android_hw->disk_dataPartition_size;
4808
4809 if (dataBytes == 0) {
4810 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
4811 }
4812
4813 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
4814
4815 if (dataImage && *dataImage) {
4816 if (filelock_create(dataImage) == NULL) {
4817 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
4818 /* Note: if there is no file= parameters, nand_add_dev() will
4819 * create a temporary file to back the partition image. */
4820 } else {
4821 /* Create the file if needed */
4822 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01004823 if (path_empty_file(dataImage) < 0) {
4824 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
4825 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01004826 }
4827 pstrcat(tmp, sizeof(tmp), ",file=");
4828 pstrcat(tmp, sizeof(tmp), dataImage);
4829 }
4830 }
4831 if (initImage && *initImage) {
4832 pstrcat(tmp, sizeof(tmp), ",initfile=");
4833 pstrcat(tmp, sizeof(tmp), initImage);
4834 }
4835 nand_add_dev(tmp);
4836 }
4837
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01004838 /* Init SD-Card stuff. For Android, it is always hda */
4839 /* If the -hda option was used, ignore the Android-provided one */
4840 if (hda_opts == NULL) {
4841 const char* sdPath = android_hw->hw_sdCard_path;
4842 if (sdPath && *sdPath) {
4843 if (!path_exists(sdPath)) {
4844 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
4845 } else if (filelock_create(sdPath) == NULL) {
4846 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
4847 } else {
4848 /* Successful locking */
4849 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02004850 /* Set this property of any operation involving the SD Card
4851 * will be x100 slower, due to the corresponding file being
4852 * mounted as O_DIRECT. Note that this is only 'unsafe' in
4853 * the context of an emulator crash. The data is already
4854 * synced properly when the emulator exits (either normally or through ^C).
4855 */
4856 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01004857 }
4858 }
4859 }
4860
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004861 if (hdb_opts == NULL) {
4862 const char* spath = android_hw->disk_snapStorage_path;
4863 if (spath && *spath) {
4864 if (!path_exists(spath)) {
4865 PANIC("Snapshot storage file does not exist: %s", spath);
4866 }
4867 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02004868 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004869 }
4870 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02004871 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02004872 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004873 }
4874 }
4875
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004876 /* Set the VM's max heap size, passed as a boot property */
4877 if (android_hw->vm_heapSize > 0) {
4878 char tmp[64];
4879 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
4880 boot_property_add("dalvik.vm.heapsize",tmp);
4881 }
4882
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004883 /* Initialize net speed and delays stuff. */
4884 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004885 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004886 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004887 }
4888
4889 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004890 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004891 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004892 }
4893
4894 if (android_op_netfast) {
4895 qemu_net_download_speed = 0;
4896 qemu_net_upload_speed = 0;
4897 qemu_net_min_latency = 0;
4898 qemu_net_max_latency = 0;
4899 }
4900
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004901 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01004902 if (android_hw->hw_lcd_density) {
4903 long density = android_hw->hw_lcd_density;
4904 if (density <= 0) {
4905 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004906 }
4907 hwLcd_setBootProperty(density);
4908 }
4909
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004910 /* Initialize TCP dump */
4911 if (android_op_tcpdump) {
4912 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
4913 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
4914 }
4915 }
4916
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004917 /* Initialize modem */
4918 if (android_op_radio) {
4919 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
4920 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004921 PANIC("unsupported character device specification: %s\n"
4922 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004923 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004924 }
4925 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
4926 } else if (android_hw->hw_gsmModem != 0 ) {
4927 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004928 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004929 }
4930 }
4931
4932 /* Initialize GPS */
4933 if (android_op_gps) {
4934 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
4935 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004936 PANIC("unsupported character device specification: %s\n"
4937 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004938 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004939 }
4940 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
4941 } else if (android_hw->hw_gps != 0) {
4942 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004943 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004944 }
4945 }
4946
4947 /* Initialize audio. */
4948 if (android_op_audio) {
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02004949 char temp[128];
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004950 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004951 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004952 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004953 }
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02004954 snprintf(temp, sizeof temp, "QEMU_AUDIO_DRV=%s", android_op_audio);
4955 putenv(temp);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004956 }
4957
David 'Digit' Turner5b481492011-04-11 17:37:32 +02004958 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02004959 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02004960
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004961 if (android_op_cpu_delay) {
4962 char* end;
4963 long delay = strtol(android_op_cpu_delay, &end, 0);
4964 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004965 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004966 }
4967 if (delay > 0)
4968 delay = (1000-delay);
4969
4970 qemu_cpu_delay = (int) delay;
4971 }
4972
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004973 if (android_op_dns_server) {
4974 char* x = strchr(android_op_dns_server, ',');
4975 dns_count = 0;
4976 if (x == NULL)
4977 {
4978 if ( add_dns_server( android_op_dns_server ) == 0 )
4979 dns_count = 1;
4980 }
4981 else
4982 {
4983 x = android_op_dns_server;
4984 while (*x) {
4985 char* y = strchr(x, ',');
4986
4987 if (y != NULL) {
4988 *y = 0;
4989 y++;
4990 } else {
4991 y = x + strlen(x);
4992 }
4993
4994 if (y > x && add_dns_server( x ) == 0) {
4995 dns_count += 1;
4996 }
4997 x = y;
4998 }
4999 }
5000 if (dns_count == 0)
5001 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5002 }
5003
5004 if (dns_count == 0)
5005 dns_count = slirp_get_system_dns_servers();
5006 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005007 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005008 }
5009
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005010#ifdef CONFIG_MEMCHECK
5011 if (android_op_memcheck) {
5012 memcheck_init(android_op_memcheck);
5013 }
5014#endif // CONFIG_MEMCHECK
5015
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005016 /* Initialize cache partition, if any */
5017 if (android_hw->disk_cachePartition != 0) {
5018 char tmp[PATH_MAX+32];
5019 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005020 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005021
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005022 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5023
5024 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5025 if (filelock_create(partPath) == NULL) {
5026 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5027 /* Note: if there is no file= parameters, nand_add_dev() will
5028 * create a temporary file to back the partition image. */
5029 } else {
5030 /* Create the file if needed */
5031 if (!path_exists(partPath)) {
5032 if (path_empty_file(partPath) < 0) {
5033 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5034 }
5035 }
5036 pstrcat(tmp, sizeof(tmp), ",file=");
5037 pstrcat(tmp, sizeof(tmp), partPath);
5038 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005039 }
5040 nand_add_dev(tmp);
5041 }
5042
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005043 /* We always force qemu=1 when running inside QEMU */
5044 stralloc_add_str(kernel_params, " qemu=1");
5045
5046 /* We always initialize the first serial port for the android-kmsg
5047 * character device (used to send kernel messages) */
5048 serial_hds_add_at(0, "android-kmsg");
5049 stralloc_add_str(kernel_params, " console=ttyS0");
5050
5051 /* We always initialize the second serial port for the android-qemud
5052 * character device as well */
5053 serial_hds_add_at(1, "android-qemud");
5054 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5055
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02005056 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5057 os_pidfile_error();
5058 exit(1);
5059 }
5060
David 'Digit' Turner36597752011-05-20 01:18:01 +02005061#if defined(CONFIG_KVM)
5062 if (kvm_allowed < 0) {
5063 kvm_allowed = kvm_check_allowed();
5064 }
5065#endif
5066
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005067#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5068 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005069 PANIC(
5070 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005071 }
5072#endif
5073
5074 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5075 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005076 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5077 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005078 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005079 }
5080
5081 if (display_type == DT_NOGRAPHIC) {
5082 if (serial_device_index == 0)
5083 serial_devices[0] = "stdio";
5084 if (parallel_device_index == 0)
5085 parallel_devices[0] = "null";
5086 if (strncmp(monitor_device, "vc", 2) == 0)
5087 monitor_device = "stdio";
5088 }
5089
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005090#ifdef CONFIG_KQEMU
5091 if (smp_cpus > 1)
5092 kqemu_allowed = 0;
5093#endif
5094 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005095 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005096 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005097
5098 if (kernel_filename == NULL) {
5099 kernel_filename = android_hw->kernel_path;
5100 }
5101 if (initrd_filename == NULL) {
5102 initrd_filename = android_hw->disk_ramdisk_path;
5103 }
5104
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005105 linux_boot = (kernel_filename != NULL);
5106 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5107
5108 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005109 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005110 }
5111
5112 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005113 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005114 }
5115
5116 /* boot to floppy or the default cd if no hard disk defined yet */
5117 if (!boot_devices[0]) {
5118 boot_devices = "cad";
5119 }
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02005120 os_set_line_buffering();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005121
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005122 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005123 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005124 }
David Turner6a9ef172010-09-09 22:54:36 +02005125 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005126
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005127 /* init network clients */
5128 if (nb_net_clients == 0) {
5129 /* if no clients, we use a default config */
5130 net_clients[nb_net_clients++] = "nic";
5131#ifdef CONFIG_SLIRP
5132 net_clients[nb_net_clients++] = "user";
5133#endif
5134 }
5135
5136 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005137 if (net_client_parse(net_clients[i]) < 0) {
5138 PANIC("Unable to parse net clients");
5139 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005140 }
5141 net_client_check();
5142
5143#ifdef TARGET_I386
5144 /* XXX: this should be moved in the PC machine instantiation code */
5145 if (net_boot != 0) {
5146 int netroms = 0;
5147 for (i = 0; i < nb_nics && i < 4; i++) {
5148 const char *model = nd_table[i].model;
5149 char buf[1024];
5150 char *filename;
5151 if (net_boot & (1 << i)) {
5152 if (model == NULL)
5153 model = "ne2k_pci";
5154 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5155 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5156 if (filename && get_image_size(filename) > 0) {
5157 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005158 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005159 }
5160 option_rom[nb_option_roms] = qemu_strdup(buf);
5161 nb_option_roms++;
5162 netroms++;
5163 }
5164 if (filename) {
5165 qemu_free(filename);
5166 }
5167 }
5168 }
5169 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005170 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005171 }
5172 }
5173#endif
5174
5175 /* init the bluetooth world */
5176 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005177 if (bt_parse(bt_opts[i])) {
5178 PANIC("Unable to parse bluetooth options");
5179 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005180
5181 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005182 if (ram_size == 0) {
5183 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5184 if (ram_size == 0) {
5185 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5186 }
5187 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005188
5189#ifdef CONFIG_KQEMU
5190 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5191 guest ram allocation. It needs to go away. */
5192 if (kqemu_allowed) {
5193 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5194 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5195 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005196 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005197 }
5198 }
5199#endif
5200
rich canningsd952f282011-03-01 15:40:09 -08005201#ifndef _WIN32
5202 init_qemu_clear_logs_sig();
5203#endif
5204
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005205 /* init the dynamic translator */
5206 cpu_exec_init_all(tb_size * 1024 * 1024);
5207
5208 bdrv_init();
5209
5210 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005211#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005212 if (nb_drives_opt < MAX_DRIVES)
5213 drive_add(NULL, CDROM_ALIAS);
5214
5215 /* we always create at least one floppy */
5216
5217 if (nb_drives_opt < MAX_DRIVES)
5218 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005219 /* we always create one sd slot, even if no card is in it */
5220
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005221 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005222 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005223 }
5224#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005225
5226 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005227 if (snapshot)
5228 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5229 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5230 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005231
David Turner6a9ef172010-09-09 22:54:36 +02005232 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005233 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5234
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005235 /* must be after terminal init, SDL library changes signal handlers */
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02005236 os_setup_signal_handling();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005237
5238 /* Maintain compatibility with multiple stdio monitors */
5239 if (!strcmp(monitor_device,"stdio")) {
5240 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5241 const char *devname = serial_devices[i];
5242 if (devname && !strcmp(devname,"mon:stdio")) {
5243 monitor_device = NULL;
5244 break;
5245 } else if (devname && !strcmp(devname,"stdio")) {
5246 monitor_device = NULL;
5247 serial_devices[i] = "mon:stdio";
5248 break;
5249 }
5250 }
5251 }
5252
5253 if (nb_numa_nodes > 0) {
5254 int i;
5255
5256 if (nb_numa_nodes > smp_cpus) {
5257 nb_numa_nodes = smp_cpus;
5258 }
5259
5260 /* If no memory size if given for any node, assume the default case
5261 * and distribute the available memory equally across all nodes
5262 */
5263 for (i = 0; i < nb_numa_nodes; i++) {
5264 if (node_mem[i] != 0)
5265 break;
5266 }
5267 if (i == nb_numa_nodes) {
5268 uint64_t usedmem = 0;
5269
5270 /* On Linux, the each node's border has to be 8MB aligned,
5271 * the final node gets the rest.
5272 */
5273 for (i = 0; i < nb_numa_nodes - 1; i++) {
5274 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5275 usedmem += node_mem[i];
5276 }
5277 node_mem[i] = ram_size - usedmem;
5278 }
5279
5280 for (i = 0; i < nb_numa_nodes; i++) {
5281 if (node_cpumask[i] != 0)
5282 break;
5283 }
5284 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5285 * must cope with this anyway, because there are BIOSes out there in
5286 * real machines which also use this scheme.
5287 */
5288 if (i == nb_numa_nodes) {
5289 for (i = 0; i < smp_cpus; i++) {
5290 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5291 }
5292 }
5293 }
5294
5295 if (kvm_enabled()) {
5296 int ret;
5297
5298 ret = kvm_init(smp_cpus);
5299 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005300 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005301 }
5302 }
5303
5304 if (monitor_device) {
5305 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5306 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005307 PANIC("qemu: could not open monitor device '%s'",
5308 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005309 }
5310 }
5311
5312 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005313 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005314 }
5315
5316 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5317 const char *devname = parallel_devices[i];
5318 if (devname && strcmp(devname, "none")) {
5319 char label[32];
5320 snprintf(label, sizeof(label), "parallel%d", i);
5321 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5322 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005323 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005324 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005325 }
5326 }
5327 }
5328
5329 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5330 const char *devname = virtio_consoles[i];
5331 if (devname && strcmp(devname, "none")) {
5332 char label[32];
5333 snprintf(label, sizeof(label), "virtcon%d", i);
5334 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5335 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005336 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005337 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005338 }
5339 }
5340 }
5341
5342 module_call_init(MODULE_INIT_DEVICE);
5343
5344
5345#ifdef CONFIG_TRACE
5346 if (trace_filename) {
5347 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005348 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5349 }
5350#endif
5351
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005352 /* Check the CPU Architecture value */
5353#if defined(TARGET_ARM)
5354 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5355 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5356 android_hw->hw_cpu_arch);
5357 exit(1);
5358 }
5359#elif defined(TARGET_X86)
5360 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5361 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5362 android_hw->hw_cpu_arch);
5363 exit(1);
5364 }
5365#endif
5366
5367 /* Grab CPU model if provided in hardware.ini */
5368 if ( !cpu_model
5369 && android_hw->hw_cpu_model
5370 && android_hw->hw_cpu_model[0] != '\0')
5371 {
5372 cpu_model = android_hw->hw_cpu_model;
5373 }
5374
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005375 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005376 * collected during initialization.
5377 *
5378 * The order is the following:
5379 * - parameters from the hw configuration (kernel.parameters)
5380 * - additionnal parameters from options (e.g. -memcheck)
5381 * - the -append parameters.
5382 */
5383 {
5384 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005385
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005386 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005387 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005388 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5389 }
5390
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005391 /* If not empty, kernel_config always contains a leading space */
5392 stralloc_append(kernel_params, kernel_config);
5393
5394 if (*kernel_cmdline) {
5395 stralloc_add_c(kernel_params, ' ');
5396 stralloc_add_str(kernel_params, kernel_cmdline);
5397 }
5398
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005399 /* Remove any leading/trailing spaces */
5400 stralloc_strip(kernel_params);
5401
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005402 kernel_parameters = stralloc_cstr(kernel_params);
5403 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5404
5405 machine->init(ram_size,
5406 boot_devices,
5407 kernel_filename,
5408 kernel_parameters,
5409 initrd_filename,
5410 cpu_model);
5411
5412 stralloc_reset(kernel_params);
5413 stralloc_reset(kernel_config);
5414 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005415
5416
5417 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5418 for (i = 0; i < nb_numa_nodes; i++) {
5419 if (node_cpumask[i] & (1 << env->cpu_index)) {
5420 env->numa_node = i;
5421 }
5422 }
5423 }
5424
5425 current_machine = machine;
5426
5427 /* Set KVM's vcpu state to qemu's initial CPUState. */
5428 if (kvm_enabled()) {
5429 int ret;
5430
5431 ret = kvm_sync_vcpus();
5432 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005433 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005434 }
5435 }
5436
5437 /* init USB devices */
5438 if (usb_enabled) {
5439 for(i = 0; i < usb_devices_index; i++) {
5440 if (usb_device_add(usb_devices[i], 0) < 0) {
5441 fprintf(stderr, "Warning: could not add USB device %s\n",
5442 usb_devices[i]);
5443 }
5444 }
5445 }
5446
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005447 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005448 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005449
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005450 /* Initialize display from the command line parameters. */
5451 android_display_reset(ds,
5452 android_display_width,
5453 android_display_height,
5454 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005455
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005456 if (display_type == DT_DEFAULT) {
5457#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5458 display_type = DT_SDL;
5459#else
5460 display_type = DT_VNC;
5461 vnc_display = "localhost:0,to=99";
5462 show_vnc_port = 1;
5463#endif
5464 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005465
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005466
5467 switch (display_type) {
5468 case DT_NOGRAPHIC:
5469 break;
5470#if defined(CONFIG_CURSES)
5471 case DT_CURSES:
5472 curses_display_init(ds, full_screen);
5473 break;
5474#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005475#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005476 case DT_SDL:
5477 sdl_display_init(ds, full_screen, no_frame);
5478 break;
5479#elif defined(CONFIG_COCOA)
5480 case DT_SDL:
5481 cocoa_display_init(ds, full_screen);
5482 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005483#elif defined(CONFIG_STANDALONE_CORE)
5484 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005485 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005486 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005487#endif
5488 case DT_VNC:
5489 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005490 if (vnc_display_open(ds, vnc_display) < 0) {
5491 PANIC("Unable to initialize VNC display");
5492 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005493
5494 if (show_vnc_port) {
5495 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5496 }
5497 break;
5498 default:
5499 break;
5500 }
5501 dpy_resize(ds);
5502
5503 dcl = ds->listeners;
5504 while (dcl != NULL) {
5505 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005506 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
5507 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005508 }
5509 dcl = dcl->next;
5510 }
5511
5512 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005513 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
5514 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005515 }
5516
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005517 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005518 qemu_chr_initial_reset();
5519
5520 if (monitor_device && monitor_hd)
5521 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5522
5523 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5524 const char *devname = serial_devices[i];
5525 if (devname && strcmp(devname, "none")) {
5526 if (strstart(devname, "vc", 0))
5527 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5528 }
5529 }
5530
5531 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5532 const char *devname = parallel_devices[i];
5533 if (devname && strcmp(devname, "none")) {
5534 if (strstart(devname, "vc", 0))
5535 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5536 }
5537 }
5538
5539 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5540 const char *devname = virtio_consoles[i];
5541 if (virtcon_hds[i] && devname) {
5542 if (strstart(devname, "vc", 0))
5543 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5544 }
5545 }
5546
5547 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005548 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005549 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005550 }
5551
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005552 /* call android-specific setup function */
5553 android_emulation_setup();
5554
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005555#if !defined(CONFIG_STANDALONE_CORE)
5556 // For the standalone emulator (UI+core in one executable) we need to
5557 // set the window title here.
5558 android_emulator_set_base_port(android_base_port);
5559#endif
5560
Ot ten Thije871da2a2010-09-20 10:29:22 +01005561 if (loadvm)
5562 do_loadvm(cur_mon, loadvm);
5563
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005564 if (incoming) {
5565 autostart = 0; /* fixme how to deal with -daemonize */
5566 qemu_start_incoming_migration(incoming);
5567 }
5568
5569 if (autostart)
5570 vm_start();
5571
David 'Digit' Turnerc1ac40a2011-06-01 16:12:04 +02005572 os_setup_post();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005573
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005574#ifdef CONFIG_ANDROID
5575 // This will notify the UI that the core is successfuly initialized
5576 android_core_init_completed();
5577#endif // CONFIG_ANDROID
5578
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005579 main_loop();
5580 quit_timers();
5581 net_cleanup();
5582 android_emulation_teardown();
5583 return 0;
5584}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005585
5586void
5587android_emulation_teardown(void)
5588{
5589 android_charmap_done();
5590}