blob: c240a44189f112b9cbbd28542bbce8774a8b2c6c [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
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003553/* Similarly, return the location of the executable */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003554static char *find_datadir(const char *argv0)
3555{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003556 char *p = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003557 char buf[PATH_MAX];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003558
3559#if defined(__linux__)
3560 {
3561 int len;
3562 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3563 if (len > 0) {
3564 buf[len] = 0;
3565 p = buf;
3566 }
3567 }
3568#elif defined(__FreeBSD__)
3569 {
3570 int len;
3571 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3572 if (len > 0) {
3573 buf[len] = 0;
3574 p = buf;
3575 }
3576 }
3577#endif
3578 /* If we don't have any way of figuring out the actual executable
3579 location then try argv[0]. */
3580 if (!p) {
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003581 p = realpath(argv0, buf);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003582 if (!p) {
3583 return NULL;
3584 }
3585 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003586
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003587 return qemu_strdup(dirname(buf));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003588}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003589#endif
3590
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003591static char*
3592qemu_find_file_with_subdir(const char* data_dir, const char* subdir, const char* name)
3593{
3594 int len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3595 char* buf = qemu_mallocz(len);
3596
3597 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3598 VERBOSE_PRINT(init," trying to find: %s\n", buf);
3599 if (access(buf, R_OK)) {
3600 qemu_free(buf);
3601 return NULL;
3602 }
3603 return buf;
3604}
3605
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003606char *qemu_find_file(int type, const char *name)
3607{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003608 const char *subdir;
3609 char *buf;
3610
3611 /* If name contains path separators then try it as a straight path. */
3612 if ((strchr(name, '/') || strchr(name, '\\'))
3613 && access(name, R_OK) == 0) {
3614 return strdup(name);
3615 }
3616 switch (type) {
3617 case QEMU_FILE_TYPE_BIOS:
3618 subdir = "";
3619 break;
3620 case QEMU_FILE_TYPE_KEYMAP:
3621 subdir = "keymaps/";
3622 break;
3623 default:
3624 abort();
3625 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003626 buf = qemu_find_file_with_subdir(data_dir, subdir, name);
3627#ifdef CONFIG_ANDROID
3628 if (type == QEMU_FILE_TYPE_BIOS) {
3629 /* This case corresponds to the emulator being used as part of an
3630 * SDK installation. NOTE: data_dir is really $bindir. */
3631 if (buf == NULL)
3632 buf = qemu_find_file_with_subdir(data_dir, "lib/pc-bios/", name);
3633 /* This case corresponds to platform builds. */
3634 if (buf == NULL)
3635 buf = qemu_find_file_with_subdir(data_dir, "../usr/share/pc-bios/", name);
3636 /* Finally, try this for standalone builds under external/qemu */
3637 if (buf == NULL)
3638 buf = qemu_find_file_with_subdir(data_dir, "../../../prebuilt/common/pc-bios/", name);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003639 }
David 'Digit' Turner24d27522011-06-01 16:50:56 +02003640#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003641 return buf;
3642}
3643
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003644static int
3645add_dns_server( const char* server_name )
3646{
3647 SockAddress addr;
3648
3649 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3650 fprintf(stdout,
3651 "### WARNING: can't resolve DNS server name '%s'\n",
3652 server_name );
3653 return -1;
3654 }
3655
3656 fprintf(stderr,
3657 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3658
3659 if ( slirp_add_dns_server( &addr ) < 0 ) {
3660 fprintf(stderr,
3661 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3662 return -1;
3663 }
3664 return 0;
3665}
3666
rich cannings7339b552011-02-16 13:43:44 -08003667/* Parses an integer
3668 * Pararm:
3669 * str String containing a number to be parsed.
3670 * result Passes the parsed integer in this argument
3671 * returns 0 if ok, -1 if failed
3672 */
3673int
3674parse_int(const char *str, int *result)
3675{
3676 char* r;
3677 *result = strtol(str, &r, 0);
3678 if (r == NULL || *r != '\0')
3679 return -1;
3680
3681 return 0;
3682}
3683
rich canningsd952f282011-03-01 15:40:09 -08003684#ifndef _WIN32
3685/*
3686 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3687 */
3688void init_qemu_clear_logs_sig() {
3689 struct sigaction act;
3690 sigfillset(&act.sa_mask);
3691 act.sa_flags = 0;
3692 act.sa_handler = rotate_qemu_logs_handler;
3693 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3694 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3695 exit(-1);
3696 }
3697}
3698#endif
3699
3700
rich cannings7339b552011-02-16 13:43:44 -08003701
3702/* parses a null-terminated string specifying a network port (e.g., "80") or
3703 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3704 * are the same. Returns 0 on success, -1 on error. */
3705
3706int parse_port_range(const char *str, unsigned short *lport,
3707 unsigned short *hport) {
3708
3709 unsigned int low = 0, high = 0;
3710 char *p, *arg = strdup(str);
3711
3712 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3713 p = arg + 1; /* skip '[' */
3714 low = atoi(strtok(p, "-"));
3715 high = atoi(strtok(NULL, "-"));
3716 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3717 *lport = low;
3718 *hport = high;
3719 }
3720 }
3721 else {
3722 low = atoi(arg);
3723 if ((0 < low) && (low < 65535)) {
3724 *lport = low;
3725 *hport = low;
3726 }
3727 }
3728 free(arg);
3729 if (low != 0)
3730 return 0;
3731 return -1;
3732}
3733
3734/*
3735 * Implements the generic port forwarding option
3736 */
3737void
3738net_slirp_forward(const char *optarg)
3739{
3740 /*
3741 * we expect the following format:
3742 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3743 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3744 */
3745 char *argument = strdup(optarg), *p = argument;
3746 char *dst_net, *dst_mask, *dst_port;
3747 char *redirect_ip, *redirect_port;
3748 uint32_t dnet, dmask, rip;
3749 unsigned short dlport, dhport, rport;
3750
3751
3752 dst_net = strtok(p, ":");
3753 dst_mask = strtok(NULL, ":");
3754 dst_port = strtok(NULL, ":");
3755 redirect_ip = strtok(NULL, ":");
3756 redirect_port = strtok(NULL, ":");
3757
3758 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3759 redirect_ip == NULL || redirect_port == NULL) {
3760 fprintf(stderr,
3761 "Invalid argument for -net-forward, we expect "
3762 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3763 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3764 ":redirect_ip:redirect_port: %s\n",
3765 optarg);
3766 exit(1);
3767 }
3768
3769 /* inet_strtoip converts dotted address to host byte order */
3770 if (inet_strtoip(dst_net, &dnet) == -1) {
3771 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3772 exit(1);
3773 }
3774 if (inet_strtoip(dst_mask, &dmask) == -1) {
3775 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3776 exit(1);
3777 }
3778 if (inet_strtoip(redirect_ip, &rip) == -1) {
3779 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3780 exit(1);
3781 }
3782
3783 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3784 fprintf(stderr, "Invalid destination port or port range\n");
3785 exit(1);
3786 }
3787
3788 rport = atoi(redirect_port);
3789 if (!rport) {
3790 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3791 exit(1);
3792 }
3793
3794 dnet &= dmask;
3795
3796 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3797 rip, rport);
3798
3799 free(argument);
3800}
3801
3802
3803/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3804 * entry in the allows list */
3805void
3806slirp_allow(const char *optarg, u_int8_t proto)
3807{
3808 /*
3809 * we expect the following format:
3810 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3811 */
3812 char *argument = strdup(optarg), *p = argument;
3813 char *dst_ip_str, *dst_port_str;
3814 uint32_t dst_ip;
3815 unsigned short dst_lport, dst_hport;
3816
3817 dst_ip_str = strtok(p, ":");
3818 dst_port_str = strtok(NULL, ":");
3819
3820 if (dst_ip_str == NULL || dst_port_str == NULL) {
3821 fprintf(stderr,
3822 "Invalid argument %s for -allow. We expect "
3823 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3824 optarg);
3825 exit(1);
3826 }
3827
3828 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3829 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3830 exit(1);
3831 }
3832 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3833 fprintf(stderr, "Invalid destination port or port range\n");
3834 exit(1);
3835 }
3836
3837 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3838
3839 free(argument);
3840}
3841
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003842/* Add a serial device at a given location in the emulated hardware table.
3843 * On failure, this function aborts the program with an error message.
3844 */
3845static void
3846serial_hds_add_at(int index, const char* devname)
3847{
3848 char label[32];
3849
3850 if (!devname || !strcmp(devname,"none"))
3851 return;
3852
3853 if (index >= MAX_SERIAL_PORTS) {
3854 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3855 devname, index, MAX_SERIAL_PORTS);
3856 }
3857 if (serial_hds[index] != NULL) {
3858 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3859 devname, index);
3860 }
3861 snprintf(label, sizeof(label), "serial%d", index);
3862 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3863 if (!serial_hds[index]) {
3864 PANIC("qemu: could not open serial device '%s'", devname);
3865 }
3866}
3867
3868
3869/* Find a free slot in the emulated serial device table, and register
3870 * it. Return the allocated table index.
3871 */
3872static int
3873serial_hds_add(const char* devname)
3874{
3875 int index;
3876
3877 /* Find first free slot */
3878 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3879 if (serial_hds[index] == NULL) {
3880 serial_hds_add_at(index, devname);
3881 return index;
3882 }
3883 }
3884
3885 PANIC("qemu: too many serial devices registered (%d)", index);
3886 return -1; /* shouldn't happen */
3887}
3888
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003889int main(int argc, char **argv, char **envp)
3890{
3891 const char *gdbstub_dev = NULL;
3892 uint32_t boot_devices_bitmap = 0;
3893 int i;
3894 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003895 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003896 const char *initrd_filename;
3897 const char *kernel_filename, *kernel_cmdline;
3898 const char *boot_devices = "";
3899 DisplayState *ds;
3900 DisplayChangeListener *dcl;
3901 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003902 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003903 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003904 const char *net_clients[MAX_NET_CLIENTS];
3905 int nb_net_clients;
3906 const char *bt_opts[MAX_BT_CMDLINE];
3907 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003908 int optind;
3909 const char *r, *optarg;
3910 CharDriverState *monitor_hd = NULL;
3911 const char *monitor_device;
3912 const char *serial_devices[MAX_SERIAL_PORTS];
3913 int serial_device_index;
3914 const char *parallel_devices[MAX_PARALLEL_PORTS];
3915 int parallel_device_index;
3916 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3917 int virtio_console_index;
3918 const char *loadvm = NULL;
3919 QEMUMachine *machine;
3920 const char *cpu_model;
3921 const char *usb_devices[MAX_USB_CMDLINE];
3922 int usb_devices_index;
3923#ifndef _WIN32
3924 int fds[2];
3925#endif
3926 int tb_size;
3927 const char *pid_file = NULL;
3928 const char *incoming = NULL;
3929#ifndef _WIN32
3930 int fd = 0;
3931 struct passwd *pwd = NULL;
3932 const char *chroot_dir = NULL;
3933 const char *run_as = NULL;
3934#endif
3935 CPUState *env;
3936 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003937 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003938 STRALLOC_DEFINE(kernel_params);
3939 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003940 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003941
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003942 /* Initialize sockets before anything else, so we can properly report
3943 * initialization failures back to the UI. */
3944#ifdef _WIN32
3945 socket_init();
3946#endif
3947
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003948 init_clocks();
3949
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003950 qemu_cache_utils_init(envp);
3951
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003952 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003953#ifndef _WIN32
3954 {
3955 struct sigaction act;
3956 sigfillset(&act.sa_mask);
3957 act.sa_flags = 0;
3958 act.sa_handler = SIG_IGN;
3959 sigaction(SIGPIPE, &act, NULL);
3960 }
3961#else
3962 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3963 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3964 QEMU to run on a single CPU */
3965 {
3966 HANDLE h;
3967 DWORD mask, smask;
3968 int i;
3969 h = GetCurrentProcess();
3970 if (GetProcessAffinityMask(h, &mask, &smask)) {
3971 for(i = 0; i < 32; i++) {
3972 if (mask & (1 << i))
3973 break;
3974 }
3975 if (i != 32) {
3976 mask = 1 << i;
3977 SetProcessAffinityMask(h, mask);
3978 }
3979 }
3980 }
3981#endif
3982
3983 module_call_init(MODULE_INIT_MACHINE);
3984 machine = find_default_machine();
3985 cpu_model = NULL;
3986 initrd_filename = NULL;
3987 ram_size = 0;
3988 snapshot = 0;
3989 kernel_filename = NULL;
3990 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003991
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003992 cyls = heads = secs = 0;
3993 translation = BIOS_ATA_TRANSLATION_AUTO;
3994 monitor_device = "vc:80Cx24C";
3995
3996 serial_devices[0] = "vc:80Cx24C";
3997 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3998 serial_devices[i] = NULL;
3999 serial_device_index = 0;
4000
4001 parallel_devices[0] = "vc:80Cx24C";
4002 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
4003 parallel_devices[i] = NULL;
4004 parallel_device_index = 0;
4005
4006 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
4007 virtio_consoles[i] = NULL;
4008 virtio_console_index = 0;
4009
4010 for (i = 0; i < MAX_NODES; i++) {
4011 node_mem[i] = 0;
4012 node_cpumask[i] = 0;
4013 }
4014
4015 usb_devices_index = 0;
4016
4017 nb_net_clients = 0;
4018 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004019#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004020 nb_drives = 0;
4021 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004022#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004023 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004024
4025 nb_nics = 0;
4026
4027 tb_size = 0;
4028 autostart= 1;
4029
4030 register_watchdogs();
4031
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004032 /* Initialize boot properties. */
4033 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02004034 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02004035 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004036
David 'Digit' Turner36597752011-05-20 01:18:01 +02004037#ifdef CONFIG_KVM
4038 /* By default, force auto-detection for kvm */
4039 kvm_allowed = -1;
4040#endif
4041
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004042 optind = 1;
4043 for(;;) {
4044 if (optind >= argc)
4045 break;
4046 r = argv[optind];
4047 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004048 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004049 } else {
4050 const QEMUOption *popt;
4051
4052 optind++;
4053 /* Treat --foo the same as -foo. */
4054 if (r[1] == '-')
4055 r++;
4056 popt = qemu_options;
4057 for(;;) {
4058 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004059 PANIC("%s: invalid option -- '%s'",
4060 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004061 }
4062 if (!strcmp(popt->name, r + 1))
4063 break;
4064 popt++;
4065 }
4066 if (popt->flags & HAS_ARG) {
4067 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004068 PANIC("%s: option '%s' requires an argument",
4069 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004070 }
4071 optarg = argv[optind++];
4072 } else {
4073 optarg = NULL;
4074 }
4075
4076 switch(popt->index) {
4077 case QEMU_OPTION_M:
4078 machine = find_machine(optarg);
4079 if (!machine) {
4080 QEMUMachine *m;
4081 printf("Supported machines are:\n");
4082 for(m = first_machine; m != NULL; m = m->next) {
4083 printf("%-10s %s%s\n",
4084 m->name, m->desc,
4085 m->is_default ? " (default)" : "");
4086 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004087 if (*optarg != '?') {
4088 PANIC("Invalid machine parameter: %s",
4089 optarg);
4090 } else {
4091 QEMU_EXIT(0);
4092 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004093 }
4094 break;
4095 case QEMU_OPTION_cpu:
4096 /* hw initialization will check this */
4097 if (*optarg == '?') {
4098/* XXX: implement xxx_cpu_list for targets that still miss it */
4099#if defined(cpu_list)
4100 cpu_list(stdout, &fprintf);
4101#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004102 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004103 } else {
4104 cpu_model = optarg;
4105 }
4106 break;
4107 case QEMU_OPTION_initrd:
4108 initrd_filename = optarg;
4109 break;
4110 case QEMU_OPTION_hda:
4111 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004112 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004113 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004114 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004115 ",cyls=%d,heads=%d,secs=%d%s",
4116 0, cyls, heads, secs,
4117 translation == BIOS_ATA_TRANSLATION_LBA ?
4118 ",trans=lba" :
4119 translation == BIOS_ATA_TRANSLATION_NONE ?
4120 ",trans=none" : "");
4121 break;
4122 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004123 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
4124 break;
4125
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004126 case QEMU_OPTION_hdc:
4127 case QEMU_OPTION_hdd:
4128 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4129 break;
4130 case QEMU_OPTION_drive:
4131 drive_add(NULL, "%s", optarg);
4132 break;
4133 case QEMU_OPTION_mtdblock:
4134 drive_add(optarg, MTD_ALIAS);
4135 break;
4136 case QEMU_OPTION_sd:
4137 drive_add(optarg, SD_ALIAS);
4138 break;
4139 case QEMU_OPTION_pflash:
4140 drive_add(optarg, PFLASH_ALIAS);
4141 break;
4142 case QEMU_OPTION_snapshot:
4143 snapshot = 1;
4144 break;
4145 case QEMU_OPTION_hdachs:
4146 {
4147 const char *p;
4148 p = optarg;
4149 cyls = strtol(p, (char **)&p, 0);
4150 if (cyls < 1 || cyls > 16383)
4151 goto chs_fail;
4152 if (*p != ',')
4153 goto chs_fail;
4154 p++;
4155 heads = strtol(p, (char **)&p, 0);
4156 if (heads < 1 || heads > 16)
4157 goto chs_fail;
4158 if (*p != ',')
4159 goto chs_fail;
4160 p++;
4161 secs = strtol(p, (char **)&p, 0);
4162 if (secs < 1 || secs > 63)
4163 goto chs_fail;
4164 if (*p == ',') {
4165 p++;
4166 if (!strcmp(p, "none"))
4167 translation = BIOS_ATA_TRANSLATION_NONE;
4168 else if (!strcmp(p, "lba"))
4169 translation = BIOS_ATA_TRANSLATION_LBA;
4170 else if (!strcmp(p, "auto"))
4171 translation = BIOS_ATA_TRANSLATION_AUTO;
4172 else
4173 goto chs_fail;
4174 } else if (*p != '\0') {
4175 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004176 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004177 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004178 if (hda_opts != NULL) {
4179 char num[16];
4180 snprintf(num, sizeof(num), "%d", cyls);
4181 qemu_opt_set(hda_opts, "cyls", num);
4182 snprintf(num, sizeof(num), "%d", heads);
4183 qemu_opt_set(hda_opts, "heads", num);
4184 snprintf(num, sizeof(num), "%d", secs);
4185 qemu_opt_set(hda_opts, "secs", num);
4186 if (translation == BIOS_ATA_TRANSLATION_LBA)
4187 qemu_opt_set(hda_opts, "trans", "lba");
4188 if (translation == BIOS_ATA_TRANSLATION_NONE)
4189 qemu_opt_set(hda_opts, "trans", "none");
4190 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004191 }
4192 break;
4193 case QEMU_OPTION_numa:
4194 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004195 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004196 }
4197 numa_add(optarg);
4198 break;
4199 case QEMU_OPTION_nographic:
4200 display_type = DT_NOGRAPHIC;
4201 break;
4202#ifdef CONFIG_CURSES
4203 case QEMU_OPTION_curses:
4204 display_type = DT_CURSES;
4205 break;
4206#endif
4207 case QEMU_OPTION_portrait:
4208 graphic_rotate = 1;
4209 break;
4210 case QEMU_OPTION_kernel:
4211 kernel_filename = optarg;
4212 break;
4213 case QEMU_OPTION_append:
4214 kernel_cmdline = optarg;
4215 break;
4216 case QEMU_OPTION_cdrom:
4217 drive_add(optarg, CDROM_ALIAS);
4218 break;
4219 case QEMU_OPTION_boot:
4220 boot_devices = optarg;
4221 /* We just do some generic consistency checks */
4222 {
4223 /* Could easily be extended to 64 devices if needed */
4224 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004225
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004226 boot_devices_bitmap = 0;
4227 for (p = boot_devices; *p != '\0'; p++) {
4228 /* Allowed boot devices are:
4229 * a b : floppy disk drives
4230 * c ... f : IDE disk drives
4231 * g ... m : machine implementation dependant drives
4232 * n ... p : network devices
4233 * It's up to each machine implementation to check
4234 * if the given boot devices match the actual hardware
4235 * implementation and firmware features.
4236 */
4237 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004238 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004239 }
4240 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004241 PANIC(
4242 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004243 }
4244 boot_devices_bitmap |= 1 << (*p - 'a');
4245 }
4246 }
4247 break;
4248 case QEMU_OPTION_fda:
4249 case QEMU_OPTION_fdb:
4250 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4251 break;
4252#ifdef TARGET_I386
4253 case QEMU_OPTION_no_fd_bootchk:
4254 fd_bootchk = 0;
4255 break;
4256#endif
4257 case QEMU_OPTION_net:
4258 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004259 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004260 }
4261 net_clients[nb_net_clients] = optarg;
4262 nb_net_clients++;
4263 break;
4264#ifdef CONFIG_SLIRP
4265 case QEMU_OPTION_tftp:
4266 tftp_prefix = optarg;
4267 break;
4268 case QEMU_OPTION_bootp:
4269 bootp_filename = optarg;
4270 break;
4271#if 0 /* ANDROID disabled */
4272#ifndef _WIN32
4273 case QEMU_OPTION_smb:
4274 net_slirp_smb(optarg);
4275 break;
4276#endif
4277#endif /* ANDROID */
4278 case QEMU_OPTION_redir:
4279 net_slirp_redir(NULL, optarg, NULL);
4280 break;
4281#endif
4282 case QEMU_OPTION_bt:
4283 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004284 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004285 }
4286 bt_opts[nb_bt_opts++] = optarg;
4287 break;
4288#ifdef HAS_AUDIO
4289 case QEMU_OPTION_audio_help:
4290 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004291 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004292 break;
4293 case QEMU_OPTION_soundhw:
4294 select_soundhw (optarg);
4295 break;
4296#endif
4297 case QEMU_OPTION_h:
4298 qemu_help(0);
4299 break;
4300 case QEMU_OPTION_version:
4301 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004302 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004303 break;
4304 case QEMU_OPTION_m: {
4305 uint64_t value;
4306 char *ptr;
4307
4308 value = strtoul(optarg, &ptr, 10);
4309 switch (*ptr) {
4310 case 0: case 'M': case 'm':
4311 value <<= 20;
4312 break;
4313 case 'G': case 'g':
4314 value <<= 30;
4315 break;
4316 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004317 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004318 }
4319
4320 /* On 32-bit hosts, QEMU is limited by virtual address space */
4321 if (value > (2047 << 20)
4322#ifndef CONFIG_KQEMU
4323 && HOST_LONG_BITS == 32
4324#endif
4325 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004326 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004327 }
4328 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004329 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004330 }
4331 ram_size = value;
4332 break;
4333 }
4334 case QEMU_OPTION_d:
4335 {
4336 int mask;
4337 const CPULogItem *item;
4338
4339 mask = cpu_str_to_log_mask(optarg);
4340 if (!mask) {
4341 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004342 for(item = cpu_log_items; item->mask != 0; item++) {
4343 printf("%-10s %s\n", item->name, item->help);
4344 }
4345 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004346 }
4347 cpu_set_log(mask);
4348 }
4349 break;
4350 case QEMU_OPTION_s:
4351 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4352 break;
4353 case QEMU_OPTION_gdb:
4354 gdbstub_dev = optarg;
4355 break;
4356 case QEMU_OPTION_L:
4357 data_dir = optarg;
4358 break;
4359 case QEMU_OPTION_bios:
4360 bios_name = optarg;
4361 break;
4362 case QEMU_OPTION_singlestep:
4363 singlestep = 1;
4364 break;
4365 case QEMU_OPTION_S:
4366#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004367 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004368#endif
4369 autostart = 0;
4370 break;
4371#ifndef _WIN32
4372 case QEMU_OPTION_k:
4373 keyboard_layout = optarg;
4374 break;
4375#endif
4376 case QEMU_OPTION_localtime:
4377 rtc_utc = 0;
4378 break;
4379 case QEMU_OPTION_vga:
4380 select_vgahw (optarg);
4381 break;
4382#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4383 case QEMU_OPTION_g:
4384 {
4385 const char *p;
4386 int w, h, depth;
4387 p = optarg;
4388 w = strtol(p, (char **)&p, 10);
4389 if (w <= 0) {
4390 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004391 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004392 }
4393 if (*p != 'x')
4394 goto graphic_error;
4395 p++;
4396 h = strtol(p, (char **)&p, 10);
4397 if (h <= 0)
4398 goto graphic_error;
4399 if (*p == 'x') {
4400 p++;
4401 depth = strtol(p, (char **)&p, 10);
4402 if (depth != 8 && depth != 15 && depth != 16 &&
4403 depth != 24 && depth != 32)
4404 goto graphic_error;
4405 } else if (*p == '\0') {
4406 depth = graphic_depth;
4407 } else {
4408 goto graphic_error;
4409 }
4410
4411 graphic_width = w;
4412 graphic_height = h;
4413 graphic_depth = depth;
4414 }
4415 break;
4416#endif
4417 case QEMU_OPTION_echr:
4418 {
4419 char *r;
4420 term_escape_char = strtol(optarg, &r, 0);
4421 if (r == optarg)
4422 printf("Bad argument to echr\n");
4423 break;
4424 }
4425 case QEMU_OPTION_monitor:
4426 monitor_device = optarg;
4427 break;
4428 case QEMU_OPTION_serial:
4429 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004430 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004431 }
4432 serial_devices[serial_device_index] = optarg;
4433 serial_device_index++;
4434 break;
4435 case QEMU_OPTION_watchdog:
4436 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004437 if (i > 0) {
4438 if (i == 1) {
4439 PANIC("Invalid watchdog parameter: %s",
4440 optarg);
4441 } else {
4442 QEMU_EXIT(0);
4443 }
4444 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004445 break;
4446 case QEMU_OPTION_watchdog_action:
4447 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004448 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004449 }
4450 break;
4451 case QEMU_OPTION_virtiocon:
4452 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004453 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004454 }
4455 virtio_consoles[virtio_console_index] = optarg;
4456 virtio_console_index++;
4457 break;
4458 case QEMU_OPTION_parallel:
4459 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004460 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004461 }
4462 parallel_devices[parallel_device_index] = optarg;
4463 parallel_device_index++;
4464 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004465 case QEMU_OPTION_loadvm:
4466 loadvm = optarg;
4467 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004468 case QEMU_OPTION_savevm_on_exit:
4469 savevm_on_exit = optarg;
4470 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004471 case QEMU_OPTION_full_screen:
4472 full_screen = 1;
4473 break;
4474#ifdef CONFIG_SDL
4475 case QEMU_OPTION_no_frame:
4476 no_frame = 1;
4477 break;
4478 case QEMU_OPTION_alt_grab:
4479 alt_grab = 1;
4480 break;
4481 case QEMU_OPTION_no_quit:
4482 no_quit = 1;
4483 break;
4484 case QEMU_OPTION_sdl:
4485 display_type = DT_SDL;
4486 break;
4487#endif
4488 case QEMU_OPTION_pidfile:
4489 pid_file = optarg;
4490 break;
4491#ifdef TARGET_I386
4492 case QEMU_OPTION_win2k_hack:
4493 win2k_install_hack = 1;
4494 break;
4495 case QEMU_OPTION_rtc_td_hack:
4496 rtc_td_hack = 1;
4497 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004498#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004499 case QEMU_OPTION_acpitable:
4500 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004501 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004502 }
4503 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004504#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004505 case QEMU_OPTION_smbios:
4506 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004507 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004508 }
4509 break;
4510#endif
4511#ifdef CONFIG_KQEMU
4512 case QEMU_OPTION_no_kqemu:
4513 kqemu_allowed = 0;
4514 break;
4515 case QEMU_OPTION_kernel_kqemu:
4516 kqemu_allowed = 2;
4517 break;
4518#endif
4519#ifdef CONFIG_KVM
4520 case QEMU_OPTION_enable_kvm:
4521 kvm_allowed = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004522 break;
David 'Digit' Turner36597752011-05-20 01:18:01 +02004523 case QEMU_OPTION_disable_kvm:
4524 kvm_allowed = 0;
4525 break;
4526#endif /* CONFIG_KVM */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004527 case QEMU_OPTION_usb:
4528 usb_enabled = 1;
4529 break;
4530 case QEMU_OPTION_usbdevice:
4531 usb_enabled = 1;
4532 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004533 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004534 }
4535 usb_devices[usb_devices_index] = optarg;
4536 usb_devices_index++;
4537 break;
4538 case QEMU_OPTION_smp:
4539 smp_cpus = atoi(optarg);
4540 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004541 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004542 }
4543 break;
4544 case QEMU_OPTION_vnc:
4545 display_type = DT_VNC;
4546 vnc_display = optarg;
4547 break;
4548#ifdef TARGET_I386
4549 case QEMU_OPTION_no_acpi:
4550 acpi_enabled = 0;
4551 break;
4552 case QEMU_OPTION_no_hpet:
4553 no_hpet = 1;
4554 break;
4555 case QEMU_OPTION_no_virtio_balloon:
4556 no_virtio_balloon = 1;
4557 break;
4558#endif
4559 case QEMU_OPTION_no_reboot:
4560 no_reboot = 1;
4561 break;
4562 case QEMU_OPTION_no_shutdown:
4563 no_shutdown = 1;
4564 break;
4565 case QEMU_OPTION_show_cursor:
4566 cursor_hide = 0;
4567 break;
4568 case QEMU_OPTION_uuid:
4569 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004570 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004571 }
4572 break;
4573#ifndef _WIN32
4574 case QEMU_OPTION_daemonize:
4575 daemonize = 1;
4576 break;
4577#endif
4578 case QEMU_OPTION_option_rom:
4579 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004580 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004581 }
4582 option_rom[nb_option_roms] = optarg;
4583 nb_option_roms++;
4584 break;
4585#if defined(TARGET_ARM) || defined(TARGET_M68K)
4586 case QEMU_OPTION_semihosting:
4587 semihosting_enabled = 1;
4588 break;
4589#endif
4590 case QEMU_OPTION_name:
4591 qemu_name = optarg;
4592 break;
4593#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4594 case QEMU_OPTION_prom_env:
4595 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004596 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004597 }
4598 prom_envs[nb_prom_envs] = optarg;
4599 nb_prom_envs++;
4600 break;
4601#endif
4602#ifdef TARGET_ARM
4603 case QEMU_OPTION_old_param:
4604 old_param = 1;
4605 break;
4606#endif
4607 case QEMU_OPTION_clock:
4608 configure_alarms(optarg);
4609 break;
4610 case QEMU_OPTION_startdate:
4611 {
4612 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004613 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004614 if (!strcmp(optarg, "now")) {
4615 rtc_date_offset = -1;
4616 } else {
4617 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4618 &tm.tm_year,
4619 &tm.tm_mon,
4620 &tm.tm_mday,
4621 &tm.tm_hour,
4622 &tm.tm_min,
4623 &tm.tm_sec) == 6) {
4624 /* OK */
4625 } else if (sscanf(optarg, "%d-%d-%d",
4626 &tm.tm_year,
4627 &tm.tm_mon,
4628 &tm.tm_mday) == 3) {
4629 tm.tm_hour = 0;
4630 tm.tm_min = 0;
4631 tm.tm_sec = 0;
4632 } else {
4633 goto date_fail;
4634 }
4635 tm.tm_year -= 1900;
4636 tm.tm_mon--;
4637 rtc_start_date = mktimegm(&tm);
4638 if (rtc_start_date == -1) {
4639 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004640 PANIC("Invalid date format. Valid format are:\n"
4641 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004642 }
4643 rtc_date_offset = time(NULL) - rtc_start_date;
4644 }
4645 }
4646 break;
rich cannings7339b552011-02-16 13:43:44 -08004647
4648 /* -------------------------------------------------------*/
4649 /* User mode network stack restrictions */
4650 case QEMU_OPTION_drop_udp:
4651 slirp_drop_udp();
4652 break;
4653 case QEMU_OPTION_drop_tcp:
4654 slirp_drop_tcp();
4655 break;
4656 case QEMU_OPTION_allow_tcp:
4657 slirp_allow(optarg, IPPROTO_TCP);
4658 break;
4659 case QEMU_OPTION_allow_udp:
4660 slirp_allow(optarg, IPPROTO_UDP);
4661 break;
4662 case QEMU_OPTION_drop_log:
4663 {
4664 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004665 drop_log_filename = optarg;
4666 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004667
4668 if (!drop_log_fd) {
4669 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4670 exit(1);
4671 }
4672
4673 slirp_drop_log_fd(drop_log_fd);
4674 }
4675 break;
4676
4677 case QEMU_OPTION_dns_log:
4678 {
4679 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004680 dns_log_filename = optarg;
4681 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004682
4683 if (dns_log_fd == NULL) {
4684 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4685 exit(1);
4686 }
4687
4688 slirp_dns_log_fd(dns_log_fd);
4689 }
4690 break;
4691
4692
4693 case QEMU_OPTION_max_dns_conns:
4694 {
4695 int max_dns_conns = 0;
4696 if (parse_int(optarg, &max_dns_conns)) {
4697 fprintf(stderr,
4698 "qemu: syntax: -max-dns-conns max_connections\n");
4699 exit(1);
4700 }
4701 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4702 fprintf(stderr,
4703 "Invalid arg for max dns connections: %s\n",
4704 optarg);
4705 exit(1);
4706 }
4707 slirp_set_max_dns_conns(max_dns_conns);
4708 }
4709 break;
4710
4711 case QEMU_OPTION_net_forward:
4712 net_slirp_forward(optarg);
4713 break;
4714 case QEMU_OPTION_net_forward_tcp2sink:
4715 {
4716 SockAddress saddr;
4717
4718 if (parse_host_port(&saddr, optarg)) {
4719 fprintf(stderr,
4720 "Invalid ip/port %s for "
4721 "-forward-dropped-tcp2sink. "
4722 "We expect 'sink_ip:sink_port'\n",
4723 optarg);
4724 exit(1);
4725 }
4726 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4727 saddr.u.inet.port);
4728 }
4729 break;
4730 /* -------------------------------------------------------*/
4731
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004732 case QEMU_OPTION_tb_size:
4733 tb_size = strtol(optarg, NULL, 0);
4734 if (tb_size < 0)
4735 tb_size = 0;
4736 break;
4737 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004738 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004739 break;
4740 case QEMU_OPTION_incoming:
4741 incoming = optarg;
4742 break;
4743#ifndef _WIN32
4744 case QEMU_OPTION_chroot:
4745 chroot_dir = optarg;
4746 break;
4747 case QEMU_OPTION_runas:
4748 run_as = optarg;
4749 break;
4750#endif
4751#ifdef CONFIG_XEN
4752 case QEMU_OPTION_xen_domid:
4753 xen_domid = atoi(optarg);
4754 break;
4755 case QEMU_OPTION_xen_create:
4756 xen_mode = XEN_CREATE;
4757 break;
4758 case QEMU_OPTION_xen_attach:
4759 xen_mode = XEN_ATTACH;
4760 break;
4761#endif
4762
4763
4764 case QEMU_OPTION_mic:
4765 audio_input_source = (char*)optarg;
4766 break;
4767#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004768 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004769 trace_filename = optarg;
4770 tracing = 1;
4771 break;
4772#if 0
4773 case QEMU_OPTION_trace_miss:
4774 trace_cache_miss = 1;
4775 break;
4776 case QEMU_OPTION_trace_addr:
4777 trace_all_addr = 1;
4778 break;
4779#endif
4780 case QEMU_OPTION_tracing:
4781 if (strcmp(optarg, "off") == 0)
4782 tracing = 0;
4783 else if (strcmp(optarg, "on") == 0 && trace_filename)
4784 tracing = 1;
4785 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004786 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004787 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004788 }
4789 break;
4790#if 0
4791 case QEMU_OPTION_dcache_load_miss:
4792 dcache_load_miss_penalty = atoi(optarg);
4793 break;
4794 case QEMU_OPTION_dcache_store_miss:
4795 dcache_store_miss_penalty = atoi(optarg);
4796 break;
4797#endif
4798#endif
4799#ifdef CONFIG_NAND
4800 case QEMU_OPTION_nand:
4801 nand_add_dev(optarg);
4802 break;
4803#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004804 case QEMU_OPTION_android_ports:
4805 android_op_ports = (char*)optarg;
4806 break;
4807
4808 case QEMU_OPTION_android_port:
4809 android_op_port = (char*)optarg;
4810 break;
4811
4812 case QEMU_OPTION_android_report_console:
4813 android_op_report_console = (char*)optarg;
4814 break;
4815
4816 case QEMU_OPTION_http_proxy:
4817 op_http_proxy = (char*)optarg;
4818 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004819
4820 case QEMU_OPTION_charmap:
4821 op_charmap_file = (char*)optarg;
4822 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004823
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004824 case QEMU_OPTION_android_hw:
4825 android_op_hwini = (char*)optarg;
4826 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004827
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004828 case QEMU_OPTION_dns_server:
4829 android_op_dns_server = (char*)optarg;
4830 break;
4831
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004832 case QEMU_OPTION_radio:
4833 android_op_radio = (char*)optarg;
4834 break;
4835
4836 case QEMU_OPTION_gps:
4837 android_op_gps = (char*)optarg;
4838 break;
4839
4840 case QEMU_OPTION_audio:
4841 android_op_audio = (char*)optarg;
4842 break;
4843
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004844 case QEMU_OPTION_cpu_delay:
4845 android_op_cpu_delay = (char*)optarg;
4846 break;
4847
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004848 case QEMU_OPTION_show_kernel:
4849 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4850 break;
4851
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004852#ifdef CONFIG_NAND_LIMITS
4853 case QEMU_OPTION_nand_limits:
4854 android_op_nand_limits = (char*)optarg;
4855 break;
4856#endif // CONFIG_NAND_LIMITS
4857
4858 case QEMU_OPTION_netspeed:
4859 android_op_netspeed = (char*)optarg;
4860 break;
4861
4862 case QEMU_OPTION_netdelay:
4863 android_op_netdelay = (char*)optarg;
4864 break;
4865
4866 case QEMU_OPTION_netfast:
4867 android_op_netfast = 1;
4868 break;
4869
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004870 case QEMU_OPTION_tcpdump:
4871 android_op_tcpdump = (char*)optarg;
4872 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004873
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004874 case QEMU_OPTION_boot_property:
4875 boot_property_parse_option((char*)optarg);
4876 break;
4877
4878 case QEMU_OPTION_lcd_density:
4879 android_op_lcd_density = (char*)optarg;
4880 break;
4881
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004882 case QEMU_OPTION_ui_port:
4883 android_op_ui_port = (char*)optarg;
4884 break;
4885
4886 case QEMU_OPTION_ui_settings:
4887 android_op_ui_settings = (char*)optarg;
4888 break;
4889
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004890 case QEMU_OPTION_audio_test_out:
4891 android_audio_test_start_out();
4892 break;
4893
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004894 case QEMU_OPTION_android_avdname:
4895 android_op_avd_name = (char*)optarg;
4896 break;
4897
4898 case QEMU_OPTION_timezone:
4899 if (timezone_set((char*)optarg)) {
4900 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4901 (char*)optarg);
4902 }
4903 break;
4904
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004905#ifdef CONFIG_MEMCHECK
4906 case QEMU_OPTION_android_memcheck:
4907 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004908 /* This will set ro.kernel.memcheck system property
4909 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004910 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004911 break;
4912#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004913
4914 case QEMU_OPTION_snapshot_no_time_update:
4915 android_snapshot_update_time = 0;
4916 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004917 }
4918 }
4919 }
4920
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004921 /* Initialize character map. */
4922 if (android_charmap_setup(op_charmap_file)) {
4923 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004924 PANIC(
4925 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004926 op_charmap_file);
4927 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004928 PANIC(
4929 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004930 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004931 }
4932
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004933 /* If no data_dir is specified then try to find it relative to the
4934 executable path. */
4935 if (!data_dir) {
4936 data_dir = find_datadir(argv[0]);
4937 }
4938 /* If all else fails use the install patch specified when building. */
4939 if (!data_dir) {
4940 data_dir = CONFIG_QEMU_SHAREDIR;
4941 }
4942
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004943 if (!android_op_hwini) {
4944 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004945 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004946 hw_ini = iniFile_newFromFile(android_op_hwini);
4947 if (hw_ini == NULL) {
4948 PANIC("Could not find %s file.", android_op_hwini);
4949 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004950
4951 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004952 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004953
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004954 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004955
4956 {
4957 int width = android_hw->hw_lcd_width;
4958 int height = android_hw->hw_lcd_height;
4959 int depth = android_hw->hw_lcd_depth;
4960
4961 /* A bit of sanity checking */
4962 if (width <= 0 || height <= 0 ||
4963 (depth != 16 && depth != 32) ||
4964 (((width|height) & 3) != 0) )
4965 {
4966 PANIC("Invalid display configuration (%d,%d,%d)",
4967 width, height, depth);
4968 }
4969 android_display_width = width;
4970 android_display_height = height;
4971 android_display_bpp = depth;
4972 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004973
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004974#ifdef CONFIG_NAND_LIMITS
4975 /* Init nand stuff. */
4976 if (android_op_nand_limits) {
4977 parse_nand_limits(android_op_nand_limits);
4978 }
4979#endif // CONFIG_NAND_LIMITS
4980
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004981 /* Initialize AVD name from hardware configuration if needed */
4982 if (!android_op_avd_name) {
4983 if (android_hw->avd_name && *android_hw->avd_name) {
4984 android_op_avd_name = android_hw->avd_name;
4985 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
4986 }
4987 }
4988
David 'Digit' Turner40841b22011-03-01 14:04:00 +01004989 /* Initialize system partition image */
4990 {
4991 char tmp[PATH_MAX+32];
4992 const char* sysImage = android_hw->disk_systemPartition_path;
4993 const char* initImage = android_hw->disk_systemPartition_initPath;
4994 uint64_t sysBytes = android_hw->disk_systemPartition_size;
4995
4996 if (sysBytes == 0) {
4997 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
4998 }
4999
5000 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
5001
5002 if (sysImage && *sysImage) {
5003 if (filelock_create(sysImage) == NULL) {
5004 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
5005 /* If there is no file= parameters, nand_add_dev will create
5006 * a temporary file to back the partition image. */
5007 } else {
5008 pstrcat(tmp,sizeof(tmp),",file=");
5009 pstrcat(tmp,sizeof(tmp),sysImage);
5010 }
5011 }
5012 if (initImage && *initImage) {
5013 if (!path_exists(initImage)) {
5014 PANIC("Invalid initial system image path: %s", initImage);
5015 }
5016 pstrcat(tmp,sizeof(tmp),",initfile=");
5017 pstrcat(tmp,sizeof(tmp),initImage);
5018 } else {
5019 PANIC("Missing initial system image path!");
5020 }
5021 nand_add_dev(tmp);
5022 }
5023
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005024 /* Initialize data partition image */
5025 {
5026 char tmp[PATH_MAX+32];
5027 const char* dataImage = android_hw->disk_dataPartition_path;
5028 const char* initImage = android_hw->disk_dataPartition_initPath;
5029 uint64_t dataBytes = android_hw->disk_dataPartition_size;
5030
5031 if (dataBytes == 0) {
5032 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
5033 }
5034
5035 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
5036
5037 if (dataImage && *dataImage) {
5038 if (filelock_create(dataImage) == NULL) {
5039 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
5040 /* Note: if there is no file= parameters, nand_add_dev() will
5041 * create a temporary file to back the partition image. */
5042 } else {
5043 /* Create the file if needed */
5044 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005045 if (path_empty_file(dataImage) < 0) {
5046 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
5047 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005048 }
5049 pstrcat(tmp, sizeof(tmp), ",file=");
5050 pstrcat(tmp, sizeof(tmp), dataImage);
5051 }
5052 }
5053 if (initImage && *initImage) {
5054 pstrcat(tmp, sizeof(tmp), ",initfile=");
5055 pstrcat(tmp, sizeof(tmp), initImage);
5056 }
5057 nand_add_dev(tmp);
5058 }
5059
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005060 /* Init SD-Card stuff. For Android, it is always hda */
5061 /* If the -hda option was used, ignore the Android-provided one */
5062 if (hda_opts == NULL) {
5063 const char* sdPath = android_hw->hw_sdCard_path;
5064 if (sdPath && *sdPath) {
5065 if (!path_exists(sdPath)) {
5066 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
5067 } else if (filelock_create(sdPath) == NULL) {
5068 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
5069 } else {
5070 /* Successful locking */
5071 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005072 /* Set this property of any operation involving the SD Card
5073 * will be x100 slower, due to the corresponding file being
5074 * mounted as O_DIRECT. Note that this is only 'unsafe' in
5075 * the context of an emulator crash. The data is already
5076 * synced properly when the emulator exits (either normally or through ^C).
5077 */
5078 qemu_opt_set(hda_opts, "cache", "unsafe");
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005079 }
5080 }
5081 }
5082
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005083 if (hdb_opts == NULL) {
5084 const char* spath = android_hw->disk_snapStorage_path;
5085 if (spath && *spath) {
5086 if (!path_exists(spath)) {
5087 PANIC("Snapshot storage file does not exist: %s", spath);
5088 }
5089 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005090 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005091 }
5092 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner8fc3e6e2011-05-09 10:17:20 +02005093 /* See comment above to understand why this is needed. */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02005094 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005095 }
5096 }
5097
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005098 /* Set the VM's max heap size, passed as a boot property */
5099 if (android_hw->vm_heapSize > 0) {
5100 char tmp[64];
5101 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
5102 boot_property_add("dalvik.vm.heapsize",tmp);
5103 }
5104
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005105 /* Initialize net speed and delays stuff. */
5106 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005107 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005108 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005109 }
5110
5111 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005112 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005113 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005114 }
5115
5116 if (android_op_netfast) {
5117 qemu_net_download_speed = 0;
5118 qemu_net_upload_speed = 0;
5119 qemu_net_min_latency = 0;
5120 qemu_net_max_latency = 0;
5121 }
5122
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005123 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005124 if (android_hw->hw_lcd_density) {
5125 long density = android_hw->hw_lcd_density;
5126 if (density <= 0) {
5127 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005128 }
5129 hwLcd_setBootProperty(density);
5130 }
5131
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07005132 /* Initialize TCP dump */
5133 if (android_op_tcpdump) {
5134 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
5135 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
5136 }
5137 }
5138
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005139 /* Initialize modem */
5140 if (android_op_radio) {
5141 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
5142 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005143 PANIC("unsupported character device specification: %s\n"
5144 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005145 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005146 }
5147 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
5148 } else if (android_hw->hw_gsmModem != 0 ) {
5149 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005150 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005151 }
5152 }
5153
5154 /* Initialize GPS */
5155 if (android_op_gps) {
5156 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
5157 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005158 PANIC("unsupported character device specification: %s\n"
5159 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005160 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005161 }
5162 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
5163 } else if (android_hw->hw_gps != 0) {
5164 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005165 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005166 }
5167 }
5168
5169 /* Initialize audio. */
5170 if (android_op_audio) {
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005171 char temp[128];
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005172 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005173 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005174 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005175 }
David 'Digit' Turnercc3b2192011-05-21 00:42:27 +02005176 snprintf(temp, sizeof temp, "QEMU_AUDIO_DRV=%s", android_op_audio);
5177 putenv(temp);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005178 }
5179
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005180 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02005181 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005182
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005183 if (android_op_cpu_delay) {
5184 char* end;
5185 long delay = strtol(android_op_cpu_delay, &end, 0);
5186 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005187 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005188 }
5189 if (delay > 0)
5190 delay = (1000-delay);
5191
5192 qemu_cpu_delay = (int) delay;
5193 }
5194
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005195 if (android_op_dns_server) {
5196 char* x = strchr(android_op_dns_server, ',');
5197 dns_count = 0;
5198 if (x == NULL)
5199 {
5200 if ( add_dns_server( android_op_dns_server ) == 0 )
5201 dns_count = 1;
5202 }
5203 else
5204 {
5205 x = android_op_dns_server;
5206 while (*x) {
5207 char* y = strchr(x, ',');
5208
5209 if (y != NULL) {
5210 *y = 0;
5211 y++;
5212 } else {
5213 y = x + strlen(x);
5214 }
5215
5216 if (y > x && add_dns_server( x ) == 0) {
5217 dns_count += 1;
5218 }
5219 x = y;
5220 }
5221 }
5222 if (dns_count == 0)
5223 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5224 }
5225
5226 if (dns_count == 0)
5227 dns_count = slirp_get_system_dns_servers();
5228 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005229 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005230 }
5231
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005232#ifdef CONFIG_MEMCHECK
5233 if (android_op_memcheck) {
5234 memcheck_init(android_op_memcheck);
5235 }
5236#endif // CONFIG_MEMCHECK
5237
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005238 /* Initialize cache partition, if any */
5239 if (android_hw->disk_cachePartition != 0) {
5240 char tmp[PATH_MAX+32];
5241 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005242 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005243
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005244 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5245
5246 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5247 if (filelock_create(partPath) == NULL) {
5248 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5249 /* Note: if there is no file= parameters, nand_add_dev() will
5250 * create a temporary file to back the partition image. */
5251 } else {
5252 /* Create the file if needed */
5253 if (!path_exists(partPath)) {
5254 if (path_empty_file(partPath) < 0) {
5255 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5256 }
5257 }
5258 pstrcat(tmp, sizeof(tmp), ",file=");
5259 pstrcat(tmp, sizeof(tmp), partPath);
5260 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005261 }
5262 nand_add_dev(tmp);
5263 }
5264
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005265 /* We always force qemu=1 when running inside QEMU */
5266 stralloc_add_str(kernel_params, " qemu=1");
5267
5268 /* We always initialize the first serial port for the android-kmsg
5269 * character device (used to send kernel messages) */
5270 serial_hds_add_at(0, "android-kmsg");
5271 stralloc_add_str(kernel_params, " console=ttyS0");
5272
5273 /* We always initialize the second serial port for the android-qemud
5274 * character device as well */
5275 serial_hds_add_at(1, "android-qemud");
5276 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5277
David 'Digit' Turner36597752011-05-20 01:18:01 +02005278#if defined(CONFIG_KVM)
5279 if (kvm_allowed < 0) {
5280 kvm_allowed = kvm_check_allowed();
5281 }
5282#endif
5283
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005284#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5285 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005286 PANIC(
5287 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005288 }
5289#endif
5290
5291 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5292 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005293 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5294 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005295 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005296 }
5297
5298 if (display_type == DT_NOGRAPHIC) {
5299 if (serial_device_index == 0)
5300 serial_devices[0] = "stdio";
5301 if (parallel_device_index == 0)
5302 parallel_devices[0] = "null";
5303 if (strncmp(monitor_device, "vc", 2) == 0)
5304 monitor_device = "stdio";
5305 }
5306
5307#ifndef _WIN32
5308 if (daemonize) {
5309 pid_t pid;
5310
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005311 if (pipe(fds) == -1) {
5312 PANIC("Unable to aquire pidfile");
5313 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005314
5315 pid = fork();
5316 if (pid > 0) {
5317 uint8_t status;
5318 ssize_t len;
5319
5320 close(fds[1]);
5321
5322 again:
5323 len = read(fds[0], &status, 1);
5324 if (len == -1 && (errno == EINTR))
5325 goto again;
5326
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005327 if (len != 1) {
5328 PANIC("Error when aquiring pidfile");
5329 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005330 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005331 PANIC("Could not acquire pidfile");
5332 } else {
5333 QEMU_EXIT(0);
5334 }
5335 } else if (pid < 0) {
5336 PANIC("Unable to daemonize");
5337 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005338
5339 setsid();
5340
5341 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005342 if (pid > 0) {
5343 QEMU_EXIT(0);
5344 } else if (pid < 0) {
5345 PANIC("Could not acquire pid file");
5346 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005347
5348 umask(027);
5349
5350 signal(SIGTSTP, SIG_IGN);
5351 signal(SIGTTOU, SIG_IGN);
5352 signal(SIGTTIN, SIG_IGN);
5353 }
5354
5355 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5356 if (daemonize) {
5357 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005358 int ret;
5359 do {
5360 ret = write(fds[1], &status, 1);
5361 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005362 PANIC("Could not acquire pid file");
5363 } else {
5364 PANIC("Could not acquire pid file");
5365 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005366 }
5367#endif
5368
5369#ifdef CONFIG_KQEMU
5370 if (smp_cpus > 1)
5371 kqemu_allowed = 0;
5372#endif
5373 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005374 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005375 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005376
5377 if (kernel_filename == NULL) {
5378 kernel_filename = android_hw->kernel_path;
5379 }
5380 if (initrd_filename == NULL) {
5381 initrd_filename = android_hw->disk_ramdisk_path;
5382 }
5383
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005384 linux_boot = (kernel_filename != NULL);
5385 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5386
5387 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005388 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005389 }
5390
5391 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005392 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005393 }
5394
5395 /* boot to floppy or the default cd if no hard disk defined yet */
5396 if (!boot_devices[0]) {
5397 boot_devices = "cad";
5398 }
5399 setvbuf(stdout, NULL, _IOLBF, 0);
5400
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005401 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005402 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005403 }
David Turner6a9ef172010-09-09 22:54:36 +02005404 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005405
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005406 /* init network clients */
5407 if (nb_net_clients == 0) {
5408 /* if no clients, we use a default config */
5409 net_clients[nb_net_clients++] = "nic";
5410#ifdef CONFIG_SLIRP
5411 net_clients[nb_net_clients++] = "user";
5412#endif
5413 }
5414
5415 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005416 if (net_client_parse(net_clients[i]) < 0) {
5417 PANIC("Unable to parse net clients");
5418 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005419 }
5420 net_client_check();
5421
5422#ifdef TARGET_I386
5423 /* XXX: this should be moved in the PC machine instantiation code */
5424 if (net_boot != 0) {
5425 int netroms = 0;
5426 for (i = 0; i < nb_nics && i < 4; i++) {
5427 const char *model = nd_table[i].model;
5428 char buf[1024];
5429 char *filename;
5430 if (net_boot & (1 << i)) {
5431 if (model == NULL)
5432 model = "ne2k_pci";
5433 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5434 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5435 if (filename && get_image_size(filename) > 0) {
5436 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005437 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005438 }
5439 option_rom[nb_option_roms] = qemu_strdup(buf);
5440 nb_option_roms++;
5441 netroms++;
5442 }
5443 if (filename) {
5444 qemu_free(filename);
5445 }
5446 }
5447 }
5448 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005449 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005450 }
5451 }
5452#endif
5453
5454 /* init the bluetooth world */
5455 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005456 if (bt_parse(bt_opts[i])) {
5457 PANIC("Unable to parse bluetooth options");
5458 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005459
5460 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005461 if (ram_size == 0) {
5462 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5463 if (ram_size == 0) {
5464 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5465 }
5466 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005467
5468#ifdef CONFIG_KQEMU
5469 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5470 guest ram allocation. It needs to go away. */
5471 if (kqemu_allowed) {
5472 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5473 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5474 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005475 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005476 }
5477 }
5478#endif
5479
rich canningsd952f282011-03-01 15:40:09 -08005480#ifndef _WIN32
5481 init_qemu_clear_logs_sig();
5482#endif
5483
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005484 /* init the dynamic translator */
5485 cpu_exec_init_all(tb_size * 1024 * 1024);
5486
5487 bdrv_init();
5488
5489 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005490#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005491 if (nb_drives_opt < MAX_DRIVES)
5492 drive_add(NULL, CDROM_ALIAS);
5493
5494 /* we always create at least one floppy */
5495
5496 if (nb_drives_opt < MAX_DRIVES)
5497 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005498 /* we always create one sd slot, even if no card is in it */
5499
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005500 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005501 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005502 }
5503#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005504
5505 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005506 if (snapshot)
5507 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5508 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5509 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005510
David Turner6a9ef172010-09-09 22:54:36 +02005511 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005512 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5513
5514#ifndef _WIN32
5515 /* must be after terminal init, SDL library changes signal handlers */
5516 sighandler_setup();
5517#endif
5518
5519 /* Maintain compatibility with multiple stdio monitors */
5520 if (!strcmp(monitor_device,"stdio")) {
5521 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5522 const char *devname = serial_devices[i];
5523 if (devname && !strcmp(devname,"mon:stdio")) {
5524 monitor_device = NULL;
5525 break;
5526 } else if (devname && !strcmp(devname,"stdio")) {
5527 monitor_device = NULL;
5528 serial_devices[i] = "mon:stdio";
5529 break;
5530 }
5531 }
5532 }
5533
5534 if (nb_numa_nodes > 0) {
5535 int i;
5536
5537 if (nb_numa_nodes > smp_cpus) {
5538 nb_numa_nodes = smp_cpus;
5539 }
5540
5541 /* If no memory size if given for any node, assume the default case
5542 * and distribute the available memory equally across all nodes
5543 */
5544 for (i = 0; i < nb_numa_nodes; i++) {
5545 if (node_mem[i] != 0)
5546 break;
5547 }
5548 if (i == nb_numa_nodes) {
5549 uint64_t usedmem = 0;
5550
5551 /* On Linux, the each node's border has to be 8MB aligned,
5552 * the final node gets the rest.
5553 */
5554 for (i = 0; i < nb_numa_nodes - 1; i++) {
5555 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5556 usedmem += node_mem[i];
5557 }
5558 node_mem[i] = ram_size - usedmem;
5559 }
5560
5561 for (i = 0; i < nb_numa_nodes; i++) {
5562 if (node_cpumask[i] != 0)
5563 break;
5564 }
5565 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5566 * must cope with this anyway, because there are BIOSes out there in
5567 * real machines which also use this scheme.
5568 */
5569 if (i == nb_numa_nodes) {
5570 for (i = 0; i < smp_cpus; i++) {
5571 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5572 }
5573 }
5574 }
5575
5576 if (kvm_enabled()) {
5577 int ret;
5578
5579 ret = kvm_init(smp_cpus);
5580 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005581 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005582 }
5583 }
5584
5585 if (monitor_device) {
5586 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5587 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005588 PANIC("qemu: could not open monitor device '%s'",
5589 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005590 }
5591 }
5592
5593 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005594 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005595 }
5596
5597 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5598 const char *devname = parallel_devices[i];
5599 if (devname && strcmp(devname, "none")) {
5600 char label[32];
5601 snprintf(label, sizeof(label), "parallel%d", i);
5602 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5603 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005604 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005605 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005606 }
5607 }
5608 }
5609
5610 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5611 const char *devname = virtio_consoles[i];
5612 if (devname && strcmp(devname, "none")) {
5613 char label[32];
5614 snprintf(label, sizeof(label), "virtcon%d", i);
5615 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5616 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005617 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005618 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005619 }
5620 }
5621 }
5622
5623 module_call_init(MODULE_INIT_DEVICE);
5624
5625
5626#ifdef CONFIG_TRACE
5627 if (trace_filename) {
5628 trace_init(trace_filename);
5629#if 0
5630 // We don't need the dcache code until we can get load and store tracing
5631 // working again.
5632 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5633 dcache_replace_policy, dcache_load_miss_penalty,
5634 dcache_store_miss_penalty);
5635#endif
5636 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5637 }
5638#endif
5639
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005640 /* Check the CPU Architecture value */
5641#if defined(TARGET_ARM)
5642 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5643 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5644 android_hw->hw_cpu_arch);
5645 exit(1);
5646 }
5647#elif defined(TARGET_X86)
5648 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5649 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5650 android_hw->hw_cpu_arch);
5651 exit(1);
5652 }
5653#endif
5654
5655 /* Grab CPU model if provided in hardware.ini */
5656 if ( !cpu_model
5657 && android_hw->hw_cpu_model
5658 && android_hw->hw_cpu_model[0] != '\0')
5659 {
5660 cpu_model = android_hw->hw_cpu_model;
5661 }
5662
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005663 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005664 * collected during initialization.
5665 *
5666 * The order is the following:
5667 * - parameters from the hw configuration (kernel.parameters)
5668 * - additionnal parameters from options (e.g. -memcheck)
5669 * - the -append parameters.
5670 */
5671 {
5672 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005673
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005674 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005675 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005676 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5677 }
5678
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005679 /* If not empty, kernel_config always contains a leading space */
5680 stralloc_append(kernel_params, kernel_config);
5681
5682 if (*kernel_cmdline) {
5683 stralloc_add_c(kernel_params, ' ');
5684 stralloc_add_str(kernel_params, kernel_cmdline);
5685 }
5686
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005687 /* Remove any leading/trailing spaces */
5688 stralloc_strip(kernel_params);
5689
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005690 kernel_parameters = stralloc_cstr(kernel_params);
5691 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5692
5693 machine->init(ram_size,
5694 boot_devices,
5695 kernel_filename,
5696 kernel_parameters,
5697 initrd_filename,
5698 cpu_model);
5699
5700 stralloc_reset(kernel_params);
5701 stralloc_reset(kernel_config);
5702 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005703
5704
5705 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5706 for (i = 0; i < nb_numa_nodes; i++) {
5707 if (node_cpumask[i] & (1 << env->cpu_index)) {
5708 env->numa_node = i;
5709 }
5710 }
5711 }
5712
5713 current_machine = machine;
5714
5715 /* Set KVM's vcpu state to qemu's initial CPUState. */
5716 if (kvm_enabled()) {
5717 int ret;
5718
5719 ret = kvm_sync_vcpus();
5720 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005721 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005722 }
5723 }
5724
5725 /* init USB devices */
5726 if (usb_enabled) {
5727 for(i = 0; i < usb_devices_index; i++) {
5728 if (usb_device_add(usb_devices[i], 0) < 0) {
5729 fprintf(stderr, "Warning: could not add USB device %s\n",
5730 usb_devices[i]);
5731 }
5732 }
5733 }
5734
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005735 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005736 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005737
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005738 /* Initialize display from the command line parameters. */
5739 android_display_reset(ds,
5740 android_display_width,
5741 android_display_height,
5742 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005743
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005744 if (display_type == DT_DEFAULT) {
5745#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5746 display_type = DT_SDL;
5747#else
5748 display_type = DT_VNC;
5749 vnc_display = "localhost:0,to=99";
5750 show_vnc_port = 1;
5751#endif
5752 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005753
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005754
5755 switch (display_type) {
5756 case DT_NOGRAPHIC:
5757 break;
5758#if defined(CONFIG_CURSES)
5759 case DT_CURSES:
5760 curses_display_init(ds, full_screen);
5761 break;
5762#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005763#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005764 case DT_SDL:
5765 sdl_display_init(ds, full_screen, no_frame);
5766 break;
5767#elif defined(CONFIG_COCOA)
5768 case DT_SDL:
5769 cocoa_display_init(ds, full_screen);
5770 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005771#elif defined(CONFIG_STANDALONE_CORE)
5772 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005773 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005774 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005775#endif
5776 case DT_VNC:
5777 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005778 if (vnc_display_open(ds, vnc_display) < 0) {
5779 PANIC("Unable to initialize VNC display");
5780 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005781
5782 if (show_vnc_port) {
5783 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5784 }
5785 break;
5786 default:
5787 break;
5788 }
5789 dpy_resize(ds);
5790
5791 dcl = ds->listeners;
5792 while (dcl != NULL) {
5793 if (dcl->dpy_refresh != NULL) {
5794 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5795 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5796 }
5797 dcl = dcl->next;
5798 }
5799
5800 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5801 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5802 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5803 }
5804
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005805 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005806 qemu_chr_initial_reset();
5807
5808 if (monitor_device && monitor_hd)
5809 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5810
5811 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5812 const char *devname = serial_devices[i];
5813 if (devname && strcmp(devname, "none")) {
5814 if (strstart(devname, "vc", 0))
5815 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5816 }
5817 }
5818
5819 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5820 const char *devname = parallel_devices[i];
5821 if (devname && strcmp(devname, "none")) {
5822 if (strstart(devname, "vc", 0))
5823 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5824 }
5825 }
5826
5827 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5828 const char *devname = virtio_consoles[i];
5829 if (virtcon_hds[i] && devname) {
5830 if (strstart(devname, "vc", 0))
5831 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5832 }
5833 }
5834
5835 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005836 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005837 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005838 }
5839
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005840 /* call android-specific setup function */
5841 android_emulation_setup();
5842
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005843#if !defined(CONFIG_STANDALONE_CORE)
5844 // For the standalone emulator (UI+core in one executable) we need to
5845 // set the window title here.
5846 android_emulator_set_base_port(android_base_port);
5847#endif
5848
Ot ten Thije871da2a2010-09-20 10:29:22 +01005849 if (loadvm)
5850 do_loadvm(cur_mon, loadvm);
5851
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005852 if (incoming) {
5853 autostart = 0; /* fixme how to deal with -daemonize */
5854 qemu_start_incoming_migration(incoming);
5855 }
5856
5857 if (autostart)
5858 vm_start();
5859
5860#ifndef _WIN32
5861 if (daemonize) {
5862 uint8_t status = 0;
5863 ssize_t len;
5864
5865 again1:
5866 len = write(fds[1], &status, 1);
5867 if (len == -1 && (errno == EINTR))
5868 goto again1;
5869
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005870 if (len != 1) {
5871 PANIC("Unable to daemonize");
5872 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005873
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005874 if (chdir("/")) {
5875 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005876 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005877 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005878 TFR(fd = open("/dev/null", O_RDWR));
5879 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005880 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005881 }
5882
5883 if (run_as) {
5884 pwd = getpwnam(run_as);
5885 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005886 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005887 }
5888 }
5889
5890 if (chroot_dir) {
5891 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005892 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005893 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005894 if (chdir("/")) {
5895 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005896 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005897 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005898 }
5899
5900 if (run_as) {
5901 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005902 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005903 }
5904 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005905 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005906 }
5907 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005908 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005909 }
5910 }
5911
5912 if (daemonize) {
5913 dup2(fd, 0);
5914 dup2(fd, 1);
5915 dup2(fd, 2);
5916
5917 close(fd);
5918 }
5919#endif
5920
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005921#ifdef CONFIG_ANDROID
5922 // This will notify the UI that the core is successfuly initialized
5923 android_core_init_completed();
5924#endif // CONFIG_ANDROID
5925
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005926 main_loop();
5927 quit_timers();
5928 net_cleanup();
5929 android_emulation_teardown();
5930 return 0;
5931}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005932
5933void
5934android_emulation_teardown(void)
5935{
5936 android_charmap_done();
5937}