blob: 08515cbd631c5c6d49b883fc49eef6b178161712 [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"
229#include "dcache.h"
230#endif
231
232#include "qemu_socket.h"
233
234#if defined(CONFIG_SLIRP)
235#include "libslirp.h"
236#endif
237
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700238
239
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700240#define DEFAULT_RAM_SIZE 128
241
242/* Max number of USB devices that can be specified on the commandline. */
243#define MAX_USB_CMDLINE 8
244
245/* Max number of bluetooth switches on the commandline. */
246#define MAX_BT_CMDLINE 10
247
248/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700249
250static const char *data_dir;
251const char *bios_name = NULL;
252static void *ioport_opaque[MAX_IOPORTS];
253static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
254static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100255#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700256/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
257 to store the VM snapshots */
258DriveInfo drives_table[MAX_DRIVES+1];
259int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100260#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700261enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700262DisplayType display_type = DT_DEFAULT;
263const char* keyboard_layout = NULL;
264int64_t ticks_per_sec;
265ram_addr_t ram_size;
266int nb_nics;
267NICInfo nd_table[MAX_NICS];
268int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100269int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700270static int rtc_utc = 1;
271static int rtc_date_offset = -1; /* -1 means no change */
272int cirrus_vga_enabled = 1;
273int std_vga_enabled = 0;
274int vmsvga_enabled = 0;
275int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700276QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700277#ifdef TARGET_SPARC
278int graphic_width = 1024;
279int graphic_height = 768;
280int graphic_depth = 8;
281#else
282int graphic_width = 800;
283int graphic_height = 600;
284int graphic_depth = 15;
285#endif
286static int full_screen = 0;
287#ifdef CONFIG_SDL
288static int no_frame = 0;
289#endif
290int no_quit = 0;
291CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100292int serial_hds_count;
293
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700294CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
295CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
296#ifdef TARGET_I386
297int win2k_install_hack = 0;
298int rtc_td_hack = 0;
299#endif
300int usb_enabled = 0;
301int singlestep = 0;
302int smp_cpus = 1;
303const char *vnc_display;
304int acpi_enabled = 1;
305int no_hpet = 0;
306int no_virtio_balloon = 0;
307int fd_bootchk = 1;
308int no_reboot = 0;
309int no_shutdown = 0;
310int cursor_hide = 1;
311int graphic_rotate = 0;
312#ifndef _WIN32
313int daemonize = 0;
314#endif
315WatchdogTimerModel *watchdog = NULL;
316int watchdog_action = WDT_RESET;
317const char *option_rom[MAX_OPTION_ROMS];
318int nb_option_roms;
319int semihosting_enabled = 0;
320#ifdef TARGET_ARM
321int old_param = 0;
322#endif
323const char *qemu_name;
324int alt_grab = 0;
325#if defined(TARGET_SPARC) || defined(TARGET_PPC)
326unsigned int nb_prom_envs = 0;
327const char *prom_envs[MAX_PROM_ENVS];
328#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100329#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700330int nb_drives_opt;
331struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100332#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700333int nb_numa_nodes;
334uint64_t node_mem[MAX_NODES];
335uint64_t node_cpumask[MAX_NODES];
336
337static CPUState *cur_cpu;
338static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700339static QEMUTimer *nographic_timer;
340
341uint8_t qemu_uuid[16];
342
343
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700344int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700345extern char* audio_input_source;
346
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700347extern char* android_op_ports;
348extern char* android_op_port;
349extern char* android_op_report_console;
350extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700351// Path to the file containing specific key character map.
352char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700353
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700354/* Path to hardware initialization file passed with -android-hw option. */
355char* android_op_hwini = NULL;
356
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700357/* Memory checker options. */
358char* android_op_memcheck = NULL;
359
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700360/* -dns-server option value. */
361char* android_op_dns_server = NULL;
362
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700363/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700364char* android_op_radio = NULL;
365
366/* -gps option value. */
367char* android_op_gps = NULL;
368
369/* -audio option value. */
370char* android_op_audio = NULL;
371
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700372/* -cpu-delay option value. */
373char* android_op_cpu_delay = NULL;
374
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700375#ifdef CONFIG_NAND_LIMITS
376/* -nand-limits option value. */
377char* android_op_nand_limits = NULL;
378#endif // CONFIG_NAND_LIMITS
379
380/* -netspeed option value. */
381char* android_op_netspeed = NULL;
382
383/* -netdelay option value. */
384char* android_op_netdelay = NULL;
385
386/* -netfast option value. */
387int android_op_netfast = 0;
388
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700389/* -tcpdump option value. */
390char* android_op_tcpdump = NULL;
391
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700392/* -lcd-density option value. */
393char* android_op_lcd_density = NULL;
394
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700395/* -ui-port option value. This port will be used to report the core
396 * initialization completion.
397 */
398char* android_op_ui_port = NULL;
399
400/* -ui-settings option value. This value will be passed to the UI when new UI
401 * process is attaching to the core.
402 */
403char* android_op_ui_settings = NULL;
404
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800405/* -android-avdname option value. */
406char* android_op_avd_name = "unknown";
407
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700408extern int android_display_width;
409extern int android_display_height;
410extern int android_display_bpp;
411
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700412extern void dprint( const char* format, ... );
413
rich canningsd952f282011-03-01 15:40:09 -0800414const char* dns_log_filename = NULL;
415const char* drop_log_filename = NULL;
416static int rotate_logs_requested = 0;
417
Tim Baverstock24204cc2010-11-25 11:37:43 +0000418const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000419
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700420#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
421
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700422/* Reports the core initialization failure to the error stdout and to the UI
423 * socket before exiting the application.
424 * Parameters that are passed to this macro are used to format the error
425 * mesage using sprintf routine.
426 */
427#ifdef CONFIG_ANDROID
428#define PANIC(...) android_core_init_failure(__VA_ARGS__)
429#else
430#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
431 exit(1); \
432 } while (0)
433#endif // CONFIG_ANDROID
434
435/* Exits the core during initialization. */
436#ifdef CONFIG_ANDROID
437#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
438#else
439#define QEMU_EXIT(exit_code) exit(exit_code)
440#endif // CONFIG_ANDROID
441
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700442/***********************************************************/
443/* x86 ISA bus support */
444
445target_phys_addr_t isa_mem_base = 0;
446PicState2 *isa_pic;
447
448static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
449static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
450
451static uint32_t ioport_read(int index, uint32_t address)
452{
453 static IOPortReadFunc *default_func[3] = {
454 default_ioport_readb,
455 default_ioport_readw,
456 default_ioport_readl
457 };
458 IOPortReadFunc *func = ioport_read_table[index][address];
459 if (!func)
460 func = default_func[index];
461 return func(ioport_opaque[address], address);
462}
463
464static void ioport_write(int index, uint32_t address, uint32_t data)
465{
466 static IOPortWriteFunc *default_func[3] = {
467 default_ioport_writeb,
468 default_ioport_writew,
469 default_ioport_writel
470 };
471 IOPortWriteFunc *func = ioport_write_table[index][address];
472 if (!func)
473 func = default_func[index];
474 func(ioport_opaque[address], address, data);
475}
476
477static uint32_t default_ioport_readb(void *opaque, uint32_t address)
478{
479#ifdef DEBUG_UNUSED_IOPORT
480 fprintf(stderr, "unused inb: port=0x%04x\n", address);
481#endif
482 return 0xff;
483}
484
485static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
486{
487#ifdef DEBUG_UNUSED_IOPORT
488 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
489#endif
490}
491
492/* default is to make two byte accesses */
493static uint32_t default_ioport_readw(void *opaque, uint32_t address)
494{
495 uint32_t data;
496 data = ioport_read(0, address);
497 address = (address + 1) & (MAX_IOPORTS - 1);
498 data |= ioport_read(0, address) << 8;
499 return data;
500}
501
502static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
503{
504 ioport_write(0, address, data & 0xff);
505 address = (address + 1) & (MAX_IOPORTS - 1);
506 ioport_write(0, address, (data >> 8) & 0xff);
507}
508
509static uint32_t default_ioport_readl(void *opaque, uint32_t address)
510{
511#ifdef DEBUG_UNUSED_IOPORT
512 fprintf(stderr, "unused inl: port=0x%04x\n", address);
513#endif
514 return 0xffffffff;
515}
516
517static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
518{
519#ifdef DEBUG_UNUSED_IOPORT
520 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
521#endif
522}
523
rich canningsd952f282011-03-01 15:40:09 -0800524/*
525 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
526 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
527 * between the tasks that do not require restarting Qemu.
528 */
529void rotate_qemu_logs_handler(int signum) {
530 rotate_logs_requested = 1;
531}
532
533/*
534 * Resets the rotate_log_requested_flag. Normally called after qemu
535 * logs has been rotated.
536 */
537void reset_rotate_qemu_logs_request(void) {
538 rotate_logs_requested = 0;
539}
540
541/*
542 * Clears the passed qemu log when the rotate_logs_requested
543 * is set. We need to clear the logs between the tasks that do not
544 * require restarting Qemu.
545 */
546FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
547 FILE* new_log_fd = NULL;
548 if (old_log_fd) {
549 if (fclose(old_log_fd) == -1) {
550 fprintf(stderr, "Cannot close old_log fd\n");
551 exit(errno);
552 }
553 }
554
555 if (!filename) {
556 fprintf(stderr, "The log filename to be rotated is not provided");
557 exit(-1);
558 }
559
560 new_log_fd = fopen(filename , "wb+");
561 if (new_log_fd == NULL) {
562 fprintf(stderr, "Cannot open the log file: %s for write.\n",
563 filename);
564 exit(1);
565 }
566
567 return new_log_fd;
568}
569
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700570/***********************************************************/
571void hw_error(const char *fmt, ...)
572{
573 va_list ap;
574 CPUState *env;
575
576 va_start(ap, fmt);
577 fprintf(stderr, "qemu: hardware error: ");
578 vfprintf(stderr, fmt, ap);
579 fprintf(stderr, "\n");
580 for(env = first_cpu; env != NULL; env = env->next_cpu) {
581 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
582#ifdef TARGET_I386
583 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
584#else
585 cpu_dump_state(env, stderr, fprintf, 0);
586#endif
587 }
588 va_end(ap);
589 abort();
590}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200591
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700592/***************/
593/* ballooning */
594
595static QEMUBalloonEvent *qemu_balloon_event;
596void *qemu_balloon_event_opaque;
597
598void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
599{
600 qemu_balloon_event = func;
601 qemu_balloon_event_opaque = opaque;
602}
603
604void qemu_balloon(ram_addr_t target)
605{
606 if (qemu_balloon_event)
607 qemu_balloon_event(qemu_balloon_event_opaque, target);
608}
609
610ram_addr_t qemu_balloon_status(void)
611{
612 if (qemu_balloon_event)
613 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
614 return 0;
615}
616
617/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700618/* host time/date access */
619void qemu_get_timedate(struct tm *tm, int offset)
620{
621 time_t ti;
622 struct tm *ret;
623
624 time(&ti);
625 ti += offset;
626 if (rtc_date_offset == -1) {
627 if (rtc_utc)
628 ret = gmtime(&ti);
629 else
630 ret = localtime(&ti);
631 } else {
632 ti -= rtc_date_offset;
633 ret = gmtime(&ti);
634 }
635
636 memcpy(tm, ret, sizeof(struct tm));
637}
638
639int qemu_timedate_diff(struct tm *tm)
640{
641 time_t seconds;
642
643 if (rtc_date_offset == -1)
644 if (rtc_utc)
645 seconds = mktimegm(tm);
646 else
647 seconds = mktime(tm);
648 else
649 seconds = mktimegm(tm) + rtc_date_offset;
650
651 return seconds - time(NULL);
652}
653
654
655#ifdef CONFIG_TRACE
656static int tbflush_requested;
657static int exit_requested;
658
659void start_tracing()
660{
661 if (trace_filename == NULL)
662 return;
663 if (!tracing) {
664 fprintf(stderr,"-- start tracing --\n");
665 start_time = Now();
666 }
667 tracing = 1;
668 tbflush_requested = 1;
669 qemu_notify_event();
670}
671
672void stop_tracing()
673{
674 if (trace_filename == NULL)
675 return;
676 if (tracing) {
677 end_time = Now();
678 elapsed_usecs += end_time - start_time;
679 fprintf(stderr,"-- stop tracing --\n");
680 }
681 tracing = 0;
682 tbflush_requested = 1;
683 qemu_notify_event();
684}
685
686#ifndef _WIN32
687/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
688 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
689 */
690void sigusr_handler(int sig)
691{
692 if (sig == SIGUSR1)
693 start_tracing();
694 else
695 stop_tracing();
696}
697#endif
698
699/* This is the handler to catch control-C so that we can exit cleanly.
700 * This is needed when tracing to flush the buffers to disk.
701 */
702void sigint_handler(int sig)
703{
704 exit_requested = 1;
705 qemu_notify_event();
706}
707#endif /* CONFIG_TRACE */
708
709
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700710/***********************************************************/
711/* Bluetooth support */
712static int nb_hcis;
713static int cur_hci;
714static struct HCIInfo *hci_table[MAX_NICS];
715
716static struct bt_vlan_s {
717 struct bt_scatternet_s net;
718 int id;
719 struct bt_vlan_s *next;
720} *first_bt_vlan;
721
722/* find or alloc a new bluetooth "VLAN" */
723static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
724{
725 struct bt_vlan_s **pvlan, *vlan;
726 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
727 if (vlan->id == id)
728 return &vlan->net;
729 }
730 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
731 vlan->id = id;
732 pvlan = &first_bt_vlan;
733 while (*pvlan != NULL)
734 pvlan = &(*pvlan)->next;
735 *pvlan = vlan;
736 return &vlan->net;
737}
738
739static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
740{
741}
742
743static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
744{
745 return -ENOTSUP;
746}
747
748static struct HCIInfo null_hci = {
749 .cmd_send = null_hci_send,
750 .sco_send = null_hci_send,
751 .acl_send = null_hci_send,
752 .bdaddr_set = null_hci_addr_set,
753};
754
755struct HCIInfo *qemu_next_hci(void)
756{
757 if (cur_hci == nb_hcis)
758 return &null_hci;
759
760 return hci_table[cur_hci++];
761}
762
763static struct HCIInfo *hci_init(const char *str)
764{
765 char *endp;
766 struct bt_scatternet_s *vlan = 0;
767
768 if (!strcmp(str, "null"))
769 /* null */
770 return &null_hci;
771 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
772 /* host[:hciN] */
773 return bt_host_hci(str[4] ? str + 5 : "hci0");
774 else if (!strncmp(str, "hci", 3)) {
775 /* hci[,vlan=n] */
776 if (str[3]) {
777 if (!strncmp(str + 3, ",vlan=", 6)) {
778 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
779 if (*endp)
780 vlan = 0;
781 }
782 } else
783 vlan = qemu_find_bt_vlan(0);
784 if (vlan)
785 return bt_new_hci(vlan);
786 }
787
788 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
789
790 return 0;
791}
792
793static int bt_hci_parse(const char *str)
794{
795 struct HCIInfo *hci;
796 bdaddr_t bdaddr;
797
798 if (nb_hcis >= MAX_NICS) {
799 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
800 return -1;
801 }
802
803 hci = hci_init(str);
804 if (!hci)
805 return -1;
806
807 bdaddr.b[0] = 0x52;
808 bdaddr.b[1] = 0x54;
809 bdaddr.b[2] = 0x00;
810 bdaddr.b[3] = 0x12;
811 bdaddr.b[4] = 0x34;
812 bdaddr.b[5] = 0x56 + nb_hcis;
813 hci->bdaddr_set(hci, bdaddr.b);
814
815 hci_table[nb_hcis++] = hci;
816
817 return 0;
818}
819
820static void bt_vhci_add(int vlan_id)
821{
822 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
823
824 if (!vlan->slave)
825 fprintf(stderr, "qemu: warning: adding a VHCI to "
826 "an empty scatternet %i\n", vlan_id);
827
828 bt_vhci_init(bt_new_hci(vlan));
829}
830
831static struct bt_device_s *bt_device_add(const char *opt)
832{
833 struct bt_scatternet_s *vlan;
834 int vlan_id = 0;
835 char *endp = strstr(opt, ",vlan=");
836 int len = (endp ? endp - opt : strlen(opt)) + 1;
837 char devname[10];
838
839 pstrcpy(devname, MIN(sizeof(devname), len), opt);
840
841 if (endp) {
842 vlan_id = strtol(endp + 6, &endp, 0);
843 if (*endp) {
844 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
845 return 0;
846 }
847 }
848
849 vlan = qemu_find_bt_vlan(vlan_id);
850
851 if (!vlan->slave)
852 fprintf(stderr, "qemu: warning: adding a slave device to "
853 "an empty scatternet %i\n", vlan_id);
854
855 if (!strcmp(devname, "keyboard"))
856 return bt_keyboard_init(vlan);
857
858 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
859 return 0;
860}
861
862static int bt_parse(const char *opt)
863{
864 const char *endp, *p;
865 int vlan;
866
867 if (strstart(opt, "hci", &endp)) {
868 if (!*endp || *endp == ',') {
869 if (*endp)
870 if (!strstart(endp, ",vlan=", 0))
871 opt = endp + 1;
872
873 return bt_hci_parse(opt);
874 }
875 } else if (strstart(opt, "vhci", &endp)) {
876 if (!*endp || *endp == ',') {
877 if (*endp) {
878 if (strstart(endp, ",vlan=", &p)) {
879 vlan = strtol(p, (char **) &endp, 0);
880 if (*endp) {
881 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
882 return 1;
883 }
884 } else {
885 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
886 return 1;
887 }
888 } else
889 vlan = 0;
890
891 bt_vhci_add(vlan);
892 return 0;
893 }
894 } else if (strstart(opt, "device:", &endp))
895 return !bt_device_add(endp);
896
897 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
898 return 1;
899}
900
901/***********************************************************/
902/* QEMU Block devices */
903
904#define HD_ALIAS "index=%d,media=disk"
905#define CDROM_ALIAS "index=2,media=cdrom"
906#define FD_ALIAS "index=%d,if=floppy"
907#define PFLASH_ALIAS "if=pflash"
908#define MTD_ALIAS "if=mtd"
909#define SD_ALIAS "index=0,if=sd"
910
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100911static int drive_init_func(QemuOpts *opts, void *opaque)
912{
913 int *use_scsi = opaque;
914 int fatal_error = 0;
915
916 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
917 if (fatal_error)
918 return 1;
919 }
920 return 0;
921}
922
923static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
924{
925 if (NULL == qemu_opt_get(opts, "snapshot")) {
926 qemu_opt_set(opts, "snapshot", "on");
927 }
928 return 0;
929}
930
931#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700932static int drive_opt_get_free_idx(void)
933{
934 int index;
935
936 for (index = 0; index < MAX_DRIVES; index++)
937 if (!drives_opt[index].used) {
938 drives_opt[index].used = 1;
939 return index;
940 }
941
942 return -1;
943}
944
945static int drive_get_free_idx(void)
946{
947 int index;
948
949 for (index = 0; index < MAX_DRIVES; index++)
950 if (!drives_table[index].used) {
951 drives_table[index].used = 1;
952 return index;
953 }
954
955 return -1;
956}
957
958int drive_add(const char *file, const char *fmt, ...)
959{
960 va_list ap;
961 int index = drive_opt_get_free_idx();
962
963 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
964 fprintf(stderr, "qemu: too many drives\n");
965 return -1;
966 }
967
968 drives_opt[index].file = file;
969 va_start(ap, fmt);
970 vsnprintf(drives_opt[index].opt,
971 sizeof(drives_opt[0].opt), fmt, ap);
972 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700973
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700974 nb_drives_opt++;
975 return index;
976}
977
978void drive_remove(int index)
979{
980 drives_opt[index].used = 0;
981 nb_drives_opt--;
982}
983
984int drive_get_index(BlockInterfaceType type, int bus, int unit)
985{
986 int index;
987
988 /* seek interface, bus and unit */
989
990 for (index = 0; index < MAX_DRIVES; index++)
991 if (drives_table[index].type == type &&
992 drives_table[index].bus == bus &&
993 drives_table[index].unit == unit &&
994 drives_table[index].used)
995 return index;
996
997 return -1;
998}
999
1000int drive_get_max_bus(BlockInterfaceType type)
1001{
1002 int max_bus;
1003 int index;
1004
1005 max_bus = -1;
1006 for (index = 0; index < nb_drives; index++) {
1007 if(drives_table[index].type == type &&
1008 drives_table[index].bus > max_bus)
1009 max_bus = drives_table[index].bus;
1010 }
1011 return max_bus;
1012}
1013
1014const char *drive_get_serial(BlockDriverState *bdrv)
1015{
1016 int index;
1017
1018 for (index = 0; index < nb_drives; index++)
1019 if (drives_table[index].bdrv == bdrv)
1020 return drives_table[index].serial;
1021
1022 return "\0";
1023}
1024
1025BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1026{
1027 int index;
1028
1029 for (index = 0; index < nb_drives; index++)
1030 if (drives_table[index].bdrv == bdrv)
1031 return drives_table[index].onerror;
1032
1033 return BLOCK_ERR_STOP_ENOSPC;
1034}
1035
1036static void bdrv_format_print(void *opaque, const char *name)
1037{
1038 fprintf(stderr, " %s", name);
1039}
1040
1041void drive_uninit(BlockDriverState *bdrv)
1042{
1043 int i;
1044
1045 for (i = 0; i < MAX_DRIVES; i++)
1046 if (drives_table[i].bdrv == bdrv) {
1047 drives_table[i].bdrv = NULL;
1048 drives_table[i].used = 0;
1049 drive_remove(drives_table[i].drive_opt_idx);
1050 nb_drives--;
1051 break;
1052 }
1053}
1054
1055int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1056{
1057 char buf[128];
1058 char file[1024];
1059 char devname[128];
1060 char serial[21];
1061 const char *mediastr = "";
1062 BlockInterfaceType type;
1063 enum { MEDIA_DISK, MEDIA_CDROM } media;
1064 int bus_id, unit_id;
1065 int cyls, heads, secs, translation;
1066 BlockDriverState *bdrv;
1067 BlockDriver *drv = NULL;
1068 QEMUMachine *machine = opaque;
1069 int max_devs;
1070 int index;
1071 int cache;
1072 int bdrv_flags, onerror;
1073 int drives_table_idx;
1074 char *str = arg->opt;
1075 static const char * const params[] = { "bus", "unit", "if", "index",
1076 "cyls", "heads", "secs", "trans",
1077 "media", "snapshot", "file",
1078 "cache", "format", "serial", "werror",
1079 NULL };
1080
1081 if (check_params(buf, sizeof(buf), params, str) < 0) {
1082 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1083 buf, str);
1084 return -1;
1085 }
1086
1087 file[0] = 0;
1088 cyls = heads = secs = 0;
1089 bus_id = 0;
1090 unit_id = -1;
1091 translation = BIOS_ATA_TRANSLATION_AUTO;
1092 index = -1;
1093 cache = 3;
1094
1095 if (machine->use_scsi) {
1096 type = IF_SCSI;
1097 max_devs = MAX_SCSI_DEVS;
1098 pstrcpy(devname, sizeof(devname), "scsi");
1099 } else {
1100 type = IF_IDE;
1101 max_devs = MAX_IDE_DEVS;
1102 pstrcpy(devname, sizeof(devname), "ide");
1103 }
1104 media = MEDIA_DISK;
1105
1106 /* extract parameters */
1107
1108 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1109 bus_id = strtol(buf, NULL, 0);
1110 if (bus_id < 0) {
1111 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1112 return -1;
1113 }
1114 }
1115
1116 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1117 unit_id = strtol(buf, NULL, 0);
1118 if (unit_id < 0) {
1119 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1120 return -1;
1121 }
1122 }
1123
1124 if (get_param_value(buf, sizeof(buf), "if", str)) {
1125 pstrcpy(devname, sizeof(devname), buf);
1126 if (!strcmp(buf, "ide")) {
1127 type = IF_IDE;
1128 max_devs = MAX_IDE_DEVS;
1129 } else if (!strcmp(buf, "scsi")) {
1130 type = IF_SCSI;
1131 max_devs = MAX_SCSI_DEVS;
1132 } else if (!strcmp(buf, "floppy")) {
1133 type = IF_FLOPPY;
1134 max_devs = 0;
1135 } else if (!strcmp(buf, "pflash")) {
1136 type = IF_PFLASH;
1137 max_devs = 0;
1138 } else if (!strcmp(buf, "mtd")) {
1139 type = IF_MTD;
1140 max_devs = 0;
1141 } else if (!strcmp(buf, "sd")) {
1142 type = IF_SD;
1143 max_devs = 0;
1144 } else if (!strcmp(buf, "virtio")) {
1145 type = IF_VIRTIO;
1146 max_devs = 0;
1147 } else if (!strcmp(buf, "xen")) {
1148 type = IF_XEN;
1149 max_devs = 0;
1150 } else {
1151 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1152 return -1;
1153 }
1154 }
1155
1156 if (get_param_value(buf, sizeof(buf), "index", str)) {
1157 index = strtol(buf, NULL, 0);
1158 if (index < 0) {
1159 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1160 return -1;
1161 }
1162 }
1163
1164 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1165 cyls = strtol(buf, NULL, 0);
1166 }
1167
1168 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1169 heads = strtol(buf, NULL, 0);
1170 }
1171
1172 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1173 secs = strtol(buf, NULL, 0);
1174 }
1175
1176 if (cyls || heads || secs) {
1177 if (cyls < 1 || cyls > 16383) {
1178 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1179 return -1;
1180 }
1181 if (heads < 1 || heads > 16) {
1182 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1183 return -1;
1184 }
1185 if (secs < 1 || secs > 63) {
1186 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1187 return -1;
1188 }
1189 }
1190
1191 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1192 if (!cyls) {
1193 fprintf(stderr,
1194 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1195 str);
1196 return -1;
1197 }
1198 if (!strcmp(buf, "none"))
1199 translation = BIOS_ATA_TRANSLATION_NONE;
1200 else if (!strcmp(buf, "lba"))
1201 translation = BIOS_ATA_TRANSLATION_LBA;
1202 else if (!strcmp(buf, "auto"))
1203 translation = BIOS_ATA_TRANSLATION_AUTO;
1204 else {
1205 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1206 return -1;
1207 }
1208 }
1209
1210 if (get_param_value(buf, sizeof(buf), "media", str)) {
1211 if (!strcmp(buf, "disk")) {
1212 media = MEDIA_DISK;
1213 } else if (!strcmp(buf, "cdrom")) {
1214 if (cyls || secs || heads) {
1215 fprintf(stderr,
1216 "qemu: '%s' invalid physical CHS format\n", str);
1217 return -1;
1218 }
1219 media = MEDIA_CDROM;
1220 } else {
1221 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1222 return -1;
1223 }
1224 }
1225
1226 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1227 if (!strcmp(buf, "on"))
1228 snapshot = 1;
1229 else if (!strcmp(buf, "off"))
1230 snapshot = 0;
1231 else {
1232 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1233 return -1;
1234 }
1235 }
1236
1237 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1238 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1239 cache = 0;
1240 else if (!strcmp(buf, "writethrough"))
1241 cache = 1;
1242 else if (!strcmp(buf, "writeback"))
1243 cache = 2;
1244 else {
1245 fprintf(stderr, "qemu: invalid cache option\n");
1246 return -1;
1247 }
1248 }
1249
1250 if (get_param_value(buf, sizeof(buf), "format", str)) {
1251 if (strcmp(buf, "?") == 0) {
1252 fprintf(stderr, "qemu: Supported formats:");
1253 bdrv_iterate_format(bdrv_format_print, NULL);
1254 fprintf(stderr, "\n");
1255 return -1;
1256 }
1257 drv = bdrv_find_format(buf);
1258 if (!drv) {
1259 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1260 return -1;
1261 }
1262 }
1263
1264 if (arg->file == NULL)
1265 get_param_value(file, sizeof(file), "file", str);
1266 else
1267 pstrcpy(file, sizeof(file), arg->file);
1268
1269 if (!get_param_value(serial, sizeof(serial), "serial", str))
1270 memset(serial, 0, sizeof(serial));
1271
1272 onerror = BLOCK_ERR_STOP_ENOSPC;
1273 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1274 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1275 fprintf(stderr, "werror is no supported by this format\n");
1276 return -1;
1277 }
1278 if (!strcmp(buf, "ignore"))
1279 onerror = BLOCK_ERR_IGNORE;
1280 else if (!strcmp(buf, "enospc"))
1281 onerror = BLOCK_ERR_STOP_ENOSPC;
1282 else if (!strcmp(buf, "stop"))
1283 onerror = BLOCK_ERR_STOP_ANY;
1284 else if (!strcmp(buf, "report"))
1285 onerror = BLOCK_ERR_REPORT;
1286 else {
1287 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1288 return -1;
1289 }
1290 }
1291
1292 /* compute bus and unit according index */
1293
1294 if (index != -1) {
1295 if (bus_id != 0 || unit_id != -1) {
1296 fprintf(stderr,
1297 "qemu: '%s' index cannot be used with bus and unit\n", str);
1298 return -1;
1299 }
1300 if (max_devs == 0)
1301 {
1302 unit_id = index;
1303 bus_id = 0;
1304 } else {
1305 unit_id = index % max_devs;
1306 bus_id = index / max_devs;
1307 }
1308 }
1309
1310 /* if user doesn't specify a unit_id,
1311 * try to find the first free
1312 */
1313
1314 if (unit_id == -1) {
1315 unit_id = 0;
1316 while (drive_get_index(type, bus_id, unit_id) != -1) {
1317 unit_id++;
1318 if (max_devs && unit_id >= max_devs) {
1319 unit_id -= max_devs;
1320 bus_id++;
1321 }
1322 }
1323 }
1324
1325 /* check unit id */
1326
1327 if (max_devs && unit_id >= max_devs) {
1328 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1329 str, unit_id, max_devs - 1);
1330 return -1;
1331 }
1332
1333 /*
1334 * ignore multiple definitions
1335 */
1336
1337 if (drive_get_index(type, bus_id, unit_id) != -1)
1338 return -2;
1339
1340 /* init */
1341
1342 if (type == IF_IDE || type == IF_SCSI)
1343 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1344 if (max_devs)
1345 snprintf(buf, sizeof(buf), "%s%i%s%i",
1346 devname, bus_id, mediastr, unit_id);
1347 else
1348 snprintf(buf, sizeof(buf), "%s%s%i",
1349 devname, mediastr, unit_id);
1350 bdrv = bdrv_new(buf);
1351 drives_table_idx = drive_get_free_idx();
1352 drives_table[drives_table_idx].bdrv = bdrv;
1353 drives_table[drives_table_idx].type = type;
1354 drives_table[drives_table_idx].bus = bus_id;
1355 drives_table[drives_table_idx].unit = unit_id;
1356 drives_table[drives_table_idx].onerror = onerror;
1357 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1358 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1359 nb_drives++;
1360
1361 switch(type) {
1362 case IF_IDE:
1363 case IF_SCSI:
1364 case IF_XEN:
1365 switch(media) {
1366 case MEDIA_DISK:
1367 if (cyls != 0) {
1368 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1369 bdrv_set_translation_hint(bdrv, translation);
1370 }
1371 break;
1372 case MEDIA_CDROM:
1373 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1374 break;
1375 }
1376 break;
1377 case IF_SD:
1378 /* FIXME: This isn't really a floppy, but it's a reasonable
1379 approximation. */
1380 case IF_FLOPPY:
1381 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1382 break;
1383 case IF_PFLASH:
1384 case IF_MTD:
1385 case IF_VIRTIO:
1386 break;
1387 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001388 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001389 abort();
1390 }
1391 if (!file[0])
1392 return -2;
1393 bdrv_flags = 0;
1394 if (snapshot) {
1395 bdrv_flags |= BDRV_O_SNAPSHOT;
1396 cache = 2; /* always use write-back with snapshot */
1397 }
1398 if (cache == 0) /* no caching */
1399 bdrv_flags |= BDRV_O_NOCACHE;
1400 else if (cache == 2) /* write-back */
1401 bdrv_flags |= BDRV_O_CACHE_WB;
1402 else if (cache == 3) /* not specified */
1403 bdrv_flags |= BDRV_O_CACHE_DEF;
1404 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1405 fprintf(stderr, "qemu: could not open disk image %s\n",
1406 file);
1407 return -1;
1408 }
1409 if (bdrv_key_required(bdrv))
1410 autostart = 0;
1411 return drives_table_idx;
1412}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001413#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001414
1415static void numa_add(const char *optarg)
1416{
1417 char option[128];
1418 char *endptr;
1419 unsigned long long value, endvalue;
1420 int nodenr;
1421
1422 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1423 if (!strcmp(option, "node")) {
1424 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1425 nodenr = nb_numa_nodes;
1426 } else {
1427 nodenr = strtoull(option, NULL, 10);
1428 }
1429
1430 if (get_param_value(option, 128, "mem", optarg) == 0) {
1431 node_mem[nodenr] = 0;
1432 } else {
1433 value = strtoull(option, &endptr, 0);
1434 switch (*endptr) {
1435 case 0: case 'M': case 'm':
1436 value <<= 20;
1437 break;
1438 case 'G': case 'g':
1439 value <<= 30;
1440 break;
1441 }
1442 node_mem[nodenr] = value;
1443 }
1444 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1445 node_cpumask[nodenr] = 0;
1446 } else {
1447 value = strtoull(option, &endptr, 10);
1448 if (value >= 64) {
1449 value = 63;
1450 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1451 } else {
1452 if (*endptr == '-') {
1453 endvalue = strtoull(endptr+1, &endptr, 10);
1454 if (endvalue >= 63) {
1455 endvalue = 62;
1456 fprintf(stderr,
1457 "only 63 CPUs in NUMA mode supported.\n");
1458 }
1459 value = (1 << (endvalue + 1)) - (1 << value);
1460 } else {
1461 value = 1 << value;
1462 }
1463 }
1464 node_cpumask[nodenr] = value;
1465 }
1466 nb_numa_nodes++;
1467 }
1468 return;
1469}
1470
1471/***********************************************************/
1472/* USB devices */
1473
1474static USBPort *used_usb_ports;
1475static USBPort *free_usb_ports;
1476
1477/* ??? Maybe change this to register a hub to keep track of the topology. */
1478void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1479 usb_attachfn attach)
1480{
1481 port->opaque = opaque;
1482 port->index = index;
1483 port->attach = attach;
1484 port->next = free_usb_ports;
1485 free_usb_ports = port;
1486}
1487
1488int usb_device_add_dev(USBDevice *dev)
1489{
1490 USBPort *port;
1491
1492 /* Find a USB port to add the device to. */
1493 port = free_usb_ports;
1494 if (!port->next) {
1495 USBDevice *hub;
1496
1497 /* Create a new hub and chain it on. */
1498 free_usb_ports = NULL;
1499 port->next = used_usb_ports;
1500 used_usb_ports = port;
1501
1502 hub = usb_hub_init(VM_USB_HUB_SIZE);
1503 usb_attach(port, hub);
1504 port = free_usb_ports;
1505 }
1506
1507 free_usb_ports = port->next;
1508 port->next = used_usb_ports;
1509 used_usb_ports = port;
1510 usb_attach(port, dev);
1511 return 0;
1512}
1513
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001514#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001515static void usb_msd_password_cb(void *opaque, int err)
1516{
1517 USBDevice *dev = opaque;
1518
1519 if (!err)
1520 usb_device_add_dev(dev);
1521 else
1522 dev->handle_destroy(dev);
1523}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001524#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001525
1526static int usb_device_add(const char *devname, int is_hotplug)
1527{
1528 const char *p;
1529 USBDevice *dev;
1530
1531 if (!free_usb_ports)
1532 return -1;
1533
1534 if (strstart(devname, "host:", &p)) {
1535 dev = usb_host_device_open(p);
1536 } else if (!strcmp(devname, "mouse")) {
1537 dev = usb_mouse_init();
1538 } else if (!strcmp(devname, "tablet")) {
1539 dev = usb_tablet_init();
1540 } else if (!strcmp(devname, "keyboard")) {
1541 dev = usb_keyboard_init();
1542 } else if (strstart(devname, "disk:", &p)) {
1543#if 0
1544 BlockDriverState *bs;
1545#endif
1546 dev = usb_msd_init(p);
1547 if (!dev)
1548 return -1;
1549#if 0
1550 bs = usb_msd_get_bdrv(dev);
1551 if (bdrv_key_required(bs)) {
1552 autostart = 0;
1553 if (is_hotplug) {
1554 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1555 dev);
1556 return 0;
1557 }
1558 }
1559 } else if (!strcmp(devname, "wacom-tablet")) {
1560 dev = usb_wacom_init();
1561 } else if (strstart(devname, "serial:", &p)) {
1562 dev = usb_serial_init(p);
1563#ifdef CONFIG_BRLAPI
1564 } else if (!strcmp(devname, "braille")) {
1565 dev = usb_baum_init();
1566#endif
1567 } else if (strstart(devname, "net:", &p)) {
1568 int nic = nb_nics;
1569
1570 if (net_client_init("nic", p) < 0)
1571 return -1;
1572 nd_table[nic].model = "usb";
1573 dev = usb_net_init(&nd_table[nic]);
1574 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1575 dev = usb_bt_init(devname[2] ? hci_init(p) :
1576 bt_new_hci(qemu_find_bt_vlan(0)));
1577#endif
1578 } else {
1579 return -1;
1580 }
1581 if (!dev)
1582 return -1;
1583
1584 return usb_device_add_dev(dev);
1585}
1586
1587int usb_device_del_addr(int bus_num, int addr)
1588{
1589 USBPort *port;
1590 USBPort **lastp;
1591 USBDevice *dev;
1592
1593 if (!used_usb_ports)
1594 return -1;
1595
1596 if (bus_num != 0)
1597 return -1;
1598
1599 lastp = &used_usb_ports;
1600 port = used_usb_ports;
1601 while (port && port->dev->addr != addr) {
1602 lastp = &port->next;
1603 port = port->next;
1604 }
1605
1606 if (!port)
1607 return -1;
1608
1609 dev = port->dev;
1610 *lastp = port->next;
1611 usb_attach(port, NULL);
1612 dev->handle_destroy(dev);
1613 port->next = free_usb_ports;
1614 free_usb_ports = port;
1615 return 0;
1616}
1617
1618static int usb_device_del(const char *devname)
1619{
1620 int bus_num, addr;
1621 const char *p;
1622
1623 if (strstart(devname, "host:", &p))
1624 return usb_host_device_close(p);
1625
1626 if (!used_usb_ports)
1627 return -1;
1628
1629 p = strchr(devname, '.');
1630 if (!p)
1631 return -1;
1632 bus_num = strtoul(devname, NULL, 0);
1633 addr = strtoul(p + 1, NULL, 0);
1634
1635 return usb_device_del_addr(bus_num, addr);
1636}
1637
1638void do_usb_add(Monitor *mon, const char *devname)
1639{
1640 usb_device_add(devname, 1);
1641}
1642
1643void do_usb_del(Monitor *mon, const char *devname)
1644{
1645 usb_device_del(devname);
1646}
1647
1648void usb_info(Monitor *mon)
1649{
1650 USBDevice *dev;
1651 USBPort *port;
1652 const char *speed_str;
1653
1654 if (!usb_enabled) {
1655 monitor_printf(mon, "USB support not enabled\n");
1656 return;
1657 }
1658
1659 for (port = used_usb_ports; port; port = port->next) {
1660 dev = port->dev;
1661 if (!dev)
1662 continue;
1663 switch(dev->speed) {
1664 case USB_SPEED_LOW:
1665 speed_str = "1.5";
1666 break;
1667 case USB_SPEED_FULL:
1668 speed_str = "12";
1669 break;
1670 case USB_SPEED_HIGH:
1671 speed_str = "480";
1672 break;
1673 default:
1674 speed_str = "?";
1675 break;
1676 }
1677 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1678 0, dev->addr, speed_str, dev->devname);
1679 }
1680}
1681
1682/***********************************************************/
1683/* PCMCIA/Cardbus */
1684
1685static struct pcmcia_socket_entry_s {
1686 PCMCIASocket *socket;
1687 struct pcmcia_socket_entry_s *next;
1688} *pcmcia_sockets = 0;
1689
1690void pcmcia_socket_register(PCMCIASocket *socket)
1691{
1692 struct pcmcia_socket_entry_s *entry;
1693
1694 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1695 entry->socket = socket;
1696 entry->next = pcmcia_sockets;
1697 pcmcia_sockets = entry;
1698}
1699
1700void pcmcia_socket_unregister(PCMCIASocket *socket)
1701{
1702 struct pcmcia_socket_entry_s *entry, **ptr;
1703
1704 ptr = &pcmcia_sockets;
1705 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1706 if (entry->socket == socket) {
1707 *ptr = entry->next;
1708 qemu_free(entry);
1709 }
1710}
1711
1712void pcmcia_info(Monitor *mon)
1713{
1714 struct pcmcia_socket_entry_s *iter;
1715
1716 if (!pcmcia_sockets)
1717 monitor_printf(mon, "No PCMCIA sockets\n");
1718
1719 for (iter = pcmcia_sockets; iter; iter = iter->next)
1720 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1721 iter->socket->attached ? iter->socket->card_string :
1722 "Empty");
1723}
1724
1725/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001726/* I/O handling */
1727
1728typedef struct IOHandlerRecord {
1729 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001730 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001731 IOHandler *fd_read;
1732 IOHandler *fd_write;
1733 int deleted;
1734 void *opaque;
1735 /* temporary data */
1736 struct pollfd *ufd;
1737 struct IOHandlerRecord *next;
1738} IOHandlerRecord;
1739
1740static IOHandlerRecord *first_io_handler;
1741
1742/* XXX: fd_read_poll should be suppressed, but an API change is
1743 necessary in the character devices to suppress fd_can_read(). */
1744int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001745 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001746 IOHandler *fd_read,
1747 IOHandler *fd_write,
1748 void *opaque)
1749{
1750 IOHandlerRecord **pioh, *ioh;
1751
1752 if (!fd_read && !fd_write) {
1753 pioh = &first_io_handler;
1754 for(;;) {
1755 ioh = *pioh;
1756 if (ioh == NULL)
1757 break;
1758 if (ioh->fd == fd) {
1759 ioh->deleted = 1;
1760 break;
1761 }
1762 pioh = &ioh->next;
1763 }
1764 } else {
1765 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1766 if (ioh->fd == fd)
1767 goto found;
1768 }
1769 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1770 ioh->next = first_io_handler;
1771 first_io_handler = ioh;
1772 found:
1773 ioh->fd = fd;
1774 ioh->fd_read_poll = fd_read_poll;
1775 ioh->fd_read = fd_read;
1776 ioh->fd_write = fd_write;
1777 ioh->opaque = opaque;
1778 ioh->deleted = 0;
1779 }
1780 return 0;
1781}
1782
1783int qemu_set_fd_handler(int fd,
1784 IOHandler *fd_read,
1785 IOHandler *fd_write,
1786 void *opaque)
1787{
1788 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1789}
1790
1791#ifdef _WIN32
1792/***********************************************************/
1793/* Polling handling */
1794
1795typedef struct PollingEntry {
1796 PollingFunc *func;
1797 void *opaque;
1798 struct PollingEntry *next;
1799} PollingEntry;
1800
1801static PollingEntry *first_polling_entry;
1802
1803int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1804{
1805 PollingEntry **ppe, *pe;
1806 pe = qemu_mallocz(sizeof(PollingEntry));
1807 pe->func = func;
1808 pe->opaque = opaque;
1809 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1810 *ppe = pe;
1811 return 0;
1812}
1813
1814void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1815{
1816 PollingEntry **ppe, *pe;
1817 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1818 pe = *ppe;
1819 if (pe->func == func && pe->opaque == opaque) {
1820 *ppe = pe->next;
1821 qemu_free(pe);
1822 break;
1823 }
1824 }
1825}
1826
1827/***********************************************************/
1828/* Wait objects support */
1829typedef struct WaitObjects {
1830 int num;
1831 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1832 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1833 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1834} WaitObjects;
1835
1836static WaitObjects wait_objects = {0};
1837
1838int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1839{
1840 WaitObjects *w = &wait_objects;
1841
1842 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1843 return -1;
1844 w->events[w->num] = handle;
1845 w->func[w->num] = func;
1846 w->opaque[w->num] = opaque;
1847 w->num++;
1848 return 0;
1849}
1850
1851void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1852{
1853 int i, found;
1854 WaitObjects *w = &wait_objects;
1855
1856 found = 0;
1857 for (i = 0; i < w->num; i++) {
1858 if (w->events[i] == handle)
1859 found = 1;
1860 if (found) {
1861 w->events[i] = w->events[i + 1];
1862 w->func[i] = w->func[i + 1];
1863 w->opaque[i] = w->opaque[i + 1];
1864 }
1865 }
1866 if (found)
1867 w->num--;
1868}
1869#endif
1870
1871/***********************************************************/
1872/* ram save/restore */
1873
1874static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1875{
1876 int v;
1877
1878 v = qemu_get_byte(f);
1879 switch(v) {
1880 case 0:
1881 if (qemu_get_buffer(f, buf, len) != len)
1882 return -EIO;
1883 break;
1884 case 1:
1885 v = qemu_get_byte(f);
1886 memset(buf, v, len);
1887 break;
1888 default:
1889 return -EINVAL;
1890 }
1891
1892 if (qemu_file_has_error(f))
1893 return -EIO;
1894
1895 return 0;
1896}
1897
1898static int ram_load_v1(QEMUFile *f, void *opaque)
1899{
1900 int ret;
1901 ram_addr_t i;
1902
1903 if (qemu_get_be32(f) != last_ram_offset)
1904 return -EINVAL;
1905 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1906 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1907 if (ret)
1908 return ret;
1909 }
1910 return 0;
1911}
1912
1913#define BDRV_HASH_BLOCK_SIZE 1024
1914#define IOBUF_SIZE 4096
1915#define RAM_CBLOCK_MAGIC 0xfabe
1916
1917typedef struct RamDecompressState {
1918 z_stream zstream;
1919 QEMUFile *f;
1920 uint8_t buf[IOBUF_SIZE];
1921} RamDecompressState;
1922
1923static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1924{
1925 int ret;
1926 memset(s, 0, sizeof(*s));
1927 s->f = f;
1928 ret = inflateInit(&s->zstream);
1929 if (ret != Z_OK)
1930 return -1;
1931 return 0;
1932}
1933
1934static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1935{
1936 int ret, clen;
1937
1938 s->zstream.avail_out = len;
1939 s->zstream.next_out = buf;
1940 while (s->zstream.avail_out > 0) {
1941 if (s->zstream.avail_in == 0) {
1942 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1943 return -1;
1944 clen = qemu_get_be16(s->f);
1945 if (clen > IOBUF_SIZE)
1946 return -1;
1947 qemu_get_buffer(s->f, s->buf, clen);
1948 s->zstream.avail_in = clen;
1949 s->zstream.next_in = s->buf;
1950 }
1951 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1952 if (ret != Z_OK && ret != Z_STREAM_END) {
1953 return -1;
1954 }
1955 }
1956 return 0;
1957}
1958
1959static void ram_decompress_close(RamDecompressState *s)
1960{
1961 inflateEnd(&s->zstream);
1962}
1963
1964#define RAM_SAVE_FLAG_FULL 0x01
1965#define RAM_SAVE_FLAG_COMPRESS 0x02
1966#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1967#define RAM_SAVE_FLAG_PAGE 0x08
1968#define RAM_SAVE_FLAG_EOS 0x10
1969
1970static int is_dup_page(uint8_t *page, uint8_t ch)
1971{
1972 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1973 uint32_t *array = (uint32_t *)page;
1974 int i;
1975
1976 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1977 if (array[i] != val)
1978 return 0;
1979 }
1980
1981 return 1;
1982}
1983
1984static int ram_save_block(QEMUFile *f)
1985{
1986 static ram_addr_t current_addr = 0;
1987 ram_addr_t saved_addr = current_addr;
1988 ram_addr_t addr = 0;
1989 int found = 0;
1990
1991 while (addr < last_ram_offset) {
1992 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1993 uint8_t *p;
1994
1995 cpu_physical_memory_reset_dirty(current_addr,
1996 current_addr + TARGET_PAGE_SIZE,
1997 MIGRATION_DIRTY_FLAG);
1998
1999 p = qemu_get_ram_ptr(current_addr);
2000
2001 if (is_dup_page(p, *p)) {
2002 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
2003 qemu_put_byte(f, *p);
2004 } else {
2005 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
2006 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
2007 }
2008
2009 found = 1;
2010 break;
2011 }
2012 addr += TARGET_PAGE_SIZE;
2013 current_addr = (saved_addr + addr) % last_ram_offset;
2014 }
2015
2016 return found;
2017}
2018
2019static uint64_t bytes_transferred = 0;
2020
2021static ram_addr_t ram_save_remaining(void)
2022{
2023 ram_addr_t addr;
2024 ram_addr_t count = 0;
2025
2026 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2027 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2028 count++;
2029 }
2030
2031 return count;
2032}
2033
2034uint64_t ram_bytes_remaining(void)
2035{
2036 return ram_save_remaining() * TARGET_PAGE_SIZE;
2037}
2038
2039uint64_t ram_bytes_transferred(void)
2040{
2041 return bytes_transferred;
2042}
2043
2044uint64_t ram_bytes_total(void)
2045{
2046 return last_ram_offset;
2047}
2048
2049static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2050{
2051 ram_addr_t addr;
2052 uint64_t bytes_transferred_last;
2053 double bwidth = 0;
2054 uint64_t expected_time = 0;
2055
2056 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2057
2058 if (stage == 1) {
2059 /* Make sure all dirty bits are set */
2060 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2061 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2062 cpu_physical_memory_set_dirty(addr);
2063 }
2064
2065 /* Enable dirty memory tracking */
2066 cpu_physical_memory_set_dirty_tracking(1);
2067
2068 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2069 }
2070
2071 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002072 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002073
2074 while (!qemu_file_rate_limit(f)) {
2075 int ret;
2076
2077 ret = ram_save_block(f);
2078 bytes_transferred += ret * TARGET_PAGE_SIZE;
2079 if (ret == 0) /* no more blocks */
2080 break;
2081 }
2082
David Turner6a9ef172010-09-09 22:54:36 +02002083 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002084 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2085
2086 /* if we haven't transferred anything this round, force expected_time to a
2087 * a very high value, but without crashing */
2088 if (bwidth == 0)
2089 bwidth = 0.000001;
2090
2091 /* try transferring iterative blocks of memory */
2092
2093 if (stage == 3) {
2094
2095 /* flush all remaining blocks regardless of rate limiting */
2096 while (ram_save_block(f) != 0) {
2097 bytes_transferred += TARGET_PAGE_SIZE;
2098 }
2099 cpu_physical_memory_set_dirty_tracking(0);
2100 }
2101
2102 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2103
2104 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2105
2106 return (stage == 2) && (expected_time <= migrate_max_downtime());
2107}
2108
2109static int ram_load_dead(QEMUFile *f, void *opaque)
2110{
2111 RamDecompressState s1, *s = &s1;
2112 uint8_t buf[10];
2113 ram_addr_t i;
2114
2115 if (ram_decompress_open(s, f) < 0)
2116 return -EINVAL;
2117 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2118 if (ram_decompress_buf(s, buf, 1) < 0) {
2119 fprintf(stderr, "Error while reading ram block header\n");
2120 goto error;
2121 }
2122 if (buf[0] == 0) {
2123 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2124 BDRV_HASH_BLOCK_SIZE) < 0) {
2125 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2126 goto error;
2127 }
2128 } else {
2129 error:
2130 printf("Error block header\n");
2131 return -EINVAL;
2132 }
2133 }
2134 ram_decompress_close(s);
2135
2136 return 0;
2137}
2138
2139static int ram_load(QEMUFile *f, void *opaque, int version_id)
2140{
2141 ram_addr_t addr;
2142 int flags;
2143
2144 if (version_id == 1)
2145 return ram_load_v1(f, opaque);
2146
2147 if (version_id == 2) {
2148 if (qemu_get_be32(f) != last_ram_offset)
2149 return -EINVAL;
2150 return ram_load_dead(f, opaque);
2151 }
2152
2153 if (version_id != 3)
2154 return -EINVAL;
2155
2156 do {
2157 addr = qemu_get_be64(f);
2158
2159 flags = addr & ~TARGET_PAGE_MASK;
2160 addr &= TARGET_PAGE_MASK;
2161
2162 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2163 if (addr != last_ram_offset)
2164 return -EINVAL;
2165 }
2166
2167 if (flags & RAM_SAVE_FLAG_FULL) {
2168 if (ram_load_dead(f, opaque) < 0)
2169 return -EINVAL;
2170 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002171
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002172 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2173 uint8_t ch = qemu_get_byte(f);
2174 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2175 } else if (flags & RAM_SAVE_FLAG_PAGE)
2176 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2177 } while (!(flags & RAM_SAVE_FLAG_EOS));
2178
2179 return 0;
2180}
2181
2182void qemu_service_io(void)
2183{
2184 qemu_notify_event();
2185}
2186
2187/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002188/* machine registration */
2189
2190static QEMUMachine *first_machine = NULL;
2191QEMUMachine *current_machine = NULL;
2192
2193int qemu_register_machine(QEMUMachine *m)
2194{
2195 QEMUMachine **pm;
2196 pm = &first_machine;
2197 while (*pm != NULL)
2198 pm = &(*pm)->next;
2199 m->next = NULL;
2200 *pm = m;
2201 return 0;
2202}
2203
2204static QEMUMachine *find_machine(const char *name)
2205{
2206 QEMUMachine *m;
2207
2208 for(m = first_machine; m != NULL; m = m->next) {
2209 if (!strcmp(m->name, name))
2210 return m;
2211 }
2212 return NULL;
2213}
2214
2215static QEMUMachine *find_default_machine(void)
2216{
2217 QEMUMachine *m;
2218
2219 for(m = first_machine; m != NULL; m = m->next) {
2220 if (m->is_default) {
2221 return m;
2222 }
2223 }
2224 return NULL;
2225}
2226
2227/***********************************************************/
2228/* main execution loop */
2229
2230static void gui_update(void *opaque)
2231{
2232 uint64_t interval = GUI_REFRESH_INTERVAL;
2233 DisplayState *ds = opaque;
2234 DisplayChangeListener *dcl = ds->listeners;
2235
2236 dpy_refresh(ds);
2237
2238 while (dcl != NULL) {
2239 if (dcl->gui_timer_interval &&
2240 dcl->gui_timer_interval < interval)
2241 interval = dcl->gui_timer_interval;
2242 dcl = dcl->next;
2243 }
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002244 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002245}
2246
2247static void nographic_update(void *opaque)
2248{
2249 uint64_t interval = GUI_REFRESH_INTERVAL;
2250
David 'Digit' Turner5973c772011-05-10 07:06:00 +02002251 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002252}
2253
2254struct vm_change_state_entry {
2255 VMChangeStateHandler *cb;
2256 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002257 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002258};
2259
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002260static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002261
2262VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2263 void *opaque)
2264{
2265 VMChangeStateEntry *e;
2266
2267 e = qemu_mallocz(sizeof (*e));
2268
2269 e->cb = cb;
2270 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002271 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002272 return e;
2273}
2274
2275void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2276{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002277 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002278 qemu_free (e);
2279}
2280
2281static void vm_state_notify(int running, int reason)
2282{
2283 VMChangeStateEntry *e;
2284
2285 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2286 e->cb(e->opaque, running, reason);
2287 }
2288}
2289
2290static void resume_all_vcpus(void);
2291static void pause_all_vcpus(void);
2292
2293void vm_start(void)
2294{
2295 if (!vm_running) {
2296 cpu_enable_ticks();
2297 vm_running = 1;
2298 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002299 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002300 resume_all_vcpus();
2301 }
2302}
2303
2304/* reset/shutdown handler */
2305
2306typedef struct QEMUResetEntry {
2307 QEMUResetHandler *func;
2308 void *opaque;
2309 int order;
2310 struct QEMUResetEntry *next;
2311} QEMUResetEntry;
2312
2313static QEMUResetEntry *first_reset_entry;
2314static int reset_requested;
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002315static int shutdown_requested, shutdown_signal = -1;
2316static pid_t shutdown_pid;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002317static int powerdown_requested;
2318static int debug_requested;
2319static int vmstop_requested;
2320
2321int qemu_shutdown_requested(void)
2322{
2323 int r = shutdown_requested;
2324 shutdown_requested = 0;
2325 return r;
2326}
2327
2328int qemu_reset_requested(void)
2329{
2330 int r = reset_requested;
2331 reset_requested = 0;
2332 return r;
2333}
2334
2335int qemu_powerdown_requested(void)
2336{
2337 int r = powerdown_requested;
2338 powerdown_requested = 0;
2339 return r;
2340}
2341
2342static int qemu_debug_requested(void)
2343{
2344 int r = debug_requested;
2345 debug_requested = 0;
2346 return r;
2347}
2348
2349static int qemu_vmstop_requested(void)
2350{
2351 int r = vmstop_requested;
2352 vmstop_requested = 0;
2353 return r;
2354}
2355
2356static void do_vm_stop(int reason)
2357{
2358 if (vm_running) {
2359 cpu_disable_ticks();
2360 vm_running = 0;
2361 pause_all_vcpus();
2362 vm_state_notify(0, reason);
2363 }
2364}
2365
2366void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2367{
2368 QEMUResetEntry **pre, *re;
2369
2370 pre = &first_reset_entry;
2371 while (*pre != NULL && (*pre)->order >= order) {
2372 pre = &(*pre)->next;
2373 }
2374 re = qemu_mallocz(sizeof(QEMUResetEntry));
2375 re->func = func;
2376 re->opaque = opaque;
2377 re->order = order;
2378 re->next = NULL;
2379 *pre = re;
2380}
2381
2382void qemu_system_reset(void)
2383{
2384 QEMUResetEntry *re;
2385
2386 /* reset all devices */
2387 for(re = first_reset_entry; re != NULL; re = re->next) {
2388 re->func(re->opaque);
2389 }
2390}
2391
2392void qemu_system_reset_request(void)
2393{
2394 if (no_reboot) {
2395 shutdown_requested = 1;
2396 } else {
2397 reset_requested = 1;
2398 }
2399 qemu_notify_event();
2400}
2401
David 'Digit' Turner088edf82011-05-09 15:59:28 +02002402void qemu_system_killed(int signal, pid_t pid)
2403{
2404 shutdown_signal = signal;
2405 shutdown_pid = pid;
2406 qemu_system_shutdown_request();
2407}
2408
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002409void qemu_system_shutdown_request(void)
2410{
2411 shutdown_requested = 1;
2412 qemu_notify_event();
2413}
2414
2415void qemu_system_powerdown_request(void)
2416{
2417 powerdown_requested = 1;
2418 qemu_notify_event();
2419}
2420
2421#ifdef CONFIG_IOTHREAD
2422static void qemu_system_vmstop_request(int reason)
2423{
2424 vmstop_requested = reason;
2425 qemu_notify_event();
2426}
2427#endif
2428
2429#ifndef _WIN32
2430static int io_thread_fd = -1;
2431
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002432#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002433static void qemu_event_increment(void)
2434{
2435 static const char byte = 0;
2436
2437 if (io_thread_fd == -1)
2438 return;
2439
2440 write(io_thread_fd, &byte, sizeof(byte));
2441}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002442#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002443
2444static void qemu_event_read(void *opaque)
2445{
2446 int fd = (unsigned long)opaque;
2447 ssize_t len;
2448
2449 /* Drain the notify pipe */
2450 do {
2451 char buffer[512];
2452 len = read(fd, buffer, sizeof(buffer));
2453 } while ((len == -1 && errno == EINTR) || len > 0);
2454}
2455
2456static int qemu_event_init(void)
2457{
2458 int err;
2459 int fds[2];
2460
2461 err = pipe(fds);
2462 if (err == -1)
2463 return -errno;
2464
2465 err = fcntl_setfl(fds[0], O_NONBLOCK);
2466 if (err < 0)
2467 goto fail;
2468
2469 err = fcntl_setfl(fds[1], O_NONBLOCK);
2470 if (err < 0)
2471 goto fail;
2472
2473 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2474 (void *)(unsigned long)fds[0]);
2475
2476 io_thread_fd = fds[1];
2477 return 0;
2478
2479fail:
2480 close(fds[0]);
2481 close(fds[1]);
2482 return err;
2483}
2484#else
2485HANDLE qemu_event_handle;
2486
2487static void dummy_event_handler(void *opaque)
2488{
2489}
2490
2491static int qemu_event_init(void)
2492{
2493 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2494 if (!qemu_event_handle) {
2495 perror("Failed CreateEvent");
2496 return -1;
2497 }
2498 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2499 return 0;
2500}
2501
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002502#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002503static void qemu_event_increment(void)
2504{
2505 SetEvent(qemu_event_handle);
2506}
2507#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002508#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002509
2510static int cpu_can_run(CPUState *env)
2511{
2512 if (env->stop)
2513 return 0;
2514 if (env->stopped)
2515 return 0;
2516 return 1;
2517}
2518
2519#ifndef CONFIG_IOTHREAD
2520static int qemu_init_main_loop(void)
2521{
2522 return qemu_event_init();
2523}
2524
2525void qemu_init_vcpu(void *_env)
2526{
2527 CPUState *env = _env;
2528
2529 if (kvm_enabled())
2530 kvm_init_vcpu(env);
2531 return;
2532}
2533
2534int qemu_cpu_self(void *env)
2535{
2536 return 1;
2537}
2538
2539static void resume_all_vcpus(void)
2540{
2541}
2542
2543static void pause_all_vcpus(void)
2544{
2545}
2546
2547void qemu_cpu_kick(void *env)
2548{
2549 return;
2550}
2551
2552void qemu_notify_event(void)
2553{
2554 CPUState *env = cpu_single_env;
2555
2556 if (env) {
2557 cpu_exit(env);
2558#ifdef USE_KQEMU
2559 if (env->kqemu_enabled)
2560 kqemu_cpu_interrupt(env);
2561#endif
2562 }
2563}
2564
2565#define qemu_mutex_lock_iothread() do { } while (0)
2566#define qemu_mutex_unlock_iothread() do { } while (0)
2567
2568void vm_stop(int reason)
2569{
2570 do_vm_stop(reason);
2571}
2572
2573#else /* CONFIG_IOTHREAD */
2574
2575#include "qemu-thread.h"
2576
2577QemuMutex qemu_global_mutex;
2578static QemuMutex qemu_fair_mutex;
2579
2580static QemuThread io_thread;
2581
2582static QemuThread *tcg_cpu_thread;
2583static QemuCond *tcg_halt_cond;
2584
2585static int qemu_system_ready;
2586/* cpu creation */
2587static QemuCond qemu_cpu_cond;
2588/* system init */
2589static QemuCond qemu_system_cond;
2590static QemuCond qemu_pause_cond;
2591
2592static void block_io_signals(void);
2593static void unblock_io_signals(void);
2594static int tcg_has_work(void);
2595
2596static int qemu_init_main_loop(void)
2597{
2598 int ret;
2599
2600 ret = qemu_event_init();
2601 if (ret)
2602 return ret;
2603
2604 qemu_cond_init(&qemu_pause_cond);
2605 qemu_mutex_init(&qemu_fair_mutex);
2606 qemu_mutex_init(&qemu_global_mutex);
2607 qemu_mutex_lock(&qemu_global_mutex);
2608
2609 unblock_io_signals();
2610 qemu_thread_self(&io_thread);
2611
2612 return 0;
2613}
2614
2615static void qemu_wait_io_event(CPUState *env)
2616{
2617 while (!tcg_has_work())
2618 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2619
2620 qemu_mutex_unlock(&qemu_global_mutex);
2621
2622 /*
2623 * Users of qemu_global_mutex can be starved, having no chance
2624 * to acquire it since this path will get to it first.
2625 * So use another lock to provide fairness.
2626 */
2627 qemu_mutex_lock(&qemu_fair_mutex);
2628 qemu_mutex_unlock(&qemu_fair_mutex);
2629
2630 qemu_mutex_lock(&qemu_global_mutex);
2631 if (env->stop) {
2632 env->stop = 0;
2633 env->stopped = 1;
2634 qemu_cond_signal(&qemu_pause_cond);
2635 }
2636}
2637
2638static int qemu_cpu_exec(CPUState *env);
2639
2640static void *kvm_cpu_thread_fn(void *arg)
2641{
2642 CPUState *env = arg;
2643
2644 block_io_signals();
2645 qemu_thread_self(env->thread);
2646
2647 /* signal CPU creation */
2648 qemu_mutex_lock(&qemu_global_mutex);
2649 env->created = 1;
2650 qemu_cond_signal(&qemu_cpu_cond);
2651
2652 /* and wait for machine initialization */
2653 while (!qemu_system_ready)
2654 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2655
2656 while (1) {
2657 if (cpu_can_run(env))
2658 qemu_cpu_exec(env);
2659 qemu_wait_io_event(env);
2660 }
2661
2662 return NULL;
2663}
2664
2665static void tcg_cpu_exec(void);
2666
2667static void *tcg_cpu_thread_fn(void *arg)
2668{
2669 CPUState *env = arg;
2670
2671 block_io_signals();
2672 qemu_thread_self(env->thread);
2673
2674 /* signal CPU creation */
2675 qemu_mutex_lock(&qemu_global_mutex);
2676 for (env = first_cpu; env != NULL; env = env->next_cpu)
2677 env->created = 1;
2678 qemu_cond_signal(&qemu_cpu_cond);
2679
2680 /* and wait for machine initialization */
2681 while (!qemu_system_ready)
2682 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2683
2684 while (1) {
2685 tcg_cpu_exec();
2686 qemu_wait_io_event(cur_cpu);
2687 }
2688
2689 return NULL;
2690}
2691
2692void qemu_cpu_kick(void *_env)
2693{
2694 CPUState *env = _env;
2695 qemu_cond_broadcast(env->halt_cond);
2696 if (kvm_enabled())
2697 qemu_thread_signal(env->thread, SIGUSR1);
2698}
2699
2700int qemu_cpu_self(void *env)
2701{
2702 return (cpu_single_env != NULL);
2703}
2704
2705static void cpu_signal(int sig)
2706{
2707 if (cpu_single_env)
2708 cpu_exit(cpu_single_env);
2709}
2710
2711static void block_io_signals(void)
2712{
2713 sigset_t set;
2714 struct sigaction sigact;
2715
2716 sigemptyset(&set);
2717 sigaddset(&set, SIGUSR2);
2718 sigaddset(&set, SIGIO);
2719 sigaddset(&set, SIGALRM);
2720 pthread_sigmask(SIG_BLOCK, &set, NULL);
2721
2722 sigemptyset(&set);
2723 sigaddset(&set, SIGUSR1);
2724 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2725
2726 memset(&sigact, 0, sizeof(sigact));
2727 sigact.sa_handler = cpu_signal;
2728 sigaction(SIGUSR1, &sigact, NULL);
2729}
2730
2731static void unblock_io_signals(void)
2732{
2733 sigset_t set;
2734
2735 sigemptyset(&set);
2736 sigaddset(&set, SIGUSR2);
2737 sigaddset(&set, SIGIO);
2738 sigaddset(&set, SIGALRM);
2739 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2740
2741 sigemptyset(&set);
2742 sigaddset(&set, SIGUSR1);
2743 pthread_sigmask(SIG_BLOCK, &set, NULL);
2744}
2745
2746static void qemu_signal_lock(unsigned int msecs)
2747{
2748 qemu_mutex_lock(&qemu_fair_mutex);
2749
2750 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2751 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2752 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2753 break;
2754 }
2755 qemu_mutex_unlock(&qemu_fair_mutex);
2756}
2757
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002758void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002759{
2760 if (kvm_enabled()) {
2761 qemu_mutex_lock(&qemu_fair_mutex);
2762 qemu_mutex_lock(&qemu_global_mutex);
2763 qemu_mutex_unlock(&qemu_fair_mutex);
2764 } else
2765 qemu_signal_lock(100);
2766}
2767
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002768void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002769{
2770 qemu_mutex_unlock(&qemu_global_mutex);
2771}
2772
2773static int all_vcpus_paused(void)
2774{
2775 CPUState *penv = first_cpu;
2776
2777 while (penv) {
2778 if (!penv->stopped)
2779 return 0;
2780 penv = (CPUState *)penv->next_cpu;
2781 }
2782
2783 return 1;
2784}
2785
2786static void pause_all_vcpus(void)
2787{
2788 CPUState *penv = first_cpu;
2789
2790 while (penv) {
2791 penv->stop = 1;
2792 qemu_thread_signal(penv->thread, SIGUSR1);
2793 qemu_cpu_kick(penv);
2794 penv = (CPUState *)penv->next_cpu;
2795 }
2796
2797 while (!all_vcpus_paused()) {
2798 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2799 penv = first_cpu;
2800 while (penv) {
2801 qemu_thread_signal(penv->thread, SIGUSR1);
2802 penv = (CPUState *)penv->next_cpu;
2803 }
2804 }
2805}
2806
2807static void resume_all_vcpus(void)
2808{
2809 CPUState *penv = first_cpu;
2810
2811 while (penv) {
2812 penv->stop = 0;
2813 penv->stopped = 0;
2814 qemu_thread_signal(penv->thread, SIGUSR1);
2815 qemu_cpu_kick(penv);
2816 penv = (CPUState *)penv->next_cpu;
2817 }
2818}
2819
2820static void tcg_init_vcpu(void *_env)
2821{
2822 CPUState *env = _env;
2823 /* share a single thread for all cpus with TCG */
2824 if (!tcg_cpu_thread) {
2825 env->thread = qemu_mallocz(sizeof(QemuThread));
2826 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2827 qemu_cond_init(env->halt_cond);
2828 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2829 while (env->created == 0)
2830 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2831 tcg_cpu_thread = env->thread;
2832 tcg_halt_cond = env->halt_cond;
2833 } else {
2834 env->thread = tcg_cpu_thread;
2835 env->halt_cond = tcg_halt_cond;
2836 }
2837}
2838
2839static void kvm_start_vcpu(CPUState *env)
2840{
2841#if 0
2842 kvm_init_vcpu(env);
2843 env->thread = qemu_mallocz(sizeof(QemuThread));
2844 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2845 qemu_cond_init(env->halt_cond);
2846 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2847 while (env->created == 0)
2848 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2849#endif
2850}
2851
2852void qemu_init_vcpu(void *_env)
2853{
2854 CPUState *env = _env;
2855
2856 if (kvm_enabled())
2857 kvm_start_vcpu(env);
2858 else
2859 tcg_init_vcpu(env);
2860}
2861
2862void qemu_notify_event(void)
2863{
2864 qemu_event_increment();
2865}
2866
2867void vm_stop(int reason)
2868{
2869 QemuThread me;
2870 qemu_thread_self(&me);
2871
2872 if (!qemu_thread_equal(&me, &io_thread)) {
2873 qemu_system_vmstop_request(reason);
2874 /*
2875 * FIXME: should not return to device code in case
2876 * vm_stop() has been requested.
2877 */
2878 if (cpu_single_env) {
2879 cpu_exit(cpu_single_env);
2880 cpu_single_env->stop = 1;
2881 }
2882 return;
2883 }
2884 do_vm_stop(reason);
2885}
2886
2887#endif
2888
2889
2890#ifdef _WIN32
2891static void host_main_loop_wait(int *timeout)
2892{
2893 int ret, ret2, i;
2894 PollingEntry *pe;
2895
2896
2897 /* XXX: need to suppress polling by better using win32 events */
2898 ret = 0;
2899 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2900 ret |= pe->func(pe->opaque);
2901 }
2902 if (ret == 0) {
2903 int err;
2904 WaitObjects *w = &wait_objects;
2905
2906 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2907 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2908 if (w->func[ret - WAIT_OBJECT_0])
2909 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2910
2911 /* Check for additional signaled events */
2912 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2913
2914 /* Check if event is signaled */
2915 ret2 = WaitForSingleObject(w->events[i], 0);
2916 if(ret2 == WAIT_OBJECT_0) {
2917 if (w->func[i])
2918 w->func[i](w->opaque[i]);
2919 } else if (ret2 == WAIT_TIMEOUT) {
2920 } else {
2921 err = GetLastError();
2922 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2923 }
2924 }
2925 } else if (ret == WAIT_TIMEOUT) {
2926 } else {
2927 err = GetLastError();
2928 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2929 }
2930 }
2931
2932 *timeout = 0;
2933}
2934#else
2935static void host_main_loop_wait(int *timeout)
2936{
2937}
2938#endif
2939
2940void main_loop_wait(int timeout)
2941{
2942 IOHandlerRecord *ioh;
2943 fd_set rfds, wfds, xfds;
2944 int ret, nfds;
2945 struct timeval tv;
2946
2947 qemu_bh_update_timeout(&timeout);
2948
2949 host_main_loop_wait(&timeout);
2950
2951 /* poll any events */
2952 /* XXX: separate device handlers from system ones */
2953 nfds = -1;
2954 FD_ZERO(&rfds);
2955 FD_ZERO(&wfds);
2956 FD_ZERO(&xfds);
2957 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2958 if (ioh->deleted)
2959 continue;
2960 if (ioh->fd_read &&
2961 (!ioh->fd_read_poll ||
2962 ioh->fd_read_poll(ioh->opaque) != 0)) {
2963 FD_SET(ioh->fd, &rfds);
2964 if (ioh->fd > nfds)
2965 nfds = ioh->fd;
2966 }
2967 if (ioh->fd_write) {
2968 FD_SET(ioh->fd, &wfds);
2969 if (ioh->fd > nfds)
2970 nfds = ioh->fd;
2971 }
2972 }
2973
2974 tv.tv_sec = timeout / 1000;
2975 tv.tv_usec = (timeout % 1000) * 1000;
2976
2977#if defined(CONFIG_SLIRP)
2978 if (slirp_is_inited()) {
2979 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2980 }
2981#endif
2982 qemu_mutex_unlock_iothread();
2983 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2984 qemu_mutex_lock_iothread();
2985 if (ret > 0) {
2986 IOHandlerRecord **pioh;
2987
2988 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2989 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2990 ioh->fd_read(ioh->opaque);
2991 }
2992 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2993 ioh->fd_write(ioh->opaque);
2994 }
2995 }
2996
David 'Digit' Turner6b512812010-10-15 15:05:04 +02002997 /* remove deleted IO handlers */
2998 pioh = &first_io_handler;
2999 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003000 ioh = *pioh;
3001 if (ioh->deleted) {
3002 *pioh = ioh->next;
3003 qemu_free(ioh);
3004 } else
3005 pioh = &ioh->next;
3006 }
3007 }
3008#if defined(CONFIG_SLIRP)
3009 if (slirp_is_inited()) {
3010 if (ret < 0) {
3011 FD_ZERO(&rfds);
3012 FD_ZERO(&wfds);
3013 FD_ZERO(&xfds);
3014 }
3015 slirp_select_poll(&rfds, &wfds, &xfds);
3016 }
3017#endif
3018 charpipe_poll();
3019
David Turner6a9ef172010-09-09 22:54:36 +02003020 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003021
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003022 /* Check bottom-halves last in case any of the earlier events triggered
3023 them. */
3024 qemu_bh_poll();
3025
3026}
3027
3028static int qemu_cpu_exec(CPUState *env)
3029{
3030 int ret;
3031#ifdef CONFIG_PROFILER
3032 int64_t ti;
3033#endif
3034
3035#ifdef CONFIG_PROFILER
3036 ti = profile_getclock();
3037#endif
3038 if (use_icount) {
3039 int64_t count;
3040 int decr;
3041 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3042 env->icount_decr.u16.low = 0;
3043 env->icount_extra = 0;
David 'Digit' Turner317c9d52011-05-10 06:38:21 +02003044 count = qemu_next_icount_deadline();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003045 count = (count + (1 << icount_time_shift) - 1)
3046 >> icount_time_shift;
3047 qemu_icount += count;
3048 decr = (count > 0xffff) ? 0xffff : count;
3049 count -= decr;
3050 env->icount_decr.u16.low = decr;
3051 env->icount_extra = count;
3052 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003053#ifdef CONFIG_TRACE
3054 if (tbflush_requested) {
3055 tbflush_requested = 0;
3056 tb_flush(env);
3057 return EXCP_INTERRUPT;
3058 }
3059#endif
3060
3061
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003062 ret = cpu_exec(env);
3063#ifdef CONFIG_PROFILER
3064 qemu_time += profile_getclock() - ti;
3065#endif
3066 if (use_icount) {
3067 /* Fold pending instructions back into the
3068 instruction counter, and clear the interrupt flag. */
3069 qemu_icount -= (env->icount_decr.u16.low
3070 + env->icount_extra);
3071 env->icount_decr.u32 = 0;
3072 env->icount_extra = 0;
3073 }
3074 return ret;
3075}
3076
3077static void tcg_cpu_exec(void)
3078{
3079 int ret = 0;
3080
3081 if (next_cpu == NULL)
3082 next_cpu = first_cpu;
3083 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3084 CPUState *env = cur_cpu = next_cpu;
3085
3086 if (!vm_running)
3087 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003088 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003089 break;
3090 }
3091 if (cpu_can_run(env))
3092 ret = qemu_cpu_exec(env);
3093 if (ret == EXCP_DEBUG) {
3094 gdb_set_stop_cpu(env);
3095 debug_requested = 1;
3096 break;
3097 }
3098 }
3099}
3100
3101static int cpu_has_work(CPUState *env)
3102{
3103 if (env->stop)
3104 return 1;
3105 if (env->stopped)
3106 return 0;
3107 if (!env->halted)
3108 return 1;
3109 if (qemu_cpu_has_work(env))
3110 return 1;
3111 return 0;
3112}
3113
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003114int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003115{
3116 CPUState *env;
3117
3118 for (env = first_cpu; env != NULL; env = env->next_cpu)
3119 if (cpu_has_work(env))
3120 return 1;
3121 return 0;
3122}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003123
3124static int vm_can_run(void)
3125{
3126 if (powerdown_requested)
3127 return 0;
3128 if (reset_requested)
3129 return 0;
3130 if (shutdown_requested)
3131 return 0;
3132 if (debug_requested)
3133 return 0;
3134 return 1;
3135}
3136
3137static void main_loop(void)
3138{
3139 int r;
3140
3141#ifdef CONFIG_IOTHREAD
3142 qemu_system_ready = 1;
3143 qemu_cond_broadcast(&qemu_system_cond);
3144#endif
3145
3146 for (;;) {
3147 do {
3148#ifdef CONFIG_PROFILER
3149 int64_t ti;
3150#endif
3151#ifndef CONFIG_IOTHREAD
3152 tcg_cpu_exec();
3153#endif
3154#ifdef CONFIG_PROFILER
3155 ti = profile_getclock();
3156#endif
3157 main_loop_wait(qemu_calculate_timeout());
3158#ifdef CONFIG_PROFILER
3159 dev_time += profile_getclock() - ti;
3160#endif
rich canningsd952f282011-03-01 15:40:09 -08003161
3162 if (rotate_logs_requested) {
3163 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
3164 dns_log_filename);
3165 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
3166 drop_log_filename);
3167 slirp_dns_log_fd(new_dns_log_fd);
3168 slirp_drop_log_fd(new_drop_log_fd);
3169 reset_rotate_qemu_logs_request();
3170 }
3171
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003172 } while (vm_can_run());
3173
3174 if (qemu_debug_requested())
3175 vm_stop(EXCP_DEBUG);
3176 if (qemu_shutdown_requested()) {
3177 if (no_shutdown) {
3178 vm_stop(0);
3179 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003180 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003181 if (savevm_on_exit != NULL) {
3182 do_savevm(cur_mon, savevm_on_exit);
3183 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003184 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003185 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003186 }
3187 if (qemu_reset_requested()) {
3188 pause_all_vcpus();
3189 qemu_system_reset();
3190 resume_all_vcpus();
3191 }
3192 if (qemu_powerdown_requested())
3193 qemu_system_powerdown();
3194 if ((r = qemu_vmstop_requested()))
3195 vm_stop(r);
3196 }
3197 pause_all_vcpus();
3198}
3199
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003200void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003201{
3202 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3203}
3204
3205void qemu_help(int exitcode)
3206{
3207 version();
3208 printf("usage: %s [options] [disk_image]\n"
3209 "\n"
3210 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3211 "\n"
3212#define DEF(option, opt_arg, opt_enum, opt_help) \
3213 opt_help
3214#define DEFHEADING(text) stringify(text) "\n"
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003215#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003216#undef DEF
3217#undef DEFHEADING
3218#undef GEN_DOCS
3219 "\n"
3220 "During emulation, the following keys are useful:\n"
3221 "ctrl-alt-f toggle full screen\n"
3222 "ctrl-alt-n switch to virtual console 'n'\n"
3223 "ctrl-alt toggle mouse and keyboard grab\n"
3224 "\n"
3225 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3226 ,
3227 "qemu",
3228 DEFAULT_RAM_SIZE,
3229#ifndef _WIN32
3230 DEFAULT_NETWORK_SCRIPT,
3231 DEFAULT_NETWORK_DOWN_SCRIPT,
3232#endif
3233 DEFAULT_GDBSTUB_PORT,
3234 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003235 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003236}
3237
3238#define HAS_ARG 0x0001
3239
3240enum {
3241#define DEF(option, opt_arg, opt_enum, opt_help) \
3242 opt_enum,
3243#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003244#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003245#undef DEF
3246#undef DEFHEADING
3247#undef GEN_DOCS
3248};
3249
3250typedef struct QEMUOption {
3251 const char *name;
3252 int flags;
3253 int index;
3254} QEMUOption;
3255
3256static const QEMUOption qemu_options[] = {
3257 { "h", 0, QEMU_OPTION_h },
3258#define DEF(option, opt_arg, opt_enum, opt_help) \
3259 { option, opt_arg, opt_enum },
3260#define DEFHEADING(text)
David 'Digit' Turner088edf82011-05-09 15:59:28 +02003261#include "qemu-options.def"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003262#undef DEF
3263#undef DEFHEADING
3264#undef GEN_DOCS
3265 { NULL, 0, 0 },
3266};
3267
3268#ifdef HAS_AUDIO
3269struct soundhw soundhw[] = {
3270#ifdef HAS_AUDIO_CHOICE
3271#if defined(TARGET_I386) || defined(TARGET_MIPS)
3272 {
3273 "pcspk",
3274 "PC speaker",
3275 0,
3276 1,
3277 { .init_isa = pcspk_audio_init }
3278 },
3279#endif
3280
3281#ifdef CONFIG_SB16
3282 {
3283 "sb16",
3284 "Creative Sound Blaster 16",
3285 0,
3286 1,
3287 { .init_isa = SB16_init }
3288 },
3289#endif
3290
3291#ifdef CONFIG_CS4231A
3292 {
3293 "cs4231a",
3294 "CS4231A",
3295 0,
3296 1,
3297 { .init_isa = cs4231a_init }
3298 },
3299#endif
3300
3301#ifdef CONFIG_ADLIB
3302 {
3303 "adlib",
3304#ifdef HAS_YMF262
3305 "Yamaha YMF262 (OPL3)",
3306#else
3307 "Yamaha YM3812 (OPL2)",
3308#endif
3309 0,
3310 1,
3311 { .init_isa = Adlib_init }
3312 },
3313#endif
3314
3315#ifdef CONFIG_GUS
3316 {
3317 "gus",
3318 "Gravis Ultrasound GF1",
3319 0,
3320 1,
3321 { .init_isa = GUS_init }
3322 },
3323#endif
3324
3325#ifdef CONFIG_AC97
3326 {
3327 "ac97",
3328 "Intel 82801AA AC97 Audio",
3329 0,
3330 0,
3331 { .init_pci = ac97_init }
3332 },
3333#endif
3334
3335#ifdef CONFIG_ES1370
3336 {
3337 "es1370",
3338 "ENSONIQ AudioPCI ES1370",
3339 0,
3340 0,
3341 { .init_pci = es1370_init }
3342 },
3343#endif
3344
3345#endif /* HAS_AUDIO_CHOICE */
3346
3347 { NULL, NULL, 0, 0, { NULL } }
3348};
3349
3350static void select_soundhw (const char *optarg)
3351{
3352 struct soundhw *c;
3353
3354 if (*optarg == '?') {
3355 show_valid_cards:
3356
3357 printf ("Valid sound card names (comma separated):\n");
3358 for (c = soundhw; c->name; ++c) {
3359 printf ("%-11s %s\n", c->name, c->descr);
3360 }
3361 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003362 if (*optarg != '?') {
3363 PANIC("Unknown sound card name: %s", optarg);
3364 } else {
3365 QEMU_EXIT(0);
3366 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003367 }
3368 else {
3369 size_t l;
3370 const char *p;
3371 char *e;
3372 int bad_card = 0;
3373
3374 if (!strcmp (optarg, "all")) {
3375 for (c = soundhw; c->name; ++c) {
3376 c->enabled = 1;
3377 }
3378 return;
3379 }
3380
3381 p = optarg;
3382 while (*p) {
3383 e = strchr (p, ',');
3384 l = !e ? strlen (p) : (size_t) (e - p);
3385
3386 for (c = soundhw; c->name; ++c) {
3387 if (!strncmp (c->name, p, l)) {
3388 c->enabled = 1;
3389 break;
3390 }
3391 }
3392
3393 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003394#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003395 if (l > 80) {
3396 fprintf (stderr,
3397 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003398 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003399 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3400 (int) l, p);
3401 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003402#endif // !CONFIG_ANDROID
3403 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003404 }
3405 p += l + (e != NULL);
3406 }
3407
3408 if (bad_card)
3409 goto show_valid_cards;
3410 }
3411}
3412#endif
3413
3414static void select_vgahw (const char *p)
3415{
3416 const char *opts;
3417
3418 cirrus_vga_enabled = 0;
3419 std_vga_enabled = 0;
3420 vmsvga_enabled = 0;
3421 xenfb_enabled = 0;
3422 if (strstart(p, "std", &opts)) {
3423 std_vga_enabled = 1;
3424 } else if (strstart(p, "cirrus", &opts)) {
3425 cirrus_vga_enabled = 1;
3426 } else if (strstart(p, "vmware", &opts)) {
3427 vmsvga_enabled = 1;
3428 } else if (strstart(p, "xenfb", &opts)) {
3429 xenfb_enabled = 1;
3430 } else if (!strstart(p, "none", &opts)) {
3431 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003432 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003433 }
3434 while (*opts) {
3435 const char *nextopt;
3436
3437 if (strstart(opts, ",retrace=", &nextopt)) {
3438 opts = nextopt;
3439 if (strstart(opts, "dumb", &nextopt))
3440 vga_retrace_method = VGA_RETRACE_DUMB;
3441 else if (strstart(opts, "precise", &nextopt))
3442 vga_retrace_method = VGA_RETRACE_PRECISE;
3443 else goto invalid_vga;
3444 } else goto invalid_vga;
3445 opts = nextopt;
3446 }
3447}
3448
3449#ifdef _WIN32
3450static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3451{
3452 exit(STATUS_CONTROL_C_EXIT);
3453 return TRUE;
3454}
3455#endif
3456
3457int qemu_uuid_parse(const char *str, uint8_t *uuid)
3458{
3459 int ret;
3460
3461 if(strlen(str) != 36)
3462 return -1;
3463
3464 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3465 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3466 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3467
3468 if(ret != 16)
3469 return -1;
3470
3471#ifdef TARGET_I386
3472 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3473#endif
3474
3475 return 0;
3476}
3477
3478#define MAX_NET_CLIENTS 32
3479
3480#ifndef _WIN32
3481
3482static void termsig_handler(int signal)
3483{
3484 qemu_system_shutdown_request();
3485}
3486
3487static void sigchld_handler(int signal)
3488{
3489 waitpid(-1, NULL, WNOHANG);
3490}
3491
3492static void sighandler_setup(void)
3493{
3494 struct sigaction act;
3495
3496 memset(&act, 0, sizeof(act));
3497 act.sa_handler = termsig_handler;
3498 sigaction(SIGINT, &act, NULL);
3499 sigaction(SIGHUP, &act, NULL);
3500 sigaction(SIGTERM, &act, NULL);
3501
3502 act.sa_handler = sigchld_handler;
3503 act.sa_flags = SA_NOCLDSTOP;
3504 sigaction(SIGCHLD, &act, NULL);
3505}
3506
3507#endif
3508
3509#ifdef _WIN32
3510/* Look for support files in the same directory as the executable. */
3511static char *find_datadir(const char *argv0)
3512{
3513 char *p;
3514 char buf[MAX_PATH];
3515 DWORD len;
3516
3517 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3518 if (len == 0) {
3519 return NULL;
3520 }
3521
3522 buf[len] = 0;
3523 p = buf + len - 1;
3524 while (p != buf && *p != '\\')
3525 p--;
3526 *p = 0;
3527 if (access(buf, R_OK) == 0) {
3528 return qemu_strdup(buf);
3529 }
3530 return NULL;
3531}
3532#else /* !_WIN32 */
3533
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003534/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003535static char *find_datadir(const char *argv0)
3536{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003537 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003538 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003539
3540#if defined(__linux__)
3541 {
3542 int len;
3543 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3544 if (len > 0) {
3545 buf[len] = 0;
3546 p = buf;
3547 }
3548 }
3549#elif defined(__FreeBSD__)
3550 {
3551 int len;
3552 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3553 if (len > 0) {
3554 buf[len] = 0;
3555 p = buf;
3556 }
3557 }
3558#endif
3559 /* If we don't have any way of figuring out the actual executable
3560 location then try argv[0]. */
3561 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003562 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003563 if (!p) {
3564 return NULL;
3565 }
3566 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003567
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003568 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003569}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003570#endif
3571
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003572static char*
3573qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
3574{
3575 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3576 char* buf = qemu_mallocz(len);
3577
3578 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3579 VERBOSE_PRINT(init," trying to find: %s\n", buf);
3580 if (access(buf, R_OK)) {
3581 qemu_free(buf);
3582 return NULL;
3583 }
3584 return buf;
3585}
3586
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003587char *qemu_find_file(int type, const char *name)
3588{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003589 const char *subdir;
3590 char *buf;
3591
3592 /* If name contains path separators then try it as a straight path. */
3593 if ((strchr(name, '/') || strchr(name, '\\'))
3594 && access(name, R_OK) == 0) {
3595 return strdup(name);
3596 }
3597 switch (type) {
3598 case QEMU_FILE_TYPE_BIOS:
3599 subdir = "";
3600 break;
3601 case QEMU_FILE_TYPE_KEYMAP:
3602 subdir = "keymaps/";
3603 break;
3604 default:
3605 abort();
3606 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003607 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
3608#ifdef CONFIG_ANDROID
3609 if (type == QEMU_FILE_TYPE_BIOS) {
3610 /* This case corresponds to the emulator being used as part of an
3611 * SDK installation. NOTE: data_dir is really $bindir. */
3612 if (buf == NULL)
3613 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
3614 /* This case corresponds to platform builds. */
3615 if (buf == NULL)
3616 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
3617 /* Finally, try this for standalone builds under external/qemu */
3618 if (buf == NULL)
3619 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003620 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003621#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003622 return buf;
3623}
3624
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003625static int
3626add_dns_server( const char* server_name )
3627{
3628 SockAddress addr;
3629
3630 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3631 fprintf(stdout,
3632 "### WARNING: can't resolve DNS server name '%s'\n",
3633 server_name );
3634 return -1;
3635 }
3636
3637 fprintf(stderr,
3638 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3639
3640 if ( slirp_add_dns_server( &addr ) < 0 ) {
3641 fprintf(stderr,
3642 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3643 return -1;
3644 }
3645 return 0;
3646}
3647
rich cannings7339b552011-02-16 13:43:44 -08003648/* Parses an integer
3649 * Pararm:
3650 * str String containing a number to be parsed.
3651 * result Passes the parsed integer in this argument
3652 * returns 0 if ok, -1 if failed
3653 */
3654int
3655parse_int(const char *str, int *result)
3656{
3657 char* r;
3658 *result = strtol(str, &r, 0);
3659 if (r == NULL || *r != '\0')
3660 return -1;
3661
3662 return 0;
3663}
3664
rich canningsd952f282011-03-01 15:40:09 -08003665#ifndef _WIN32
3666/*
3667 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3668 */
3669void init_qemu_clear_logs_sig() {
3670 struct sigaction act;
3671 sigfillset(&act.sa_mask);
3672 act.sa_flags = 0;
3673 act.sa_handler = rotate_qemu_logs_handler;
3674 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3675 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3676 exit(-1);
3677 }
3678}
3679#endif
3680
3681
rich cannings7339b552011-02-16 13:43:44 -08003682
3683/* parses a null-terminated string specifying a network port (e.g., "80") or
3684 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3685 * are the same. Returns 0 on success, -1 on error. */
3686
3687int parse_port_range(const char *str, unsigned short *lport,
3688 unsigned short *hport) {
3689
3690 unsigned int low = 0, high = 0;
3691 char *p, *arg = strdup(str);
3692
3693 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3694 p = arg + 1; /* skip '[' */
3695 low = atoi(strtok(p, "-"));
3696 high = atoi(strtok(NULL, "-"));
3697 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3698 *lport = low;
3699 *hport = high;
3700 }
3701 }
3702 else {
3703 low = atoi(arg);
3704 if ((0 < low) && (low < 65535)) {
3705 *lport = low;
3706 *hport = low;
3707 }
3708 }
3709 free(arg);
3710 if (low != 0)
3711 return 0;
3712 return -1;
3713}
3714
3715/*
3716 * Implements the generic port forwarding option
3717 */
3718void
3719net_slirp_forward(const char *optarg)
3720{
3721 /*
3722 * we expect the following format:
3723 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3724 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3725 */
3726 char *argument = strdup(optarg), *p = argument;
3727 char *dst_net, *dst_mask, *dst_port;
3728 char *redirect_ip, *redirect_port;
3729 uint32_t dnet, dmask, rip;
3730 unsigned short dlport, dhport, rport;
3731
3732
3733 dst_net = strtok(p, ":");
3734 dst_mask = strtok(NULL, ":");
3735 dst_port = strtok(NULL, ":");
3736 redirect_ip = strtok(NULL, ":");
3737 redirect_port = strtok(NULL, ":");
3738
3739 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3740 redirect_ip == NULL || redirect_port == NULL) {
3741 fprintf(stderr,
3742 "Invalid argument for -net-forward, we expect "
3743 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3744 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3745 ":redirect_ip:redirect_port: %s\n",
3746 optarg);
3747 exit(1);
3748 }
3749
3750 /* inet_strtoip converts dotted address to host byte order */
3751 if (inet_strtoip(dst_net, &dnet) == -1) {
3752 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3753 exit(1);
3754 }
3755 if (inet_strtoip(dst_mask, &dmask) == -1) {
3756 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3757 exit(1);
3758 }
3759 if (inet_strtoip(redirect_ip, &rip) == -1) {
3760 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3761 exit(1);
3762 }
3763
3764 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3765 fprintf(stderr, "Invalid destination port or port range\n");
3766 exit(1);
3767 }
3768
3769 rport = atoi(redirect_port);
3770 if (!rport) {
3771 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3772 exit(1);
3773 }
3774
3775 dnet &= dmask;
3776
3777 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3778 rip, rport);
3779
3780 free(argument);
3781}
3782
3783
3784/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3785 * entry in the allows list */
3786void
3787slirp_allow(const char *optarg, u_int8_t proto)
3788{
3789 /*
3790 * we expect the following format:
3791 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3792 */
3793 char *argument = strdup(optarg), *p = argument;
3794 char *dst_ip_str, *dst_port_str;
3795 uint32_t dst_ip;
3796 unsigned short dst_lport, dst_hport;
3797
3798 dst_ip_str = strtok(p, ":");
3799 dst_port_str = strtok(NULL, ":");
3800
3801 if (dst_ip_str == NULL || dst_port_str == NULL) {
3802 fprintf(stderr,
3803 "Invalid argument %s for -allow. We expect "
3804 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3805 optarg);
3806 exit(1);
3807 }
3808
3809 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3810 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3811 exit(1);
3812 }
3813 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3814 fprintf(stderr, "Invalid destination port or port range\n");
3815 exit(1);
3816 }
3817
3818 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3819
3820 free(argument);
3821}
3822
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003823/* Add a serial device at a given location in the emulated hardware table.
3824 * On failure, this function aborts the program with an error message.
3825 */
3826static void
3827serial_hds_add_at(int index, const char* devname)
3828{
3829 char label[32];
3830
3831 if (!devname || !strcmp(devname,"none"))
3832 return;
3833
3834 if (index >= MAX_SERIAL_PORTS) {
3835 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3836 devname, index, MAX_SERIAL_PORTS);
3837 }
3838 if (serial_hds[index] != NULL) {
3839 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3840 devname, index);
3841 }
3842 snprintf(label, sizeof(label), "serial%d", index);
3843 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3844 if (!serial_hds[index]) {
3845 PANIC("qemu: could not open serial device '%s'", devname);
3846 }
3847}
3848
3849
3850/* Find a free slot in the emulated serial device table, and register
3851 * it. Return the allocated table index.
3852 */
3853static int
3854serial_hds_add(const char* devname)
3855{
3856 int index;
3857
3858 /* Find first free slot */
3859 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3860 if (serial_hds[index] == NULL) {
3861 serial_hds_add_at(index, devname);
3862 return index;
3863 }
3864 }
3865
3866 PANIC("qemu: too many serial devices registered (%d)", index);
3867 return -1; /* shouldn't happen */
3868}
3869
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003870int main(int argc, char **argv, char **envp)
3871{
3872 const char *gdbstub_dev = NULL;
3873 uint32_t boot_devices_bitmap = 0;
3874 int i;
3875 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003876 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003877 const char *initrd_filename;
3878 const char *kernel_filename, *kernel_cmdline;
3879 const char *boot_devices = "";
3880 DisplayState *ds;
3881 DisplayChangeListener *dcl;
3882 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003883 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003884 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003885 const char *net_clients[MAX_NET_CLIENTS];
3886 int nb_net_clients;
3887 const char *bt_opts[MAX_BT_CMDLINE];
3888 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003889 int optind;
3890 const char *r, *optarg;
3891 CharDriverState *monitor_hd = NULL;
3892 const char *monitor_device;
3893 const char *serial_devices[MAX_SERIAL_PORTS];
3894 int serial_device_index;
3895 const char *parallel_devices[MAX_PARALLEL_PORTS];
3896 int parallel_device_index;
3897 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3898 int virtio_console_index;
3899 const char *loadvm = NULL;
3900 QEMUMachine *machine;
3901 const char *cpu_model;
3902 const char *usb_devices[MAX_USB_CMDLINE];
3903 int usb_devices_index;
3904#ifndef _WIN32
3905 int fds[2];
3906#endif
3907 int tb_size;
3908 const char *pid_file = NULL;
3909 const char *incoming = NULL;
3910#ifndef _WIN32
3911 int fd = 0;
3912 struct passwd *pwd = NULL;
3913 const char *chroot_dir = NULL;
3914 const char *run_as = NULL;
3915#endif
3916 CPUState *env;
3917 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003918 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003919 STRALLOC_DEFINE(kernel_params);
3920 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003921 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003922
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003923 /* Initialize sockets before anything else, so we can properly report
3924 * initialization failures back to the UI. */
3925#ifdef _WIN32
3926 socket_init();
3927#endif
3928
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003929 init_clocks();
3930
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003931 qemu_cache_utils_init(envp);
3932
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003933 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003934#ifndef _WIN32
3935 {
3936 struct sigaction act;
3937 sigfillset(&act.sa_mask);
3938 act.sa_flags = 0;
3939 act.sa_handler = SIG_IGN;
3940 sigaction(SIGPIPE, &act, NULL);
3941 }
3942#else
3943 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3944 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3945 QEMU to run on a single CPU */
3946 {
3947 HANDLE h;
3948 DWORD mask, smask;
3949 int i;
3950 h = GetCurrentProcess();
3951 if (GetProcessAffinityMask(h, &mask, &smask)) {
3952 for(i = 0; i < 32; i++) {
3953 if (mask & (1 << i))
3954 break;
3955 }
3956 if (i != 32) {
3957 mask = 1 << i;
3958 SetProcessAffinityMask(h, mask);
3959 }
3960 }
3961 }
3962#endif
3963
3964 module_call_init(MODULE_INIT_MACHINE);
3965 machine = find_default_machine();
3966 cpu_model = NULL;
3967 initrd_filename = NULL;
3968 ram_size = 0;
3969 snapshot = 0;
3970 kernel_filename = NULL;
3971 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003972
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003973 cyls = heads = secs = 0;
3974 translation = BIOS_ATA_TRANSLATION_AUTO;
3975 monitor_device = "vc:80Cx24C";
3976
3977 serial_devices[0] = "vc:80Cx24C";
3978 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3979 serial_devices[i] = NULL;
3980 serial_device_index = 0;
3981
3982 parallel_devices[0] = "vc:80Cx24C";
3983 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3984 parallel_devices[i] = NULL;
3985 parallel_device_index = 0;
3986
3987 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3988 virtio_consoles[i] = NULL;
3989 virtio_console_index = 0;
3990
3991 for (i = 0; i < MAX_NODES; i++) {
3992 node_mem[i] = 0;
3993 node_cpumask[i] = 0;
3994 }
3995
3996 usb_devices_index = 0;
3997
3998 nb_net_clients = 0;
3999 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004000#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004001 nb_drives = 0;
4002 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004003#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004004 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004005
4006 nb_nics = 0;
4007
4008 tb_size = 0;
4009 autostart= 1;
4010
4011 register_watchdogs();
4012
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004013 /* Initialize boot properties. */
4014 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02004015 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02004016 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004017
David 'Digit' Turner36597752011-05-20 01:18:01 +02004018#ifdef CONFIG_KVM
4019 /* By default, force auto-detection for kvm */
4020 kvm_allowed = -1;
4021#endif
4022
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004023 optind = 1;
4024 for(;;) {
4025 if (optind >= argc)
4026 break;
4027 r = argv[optind];
4028 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004029 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004030 } else {
4031 const QEMUOption *popt;
4032
4033 optind++;
4034 /* Treat --foo the same as -foo. */
4035 if (r[1] == '-')
4036 r++;
4037 popt = qemu_options;
4038 for(;;) {
4039 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004040 PANIC("%s: invalid option -- '%s'",
4041 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004042 }
4043 if (!strcmp(popt->name, r + 1))
4044 break;
4045 popt++;
4046 }
4047 if (popt->flags & HAS_ARG) {
4048 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004049 PANIC("%s: option '%s' requires an argument",
4050 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004051 }
4052 optarg = argv[optind++];
4053 } else {
4054 optarg = NULL;
4055 }
4056
4057 switch(popt->index) {
4058 case QEMU_OPTION_M:
4059 machine = find_machine(optarg);
4060 if (!machine) {
4061 QEMUMachine *m;
4062 printf("Supported machines are:\n");
4063 for(m = first_machine; m != NULL; m = m->next) {
4064 printf("%-10s %s%s\n",
4065 m->name, m->desc,
4066 m->is_default ? " (default)" : "");
4067 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004068 if (*optarg != '?') {
4069 PANIC("Invalid machine parameter: %s",
4070 optarg);
4071 } else {
4072 QEMU_EXIT(0);
4073 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004074 }
4075 break;
4076 case QEMU_OPTION_cpu:
4077 /* hw initialization will check this */
4078 if (*optarg == '?') {
4079/* XXX: implement xxx_cpu_list for targets that still miss it */
4080#if defined(cpu_list)
4081 cpu_list(stdout, &fprintf);
4082#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004083 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004084 } else {
4085 cpu_model = optarg;
4086 }
4087 break;
4088 case QEMU_OPTION_initrd:
4089 initrd_filename = optarg;
4090 break;
4091 case QEMU_OPTION_hda:
4092 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004093 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004094 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004095 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004096 ",cyls=%d,heads=%d,secs=%d%s",
4097 0, cyls, heads, secs,
4098 translation == BIOS_ATA_TRANSLATION_LBA ?
4099 ",trans=lba" :
4100 translation == BIOS_ATA_TRANSLATION_NONE ?
4101 ",trans=none" : "");
4102 break;
4103 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004104 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
4105 break;
4106
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004107 case QEMU_OPTION_hdc:
4108 case QEMU_OPTION_hdd:
4109 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4110 break;
4111 case QEMU_OPTION_drive:
4112 drive_add(NULL, "%s", optarg);
4113 break;
4114 case QEMU_OPTION_mtdblock:
4115 drive_add(optarg, MTD_ALIAS);
4116 break;
4117 case QEMU_OPTION_sd:
4118 drive_add(optarg, SD_ALIAS);
4119 break;
4120 case QEMU_OPTION_pflash:
4121 drive_add(optarg, PFLASH_ALIAS);
4122 break;
4123 case QEMU_OPTION_snapshot:
4124 snapshot = 1;
4125 break;
4126 case QEMU_OPTION_hdachs:
4127 {
4128 const char *p;
4129 p = optarg;
4130 cyls = strtol(p, (char **)&p, 0);
4131 if (cyls < 1 || cyls > 16383)
4132 goto chs_fail;
4133 if (*p != ',')
4134 goto chs_fail;
4135 p++;
4136 heads = strtol(p, (char **)&p, 0);
4137 if (heads < 1 || heads > 16)
4138 goto chs_fail;
4139 if (*p != ',')
4140 goto chs_fail;
4141 p++;
4142 secs = strtol(p, (char **)&p, 0);
4143 if (secs < 1 || secs > 63)
4144 goto chs_fail;
4145 if (*p == ',') {
4146 p++;
4147 if (!strcmp(p, "none"))
4148 translation = BIOS_ATA_TRANSLATION_NONE;
4149 else if (!strcmp(p, "lba"))
4150 translation = BIOS_ATA_TRANSLATION_LBA;
4151 else if (!strcmp(p, "auto"))
4152 translation = BIOS_ATA_TRANSLATION_AUTO;
4153 else
4154 goto chs_fail;
4155 } else if (*p != '\0') {
4156 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004157 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004158 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004159 if (hda_opts != NULL) {
4160 char num[16];
4161 snprintf(num, sizeof(num), "%d", cyls);
4162 qemu_opt_set(hda_opts, "cyls", num);
4163 snprintf(num, sizeof(num), "%d", heads);
4164 qemu_opt_set(hda_opts, "heads", num);
4165 snprintf(num, sizeof(num), "%d", secs);
4166 qemu_opt_set(hda_opts, "secs", num);
4167 if (translation == BIOS_ATA_TRANSLATION_LBA)
4168 qemu_opt_set(hda_opts, "trans", "lba");
4169 if (translation == BIOS_ATA_TRANSLATION_NONE)
4170 qemu_opt_set(hda_opts, "trans", "none");
4171 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004172 }
4173 break;
4174 case QEMU_OPTION_numa:
4175 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004176 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004177 }
4178 numa_add(optarg);
4179 break;
4180 case QEMU_OPTION_nographic:
4181 display_type = DT_NOGRAPHIC;
4182 break;
4183#ifdef CONFIG_CURSES
4184 case QEMU_OPTION_curses:
4185 display_type = DT_CURSES;
4186 break;
4187#endif
4188 case QEMU_OPTION_portrait:
4189 graphic_rotate = 1;
4190 break;
4191 case QEMU_OPTION_kernel:
4192 kernel_filename = optarg;
4193 break;
4194 case QEMU_OPTION_append:
4195 kernel_cmdline = optarg;
4196 break;
4197 case QEMU_OPTION_cdrom:
4198 drive_add(optarg, CDROM_ALIAS);
4199 break;
4200 case QEMU_OPTION_boot:
4201 boot_devices = optarg;
4202 /* We just do some generic consistency checks */
4203 {
4204 /* Could easily be extended to 64 devices if needed */
4205 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004206
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004207 boot_devices_bitmap = 0;
4208 for (p = boot_devices; *p != '\0'; p++) {
4209 /* Allowed boot devices are:
4210 * a b : floppy disk drives
4211 * c ... f : IDE disk drives
4212 * g ... m : machine implementation dependant drives
4213 * n ... p : network devices
4214 * It's up to each machine implementation to check
4215 * if the given boot devices match the actual hardware
4216 * implementation and firmware features.
4217 */
4218 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004219 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004220 }
4221 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004222 PANIC(
4223 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004224 }
4225 boot_devices_bitmap |= 1 << (*p - 'a');
4226 }
4227 }
4228 break;
4229 case QEMU_OPTION_fda:
4230 case QEMU_OPTION_fdb:
4231 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4232 break;
4233#ifdef TARGET_I386
4234 case QEMU_OPTION_no_fd_bootchk:
4235 fd_bootchk = 0;
4236 break;
4237#endif
4238 case QEMU_OPTION_net:
4239 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004240 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004241 }
4242 net_clients[nb_net_clients] = optarg;
4243 nb_net_clients++;
4244 break;
4245#ifdef CONFIG_SLIRP
4246 case QEMU_OPTION_tftp:
4247 tftp_prefix = optarg;
4248 break;
4249 case QEMU_OPTION_bootp:
4250 bootp_filename = optarg;
4251 break;
4252#if 0 /* ANDROID disabled */
4253#ifndef _WIN32
4254 case QEMU_OPTION_smb:
4255 net_slirp_smb(optarg);
4256 break;
4257#endif
4258#endif /* ANDROID */
4259 case QEMU_OPTION_redir:
4260 net_slirp_redir(NULL, optarg, NULL);
4261 break;
4262#endif
4263 case QEMU_OPTION_bt:
4264 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004265 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004266 }
4267 bt_opts[nb_bt_opts++] = optarg;
4268 break;
4269#ifdef HAS_AUDIO
4270 case QEMU_OPTION_audio_help:
4271 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004272 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004273 break;
4274 case QEMU_OPTION_soundhw:
4275 select_soundhw (optarg);
4276 break;
4277#endif
4278 case QEMU_OPTION_h:
4279 qemu_help(0);
4280 break;
4281 case QEMU_OPTION_version:
4282 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004283 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004284 break;
4285 case QEMU_OPTION_m: {
4286 uint64_t value;
4287 char *ptr;
4288
4289 value = strtoul(optarg, &ptr, 10);
4290 switch (*ptr) {
4291 case 0: case 'M': case 'm':
4292 value <<= 20;
4293 break;
4294 case 'G': case 'g':
4295 value <<= 30;
4296 break;
4297 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004298 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004299 }
4300
4301 /* On 32-bit hosts, QEMU is limited by virtual address space */
4302 if (value > (2047 << 20)
4303#ifndef CONFIG_KQEMU
4304 && HOST_LONG_BITS == 32
4305#endif
4306 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004307 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004308 }
4309 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004310 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004311 }
4312 ram_size = value;
4313 break;
4314 }
4315 case QEMU_OPTION_d:
4316 {
4317 int mask;
4318 const CPULogItem *item;
4319
4320 mask = cpu_str_to_log_mask(optarg);
4321 if (!mask) {
4322 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004323 for(item = cpu_log_items; item->mask != 0; item++) {
4324 printf("%-10s %s\n", item->name, item->help);
4325 }
4326 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004327 }
4328 cpu_set_log(mask);
4329 }
4330 break;
4331 case QEMU_OPTION_s:
4332 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4333 break;
4334 case QEMU_OPTION_gdb:
4335 gdbstub_dev = optarg;
4336 break;
4337 case QEMU_OPTION_L:
4338 data_dir = optarg;
4339 break;
4340 case QEMU_OPTION_bios:
4341 bios_name = optarg;
4342 break;
4343 case QEMU_OPTION_singlestep:
4344 singlestep = 1;
4345 break;
4346 case QEMU_OPTION_S:
4347#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004348 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004349#endif
4350 autostart = 0;
4351 break;
4352#ifndef _WIN32
4353 case QEMU_OPTION_k:
4354 keyboard_layout = optarg;
4355 break;
4356#endif
4357 case QEMU_OPTION_localtime:
4358 rtc_utc = 0;
4359 break;
4360 case QEMU_OPTION_vga:
4361 select_vgahw (optarg);
4362 break;
4363#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4364 case QEMU_OPTION_g:
4365 {
4366 const char *p;
4367 int w, h, depth;
4368 p = optarg;
4369 w = strtol(p, (char **)&p, 10);
4370 if (w <= 0) {
4371 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004372 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004373 }
4374 if (*p != 'x')
4375 goto graphic_error;
4376 p++;
4377 h = strtol(p, (char **)&p, 10);
4378 if (h <= 0)
4379 goto graphic_error;
4380 if (*p == 'x') {
4381 p++;
4382 depth = strtol(p, (char **)&p, 10);
4383 if (depth != 8 && depth != 15 && depth != 16 &&
4384 depth != 24 && depth != 32)
4385 goto graphic_error;
4386 } else if (*p == '\0') {
4387 depth = graphic_depth;
4388 } else {
4389 goto graphic_error;
4390 }
4391
4392 graphic_width = w;
4393 graphic_height = h;
4394 graphic_depth = depth;
4395 }
4396 break;
4397#endif
4398 case QEMU_OPTION_echr:
4399 {
4400 char *r;
4401 term_escape_char = strtol(optarg, &r, 0);
4402 if (r == optarg)
4403 printf("Bad argument to echr\n");
4404 break;
4405 }
4406 case QEMU_OPTION_monitor:
4407 monitor_device = optarg;
4408 break;
4409 case QEMU_OPTION_serial:
4410 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004411 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004412 }
4413 serial_devices[serial_device_index] = optarg;
4414 serial_device_index++;
4415 break;
4416 case QEMU_OPTION_watchdog:
4417 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004418 if (i > 0) {
4419 if (i == 1) {
4420 PANIC("Invalid watchdog parameter: %s",
4421 optarg);
4422 } else {
4423 QEMU_EXIT(0);
4424 }
4425 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004426 break;
4427 case QEMU_OPTION_watchdog_action:
4428 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004429 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004430 }
4431 break;
4432 case QEMU_OPTION_virtiocon:
4433 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004434 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004435 }
4436 virtio_consoles[virtio_console_index] = optarg;
4437 virtio_console_index++;
4438 break;
4439 case QEMU_OPTION_parallel:
4440 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004441 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004442 }
4443 parallel_devices[parallel_device_index] = optarg;
4444 parallel_device_index++;
4445 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004446 case QEMU_OPTION_loadvm:
4447 loadvm = optarg;
4448 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004449 case QEMU_OPTION_savevm_on_exit:
4450 savevm_on_exit = optarg;
4451 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004452 case QEMU_OPTION_full_screen:
4453 full_screen = 1;
4454 break;
4455#ifdef CONFIG_SDL
4456 case QEMU_OPTION_no_frame:
4457 no_frame = 1;
4458 break;
4459 case QEMU_OPTION_alt_grab:
4460 alt_grab = 1;
4461 break;
4462 case QEMU_OPTION_no_quit:
4463 no_quit = 1;
4464 break;
4465 case QEMU_OPTION_sdl:
4466 display_type = DT_SDL;
4467 break;
4468#endif
4469 case QEMU_OPTION_pidfile:
4470 pid_file = optarg;
4471 break;
4472#ifdef TARGET_I386
4473 case QEMU_OPTION_win2k_hack:
4474 win2k_install_hack = 1;
4475 break;
4476 case QEMU_OPTION_rtc_td_hack:
4477 rtc_td_hack = 1;
4478 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004479#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004480 case QEMU_OPTION_acpitable:
4481 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004482 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004483 }
4484 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004485#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004486 case QEMU_OPTION_smbios:
4487 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004488 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004489 }
4490 break;
4491#endif
4492#ifdef CONFIG_KQEMU
4493 case QEMU_OPTION_no_kqemu:
4494 kqemu_allowed = 0;
4495 break;
4496 case QEMU_OPTION_kernel_kqemu:
4497 kqemu_allowed = 2;
4498 break;
4499#endif
4500#ifdef CONFIG_KVM
4501 case QEMU_OPTION_enable_kvm:
4502 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004503 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02004504 case QEMU_OPTION_disable_kvm:
4505 kvm_allowed = 0;
4506 break;
4507#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004508 case QEMU_OPTION_usb:
4509 usb_enabled = 1;
4510 break;
4511 case QEMU_OPTION_usbdevice:
4512 usb_enabled = 1;
4513 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004514 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004515 }
4516 usb_devices[usb_devices_index] = optarg;
4517 usb_devices_index++;
4518 break;
4519 case QEMU_OPTION_smp:
4520 smp_cpus = atoi(optarg);
4521 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004522 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004523 }
4524 break;
4525 case QEMU_OPTION_vnc:
4526 display_type = DT_VNC;
4527 vnc_display = optarg;
4528 break;
4529#ifdef TARGET_I386
4530 case QEMU_OPTION_no_acpi:
4531 acpi_enabled = 0;
4532 break;
4533 case QEMU_OPTION_no_hpet:
4534 no_hpet = 1;
4535 break;
4536 case QEMU_OPTION_no_virtio_balloon:
4537 no_virtio_balloon = 1;
4538 break;
4539#endif
4540 case QEMU_OPTION_no_reboot:
4541 no_reboot = 1;
4542 break;
4543 case QEMU_OPTION_no_shutdown:
4544 no_shutdown = 1;
4545 break;
4546 case QEMU_OPTION_show_cursor:
4547 cursor_hide = 0;
4548 break;
4549 case QEMU_OPTION_uuid:
4550 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004551 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004552 }
4553 break;
4554#ifndef _WIN32
4555 case QEMU_OPTION_daemonize:
4556 daemonize = 1;
4557 break;
4558#endif
4559 case QEMU_OPTION_option_rom:
4560 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004561 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004562 }
4563 option_rom[nb_option_roms] = optarg;
4564 nb_option_roms++;
4565 break;
4566#if defined(TARGET_ARM) || defined(TARGET_M68K)
4567 case QEMU_OPTION_semihosting:
4568 semihosting_enabled = 1;
4569 break;
4570#endif
4571 case QEMU_OPTION_name:
4572 qemu_name = optarg;
4573 break;
4574#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4575 case QEMU_OPTION_prom_env:
4576 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004577 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004578 }
4579 prom_envs[nb_prom_envs] = optarg;
4580 nb_prom_envs++;
4581 break;
4582#endif
4583#ifdef TARGET_ARM
4584 case QEMU_OPTION_old_param:
4585 old_param = 1;
4586 break;
4587#endif
4588 case QEMU_OPTION_clock:
4589 configure_alarms(optarg);
4590 break;
4591 case QEMU_OPTION_startdate:
4592 {
4593 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004594 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004595 if (!strcmp(optarg, "now")) {
4596 rtc_date_offset = -1;
4597 } else {
4598 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4599 &tm.tm_year,
4600 &tm.tm_mon,
4601 &tm.tm_mday,
4602 &tm.tm_hour,
4603 &tm.tm_min,
4604 &tm.tm_sec) == 6) {
4605 /* OK */
4606 } else if (sscanf(optarg, "%d-%d-%d",
4607 &tm.tm_year,
4608 &tm.tm_mon,
4609 &tm.tm_mday) == 3) {
4610 tm.tm_hour = 0;
4611 tm.tm_min = 0;
4612 tm.tm_sec = 0;
4613 } else {
4614 goto date_fail;
4615 }
4616 tm.tm_year -= 1900;
4617 tm.tm_mon--;
4618 rtc_start_date = mktimegm(&tm);
4619 if (rtc_start_date == -1) {
4620 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004621 PANIC("Invalid date format. Valid format are:\n"
4622 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004623 }
4624 rtc_date_offset = time(NULL) - rtc_start_date;
4625 }
4626 }
4627 break;
rich cannings7339b552011-02-16 13:43:44 -08004628
4629 /* -------------------------------------------------------*/
4630 /* User mode network stack restrictions */
4631 case QEMU_OPTION_drop_udp:
4632 slirp_drop_udp();
4633 break;
4634 case QEMU_OPTION_drop_tcp:
4635 slirp_drop_tcp();
4636 break;
4637 case QEMU_OPTION_allow_tcp:
4638 slirp_allow(optarg, IPPROTO_TCP);
4639 break;
4640 case QEMU_OPTION_allow_udp:
4641 slirp_allow(optarg, IPPROTO_UDP);
4642 break;
4643 case QEMU_OPTION_drop_log:
4644 {
4645 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004646 drop_log_filename = optarg;
4647 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004648
4649 if (!drop_log_fd) {
4650 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4651 exit(1);
4652 }
4653
4654 slirp_drop_log_fd(drop_log_fd);
4655 }
4656 break;
4657
4658 case QEMU_OPTION_dns_log:
4659 {
4660 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004661 dns_log_filename = optarg;
4662 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004663
4664 if (dns_log_fd == NULL) {
4665 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4666 exit(1);
4667 }
4668
4669 slirp_dns_log_fd(dns_log_fd);
4670 }
4671 break;
4672
4673
4674 case QEMU_OPTION_max_dns_conns:
4675 {
4676 int max_dns_conns = 0;
4677 if (parse_int(optarg, &max_dns_conns)) {
4678 fprintf(stderr,
4679 "qemu: syntax: -max-dns-conns max_connections\n");
4680 exit(1);
4681 }
4682 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4683 fprintf(stderr,
4684 "Invalid arg for max dns connections: %s\n",
4685 optarg);
4686 exit(1);
4687 }
4688 slirp_set_max_dns_conns(max_dns_conns);
4689 }
4690 break;
4691
4692 case QEMU_OPTION_net_forward:
4693 net_slirp_forward(optarg);
4694 break;
4695 case QEMU_OPTION_net_forward_tcp2sink:
4696 {
4697 SockAddress saddr;
4698
4699 if (parse_host_port(&saddr, optarg)) {
4700 fprintf(stderr,
4701 "Invalid ip/port %s for "
4702 "-forward-dropped-tcp2sink. "
4703 "We expect 'sink_ip:sink_port'\n",
4704 optarg);
4705 exit(1);
4706 }
4707 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4708 saddr.u.inet.port);
4709 }
4710 break;
4711 /* -------------------------------------------------------*/
4712
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004713 case QEMU_OPTION_tb_size:
4714 tb_size = strtol(optarg, NULL, 0);
4715 if (tb_size < 0)
4716 tb_size = 0;
4717 break;
4718 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004719 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004720 break;
4721 case QEMU_OPTION_incoming:
4722 incoming = optarg;
4723 break;
4724#ifndef _WIN32
4725 case QEMU_OPTION_chroot:
4726 chroot_dir = optarg;
4727 break;
4728 case QEMU_OPTION_runas:
4729 run_as = optarg;
4730 break;
4731#endif
4732#ifdef CONFIG_XEN
4733 case QEMU_OPTION_xen_domid:
4734 xen_domid = atoi(optarg);
4735 break;
4736 case QEMU_OPTION_xen_create:
4737 xen_mode = XEN_CREATE;
4738 break;
4739 case QEMU_OPTION_xen_attach:
4740 xen_mode = XEN_ATTACH;
4741 break;
4742#endif
4743
4744
4745 case QEMU_OPTION_mic:
4746 audio_input_source = (char*)optarg;
4747 break;
4748#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004749 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004750 trace_filename = optarg;
4751 tracing = 1;
4752 break;
4753#if 0
4754 case QEMU_OPTION_trace_miss:
4755 trace_cache_miss = 1;
4756 break;
4757 case QEMU_OPTION_trace_addr:
4758 trace_all_addr = 1;
4759 break;
4760#endif
4761 case QEMU_OPTION_tracing:
4762 if (strcmp(optarg, "off") == 0)
4763 tracing = 0;
4764 else if (strcmp(optarg, "on") == 0 && trace_filename)
4765 tracing = 1;
4766 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004767 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004768 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004769 }
4770 break;
4771#if 0
4772 case QEMU_OPTION_dcache_load_miss:
4773 dcache_load_miss_penalty = atoi(optarg);
4774 break;
4775 case QEMU_OPTION_dcache_store_miss:
4776 dcache_store_miss_penalty = atoi(optarg);
4777 break;
4778#endif
4779#endif
4780#ifdef CONFIG_NAND
4781 case QEMU_OPTION_nand:
4782 nand_add_dev(optarg);
4783 break;
4784#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004785 case QEMU_OPTION_android_ports:
4786 android_op_ports = (char*)optarg;
4787 break;
4788
4789 case QEMU_OPTION_android_port:
4790 android_op_port = (char*)optarg;
4791 break;
4792
4793 case QEMU_OPTION_android_report_console:
4794 android_op_report_console = (char*)optarg;
4795 break;
4796
4797 case QEMU_OPTION_http_proxy:
4798 op_http_proxy = (char*)optarg;
4799 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004800
4801 case QEMU_OPTION_charmap:
4802 op_charmap_file = (char*)optarg;
4803 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004804
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004805 case QEMU_OPTION_android_hw:
4806 android_op_hwini = (char*)optarg;
4807 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004808
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004809 case QEMU_OPTION_dns_server:
4810 android_op_dns_server = (char*)optarg;
4811 break;
4812
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004813 case QEMU_OPTION_radio:
4814 android_op_radio = (char*)optarg;
4815 break;
4816
4817 case QEMU_OPTION_gps:
4818 android_op_gps = (char*)optarg;
4819 break;
4820
4821 case QEMU_OPTION_audio:
4822 android_op_audio = (char*)optarg;
4823 break;
4824
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004825 case QEMU_OPTION_cpu_delay:
4826 android_op_cpu_delay = (char*)optarg;
4827 break;
4828
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004829 case QEMU_OPTION_show_kernel:
4830 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4831 break;
4832
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004833#ifdef CONFIG_NAND_LIMITS
4834 case QEMU_OPTION_nand_limits:
4835 android_op_nand_limits = (char*)optarg;
4836 break;
4837#endif // CONFIG_NAND_LIMITS
4838
4839 case QEMU_OPTION_netspeed:
4840 android_op_netspeed = (char*)optarg;
4841 break;
4842
4843 case QEMU_OPTION_netdelay:
4844 android_op_netdelay = (char*)optarg;
4845 break;
4846
4847 case QEMU_OPTION_netfast:
4848 android_op_netfast = 1;
4849 break;
4850
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004851 case QEMU_OPTION_tcpdump:
4852 android_op_tcpdump = (char*)optarg;
4853 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004854
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004855 case QEMU_OPTION_boot_property:
4856 boot_property_parse_option((char*)optarg);
4857 break;
4858
4859 case QEMU_OPTION_lcd_density:
4860 android_op_lcd_density = (char*)optarg;
4861 break;
4862
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004863 case QEMU_OPTION_ui_port:
4864 android_op_ui_port = (char*)optarg;
4865 break;
4866
4867 case QEMU_OPTION_ui_settings:
4868 android_op_ui_settings = (char*)optarg;
4869 break;
4870
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004871 case QEMU_OPTION_audio_test_out:
4872 android_audio_test_start_out();
4873 break;
4874
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004875 case QEMU_OPTION_android_avdname:
4876 android_op_avd_name = (char*)optarg;
4877 break;
4878
4879 case QEMU_OPTION_timezone:
4880 if (timezone_set((char*)optarg)) {
4881 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4882 (char*)optarg);
4883 }
4884 break;
4885
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004886#ifdef CONFIG_MEMCHECK
4887 case QEMU_OPTION_android_memcheck:
4888 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004889 /* This will set ro.kernel.memcheck system property
4890 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004891 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004892 break;
4893#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004894
4895 case QEMU_OPTION_snapshot_no_time_update:
4896 android_snapshot_update_time = 0;
4897 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004898 }
4899 }
4900 }
4901
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004902 /* Initialize character map. */
4903 if (android_charmap_setup(op_charmap_file)) {
4904 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004905 PANIC(
4906 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004907 op_charmap_file);
4908 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004909 PANIC(
4910 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004911 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004912 }
4913
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004914 /* If no data_dir is specified then try to find it relative to the
4915 executable path. */
4916 if (!data_dir) {
4917 data_dir = find_datadir(argv[0]);
4918 }
4919 /* If all else fails use the install patch specified when building. */
4920 if (!data_dir) {
4921 data_dir = CONFIG_QEMU_SHAREDIR;
4922 }
4923
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004924 if (!android_op_hwini) {
4925 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004926 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004927 hw_ini = iniFile_newFromFile(android_op_hwini);
4928 if (hw_ini == NULL) {
4929 PANIC("Could not find %s file.", android_op_hwini);
4930 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004931
4932 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004933 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004934
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004935 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004936
4937 {
4938 int width = android_hw->hw_lcd_width;
4939 int height = android_hw->hw_lcd_height;
4940 int depth = android_hw->hw_lcd_depth;
4941
4942 /* A bit of sanity checking */
4943 if (width <= 0 || height <= 0 ||
4944 (depth != 16 && depth != 32) ||
4945 (((width|height) & 3) != 0) )
4946 {
4947 PANIC("Invalid display configuration (%d,%d,%d)",
4948 width, height, depth);
4949 }
4950 android_display_width = width;
4951 android_display_height = height;
4952 android_display_bpp = depth;
4953 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004954
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004955#ifdef CONFIG_NAND_LIMITS
4956 /* Init nand stuff. */
4957 if (android_op_nand_limits) {
4958 parse_nand_limits(android_op_nand_limits);
4959 }
4960#endif // CONFIG_NAND_LIMITS
4961
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004962 /* Initialize AVD name from hardware configuration if needed */
4963 if (!android_op_avd_name) {
4964 if (android_hw->avd_name && *android_hw->avd_name) {
4965 android_op_avd_name = android_hw->avd_name;
4966 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
4967 }
4968 }
4969
David 'Digit' Turner40841b22011-03-01 14:04:00 +01004970 /* Initialize system partition image */
4971 {
4972 char tmp[PATH_MAX+32];
4973 const char* sysImage = android_hw->disk_systemPartition_path;
4974 const char* initImage = android_hw->disk_systemPartition_initPath;
4975 uint64_t sysBytes = android_hw->disk_systemPartition_size;
4976
4977 if (sysBytes == 0) {
4978 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
4979 }
4980
4981 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
4982
4983 if (sysImage && *sysImage) {
4984 if (filelock_create(sysImage) == NULL) {
4985 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
4986 /* If there is no file= parameters, nand_add_dev will create
4987 * a temporary file to back the partition image. */
4988 } else {
4989 pstrcat(tmp,sizeof(tmp),",file=");
4990 pstrcat(tmp,sizeof(tmp),sysImage);
4991 }
4992 }
4993 if (initImage && *initImage) {
4994 if (!path_exists(initImage)) {
4995 PANIC("Invalid initial system image path: %s", initImage);
4996 }
4997 pstrcat(tmp,sizeof(tmp),",initfile=");
4998 pstrcat(tmp,sizeof(tmp),initImage);
4999 } else {
5000 PANIC("Missing initial system image path!");
5001 }
5002 nand_add_dev(tmp);
5003 }
5004
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005005 /* Initialize data partition image */
5006 {
5007 char tmp[PATH_MAX+32];
5008 const char* dataImage = android_hw->disk_dataPartition_path;
5009 const char* initImage = android_hw->disk_dataPartition_initPath;
5010 uint64_t dataBytes = android_hw->disk_dataPartition_size;
5011
5012 if (dataBytes == 0) {
5013 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
5014 }
5015
5016 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
5017
5018 if (dataImage && *dataImage) {
5019 if (filelock_create(dataImage) == NULL) {
5020 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
5021 /* Note: if there is no file= parameters, nand_add_dev() will
5022 * create a temporary file to back the partition image. */
5023 } else {
5024 /* Create the file if needed */
5025 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005026 if (path_empty_file(dataImage) < 0) {
5027 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
5028 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005029 }
5030 pstrcat(tmp, sizeof(tmp), ",file=");
5031 pstrcat(tmp, sizeof(tmp), dataImage);
5032 }
5033 }
5034 if (initImage && *initImage) {
5035 pstrcat(tmp, sizeof(tmp), ",initfile=");
5036 pstrcat(tmp, sizeof(tmp), initImage);
5037 }
5038 nand_add_dev(tmp);
5039 }
5040
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005041 /* Init SD-Card stuff. For Android, it is always hda */
5042 /* If the -hda option was used, ignore the Android-provided one */
5043 if (hda_opts == NULL) {
5044 const char* sdPath = android_hw->hw_sdCard_path;
5045 if (sdPath && *sdPath) {
5046 if (!path_exists(sdPath)) {
5047 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
5048 } else if (filelock_create(sdPath) == NULL) {
5049 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
5050 } else {
5051 /* Successful locking */
5052 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005053 /* Set this property of any operation involving the SD Card
5054 * will be x100 slower, due to the corresponding file being
5055 * mounted as O_DIRECT. Note that this is only 'unsafe' in
5056 * the context of an emulator crash. The data is already
5057 * synced properly when the emulator exits (either normally or through ^C).
5058 */
5059 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005060 }
5061 }
5062 }
5063
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005064 if (hdb_opts == NULL) {
5065 const char* spath = android_hw->disk_snapStorage_path;
5066 if (spath && *spath) {
5067 if (!path_exists(spath)) {
5068 PANIC("Snapshot storage file does not exist: %s", spath);
5069 }
5070 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005071 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005072 }
5073 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005074 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02005075 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005076 }
5077 }
5078
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005079 /* Set the VM's max heap size, passed as a boot property */
5080 if (android_hw->vm_heapSize > 0) {
5081 char tmp[64];
5082 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
5083 boot_property_add("dalvik.vm.heapsize",tmp);
5084 }
5085
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005086 /* Initialize net speed and delays stuff. */
5087 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005088 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005089 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005090 }
5091
5092 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005093 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005094 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005095 }
5096
5097 if (android_op_netfast) {
5098 qemu_net_download_speed = 0;
5099 qemu_net_upload_speed = 0;
5100 qemu_net_min_latency = 0;
5101 qemu_net_max_latency = 0;
5102 }
5103
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005104 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005105 if (android_hw->hw_lcd_density) {
5106 long density = android_hw->hw_lcd_density;
5107 if (density <= 0) {
5108 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005109 }
5110 hwLcd_setBootProperty(density);
5111 }
5112
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07005113 /* Initialize TCP dump */
5114 if (android_op_tcpdump) {
5115 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
5116 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
5117 }
5118 }
5119
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005120 /* Initialize modem */
5121 if (android_op_radio) {
5122 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
5123 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005124 PANIC("unsupported character device specification: %s\n"
5125 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005126 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005127 }
5128 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
5129 } else if (android_hw->hw_gsmModem != 0 ) {
5130 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005131 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005132 }
5133 }
5134
5135 /* Initialize GPS */
5136 if (android_op_gps) {
5137 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
5138 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005139 PANIC("unsupported character device specification: %s\n"
5140 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005141 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005142 }
5143 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
5144 } else if (android_hw->hw_gps != 0) {
5145 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005146 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005147 }
5148 }
5149
5150 /* Initialize audio. */
5151 if (android_op_audio) {
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005152 char temp[128];
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005153 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005154 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005155 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005156 }
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005157 snprintf(temp, sizeof temp, "QEMU_AUDIO_DRV=%s", android_op_audio);
5158 putenv(temp);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005159 }
5160
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005161 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02005162 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005163
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005164 if (android_op_cpu_delay) {
5165 char* end;
5166 long delay = strtol(android_op_cpu_delay, &end, 0);
5167 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005168 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005169 }
5170 if (delay > 0)
5171 delay = (1000-delay);
5172
5173 qemu_cpu_delay = (int) delay;
5174 }
5175
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005176 if (android_op_dns_server) {
5177 char* x = strchr(android_op_dns_server, ',');
5178 dns_count = 0;
5179 if (x == NULL)
5180 {
5181 if ( add_dns_server( android_op_dns_server ) == 0 )
5182 dns_count = 1;
5183 }
5184 else
5185 {
5186 x = android_op_dns_server;
5187 while (*x) {
5188 char* y = strchr(x, ',');
5189
5190 if (y != NULL) {
5191 *y = 0;
5192 y++;
5193 } else {
5194 y = x + strlen(x);
5195 }
5196
5197 if (y > x && add_dns_server( x ) == 0) {
5198 dns_count += 1;
5199 }
5200 x = y;
5201 }
5202 }
5203 if (dns_count == 0)
5204 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5205 }
5206
5207 if (dns_count == 0)
5208 dns_count = slirp_get_system_dns_servers();
5209 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005210 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005211 }
5212
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005213#ifdef CONFIG_MEMCHECK
5214 if (android_op_memcheck) {
5215 memcheck_init(android_op_memcheck);
5216 }
5217#endif // CONFIG_MEMCHECK
5218
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005219 /* Initialize cache partition, if any */
5220 if (android_hw->disk_cachePartition != 0) {
5221 char tmp[PATH_MAX+32];
5222 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005223 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005224
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005225 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5226
5227 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5228 if (filelock_create(partPath) == NULL) {
5229 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5230 /* Note: if there is no file= parameters, nand_add_dev() will
5231 * create a temporary file to back the partition image. */
5232 } else {
5233 /* Create the file if needed */
5234 if (!path_exists(partPath)) {
5235 if (path_empty_file(partPath) < 0) {
5236 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5237 }
5238 }
5239 pstrcat(tmp, sizeof(tmp), ",file=");
5240 pstrcat(tmp, sizeof(tmp), partPath);
5241 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005242 }
5243 nand_add_dev(tmp);
5244 }
5245
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005246 /* We always force qemu=1 when running inside QEMU */
5247 stralloc_add_str(kernel_params, " qemu=1");
5248
5249 /* We always initialize the first serial port for the android-kmsg
5250 * character device (used to send kernel messages) */
5251 serial_hds_add_at(0, "android-kmsg");
5252 stralloc_add_str(kernel_params, " console=ttyS0");
5253
5254 /* We always initialize the second serial port for the android-qemud
5255 * character device as well */
5256 serial_hds_add_at(1, "android-qemud");
5257 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5258
David 'Digit' Turner36597752011-05-20 01:18:01 +02005259#if defined(CONFIG_KVM)
5260 if (kvm_allowed < 0) {
5261 kvm_allowed = kvm_check_allowed();
5262 }
5263#endif
5264
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005265#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5266 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005267 PANIC(
5268 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005269 }
5270#endif
5271
5272 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5273 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005274 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5275 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005276 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005277 }
5278
5279 if (display_type == DT_NOGRAPHIC) {
5280 if (serial_device_index == 0)
5281 serial_devices[0] = "stdio";
5282 if (parallel_device_index == 0)
5283 parallel_devices[0] = "null";
5284 if (strncmp(monitor_device, "vc", 2) == 0)
5285 monitor_device = "stdio";
5286 }
5287
5288#ifndef _WIN32
5289 if (daemonize) {
5290 pid_t pid;
5291
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005292 if (pipe(fds) == -1) {
5293 PANIC("Unable to aquire pidfile");
5294 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005295
5296 pid = fork();
5297 if (pid > 0) {
5298 uint8_t status;
5299 ssize_t len;
5300
5301 close(fds[1]);
5302
5303 again:
5304 len = read(fds[0], &status, 1);
5305 if (len == -1 && (errno == EINTR))
5306 goto again;
5307
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005308 if (len != 1) {
5309 PANIC("Error when aquiring pidfile");
5310 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005311 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005312 PANIC("Could not acquire pidfile");
5313 } else {
5314 QEMU_EXIT(0);
5315 }
5316 } else if (pid < 0) {
5317 PANIC("Unable to daemonize");
5318 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005319
5320 setsid();
5321
5322 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005323 if (pid > 0) {
5324 QEMU_EXIT(0);
5325 } else if (pid < 0) {
5326 PANIC("Could not acquire pid file");
5327 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005328
5329 umask(027);
5330
5331 signal(SIGTSTP, SIG_IGN);
5332 signal(SIGTTOU, SIG_IGN);
5333 signal(SIGTTIN, SIG_IGN);
5334 }
5335
5336 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5337 if (daemonize) {
5338 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005339 int ret;
5340 do {
5341 ret = write(fds[1], &status, 1);
5342 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005343 PANIC("Could not acquire pid file");
5344 } else {
5345 PANIC("Could not acquire pid file");
5346 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005347 }
5348#endif
5349
5350#ifdef CONFIG_KQEMU
5351 if (smp_cpus > 1)
5352 kqemu_allowed = 0;
5353#endif
5354 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005355 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005356 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005357
5358 if (kernel_filename == NULL) {
5359 kernel_filename = android_hw->kernel_path;
5360 }
5361 if (initrd_filename == NULL) {
5362 initrd_filename = android_hw->disk_ramdisk_path;
5363 }
5364
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005365 linux_boot = (kernel_filename != NULL);
5366 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5367
5368 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005369 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005370 }
5371
5372 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005373 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005374 }
5375
5376 /* boot to floppy or the default cd if no hard disk defined yet */
5377 if (!boot_devices[0]) {
5378 boot_devices = "cad";
5379 }
5380 setvbuf(stdout, NULL, _IOLBF, 0);
5381
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005382 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005383 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005384 }
David Turner6a9ef172010-09-09 22:54:36 +02005385 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005386
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005387 /* init network clients */
5388 if (nb_net_clients == 0) {
5389 /* if no clients, we use a default config */
5390 net_clients[nb_net_clients++] = "nic";
5391#ifdef CONFIG_SLIRP
5392 net_clients[nb_net_clients++] = "user";
5393#endif
5394 }
5395
5396 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005397 if (net_client_parse(net_clients[i]) < 0) {
5398 PANIC("Unable to parse net clients");
5399 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005400 }
5401 net_client_check();
5402
5403#ifdef TARGET_I386
5404 /* XXX: this should be moved in the PC machine instantiation code */
5405 if (net_boot != 0) {
5406 int netroms = 0;
5407 for (i = 0; i < nb_nics && i < 4; i++) {
5408 const char *model = nd_table[i].model;
5409 char buf[1024];
5410 char *filename;
5411 if (net_boot & (1 << i)) {
5412 if (model == NULL)
5413 model = "ne2k_pci";
5414 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5415 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5416 if (filename && get_image_size(filename) > 0) {
5417 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005418 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005419 }
5420 option_rom[nb_option_roms] = qemu_strdup(buf);
5421 nb_option_roms++;
5422 netroms++;
5423 }
5424 if (filename) {
5425 qemu_free(filename);
5426 }
5427 }
5428 }
5429 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005430 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005431 }
5432 }
5433#endif
5434
5435 /* init the bluetooth world */
5436 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005437 if (bt_parse(bt_opts[i])) {
5438 PANIC("Unable to parse bluetooth options");
5439 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005440
5441 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005442 if (ram_size == 0) {
5443 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5444 if (ram_size == 0) {
5445 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5446 }
5447 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005448
5449#ifdef CONFIG_KQEMU
5450 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5451 guest ram allocation. It needs to go away. */
5452 if (kqemu_allowed) {
5453 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5454 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5455 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005456 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005457 }
5458 }
5459#endif
5460
rich canningsd952f282011-03-01 15:40:09 -08005461#ifndef _WIN32
5462 init_qemu_clear_logs_sig();
5463#endif
5464
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005465 /* init the dynamic translator */
5466 cpu_exec_init_all(tb_size * 1024 * 1024);
5467
5468 bdrv_init();
5469
5470 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005471#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005472 if (nb_drives_opt < MAX_DRIVES)
5473 drive_add(NULL, CDROM_ALIAS);
5474
5475 /* we always create at least one floppy */
5476
5477 if (nb_drives_opt < MAX_DRIVES)
5478 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005479 /* we always create one sd slot, even if no card is in it */
5480
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005481 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005482 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005483 }
5484#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005485
5486 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005487 if (snapshot)
5488 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5489 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5490 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005491
David Turner6a9ef172010-09-09 22:54:36 +02005492 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005493 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5494
5495#ifndef _WIN32
5496 /* must be after terminal init, SDL library changes signal handlers */
5497 sighandler_setup();
5498#endif
5499
5500 /* Maintain compatibility with multiple stdio monitors */
5501 if (!strcmp(monitor_device,"stdio")) {
5502 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5503 const char *devname = serial_devices[i];
5504 if (devname && !strcmp(devname,"mon:stdio")) {
5505 monitor_device = NULL;
5506 break;
5507 } else if (devname && !strcmp(devname,"stdio")) {
5508 monitor_device = NULL;
5509 serial_devices[i] = "mon:stdio";
5510 break;
5511 }
5512 }
5513 }
5514
5515 if (nb_numa_nodes > 0) {
5516 int i;
5517
5518 if (nb_numa_nodes > smp_cpus) {
5519 nb_numa_nodes = smp_cpus;
5520 }
5521
5522 /* If no memory size if given for any node, assume the default case
5523 * and distribute the available memory equally across all nodes
5524 */
5525 for (i = 0; i < nb_numa_nodes; i++) {
5526 if (node_mem[i] != 0)
5527 break;
5528 }
5529 if (i == nb_numa_nodes) {
5530 uint64_t usedmem = 0;
5531
5532 /* On Linux, the each node's border has to be 8MB aligned,
5533 * the final node gets the rest.
5534 */
5535 for (i = 0; i < nb_numa_nodes - 1; i++) {
5536 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5537 usedmem += node_mem[i];
5538 }
5539 node_mem[i] = ram_size - usedmem;
5540 }
5541
5542 for (i = 0; i < nb_numa_nodes; i++) {
5543 if (node_cpumask[i] != 0)
5544 break;
5545 }
5546 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5547 * must cope with this anyway, because there are BIOSes out there in
5548 * real machines which also use this scheme.
5549 */
5550 if (i == nb_numa_nodes) {
5551 for (i = 0; i < smp_cpus; i++) {
5552 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5553 }
5554 }
5555 }
5556
5557 if (kvm_enabled()) {
5558 int ret;
5559
5560 ret = kvm_init(smp_cpus);
5561 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005562 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005563 }
5564 }
5565
5566 if (monitor_device) {
5567 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5568 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005569 PANIC("qemu: could not open monitor device '%s'",
5570 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005571 }
5572 }
5573
5574 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005575 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005576 }
5577
5578 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5579 const char *devname = parallel_devices[i];
5580 if (devname && strcmp(devname, "none")) {
5581 char label[32];
5582 snprintf(label, sizeof(label), "parallel%d", i);
5583 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5584 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005585 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005586 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005587 }
5588 }
5589 }
5590
5591 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5592 const char *devname = virtio_consoles[i];
5593 if (devname && strcmp(devname, "none")) {
5594 char label[32];
5595 snprintf(label, sizeof(label), "virtcon%d", i);
5596 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5597 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005598 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005599 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005600 }
5601 }
5602 }
5603
5604 module_call_init(MODULE_INIT_DEVICE);
5605
5606
5607#ifdef CONFIG_TRACE
5608 if (trace_filename) {
5609 trace_init(trace_filename);
5610#if 0
5611 // We don't need the dcache code until we can get load and store tracing
5612 // working again.
5613 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5614 dcache_replace_policy, dcache_load_miss_penalty,
5615 dcache_store_miss_penalty);
5616#endif
5617 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5618 }
5619#endif
5620
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005621 /* Check the CPU Architecture value */
5622#if defined(TARGET_ARM)
5623 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5624 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5625 android_hw->hw_cpu_arch);
5626 exit(1);
5627 }
5628#elif defined(TARGET_X86)
5629 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5630 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5631 android_hw->hw_cpu_arch);
5632 exit(1);
5633 }
5634#endif
5635
5636 /* Grab CPU model if provided in hardware.ini */
5637 if ( !cpu_model
5638 && android_hw->hw_cpu_model
5639 && android_hw->hw_cpu_model[0] != '\0')
5640 {
5641 cpu_model = android_hw->hw_cpu_model;
5642 }
5643
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005644 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005645 * collected during initialization.
5646 *
5647 * The order is the following:
5648 * - parameters from the hw configuration (kernel.parameters)
5649 * - additionnal parameters from options (e.g. -memcheck)
5650 * - the -append parameters.
5651 */
5652 {
5653 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005654
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005655 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005656 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005657 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5658 }
5659
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005660 /* If not empty, kernel_config always contains a leading space */
5661 stralloc_append(kernel_params, kernel_config);
5662
5663 if (*kernel_cmdline) {
5664 stralloc_add_c(kernel_params, ' ');
5665 stralloc_add_str(kernel_params, kernel_cmdline);
5666 }
5667
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005668 /* Remove any leading/trailing spaces */
5669 stralloc_strip(kernel_params);
5670
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005671 kernel_parameters = stralloc_cstr(kernel_params);
5672 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5673
5674 machine->init(ram_size,
5675 boot_devices,
5676 kernel_filename,
5677 kernel_parameters,
5678 initrd_filename,
5679 cpu_model);
5680
5681 stralloc_reset(kernel_params);
5682 stralloc_reset(kernel_config);
5683 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005684
5685
5686 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5687 for (i = 0; i < nb_numa_nodes; i++) {
5688 if (node_cpumask[i] & (1 << env->cpu_index)) {
5689 env->numa_node = i;
5690 }
5691 }
5692 }
5693
5694 current_machine = machine;
5695
5696 /* Set KVM's vcpu state to qemu's initial CPUState. */
5697 if (kvm_enabled()) {
5698 int ret;
5699
5700 ret = kvm_sync_vcpus();
5701 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005702 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005703 }
5704 }
5705
5706 /* init USB devices */
5707 if (usb_enabled) {
5708 for(i = 0; i < usb_devices_index; i++) {
5709 if (usb_device_add(usb_devices[i], 0) < 0) {
5710 fprintf(stderr, "Warning: could not add USB device %s\n",
5711 usb_devices[i]);
5712 }
5713 }
5714 }
5715
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005716 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005717 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005718
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005719 /* Initialize display from the command line parameters. */
5720 android_display_reset(ds,
5721 android_display_width,
5722 android_display_height,
5723 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005724
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005725 if (display_type == DT_DEFAULT) {
5726#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5727 display_type = DT_SDL;
5728#else
5729 display_type = DT_VNC;
5730 vnc_display = "localhost:0,to=99";
5731 show_vnc_port = 1;
5732#endif
5733 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005734
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005735
5736 switch (display_type) {
5737 case DT_NOGRAPHIC:
5738 break;
5739#if defined(CONFIG_CURSES)
5740 case DT_CURSES:
5741 curses_display_init(ds, full_screen);
5742 break;
5743#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005744#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005745 case DT_SDL:
5746 sdl_display_init(ds, full_screen, no_frame);
5747 break;
5748#elif defined(CONFIG_COCOA)
5749 case DT_SDL:
5750 cocoa_display_init(ds, full_screen);
5751 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005752#elif defined(CONFIG_STANDALONE_CORE)
5753 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005754 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005755 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005756#endif
5757 case DT_VNC:
5758 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005759 if (vnc_display_open(ds, vnc_display) < 0) {
5760 PANIC("Unable to initialize VNC display");
5761 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005762
5763 if (show_vnc_port) {
5764 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5765 }
5766 break;
5767 default:
5768 break;
5769 }
5770 dpy_resize(ds);
5771
5772 dcl = ds->listeners;
5773 while (dcl != NULL) {
5774 if (dcl->dpy_refresh != NULL) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005775 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
5776 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005777 }
5778 dcl = dcl->next;
5779 }
5780
5781 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
David 'Digit' Turner5973c772011-05-10 07:06:00 +02005782 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
5783 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005784 }
5785
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005786 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005787 qemu_chr_initial_reset();
5788
5789 if (monitor_device && monitor_hd)
5790 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5791
5792 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5793 const char *devname = serial_devices[i];
5794 if (devname && strcmp(devname, "none")) {
5795 if (strstart(devname, "vc", 0))
5796 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5797 }
5798 }
5799
5800 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5801 const char *devname = parallel_devices[i];
5802 if (devname && strcmp(devname, "none")) {
5803 if (strstart(devname, "vc", 0))
5804 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5805 }
5806 }
5807
5808 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5809 const char *devname = virtio_consoles[i];
5810 if (virtcon_hds[i] && devname) {
5811 if (strstart(devname, "vc", 0))
5812 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5813 }
5814 }
5815
5816 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005817 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005818 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005819 }
5820
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005821 /* call android-specific setup function */
5822 android_emulation_setup();
5823
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005824#if !defined(CONFIG_STANDALONE_CORE)
5825 // For the standalone emulator (UI+core in one executable) we need to
5826 // set the window title here.
5827 android_emulator_set_base_port(android_base_port);
5828#endif
5829
Ot ten Thije871da2a2010-09-20 10:29:22 +01005830 if (loadvm)
5831 do_loadvm(cur_mon, loadvm);
5832
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005833 if (incoming) {
5834 autostart = 0; /* fixme how to deal with -daemonize */
5835 qemu_start_incoming_migration(incoming);
5836 }
5837
5838 if (autostart)
5839 vm_start();
5840
5841#ifndef _WIN32
5842 if (daemonize) {
5843 uint8_t status = 0;
5844 ssize_t len;
5845
5846 again1:
5847 len = write(fds[1], &status, 1);
5848 if (len == -1 && (errno == EINTR))
5849 goto again1;
5850
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005851 if (len != 1) {
5852 PANIC("Unable to daemonize");
5853 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005854
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005855 if (chdir("/")) {
5856 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005857 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005858 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005859 TFR(fd = open("/dev/null", O_RDWR));
5860 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005861 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005862 }
5863
5864 if (run_as) {
5865 pwd = getpwnam(run_as);
5866 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005867 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005868 }
5869 }
5870
5871 if (chroot_dir) {
5872 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005873 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005874 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005875 if (chdir("/")) {
5876 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005877 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005878 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005879 }
5880
5881 if (run_as) {
5882 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005883 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005884 }
5885 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005886 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005887 }
5888 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005889 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005890 }
5891 }
5892
5893 if (daemonize) {
5894 dup2(fd, 0);
5895 dup2(fd, 1);
5896 dup2(fd, 2);
5897
5898 close(fd);
5899 }
5900#endif
5901
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005902#ifdef CONFIG_ANDROID
5903 // This will notify the UI that the core is successfuly initialized
5904 android_core_init_completed();
5905#endif // CONFIG_ANDROID
5906
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005907 main_loop();
5908 quit_timers();
5909 net_cleanup();
5910 android_emulation_teardown();
5911 return 0;
5912}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005913
5914void
5915android_emulation_teardown(void)
5916{
5917 android_charmap_done();
5918}