blob: 2c3309212c0510dff2e2577b415d5d6db6bce2ec [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
1724/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001725/* I/O handling */
1726
1727typedef struct IOHandlerRecord {
1728 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001729 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001730 IOHandler *fd_read;
1731 IOHandler *fd_write;
1732 int deleted;
1733 void *opaque;
1734 /* temporary data */
1735 struct pollfd *ufd;
1736 struct IOHandlerRecord *next;
1737} IOHandlerRecord;
1738
1739static IOHandlerRecord *first_io_handler;
1740
1741/* XXX: fd_read_poll should be suppressed, but an API change is
1742 necessary in the character devices to suppress fd_can_read(). */
1743int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001744 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001745 IOHandler *fd_read,
1746 IOHandler *fd_write,
1747 void *opaque)
1748{
1749 IOHandlerRecord **pioh, *ioh;
1750
1751 if (!fd_read && !fd_write) {
1752 pioh = &first_io_handler;
1753 for(;;) {
1754 ioh = *pioh;
1755 if (ioh == NULL)
1756 break;
1757 if (ioh->fd == fd) {
1758 ioh->deleted = 1;
1759 break;
1760 }
1761 pioh = &ioh->next;
1762 }
1763 } else {
1764 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1765 if (ioh->fd == fd)
1766 goto found;
1767 }
1768 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1769 ioh->next = first_io_handler;
1770 first_io_handler = ioh;
1771 found:
1772 ioh->fd = fd;
1773 ioh->fd_read_poll = fd_read_poll;
1774 ioh->fd_read = fd_read;
1775 ioh->fd_write = fd_write;
1776 ioh->opaque = opaque;
1777 ioh->deleted = 0;
1778 }
1779 return 0;
1780}
1781
1782int qemu_set_fd_handler(int fd,
1783 IOHandler *fd_read,
1784 IOHandler *fd_write,
1785 void *opaque)
1786{
1787 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1788}
1789
1790#ifdef _WIN32
1791/***********************************************************/
1792/* Polling handling */
1793
1794typedef struct PollingEntry {
1795 PollingFunc *func;
1796 void *opaque;
1797 struct PollingEntry *next;
1798} PollingEntry;
1799
1800static PollingEntry *first_polling_entry;
1801
1802int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1803{
1804 PollingEntry **ppe, *pe;
1805 pe = qemu_mallocz(sizeof(PollingEntry));
1806 pe->func = func;
1807 pe->opaque = opaque;
1808 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1809 *ppe = pe;
1810 return 0;
1811}
1812
1813void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1814{
1815 PollingEntry **ppe, *pe;
1816 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1817 pe = *ppe;
1818 if (pe->func == func && pe->opaque == opaque) {
1819 *ppe = pe->next;
1820 qemu_free(pe);
1821 break;
1822 }
1823 }
1824}
1825
1826/***********************************************************/
1827/* Wait objects support */
1828typedef struct WaitObjects {
1829 int num;
1830 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1831 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1832 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1833} WaitObjects;
1834
1835static WaitObjects wait_objects = {0};
1836
1837int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1838{
1839 WaitObjects *w = &wait_objects;
1840
1841 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1842 return -1;
1843 w->events[w->num] = handle;
1844 w->func[w->num] = func;
1845 w->opaque[w->num] = opaque;
1846 w->num++;
1847 return 0;
1848}
1849
1850void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1851{
1852 int i, found;
1853 WaitObjects *w = &wait_objects;
1854
1855 found = 0;
1856 for (i = 0; i < w->num; i++) {
1857 if (w->events[i] == handle)
1858 found = 1;
1859 if (found) {
1860 w->events[i] = w->events[i + 1];
1861 w->func[i] = w->func[i + 1];
1862 w->opaque[i] = w->opaque[i + 1];
1863 }
1864 }
1865 if (found)
1866 w->num--;
1867}
1868#endif
1869
1870/***********************************************************/
1871/* ram save/restore */
1872
1873static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1874{
1875 int v;
1876
1877 v = qemu_get_byte(f);
1878 switch(v) {
1879 case 0:
1880 if (qemu_get_buffer(f, buf, len) != len)
1881 return -EIO;
1882 break;
1883 case 1:
1884 v = qemu_get_byte(f);
1885 memset(buf, v, len);
1886 break;
1887 default:
1888 return -EINVAL;
1889 }
1890
1891 if (qemu_file_has_error(f))
1892 return -EIO;
1893
1894 return 0;
1895}
1896
1897static int ram_load_v1(QEMUFile *f, void *opaque)
1898{
1899 int ret;
1900 ram_addr_t i;
1901
1902 if (qemu_get_be32(f) != last_ram_offset)
1903 return -EINVAL;
1904 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1905 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1906 if (ret)
1907 return ret;
1908 }
1909 return 0;
1910}
1911
1912#define BDRV_HASH_BLOCK_SIZE 1024
1913#define IOBUF_SIZE 4096
1914#define RAM_CBLOCK_MAGIC 0xfabe
1915
1916typedef struct RamDecompressState {
1917 z_stream zstream;
1918 QEMUFile *f;
1919 uint8_t buf[IOBUF_SIZE];
1920} RamDecompressState;
1921
1922static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1923{
1924 int ret;
1925 memset(s, 0, sizeof(*s));
1926 s->f = f;
1927 ret = inflateInit(&s->zstream);
1928 if (ret != Z_OK)
1929 return -1;
1930 return 0;
1931}
1932
1933static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1934{
1935 int ret, clen;
1936
1937 s->zstream.avail_out = len;
1938 s->zstream.next_out = buf;
1939 while (s->zstream.avail_out > 0) {
1940 if (s->zstream.avail_in == 0) {
1941 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1942 return -1;
1943 clen = qemu_get_be16(s->f);
1944 if (clen > IOBUF_SIZE)
1945 return -1;
1946 qemu_get_buffer(s->f, s->buf, clen);
1947 s->zstream.avail_in = clen;
1948 s->zstream.next_in = s->buf;
1949 }
1950 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1951 if (ret != Z_OK && ret != Z_STREAM_END) {
1952 return -1;
1953 }
1954 }
1955 return 0;
1956}
1957
1958static void ram_decompress_close(RamDecompressState *s)
1959{
1960 inflateEnd(&s->zstream);
1961}
1962
1963#define RAM_SAVE_FLAG_FULL 0x01
1964#define RAM_SAVE_FLAG_COMPRESS 0x02
1965#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1966#define RAM_SAVE_FLAG_PAGE 0x08
1967#define RAM_SAVE_FLAG_EOS 0x10
1968
1969static int is_dup_page(uint8_t *page, uint8_t ch)
1970{
1971 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1972 uint32_t *array = (uint32_t *)page;
1973 int i;
1974
1975 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1976 if (array[i] != val)
1977 return 0;
1978 }
1979
1980 return 1;
1981}
1982
1983static int ram_save_block(QEMUFile *f)
1984{
1985 static ram_addr_t current_addr = 0;
1986 ram_addr_t saved_addr = current_addr;
1987 ram_addr_t addr = 0;
1988 int found = 0;
1989
1990 while (addr < last_ram_offset) {
1991 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1992 uint8_t *p;
1993
1994 cpu_physical_memory_reset_dirty(current_addr,
1995 current_addr + TARGET_PAGE_SIZE,
1996 MIGRATION_DIRTY_FLAG);
1997
1998 p = qemu_get_ram_ptr(current_addr);
1999
2000 if (is_dup_page(p, *p)) {
2001 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
2002 qemu_put_byte(f, *p);
2003 } else {
2004 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
2005 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
2006 }
2007
2008 found = 1;
2009 break;
2010 }
2011 addr += TARGET_PAGE_SIZE;
2012 current_addr = (saved_addr + addr) % last_ram_offset;
2013 }
2014
2015 return found;
2016}
2017
2018static uint64_t bytes_transferred = 0;
2019
2020static ram_addr_t ram_save_remaining(void)
2021{
2022 ram_addr_t addr;
2023 ram_addr_t count = 0;
2024
2025 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2026 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2027 count++;
2028 }
2029
2030 return count;
2031}
2032
2033uint64_t ram_bytes_remaining(void)
2034{
2035 return ram_save_remaining() * TARGET_PAGE_SIZE;
2036}
2037
2038uint64_t ram_bytes_transferred(void)
2039{
2040 return bytes_transferred;
2041}
2042
2043uint64_t ram_bytes_total(void)
2044{
2045 return last_ram_offset;
2046}
2047
2048static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2049{
2050 ram_addr_t addr;
2051 uint64_t bytes_transferred_last;
2052 double bwidth = 0;
2053 uint64_t expected_time = 0;
2054
2055 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2056
2057 if (stage == 1) {
2058 /* Make sure all dirty bits are set */
2059 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2060 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2061 cpu_physical_memory_set_dirty(addr);
2062 }
2063
2064 /* Enable dirty memory tracking */
2065 cpu_physical_memory_set_dirty_tracking(1);
2066
2067 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2068 }
2069
2070 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002071 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002072
2073 while (!qemu_file_rate_limit(f)) {
2074 int ret;
2075
2076 ret = ram_save_block(f);
2077 bytes_transferred += ret * TARGET_PAGE_SIZE;
2078 if (ret == 0) /* no more blocks */
2079 break;
2080 }
2081
David Turner6a9ef172010-09-09 22:54:36 +02002082 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002083 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2084
2085 /* if we haven't transferred anything this round, force expected_time to a
2086 * a very high value, but without crashing */
2087 if (bwidth == 0)
2088 bwidth = 0.000001;
2089
2090 /* try transferring iterative blocks of memory */
2091
2092 if (stage == 3) {
2093
2094 /* flush all remaining blocks regardless of rate limiting */
2095 while (ram_save_block(f) != 0) {
2096 bytes_transferred += TARGET_PAGE_SIZE;
2097 }
2098 cpu_physical_memory_set_dirty_tracking(0);
2099 }
2100
2101 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2102
2103 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2104
2105 return (stage == 2) && (expected_time <= migrate_max_downtime());
2106}
2107
2108static int ram_load_dead(QEMUFile *f, void *opaque)
2109{
2110 RamDecompressState s1, *s = &s1;
2111 uint8_t buf[10];
2112 ram_addr_t i;
2113
2114 if (ram_decompress_open(s, f) < 0)
2115 return -EINVAL;
2116 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2117 if (ram_decompress_buf(s, buf, 1) < 0) {
2118 fprintf(stderr, "Error while reading ram block header\n");
2119 goto error;
2120 }
2121 if (buf[0] == 0) {
2122 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2123 BDRV_HASH_BLOCK_SIZE) < 0) {
2124 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2125 goto error;
2126 }
2127 } else {
2128 error:
2129 printf("Error block header\n");
2130 return -EINVAL;
2131 }
2132 }
2133 ram_decompress_close(s);
2134
2135 return 0;
2136}
2137
2138static int ram_load(QEMUFile *f, void *opaque, int version_id)
2139{
2140 ram_addr_t addr;
2141 int flags;
2142
2143 if (version_id == 1)
2144 return ram_load_v1(f, opaque);
2145
2146 if (version_id == 2) {
2147 if (qemu_get_be32(f) != last_ram_offset)
2148 return -EINVAL;
2149 return ram_load_dead(f, opaque);
2150 }
2151
2152 if (version_id != 3)
2153 return -EINVAL;
2154
2155 do {
2156 addr = qemu_get_be64(f);
2157
2158 flags = addr & ~TARGET_PAGE_MASK;
2159 addr &= TARGET_PAGE_MASK;
2160
2161 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2162 if (addr != last_ram_offset)
2163 return -EINVAL;
2164 }
2165
2166 if (flags & RAM_SAVE_FLAG_FULL) {
2167 if (ram_load_dead(f, opaque) < 0)
2168 return -EINVAL;
2169 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002170
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002171 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2172 uint8_t ch = qemu_get_byte(f);
2173 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2174 } else if (flags & RAM_SAVE_FLAG_PAGE)
2175 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2176 } while (!(flags & RAM_SAVE_FLAG_EOS));
2177
2178 return 0;
2179}
2180
2181void qemu_service_io(void)
2182{
2183 qemu_notify_event();
2184}
2185
2186/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002187/* machine registration */
2188
2189static QEMUMachine *first_machine = NULL;
2190QEMUMachine *current_machine = NULL;
2191
2192int qemu_register_machine(QEMUMachine *m)
2193{
2194 QEMUMachine **pm;
2195 pm = &first_machine;
2196 while (*pm != NULL)
2197 pm = &(*pm)->next;
2198 m->next = NULL;
2199 *pm = m;
2200 return 0;
2201}
2202
2203static QEMUMachine *find_machine(const char *name)
2204{
2205 QEMUMachine *m;
2206
2207 for(m = first_machine; m != NULL; m = m->next) {
2208 if (!strcmp(m->name, name))
2209 return m;
2210 }
2211 return NULL;
2212}
2213
2214static QEMUMachine *find_default_machine(void)
2215{
2216 QEMUMachine *m;
2217
2218 for(m = first_machine; m != NULL; m = m->next) {
2219 if (m->is_default) {
2220 return m;
2221 }
2222 }
2223 return NULL;
2224}
2225
2226/***********************************************************/
2227/* main execution loop */
2228
2229static void gui_update(void *opaque)
2230{
2231 uint64_t interval = GUI_REFRESH_INTERVAL;
2232 DisplayState *ds = opaque;
2233 DisplayChangeListener *dcl = ds->listeners;
2234
2235 dpy_refresh(ds);
2236
2237 while (dcl != NULL) {
2238 if (dcl->gui_timer_interval &&
2239 dcl->gui_timer_interval < interval)
2240 interval = dcl->gui_timer_interval;
2241 dcl = dcl->next;
2242 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002243 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002244}
2245
2246static void nographic_update(void *opaque)
2247{
2248 uint64_t interval = GUI_REFRESH_INTERVAL;
2249
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002250 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002251}
2252
2253struct vm_change_state_entry {
2254 VMChangeStateHandler *cb;
2255 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002256 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002257};
2258
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002259static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002260
2261VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2262 void *opaque)
2263{
2264 VMChangeStateEntry *e;
2265
2266 e = qemu_mallocz(sizeof (*e));
2267
2268 e->cb = cb;
2269 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002270 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002271 return e;
2272}
2273
2274void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2275{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002276 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002277 qemu_free (e);
2278}
2279
2280static void vm_state_notify(int running, int reason)
2281{
2282 VMChangeStateEntry *e;
2283
2284 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2285 e->cb(e->opaque, running, reason);
2286 }
2287}
2288
2289static void resume_all_vcpus(void);
2290static void pause_all_vcpus(void);
2291
2292void vm_start(void)
2293{
2294 if (!vm_running) {
2295 cpu_enable_ticks();
2296 vm_running = 1;
2297 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002298 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002299 resume_all_vcpus();
2300 }
2301}
2302
2303/* reset/shutdown handler */
2304
2305typedef struct QEMUResetEntry {
2306 QEMUResetHandler *func;
2307 void *opaque;
2308 int order;
2309 struct QEMUResetEntry *next;
2310} QEMUResetEntry;
2311
2312static QEMUResetEntry *first_reset_entry;
2313static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002314static int shutdown_requested, shutdown_signal = -1;
2315static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002316static int powerdown_requested;
2317static int debug_requested;
2318static int vmstop_requested;
2319
2320int qemu_shutdown_requested(void)
2321{
2322 int r = shutdown_requested;
2323 shutdown_requested = 0;
2324 return r;
2325}
2326
2327int qemu_reset_requested(void)
2328{
2329 int r = reset_requested;
2330 reset_requested = 0;
2331 return r;
2332}
2333
2334int qemu_powerdown_requested(void)
2335{
2336 int r = powerdown_requested;
2337 powerdown_requested = 0;
2338 return r;
2339}
2340
2341static int qemu_debug_requested(void)
2342{
2343 int r = debug_requested;
2344 debug_requested = 0;
2345 return r;
2346}
2347
2348static int qemu_vmstop_requested(void)
2349{
2350 int r = vmstop_requested;
2351 vmstop_requested = 0;
2352 return r;
2353}
2354
2355static void do_vm_stop(int reason)
2356{
2357 if (vm_running) {
2358 cpu_disable_ticks();
2359 vm_running = 0;
2360 pause_all_vcpus();
2361 vm_state_notify(0, reason);
2362 }
2363}
2364
2365void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2366{
2367 QEMUResetEntry **pre, *re;
2368
2369 pre = &first_reset_entry;
2370 while (*pre != NULL && (*pre)->order >= order) {
2371 pre = &(*pre)->next;
2372 }
2373 re = qemu_mallocz(sizeof(QEMUResetEntry));
2374 re->func = func;
2375 re->opaque = opaque;
2376 re->order = order;
2377 re->next = NULL;
2378 *pre = re;
2379}
2380
2381void qemu_system_reset(void)
2382{
2383 QEMUResetEntry *re;
2384
2385 /* reset all devices */
2386 for(re = first_reset_entry; re != NULL; re = re->next) {
2387 re->func(re->opaque);
2388 }
2389}
2390
2391void qemu_system_reset_request(void)
2392{
2393 if (no_reboot) {
2394 shutdown_requested = 1;
2395 } else {
2396 reset_requested = 1;
2397 }
2398 qemu_notify_event();
2399}
2400
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002401void qemu_system_killed(int signal, pid_t pid)
2402{
2403 shutdown_signal = signal;
2404 shutdown_pid = pid;
2405 qemu_system_shutdown_request();
2406}
2407
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002408void qemu_system_shutdown_request(void)
2409{
2410 shutdown_requested = 1;
2411 qemu_notify_event();
2412}
2413
2414void qemu_system_powerdown_request(void)
2415{
2416 powerdown_requested = 1;
2417 qemu_notify_event();
2418}
2419
2420#ifdef CONFIG_IOTHREAD
2421static void qemu_system_vmstop_request(int reason)
2422{
2423 vmstop_requested = reason;
2424 qemu_notify_event();
2425}
2426#endif
2427
2428#ifndef _WIN32
2429static int io_thread_fd = -1;
2430
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002431#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002432static void qemu_event_increment(void)
2433{
2434 static const char byte = 0;
2435
2436 if (io_thread_fd == -1)
2437 return;
2438
2439 write(io_thread_fd, &byte, sizeof(byte));
2440}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002441#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002442
2443static void qemu_event_read(void *opaque)
2444{
2445 int fd = (unsigned long)opaque;
2446 ssize_t len;
2447
2448 /* Drain the notify pipe */
2449 do {
2450 char buffer[512];
2451 len = read(fd, buffer, sizeof(buffer));
2452 } while ((len == -1 && errno == EINTR) || len > 0);
2453}
2454
2455static int qemu_event_init(void)
2456{
2457 int err;
2458 int fds[2];
2459
2460 err = pipe(fds);
2461 if (err == -1)
2462 return -errno;
2463
2464 err = fcntl_setfl(fds[0], O_NONBLOCK);
2465 if (err < 0)
2466 goto fail;
2467
2468 err = fcntl_setfl(fds[1], O_NONBLOCK);
2469 if (err < 0)
2470 goto fail;
2471
2472 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2473 (void *)(unsigned long)fds[0]);
2474
2475 io_thread_fd = fds[1];
2476 return 0;
2477
2478fail:
2479 close(fds[0]);
2480 close(fds[1]);
2481 return err;
2482}
2483#else
2484HANDLE qemu_event_handle;
2485
2486static void dummy_event_handler(void *opaque)
2487{
2488}
2489
2490static int qemu_event_init(void)
2491{
2492 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2493 if (!qemu_event_handle) {
2494 perror("Failed CreateEvent");
2495 return -1;
2496 }
2497 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2498 return 0;
2499}
2500
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002501#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002502static void qemu_event_increment(void)
2503{
2504 SetEvent(qemu_event_handle);
2505}
2506#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002507#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002508
2509static int cpu_can_run(CPUState *env)
2510{
2511 if (env->stop)
2512 return 0;
2513 if (env->stopped)
2514 return 0;
2515 return 1;
2516}
2517
2518#ifndef CONFIG_IOTHREAD
2519static int qemu_init_main_loop(void)
2520{
2521 return qemu_event_init();
2522}
2523
2524void qemu_init_vcpu(void *_env)
2525{
2526 CPUState *env = _env;
2527
2528 if (kvm_enabled())
2529 kvm_init_vcpu(env);
2530 return;
2531}
2532
2533int qemu_cpu_self(void *env)
2534{
2535 return 1;
2536}
2537
2538static void resume_all_vcpus(void)
2539{
2540}
2541
2542static void pause_all_vcpus(void)
2543{
2544}
2545
2546void qemu_cpu_kick(void *env)
2547{
2548 return;
2549}
2550
2551void qemu_notify_event(void)
2552{
2553 CPUState *env = cpu_single_env;
2554
2555 if (env) {
2556 cpu_exit(env);
2557#ifdef USE_KQEMU
2558 if (env->kqemu_enabled)
2559 kqemu_cpu_interrupt(env);
2560#endif
2561 }
2562}
2563
2564#define qemu_mutex_lock_iothread() do { } while (0)
2565#define qemu_mutex_unlock_iothread() do { } while (0)
2566
2567void vm_stop(int reason)
2568{
2569 do_vm_stop(reason);
2570}
2571
2572#else /* CONFIG_IOTHREAD */
2573
2574#include "qemu-thread.h"
2575
2576QemuMutex qemu_global_mutex;
2577static QemuMutex qemu_fair_mutex;
2578
2579static QemuThread io_thread;
2580
2581static QemuThread *tcg_cpu_thread;
2582static QemuCond *tcg_halt_cond;
2583
2584static int qemu_system_ready;
2585/* cpu creation */
2586static QemuCond qemu_cpu_cond;
2587/* system init */
2588static QemuCond qemu_system_cond;
2589static QemuCond qemu_pause_cond;
2590
2591static void block_io_signals(void);
2592static void unblock_io_signals(void);
2593static int tcg_has_work(void);
2594
2595static int qemu_init_main_loop(void)
2596{
2597 int ret;
2598
2599 ret = qemu_event_init();
2600 if (ret)
2601 return ret;
2602
2603 qemu_cond_init(&qemu_pause_cond);
2604 qemu_mutex_init(&qemu_fair_mutex);
2605 qemu_mutex_init(&qemu_global_mutex);
2606 qemu_mutex_lock(&qemu_global_mutex);
2607
2608 unblock_io_signals();
2609 qemu_thread_self(&io_thread);
2610
2611 return 0;
2612}
2613
2614static void qemu_wait_io_event(CPUState *env)
2615{
2616 while (!tcg_has_work())
2617 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2618
2619 qemu_mutex_unlock(&qemu_global_mutex);
2620
2621 /*
2622 * Users of qemu_global_mutex can be starved, having no chance
2623 * to acquire it since this path will get to it first.
2624 * So use another lock to provide fairness.
2625 */
2626 qemu_mutex_lock(&qemu_fair_mutex);
2627 qemu_mutex_unlock(&qemu_fair_mutex);
2628
2629 qemu_mutex_lock(&qemu_global_mutex);
2630 if (env->stop) {
2631 env->stop = 0;
2632 env->stopped = 1;
2633 qemu_cond_signal(&qemu_pause_cond);
2634 }
2635}
2636
2637static int qemu_cpu_exec(CPUState *env);
2638
2639static void *kvm_cpu_thread_fn(void *arg)
2640{
2641 CPUState *env = arg;
2642
2643 block_io_signals();
2644 qemu_thread_self(env->thread);
2645
2646 /* signal CPU creation */
2647 qemu_mutex_lock(&qemu_global_mutex);
2648 env->created = 1;
2649 qemu_cond_signal(&qemu_cpu_cond);
2650
2651 /* and wait for machine initialization */
2652 while (!qemu_system_ready)
2653 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2654
2655 while (1) {
2656 if (cpu_can_run(env))
2657 qemu_cpu_exec(env);
2658 qemu_wait_io_event(env);
2659 }
2660
2661 return NULL;
2662}
2663
2664static void tcg_cpu_exec(void);
2665
2666static void *tcg_cpu_thread_fn(void *arg)
2667{
2668 CPUState *env = arg;
2669
2670 block_io_signals();
2671 qemu_thread_self(env->thread);
2672
2673 /* signal CPU creation */
2674 qemu_mutex_lock(&qemu_global_mutex);
2675 for (env = first_cpu; env != NULL; env = env->next_cpu)
2676 env->created = 1;
2677 qemu_cond_signal(&qemu_cpu_cond);
2678
2679 /* and wait for machine initialization */
2680 while (!qemu_system_ready)
2681 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2682
2683 while (1) {
2684 tcg_cpu_exec();
2685 qemu_wait_io_event(cur_cpu);
2686 }
2687
2688 return NULL;
2689}
2690
2691void qemu_cpu_kick(void *_env)
2692{
2693 CPUState *env = _env;
2694 qemu_cond_broadcast(env->halt_cond);
2695 if (kvm_enabled())
2696 qemu_thread_signal(env->thread, SIGUSR1);
2697}
2698
2699int qemu_cpu_self(void *env)
2700{
2701 return (cpu_single_env != NULL);
2702}
2703
2704static void cpu_signal(int sig)
2705{
2706 if (cpu_single_env)
2707 cpu_exit(cpu_single_env);
2708}
2709
2710static void block_io_signals(void)
2711{
2712 sigset_t set;
2713 struct sigaction sigact;
2714
2715 sigemptyset(&set);
2716 sigaddset(&set, SIGUSR2);
2717 sigaddset(&set, SIGIO);
2718 sigaddset(&set, SIGALRM);
2719 pthread_sigmask(SIG_BLOCK, &set, NULL);
2720
2721 sigemptyset(&set);
2722 sigaddset(&set, SIGUSR1);
2723 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2724
2725 memset(&sigact, 0, sizeof(sigact));
2726 sigact.sa_handler = cpu_signal;
2727 sigaction(SIGUSR1, &sigact, NULL);
2728}
2729
2730static void unblock_io_signals(void)
2731{
2732 sigset_t set;
2733
2734 sigemptyset(&set);
2735 sigaddset(&set, SIGUSR2);
2736 sigaddset(&set, SIGIO);
2737 sigaddset(&set, SIGALRM);
2738 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2739
2740 sigemptyset(&set);
2741 sigaddset(&set, SIGUSR1);
2742 pthread_sigmask(SIG_BLOCK, &set, NULL);
2743}
2744
2745static void qemu_signal_lock(unsigned int msecs)
2746{
2747 qemu_mutex_lock(&qemu_fair_mutex);
2748
2749 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2750 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2751 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2752 break;
2753 }
2754 qemu_mutex_unlock(&qemu_fair_mutex);
2755}
2756
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002757void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002758{
2759 if (kvm_enabled()) {
2760 qemu_mutex_lock(&qemu_fair_mutex);
2761 qemu_mutex_lock(&qemu_global_mutex);
2762 qemu_mutex_unlock(&qemu_fair_mutex);
2763 } else
2764 qemu_signal_lock(100);
2765}
2766
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002767void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002768{
2769 qemu_mutex_unlock(&qemu_global_mutex);
2770}
2771
2772static int all_vcpus_paused(void)
2773{
2774 CPUState *penv = first_cpu;
2775
2776 while (penv) {
2777 if (!penv->stopped)
2778 return 0;
2779 penv = (CPUState *)penv->next_cpu;
2780 }
2781
2782 return 1;
2783}
2784
2785static void pause_all_vcpus(void)
2786{
2787 CPUState *penv = first_cpu;
2788
2789 while (penv) {
2790 penv->stop = 1;
2791 qemu_thread_signal(penv->thread, SIGUSR1);
2792 qemu_cpu_kick(penv);
2793 penv = (CPUState *)penv->next_cpu;
2794 }
2795
2796 while (!all_vcpus_paused()) {
2797 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2798 penv = first_cpu;
2799 while (penv) {
2800 qemu_thread_signal(penv->thread, SIGUSR1);
2801 penv = (CPUState *)penv->next_cpu;
2802 }
2803 }
2804}
2805
2806static void resume_all_vcpus(void)
2807{
2808 CPUState *penv = first_cpu;
2809
2810 while (penv) {
2811 penv->stop = 0;
2812 penv->stopped = 0;
2813 qemu_thread_signal(penv->thread, SIGUSR1);
2814 qemu_cpu_kick(penv);
2815 penv = (CPUState *)penv->next_cpu;
2816 }
2817}
2818
2819static void tcg_init_vcpu(void *_env)
2820{
2821 CPUState *env = _env;
2822 /* share a single thread for all cpus with TCG */
2823 if (!tcg_cpu_thread) {
2824 env->thread = qemu_mallocz(sizeof(QemuThread));
2825 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2826 qemu_cond_init(env->halt_cond);
2827 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2828 while (env->created == 0)
2829 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2830 tcg_cpu_thread = env->thread;
2831 tcg_halt_cond = env->halt_cond;
2832 } else {
2833 env->thread = tcg_cpu_thread;
2834 env->halt_cond = tcg_halt_cond;
2835 }
2836}
2837
2838static void kvm_start_vcpu(CPUState *env)
2839{
2840#if 0
2841 kvm_init_vcpu(env);
2842 env->thread = qemu_mallocz(sizeof(QemuThread));
2843 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2844 qemu_cond_init(env->halt_cond);
2845 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2846 while (env->created == 0)
2847 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2848#endif
2849}
2850
2851void qemu_init_vcpu(void *_env)
2852{
2853 CPUState *env = _env;
2854
2855 if (kvm_enabled())
2856 kvm_start_vcpu(env);
2857 else
2858 tcg_init_vcpu(env);
2859}
2860
2861void qemu_notify_event(void)
2862{
2863 qemu_event_increment();
2864}
2865
2866void vm_stop(int reason)
2867{
2868 QemuThread me;
2869 qemu_thread_self(&me);
2870
2871 if (!qemu_thread_equal(&me, &io_thread)) {
2872 qemu_system_vmstop_request(reason);
2873 /*
2874 * FIXME: should not return to device code in case
2875 * vm_stop() has been requested.
2876 */
2877 if (cpu_single_env) {
2878 cpu_exit(cpu_single_env);
2879 cpu_single_env->stop = 1;
2880 }
2881 return;
2882 }
2883 do_vm_stop(reason);
2884}
2885
2886#endif
2887
2888
2889#ifdef _WIN32
2890static void host_main_loop_wait(int *timeout)
2891{
2892 int ret, ret2, i;
2893 PollingEntry *pe;
2894
2895
2896 /* XXX: need to suppress polling by better using win32 events */
2897 ret = 0;
2898 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2899 ret |= pe->func(pe->opaque);
2900 }
2901 if (ret == 0) {
2902 int err;
2903 WaitObjects *w = &wait_objects;
2904
2905 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2906 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2907 if (w->func[ret - WAIT_OBJECT_0])
2908 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2909
2910 /* Check for additional signaled events */
2911 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2912
2913 /* Check if event is signaled */
2914 ret2 = WaitForSingleObject(w->events[i], 0);
2915 if(ret2 == WAIT_OBJECT_0) {
2916 if (w->func[i])
2917 w->func[i](w->opaque[i]);
2918 } else if (ret2 == WAIT_TIMEOUT) {
2919 } else {
2920 err = GetLastError();
2921 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2922 }
2923 }
2924 } else if (ret == WAIT_TIMEOUT) {
2925 } else {
2926 err = GetLastError();
2927 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2928 }
2929 }
2930
2931 *timeout = 0;
2932}
2933#else
2934static void host_main_loop_wait(int *timeout)
2935{
2936}
2937#endif
2938
2939void main_loop_wait(int timeout)
2940{
2941 IOHandlerRecord *ioh;
2942 fd_set rfds, wfds, xfds;
2943 int ret, nfds;
2944 struct timeval tv;
2945
2946 qemu_bh_update_timeout(&timeout);
2947
2948 host_main_loop_wait(&timeout);
2949
2950 /* poll any events */
2951 /* XXX: separate device handlers from system ones */
2952 nfds = -1;
2953 FD_ZERO(&rfds);
2954 FD_ZERO(&wfds);
2955 FD_ZERO(&xfds);
2956 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2957 if (ioh->deleted)
2958 continue;
2959 if (ioh->fd_read &&
2960 (!ioh->fd_read_poll ||
2961 ioh->fd_read_poll(ioh->opaque) != 0)) {
2962 FD_SET(ioh->fd, &rfds);
2963 if (ioh->fd > nfds)
2964 nfds = ioh->fd;
2965 }
2966 if (ioh->fd_write) {
2967 FD_SET(ioh->fd, &wfds);
2968 if (ioh->fd > nfds)
2969 nfds = ioh->fd;
2970 }
2971 }
2972
2973 tv.tv_sec = timeout / 1000;
2974 tv.tv_usec = (timeout % 1000) * 1000;
2975
2976#if defined(CONFIG_SLIRP)
2977 if (slirp_is_inited()) {
2978 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2979 }
2980#endif
2981 qemu_mutex_unlock_iothread();
2982 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2983 qemu_mutex_lock_iothread();
2984 if (ret > 0) {
2985 IOHandlerRecord **pioh;
2986
2987 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2988 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2989 ioh->fd_read(ioh->opaque);
2990 }
2991 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2992 ioh->fd_write(ioh->opaque);
2993 }
2994 }
2995
David 'Digit' Turner6b512812010-10-15 15:05:04 +02002996 /* remove deleted IO handlers */
2997 pioh = &first_io_handler;
2998 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002999 ioh = *pioh;
3000 if (ioh->deleted) {
3001 *pioh = ioh->next;
3002 qemu_free(ioh);
3003 } else
3004 pioh = &ioh->next;
3005 }
3006 }
3007#if defined(CONFIG_SLIRP)
3008 if (slirp_is_inited()) {
3009 if (ret < 0) {
3010 FD_ZERO(&rfds);
3011 FD_ZERO(&wfds);
3012 FD_ZERO(&xfds);
3013 }
3014 slirp_select_poll(&rfds, &wfds, &xfds);
3015 }
3016#endif
3017 charpipe_poll();
3018
David Turner6a9ef172010-09-09 22:54:36 +02003019 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003020
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003021 /* Check bottom-halves last in case any of the earlier events triggered
3022 them. */
3023 qemu_bh_poll();
3024
3025}
3026
3027static int qemu_cpu_exec(CPUState *env)
3028{
3029 int ret;
3030#ifdef CONFIG_PROFILER
3031 int64_t ti;
3032#endif
3033
3034#ifdef CONFIG_PROFILER
3035 ti = profile_getclock();
3036#endif
3037 if (use_icount) {
3038 int64_t count;
3039 int decr;
3040 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3041 env->icount_decr.u16.low = 0;
3042 env->icount_extra = 0;
David 'Digit' Turner317c9d52011-05-10 06:38:21 +02003043 count = qemu_next_icount_deadline();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003044 count = (count + (1 << icount_time_shift) - 1)
3045 >> icount_time_shift;
3046 qemu_icount += count;
3047 decr = (count > 0xffff) ? 0xffff : count;
3048 count -= decr;
3049 env->icount_decr.u16.low = decr;
3050 env->icount_extra = count;
3051 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003052#ifdef CONFIG_TRACE
3053 if (tbflush_requested) {
3054 tbflush_requested = 0;
3055 tb_flush(env);
3056 return EXCP_INTERRUPT;
3057 }
3058#endif
3059
3060
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003061 ret = cpu_exec(env);
3062#ifdef CONFIG_PROFILER
3063 qemu_time += profile_getclock() - ti;
3064#endif
3065 if (use_icount) {
3066 /* Fold pending instructions back into the
3067 instruction counter, and clear the interrupt flag. */
3068 qemu_icount -= (env->icount_decr.u16.low
3069 + env->icount_extra);
3070 env->icount_decr.u32 = 0;
3071 env->icount_extra = 0;
3072 }
3073 return ret;
3074}
3075
3076static void tcg_cpu_exec(void)
3077{
3078 int ret = 0;
3079
3080 if (next_cpu == NULL)
3081 next_cpu = first_cpu;
3082 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3083 CPUState *env = cur_cpu = next_cpu;
3084
3085 if (!vm_running)
3086 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003087 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003088 break;
3089 }
3090 if (cpu_can_run(env))
3091 ret = qemu_cpu_exec(env);
3092 if (ret == EXCP_DEBUG) {
3093 gdb_set_stop_cpu(env);
3094 debug_requested = 1;
3095 break;
3096 }
3097 }
3098}
3099
3100static int cpu_has_work(CPUState *env)
3101{
3102 if (env->stop)
3103 return 1;
3104 if (env->stopped)
3105 return 0;
3106 if (!env->halted)
3107 return 1;
3108 if (qemu_cpu_has_work(env))
3109 return 1;
3110 return 0;
3111}
3112
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003113int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003114{
3115 CPUState *env;
3116
3117 for (env = first_cpu; env != NULL; env = env->next_cpu)
3118 if (cpu_has_work(env))
3119 return 1;
3120 return 0;
3121}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003122
3123static int vm_can_run(void)
3124{
3125 if (powerdown_requested)
3126 return 0;
3127 if (reset_requested)
3128 return 0;
3129 if (shutdown_requested)
3130 return 0;
3131 if (debug_requested)
3132 return 0;
3133 return 1;
3134}
3135
3136static void main_loop(void)
3137{
3138 int r;
3139
3140#ifdef CONFIG_IOTHREAD
3141 qemu_system_ready = 1;
3142 qemu_cond_broadcast(&qemu_system_cond);
3143#endif
3144
3145 for (;;) {
3146 do {
3147#ifdef CONFIG_PROFILER
3148 int64_t ti;
3149#endif
3150#ifndef CONFIG_IOTHREAD
3151 tcg_cpu_exec();
3152#endif
3153#ifdef CONFIG_PROFILER
3154 ti = profile_getclock();
3155#endif
3156 main_loop_wait(qemu_calculate_timeout());
3157#ifdef CONFIG_PROFILER
3158 dev_time += profile_getclock() - ti;
3159#endif
rich canningsd952f282011-03-01 15:40:09 -08003160
3161 if (rotate_logs_requested) {
3162 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
3163 dns_log_filename);
3164 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
3165 drop_log_filename);
3166 slirp_dns_log_fd(new_dns_log_fd);
3167 slirp_drop_log_fd(new_drop_log_fd);
3168 reset_rotate_qemu_logs_request();
3169 }
3170
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003171 } while (vm_can_run());
3172
3173 if (qemu_debug_requested())
3174 vm_stop(EXCP_DEBUG);
3175 if (qemu_shutdown_requested()) {
3176 if (no_shutdown) {
3177 vm_stop(0);
3178 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003179 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003180 if (savevm_on_exit != NULL) {
3181 do_savevm(cur_mon, savevm_on_exit);
3182 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003183 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003184 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003185 }
3186 if (qemu_reset_requested()) {
3187 pause_all_vcpus();
3188 qemu_system_reset();
3189 resume_all_vcpus();
3190 }
3191 if (qemu_powerdown_requested())
3192 qemu_system_powerdown();
3193 if ((r = qemu_vmstop_requested()))
3194 vm_stop(r);
3195 }
3196 pause_all_vcpus();
3197}
3198
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003199void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003200{
3201 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3202}
3203
3204void qemu_help(int exitcode)
3205{
3206 version();
3207 printf("usage: %s [options] [disk_image]\n"
3208 "\n"
3209 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3210 "\n"
3211#define DEF(option, opt_arg, opt_enum, opt_help) \
3212 opt_help
3213#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003214#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003215#undef DEF
3216#undef DEFHEADING
3217#undef GEN_DOCS
3218 "\n"
3219 "During emulation, the following keys are useful:\n"
3220 "ctrl-alt-f toggle full screen\n"
3221 "ctrl-alt-n switch to virtual console 'n'\n"
3222 "ctrl-alt toggle mouse and keyboard grab\n"
3223 "\n"
3224 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3225 ,
3226 "qemu",
3227 DEFAULT_RAM_SIZE,
3228#ifndef _WIN32
3229 DEFAULT_NETWORK_SCRIPT,
3230 DEFAULT_NETWORK_DOWN_SCRIPT,
3231#endif
3232 DEFAULT_GDBSTUB_PORT,
3233 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003234 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003235}
3236
3237#define HAS_ARG 0x0001
3238
3239enum {
3240#define DEF(option, opt_arg, opt_enum, opt_help) \
3241 opt_enum,
3242#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003243#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003244#undef DEF
3245#undef DEFHEADING
3246#undef GEN_DOCS
3247};
3248
3249typedef struct QEMUOption {
3250 const char *name;
3251 int flags;
3252 int index;
3253} QEMUOption;
3254
3255static const QEMUOption qemu_options[] = {
3256 { "h", 0, QEMU_OPTION_h },
3257#define DEF(option, opt_arg, opt_enum, opt_help) \
3258 { option, opt_arg, opt_enum },
3259#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003260#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003261#undef DEF
3262#undef DEFHEADING
3263#undef GEN_DOCS
3264 { NULL, 0, 0 },
3265};
3266
3267#ifdef HAS_AUDIO
3268struct soundhw soundhw[] = {
3269#ifdef HAS_AUDIO_CHOICE
3270#if defined(TARGET_I386) || defined(TARGET_MIPS)
3271 {
3272 "pcspk",
3273 "PC speaker",
3274 0,
3275 1,
3276 { .init_isa = pcspk_audio_init }
3277 },
3278#endif
3279
3280#ifdef CONFIG_SB16
3281 {
3282 "sb16",
3283 "Creative Sound Blaster 16",
3284 0,
3285 1,
3286 { .init_isa = SB16_init }
3287 },
3288#endif
3289
3290#ifdef CONFIG_CS4231A
3291 {
3292 "cs4231a",
3293 "CS4231A",
3294 0,
3295 1,
3296 { .init_isa = cs4231a_init }
3297 },
3298#endif
3299
3300#ifdef CONFIG_ADLIB
3301 {
3302 "adlib",
3303#ifdef HAS_YMF262
3304 "Yamaha YMF262 (OPL3)",
3305#else
3306 "Yamaha YM3812 (OPL2)",
3307#endif
3308 0,
3309 1,
3310 { .init_isa = Adlib_init }
3311 },
3312#endif
3313
3314#ifdef CONFIG_GUS
3315 {
3316 "gus",
3317 "Gravis Ultrasound GF1",
3318 0,
3319 1,
3320 { .init_isa = GUS_init }
3321 },
3322#endif
3323
3324#ifdef CONFIG_AC97
3325 {
3326 "ac97",
3327 "Intel 82801AA AC97 Audio",
3328 0,
3329 0,
3330 { .init_pci = ac97_init }
3331 },
3332#endif
3333
3334#ifdef CONFIG_ES1370
3335 {
3336 "es1370",
3337 "ENSONIQ AudioPCI ES1370",
3338 0,
3339 0,
3340 { .init_pci = es1370_init }
3341 },
3342#endif
3343
3344#endif /* HAS_AUDIO_CHOICE */
3345
3346 { NULL, NULL, 0, 0, { NULL } }
3347};
3348
3349static void select_soundhw (const char *optarg)
3350{
3351 struct soundhw *c;
3352
3353 if (*optarg == '?') {
3354 show_valid_cards:
3355
3356 printf ("Valid sound card names (comma separated):\n");
3357 for (c = soundhw; c->name; ++c) {
3358 printf ("%-11s %s\n", c->name, c->descr);
3359 }
3360 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003361 if (*optarg != '?') {
3362 PANIC("Unknown sound card name: %s", optarg);
3363 } else {
3364 QEMU_EXIT(0);
3365 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003366 }
3367 else {
3368 size_t l;
3369 const char *p;
3370 char *e;
3371 int bad_card = 0;
3372
3373 if (!strcmp (optarg, "all")) {
3374 for (c = soundhw; c->name; ++c) {
3375 c->enabled = 1;
3376 }
3377 return;
3378 }
3379
3380 p = optarg;
3381 while (*p) {
3382 e = strchr (p, ',');
3383 l = !e ? strlen (p) : (size_t) (e - p);
3384
3385 for (c = soundhw; c->name; ++c) {
3386 if (!strncmp (c->name, p, l)) {
3387 c->enabled = 1;
3388 break;
3389 }
3390 }
3391
3392 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003393#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003394 if (l > 80) {
3395 fprintf (stderr,
3396 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003397 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003398 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3399 (int) l, p);
3400 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003401#endif // !CONFIG_ANDROID
3402 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003403 }
3404 p += l + (e != NULL);
3405 }
3406
3407 if (bad_card)
3408 goto show_valid_cards;
3409 }
3410}
3411#endif
3412
3413static void select_vgahw (const char *p)
3414{
3415 const char *opts;
3416
3417 cirrus_vga_enabled = 0;
3418 std_vga_enabled = 0;
3419 vmsvga_enabled = 0;
3420 xenfb_enabled = 0;
3421 if (strstart(p, "std", &opts)) {
3422 std_vga_enabled = 1;
3423 } else if (strstart(p, "cirrus", &opts)) {
3424 cirrus_vga_enabled = 1;
3425 } else if (strstart(p, "vmware", &opts)) {
3426 vmsvga_enabled = 1;
3427 } else if (strstart(p, "xenfb", &opts)) {
3428 xenfb_enabled = 1;
3429 } else if (!strstart(p, "none", &opts)) {
3430 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003431 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003432 }
3433 while (*opts) {
3434 const char *nextopt;
3435
3436 if (strstart(opts, ",retrace=", &nextopt)) {
3437 opts = nextopt;
3438 if (strstart(opts, "dumb", &nextopt))
3439 vga_retrace_method = VGA_RETRACE_DUMB;
3440 else if (strstart(opts, "precise", &nextopt))
3441 vga_retrace_method = VGA_RETRACE_PRECISE;
3442 else goto invalid_vga;
3443 } else goto invalid_vga;
3444 opts = nextopt;
3445 }
3446}
3447
3448#ifdef _WIN32
3449static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3450{
3451 exit(STATUS_CONTROL_C_EXIT);
3452 return TRUE;
3453}
3454#endif
3455
3456int qemu_uuid_parse(const char *str, uint8_t *uuid)
3457{
3458 int ret;
3459
3460 if(strlen(str) != 36)
3461 return -1;
3462
3463 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3464 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3465 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3466
3467 if(ret != 16)
3468 return -1;
3469
3470#ifdef TARGET_I386
3471 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3472#endif
3473
3474 return 0;
3475}
3476
3477#define MAX_NET_CLIENTS 32
3478
3479#ifndef _WIN32
3480
3481static void termsig_handler(int signal)
3482{
3483 qemu_system_shutdown_request();
3484}
3485
3486static void sigchld_handler(int signal)
3487{
3488 waitpid(-1, NULL, WNOHANG);
3489}
3490
3491static void sighandler_setup(void)
3492{
3493 struct sigaction act;
3494
3495 memset(&act, 0, sizeof(act));
3496 act.sa_handler = termsig_handler;
3497 sigaction(SIGINT, &act, NULL);
3498 sigaction(SIGHUP, &act, NULL);
3499 sigaction(SIGTERM, &act, NULL);
3500
3501 act.sa_handler = sigchld_handler;
3502 act.sa_flags = SA_NOCLDSTOP;
3503 sigaction(SIGCHLD, &act, NULL);
3504}
3505
3506#endif
3507
3508#ifdef _WIN32
3509/* Look for support files in the same directory as the executable. */
3510static char *find_datadir(const char *argv0)
3511{
3512 char *p;
3513 char buf[MAX_PATH];
3514 DWORD len;
3515
3516 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3517 if (len == 0) {
3518 return NULL;
3519 }
3520
3521 buf[len] = 0;
3522 p = buf + len - 1;
3523 while (p != buf && *p != '\\')
3524 p--;
3525 *p = 0;
3526 if (access(buf, R_OK) == 0) {
3527 return qemu_strdup(buf);
3528 }
3529 return NULL;
3530}
3531#else /* !_WIN32 */
3532
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003533/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003534static char *find_datadir(const char *argv0)
3535{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003536 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003537 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003538
3539#if defined(__linux__)
3540 {
3541 int len;
3542 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3543 if (len > 0) {
3544 buf[len] = 0;
3545 p = buf;
3546 }
3547 }
3548#elif defined(__FreeBSD__)
3549 {
3550 int len;
3551 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3552 if (len > 0) {
3553 buf[len] = 0;
3554 p = buf;
3555 }
3556 }
3557#endif
3558 /* If we don't have any way of figuring out the actual executable
3559 location then try argv[0]. */
3560 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003561 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003562 if (!p) {
3563 return NULL;
3564 }
3565 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003566
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003567 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003568}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003569#endif
3570
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003571static char*
3572qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
3573{
3574 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3575 char* buf = qemu_mallocz(len);
3576
3577 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3578 VERBOSE_PRINT(init," trying to find: %s\n", buf);
3579 if (access(buf, R_OK)) {
3580 qemu_free(buf);
3581 return NULL;
3582 }
3583 return buf;
3584}
3585
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003586char *qemu_find_file(int type, const char *name)
3587{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003588 const char *subdir;
3589 char *buf;
3590
3591 /* If name contains path separators then try it as a straight path. */
3592 if ((strchr(name, '/') || strchr(name, '\\'))
3593 && access(name, R_OK) == 0) {
3594 return strdup(name);
3595 }
3596 switch (type) {
3597 case QEMU_FILE_TYPE_BIOS:
3598 subdir = "";
3599 break;
3600 case QEMU_FILE_TYPE_KEYMAP:
3601 subdir = "keymaps/";
3602 break;
3603 default:
3604 abort();
3605 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003606 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
3607#ifdef CONFIG_ANDROID
3608 if (type == QEMU_FILE_TYPE_BIOS) {
3609 /* This case corresponds to the emulator being used as part of an
3610 * SDK installation. NOTE: data_dir is really $bindir. */
3611 if (buf == NULL)
3612 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
3613 /* This case corresponds to platform builds. */
3614 if (buf == NULL)
3615 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
3616 /* Finally, try this for standalone builds under external/qemu */
3617 if (buf == NULL)
3618 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003619 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003620#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003621 return buf;
3622}
3623
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003624static int
3625add_dns_server( const char* server_name )
3626{
3627 SockAddress addr;
3628
3629 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3630 fprintf(stdout,
3631 "### WARNING: can't resolve DNS server name '%s'\n",
3632 server_name );
3633 return -1;
3634 }
3635
3636 fprintf(stderr,
3637 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3638
3639 if ( slirp_add_dns_server( &addr ) < 0 ) {
3640 fprintf(stderr,
3641 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3642 return -1;
3643 }
3644 return 0;
3645}
3646
rich cannings7339b552011-02-16 13:43:44 -08003647/* Parses an integer
3648 * Pararm:
3649 * str String containing a number to be parsed.
3650 * result Passes the parsed integer in this argument
3651 * returns 0 if ok, -1 if failed
3652 */
3653int
3654parse_int(const char *str, int *result)
3655{
3656 char* r;
3657 *result = strtol(str, &r, 0);
3658 if (r == NULL || *r != '\0')
3659 return -1;
3660
3661 return 0;
3662}
3663
rich canningsd952f282011-03-01 15:40:09 -08003664#ifndef _WIN32
3665/*
3666 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3667 */
3668void init_qemu_clear_logs_sig() {
3669 struct sigaction act;
3670 sigfillset(&act.sa_mask);
3671 act.sa_flags = 0;
3672 act.sa_handler = rotate_qemu_logs_handler;
3673 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3674 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3675 exit(-1);
3676 }
3677}
3678#endif
3679
3680
rich cannings7339b552011-02-16 13:43:44 -08003681
3682/* parses a null-terminated string specifying a network port (e.g., "80") or
3683 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3684 * are the same. Returns 0 on success, -1 on error. */
3685
3686int parse_port_range(const char *str, unsigned short *lport,
3687 unsigned short *hport) {
3688
3689 unsigned int low = 0, high = 0;
3690 char *p, *arg = strdup(str);
3691
3692 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3693 p = arg + 1; /* skip '[' */
3694 low = atoi(strtok(p, "-"));
3695 high = atoi(strtok(NULL, "-"));
3696 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3697 *lport = low;
3698 *hport = high;
3699 }
3700 }
3701 else {
3702 low = atoi(arg);
3703 if ((0 < low) && (low < 65535)) {
3704 *lport = low;
3705 *hport = low;
3706 }
3707 }
3708 free(arg);
3709 if (low != 0)
3710 return 0;
3711 return -1;
3712}
3713
3714/*
3715 * Implements the generic port forwarding option
3716 */
3717void
3718net_slirp_forward(const char *optarg)
3719{
3720 /*
3721 * we expect the following format:
3722 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3723 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3724 */
3725 char *argument = strdup(optarg), *p = argument;
3726 char *dst_net, *dst_mask, *dst_port;
3727 char *redirect_ip, *redirect_port;
3728 uint32_t dnet, dmask, rip;
3729 unsigned short dlport, dhport, rport;
3730
3731
3732 dst_net = strtok(p, ":");
3733 dst_mask = strtok(NULL, ":");
3734 dst_port = strtok(NULL, ":");
3735 redirect_ip = strtok(NULL, ":");
3736 redirect_port = strtok(NULL, ":");
3737
3738 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3739 redirect_ip == NULL || redirect_port == NULL) {
3740 fprintf(stderr,
3741 "Invalid argument for -net-forward, we expect "
3742 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3743 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3744 ":redirect_ip:redirect_port: %s\n",
3745 optarg);
3746 exit(1);
3747 }
3748
3749 /* inet_strtoip converts dotted address to host byte order */
3750 if (inet_strtoip(dst_net, &dnet) == -1) {
3751 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3752 exit(1);
3753 }
3754 if (inet_strtoip(dst_mask, &dmask) == -1) {
3755 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3756 exit(1);
3757 }
3758 if (inet_strtoip(redirect_ip, &rip) == -1) {
3759 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3760 exit(1);
3761 }
3762
3763 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3764 fprintf(stderr, "Invalid destination port or port range\n");
3765 exit(1);
3766 }
3767
3768 rport = atoi(redirect_port);
3769 if (!rport) {
3770 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3771 exit(1);
3772 }
3773
3774 dnet &= dmask;
3775
3776 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3777 rip, rport);
3778
3779 free(argument);
3780}
3781
3782
3783/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3784 * entry in the allows list */
3785void
3786slirp_allow(const char *optarg, u_int8_t proto)
3787{
3788 /*
3789 * we expect the following format:
3790 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3791 */
3792 char *argument = strdup(optarg), *p = argument;
3793 char *dst_ip_str, *dst_port_str;
3794 uint32_t dst_ip;
3795 unsigned short dst_lport, dst_hport;
3796
3797 dst_ip_str = strtok(p, ":");
3798 dst_port_str = strtok(NULL, ":");
3799
3800 if (dst_ip_str == NULL || dst_port_str == NULL) {
3801 fprintf(stderr,
3802 "Invalid argument %s for -allow. We expect "
3803 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3804 optarg);
3805 exit(1);
3806 }
3807
3808 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3809 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3810 exit(1);
3811 }
3812 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3813 fprintf(stderr, "Invalid destination port or port range\n");
3814 exit(1);
3815 }
3816
3817 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3818
3819 free(argument);
3820}
3821
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003822/* Add a serial device at a given location in the emulated hardware table.
3823 * On failure, this function aborts the program with an error message.
3824 */
3825static void
3826serial_hds_add_at(int index, const char* devname)
3827{
3828 char label[32];
3829
3830 if (!devname || !strcmp(devname,"none"))
3831 return;
3832
3833 if (index >= MAX_SERIAL_PORTS) {
3834 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3835 devname, index, MAX_SERIAL_PORTS);
3836 }
3837 if (serial_hds[index] != NULL) {
3838 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3839 devname, index);
3840 }
3841 snprintf(label, sizeof(label), "serial%d", index);
3842 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3843 if (!serial_hds[index]) {
3844 PANIC("qemu: could not open serial device '%s'", devname);
3845 }
3846}
3847
3848
3849/* Find a free slot in the emulated serial device table, and register
3850 * it. Return the allocated table index.
3851 */
3852static int
3853serial_hds_add(const char* devname)
3854{
3855 int index;
3856
3857 /* Find first free slot */
3858 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3859 if (serial_hds[index] == NULL) {
3860 serial_hds_add_at(index, devname);
3861 return index;
3862 }
3863 }
3864
3865 PANIC("qemu: too many serial devices registered (%d)", index);
3866 return -1; /* shouldn't happen */
3867}
3868
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003869int main(int argc, char **argv, char **envp)
3870{
3871 const char *gdbstub_dev = NULL;
3872 uint32_t boot_devices_bitmap = 0;
3873 int i;
3874 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003875 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003876 const char *initrd_filename;
3877 const char *kernel_filename, *kernel_cmdline;
3878 const char *boot_devices = "";
3879 DisplayState *ds;
3880 DisplayChangeListener *dcl;
3881 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003882 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003883 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003884 const char *net_clients[MAX_NET_CLIENTS];
3885 int nb_net_clients;
3886 const char *bt_opts[MAX_BT_CMDLINE];
3887 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003888 int optind;
3889 const char *r, *optarg;
3890 CharDriverState *monitor_hd = NULL;
3891 const char *monitor_device;
3892 const char *serial_devices[MAX_SERIAL_PORTS];
3893 int serial_device_index;
3894 const char *parallel_devices[MAX_PARALLEL_PORTS];
3895 int parallel_device_index;
3896 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3897 int virtio_console_index;
3898 const char *loadvm = NULL;
3899 QEMUMachine *machine;
3900 const char *cpu_model;
3901 const char *usb_devices[MAX_USB_CMDLINE];
3902 int usb_devices_index;
3903#ifndef _WIN32
3904 int fds[2];
3905#endif
3906 int tb_size;
3907 const char *pid_file = NULL;
3908 const char *incoming = NULL;
3909#ifndef _WIN32
3910 int fd = 0;
3911 struct passwd *pwd = NULL;
3912 const char *chroot_dir = NULL;
3913 const char *run_as = NULL;
3914#endif
3915 CPUState *env;
3916 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003917 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003918 STRALLOC_DEFINE(kernel_params);
3919 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003920 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003921
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003922 /* Initialize sockets before anything else, so we can properly report
3923 * initialization failures back to the UI. */
3924#ifdef _WIN32
3925 socket_init();
3926#endif
3927
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003928 init_clocks();
3929
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003930 qemu_cache_utils_init(envp);
3931
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003932 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003933#ifndef _WIN32
3934 {
3935 struct sigaction act;
3936 sigfillset(&act.sa_mask);
3937 act.sa_flags = 0;
3938 act.sa_handler = SIG_IGN;
3939 sigaction(SIGPIPE, &act, NULL);
3940 }
3941#else
3942 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3943 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3944 QEMU to run on a single CPU */
3945 {
3946 HANDLE h;
3947 DWORD mask, smask;
3948 int i;
3949 h = GetCurrentProcess();
3950 if (GetProcessAffinityMask(h, &mask, &smask)) {
3951 for(i = 0; i < 32; i++) {
3952 if (mask & (1 << i))
3953 break;
3954 }
3955 if (i != 32) {
3956 mask = 1 << i;
3957 SetProcessAffinityMask(h, mask);
3958 }
3959 }
3960 }
3961#endif
3962
3963 module_call_init(MODULE_INIT_MACHINE);
3964 machine = find_default_machine();
3965 cpu_model = NULL;
3966 initrd_filename = NULL;
3967 ram_size = 0;
3968 snapshot = 0;
3969 kernel_filename = NULL;
3970 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003971
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003972 cyls = heads = secs = 0;
3973 translation = BIOS_ATA_TRANSLATION_AUTO;
3974 monitor_device = "vc:80Cx24C";
3975
3976 serial_devices[0] = "vc:80Cx24C";
3977 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3978 serial_devices[i] = NULL;
3979 serial_device_index = 0;
3980
3981 parallel_devices[0] = "vc:80Cx24C";
3982 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3983 parallel_devices[i] = NULL;
3984 parallel_device_index = 0;
3985
3986 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3987 virtio_consoles[i] = NULL;
3988 virtio_console_index = 0;
3989
3990 for (i = 0; i < MAX_NODES; i++) {
3991 node_mem[i] = 0;
3992 node_cpumask[i] = 0;
3993 }
3994
3995 usb_devices_index = 0;
3996
3997 nb_net_clients = 0;
3998 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003999#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004000 nb_drives = 0;
4001 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004002#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004003 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004004
4005 nb_nics = 0;
4006
4007 tb_size = 0;
4008 autostart= 1;
4009
4010 register_watchdogs();
4011
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004012 /* Initialize boot properties. */
4013 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02004014 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02004015 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004016
David 'Digit' Turner36597752011-05-20 01:18:01 +02004017#ifdef CONFIG_KVM
4018 /* By default, force auto-detection for kvm */
4019 kvm_allowed = -1;
4020#endif
4021
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004022 optind = 1;
4023 for(;;) {
4024 if (optind >= argc)
4025 break;
4026 r = argv[optind];
4027 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004028 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004029 } else {
4030 const QEMUOption *popt;
4031
4032 optind++;
4033 /* Treat --foo the same as -foo. */
4034 if (r[1] == '-')
4035 r++;
4036 popt = qemu_options;
4037 for(;;) {
4038 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004039 PANIC("%s: invalid option -- '%s'",
4040 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004041 }
4042 if (!strcmp(popt->name, r + 1))
4043 break;
4044 popt++;
4045 }
4046 if (popt->flags & HAS_ARG) {
4047 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004048 PANIC("%s: option '%s' requires an argument",
4049 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004050 }
4051 optarg = argv[optind++];
4052 } else {
4053 optarg = NULL;
4054 }
4055
4056 switch(popt->index) {
4057 case QEMU_OPTION_M:
4058 machine = find_machine(optarg);
4059 if (!machine) {
4060 QEMUMachine *m;
4061 printf("Supported machines are:\n");
4062 for(m = first_machine; m != NULL; m = m->next) {
4063 printf("%-10s %s%s\n",
4064 m->name, m->desc,
4065 m->is_default ? " (default)" : "");
4066 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004067 if (*optarg != '?') {
4068 PANIC("Invalid machine parameter: %s",
4069 optarg);
4070 } else {
4071 QEMU_EXIT(0);
4072 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004073 }
4074 break;
4075 case QEMU_OPTION_cpu:
4076 /* hw initialization will check this */
4077 if (*optarg == '?') {
4078/* XXX: implement xxx_cpu_list for targets that still miss it */
4079#if defined(cpu_list)
4080 cpu_list(stdout, &fprintf);
4081#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004082 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004083 } else {
4084 cpu_model = optarg;
4085 }
4086 break;
4087 case QEMU_OPTION_initrd:
4088 initrd_filename = optarg;
4089 break;
4090 case QEMU_OPTION_hda:
4091 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004092 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004093 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004094 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004095 ",cyls=%d,heads=%d,secs=%d%s",
4096 0, cyls, heads, secs,
4097 translation == BIOS_ATA_TRANSLATION_LBA ?
4098 ",trans=lba" :
4099 translation == BIOS_ATA_TRANSLATION_NONE ?
4100 ",trans=none" : "");
4101 break;
4102 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004103 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
4104 break;
4105
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004106 case QEMU_OPTION_hdc:
4107 case QEMU_OPTION_hdd:
4108 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4109 break;
4110 case QEMU_OPTION_drive:
4111 drive_add(NULL, "%s", optarg);
4112 break;
4113 case QEMU_OPTION_mtdblock:
4114 drive_add(optarg, MTD_ALIAS);
4115 break;
4116 case QEMU_OPTION_sd:
4117 drive_add(optarg, SD_ALIAS);
4118 break;
4119 case QEMU_OPTION_pflash:
4120 drive_add(optarg, PFLASH_ALIAS);
4121 break;
4122 case QEMU_OPTION_snapshot:
4123 snapshot = 1;
4124 break;
4125 case QEMU_OPTION_hdachs:
4126 {
4127 const char *p;
4128 p = optarg;
4129 cyls = strtol(p, (char **)&p, 0);
4130 if (cyls < 1 || cyls > 16383)
4131 goto chs_fail;
4132 if (*p != ',')
4133 goto chs_fail;
4134 p++;
4135 heads = strtol(p, (char **)&p, 0);
4136 if (heads < 1 || heads > 16)
4137 goto chs_fail;
4138 if (*p != ',')
4139 goto chs_fail;
4140 p++;
4141 secs = strtol(p, (char **)&p, 0);
4142 if (secs < 1 || secs > 63)
4143 goto chs_fail;
4144 if (*p == ',') {
4145 p++;
4146 if (!strcmp(p, "none"))
4147 translation = BIOS_ATA_TRANSLATION_NONE;
4148 else if (!strcmp(p, "lba"))
4149 translation = BIOS_ATA_TRANSLATION_LBA;
4150 else if (!strcmp(p, "auto"))
4151 translation = BIOS_ATA_TRANSLATION_AUTO;
4152 else
4153 goto chs_fail;
4154 } else if (*p != '\0') {
4155 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004156 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004157 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004158 if (hda_opts != NULL) {
4159 char num[16];
4160 snprintf(num, sizeof(num), "%d", cyls);
4161 qemu_opt_set(hda_opts, "cyls", num);
4162 snprintf(num, sizeof(num), "%d", heads);
4163 qemu_opt_set(hda_opts, "heads", num);
4164 snprintf(num, sizeof(num), "%d", secs);
4165 qemu_opt_set(hda_opts, "secs", num);
4166 if (translation == BIOS_ATA_TRANSLATION_LBA)
4167 qemu_opt_set(hda_opts, "trans", "lba");
4168 if (translation == BIOS_ATA_TRANSLATION_NONE)
4169 qemu_opt_set(hda_opts, "trans", "none");
4170 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004171 }
4172 break;
4173 case QEMU_OPTION_numa:
4174 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004175 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004176 }
4177 numa_add(optarg);
4178 break;
4179 case QEMU_OPTION_nographic:
4180 display_type = DT_NOGRAPHIC;
4181 break;
4182#ifdef CONFIG_CURSES
4183 case QEMU_OPTION_curses:
4184 display_type = DT_CURSES;
4185 break;
4186#endif
4187 case QEMU_OPTION_portrait:
4188 graphic_rotate = 1;
4189 break;
4190 case QEMU_OPTION_kernel:
4191 kernel_filename = optarg;
4192 break;
4193 case QEMU_OPTION_append:
4194 kernel_cmdline = optarg;
4195 break;
4196 case QEMU_OPTION_cdrom:
4197 drive_add(optarg, CDROM_ALIAS);
4198 break;
4199 case QEMU_OPTION_boot:
4200 boot_devices = optarg;
4201 /* We just do some generic consistency checks */
4202 {
4203 /* Could easily be extended to 64 devices if needed */
4204 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004205
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004206 boot_devices_bitmap = 0;
4207 for (p = boot_devices; *p != '\0'; p++) {
4208 /* Allowed boot devices are:
4209 * a b : floppy disk drives
4210 * c ... f : IDE disk drives
4211 * g ... m : machine implementation dependant drives
4212 * n ... p : network devices
4213 * It's up to each machine implementation to check
4214 * if the given boot devices match the actual hardware
4215 * implementation and firmware features.
4216 */
4217 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004218 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004219 }
4220 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004221 PANIC(
4222 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004223 }
4224 boot_devices_bitmap |= 1 << (*p - 'a');
4225 }
4226 }
4227 break;
4228 case QEMU_OPTION_fda:
4229 case QEMU_OPTION_fdb:
4230 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4231 break;
4232#ifdef TARGET_I386
4233 case QEMU_OPTION_no_fd_bootchk:
4234 fd_bootchk = 0;
4235 break;
4236#endif
4237 case QEMU_OPTION_net:
4238 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004239 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004240 }
4241 net_clients[nb_net_clients] = optarg;
4242 nb_net_clients++;
4243 break;
4244#ifdef CONFIG_SLIRP
4245 case QEMU_OPTION_tftp:
4246 tftp_prefix = optarg;
4247 break;
4248 case QEMU_OPTION_bootp:
4249 bootp_filename = optarg;
4250 break;
4251#if 0 /* ANDROID disabled */
4252#ifndef _WIN32
4253 case QEMU_OPTION_smb:
4254 net_slirp_smb(optarg);
4255 break;
4256#endif
4257#endif /* ANDROID */
4258 case QEMU_OPTION_redir:
4259 net_slirp_redir(NULL, optarg, NULL);
4260 break;
4261#endif
4262 case QEMU_OPTION_bt:
4263 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004264 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004265 }
4266 bt_opts[nb_bt_opts++] = optarg;
4267 break;
4268#ifdef HAS_AUDIO
4269 case QEMU_OPTION_audio_help:
4270 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004271 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004272 break;
4273 case QEMU_OPTION_soundhw:
4274 select_soundhw (optarg);
4275 break;
4276#endif
4277 case QEMU_OPTION_h:
4278 qemu_help(0);
4279 break;
4280 case QEMU_OPTION_version:
4281 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004282 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004283 break;
4284 case QEMU_OPTION_m: {
4285 uint64_t value;
4286 char *ptr;
4287
4288 value = strtoul(optarg, &ptr, 10);
4289 switch (*ptr) {
4290 case 0: case 'M': case 'm':
4291 value <<= 20;
4292 break;
4293 case 'G': case 'g':
4294 value <<= 30;
4295 break;
4296 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004297 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004298 }
4299
4300 /* On 32-bit hosts, QEMU is limited by virtual address space */
4301 if (value > (2047 << 20)
4302#ifndef CONFIG_KQEMU
4303 && HOST_LONG_BITS == 32
4304#endif
4305 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004306 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004307 }
4308 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004309 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004310 }
4311 ram_size = value;
4312 break;
4313 }
4314 case QEMU_OPTION_d:
4315 {
4316 int mask;
4317 const CPULogItem *item;
4318
4319 mask = cpu_str_to_log_mask(optarg);
4320 if (!mask) {
4321 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004322 for(item = cpu_log_items; item->mask != 0; item++) {
4323 printf("%-10s %s\n", item->name, item->help);
4324 }
4325 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004326 }
4327 cpu_set_log(mask);
4328 }
4329 break;
4330 case QEMU_OPTION_s:
4331 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4332 break;
4333 case QEMU_OPTION_gdb:
4334 gdbstub_dev = optarg;
4335 break;
4336 case QEMU_OPTION_L:
4337 data_dir = optarg;
4338 break;
4339 case QEMU_OPTION_bios:
4340 bios_name = optarg;
4341 break;
4342 case QEMU_OPTION_singlestep:
4343 singlestep = 1;
4344 break;
4345 case QEMU_OPTION_S:
4346#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004347 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004348#endif
4349 autostart = 0;
4350 break;
4351#ifndef _WIN32
4352 case QEMU_OPTION_k:
4353 keyboard_layout = optarg;
4354 break;
4355#endif
4356 case QEMU_OPTION_localtime:
4357 rtc_utc = 0;
4358 break;
4359 case QEMU_OPTION_vga:
4360 select_vgahw (optarg);
4361 break;
4362#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4363 case QEMU_OPTION_g:
4364 {
4365 const char *p;
4366 int w, h, depth;
4367 p = optarg;
4368 w = strtol(p, (char **)&p, 10);
4369 if (w <= 0) {
4370 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004371 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004372 }
4373 if (*p != 'x')
4374 goto graphic_error;
4375 p++;
4376 h = strtol(p, (char **)&p, 10);
4377 if (h <= 0)
4378 goto graphic_error;
4379 if (*p == 'x') {
4380 p++;
4381 depth = strtol(p, (char **)&p, 10);
4382 if (depth != 8 && depth != 15 && depth != 16 &&
4383 depth != 24 && depth != 32)
4384 goto graphic_error;
4385 } else if (*p == '\0') {
4386 depth = graphic_depth;
4387 } else {
4388 goto graphic_error;
4389 }
4390
4391 graphic_width = w;
4392 graphic_height = h;
4393 graphic_depth = depth;
4394 }
4395 break;
4396#endif
4397 case QEMU_OPTION_echr:
4398 {
4399 char *r;
4400 term_escape_char = strtol(optarg, &r, 0);
4401 if (r == optarg)
4402 printf("Bad argument to echr\n");
4403 break;
4404 }
4405 case QEMU_OPTION_monitor:
4406 monitor_device = optarg;
4407 break;
4408 case QEMU_OPTION_serial:
4409 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004410 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004411 }
4412 serial_devices[serial_device_index] = optarg;
4413 serial_device_index++;
4414 break;
4415 case QEMU_OPTION_watchdog:
4416 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004417 if (i > 0) {
4418 if (i == 1) {
4419 PANIC("Invalid watchdog parameter: %s",
4420 optarg);
4421 } else {
4422 QEMU_EXIT(0);
4423 }
4424 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004425 break;
4426 case QEMU_OPTION_watchdog_action:
4427 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004428 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004429 }
4430 break;
4431 case QEMU_OPTION_virtiocon:
4432 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004433 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004434 }
4435 virtio_consoles[virtio_console_index] = optarg;
4436 virtio_console_index++;
4437 break;
4438 case QEMU_OPTION_parallel:
4439 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004440 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004441 }
4442 parallel_devices[parallel_device_index] = optarg;
4443 parallel_device_index++;
4444 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004445 case QEMU_OPTION_loadvm:
4446 loadvm = optarg;
4447 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004448 case QEMU_OPTION_savevm_on_exit:
4449 savevm_on_exit = optarg;
4450 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004451 case QEMU_OPTION_full_screen:
4452 full_screen = 1;
4453 break;
4454#ifdef CONFIG_SDL
4455 case QEMU_OPTION_no_frame:
4456 no_frame = 1;
4457 break;
4458 case QEMU_OPTION_alt_grab:
4459 alt_grab = 1;
4460 break;
4461 case QEMU_OPTION_no_quit:
4462 no_quit = 1;
4463 break;
4464 case QEMU_OPTION_sdl:
4465 display_type = DT_SDL;
4466 break;
4467#endif
4468 case QEMU_OPTION_pidfile:
4469 pid_file = optarg;
4470 break;
4471#ifdef TARGET_I386
4472 case QEMU_OPTION_win2k_hack:
4473 win2k_install_hack = 1;
4474 break;
4475 case QEMU_OPTION_rtc_td_hack:
4476 rtc_td_hack = 1;
4477 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004478#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004479 case QEMU_OPTION_acpitable:
4480 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004481 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004482 }
4483 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004484#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004485 case QEMU_OPTION_smbios:
4486 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004487 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004488 }
4489 break;
4490#endif
4491#ifdef CONFIG_KQEMU
4492 case QEMU_OPTION_no_kqemu:
4493 kqemu_allowed = 0;
4494 break;
4495 case QEMU_OPTION_kernel_kqemu:
4496 kqemu_allowed = 2;
4497 break;
4498#endif
4499#ifdef CONFIG_KVM
4500 case QEMU_OPTION_enable_kvm:
4501 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004502 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02004503 case QEMU_OPTION_disable_kvm:
4504 kvm_allowed = 0;
4505 break;
4506#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004507 case QEMU_OPTION_usb:
4508 usb_enabled = 1;
4509 break;
4510 case QEMU_OPTION_usbdevice:
4511 usb_enabled = 1;
4512 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004513 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004514 }
4515 usb_devices[usb_devices_index] = optarg;
4516 usb_devices_index++;
4517 break;
4518 case QEMU_OPTION_smp:
4519 smp_cpus = atoi(optarg);
4520 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004521 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004522 }
4523 break;
4524 case QEMU_OPTION_vnc:
4525 display_type = DT_VNC;
4526 vnc_display = optarg;
4527 break;
4528#ifdef TARGET_I386
4529 case QEMU_OPTION_no_acpi:
4530 acpi_enabled = 0;
4531 break;
4532 case QEMU_OPTION_no_hpet:
4533 no_hpet = 1;
4534 break;
4535 case QEMU_OPTION_no_virtio_balloon:
4536 no_virtio_balloon = 1;
4537 break;
4538#endif
4539 case QEMU_OPTION_no_reboot:
4540 no_reboot = 1;
4541 break;
4542 case QEMU_OPTION_no_shutdown:
4543 no_shutdown = 1;
4544 break;
4545 case QEMU_OPTION_show_cursor:
4546 cursor_hide = 0;
4547 break;
4548 case QEMU_OPTION_uuid:
4549 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004550 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004551 }
4552 break;
4553#ifndef _WIN32
4554 case QEMU_OPTION_daemonize:
4555 daemonize = 1;
4556 break;
4557#endif
4558 case QEMU_OPTION_option_rom:
4559 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004560 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004561 }
4562 option_rom[nb_option_roms] = optarg;
4563 nb_option_roms++;
4564 break;
4565#if defined(TARGET_ARM) || defined(TARGET_M68K)
4566 case QEMU_OPTION_semihosting:
4567 semihosting_enabled = 1;
4568 break;
4569#endif
4570 case QEMU_OPTION_name:
4571 qemu_name = optarg;
4572 break;
4573#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4574 case QEMU_OPTION_prom_env:
4575 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004576 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004577 }
4578 prom_envs[nb_prom_envs] = optarg;
4579 nb_prom_envs++;
4580 break;
4581#endif
4582#ifdef TARGET_ARM
4583 case QEMU_OPTION_old_param:
4584 old_param = 1;
4585 break;
4586#endif
4587 case QEMU_OPTION_clock:
4588 configure_alarms(optarg);
4589 break;
4590 case QEMU_OPTION_startdate:
4591 {
4592 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004593 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004594 if (!strcmp(optarg, "now")) {
4595 rtc_date_offset = -1;
4596 } else {
4597 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4598 &tm.tm_year,
4599 &tm.tm_mon,
4600 &tm.tm_mday,
4601 &tm.tm_hour,
4602 &tm.tm_min,
4603 &tm.tm_sec) == 6) {
4604 /* OK */
4605 } else if (sscanf(optarg, "%d-%d-%d",
4606 &tm.tm_year,
4607 &tm.tm_mon,
4608 &tm.tm_mday) == 3) {
4609 tm.tm_hour = 0;
4610 tm.tm_min = 0;
4611 tm.tm_sec = 0;
4612 } else {
4613 goto date_fail;
4614 }
4615 tm.tm_year -= 1900;
4616 tm.tm_mon--;
4617 rtc_start_date = mktimegm(&tm);
4618 if (rtc_start_date == -1) {
4619 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004620 PANIC("Invalid date format. Valid format are:\n"
4621 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004622 }
4623 rtc_date_offset = time(NULL) - rtc_start_date;
4624 }
4625 }
4626 break;
rich cannings7339b552011-02-16 13:43:44 -08004627
4628 /* -------------------------------------------------------*/
4629 /* User mode network stack restrictions */
4630 case QEMU_OPTION_drop_udp:
4631 slirp_drop_udp();
4632 break;
4633 case QEMU_OPTION_drop_tcp:
4634 slirp_drop_tcp();
4635 break;
4636 case QEMU_OPTION_allow_tcp:
4637 slirp_allow(optarg, IPPROTO_TCP);
4638 break;
4639 case QEMU_OPTION_allow_udp:
4640 slirp_allow(optarg, IPPROTO_UDP);
4641 break;
4642 case QEMU_OPTION_drop_log:
4643 {
4644 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004645 drop_log_filename = optarg;
4646 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004647
4648 if (!drop_log_fd) {
4649 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4650 exit(1);
4651 }
4652
4653 slirp_drop_log_fd(drop_log_fd);
4654 }
4655 break;
4656
4657 case QEMU_OPTION_dns_log:
4658 {
4659 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004660 dns_log_filename = optarg;
4661 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004662
4663 if (dns_log_fd == NULL) {
4664 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4665 exit(1);
4666 }
4667
4668 slirp_dns_log_fd(dns_log_fd);
4669 }
4670 break;
4671
4672
4673 case QEMU_OPTION_max_dns_conns:
4674 {
4675 int max_dns_conns = 0;
4676 if (parse_int(optarg, &max_dns_conns)) {
4677 fprintf(stderr,
4678 "qemu: syntax: -max-dns-conns max_connections\n");
4679 exit(1);
4680 }
4681 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4682 fprintf(stderr,
4683 "Invalid arg for max dns connections: %s\n",
4684 optarg);
4685 exit(1);
4686 }
4687 slirp_set_max_dns_conns(max_dns_conns);
4688 }
4689 break;
4690
4691 case QEMU_OPTION_net_forward:
4692 net_slirp_forward(optarg);
4693 break;
4694 case QEMU_OPTION_net_forward_tcp2sink:
4695 {
4696 SockAddress saddr;
4697
4698 if (parse_host_port(&saddr, optarg)) {
4699 fprintf(stderr,
4700 "Invalid ip/port %s for "
4701 "-forward-dropped-tcp2sink. "
4702 "We expect 'sink_ip:sink_port'\n",
4703 optarg);
4704 exit(1);
4705 }
4706 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4707 saddr.u.inet.port);
4708 }
4709 break;
4710 /* -------------------------------------------------------*/
4711
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004712 case QEMU_OPTION_tb_size:
4713 tb_size = strtol(optarg, NULL, 0);
4714 if (tb_size < 0)
4715 tb_size = 0;
4716 break;
4717 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004718 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004719 break;
4720 case QEMU_OPTION_incoming:
4721 incoming = optarg;
4722 break;
4723#ifndef _WIN32
4724 case QEMU_OPTION_chroot:
4725 chroot_dir = optarg;
4726 break;
4727 case QEMU_OPTION_runas:
4728 run_as = optarg;
4729 break;
4730#endif
4731#ifdef CONFIG_XEN
4732 case QEMU_OPTION_xen_domid:
4733 xen_domid = atoi(optarg);
4734 break;
4735 case QEMU_OPTION_xen_create:
4736 xen_mode = XEN_CREATE;
4737 break;
4738 case QEMU_OPTION_xen_attach:
4739 xen_mode = XEN_ATTACH;
4740 break;
4741#endif
4742
4743
4744 case QEMU_OPTION_mic:
4745 audio_input_source = (char*)optarg;
4746 break;
4747#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004748 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004749 trace_filename = optarg;
4750 tracing = 1;
4751 break;
4752#if 0
4753 case QEMU_OPTION_trace_miss:
4754 trace_cache_miss = 1;
4755 break;
4756 case QEMU_OPTION_trace_addr:
4757 trace_all_addr = 1;
4758 break;
4759#endif
4760 case QEMU_OPTION_tracing:
4761 if (strcmp(optarg, "off") == 0)
4762 tracing = 0;
4763 else if (strcmp(optarg, "on") == 0 && trace_filename)
4764 tracing = 1;
4765 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004766 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004767 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004768 }
4769 break;
4770#if 0
4771 case QEMU_OPTION_dcache_load_miss:
4772 dcache_load_miss_penalty = atoi(optarg);
4773 break;
4774 case QEMU_OPTION_dcache_store_miss:
4775 dcache_store_miss_penalty = atoi(optarg);
4776 break;
4777#endif
4778#endif
4779#ifdef CONFIG_NAND
4780 case QEMU_OPTION_nand:
4781 nand_add_dev(optarg);
4782 break;
4783#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004784 case QEMU_OPTION_android_ports:
4785 android_op_ports = (char*)optarg;
4786 break;
4787
4788 case QEMU_OPTION_android_port:
4789 android_op_port = (char*)optarg;
4790 break;
4791
4792 case QEMU_OPTION_android_report_console:
4793 android_op_report_console = (char*)optarg;
4794 break;
4795
4796 case QEMU_OPTION_http_proxy:
4797 op_http_proxy = (char*)optarg;
4798 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004799
4800 case QEMU_OPTION_charmap:
4801 op_charmap_file = (char*)optarg;
4802 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004803
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004804 case QEMU_OPTION_android_hw:
4805 android_op_hwini = (char*)optarg;
4806 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004807
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004808 case QEMU_OPTION_dns_server:
4809 android_op_dns_server = (char*)optarg;
4810 break;
4811
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004812 case QEMU_OPTION_radio:
4813 android_op_radio = (char*)optarg;
4814 break;
4815
4816 case QEMU_OPTION_gps:
4817 android_op_gps = (char*)optarg;
4818 break;
4819
4820 case QEMU_OPTION_audio:
4821 android_op_audio = (char*)optarg;
4822 break;
4823
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004824 case QEMU_OPTION_cpu_delay:
4825 android_op_cpu_delay = (char*)optarg;
4826 break;
4827
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004828 case QEMU_OPTION_show_kernel:
4829 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4830 break;
4831
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004832#ifdef CONFIG_NAND_LIMITS
4833 case QEMU_OPTION_nand_limits:
4834 android_op_nand_limits = (char*)optarg;
4835 break;
4836#endif // CONFIG_NAND_LIMITS
4837
4838 case QEMU_OPTION_netspeed:
4839 android_op_netspeed = (char*)optarg;
4840 break;
4841
4842 case QEMU_OPTION_netdelay:
4843 android_op_netdelay = (char*)optarg;
4844 break;
4845
4846 case QEMU_OPTION_netfast:
4847 android_op_netfast = 1;
4848 break;
4849
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004850 case QEMU_OPTION_tcpdump:
4851 android_op_tcpdump = (char*)optarg;
4852 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004853
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004854 case QEMU_OPTION_boot_property:
4855 boot_property_parse_option((char*)optarg);
4856 break;
4857
4858 case QEMU_OPTION_lcd_density:
4859 android_op_lcd_density = (char*)optarg;
4860 break;
4861
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004862 case QEMU_OPTION_ui_port:
4863 android_op_ui_port = (char*)optarg;
4864 break;
4865
4866 case QEMU_OPTION_ui_settings:
4867 android_op_ui_settings = (char*)optarg;
4868 break;
4869
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004870 case QEMU_OPTION_audio_test_out:
4871 android_audio_test_start_out();
4872 break;
4873
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004874 case QEMU_OPTION_android_avdname:
4875 android_op_avd_name = (char*)optarg;
4876 break;
4877
4878 case QEMU_OPTION_timezone:
4879 if (timezone_set((char*)optarg)) {
4880 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4881 (char*)optarg);
4882 }
4883 break;
4884
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004885#ifdef CONFIG_MEMCHECK
4886 case QEMU_OPTION_android_memcheck:
4887 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004888 /* This will set ro.kernel.memcheck system property
4889 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004890 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004891 break;
4892#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004893
4894 case QEMU_OPTION_snapshot_no_time_update:
4895 android_snapshot_update_time = 0;
4896 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004897 }
4898 }
4899 }
4900
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004901 /* Initialize character map. */
4902 if (android_charmap_setup(op_charmap_file)) {
4903 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004904 PANIC(
4905 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004906 op_charmap_file);
4907 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004908 PANIC(
4909 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004910 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004911 }
4912
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004913 /* If no data_dir is specified then try to find it relative to the
4914 executable path. */
4915 if (!data_dir) {
4916 data_dir = find_datadir(argv[0]);
4917 }
4918 /* If all else fails use the install patch specified when building. */
4919 if (!data_dir) {
4920 data_dir = CONFIG_QEMU_SHAREDIR;
4921 }
4922
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004923 if (!android_op_hwini) {
4924 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004925 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004926 hw_ini = iniFile_newFromFile(android_op_hwini);
4927 if (hw_ini == NULL) {
4928 PANIC("Could not find %s file.", android_op_hwini);
4929 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004930
4931 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004932 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004933
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004934 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004935
4936 {
4937 int width = android_hw->hw_lcd_width;
4938 int height = android_hw->hw_lcd_height;
4939 int depth = android_hw->hw_lcd_depth;
4940
4941 /* A bit of sanity checking */
4942 if (width <= 0 || height <= 0 ||
4943 (depth != 16 && depth != 32) ||
4944 (((width|height) & 3) != 0) )
4945 {
4946 PANIC("Invalid display configuration (%d,%d,%d)",
4947 width, height, depth);
4948 }
4949 android_display_width = width;
4950 android_display_height = height;
4951 android_display_bpp = depth;
4952 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004953
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004954#ifdef CONFIG_NAND_LIMITS
4955 /* Init nand stuff. */
4956 if (android_op_nand_limits) {
4957 parse_nand_limits(android_op_nand_limits);
4958 }
4959#endif // CONFIG_NAND_LIMITS
4960
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004961 /* Initialize AVD name from hardware configuration if needed */
4962 if (!android_op_avd_name) {
4963 if (android_hw->avd_name && *android_hw->avd_name) {
4964 android_op_avd_name = android_hw->avd_name;
4965 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
4966 }
4967 }
4968
David 'Digit' Turner40841b22011-03-01 14:04:00 +01004969 /* Initialize system partition image */
4970 {
4971 char tmp[PATH_MAX+32];
4972 const char* sysImage = android_hw->disk_systemPartition_path;
4973 const char* initImage = android_hw->disk_systemPartition_initPath;
4974 uint64_t sysBytes = android_hw->disk_systemPartition_size;
4975
4976 if (sysBytes == 0) {
4977 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
4978 }
4979
4980 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
4981
4982 if (sysImage && *sysImage) {
4983 if (filelock_create(sysImage) == NULL) {
4984 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
4985 /* If there is no file= parameters, nand_add_dev will create
4986 * a temporary file to back the partition image. */
4987 } else {
4988 pstrcat(tmp,sizeof(tmp),",file=");
4989 pstrcat(tmp,sizeof(tmp),sysImage);
4990 }
4991 }
4992 if (initImage && *initImage) {
4993 if (!path_exists(initImage)) {
4994 PANIC("Invalid initial system image path: %s", initImage);
4995 }
4996 pstrcat(tmp,sizeof(tmp),",initfile=");
4997 pstrcat(tmp,sizeof(tmp),initImage);
4998 } else {
4999 PANIC("Missing initial system image path!");
5000 }
5001 nand_add_dev(tmp);
5002 }
5003
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005004 /* Initialize data partition image */
5005 {
5006 char tmp[PATH_MAX+32];
5007 const char* dataImage = android_hw->disk_dataPartition_path;
5008 const char* initImage = android_hw->disk_dataPartition_initPath;
5009 uint64_t dataBytes = android_hw->disk_dataPartition_size;
5010
5011 if (dataBytes == 0) {
5012 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
5013 }
5014
5015 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
5016
5017 if (dataImage && *dataImage) {
5018 if (filelock_create(dataImage) == NULL) {
5019 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
5020 /* Note: if there is no file= parameters, nand_add_dev() will
5021 * create a temporary file to back the partition image. */
5022 } else {
5023 /* Create the file if needed */
5024 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005025 if (path_empty_file(dataImage) < 0) {
5026 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
5027 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005028 }
5029 pstrcat(tmp, sizeof(tmp), ",file=");
5030 pstrcat(tmp, sizeof(tmp), dataImage);
5031 }
5032 }
5033 if (initImage && *initImage) {
5034 pstrcat(tmp, sizeof(tmp), ",initfile=");
5035 pstrcat(tmp, sizeof(tmp), initImage);
5036 }
5037 nand_add_dev(tmp);
5038 }
5039
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005040 /* Init SD-Card stuff. For Android, it is always hda */
5041 /* If the -hda option was used, ignore the Android-provided one */
5042 if (hda_opts == NULL) {
5043 const char* sdPath = android_hw->hw_sdCard_path;
5044 if (sdPath && *sdPath) {
5045 if (!path_exists(sdPath)) {
5046 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
5047 } else if (filelock_create(sdPath) == NULL) {
5048 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
5049 } else {
5050 /* Successful locking */
5051 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005052 /* Set this property of any operation involving the SD Card
5053 * will be x100 slower, due to the corresponding file being
5054 * mounted as O_DIRECT. Note that this is only 'unsafe' in
5055 * the context of an emulator crash. The data is already
5056 * synced properly when the emulator exits (either normally or through ^C).
5057 */
5058 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005059 }
5060 }
5061 }
5062
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005063 if (hdb_opts == NULL) {
5064 const char* spath = android_hw->disk_snapStorage_path;
5065 if (spath && *spath) {
5066 if (!path_exists(spath)) {
5067 PANIC("Snapshot storage file does not exist: %s", spath);
5068 }
5069 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005070 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005071 }
5072 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005073 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02005074 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005075 }
5076 }
5077
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005078 /* Set the VM's max heap size, passed as a boot property */
5079 if (android_hw->vm_heapSize > 0) {
5080 char tmp[64];
5081 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
5082 boot_property_add("dalvik.vm.heapsize",tmp);
5083 }
5084
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005085 /* Initialize net speed and delays stuff. */
5086 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005087 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005088 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005089 }
5090
5091 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005092 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005093 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005094 }
5095
5096 if (android_op_netfast) {
5097 qemu_net_download_speed = 0;
5098 qemu_net_upload_speed = 0;
5099 qemu_net_min_latency = 0;
5100 qemu_net_max_latency = 0;
5101 }
5102
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005103 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005104 if (android_hw->hw_lcd_density) {
5105 long density = android_hw->hw_lcd_density;
5106 if (density <= 0) {
5107 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005108 }
5109 hwLcd_setBootProperty(density);
5110 }
5111
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07005112 /* Initialize TCP dump */
5113 if (android_op_tcpdump) {
5114 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
5115 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
5116 }
5117 }
5118
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005119 /* Initialize modem */
5120 if (android_op_radio) {
5121 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
5122 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005123 PANIC("unsupported character device specification: %s\n"
5124 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005125 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005126 }
5127 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
5128 } else if (android_hw->hw_gsmModem != 0 ) {
5129 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005130 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005131 }
5132 }
5133
5134 /* Initialize GPS */
5135 if (android_op_gps) {
5136 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
5137 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005138 PANIC("unsupported character device specification: %s\n"
5139 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005140 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005141 }
5142 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
5143 } else if (android_hw->hw_gps != 0) {
5144 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005145 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005146 }
5147 }
5148
5149 /* Initialize audio. */
5150 if (android_op_audio) {
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005151 char temp[128];
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005152 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005153 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005154 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005155 }
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005156 snprintf(temp, sizeof temp, "QEMU_AUDIO_DRV=%s", android_op_audio);
5157 putenv(temp);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005158 }
5159
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005160 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02005161 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005162
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005163 if (android_op_cpu_delay) {
5164 char* end;
5165 long delay = strtol(android_op_cpu_delay, &end, 0);
5166 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005167 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005168 }
5169 if (delay > 0)
5170 delay = (1000-delay);
5171
5172 qemu_cpu_delay = (int) delay;
5173 }
5174
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005175 if (android_op_dns_server) {
5176 char* x = strchr(android_op_dns_server, ',');
5177 dns_count = 0;
5178 if (x == NULL)
5179 {
5180 if ( add_dns_server( android_op_dns_server ) == 0 )
5181 dns_count = 1;
5182 }
5183 else
5184 {
5185 x = android_op_dns_server;
5186 while (*x) {
5187 char* y = strchr(x, ',');
5188
5189 if (y != NULL) {
5190 *y = 0;
5191 y++;
5192 } else {
5193 y = x + strlen(x);
5194 }
5195
5196 if (y > x && add_dns_server( x ) == 0) {
5197 dns_count += 1;
5198 }
5199 x = y;
5200 }
5201 }
5202 if (dns_count == 0)
5203 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5204 }
5205
5206 if (dns_count == 0)
5207 dns_count = slirp_get_system_dns_servers();
5208 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005209 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005210 }
5211
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005212#ifdef CONFIG_MEMCHECK
5213 if (android_op_memcheck) {
5214 memcheck_init(android_op_memcheck);
5215 }
5216#endif // CONFIG_MEMCHECK
5217
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005218 /* Initialize cache partition, if any */
5219 if (android_hw->disk_cachePartition != 0) {
5220 char tmp[PATH_MAX+32];
5221 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005222 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005223
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005224 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5225
5226 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5227 if (filelock_create(partPath) == NULL) {
5228 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5229 /* Note: if there is no file= parameters, nand_add_dev() will
5230 * create a temporary file to back the partition image. */
5231 } else {
5232 /* Create the file if needed */
5233 if (!path_exists(partPath)) {
5234 if (path_empty_file(partPath) < 0) {
5235 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5236 }
5237 }
5238 pstrcat(tmp, sizeof(tmp), ",file=");
5239 pstrcat(tmp, sizeof(tmp), partPath);
5240 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005241 }
5242 nand_add_dev(tmp);
5243 }
5244
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005245 /* We always force qemu=1 when running inside QEMU */
5246 stralloc_add_str(kernel_params, " qemu=1");
5247
5248 /* We always initialize the first serial port for the android-kmsg
5249 * character device (used to send kernel messages) */
5250 serial_hds_add_at(0, "android-kmsg");
5251 stralloc_add_str(kernel_params, " console=ttyS0");
5252
5253 /* We always initialize the second serial port for the android-qemud
5254 * character device as well */
5255 serial_hds_add_at(1, "android-qemud");
5256 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5257
David 'Digit' Turner36597752011-05-20 01:18:01 +02005258#if defined(CONFIG_KVM)
5259 if (kvm_allowed < 0) {
5260 kvm_allowed = kvm_check_allowed();
5261 }
5262#endif
5263
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005264#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5265 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005266 PANIC(
5267 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005268 }
5269#endif
5270
5271 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5272 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005273 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5274 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005275 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005276 }
5277
5278 if (display_type == DT_NOGRAPHIC) {
5279 if (serial_device_index == 0)
5280 serial_devices[0] = "stdio";
5281 if (parallel_device_index == 0)
5282 parallel_devices[0] = "null";
5283 if (strncmp(monitor_device, "vc", 2) == 0)
5284 monitor_device = "stdio";
5285 }
5286
5287#ifndef _WIN32
5288 if (daemonize) {
5289 pid_t pid;
5290
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005291 if (pipe(fds) == -1) {
5292 PANIC("Unable to aquire pidfile");
5293 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005294
5295 pid = fork();
5296 if (pid > 0) {
5297 uint8_t status;
5298 ssize_t len;
5299
5300 close(fds[1]);
5301
5302 again:
5303 len = read(fds[0], &status, 1);
5304 if (len == -1 && (errno == EINTR))
5305 goto again;
5306
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005307 if (len != 1) {
5308 PANIC("Error when aquiring pidfile");
5309 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005310 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005311 PANIC("Could not acquire pidfile");
5312 } else {
5313 QEMU_EXIT(0);
5314 }
5315 } else if (pid < 0) {
5316 PANIC("Unable to daemonize");
5317 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005318
5319 setsid();
5320
5321 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005322 if (pid > 0) {
5323 QEMU_EXIT(0);
5324 } else if (pid < 0) {
5325 PANIC("Could not acquire pid file");
5326 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005327
5328 umask(027);
5329
5330 signal(SIGTSTP, SIG_IGN);
5331 signal(SIGTTOU, SIG_IGN);
5332 signal(SIGTTIN, SIG_IGN);
5333 }
5334
5335 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5336 if (daemonize) {
5337 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005338 int ret;
5339 do {
5340 ret = write(fds[1], &status, 1);
5341 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005342 PANIC("Could not acquire pid file");
5343 } else {
5344 PANIC("Could not acquire pid file");
5345 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005346 }
5347#endif
5348
5349#ifdef CONFIG_KQEMU
5350 if (smp_cpus > 1)
5351 kqemu_allowed = 0;
5352#endif
5353 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005354 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005355 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005356
5357 if (kernel_filename == NULL) {
5358 kernel_filename = android_hw->kernel_path;
5359 }
5360 if (initrd_filename == NULL) {
5361 initrd_filename = android_hw->disk_ramdisk_path;
5362 }
5363
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005364 linux_boot = (kernel_filename != NULL);
5365 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5366
5367 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005368 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005369 }
5370
5371 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005372 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005373 }
5374
5375 /* boot to floppy or the default cd if no hard disk defined yet */
5376 if (!boot_devices[0]) {
5377 boot_devices = "cad";
5378 }
5379 setvbuf(stdout, NULL, _IOLBF, 0);
5380
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005381 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005382 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005383 }
David Turner6a9ef172010-09-09 22:54:36 +02005384 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005385
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005386 /* init network clients */
5387 if (nb_net_clients == 0) {
5388 /* if no clients, we use a default config */
5389 net_clients[nb_net_clients++] = "nic";
5390#ifdef CONFIG_SLIRP
5391 net_clients[nb_net_clients++] = "user";
5392#endif
5393 }
5394
5395 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005396 if (net_client_parse(net_clients[i]) < 0) {
5397 PANIC("Unable to parse net clients");
5398 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005399 }
5400 net_client_check();
5401
5402#ifdef TARGET_I386
5403 /* XXX: this should be moved in the PC machine instantiation code */
5404 if (net_boot != 0) {
5405 int netroms = 0;
5406 for (i = 0; i < nb_nics && i < 4; i++) {
5407 const char *model = nd_table[i].model;
5408 char buf[1024];
5409 char *filename;
5410 if (net_boot & (1 << i)) {
5411 if (model == NULL)
5412 model = "ne2k_pci";
5413 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5414 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5415 if (filename && get_image_size(filename) > 0) {
5416 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005417 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005418 }
5419 option_rom[nb_option_roms] = qemu_strdup(buf);
5420 nb_option_roms++;
5421 netroms++;
5422 }
5423 if (filename) {
5424 qemu_free(filename);
5425 }
5426 }
5427 }
5428 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005429 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005430 }
5431 }
5432#endif
5433
5434 /* init the bluetooth world */
5435 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005436 if (bt_parse(bt_opts[i])) {
5437 PANIC("Unable to parse bluetooth options");
5438 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005439
5440 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005441 if (ram_size == 0) {
5442 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5443 if (ram_size == 0) {
5444 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5445 }
5446 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005447
5448#ifdef CONFIG_KQEMU
5449 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5450 guest ram allocation. It needs to go away. */
5451 if (kqemu_allowed) {
5452 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5453 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5454 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005455 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005456 }
5457 }
5458#endif
5459
rich canningsd952f282011-03-01 15:40:09 -08005460#ifndef _WIN32
5461 init_qemu_clear_logs_sig();
5462#endif
5463
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005464 /* init the dynamic translator */
5465 cpu_exec_init_all(tb_size * 1024 * 1024);
5466
5467 bdrv_init();
5468
5469 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005470#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005471 if (nb_drives_opt < MAX_DRIVES)
5472 drive_add(NULL, CDROM_ALIAS);
5473
5474 /* we always create at least one floppy */
5475
5476 if (nb_drives_opt < MAX_DRIVES)
5477 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005478 /* we always create one sd slot, even if no card is in it */
5479
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005480 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005481 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005482 }
5483#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005484
5485 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005486 if (snapshot)
5487 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5488 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5489 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005490
David Turner6a9ef172010-09-09 22:54:36 +02005491 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005492 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5493
5494#ifndef _WIN32
5495 /* must be after terminal init, SDL library changes signal handlers */
5496 sighandler_setup();
5497#endif
5498
5499 /* Maintain compatibility with multiple stdio monitors */
5500 if (!strcmp(monitor_device,"stdio")) {
5501 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5502 const char *devname = serial_devices[i];
5503 if (devname && !strcmp(devname,"mon:stdio")) {
5504 monitor_device = NULL;
5505 break;
5506 } else if (devname && !strcmp(devname,"stdio")) {
5507 monitor_device = NULL;
5508 serial_devices[i] = "mon:stdio";
5509 break;
5510 }
5511 }
5512 }
5513
5514 if (nb_numa_nodes > 0) {
5515 int i;
5516
5517 if (nb_numa_nodes > smp_cpus) {
5518 nb_numa_nodes = smp_cpus;
5519 }
5520
5521 /* If no memory size if given for any node, assume the default case
5522 * and distribute the available memory equally across all nodes
5523 */
5524 for (i = 0; i < nb_numa_nodes; i++) {
5525 if (node_mem[i] != 0)
5526 break;
5527 }
5528 if (i == nb_numa_nodes) {
5529 uint64_t usedmem = 0;
5530
5531 /* On Linux, the each node's border has to be 8MB aligned,
5532 * the final node gets the rest.
5533 */
5534 for (i = 0; i < nb_numa_nodes - 1; i++) {
5535 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5536 usedmem += node_mem[i];
5537 }
5538 node_mem[i] = ram_size - usedmem;
5539 }
5540
5541 for (i = 0; i < nb_numa_nodes; i++) {
5542 if (node_cpumask[i] != 0)
5543 break;
5544 }
5545 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5546 * must cope with this anyway, because there are BIOSes out there in
5547 * real machines which also use this scheme.
5548 */
5549 if (i == nb_numa_nodes) {
5550 for (i = 0; i < smp_cpus; i++) {
5551 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5552 }
5553 }
5554 }
5555
5556 if (kvm_enabled()) {
5557 int ret;
5558
5559 ret = kvm_init(smp_cpus);
5560 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005561 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005562 }
5563 }
5564
5565 if (monitor_device) {
5566 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5567 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005568 PANIC("qemu: could not open monitor device '%s'",
5569 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005570 }
5571 }
5572
5573 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005574 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005575 }
5576
5577 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5578 const char *devname = parallel_devices[i];
5579 if (devname && strcmp(devname, "none")) {
5580 char label[32];
5581 snprintf(label, sizeof(label), "parallel%d", i);
5582 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5583 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005584 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005585 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005586 }
5587 }
5588 }
5589
5590 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5591 const char *devname = virtio_consoles[i];
5592 if (devname && strcmp(devname, "none")) {
5593 char label[32];
5594 snprintf(label, sizeof(label), "virtcon%d", i);
5595 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5596 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005597 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005598 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005599 }
5600 }
5601 }
5602
5603 module_call_init(MODULE_INIT_DEVICE);
5604
5605
5606#ifdef CONFIG_TRACE
5607 if (trace_filename) {
5608 trace_init(trace_filename);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005609 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5610 }
5611#endif
5612
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005613 /* Check the CPU Architecture value */
5614#if defined(TARGET_ARM)
5615 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5616 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5617 android_hw->hw_cpu_arch);
5618 exit(1);
5619 }
5620#elif defined(TARGET_X86)
5621 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5622 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5623 android_hw->hw_cpu_arch);
5624 exit(1);
5625 }
5626#endif
5627
5628 /* Grab CPU model if provided in hardware.ini */
5629 if ( !cpu_model
5630 && android_hw->hw_cpu_model
5631 && android_hw->hw_cpu_model[0] != '\0')
5632 {
5633 cpu_model = android_hw->hw_cpu_model;
5634 }
5635
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005636 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005637 * collected during initialization.
5638 *
5639 * The order is the following:
5640 * - parameters from the hw configuration (kernel.parameters)
5641 * - additionnal parameters from options (e.g. -memcheck)
5642 * - the -append parameters.
5643 */
5644 {
5645 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005646
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005647 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005648 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005649 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5650 }
5651
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005652 /* If not empty, kernel_config always contains a leading space */
5653 stralloc_append(kernel_params, kernel_config);
5654
5655 if (*kernel_cmdline) {
5656 stralloc_add_c(kernel_params, ' ');
5657 stralloc_add_str(kernel_params, kernel_cmdline);
5658 }
5659
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005660 /* Remove any leading/trailing spaces */
5661 stralloc_strip(kernel_params);
5662
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005663 kernel_parameters = stralloc_cstr(kernel_params);
5664 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5665
5666 machine->init(ram_size,
5667 boot_devices,
5668 kernel_filename,
5669 kernel_parameters,
5670 initrd_filename,
5671 cpu_model);
5672
5673 stralloc_reset(kernel_params);
5674 stralloc_reset(kernel_config);
5675 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005676
5677
5678 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5679 for (i = 0; i < nb_numa_nodes; i++) {
5680 if (node_cpumask[i] & (1 << env->cpu_index)) {
5681 env->numa_node = i;
5682 }
5683 }
5684 }
5685
5686 current_machine = machine;
5687
5688 /* Set KVM's vcpu state to qemu's initial CPUState. */
5689 if (kvm_enabled()) {
5690 int ret;
5691
5692 ret = kvm_sync_vcpus();
5693 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005694 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005695 }
5696 }
5697
5698 /* init USB devices */
5699 if (usb_enabled) {
5700 for(i = 0; i < usb_devices_index; i++) {
5701 if (usb_device_add(usb_devices[i], 0) < 0) {
5702 fprintf(stderr, "Warning: could not add USB device %s\n",
5703 usb_devices[i]);
5704 }
5705 }
5706 }
5707
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005708 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005709 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005710
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005711 /* Initialize display from the command line parameters. */
5712 android_display_reset(ds,
5713 android_display_width,
5714 android_display_height,
5715 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005716
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005717 if (display_type == DT_DEFAULT) {
5718#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5719 display_type = DT_SDL;
5720#else
5721 display_type = DT_VNC;
5722 vnc_display = "localhost:0,to=99";
5723 show_vnc_port = 1;
5724#endif
5725 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005726
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005727
5728 switch (display_type) {
5729 case DT_NOGRAPHIC:
5730 break;
5731#if defined(CONFIG_CURSES)
5732 case DT_CURSES:
5733 curses_display_init(ds, full_screen);
5734 break;
5735#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005736#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005737 case DT_SDL:
5738 sdl_display_init(ds, full_screen, no_frame);
5739 break;
5740#elif defined(CONFIG_COCOA)
5741 case DT_SDL:
5742 cocoa_display_init(ds, full_screen);
5743 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005744#elif defined(CONFIG_STANDALONE_CORE)
5745 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005746 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005747 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005748#endif
5749 case DT_VNC:
5750 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005751 if (vnc_display_open(ds, vnc_display) < 0) {
5752 PANIC("Unable to initialize VNC display");
5753 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005754
5755 if (show_vnc_port) {
5756 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5757 }
5758 break;
5759 default:
5760 break;
5761 }
5762 dpy_resize(ds);
5763
5764 dcl = ds->listeners;
5765 while (dcl != NULL) {
5766 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005767 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
5768 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005769 }
5770 dcl = dcl->next;
5771 }
5772
5773 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005774 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
5775 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005776 }
5777
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005778 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005779 qemu_chr_initial_reset();
5780
5781 if (monitor_device && monitor_hd)
5782 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5783
5784 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5785 const char *devname = serial_devices[i];
5786 if (devname && strcmp(devname, "none")) {
5787 if (strstart(devname, "vc", 0))
5788 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5789 }
5790 }
5791
5792 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5793 const char *devname = parallel_devices[i];
5794 if (devname && strcmp(devname, "none")) {
5795 if (strstart(devname, "vc", 0))
5796 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5797 }
5798 }
5799
5800 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5801 const char *devname = virtio_consoles[i];
5802 if (virtcon_hds[i] && devname) {
5803 if (strstart(devname, "vc", 0))
5804 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5805 }
5806 }
5807
5808 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005809 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005810 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005811 }
5812
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005813 /* call android-specific setup function */
5814 android_emulation_setup();
5815
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005816#if !defined(CONFIG_STANDALONE_CORE)
5817 // For the standalone emulator (UI+core in one executable) we need to
5818 // set the window title here.
5819 android_emulator_set_base_port(android_base_port);
5820#endif
5821
Ot ten Thije871da2a2010-09-20 10:29:22 +01005822 if (loadvm)
5823 do_loadvm(cur_mon, loadvm);
5824
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005825 if (incoming) {
5826 autostart = 0; /* fixme how to deal with -daemonize */
5827 qemu_start_incoming_migration(incoming);
5828 }
5829
5830 if (autostart)
5831 vm_start();
5832
5833#ifndef _WIN32
5834 if (daemonize) {
5835 uint8_t status = 0;
5836 ssize_t len;
5837
5838 again1:
5839 len = write(fds[1], &status, 1);
5840 if (len == -1 && (errno == EINTR))
5841 goto again1;
5842
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005843 if (len != 1) {
5844 PANIC("Unable to daemonize");
5845 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005846
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005847 if (chdir("/")) {
5848 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005849 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005850 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005851 TFR(fd = open("/dev/null", O_RDWR));
5852 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005853 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005854 }
5855
5856 if (run_as) {
5857 pwd = getpwnam(run_as);
5858 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005859 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005860 }
5861 }
5862
5863 if (chroot_dir) {
5864 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005865 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005866 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005867 if (chdir("/")) {
5868 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005869 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005870 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005871 }
5872
5873 if (run_as) {
5874 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005875 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005876 }
5877 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005878 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005879 }
5880 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005881 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005882 }
5883 }
5884
5885 if (daemonize) {
5886 dup2(fd, 0);
5887 dup2(fd, 1);
5888 dup2(fd, 2);
5889
5890 close(fd);
5891 }
5892#endif
5893
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005894#ifdef CONFIG_ANDROID
5895 // This will notify the UI that the core is successfuly initialized
5896 android_core_init_completed();
5897#endif // CONFIG_ANDROID
5898
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005899 main_loop();
5900 quit_timers();
5901 net_cleanup();
5902 android_emulation_teardown();
5903 return 0;
5904}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005905
5906void
5907android_emulation_teardown(void)
5908{
5909 android_charmap_done();
5910}