blob: 0a64d0dd6c5739c0409cafc9fcde11be6068b83b [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 Turner025c32f2010-09-10 14:52:42 +0200618/* real time host monotonic timer */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700619
620/* compute with 96 bit intermediate result: (a*b)/c */
621uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
622{
623 union {
624 uint64_t ll;
625 struct {
David 'Digit' Turner20894ae2010-05-10 17:07:36 -0700626#ifdef HOST_WORDS_BIGENDIAN
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700627 uint32_t high, low;
628#else
629 uint32_t low, high;
630#endif
631 } l;
632 } u, res;
633 uint64_t rl, rh;
634
635 u.ll = a;
636 rl = (uint64_t)u.l.low * (uint64_t)b;
637 rh = (uint64_t)u.l.high * (uint64_t)b;
638 rh += (rl >> 32);
639 res.l.high = rh / c;
640 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
641 return res.ll;
642}
643
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700644/***********************************************************/
645/* host time/date access */
646void qemu_get_timedate(struct tm *tm, int offset)
647{
648 time_t ti;
649 struct tm *ret;
650
651 time(&ti);
652 ti += offset;
653 if (rtc_date_offset == -1) {
654 if (rtc_utc)
655 ret = gmtime(&ti);
656 else
657 ret = localtime(&ti);
658 } else {
659 ti -= rtc_date_offset;
660 ret = gmtime(&ti);
661 }
662
663 memcpy(tm, ret, sizeof(struct tm));
664}
665
666int qemu_timedate_diff(struct tm *tm)
667{
668 time_t seconds;
669
670 if (rtc_date_offset == -1)
671 if (rtc_utc)
672 seconds = mktimegm(tm);
673 else
674 seconds = mktime(tm);
675 else
676 seconds = mktimegm(tm) + rtc_date_offset;
677
678 return seconds - time(NULL);
679}
680
681
682#ifdef CONFIG_TRACE
683static int tbflush_requested;
684static int exit_requested;
685
686void start_tracing()
687{
688 if (trace_filename == NULL)
689 return;
690 if (!tracing) {
691 fprintf(stderr,"-- start tracing --\n");
692 start_time = Now();
693 }
694 tracing = 1;
695 tbflush_requested = 1;
696 qemu_notify_event();
697}
698
699void stop_tracing()
700{
701 if (trace_filename == NULL)
702 return;
703 if (tracing) {
704 end_time = Now();
705 elapsed_usecs += end_time - start_time;
706 fprintf(stderr,"-- stop tracing --\n");
707 }
708 tracing = 0;
709 tbflush_requested = 1;
710 qemu_notify_event();
711}
712
713#ifndef _WIN32
714/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
715 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
716 */
717void sigusr_handler(int sig)
718{
719 if (sig == SIGUSR1)
720 start_tracing();
721 else
722 stop_tracing();
723}
724#endif
725
726/* This is the handler to catch control-C so that we can exit cleanly.
727 * This is needed when tracing to flush the buffers to disk.
728 */
729void sigint_handler(int sig)
730{
731 exit_requested = 1;
732 qemu_notify_event();
733}
734#endif /* CONFIG_TRACE */
735
736
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700737/***********************************************************/
738/* Bluetooth support */
739static int nb_hcis;
740static int cur_hci;
741static struct HCIInfo *hci_table[MAX_NICS];
742
743static struct bt_vlan_s {
744 struct bt_scatternet_s net;
745 int id;
746 struct bt_vlan_s *next;
747} *first_bt_vlan;
748
749/* find or alloc a new bluetooth "VLAN" */
750static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
751{
752 struct bt_vlan_s **pvlan, *vlan;
753 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
754 if (vlan->id == id)
755 return &vlan->net;
756 }
757 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
758 vlan->id = id;
759 pvlan = &first_bt_vlan;
760 while (*pvlan != NULL)
761 pvlan = &(*pvlan)->next;
762 *pvlan = vlan;
763 return &vlan->net;
764}
765
766static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
767{
768}
769
770static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
771{
772 return -ENOTSUP;
773}
774
775static struct HCIInfo null_hci = {
776 .cmd_send = null_hci_send,
777 .sco_send = null_hci_send,
778 .acl_send = null_hci_send,
779 .bdaddr_set = null_hci_addr_set,
780};
781
782struct HCIInfo *qemu_next_hci(void)
783{
784 if (cur_hci == nb_hcis)
785 return &null_hci;
786
787 return hci_table[cur_hci++];
788}
789
790static struct HCIInfo *hci_init(const char *str)
791{
792 char *endp;
793 struct bt_scatternet_s *vlan = 0;
794
795 if (!strcmp(str, "null"))
796 /* null */
797 return &null_hci;
798 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
799 /* host[:hciN] */
800 return bt_host_hci(str[4] ? str + 5 : "hci0");
801 else if (!strncmp(str, "hci", 3)) {
802 /* hci[,vlan=n] */
803 if (str[3]) {
804 if (!strncmp(str + 3, ",vlan=", 6)) {
805 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
806 if (*endp)
807 vlan = 0;
808 }
809 } else
810 vlan = qemu_find_bt_vlan(0);
811 if (vlan)
812 return bt_new_hci(vlan);
813 }
814
815 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
816
817 return 0;
818}
819
820static int bt_hci_parse(const char *str)
821{
822 struct HCIInfo *hci;
823 bdaddr_t bdaddr;
824
825 if (nb_hcis >= MAX_NICS) {
826 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
827 return -1;
828 }
829
830 hci = hci_init(str);
831 if (!hci)
832 return -1;
833
834 bdaddr.b[0] = 0x52;
835 bdaddr.b[1] = 0x54;
836 bdaddr.b[2] = 0x00;
837 bdaddr.b[3] = 0x12;
838 bdaddr.b[4] = 0x34;
839 bdaddr.b[5] = 0x56 + nb_hcis;
840 hci->bdaddr_set(hci, bdaddr.b);
841
842 hci_table[nb_hcis++] = hci;
843
844 return 0;
845}
846
847static void bt_vhci_add(int vlan_id)
848{
849 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
850
851 if (!vlan->slave)
852 fprintf(stderr, "qemu: warning: adding a VHCI to "
853 "an empty scatternet %i\n", vlan_id);
854
855 bt_vhci_init(bt_new_hci(vlan));
856}
857
858static struct bt_device_s *bt_device_add(const char *opt)
859{
860 struct bt_scatternet_s *vlan;
861 int vlan_id = 0;
862 char *endp = strstr(opt, ",vlan=");
863 int len = (endp ? endp - opt : strlen(opt)) + 1;
864 char devname[10];
865
866 pstrcpy(devname, MIN(sizeof(devname), len), opt);
867
868 if (endp) {
869 vlan_id = strtol(endp + 6, &endp, 0);
870 if (*endp) {
871 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
872 return 0;
873 }
874 }
875
876 vlan = qemu_find_bt_vlan(vlan_id);
877
878 if (!vlan->slave)
879 fprintf(stderr, "qemu: warning: adding a slave device to "
880 "an empty scatternet %i\n", vlan_id);
881
882 if (!strcmp(devname, "keyboard"))
883 return bt_keyboard_init(vlan);
884
885 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
886 return 0;
887}
888
889static int bt_parse(const char *opt)
890{
891 const char *endp, *p;
892 int vlan;
893
894 if (strstart(opt, "hci", &endp)) {
895 if (!*endp || *endp == ',') {
896 if (*endp)
897 if (!strstart(endp, ",vlan=", 0))
898 opt = endp + 1;
899
900 return bt_hci_parse(opt);
901 }
902 } else if (strstart(opt, "vhci", &endp)) {
903 if (!*endp || *endp == ',') {
904 if (*endp) {
905 if (strstart(endp, ",vlan=", &p)) {
906 vlan = strtol(p, (char **) &endp, 0);
907 if (*endp) {
908 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
909 return 1;
910 }
911 } else {
912 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
913 return 1;
914 }
915 } else
916 vlan = 0;
917
918 bt_vhci_add(vlan);
919 return 0;
920 }
921 } else if (strstart(opt, "device:", &endp))
922 return !bt_device_add(endp);
923
924 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
925 return 1;
926}
927
928/***********************************************************/
929/* QEMU Block devices */
930
931#define HD_ALIAS "index=%d,media=disk"
932#define CDROM_ALIAS "index=2,media=cdrom"
933#define FD_ALIAS "index=%d,if=floppy"
934#define PFLASH_ALIAS "if=pflash"
935#define MTD_ALIAS "if=mtd"
936#define SD_ALIAS "index=0,if=sd"
937
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100938static int drive_init_func(QemuOpts *opts, void *opaque)
939{
940 int *use_scsi = opaque;
941 int fatal_error = 0;
942
943 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
944 if (fatal_error)
945 return 1;
946 }
947 return 0;
948}
949
950static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
951{
952 if (NULL == qemu_opt_get(opts, "snapshot")) {
953 qemu_opt_set(opts, "snapshot", "on");
954 }
955 return 0;
956}
957
958#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700959static int drive_opt_get_free_idx(void)
960{
961 int index;
962
963 for (index = 0; index < MAX_DRIVES; index++)
964 if (!drives_opt[index].used) {
965 drives_opt[index].used = 1;
966 return index;
967 }
968
969 return -1;
970}
971
972static int drive_get_free_idx(void)
973{
974 int index;
975
976 for (index = 0; index < MAX_DRIVES; index++)
977 if (!drives_table[index].used) {
978 drives_table[index].used = 1;
979 return index;
980 }
981
982 return -1;
983}
984
985int drive_add(const char *file, const char *fmt, ...)
986{
987 va_list ap;
988 int index = drive_opt_get_free_idx();
989
990 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
991 fprintf(stderr, "qemu: too many drives\n");
992 return -1;
993 }
994
995 drives_opt[index].file = file;
996 va_start(ap, fmt);
997 vsnprintf(drives_opt[index].opt,
998 sizeof(drives_opt[0].opt), fmt, ap);
999 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -07001000
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001001 nb_drives_opt++;
1002 return index;
1003}
1004
1005void drive_remove(int index)
1006{
1007 drives_opt[index].used = 0;
1008 nb_drives_opt--;
1009}
1010
1011int drive_get_index(BlockInterfaceType type, int bus, int unit)
1012{
1013 int index;
1014
1015 /* seek interface, bus and unit */
1016
1017 for (index = 0; index < MAX_DRIVES; index++)
1018 if (drives_table[index].type == type &&
1019 drives_table[index].bus == bus &&
1020 drives_table[index].unit == unit &&
1021 drives_table[index].used)
1022 return index;
1023
1024 return -1;
1025}
1026
1027int drive_get_max_bus(BlockInterfaceType type)
1028{
1029 int max_bus;
1030 int index;
1031
1032 max_bus = -1;
1033 for (index = 0; index < nb_drives; index++) {
1034 if(drives_table[index].type == type &&
1035 drives_table[index].bus > max_bus)
1036 max_bus = drives_table[index].bus;
1037 }
1038 return max_bus;
1039}
1040
1041const char *drive_get_serial(BlockDriverState *bdrv)
1042{
1043 int index;
1044
1045 for (index = 0; index < nb_drives; index++)
1046 if (drives_table[index].bdrv == bdrv)
1047 return drives_table[index].serial;
1048
1049 return "\0";
1050}
1051
1052BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1053{
1054 int index;
1055
1056 for (index = 0; index < nb_drives; index++)
1057 if (drives_table[index].bdrv == bdrv)
1058 return drives_table[index].onerror;
1059
1060 return BLOCK_ERR_STOP_ENOSPC;
1061}
1062
1063static void bdrv_format_print(void *opaque, const char *name)
1064{
1065 fprintf(stderr, " %s", name);
1066}
1067
1068void drive_uninit(BlockDriverState *bdrv)
1069{
1070 int i;
1071
1072 for (i = 0; i < MAX_DRIVES; i++)
1073 if (drives_table[i].bdrv == bdrv) {
1074 drives_table[i].bdrv = NULL;
1075 drives_table[i].used = 0;
1076 drive_remove(drives_table[i].drive_opt_idx);
1077 nb_drives--;
1078 break;
1079 }
1080}
1081
1082int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1083{
1084 char buf[128];
1085 char file[1024];
1086 char devname[128];
1087 char serial[21];
1088 const char *mediastr = "";
1089 BlockInterfaceType type;
1090 enum { MEDIA_DISK, MEDIA_CDROM } media;
1091 int bus_id, unit_id;
1092 int cyls, heads, secs, translation;
1093 BlockDriverState *bdrv;
1094 BlockDriver *drv = NULL;
1095 QEMUMachine *machine = opaque;
1096 int max_devs;
1097 int index;
1098 int cache;
1099 int bdrv_flags, onerror;
1100 int drives_table_idx;
1101 char *str = arg->opt;
1102 static const char * const params[] = { "bus", "unit", "if", "index",
1103 "cyls", "heads", "secs", "trans",
1104 "media", "snapshot", "file",
1105 "cache", "format", "serial", "werror",
1106 NULL };
1107
1108 if (check_params(buf, sizeof(buf), params, str) < 0) {
1109 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1110 buf, str);
1111 return -1;
1112 }
1113
1114 file[0] = 0;
1115 cyls = heads = secs = 0;
1116 bus_id = 0;
1117 unit_id = -1;
1118 translation = BIOS_ATA_TRANSLATION_AUTO;
1119 index = -1;
1120 cache = 3;
1121
1122 if (machine->use_scsi) {
1123 type = IF_SCSI;
1124 max_devs = MAX_SCSI_DEVS;
1125 pstrcpy(devname, sizeof(devname), "scsi");
1126 } else {
1127 type = IF_IDE;
1128 max_devs = MAX_IDE_DEVS;
1129 pstrcpy(devname, sizeof(devname), "ide");
1130 }
1131 media = MEDIA_DISK;
1132
1133 /* extract parameters */
1134
1135 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1136 bus_id = strtol(buf, NULL, 0);
1137 if (bus_id < 0) {
1138 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1139 return -1;
1140 }
1141 }
1142
1143 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1144 unit_id = strtol(buf, NULL, 0);
1145 if (unit_id < 0) {
1146 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1147 return -1;
1148 }
1149 }
1150
1151 if (get_param_value(buf, sizeof(buf), "if", str)) {
1152 pstrcpy(devname, sizeof(devname), buf);
1153 if (!strcmp(buf, "ide")) {
1154 type = IF_IDE;
1155 max_devs = MAX_IDE_DEVS;
1156 } else if (!strcmp(buf, "scsi")) {
1157 type = IF_SCSI;
1158 max_devs = MAX_SCSI_DEVS;
1159 } else if (!strcmp(buf, "floppy")) {
1160 type = IF_FLOPPY;
1161 max_devs = 0;
1162 } else if (!strcmp(buf, "pflash")) {
1163 type = IF_PFLASH;
1164 max_devs = 0;
1165 } else if (!strcmp(buf, "mtd")) {
1166 type = IF_MTD;
1167 max_devs = 0;
1168 } else if (!strcmp(buf, "sd")) {
1169 type = IF_SD;
1170 max_devs = 0;
1171 } else if (!strcmp(buf, "virtio")) {
1172 type = IF_VIRTIO;
1173 max_devs = 0;
1174 } else if (!strcmp(buf, "xen")) {
1175 type = IF_XEN;
1176 max_devs = 0;
1177 } else {
1178 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1179 return -1;
1180 }
1181 }
1182
1183 if (get_param_value(buf, sizeof(buf), "index", str)) {
1184 index = strtol(buf, NULL, 0);
1185 if (index < 0) {
1186 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1187 return -1;
1188 }
1189 }
1190
1191 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1192 cyls = strtol(buf, NULL, 0);
1193 }
1194
1195 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1196 heads = strtol(buf, NULL, 0);
1197 }
1198
1199 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1200 secs = strtol(buf, NULL, 0);
1201 }
1202
1203 if (cyls || heads || secs) {
1204 if (cyls < 1 || cyls > 16383) {
1205 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1206 return -1;
1207 }
1208 if (heads < 1 || heads > 16) {
1209 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1210 return -1;
1211 }
1212 if (secs < 1 || secs > 63) {
1213 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1214 return -1;
1215 }
1216 }
1217
1218 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1219 if (!cyls) {
1220 fprintf(stderr,
1221 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1222 str);
1223 return -1;
1224 }
1225 if (!strcmp(buf, "none"))
1226 translation = BIOS_ATA_TRANSLATION_NONE;
1227 else if (!strcmp(buf, "lba"))
1228 translation = BIOS_ATA_TRANSLATION_LBA;
1229 else if (!strcmp(buf, "auto"))
1230 translation = BIOS_ATA_TRANSLATION_AUTO;
1231 else {
1232 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1233 return -1;
1234 }
1235 }
1236
1237 if (get_param_value(buf, sizeof(buf), "media", str)) {
1238 if (!strcmp(buf, "disk")) {
1239 media = MEDIA_DISK;
1240 } else if (!strcmp(buf, "cdrom")) {
1241 if (cyls || secs || heads) {
1242 fprintf(stderr,
1243 "qemu: '%s' invalid physical CHS format\n", str);
1244 return -1;
1245 }
1246 media = MEDIA_CDROM;
1247 } else {
1248 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1249 return -1;
1250 }
1251 }
1252
1253 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1254 if (!strcmp(buf, "on"))
1255 snapshot = 1;
1256 else if (!strcmp(buf, "off"))
1257 snapshot = 0;
1258 else {
1259 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1260 return -1;
1261 }
1262 }
1263
1264 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1265 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1266 cache = 0;
1267 else if (!strcmp(buf, "writethrough"))
1268 cache = 1;
1269 else if (!strcmp(buf, "writeback"))
1270 cache = 2;
1271 else {
1272 fprintf(stderr, "qemu: invalid cache option\n");
1273 return -1;
1274 }
1275 }
1276
1277 if (get_param_value(buf, sizeof(buf), "format", str)) {
1278 if (strcmp(buf, "?") == 0) {
1279 fprintf(stderr, "qemu: Supported formats:");
1280 bdrv_iterate_format(bdrv_format_print, NULL);
1281 fprintf(stderr, "\n");
1282 return -1;
1283 }
1284 drv = bdrv_find_format(buf);
1285 if (!drv) {
1286 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1287 return -1;
1288 }
1289 }
1290
1291 if (arg->file == NULL)
1292 get_param_value(file, sizeof(file), "file", str);
1293 else
1294 pstrcpy(file, sizeof(file), arg->file);
1295
1296 if (!get_param_value(serial, sizeof(serial), "serial", str))
1297 memset(serial, 0, sizeof(serial));
1298
1299 onerror = BLOCK_ERR_STOP_ENOSPC;
1300 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1301 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1302 fprintf(stderr, "werror is no supported by this format\n");
1303 return -1;
1304 }
1305 if (!strcmp(buf, "ignore"))
1306 onerror = BLOCK_ERR_IGNORE;
1307 else if (!strcmp(buf, "enospc"))
1308 onerror = BLOCK_ERR_STOP_ENOSPC;
1309 else if (!strcmp(buf, "stop"))
1310 onerror = BLOCK_ERR_STOP_ANY;
1311 else if (!strcmp(buf, "report"))
1312 onerror = BLOCK_ERR_REPORT;
1313 else {
1314 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1315 return -1;
1316 }
1317 }
1318
1319 /* compute bus and unit according index */
1320
1321 if (index != -1) {
1322 if (bus_id != 0 || unit_id != -1) {
1323 fprintf(stderr,
1324 "qemu: '%s' index cannot be used with bus and unit\n", str);
1325 return -1;
1326 }
1327 if (max_devs == 0)
1328 {
1329 unit_id = index;
1330 bus_id = 0;
1331 } else {
1332 unit_id = index % max_devs;
1333 bus_id = index / max_devs;
1334 }
1335 }
1336
1337 /* if user doesn't specify a unit_id,
1338 * try to find the first free
1339 */
1340
1341 if (unit_id == -1) {
1342 unit_id = 0;
1343 while (drive_get_index(type, bus_id, unit_id) != -1) {
1344 unit_id++;
1345 if (max_devs && unit_id >= max_devs) {
1346 unit_id -= max_devs;
1347 bus_id++;
1348 }
1349 }
1350 }
1351
1352 /* check unit id */
1353
1354 if (max_devs && unit_id >= max_devs) {
1355 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1356 str, unit_id, max_devs - 1);
1357 return -1;
1358 }
1359
1360 /*
1361 * ignore multiple definitions
1362 */
1363
1364 if (drive_get_index(type, bus_id, unit_id) != -1)
1365 return -2;
1366
1367 /* init */
1368
1369 if (type == IF_IDE || type == IF_SCSI)
1370 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1371 if (max_devs)
1372 snprintf(buf, sizeof(buf), "%s%i%s%i",
1373 devname, bus_id, mediastr, unit_id);
1374 else
1375 snprintf(buf, sizeof(buf), "%s%s%i",
1376 devname, mediastr, unit_id);
1377 bdrv = bdrv_new(buf);
1378 drives_table_idx = drive_get_free_idx();
1379 drives_table[drives_table_idx].bdrv = bdrv;
1380 drives_table[drives_table_idx].type = type;
1381 drives_table[drives_table_idx].bus = bus_id;
1382 drives_table[drives_table_idx].unit = unit_id;
1383 drives_table[drives_table_idx].onerror = onerror;
1384 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1385 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1386 nb_drives++;
1387
1388 switch(type) {
1389 case IF_IDE:
1390 case IF_SCSI:
1391 case IF_XEN:
1392 switch(media) {
1393 case MEDIA_DISK:
1394 if (cyls != 0) {
1395 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1396 bdrv_set_translation_hint(bdrv, translation);
1397 }
1398 break;
1399 case MEDIA_CDROM:
1400 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1401 break;
1402 }
1403 break;
1404 case IF_SD:
1405 /* FIXME: This isn't really a floppy, but it's a reasonable
1406 approximation. */
1407 case IF_FLOPPY:
1408 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1409 break;
1410 case IF_PFLASH:
1411 case IF_MTD:
1412 case IF_VIRTIO:
1413 break;
1414 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001415 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001416 abort();
1417 }
1418 if (!file[0])
1419 return -2;
1420 bdrv_flags = 0;
1421 if (snapshot) {
1422 bdrv_flags |= BDRV_O_SNAPSHOT;
1423 cache = 2; /* always use write-back with snapshot */
1424 }
1425 if (cache == 0) /* no caching */
1426 bdrv_flags |= BDRV_O_NOCACHE;
1427 else if (cache == 2) /* write-back */
1428 bdrv_flags |= BDRV_O_CACHE_WB;
1429 else if (cache == 3) /* not specified */
1430 bdrv_flags |= BDRV_O_CACHE_DEF;
1431 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1432 fprintf(stderr, "qemu: could not open disk image %s\n",
1433 file);
1434 return -1;
1435 }
1436 if (bdrv_key_required(bdrv))
1437 autostart = 0;
1438 return drives_table_idx;
1439}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001440#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001441
1442static void numa_add(const char *optarg)
1443{
1444 char option[128];
1445 char *endptr;
1446 unsigned long long value, endvalue;
1447 int nodenr;
1448
1449 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1450 if (!strcmp(option, "node")) {
1451 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1452 nodenr = nb_numa_nodes;
1453 } else {
1454 nodenr = strtoull(option, NULL, 10);
1455 }
1456
1457 if (get_param_value(option, 128, "mem", optarg) == 0) {
1458 node_mem[nodenr] = 0;
1459 } else {
1460 value = strtoull(option, &endptr, 0);
1461 switch (*endptr) {
1462 case 0: case 'M': case 'm':
1463 value <<= 20;
1464 break;
1465 case 'G': case 'g':
1466 value <<= 30;
1467 break;
1468 }
1469 node_mem[nodenr] = value;
1470 }
1471 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1472 node_cpumask[nodenr] = 0;
1473 } else {
1474 value = strtoull(option, &endptr, 10);
1475 if (value >= 64) {
1476 value = 63;
1477 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1478 } else {
1479 if (*endptr == '-') {
1480 endvalue = strtoull(endptr+1, &endptr, 10);
1481 if (endvalue >= 63) {
1482 endvalue = 62;
1483 fprintf(stderr,
1484 "only 63 CPUs in NUMA mode supported.\n");
1485 }
1486 value = (1 << (endvalue + 1)) - (1 << value);
1487 } else {
1488 value = 1 << value;
1489 }
1490 }
1491 node_cpumask[nodenr] = value;
1492 }
1493 nb_numa_nodes++;
1494 }
1495 return;
1496}
1497
1498/***********************************************************/
1499/* USB devices */
1500
1501static USBPort *used_usb_ports;
1502static USBPort *free_usb_ports;
1503
1504/* ??? Maybe change this to register a hub to keep track of the topology. */
1505void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1506 usb_attachfn attach)
1507{
1508 port->opaque = opaque;
1509 port->index = index;
1510 port->attach = attach;
1511 port->next = free_usb_ports;
1512 free_usb_ports = port;
1513}
1514
1515int usb_device_add_dev(USBDevice *dev)
1516{
1517 USBPort *port;
1518
1519 /* Find a USB port to add the device to. */
1520 port = free_usb_ports;
1521 if (!port->next) {
1522 USBDevice *hub;
1523
1524 /* Create a new hub and chain it on. */
1525 free_usb_ports = NULL;
1526 port->next = used_usb_ports;
1527 used_usb_ports = port;
1528
1529 hub = usb_hub_init(VM_USB_HUB_SIZE);
1530 usb_attach(port, hub);
1531 port = free_usb_ports;
1532 }
1533
1534 free_usb_ports = port->next;
1535 port->next = used_usb_ports;
1536 used_usb_ports = port;
1537 usb_attach(port, dev);
1538 return 0;
1539}
1540
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001541#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001542static void usb_msd_password_cb(void *opaque, int err)
1543{
1544 USBDevice *dev = opaque;
1545
1546 if (!err)
1547 usb_device_add_dev(dev);
1548 else
1549 dev->handle_destroy(dev);
1550}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001551#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001552
1553static int usb_device_add(const char *devname, int is_hotplug)
1554{
1555 const char *p;
1556 USBDevice *dev;
1557
1558 if (!free_usb_ports)
1559 return -1;
1560
1561 if (strstart(devname, "host:", &p)) {
1562 dev = usb_host_device_open(p);
1563 } else if (!strcmp(devname, "mouse")) {
1564 dev = usb_mouse_init();
1565 } else if (!strcmp(devname, "tablet")) {
1566 dev = usb_tablet_init();
1567 } else if (!strcmp(devname, "keyboard")) {
1568 dev = usb_keyboard_init();
1569 } else if (strstart(devname, "disk:", &p)) {
1570#if 0
1571 BlockDriverState *bs;
1572#endif
1573 dev = usb_msd_init(p);
1574 if (!dev)
1575 return -1;
1576#if 0
1577 bs = usb_msd_get_bdrv(dev);
1578 if (bdrv_key_required(bs)) {
1579 autostart = 0;
1580 if (is_hotplug) {
1581 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1582 dev);
1583 return 0;
1584 }
1585 }
1586 } else if (!strcmp(devname, "wacom-tablet")) {
1587 dev = usb_wacom_init();
1588 } else if (strstart(devname, "serial:", &p)) {
1589 dev = usb_serial_init(p);
1590#ifdef CONFIG_BRLAPI
1591 } else if (!strcmp(devname, "braille")) {
1592 dev = usb_baum_init();
1593#endif
1594 } else if (strstart(devname, "net:", &p)) {
1595 int nic = nb_nics;
1596
1597 if (net_client_init("nic", p) < 0)
1598 return -1;
1599 nd_table[nic].model = "usb";
1600 dev = usb_net_init(&nd_table[nic]);
1601 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1602 dev = usb_bt_init(devname[2] ? hci_init(p) :
1603 bt_new_hci(qemu_find_bt_vlan(0)));
1604#endif
1605 } else {
1606 return -1;
1607 }
1608 if (!dev)
1609 return -1;
1610
1611 return usb_device_add_dev(dev);
1612}
1613
1614int usb_device_del_addr(int bus_num, int addr)
1615{
1616 USBPort *port;
1617 USBPort **lastp;
1618 USBDevice *dev;
1619
1620 if (!used_usb_ports)
1621 return -1;
1622
1623 if (bus_num != 0)
1624 return -1;
1625
1626 lastp = &used_usb_ports;
1627 port = used_usb_ports;
1628 while (port && port->dev->addr != addr) {
1629 lastp = &port->next;
1630 port = port->next;
1631 }
1632
1633 if (!port)
1634 return -1;
1635
1636 dev = port->dev;
1637 *lastp = port->next;
1638 usb_attach(port, NULL);
1639 dev->handle_destroy(dev);
1640 port->next = free_usb_ports;
1641 free_usb_ports = port;
1642 return 0;
1643}
1644
1645static int usb_device_del(const char *devname)
1646{
1647 int bus_num, addr;
1648 const char *p;
1649
1650 if (strstart(devname, "host:", &p))
1651 return usb_host_device_close(p);
1652
1653 if (!used_usb_ports)
1654 return -1;
1655
1656 p = strchr(devname, '.');
1657 if (!p)
1658 return -1;
1659 bus_num = strtoul(devname, NULL, 0);
1660 addr = strtoul(p + 1, NULL, 0);
1661
1662 return usb_device_del_addr(bus_num, addr);
1663}
1664
1665void do_usb_add(Monitor *mon, const char *devname)
1666{
1667 usb_device_add(devname, 1);
1668}
1669
1670void do_usb_del(Monitor *mon, const char *devname)
1671{
1672 usb_device_del(devname);
1673}
1674
1675void usb_info(Monitor *mon)
1676{
1677 USBDevice *dev;
1678 USBPort *port;
1679 const char *speed_str;
1680
1681 if (!usb_enabled) {
1682 monitor_printf(mon, "USB support not enabled\n");
1683 return;
1684 }
1685
1686 for (port = used_usb_ports; port; port = port->next) {
1687 dev = port->dev;
1688 if (!dev)
1689 continue;
1690 switch(dev->speed) {
1691 case USB_SPEED_LOW:
1692 speed_str = "1.5";
1693 break;
1694 case USB_SPEED_FULL:
1695 speed_str = "12";
1696 break;
1697 case USB_SPEED_HIGH:
1698 speed_str = "480";
1699 break;
1700 default:
1701 speed_str = "?";
1702 break;
1703 }
1704 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1705 0, dev->addr, speed_str, dev->devname);
1706 }
1707}
1708
1709/***********************************************************/
1710/* PCMCIA/Cardbus */
1711
1712static struct pcmcia_socket_entry_s {
1713 PCMCIASocket *socket;
1714 struct pcmcia_socket_entry_s *next;
1715} *pcmcia_sockets = 0;
1716
1717void pcmcia_socket_register(PCMCIASocket *socket)
1718{
1719 struct pcmcia_socket_entry_s *entry;
1720
1721 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1722 entry->socket = socket;
1723 entry->next = pcmcia_sockets;
1724 pcmcia_sockets = entry;
1725}
1726
1727void pcmcia_socket_unregister(PCMCIASocket *socket)
1728{
1729 struct pcmcia_socket_entry_s *entry, **ptr;
1730
1731 ptr = &pcmcia_sockets;
1732 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1733 if (entry->socket == socket) {
1734 *ptr = entry->next;
1735 qemu_free(entry);
1736 }
1737}
1738
1739void pcmcia_info(Monitor *mon)
1740{
1741 struct pcmcia_socket_entry_s *iter;
1742
1743 if (!pcmcia_sockets)
1744 monitor_printf(mon, "No PCMCIA sockets\n");
1745
1746 for (iter = pcmcia_sockets; iter; iter = iter->next)
1747 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1748 iter->socket->attached ? iter->socket->card_string :
1749 "Empty");
1750}
1751
1752/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001753/* I/O handling */
1754
1755typedef struct IOHandlerRecord {
1756 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001757 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001758 IOHandler *fd_read;
1759 IOHandler *fd_write;
1760 int deleted;
1761 void *opaque;
1762 /* temporary data */
1763 struct pollfd *ufd;
1764 struct IOHandlerRecord *next;
1765} IOHandlerRecord;
1766
1767static IOHandlerRecord *first_io_handler;
1768
1769/* XXX: fd_read_poll should be suppressed, but an API change is
1770 necessary in the character devices to suppress fd_can_read(). */
1771int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001772 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001773 IOHandler *fd_read,
1774 IOHandler *fd_write,
1775 void *opaque)
1776{
1777 IOHandlerRecord **pioh, *ioh;
1778
1779 if (!fd_read && !fd_write) {
1780 pioh = &first_io_handler;
1781 for(;;) {
1782 ioh = *pioh;
1783 if (ioh == NULL)
1784 break;
1785 if (ioh->fd == fd) {
1786 ioh->deleted = 1;
1787 break;
1788 }
1789 pioh = &ioh->next;
1790 }
1791 } else {
1792 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1793 if (ioh->fd == fd)
1794 goto found;
1795 }
1796 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1797 ioh->next = first_io_handler;
1798 first_io_handler = ioh;
1799 found:
1800 ioh->fd = fd;
1801 ioh->fd_read_poll = fd_read_poll;
1802 ioh->fd_read = fd_read;
1803 ioh->fd_write = fd_write;
1804 ioh->opaque = opaque;
1805 ioh->deleted = 0;
1806 }
1807 return 0;
1808}
1809
1810int qemu_set_fd_handler(int fd,
1811 IOHandler *fd_read,
1812 IOHandler *fd_write,
1813 void *opaque)
1814{
1815 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1816}
1817
1818#ifdef _WIN32
1819/***********************************************************/
1820/* Polling handling */
1821
1822typedef struct PollingEntry {
1823 PollingFunc *func;
1824 void *opaque;
1825 struct PollingEntry *next;
1826} PollingEntry;
1827
1828static PollingEntry *first_polling_entry;
1829
1830int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1831{
1832 PollingEntry **ppe, *pe;
1833 pe = qemu_mallocz(sizeof(PollingEntry));
1834 pe->func = func;
1835 pe->opaque = opaque;
1836 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1837 *ppe = pe;
1838 return 0;
1839}
1840
1841void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1842{
1843 PollingEntry **ppe, *pe;
1844 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1845 pe = *ppe;
1846 if (pe->func == func && pe->opaque == opaque) {
1847 *ppe = pe->next;
1848 qemu_free(pe);
1849 break;
1850 }
1851 }
1852}
1853
1854/***********************************************************/
1855/* Wait objects support */
1856typedef struct WaitObjects {
1857 int num;
1858 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1859 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1860 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1861} WaitObjects;
1862
1863static WaitObjects wait_objects = {0};
1864
1865int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1866{
1867 WaitObjects *w = &wait_objects;
1868
1869 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1870 return -1;
1871 w->events[w->num] = handle;
1872 w->func[w->num] = func;
1873 w->opaque[w->num] = opaque;
1874 w->num++;
1875 return 0;
1876}
1877
1878void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1879{
1880 int i, found;
1881 WaitObjects *w = &wait_objects;
1882
1883 found = 0;
1884 for (i = 0; i < w->num; i++) {
1885 if (w->events[i] == handle)
1886 found = 1;
1887 if (found) {
1888 w->events[i] = w->events[i + 1];
1889 w->func[i] = w->func[i + 1];
1890 w->opaque[i] = w->opaque[i + 1];
1891 }
1892 }
1893 if (found)
1894 w->num--;
1895}
1896#endif
1897
1898/***********************************************************/
1899/* ram save/restore */
1900
1901static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1902{
1903 int v;
1904
1905 v = qemu_get_byte(f);
1906 switch(v) {
1907 case 0:
1908 if (qemu_get_buffer(f, buf, len) != len)
1909 return -EIO;
1910 break;
1911 case 1:
1912 v = qemu_get_byte(f);
1913 memset(buf, v, len);
1914 break;
1915 default:
1916 return -EINVAL;
1917 }
1918
1919 if (qemu_file_has_error(f))
1920 return -EIO;
1921
1922 return 0;
1923}
1924
1925static int ram_load_v1(QEMUFile *f, void *opaque)
1926{
1927 int ret;
1928 ram_addr_t i;
1929
1930 if (qemu_get_be32(f) != last_ram_offset)
1931 return -EINVAL;
1932 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1933 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1934 if (ret)
1935 return ret;
1936 }
1937 return 0;
1938}
1939
1940#define BDRV_HASH_BLOCK_SIZE 1024
1941#define IOBUF_SIZE 4096
1942#define RAM_CBLOCK_MAGIC 0xfabe
1943
1944typedef struct RamDecompressState {
1945 z_stream zstream;
1946 QEMUFile *f;
1947 uint8_t buf[IOBUF_SIZE];
1948} RamDecompressState;
1949
1950static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1951{
1952 int ret;
1953 memset(s, 0, sizeof(*s));
1954 s->f = f;
1955 ret = inflateInit(&s->zstream);
1956 if (ret != Z_OK)
1957 return -1;
1958 return 0;
1959}
1960
1961static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1962{
1963 int ret, clen;
1964
1965 s->zstream.avail_out = len;
1966 s->zstream.next_out = buf;
1967 while (s->zstream.avail_out > 0) {
1968 if (s->zstream.avail_in == 0) {
1969 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1970 return -1;
1971 clen = qemu_get_be16(s->f);
1972 if (clen > IOBUF_SIZE)
1973 return -1;
1974 qemu_get_buffer(s->f, s->buf, clen);
1975 s->zstream.avail_in = clen;
1976 s->zstream.next_in = s->buf;
1977 }
1978 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1979 if (ret != Z_OK && ret != Z_STREAM_END) {
1980 return -1;
1981 }
1982 }
1983 return 0;
1984}
1985
1986static void ram_decompress_close(RamDecompressState *s)
1987{
1988 inflateEnd(&s->zstream);
1989}
1990
1991#define RAM_SAVE_FLAG_FULL 0x01
1992#define RAM_SAVE_FLAG_COMPRESS 0x02
1993#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1994#define RAM_SAVE_FLAG_PAGE 0x08
1995#define RAM_SAVE_FLAG_EOS 0x10
1996
1997static int is_dup_page(uint8_t *page, uint8_t ch)
1998{
1999 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2000 uint32_t *array = (uint32_t *)page;
2001 int i;
2002
2003 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2004 if (array[i] != val)
2005 return 0;
2006 }
2007
2008 return 1;
2009}
2010
2011static int ram_save_block(QEMUFile *f)
2012{
2013 static ram_addr_t current_addr = 0;
2014 ram_addr_t saved_addr = current_addr;
2015 ram_addr_t addr = 0;
2016 int found = 0;
2017
2018 while (addr < last_ram_offset) {
2019 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
2020 uint8_t *p;
2021
2022 cpu_physical_memory_reset_dirty(current_addr,
2023 current_addr + TARGET_PAGE_SIZE,
2024 MIGRATION_DIRTY_FLAG);
2025
2026 p = qemu_get_ram_ptr(current_addr);
2027
2028 if (is_dup_page(p, *p)) {
2029 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
2030 qemu_put_byte(f, *p);
2031 } else {
2032 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
2033 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
2034 }
2035
2036 found = 1;
2037 break;
2038 }
2039 addr += TARGET_PAGE_SIZE;
2040 current_addr = (saved_addr + addr) % last_ram_offset;
2041 }
2042
2043 return found;
2044}
2045
2046static uint64_t bytes_transferred = 0;
2047
2048static ram_addr_t ram_save_remaining(void)
2049{
2050 ram_addr_t addr;
2051 ram_addr_t count = 0;
2052
2053 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2054 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2055 count++;
2056 }
2057
2058 return count;
2059}
2060
2061uint64_t ram_bytes_remaining(void)
2062{
2063 return ram_save_remaining() * TARGET_PAGE_SIZE;
2064}
2065
2066uint64_t ram_bytes_transferred(void)
2067{
2068 return bytes_transferred;
2069}
2070
2071uint64_t ram_bytes_total(void)
2072{
2073 return last_ram_offset;
2074}
2075
2076static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2077{
2078 ram_addr_t addr;
2079 uint64_t bytes_transferred_last;
2080 double bwidth = 0;
2081 uint64_t expected_time = 0;
2082
2083 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2084
2085 if (stage == 1) {
2086 /* Make sure all dirty bits are set */
2087 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2088 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2089 cpu_physical_memory_set_dirty(addr);
2090 }
2091
2092 /* Enable dirty memory tracking */
2093 cpu_physical_memory_set_dirty_tracking(1);
2094
2095 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2096 }
2097
2098 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002099 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002100
2101 while (!qemu_file_rate_limit(f)) {
2102 int ret;
2103
2104 ret = ram_save_block(f);
2105 bytes_transferred += ret * TARGET_PAGE_SIZE;
2106 if (ret == 0) /* no more blocks */
2107 break;
2108 }
2109
David Turner6a9ef172010-09-09 22:54:36 +02002110 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002111 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2112
2113 /* if we haven't transferred anything this round, force expected_time to a
2114 * a very high value, but without crashing */
2115 if (bwidth == 0)
2116 bwidth = 0.000001;
2117
2118 /* try transferring iterative blocks of memory */
2119
2120 if (stage == 3) {
2121
2122 /* flush all remaining blocks regardless of rate limiting */
2123 while (ram_save_block(f) != 0) {
2124 bytes_transferred += TARGET_PAGE_SIZE;
2125 }
2126 cpu_physical_memory_set_dirty_tracking(0);
2127 }
2128
2129 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2130
2131 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2132
2133 return (stage == 2) && (expected_time <= migrate_max_downtime());
2134}
2135
2136static int ram_load_dead(QEMUFile *f, void *opaque)
2137{
2138 RamDecompressState s1, *s = &s1;
2139 uint8_t buf[10];
2140 ram_addr_t i;
2141
2142 if (ram_decompress_open(s, f) < 0)
2143 return -EINVAL;
2144 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2145 if (ram_decompress_buf(s, buf, 1) < 0) {
2146 fprintf(stderr, "Error while reading ram block header\n");
2147 goto error;
2148 }
2149 if (buf[0] == 0) {
2150 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2151 BDRV_HASH_BLOCK_SIZE) < 0) {
2152 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2153 goto error;
2154 }
2155 } else {
2156 error:
2157 printf("Error block header\n");
2158 return -EINVAL;
2159 }
2160 }
2161 ram_decompress_close(s);
2162
2163 return 0;
2164}
2165
2166static int ram_load(QEMUFile *f, void *opaque, int version_id)
2167{
2168 ram_addr_t addr;
2169 int flags;
2170
2171 if (version_id == 1)
2172 return ram_load_v1(f, opaque);
2173
2174 if (version_id == 2) {
2175 if (qemu_get_be32(f) != last_ram_offset)
2176 return -EINVAL;
2177 return ram_load_dead(f, opaque);
2178 }
2179
2180 if (version_id != 3)
2181 return -EINVAL;
2182
2183 do {
2184 addr = qemu_get_be64(f);
2185
2186 flags = addr & ~TARGET_PAGE_MASK;
2187 addr &= TARGET_PAGE_MASK;
2188
2189 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2190 if (addr != last_ram_offset)
2191 return -EINVAL;
2192 }
2193
2194 if (flags & RAM_SAVE_FLAG_FULL) {
2195 if (ram_load_dead(f, opaque) < 0)
2196 return -EINVAL;
2197 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002198
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002199 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2200 uint8_t ch = qemu_get_byte(f);
2201 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2202 } else if (flags & RAM_SAVE_FLAG_PAGE)
2203 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2204 } while (!(flags & RAM_SAVE_FLAG_EOS));
2205
2206 return 0;
2207}
2208
2209void qemu_service_io(void)
2210{
2211 qemu_notify_event();
2212}
2213
2214/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002215/* machine registration */
2216
2217static QEMUMachine *first_machine = NULL;
2218QEMUMachine *current_machine = NULL;
2219
2220int qemu_register_machine(QEMUMachine *m)
2221{
2222 QEMUMachine **pm;
2223 pm = &first_machine;
2224 while (*pm != NULL)
2225 pm = &(*pm)->next;
2226 m->next = NULL;
2227 *pm = m;
2228 return 0;
2229}
2230
2231static QEMUMachine *find_machine(const char *name)
2232{
2233 QEMUMachine *m;
2234
2235 for(m = first_machine; m != NULL; m = m->next) {
2236 if (!strcmp(m->name, name))
2237 return m;
2238 }
2239 return NULL;
2240}
2241
2242static QEMUMachine *find_default_machine(void)
2243{
2244 QEMUMachine *m;
2245
2246 for(m = first_machine; m != NULL; m = m->next) {
2247 if (m->is_default) {
2248 return m;
2249 }
2250 }
2251 return NULL;
2252}
2253
2254/***********************************************************/
2255/* main execution loop */
2256
2257static void gui_update(void *opaque)
2258{
2259 uint64_t interval = GUI_REFRESH_INTERVAL;
2260 DisplayState *ds = opaque;
2261 DisplayChangeListener *dcl = ds->listeners;
2262
2263 dpy_refresh(ds);
2264
2265 while (dcl != NULL) {
2266 if (dcl->gui_timer_interval &&
2267 dcl->gui_timer_interval < interval)
2268 interval = dcl->gui_timer_interval;
2269 dcl = dcl->next;
2270 }
2271 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
2272}
2273
2274static void nographic_update(void *opaque)
2275{
2276 uint64_t interval = GUI_REFRESH_INTERVAL;
2277
2278 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
2279}
2280
2281struct vm_change_state_entry {
2282 VMChangeStateHandler *cb;
2283 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002284 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002285};
2286
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002287static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002288
2289VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2290 void *opaque)
2291{
2292 VMChangeStateEntry *e;
2293
2294 e = qemu_mallocz(sizeof (*e));
2295
2296 e->cb = cb;
2297 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002298 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002299 return e;
2300}
2301
2302void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2303{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002304 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002305 qemu_free (e);
2306}
2307
2308static void vm_state_notify(int running, int reason)
2309{
2310 VMChangeStateEntry *e;
2311
2312 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2313 e->cb(e->opaque, running, reason);
2314 }
2315}
2316
2317static void resume_all_vcpus(void);
2318static void pause_all_vcpus(void);
2319
2320void vm_start(void)
2321{
2322 if (!vm_running) {
2323 cpu_enable_ticks();
2324 vm_running = 1;
2325 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002326 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002327 resume_all_vcpus();
2328 }
2329}
2330
2331/* reset/shutdown handler */
2332
2333typedef struct QEMUResetEntry {
2334 QEMUResetHandler *func;
2335 void *opaque;
2336 int order;
2337 struct QEMUResetEntry *next;
2338} QEMUResetEntry;
2339
2340static QEMUResetEntry *first_reset_entry;
2341static int reset_requested;
2342static int shutdown_requested;
2343static int powerdown_requested;
2344static int debug_requested;
2345static int vmstop_requested;
2346
2347int qemu_shutdown_requested(void)
2348{
2349 int r = shutdown_requested;
2350 shutdown_requested = 0;
2351 return r;
2352}
2353
2354int qemu_reset_requested(void)
2355{
2356 int r = reset_requested;
2357 reset_requested = 0;
2358 return r;
2359}
2360
2361int qemu_powerdown_requested(void)
2362{
2363 int r = powerdown_requested;
2364 powerdown_requested = 0;
2365 return r;
2366}
2367
2368static int qemu_debug_requested(void)
2369{
2370 int r = debug_requested;
2371 debug_requested = 0;
2372 return r;
2373}
2374
2375static int qemu_vmstop_requested(void)
2376{
2377 int r = vmstop_requested;
2378 vmstop_requested = 0;
2379 return r;
2380}
2381
2382static void do_vm_stop(int reason)
2383{
2384 if (vm_running) {
2385 cpu_disable_ticks();
2386 vm_running = 0;
2387 pause_all_vcpus();
2388 vm_state_notify(0, reason);
2389 }
2390}
2391
2392void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2393{
2394 QEMUResetEntry **pre, *re;
2395
2396 pre = &first_reset_entry;
2397 while (*pre != NULL && (*pre)->order >= order) {
2398 pre = &(*pre)->next;
2399 }
2400 re = qemu_mallocz(sizeof(QEMUResetEntry));
2401 re->func = func;
2402 re->opaque = opaque;
2403 re->order = order;
2404 re->next = NULL;
2405 *pre = re;
2406}
2407
2408void qemu_system_reset(void)
2409{
2410 QEMUResetEntry *re;
2411
2412 /* reset all devices */
2413 for(re = first_reset_entry; re != NULL; re = re->next) {
2414 re->func(re->opaque);
2415 }
2416}
2417
2418void qemu_system_reset_request(void)
2419{
2420 if (no_reboot) {
2421 shutdown_requested = 1;
2422 } else {
2423 reset_requested = 1;
2424 }
2425 qemu_notify_event();
2426}
2427
2428void qemu_system_shutdown_request(void)
2429{
2430 shutdown_requested = 1;
2431 qemu_notify_event();
2432}
2433
2434void qemu_system_powerdown_request(void)
2435{
2436 powerdown_requested = 1;
2437 qemu_notify_event();
2438}
2439
2440#ifdef CONFIG_IOTHREAD
2441static void qemu_system_vmstop_request(int reason)
2442{
2443 vmstop_requested = reason;
2444 qemu_notify_event();
2445}
2446#endif
2447
2448#ifndef _WIN32
2449static int io_thread_fd = -1;
2450
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002451#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002452static void qemu_event_increment(void)
2453{
2454 static const char byte = 0;
2455
2456 if (io_thread_fd == -1)
2457 return;
2458
2459 write(io_thread_fd, &byte, sizeof(byte));
2460}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002461#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002462
2463static void qemu_event_read(void *opaque)
2464{
2465 int fd = (unsigned long)opaque;
2466 ssize_t len;
2467
2468 /* Drain the notify pipe */
2469 do {
2470 char buffer[512];
2471 len = read(fd, buffer, sizeof(buffer));
2472 } while ((len == -1 && errno == EINTR) || len > 0);
2473}
2474
2475static int qemu_event_init(void)
2476{
2477 int err;
2478 int fds[2];
2479
2480 err = pipe(fds);
2481 if (err == -1)
2482 return -errno;
2483
2484 err = fcntl_setfl(fds[0], O_NONBLOCK);
2485 if (err < 0)
2486 goto fail;
2487
2488 err = fcntl_setfl(fds[1], O_NONBLOCK);
2489 if (err < 0)
2490 goto fail;
2491
2492 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2493 (void *)(unsigned long)fds[0]);
2494
2495 io_thread_fd = fds[1];
2496 return 0;
2497
2498fail:
2499 close(fds[0]);
2500 close(fds[1]);
2501 return err;
2502}
2503#else
2504HANDLE qemu_event_handle;
2505
2506static void dummy_event_handler(void *opaque)
2507{
2508}
2509
2510static int qemu_event_init(void)
2511{
2512 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2513 if (!qemu_event_handle) {
2514 perror("Failed CreateEvent");
2515 return -1;
2516 }
2517 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2518 return 0;
2519}
2520
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002521#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002522static void qemu_event_increment(void)
2523{
2524 SetEvent(qemu_event_handle);
2525}
2526#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002527#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002528
2529static int cpu_can_run(CPUState *env)
2530{
2531 if (env->stop)
2532 return 0;
2533 if (env->stopped)
2534 return 0;
2535 return 1;
2536}
2537
2538#ifndef CONFIG_IOTHREAD
2539static int qemu_init_main_loop(void)
2540{
2541 return qemu_event_init();
2542}
2543
2544void qemu_init_vcpu(void *_env)
2545{
2546 CPUState *env = _env;
2547
2548 if (kvm_enabled())
2549 kvm_init_vcpu(env);
2550 return;
2551}
2552
2553int qemu_cpu_self(void *env)
2554{
2555 return 1;
2556}
2557
2558static void resume_all_vcpus(void)
2559{
2560}
2561
2562static void pause_all_vcpus(void)
2563{
2564}
2565
2566void qemu_cpu_kick(void *env)
2567{
2568 return;
2569}
2570
2571void qemu_notify_event(void)
2572{
2573 CPUState *env = cpu_single_env;
2574
2575 if (env) {
2576 cpu_exit(env);
2577#ifdef USE_KQEMU
2578 if (env->kqemu_enabled)
2579 kqemu_cpu_interrupt(env);
2580#endif
2581 }
2582}
2583
2584#define qemu_mutex_lock_iothread() do { } while (0)
2585#define qemu_mutex_unlock_iothread() do { } while (0)
2586
2587void vm_stop(int reason)
2588{
2589 do_vm_stop(reason);
2590}
2591
2592#else /* CONFIG_IOTHREAD */
2593
2594#include "qemu-thread.h"
2595
2596QemuMutex qemu_global_mutex;
2597static QemuMutex qemu_fair_mutex;
2598
2599static QemuThread io_thread;
2600
2601static QemuThread *tcg_cpu_thread;
2602static QemuCond *tcg_halt_cond;
2603
2604static int qemu_system_ready;
2605/* cpu creation */
2606static QemuCond qemu_cpu_cond;
2607/* system init */
2608static QemuCond qemu_system_cond;
2609static QemuCond qemu_pause_cond;
2610
2611static void block_io_signals(void);
2612static void unblock_io_signals(void);
2613static int tcg_has_work(void);
2614
2615static int qemu_init_main_loop(void)
2616{
2617 int ret;
2618
2619 ret = qemu_event_init();
2620 if (ret)
2621 return ret;
2622
2623 qemu_cond_init(&qemu_pause_cond);
2624 qemu_mutex_init(&qemu_fair_mutex);
2625 qemu_mutex_init(&qemu_global_mutex);
2626 qemu_mutex_lock(&qemu_global_mutex);
2627
2628 unblock_io_signals();
2629 qemu_thread_self(&io_thread);
2630
2631 return 0;
2632}
2633
2634static void qemu_wait_io_event(CPUState *env)
2635{
2636 while (!tcg_has_work())
2637 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2638
2639 qemu_mutex_unlock(&qemu_global_mutex);
2640
2641 /*
2642 * Users of qemu_global_mutex can be starved, having no chance
2643 * to acquire it since this path will get to it first.
2644 * So use another lock to provide fairness.
2645 */
2646 qemu_mutex_lock(&qemu_fair_mutex);
2647 qemu_mutex_unlock(&qemu_fair_mutex);
2648
2649 qemu_mutex_lock(&qemu_global_mutex);
2650 if (env->stop) {
2651 env->stop = 0;
2652 env->stopped = 1;
2653 qemu_cond_signal(&qemu_pause_cond);
2654 }
2655}
2656
2657static int qemu_cpu_exec(CPUState *env);
2658
2659static void *kvm_cpu_thread_fn(void *arg)
2660{
2661 CPUState *env = arg;
2662
2663 block_io_signals();
2664 qemu_thread_self(env->thread);
2665
2666 /* signal CPU creation */
2667 qemu_mutex_lock(&qemu_global_mutex);
2668 env->created = 1;
2669 qemu_cond_signal(&qemu_cpu_cond);
2670
2671 /* and wait for machine initialization */
2672 while (!qemu_system_ready)
2673 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2674
2675 while (1) {
2676 if (cpu_can_run(env))
2677 qemu_cpu_exec(env);
2678 qemu_wait_io_event(env);
2679 }
2680
2681 return NULL;
2682}
2683
2684static void tcg_cpu_exec(void);
2685
2686static void *tcg_cpu_thread_fn(void *arg)
2687{
2688 CPUState *env = arg;
2689
2690 block_io_signals();
2691 qemu_thread_self(env->thread);
2692
2693 /* signal CPU creation */
2694 qemu_mutex_lock(&qemu_global_mutex);
2695 for (env = first_cpu; env != NULL; env = env->next_cpu)
2696 env->created = 1;
2697 qemu_cond_signal(&qemu_cpu_cond);
2698
2699 /* and wait for machine initialization */
2700 while (!qemu_system_ready)
2701 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2702
2703 while (1) {
2704 tcg_cpu_exec();
2705 qemu_wait_io_event(cur_cpu);
2706 }
2707
2708 return NULL;
2709}
2710
2711void qemu_cpu_kick(void *_env)
2712{
2713 CPUState *env = _env;
2714 qemu_cond_broadcast(env->halt_cond);
2715 if (kvm_enabled())
2716 qemu_thread_signal(env->thread, SIGUSR1);
2717}
2718
2719int qemu_cpu_self(void *env)
2720{
2721 return (cpu_single_env != NULL);
2722}
2723
2724static void cpu_signal(int sig)
2725{
2726 if (cpu_single_env)
2727 cpu_exit(cpu_single_env);
2728}
2729
2730static void block_io_signals(void)
2731{
2732 sigset_t set;
2733 struct sigaction sigact;
2734
2735 sigemptyset(&set);
2736 sigaddset(&set, SIGUSR2);
2737 sigaddset(&set, SIGIO);
2738 sigaddset(&set, SIGALRM);
2739 pthread_sigmask(SIG_BLOCK, &set, NULL);
2740
2741 sigemptyset(&set);
2742 sigaddset(&set, SIGUSR1);
2743 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2744
2745 memset(&sigact, 0, sizeof(sigact));
2746 sigact.sa_handler = cpu_signal;
2747 sigaction(SIGUSR1, &sigact, NULL);
2748}
2749
2750static void unblock_io_signals(void)
2751{
2752 sigset_t set;
2753
2754 sigemptyset(&set);
2755 sigaddset(&set, SIGUSR2);
2756 sigaddset(&set, SIGIO);
2757 sigaddset(&set, SIGALRM);
2758 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2759
2760 sigemptyset(&set);
2761 sigaddset(&set, SIGUSR1);
2762 pthread_sigmask(SIG_BLOCK, &set, NULL);
2763}
2764
2765static void qemu_signal_lock(unsigned int msecs)
2766{
2767 qemu_mutex_lock(&qemu_fair_mutex);
2768
2769 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2770 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2771 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2772 break;
2773 }
2774 qemu_mutex_unlock(&qemu_fair_mutex);
2775}
2776
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002777void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002778{
2779 if (kvm_enabled()) {
2780 qemu_mutex_lock(&qemu_fair_mutex);
2781 qemu_mutex_lock(&qemu_global_mutex);
2782 qemu_mutex_unlock(&qemu_fair_mutex);
2783 } else
2784 qemu_signal_lock(100);
2785}
2786
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002787void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002788{
2789 qemu_mutex_unlock(&qemu_global_mutex);
2790}
2791
2792static int all_vcpus_paused(void)
2793{
2794 CPUState *penv = first_cpu;
2795
2796 while (penv) {
2797 if (!penv->stopped)
2798 return 0;
2799 penv = (CPUState *)penv->next_cpu;
2800 }
2801
2802 return 1;
2803}
2804
2805static void pause_all_vcpus(void)
2806{
2807 CPUState *penv = first_cpu;
2808
2809 while (penv) {
2810 penv->stop = 1;
2811 qemu_thread_signal(penv->thread, SIGUSR1);
2812 qemu_cpu_kick(penv);
2813 penv = (CPUState *)penv->next_cpu;
2814 }
2815
2816 while (!all_vcpus_paused()) {
2817 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2818 penv = first_cpu;
2819 while (penv) {
2820 qemu_thread_signal(penv->thread, SIGUSR1);
2821 penv = (CPUState *)penv->next_cpu;
2822 }
2823 }
2824}
2825
2826static void resume_all_vcpus(void)
2827{
2828 CPUState *penv = first_cpu;
2829
2830 while (penv) {
2831 penv->stop = 0;
2832 penv->stopped = 0;
2833 qemu_thread_signal(penv->thread, SIGUSR1);
2834 qemu_cpu_kick(penv);
2835 penv = (CPUState *)penv->next_cpu;
2836 }
2837}
2838
2839static void tcg_init_vcpu(void *_env)
2840{
2841 CPUState *env = _env;
2842 /* share a single thread for all cpus with TCG */
2843 if (!tcg_cpu_thread) {
2844 env->thread = qemu_mallocz(sizeof(QemuThread));
2845 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2846 qemu_cond_init(env->halt_cond);
2847 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2848 while (env->created == 0)
2849 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2850 tcg_cpu_thread = env->thread;
2851 tcg_halt_cond = env->halt_cond;
2852 } else {
2853 env->thread = tcg_cpu_thread;
2854 env->halt_cond = tcg_halt_cond;
2855 }
2856}
2857
2858static void kvm_start_vcpu(CPUState *env)
2859{
2860#if 0
2861 kvm_init_vcpu(env);
2862 env->thread = qemu_mallocz(sizeof(QemuThread));
2863 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2864 qemu_cond_init(env->halt_cond);
2865 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2866 while (env->created == 0)
2867 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2868#endif
2869}
2870
2871void qemu_init_vcpu(void *_env)
2872{
2873 CPUState *env = _env;
2874
2875 if (kvm_enabled())
2876 kvm_start_vcpu(env);
2877 else
2878 tcg_init_vcpu(env);
2879}
2880
2881void qemu_notify_event(void)
2882{
2883 qemu_event_increment();
2884}
2885
2886void vm_stop(int reason)
2887{
2888 QemuThread me;
2889 qemu_thread_self(&me);
2890
2891 if (!qemu_thread_equal(&me, &io_thread)) {
2892 qemu_system_vmstop_request(reason);
2893 /*
2894 * FIXME: should not return to device code in case
2895 * vm_stop() has been requested.
2896 */
2897 if (cpu_single_env) {
2898 cpu_exit(cpu_single_env);
2899 cpu_single_env->stop = 1;
2900 }
2901 return;
2902 }
2903 do_vm_stop(reason);
2904}
2905
2906#endif
2907
2908
2909#ifdef _WIN32
2910static void host_main_loop_wait(int *timeout)
2911{
2912 int ret, ret2, i;
2913 PollingEntry *pe;
2914
2915
2916 /* XXX: need to suppress polling by better using win32 events */
2917 ret = 0;
2918 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2919 ret |= pe->func(pe->opaque);
2920 }
2921 if (ret == 0) {
2922 int err;
2923 WaitObjects *w = &wait_objects;
2924
2925 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2926 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2927 if (w->func[ret - WAIT_OBJECT_0])
2928 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2929
2930 /* Check for additional signaled events */
2931 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2932
2933 /* Check if event is signaled */
2934 ret2 = WaitForSingleObject(w->events[i], 0);
2935 if(ret2 == WAIT_OBJECT_0) {
2936 if (w->func[i])
2937 w->func[i](w->opaque[i]);
2938 } else if (ret2 == WAIT_TIMEOUT) {
2939 } else {
2940 err = GetLastError();
2941 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2942 }
2943 }
2944 } else if (ret == WAIT_TIMEOUT) {
2945 } else {
2946 err = GetLastError();
2947 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2948 }
2949 }
2950
2951 *timeout = 0;
2952}
2953#else
2954static void host_main_loop_wait(int *timeout)
2955{
2956}
2957#endif
2958
2959void main_loop_wait(int timeout)
2960{
2961 IOHandlerRecord *ioh;
2962 fd_set rfds, wfds, xfds;
2963 int ret, nfds;
2964 struct timeval tv;
2965
2966 qemu_bh_update_timeout(&timeout);
2967
2968 host_main_loop_wait(&timeout);
2969
2970 /* poll any events */
2971 /* XXX: separate device handlers from system ones */
2972 nfds = -1;
2973 FD_ZERO(&rfds);
2974 FD_ZERO(&wfds);
2975 FD_ZERO(&xfds);
2976 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2977 if (ioh->deleted)
2978 continue;
2979 if (ioh->fd_read &&
2980 (!ioh->fd_read_poll ||
2981 ioh->fd_read_poll(ioh->opaque) != 0)) {
2982 FD_SET(ioh->fd, &rfds);
2983 if (ioh->fd > nfds)
2984 nfds = ioh->fd;
2985 }
2986 if (ioh->fd_write) {
2987 FD_SET(ioh->fd, &wfds);
2988 if (ioh->fd > nfds)
2989 nfds = ioh->fd;
2990 }
2991 }
2992
2993 tv.tv_sec = timeout / 1000;
2994 tv.tv_usec = (timeout % 1000) * 1000;
2995
2996#if defined(CONFIG_SLIRP)
2997 if (slirp_is_inited()) {
2998 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2999 }
3000#endif
3001 qemu_mutex_unlock_iothread();
3002 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3003 qemu_mutex_lock_iothread();
3004 if (ret > 0) {
3005 IOHandlerRecord **pioh;
3006
3007 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3008 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
3009 ioh->fd_read(ioh->opaque);
3010 }
3011 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
3012 ioh->fd_write(ioh->opaque);
3013 }
3014 }
3015
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003016 /* remove deleted IO handlers */
3017 pioh = &first_io_handler;
3018 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003019 ioh = *pioh;
3020 if (ioh->deleted) {
3021 *pioh = ioh->next;
3022 qemu_free(ioh);
3023 } else
3024 pioh = &ioh->next;
3025 }
3026 }
3027#if defined(CONFIG_SLIRP)
3028 if (slirp_is_inited()) {
3029 if (ret < 0) {
3030 FD_ZERO(&rfds);
3031 FD_ZERO(&wfds);
3032 FD_ZERO(&xfds);
3033 }
3034 slirp_select_poll(&rfds, &wfds, &xfds);
3035 }
3036#endif
3037 charpipe_poll();
3038
David Turner6a9ef172010-09-09 22:54:36 +02003039 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003040
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003041 /* Check bottom-halves last in case any of the earlier events triggered
3042 them. */
3043 qemu_bh_poll();
3044
3045}
3046
3047static int qemu_cpu_exec(CPUState *env)
3048{
3049 int ret;
3050#ifdef CONFIG_PROFILER
3051 int64_t ti;
3052#endif
3053
3054#ifdef CONFIG_PROFILER
3055 ti = profile_getclock();
3056#endif
3057 if (use_icount) {
3058 int64_t count;
3059 int decr;
3060 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3061 env->icount_decr.u16.low = 0;
3062 env->icount_extra = 0;
3063 count = qemu_next_deadline();
3064 count = (count + (1 << icount_time_shift) - 1)
3065 >> icount_time_shift;
3066 qemu_icount += count;
3067 decr = (count > 0xffff) ? 0xffff : count;
3068 count -= decr;
3069 env->icount_decr.u16.low = decr;
3070 env->icount_extra = count;
3071 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003072#ifdef CONFIG_TRACE
3073 if (tbflush_requested) {
3074 tbflush_requested = 0;
3075 tb_flush(env);
3076 return EXCP_INTERRUPT;
3077 }
3078#endif
3079
3080
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003081 ret = cpu_exec(env);
3082#ifdef CONFIG_PROFILER
3083 qemu_time += profile_getclock() - ti;
3084#endif
3085 if (use_icount) {
3086 /* Fold pending instructions back into the
3087 instruction counter, and clear the interrupt flag. */
3088 qemu_icount -= (env->icount_decr.u16.low
3089 + env->icount_extra);
3090 env->icount_decr.u32 = 0;
3091 env->icount_extra = 0;
3092 }
3093 return ret;
3094}
3095
3096static void tcg_cpu_exec(void)
3097{
3098 int ret = 0;
3099
3100 if (next_cpu == NULL)
3101 next_cpu = first_cpu;
3102 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3103 CPUState *env = cur_cpu = next_cpu;
3104
3105 if (!vm_running)
3106 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003107 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003108 break;
3109 }
3110 if (cpu_can_run(env))
3111 ret = qemu_cpu_exec(env);
3112 if (ret == EXCP_DEBUG) {
3113 gdb_set_stop_cpu(env);
3114 debug_requested = 1;
3115 break;
3116 }
3117 }
3118}
3119
3120static int cpu_has_work(CPUState *env)
3121{
3122 if (env->stop)
3123 return 1;
3124 if (env->stopped)
3125 return 0;
3126 if (!env->halted)
3127 return 1;
3128 if (qemu_cpu_has_work(env))
3129 return 1;
3130 return 0;
3131}
3132
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003133int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003134{
3135 CPUState *env;
3136
3137 for (env = first_cpu; env != NULL; env = env->next_cpu)
3138 if (cpu_has_work(env))
3139 return 1;
3140 return 0;
3141}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003142
3143static int vm_can_run(void)
3144{
3145 if (powerdown_requested)
3146 return 0;
3147 if (reset_requested)
3148 return 0;
3149 if (shutdown_requested)
3150 return 0;
3151 if (debug_requested)
3152 return 0;
3153 return 1;
3154}
3155
3156static void main_loop(void)
3157{
3158 int r;
3159
3160#ifdef CONFIG_IOTHREAD
3161 qemu_system_ready = 1;
3162 qemu_cond_broadcast(&qemu_system_cond);
3163#endif
3164
3165 for (;;) {
3166 do {
3167#ifdef CONFIG_PROFILER
3168 int64_t ti;
3169#endif
3170#ifndef CONFIG_IOTHREAD
3171 tcg_cpu_exec();
3172#endif
3173#ifdef CONFIG_PROFILER
3174 ti = profile_getclock();
3175#endif
3176 main_loop_wait(qemu_calculate_timeout());
3177#ifdef CONFIG_PROFILER
3178 dev_time += profile_getclock() - ti;
3179#endif
rich canningsd952f282011-03-01 15:40:09 -08003180
3181 if (rotate_logs_requested) {
3182 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
3183 dns_log_filename);
3184 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
3185 drop_log_filename);
3186 slirp_dns_log_fd(new_dns_log_fd);
3187 slirp_drop_log_fd(new_drop_log_fd);
3188 reset_rotate_qemu_logs_request();
3189 }
3190
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003191 } while (vm_can_run());
3192
3193 if (qemu_debug_requested())
3194 vm_stop(EXCP_DEBUG);
3195 if (qemu_shutdown_requested()) {
3196 if (no_shutdown) {
3197 vm_stop(0);
3198 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003199 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003200 if (savevm_on_exit != NULL) {
3201 do_savevm(cur_mon, savevm_on_exit);
3202 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003203 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003204 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003205 }
3206 if (qemu_reset_requested()) {
3207 pause_all_vcpus();
3208 qemu_system_reset();
3209 resume_all_vcpus();
3210 }
3211 if (qemu_powerdown_requested())
3212 qemu_system_powerdown();
3213 if ((r = qemu_vmstop_requested()))
3214 vm_stop(r);
3215 }
3216 pause_all_vcpus();
3217}
3218
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003219void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003220{
3221 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3222}
3223
3224void qemu_help(int exitcode)
3225{
3226 version();
3227 printf("usage: %s [options] [disk_image]\n"
3228 "\n"
3229 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3230 "\n"
3231#define DEF(option, opt_arg, opt_enum, opt_help) \
3232 opt_help
3233#define DEFHEADING(text) stringify(text) "\n"
3234#include "qemu-options.h"
3235#undef DEF
3236#undef DEFHEADING
3237#undef GEN_DOCS
3238 "\n"
3239 "During emulation, the following keys are useful:\n"
3240 "ctrl-alt-f toggle full screen\n"
3241 "ctrl-alt-n switch to virtual console 'n'\n"
3242 "ctrl-alt toggle mouse and keyboard grab\n"
3243 "\n"
3244 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3245 ,
3246 "qemu",
3247 DEFAULT_RAM_SIZE,
3248#ifndef _WIN32
3249 DEFAULT_NETWORK_SCRIPT,
3250 DEFAULT_NETWORK_DOWN_SCRIPT,
3251#endif
3252 DEFAULT_GDBSTUB_PORT,
3253 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003254 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003255}
3256
3257#define HAS_ARG 0x0001
3258
3259enum {
3260#define DEF(option, opt_arg, opt_enum, opt_help) \
3261 opt_enum,
3262#define DEFHEADING(text)
3263#include "qemu-options.h"
3264#undef DEF
3265#undef DEFHEADING
3266#undef GEN_DOCS
3267};
3268
3269typedef struct QEMUOption {
3270 const char *name;
3271 int flags;
3272 int index;
3273} QEMUOption;
3274
3275static const QEMUOption qemu_options[] = {
3276 { "h", 0, QEMU_OPTION_h },
3277#define DEF(option, opt_arg, opt_enum, opt_help) \
3278 { option, opt_arg, opt_enum },
3279#define DEFHEADING(text)
3280#include "qemu-options.h"
3281#undef DEF
3282#undef DEFHEADING
3283#undef GEN_DOCS
3284 { NULL, 0, 0 },
3285};
3286
3287#ifdef HAS_AUDIO
3288struct soundhw soundhw[] = {
3289#ifdef HAS_AUDIO_CHOICE
3290#if defined(TARGET_I386) || defined(TARGET_MIPS)
3291 {
3292 "pcspk",
3293 "PC speaker",
3294 0,
3295 1,
3296 { .init_isa = pcspk_audio_init }
3297 },
3298#endif
3299
3300#ifdef CONFIG_SB16
3301 {
3302 "sb16",
3303 "Creative Sound Blaster 16",
3304 0,
3305 1,
3306 { .init_isa = SB16_init }
3307 },
3308#endif
3309
3310#ifdef CONFIG_CS4231A
3311 {
3312 "cs4231a",
3313 "CS4231A",
3314 0,
3315 1,
3316 { .init_isa = cs4231a_init }
3317 },
3318#endif
3319
3320#ifdef CONFIG_ADLIB
3321 {
3322 "adlib",
3323#ifdef HAS_YMF262
3324 "Yamaha YMF262 (OPL3)",
3325#else
3326 "Yamaha YM3812 (OPL2)",
3327#endif
3328 0,
3329 1,
3330 { .init_isa = Adlib_init }
3331 },
3332#endif
3333
3334#ifdef CONFIG_GUS
3335 {
3336 "gus",
3337 "Gravis Ultrasound GF1",
3338 0,
3339 1,
3340 { .init_isa = GUS_init }
3341 },
3342#endif
3343
3344#ifdef CONFIG_AC97
3345 {
3346 "ac97",
3347 "Intel 82801AA AC97 Audio",
3348 0,
3349 0,
3350 { .init_pci = ac97_init }
3351 },
3352#endif
3353
3354#ifdef CONFIG_ES1370
3355 {
3356 "es1370",
3357 "ENSONIQ AudioPCI ES1370",
3358 0,
3359 0,
3360 { .init_pci = es1370_init }
3361 },
3362#endif
3363
3364#endif /* HAS_AUDIO_CHOICE */
3365
3366 { NULL, NULL, 0, 0, { NULL } }
3367};
3368
3369static void select_soundhw (const char *optarg)
3370{
3371 struct soundhw *c;
3372
3373 if (*optarg == '?') {
3374 show_valid_cards:
3375
3376 printf ("Valid sound card names (comma separated):\n");
3377 for (c = soundhw; c->name; ++c) {
3378 printf ("%-11s %s\n", c->name, c->descr);
3379 }
3380 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003381 if (*optarg != '?') {
3382 PANIC("Unknown sound card name: %s", optarg);
3383 } else {
3384 QEMU_EXIT(0);
3385 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003386 }
3387 else {
3388 size_t l;
3389 const char *p;
3390 char *e;
3391 int bad_card = 0;
3392
3393 if (!strcmp (optarg, "all")) {
3394 for (c = soundhw; c->name; ++c) {
3395 c->enabled = 1;
3396 }
3397 return;
3398 }
3399
3400 p = optarg;
3401 while (*p) {
3402 e = strchr (p, ',');
3403 l = !e ? strlen (p) : (size_t) (e - p);
3404
3405 for (c = soundhw; c->name; ++c) {
3406 if (!strncmp (c->name, p, l)) {
3407 c->enabled = 1;
3408 break;
3409 }
3410 }
3411
3412 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003413#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003414 if (l > 80) {
3415 fprintf (stderr,
3416 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003417 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003418 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3419 (int) l, p);
3420 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003421#endif // !CONFIG_ANDROID
3422 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003423 }
3424 p += l + (e != NULL);
3425 }
3426
3427 if (bad_card)
3428 goto show_valid_cards;
3429 }
3430}
3431#endif
3432
3433static void select_vgahw (const char *p)
3434{
3435 const char *opts;
3436
3437 cirrus_vga_enabled = 0;
3438 std_vga_enabled = 0;
3439 vmsvga_enabled = 0;
3440 xenfb_enabled = 0;
3441 if (strstart(p, "std", &opts)) {
3442 std_vga_enabled = 1;
3443 } else if (strstart(p, "cirrus", &opts)) {
3444 cirrus_vga_enabled = 1;
3445 } else if (strstart(p, "vmware", &opts)) {
3446 vmsvga_enabled = 1;
3447 } else if (strstart(p, "xenfb", &opts)) {
3448 xenfb_enabled = 1;
3449 } else if (!strstart(p, "none", &opts)) {
3450 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003451 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003452 }
3453 while (*opts) {
3454 const char *nextopt;
3455
3456 if (strstart(opts, ",retrace=", &nextopt)) {
3457 opts = nextopt;
3458 if (strstart(opts, "dumb", &nextopt))
3459 vga_retrace_method = VGA_RETRACE_DUMB;
3460 else if (strstart(opts, "precise", &nextopt))
3461 vga_retrace_method = VGA_RETRACE_PRECISE;
3462 else goto invalid_vga;
3463 } else goto invalid_vga;
3464 opts = nextopt;
3465 }
3466}
3467
3468#ifdef _WIN32
3469static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3470{
3471 exit(STATUS_CONTROL_C_EXIT);
3472 return TRUE;
3473}
3474#endif
3475
3476int qemu_uuid_parse(const char *str, uint8_t *uuid)
3477{
3478 int ret;
3479
3480 if(strlen(str) != 36)
3481 return -1;
3482
3483 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3484 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3485 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3486
3487 if(ret != 16)
3488 return -1;
3489
3490#ifdef TARGET_I386
3491 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3492#endif
3493
3494 return 0;
3495}
3496
3497#define MAX_NET_CLIENTS 32
3498
3499#ifndef _WIN32
3500
3501static void termsig_handler(int signal)
3502{
3503 qemu_system_shutdown_request();
3504}
3505
3506static void sigchld_handler(int signal)
3507{
3508 waitpid(-1, NULL, WNOHANG);
3509}
3510
3511static void sighandler_setup(void)
3512{
3513 struct sigaction act;
3514
3515 memset(&act, 0, sizeof(act));
3516 act.sa_handler = termsig_handler;
3517 sigaction(SIGINT, &act, NULL);
3518 sigaction(SIGHUP, &act, NULL);
3519 sigaction(SIGTERM, &act, NULL);
3520
3521 act.sa_handler = sigchld_handler;
3522 act.sa_flags = SA_NOCLDSTOP;
3523 sigaction(SIGCHLD, &act, NULL);
3524}
3525
3526#endif
3527
3528#ifdef _WIN32
3529/* Look for support files in the same directory as the executable. */
3530static char *find_datadir(const char *argv0)
3531{
3532 char *p;
3533 char buf[MAX_PATH];
3534 DWORD len;
3535
3536 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3537 if (len == 0) {
3538 return NULL;
3539 }
3540
3541 buf[len] = 0;
3542 p = buf + len - 1;
3543 while (p != buf && *p != '\\')
3544 p--;
3545 *p = 0;
3546 if (access(buf, R_OK) == 0) {
3547 return qemu_strdup(buf);
3548 }
3549 return NULL;
3550}
3551#else /* !_WIN32 */
3552
3553/* Find a likely location for support files using the location of the binary.
3554 For installed binaries this will be "$bindir/../share/qemu". When
David 'Digit' Turnerf0cee2e2011-05-14 10:08:51 +02003555 running from the build tree this will be "$bindir/../usr/share/pc-bios".
Bruce Beare02c63852011-02-24 09:10:52 -08003556 The emulator running from the SDK will find the support files in $bindir/lib/pc-bios. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003557#define SHARE_SUFFIX "/share/qemu"
David 'Digit' Turnerf0cee2e2011-05-14 10:08:51 +02003558#define BUILD_SUFFIX "/usr/share/pc-bios"
Bruce Beare02c63852011-02-24 09:10:52 -08003559#define SDK_SUFFIX "/lib/pc-bios"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003560static char *find_datadir(const char *argv0)
3561{
3562 char *dir;
3563 char *p = NULL;
3564 char *res;
3565#ifdef PATH_MAX
3566 char buf[PATH_MAX];
3567#endif
3568 size_t max_len;
3569
3570#if defined(__linux__)
3571 {
3572 int len;
3573 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3574 if (len > 0) {
3575 buf[len] = 0;
3576 p = buf;
3577 }
3578 }
3579#elif defined(__FreeBSD__)
3580 {
3581 int len;
3582 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3583 if (len > 0) {
3584 buf[len] = 0;
3585 p = buf;
3586 }
3587 }
3588#endif
3589 /* If we don't have any way of figuring out the actual executable
3590 location then try argv[0]. */
3591 if (!p) {
3592#ifdef PATH_MAX
3593 p = buf;
3594#endif
3595 p = realpath(argv0, p);
3596 if (!p) {
3597 return NULL;
3598 }
3599 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003600
Bruce Beare02c63852011-02-24 09:10:52 -08003601#define STRLEN_CONST(str) (sizeof(str)-1)
3602 dir = dirname(p);
3603 max_len = strlen(dir) + 1 +
3604 MAX(STRLEN_CONST(SDK_SUFFIX), MAX(STRLEN_CONST(SHARE_SUFFIX), STRLEN_CONST(BUILD_SUFFIX)));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003605 res = qemu_mallocz(max_len);
Bruce Beare02c63852011-02-24 09:10:52 -08003606
3607 snprintf(res, max_len, "%s%s", dir, SDK_SUFFIX);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003608 if (access(res, R_OK)) {
Bruce Beare02c63852011-02-24 09:10:52 -08003609 dir = dirname(dir);
3610
3611 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
3612 if (access(res, R_OK)) {
3613 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
3614 if (access(res, R_OK)) {
3615 qemu_free(res);
3616 res = NULL;
3617 }
3618 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003619 }
3620#ifndef PATH_MAX
3621 free(p);
3622#endif
3623 return res;
3624}
3625#undef SHARE_SUFFIX
3626#undef BUILD_SUFFIX
3627#endif
3628
3629char *qemu_find_file(int type, const char *name)
3630{
3631 int len;
3632 const char *subdir;
3633 char *buf;
3634
3635 /* If name contains path separators then try it as a straight path. */
3636 if ((strchr(name, '/') || strchr(name, '\\'))
3637 && access(name, R_OK) == 0) {
3638 return strdup(name);
3639 }
3640 switch (type) {
3641 case QEMU_FILE_TYPE_BIOS:
3642 subdir = "";
3643 break;
3644 case QEMU_FILE_TYPE_KEYMAP:
3645 subdir = "keymaps/";
3646 break;
3647 default:
3648 abort();
3649 }
3650 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3651 buf = qemu_mallocz(len);
3652 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3653 if (access(buf, R_OK)) {
3654 qemu_free(buf);
3655 return NULL;
3656 }
3657 return buf;
3658}
3659
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003660static int
3661add_dns_server( const char* server_name )
3662{
3663 SockAddress addr;
3664
3665 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3666 fprintf(stdout,
3667 "### WARNING: can't resolve DNS server name '%s'\n",
3668 server_name );
3669 return -1;
3670 }
3671
3672 fprintf(stderr,
3673 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3674
3675 if ( slirp_add_dns_server( &addr ) < 0 ) {
3676 fprintf(stderr,
3677 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3678 return -1;
3679 }
3680 return 0;
3681}
3682
rich cannings7339b552011-02-16 13:43:44 -08003683/* Parses an integer
3684 * Pararm:
3685 * str String containing a number to be parsed.
3686 * result Passes the parsed integer in this argument
3687 * returns 0 if ok, -1 if failed
3688 */
3689int
3690parse_int(const char *str, int *result)
3691{
3692 char* r;
3693 *result = strtol(str, &r, 0);
3694 if (r == NULL || *r != '\0')
3695 return -1;
3696
3697 return 0;
3698}
3699
rich canningsd952f282011-03-01 15:40:09 -08003700#ifndef _WIN32
3701/*
3702 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3703 */
3704void init_qemu_clear_logs_sig() {
3705 struct sigaction act;
3706 sigfillset(&act.sa_mask);
3707 act.sa_flags = 0;
3708 act.sa_handler = rotate_qemu_logs_handler;
3709 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3710 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3711 exit(-1);
3712 }
3713}
3714#endif
3715
3716
rich cannings7339b552011-02-16 13:43:44 -08003717
3718/* parses a null-terminated string specifying a network port (e.g., "80") or
3719 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3720 * are the same. Returns 0 on success, -1 on error. */
3721
3722int parse_port_range(const char *str, unsigned short *lport,
3723 unsigned short *hport) {
3724
3725 unsigned int low = 0, high = 0;
3726 char *p, *arg = strdup(str);
3727
3728 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3729 p = arg + 1; /* skip '[' */
3730 low = atoi(strtok(p, "-"));
3731 high = atoi(strtok(NULL, "-"));
3732 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3733 *lport = low;
3734 *hport = high;
3735 }
3736 }
3737 else {
3738 low = atoi(arg);
3739 if ((0 < low) && (low < 65535)) {
3740 *lport = low;
3741 *hport = low;
3742 }
3743 }
3744 free(arg);
3745 if (low != 0)
3746 return 0;
3747 return -1;
3748}
3749
3750/*
3751 * Implements the generic port forwarding option
3752 */
3753void
3754net_slirp_forward(const char *optarg)
3755{
3756 /*
3757 * we expect the following format:
3758 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3759 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3760 */
3761 char *argument = strdup(optarg), *p = argument;
3762 char *dst_net, *dst_mask, *dst_port;
3763 char *redirect_ip, *redirect_port;
3764 uint32_t dnet, dmask, rip;
3765 unsigned short dlport, dhport, rport;
3766
3767
3768 dst_net = strtok(p, ":");
3769 dst_mask = strtok(NULL, ":");
3770 dst_port = strtok(NULL, ":");
3771 redirect_ip = strtok(NULL, ":");
3772 redirect_port = strtok(NULL, ":");
3773
3774 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3775 redirect_ip == NULL || redirect_port == NULL) {
3776 fprintf(stderr,
3777 "Invalid argument for -net-forward, we expect "
3778 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3779 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3780 ":redirect_ip:redirect_port: %s\n",
3781 optarg);
3782 exit(1);
3783 }
3784
3785 /* inet_strtoip converts dotted address to host byte order */
3786 if (inet_strtoip(dst_net, &dnet) == -1) {
3787 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3788 exit(1);
3789 }
3790 if (inet_strtoip(dst_mask, &dmask) == -1) {
3791 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3792 exit(1);
3793 }
3794 if (inet_strtoip(redirect_ip, &rip) == -1) {
3795 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3796 exit(1);
3797 }
3798
3799 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3800 fprintf(stderr, "Invalid destination port or port range\n");
3801 exit(1);
3802 }
3803
3804 rport = atoi(redirect_port);
3805 if (!rport) {
3806 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3807 exit(1);
3808 }
3809
3810 dnet &= dmask;
3811
3812 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3813 rip, rport);
3814
3815 free(argument);
3816}
3817
3818
3819/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3820 * entry in the allows list */
3821void
3822slirp_allow(const char *optarg, u_int8_t proto)
3823{
3824 /*
3825 * we expect the following format:
3826 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3827 */
3828 char *argument = strdup(optarg), *p = argument;
3829 char *dst_ip_str, *dst_port_str;
3830 uint32_t dst_ip;
3831 unsigned short dst_lport, dst_hport;
3832
3833 dst_ip_str = strtok(p, ":");
3834 dst_port_str = strtok(NULL, ":");
3835
3836 if (dst_ip_str == NULL || dst_port_str == NULL) {
3837 fprintf(stderr,
3838 "Invalid argument %s for -allow. We expect "
3839 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3840 optarg);
3841 exit(1);
3842 }
3843
3844 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3845 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3846 exit(1);
3847 }
3848 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3849 fprintf(stderr, "Invalid destination port or port range\n");
3850 exit(1);
3851 }
3852
3853 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3854
3855 free(argument);
3856}
3857
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003858/* Add a serial device at a given location in the emulated hardware table.
3859 * On failure, this function aborts the program with an error message.
3860 */
3861static void
3862serial_hds_add_at(int index, const char* devname)
3863{
3864 char label[32];
3865
3866 if (!devname || !strcmp(devname,"none"))
3867 return;
3868
3869 if (index >= MAX_SERIAL_PORTS) {
3870 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3871 devname, index, MAX_SERIAL_PORTS);
3872 }
3873 if (serial_hds[index] != NULL) {
3874 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3875 devname, index);
3876 }
3877 snprintf(label, sizeof(label), "serial%d", index);
3878 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3879 if (!serial_hds[index]) {
3880 PANIC("qemu: could not open serial device '%s'", devname);
3881 }
3882}
3883
3884
3885/* Find a free slot in the emulated serial device table, and register
3886 * it. Return the allocated table index.
3887 */
3888static int
3889serial_hds_add(const char* devname)
3890{
3891 int index;
3892
3893 /* Find first free slot */
3894 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3895 if (serial_hds[index] == NULL) {
3896 serial_hds_add_at(index, devname);
3897 return index;
3898 }
3899 }
3900
3901 PANIC("qemu: too many serial devices registered (%d)", index);
3902 return -1; /* shouldn't happen */
3903}
3904
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003905int main(int argc, char **argv, char **envp)
3906{
3907 const char *gdbstub_dev = NULL;
3908 uint32_t boot_devices_bitmap = 0;
3909 int i;
3910 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003911 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003912 const char *initrd_filename;
3913 const char *kernel_filename, *kernel_cmdline;
3914 const char *boot_devices = "";
3915 DisplayState *ds;
3916 DisplayChangeListener *dcl;
3917 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003918 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003919 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003920 const char *net_clients[MAX_NET_CLIENTS];
3921 int nb_net_clients;
3922 const char *bt_opts[MAX_BT_CMDLINE];
3923 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003924 int optind;
3925 const char *r, *optarg;
3926 CharDriverState *monitor_hd = NULL;
3927 const char *monitor_device;
3928 const char *serial_devices[MAX_SERIAL_PORTS];
3929 int serial_device_index;
3930 const char *parallel_devices[MAX_PARALLEL_PORTS];
3931 int parallel_device_index;
3932 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3933 int virtio_console_index;
3934 const char *loadvm = NULL;
3935 QEMUMachine *machine;
3936 const char *cpu_model;
3937 const char *usb_devices[MAX_USB_CMDLINE];
3938 int usb_devices_index;
3939#ifndef _WIN32
3940 int fds[2];
3941#endif
3942 int tb_size;
3943 const char *pid_file = NULL;
3944 const char *incoming = NULL;
3945#ifndef _WIN32
3946 int fd = 0;
3947 struct passwd *pwd = NULL;
3948 const char *chroot_dir = NULL;
3949 const char *run_as = NULL;
3950#endif
3951 CPUState *env;
3952 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003953 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003954 STRALLOC_DEFINE(kernel_params);
3955 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003956 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003957
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003958 /* Initialize sockets before anything else, so we can properly report
3959 * initialization failures back to the UI. */
3960#ifdef _WIN32
3961 socket_init();
3962#endif
3963
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003964 init_clocks();
3965
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003966 qemu_cache_utils_init(envp);
3967
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003968 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003969#ifndef _WIN32
3970 {
3971 struct sigaction act;
3972 sigfillset(&act.sa_mask);
3973 act.sa_flags = 0;
3974 act.sa_handler = SIG_IGN;
3975 sigaction(SIGPIPE, &act, NULL);
3976 }
3977#else
3978 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3979 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3980 QEMU to run on a single CPU */
3981 {
3982 HANDLE h;
3983 DWORD mask, smask;
3984 int i;
3985 h = GetCurrentProcess();
3986 if (GetProcessAffinityMask(h, &mask, &smask)) {
3987 for(i = 0; i < 32; i++) {
3988 if (mask & (1 << i))
3989 break;
3990 }
3991 if (i != 32) {
3992 mask = 1 << i;
3993 SetProcessAffinityMask(h, mask);
3994 }
3995 }
3996 }
3997#endif
3998
3999 module_call_init(MODULE_INIT_MACHINE);
4000 machine = find_default_machine();
4001 cpu_model = NULL;
4002 initrd_filename = NULL;
4003 ram_size = 0;
4004 snapshot = 0;
4005 kernel_filename = NULL;
4006 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004007
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004008 cyls = heads = secs = 0;
4009 translation = BIOS_ATA_TRANSLATION_AUTO;
4010 monitor_device = "vc:80Cx24C";
4011
4012 serial_devices[0] = "vc:80Cx24C";
4013 for(i = 1; i < MAX_SERIAL_PORTS; i++)
4014 serial_devices[i] = NULL;
4015 serial_device_index = 0;
4016
4017 parallel_devices[0] = "vc:80Cx24C";
4018 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
4019 parallel_devices[i] = NULL;
4020 parallel_device_index = 0;
4021
4022 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
4023 virtio_consoles[i] = NULL;
4024 virtio_console_index = 0;
4025
4026 for (i = 0; i < MAX_NODES; i++) {
4027 node_mem[i] = 0;
4028 node_cpumask[i] = 0;
4029 }
4030
4031 usb_devices_index = 0;
4032
4033 nb_net_clients = 0;
4034 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004035#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004036 nb_drives = 0;
4037 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004038#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004039 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004040
4041 nb_nics = 0;
4042
4043 tb_size = 0;
4044 autostart= 1;
4045
4046 register_watchdogs();
4047
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004048 /* Initialize boot properties. */
4049 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02004050 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02004051 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004052
David 'Digit' Turner36597752011-05-20 01:18:01 +02004053#ifdef CONFIG_KVM
4054 /* By default, force auto-detection for kvm */
4055 kvm_allowed = -1;
4056#endif
4057
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004058 optind = 1;
4059 for(;;) {
4060 if (optind >= argc)
4061 break;
4062 r = argv[optind];
4063 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004064 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004065 } else {
4066 const QEMUOption *popt;
4067
4068 optind++;
4069 /* Treat --foo the same as -foo. */
4070 if (r[1] == '-')
4071 r++;
4072 popt = qemu_options;
4073 for(;;) {
4074 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004075 PANIC("%s: invalid option -- '%s'",
4076 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004077 }
4078 if (!strcmp(popt->name, r + 1))
4079 break;
4080 popt++;
4081 }
4082 if (popt->flags & HAS_ARG) {
4083 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004084 PANIC("%s: option '%s' requires an argument",
4085 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004086 }
4087 optarg = argv[optind++];
4088 } else {
4089 optarg = NULL;
4090 }
4091
4092 switch(popt->index) {
4093 case QEMU_OPTION_M:
4094 machine = find_machine(optarg);
4095 if (!machine) {
4096 QEMUMachine *m;
4097 printf("Supported machines are:\n");
4098 for(m = first_machine; m != NULL; m = m->next) {
4099 printf("%-10s %s%s\n",
4100 m->name, m->desc,
4101 m->is_default ? " (default)" : "");
4102 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004103 if (*optarg != '?') {
4104 PANIC("Invalid machine parameter: %s",
4105 optarg);
4106 } else {
4107 QEMU_EXIT(0);
4108 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004109 }
4110 break;
4111 case QEMU_OPTION_cpu:
4112 /* hw initialization will check this */
4113 if (*optarg == '?') {
4114/* XXX: implement xxx_cpu_list for targets that still miss it */
4115#if defined(cpu_list)
4116 cpu_list(stdout, &fprintf);
4117#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004118 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004119 } else {
4120 cpu_model = optarg;
4121 }
4122 break;
4123 case QEMU_OPTION_initrd:
4124 initrd_filename = optarg;
4125 break;
4126 case QEMU_OPTION_hda:
4127 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004128 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004129 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004130 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004131 ",cyls=%d,heads=%d,secs=%d%s",
4132 0, cyls, heads, secs,
4133 translation == BIOS_ATA_TRANSLATION_LBA ?
4134 ",trans=lba" :
4135 translation == BIOS_ATA_TRANSLATION_NONE ?
4136 ",trans=none" : "");
4137 break;
4138 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004139 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
4140 break;
4141
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004142 case QEMU_OPTION_hdc:
4143 case QEMU_OPTION_hdd:
4144 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4145 break;
4146 case QEMU_OPTION_drive:
4147 drive_add(NULL, "%s", optarg);
4148 break;
4149 case QEMU_OPTION_mtdblock:
4150 drive_add(optarg, MTD_ALIAS);
4151 break;
4152 case QEMU_OPTION_sd:
4153 drive_add(optarg, SD_ALIAS);
4154 break;
4155 case QEMU_OPTION_pflash:
4156 drive_add(optarg, PFLASH_ALIAS);
4157 break;
4158 case QEMU_OPTION_snapshot:
4159 snapshot = 1;
4160 break;
4161 case QEMU_OPTION_hdachs:
4162 {
4163 const char *p;
4164 p = optarg;
4165 cyls = strtol(p, (char **)&p, 0);
4166 if (cyls < 1 || cyls > 16383)
4167 goto chs_fail;
4168 if (*p != ',')
4169 goto chs_fail;
4170 p++;
4171 heads = strtol(p, (char **)&p, 0);
4172 if (heads < 1 || heads > 16)
4173 goto chs_fail;
4174 if (*p != ',')
4175 goto chs_fail;
4176 p++;
4177 secs = strtol(p, (char **)&p, 0);
4178 if (secs < 1 || secs > 63)
4179 goto chs_fail;
4180 if (*p == ',') {
4181 p++;
4182 if (!strcmp(p, "none"))
4183 translation = BIOS_ATA_TRANSLATION_NONE;
4184 else if (!strcmp(p, "lba"))
4185 translation = BIOS_ATA_TRANSLATION_LBA;
4186 else if (!strcmp(p, "auto"))
4187 translation = BIOS_ATA_TRANSLATION_AUTO;
4188 else
4189 goto chs_fail;
4190 } else if (*p != '\0') {
4191 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004192 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004193 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004194 if (hda_opts != NULL) {
4195 char num[16];
4196 snprintf(num, sizeof(num), "%d", cyls);
4197 qemu_opt_set(hda_opts, "cyls", num);
4198 snprintf(num, sizeof(num), "%d", heads);
4199 qemu_opt_set(hda_opts, "heads", num);
4200 snprintf(num, sizeof(num), "%d", secs);
4201 qemu_opt_set(hda_opts, "secs", num);
4202 if (translation == BIOS_ATA_TRANSLATION_LBA)
4203 qemu_opt_set(hda_opts, "trans", "lba");
4204 if (translation == BIOS_ATA_TRANSLATION_NONE)
4205 qemu_opt_set(hda_opts, "trans", "none");
4206 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004207 }
4208 break;
4209 case QEMU_OPTION_numa:
4210 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004211 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004212 }
4213 numa_add(optarg);
4214 break;
4215 case QEMU_OPTION_nographic:
4216 display_type = DT_NOGRAPHIC;
4217 break;
4218#ifdef CONFIG_CURSES
4219 case QEMU_OPTION_curses:
4220 display_type = DT_CURSES;
4221 break;
4222#endif
4223 case QEMU_OPTION_portrait:
4224 graphic_rotate = 1;
4225 break;
4226 case QEMU_OPTION_kernel:
4227 kernel_filename = optarg;
4228 break;
4229 case QEMU_OPTION_append:
4230 kernel_cmdline = optarg;
4231 break;
4232 case QEMU_OPTION_cdrom:
4233 drive_add(optarg, CDROM_ALIAS);
4234 break;
4235 case QEMU_OPTION_boot:
4236 boot_devices = optarg;
4237 /* We just do some generic consistency checks */
4238 {
4239 /* Could easily be extended to 64 devices if needed */
4240 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004241
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004242 boot_devices_bitmap = 0;
4243 for (p = boot_devices; *p != '\0'; p++) {
4244 /* Allowed boot devices are:
4245 * a b : floppy disk drives
4246 * c ... f : IDE disk drives
4247 * g ... m : machine implementation dependant drives
4248 * n ... p : network devices
4249 * It's up to each machine implementation to check
4250 * if the given boot devices match the actual hardware
4251 * implementation and firmware features.
4252 */
4253 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004254 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004255 }
4256 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004257 PANIC(
4258 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004259 }
4260 boot_devices_bitmap |= 1 << (*p - 'a');
4261 }
4262 }
4263 break;
4264 case QEMU_OPTION_fda:
4265 case QEMU_OPTION_fdb:
4266 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4267 break;
4268#ifdef TARGET_I386
4269 case QEMU_OPTION_no_fd_bootchk:
4270 fd_bootchk = 0;
4271 break;
4272#endif
4273 case QEMU_OPTION_net:
4274 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004275 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004276 }
4277 net_clients[nb_net_clients] = optarg;
4278 nb_net_clients++;
4279 break;
4280#ifdef CONFIG_SLIRP
4281 case QEMU_OPTION_tftp:
4282 tftp_prefix = optarg;
4283 break;
4284 case QEMU_OPTION_bootp:
4285 bootp_filename = optarg;
4286 break;
4287#if 0 /* ANDROID disabled */
4288#ifndef _WIN32
4289 case QEMU_OPTION_smb:
4290 net_slirp_smb(optarg);
4291 break;
4292#endif
4293#endif /* ANDROID */
4294 case QEMU_OPTION_redir:
4295 net_slirp_redir(NULL, optarg, NULL);
4296 break;
4297#endif
4298 case QEMU_OPTION_bt:
4299 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004300 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004301 }
4302 bt_opts[nb_bt_opts++] = optarg;
4303 break;
4304#ifdef HAS_AUDIO
4305 case QEMU_OPTION_audio_help:
4306 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004307 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004308 break;
4309 case QEMU_OPTION_soundhw:
4310 select_soundhw (optarg);
4311 break;
4312#endif
4313 case QEMU_OPTION_h:
4314 qemu_help(0);
4315 break;
4316 case QEMU_OPTION_version:
4317 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004318 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004319 break;
4320 case QEMU_OPTION_m: {
4321 uint64_t value;
4322 char *ptr;
4323
4324 value = strtoul(optarg, &ptr, 10);
4325 switch (*ptr) {
4326 case 0: case 'M': case 'm':
4327 value <<= 20;
4328 break;
4329 case 'G': case 'g':
4330 value <<= 30;
4331 break;
4332 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004333 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004334 }
4335
4336 /* On 32-bit hosts, QEMU is limited by virtual address space */
4337 if (value > (2047 << 20)
4338#ifndef CONFIG_KQEMU
4339 && HOST_LONG_BITS == 32
4340#endif
4341 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004342 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004343 }
4344 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004345 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004346 }
4347 ram_size = value;
4348 break;
4349 }
4350 case QEMU_OPTION_d:
4351 {
4352 int mask;
4353 const CPULogItem *item;
4354
4355 mask = cpu_str_to_log_mask(optarg);
4356 if (!mask) {
4357 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004358 for(item = cpu_log_items; item->mask != 0; item++) {
4359 printf("%-10s %s\n", item->name, item->help);
4360 }
4361 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004362 }
4363 cpu_set_log(mask);
4364 }
4365 break;
4366 case QEMU_OPTION_s:
4367 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4368 break;
4369 case QEMU_OPTION_gdb:
4370 gdbstub_dev = optarg;
4371 break;
4372 case QEMU_OPTION_L:
4373 data_dir = optarg;
4374 break;
4375 case QEMU_OPTION_bios:
4376 bios_name = optarg;
4377 break;
4378 case QEMU_OPTION_singlestep:
4379 singlestep = 1;
4380 break;
4381 case QEMU_OPTION_S:
4382#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004383 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004384#endif
4385 autostart = 0;
4386 break;
4387#ifndef _WIN32
4388 case QEMU_OPTION_k:
4389 keyboard_layout = optarg;
4390 break;
4391#endif
4392 case QEMU_OPTION_localtime:
4393 rtc_utc = 0;
4394 break;
4395 case QEMU_OPTION_vga:
4396 select_vgahw (optarg);
4397 break;
4398#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4399 case QEMU_OPTION_g:
4400 {
4401 const char *p;
4402 int w, h, depth;
4403 p = optarg;
4404 w = strtol(p, (char **)&p, 10);
4405 if (w <= 0) {
4406 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004407 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004408 }
4409 if (*p != 'x')
4410 goto graphic_error;
4411 p++;
4412 h = strtol(p, (char **)&p, 10);
4413 if (h <= 0)
4414 goto graphic_error;
4415 if (*p == 'x') {
4416 p++;
4417 depth = strtol(p, (char **)&p, 10);
4418 if (depth != 8 && depth != 15 && depth != 16 &&
4419 depth != 24 && depth != 32)
4420 goto graphic_error;
4421 } else if (*p == '\0') {
4422 depth = graphic_depth;
4423 } else {
4424 goto graphic_error;
4425 }
4426
4427 graphic_width = w;
4428 graphic_height = h;
4429 graphic_depth = depth;
4430 }
4431 break;
4432#endif
4433 case QEMU_OPTION_echr:
4434 {
4435 char *r;
4436 term_escape_char = strtol(optarg, &r, 0);
4437 if (r == optarg)
4438 printf("Bad argument to echr\n");
4439 break;
4440 }
4441 case QEMU_OPTION_monitor:
4442 monitor_device = optarg;
4443 break;
4444 case QEMU_OPTION_serial:
4445 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004446 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004447 }
4448 serial_devices[serial_device_index] = optarg;
4449 serial_device_index++;
4450 break;
4451 case QEMU_OPTION_watchdog:
4452 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004453 if (i > 0) {
4454 if (i == 1) {
4455 PANIC("Invalid watchdog parameter: %s",
4456 optarg);
4457 } else {
4458 QEMU_EXIT(0);
4459 }
4460 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004461 break;
4462 case QEMU_OPTION_watchdog_action:
4463 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004464 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004465 }
4466 break;
4467 case QEMU_OPTION_virtiocon:
4468 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004469 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004470 }
4471 virtio_consoles[virtio_console_index] = optarg;
4472 virtio_console_index++;
4473 break;
4474 case QEMU_OPTION_parallel:
4475 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004476 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004477 }
4478 parallel_devices[parallel_device_index] = optarg;
4479 parallel_device_index++;
4480 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004481 case QEMU_OPTION_loadvm:
4482 loadvm = optarg;
4483 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004484 case QEMU_OPTION_savevm_on_exit:
4485 savevm_on_exit = optarg;
4486 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004487 case QEMU_OPTION_full_screen:
4488 full_screen = 1;
4489 break;
4490#ifdef CONFIG_SDL
4491 case QEMU_OPTION_no_frame:
4492 no_frame = 1;
4493 break;
4494 case QEMU_OPTION_alt_grab:
4495 alt_grab = 1;
4496 break;
4497 case QEMU_OPTION_no_quit:
4498 no_quit = 1;
4499 break;
4500 case QEMU_OPTION_sdl:
4501 display_type = DT_SDL;
4502 break;
4503#endif
4504 case QEMU_OPTION_pidfile:
4505 pid_file = optarg;
4506 break;
4507#ifdef TARGET_I386
4508 case QEMU_OPTION_win2k_hack:
4509 win2k_install_hack = 1;
4510 break;
4511 case QEMU_OPTION_rtc_td_hack:
4512 rtc_td_hack = 1;
4513 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004514#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004515 case QEMU_OPTION_acpitable:
4516 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004517 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004518 }
4519 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004520#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004521 case QEMU_OPTION_smbios:
4522 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004523 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004524 }
4525 break;
4526#endif
4527#ifdef CONFIG_KQEMU
4528 case QEMU_OPTION_no_kqemu:
4529 kqemu_allowed = 0;
4530 break;
4531 case QEMU_OPTION_kernel_kqemu:
4532 kqemu_allowed = 2;
4533 break;
4534#endif
4535#ifdef CONFIG_KVM
4536 case QEMU_OPTION_enable_kvm:
4537 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004538 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02004539 case QEMU_OPTION_disable_kvm:
4540 kvm_allowed = 0;
4541 break;
4542#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004543 case QEMU_OPTION_usb:
4544 usb_enabled = 1;
4545 break;
4546 case QEMU_OPTION_usbdevice:
4547 usb_enabled = 1;
4548 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004549 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004550 }
4551 usb_devices[usb_devices_index] = optarg;
4552 usb_devices_index++;
4553 break;
4554 case QEMU_OPTION_smp:
4555 smp_cpus = atoi(optarg);
4556 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004557 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004558 }
4559 break;
4560 case QEMU_OPTION_vnc:
4561 display_type = DT_VNC;
4562 vnc_display = optarg;
4563 break;
4564#ifdef TARGET_I386
4565 case QEMU_OPTION_no_acpi:
4566 acpi_enabled = 0;
4567 break;
4568 case QEMU_OPTION_no_hpet:
4569 no_hpet = 1;
4570 break;
4571 case QEMU_OPTION_no_virtio_balloon:
4572 no_virtio_balloon = 1;
4573 break;
4574#endif
4575 case QEMU_OPTION_no_reboot:
4576 no_reboot = 1;
4577 break;
4578 case QEMU_OPTION_no_shutdown:
4579 no_shutdown = 1;
4580 break;
4581 case QEMU_OPTION_show_cursor:
4582 cursor_hide = 0;
4583 break;
4584 case QEMU_OPTION_uuid:
4585 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004586 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004587 }
4588 break;
4589#ifndef _WIN32
4590 case QEMU_OPTION_daemonize:
4591 daemonize = 1;
4592 break;
4593#endif
4594 case QEMU_OPTION_option_rom:
4595 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004596 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004597 }
4598 option_rom[nb_option_roms] = optarg;
4599 nb_option_roms++;
4600 break;
4601#if defined(TARGET_ARM) || defined(TARGET_M68K)
4602 case QEMU_OPTION_semihosting:
4603 semihosting_enabled = 1;
4604 break;
4605#endif
4606 case QEMU_OPTION_name:
4607 qemu_name = optarg;
4608 break;
4609#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4610 case QEMU_OPTION_prom_env:
4611 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004612 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004613 }
4614 prom_envs[nb_prom_envs] = optarg;
4615 nb_prom_envs++;
4616 break;
4617#endif
4618#ifdef TARGET_ARM
4619 case QEMU_OPTION_old_param:
4620 old_param = 1;
4621 break;
4622#endif
4623 case QEMU_OPTION_clock:
4624 configure_alarms(optarg);
4625 break;
4626 case QEMU_OPTION_startdate:
4627 {
4628 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004629 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004630 if (!strcmp(optarg, "now")) {
4631 rtc_date_offset = -1;
4632 } else {
4633 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4634 &tm.tm_year,
4635 &tm.tm_mon,
4636 &tm.tm_mday,
4637 &tm.tm_hour,
4638 &tm.tm_min,
4639 &tm.tm_sec) == 6) {
4640 /* OK */
4641 } else if (sscanf(optarg, "%d-%d-%d",
4642 &tm.tm_year,
4643 &tm.tm_mon,
4644 &tm.tm_mday) == 3) {
4645 tm.tm_hour = 0;
4646 tm.tm_min = 0;
4647 tm.tm_sec = 0;
4648 } else {
4649 goto date_fail;
4650 }
4651 tm.tm_year -= 1900;
4652 tm.tm_mon--;
4653 rtc_start_date = mktimegm(&tm);
4654 if (rtc_start_date == -1) {
4655 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004656 PANIC("Invalid date format. Valid format are:\n"
4657 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004658 }
4659 rtc_date_offset = time(NULL) - rtc_start_date;
4660 }
4661 }
4662 break;
rich cannings7339b552011-02-16 13:43:44 -08004663
4664 /* -------------------------------------------------------*/
4665 /* User mode network stack restrictions */
4666 case QEMU_OPTION_drop_udp:
4667 slirp_drop_udp();
4668 break;
4669 case QEMU_OPTION_drop_tcp:
4670 slirp_drop_tcp();
4671 break;
4672 case QEMU_OPTION_allow_tcp:
4673 slirp_allow(optarg, IPPROTO_TCP);
4674 break;
4675 case QEMU_OPTION_allow_udp:
4676 slirp_allow(optarg, IPPROTO_UDP);
4677 break;
4678 case QEMU_OPTION_drop_log:
4679 {
4680 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004681 drop_log_filename = optarg;
4682 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004683
4684 if (!drop_log_fd) {
4685 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4686 exit(1);
4687 }
4688
4689 slirp_drop_log_fd(drop_log_fd);
4690 }
4691 break;
4692
4693 case QEMU_OPTION_dns_log:
4694 {
4695 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004696 dns_log_filename = optarg;
4697 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004698
4699 if (dns_log_fd == NULL) {
4700 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4701 exit(1);
4702 }
4703
4704 slirp_dns_log_fd(dns_log_fd);
4705 }
4706 break;
4707
4708
4709 case QEMU_OPTION_max_dns_conns:
4710 {
4711 int max_dns_conns = 0;
4712 if (parse_int(optarg, &max_dns_conns)) {
4713 fprintf(stderr,
4714 "qemu: syntax: -max-dns-conns max_connections\n");
4715 exit(1);
4716 }
4717 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4718 fprintf(stderr,
4719 "Invalid arg for max dns connections: %s\n",
4720 optarg);
4721 exit(1);
4722 }
4723 slirp_set_max_dns_conns(max_dns_conns);
4724 }
4725 break;
4726
4727 case QEMU_OPTION_net_forward:
4728 net_slirp_forward(optarg);
4729 break;
4730 case QEMU_OPTION_net_forward_tcp2sink:
4731 {
4732 SockAddress saddr;
4733
4734 if (parse_host_port(&saddr, optarg)) {
4735 fprintf(stderr,
4736 "Invalid ip/port %s for "
4737 "-forward-dropped-tcp2sink. "
4738 "We expect 'sink_ip:sink_port'\n",
4739 optarg);
4740 exit(1);
4741 }
4742 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4743 saddr.u.inet.port);
4744 }
4745 break;
4746 /* -------------------------------------------------------*/
4747
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004748 case QEMU_OPTION_tb_size:
4749 tb_size = strtol(optarg, NULL, 0);
4750 if (tb_size < 0)
4751 tb_size = 0;
4752 break;
4753 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004754 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004755 break;
4756 case QEMU_OPTION_incoming:
4757 incoming = optarg;
4758 break;
4759#ifndef _WIN32
4760 case QEMU_OPTION_chroot:
4761 chroot_dir = optarg;
4762 break;
4763 case QEMU_OPTION_runas:
4764 run_as = optarg;
4765 break;
4766#endif
4767#ifdef CONFIG_XEN
4768 case QEMU_OPTION_xen_domid:
4769 xen_domid = atoi(optarg);
4770 break;
4771 case QEMU_OPTION_xen_create:
4772 xen_mode = XEN_CREATE;
4773 break;
4774 case QEMU_OPTION_xen_attach:
4775 xen_mode = XEN_ATTACH;
4776 break;
4777#endif
4778
4779
4780 case QEMU_OPTION_mic:
4781 audio_input_source = (char*)optarg;
4782 break;
4783#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004784 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004785 trace_filename = optarg;
4786 tracing = 1;
4787 break;
4788#if 0
4789 case QEMU_OPTION_trace_miss:
4790 trace_cache_miss = 1;
4791 break;
4792 case QEMU_OPTION_trace_addr:
4793 trace_all_addr = 1;
4794 break;
4795#endif
4796 case QEMU_OPTION_tracing:
4797 if (strcmp(optarg, "off") == 0)
4798 tracing = 0;
4799 else if (strcmp(optarg, "on") == 0 && trace_filename)
4800 tracing = 1;
4801 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004802 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004803 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004804 }
4805 break;
4806#if 0
4807 case QEMU_OPTION_dcache_load_miss:
4808 dcache_load_miss_penalty = atoi(optarg);
4809 break;
4810 case QEMU_OPTION_dcache_store_miss:
4811 dcache_store_miss_penalty = atoi(optarg);
4812 break;
4813#endif
4814#endif
4815#ifdef CONFIG_NAND
4816 case QEMU_OPTION_nand:
4817 nand_add_dev(optarg);
4818 break;
4819#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004820 case QEMU_OPTION_android_ports:
4821 android_op_ports = (char*)optarg;
4822 break;
4823
4824 case QEMU_OPTION_android_port:
4825 android_op_port = (char*)optarg;
4826 break;
4827
4828 case QEMU_OPTION_android_report_console:
4829 android_op_report_console = (char*)optarg;
4830 break;
4831
4832 case QEMU_OPTION_http_proxy:
4833 op_http_proxy = (char*)optarg;
4834 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004835
4836 case QEMU_OPTION_charmap:
4837 op_charmap_file = (char*)optarg;
4838 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004839
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004840 case QEMU_OPTION_android_hw:
4841 android_op_hwini = (char*)optarg;
4842 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004843
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004844 case QEMU_OPTION_dns_server:
4845 android_op_dns_server = (char*)optarg;
4846 break;
4847
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004848 case QEMU_OPTION_radio:
4849 android_op_radio = (char*)optarg;
4850 break;
4851
4852 case QEMU_OPTION_gps:
4853 android_op_gps = (char*)optarg;
4854 break;
4855
4856 case QEMU_OPTION_audio:
4857 android_op_audio = (char*)optarg;
4858 break;
4859
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004860 case QEMU_OPTION_cpu_delay:
4861 android_op_cpu_delay = (char*)optarg;
4862 break;
4863
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004864 case QEMU_OPTION_show_kernel:
4865 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4866 break;
4867
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004868#ifdef CONFIG_NAND_LIMITS
4869 case QEMU_OPTION_nand_limits:
4870 android_op_nand_limits = (char*)optarg;
4871 break;
4872#endif // CONFIG_NAND_LIMITS
4873
4874 case QEMU_OPTION_netspeed:
4875 android_op_netspeed = (char*)optarg;
4876 break;
4877
4878 case QEMU_OPTION_netdelay:
4879 android_op_netdelay = (char*)optarg;
4880 break;
4881
4882 case QEMU_OPTION_netfast:
4883 android_op_netfast = 1;
4884 break;
4885
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004886 case QEMU_OPTION_tcpdump:
4887 android_op_tcpdump = (char*)optarg;
4888 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004889
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004890 case QEMU_OPTION_boot_property:
4891 boot_property_parse_option((char*)optarg);
4892 break;
4893
4894 case QEMU_OPTION_lcd_density:
4895 android_op_lcd_density = (char*)optarg;
4896 break;
4897
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004898 case QEMU_OPTION_ui_port:
4899 android_op_ui_port = (char*)optarg;
4900 break;
4901
4902 case QEMU_OPTION_ui_settings:
4903 android_op_ui_settings = (char*)optarg;
4904 break;
4905
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004906 case QEMU_OPTION_audio_test_out:
4907 android_audio_test_start_out();
4908 break;
4909
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004910 case QEMU_OPTION_android_avdname:
4911 android_op_avd_name = (char*)optarg;
4912 break;
4913
4914 case QEMU_OPTION_timezone:
4915 if (timezone_set((char*)optarg)) {
4916 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4917 (char*)optarg);
4918 }
4919 break;
4920
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004921#ifdef CONFIG_MEMCHECK
4922 case QEMU_OPTION_android_memcheck:
4923 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004924 /* This will set ro.kernel.memcheck system property
4925 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004926 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004927 break;
4928#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004929
4930 case QEMU_OPTION_snapshot_no_time_update:
4931 android_snapshot_update_time = 0;
4932 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004933 }
4934 }
4935 }
4936
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004937 /* Initialize character map. */
4938 if (android_charmap_setup(op_charmap_file)) {
4939 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004940 PANIC(
4941 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004942 op_charmap_file);
4943 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004944 PANIC(
4945 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004946 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004947 }
4948
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004949 /* If no data_dir is specified then try to find it relative to the
4950 executable path. */
4951 if (!data_dir) {
4952 data_dir = find_datadir(argv[0]);
4953 }
4954 /* If all else fails use the install patch specified when building. */
4955 if (!data_dir) {
4956 data_dir = CONFIG_QEMU_SHAREDIR;
4957 }
4958
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004959 if (!android_op_hwini) {
4960 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004961 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004962 hw_ini = iniFile_newFromFile(android_op_hwini);
4963 if (hw_ini == NULL) {
4964 PANIC("Could not find %s file.", android_op_hwini);
4965 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004966
4967 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004968 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004969
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004970 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004971
4972 {
4973 int width = android_hw->hw_lcd_width;
4974 int height = android_hw->hw_lcd_height;
4975 int depth = android_hw->hw_lcd_depth;
4976
4977 /* A bit of sanity checking */
4978 if (width <= 0 || height <= 0 ||
4979 (depth != 16 && depth != 32) ||
4980 (((width|height) & 3) != 0) )
4981 {
4982 PANIC("Invalid display configuration (%d,%d,%d)",
4983 width, height, depth);
4984 }
4985 android_display_width = width;
4986 android_display_height = height;
4987 android_display_bpp = depth;
4988 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004989
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004990#ifdef CONFIG_NAND_LIMITS
4991 /* Init nand stuff. */
4992 if (android_op_nand_limits) {
4993 parse_nand_limits(android_op_nand_limits);
4994 }
4995#endif // CONFIG_NAND_LIMITS
4996
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004997 /* Initialize AVD name from hardware configuration if needed */
4998 if (!android_op_avd_name) {
4999 if (android_hw->avd_name && *android_hw->avd_name) {
5000 android_op_avd_name = android_hw->avd_name;
5001 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
5002 }
5003 }
5004
David 'Digit' Turner40841b22011-03-01 14:04:00 +01005005 /* Initialize system partition image */
5006 {
5007 char tmp[PATH_MAX+32];
5008 const char* sysImage = android_hw->disk_systemPartition_path;
5009 const char* initImage = android_hw->disk_systemPartition_initPath;
5010 uint64_t sysBytes = android_hw->disk_systemPartition_size;
5011
5012 if (sysBytes == 0) {
5013 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
5014 }
5015
5016 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
5017
5018 if (sysImage && *sysImage) {
5019 if (filelock_create(sysImage) == NULL) {
5020 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
5021 /* If there is no file= parameters, nand_add_dev will create
5022 * a temporary file to back the partition image. */
5023 } else {
5024 pstrcat(tmp,sizeof(tmp),",file=");
5025 pstrcat(tmp,sizeof(tmp),sysImage);
5026 }
5027 }
5028 if (initImage && *initImage) {
5029 if (!path_exists(initImage)) {
5030 PANIC("Invalid initial system image path: %s", initImage);
5031 }
5032 pstrcat(tmp,sizeof(tmp),",initfile=");
5033 pstrcat(tmp,sizeof(tmp),initImage);
5034 } else {
5035 PANIC("Missing initial system image path!");
5036 }
5037 nand_add_dev(tmp);
5038 }
5039
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005040 /* Initialize data partition image */
5041 {
5042 char tmp[PATH_MAX+32];
5043 const char* dataImage = android_hw->disk_dataPartition_path;
5044 const char* initImage = android_hw->disk_dataPartition_initPath;
5045 uint64_t dataBytes = android_hw->disk_dataPartition_size;
5046
5047 if (dataBytes == 0) {
5048 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
5049 }
5050
5051 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
5052
5053 if (dataImage && *dataImage) {
5054 if (filelock_create(dataImage) == NULL) {
5055 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
5056 /* Note: if there is no file= parameters, nand_add_dev() will
5057 * create a temporary file to back the partition image. */
5058 } else {
5059 /* Create the file if needed */
5060 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005061 if (path_empty_file(dataImage) < 0) {
5062 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
5063 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005064 }
5065 pstrcat(tmp, sizeof(tmp), ",file=");
5066 pstrcat(tmp, sizeof(tmp), dataImage);
5067 }
5068 }
5069 if (initImage && *initImage) {
5070 pstrcat(tmp, sizeof(tmp), ",initfile=");
5071 pstrcat(tmp, sizeof(tmp), initImage);
5072 }
5073 nand_add_dev(tmp);
5074 }
5075
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005076 /* Init SD-Card stuff. For Android, it is always hda */
5077 /* If the -hda option was used, ignore the Android-provided one */
5078 if (hda_opts == NULL) {
5079 const char* sdPath = android_hw->hw_sdCard_path;
5080 if (sdPath && *sdPath) {
5081 if (!path_exists(sdPath)) {
5082 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
5083 } else if (filelock_create(sdPath) == NULL) {
5084 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
5085 } else {
5086 /* Successful locking */
5087 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005088 /* Set this property of any operation involving the SD Card
5089 * will be x100 slower, due to the corresponding file being
5090 * mounted as O_DIRECT. Note that this is only 'unsafe' in
5091 * the context of an emulator crash. The data is already
5092 * synced properly when the emulator exits (either normally or through ^C).
5093 */
5094 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005095 }
5096 }
5097 }
5098
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005099 if (hdb_opts == NULL) {
5100 const char* spath = android_hw->disk_snapStorage_path;
5101 if (spath && *spath) {
5102 if (!path_exists(spath)) {
5103 PANIC("Snapshot storage file does not exist: %s", spath);
5104 }
5105 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005106 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005107 }
5108 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005109 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02005110 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005111 }
5112 }
5113
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005114 /* Set the VM's max heap size, passed as a boot property */
5115 if (android_hw->vm_heapSize > 0) {
5116 char tmp[64];
5117 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
5118 boot_property_add("dalvik.vm.heapsize",tmp);
5119 }
5120
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005121 /* Initialize net speed and delays stuff. */
5122 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005123 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005124 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005125 }
5126
5127 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005128 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005129 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005130 }
5131
5132 if (android_op_netfast) {
5133 qemu_net_download_speed = 0;
5134 qemu_net_upload_speed = 0;
5135 qemu_net_min_latency = 0;
5136 qemu_net_max_latency = 0;
5137 }
5138
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005139 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005140 if (android_hw->hw_lcd_density) {
5141 long density = android_hw->hw_lcd_density;
5142 if (density <= 0) {
5143 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005144 }
5145 hwLcd_setBootProperty(density);
5146 }
5147
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07005148 /* Initialize TCP dump */
5149 if (android_op_tcpdump) {
5150 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
5151 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
5152 }
5153 }
5154
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005155 /* Initialize modem */
5156 if (android_op_radio) {
5157 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
5158 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005159 PANIC("unsupported character device specification: %s\n"
5160 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005161 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005162 }
5163 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
5164 } else if (android_hw->hw_gsmModem != 0 ) {
5165 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005166 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005167 }
5168 }
5169
5170 /* Initialize GPS */
5171 if (android_op_gps) {
5172 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
5173 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005174 PANIC("unsupported character device specification: %s\n"
5175 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005176 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005177 }
5178 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
5179 } else if (android_hw->hw_gps != 0) {
5180 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005181 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005182 }
5183 }
5184
5185 /* Initialize audio. */
5186 if (android_op_audio) {
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005187 char temp[128];
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005188 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005189 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005190 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005191 }
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005192 snprintf(temp, sizeof temp, "QEMU_AUDIO_DRV=%s", android_op_audio);
5193 putenv(temp);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005194 }
5195
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005196 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02005197 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005198
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005199 if (android_op_cpu_delay) {
5200 char* end;
5201 long delay = strtol(android_op_cpu_delay, &end, 0);
5202 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005203 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005204 }
5205 if (delay > 0)
5206 delay = (1000-delay);
5207
5208 qemu_cpu_delay = (int) delay;
5209 }
5210
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005211 if (android_op_dns_server) {
5212 char* x = strchr(android_op_dns_server, ',');
5213 dns_count = 0;
5214 if (x == NULL)
5215 {
5216 if ( add_dns_server( android_op_dns_server ) == 0 )
5217 dns_count = 1;
5218 }
5219 else
5220 {
5221 x = android_op_dns_server;
5222 while (*x) {
5223 char* y = strchr(x, ',');
5224
5225 if (y != NULL) {
5226 *y = 0;
5227 y++;
5228 } else {
5229 y = x + strlen(x);
5230 }
5231
5232 if (y > x && add_dns_server( x ) == 0) {
5233 dns_count += 1;
5234 }
5235 x = y;
5236 }
5237 }
5238 if (dns_count == 0)
5239 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5240 }
5241
5242 if (dns_count == 0)
5243 dns_count = slirp_get_system_dns_servers();
5244 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005245 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005246 }
5247
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005248#ifdef CONFIG_MEMCHECK
5249 if (android_op_memcheck) {
5250 memcheck_init(android_op_memcheck);
5251 }
5252#endif // CONFIG_MEMCHECK
5253
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005254 /* Initialize cache partition, if any */
5255 if (android_hw->disk_cachePartition != 0) {
5256 char tmp[PATH_MAX+32];
5257 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005258 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005259
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005260 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5261
5262 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5263 if (filelock_create(partPath) == NULL) {
5264 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5265 /* Note: if there is no file= parameters, nand_add_dev() will
5266 * create a temporary file to back the partition image. */
5267 } else {
5268 /* Create the file if needed */
5269 if (!path_exists(partPath)) {
5270 if (path_empty_file(partPath) < 0) {
5271 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5272 }
5273 }
5274 pstrcat(tmp, sizeof(tmp), ",file=");
5275 pstrcat(tmp, sizeof(tmp), partPath);
5276 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005277 }
5278 nand_add_dev(tmp);
5279 }
5280
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005281 /* We always force qemu=1 when running inside QEMU */
5282 stralloc_add_str(kernel_params, " qemu=1");
5283
5284 /* We always initialize the first serial port for the android-kmsg
5285 * character device (used to send kernel messages) */
5286 serial_hds_add_at(0, "android-kmsg");
5287 stralloc_add_str(kernel_params, " console=ttyS0");
5288
5289 /* We always initialize the second serial port for the android-qemud
5290 * character device as well */
5291 serial_hds_add_at(1, "android-qemud");
5292 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5293
David 'Digit' Turner36597752011-05-20 01:18:01 +02005294#if defined(CONFIG_KVM)
5295 if (kvm_allowed < 0) {
5296 kvm_allowed = kvm_check_allowed();
5297 }
5298#endif
5299
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005300#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5301 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005302 PANIC(
5303 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005304 }
5305#endif
5306
5307 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5308 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005309 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5310 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005311 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005312 }
5313
5314 if (display_type == DT_NOGRAPHIC) {
5315 if (serial_device_index == 0)
5316 serial_devices[0] = "stdio";
5317 if (parallel_device_index == 0)
5318 parallel_devices[0] = "null";
5319 if (strncmp(monitor_device, "vc", 2) == 0)
5320 monitor_device = "stdio";
5321 }
5322
5323#ifndef _WIN32
5324 if (daemonize) {
5325 pid_t pid;
5326
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005327 if (pipe(fds) == -1) {
5328 PANIC("Unable to aquire pidfile");
5329 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005330
5331 pid = fork();
5332 if (pid > 0) {
5333 uint8_t status;
5334 ssize_t len;
5335
5336 close(fds[1]);
5337
5338 again:
5339 len = read(fds[0], &status, 1);
5340 if (len == -1 && (errno == EINTR))
5341 goto again;
5342
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005343 if (len != 1) {
5344 PANIC("Error when aquiring pidfile");
5345 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005346 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005347 PANIC("Could not acquire pidfile");
5348 } else {
5349 QEMU_EXIT(0);
5350 }
5351 } else if (pid < 0) {
5352 PANIC("Unable to daemonize");
5353 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005354
5355 setsid();
5356
5357 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005358 if (pid > 0) {
5359 QEMU_EXIT(0);
5360 } else if (pid < 0) {
5361 PANIC("Could not acquire pid file");
5362 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005363
5364 umask(027);
5365
5366 signal(SIGTSTP, SIG_IGN);
5367 signal(SIGTTOU, SIG_IGN);
5368 signal(SIGTTIN, SIG_IGN);
5369 }
5370
5371 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5372 if (daemonize) {
5373 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005374 int ret;
5375 do {
5376 ret = write(fds[1], &status, 1);
5377 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005378 PANIC("Could not acquire pid file");
5379 } else {
5380 PANIC("Could not acquire pid file");
5381 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005382 }
5383#endif
5384
5385#ifdef CONFIG_KQEMU
5386 if (smp_cpus > 1)
5387 kqemu_allowed = 0;
5388#endif
5389 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005390 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005391 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005392
5393 if (kernel_filename == NULL) {
5394 kernel_filename = android_hw->kernel_path;
5395 }
5396 if (initrd_filename == NULL) {
5397 initrd_filename = android_hw->disk_ramdisk_path;
5398 }
5399
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005400 linux_boot = (kernel_filename != NULL);
5401 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5402
5403 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005404 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005405 }
5406
5407 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005408 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005409 }
5410
5411 /* boot to floppy or the default cd if no hard disk defined yet */
5412 if (!boot_devices[0]) {
5413 boot_devices = "cad";
5414 }
5415 setvbuf(stdout, NULL, _IOLBF, 0);
5416
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005417 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005418 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005419 }
David Turner6a9ef172010-09-09 22:54:36 +02005420 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005421
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005422 /* init network clients */
5423 if (nb_net_clients == 0) {
5424 /* if no clients, we use a default config */
5425 net_clients[nb_net_clients++] = "nic";
5426#ifdef CONFIG_SLIRP
5427 net_clients[nb_net_clients++] = "user";
5428#endif
5429 }
5430
5431 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005432 if (net_client_parse(net_clients[i]) < 0) {
5433 PANIC("Unable to parse net clients");
5434 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005435 }
5436 net_client_check();
5437
5438#ifdef TARGET_I386
5439 /* XXX: this should be moved in the PC machine instantiation code */
5440 if (net_boot != 0) {
5441 int netroms = 0;
5442 for (i = 0; i < nb_nics && i < 4; i++) {
5443 const char *model = nd_table[i].model;
5444 char buf[1024];
5445 char *filename;
5446 if (net_boot & (1 << i)) {
5447 if (model == NULL)
5448 model = "ne2k_pci";
5449 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5450 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5451 if (filename && get_image_size(filename) > 0) {
5452 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005453 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005454 }
5455 option_rom[nb_option_roms] = qemu_strdup(buf);
5456 nb_option_roms++;
5457 netroms++;
5458 }
5459 if (filename) {
5460 qemu_free(filename);
5461 }
5462 }
5463 }
5464 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005465 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005466 }
5467 }
5468#endif
5469
5470 /* init the bluetooth world */
5471 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005472 if (bt_parse(bt_opts[i])) {
5473 PANIC("Unable to parse bluetooth options");
5474 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005475
5476 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005477 if (ram_size == 0) {
5478 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5479 if (ram_size == 0) {
5480 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5481 }
5482 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005483
5484#ifdef CONFIG_KQEMU
5485 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5486 guest ram allocation. It needs to go away. */
5487 if (kqemu_allowed) {
5488 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5489 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5490 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005491 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005492 }
5493 }
5494#endif
5495
rich canningsd952f282011-03-01 15:40:09 -08005496#ifndef _WIN32
5497 init_qemu_clear_logs_sig();
5498#endif
5499
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005500 /* init the dynamic translator */
5501 cpu_exec_init_all(tb_size * 1024 * 1024);
5502
5503 bdrv_init();
5504
5505 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005506#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005507 if (nb_drives_opt < MAX_DRIVES)
5508 drive_add(NULL, CDROM_ALIAS);
5509
5510 /* we always create at least one floppy */
5511
5512 if (nb_drives_opt < MAX_DRIVES)
5513 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005514 /* we always create one sd slot, even if no card is in it */
5515
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005516 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005517 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005518 }
5519#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005520
5521 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005522 if (snapshot)
5523 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5524 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5525 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005526
David Turner6a9ef172010-09-09 22:54:36 +02005527 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005528 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5529
5530#ifndef _WIN32
5531 /* must be after terminal init, SDL library changes signal handlers */
5532 sighandler_setup();
5533#endif
5534
5535 /* Maintain compatibility with multiple stdio monitors */
5536 if (!strcmp(monitor_device,"stdio")) {
5537 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5538 const char *devname = serial_devices[i];
5539 if (devname && !strcmp(devname,"mon:stdio")) {
5540 monitor_device = NULL;
5541 break;
5542 } else if (devname && !strcmp(devname,"stdio")) {
5543 monitor_device = NULL;
5544 serial_devices[i] = "mon:stdio";
5545 break;
5546 }
5547 }
5548 }
5549
5550 if (nb_numa_nodes > 0) {
5551 int i;
5552
5553 if (nb_numa_nodes > smp_cpus) {
5554 nb_numa_nodes = smp_cpus;
5555 }
5556
5557 /* If no memory size if given for any node, assume the default case
5558 * and distribute the available memory equally across all nodes
5559 */
5560 for (i = 0; i < nb_numa_nodes; i++) {
5561 if (node_mem[i] != 0)
5562 break;
5563 }
5564 if (i == nb_numa_nodes) {
5565 uint64_t usedmem = 0;
5566
5567 /* On Linux, the each node's border has to be 8MB aligned,
5568 * the final node gets the rest.
5569 */
5570 for (i = 0; i < nb_numa_nodes - 1; i++) {
5571 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5572 usedmem += node_mem[i];
5573 }
5574 node_mem[i] = ram_size - usedmem;
5575 }
5576
5577 for (i = 0; i < nb_numa_nodes; i++) {
5578 if (node_cpumask[i] != 0)
5579 break;
5580 }
5581 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5582 * must cope with this anyway, because there are BIOSes out there in
5583 * real machines which also use this scheme.
5584 */
5585 if (i == nb_numa_nodes) {
5586 for (i = 0; i < smp_cpus; i++) {
5587 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5588 }
5589 }
5590 }
5591
5592 if (kvm_enabled()) {
5593 int ret;
5594
5595 ret = kvm_init(smp_cpus);
5596 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005597 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005598 }
5599 }
5600
5601 if (monitor_device) {
5602 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5603 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005604 PANIC("qemu: could not open monitor device '%s'",
5605 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005606 }
5607 }
5608
5609 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005610 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005611 }
5612
5613 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5614 const char *devname = parallel_devices[i];
5615 if (devname && strcmp(devname, "none")) {
5616 char label[32];
5617 snprintf(label, sizeof(label), "parallel%d", i);
5618 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5619 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005620 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005621 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005622 }
5623 }
5624 }
5625
5626 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5627 const char *devname = virtio_consoles[i];
5628 if (devname && strcmp(devname, "none")) {
5629 char label[32];
5630 snprintf(label, sizeof(label), "virtcon%d", i);
5631 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5632 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005633 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005634 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005635 }
5636 }
5637 }
5638
5639 module_call_init(MODULE_INIT_DEVICE);
5640
5641
5642#ifdef CONFIG_TRACE
5643 if (trace_filename) {
5644 trace_init(trace_filename);
5645#if 0
5646 // We don't need the dcache code until we can get load and store tracing
5647 // working again.
5648 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5649 dcache_replace_policy, dcache_load_miss_penalty,
5650 dcache_store_miss_penalty);
5651#endif
5652 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5653 }
5654#endif
5655
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005656 /* Check the CPU Architecture value */
5657#if defined(TARGET_ARM)
5658 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5659 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5660 android_hw->hw_cpu_arch);
5661 exit(1);
5662 }
5663#elif defined(TARGET_X86)
5664 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5665 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5666 android_hw->hw_cpu_arch);
5667 exit(1);
5668 }
5669#endif
5670
5671 /* Grab CPU model if provided in hardware.ini */
5672 if ( !cpu_model
5673 && android_hw->hw_cpu_model
5674 && android_hw->hw_cpu_model[0] != '\0')
5675 {
5676 cpu_model = android_hw->hw_cpu_model;
5677 }
5678
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005679 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005680 * collected during initialization.
5681 *
5682 * The order is the following:
5683 * - parameters from the hw configuration (kernel.parameters)
5684 * - additionnal parameters from options (e.g. -memcheck)
5685 * - the -append parameters.
5686 */
5687 {
5688 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005689
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005690 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005691 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005692 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5693 }
5694
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005695 /* If not empty, kernel_config always contains a leading space */
5696 stralloc_append(kernel_params, kernel_config);
5697
5698 if (*kernel_cmdline) {
5699 stralloc_add_c(kernel_params, ' ');
5700 stralloc_add_str(kernel_params, kernel_cmdline);
5701 }
5702
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005703 /* Remove any leading/trailing spaces */
5704 stralloc_strip(kernel_params);
5705
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005706 kernel_parameters = stralloc_cstr(kernel_params);
5707 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5708
5709 machine->init(ram_size,
5710 boot_devices,
5711 kernel_filename,
5712 kernel_parameters,
5713 initrd_filename,
5714 cpu_model);
5715
5716 stralloc_reset(kernel_params);
5717 stralloc_reset(kernel_config);
5718 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005719
5720
5721 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5722 for (i = 0; i < nb_numa_nodes; i++) {
5723 if (node_cpumask[i] & (1 << env->cpu_index)) {
5724 env->numa_node = i;
5725 }
5726 }
5727 }
5728
5729 current_machine = machine;
5730
5731 /* Set KVM's vcpu state to qemu's initial CPUState. */
5732 if (kvm_enabled()) {
5733 int ret;
5734
5735 ret = kvm_sync_vcpus();
5736 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005737 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005738 }
5739 }
5740
5741 /* init USB devices */
5742 if (usb_enabled) {
5743 for(i = 0; i < usb_devices_index; i++) {
5744 if (usb_device_add(usb_devices[i], 0) < 0) {
5745 fprintf(stderr, "Warning: could not add USB device %s\n",
5746 usb_devices[i]);
5747 }
5748 }
5749 }
5750
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005751 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005752 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005753
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005754 /* Initialize display from the command line parameters. */
5755 android_display_reset(ds,
5756 android_display_width,
5757 android_display_height,
5758 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005759
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005760 if (display_type == DT_DEFAULT) {
5761#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5762 display_type = DT_SDL;
5763#else
5764 display_type = DT_VNC;
5765 vnc_display = "localhost:0,to=99";
5766 show_vnc_port = 1;
5767#endif
5768 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005769
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005770
5771 switch (display_type) {
5772 case DT_NOGRAPHIC:
5773 break;
5774#if defined(CONFIG_CURSES)
5775 case DT_CURSES:
5776 curses_display_init(ds, full_screen);
5777 break;
5778#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005779#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005780 case DT_SDL:
5781 sdl_display_init(ds, full_screen, no_frame);
5782 break;
5783#elif defined(CONFIG_COCOA)
5784 case DT_SDL:
5785 cocoa_display_init(ds, full_screen);
5786 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005787#elif defined(CONFIG_STANDALONE_CORE)
5788 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005789 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005790 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005791#endif
5792 case DT_VNC:
5793 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005794 if (vnc_display_open(ds, vnc_display) < 0) {
5795 PANIC("Unable to initialize VNC display");
5796 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005797
5798 if (show_vnc_port) {
5799 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5800 }
5801 break;
5802 default:
5803 break;
5804 }
5805 dpy_resize(ds);
5806
5807 dcl = ds->listeners;
5808 while (dcl != NULL) {
5809 if (dcl->dpy_refresh != NULL) {
5810 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5811 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5812 }
5813 dcl = dcl->next;
5814 }
5815
5816 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5817 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5818 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5819 }
5820
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005821 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005822 qemu_chr_initial_reset();
5823
5824 if (monitor_device && monitor_hd)
5825 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5826
5827 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5828 const char *devname = serial_devices[i];
5829 if (devname && strcmp(devname, "none")) {
5830 if (strstart(devname, "vc", 0))
5831 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5832 }
5833 }
5834
5835 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5836 const char *devname = parallel_devices[i];
5837 if (devname && strcmp(devname, "none")) {
5838 if (strstart(devname, "vc", 0))
5839 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5840 }
5841 }
5842
5843 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5844 const char *devname = virtio_consoles[i];
5845 if (virtcon_hds[i] && devname) {
5846 if (strstart(devname, "vc", 0))
5847 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5848 }
5849 }
5850
5851 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005852 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005853 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005854 }
5855
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005856 /* call android-specific setup function */
5857 android_emulation_setup();
5858
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005859#if !defined(CONFIG_STANDALONE_CORE)
5860 // For the standalone emulator (UI+core in one executable) we need to
5861 // set the window title here.
5862 android_emulator_set_base_port(android_base_port);
5863#endif
5864
Ot ten Thije871da2a2010-09-20 10:29:22 +01005865 if (loadvm)
5866 do_loadvm(cur_mon, loadvm);
5867
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005868 if (incoming) {
5869 autostart = 0; /* fixme how to deal with -daemonize */
5870 qemu_start_incoming_migration(incoming);
5871 }
5872
5873 if (autostart)
5874 vm_start();
5875
5876#ifndef _WIN32
5877 if (daemonize) {
5878 uint8_t status = 0;
5879 ssize_t len;
5880
5881 again1:
5882 len = write(fds[1], &status, 1);
5883 if (len == -1 && (errno == EINTR))
5884 goto again1;
5885
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005886 if (len != 1) {
5887 PANIC("Unable to daemonize");
5888 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005889
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005890 if (chdir("/")) {
5891 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005892 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005893 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005894 TFR(fd = open("/dev/null", O_RDWR));
5895 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005896 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005897 }
5898
5899 if (run_as) {
5900 pwd = getpwnam(run_as);
5901 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005902 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005903 }
5904 }
5905
5906 if (chroot_dir) {
5907 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005908 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005909 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005910 if (chdir("/")) {
5911 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005912 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005913 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005914 }
5915
5916 if (run_as) {
5917 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005918 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005919 }
5920 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005921 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005922 }
5923 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005924 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005925 }
5926 }
5927
5928 if (daemonize) {
5929 dup2(fd, 0);
5930 dup2(fd, 1);
5931 dup2(fd, 2);
5932
5933 close(fd);
5934 }
5935#endif
5936
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005937#ifdef CONFIG_ANDROID
5938 // This will notify the UI that the core is successfuly initialized
5939 android_core_init_completed();
5940#endif // CONFIG_ANDROID
5941
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005942 main_loop();
5943 quit_timers();
5944 net_cleanup();
5945 android_emulation_teardown();
5946 return 0;
5947}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005948
5949void
5950android_emulation_teardown(void)
5951{
5952 android_charmap_done();
5953}