blob: 52f29e751332b4edc6ec03ab42031e4bd0eefe13 [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>
88#include <pwd.h>
89#include <sys/times.h>
90#include <sys/wait.h>
91#include <termios.h>
92#include <sys/mman.h>
93#include <sys/ioctl.h>
94#include <sys/resource.h>
95#include <sys/socket.h>
96#include <netinet/in.h>
97#include <net/if.h>
98#if defined(__NetBSD__)
99#include <net/if_tap.h>
100#endif
101#ifdef __linux__
102#include <linux/if_tun.h>
103#endif
104#include <arpa/inet.h>
105#include <dirent.h>
106#include <netdb.h>
107#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700108#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700109#include <sys/stat.h>
110#if defined(__FreeBSD__) || defined(__DragonFly__)
111#include <libutil.h>
112#else
113#include <util.h>
114#endif
115#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
116#include <freebsd/stdlib.h>
117#else
118#ifdef __linux__
119#include <pty.h>
120#include <malloc.h>
121#include <linux/rtc.h>
122
123/* For the benefit of older linux systems which don't supply it,
124 we use a local copy of hpet.h. */
125/* #include <linux/hpet.h> */
126#include "hpet.h"
127
128#include <linux/ppdev.h>
129#include <linux/parport.h>
130#endif
131#ifdef __sun__
132#include <sys/stat.h>
133#include <sys/ethernet.h>
134#include <sys/sockio.h>
135#include <netinet/arp.h>
136#include <netinet/in.h>
137#include <netinet/in_systm.h>
138#include <netinet/ip.h>
139#include <netinet/ip_icmp.h> // must come after ip.h
140#include <netinet/udp.h>
141#include <netinet/tcp.h>
142#include <net/if.h>
143#include <syslog.h>
144#include <stropts.h>
145#endif
146#endif
147#endif
148
149#if defined(__OpenBSD__)
150#include <util.h>
151#endif
152
153#if defined(CONFIG_VDE)
154#include <libvdeplug.h>
155#endif
156
157#ifdef _WIN32
158#include <windows.h>
159#include <malloc.h>
160#include <sys/timeb.h>
161#include <mmsystem.h>
162#define getopt_long_only getopt_long
163#define memalign(align, size) malloc(size)
164#endif
165
166
167#ifdef CONFIG_COCOA
168#undef main
169#define main qemu_main
170#endif /* CONFIG_COCOA */
171
172#include "hw/hw.h"
173#include "hw/boards.h"
174#include "hw/usb.h"
175#include "hw/pcmcia.h"
176#include "hw/pc.h"
177#include "hw/audiodev.h"
178#include "hw/isa.h"
179#include "hw/baum.h"
180#include "hw/bt.h"
181#include "hw/watchdog.h"
182#include "hw/smbios.h"
183#include "hw/xen.h"
184#include "bt-host.h"
185#include "net.h"
186#include "monitor.h"
187#include "console.h"
188#include "sysemu.h"
189#include "gdbstub.h"
190#include "qemu-timer.h"
191#include "qemu-char.h"
192#include "cache-utils.h"
193#include "block.h"
194#include "dma.h"
195#include "audio/audio.h"
196#include "migration.h"
197#include "kvm.h"
David 'Digit' Turner36597752011-05-20 01:18:01 +0200198#ifdef CONFIG_KVM
199#include "kvm-android.h"
200#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700201#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700202#include "android/hw-lcd.h"
203#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200204#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700205#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100206#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700207
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700208#ifdef CONFIG_STANDALONE_CORE
209/* Verbose value used by the standalone emulator core (without UI) */
210unsigned long android_verbose;
211#endif // CONFIG_STANDALONE_CORE
212
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800213#if !defined(CONFIG_STANDALONE_CORE)
214/* in android/qemulator.c */
215extern void android_emulator_set_base_port(int port);
216#endif
217
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700218#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700219#undef main
220#define main qemu_main
221#endif
222
223#include "disas.h"
224
225#include "exec-all.h"
226
227#ifdef CONFIG_TRACE
228#include "trace.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700229#endif
230
231#include "qemu_socket.h"
232
233#if defined(CONFIG_SLIRP)
234#include "libslirp.h"
235#endif
236
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700237
238
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700239#define DEFAULT_RAM_SIZE 128
240
241/* Max number of USB devices that can be specified on the commandline. */
242#define MAX_USB_CMDLINE 8
243
244/* Max number of bluetooth switches on the commandline. */
245#define MAX_BT_CMDLINE 10
246
247/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700248
249static const char *data_dir;
250const char *bios_name = NULL;
251static void *ioport_opaque[MAX_IOPORTS];
252static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
253static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100254#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700255/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
256 to store the VM snapshots */
257DriveInfo drives_table[MAX_DRIVES+1];
258int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100259#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700260enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700261DisplayType display_type = DT_DEFAULT;
262const char* keyboard_layout = NULL;
263int64_t ticks_per_sec;
264ram_addr_t ram_size;
265int nb_nics;
266NICInfo nd_table[MAX_NICS];
267int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100268int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700269static int rtc_utc = 1;
270static int rtc_date_offset = -1; /* -1 means no change */
271int cirrus_vga_enabled = 1;
272int std_vga_enabled = 0;
273int vmsvga_enabled = 0;
274int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700275QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700276#ifdef TARGET_SPARC
277int graphic_width = 1024;
278int graphic_height = 768;
279int graphic_depth = 8;
280#else
281int graphic_width = 800;
282int graphic_height = 600;
283int graphic_depth = 15;
284#endif
285static int full_screen = 0;
286#ifdef CONFIG_SDL
287static int no_frame = 0;
288#endif
289int no_quit = 0;
290CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100291int serial_hds_count;
292
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700293CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
294CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
295#ifdef TARGET_I386
296int win2k_install_hack = 0;
297int rtc_td_hack = 0;
298#endif
299int usb_enabled = 0;
300int singlestep = 0;
301int smp_cpus = 1;
302const char *vnc_display;
303int acpi_enabled = 1;
304int no_hpet = 0;
305int no_virtio_balloon = 0;
306int fd_bootchk = 1;
307int no_reboot = 0;
308int no_shutdown = 0;
309int cursor_hide = 1;
310int graphic_rotate = 0;
311#ifndef _WIN32
312int daemonize = 0;
313#endif
314WatchdogTimerModel *watchdog = NULL;
315int watchdog_action = WDT_RESET;
316const char *option_rom[MAX_OPTION_ROMS];
317int nb_option_roms;
318int semihosting_enabled = 0;
319#ifdef TARGET_ARM
320int old_param = 0;
321#endif
322const char *qemu_name;
323int alt_grab = 0;
324#if defined(TARGET_SPARC) || defined(TARGET_PPC)
325unsigned int nb_prom_envs = 0;
326const char *prom_envs[MAX_PROM_ENVS];
327#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100328#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700329int nb_drives_opt;
330struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100331#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700332int nb_numa_nodes;
333uint64_t node_mem[MAX_NODES];
334uint64_t node_cpumask[MAX_NODES];
335
336static CPUState *cur_cpu;
337static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700338static QEMUTimer *nographic_timer;
339
340uint8_t qemu_uuid[16];
341
342
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700343int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700344extern char* audio_input_source;
345
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700346extern char* android_op_ports;
347extern char* android_op_port;
348extern char* android_op_report_console;
349extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700350// Path to the file containing specific key character map.
351char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700352
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700353/* Path to hardware initialization file passed with -android-hw option. */
354char* android_op_hwini = NULL;
355
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700356/* Memory checker options. */
357char* android_op_memcheck = NULL;
358
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700359/* -dns-server option value. */
360char* android_op_dns_server = NULL;
361
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700362/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700363char* android_op_radio = NULL;
364
365/* -gps option value. */
366char* android_op_gps = NULL;
367
368/* -audio option value. */
369char* android_op_audio = NULL;
370
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700371/* -cpu-delay option value. */
372char* android_op_cpu_delay = NULL;
373
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700374#ifdef CONFIG_NAND_LIMITS
375/* -nand-limits option value. */
376char* android_op_nand_limits = NULL;
377#endif // CONFIG_NAND_LIMITS
378
379/* -netspeed option value. */
380char* android_op_netspeed = NULL;
381
382/* -netdelay option value. */
383char* android_op_netdelay = NULL;
384
385/* -netfast option value. */
386int android_op_netfast = 0;
387
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700388/* -tcpdump option value. */
389char* android_op_tcpdump = NULL;
390
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700391/* -lcd-density option value. */
392char* android_op_lcd_density = NULL;
393
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700394/* -ui-port option value. This port will be used to report the core
395 * initialization completion.
396 */
397char* android_op_ui_port = NULL;
398
399/* -ui-settings option value. This value will be passed to the UI when new UI
400 * process is attaching to the core.
401 */
402char* android_op_ui_settings = NULL;
403
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800404/* -android-avdname option value. */
405char* android_op_avd_name = "unknown";
406
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700407extern int android_display_width;
408extern int android_display_height;
409extern int android_display_bpp;
410
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700411extern void dprint( const char* format, ... );
412
rich canningsd952f282011-03-01 15:40:09 -0800413const char* dns_log_filename = NULL;
414const char* drop_log_filename = NULL;
415static int rotate_logs_requested = 0;
416
Tim Baverstock24204cc2010-11-25 11:37:43 +0000417const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000418
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700419#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
420
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700421/* Reports the core initialization failure to the error stdout and to the UI
422 * socket before exiting the application.
423 * Parameters that are passed to this macro are used to format the error
424 * mesage using sprintf routine.
425 */
426#ifdef CONFIG_ANDROID
427#define PANIC(...) android_core_init_failure(__VA_ARGS__)
428#else
429#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
430 exit(1); \
431 } while (0)
432#endif // CONFIG_ANDROID
433
434/* Exits the core during initialization. */
435#ifdef CONFIG_ANDROID
436#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
437#else
438#define QEMU_EXIT(exit_code) exit(exit_code)
439#endif // CONFIG_ANDROID
440
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700441/***********************************************************/
442/* x86 ISA bus support */
443
444target_phys_addr_t isa_mem_base = 0;
445PicState2 *isa_pic;
446
447static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
448static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
449
450static uint32_t ioport_read(int index, uint32_t address)
451{
452 static IOPortReadFunc *default_func[3] = {
453 default_ioport_readb,
454 default_ioport_readw,
455 default_ioport_readl
456 };
457 IOPortReadFunc *func = ioport_read_table[index][address];
458 if (!func)
459 func = default_func[index];
460 return func(ioport_opaque[address], address);
461}
462
463static void ioport_write(int index, uint32_t address, uint32_t data)
464{
465 static IOPortWriteFunc *default_func[3] = {
466 default_ioport_writeb,
467 default_ioport_writew,
468 default_ioport_writel
469 };
470 IOPortWriteFunc *func = ioport_write_table[index][address];
471 if (!func)
472 func = default_func[index];
473 func(ioport_opaque[address], address, data);
474}
475
476static uint32_t default_ioport_readb(void *opaque, uint32_t address)
477{
478#ifdef DEBUG_UNUSED_IOPORT
479 fprintf(stderr, "unused inb: port=0x%04x\n", address);
480#endif
481 return 0xff;
482}
483
484static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
485{
486#ifdef DEBUG_UNUSED_IOPORT
487 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
488#endif
489}
490
491/* default is to make two byte accesses */
492static uint32_t default_ioport_readw(void *opaque, uint32_t address)
493{
494 uint32_t data;
495 data = ioport_read(0, address);
496 address = (address + 1) & (MAX_IOPORTS - 1);
497 data |= ioport_read(0, address) << 8;
498 return data;
499}
500
501static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
502{
503 ioport_write(0, address, data & 0xff);
504 address = (address + 1) & (MAX_IOPORTS - 1);
505 ioport_write(0, address, (data >> 8) & 0xff);
506}
507
508static uint32_t default_ioport_readl(void *opaque, uint32_t address)
509{
510#ifdef DEBUG_UNUSED_IOPORT
511 fprintf(stderr, "unused inl: port=0x%04x\n", address);
512#endif
513 return 0xffffffff;
514}
515
516static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
517{
518#ifdef DEBUG_UNUSED_IOPORT
519 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
520#endif
521}
522
rich canningsd952f282011-03-01 15:40:09 -0800523/*
524 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
525 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
526 * between the tasks that do not require restarting Qemu.
527 */
528void rotate_qemu_logs_handler(int signum) {
529 rotate_logs_requested = 1;
530}
531
532/*
533 * Resets the rotate_log_requested_flag. Normally called after qemu
534 * logs has been rotated.
535 */
536void reset_rotate_qemu_logs_request(void) {
537 rotate_logs_requested = 0;
538}
539
540/*
541 * Clears the passed qemu log when the rotate_logs_requested
542 * is set. We need to clear the logs between the tasks that do not
543 * require restarting Qemu.
544 */
545FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
546 FILE* new_log_fd = NULL;
547 if (old_log_fd) {
548 if (fclose(old_log_fd) == -1) {
549 fprintf(stderr, "Cannot close old_log fd\n");
550 exit(errno);
551 }
552 }
553
554 if (!filename) {
555 fprintf(stderr, "The log filename to be rotated is not provided");
556 exit(-1);
557 }
558
559 new_log_fd = fopen(filename , "wb+");
560 if (new_log_fd == NULL) {
561 fprintf(stderr, "Cannot open the log file: %s for write.\n",
562 filename);
563 exit(1);
564 }
565
566 return new_log_fd;
567}
568
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700569/***********************************************************/
570void hw_error(const char *fmt, ...)
571{
572 va_list ap;
573 CPUState *env;
574
575 va_start(ap, fmt);
576 fprintf(stderr, "qemu: hardware error: ");
577 vfprintf(stderr, fmt, ap);
578 fprintf(stderr, "\n");
579 for(env = first_cpu; env != NULL; env = env->next_cpu) {
580 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
581#ifdef TARGET_I386
582 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
583#else
584 cpu_dump_state(env, stderr, fprintf, 0);
585#endif
586 }
587 va_end(ap);
588 abort();
589}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200590
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700591/***************/
592/* ballooning */
593
594static QEMUBalloonEvent *qemu_balloon_event;
595void *qemu_balloon_event_opaque;
596
597void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
598{
599 qemu_balloon_event = func;
600 qemu_balloon_event_opaque = opaque;
601}
602
603void qemu_balloon(ram_addr_t target)
604{
605 if (qemu_balloon_event)
606 qemu_balloon_event(qemu_balloon_event_opaque, target);
607}
608
609ram_addr_t qemu_balloon_status(void)
610{
611 if (qemu_balloon_event)
612 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
613 return 0;
614}
615
616/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700617/* host time/date access */
618void qemu_get_timedate(struct tm *tm, int offset)
619{
620 time_t ti;
621 struct tm *ret;
622
623 time(&ti);
624 ti += offset;
625 if (rtc_date_offset == -1) {
626 if (rtc_utc)
627 ret = gmtime(&ti);
628 else
629 ret = localtime(&ti);
630 } else {
631 ti -= rtc_date_offset;
632 ret = gmtime(&ti);
633 }
634
635 memcpy(tm, ret, sizeof(struct tm));
636}
637
638int qemu_timedate_diff(struct tm *tm)
639{
640 time_t seconds;
641
642 if (rtc_date_offset == -1)
643 if (rtc_utc)
644 seconds = mktimegm(tm);
645 else
646 seconds = mktime(tm);
647 else
648 seconds = mktimegm(tm) + rtc_date_offset;
649
650 return seconds - time(NULL);
651}
652
653
654#ifdef CONFIG_TRACE
655static int tbflush_requested;
656static int exit_requested;
657
658void start_tracing()
659{
660 if (trace_filename == NULL)
661 return;
662 if (!tracing) {
663 fprintf(stderr,"-- start tracing --\n");
664 start_time = Now();
665 }
666 tracing = 1;
667 tbflush_requested = 1;
668 qemu_notify_event();
669}
670
671void stop_tracing()
672{
673 if (trace_filename == NULL)
674 return;
675 if (tracing) {
676 end_time = Now();
677 elapsed_usecs += end_time - start_time;
678 fprintf(stderr,"-- stop tracing --\n");
679 }
680 tracing = 0;
681 tbflush_requested = 1;
682 qemu_notify_event();
683}
684
685#ifndef _WIN32
686/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
687 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
688 */
689void sigusr_handler(int sig)
690{
691 if (sig == SIGUSR1)
692 start_tracing();
693 else
694 stop_tracing();
695}
696#endif
697
698/* This is the handler to catch control-C so that we can exit cleanly.
699 * This is needed when tracing to flush the buffers to disk.
700 */
701void sigint_handler(int sig)
702{
703 exit_requested = 1;
704 qemu_notify_event();
705}
706#endif /* CONFIG_TRACE */
707
708
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700709/***********************************************************/
710/* Bluetooth support */
711static int nb_hcis;
712static int cur_hci;
713static struct HCIInfo *hci_table[MAX_NICS];
714
715static struct bt_vlan_s {
716 struct bt_scatternet_s net;
717 int id;
718 struct bt_vlan_s *next;
719} *first_bt_vlan;
720
721/* find or alloc a new bluetooth "VLAN" */
722static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
723{
724 struct bt_vlan_s **pvlan, *vlan;
725 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
726 if (vlan->id == id)
727 return &vlan->net;
728 }
729 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
730 vlan->id = id;
731 pvlan = &first_bt_vlan;
732 while (*pvlan != NULL)
733 pvlan = &(*pvlan)->next;
734 *pvlan = vlan;
735 return &vlan->net;
736}
737
738static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
739{
740}
741
742static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
743{
744 return -ENOTSUP;
745}
746
747static struct HCIInfo null_hci = {
748 .cmd_send = null_hci_send,
749 .sco_send = null_hci_send,
750 .acl_send = null_hci_send,
751 .bdaddr_set = null_hci_addr_set,
752};
753
754struct HCIInfo *qemu_next_hci(void)
755{
756 if (cur_hci == nb_hcis)
757 return &null_hci;
758
759 return hci_table[cur_hci++];
760}
761
762static struct HCIInfo *hci_init(const char *str)
763{
764 char *endp;
765 struct bt_scatternet_s *vlan = 0;
766
767 if (!strcmp(str, "null"))
768 /* null */
769 return &null_hci;
770 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
771 /* host[:hciN] */
772 return bt_host_hci(str[4] ? str + 5 : "hci0");
773 else if (!strncmp(str, "hci", 3)) {
774 /* hci[,vlan=n] */
775 if (str[3]) {
776 if (!strncmp(str + 3, ",vlan=", 6)) {
777 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
778 if (*endp)
779 vlan = 0;
780 }
781 } else
782 vlan = qemu_find_bt_vlan(0);
783 if (vlan)
784 return bt_new_hci(vlan);
785 }
786
787 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
788
789 return 0;
790}
791
792static int bt_hci_parse(const char *str)
793{
794 struct HCIInfo *hci;
795 bdaddr_t bdaddr;
796
797 if (nb_hcis >= MAX_NICS) {
798 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
799 return -1;
800 }
801
802 hci = hci_init(str);
803 if (!hci)
804 return -1;
805
806 bdaddr.b[0] = 0x52;
807 bdaddr.b[1] = 0x54;
808 bdaddr.b[2] = 0x00;
809 bdaddr.b[3] = 0x12;
810 bdaddr.b[4] = 0x34;
811 bdaddr.b[5] = 0x56 + nb_hcis;
812 hci->bdaddr_set(hci, bdaddr.b);
813
814 hci_table[nb_hcis++] = hci;
815
816 return 0;
817}
818
819static void bt_vhci_add(int vlan_id)
820{
821 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
822
823 if (!vlan->slave)
824 fprintf(stderr, "qemu: warning: adding a VHCI to "
825 "an empty scatternet %i\n", vlan_id);
826
827 bt_vhci_init(bt_new_hci(vlan));
828}
829
830static struct bt_device_s *bt_device_add(const char *opt)
831{
832 struct bt_scatternet_s *vlan;
833 int vlan_id = 0;
834 char *endp = strstr(opt, ",vlan=");
835 int len = (endp ? endp - opt : strlen(opt)) + 1;
836 char devname[10];
837
838 pstrcpy(devname, MIN(sizeof(devname), len), opt);
839
840 if (endp) {
841 vlan_id = strtol(endp + 6, &endp, 0);
842 if (*endp) {
843 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
844 return 0;
845 }
846 }
847
848 vlan = qemu_find_bt_vlan(vlan_id);
849
850 if (!vlan->slave)
851 fprintf(stderr, "qemu: warning: adding a slave device to "
852 "an empty scatternet %i\n", vlan_id);
853
854 if (!strcmp(devname, "keyboard"))
855 return bt_keyboard_init(vlan);
856
857 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
858 return 0;
859}
860
861static int bt_parse(const char *opt)
862{
863 const char *endp, *p;
864 int vlan;
865
866 if (strstart(opt, "hci", &endp)) {
867 if (!*endp || *endp == ',') {
868 if (*endp)
869 if (!strstart(endp, ",vlan=", 0))
870 opt = endp + 1;
871
872 return bt_hci_parse(opt);
873 }
874 } else if (strstart(opt, "vhci", &endp)) {
875 if (!*endp || *endp == ',') {
876 if (*endp) {
877 if (strstart(endp, ",vlan=", &p)) {
878 vlan = strtol(p, (char **) &endp, 0);
879 if (*endp) {
880 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
881 return 1;
882 }
883 } else {
884 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
885 return 1;
886 }
887 } else
888 vlan = 0;
889
890 bt_vhci_add(vlan);
891 return 0;
892 }
893 } else if (strstart(opt, "device:", &endp))
894 return !bt_device_add(endp);
895
896 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
897 return 1;
898}
899
900/***********************************************************/
901/* QEMU Block devices */
902
903#define HD_ALIAS "index=%d,media=disk"
904#define CDROM_ALIAS "index=2,media=cdrom"
905#define FD_ALIAS "index=%d,if=floppy"
906#define PFLASH_ALIAS "if=pflash"
907#define MTD_ALIAS "if=mtd"
908#define SD_ALIAS "index=0,if=sd"
909
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100910static int drive_init_func(QemuOpts *opts, void *opaque)
911{
912 int *use_scsi = opaque;
913 int fatal_error = 0;
914
915 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
916 if (fatal_error)
917 return 1;
918 }
919 return 0;
920}
921
922static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
923{
924 if (NULL == qemu_opt_get(opts, "snapshot")) {
925 qemu_opt_set(opts, "snapshot", "on");
926 }
927 return 0;
928}
929
930#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700931static int drive_opt_get_free_idx(void)
932{
933 int index;
934
935 for (index = 0; index < MAX_DRIVES; index++)
936 if (!drives_opt[index].used) {
937 drives_opt[index].used = 1;
938 return index;
939 }
940
941 return -1;
942}
943
944static int drive_get_free_idx(void)
945{
946 int index;
947
948 for (index = 0; index < MAX_DRIVES; index++)
949 if (!drives_table[index].used) {
950 drives_table[index].used = 1;
951 return index;
952 }
953
954 return -1;
955}
956
957int drive_add(const char *file, const char *fmt, ...)
958{
959 va_list ap;
960 int index = drive_opt_get_free_idx();
961
962 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
963 fprintf(stderr, "qemu: too many drives\n");
964 return -1;
965 }
966
967 drives_opt[index].file = file;
968 va_start(ap, fmt);
969 vsnprintf(drives_opt[index].opt,
970 sizeof(drives_opt[0].opt), fmt, ap);
971 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700972
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700973 nb_drives_opt++;
974 return index;
975}
976
977void drive_remove(int index)
978{
979 drives_opt[index].used = 0;
980 nb_drives_opt--;
981}
982
983int drive_get_index(BlockInterfaceType type, int bus, int unit)
984{
985 int index;
986
987 /* seek interface, bus and unit */
988
989 for (index = 0; index < MAX_DRIVES; index++)
990 if (drives_table[index].type == type &&
991 drives_table[index].bus == bus &&
992 drives_table[index].unit == unit &&
993 drives_table[index].used)
994 return index;
995
996 return -1;
997}
998
999int drive_get_max_bus(BlockInterfaceType type)
1000{
1001 int max_bus;
1002 int index;
1003
1004 max_bus = -1;
1005 for (index = 0; index < nb_drives; index++) {
1006 if(drives_table[index].type == type &&
1007 drives_table[index].bus > max_bus)
1008 max_bus = drives_table[index].bus;
1009 }
1010 return max_bus;
1011}
1012
1013const char *drive_get_serial(BlockDriverState *bdrv)
1014{
1015 int index;
1016
1017 for (index = 0; index < nb_drives; index++)
1018 if (drives_table[index].bdrv == bdrv)
1019 return drives_table[index].serial;
1020
1021 return "\0";
1022}
1023
1024BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1025{
1026 int index;
1027
1028 for (index = 0; index < nb_drives; index++)
1029 if (drives_table[index].bdrv == bdrv)
1030 return drives_table[index].onerror;
1031
1032 return BLOCK_ERR_STOP_ENOSPC;
1033}
1034
1035static void bdrv_format_print(void *opaque, const char *name)
1036{
1037 fprintf(stderr, " %s", name);
1038}
1039
1040void drive_uninit(BlockDriverState *bdrv)
1041{
1042 int i;
1043
1044 for (i = 0; i < MAX_DRIVES; i++)
1045 if (drives_table[i].bdrv == bdrv) {
1046 drives_table[i].bdrv = NULL;
1047 drives_table[i].used = 0;
1048 drive_remove(drives_table[i].drive_opt_idx);
1049 nb_drives--;
1050 break;
1051 }
1052}
1053
1054int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1055{
1056 char buf[128];
1057 char file[1024];
1058 char devname[128];
1059 char serial[21];
1060 const char *mediastr = "";
1061 BlockInterfaceType type;
1062 enum { MEDIA_DISK, MEDIA_CDROM } media;
1063 int bus_id, unit_id;
1064 int cyls, heads, secs, translation;
1065 BlockDriverState *bdrv;
1066 BlockDriver *drv = NULL;
1067 QEMUMachine *machine = opaque;
1068 int max_devs;
1069 int index;
1070 int cache;
1071 int bdrv_flags, onerror;
1072 int drives_table_idx;
1073 char *str = arg->opt;
1074 static const char * const params[] = { "bus", "unit", "if", "index",
1075 "cyls", "heads", "secs", "trans",
1076 "media", "snapshot", "file",
1077 "cache", "format", "serial", "werror",
1078 NULL };
1079
1080 if (check_params(buf, sizeof(buf), params, str) < 0) {
1081 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1082 buf, str);
1083 return -1;
1084 }
1085
1086 file[0] = 0;
1087 cyls = heads = secs = 0;
1088 bus_id = 0;
1089 unit_id = -1;
1090 translation = BIOS_ATA_TRANSLATION_AUTO;
1091 index = -1;
1092 cache = 3;
1093
1094 if (machine->use_scsi) {
1095 type = IF_SCSI;
1096 max_devs = MAX_SCSI_DEVS;
1097 pstrcpy(devname, sizeof(devname), "scsi");
1098 } else {
1099 type = IF_IDE;
1100 max_devs = MAX_IDE_DEVS;
1101 pstrcpy(devname, sizeof(devname), "ide");
1102 }
1103 media = MEDIA_DISK;
1104
1105 /* extract parameters */
1106
1107 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1108 bus_id = strtol(buf, NULL, 0);
1109 if (bus_id < 0) {
1110 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1111 return -1;
1112 }
1113 }
1114
1115 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1116 unit_id = strtol(buf, NULL, 0);
1117 if (unit_id < 0) {
1118 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1119 return -1;
1120 }
1121 }
1122
1123 if (get_param_value(buf, sizeof(buf), "if", str)) {
1124 pstrcpy(devname, sizeof(devname), buf);
1125 if (!strcmp(buf, "ide")) {
1126 type = IF_IDE;
1127 max_devs = MAX_IDE_DEVS;
1128 } else if (!strcmp(buf, "scsi")) {
1129 type = IF_SCSI;
1130 max_devs = MAX_SCSI_DEVS;
1131 } else if (!strcmp(buf, "floppy")) {
1132 type = IF_FLOPPY;
1133 max_devs = 0;
1134 } else if (!strcmp(buf, "pflash")) {
1135 type = IF_PFLASH;
1136 max_devs = 0;
1137 } else if (!strcmp(buf, "mtd")) {
1138 type = IF_MTD;
1139 max_devs = 0;
1140 } else if (!strcmp(buf, "sd")) {
1141 type = IF_SD;
1142 max_devs = 0;
1143 } else if (!strcmp(buf, "virtio")) {
1144 type = IF_VIRTIO;
1145 max_devs = 0;
1146 } else if (!strcmp(buf, "xen")) {
1147 type = IF_XEN;
1148 max_devs = 0;
1149 } else {
1150 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1151 return -1;
1152 }
1153 }
1154
1155 if (get_param_value(buf, sizeof(buf), "index", str)) {
1156 index = strtol(buf, NULL, 0);
1157 if (index < 0) {
1158 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1159 return -1;
1160 }
1161 }
1162
1163 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1164 cyls = strtol(buf, NULL, 0);
1165 }
1166
1167 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1168 heads = strtol(buf, NULL, 0);
1169 }
1170
1171 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1172 secs = strtol(buf, NULL, 0);
1173 }
1174
1175 if (cyls || heads || secs) {
1176 if (cyls < 1 || cyls > 16383) {
1177 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1178 return -1;
1179 }
1180 if (heads < 1 || heads > 16) {
1181 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1182 return -1;
1183 }
1184 if (secs < 1 || secs > 63) {
1185 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1186 return -1;
1187 }
1188 }
1189
1190 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1191 if (!cyls) {
1192 fprintf(stderr,
1193 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1194 str);
1195 return -1;
1196 }
1197 if (!strcmp(buf, "none"))
1198 translation = BIOS_ATA_TRANSLATION_NONE;
1199 else if (!strcmp(buf, "lba"))
1200 translation = BIOS_ATA_TRANSLATION_LBA;
1201 else if (!strcmp(buf, "auto"))
1202 translation = BIOS_ATA_TRANSLATION_AUTO;
1203 else {
1204 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1205 return -1;
1206 }
1207 }
1208
1209 if (get_param_value(buf, sizeof(buf), "media", str)) {
1210 if (!strcmp(buf, "disk")) {
1211 media = MEDIA_DISK;
1212 } else if (!strcmp(buf, "cdrom")) {
1213 if (cyls || secs || heads) {
1214 fprintf(stderr,
1215 "qemu: '%s' invalid physical CHS format\n", str);
1216 return -1;
1217 }
1218 media = MEDIA_CDROM;
1219 } else {
1220 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1221 return -1;
1222 }
1223 }
1224
1225 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1226 if (!strcmp(buf, "on"))
1227 snapshot = 1;
1228 else if (!strcmp(buf, "off"))
1229 snapshot = 0;
1230 else {
1231 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1232 return -1;
1233 }
1234 }
1235
1236 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1237 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1238 cache = 0;
1239 else if (!strcmp(buf, "writethrough"))
1240 cache = 1;
1241 else if (!strcmp(buf, "writeback"))
1242 cache = 2;
1243 else {
1244 fprintf(stderr, "qemu: invalid cache option\n");
1245 return -1;
1246 }
1247 }
1248
1249 if (get_param_value(buf, sizeof(buf), "format", str)) {
1250 if (strcmp(buf, "?") == 0) {
1251 fprintf(stderr, "qemu: Supported formats:");
1252 bdrv_iterate_format(bdrv_format_print, NULL);
1253 fprintf(stderr, "\n");
1254 return -1;
1255 }
1256 drv = bdrv_find_format(buf);
1257 if (!drv) {
1258 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1259 return -1;
1260 }
1261 }
1262
1263 if (arg->file == NULL)
1264 get_param_value(file, sizeof(file), "file", str);
1265 else
1266 pstrcpy(file, sizeof(file), arg->file);
1267
1268 if (!get_param_value(serial, sizeof(serial), "serial", str))
1269 memset(serial, 0, sizeof(serial));
1270
1271 onerror = BLOCK_ERR_STOP_ENOSPC;
1272 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1273 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1274 fprintf(stderr, "werror is no supported by this format\n");
1275 return -1;
1276 }
1277 if (!strcmp(buf, "ignore"))
1278 onerror = BLOCK_ERR_IGNORE;
1279 else if (!strcmp(buf, "enospc"))
1280 onerror = BLOCK_ERR_STOP_ENOSPC;
1281 else if (!strcmp(buf, "stop"))
1282 onerror = BLOCK_ERR_STOP_ANY;
1283 else if (!strcmp(buf, "report"))
1284 onerror = BLOCK_ERR_REPORT;
1285 else {
1286 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1287 return -1;
1288 }
1289 }
1290
1291 /* compute bus and unit according index */
1292
1293 if (index != -1) {
1294 if (bus_id != 0 || unit_id != -1) {
1295 fprintf(stderr,
1296 "qemu: '%s' index cannot be used with bus and unit\n", str);
1297 return -1;
1298 }
1299 if (max_devs == 0)
1300 {
1301 unit_id = index;
1302 bus_id = 0;
1303 } else {
1304 unit_id = index % max_devs;
1305 bus_id = index / max_devs;
1306 }
1307 }
1308
1309 /* if user doesn't specify a unit_id,
1310 * try to find the first free
1311 */
1312
1313 if (unit_id == -1) {
1314 unit_id = 0;
1315 while (drive_get_index(type, bus_id, unit_id) != -1) {
1316 unit_id++;
1317 if (max_devs && unit_id >= max_devs) {
1318 unit_id -= max_devs;
1319 bus_id++;
1320 }
1321 }
1322 }
1323
1324 /* check unit id */
1325
1326 if (max_devs && unit_id >= max_devs) {
1327 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1328 str, unit_id, max_devs - 1);
1329 return -1;
1330 }
1331
1332 /*
1333 * ignore multiple definitions
1334 */
1335
1336 if (drive_get_index(type, bus_id, unit_id) != -1)
1337 return -2;
1338
1339 /* init */
1340
1341 if (type == IF_IDE || type == IF_SCSI)
1342 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1343 if (max_devs)
1344 snprintf(buf, sizeof(buf), "%s%i%s%i",
1345 devname, bus_id, mediastr, unit_id);
1346 else
1347 snprintf(buf, sizeof(buf), "%s%s%i",
1348 devname, mediastr, unit_id);
1349 bdrv = bdrv_new(buf);
1350 drives_table_idx = drive_get_free_idx();
1351 drives_table[drives_table_idx].bdrv = bdrv;
1352 drives_table[drives_table_idx].type = type;
1353 drives_table[drives_table_idx].bus = bus_id;
1354 drives_table[drives_table_idx].unit = unit_id;
1355 drives_table[drives_table_idx].onerror = onerror;
1356 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1357 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1358 nb_drives++;
1359
1360 switch(type) {
1361 case IF_IDE:
1362 case IF_SCSI:
1363 case IF_XEN:
1364 switch(media) {
1365 case MEDIA_DISK:
1366 if (cyls != 0) {
1367 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1368 bdrv_set_translation_hint(bdrv, translation);
1369 }
1370 break;
1371 case MEDIA_CDROM:
1372 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1373 break;
1374 }
1375 break;
1376 case IF_SD:
1377 /* FIXME: This isn't really a floppy, but it's a reasonable
1378 approximation. */
1379 case IF_FLOPPY:
1380 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1381 break;
1382 case IF_PFLASH:
1383 case IF_MTD:
1384 case IF_VIRTIO:
1385 break;
1386 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001387 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001388 abort();
1389 }
1390 if (!file[0])
1391 return -2;
1392 bdrv_flags = 0;
1393 if (snapshot) {
1394 bdrv_flags |= BDRV_O_SNAPSHOT;
1395 cache = 2; /* always use write-back with snapshot */
1396 }
1397 if (cache == 0) /* no caching */
1398 bdrv_flags |= BDRV_O_NOCACHE;
1399 else if (cache == 2) /* write-back */
1400 bdrv_flags |= BDRV_O_CACHE_WB;
1401 else if (cache == 3) /* not specified */
1402 bdrv_flags |= BDRV_O_CACHE_DEF;
1403 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1404 fprintf(stderr, "qemu: could not open disk image %s\n",
1405 file);
1406 return -1;
1407 }
1408 if (bdrv_key_required(bdrv))
1409 autostart = 0;
1410 return drives_table_idx;
1411}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001412#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001413
1414static void numa_add(const char *optarg)
1415{
1416 char option[128];
1417 char *endptr;
1418 unsigned long long value, endvalue;
1419 int nodenr;
1420
1421 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1422 if (!strcmp(option, "node")) {
1423 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1424 nodenr = nb_numa_nodes;
1425 } else {
1426 nodenr = strtoull(option, NULL, 10);
1427 }
1428
1429 if (get_param_value(option, 128, "mem", optarg) == 0) {
1430 node_mem[nodenr] = 0;
1431 } else {
1432 value = strtoull(option, &endptr, 0);
1433 switch (*endptr) {
1434 case 0: case 'M': case 'm':
1435 value <<= 20;
1436 break;
1437 case 'G': case 'g':
1438 value <<= 30;
1439 break;
1440 }
1441 node_mem[nodenr] = value;
1442 }
1443 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1444 node_cpumask[nodenr] = 0;
1445 } else {
1446 value = strtoull(option, &endptr, 10);
1447 if (value >= 64) {
1448 value = 63;
1449 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1450 } else {
1451 if (*endptr == '-') {
1452 endvalue = strtoull(endptr+1, &endptr, 10);
1453 if (endvalue >= 63) {
1454 endvalue = 62;
1455 fprintf(stderr,
1456 "only 63 CPUs in NUMA mode supported.\n");
1457 }
1458 value = (1 << (endvalue + 1)) - (1 << value);
1459 } else {
1460 value = 1 << value;
1461 }
1462 }
1463 node_cpumask[nodenr] = value;
1464 }
1465 nb_numa_nodes++;
1466 }
1467 return;
1468}
1469
1470/***********************************************************/
1471/* USB devices */
1472
1473static USBPort *used_usb_ports;
1474static USBPort *free_usb_ports;
1475
1476/* ??? Maybe change this to register a hub to keep track of the topology. */
1477void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1478 usb_attachfn attach)
1479{
1480 port->opaque = opaque;
1481 port->index = index;
1482 port->attach = attach;
1483 port->next = free_usb_ports;
1484 free_usb_ports = port;
1485}
1486
1487int usb_device_add_dev(USBDevice *dev)
1488{
1489 USBPort *port;
1490
1491 /* Find a USB port to add the device to. */
1492 port = free_usb_ports;
1493 if (!port->next) {
1494 USBDevice *hub;
1495
1496 /* Create a new hub and chain it on. */
1497 free_usb_ports = NULL;
1498 port->next = used_usb_ports;
1499 used_usb_ports = port;
1500
1501 hub = usb_hub_init(VM_USB_HUB_SIZE);
1502 usb_attach(port, hub);
1503 port = free_usb_ports;
1504 }
1505
1506 free_usb_ports = port->next;
1507 port->next = used_usb_ports;
1508 used_usb_ports = port;
1509 usb_attach(port, dev);
1510 return 0;
1511}
1512
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001513#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001514static void usb_msd_password_cb(void *opaque, int err)
1515{
1516 USBDevice *dev = opaque;
1517
1518 if (!err)
1519 usb_device_add_dev(dev);
1520 else
1521 dev->handle_destroy(dev);
1522}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001523#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001524
1525static int usb_device_add(const char *devname, int is_hotplug)
1526{
1527 const char *p;
1528 USBDevice *dev;
1529
1530 if (!free_usb_ports)
1531 return -1;
1532
1533 if (strstart(devname, "host:", &p)) {
1534 dev = usb_host_device_open(p);
1535 } else if (!strcmp(devname, "mouse")) {
1536 dev = usb_mouse_init();
1537 } else if (!strcmp(devname, "tablet")) {
1538 dev = usb_tablet_init();
1539 } else if (!strcmp(devname, "keyboard")) {
1540 dev = usb_keyboard_init();
1541 } else if (strstart(devname, "disk:", &p)) {
1542#if 0
1543 BlockDriverState *bs;
1544#endif
1545 dev = usb_msd_init(p);
1546 if (!dev)
1547 return -1;
1548#if 0
1549 bs = usb_msd_get_bdrv(dev);
1550 if (bdrv_key_required(bs)) {
1551 autostart = 0;
1552 if (is_hotplug) {
1553 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1554 dev);
1555 return 0;
1556 }
1557 }
1558 } else if (!strcmp(devname, "wacom-tablet")) {
1559 dev = usb_wacom_init();
1560 } else if (strstart(devname, "serial:", &p)) {
1561 dev = usb_serial_init(p);
1562#ifdef CONFIG_BRLAPI
1563 } else if (!strcmp(devname, "braille")) {
1564 dev = usb_baum_init();
1565#endif
1566 } else if (strstart(devname, "net:", &p)) {
1567 int nic = nb_nics;
1568
1569 if (net_client_init("nic", p) < 0)
1570 return -1;
1571 nd_table[nic].model = "usb";
1572 dev = usb_net_init(&nd_table[nic]);
1573 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1574 dev = usb_bt_init(devname[2] ? hci_init(p) :
1575 bt_new_hci(qemu_find_bt_vlan(0)));
1576#endif
1577 } else {
1578 return -1;
1579 }
1580 if (!dev)
1581 return -1;
1582
1583 return usb_device_add_dev(dev);
1584}
1585
1586int usb_device_del_addr(int bus_num, int addr)
1587{
1588 USBPort *port;
1589 USBPort **lastp;
1590 USBDevice *dev;
1591
1592 if (!used_usb_ports)
1593 return -1;
1594
1595 if (bus_num != 0)
1596 return -1;
1597
1598 lastp = &used_usb_ports;
1599 port = used_usb_ports;
1600 while (port && port->dev->addr != addr) {
1601 lastp = &port->next;
1602 port = port->next;
1603 }
1604
1605 if (!port)
1606 return -1;
1607
1608 dev = port->dev;
1609 *lastp = port->next;
1610 usb_attach(port, NULL);
1611 dev->handle_destroy(dev);
1612 port->next = free_usb_ports;
1613 free_usb_ports = port;
1614 return 0;
1615}
1616
1617static int usb_device_del(const char *devname)
1618{
1619 int bus_num, addr;
1620 const char *p;
1621
1622 if (strstart(devname, "host:", &p))
1623 return usb_host_device_close(p);
1624
1625 if (!used_usb_ports)
1626 return -1;
1627
1628 p = strchr(devname, '.');
1629 if (!p)
1630 return -1;
1631 bus_num = strtoul(devname, NULL, 0);
1632 addr = strtoul(p + 1, NULL, 0);
1633
1634 return usb_device_del_addr(bus_num, addr);
1635}
1636
1637void do_usb_add(Monitor *mon, const char *devname)
1638{
1639 usb_device_add(devname, 1);
1640}
1641
1642void do_usb_del(Monitor *mon, const char *devname)
1643{
1644 usb_device_del(devname);
1645}
1646
1647void usb_info(Monitor *mon)
1648{
1649 USBDevice *dev;
1650 USBPort *port;
1651 const char *speed_str;
1652
1653 if (!usb_enabled) {
1654 monitor_printf(mon, "USB support not enabled\n");
1655 return;
1656 }
1657
1658 for (port = used_usb_ports; port; port = port->next) {
1659 dev = port->dev;
1660 if (!dev)
1661 continue;
1662 switch(dev->speed) {
1663 case USB_SPEED_LOW:
1664 speed_str = "1.5";
1665 break;
1666 case USB_SPEED_FULL:
1667 speed_str = "12";
1668 break;
1669 case USB_SPEED_HIGH:
1670 speed_str = "480";
1671 break;
1672 default:
1673 speed_str = "?";
1674 break;
1675 }
1676 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1677 0, dev->addr, speed_str, dev->devname);
1678 }
1679}
1680
1681/***********************************************************/
1682/* PCMCIA/Cardbus */
1683
1684static struct pcmcia_socket_entry_s {
1685 PCMCIASocket *socket;
1686 struct pcmcia_socket_entry_s *next;
1687} *pcmcia_sockets = 0;
1688
1689void pcmcia_socket_register(PCMCIASocket *socket)
1690{
1691 struct pcmcia_socket_entry_s *entry;
1692
1693 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1694 entry->socket = socket;
1695 entry->next = pcmcia_sockets;
1696 pcmcia_sockets = entry;
1697}
1698
1699void pcmcia_socket_unregister(PCMCIASocket *socket)
1700{
1701 struct pcmcia_socket_entry_s *entry, **ptr;
1702
1703 ptr = &pcmcia_sockets;
1704 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1705 if (entry->socket == socket) {
1706 *ptr = entry->next;
1707 qemu_free(entry);
1708 }
1709}
1710
1711void pcmcia_info(Monitor *mon)
1712{
1713 struct pcmcia_socket_entry_s *iter;
1714
1715 if (!pcmcia_sockets)
1716 monitor_printf(mon, "No PCMCIA sockets\n");
1717
1718 for (iter = pcmcia_sockets; iter; iter = iter->next)
1719 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1720 iter->socket->attached ? iter->socket->card_string :
1721 "Empty");
1722}
1723
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001724#ifdef _WIN32
1725/***********************************************************/
1726/* Polling handling */
1727
1728typedef struct PollingEntry {
1729 PollingFunc *func;
1730 void *opaque;
1731 struct PollingEntry *next;
1732} PollingEntry;
1733
1734static PollingEntry *first_polling_entry;
1735
1736int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1737{
1738 PollingEntry **ppe, *pe;
1739 pe = qemu_mallocz(sizeof(PollingEntry));
1740 pe->func = func;
1741 pe->opaque = opaque;
1742 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1743 *ppe = pe;
1744 return 0;
1745}
1746
1747void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1748{
1749 PollingEntry **ppe, *pe;
1750 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1751 pe = *ppe;
1752 if (pe->func == func && pe->opaque == opaque) {
1753 *ppe = pe->next;
1754 qemu_free(pe);
1755 break;
1756 }
1757 }
1758}
1759
1760/***********************************************************/
1761/* Wait objects support */
1762typedef struct WaitObjects {
1763 int num;
1764 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1765 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1766 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1767} WaitObjects;
1768
1769static WaitObjects wait_objects = {0};
1770
1771int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1772{
1773 WaitObjects *w = &wait_objects;
1774
1775 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1776 return -1;
1777 w->events[w->num] = handle;
1778 w->func[w->num] = func;
1779 w->opaque[w->num] = opaque;
1780 w->num++;
1781 return 0;
1782}
1783
1784void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1785{
1786 int i, found;
1787 WaitObjects *w = &wait_objects;
1788
1789 found = 0;
1790 for (i = 0; i < w->num; i++) {
1791 if (w->events[i] == handle)
1792 found = 1;
1793 if (found) {
1794 w->events[i] = w->events[i + 1];
1795 w->func[i] = w->func[i + 1];
1796 w->opaque[i] = w->opaque[i + 1];
1797 }
1798 }
1799 if (found)
1800 w->num--;
1801}
1802#endif
1803
1804/***********************************************************/
1805/* ram save/restore */
1806
1807static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1808{
1809 int v;
1810
1811 v = qemu_get_byte(f);
1812 switch(v) {
1813 case 0:
1814 if (qemu_get_buffer(f, buf, len) != len)
1815 return -EIO;
1816 break;
1817 case 1:
1818 v = qemu_get_byte(f);
1819 memset(buf, v, len);
1820 break;
1821 default:
1822 return -EINVAL;
1823 }
1824
1825 if (qemu_file_has_error(f))
1826 return -EIO;
1827
1828 return 0;
1829}
1830
1831static int ram_load_v1(QEMUFile *f, void *opaque)
1832{
1833 int ret;
1834 ram_addr_t i;
1835
1836 if (qemu_get_be32(f) != last_ram_offset)
1837 return -EINVAL;
1838 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1839 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1840 if (ret)
1841 return ret;
1842 }
1843 return 0;
1844}
1845
1846#define BDRV_HASH_BLOCK_SIZE 1024
1847#define IOBUF_SIZE 4096
1848#define RAM_CBLOCK_MAGIC 0xfabe
1849
1850typedef struct RamDecompressState {
1851 z_stream zstream;
1852 QEMUFile *f;
1853 uint8_t buf[IOBUF_SIZE];
1854} RamDecompressState;
1855
1856static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1857{
1858 int ret;
1859 memset(s, 0, sizeof(*s));
1860 s->f = f;
1861 ret = inflateInit(&s->zstream);
1862 if (ret != Z_OK)
1863 return -1;
1864 return 0;
1865}
1866
1867static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1868{
1869 int ret, clen;
1870
1871 s->zstream.avail_out = len;
1872 s->zstream.next_out = buf;
1873 while (s->zstream.avail_out > 0) {
1874 if (s->zstream.avail_in == 0) {
1875 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1876 return -1;
1877 clen = qemu_get_be16(s->f);
1878 if (clen > IOBUF_SIZE)
1879 return -1;
1880 qemu_get_buffer(s->f, s->buf, clen);
1881 s->zstream.avail_in = clen;
1882 s->zstream.next_in = s->buf;
1883 }
1884 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1885 if (ret != Z_OK && ret != Z_STREAM_END) {
1886 return -1;
1887 }
1888 }
1889 return 0;
1890}
1891
1892static void ram_decompress_close(RamDecompressState *s)
1893{
1894 inflateEnd(&s->zstream);
1895}
1896
1897#define RAM_SAVE_FLAG_FULL 0x01
1898#define RAM_SAVE_FLAG_COMPRESS 0x02
1899#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1900#define RAM_SAVE_FLAG_PAGE 0x08
1901#define RAM_SAVE_FLAG_EOS 0x10
1902
1903static int is_dup_page(uint8_t *page, uint8_t ch)
1904{
1905 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1906 uint32_t *array = (uint32_t *)page;
1907 int i;
1908
1909 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1910 if (array[i] != val)
1911 return 0;
1912 }
1913
1914 return 1;
1915}
1916
1917static int ram_save_block(QEMUFile *f)
1918{
1919 static ram_addr_t current_addr = 0;
1920 ram_addr_t saved_addr = current_addr;
1921 ram_addr_t addr = 0;
1922 int found = 0;
1923
1924 while (addr < last_ram_offset) {
1925 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1926 uint8_t *p;
1927
1928 cpu_physical_memory_reset_dirty(current_addr,
1929 current_addr + TARGET_PAGE_SIZE,
1930 MIGRATION_DIRTY_FLAG);
1931
1932 p = qemu_get_ram_ptr(current_addr);
1933
1934 if (is_dup_page(p, *p)) {
1935 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
1936 qemu_put_byte(f, *p);
1937 } else {
1938 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
1939 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
1940 }
1941
1942 found = 1;
1943 break;
1944 }
1945 addr += TARGET_PAGE_SIZE;
1946 current_addr = (saved_addr + addr) % last_ram_offset;
1947 }
1948
1949 return found;
1950}
1951
1952static uint64_t bytes_transferred = 0;
1953
1954static ram_addr_t ram_save_remaining(void)
1955{
1956 ram_addr_t addr;
1957 ram_addr_t count = 0;
1958
1959 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
1960 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1961 count++;
1962 }
1963
1964 return count;
1965}
1966
1967uint64_t ram_bytes_remaining(void)
1968{
1969 return ram_save_remaining() * TARGET_PAGE_SIZE;
1970}
1971
1972uint64_t ram_bytes_transferred(void)
1973{
1974 return bytes_transferred;
1975}
1976
1977uint64_t ram_bytes_total(void)
1978{
1979 return last_ram_offset;
1980}
1981
1982static int ram_save_live(QEMUFile *f, int stage, void *opaque)
1983{
1984 ram_addr_t addr;
1985 uint64_t bytes_transferred_last;
1986 double bwidth = 0;
1987 uint64_t expected_time = 0;
1988
1989 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
1990
1991 if (stage == 1) {
1992 /* Make sure all dirty bits are set */
1993 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
1994 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1995 cpu_physical_memory_set_dirty(addr);
1996 }
1997
1998 /* Enable dirty memory tracking */
1999 cpu_physical_memory_set_dirty_tracking(1);
2000
2001 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2002 }
2003
2004 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002005 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002006
2007 while (!qemu_file_rate_limit(f)) {
2008 int ret;
2009
2010 ret = ram_save_block(f);
2011 bytes_transferred += ret * TARGET_PAGE_SIZE;
2012 if (ret == 0) /* no more blocks */
2013 break;
2014 }
2015
David Turner6a9ef172010-09-09 22:54:36 +02002016 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002017 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2018
2019 /* if we haven't transferred anything this round, force expected_time to a
2020 * a very high value, but without crashing */
2021 if (bwidth == 0)
2022 bwidth = 0.000001;
2023
2024 /* try transferring iterative blocks of memory */
2025
2026 if (stage == 3) {
2027
2028 /* flush all remaining blocks regardless of rate limiting */
2029 while (ram_save_block(f) != 0) {
2030 bytes_transferred += TARGET_PAGE_SIZE;
2031 }
2032 cpu_physical_memory_set_dirty_tracking(0);
2033 }
2034
2035 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2036
2037 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2038
2039 return (stage == 2) && (expected_time <= migrate_max_downtime());
2040}
2041
2042static int ram_load_dead(QEMUFile *f, void *opaque)
2043{
2044 RamDecompressState s1, *s = &s1;
2045 uint8_t buf[10];
2046 ram_addr_t i;
2047
2048 if (ram_decompress_open(s, f) < 0)
2049 return -EINVAL;
2050 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2051 if (ram_decompress_buf(s, buf, 1) < 0) {
2052 fprintf(stderr, "Error while reading ram block header\n");
2053 goto error;
2054 }
2055 if (buf[0] == 0) {
2056 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2057 BDRV_HASH_BLOCK_SIZE) < 0) {
2058 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2059 goto error;
2060 }
2061 } else {
2062 error:
2063 printf("Error block header\n");
2064 return -EINVAL;
2065 }
2066 }
2067 ram_decompress_close(s);
2068
2069 return 0;
2070}
2071
2072static int ram_load(QEMUFile *f, void *opaque, int version_id)
2073{
2074 ram_addr_t addr;
2075 int flags;
2076
2077 if (version_id == 1)
2078 return ram_load_v1(f, opaque);
2079
2080 if (version_id == 2) {
2081 if (qemu_get_be32(f) != last_ram_offset)
2082 return -EINVAL;
2083 return ram_load_dead(f, opaque);
2084 }
2085
2086 if (version_id != 3)
2087 return -EINVAL;
2088
2089 do {
2090 addr = qemu_get_be64(f);
2091
2092 flags = addr & ~TARGET_PAGE_MASK;
2093 addr &= TARGET_PAGE_MASK;
2094
2095 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2096 if (addr != last_ram_offset)
2097 return -EINVAL;
2098 }
2099
2100 if (flags & RAM_SAVE_FLAG_FULL) {
2101 if (ram_load_dead(f, opaque) < 0)
2102 return -EINVAL;
2103 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002104
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002105 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2106 uint8_t ch = qemu_get_byte(f);
2107 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2108 } else if (flags & RAM_SAVE_FLAG_PAGE)
2109 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2110 } while (!(flags & RAM_SAVE_FLAG_EOS));
2111
2112 return 0;
2113}
2114
2115void qemu_service_io(void)
2116{
2117 qemu_notify_event();
2118}
2119
2120/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002121/* machine registration */
2122
2123static QEMUMachine *first_machine = NULL;
2124QEMUMachine *current_machine = NULL;
2125
2126int qemu_register_machine(QEMUMachine *m)
2127{
2128 QEMUMachine **pm;
2129 pm = &first_machine;
2130 while (*pm != NULL)
2131 pm = &(*pm)->next;
2132 m->next = NULL;
2133 *pm = m;
2134 return 0;
2135}
2136
2137static QEMUMachine *find_machine(const char *name)
2138{
2139 QEMUMachine *m;
2140
2141 for(m = first_machine; m != NULL; m = m->next) {
2142 if (!strcmp(m->name, name))
2143 return m;
2144 }
2145 return NULL;
2146}
2147
2148static QEMUMachine *find_default_machine(void)
2149{
2150 QEMUMachine *m;
2151
2152 for(m = first_machine; m != NULL; m = m->next) {
2153 if (m->is_default) {
2154 return m;
2155 }
2156 }
2157 return NULL;
2158}
2159
2160/***********************************************************/
2161/* main execution loop */
2162
2163static void gui_update(void *opaque)
2164{
2165 uint64_t interval = GUI_REFRESH_INTERVAL;
2166 DisplayState *ds = opaque;
2167 DisplayChangeListener *dcl = ds->listeners;
2168
2169 dpy_refresh(ds);
2170
2171 while (dcl != NULL) {
2172 if (dcl->gui_timer_interval &&
2173 dcl->gui_timer_interval < interval)
2174 interval = dcl->gui_timer_interval;
2175 dcl = dcl->next;
2176 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002177 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002178}
2179
2180static void nographic_update(void *opaque)
2181{
2182 uint64_t interval = GUI_REFRESH_INTERVAL;
2183
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002184 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002185}
2186
2187struct vm_change_state_entry {
2188 VMChangeStateHandler *cb;
2189 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002190 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002191};
2192
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002193static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002194
2195VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2196 void *opaque)
2197{
2198 VMChangeStateEntry *e;
2199
2200 e = qemu_mallocz(sizeof (*e));
2201
2202 e->cb = cb;
2203 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002204 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002205 return e;
2206}
2207
2208void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2209{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002210 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002211 qemu_free (e);
2212}
2213
2214static void vm_state_notify(int running, int reason)
2215{
2216 VMChangeStateEntry *e;
2217
2218 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2219 e->cb(e->opaque, running, reason);
2220 }
2221}
2222
2223static void resume_all_vcpus(void);
2224static void pause_all_vcpus(void);
2225
2226void vm_start(void)
2227{
2228 if (!vm_running) {
2229 cpu_enable_ticks();
2230 vm_running = 1;
2231 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002232 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002233 resume_all_vcpus();
2234 }
2235}
2236
2237/* reset/shutdown handler */
2238
2239typedef struct QEMUResetEntry {
2240 QEMUResetHandler *func;
2241 void *opaque;
2242 int order;
2243 struct QEMUResetEntry *next;
2244} QEMUResetEntry;
2245
2246static QEMUResetEntry *first_reset_entry;
2247static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002248static int shutdown_requested, shutdown_signal = -1;
2249static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002250static int powerdown_requested;
2251static int debug_requested;
2252static int vmstop_requested;
2253
2254int qemu_shutdown_requested(void)
2255{
2256 int r = shutdown_requested;
2257 shutdown_requested = 0;
2258 return r;
2259}
2260
2261int qemu_reset_requested(void)
2262{
2263 int r = reset_requested;
2264 reset_requested = 0;
2265 return r;
2266}
2267
2268int qemu_powerdown_requested(void)
2269{
2270 int r = powerdown_requested;
2271 powerdown_requested = 0;
2272 return r;
2273}
2274
2275static int qemu_debug_requested(void)
2276{
2277 int r = debug_requested;
2278 debug_requested = 0;
2279 return r;
2280}
2281
2282static int qemu_vmstop_requested(void)
2283{
2284 int r = vmstop_requested;
2285 vmstop_requested = 0;
2286 return r;
2287}
2288
2289static void do_vm_stop(int reason)
2290{
2291 if (vm_running) {
2292 cpu_disable_ticks();
2293 vm_running = 0;
2294 pause_all_vcpus();
2295 vm_state_notify(0, reason);
2296 }
2297}
2298
2299void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2300{
2301 QEMUResetEntry **pre, *re;
2302
2303 pre = &first_reset_entry;
2304 while (*pre != NULL && (*pre)->order >= order) {
2305 pre = &(*pre)->next;
2306 }
2307 re = qemu_mallocz(sizeof(QEMUResetEntry));
2308 re->func = func;
2309 re->opaque = opaque;
2310 re->order = order;
2311 re->next = NULL;
2312 *pre = re;
2313}
2314
2315void qemu_system_reset(void)
2316{
2317 QEMUResetEntry *re;
2318
2319 /* reset all devices */
2320 for(re = first_reset_entry; re != NULL; re = re->next) {
2321 re->func(re->opaque);
2322 }
2323}
2324
2325void qemu_system_reset_request(void)
2326{
2327 if (no_reboot) {
2328 shutdown_requested = 1;
2329 } else {
2330 reset_requested = 1;
2331 }
2332 qemu_notify_event();
2333}
2334
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002335void qemu_system_killed(int signal, pid_t pid)
2336{
2337 shutdown_signal = signal;
2338 shutdown_pid = pid;
2339 qemu_system_shutdown_request();
2340}
2341
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002342void qemu_system_shutdown_request(void)
2343{
2344 shutdown_requested = 1;
2345 qemu_notify_event();
2346}
2347
2348void qemu_system_powerdown_request(void)
2349{
2350 powerdown_requested = 1;
2351 qemu_notify_event();
2352}
2353
2354#ifdef CONFIG_IOTHREAD
2355static void qemu_system_vmstop_request(int reason)
2356{
2357 vmstop_requested = reason;
2358 qemu_notify_event();
2359}
2360#endif
2361
2362#ifndef _WIN32
2363static int io_thread_fd = -1;
2364
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002365#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002366static void qemu_event_increment(void)
2367{
2368 static const char byte = 0;
2369
2370 if (io_thread_fd == -1)
2371 return;
2372
2373 write(io_thread_fd, &byte, sizeof(byte));
2374}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002375#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002376
2377static void qemu_event_read(void *opaque)
2378{
2379 int fd = (unsigned long)opaque;
2380 ssize_t len;
2381
2382 /* Drain the notify pipe */
2383 do {
2384 char buffer[512];
2385 len = read(fd, buffer, sizeof(buffer));
2386 } while ((len == -1 && errno == EINTR) || len > 0);
2387}
2388
2389static int qemu_event_init(void)
2390{
2391 int err;
2392 int fds[2];
2393
2394 err = pipe(fds);
2395 if (err == -1)
2396 return -errno;
2397
2398 err = fcntl_setfl(fds[0], O_NONBLOCK);
2399 if (err < 0)
2400 goto fail;
2401
2402 err = fcntl_setfl(fds[1], O_NONBLOCK);
2403 if (err < 0)
2404 goto fail;
2405
2406 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2407 (void *)(unsigned long)fds[0]);
2408
2409 io_thread_fd = fds[1];
2410 return 0;
2411
2412fail:
2413 close(fds[0]);
2414 close(fds[1]);
2415 return err;
2416}
2417#else
2418HANDLE qemu_event_handle;
2419
2420static void dummy_event_handler(void *opaque)
2421{
2422}
2423
2424static int qemu_event_init(void)
2425{
2426 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2427 if (!qemu_event_handle) {
2428 perror("Failed CreateEvent");
2429 return -1;
2430 }
2431 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2432 return 0;
2433}
2434
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002435#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002436static void qemu_event_increment(void)
2437{
2438 SetEvent(qemu_event_handle);
2439}
2440#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002441#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002442
2443static int cpu_can_run(CPUState *env)
2444{
2445 if (env->stop)
2446 return 0;
2447 if (env->stopped)
2448 return 0;
2449 return 1;
2450}
2451
2452#ifndef CONFIG_IOTHREAD
2453static int qemu_init_main_loop(void)
2454{
2455 return qemu_event_init();
2456}
2457
2458void qemu_init_vcpu(void *_env)
2459{
2460 CPUState *env = _env;
2461
2462 if (kvm_enabled())
2463 kvm_init_vcpu(env);
2464 return;
2465}
2466
2467int qemu_cpu_self(void *env)
2468{
2469 return 1;
2470}
2471
2472static void resume_all_vcpus(void)
2473{
2474}
2475
2476static void pause_all_vcpus(void)
2477{
2478}
2479
2480void qemu_cpu_kick(void *env)
2481{
2482 return;
2483}
2484
2485void qemu_notify_event(void)
2486{
2487 CPUState *env = cpu_single_env;
2488
2489 if (env) {
2490 cpu_exit(env);
2491#ifdef USE_KQEMU
2492 if (env->kqemu_enabled)
2493 kqemu_cpu_interrupt(env);
2494#endif
2495 }
2496}
2497
2498#define qemu_mutex_lock_iothread() do { } while (0)
2499#define qemu_mutex_unlock_iothread() do { } while (0)
2500
2501void vm_stop(int reason)
2502{
2503 do_vm_stop(reason);
2504}
2505
2506#else /* CONFIG_IOTHREAD */
2507
2508#include "qemu-thread.h"
2509
2510QemuMutex qemu_global_mutex;
2511static QemuMutex qemu_fair_mutex;
2512
2513static QemuThread io_thread;
2514
2515static QemuThread *tcg_cpu_thread;
2516static QemuCond *tcg_halt_cond;
2517
2518static int qemu_system_ready;
2519/* cpu creation */
2520static QemuCond qemu_cpu_cond;
2521/* system init */
2522static QemuCond qemu_system_cond;
2523static QemuCond qemu_pause_cond;
2524
2525static void block_io_signals(void);
2526static void unblock_io_signals(void);
2527static int tcg_has_work(void);
2528
2529static int qemu_init_main_loop(void)
2530{
2531 int ret;
2532
2533 ret = qemu_event_init();
2534 if (ret)
2535 return ret;
2536
2537 qemu_cond_init(&qemu_pause_cond);
2538 qemu_mutex_init(&qemu_fair_mutex);
2539 qemu_mutex_init(&qemu_global_mutex);
2540 qemu_mutex_lock(&qemu_global_mutex);
2541
2542 unblock_io_signals();
2543 qemu_thread_self(&io_thread);
2544
2545 return 0;
2546}
2547
2548static void qemu_wait_io_event(CPUState *env)
2549{
2550 while (!tcg_has_work())
2551 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2552
2553 qemu_mutex_unlock(&qemu_global_mutex);
2554
2555 /*
2556 * Users of qemu_global_mutex can be starved, having no chance
2557 * to acquire it since this path will get to it first.
2558 * So use another lock to provide fairness.
2559 */
2560 qemu_mutex_lock(&qemu_fair_mutex);
2561 qemu_mutex_unlock(&qemu_fair_mutex);
2562
2563 qemu_mutex_lock(&qemu_global_mutex);
2564 if (env->stop) {
2565 env->stop = 0;
2566 env->stopped = 1;
2567 qemu_cond_signal(&qemu_pause_cond);
2568 }
2569}
2570
2571static int qemu_cpu_exec(CPUState *env);
2572
2573static void *kvm_cpu_thread_fn(void *arg)
2574{
2575 CPUState *env = arg;
2576
2577 block_io_signals();
2578 qemu_thread_self(env->thread);
2579
2580 /* signal CPU creation */
2581 qemu_mutex_lock(&qemu_global_mutex);
2582 env->created = 1;
2583 qemu_cond_signal(&qemu_cpu_cond);
2584
2585 /* and wait for machine initialization */
2586 while (!qemu_system_ready)
2587 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2588
2589 while (1) {
2590 if (cpu_can_run(env))
2591 qemu_cpu_exec(env);
2592 qemu_wait_io_event(env);
2593 }
2594
2595 return NULL;
2596}
2597
2598static void tcg_cpu_exec(void);
2599
2600static void *tcg_cpu_thread_fn(void *arg)
2601{
2602 CPUState *env = arg;
2603
2604 block_io_signals();
2605 qemu_thread_self(env->thread);
2606
2607 /* signal CPU creation */
2608 qemu_mutex_lock(&qemu_global_mutex);
2609 for (env = first_cpu; env != NULL; env = env->next_cpu)
2610 env->created = 1;
2611 qemu_cond_signal(&qemu_cpu_cond);
2612
2613 /* and wait for machine initialization */
2614 while (!qemu_system_ready)
2615 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2616
2617 while (1) {
2618 tcg_cpu_exec();
2619 qemu_wait_io_event(cur_cpu);
2620 }
2621
2622 return NULL;
2623}
2624
2625void qemu_cpu_kick(void *_env)
2626{
2627 CPUState *env = _env;
2628 qemu_cond_broadcast(env->halt_cond);
2629 if (kvm_enabled())
2630 qemu_thread_signal(env->thread, SIGUSR1);
2631}
2632
2633int qemu_cpu_self(void *env)
2634{
2635 return (cpu_single_env != NULL);
2636}
2637
2638static void cpu_signal(int sig)
2639{
2640 if (cpu_single_env)
2641 cpu_exit(cpu_single_env);
2642}
2643
2644static void block_io_signals(void)
2645{
2646 sigset_t set;
2647 struct sigaction sigact;
2648
2649 sigemptyset(&set);
2650 sigaddset(&set, SIGUSR2);
2651 sigaddset(&set, SIGIO);
2652 sigaddset(&set, SIGALRM);
2653 pthread_sigmask(SIG_BLOCK, &set, NULL);
2654
2655 sigemptyset(&set);
2656 sigaddset(&set, SIGUSR1);
2657 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2658
2659 memset(&sigact, 0, sizeof(sigact));
2660 sigact.sa_handler = cpu_signal;
2661 sigaction(SIGUSR1, &sigact, NULL);
2662}
2663
2664static void unblock_io_signals(void)
2665{
2666 sigset_t set;
2667
2668 sigemptyset(&set);
2669 sigaddset(&set, SIGUSR2);
2670 sigaddset(&set, SIGIO);
2671 sigaddset(&set, SIGALRM);
2672 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2673
2674 sigemptyset(&set);
2675 sigaddset(&set, SIGUSR1);
2676 pthread_sigmask(SIG_BLOCK, &set, NULL);
2677}
2678
2679static void qemu_signal_lock(unsigned int msecs)
2680{
2681 qemu_mutex_lock(&qemu_fair_mutex);
2682
2683 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2684 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2685 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2686 break;
2687 }
2688 qemu_mutex_unlock(&qemu_fair_mutex);
2689}
2690
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002691void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002692{
2693 if (kvm_enabled()) {
2694 qemu_mutex_lock(&qemu_fair_mutex);
2695 qemu_mutex_lock(&qemu_global_mutex);
2696 qemu_mutex_unlock(&qemu_fair_mutex);
2697 } else
2698 qemu_signal_lock(100);
2699}
2700
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002701void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002702{
2703 qemu_mutex_unlock(&qemu_global_mutex);
2704}
2705
2706static int all_vcpus_paused(void)
2707{
2708 CPUState *penv = first_cpu;
2709
2710 while (penv) {
2711 if (!penv->stopped)
2712 return 0;
2713 penv = (CPUState *)penv->next_cpu;
2714 }
2715
2716 return 1;
2717}
2718
2719static void pause_all_vcpus(void)
2720{
2721 CPUState *penv = first_cpu;
2722
2723 while (penv) {
2724 penv->stop = 1;
2725 qemu_thread_signal(penv->thread, SIGUSR1);
2726 qemu_cpu_kick(penv);
2727 penv = (CPUState *)penv->next_cpu;
2728 }
2729
2730 while (!all_vcpus_paused()) {
2731 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2732 penv = first_cpu;
2733 while (penv) {
2734 qemu_thread_signal(penv->thread, SIGUSR1);
2735 penv = (CPUState *)penv->next_cpu;
2736 }
2737 }
2738}
2739
2740static void resume_all_vcpus(void)
2741{
2742 CPUState *penv = first_cpu;
2743
2744 while (penv) {
2745 penv->stop = 0;
2746 penv->stopped = 0;
2747 qemu_thread_signal(penv->thread, SIGUSR1);
2748 qemu_cpu_kick(penv);
2749 penv = (CPUState *)penv->next_cpu;
2750 }
2751}
2752
2753static void tcg_init_vcpu(void *_env)
2754{
2755 CPUState *env = _env;
2756 /* share a single thread for all cpus with TCG */
2757 if (!tcg_cpu_thread) {
2758 env->thread = qemu_mallocz(sizeof(QemuThread));
2759 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2760 qemu_cond_init(env->halt_cond);
2761 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2762 while (env->created == 0)
2763 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2764 tcg_cpu_thread = env->thread;
2765 tcg_halt_cond = env->halt_cond;
2766 } else {
2767 env->thread = tcg_cpu_thread;
2768 env->halt_cond = tcg_halt_cond;
2769 }
2770}
2771
2772static void kvm_start_vcpu(CPUState *env)
2773{
2774#if 0
2775 kvm_init_vcpu(env);
2776 env->thread = qemu_mallocz(sizeof(QemuThread));
2777 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2778 qemu_cond_init(env->halt_cond);
2779 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2780 while (env->created == 0)
2781 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2782#endif
2783}
2784
2785void qemu_init_vcpu(void *_env)
2786{
2787 CPUState *env = _env;
2788
2789 if (kvm_enabled())
2790 kvm_start_vcpu(env);
2791 else
2792 tcg_init_vcpu(env);
2793}
2794
2795void qemu_notify_event(void)
2796{
2797 qemu_event_increment();
2798}
2799
2800void vm_stop(int reason)
2801{
2802 QemuThread me;
2803 qemu_thread_self(&me);
2804
2805 if (!qemu_thread_equal(&me, &io_thread)) {
2806 qemu_system_vmstop_request(reason);
2807 /*
2808 * FIXME: should not return to device code in case
2809 * vm_stop() has been requested.
2810 */
2811 if (cpu_single_env) {
2812 cpu_exit(cpu_single_env);
2813 cpu_single_env->stop = 1;
2814 }
2815 return;
2816 }
2817 do_vm_stop(reason);
2818}
2819
2820#endif
2821
2822
2823#ifdef _WIN32
2824static void host_main_loop_wait(int *timeout)
2825{
2826 int ret, ret2, i;
2827 PollingEntry *pe;
2828
2829
2830 /* XXX: need to suppress polling by better using win32 events */
2831 ret = 0;
2832 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2833 ret |= pe->func(pe->opaque);
2834 }
2835 if (ret == 0) {
2836 int err;
2837 WaitObjects *w = &wait_objects;
2838
2839 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2840 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2841 if (w->func[ret - WAIT_OBJECT_0])
2842 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2843
2844 /* Check for additional signaled events */
2845 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2846
2847 /* Check if event is signaled */
2848 ret2 = WaitForSingleObject(w->events[i], 0);
2849 if(ret2 == WAIT_OBJECT_0) {
2850 if (w->func[i])
2851 w->func[i](w->opaque[i]);
2852 } else if (ret2 == WAIT_TIMEOUT) {
2853 } else {
2854 err = GetLastError();
2855 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2856 }
2857 }
2858 } else if (ret == WAIT_TIMEOUT) {
2859 } else {
2860 err = GetLastError();
2861 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2862 }
2863 }
2864
2865 *timeout = 0;
2866}
2867#else
2868static void host_main_loop_wait(int *timeout)
2869{
2870}
2871#endif
2872
2873void main_loop_wait(int timeout)
2874{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002875 fd_set rfds, wfds, xfds;
2876 int ret, nfds;
2877 struct timeval tv;
2878
2879 qemu_bh_update_timeout(&timeout);
2880
2881 host_main_loop_wait(&timeout);
2882
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002883
2884 tv.tv_sec = timeout / 1000;
2885 tv.tv_usec = (timeout % 1000) * 1000;
2886
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002887 /* poll any events */
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002888
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002889 /* XXX: separate device handlers from system ones */
2890 nfds = -1;
2891 FD_ZERO(&rfds);
2892 FD_ZERO(&wfds);
2893 FD_ZERO(&xfds);
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002894 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002895 if (slirp_is_inited()) {
2896 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2897 }
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002898
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002899 qemu_mutex_unlock_iothread();
2900 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2901 qemu_mutex_lock_iothread();
David 'Digit' Turner8354d2d2011-05-11 00:19:06 +02002902 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002903 if (slirp_is_inited()) {
2904 if (ret < 0) {
2905 FD_ZERO(&rfds);
2906 FD_ZERO(&wfds);
2907 FD_ZERO(&xfds);
2908 }
2909 slirp_select_poll(&rfds, &wfds, &xfds);
2910 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002911 charpipe_poll();
2912
David Turner6a9ef172010-09-09 22:54:36 +02002913 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002914
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002915 /* Check bottom-halves last in case any of the earlier events triggered
2916 them. */
2917 qemu_bh_poll();
2918
2919}
2920
2921static int qemu_cpu_exec(CPUState *env)
2922{
2923 int ret;
2924#ifdef CONFIG_PROFILER
2925 int64_t ti;
2926#endif
2927
2928#ifdef CONFIG_PROFILER
2929 ti = profile_getclock();
2930#endif
2931 if (use_icount) {
2932 int64_t count;
2933 int decr;
2934 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
2935 env->icount_decr.u16.low = 0;
2936 env->icount_extra = 0;
David 'Digit' Turner317c9d52011-05-10 06:38:21 +02002937 count = qemu_next_icount_deadline();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002938 count = (count + (1 << icount_time_shift) - 1)
2939 >> icount_time_shift;
2940 qemu_icount += count;
2941 decr = (count > 0xffff) ? 0xffff : count;
2942 count -= decr;
2943 env->icount_decr.u16.low = decr;
2944 env->icount_extra = count;
2945 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07002946#ifdef CONFIG_TRACE
2947 if (tbflush_requested) {
2948 tbflush_requested = 0;
2949 tb_flush(env);
2950 return EXCP_INTERRUPT;
2951 }
2952#endif
2953
2954
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002955 ret = cpu_exec(env);
2956#ifdef CONFIG_PROFILER
2957 qemu_time += profile_getclock() - ti;
2958#endif
2959 if (use_icount) {
2960 /* Fold pending instructions back into the
2961 instruction counter, and clear the interrupt flag. */
2962 qemu_icount -= (env->icount_decr.u16.low
2963 + env->icount_extra);
2964 env->icount_decr.u32 = 0;
2965 env->icount_extra = 0;
2966 }
2967 return ret;
2968}
2969
2970static void tcg_cpu_exec(void)
2971{
2972 int ret = 0;
2973
2974 if (next_cpu == NULL)
2975 next_cpu = first_cpu;
2976 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
2977 CPUState *env = cur_cpu = next_cpu;
2978
2979 if (!vm_running)
2980 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02002981 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002982 break;
2983 }
2984 if (cpu_can_run(env))
2985 ret = qemu_cpu_exec(env);
2986 if (ret == EXCP_DEBUG) {
2987 gdb_set_stop_cpu(env);
2988 debug_requested = 1;
2989 break;
2990 }
2991 }
2992}
2993
2994static int cpu_has_work(CPUState *env)
2995{
2996 if (env->stop)
2997 return 1;
2998 if (env->stopped)
2999 return 0;
3000 if (!env->halted)
3001 return 1;
3002 if (qemu_cpu_has_work(env))
3003 return 1;
3004 return 0;
3005}
3006
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003007int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003008{
3009 CPUState *env;
3010
3011 for (env = first_cpu; env != NULL; env = env->next_cpu)
3012 if (cpu_has_work(env))
3013 return 1;
3014 return 0;
3015}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003016
3017static int vm_can_run(void)
3018{
3019 if (powerdown_requested)
3020 return 0;
3021 if (reset_requested)
3022 return 0;
3023 if (shutdown_requested)
3024 return 0;
3025 if (debug_requested)
3026 return 0;
3027 return 1;
3028}
3029
3030static void main_loop(void)
3031{
3032 int r;
3033
3034#ifdef CONFIG_IOTHREAD
3035 qemu_system_ready = 1;
3036 qemu_cond_broadcast(&qemu_system_cond);
3037#endif
3038
3039 for (;;) {
3040 do {
3041#ifdef CONFIG_PROFILER
3042 int64_t ti;
3043#endif
3044#ifndef CONFIG_IOTHREAD
3045 tcg_cpu_exec();
3046#endif
3047#ifdef CONFIG_PROFILER
3048 ti = profile_getclock();
3049#endif
3050 main_loop_wait(qemu_calculate_timeout());
3051#ifdef CONFIG_PROFILER
3052 dev_time += profile_getclock() - ti;
3053#endif
rich canningsd952f282011-03-01 15:40:09 -08003054
3055 if (rotate_logs_requested) {
3056 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
3057 dns_log_filename);
3058 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
3059 drop_log_filename);
3060 slirp_dns_log_fd(new_dns_log_fd);
3061 slirp_drop_log_fd(new_drop_log_fd);
3062 reset_rotate_qemu_logs_request();
3063 }
3064
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003065 } while (vm_can_run());
3066
3067 if (qemu_debug_requested())
3068 vm_stop(EXCP_DEBUG);
3069 if (qemu_shutdown_requested()) {
3070 if (no_shutdown) {
3071 vm_stop(0);
3072 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003073 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003074 if (savevm_on_exit != NULL) {
3075 do_savevm(cur_mon, savevm_on_exit);
3076 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003077 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003078 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003079 }
3080 if (qemu_reset_requested()) {
3081 pause_all_vcpus();
3082 qemu_system_reset();
3083 resume_all_vcpus();
3084 }
3085 if (qemu_powerdown_requested())
3086 qemu_system_powerdown();
3087 if ((r = qemu_vmstop_requested()))
3088 vm_stop(r);
3089 }
3090 pause_all_vcpus();
3091}
3092
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003093void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003094{
3095 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3096}
3097
3098void qemu_help(int exitcode)
3099{
3100 version();
3101 printf("usage: %s [options] [disk_image]\n"
3102 "\n"
3103 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3104 "\n"
3105#define DEF(option, opt_arg, opt_enum, opt_help) \
3106 opt_help
3107#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003108#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003109#undef DEF
3110#undef DEFHEADING
3111#undef GEN_DOCS
3112 "\n"
3113 "During emulation, the following keys are useful:\n"
3114 "ctrl-alt-f toggle full screen\n"
3115 "ctrl-alt-n switch to virtual console 'n'\n"
3116 "ctrl-alt toggle mouse and keyboard grab\n"
3117 "\n"
3118 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3119 ,
3120 "qemu",
3121 DEFAULT_RAM_SIZE,
3122#ifndef _WIN32
3123 DEFAULT_NETWORK_SCRIPT,
3124 DEFAULT_NETWORK_DOWN_SCRIPT,
3125#endif
3126 DEFAULT_GDBSTUB_PORT,
3127 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003128 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003129}
3130
3131#define HAS_ARG 0x0001
3132
3133enum {
3134#define DEF(option, opt_arg, opt_enum, opt_help) \
3135 opt_enum,
3136#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003137#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003138#undef DEF
3139#undef DEFHEADING
3140#undef GEN_DOCS
3141};
3142
3143typedef struct QEMUOption {
3144 const char *name;
3145 int flags;
3146 int index;
3147} QEMUOption;
3148
3149static const QEMUOption qemu_options[] = {
3150 { "h", 0, QEMU_OPTION_h },
3151#define DEF(option, opt_arg, opt_enum, opt_help) \
3152 { option, opt_arg, opt_enum },
3153#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003154#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003155#undef DEF
3156#undef DEFHEADING
3157#undef GEN_DOCS
3158 { NULL, 0, 0 },
3159};
3160
3161#ifdef HAS_AUDIO
3162struct soundhw soundhw[] = {
3163#ifdef HAS_AUDIO_CHOICE
3164#if defined(TARGET_I386) || defined(TARGET_MIPS)
3165 {
3166 "pcspk",
3167 "PC speaker",
3168 0,
3169 1,
3170 { .init_isa = pcspk_audio_init }
3171 },
3172#endif
3173
3174#ifdef CONFIG_SB16
3175 {
3176 "sb16",
3177 "Creative Sound Blaster 16",
3178 0,
3179 1,
3180 { .init_isa = SB16_init }
3181 },
3182#endif
3183
3184#ifdef CONFIG_CS4231A
3185 {
3186 "cs4231a",
3187 "CS4231A",
3188 0,
3189 1,
3190 { .init_isa = cs4231a_init }
3191 },
3192#endif
3193
3194#ifdef CONFIG_ADLIB
3195 {
3196 "adlib",
3197#ifdef HAS_YMF262
3198 "Yamaha YMF262 (OPL3)",
3199#else
3200 "Yamaha YM3812 (OPL2)",
3201#endif
3202 0,
3203 1,
3204 { .init_isa = Adlib_init }
3205 },
3206#endif
3207
3208#ifdef CONFIG_GUS
3209 {
3210 "gus",
3211 "Gravis Ultrasound GF1",
3212 0,
3213 1,
3214 { .init_isa = GUS_init }
3215 },
3216#endif
3217
3218#ifdef CONFIG_AC97
3219 {
3220 "ac97",
3221 "Intel 82801AA AC97 Audio",
3222 0,
3223 0,
3224 { .init_pci = ac97_init }
3225 },
3226#endif
3227
3228#ifdef CONFIG_ES1370
3229 {
3230 "es1370",
3231 "ENSONIQ AudioPCI ES1370",
3232 0,
3233 0,
3234 { .init_pci = es1370_init }
3235 },
3236#endif
3237
3238#endif /* HAS_AUDIO_CHOICE */
3239
3240 { NULL, NULL, 0, 0, { NULL } }
3241};
3242
3243static void select_soundhw (const char *optarg)
3244{
3245 struct soundhw *c;
3246
3247 if (*optarg == '?') {
3248 show_valid_cards:
3249
3250 printf ("Valid sound card names (comma separated):\n");
3251 for (c = soundhw; c->name; ++c) {
3252 printf ("%-11s %s\n", c->name, c->descr);
3253 }
3254 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003255 if (*optarg != '?') {
3256 PANIC("Unknown sound card name: %s", optarg);
3257 } else {
3258 QEMU_EXIT(0);
3259 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003260 }
3261 else {
3262 size_t l;
3263 const char *p;
3264 char *e;
3265 int bad_card = 0;
3266
3267 if (!strcmp (optarg, "all")) {
3268 for (c = soundhw; c->name; ++c) {
3269 c->enabled = 1;
3270 }
3271 return;
3272 }
3273
3274 p = optarg;
3275 while (*p) {
3276 e = strchr (p, ',');
3277 l = !e ? strlen (p) : (size_t) (e - p);
3278
3279 for (c = soundhw; c->name; ++c) {
3280 if (!strncmp (c->name, p, l)) {
3281 c->enabled = 1;
3282 break;
3283 }
3284 }
3285
3286 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003287#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003288 if (l > 80) {
3289 fprintf (stderr,
3290 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003291 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003292 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3293 (int) l, p);
3294 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003295#endif // !CONFIG_ANDROID
3296 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003297 }
3298 p += l + (e != NULL);
3299 }
3300
3301 if (bad_card)
3302 goto show_valid_cards;
3303 }
3304}
3305#endif
3306
3307static void select_vgahw (const char *p)
3308{
3309 const char *opts;
3310
3311 cirrus_vga_enabled = 0;
3312 std_vga_enabled = 0;
3313 vmsvga_enabled = 0;
3314 xenfb_enabled = 0;
3315 if (strstart(p, "std", &opts)) {
3316 std_vga_enabled = 1;
3317 } else if (strstart(p, "cirrus", &opts)) {
3318 cirrus_vga_enabled = 1;
3319 } else if (strstart(p, "vmware", &opts)) {
3320 vmsvga_enabled = 1;
3321 } else if (strstart(p, "xenfb", &opts)) {
3322 xenfb_enabled = 1;
3323 } else if (!strstart(p, "none", &opts)) {
3324 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003325 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003326 }
3327 while (*opts) {
3328 const char *nextopt;
3329
3330 if (strstart(opts, ",retrace=", &nextopt)) {
3331 opts = nextopt;
3332 if (strstart(opts, "dumb", &nextopt))
3333 vga_retrace_method = VGA_RETRACE_DUMB;
3334 else if (strstart(opts, "precise", &nextopt))
3335 vga_retrace_method = VGA_RETRACE_PRECISE;
3336 else goto invalid_vga;
3337 } else goto invalid_vga;
3338 opts = nextopt;
3339 }
3340}
3341
3342#ifdef _WIN32
3343static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3344{
3345 exit(STATUS_CONTROL_C_EXIT);
3346 return TRUE;
3347}
3348#endif
3349
3350int qemu_uuid_parse(const char *str, uint8_t *uuid)
3351{
3352 int ret;
3353
3354 if(strlen(str) != 36)
3355 return -1;
3356
3357 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3358 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3359 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3360
3361 if(ret != 16)
3362 return -1;
3363
3364#ifdef TARGET_I386
3365 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3366#endif
3367
3368 return 0;
3369}
3370
3371#define MAX_NET_CLIENTS 32
3372
3373#ifndef _WIN32
3374
3375static void termsig_handler(int signal)
3376{
3377 qemu_system_shutdown_request();
3378}
3379
3380static void sigchld_handler(int signal)
3381{
3382 waitpid(-1, NULL, WNOHANG);
3383}
3384
3385static void sighandler_setup(void)
3386{
3387 struct sigaction act;
3388
3389 memset(&act, 0, sizeof(act));
3390 act.sa_handler = termsig_handler;
3391 sigaction(SIGINT, &act, NULL);
3392 sigaction(SIGHUP, &act, NULL);
3393 sigaction(SIGTERM, &act, NULL);
3394
3395 act.sa_handler = sigchld_handler;
3396 act.sa_flags = SA_NOCLDSTOP;
3397 sigaction(SIGCHLD, &act, NULL);
3398}
3399
3400#endif
3401
3402#ifdef _WIN32
3403/* Look for support files in the same directory as the executable. */
3404static char *find_datadir(const char *argv0)
3405{
3406 char *p;
3407 char buf[MAX_PATH];
3408 DWORD len;
3409
3410 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3411 if (len == 0) {
3412 return NULL;
3413 }
3414
3415 buf[len] = 0;
3416 p = buf + len - 1;
3417 while (p != buf && *p != '\\')
3418 p--;
3419 *p = 0;
3420 if (access(buf, R_OK) == 0) {
3421 return qemu_strdup(buf);
3422 }
3423 return NULL;
3424}
3425#else /* !_WIN32 */
3426
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003427/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003428static char *find_datadir(const char *argv0)
3429{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003430 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003431 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003432
3433#if defined(__linux__)
3434 {
3435 int len;
3436 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3437 if (len > 0) {
3438 buf[len] = 0;
3439 p = buf;
3440 }
3441 }
3442#elif defined(__FreeBSD__)
3443 {
3444 int len;
3445 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3446 if (len > 0) {
3447 buf[len] = 0;
3448 p = buf;
3449 }
3450 }
3451#endif
3452 /* If we don't have any way of figuring out the actual executable
3453 location then try argv[0]. */
3454 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003455 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003456 if (!p) {
3457 return NULL;
3458 }
3459 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003460
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003461 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003462}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003463#endif
3464
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003465static char*
3466qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
3467{
3468 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3469 char* buf = qemu_mallocz(len);
3470
3471 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3472 VERBOSE_PRINT(init," trying to find: %s\n", buf);
3473 if (access(buf, R_OK)) {
3474 qemu_free(buf);
3475 return NULL;
3476 }
3477 return buf;
3478}
3479
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003480char *qemu_find_file(int type, const char *name)
3481{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003482 const char *subdir;
3483 char *buf;
3484
3485 /* If name contains path separators then try it as a straight path. */
3486 if ((strchr(name, '/') || strchr(name, '\\'))
3487 && access(name, R_OK) == 0) {
3488 return strdup(name);
3489 }
3490 switch (type) {
3491 case QEMU_FILE_TYPE_BIOS:
3492 subdir = "";
3493 break;
3494 case QEMU_FILE_TYPE_KEYMAP:
3495 subdir = "keymaps/";
3496 break;
3497 default:
3498 abort();
3499 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003500 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
3501#ifdef CONFIG_ANDROID
3502 if (type == QEMU_FILE_TYPE_BIOS) {
3503 /* This case corresponds to the emulator being used as part of an
3504 * SDK installation. NOTE: data_dir is really $bindir. */
3505 if (buf == NULL)
3506 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
3507 /* This case corresponds to platform builds. */
3508 if (buf == NULL)
3509 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
3510 /* Finally, try this for standalone builds under external/qemu */
3511 if (buf == NULL)
3512 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003513 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003514#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003515 return buf;
3516}
3517
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003518static int
3519add_dns_server( const char* server_name )
3520{
3521 SockAddress addr;
3522
3523 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3524 fprintf(stdout,
3525 "### WARNING: can't resolve DNS server name '%s'\n",
3526 server_name );
3527 return -1;
3528 }
3529
3530 fprintf(stderr,
3531 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3532
3533 if ( slirp_add_dns_server( &addr ) < 0 ) {
3534 fprintf(stderr,
3535 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3536 return -1;
3537 }
3538 return 0;
3539}
3540
rich cannings7339b552011-02-16 13:43:44 -08003541/* Parses an integer
3542 * Pararm:
3543 * str String containing a number to be parsed.
3544 * result Passes the parsed integer in this argument
3545 * returns 0 if ok, -1 if failed
3546 */
3547int
3548parse_int(const char *str, int *result)
3549{
3550 char* r;
3551 *result = strtol(str, &r, 0);
3552 if (r == NULL || *r != '\0')
3553 return -1;
3554
3555 return 0;
3556}
3557
rich canningsd952f282011-03-01 15:40:09 -08003558#ifndef _WIN32
3559/*
3560 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3561 */
3562void init_qemu_clear_logs_sig() {
3563 struct sigaction act;
3564 sigfillset(&act.sa_mask);
3565 act.sa_flags = 0;
3566 act.sa_handler = rotate_qemu_logs_handler;
3567 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3568 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3569 exit(-1);
3570 }
3571}
3572#endif
3573
3574
rich cannings7339b552011-02-16 13:43:44 -08003575
3576/* parses a null-terminated string specifying a network port (e.g., "80") or
3577 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3578 * are the same. Returns 0 on success, -1 on error. */
3579
3580int parse_port_range(const char *str, unsigned short *lport,
3581 unsigned short *hport) {
3582
3583 unsigned int low = 0, high = 0;
3584 char *p, *arg = strdup(str);
3585
3586 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3587 p = arg + 1; /* skip '[' */
3588 low = atoi(strtok(p, "-"));
3589 high = atoi(strtok(NULL, "-"));
3590 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3591 *lport = low;
3592 *hport = high;
3593 }
3594 }
3595 else {
3596 low = atoi(arg);
3597 if ((0 < low) && (low < 65535)) {
3598 *lport = low;
3599 *hport = low;
3600 }
3601 }
3602 free(arg);
3603 if (low != 0)
3604 return 0;
3605 return -1;
3606}
3607
3608/*
3609 * Implements the generic port forwarding option
3610 */
3611void
3612net_slirp_forward(const char *optarg)
3613{
3614 /*
3615 * we expect the following format:
3616 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3617 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3618 */
3619 char *argument = strdup(optarg), *p = argument;
3620 char *dst_net, *dst_mask, *dst_port;
3621 char *redirect_ip, *redirect_port;
3622 uint32_t dnet, dmask, rip;
3623 unsigned short dlport, dhport, rport;
3624
3625
3626 dst_net = strtok(p, ":");
3627 dst_mask = strtok(NULL, ":");
3628 dst_port = strtok(NULL, ":");
3629 redirect_ip = strtok(NULL, ":");
3630 redirect_port = strtok(NULL, ":");
3631
3632 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3633 redirect_ip == NULL || redirect_port == NULL) {
3634 fprintf(stderr,
3635 "Invalid argument for -net-forward, we expect "
3636 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3637 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3638 ":redirect_ip:redirect_port: %s\n",
3639 optarg);
3640 exit(1);
3641 }
3642
3643 /* inet_strtoip converts dotted address to host byte order */
3644 if (inet_strtoip(dst_net, &dnet) == -1) {
3645 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3646 exit(1);
3647 }
3648 if (inet_strtoip(dst_mask, &dmask) == -1) {
3649 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3650 exit(1);
3651 }
3652 if (inet_strtoip(redirect_ip, &rip) == -1) {
3653 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3654 exit(1);
3655 }
3656
3657 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3658 fprintf(stderr, "Invalid destination port or port range\n");
3659 exit(1);
3660 }
3661
3662 rport = atoi(redirect_port);
3663 if (!rport) {
3664 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3665 exit(1);
3666 }
3667
3668 dnet &= dmask;
3669
3670 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3671 rip, rport);
3672
3673 free(argument);
3674}
3675
3676
3677/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3678 * entry in the allows list */
3679void
3680slirp_allow(const char *optarg, u_int8_t proto)
3681{
3682 /*
3683 * we expect the following format:
3684 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3685 */
3686 char *argument = strdup(optarg), *p = argument;
3687 char *dst_ip_str, *dst_port_str;
3688 uint32_t dst_ip;
3689 unsigned short dst_lport, dst_hport;
3690
3691 dst_ip_str = strtok(p, ":");
3692 dst_port_str = strtok(NULL, ":");
3693
3694 if (dst_ip_str == NULL || dst_port_str == NULL) {
3695 fprintf(stderr,
3696 "Invalid argument %s for -allow. We expect "
3697 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3698 optarg);
3699 exit(1);
3700 }
3701
3702 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3703 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3704 exit(1);
3705 }
3706 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3707 fprintf(stderr, "Invalid destination port or port range\n");
3708 exit(1);
3709 }
3710
3711 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3712
3713 free(argument);
3714}
3715
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003716/* Add a serial device at a given location in the emulated hardware table.
3717 * On failure, this function aborts the program with an error message.
3718 */
3719static void
3720serial_hds_add_at(int index, const char* devname)
3721{
3722 char label[32];
3723
3724 if (!devname || !strcmp(devname,"none"))
3725 return;
3726
3727 if (index >= MAX_SERIAL_PORTS) {
3728 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3729 devname, index, MAX_SERIAL_PORTS);
3730 }
3731 if (serial_hds[index] != NULL) {
3732 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3733 devname, index);
3734 }
3735 snprintf(label, sizeof(label), "serial%d", index);
3736 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3737 if (!serial_hds[index]) {
3738 PANIC("qemu: could not open serial device '%s'", devname);
3739 }
3740}
3741
3742
3743/* Find a free slot in the emulated serial device table, and register
3744 * it. Return the allocated table index.
3745 */
3746static int
3747serial_hds_add(const char* devname)
3748{
3749 int index;
3750
3751 /* Find first free slot */
3752 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3753 if (serial_hds[index] == NULL) {
3754 serial_hds_add_at(index, devname);
3755 return index;
3756 }
3757 }
3758
3759 PANIC("qemu: too many serial devices registered (%d)", index);
3760 return -1; /* shouldn't happen */
3761}
3762
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003763int main(int argc, char **argv, char **envp)
3764{
3765 const char *gdbstub_dev = NULL;
3766 uint32_t boot_devices_bitmap = 0;
3767 int i;
3768 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003769 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003770 const char *initrd_filename;
3771 const char *kernel_filename, *kernel_cmdline;
3772 const char *boot_devices = "";
3773 DisplayState *ds;
3774 DisplayChangeListener *dcl;
3775 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003776 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003777 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003778 const char *net_clients[MAX_NET_CLIENTS];
3779 int nb_net_clients;
3780 const char *bt_opts[MAX_BT_CMDLINE];
3781 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003782 int optind;
3783 const char *r, *optarg;
3784 CharDriverState *monitor_hd = NULL;
3785 const char *monitor_device;
3786 const char *serial_devices[MAX_SERIAL_PORTS];
3787 int serial_device_index;
3788 const char *parallel_devices[MAX_PARALLEL_PORTS];
3789 int parallel_device_index;
3790 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3791 int virtio_console_index;
3792 const char *loadvm = NULL;
3793 QEMUMachine *machine;
3794 const char *cpu_model;
3795 const char *usb_devices[MAX_USB_CMDLINE];
3796 int usb_devices_index;
3797#ifndef _WIN32
3798 int fds[2];
3799#endif
3800 int tb_size;
3801 const char *pid_file = NULL;
3802 const char *incoming = NULL;
3803#ifndef _WIN32
3804 int fd = 0;
3805 struct passwd *pwd = NULL;
3806 const char *chroot_dir = NULL;
3807 const char *run_as = NULL;
3808#endif
3809 CPUState *env;
3810 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003811 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003812 STRALLOC_DEFINE(kernel_params);
3813 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003814 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003815
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003816 /* Initialize sockets before anything else, so we can properly report
3817 * initialization failures back to the UI. */
3818#ifdef _WIN32
3819 socket_init();
3820#endif
3821
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003822 init_clocks();
3823
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003824 qemu_cache_utils_init(envp);
3825
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003826 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003827#ifndef _WIN32
3828 {
3829 struct sigaction act;
3830 sigfillset(&act.sa_mask);
3831 act.sa_flags = 0;
3832 act.sa_handler = SIG_IGN;
3833 sigaction(SIGPIPE, &act, NULL);
3834 }
3835#else
3836 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3837 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3838 QEMU to run on a single CPU */
3839 {
3840 HANDLE h;
3841 DWORD mask, smask;
3842 int i;
3843 h = GetCurrentProcess();
3844 if (GetProcessAffinityMask(h, &mask, &smask)) {
3845 for(i = 0; i < 32; i++) {
3846 if (mask & (1 << i))
3847 break;
3848 }
3849 if (i != 32) {
3850 mask = 1 << i;
3851 SetProcessAffinityMask(h, mask);
3852 }
3853 }
3854 }
3855#endif
3856
3857 module_call_init(MODULE_INIT_MACHINE);
3858 machine = find_default_machine();
3859 cpu_model = NULL;
3860 initrd_filename = NULL;
3861 ram_size = 0;
3862 snapshot = 0;
3863 kernel_filename = NULL;
3864 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003865
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003866 cyls = heads = secs = 0;
3867 translation = BIOS_ATA_TRANSLATION_AUTO;
3868 monitor_device = "vc:80Cx24C";
3869
3870 serial_devices[0] = "vc:80Cx24C";
3871 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3872 serial_devices[i] = NULL;
3873 serial_device_index = 0;
3874
3875 parallel_devices[0] = "vc:80Cx24C";
3876 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3877 parallel_devices[i] = NULL;
3878 parallel_device_index = 0;
3879
3880 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3881 virtio_consoles[i] = NULL;
3882 virtio_console_index = 0;
3883
3884 for (i = 0; i < MAX_NODES; i++) {
3885 node_mem[i] = 0;
3886 node_cpumask[i] = 0;
3887 }
3888
3889 usb_devices_index = 0;
3890
3891 nb_net_clients = 0;
3892 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003893#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003894 nb_drives = 0;
3895 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003896#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003897 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003898
3899 nb_nics = 0;
3900
3901 tb_size = 0;
3902 autostart= 1;
3903
3904 register_watchdogs();
3905
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003906 /* Initialize boot properties. */
3907 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02003908 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02003909 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003910
David 'Digit' Turner36597752011-05-20 01:18:01 +02003911#ifdef CONFIG_KVM
3912 /* By default, force auto-detection for kvm */
3913 kvm_allowed = -1;
3914#endif
3915
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003916 optind = 1;
3917 for(;;) {
3918 if (optind >= argc)
3919 break;
3920 r = argv[optind];
3921 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003922 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003923 } else {
3924 const QEMUOption *popt;
3925
3926 optind++;
3927 /* Treat --foo the same as -foo. */
3928 if (r[1] == '-')
3929 r++;
3930 popt = qemu_options;
3931 for(;;) {
3932 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003933 PANIC("%s: invalid option -- '%s'",
3934 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003935 }
3936 if (!strcmp(popt->name, r + 1))
3937 break;
3938 popt++;
3939 }
3940 if (popt->flags & HAS_ARG) {
3941 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003942 PANIC("%s: option '%s' requires an argument",
3943 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003944 }
3945 optarg = argv[optind++];
3946 } else {
3947 optarg = NULL;
3948 }
3949
3950 switch(popt->index) {
3951 case QEMU_OPTION_M:
3952 machine = find_machine(optarg);
3953 if (!machine) {
3954 QEMUMachine *m;
3955 printf("Supported machines are:\n");
3956 for(m = first_machine; m != NULL; m = m->next) {
3957 printf("%-10s %s%s\n",
3958 m->name, m->desc,
3959 m->is_default ? " (default)" : "");
3960 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003961 if (*optarg != '?') {
3962 PANIC("Invalid machine parameter: %s",
3963 optarg);
3964 } else {
3965 QEMU_EXIT(0);
3966 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003967 }
3968 break;
3969 case QEMU_OPTION_cpu:
3970 /* hw initialization will check this */
3971 if (*optarg == '?') {
3972/* XXX: implement xxx_cpu_list for targets that still miss it */
3973#if defined(cpu_list)
3974 cpu_list(stdout, &fprintf);
3975#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003976 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003977 } else {
3978 cpu_model = optarg;
3979 }
3980 break;
3981 case QEMU_OPTION_initrd:
3982 initrd_filename = optarg;
3983 break;
3984 case QEMU_OPTION_hda:
3985 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003986 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003987 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003988 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003989 ",cyls=%d,heads=%d,secs=%d%s",
3990 0, cyls, heads, secs,
3991 translation == BIOS_ATA_TRANSLATION_LBA ?
3992 ",trans=lba" :
3993 translation == BIOS_ATA_TRANSLATION_NONE ?
3994 ",trans=none" : "");
3995 break;
3996 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003997 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
3998 break;
3999
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004000 case QEMU_OPTION_hdc:
4001 case QEMU_OPTION_hdd:
4002 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4003 break;
4004 case QEMU_OPTION_drive:
4005 drive_add(NULL, "%s", optarg);
4006 break;
4007 case QEMU_OPTION_mtdblock:
4008 drive_add(optarg, MTD_ALIAS);
4009 break;
4010 case QEMU_OPTION_sd:
4011 drive_add(optarg, SD_ALIAS);
4012 break;
4013 case QEMU_OPTION_pflash:
4014 drive_add(optarg, PFLASH_ALIAS);
4015 break;
4016 case QEMU_OPTION_snapshot:
4017 snapshot = 1;
4018 break;
4019 case QEMU_OPTION_hdachs:
4020 {
4021 const char *p;
4022 p = optarg;
4023 cyls = strtol(p, (char **)&p, 0);
4024 if (cyls < 1 || cyls > 16383)
4025 goto chs_fail;
4026 if (*p != ',')
4027 goto chs_fail;
4028 p++;
4029 heads = strtol(p, (char **)&p, 0);
4030 if (heads < 1 || heads > 16)
4031 goto chs_fail;
4032 if (*p != ',')
4033 goto chs_fail;
4034 p++;
4035 secs = strtol(p, (char **)&p, 0);
4036 if (secs < 1 || secs > 63)
4037 goto chs_fail;
4038 if (*p == ',') {
4039 p++;
4040 if (!strcmp(p, "none"))
4041 translation = BIOS_ATA_TRANSLATION_NONE;
4042 else if (!strcmp(p, "lba"))
4043 translation = BIOS_ATA_TRANSLATION_LBA;
4044 else if (!strcmp(p, "auto"))
4045 translation = BIOS_ATA_TRANSLATION_AUTO;
4046 else
4047 goto chs_fail;
4048 } else if (*p != '\0') {
4049 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004050 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004051 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004052 if (hda_opts != NULL) {
4053 char num[16];
4054 snprintf(num, sizeof(num), "%d", cyls);
4055 qemu_opt_set(hda_opts, "cyls", num);
4056 snprintf(num, sizeof(num), "%d", heads);
4057 qemu_opt_set(hda_opts, "heads", num);
4058 snprintf(num, sizeof(num), "%d", secs);
4059 qemu_opt_set(hda_opts, "secs", num);
4060 if (translation == BIOS_ATA_TRANSLATION_LBA)
4061 qemu_opt_set(hda_opts, "trans", "lba");
4062 if (translation == BIOS_ATA_TRANSLATION_NONE)
4063 qemu_opt_set(hda_opts, "trans", "none");
4064 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004065 }
4066 break;
4067 case QEMU_OPTION_numa:
4068 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004069 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004070 }
4071 numa_add(optarg);
4072 break;
4073 case QEMU_OPTION_nographic:
4074 display_type = DT_NOGRAPHIC;
4075 break;
4076#ifdef CONFIG_CURSES
4077 case QEMU_OPTION_curses:
4078 display_type = DT_CURSES;
4079 break;
4080#endif
4081 case QEMU_OPTION_portrait:
4082 graphic_rotate = 1;
4083 break;
4084 case QEMU_OPTION_kernel:
4085 kernel_filename = optarg;
4086 break;
4087 case QEMU_OPTION_append:
4088 kernel_cmdline = optarg;
4089 break;
4090 case QEMU_OPTION_cdrom:
4091 drive_add(optarg, CDROM_ALIAS);
4092 break;
4093 case QEMU_OPTION_boot:
4094 boot_devices = optarg;
4095 /* We just do some generic consistency checks */
4096 {
4097 /* Could easily be extended to 64 devices if needed */
4098 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004099
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004100 boot_devices_bitmap = 0;
4101 for (p = boot_devices; *p != '\0'; p++) {
4102 /* Allowed boot devices are:
4103 * a b : floppy disk drives
4104 * c ... f : IDE disk drives
4105 * g ... m : machine implementation dependant drives
4106 * n ... p : network devices
4107 * It's up to each machine implementation to check
4108 * if the given boot devices match the actual hardware
4109 * implementation and firmware features.
4110 */
4111 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004112 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004113 }
4114 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004115 PANIC(
4116 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004117 }
4118 boot_devices_bitmap |= 1 << (*p - 'a');
4119 }
4120 }
4121 break;
4122 case QEMU_OPTION_fda:
4123 case QEMU_OPTION_fdb:
4124 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4125 break;
4126#ifdef TARGET_I386
4127 case QEMU_OPTION_no_fd_bootchk:
4128 fd_bootchk = 0;
4129 break;
4130#endif
4131 case QEMU_OPTION_net:
4132 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004133 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004134 }
4135 net_clients[nb_net_clients] = optarg;
4136 nb_net_clients++;
4137 break;
4138#ifdef CONFIG_SLIRP
4139 case QEMU_OPTION_tftp:
4140 tftp_prefix = optarg;
4141 break;
4142 case QEMU_OPTION_bootp:
4143 bootp_filename = optarg;
4144 break;
4145#if 0 /* ANDROID disabled */
4146#ifndef _WIN32
4147 case QEMU_OPTION_smb:
4148 net_slirp_smb(optarg);
4149 break;
4150#endif
4151#endif /* ANDROID */
4152 case QEMU_OPTION_redir:
4153 net_slirp_redir(NULL, optarg, NULL);
4154 break;
4155#endif
4156 case QEMU_OPTION_bt:
4157 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004158 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004159 }
4160 bt_opts[nb_bt_opts++] = optarg;
4161 break;
4162#ifdef HAS_AUDIO
4163 case QEMU_OPTION_audio_help:
4164 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004165 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004166 break;
4167 case QEMU_OPTION_soundhw:
4168 select_soundhw (optarg);
4169 break;
4170#endif
4171 case QEMU_OPTION_h:
4172 qemu_help(0);
4173 break;
4174 case QEMU_OPTION_version:
4175 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004176 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004177 break;
4178 case QEMU_OPTION_m: {
4179 uint64_t value;
4180 char *ptr;
4181
4182 value = strtoul(optarg, &ptr, 10);
4183 switch (*ptr) {
4184 case 0: case 'M': case 'm':
4185 value <<= 20;
4186 break;
4187 case 'G': case 'g':
4188 value <<= 30;
4189 break;
4190 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004191 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004192 }
4193
4194 /* On 32-bit hosts, QEMU is limited by virtual address space */
4195 if (value > (2047 << 20)
4196#ifndef CONFIG_KQEMU
4197 && HOST_LONG_BITS == 32
4198#endif
4199 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004200 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004201 }
4202 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004203 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004204 }
4205 ram_size = value;
4206 break;
4207 }
4208 case QEMU_OPTION_d:
4209 {
4210 int mask;
4211 const CPULogItem *item;
4212
4213 mask = cpu_str_to_log_mask(optarg);
4214 if (!mask) {
4215 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004216 for(item = cpu_log_items; item->mask != 0; item++) {
4217 printf("%-10s %s\n", item->name, item->help);
4218 }
4219 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004220 }
4221 cpu_set_log(mask);
4222 }
4223 break;
4224 case QEMU_OPTION_s:
4225 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4226 break;
4227 case QEMU_OPTION_gdb:
4228 gdbstub_dev = optarg;
4229 break;
4230 case QEMU_OPTION_L:
4231 data_dir = optarg;
4232 break;
4233 case QEMU_OPTION_bios:
4234 bios_name = optarg;
4235 break;
4236 case QEMU_OPTION_singlestep:
4237 singlestep = 1;
4238 break;
4239 case QEMU_OPTION_S:
4240#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004241 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004242#endif
4243 autostart = 0;
4244 break;
4245#ifndef _WIN32
4246 case QEMU_OPTION_k:
4247 keyboard_layout = optarg;
4248 break;
4249#endif
4250 case QEMU_OPTION_localtime:
4251 rtc_utc = 0;
4252 break;
4253 case QEMU_OPTION_vga:
4254 select_vgahw (optarg);
4255 break;
4256#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4257 case QEMU_OPTION_g:
4258 {
4259 const char *p;
4260 int w, h, depth;
4261 p = optarg;
4262 w = strtol(p, (char **)&p, 10);
4263 if (w <= 0) {
4264 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004265 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004266 }
4267 if (*p != 'x')
4268 goto graphic_error;
4269 p++;
4270 h = strtol(p, (char **)&p, 10);
4271 if (h <= 0)
4272 goto graphic_error;
4273 if (*p == 'x') {
4274 p++;
4275 depth = strtol(p, (char **)&p, 10);
4276 if (depth != 8 && depth != 15 && depth != 16 &&
4277 depth != 24 && depth != 32)
4278 goto graphic_error;
4279 } else if (*p == '\0') {
4280 depth = graphic_depth;
4281 } else {
4282 goto graphic_error;
4283 }
4284
4285 graphic_width = w;
4286 graphic_height = h;
4287 graphic_depth = depth;
4288 }
4289 break;
4290#endif
4291 case QEMU_OPTION_echr:
4292 {
4293 char *r;
4294 term_escape_char = strtol(optarg, &r, 0);
4295 if (r == optarg)
4296 printf("Bad argument to echr\n");
4297 break;
4298 }
4299 case QEMU_OPTION_monitor:
4300 monitor_device = optarg;
4301 break;
4302 case QEMU_OPTION_serial:
4303 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004304 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004305 }
4306 serial_devices[serial_device_index] = optarg;
4307 serial_device_index++;
4308 break;
4309 case QEMU_OPTION_watchdog:
4310 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004311 if (i > 0) {
4312 if (i == 1) {
4313 PANIC("Invalid watchdog parameter: %s",
4314 optarg);
4315 } else {
4316 QEMU_EXIT(0);
4317 }
4318 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004319 break;
4320 case QEMU_OPTION_watchdog_action:
4321 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004322 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004323 }
4324 break;
4325 case QEMU_OPTION_virtiocon:
4326 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004327 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004328 }
4329 virtio_consoles[virtio_console_index] = optarg;
4330 virtio_console_index++;
4331 break;
4332 case QEMU_OPTION_parallel:
4333 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004334 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004335 }
4336 parallel_devices[parallel_device_index] = optarg;
4337 parallel_device_index++;
4338 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004339 case QEMU_OPTION_loadvm:
4340 loadvm = optarg;
4341 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004342 case QEMU_OPTION_savevm_on_exit:
4343 savevm_on_exit = optarg;
4344 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004345 case QEMU_OPTION_full_screen:
4346 full_screen = 1;
4347 break;
4348#ifdef CONFIG_SDL
4349 case QEMU_OPTION_no_frame:
4350 no_frame = 1;
4351 break;
4352 case QEMU_OPTION_alt_grab:
4353 alt_grab = 1;
4354 break;
4355 case QEMU_OPTION_no_quit:
4356 no_quit = 1;
4357 break;
4358 case QEMU_OPTION_sdl:
4359 display_type = DT_SDL;
4360 break;
4361#endif
4362 case QEMU_OPTION_pidfile:
4363 pid_file = optarg;
4364 break;
4365#ifdef TARGET_I386
4366 case QEMU_OPTION_win2k_hack:
4367 win2k_install_hack = 1;
4368 break;
4369 case QEMU_OPTION_rtc_td_hack:
4370 rtc_td_hack = 1;
4371 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004372#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004373 case QEMU_OPTION_acpitable:
4374 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004375 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004376 }
4377 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004378#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004379 case QEMU_OPTION_smbios:
4380 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004381 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004382 }
4383 break;
4384#endif
4385#ifdef CONFIG_KQEMU
4386 case QEMU_OPTION_no_kqemu:
4387 kqemu_allowed = 0;
4388 break;
4389 case QEMU_OPTION_kernel_kqemu:
4390 kqemu_allowed = 2;
4391 break;
4392#endif
4393#ifdef CONFIG_KVM
4394 case QEMU_OPTION_enable_kvm:
4395 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004396 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02004397 case QEMU_OPTION_disable_kvm:
4398 kvm_allowed = 0;
4399 break;
4400#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004401 case QEMU_OPTION_usb:
4402 usb_enabled = 1;
4403 break;
4404 case QEMU_OPTION_usbdevice:
4405 usb_enabled = 1;
4406 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004407 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004408 }
4409 usb_devices[usb_devices_index] = optarg;
4410 usb_devices_index++;
4411 break;
4412 case QEMU_OPTION_smp:
4413 smp_cpus = atoi(optarg);
4414 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004415 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004416 }
4417 break;
4418 case QEMU_OPTION_vnc:
4419 display_type = DT_VNC;
4420 vnc_display = optarg;
4421 break;
4422#ifdef TARGET_I386
4423 case QEMU_OPTION_no_acpi:
4424 acpi_enabled = 0;
4425 break;
4426 case QEMU_OPTION_no_hpet:
4427 no_hpet = 1;
4428 break;
4429 case QEMU_OPTION_no_virtio_balloon:
4430 no_virtio_balloon = 1;
4431 break;
4432#endif
4433 case QEMU_OPTION_no_reboot:
4434 no_reboot = 1;
4435 break;
4436 case QEMU_OPTION_no_shutdown:
4437 no_shutdown = 1;
4438 break;
4439 case QEMU_OPTION_show_cursor:
4440 cursor_hide = 0;
4441 break;
4442 case QEMU_OPTION_uuid:
4443 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004444 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004445 }
4446 break;
4447#ifndef _WIN32
4448 case QEMU_OPTION_daemonize:
4449 daemonize = 1;
4450 break;
4451#endif
4452 case QEMU_OPTION_option_rom:
4453 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004454 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004455 }
4456 option_rom[nb_option_roms] = optarg;
4457 nb_option_roms++;
4458 break;
4459#if defined(TARGET_ARM) || defined(TARGET_M68K)
4460 case QEMU_OPTION_semihosting:
4461 semihosting_enabled = 1;
4462 break;
4463#endif
4464 case QEMU_OPTION_name:
4465 qemu_name = optarg;
4466 break;
4467#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4468 case QEMU_OPTION_prom_env:
4469 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004470 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004471 }
4472 prom_envs[nb_prom_envs] = optarg;
4473 nb_prom_envs++;
4474 break;
4475#endif
4476#ifdef TARGET_ARM
4477 case QEMU_OPTION_old_param:
4478 old_param = 1;
4479 break;
4480#endif
4481 case QEMU_OPTION_clock:
4482 configure_alarms(optarg);
4483 break;
4484 case QEMU_OPTION_startdate:
4485 {
4486 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004487 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004488 if (!strcmp(optarg, "now")) {
4489 rtc_date_offset = -1;
4490 } else {
4491 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4492 &tm.tm_year,
4493 &tm.tm_mon,
4494 &tm.tm_mday,
4495 &tm.tm_hour,
4496 &tm.tm_min,
4497 &tm.tm_sec) == 6) {
4498 /* OK */
4499 } else if (sscanf(optarg, "%d-%d-%d",
4500 &tm.tm_year,
4501 &tm.tm_mon,
4502 &tm.tm_mday) == 3) {
4503 tm.tm_hour = 0;
4504 tm.tm_min = 0;
4505 tm.tm_sec = 0;
4506 } else {
4507 goto date_fail;
4508 }
4509 tm.tm_year -= 1900;
4510 tm.tm_mon--;
4511 rtc_start_date = mktimegm(&tm);
4512 if (rtc_start_date == -1) {
4513 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004514 PANIC("Invalid date format. Valid format are:\n"
4515 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004516 }
4517 rtc_date_offset = time(NULL) - rtc_start_date;
4518 }
4519 }
4520 break;
rich cannings7339b552011-02-16 13:43:44 -08004521
4522 /* -------------------------------------------------------*/
4523 /* User mode network stack restrictions */
4524 case QEMU_OPTION_drop_udp:
4525 slirp_drop_udp();
4526 break;
4527 case QEMU_OPTION_drop_tcp:
4528 slirp_drop_tcp();
4529 break;
4530 case QEMU_OPTION_allow_tcp:
4531 slirp_allow(optarg, IPPROTO_TCP);
4532 break;
4533 case QEMU_OPTION_allow_udp:
4534 slirp_allow(optarg, IPPROTO_UDP);
4535 break;
4536 case QEMU_OPTION_drop_log:
4537 {
4538 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004539 drop_log_filename = optarg;
4540 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004541
4542 if (!drop_log_fd) {
4543 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4544 exit(1);
4545 }
4546
4547 slirp_drop_log_fd(drop_log_fd);
4548 }
4549 break;
4550
4551 case QEMU_OPTION_dns_log:
4552 {
4553 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004554 dns_log_filename = optarg;
4555 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004556
4557 if (dns_log_fd == NULL) {
4558 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4559 exit(1);
4560 }
4561
4562 slirp_dns_log_fd(dns_log_fd);
4563 }
4564 break;
4565
4566
4567 case QEMU_OPTION_max_dns_conns:
4568 {
4569 int max_dns_conns = 0;
4570 if (parse_int(optarg, &max_dns_conns)) {
4571 fprintf(stderr,
4572 "qemu: syntax: -max-dns-conns max_connections\n");
4573 exit(1);
4574 }
4575 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4576 fprintf(stderr,
4577 "Invalid arg for max dns connections: %s\n",
4578 optarg);
4579 exit(1);
4580 }
4581 slirp_set_max_dns_conns(max_dns_conns);
4582 }
4583 break;
4584
4585 case QEMU_OPTION_net_forward:
4586 net_slirp_forward(optarg);
4587 break;
4588 case QEMU_OPTION_net_forward_tcp2sink:
4589 {
4590 SockAddress saddr;
4591
4592 if (parse_host_port(&saddr, optarg)) {
4593 fprintf(stderr,
4594 "Invalid ip/port %s for "
4595 "-forward-dropped-tcp2sink. "
4596 "We expect 'sink_ip:sink_port'\n",
4597 optarg);
4598 exit(1);
4599 }
4600 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4601 saddr.u.inet.port);
4602 }
4603 break;
4604 /* -------------------------------------------------------*/
4605
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004606 case QEMU_OPTION_tb_size:
4607 tb_size = strtol(optarg, NULL, 0);
4608 if (tb_size < 0)
4609 tb_size = 0;
4610 break;
4611 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004612 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004613 break;
4614 case QEMU_OPTION_incoming:
4615 incoming = optarg;
4616 break;
4617#ifndef _WIN32
4618 case QEMU_OPTION_chroot:
4619 chroot_dir = optarg;
4620 break;
4621 case QEMU_OPTION_runas:
4622 run_as = optarg;
4623 break;
4624#endif
4625#ifdef CONFIG_XEN
4626 case QEMU_OPTION_xen_domid:
4627 xen_domid = atoi(optarg);
4628 break;
4629 case QEMU_OPTION_xen_create:
4630 xen_mode = XEN_CREATE;
4631 break;
4632 case QEMU_OPTION_xen_attach:
4633 xen_mode = XEN_ATTACH;
4634 break;
4635#endif
4636
4637
4638 case QEMU_OPTION_mic:
4639 audio_input_source = (char*)optarg;
4640 break;
4641#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004642 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004643 trace_filename = optarg;
4644 tracing = 1;
4645 break;
4646#if 0
4647 case QEMU_OPTION_trace_miss:
4648 trace_cache_miss = 1;
4649 break;
4650 case QEMU_OPTION_trace_addr:
4651 trace_all_addr = 1;
4652 break;
4653#endif
4654 case QEMU_OPTION_tracing:
4655 if (strcmp(optarg, "off") == 0)
4656 tracing = 0;
4657 else if (strcmp(optarg, "on") == 0 && trace_filename)
4658 tracing = 1;
4659 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004660 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004661 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004662 }
4663 break;
4664#if 0
4665 case QEMU_OPTION_dcache_load_miss:
4666 dcache_load_miss_penalty = atoi(optarg);
4667 break;
4668 case QEMU_OPTION_dcache_store_miss:
4669 dcache_store_miss_penalty = atoi(optarg);
4670 break;
4671#endif
4672#endif
4673#ifdef CONFIG_NAND
4674 case QEMU_OPTION_nand:
4675 nand_add_dev(optarg);
4676 break;
4677#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004678 case QEMU_OPTION_android_ports:
4679 android_op_ports = (char*)optarg;
4680 break;
4681
4682 case QEMU_OPTION_android_port:
4683 android_op_port = (char*)optarg;
4684 break;
4685
4686 case QEMU_OPTION_android_report_console:
4687 android_op_report_console = (char*)optarg;
4688 break;
4689
4690 case QEMU_OPTION_http_proxy:
4691 op_http_proxy = (char*)optarg;
4692 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004693
4694 case QEMU_OPTION_charmap:
4695 op_charmap_file = (char*)optarg;
4696 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004697
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004698 case QEMU_OPTION_android_hw:
4699 android_op_hwini = (char*)optarg;
4700 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004701
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004702 case QEMU_OPTION_dns_server:
4703 android_op_dns_server = (char*)optarg;
4704 break;
4705
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004706 case QEMU_OPTION_radio:
4707 android_op_radio = (char*)optarg;
4708 break;
4709
4710 case QEMU_OPTION_gps:
4711 android_op_gps = (char*)optarg;
4712 break;
4713
4714 case QEMU_OPTION_audio:
4715 android_op_audio = (char*)optarg;
4716 break;
4717
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004718 case QEMU_OPTION_cpu_delay:
4719 android_op_cpu_delay = (char*)optarg;
4720 break;
4721
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004722 case QEMU_OPTION_show_kernel:
4723 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4724 break;
4725
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004726#ifdef CONFIG_NAND_LIMITS
4727 case QEMU_OPTION_nand_limits:
4728 android_op_nand_limits = (char*)optarg;
4729 break;
4730#endif // CONFIG_NAND_LIMITS
4731
4732 case QEMU_OPTION_netspeed:
4733 android_op_netspeed = (char*)optarg;
4734 break;
4735
4736 case QEMU_OPTION_netdelay:
4737 android_op_netdelay = (char*)optarg;
4738 break;
4739
4740 case QEMU_OPTION_netfast:
4741 android_op_netfast = 1;
4742 break;
4743
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004744 case QEMU_OPTION_tcpdump:
4745 android_op_tcpdump = (char*)optarg;
4746 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004747
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004748 case QEMU_OPTION_boot_property:
4749 boot_property_parse_option((char*)optarg);
4750 break;
4751
4752 case QEMU_OPTION_lcd_density:
4753 android_op_lcd_density = (char*)optarg;
4754 break;
4755
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004756 case QEMU_OPTION_ui_port:
4757 android_op_ui_port = (char*)optarg;
4758 break;
4759
4760 case QEMU_OPTION_ui_settings:
4761 android_op_ui_settings = (char*)optarg;
4762 break;
4763
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004764 case QEMU_OPTION_audio_test_out:
4765 android_audio_test_start_out();
4766 break;
4767
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004768 case QEMU_OPTION_android_avdname:
4769 android_op_avd_name = (char*)optarg;
4770 break;
4771
4772 case QEMU_OPTION_timezone:
4773 if (timezone_set((char*)optarg)) {
4774 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4775 (char*)optarg);
4776 }
4777 break;
4778
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004779#ifdef CONFIG_MEMCHECK
4780 case QEMU_OPTION_android_memcheck:
4781 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004782 /* This will set ro.kernel.memcheck system property
4783 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004784 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004785 break;
4786#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004787
4788 case QEMU_OPTION_snapshot_no_time_update:
4789 android_snapshot_update_time = 0;
4790 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004791 }
4792 }
4793 }
4794
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004795 /* Initialize character map. */
4796 if (android_charmap_setup(op_charmap_file)) {
4797 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004798 PANIC(
4799 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004800 op_charmap_file);
4801 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004802 PANIC(
4803 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004804 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004805 }
4806
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004807 /* If no data_dir is specified then try to find it relative to the
4808 executable path. */
4809 if (!data_dir) {
4810 data_dir = find_datadir(argv[0]);
4811 }
4812 /* If all else fails use the install patch specified when building. */
4813 if (!data_dir) {
4814 data_dir = CONFIG_QEMU_SHAREDIR;
4815 }
4816
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004817 if (!android_op_hwini) {
4818 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004819 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004820 hw_ini = iniFile_newFromFile(android_op_hwini);
4821 if (hw_ini == NULL) {
4822 PANIC("Could not find %s file.", android_op_hwini);
4823 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004824
4825 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004826 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004827
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004828 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004829
4830 {
4831 int width = android_hw->hw_lcd_width;
4832 int height = android_hw->hw_lcd_height;
4833 int depth = android_hw->hw_lcd_depth;
4834
4835 /* A bit of sanity checking */
4836 if (width <= 0 || height <= 0 ||
4837 (depth != 16 && depth != 32) ||
4838 (((width|height) & 3) != 0) )
4839 {
4840 PANIC("Invalid display configuration (%d,%d,%d)",
4841 width, height, depth);
4842 }
4843 android_display_width = width;
4844 android_display_height = height;
4845 android_display_bpp = depth;
4846 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004847
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004848#ifdef CONFIG_NAND_LIMITS
4849 /* Init nand stuff. */
4850 if (android_op_nand_limits) {
4851 parse_nand_limits(android_op_nand_limits);
4852 }
4853#endif // CONFIG_NAND_LIMITS
4854
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004855 /* Initialize AVD name from hardware configuration if needed */
4856 if (!android_op_avd_name) {
4857 if (android_hw->avd_name && *android_hw->avd_name) {
4858 android_op_avd_name = android_hw->avd_name;
4859 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
4860 }
4861 }
4862
David 'Digit' Turner40841b22011-03-01 14:04:00 +01004863 /* Initialize system partition image */
4864 {
4865 char tmp[PATH_MAX+32];
4866 const char* sysImage = android_hw->disk_systemPartition_path;
4867 const char* initImage = android_hw->disk_systemPartition_initPath;
4868 uint64_t sysBytes = android_hw->disk_systemPartition_size;
4869
4870 if (sysBytes == 0) {
4871 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
4872 }
4873
4874 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
4875
4876 if (sysImage && *sysImage) {
4877 if (filelock_create(sysImage) == NULL) {
4878 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
4879 /* If there is no file= parameters, nand_add_dev will create
4880 * a temporary file to back the partition image. */
4881 } else {
4882 pstrcat(tmp,sizeof(tmp),",file=");
4883 pstrcat(tmp,sizeof(tmp),sysImage);
4884 }
4885 }
4886 if (initImage && *initImage) {
4887 if (!path_exists(initImage)) {
4888 PANIC("Invalid initial system image path: %s", initImage);
4889 }
4890 pstrcat(tmp,sizeof(tmp),",initfile=");
4891 pstrcat(tmp,sizeof(tmp),initImage);
4892 } else {
4893 PANIC("Missing initial system image path!");
4894 }
4895 nand_add_dev(tmp);
4896 }
4897
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01004898 /* Initialize data partition image */
4899 {
4900 char tmp[PATH_MAX+32];
4901 const char* dataImage = android_hw->disk_dataPartition_path;
4902 const char* initImage = android_hw->disk_dataPartition_initPath;
4903 uint64_t dataBytes = android_hw->disk_dataPartition_size;
4904
4905 if (dataBytes == 0) {
4906 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
4907 }
4908
4909 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
4910
4911 if (dataImage && *dataImage) {
4912 if (filelock_create(dataImage) == NULL) {
4913 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
4914 /* Note: if there is no file= parameters, nand_add_dev() will
4915 * create a temporary file to back the partition image. */
4916 } else {
4917 /* Create the file if needed */
4918 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01004919 if (path_empty_file(dataImage) < 0) {
4920 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
4921 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01004922 }
4923 pstrcat(tmp, sizeof(tmp), ",file=");
4924 pstrcat(tmp, sizeof(tmp), dataImage);
4925 }
4926 }
4927 if (initImage && *initImage) {
4928 pstrcat(tmp, sizeof(tmp), ",initfile=");
4929 pstrcat(tmp, sizeof(tmp), initImage);
4930 }
4931 nand_add_dev(tmp);
4932 }
4933
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01004934 /* Init SD-Card stuff. For Android, it is always hda */
4935 /* If the -hda option was used, ignore the Android-provided one */
4936 if (hda_opts == NULL) {
4937 const char* sdPath = android_hw->hw_sdCard_path;
4938 if (sdPath && *sdPath) {
4939 if (!path_exists(sdPath)) {
4940 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
4941 } else if (filelock_create(sdPath) == NULL) {
4942 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
4943 } else {
4944 /* Successful locking */
4945 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02004946 /* Set this property of any operation involving the SD Card
4947 * will be x100 slower, due to the corresponding file being
4948 * mounted as O_DIRECT. Note that this is only 'unsafe' in
4949 * the context of an emulator crash. The data is already
4950 * synced properly when the emulator exits (either normally or through ^C).
4951 */
4952 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01004953 }
4954 }
4955 }
4956
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004957 if (hdb_opts == NULL) {
4958 const char* spath = android_hw->disk_snapStorage_path;
4959 if (spath && *spath) {
4960 if (!path_exists(spath)) {
4961 PANIC("Snapshot storage file does not exist: %s", spath);
4962 }
4963 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02004964 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004965 }
4966 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02004967 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02004968 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004969 }
4970 }
4971
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004972 /* Set the VM's max heap size, passed as a boot property */
4973 if (android_hw->vm_heapSize > 0) {
4974 char tmp[64];
4975 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
4976 boot_property_add("dalvik.vm.heapsize",tmp);
4977 }
4978
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004979 /* Initialize net speed and delays stuff. */
4980 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004981 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004982 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004983 }
4984
4985 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004986 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004987 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004988 }
4989
4990 if (android_op_netfast) {
4991 qemu_net_download_speed = 0;
4992 qemu_net_upload_speed = 0;
4993 qemu_net_min_latency = 0;
4994 qemu_net_max_latency = 0;
4995 }
4996
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004997 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01004998 if (android_hw->hw_lcd_density) {
4999 long density = android_hw->hw_lcd_density;
5000 if (density <= 0) {
5001 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005002 }
5003 hwLcd_setBootProperty(density);
5004 }
5005
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07005006 /* Initialize TCP dump */
5007 if (android_op_tcpdump) {
5008 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
5009 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
5010 }
5011 }
5012
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005013 /* Initialize modem */
5014 if (android_op_radio) {
5015 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
5016 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005017 PANIC("unsupported character device specification: %s\n"
5018 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005019 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005020 }
5021 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
5022 } else if (android_hw->hw_gsmModem != 0 ) {
5023 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005024 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005025 }
5026 }
5027
5028 /* Initialize GPS */
5029 if (android_op_gps) {
5030 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
5031 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005032 PANIC("unsupported character device specification: %s\n"
5033 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005034 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005035 }
5036 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
5037 } else if (android_hw->hw_gps != 0) {
5038 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005039 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005040 }
5041 }
5042
5043 /* Initialize audio. */
5044 if (android_op_audio) {
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005045 char temp[128];
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005046 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005047 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005048 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005049 }
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005050 snprintf(temp, sizeof temp, "QEMU_AUDIO_DRV=%s", android_op_audio);
5051 putenv(temp);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005052 }
5053
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005054 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02005055 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005056
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005057 if (android_op_cpu_delay) {
5058 char* end;
5059 long delay = strtol(android_op_cpu_delay, &end, 0);
5060 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005061 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005062 }
5063 if (delay > 0)
5064 delay = (1000-delay);
5065
5066 qemu_cpu_delay = (int) delay;
5067 }
5068
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005069 if (android_op_dns_server) {
5070 char* x = strchr(android_op_dns_server, ',');
5071 dns_count = 0;
5072 if (x == NULL)
5073 {
5074 if ( add_dns_server( android_op_dns_server ) == 0 )
5075 dns_count = 1;
5076 }
5077 else
5078 {
5079 x = android_op_dns_server;
5080 while (*x) {
5081 char* y = strchr(x, ',');
5082
5083 if (y != NULL) {
5084 *y = 0;
5085 y++;
5086 } else {
5087 y = x + strlen(x);
5088 }
5089
5090 if (y > x && add_dns_server( x ) == 0) {
5091 dns_count += 1;
5092 }
5093 x = y;
5094 }
5095 }
5096 if (dns_count == 0)
5097 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5098 }
5099
5100 if (dns_count == 0)
5101 dns_count = slirp_get_system_dns_servers();
5102 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005103 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005104 }
5105
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005106#ifdef CONFIG_MEMCHECK
5107 if (android_op_memcheck) {
5108 memcheck_init(android_op_memcheck);
5109 }
5110#endif // CONFIG_MEMCHECK
5111
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005112 /* Initialize cache partition, if any */
5113 if (android_hw->disk_cachePartition != 0) {
5114 char tmp[PATH_MAX+32];
5115 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005116 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005117
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005118 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5119
5120 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5121 if (filelock_create(partPath) == NULL) {
5122 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5123 /* Note: if there is no file= parameters, nand_add_dev() will
5124 * create a temporary file to back the partition image. */
5125 } else {
5126 /* Create the file if needed */
5127 if (!path_exists(partPath)) {
5128 if (path_empty_file(partPath) < 0) {
5129 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5130 }
5131 }
5132 pstrcat(tmp, sizeof(tmp), ",file=");
5133 pstrcat(tmp, sizeof(tmp), partPath);
5134 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005135 }
5136 nand_add_dev(tmp);
5137 }
5138
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005139 /* We always force qemu=1 when running inside QEMU */
5140 stralloc_add_str(kernel_params, " qemu=1");
5141
5142 /* We always initialize the first serial port for the android-kmsg
5143 * character device (used to send kernel messages) */
5144 serial_hds_add_at(0, "android-kmsg");
5145 stralloc_add_str(kernel_params, " console=ttyS0");
5146
5147 /* We always initialize the second serial port for the android-qemud
5148 * character device as well */
5149 serial_hds_add_at(1, "android-qemud");
5150 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5151
David 'Digit' Turner36597752011-05-20 01:18:01 +02005152#if defined(CONFIG_KVM)
5153 if (kvm_allowed < 0) {
5154 kvm_allowed = kvm_check_allowed();
5155 }
5156#endif
5157
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005158#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5159 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005160 PANIC(
5161 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005162 }
5163#endif
5164
5165 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5166 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005167 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5168 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005169 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005170 }
5171
5172 if (display_type == DT_NOGRAPHIC) {
5173 if (serial_device_index == 0)
5174 serial_devices[0] = "stdio";
5175 if (parallel_device_index == 0)
5176 parallel_devices[0] = "null";
5177 if (strncmp(monitor_device, "vc", 2) == 0)
5178 monitor_device = "stdio";
5179 }
5180
5181#ifndef _WIN32
5182 if (daemonize) {
5183 pid_t pid;
5184
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005185 if (pipe(fds) == -1) {
5186 PANIC("Unable to aquire pidfile");
5187 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005188
5189 pid = fork();
5190 if (pid > 0) {
5191 uint8_t status;
5192 ssize_t len;
5193
5194 close(fds[1]);
5195
5196 again:
5197 len = read(fds[0], &status, 1);
5198 if (len == -1 && (errno == EINTR))
5199 goto again;
5200
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005201 if (len != 1) {
5202 PANIC("Error when aquiring pidfile");
5203 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005204 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005205 PANIC("Could not acquire pidfile");
5206 } else {
5207 QEMU_EXIT(0);
5208 }
5209 } else if (pid < 0) {
5210 PANIC("Unable to daemonize");
5211 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005212
5213 setsid();
5214
5215 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005216 if (pid > 0) {
5217 QEMU_EXIT(0);
5218 } else if (pid < 0) {
5219 PANIC("Could not acquire pid file");
5220 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005221
5222 umask(027);
5223
5224 signal(SIGTSTP, SIG_IGN);
5225 signal(SIGTTOU, SIG_IGN);
5226 signal(SIGTTIN, SIG_IGN);
5227 }
5228
5229 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5230 if (daemonize) {
5231 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005232 int ret;
5233 do {
5234 ret = write(fds[1], &status, 1);
5235 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005236 PANIC("Could not acquire pid file");
5237 } else {
5238 PANIC("Could not acquire pid file");
5239 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005240 }
5241#endif
5242
5243#ifdef CONFIG_KQEMU
5244 if (smp_cpus > 1)
5245 kqemu_allowed = 0;
5246#endif
5247 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005248 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005249 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005250
5251 if (kernel_filename == NULL) {
5252 kernel_filename = android_hw->kernel_path;
5253 }
5254 if (initrd_filename == NULL) {
5255 initrd_filename = android_hw->disk_ramdisk_path;
5256 }
5257
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005258 linux_boot = (kernel_filename != NULL);
5259 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5260
5261 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005262 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005263 }
5264
5265 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005266 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005267 }
5268
5269 /* boot to floppy or the default cd if no hard disk defined yet */
5270 if (!boot_devices[0]) {
5271 boot_devices = "cad";
5272 }
5273 setvbuf(stdout, NULL, _IOLBF, 0);
5274
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005275 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005276 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005277 }
David Turner6a9ef172010-09-09 22:54:36 +02005278 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005279
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005280 /* init network clients */
5281 if (nb_net_clients == 0) {
5282 /* if no clients, we use a default config */
5283 net_clients[nb_net_clients++] = "nic";
5284#ifdef CONFIG_SLIRP
5285 net_clients[nb_net_clients++] = "user";
5286#endif
5287 }
5288
5289 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005290 if (net_client_parse(net_clients[i]) < 0) {
5291 PANIC("Unable to parse net clients");
5292 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005293 }
5294 net_client_check();
5295
5296#ifdef TARGET_I386
5297 /* XXX: this should be moved in the PC machine instantiation code */
5298 if (net_boot != 0) {
5299 int netroms = 0;
5300 for (i = 0; i < nb_nics && i < 4; i++) {
5301 const char *model = nd_table[i].model;
5302 char buf[1024];
5303 char *filename;
5304 if (net_boot & (1 << i)) {
5305 if (model == NULL)
5306 model = "ne2k_pci";
5307 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5308 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5309 if (filename && get_image_size(filename) > 0) {
5310 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005311 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005312 }
5313 option_rom[nb_option_roms] = qemu_strdup(buf);
5314 nb_option_roms++;
5315 netroms++;
5316 }
5317 if (filename) {
5318 qemu_free(filename);
5319 }
5320 }
5321 }
5322 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005323 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005324 }
5325 }
5326#endif
5327
5328 /* init the bluetooth world */
5329 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005330 if (bt_parse(bt_opts[i])) {
5331 PANIC("Unable to parse bluetooth options");
5332 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005333
5334 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005335 if (ram_size == 0) {
5336 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5337 if (ram_size == 0) {
5338 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5339 }
5340 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005341
5342#ifdef CONFIG_KQEMU
5343 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5344 guest ram allocation. It needs to go away. */
5345 if (kqemu_allowed) {
5346 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5347 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5348 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005349 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005350 }
5351 }
5352#endif
5353
rich canningsd952f282011-03-01 15:40:09 -08005354#ifndef _WIN32
5355 init_qemu_clear_logs_sig();
5356#endif
5357
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005358 /* init the dynamic translator */
5359 cpu_exec_init_all(tb_size * 1024 * 1024);
5360
5361 bdrv_init();
5362
5363 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005364#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005365 if (nb_drives_opt < MAX_DRIVES)
5366 drive_add(NULL, CDROM_ALIAS);
5367
5368 /* we always create at least one floppy */
5369
5370 if (nb_drives_opt < MAX_DRIVES)
5371 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005372 /* we always create one sd slot, even if no card is in it */
5373
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005374 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005375 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005376 }
5377#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005378
5379 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005380 if (snapshot)
5381 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5382 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5383 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005384
David Turner6a9ef172010-09-09 22:54:36 +02005385 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005386 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5387
5388#ifndef _WIN32
5389 /* must be after terminal init, SDL library changes signal handlers */
5390 sighandler_setup();
5391#endif
5392
5393 /* Maintain compatibility with multiple stdio monitors */
5394 if (!strcmp(monitor_device,"stdio")) {
5395 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5396 const char *devname = serial_devices[i];
5397 if (devname && !strcmp(devname,"mon:stdio")) {
5398 monitor_device = NULL;
5399 break;
5400 } else if (devname && !strcmp(devname,"stdio")) {
5401 monitor_device = NULL;
5402 serial_devices[i] = "mon:stdio";
5403 break;
5404 }
5405 }
5406 }
5407
5408 if (nb_numa_nodes > 0) {
5409 int i;
5410
5411 if (nb_numa_nodes > smp_cpus) {
5412 nb_numa_nodes = smp_cpus;
5413 }
5414
5415 /* If no memory size if given for any node, assume the default case
5416 * and distribute the available memory equally across all nodes
5417 */
5418 for (i = 0; i < nb_numa_nodes; i++) {
5419 if (node_mem[i] != 0)
5420 break;
5421 }
5422 if (i == nb_numa_nodes) {
5423 uint64_t usedmem = 0;
5424
5425 /* On Linux, the each node's border has to be 8MB aligned,
5426 * the final node gets the rest.
5427 */
5428 for (i = 0; i < nb_numa_nodes - 1; i++) {
5429 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5430 usedmem += node_mem[i];
5431 }
5432 node_mem[i] = ram_size - usedmem;
5433 }
5434
5435 for (i = 0; i < nb_numa_nodes; i++) {
5436 if (node_cpumask[i] != 0)
5437 break;
5438 }
5439 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5440 * must cope with this anyway, because there are BIOSes out there in
5441 * real machines which also use this scheme.
5442 */
5443 if (i == nb_numa_nodes) {
5444 for (i = 0; i < smp_cpus; i++) {
5445 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5446 }
5447 }
5448 }
5449
5450 if (kvm_enabled()) {
5451 int ret;
5452
5453 ret = kvm_init(smp_cpus);
5454 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005455 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005456 }
5457 }
5458
5459 if (monitor_device) {
5460 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5461 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005462 PANIC("qemu: could not open monitor device '%s'",
5463 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005464 }
5465 }
5466
5467 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005468 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005469 }
5470
5471 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5472 const char *devname = parallel_devices[i];
5473 if (devname && strcmp(devname, "none")) {
5474 char label[32];
5475 snprintf(label, sizeof(label), "parallel%d", i);
5476 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5477 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005478 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005479 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005480 }
5481 }
5482 }
5483
5484 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5485 const char *devname = virtio_consoles[i];
5486 if (devname && strcmp(devname, "none")) {
5487 char label[32];
5488 snprintf(label, sizeof(label), "virtcon%d", i);
5489 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5490 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005491 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005492 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005493 }
5494 }
5495 }
5496
5497 module_call_init(MODULE_INIT_DEVICE);
5498
5499
5500#ifdef CONFIG_TRACE
5501 if (trace_filename) {
5502 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005503 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5504 }
5505#endif
5506
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005507 /* Check the CPU Architecture value */
5508#if defined(TARGET_ARM)
5509 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5510 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5511 android_hw->hw_cpu_arch);
5512 exit(1);
5513 }
5514#elif defined(TARGET_X86)
5515 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5516 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5517 android_hw->hw_cpu_arch);
5518 exit(1);
5519 }
5520#endif
5521
5522 /* Grab CPU model if provided in hardware.ini */
5523 if ( !cpu_model
5524 && android_hw->hw_cpu_model
5525 && android_hw->hw_cpu_model[0] != '\0')
5526 {
5527 cpu_model = android_hw->hw_cpu_model;
5528 }
5529
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005530 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005531 * collected during initialization.
5532 *
5533 * The order is the following:
5534 * - parameters from the hw configuration (kernel.parameters)
5535 * - additionnal parameters from options (e.g. -memcheck)
5536 * - the -append parameters.
5537 */
5538 {
5539 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005540
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005541 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005542 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005543 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5544 }
5545
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005546 /* If not empty, kernel_config always contains a leading space */
5547 stralloc_append(kernel_params, kernel_config);
5548
5549 if (*kernel_cmdline) {
5550 stralloc_add_c(kernel_params, ' ');
5551 stralloc_add_str(kernel_params, kernel_cmdline);
5552 }
5553
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005554 /* Remove any leading/trailing spaces */
5555 stralloc_strip(kernel_params);
5556
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005557 kernel_parameters = stralloc_cstr(kernel_params);
5558 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5559
5560 machine->init(ram_size,
5561 boot_devices,
5562 kernel_filename,
5563 kernel_parameters,
5564 initrd_filename,
5565 cpu_model);
5566
5567 stralloc_reset(kernel_params);
5568 stralloc_reset(kernel_config);
5569 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005570
5571
5572 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5573 for (i = 0; i < nb_numa_nodes; i++) {
5574 if (node_cpumask[i] & (1 << env->cpu_index)) {
5575 env->numa_node = i;
5576 }
5577 }
5578 }
5579
5580 current_machine = machine;
5581
5582 /* Set KVM's vcpu state to qemu's initial CPUState. */
5583 if (kvm_enabled()) {
5584 int ret;
5585
5586 ret = kvm_sync_vcpus();
5587 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005588 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005589 }
5590 }
5591
5592 /* init USB devices */
5593 if (usb_enabled) {
5594 for(i = 0; i < usb_devices_index; i++) {
5595 if (usb_device_add(usb_devices[i], 0) < 0) {
5596 fprintf(stderr, "Warning: could not add USB device %s\n",
5597 usb_devices[i]);
5598 }
5599 }
5600 }
5601
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005602 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005603 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005604
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005605 /* Initialize display from the command line parameters. */
5606 android_display_reset(ds,
5607 android_display_width,
5608 android_display_height,
5609 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005610
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005611 if (display_type == DT_DEFAULT) {
5612#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5613 display_type = DT_SDL;
5614#else
5615 display_type = DT_VNC;
5616 vnc_display = "localhost:0,to=99";
5617 show_vnc_port = 1;
5618#endif
5619 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005620
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005621
5622 switch (display_type) {
5623 case DT_NOGRAPHIC:
5624 break;
5625#if defined(CONFIG_CURSES)
5626 case DT_CURSES:
5627 curses_display_init(ds, full_screen);
5628 break;
5629#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005630#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005631 case DT_SDL:
5632 sdl_display_init(ds, full_screen, no_frame);
5633 break;
5634#elif defined(CONFIG_COCOA)
5635 case DT_SDL:
5636 cocoa_display_init(ds, full_screen);
5637 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005638#elif defined(CONFIG_STANDALONE_CORE)
5639 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005640 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005641 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005642#endif
5643 case DT_VNC:
5644 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005645 if (vnc_display_open(ds, vnc_display) < 0) {
5646 PANIC("Unable to initialize VNC display");
5647 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005648
5649 if (show_vnc_port) {
5650 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5651 }
5652 break;
5653 default:
5654 break;
5655 }
5656 dpy_resize(ds);
5657
5658 dcl = ds->listeners;
5659 while (dcl != NULL) {
5660 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005661 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
5662 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005663 }
5664 dcl = dcl->next;
5665 }
5666
5667 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005668 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
5669 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005670 }
5671
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005672 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005673 qemu_chr_initial_reset();
5674
5675 if (monitor_device && monitor_hd)
5676 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5677
5678 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5679 const char *devname = serial_devices[i];
5680 if (devname && strcmp(devname, "none")) {
5681 if (strstart(devname, "vc", 0))
5682 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5683 }
5684 }
5685
5686 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5687 const char *devname = parallel_devices[i];
5688 if (devname && strcmp(devname, "none")) {
5689 if (strstart(devname, "vc", 0))
5690 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5691 }
5692 }
5693
5694 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5695 const char *devname = virtio_consoles[i];
5696 if (virtcon_hds[i] && devname) {
5697 if (strstart(devname, "vc", 0))
5698 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5699 }
5700 }
5701
5702 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005703 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005704 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005705 }
5706
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005707 /* call android-specific setup function */
5708 android_emulation_setup();
5709
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005710#if !defined(CONFIG_STANDALONE_CORE)
5711 // For the standalone emulator (UI+core in one executable) we need to
5712 // set the window title here.
5713 android_emulator_set_base_port(android_base_port);
5714#endif
5715
Ot ten Thije871da2a2010-09-20 10:29:22 +01005716 if (loadvm)
5717 do_loadvm(cur_mon, loadvm);
5718
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005719 if (incoming) {
5720 autostart = 0; /* fixme how to deal with -daemonize */
5721 qemu_start_incoming_migration(incoming);
5722 }
5723
5724 if (autostart)
5725 vm_start();
5726
5727#ifndef _WIN32
5728 if (daemonize) {
5729 uint8_t status = 0;
5730 ssize_t len;
5731
5732 again1:
5733 len = write(fds[1], &status, 1);
5734 if (len == -1 && (errno == EINTR))
5735 goto again1;
5736
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005737 if (len != 1) {
5738 PANIC("Unable to daemonize");
5739 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005740
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005741 if (chdir("/")) {
5742 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005743 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005744 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005745 TFR(fd = open("/dev/null", O_RDWR));
5746 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005747 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005748 }
5749
5750 if (run_as) {
5751 pwd = getpwnam(run_as);
5752 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005753 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005754 }
5755 }
5756
5757 if (chroot_dir) {
5758 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005759 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005760 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005761 if (chdir("/")) {
5762 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005763 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005764 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005765 }
5766
5767 if (run_as) {
5768 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005769 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005770 }
5771 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005772 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005773 }
5774 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005775 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005776 }
5777 }
5778
5779 if (daemonize) {
5780 dup2(fd, 0);
5781 dup2(fd, 1);
5782 dup2(fd, 2);
5783
5784 close(fd);
5785 }
5786#endif
5787
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005788#ifdef CONFIG_ANDROID
5789 // This will notify the UI that the core is successfuly initialized
5790 android_core_init_completed();
5791#endif // CONFIG_ANDROID
5792
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005793 main_loop();
5794 quit_timers();
5795 net_cleanup();
5796 android_emulation_teardown();
5797 return 0;
5798}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005799
5800void
5801android_emulation_teardown(void)
5802{
5803 android_charmap_done();
5804}