blob: a1b88bed5149fd410a7b181de12b949916dead4d [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"
198#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700199#include "android/hw-lcd.h"
200#include "android/boot-properties.h"
David 'Digit' Turnerca950592011-04-27 12:26:15 +0200201#include "android/hw-control.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700202#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100203#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700204
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700205#ifdef CONFIG_STANDALONE_CORE
206/* Verbose value used by the standalone emulator core (without UI) */
207unsigned long android_verbose;
208#endif // CONFIG_STANDALONE_CORE
209
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800210#if !defined(CONFIG_STANDALONE_CORE)
211/* in android/qemulator.c */
212extern void android_emulator_set_base_port(int port);
213#endif
214
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700215#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700216#undef main
217#define main qemu_main
218#endif
219
220#include "disas.h"
221
222#include "exec-all.h"
223
224#ifdef CONFIG_TRACE
225#include "trace.h"
226#include "dcache.h"
227#endif
228
229#include "qemu_socket.h"
230
231#if defined(CONFIG_SLIRP)
232#include "libslirp.h"
233#endif
234
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700235
236
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700237#define DEFAULT_RAM_SIZE 128
238
239/* Max number of USB devices that can be specified on the commandline. */
240#define MAX_USB_CMDLINE 8
241
242/* Max number of bluetooth switches on the commandline. */
243#define MAX_BT_CMDLINE 10
244
245/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700246
247static const char *data_dir;
248const char *bios_name = NULL;
249static void *ioport_opaque[MAX_IOPORTS];
250static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
251static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100252#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700253/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
254 to store the VM snapshots */
255DriveInfo drives_table[MAX_DRIVES+1];
256int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100257#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700258enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259DisplayType display_type = DT_DEFAULT;
260const char* keyboard_layout = NULL;
261int64_t ticks_per_sec;
262ram_addr_t ram_size;
263int nb_nics;
264NICInfo nd_table[MAX_NICS];
265int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100266int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700267static int rtc_utc = 1;
268static int rtc_date_offset = -1; /* -1 means no change */
269int cirrus_vga_enabled = 1;
270int std_vga_enabled = 0;
271int vmsvga_enabled = 0;
272int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700273QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700274#ifdef TARGET_SPARC
275int graphic_width = 1024;
276int graphic_height = 768;
277int graphic_depth = 8;
278#else
279int graphic_width = 800;
280int graphic_height = 600;
281int graphic_depth = 15;
282#endif
283static int full_screen = 0;
284#ifdef CONFIG_SDL
285static int no_frame = 0;
286#endif
287int no_quit = 0;
288CharDriverState *serial_hds[MAX_SERIAL_PORTS];
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +0100289int serial_hds_count;
290
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700291CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
292CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
293#ifdef TARGET_I386
294int win2k_install_hack = 0;
295int rtc_td_hack = 0;
296#endif
297int usb_enabled = 0;
298int singlestep = 0;
299int smp_cpus = 1;
300const char *vnc_display;
301int acpi_enabled = 1;
302int no_hpet = 0;
303int no_virtio_balloon = 0;
304int fd_bootchk = 1;
305int no_reboot = 0;
306int no_shutdown = 0;
307int cursor_hide = 1;
308int graphic_rotate = 0;
309#ifndef _WIN32
310int daemonize = 0;
311#endif
312WatchdogTimerModel *watchdog = NULL;
313int watchdog_action = WDT_RESET;
314const char *option_rom[MAX_OPTION_ROMS];
315int nb_option_roms;
316int semihosting_enabled = 0;
317#ifdef TARGET_ARM
318int old_param = 0;
319#endif
320const char *qemu_name;
321int alt_grab = 0;
322#if defined(TARGET_SPARC) || defined(TARGET_PPC)
323unsigned int nb_prom_envs = 0;
324const char *prom_envs[MAX_PROM_ENVS];
325#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100326#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700327int nb_drives_opt;
328struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100329#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700330int nb_numa_nodes;
331uint64_t node_mem[MAX_NODES];
332uint64_t node_cpumask[MAX_NODES];
333
334static CPUState *cur_cpu;
335static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700336static QEMUTimer *nographic_timer;
337
338uint8_t qemu_uuid[16];
339
340
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700341int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700342extern char* audio_input_source;
343
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700344extern char* android_op_ports;
345extern char* android_op_port;
346extern char* android_op_report_console;
347extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700348// Path to the file containing specific key character map.
349char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700350
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700351/* Path to hardware initialization file passed with -android-hw option. */
352char* android_op_hwini = NULL;
353
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700354/* Memory checker options. */
355char* android_op_memcheck = NULL;
356
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700357/* -dns-server option value. */
358char* android_op_dns_server = NULL;
359
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700360/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700361char* android_op_radio = NULL;
362
363/* -gps option value. */
364char* android_op_gps = NULL;
365
366/* -audio option value. */
367char* android_op_audio = NULL;
368
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700369/* -cpu-delay option value. */
370char* android_op_cpu_delay = NULL;
371
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700372#ifdef CONFIG_NAND_LIMITS
373/* -nand-limits option value. */
374char* android_op_nand_limits = NULL;
375#endif // CONFIG_NAND_LIMITS
376
377/* -netspeed option value. */
378char* android_op_netspeed = NULL;
379
380/* -netdelay option value. */
381char* android_op_netdelay = NULL;
382
383/* -netfast option value. */
384int android_op_netfast = 0;
385
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700386/* -tcpdump option value. */
387char* android_op_tcpdump = NULL;
388
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700389/* -lcd-density option value. */
390char* android_op_lcd_density = NULL;
391
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700392/* -ui-port option value. This port will be used to report the core
393 * initialization completion.
394 */
395char* android_op_ui_port = NULL;
396
397/* -ui-settings option value. This value will be passed to the UI when new UI
398 * process is attaching to the core.
399 */
400char* android_op_ui_settings = NULL;
401
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800402/* -android-avdname option value. */
403char* android_op_avd_name = "unknown";
404
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700405extern int android_display_width;
406extern int android_display_height;
407extern int android_display_bpp;
408
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700409extern void dprint( const char* format, ... );
410
rich canningsd952f282011-03-01 15:40:09 -0800411const char* dns_log_filename = NULL;
412const char* drop_log_filename = NULL;
413static int rotate_logs_requested = 0;
414
Tim Baverstock24204cc2010-11-25 11:37:43 +0000415const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000416
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700417#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
418
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700419/* Reports the core initialization failure to the error stdout and to the UI
420 * socket before exiting the application.
421 * Parameters that are passed to this macro are used to format the error
422 * mesage using sprintf routine.
423 */
424#ifdef CONFIG_ANDROID
425#define PANIC(...) android_core_init_failure(__VA_ARGS__)
426#else
427#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
428 exit(1); \
429 } while (0)
430#endif // CONFIG_ANDROID
431
432/* Exits the core during initialization. */
433#ifdef CONFIG_ANDROID
434#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
435#else
436#define QEMU_EXIT(exit_code) exit(exit_code)
437#endif // CONFIG_ANDROID
438
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700439/***********************************************************/
440/* x86 ISA bus support */
441
442target_phys_addr_t isa_mem_base = 0;
443PicState2 *isa_pic;
444
445static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
446static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
447
448static uint32_t ioport_read(int index, uint32_t address)
449{
450 static IOPortReadFunc *default_func[3] = {
451 default_ioport_readb,
452 default_ioport_readw,
453 default_ioport_readl
454 };
455 IOPortReadFunc *func = ioport_read_table[index][address];
456 if (!func)
457 func = default_func[index];
458 return func(ioport_opaque[address], address);
459}
460
461static void ioport_write(int index, uint32_t address, uint32_t data)
462{
463 static IOPortWriteFunc *default_func[3] = {
464 default_ioport_writeb,
465 default_ioport_writew,
466 default_ioport_writel
467 };
468 IOPortWriteFunc *func = ioport_write_table[index][address];
469 if (!func)
470 func = default_func[index];
471 func(ioport_opaque[address], address, data);
472}
473
474static uint32_t default_ioport_readb(void *opaque, uint32_t address)
475{
476#ifdef DEBUG_UNUSED_IOPORT
477 fprintf(stderr, "unused inb: port=0x%04x\n", address);
478#endif
479 return 0xff;
480}
481
482static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
483{
484#ifdef DEBUG_UNUSED_IOPORT
485 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
486#endif
487}
488
489/* default is to make two byte accesses */
490static uint32_t default_ioport_readw(void *opaque, uint32_t address)
491{
492 uint32_t data;
493 data = ioport_read(0, address);
494 address = (address + 1) & (MAX_IOPORTS - 1);
495 data |= ioport_read(0, address) << 8;
496 return data;
497}
498
499static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
500{
501 ioport_write(0, address, data & 0xff);
502 address = (address + 1) & (MAX_IOPORTS - 1);
503 ioport_write(0, address, (data >> 8) & 0xff);
504}
505
506static uint32_t default_ioport_readl(void *opaque, uint32_t address)
507{
508#ifdef DEBUG_UNUSED_IOPORT
509 fprintf(stderr, "unused inl: port=0x%04x\n", address);
510#endif
511 return 0xffffffff;
512}
513
514static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
515{
516#ifdef DEBUG_UNUSED_IOPORT
517 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
518#endif
519}
520
rich canningsd952f282011-03-01 15:40:09 -0800521/*
522 * Sets a flag (rotate_logs_requested) to clear both the DNS and the
523 * drop logs upon receiving a SIGUSR1 signal. We need to clear the logs
524 * between the tasks that do not require restarting Qemu.
525 */
526void rotate_qemu_logs_handler(int signum) {
527 rotate_logs_requested = 1;
528}
529
530/*
531 * Resets the rotate_log_requested_flag. Normally called after qemu
532 * logs has been rotated.
533 */
534void reset_rotate_qemu_logs_request(void) {
535 rotate_logs_requested = 0;
536}
537
538/*
539 * Clears the passed qemu log when the rotate_logs_requested
540 * is set. We need to clear the logs between the tasks that do not
541 * require restarting Qemu.
542 */
543FILE* rotate_qemu_log(FILE* old_log_fd, const char* filename) {
544 FILE* new_log_fd = NULL;
545 if (old_log_fd) {
546 if (fclose(old_log_fd) == -1) {
547 fprintf(stderr, "Cannot close old_log fd\n");
548 exit(errno);
549 }
550 }
551
552 if (!filename) {
553 fprintf(stderr, "The log filename to be rotated is not provided");
554 exit(-1);
555 }
556
557 new_log_fd = fopen(filename , "wb+");
558 if (new_log_fd == NULL) {
559 fprintf(stderr, "Cannot open the log file: %s for write.\n",
560 filename);
561 exit(1);
562 }
563
564 return new_log_fd;
565}
566
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700567/***********************************************************/
568void hw_error(const char *fmt, ...)
569{
570 va_list ap;
571 CPUState *env;
572
573 va_start(ap, fmt);
574 fprintf(stderr, "qemu: hardware error: ");
575 vfprintf(stderr, fmt, ap);
576 fprintf(stderr, "\n");
577 for(env = first_cpu; env != NULL; env = env->next_cpu) {
578 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
579#ifdef TARGET_I386
580 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
581#else
582 cpu_dump_state(env, stderr, fprintf, 0);
583#endif
584 }
585 va_end(ap);
586 abort();
587}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200588
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700589/***************/
590/* ballooning */
591
592static QEMUBalloonEvent *qemu_balloon_event;
593void *qemu_balloon_event_opaque;
594
595void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
596{
597 qemu_balloon_event = func;
598 qemu_balloon_event_opaque = opaque;
599}
600
601void qemu_balloon(ram_addr_t target)
602{
603 if (qemu_balloon_event)
604 qemu_balloon_event(qemu_balloon_event_opaque, target);
605}
606
607ram_addr_t qemu_balloon_status(void)
608{
609 if (qemu_balloon_event)
610 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
611 return 0;
612}
613
614/***********************************************************/
David Turner025c32f2010-09-10 14:52:42 +0200615/* real time host monotonic timer */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700616
617/* compute with 96 bit intermediate result: (a*b)/c */
618uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
619{
620 union {
621 uint64_t ll;
622 struct {
David 'Digit' Turner20894ae2010-05-10 17:07:36 -0700623#ifdef HOST_WORDS_BIGENDIAN
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700624 uint32_t high, low;
625#else
626 uint32_t low, high;
627#endif
628 } l;
629 } u, res;
630 uint64_t rl, rh;
631
632 u.ll = a;
633 rl = (uint64_t)u.l.low * (uint64_t)b;
634 rh = (uint64_t)u.l.high * (uint64_t)b;
635 rh += (rl >> 32);
636 res.l.high = rh / c;
637 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
638 return res.ll;
639}
640
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700641/***********************************************************/
642/* host time/date access */
643void qemu_get_timedate(struct tm *tm, int offset)
644{
645 time_t ti;
646 struct tm *ret;
647
648 time(&ti);
649 ti += offset;
650 if (rtc_date_offset == -1) {
651 if (rtc_utc)
652 ret = gmtime(&ti);
653 else
654 ret = localtime(&ti);
655 } else {
656 ti -= rtc_date_offset;
657 ret = gmtime(&ti);
658 }
659
660 memcpy(tm, ret, sizeof(struct tm));
661}
662
663int qemu_timedate_diff(struct tm *tm)
664{
665 time_t seconds;
666
667 if (rtc_date_offset == -1)
668 if (rtc_utc)
669 seconds = mktimegm(tm);
670 else
671 seconds = mktime(tm);
672 else
673 seconds = mktimegm(tm) + rtc_date_offset;
674
675 return seconds - time(NULL);
676}
677
678
679#ifdef CONFIG_TRACE
680static int tbflush_requested;
681static int exit_requested;
682
683void start_tracing()
684{
685 if (trace_filename == NULL)
686 return;
687 if (!tracing) {
688 fprintf(stderr,"-- start tracing --\n");
689 start_time = Now();
690 }
691 tracing = 1;
692 tbflush_requested = 1;
693 qemu_notify_event();
694}
695
696void stop_tracing()
697{
698 if (trace_filename == NULL)
699 return;
700 if (tracing) {
701 end_time = Now();
702 elapsed_usecs += end_time - start_time;
703 fprintf(stderr,"-- stop tracing --\n");
704 }
705 tracing = 0;
706 tbflush_requested = 1;
707 qemu_notify_event();
708}
709
710#ifndef _WIN32
711/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
712 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
713 */
714void sigusr_handler(int sig)
715{
716 if (sig == SIGUSR1)
717 start_tracing();
718 else
719 stop_tracing();
720}
721#endif
722
723/* This is the handler to catch control-C so that we can exit cleanly.
724 * This is needed when tracing to flush the buffers to disk.
725 */
726void sigint_handler(int sig)
727{
728 exit_requested = 1;
729 qemu_notify_event();
730}
731#endif /* CONFIG_TRACE */
732
733
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700734/***********************************************************/
735/* Bluetooth support */
736static int nb_hcis;
737static int cur_hci;
738static struct HCIInfo *hci_table[MAX_NICS];
739
740static struct bt_vlan_s {
741 struct bt_scatternet_s net;
742 int id;
743 struct bt_vlan_s *next;
744} *first_bt_vlan;
745
746/* find or alloc a new bluetooth "VLAN" */
747static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
748{
749 struct bt_vlan_s **pvlan, *vlan;
750 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
751 if (vlan->id == id)
752 return &vlan->net;
753 }
754 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
755 vlan->id = id;
756 pvlan = &first_bt_vlan;
757 while (*pvlan != NULL)
758 pvlan = &(*pvlan)->next;
759 *pvlan = vlan;
760 return &vlan->net;
761}
762
763static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
764{
765}
766
767static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
768{
769 return -ENOTSUP;
770}
771
772static struct HCIInfo null_hci = {
773 .cmd_send = null_hci_send,
774 .sco_send = null_hci_send,
775 .acl_send = null_hci_send,
776 .bdaddr_set = null_hci_addr_set,
777};
778
779struct HCIInfo *qemu_next_hci(void)
780{
781 if (cur_hci == nb_hcis)
782 return &null_hci;
783
784 return hci_table[cur_hci++];
785}
786
787static struct HCIInfo *hci_init(const char *str)
788{
789 char *endp;
790 struct bt_scatternet_s *vlan = 0;
791
792 if (!strcmp(str, "null"))
793 /* null */
794 return &null_hci;
795 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
796 /* host[:hciN] */
797 return bt_host_hci(str[4] ? str + 5 : "hci0");
798 else if (!strncmp(str, "hci", 3)) {
799 /* hci[,vlan=n] */
800 if (str[3]) {
801 if (!strncmp(str + 3, ",vlan=", 6)) {
802 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
803 if (*endp)
804 vlan = 0;
805 }
806 } else
807 vlan = qemu_find_bt_vlan(0);
808 if (vlan)
809 return bt_new_hci(vlan);
810 }
811
812 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
813
814 return 0;
815}
816
817static int bt_hci_parse(const char *str)
818{
819 struct HCIInfo *hci;
820 bdaddr_t bdaddr;
821
822 if (nb_hcis >= MAX_NICS) {
823 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
824 return -1;
825 }
826
827 hci = hci_init(str);
828 if (!hci)
829 return -1;
830
831 bdaddr.b[0] = 0x52;
832 bdaddr.b[1] = 0x54;
833 bdaddr.b[2] = 0x00;
834 bdaddr.b[3] = 0x12;
835 bdaddr.b[4] = 0x34;
836 bdaddr.b[5] = 0x56 + nb_hcis;
837 hci->bdaddr_set(hci, bdaddr.b);
838
839 hci_table[nb_hcis++] = hci;
840
841 return 0;
842}
843
844static void bt_vhci_add(int vlan_id)
845{
846 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
847
848 if (!vlan->slave)
849 fprintf(stderr, "qemu: warning: adding a VHCI to "
850 "an empty scatternet %i\n", vlan_id);
851
852 bt_vhci_init(bt_new_hci(vlan));
853}
854
855static struct bt_device_s *bt_device_add(const char *opt)
856{
857 struct bt_scatternet_s *vlan;
858 int vlan_id = 0;
859 char *endp = strstr(opt, ",vlan=");
860 int len = (endp ? endp - opt : strlen(opt)) + 1;
861 char devname[10];
862
863 pstrcpy(devname, MIN(sizeof(devname), len), opt);
864
865 if (endp) {
866 vlan_id = strtol(endp + 6, &endp, 0);
867 if (*endp) {
868 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
869 return 0;
870 }
871 }
872
873 vlan = qemu_find_bt_vlan(vlan_id);
874
875 if (!vlan->slave)
876 fprintf(stderr, "qemu: warning: adding a slave device to "
877 "an empty scatternet %i\n", vlan_id);
878
879 if (!strcmp(devname, "keyboard"))
880 return bt_keyboard_init(vlan);
881
882 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
883 return 0;
884}
885
886static int bt_parse(const char *opt)
887{
888 const char *endp, *p;
889 int vlan;
890
891 if (strstart(opt, "hci", &endp)) {
892 if (!*endp || *endp == ',') {
893 if (*endp)
894 if (!strstart(endp, ",vlan=", 0))
895 opt = endp + 1;
896
897 return bt_hci_parse(opt);
898 }
899 } else if (strstart(opt, "vhci", &endp)) {
900 if (!*endp || *endp == ',') {
901 if (*endp) {
902 if (strstart(endp, ",vlan=", &p)) {
903 vlan = strtol(p, (char **) &endp, 0);
904 if (*endp) {
905 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
906 return 1;
907 }
908 } else {
909 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
910 return 1;
911 }
912 } else
913 vlan = 0;
914
915 bt_vhci_add(vlan);
916 return 0;
917 }
918 } else if (strstart(opt, "device:", &endp))
919 return !bt_device_add(endp);
920
921 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
922 return 1;
923}
924
925/***********************************************************/
926/* QEMU Block devices */
927
928#define HD_ALIAS "index=%d,media=disk"
929#define CDROM_ALIAS "index=2,media=cdrom"
930#define FD_ALIAS "index=%d,if=floppy"
931#define PFLASH_ALIAS "if=pflash"
932#define MTD_ALIAS "if=mtd"
933#define SD_ALIAS "index=0,if=sd"
934
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100935static int drive_init_func(QemuOpts *opts, void *opaque)
936{
937 int *use_scsi = opaque;
938 int fatal_error = 0;
939
940 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
941 if (fatal_error)
942 return 1;
943 }
944 return 0;
945}
946
947static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
948{
949 if (NULL == qemu_opt_get(opts, "snapshot")) {
950 qemu_opt_set(opts, "snapshot", "on");
951 }
952 return 0;
953}
954
955#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700956static int drive_opt_get_free_idx(void)
957{
958 int index;
959
960 for (index = 0; index < MAX_DRIVES; index++)
961 if (!drives_opt[index].used) {
962 drives_opt[index].used = 1;
963 return index;
964 }
965
966 return -1;
967}
968
969static int drive_get_free_idx(void)
970{
971 int index;
972
973 for (index = 0; index < MAX_DRIVES; index++)
974 if (!drives_table[index].used) {
975 drives_table[index].used = 1;
976 return index;
977 }
978
979 return -1;
980}
981
982int drive_add(const char *file, const char *fmt, ...)
983{
984 va_list ap;
985 int index = drive_opt_get_free_idx();
986
987 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
988 fprintf(stderr, "qemu: too many drives\n");
989 return -1;
990 }
991
992 drives_opt[index].file = file;
993 va_start(ap, fmt);
994 vsnprintf(drives_opt[index].opt,
995 sizeof(drives_opt[0].opt), fmt, ap);
996 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700997
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700998 nb_drives_opt++;
999 return index;
1000}
1001
1002void drive_remove(int index)
1003{
1004 drives_opt[index].used = 0;
1005 nb_drives_opt--;
1006}
1007
1008int drive_get_index(BlockInterfaceType type, int bus, int unit)
1009{
1010 int index;
1011
1012 /* seek interface, bus and unit */
1013
1014 for (index = 0; index < MAX_DRIVES; index++)
1015 if (drives_table[index].type == type &&
1016 drives_table[index].bus == bus &&
1017 drives_table[index].unit == unit &&
1018 drives_table[index].used)
1019 return index;
1020
1021 return -1;
1022}
1023
1024int drive_get_max_bus(BlockInterfaceType type)
1025{
1026 int max_bus;
1027 int index;
1028
1029 max_bus = -1;
1030 for (index = 0; index < nb_drives; index++) {
1031 if(drives_table[index].type == type &&
1032 drives_table[index].bus > max_bus)
1033 max_bus = drives_table[index].bus;
1034 }
1035 return max_bus;
1036}
1037
1038const char *drive_get_serial(BlockDriverState *bdrv)
1039{
1040 int index;
1041
1042 for (index = 0; index < nb_drives; index++)
1043 if (drives_table[index].bdrv == bdrv)
1044 return drives_table[index].serial;
1045
1046 return "\0";
1047}
1048
1049BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1050{
1051 int index;
1052
1053 for (index = 0; index < nb_drives; index++)
1054 if (drives_table[index].bdrv == bdrv)
1055 return drives_table[index].onerror;
1056
1057 return BLOCK_ERR_STOP_ENOSPC;
1058}
1059
1060static void bdrv_format_print(void *opaque, const char *name)
1061{
1062 fprintf(stderr, " %s", name);
1063}
1064
1065void drive_uninit(BlockDriverState *bdrv)
1066{
1067 int i;
1068
1069 for (i = 0; i < MAX_DRIVES; i++)
1070 if (drives_table[i].bdrv == bdrv) {
1071 drives_table[i].bdrv = NULL;
1072 drives_table[i].used = 0;
1073 drive_remove(drives_table[i].drive_opt_idx);
1074 nb_drives--;
1075 break;
1076 }
1077}
1078
1079int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1080{
1081 char buf[128];
1082 char file[1024];
1083 char devname[128];
1084 char serial[21];
1085 const char *mediastr = "";
1086 BlockInterfaceType type;
1087 enum { MEDIA_DISK, MEDIA_CDROM } media;
1088 int bus_id, unit_id;
1089 int cyls, heads, secs, translation;
1090 BlockDriverState *bdrv;
1091 BlockDriver *drv = NULL;
1092 QEMUMachine *machine = opaque;
1093 int max_devs;
1094 int index;
1095 int cache;
1096 int bdrv_flags, onerror;
1097 int drives_table_idx;
1098 char *str = arg->opt;
1099 static const char * const params[] = { "bus", "unit", "if", "index",
1100 "cyls", "heads", "secs", "trans",
1101 "media", "snapshot", "file",
1102 "cache", "format", "serial", "werror",
1103 NULL };
1104
1105 if (check_params(buf, sizeof(buf), params, str) < 0) {
1106 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1107 buf, str);
1108 return -1;
1109 }
1110
1111 file[0] = 0;
1112 cyls = heads = secs = 0;
1113 bus_id = 0;
1114 unit_id = -1;
1115 translation = BIOS_ATA_TRANSLATION_AUTO;
1116 index = -1;
1117 cache = 3;
1118
1119 if (machine->use_scsi) {
1120 type = IF_SCSI;
1121 max_devs = MAX_SCSI_DEVS;
1122 pstrcpy(devname, sizeof(devname), "scsi");
1123 } else {
1124 type = IF_IDE;
1125 max_devs = MAX_IDE_DEVS;
1126 pstrcpy(devname, sizeof(devname), "ide");
1127 }
1128 media = MEDIA_DISK;
1129
1130 /* extract parameters */
1131
1132 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1133 bus_id = strtol(buf, NULL, 0);
1134 if (bus_id < 0) {
1135 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1136 return -1;
1137 }
1138 }
1139
1140 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1141 unit_id = strtol(buf, NULL, 0);
1142 if (unit_id < 0) {
1143 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1144 return -1;
1145 }
1146 }
1147
1148 if (get_param_value(buf, sizeof(buf), "if", str)) {
1149 pstrcpy(devname, sizeof(devname), buf);
1150 if (!strcmp(buf, "ide")) {
1151 type = IF_IDE;
1152 max_devs = MAX_IDE_DEVS;
1153 } else if (!strcmp(buf, "scsi")) {
1154 type = IF_SCSI;
1155 max_devs = MAX_SCSI_DEVS;
1156 } else if (!strcmp(buf, "floppy")) {
1157 type = IF_FLOPPY;
1158 max_devs = 0;
1159 } else if (!strcmp(buf, "pflash")) {
1160 type = IF_PFLASH;
1161 max_devs = 0;
1162 } else if (!strcmp(buf, "mtd")) {
1163 type = IF_MTD;
1164 max_devs = 0;
1165 } else if (!strcmp(buf, "sd")) {
1166 type = IF_SD;
1167 max_devs = 0;
1168 } else if (!strcmp(buf, "virtio")) {
1169 type = IF_VIRTIO;
1170 max_devs = 0;
1171 } else if (!strcmp(buf, "xen")) {
1172 type = IF_XEN;
1173 max_devs = 0;
1174 } else {
1175 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1176 return -1;
1177 }
1178 }
1179
1180 if (get_param_value(buf, sizeof(buf), "index", str)) {
1181 index = strtol(buf, NULL, 0);
1182 if (index < 0) {
1183 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1184 return -1;
1185 }
1186 }
1187
1188 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1189 cyls = strtol(buf, NULL, 0);
1190 }
1191
1192 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1193 heads = strtol(buf, NULL, 0);
1194 }
1195
1196 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1197 secs = strtol(buf, NULL, 0);
1198 }
1199
1200 if (cyls || heads || secs) {
1201 if (cyls < 1 || cyls > 16383) {
1202 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1203 return -1;
1204 }
1205 if (heads < 1 || heads > 16) {
1206 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1207 return -1;
1208 }
1209 if (secs < 1 || secs > 63) {
1210 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1211 return -1;
1212 }
1213 }
1214
1215 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1216 if (!cyls) {
1217 fprintf(stderr,
1218 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1219 str);
1220 return -1;
1221 }
1222 if (!strcmp(buf, "none"))
1223 translation = BIOS_ATA_TRANSLATION_NONE;
1224 else if (!strcmp(buf, "lba"))
1225 translation = BIOS_ATA_TRANSLATION_LBA;
1226 else if (!strcmp(buf, "auto"))
1227 translation = BIOS_ATA_TRANSLATION_AUTO;
1228 else {
1229 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1230 return -1;
1231 }
1232 }
1233
1234 if (get_param_value(buf, sizeof(buf), "media", str)) {
1235 if (!strcmp(buf, "disk")) {
1236 media = MEDIA_DISK;
1237 } else if (!strcmp(buf, "cdrom")) {
1238 if (cyls || secs || heads) {
1239 fprintf(stderr,
1240 "qemu: '%s' invalid physical CHS format\n", str);
1241 return -1;
1242 }
1243 media = MEDIA_CDROM;
1244 } else {
1245 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1246 return -1;
1247 }
1248 }
1249
1250 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1251 if (!strcmp(buf, "on"))
1252 snapshot = 1;
1253 else if (!strcmp(buf, "off"))
1254 snapshot = 0;
1255 else {
1256 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1257 return -1;
1258 }
1259 }
1260
1261 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1262 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1263 cache = 0;
1264 else if (!strcmp(buf, "writethrough"))
1265 cache = 1;
1266 else if (!strcmp(buf, "writeback"))
1267 cache = 2;
1268 else {
1269 fprintf(stderr, "qemu: invalid cache option\n");
1270 return -1;
1271 }
1272 }
1273
1274 if (get_param_value(buf, sizeof(buf), "format", str)) {
1275 if (strcmp(buf, "?") == 0) {
1276 fprintf(stderr, "qemu: Supported formats:");
1277 bdrv_iterate_format(bdrv_format_print, NULL);
1278 fprintf(stderr, "\n");
1279 return -1;
1280 }
1281 drv = bdrv_find_format(buf);
1282 if (!drv) {
1283 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1284 return -1;
1285 }
1286 }
1287
1288 if (arg->file == NULL)
1289 get_param_value(file, sizeof(file), "file", str);
1290 else
1291 pstrcpy(file, sizeof(file), arg->file);
1292
1293 if (!get_param_value(serial, sizeof(serial), "serial", str))
1294 memset(serial, 0, sizeof(serial));
1295
1296 onerror = BLOCK_ERR_STOP_ENOSPC;
1297 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1298 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1299 fprintf(stderr, "werror is no supported by this format\n");
1300 return -1;
1301 }
1302 if (!strcmp(buf, "ignore"))
1303 onerror = BLOCK_ERR_IGNORE;
1304 else if (!strcmp(buf, "enospc"))
1305 onerror = BLOCK_ERR_STOP_ENOSPC;
1306 else if (!strcmp(buf, "stop"))
1307 onerror = BLOCK_ERR_STOP_ANY;
1308 else if (!strcmp(buf, "report"))
1309 onerror = BLOCK_ERR_REPORT;
1310 else {
1311 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1312 return -1;
1313 }
1314 }
1315
1316 /* compute bus and unit according index */
1317
1318 if (index != -1) {
1319 if (bus_id != 0 || unit_id != -1) {
1320 fprintf(stderr,
1321 "qemu: '%s' index cannot be used with bus and unit\n", str);
1322 return -1;
1323 }
1324 if (max_devs == 0)
1325 {
1326 unit_id = index;
1327 bus_id = 0;
1328 } else {
1329 unit_id = index % max_devs;
1330 bus_id = index / max_devs;
1331 }
1332 }
1333
1334 /* if user doesn't specify a unit_id,
1335 * try to find the first free
1336 */
1337
1338 if (unit_id == -1) {
1339 unit_id = 0;
1340 while (drive_get_index(type, bus_id, unit_id) != -1) {
1341 unit_id++;
1342 if (max_devs && unit_id >= max_devs) {
1343 unit_id -= max_devs;
1344 bus_id++;
1345 }
1346 }
1347 }
1348
1349 /* check unit id */
1350
1351 if (max_devs && unit_id >= max_devs) {
1352 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1353 str, unit_id, max_devs - 1);
1354 return -1;
1355 }
1356
1357 /*
1358 * ignore multiple definitions
1359 */
1360
1361 if (drive_get_index(type, bus_id, unit_id) != -1)
1362 return -2;
1363
1364 /* init */
1365
1366 if (type == IF_IDE || type == IF_SCSI)
1367 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1368 if (max_devs)
1369 snprintf(buf, sizeof(buf), "%s%i%s%i",
1370 devname, bus_id, mediastr, unit_id);
1371 else
1372 snprintf(buf, sizeof(buf), "%s%s%i",
1373 devname, mediastr, unit_id);
1374 bdrv = bdrv_new(buf);
1375 drives_table_idx = drive_get_free_idx();
1376 drives_table[drives_table_idx].bdrv = bdrv;
1377 drives_table[drives_table_idx].type = type;
1378 drives_table[drives_table_idx].bus = bus_id;
1379 drives_table[drives_table_idx].unit = unit_id;
1380 drives_table[drives_table_idx].onerror = onerror;
1381 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1382 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1383 nb_drives++;
1384
1385 switch(type) {
1386 case IF_IDE:
1387 case IF_SCSI:
1388 case IF_XEN:
1389 switch(media) {
1390 case MEDIA_DISK:
1391 if (cyls != 0) {
1392 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1393 bdrv_set_translation_hint(bdrv, translation);
1394 }
1395 break;
1396 case MEDIA_CDROM:
1397 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1398 break;
1399 }
1400 break;
1401 case IF_SD:
1402 /* FIXME: This isn't really a floppy, but it's a reasonable
1403 approximation. */
1404 case IF_FLOPPY:
1405 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1406 break;
1407 case IF_PFLASH:
1408 case IF_MTD:
1409 case IF_VIRTIO:
1410 break;
1411 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001412 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001413 abort();
1414 }
1415 if (!file[0])
1416 return -2;
1417 bdrv_flags = 0;
1418 if (snapshot) {
1419 bdrv_flags |= BDRV_O_SNAPSHOT;
1420 cache = 2; /* always use write-back with snapshot */
1421 }
1422 if (cache == 0) /* no caching */
1423 bdrv_flags |= BDRV_O_NOCACHE;
1424 else if (cache == 2) /* write-back */
1425 bdrv_flags |= BDRV_O_CACHE_WB;
1426 else if (cache == 3) /* not specified */
1427 bdrv_flags |= BDRV_O_CACHE_DEF;
1428 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1429 fprintf(stderr, "qemu: could not open disk image %s\n",
1430 file);
1431 return -1;
1432 }
1433 if (bdrv_key_required(bdrv))
1434 autostart = 0;
1435 return drives_table_idx;
1436}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001437#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001438
1439static void numa_add(const char *optarg)
1440{
1441 char option[128];
1442 char *endptr;
1443 unsigned long long value, endvalue;
1444 int nodenr;
1445
1446 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1447 if (!strcmp(option, "node")) {
1448 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1449 nodenr = nb_numa_nodes;
1450 } else {
1451 nodenr = strtoull(option, NULL, 10);
1452 }
1453
1454 if (get_param_value(option, 128, "mem", optarg) == 0) {
1455 node_mem[nodenr] = 0;
1456 } else {
1457 value = strtoull(option, &endptr, 0);
1458 switch (*endptr) {
1459 case 0: case 'M': case 'm':
1460 value <<= 20;
1461 break;
1462 case 'G': case 'g':
1463 value <<= 30;
1464 break;
1465 }
1466 node_mem[nodenr] = value;
1467 }
1468 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1469 node_cpumask[nodenr] = 0;
1470 } else {
1471 value = strtoull(option, &endptr, 10);
1472 if (value >= 64) {
1473 value = 63;
1474 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1475 } else {
1476 if (*endptr == '-') {
1477 endvalue = strtoull(endptr+1, &endptr, 10);
1478 if (endvalue >= 63) {
1479 endvalue = 62;
1480 fprintf(stderr,
1481 "only 63 CPUs in NUMA mode supported.\n");
1482 }
1483 value = (1 << (endvalue + 1)) - (1 << value);
1484 } else {
1485 value = 1 << value;
1486 }
1487 }
1488 node_cpumask[nodenr] = value;
1489 }
1490 nb_numa_nodes++;
1491 }
1492 return;
1493}
1494
1495/***********************************************************/
1496/* USB devices */
1497
1498static USBPort *used_usb_ports;
1499static USBPort *free_usb_ports;
1500
1501/* ??? Maybe change this to register a hub to keep track of the topology. */
1502void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1503 usb_attachfn attach)
1504{
1505 port->opaque = opaque;
1506 port->index = index;
1507 port->attach = attach;
1508 port->next = free_usb_ports;
1509 free_usb_ports = port;
1510}
1511
1512int usb_device_add_dev(USBDevice *dev)
1513{
1514 USBPort *port;
1515
1516 /* Find a USB port to add the device to. */
1517 port = free_usb_ports;
1518 if (!port->next) {
1519 USBDevice *hub;
1520
1521 /* Create a new hub and chain it on. */
1522 free_usb_ports = NULL;
1523 port->next = used_usb_ports;
1524 used_usb_ports = port;
1525
1526 hub = usb_hub_init(VM_USB_HUB_SIZE);
1527 usb_attach(port, hub);
1528 port = free_usb_ports;
1529 }
1530
1531 free_usb_ports = port->next;
1532 port->next = used_usb_ports;
1533 used_usb_ports = port;
1534 usb_attach(port, dev);
1535 return 0;
1536}
1537
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001538#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001539static void usb_msd_password_cb(void *opaque, int err)
1540{
1541 USBDevice *dev = opaque;
1542
1543 if (!err)
1544 usb_device_add_dev(dev);
1545 else
1546 dev->handle_destroy(dev);
1547}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001548#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001549
1550static int usb_device_add(const char *devname, int is_hotplug)
1551{
1552 const char *p;
1553 USBDevice *dev;
1554
1555 if (!free_usb_ports)
1556 return -1;
1557
1558 if (strstart(devname, "host:", &p)) {
1559 dev = usb_host_device_open(p);
1560 } else if (!strcmp(devname, "mouse")) {
1561 dev = usb_mouse_init();
1562 } else if (!strcmp(devname, "tablet")) {
1563 dev = usb_tablet_init();
1564 } else if (!strcmp(devname, "keyboard")) {
1565 dev = usb_keyboard_init();
1566 } else if (strstart(devname, "disk:", &p)) {
1567#if 0
1568 BlockDriverState *bs;
1569#endif
1570 dev = usb_msd_init(p);
1571 if (!dev)
1572 return -1;
1573#if 0
1574 bs = usb_msd_get_bdrv(dev);
1575 if (bdrv_key_required(bs)) {
1576 autostart = 0;
1577 if (is_hotplug) {
1578 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1579 dev);
1580 return 0;
1581 }
1582 }
1583 } else if (!strcmp(devname, "wacom-tablet")) {
1584 dev = usb_wacom_init();
1585 } else if (strstart(devname, "serial:", &p)) {
1586 dev = usb_serial_init(p);
1587#ifdef CONFIG_BRLAPI
1588 } else if (!strcmp(devname, "braille")) {
1589 dev = usb_baum_init();
1590#endif
1591 } else if (strstart(devname, "net:", &p)) {
1592 int nic = nb_nics;
1593
1594 if (net_client_init("nic", p) < 0)
1595 return -1;
1596 nd_table[nic].model = "usb";
1597 dev = usb_net_init(&nd_table[nic]);
1598 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1599 dev = usb_bt_init(devname[2] ? hci_init(p) :
1600 bt_new_hci(qemu_find_bt_vlan(0)));
1601#endif
1602 } else {
1603 return -1;
1604 }
1605 if (!dev)
1606 return -1;
1607
1608 return usb_device_add_dev(dev);
1609}
1610
1611int usb_device_del_addr(int bus_num, int addr)
1612{
1613 USBPort *port;
1614 USBPort **lastp;
1615 USBDevice *dev;
1616
1617 if (!used_usb_ports)
1618 return -1;
1619
1620 if (bus_num != 0)
1621 return -1;
1622
1623 lastp = &used_usb_ports;
1624 port = used_usb_ports;
1625 while (port && port->dev->addr != addr) {
1626 lastp = &port->next;
1627 port = port->next;
1628 }
1629
1630 if (!port)
1631 return -1;
1632
1633 dev = port->dev;
1634 *lastp = port->next;
1635 usb_attach(port, NULL);
1636 dev->handle_destroy(dev);
1637 port->next = free_usb_ports;
1638 free_usb_ports = port;
1639 return 0;
1640}
1641
1642static int usb_device_del(const char *devname)
1643{
1644 int bus_num, addr;
1645 const char *p;
1646
1647 if (strstart(devname, "host:", &p))
1648 return usb_host_device_close(p);
1649
1650 if (!used_usb_ports)
1651 return -1;
1652
1653 p = strchr(devname, '.');
1654 if (!p)
1655 return -1;
1656 bus_num = strtoul(devname, NULL, 0);
1657 addr = strtoul(p + 1, NULL, 0);
1658
1659 return usb_device_del_addr(bus_num, addr);
1660}
1661
1662void do_usb_add(Monitor *mon, const char *devname)
1663{
1664 usb_device_add(devname, 1);
1665}
1666
1667void do_usb_del(Monitor *mon, const char *devname)
1668{
1669 usb_device_del(devname);
1670}
1671
1672void usb_info(Monitor *mon)
1673{
1674 USBDevice *dev;
1675 USBPort *port;
1676 const char *speed_str;
1677
1678 if (!usb_enabled) {
1679 monitor_printf(mon, "USB support not enabled\n");
1680 return;
1681 }
1682
1683 for (port = used_usb_ports; port; port = port->next) {
1684 dev = port->dev;
1685 if (!dev)
1686 continue;
1687 switch(dev->speed) {
1688 case USB_SPEED_LOW:
1689 speed_str = "1.5";
1690 break;
1691 case USB_SPEED_FULL:
1692 speed_str = "12";
1693 break;
1694 case USB_SPEED_HIGH:
1695 speed_str = "480";
1696 break;
1697 default:
1698 speed_str = "?";
1699 break;
1700 }
1701 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1702 0, dev->addr, speed_str, dev->devname);
1703 }
1704}
1705
1706/***********************************************************/
1707/* PCMCIA/Cardbus */
1708
1709static struct pcmcia_socket_entry_s {
1710 PCMCIASocket *socket;
1711 struct pcmcia_socket_entry_s *next;
1712} *pcmcia_sockets = 0;
1713
1714void pcmcia_socket_register(PCMCIASocket *socket)
1715{
1716 struct pcmcia_socket_entry_s *entry;
1717
1718 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1719 entry->socket = socket;
1720 entry->next = pcmcia_sockets;
1721 pcmcia_sockets = entry;
1722}
1723
1724void pcmcia_socket_unregister(PCMCIASocket *socket)
1725{
1726 struct pcmcia_socket_entry_s *entry, **ptr;
1727
1728 ptr = &pcmcia_sockets;
1729 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1730 if (entry->socket == socket) {
1731 *ptr = entry->next;
1732 qemu_free(entry);
1733 }
1734}
1735
1736void pcmcia_info(Monitor *mon)
1737{
1738 struct pcmcia_socket_entry_s *iter;
1739
1740 if (!pcmcia_sockets)
1741 monitor_printf(mon, "No PCMCIA sockets\n");
1742
1743 for (iter = pcmcia_sockets; iter; iter = iter->next)
1744 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1745 iter->socket->attached ? iter->socket->card_string :
1746 "Empty");
1747}
1748
1749/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001750/* I/O handling */
1751
1752typedef struct IOHandlerRecord {
1753 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001754 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001755 IOHandler *fd_read;
1756 IOHandler *fd_write;
1757 int deleted;
1758 void *opaque;
1759 /* temporary data */
1760 struct pollfd *ufd;
1761 struct IOHandlerRecord *next;
1762} IOHandlerRecord;
1763
1764static IOHandlerRecord *first_io_handler;
1765
1766/* XXX: fd_read_poll should be suppressed, but an API change is
1767 necessary in the character devices to suppress fd_can_read(). */
1768int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001769 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001770 IOHandler *fd_read,
1771 IOHandler *fd_write,
1772 void *opaque)
1773{
1774 IOHandlerRecord **pioh, *ioh;
1775
1776 if (!fd_read && !fd_write) {
1777 pioh = &first_io_handler;
1778 for(;;) {
1779 ioh = *pioh;
1780 if (ioh == NULL)
1781 break;
1782 if (ioh->fd == fd) {
1783 ioh->deleted = 1;
1784 break;
1785 }
1786 pioh = &ioh->next;
1787 }
1788 } else {
1789 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1790 if (ioh->fd == fd)
1791 goto found;
1792 }
1793 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1794 ioh->next = first_io_handler;
1795 first_io_handler = ioh;
1796 found:
1797 ioh->fd = fd;
1798 ioh->fd_read_poll = fd_read_poll;
1799 ioh->fd_read = fd_read;
1800 ioh->fd_write = fd_write;
1801 ioh->opaque = opaque;
1802 ioh->deleted = 0;
1803 }
1804 return 0;
1805}
1806
1807int qemu_set_fd_handler(int fd,
1808 IOHandler *fd_read,
1809 IOHandler *fd_write,
1810 void *opaque)
1811{
1812 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1813}
1814
1815#ifdef _WIN32
1816/***********************************************************/
1817/* Polling handling */
1818
1819typedef struct PollingEntry {
1820 PollingFunc *func;
1821 void *opaque;
1822 struct PollingEntry *next;
1823} PollingEntry;
1824
1825static PollingEntry *first_polling_entry;
1826
1827int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1828{
1829 PollingEntry **ppe, *pe;
1830 pe = qemu_mallocz(sizeof(PollingEntry));
1831 pe->func = func;
1832 pe->opaque = opaque;
1833 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1834 *ppe = pe;
1835 return 0;
1836}
1837
1838void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1839{
1840 PollingEntry **ppe, *pe;
1841 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1842 pe = *ppe;
1843 if (pe->func == func && pe->opaque == opaque) {
1844 *ppe = pe->next;
1845 qemu_free(pe);
1846 break;
1847 }
1848 }
1849}
1850
1851/***********************************************************/
1852/* Wait objects support */
1853typedef struct WaitObjects {
1854 int num;
1855 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1856 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1857 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1858} WaitObjects;
1859
1860static WaitObjects wait_objects = {0};
1861
1862int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1863{
1864 WaitObjects *w = &wait_objects;
1865
1866 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1867 return -1;
1868 w->events[w->num] = handle;
1869 w->func[w->num] = func;
1870 w->opaque[w->num] = opaque;
1871 w->num++;
1872 return 0;
1873}
1874
1875void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1876{
1877 int i, found;
1878 WaitObjects *w = &wait_objects;
1879
1880 found = 0;
1881 for (i = 0; i < w->num; i++) {
1882 if (w->events[i] == handle)
1883 found = 1;
1884 if (found) {
1885 w->events[i] = w->events[i + 1];
1886 w->func[i] = w->func[i + 1];
1887 w->opaque[i] = w->opaque[i + 1];
1888 }
1889 }
1890 if (found)
1891 w->num--;
1892}
1893#endif
1894
1895/***********************************************************/
1896/* ram save/restore */
1897
1898static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1899{
1900 int v;
1901
1902 v = qemu_get_byte(f);
1903 switch(v) {
1904 case 0:
1905 if (qemu_get_buffer(f, buf, len) != len)
1906 return -EIO;
1907 break;
1908 case 1:
1909 v = qemu_get_byte(f);
1910 memset(buf, v, len);
1911 break;
1912 default:
1913 return -EINVAL;
1914 }
1915
1916 if (qemu_file_has_error(f))
1917 return -EIO;
1918
1919 return 0;
1920}
1921
1922static int ram_load_v1(QEMUFile *f, void *opaque)
1923{
1924 int ret;
1925 ram_addr_t i;
1926
1927 if (qemu_get_be32(f) != last_ram_offset)
1928 return -EINVAL;
1929 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1930 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1931 if (ret)
1932 return ret;
1933 }
1934 return 0;
1935}
1936
1937#define BDRV_HASH_BLOCK_SIZE 1024
1938#define IOBUF_SIZE 4096
1939#define RAM_CBLOCK_MAGIC 0xfabe
1940
1941typedef struct RamDecompressState {
1942 z_stream zstream;
1943 QEMUFile *f;
1944 uint8_t buf[IOBUF_SIZE];
1945} RamDecompressState;
1946
1947static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1948{
1949 int ret;
1950 memset(s, 0, sizeof(*s));
1951 s->f = f;
1952 ret = inflateInit(&s->zstream);
1953 if (ret != Z_OK)
1954 return -1;
1955 return 0;
1956}
1957
1958static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1959{
1960 int ret, clen;
1961
1962 s->zstream.avail_out = len;
1963 s->zstream.next_out = buf;
1964 while (s->zstream.avail_out > 0) {
1965 if (s->zstream.avail_in == 0) {
1966 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1967 return -1;
1968 clen = qemu_get_be16(s->f);
1969 if (clen > IOBUF_SIZE)
1970 return -1;
1971 qemu_get_buffer(s->f, s->buf, clen);
1972 s->zstream.avail_in = clen;
1973 s->zstream.next_in = s->buf;
1974 }
1975 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1976 if (ret != Z_OK && ret != Z_STREAM_END) {
1977 return -1;
1978 }
1979 }
1980 return 0;
1981}
1982
1983static void ram_decompress_close(RamDecompressState *s)
1984{
1985 inflateEnd(&s->zstream);
1986}
1987
1988#define RAM_SAVE_FLAG_FULL 0x01
1989#define RAM_SAVE_FLAG_COMPRESS 0x02
1990#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1991#define RAM_SAVE_FLAG_PAGE 0x08
1992#define RAM_SAVE_FLAG_EOS 0x10
1993
1994static int is_dup_page(uint8_t *page, uint8_t ch)
1995{
1996 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1997 uint32_t *array = (uint32_t *)page;
1998 int i;
1999
2000 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2001 if (array[i] != val)
2002 return 0;
2003 }
2004
2005 return 1;
2006}
2007
2008static int ram_save_block(QEMUFile *f)
2009{
2010 static ram_addr_t current_addr = 0;
2011 ram_addr_t saved_addr = current_addr;
2012 ram_addr_t addr = 0;
2013 int found = 0;
2014
2015 while (addr < last_ram_offset) {
2016 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
2017 uint8_t *p;
2018
2019 cpu_physical_memory_reset_dirty(current_addr,
2020 current_addr + TARGET_PAGE_SIZE,
2021 MIGRATION_DIRTY_FLAG);
2022
2023 p = qemu_get_ram_ptr(current_addr);
2024
2025 if (is_dup_page(p, *p)) {
2026 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
2027 qemu_put_byte(f, *p);
2028 } else {
2029 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
2030 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
2031 }
2032
2033 found = 1;
2034 break;
2035 }
2036 addr += TARGET_PAGE_SIZE;
2037 current_addr = (saved_addr + addr) % last_ram_offset;
2038 }
2039
2040 return found;
2041}
2042
2043static uint64_t bytes_transferred = 0;
2044
2045static ram_addr_t ram_save_remaining(void)
2046{
2047 ram_addr_t addr;
2048 ram_addr_t count = 0;
2049
2050 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2051 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2052 count++;
2053 }
2054
2055 return count;
2056}
2057
2058uint64_t ram_bytes_remaining(void)
2059{
2060 return ram_save_remaining() * TARGET_PAGE_SIZE;
2061}
2062
2063uint64_t ram_bytes_transferred(void)
2064{
2065 return bytes_transferred;
2066}
2067
2068uint64_t ram_bytes_total(void)
2069{
2070 return last_ram_offset;
2071}
2072
2073static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2074{
2075 ram_addr_t addr;
2076 uint64_t bytes_transferred_last;
2077 double bwidth = 0;
2078 uint64_t expected_time = 0;
2079
2080 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2081
2082 if (stage == 1) {
2083 /* Make sure all dirty bits are set */
2084 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2085 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2086 cpu_physical_memory_set_dirty(addr);
2087 }
2088
2089 /* Enable dirty memory tracking */
2090 cpu_physical_memory_set_dirty_tracking(1);
2091
2092 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2093 }
2094
2095 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002096 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002097
2098 while (!qemu_file_rate_limit(f)) {
2099 int ret;
2100
2101 ret = ram_save_block(f);
2102 bytes_transferred += ret * TARGET_PAGE_SIZE;
2103 if (ret == 0) /* no more blocks */
2104 break;
2105 }
2106
David Turner6a9ef172010-09-09 22:54:36 +02002107 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002108 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2109
2110 /* if we haven't transferred anything this round, force expected_time to a
2111 * a very high value, but without crashing */
2112 if (bwidth == 0)
2113 bwidth = 0.000001;
2114
2115 /* try transferring iterative blocks of memory */
2116
2117 if (stage == 3) {
2118
2119 /* flush all remaining blocks regardless of rate limiting */
2120 while (ram_save_block(f) != 0) {
2121 bytes_transferred += TARGET_PAGE_SIZE;
2122 }
2123 cpu_physical_memory_set_dirty_tracking(0);
2124 }
2125
2126 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2127
2128 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2129
2130 return (stage == 2) && (expected_time <= migrate_max_downtime());
2131}
2132
2133static int ram_load_dead(QEMUFile *f, void *opaque)
2134{
2135 RamDecompressState s1, *s = &s1;
2136 uint8_t buf[10];
2137 ram_addr_t i;
2138
2139 if (ram_decompress_open(s, f) < 0)
2140 return -EINVAL;
2141 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2142 if (ram_decompress_buf(s, buf, 1) < 0) {
2143 fprintf(stderr, "Error while reading ram block header\n");
2144 goto error;
2145 }
2146 if (buf[0] == 0) {
2147 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2148 BDRV_HASH_BLOCK_SIZE) < 0) {
2149 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2150 goto error;
2151 }
2152 } else {
2153 error:
2154 printf("Error block header\n");
2155 return -EINVAL;
2156 }
2157 }
2158 ram_decompress_close(s);
2159
2160 return 0;
2161}
2162
2163static int ram_load(QEMUFile *f, void *opaque, int version_id)
2164{
2165 ram_addr_t addr;
2166 int flags;
2167
2168 if (version_id == 1)
2169 return ram_load_v1(f, opaque);
2170
2171 if (version_id == 2) {
2172 if (qemu_get_be32(f) != last_ram_offset)
2173 return -EINVAL;
2174 return ram_load_dead(f, opaque);
2175 }
2176
2177 if (version_id != 3)
2178 return -EINVAL;
2179
2180 do {
2181 addr = qemu_get_be64(f);
2182
2183 flags = addr & ~TARGET_PAGE_MASK;
2184 addr &= TARGET_PAGE_MASK;
2185
2186 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2187 if (addr != last_ram_offset)
2188 return -EINVAL;
2189 }
2190
2191 if (flags & RAM_SAVE_FLAG_FULL) {
2192 if (ram_load_dead(f, opaque) < 0)
2193 return -EINVAL;
2194 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002195
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002196 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2197 uint8_t ch = qemu_get_byte(f);
2198 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2199 } else if (flags & RAM_SAVE_FLAG_PAGE)
2200 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2201 } while (!(flags & RAM_SAVE_FLAG_EOS));
2202
2203 return 0;
2204}
2205
2206void qemu_service_io(void)
2207{
2208 qemu_notify_event();
2209}
2210
2211/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002212/* machine registration */
2213
2214static QEMUMachine *first_machine = NULL;
2215QEMUMachine *current_machine = NULL;
2216
2217int qemu_register_machine(QEMUMachine *m)
2218{
2219 QEMUMachine **pm;
2220 pm = &first_machine;
2221 while (*pm != NULL)
2222 pm = &(*pm)->next;
2223 m->next = NULL;
2224 *pm = m;
2225 return 0;
2226}
2227
2228static QEMUMachine *find_machine(const char *name)
2229{
2230 QEMUMachine *m;
2231
2232 for(m = first_machine; m != NULL; m = m->next) {
2233 if (!strcmp(m->name, name))
2234 return m;
2235 }
2236 return NULL;
2237}
2238
2239static QEMUMachine *find_default_machine(void)
2240{
2241 QEMUMachine *m;
2242
2243 for(m = first_machine; m != NULL; m = m->next) {
2244 if (m->is_default) {
2245 return m;
2246 }
2247 }
2248 return NULL;
2249}
2250
2251/***********************************************************/
2252/* main execution loop */
2253
2254static void gui_update(void *opaque)
2255{
2256 uint64_t interval = GUI_REFRESH_INTERVAL;
2257 DisplayState *ds = opaque;
2258 DisplayChangeListener *dcl = ds->listeners;
2259
2260 dpy_refresh(ds);
2261
2262 while (dcl != NULL) {
2263 if (dcl->gui_timer_interval &&
2264 dcl->gui_timer_interval < interval)
2265 interval = dcl->gui_timer_interval;
2266 dcl = dcl->next;
2267 }
2268 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
2269}
2270
2271static void nographic_update(void *opaque)
2272{
2273 uint64_t interval = GUI_REFRESH_INTERVAL;
2274
2275 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
2276}
2277
2278struct vm_change_state_entry {
2279 VMChangeStateHandler *cb;
2280 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002281 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002282};
2283
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002284static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002285
2286VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2287 void *opaque)
2288{
2289 VMChangeStateEntry *e;
2290
2291 e = qemu_mallocz(sizeof (*e));
2292
2293 e->cb = cb;
2294 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002295 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002296 return e;
2297}
2298
2299void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2300{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002301 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002302 qemu_free (e);
2303}
2304
2305static void vm_state_notify(int running, int reason)
2306{
2307 VMChangeStateEntry *e;
2308
2309 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2310 e->cb(e->opaque, running, reason);
2311 }
2312}
2313
2314static void resume_all_vcpus(void);
2315static void pause_all_vcpus(void);
2316
2317void vm_start(void)
2318{
2319 if (!vm_running) {
2320 cpu_enable_ticks();
2321 vm_running = 1;
2322 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002323 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002324 resume_all_vcpus();
2325 }
2326}
2327
2328/* reset/shutdown handler */
2329
2330typedef struct QEMUResetEntry {
2331 QEMUResetHandler *func;
2332 void *opaque;
2333 int order;
2334 struct QEMUResetEntry *next;
2335} QEMUResetEntry;
2336
2337static QEMUResetEntry *first_reset_entry;
2338static int reset_requested;
2339static int shutdown_requested;
2340static int powerdown_requested;
2341static int debug_requested;
2342static int vmstop_requested;
2343
2344int qemu_shutdown_requested(void)
2345{
2346 int r = shutdown_requested;
2347 shutdown_requested = 0;
2348 return r;
2349}
2350
2351int qemu_reset_requested(void)
2352{
2353 int r = reset_requested;
2354 reset_requested = 0;
2355 return r;
2356}
2357
2358int qemu_powerdown_requested(void)
2359{
2360 int r = powerdown_requested;
2361 powerdown_requested = 0;
2362 return r;
2363}
2364
2365static int qemu_debug_requested(void)
2366{
2367 int r = debug_requested;
2368 debug_requested = 0;
2369 return r;
2370}
2371
2372static int qemu_vmstop_requested(void)
2373{
2374 int r = vmstop_requested;
2375 vmstop_requested = 0;
2376 return r;
2377}
2378
2379static void do_vm_stop(int reason)
2380{
2381 if (vm_running) {
2382 cpu_disable_ticks();
2383 vm_running = 0;
2384 pause_all_vcpus();
2385 vm_state_notify(0, reason);
2386 }
2387}
2388
2389void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2390{
2391 QEMUResetEntry **pre, *re;
2392
2393 pre = &first_reset_entry;
2394 while (*pre != NULL && (*pre)->order >= order) {
2395 pre = &(*pre)->next;
2396 }
2397 re = qemu_mallocz(sizeof(QEMUResetEntry));
2398 re->func = func;
2399 re->opaque = opaque;
2400 re->order = order;
2401 re->next = NULL;
2402 *pre = re;
2403}
2404
2405void qemu_system_reset(void)
2406{
2407 QEMUResetEntry *re;
2408
2409 /* reset all devices */
2410 for(re = first_reset_entry; re != NULL; re = re->next) {
2411 re->func(re->opaque);
2412 }
2413}
2414
2415void qemu_system_reset_request(void)
2416{
2417 if (no_reboot) {
2418 shutdown_requested = 1;
2419 } else {
2420 reset_requested = 1;
2421 }
2422 qemu_notify_event();
2423}
2424
2425void qemu_system_shutdown_request(void)
2426{
2427 shutdown_requested = 1;
2428 qemu_notify_event();
2429}
2430
2431void qemu_system_powerdown_request(void)
2432{
2433 powerdown_requested = 1;
2434 qemu_notify_event();
2435}
2436
2437#ifdef CONFIG_IOTHREAD
2438static void qemu_system_vmstop_request(int reason)
2439{
2440 vmstop_requested = reason;
2441 qemu_notify_event();
2442}
2443#endif
2444
2445#ifndef _WIN32
2446static int io_thread_fd = -1;
2447
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002448#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002449static void qemu_event_increment(void)
2450{
2451 static const char byte = 0;
2452
2453 if (io_thread_fd == -1)
2454 return;
2455
2456 write(io_thread_fd, &byte, sizeof(byte));
2457}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002458#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002459
2460static void qemu_event_read(void *opaque)
2461{
2462 int fd = (unsigned long)opaque;
2463 ssize_t len;
2464
2465 /* Drain the notify pipe */
2466 do {
2467 char buffer[512];
2468 len = read(fd, buffer, sizeof(buffer));
2469 } while ((len == -1 && errno == EINTR) || len > 0);
2470}
2471
2472static int qemu_event_init(void)
2473{
2474 int err;
2475 int fds[2];
2476
2477 err = pipe(fds);
2478 if (err == -1)
2479 return -errno;
2480
2481 err = fcntl_setfl(fds[0], O_NONBLOCK);
2482 if (err < 0)
2483 goto fail;
2484
2485 err = fcntl_setfl(fds[1], O_NONBLOCK);
2486 if (err < 0)
2487 goto fail;
2488
2489 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2490 (void *)(unsigned long)fds[0]);
2491
2492 io_thread_fd = fds[1];
2493 return 0;
2494
2495fail:
2496 close(fds[0]);
2497 close(fds[1]);
2498 return err;
2499}
2500#else
2501HANDLE qemu_event_handle;
2502
2503static void dummy_event_handler(void *opaque)
2504{
2505}
2506
2507static int qemu_event_init(void)
2508{
2509 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2510 if (!qemu_event_handle) {
2511 perror("Failed CreateEvent");
2512 return -1;
2513 }
2514 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2515 return 0;
2516}
2517
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002518#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002519static void qemu_event_increment(void)
2520{
2521 SetEvent(qemu_event_handle);
2522}
2523#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002524#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002525
2526static int cpu_can_run(CPUState *env)
2527{
2528 if (env->stop)
2529 return 0;
2530 if (env->stopped)
2531 return 0;
2532 return 1;
2533}
2534
2535#ifndef CONFIG_IOTHREAD
2536static int qemu_init_main_loop(void)
2537{
2538 return qemu_event_init();
2539}
2540
2541void qemu_init_vcpu(void *_env)
2542{
2543 CPUState *env = _env;
2544
2545 if (kvm_enabled())
2546 kvm_init_vcpu(env);
2547 return;
2548}
2549
2550int qemu_cpu_self(void *env)
2551{
2552 return 1;
2553}
2554
2555static void resume_all_vcpus(void)
2556{
2557}
2558
2559static void pause_all_vcpus(void)
2560{
2561}
2562
2563void qemu_cpu_kick(void *env)
2564{
2565 return;
2566}
2567
2568void qemu_notify_event(void)
2569{
2570 CPUState *env = cpu_single_env;
2571
2572 if (env) {
2573 cpu_exit(env);
2574#ifdef USE_KQEMU
2575 if (env->kqemu_enabled)
2576 kqemu_cpu_interrupt(env);
2577#endif
2578 }
2579}
2580
2581#define qemu_mutex_lock_iothread() do { } while (0)
2582#define qemu_mutex_unlock_iothread() do { } while (0)
2583
2584void vm_stop(int reason)
2585{
2586 do_vm_stop(reason);
2587}
2588
2589#else /* CONFIG_IOTHREAD */
2590
2591#include "qemu-thread.h"
2592
2593QemuMutex qemu_global_mutex;
2594static QemuMutex qemu_fair_mutex;
2595
2596static QemuThread io_thread;
2597
2598static QemuThread *tcg_cpu_thread;
2599static QemuCond *tcg_halt_cond;
2600
2601static int qemu_system_ready;
2602/* cpu creation */
2603static QemuCond qemu_cpu_cond;
2604/* system init */
2605static QemuCond qemu_system_cond;
2606static QemuCond qemu_pause_cond;
2607
2608static void block_io_signals(void);
2609static void unblock_io_signals(void);
2610static int tcg_has_work(void);
2611
2612static int qemu_init_main_loop(void)
2613{
2614 int ret;
2615
2616 ret = qemu_event_init();
2617 if (ret)
2618 return ret;
2619
2620 qemu_cond_init(&qemu_pause_cond);
2621 qemu_mutex_init(&qemu_fair_mutex);
2622 qemu_mutex_init(&qemu_global_mutex);
2623 qemu_mutex_lock(&qemu_global_mutex);
2624
2625 unblock_io_signals();
2626 qemu_thread_self(&io_thread);
2627
2628 return 0;
2629}
2630
2631static void qemu_wait_io_event(CPUState *env)
2632{
2633 while (!tcg_has_work())
2634 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2635
2636 qemu_mutex_unlock(&qemu_global_mutex);
2637
2638 /*
2639 * Users of qemu_global_mutex can be starved, having no chance
2640 * to acquire it since this path will get to it first.
2641 * So use another lock to provide fairness.
2642 */
2643 qemu_mutex_lock(&qemu_fair_mutex);
2644 qemu_mutex_unlock(&qemu_fair_mutex);
2645
2646 qemu_mutex_lock(&qemu_global_mutex);
2647 if (env->stop) {
2648 env->stop = 0;
2649 env->stopped = 1;
2650 qemu_cond_signal(&qemu_pause_cond);
2651 }
2652}
2653
2654static int qemu_cpu_exec(CPUState *env);
2655
2656static void *kvm_cpu_thread_fn(void *arg)
2657{
2658 CPUState *env = arg;
2659
2660 block_io_signals();
2661 qemu_thread_self(env->thread);
2662
2663 /* signal CPU creation */
2664 qemu_mutex_lock(&qemu_global_mutex);
2665 env->created = 1;
2666 qemu_cond_signal(&qemu_cpu_cond);
2667
2668 /* and wait for machine initialization */
2669 while (!qemu_system_ready)
2670 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2671
2672 while (1) {
2673 if (cpu_can_run(env))
2674 qemu_cpu_exec(env);
2675 qemu_wait_io_event(env);
2676 }
2677
2678 return NULL;
2679}
2680
2681static void tcg_cpu_exec(void);
2682
2683static void *tcg_cpu_thread_fn(void *arg)
2684{
2685 CPUState *env = arg;
2686
2687 block_io_signals();
2688 qemu_thread_self(env->thread);
2689
2690 /* signal CPU creation */
2691 qemu_mutex_lock(&qemu_global_mutex);
2692 for (env = first_cpu; env != NULL; env = env->next_cpu)
2693 env->created = 1;
2694 qemu_cond_signal(&qemu_cpu_cond);
2695
2696 /* and wait for machine initialization */
2697 while (!qemu_system_ready)
2698 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2699
2700 while (1) {
2701 tcg_cpu_exec();
2702 qemu_wait_io_event(cur_cpu);
2703 }
2704
2705 return NULL;
2706}
2707
2708void qemu_cpu_kick(void *_env)
2709{
2710 CPUState *env = _env;
2711 qemu_cond_broadcast(env->halt_cond);
2712 if (kvm_enabled())
2713 qemu_thread_signal(env->thread, SIGUSR1);
2714}
2715
2716int qemu_cpu_self(void *env)
2717{
2718 return (cpu_single_env != NULL);
2719}
2720
2721static void cpu_signal(int sig)
2722{
2723 if (cpu_single_env)
2724 cpu_exit(cpu_single_env);
2725}
2726
2727static void block_io_signals(void)
2728{
2729 sigset_t set;
2730 struct sigaction sigact;
2731
2732 sigemptyset(&set);
2733 sigaddset(&set, SIGUSR2);
2734 sigaddset(&set, SIGIO);
2735 sigaddset(&set, SIGALRM);
2736 pthread_sigmask(SIG_BLOCK, &set, NULL);
2737
2738 sigemptyset(&set);
2739 sigaddset(&set, SIGUSR1);
2740 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2741
2742 memset(&sigact, 0, sizeof(sigact));
2743 sigact.sa_handler = cpu_signal;
2744 sigaction(SIGUSR1, &sigact, NULL);
2745}
2746
2747static void unblock_io_signals(void)
2748{
2749 sigset_t set;
2750
2751 sigemptyset(&set);
2752 sigaddset(&set, SIGUSR2);
2753 sigaddset(&set, SIGIO);
2754 sigaddset(&set, SIGALRM);
2755 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2756
2757 sigemptyset(&set);
2758 sigaddset(&set, SIGUSR1);
2759 pthread_sigmask(SIG_BLOCK, &set, NULL);
2760}
2761
2762static void qemu_signal_lock(unsigned int msecs)
2763{
2764 qemu_mutex_lock(&qemu_fair_mutex);
2765
2766 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2767 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2768 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2769 break;
2770 }
2771 qemu_mutex_unlock(&qemu_fair_mutex);
2772}
2773
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002774void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002775{
2776 if (kvm_enabled()) {
2777 qemu_mutex_lock(&qemu_fair_mutex);
2778 qemu_mutex_lock(&qemu_global_mutex);
2779 qemu_mutex_unlock(&qemu_fair_mutex);
2780 } else
2781 qemu_signal_lock(100);
2782}
2783
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002784void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002785{
2786 qemu_mutex_unlock(&qemu_global_mutex);
2787}
2788
2789static int all_vcpus_paused(void)
2790{
2791 CPUState *penv = first_cpu;
2792
2793 while (penv) {
2794 if (!penv->stopped)
2795 return 0;
2796 penv = (CPUState *)penv->next_cpu;
2797 }
2798
2799 return 1;
2800}
2801
2802static void pause_all_vcpus(void)
2803{
2804 CPUState *penv = first_cpu;
2805
2806 while (penv) {
2807 penv->stop = 1;
2808 qemu_thread_signal(penv->thread, SIGUSR1);
2809 qemu_cpu_kick(penv);
2810 penv = (CPUState *)penv->next_cpu;
2811 }
2812
2813 while (!all_vcpus_paused()) {
2814 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2815 penv = first_cpu;
2816 while (penv) {
2817 qemu_thread_signal(penv->thread, SIGUSR1);
2818 penv = (CPUState *)penv->next_cpu;
2819 }
2820 }
2821}
2822
2823static void resume_all_vcpus(void)
2824{
2825 CPUState *penv = first_cpu;
2826
2827 while (penv) {
2828 penv->stop = 0;
2829 penv->stopped = 0;
2830 qemu_thread_signal(penv->thread, SIGUSR1);
2831 qemu_cpu_kick(penv);
2832 penv = (CPUState *)penv->next_cpu;
2833 }
2834}
2835
2836static void tcg_init_vcpu(void *_env)
2837{
2838 CPUState *env = _env;
2839 /* share a single thread for all cpus with TCG */
2840 if (!tcg_cpu_thread) {
2841 env->thread = qemu_mallocz(sizeof(QemuThread));
2842 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2843 qemu_cond_init(env->halt_cond);
2844 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2845 while (env->created == 0)
2846 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2847 tcg_cpu_thread = env->thread;
2848 tcg_halt_cond = env->halt_cond;
2849 } else {
2850 env->thread = tcg_cpu_thread;
2851 env->halt_cond = tcg_halt_cond;
2852 }
2853}
2854
2855static void kvm_start_vcpu(CPUState *env)
2856{
2857#if 0
2858 kvm_init_vcpu(env);
2859 env->thread = qemu_mallocz(sizeof(QemuThread));
2860 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2861 qemu_cond_init(env->halt_cond);
2862 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2863 while (env->created == 0)
2864 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2865#endif
2866}
2867
2868void qemu_init_vcpu(void *_env)
2869{
2870 CPUState *env = _env;
2871
2872 if (kvm_enabled())
2873 kvm_start_vcpu(env);
2874 else
2875 tcg_init_vcpu(env);
2876}
2877
2878void qemu_notify_event(void)
2879{
2880 qemu_event_increment();
2881}
2882
2883void vm_stop(int reason)
2884{
2885 QemuThread me;
2886 qemu_thread_self(&me);
2887
2888 if (!qemu_thread_equal(&me, &io_thread)) {
2889 qemu_system_vmstop_request(reason);
2890 /*
2891 * FIXME: should not return to device code in case
2892 * vm_stop() has been requested.
2893 */
2894 if (cpu_single_env) {
2895 cpu_exit(cpu_single_env);
2896 cpu_single_env->stop = 1;
2897 }
2898 return;
2899 }
2900 do_vm_stop(reason);
2901}
2902
2903#endif
2904
2905
2906#ifdef _WIN32
2907static void host_main_loop_wait(int *timeout)
2908{
2909 int ret, ret2, i;
2910 PollingEntry *pe;
2911
2912
2913 /* XXX: need to suppress polling by better using win32 events */
2914 ret = 0;
2915 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2916 ret |= pe->func(pe->opaque);
2917 }
2918 if (ret == 0) {
2919 int err;
2920 WaitObjects *w = &wait_objects;
2921
2922 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2923 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2924 if (w->func[ret - WAIT_OBJECT_0])
2925 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2926
2927 /* Check for additional signaled events */
2928 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2929
2930 /* Check if event is signaled */
2931 ret2 = WaitForSingleObject(w->events[i], 0);
2932 if(ret2 == WAIT_OBJECT_0) {
2933 if (w->func[i])
2934 w->func[i](w->opaque[i]);
2935 } else if (ret2 == WAIT_TIMEOUT) {
2936 } else {
2937 err = GetLastError();
2938 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2939 }
2940 }
2941 } else if (ret == WAIT_TIMEOUT) {
2942 } else {
2943 err = GetLastError();
2944 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2945 }
2946 }
2947
2948 *timeout = 0;
2949}
2950#else
2951static void host_main_loop_wait(int *timeout)
2952{
2953}
2954#endif
2955
2956void main_loop_wait(int timeout)
2957{
2958 IOHandlerRecord *ioh;
2959 fd_set rfds, wfds, xfds;
2960 int ret, nfds;
2961 struct timeval tv;
2962
2963 qemu_bh_update_timeout(&timeout);
2964
2965 host_main_loop_wait(&timeout);
2966
2967 /* poll any events */
2968 /* XXX: separate device handlers from system ones */
2969 nfds = -1;
2970 FD_ZERO(&rfds);
2971 FD_ZERO(&wfds);
2972 FD_ZERO(&xfds);
2973 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2974 if (ioh->deleted)
2975 continue;
2976 if (ioh->fd_read &&
2977 (!ioh->fd_read_poll ||
2978 ioh->fd_read_poll(ioh->opaque) != 0)) {
2979 FD_SET(ioh->fd, &rfds);
2980 if (ioh->fd > nfds)
2981 nfds = ioh->fd;
2982 }
2983 if (ioh->fd_write) {
2984 FD_SET(ioh->fd, &wfds);
2985 if (ioh->fd > nfds)
2986 nfds = ioh->fd;
2987 }
2988 }
2989
2990 tv.tv_sec = timeout / 1000;
2991 tv.tv_usec = (timeout % 1000) * 1000;
2992
2993#if defined(CONFIG_SLIRP)
2994 if (slirp_is_inited()) {
2995 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2996 }
2997#endif
2998 qemu_mutex_unlock_iothread();
2999 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3000 qemu_mutex_lock_iothread();
3001 if (ret > 0) {
3002 IOHandlerRecord **pioh;
3003
3004 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3005 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
3006 ioh->fd_read(ioh->opaque);
3007 }
3008 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
3009 ioh->fd_write(ioh->opaque);
3010 }
3011 }
3012
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003013 /* remove deleted IO handlers */
3014 pioh = &first_io_handler;
3015 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003016 ioh = *pioh;
3017 if (ioh->deleted) {
3018 *pioh = ioh->next;
3019 qemu_free(ioh);
3020 } else
3021 pioh = &ioh->next;
3022 }
3023 }
3024#if defined(CONFIG_SLIRP)
3025 if (slirp_is_inited()) {
3026 if (ret < 0) {
3027 FD_ZERO(&rfds);
3028 FD_ZERO(&wfds);
3029 FD_ZERO(&xfds);
3030 }
3031 slirp_select_poll(&rfds, &wfds, &xfds);
3032 }
3033#endif
3034 charpipe_poll();
3035
David Turner6a9ef172010-09-09 22:54:36 +02003036 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003037
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003038 /* Check bottom-halves last in case any of the earlier events triggered
3039 them. */
3040 qemu_bh_poll();
3041
3042}
3043
3044static int qemu_cpu_exec(CPUState *env)
3045{
3046 int ret;
3047#ifdef CONFIG_PROFILER
3048 int64_t ti;
3049#endif
3050
3051#ifdef CONFIG_PROFILER
3052 ti = profile_getclock();
3053#endif
3054 if (use_icount) {
3055 int64_t count;
3056 int decr;
3057 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3058 env->icount_decr.u16.low = 0;
3059 env->icount_extra = 0;
3060 count = qemu_next_deadline();
3061 count = (count + (1 << icount_time_shift) - 1)
3062 >> icount_time_shift;
3063 qemu_icount += count;
3064 decr = (count > 0xffff) ? 0xffff : count;
3065 count -= decr;
3066 env->icount_decr.u16.low = decr;
3067 env->icount_extra = count;
3068 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003069#ifdef CONFIG_TRACE
3070 if (tbflush_requested) {
3071 tbflush_requested = 0;
3072 tb_flush(env);
3073 return EXCP_INTERRUPT;
3074 }
3075#endif
3076
3077
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003078 ret = cpu_exec(env);
3079#ifdef CONFIG_PROFILER
3080 qemu_time += profile_getclock() - ti;
3081#endif
3082 if (use_icount) {
3083 /* Fold pending instructions back into the
3084 instruction counter, and clear the interrupt flag. */
3085 qemu_icount -= (env->icount_decr.u16.low
3086 + env->icount_extra);
3087 env->icount_decr.u32 = 0;
3088 env->icount_extra = 0;
3089 }
3090 return ret;
3091}
3092
3093static void tcg_cpu_exec(void)
3094{
3095 int ret = 0;
3096
3097 if (next_cpu == NULL)
3098 next_cpu = first_cpu;
3099 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3100 CPUState *env = cur_cpu = next_cpu;
3101
3102 if (!vm_running)
3103 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003104 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003105 break;
3106 }
3107 if (cpu_can_run(env))
3108 ret = qemu_cpu_exec(env);
3109 if (ret == EXCP_DEBUG) {
3110 gdb_set_stop_cpu(env);
3111 debug_requested = 1;
3112 break;
3113 }
3114 }
3115}
3116
3117static int cpu_has_work(CPUState *env)
3118{
3119 if (env->stop)
3120 return 1;
3121 if (env->stopped)
3122 return 0;
3123 if (!env->halted)
3124 return 1;
3125 if (qemu_cpu_has_work(env))
3126 return 1;
3127 return 0;
3128}
3129
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003130int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003131{
3132 CPUState *env;
3133
3134 for (env = first_cpu; env != NULL; env = env->next_cpu)
3135 if (cpu_has_work(env))
3136 return 1;
3137 return 0;
3138}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003139
3140static int vm_can_run(void)
3141{
3142 if (powerdown_requested)
3143 return 0;
3144 if (reset_requested)
3145 return 0;
3146 if (shutdown_requested)
3147 return 0;
3148 if (debug_requested)
3149 return 0;
3150 return 1;
3151}
3152
3153static void main_loop(void)
3154{
3155 int r;
3156
3157#ifdef CONFIG_IOTHREAD
3158 qemu_system_ready = 1;
3159 qemu_cond_broadcast(&qemu_system_cond);
3160#endif
3161
3162 for (;;) {
3163 do {
3164#ifdef CONFIG_PROFILER
3165 int64_t ti;
3166#endif
3167#ifndef CONFIG_IOTHREAD
3168 tcg_cpu_exec();
3169#endif
3170#ifdef CONFIG_PROFILER
3171 ti = profile_getclock();
3172#endif
3173 main_loop_wait(qemu_calculate_timeout());
3174#ifdef CONFIG_PROFILER
3175 dev_time += profile_getclock() - ti;
3176#endif
rich canningsd952f282011-03-01 15:40:09 -08003177
3178 if (rotate_logs_requested) {
3179 FILE* new_dns_log_fd = rotate_qemu_log(get_slirp_dns_log_fd(),
3180 dns_log_filename);
3181 FILE* new_drop_log_fd = rotate_qemu_log(get_slirp_drop_log_fd(),
3182 drop_log_filename);
3183 slirp_dns_log_fd(new_dns_log_fd);
3184 slirp_drop_log_fd(new_drop_log_fd);
3185 reset_rotate_qemu_logs_request();
3186 }
3187
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003188 } while (vm_can_run());
3189
3190 if (qemu_debug_requested())
3191 vm_stop(EXCP_DEBUG);
3192 if (qemu_shutdown_requested()) {
3193 if (no_shutdown) {
3194 vm_stop(0);
3195 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003196 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003197 if (savevm_on_exit != NULL) {
3198 do_savevm(cur_mon, savevm_on_exit);
3199 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003200 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003201 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003202 }
3203 if (qemu_reset_requested()) {
3204 pause_all_vcpus();
3205 qemu_system_reset();
3206 resume_all_vcpus();
3207 }
3208 if (qemu_powerdown_requested())
3209 qemu_system_powerdown();
3210 if ((r = qemu_vmstop_requested()))
3211 vm_stop(r);
3212 }
3213 pause_all_vcpus();
3214}
3215
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003216void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003217{
3218 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3219}
3220
3221void qemu_help(int exitcode)
3222{
3223 version();
3224 printf("usage: %s [options] [disk_image]\n"
3225 "\n"
3226 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3227 "\n"
3228#define DEF(option, opt_arg, opt_enum, opt_help) \
3229 opt_help
3230#define DEFHEADING(text) stringify(text) "\n"
3231#include "qemu-options.h"
3232#undef DEF
3233#undef DEFHEADING
3234#undef GEN_DOCS
3235 "\n"
3236 "During emulation, the following keys are useful:\n"
3237 "ctrl-alt-f toggle full screen\n"
3238 "ctrl-alt-n switch to virtual console 'n'\n"
3239 "ctrl-alt toggle mouse and keyboard grab\n"
3240 "\n"
3241 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3242 ,
3243 "qemu",
3244 DEFAULT_RAM_SIZE,
3245#ifndef _WIN32
3246 DEFAULT_NETWORK_SCRIPT,
3247 DEFAULT_NETWORK_DOWN_SCRIPT,
3248#endif
3249 DEFAULT_GDBSTUB_PORT,
3250 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003251 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003252}
3253
3254#define HAS_ARG 0x0001
3255
3256enum {
3257#define DEF(option, opt_arg, opt_enum, opt_help) \
3258 opt_enum,
3259#define DEFHEADING(text)
3260#include "qemu-options.h"
3261#undef DEF
3262#undef DEFHEADING
3263#undef GEN_DOCS
3264};
3265
3266typedef struct QEMUOption {
3267 const char *name;
3268 int flags;
3269 int index;
3270} QEMUOption;
3271
3272static const QEMUOption qemu_options[] = {
3273 { "h", 0, QEMU_OPTION_h },
3274#define DEF(option, opt_arg, opt_enum, opt_help) \
3275 { option, opt_arg, opt_enum },
3276#define DEFHEADING(text)
3277#include "qemu-options.h"
3278#undef DEF
3279#undef DEFHEADING
3280#undef GEN_DOCS
3281 { NULL, 0, 0 },
3282};
3283
3284#ifdef HAS_AUDIO
3285struct soundhw soundhw[] = {
3286#ifdef HAS_AUDIO_CHOICE
3287#if defined(TARGET_I386) || defined(TARGET_MIPS)
3288 {
3289 "pcspk",
3290 "PC speaker",
3291 0,
3292 1,
3293 { .init_isa = pcspk_audio_init }
3294 },
3295#endif
3296
3297#ifdef CONFIG_SB16
3298 {
3299 "sb16",
3300 "Creative Sound Blaster 16",
3301 0,
3302 1,
3303 { .init_isa = SB16_init }
3304 },
3305#endif
3306
3307#ifdef CONFIG_CS4231A
3308 {
3309 "cs4231a",
3310 "CS4231A",
3311 0,
3312 1,
3313 { .init_isa = cs4231a_init }
3314 },
3315#endif
3316
3317#ifdef CONFIG_ADLIB
3318 {
3319 "adlib",
3320#ifdef HAS_YMF262
3321 "Yamaha YMF262 (OPL3)",
3322#else
3323 "Yamaha YM3812 (OPL2)",
3324#endif
3325 0,
3326 1,
3327 { .init_isa = Adlib_init }
3328 },
3329#endif
3330
3331#ifdef CONFIG_GUS
3332 {
3333 "gus",
3334 "Gravis Ultrasound GF1",
3335 0,
3336 1,
3337 { .init_isa = GUS_init }
3338 },
3339#endif
3340
3341#ifdef CONFIG_AC97
3342 {
3343 "ac97",
3344 "Intel 82801AA AC97 Audio",
3345 0,
3346 0,
3347 { .init_pci = ac97_init }
3348 },
3349#endif
3350
3351#ifdef CONFIG_ES1370
3352 {
3353 "es1370",
3354 "ENSONIQ AudioPCI ES1370",
3355 0,
3356 0,
3357 { .init_pci = es1370_init }
3358 },
3359#endif
3360
3361#endif /* HAS_AUDIO_CHOICE */
3362
3363 { NULL, NULL, 0, 0, { NULL } }
3364};
3365
3366static void select_soundhw (const char *optarg)
3367{
3368 struct soundhw *c;
3369
3370 if (*optarg == '?') {
3371 show_valid_cards:
3372
3373 printf ("Valid sound card names (comma separated):\n");
3374 for (c = soundhw; c->name; ++c) {
3375 printf ("%-11s %s\n", c->name, c->descr);
3376 }
3377 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003378 if (*optarg != '?') {
3379 PANIC("Unknown sound card name: %s", optarg);
3380 } else {
3381 QEMU_EXIT(0);
3382 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003383 }
3384 else {
3385 size_t l;
3386 const char *p;
3387 char *e;
3388 int bad_card = 0;
3389
3390 if (!strcmp (optarg, "all")) {
3391 for (c = soundhw; c->name; ++c) {
3392 c->enabled = 1;
3393 }
3394 return;
3395 }
3396
3397 p = optarg;
3398 while (*p) {
3399 e = strchr (p, ',');
3400 l = !e ? strlen (p) : (size_t) (e - p);
3401
3402 for (c = soundhw; c->name; ++c) {
3403 if (!strncmp (c->name, p, l)) {
3404 c->enabled = 1;
3405 break;
3406 }
3407 }
3408
3409 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003410#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003411 if (l > 80) {
3412 fprintf (stderr,
3413 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003414 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003415 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3416 (int) l, p);
3417 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003418#endif // !CONFIG_ANDROID
3419 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003420 }
3421 p += l + (e != NULL);
3422 }
3423
3424 if (bad_card)
3425 goto show_valid_cards;
3426 }
3427}
3428#endif
3429
3430static void select_vgahw (const char *p)
3431{
3432 const char *opts;
3433
3434 cirrus_vga_enabled = 0;
3435 std_vga_enabled = 0;
3436 vmsvga_enabled = 0;
3437 xenfb_enabled = 0;
3438 if (strstart(p, "std", &opts)) {
3439 std_vga_enabled = 1;
3440 } else if (strstart(p, "cirrus", &opts)) {
3441 cirrus_vga_enabled = 1;
3442 } else if (strstart(p, "vmware", &opts)) {
3443 vmsvga_enabled = 1;
3444 } else if (strstart(p, "xenfb", &opts)) {
3445 xenfb_enabled = 1;
3446 } else if (!strstart(p, "none", &opts)) {
3447 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003448 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003449 }
3450 while (*opts) {
3451 const char *nextopt;
3452
3453 if (strstart(opts, ",retrace=", &nextopt)) {
3454 opts = nextopt;
3455 if (strstart(opts, "dumb", &nextopt))
3456 vga_retrace_method = VGA_RETRACE_DUMB;
3457 else if (strstart(opts, "precise", &nextopt))
3458 vga_retrace_method = VGA_RETRACE_PRECISE;
3459 else goto invalid_vga;
3460 } else goto invalid_vga;
3461 opts = nextopt;
3462 }
3463}
3464
3465#ifdef _WIN32
3466static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3467{
3468 exit(STATUS_CONTROL_C_EXIT);
3469 return TRUE;
3470}
3471#endif
3472
3473int qemu_uuid_parse(const char *str, uint8_t *uuid)
3474{
3475 int ret;
3476
3477 if(strlen(str) != 36)
3478 return -1;
3479
3480 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3481 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3482 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3483
3484 if(ret != 16)
3485 return -1;
3486
3487#ifdef TARGET_I386
3488 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3489#endif
3490
3491 return 0;
3492}
3493
3494#define MAX_NET_CLIENTS 32
3495
3496#ifndef _WIN32
3497
3498static void termsig_handler(int signal)
3499{
3500 qemu_system_shutdown_request();
3501}
3502
3503static void sigchld_handler(int signal)
3504{
3505 waitpid(-1, NULL, WNOHANG);
3506}
3507
3508static void sighandler_setup(void)
3509{
3510 struct sigaction act;
3511
3512 memset(&act, 0, sizeof(act));
3513 act.sa_handler = termsig_handler;
3514 sigaction(SIGINT, &act, NULL);
3515 sigaction(SIGHUP, &act, NULL);
3516 sigaction(SIGTERM, &act, NULL);
3517
3518 act.sa_handler = sigchld_handler;
3519 act.sa_flags = SA_NOCLDSTOP;
3520 sigaction(SIGCHLD, &act, NULL);
3521}
3522
3523#endif
3524
3525#ifdef _WIN32
3526/* Look for support files in the same directory as the executable. */
3527static char *find_datadir(const char *argv0)
3528{
3529 char *p;
3530 char buf[MAX_PATH];
3531 DWORD len;
3532
3533 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3534 if (len == 0) {
3535 return NULL;
3536 }
3537
3538 buf[len] = 0;
3539 p = buf + len - 1;
3540 while (p != buf && *p != '\\')
3541 p--;
3542 *p = 0;
3543 if (access(buf, R_OK) == 0) {
3544 return qemu_strdup(buf);
3545 }
3546 return NULL;
3547}
3548#else /* !_WIN32 */
3549
3550/* Find a likely location for support files using the location of the binary.
3551 For installed binaries this will be "$bindir/../share/qemu". When
Bruce Beare02c63852011-02-24 09:10:52 -08003552 running from the build tree this will be "$bindir/../pc-bios".
3553 The emulator running from the SDK will find the support files in $bindir/lib/pc-bios. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003554#define SHARE_SUFFIX "/share/qemu"
3555#define BUILD_SUFFIX "/pc-bios"
Bruce Beare02c63852011-02-24 09:10:52 -08003556#define SDK_SUFFIX "/lib/pc-bios"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003557static char *find_datadir(const char *argv0)
3558{
3559 char *dir;
3560 char *p = NULL;
3561 char *res;
3562#ifdef PATH_MAX
3563 char buf[PATH_MAX];
3564#endif
3565 size_t max_len;
3566
3567#if defined(__linux__)
3568 {
3569 int len;
3570 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3571 if (len > 0) {
3572 buf[len] = 0;
3573 p = buf;
3574 }
3575 }
3576#elif defined(__FreeBSD__)
3577 {
3578 int len;
3579 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3580 if (len > 0) {
3581 buf[len] = 0;
3582 p = buf;
3583 }
3584 }
3585#endif
3586 /* If we don't have any way of figuring out the actual executable
3587 location then try argv[0]. */
3588 if (!p) {
3589#ifdef PATH_MAX
3590 p = buf;
3591#endif
3592 p = realpath(argv0, p);
3593 if (!p) {
3594 return NULL;
3595 }
3596 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003597
Bruce Beare02c63852011-02-24 09:10:52 -08003598#define STRLEN_CONST(str) (sizeof(str)-1)
3599 dir = dirname(p);
3600 max_len = strlen(dir) + 1 +
3601 MAX(STRLEN_CONST(SDK_SUFFIX), MAX(STRLEN_CONST(SHARE_SUFFIX), STRLEN_CONST(BUILD_SUFFIX)));
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003602 res = qemu_mallocz(max_len);
Bruce Beare02c63852011-02-24 09:10:52 -08003603
3604 snprintf(res, max_len, "%s%s", dir, SDK_SUFFIX);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003605 if (access(res, R_OK)) {
Bruce Beare02c63852011-02-24 09:10:52 -08003606 dir = dirname(dir);
3607
3608 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
3609 if (access(res, R_OK)) {
3610 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
3611 if (access(res, R_OK)) {
3612 qemu_free(res);
3613 res = NULL;
3614 }
3615 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003616 }
3617#ifndef PATH_MAX
3618 free(p);
3619#endif
3620 return res;
3621}
3622#undef SHARE_SUFFIX
3623#undef BUILD_SUFFIX
3624#endif
3625
3626char *qemu_find_file(int type, const char *name)
3627{
3628 int len;
3629 const char *subdir;
3630 char *buf;
3631
3632 /* If name contains path separators then try it as a straight path. */
3633 if ((strchr(name, '/') || strchr(name, '\\'))
3634 && access(name, R_OK) == 0) {
3635 return strdup(name);
3636 }
3637 switch (type) {
3638 case QEMU_FILE_TYPE_BIOS:
3639 subdir = "";
3640 break;
3641 case QEMU_FILE_TYPE_KEYMAP:
3642 subdir = "keymaps/";
3643 break;
3644 default:
3645 abort();
3646 }
3647 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3648 buf = qemu_mallocz(len);
3649 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3650 if (access(buf, R_OK)) {
3651 qemu_free(buf);
3652 return NULL;
3653 }
3654 return buf;
3655}
3656
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003657static int
3658add_dns_server( const char* server_name )
3659{
3660 SockAddress addr;
3661
3662 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3663 fprintf(stdout,
3664 "### WARNING: can't resolve DNS server name '%s'\n",
3665 server_name );
3666 return -1;
3667 }
3668
3669 fprintf(stderr,
3670 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3671
3672 if ( slirp_add_dns_server( &addr ) < 0 ) {
3673 fprintf(stderr,
3674 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3675 return -1;
3676 }
3677 return 0;
3678}
3679
rich cannings7339b552011-02-16 13:43:44 -08003680/* Parses an integer
3681 * Pararm:
3682 * str String containing a number to be parsed.
3683 * result Passes the parsed integer in this argument
3684 * returns 0 if ok, -1 if failed
3685 */
3686int
3687parse_int(const char *str, int *result)
3688{
3689 char* r;
3690 *result = strtol(str, &r, 0);
3691 if (r == NULL || *r != '\0')
3692 return -1;
3693
3694 return 0;
3695}
3696
rich canningsd952f282011-03-01 15:40:09 -08003697#ifndef _WIN32
3698/*
3699 * Initializes the SIGUSR1 signal handler to clear Qemu logs.
3700 */
3701void init_qemu_clear_logs_sig() {
3702 struct sigaction act;
3703 sigfillset(&act.sa_mask);
3704 act.sa_flags = 0;
3705 act.sa_handler = rotate_qemu_logs_handler;
3706 if (sigaction(SIGUSR1, &act, NULL) == -1) {
3707 fprintf(stderr, "Failed to setup SIGUSR1 handler to clear Qemu logs\n");
3708 exit(-1);
3709 }
3710}
3711#endif
3712
3713
rich cannings7339b552011-02-16 13:43:44 -08003714
3715/* parses a null-terminated string specifying a network port (e.g., "80") or
3716 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3717 * are the same. Returns 0 on success, -1 on error. */
3718
3719int parse_port_range(const char *str, unsigned short *lport,
3720 unsigned short *hport) {
3721
3722 unsigned int low = 0, high = 0;
3723 char *p, *arg = strdup(str);
3724
3725 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3726 p = arg + 1; /* skip '[' */
3727 low = atoi(strtok(p, "-"));
3728 high = atoi(strtok(NULL, "-"));
3729 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3730 *lport = low;
3731 *hport = high;
3732 }
3733 }
3734 else {
3735 low = atoi(arg);
3736 if ((0 < low) && (low < 65535)) {
3737 *lport = low;
3738 *hport = low;
3739 }
3740 }
3741 free(arg);
3742 if (low != 0)
3743 return 0;
3744 return -1;
3745}
3746
3747/*
3748 * Implements the generic port forwarding option
3749 */
3750void
3751net_slirp_forward(const char *optarg)
3752{
3753 /*
3754 * we expect the following format:
3755 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3756 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3757 */
3758 char *argument = strdup(optarg), *p = argument;
3759 char *dst_net, *dst_mask, *dst_port;
3760 char *redirect_ip, *redirect_port;
3761 uint32_t dnet, dmask, rip;
3762 unsigned short dlport, dhport, rport;
3763
3764
3765 dst_net = strtok(p, ":");
3766 dst_mask = strtok(NULL, ":");
3767 dst_port = strtok(NULL, ":");
3768 redirect_ip = strtok(NULL, ":");
3769 redirect_port = strtok(NULL, ":");
3770
3771 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3772 redirect_ip == NULL || redirect_port == NULL) {
3773 fprintf(stderr,
3774 "Invalid argument for -net-forward, we expect "
3775 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3776 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3777 ":redirect_ip:redirect_port: %s\n",
3778 optarg);
3779 exit(1);
3780 }
3781
3782 /* inet_strtoip converts dotted address to host byte order */
3783 if (inet_strtoip(dst_net, &dnet) == -1) {
3784 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3785 exit(1);
3786 }
3787 if (inet_strtoip(dst_mask, &dmask) == -1) {
3788 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3789 exit(1);
3790 }
3791 if (inet_strtoip(redirect_ip, &rip) == -1) {
3792 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3793 exit(1);
3794 }
3795
3796 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3797 fprintf(stderr, "Invalid destination port or port range\n");
3798 exit(1);
3799 }
3800
3801 rport = atoi(redirect_port);
3802 if (!rport) {
3803 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3804 exit(1);
3805 }
3806
3807 dnet &= dmask;
3808
3809 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3810 rip, rport);
3811
3812 free(argument);
3813}
3814
3815
3816/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3817 * entry in the allows list */
3818void
3819slirp_allow(const char *optarg, u_int8_t proto)
3820{
3821 /*
3822 * we expect the following format:
3823 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3824 */
3825 char *argument = strdup(optarg), *p = argument;
3826 char *dst_ip_str, *dst_port_str;
3827 uint32_t dst_ip;
3828 unsigned short dst_lport, dst_hport;
3829
3830 dst_ip_str = strtok(p, ":");
3831 dst_port_str = strtok(NULL, ":");
3832
3833 if (dst_ip_str == NULL || dst_port_str == NULL) {
3834 fprintf(stderr,
3835 "Invalid argument %s for -allow. We expect "
3836 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3837 optarg);
3838 exit(1);
3839 }
3840
3841 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3842 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3843 exit(1);
3844 }
3845 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3846 fprintf(stderr, "Invalid destination port or port range\n");
3847 exit(1);
3848 }
3849
3850 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3851
3852 free(argument);
3853}
3854
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01003855/* Add a serial device at a given location in the emulated hardware table.
3856 * On failure, this function aborts the program with an error message.
3857 */
3858static void
3859serial_hds_add_at(int index, const char* devname)
3860{
3861 char label[32];
3862
3863 if (!devname || !strcmp(devname,"none"))
3864 return;
3865
3866 if (index >= MAX_SERIAL_PORTS) {
3867 PANIC("qemu: invalid serial index for %s (%d >= %d)",
3868 devname, index, MAX_SERIAL_PORTS);
3869 }
3870 if (serial_hds[index] != NULL) {
3871 PANIC("qemu: invalid serial index for %s (%d: already taken!)",
3872 devname, index);
3873 }
3874 snprintf(label, sizeof(label), "serial%d", index);
3875 serial_hds[index] = qemu_chr_open(label, devname, NULL);
3876 if (!serial_hds[index]) {
3877 PANIC("qemu: could not open serial device '%s'", devname);
3878 }
3879}
3880
3881
3882/* Find a free slot in the emulated serial device table, and register
3883 * it. Return the allocated table index.
3884 */
3885static int
3886serial_hds_add(const char* devname)
3887{
3888 int index;
3889
3890 /* Find first free slot */
3891 for (index = 0; index < MAX_SERIAL_PORTS; index++) {
3892 if (serial_hds[index] == NULL) {
3893 serial_hds_add_at(index, devname);
3894 return index;
3895 }
3896 }
3897
3898 PANIC("qemu: too many serial devices registered (%d)", index);
3899 return -1; /* shouldn't happen */
3900}
3901
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003902int main(int argc, char **argv, char **envp)
3903{
3904 const char *gdbstub_dev = NULL;
3905 uint32_t boot_devices_bitmap = 0;
3906 int i;
3907 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003908 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003909 const char *initrd_filename;
3910 const char *kernel_filename, *kernel_cmdline;
3911 const char *boot_devices = "";
3912 DisplayState *ds;
3913 DisplayChangeListener *dcl;
3914 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003915 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01003916 QemuOpts *hdb_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003917 const char *net_clients[MAX_NET_CLIENTS];
3918 int nb_net_clients;
3919 const char *bt_opts[MAX_BT_CMDLINE];
3920 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003921 int optind;
3922 const char *r, *optarg;
3923 CharDriverState *monitor_hd = NULL;
3924 const char *monitor_device;
3925 const char *serial_devices[MAX_SERIAL_PORTS];
3926 int serial_device_index;
3927 const char *parallel_devices[MAX_PARALLEL_PORTS];
3928 int parallel_device_index;
3929 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3930 int virtio_console_index;
3931 const char *loadvm = NULL;
3932 QEMUMachine *machine;
3933 const char *cpu_model;
3934 const char *usb_devices[MAX_USB_CMDLINE];
3935 int usb_devices_index;
3936#ifndef _WIN32
3937 int fds[2];
3938#endif
3939 int tb_size;
3940 const char *pid_file = NULL;
3941 const char *incoming = NULL;
3942#ifndef _WIN32
3943 int fd = 0;
3944 struct passwd *pwd = NULL;
3945 const char *chroot_dir = NULL;
3946 const char *run_as = NULL;
3947#endif
3948 CPUState *env;
3949 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003950 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003951 STRALLOC_DEFINE(kernel_params);
3952 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003953 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003954
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003955 /* Initialize sockets before anything else, so we can properly report
3956 * initialization failures back to the UI. */
3957#ifdef _WIN32
3958 socket_init();
3959#endif
3960
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003961 init_clocks();
3962
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003963 qemu_cache_utils_init(envp);
3964
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003965 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003966#ifndef _WIN32
3967 {
3968 struct sigaction act;
3969 sigfillset(&act.sa_mask);
3970 act.sa_flags = 0;
3971 act.sa_handler = SIG_IGN;
3972 sigaction(SIGPIPE, &act, NULL);
3973 }
3974#else
3975 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3976 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3977 QEMU to run on a single CPU */
3978 {
3979 HANDLE h;
3980 DWORD mask, smask;
3981 int i;
3982 h = GetCurrentProcess();
3983 if (GetProcessAffinityMask(h, &mask, &smask)) {
3984 for(i = 0; i < 32; i++) {
3985 if (mask & (1 << i))
3986 break;
3987 }
3988 if (i != 32) {
3989 mask = 1 << i;
3990 SetProcessAffinityMask(h, mask);
3991 }
3992 }
3993 }
3994#endif
3995
3996 module_call_init(MODULE_INIT_MACHINE);
3997 machine = find_default_machine();
3998 cpu_model = NULL;
3999 initrd_filename = NULL;
4000 ram_size = 0;
4001 snapshot = 0;
4002 kernel_filename = NULL;
4003 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004004
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004005 cyls = heads = secs = 0;
4006 translation = BIOS_ATA_TRANSLATION_AUTO;
4007 monitor_device = "vc:80Cx24C";
4008
4009 serial_devices[0] = "vc:80Cx24C";
4010 for(i = 1; i < MAX_SERIAL_PORTS; i++)
4011 serial_devices[i] = NULL;
4012 serial_device_index = 0;
4013
4014 parallel_devices[0] = "vc:80Cx24C";
4015 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
4016 parallel_devices[i] = NULL;
4017 parallel_device_index = 0;
4018
4019 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
4020 virtio_consoles[i] = NULL;
4021 virtio_console_index = 0;
4022
4023 for (i = 0; i < MAX_NODES; i++) {
4024 node_mem[i] = 0;
4025 node_cpumask[i] = 0;
4026 }
4027
4028 usb_devices_index = 0;
4029
4030 nb_net_clients = 0;
4031 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004032#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004033 nb_drives = 0;
4034 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004035#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004036 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004037
4038 nb_nics = 0;
4039
4040 tb_size = 0;
4041 autostart= 1;
4042
4043 register_watchdogs();
4044
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004045 /* Initialize boot properties. */
4046 boot_property_init_service();
David 'Digit' Turnerca950592011-04-27 12:26:15 +02004047 android_hw_control_init();
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02004048 android_net_pipes_init();
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004049
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004050 optind = 1;
4051 for(;;) {
4052 if (optind >= argc)
4053 break;
4054 r = argv[optind];
4055 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004056 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004057 } else {
4058 const QEMUOption *popt;
4059
4060 optind++;
4061 /* Treat --foo the same as -foo. */
4062 if (r[1] == '-')
4063 r++;
4064 popt = qemu_options;
4065 for(;;) {
4066 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004067 PANIC("%s: invalid option -- '%s'",
4068 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004069 }
4070 if (!strcmp(popt->name, r + 1))
4071 break;
4072 popt++;
4073 }
4074 if (popt->flags & HAS_ARG) {
4075 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004076 PANIC("%s: option '%s' requires an argument",
4077 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004078 }
4079 optarg = argv[optind++];
4080 } else {
4081 optarg = NULL;
4082 }
4083
4084 switch(popt->index) {
4085 case QEMU_OPTION_M:
4086 machine = find_machine(optarg);
4087 if (!machine) {
4088 QEMUMachine *m;
4089 printf("Supported machines are:\n");
4090 for(m = first_machine; m != NULL; m = m->next) {
4091 printf("%-10s %s%s\n",
4092 m->name, m->desc,
4093 m->is_default ? " (default)" : "");
4094 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004095 if (*optarg != '?') {
4096 PANIC("Invalid machine parameter: %s",
4097 optarg);
4098 } else {
4099 QEMU_EXIT(0);
4100 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004101 }
4102 break;
4103 case QEMU_OPTION_cpu:
4104 /* hw initialization will check this */
4105 if (*optarg == '?') {
4106/* XXX: implement xxx_cpu_list for targets that still miss it */
4107#if defined(cpu_list)
4108 cpu_list(stdout, &fprintf);
4109#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004110 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004111 } else {
4112 cpu_model = optarg;
4113 }
4114 break;
4115 case QEMU_OPTION_initrd:
4116 initrd_filename = optarg;
4117 break;
4118 case QEMU_OPTION_hda:
4119 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004120 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004121 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004122 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004123 ",cyls=%d,heads=%d,secs=%d%s",
4124 0, cyls, heads, secs,
4125 translation == BIOS_ATA_TRANSLATION_LBA ?
4126 ",trans=lba" :
4127 translation == BIOS_ATA_TRANSLATION_NONE ?
4128 ",trans=none" : "");
4129 break;
4130 case QEMU_OPTION_hdb:
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01004131 hdb_opts = drive_add(optarg, HD_ALIAS, 1);
4132 break;
4133
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004134 case QEMU_OPTION_hdc:
4135 case QEMU_OPTION_hdd:
4136 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
4137 break;
4138 case QEMU_OPTION_drive:
4139 drive_add(NULL, "%s", optarg);
4140 break;
4141 case QEMU_OPTION_mtdblock:
4142 drive_add(optarg, MTD_ALIAS);
4143 break;
4144 case QEMU_OPTION_sd:
4145 drive_add(optarg, SD_ALIAS);
4146 break;
4147 case QEMU_OPTION_pflash:
4148 drive_add(optarg, PFLASH_ALIAS);
4149 break;
4150 case QEMU_OPTION_snapshot:
4151 snapshot = 1;
4152 break;
4153 case QEMU_OPTION_hdachs:
4154 {
4155 const char *p;
4156 p = optarg;
4157 cyls = strtol(p, (char **)&p, 0);
4158 if (cyls < 1 || cyls > 16383)
4159 goto chs_fail;
4160 if (*p != ',')
4161 goto chs_fail;
4162 p++;
4163 heads = strtol(p, (char **)&p, 0);
4164 if (heads < 1 || heads > 16)
4165 goto chs_fail;
4166 if (*p != ',')
4167 goto chs_fail;
4168 p++;
4169 secs = strtol(p, (char **)&p, 0);
4170 if (secs < 1 || secs > 63)
4171 goto chs_fail;
4172 if (*p == ',') {
4173 p++;
4174 if (!strcmp(p, "none"))
4175 translation = BIOS_ATA_TRANSLATION_NONE;
4176 else if (!strcmp(p, "lba"))
4177 translation = BIOS_ATA_TRANSLATION_LBA;
4178 else if (!strcmp(p, "auto"))
4179 translation = BIOS_ATA_TRANSLATION_AUTO;
4180 else
4181 goto chs_fail;
4182 } else if (*p != '\0') {
4183 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004184 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004185 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004186 if (hda_opts != NULL) {
4187 char num[16];
4188 snprintf(num, sizeof(num), "%d", cyls);
4189 qemu_opt_set(hda_opts, "cyls", num);
4190 snprintf(num, sizeof(num), "%d", heads);
4191 qemu_opt_set(hda_opts, "heads", num);
4192 snprintf(num, sizeof(num), "%d", secs);
4193 qemu_opt_set(hda_opts, "secs", num);
4194 if (translation == BIOS_ATA_TRANSLATION_LBA)
4195 qemu_opt_set(hda_opts, "trans", "lba");
4196 if (translation == BIOS_ATA_TRANSLATION_NONE)
4197 qemu_opt_set(hda_opts, "trans", "none");
4198 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004199 }
4200 break;
4201 case QEMU_OPTION_numa:
4202 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004203 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004204 }
4205 numa_add(optarg);
4206 break;
4207 case QEMU_OPTION_nographic:
4208 display_type = DT_NOGRAPHIC;
4209 break;
4210#ifdef CONFIG_CURSES
4211 case QEMU_OPTION_curses:
4212 display_type = DT_CURSES;
4213 break;
4214#endif
4215 case QEMU_OPTION_portrait:
4216 graphic_rotate = 1;
4217 break;
4218 case QEMU_OPTION_kernel:
4219 kernel_filename = optarg;
4220 break;
4221 case QEMU_OPTION_append:
4222 kernel_cmdline = optarg;
4223 break;
4224 case QEMU_OPTION_cdrom:
4225 drive_add(optarg, CDROM_ALIAS);
4226 break;
4227 case QEMU_OPTION_boot:
4228 boot_devices = optarg;
4229 /* We just do some generic consistency checks */
4230 {
4231 /* Could easily be extended to 64 devices if needed */
4232 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004233
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004234 boot_devices_bitmap = 0;
4235 for (p = boot_devices; *p != '\0'; p++) {
4236 /* Allowed boot devices are:
4237 * a b : floppy disk drives
4238 * c ... f : IDE disk drives
4239 * g ... m : machine implementation dependant drives
4240 * n ... p : network devices
4241 * It's up to each machine implementation to check
4242 * if the given boot devices match the actual hardware
4243 * implementation and firmware features.
4244 */
4245 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004246 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004247 }
4248 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004249 PANIC(
4250 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004251 }
4252 boot_devices_bitmap |= 1 << (*p - 'a');
4253 }
4254 }
4255 break;
4256 case QEMU_OPTION_fda:
4257 case QEMU_OPTION_fdb:
4258 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4259 break;
4260#ifdef TARGET_I386
4261 case QEMU_OPTION_no_fd_bootchk:
4262 fd_bootchk = 0;
4263 break;
4264#endif
4265 case QEMU_OPTION_net:
4266 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004267 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004268 }
4269 net_clients[nb_net_clients] = optarg;
4270 nb_net_clients++;
4271 break;
4272#ifdef CONFIG_SLIRP
4273 case QEMU_OPTION_tftp:
4274 tftp_prefix = optarg;
4275 break;
4276 case QEMU_OPTION_bootp:
4277 bootp_filename = optarg;
4278 break;
4279#if 0 /* ANDROID disabled */
4280#ifndef _WIN32
4281 case QEMU_OPTION_smb:
4282 net_slirp_smb(optarg);
4283 break;
4284#endif
4285#endif /* ANDROID */
4286 case QEMU_OPTION_redir:
4287 net_slirp_redir(NULL, optarg, NULL);
4288 break;
4289#endif
4290 case QEMU_OPTION_bt:
4291 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004292 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004293 }
4294 bt_opts[nb_bt_opts++] = optarg;
4295 break;
4296#ifdef HAS_AUDIO
4297 case QEMU_OPTION_audio_help:
4298 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004299 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004300 break;
4301 case QEMU_OPTION_soundhw:
4302 select_soundhw (optarg);
4303 break;
4304#endif
4305 case QEMU_OPTION_h:
4306 qemu_help(0);
4307 break;
4308 case QEMU_OPTION_version:
4309 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004310 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004311 break;
4312 case QEMU_OPTION_m: {
4313 uint64_t value;
4314 char *ptr;
4315
4316 value = strtoul(optarg, &ptr, 10);
4317 switch (*ptr) {
4318 case 0: case 'M': case 'm':
4319 value <<= 20;
4320 break;
4321 case 'G': case 'g':
4322 value <<= 30;
4323 break;
4324 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004325 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004326 }
4327
4328 /* On 32-bit hosts, QEMU is limited by virtual address space */
4329 if (value > (2047 << 20)
4330#ifndef CONFIG_KQEMU
4331 && HOST_LONG_BITS == 32
4332#endif
4333 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004334 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004335 }
4336 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004337 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004338 }
4339 ram_size = value;
4340 break;
4341 }
4342 case QEMU_OPTION_d:
4343 {
4344 int mask;
4345 const CPULogItem *item;
4346
4347 mask = cpu_str_to_log_mask(optarg);
4348 if (!mask) {
4349 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004350 for(item = cpu_log_items; item->mask != 0; item++) {
4351 printf("%-10s %s\n", item->name, item->help);
4352 }
4353 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004354 }
4355 cpu_set_log(mask);
4356 }
4357 break;
4358 case QEMU_OPTION_s:
4359 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4360 break;
4361 case QEMU_OPTION_gdb:
4362 gdbstub_dev = optarg;
4363 break;
4364 case QEMU_OPTION_L:
4365 data_dir = optarg;
4366 break;
4367 case QEMU_OPTION_bios:
4368 bios_name = optarg;
4369 break;
4370 case QEMU_OPTION_singlestep:
4371 singlestep = 1;
4372 break;
4373 case QEMU_OPTION_S:
4374#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004375 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004376#endif
4377 autostart = 0;
4378 break;
4379#ifndef _WIN32
4380 case QEMU_OPTION_k:
4381 keyboard_layout = optarg;
4382 break;
4383#endif
4384 case QEMU_OPTION_localtime:
4385 rtc_utc = 0;
4386 break;
4387 case QEMU_OPTION_vga:
4388 select_vgahw (optarg);
4389 break;
4390#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4391 case QEMU_OPTION_g:
4392 {
4393 const char *p;
4394 int w, h, depth;
4395 p = optarg;
4396 w = strtol(p, (char **)&p, 10);
4397 if (w <= 0) {
4398 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004399 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004400 }
4401 if (*p != 'x')
4402 goto graphic_error;
4403 p++;
4404 h = strtol(p, (char **)&p, 10);
4405 if (h <= 0)
4406 goto graphic_error;
4407 if (*p == 'x') {
4408 p++;
4409 depth = strtol(p, (char **)&p, 10);
4410 if (depth != 8 && depth != 15 && depth != 16 &&
4411 depth != 24 && depth != 32)
4412 goto graphic_error;
4413 } else if (*p == '\0') {
4414 depth = graphic_depth;
4415 } else {
4416 goto graphic_error;
4417 }
4418
4419 graphic_width = w;
4420 graphic_height = h;
4421 graphic_depth = depth;
4422 }
4423 break;
4424#endif
4425 case QEMU_OPTION_echr:
4426 {
4427 char *r;
4428 term_escape_char = strtol(optarg, &r, 0);
4429 if (r == optarg)
4430 printf("Bad argument to echr\n");
4431 break;
4432 }
4433 case QEMU_OPTION_monitor:
4434 monitor_device = optarg;
4435 break;
4436 case QEMU_OPTION_serial:
4437 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004438 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004439 }
4440 serial_devices[serial_device_index] = optarg;
4441 serial_device_index++;
4442 break;
4443 case QEMU_OPTION_watchdog:
4444 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004445 if (i > 0) {
4446 if (i == 1) {
4447 PANIC("Invalid watchdog parameter: %s",
4448 optarg);
4449 } else {
4450 QEMU_EXIT(0);
4451 }
4452 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004453 break;
4454 case QEMU_OPTION_watchdog_action:
4455 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004456 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004457 }
4458 break;
4459 case QEMU_OPTION_virtiocon:
4460 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004461 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004462 }
4463 virtio_consoles[virtio_console_index] = optarg;
4464 virtio_console_index++;
4465 break;
4466 case QEMU_OPTION_parallel:
4467 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004468 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004469 }
4470 parallel_devices[parallel_device_index] = optarg;
4471 parallel_device_index++;
4472 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004473 case QEMU_OPTION_loadvm:
4474 loadvm = optarg;
4475 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004476 case QEMU_OPTION_savevm_on_exit:
4477 savevm_on_exit = optarg;
4478 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004479 case QEMU_OPTION_full_screen:
4480 full_screen = 1;
4481 break;
4482#ifdef CONFIG_SDL
4483 case QEMU_OPTION_no_frame:
4484 no_frame = 1;
4485 break;
4486 case QEMU_OPTION_alt_grab:
4487 alt_grab = 1;
4488 break;
4489 case QEMU_OPTION_no_quit:
4490 no_quit = 1;
4491 break;
4492 case QEMU_OPTION_sdl:
4493 display_type = DT_SDL;
4494 break;
4495#endif
4496 case QEMU_OPTION_pidfile:
4497 pid_file = optarg;
4498 break;
4499#ifdef TARGET_I386
4500 case QEMU_OPTION_win2k_hack:
4501 win2k_install_hack = 1;
4502 break;
4503 case QEMU_OPTION_rtc_td_hack:
4504 rtc_td_hack = 1;
4505 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004506#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004507 case QEMU_OPTION_acpitable:
4508 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004509 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004510 }
4511 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004512#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004513 case QEMU_OPTION_smbios:
4514 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004515 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004516 }
4517 break;
4518#endif
4519#ifdef CONFIG_KQEMU
4520 case QEMU_OPTION_no_kqemu:
4521 kqemu_allowed = 0;
4522 break;
4523 case QEMU_OPTION_kernel_kqemu:
4524 kqemu_allowed = 2;
4525 break;
4526#endif
Jun Nakajima1321c762011-03-04 17:17:45 -08004527#ifdef TARGET_I386
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004528#ifdef CONFIG_KVM
4529 case QEMU_OPTION_enable_kvm:
4530 kvm_allowed = 1;
4531#ifdef CONFIG_KQEMU
4532 kqemu_allowed = 0;
4533#endif
4534 break;
4535#endif
Jun Nakajima1321c762011-03-04 17:17:45 -08004536#endif /* TARGET_I386 */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004537 case QEMU_OPTION_usb:
4538 usb_enabled = 1;
4539 break;
4540 case QEMU_OPTION_usbdevice:
4541 usb_enabled = 1;
4542 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004543 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004544 }
4545 usb_devices[usb_devices_index] = optarg;
4546 usb_devices_index++;
4547 break;
4548 case QEMU_OPTION_smp:
4549 smp_cpus = atoi(optarg);
4550 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004551 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004552 }
4553 break;
4554 case QEMU_OPTION_vnc:
4555 display_type = DT_VNC;
4556 vnc_display = optarg;
4557 break;
4558#ifdef TARGET_I386
4559 case QEMU_OPTION_no_acpi:
4560 acpi_enabled = 0;
4561 break;
4562 case QEMU_OPTION_no_hpet:
4563 no_hpet = 1;
4564 break;
4565 case QEMU_OPTION_no_virtio_balloon:
4566 no_virtio_balloon = 1;
4567 break;
4568#endif
4569 case QEMU_OPTION_no_reboot:
4570 no_reboot = 1;
4571 break;
4572 case QEMU_OPTION_no_shutdown:
4573 no_shutdown = 1;
4574 break;
4575 case QEMU_OPTION_show_cursor:
4576 cursor_hide = 0;
4577 break;
4578 case QEMU_OPTION_uuid:
4579 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004580 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004581 }
4582 break;
4583#ifndef _WIN32
4584 case QEMU_OPTION_daemonize:
4585 daemonize = 1;
4586 break;
4587#endif
4588 case QEMU_OPTION_option_rom:
4589 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004590 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004591 }
4592 option_rom[nb_option_roms] = optarg;
4593 nb_option_roms++;
4594 break;
4595#if defined(TARGET_ARM) || defined(TARGET_M68K)
4596 case QEMU_OPTION_semihosting:
4597 semihosting_enabled = 1;
4598 break;
4599#endif
4600 case QEMU_OPTION_name:
4601 qemu_name = optarg;
4602 break;
4603#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4604 case QEMU_OPTION_prom_env:
4605 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004606 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004607 }
4608 prom_envs[nb_prom_envs] = optarg;
4609 nb_prom_envs++;
4610 break;
4611#endif
4612#ifdef TARGET_ARM
4613 case QEMU_OPTION_old_param:
4614 old_param = 1;
4615 break;
4616#endif
4617 case QEMU_OPTION_clock:
4618 configure_alarms(optarg);
4619 break;
4620 case QEMU_OPTION_startdate:
4621 {
4622 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004623 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004624 if (!strcmp(optarg, "now")) {
4625 rtc_date_offset = -1;
4626 } else {
4627 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4628 &tm.tm_year,
4629 &tm.tm_mon,
4630 &tm.tm_mday,
4631 &tm.tm_hour,
4632 &tm.tm_min,
4633 &tm.tm_sec) == 6) {
4634 /* OK */
4635 } else if (sscanf(optarg, "%d-%d-%d",
4636 &tm.tm_year,
4637 &tm.tm_mon,
4638 &tm.tm_mday) == 3) {
4639 tm.tm_hour = 0;
4640 tm.tm_min = 0;
4641 tm.tm_sec = 0;
4642 } else {
4643 goto date_fail;
4644 }
4645 tm.tm_year -= 1900;
4646 tm.tm_mon--;
4647 rtc_start_date = mktimegm(&tm);
4648 if (rtc_start_date == -1) {
4649 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004650 PANIC("Invalid date format. Valid format are:\n"
4651 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004652 }
4653 rtc_date_offset = time(NULL) - rtc_start_date;
4654 }
4655 }
4656 break;
rich cannings7339b552011-02-16 13:43:44 -08004657
4658 /* -------------------------------------------------------*/
4659 /* User mode network stack restrictions */
4660 case QEMU_OPTION_drop_udp:
4661 slirp_drop_udp();
4662 break;
4663 case QEMU_OPTION_drop_tcp:
4664 slirp_drop_tcp();
4665 break;
4666 case QEMU_OPTION_allow_tcp:
4667 slirp_allow(optarg, IPPROTO_TCP);
4668 break;
4669 case QEMU_OPTION_allow_udp:
4670 slirp_allow(optarg, IPPROTO_UDP);
4671 break;
4672 case QEMU_OPTION_drop_log:
4673 {
4674 FILE* drop_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004675 drop_log_filename = optarg;
4676 drop_log_fd = fopen(optarg, "w+");
rich cannings7339b552011-02-16 13:43:44 -08004677
4678 if (!drop_log_fd) {
4679 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4680 exit(1);
4681 }
4682
4683 slirp_drop_log_fd(drop_log_fd);
4684 }
4685 break;
4686
4687 case QEMU_OPTION_dns_log:
4688 {
4689 FILE* dns_log_fd;
rich canningsd952f282011-03-01 15:40:09 -08004690 dns_log_filename = optarg;
4691 dns_log_fd = fopen(optarg, "wb+");
rich cannings7339b552011-02-16 13:43:44 -08004692
4693 if (dns_log_fd == NULL) {
4694 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4695 exit(1);
4696 }
4697
4698 slirp_dns_log_fd(dns_log_fd);
4699 }
4700 break;
4701
4702
4703 case QEMU_OPTION_max_dns_conns:
4704 {
4705 int max_dns_conns = 0;
4706 if (parse_int(optarg, &max_dns_conns)) {
4707 fprintf(stderr,
4708 "qemu: syntax: -max-dns-conns max_connections\n");
4709 exit(1);
4710 }
4711 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4712 fprintf(stderr,
4713 "Invalid arg for max dns connections: %s\n",
4714 optarg);
4715 exit(1);
4716 }
4717 slirp_set_max_dns_conns(max_dns_conns);
4718 }
4719 break;
4720
4721 case QEMU_OPTION_net_forward:
4722 net_slirp_forward(optarg);
4723 break;
4724 case QEMU_OPTION_net_forward_tcp2sink:
4725 {
4726 SockAddress saddr;
4727
4728 if (parse_host_port(&saddr, optarg)) {
4729 fprintf(stderr,
4730 "Invalid ip/port %s for "
4731 "-forward-dropped-tcp2sink. "
4732 "We expect 'sink_ip:sink_port'\n",
4733 optarg);
4734 exit(1);
4735 }
4736 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4737 saddr.u.inet.port);
4738 }
4739 break;
4740 /* -------------------------------------------------------*/
4741
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004742 case QEMU_OPTION_tb_size:
4743 tb_size = strtol(optarg, NULL, 0);
4744 if (tb_size < 0)
4745 tb_size = 0;
4746 break;
4747 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004748 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004749 break;
4750 case QEMU_OPTION_incoming:
4751 incoming = optarg;
4752 break;
4753#ifndef _WIN32
4754 case QEMU_OPTION_chroot:
4755 chroot_dir = optarg;
4756 break;
4757 case QEMU_OPTION_runas:
4758 run_as = optarg;
4759 break;
4760#endif
4761#ifdef CONFIG_XEN
4762 case QEMU_OPTION_xen_domid:
4763 xen_domid = atoi(optarg);
4764 break;
4765 case QEMU_OPTION_xen_create:
4766 xen_mode = XEN_CREATE;
4767 break;
4768 case QEMU_OPTION_xen_attach:
4769 xen_mode = XEN_ATTACH;
4770 break;
4771#endif
4772
4773
4774 case QEMU_OPTION_mic:
4775 audio_input_source = (char*)optarg;
4776 break;
4777#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004778 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004779 trace_filename = optarg;
4780 tracing = 1;
4781 break;
4782#if 0
4783 case QEMU_OPTION_trace_miss:
4784 trace_cache_miss = 1;
4785 break;
4786 case QEMU_OPTION_trace_addr:
4787 trace_all_addr = 1;
4788 break;
4789#endif
4790 case QEMU_OPTION_tracing:
4791 if (strcmp(optarg, "off") == 0)
4792 tracing = 0;
4793 else if (strcmp(optarg, "on") == 0 && trace_filename)
4794 tracing = 1;
4795 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004796 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004797 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004798 }
4799 break;
4800#if 0
4801 case QEMU_OPTION_dcache_load_miss:
4802 dcache_load_miss_penalty = atoi(optarg);
4803 break;
4804 case QEMU_OPTION_dcache_store_miss:
4805 dcache_store_miss_penalty = atoi(optarg);
4806 break;
4807#endif
4808#endif
4809#ifdef CONFIG_NAND
4810 case QEMU_OPTION_nand:
4811 nand_add_dev(optarg);
4812 break;
4813#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004814 case QEMU_OPTION_android_ports:
4815 android_op_ports = (char*)optarg;
4816 break;
4817
4818 case QEMU_OPTION_android_port:
4819 android_op_port = (char*)optarg;
4820 break;
4821
4822 case QEMU_OPTION_android_report_console:
4823 android_op_report_console = (char*)optarg;
4824 break;
4825
4826 case QEMU_OPTION_http_proxy:
4827 op_http_proxy = (char*)optarg;
4828 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004829
4830 case QEMU_OPTION_charmap:
4831 op_charmap_file = (char*)optarg;
4832 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004833
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004834 case QEMU_OPTION_android_hw:
4835 android_op_hwini = (char*)optarg;
4836 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004837
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004838 case QEMU_OPTION_dns_server:
4839 android_op_dns_server = (char*)optarg;
4840 break;
4841
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004842 case QEMU_OPTION_radio:
4843 android_op_radio = (char*)optarg;
4844 break;
4845
4846 case QEMU_OPTION_gps:
4847 android_op_gps = (char*)optarg;
4848 break;
4849
4850 case QEMU_OPTION_audio:
4851 android_op_audio = (char*)optarg;
4852 break;
4853
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004854 case QEMU_OPTION_cpu_delay:
4855 android_op_cpu_delay = (char*)optarg;
4856 break;
4857
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004858 case QEMU_OPTION_show_kernel:
4859 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4860 break;
4861
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004862#ifdef CONFIG_NAND_LIMITS
4863 case QEMU_OPTION_nand_limits:
4864 android_op_nand_limits = (char*)optarg;
4865 break;
4866#endif // CONFIG_NAND_LIMITS
4867
4868 case QEMU_OPTION_netspeed:
4869 android_op_netspeed = (char*)optarg;
4870 break;
4871
4872 case QEMU_OPTION_netdelay:
4873 android_op_netdelay = (char*)optarg;
4874 break;
4875
4876 case QEMU_OPTION_netfast:
4877 android_op_netfast = 1;
4878 break;
4879
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004880 case QEMU_OPTION_tcpdump:
4881 android_op_tcpdump = (char*)optarg;
4882 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004883
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004884 case QEMU_OPTION_boot_property:
4885 boot_property_parse_option((char*)optarg);
4886 break;
4887
4888 case QEMU_OPTION_lcd_density:
4889 android_op_lcd_density = (char*)optarg;
4890 break;
4891
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004892 case QEMU_OPTION_ui_port:
4893 android_op_ui_port = (char*)optarg;
4894 break;
4895
4896 case QEMU_OPTION_ui_settings:
4897 android_op_ui_settings = (char*)optarg;
4898 break;
4899
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004900 case QEMU_OPTION_audio_test_out:
4901 android_audio_test_start_out();
4902 break;
4903
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004904 case QEMU_OPTION_android_avdname:
4905 android_op_avd_name = (char*)optarg;
4906 break;
4907
4908 case QEMU_OPTION_timezone:
4909 if (timezone_set((char*)optarg)) {
4910 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4911 (char*)optarg);
4912 }
4913 break;
4914
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004915#ifdef CONFIG_MEMCHECK
4916 case QEMU_OPTION_android_memcheck:
4917 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004918 /* This will set ro.kernel.memcheck system property
4919 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004920 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004921 break;
4922#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004923
4924 case QEMU_OPTION_snapshot_no_time_update:
4925 android_snapshot_update_time = 0;
4926 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004927 }
4928 }
4929 }
4930
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004931 /* Initialize character map. */
4932 if (android_charmap_setup(op_charmap_file)) {
4933 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004934 PANIC(
4935 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004936 op_charmap_file);
4937 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004938 PANIC(
4939 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004940 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004941 }
4942
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004943 /* If no data_dir is specified then try to find it relative to the
4944 executable path. */
4945 if (!data_dir) {
4946 data_dir = find_datadir(argv[0]);
4947 }
4948 /* If all else fails use the install patch specified when building. */
4949 if (!data_dir) {
4950 data_dir = CONFIG_QEMU_SHAREDIR;
4951 }
4952
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004953 if (!android_op_hwini) {
4954 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004955 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004956 hw_ini = iniFile_newFromFile(android_op_hwini);
4957 if (hw_ini == NULL) {
4958 PANIC("Could not find %s file.", android_op_hwini);
4959 }
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004960
4961 androidHwConfig_init(android_hw, 0);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004962 androidHwConfig_read(android_hw, hw_ini);
David 'Digit' Turnerb64325d2011-03-22 16:07:01 +01004963
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004964 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004965
4966 {
4967 int width = android_hw->hw_lcd_width;
4968 int height = android_hw->hw_lcd_height;
4969 int depth = android_hw->hw_lcd_depth;
4970
4971 /* A bit of sanity checking */
4972 if (width <= 0 || height <= 0 ||
4973 (depth != 16 && depth != 32) ||
4974 (((width|height) & 3) != 0) )
4975 {
4976 PANIC("Invalid display configuration (%d,%d,%d)",
4977 width, height, depth);
4978 }
4979 android_display_width = width;
4980 android_display_height = height;
4981 android_display_bpp = depth;
4982 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004983
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004984#ifdef CONFIG_NAND_LIMITS
4985 /* Init nand stuff. */
4986 if (android_op_nand_limits) {
4987 parse_nand_limits(android_op_nand_limits);
4988 }
4989#endif // CONFIG_NAND_LIMITS
4990
David 'Digit' Turner53eb66d2011-03-01 14:58:23 +01004991 /* Initialize AVD name from hardware configuration if needed */
4992 if (!android_op_avd_name) {
4993 if (android_hw->avd_name && *android_hw->avd_name) {
4994 android_op_avd_name = android_hw->avd_name;
4995 VERBOSE_PRINT(init,"AVD Name: %s", android_op_avd_name);
4996 }
4997 }
4998
David 'Digit' Turner40841b22011-03-01 14:04:00 +01004999 /* Initialize system partition image */
5000 {
5001 char tmp[PATH_MAX+32];
5002 const char* sysImage = android_hw->disk_systemPartition_path;
5003 const char* initImage = android_hw->disk_systemPartition_initPath;
5004 uint64_t sysBytes = android_hw->disk_systemPartition_size;
5005
5006 if (sysBytes == 0) {
5007 PANIC("Invalid system partition size: %" PRUd64, sysBytes);
5008 }
5009
5010 snprintf(tmp,sizeof(tmp),"system,size=0x%" PRUx64, sysBytes);
5011
5012 if (sysImage && *sysImage) {
5013 if (filelock_create(sysImage) == NULL) {
5014 fprintf(stderr,"WARNING: System image already in use, changes will not persist!\n");
5015 /* If there is no file= parameters, nand_add_dev will create
5016 * a temporary file to back the partition image. */
5017 } else {
5018 pstrcat(tmp,sizeof(tmp),",file=");
5019 pstrcat(tmp,sizeof(tmp),sysImage);
5020 }
5021 }
5022 if (initImage && *initImage) {
5023 if (!path_exists(initImage)) {
5024 PANIC("Invalid initial system image path: %s", initImage);
5025 }
5026 pstrcat(tmp,sizeof(tmp),",initfile=");
5027 pstrcat(tmp,sizeof(tmp),initImage);
5028 } else {
5029 PANIC("Missing initial system image path!");
5030 }
5031 nand_add_dev(tmp);
5032 }
5033
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005034 /* Initialize data partition image */
5035 {
5036 char tmp[PATH_MAX+32];
5037 const char* dataImage = android_hw->disk_dataPartition_path;
5038 const char* initImage = android_hw->disk_dataPartition_initPath;
5039 uint64_t dataBytes = android_hw->disk_dataPartition_size;
5040
5041 if (dataBytes == 0) {
5042 PANIC("Invalid data partition size: %" PRUd64, dataBytes);
5043 }
5044
5045 snprintf(tmp,sizeof(tmp),"userdata,size=0x%" PRUx64, dataBytes);
5046
5047 if (dataImage && *dataImage) {
5048 if (filelock_create(dataImage) == NULL) {
5049 fprintf(stderr, "WARNING: Data partition already in use. Changes will not persist!\n");
5050 /* Note: if there is no file= parameters, nand_add_dev() will
5051 * create a temporary file to back the partition image. */
5052 } else {
5053 /* Create the file if needed */
5054 if (!path_exists(dataImage)) {
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005055 if (path_empty_file(dataImage) < 0) {
5056 PANIC("Could not create data image file %s: %s", dataImage, strerror(errno));
5057 }
David 'Digit' Turnerfd59c332011-03-01 00:48:52 +01005058 }
5059 pstrcat(tmp, sizeof(tmp), ",file=");
5060 pstrcat(tmp, sizeof(tmp), dataImage);
5061 }
5062 }
5063 if (initImage && *initImage) {
5064 pstrcat(tmp, sizeof(tmp), ",initfile=");
5065 pstrcat(tmp, sizeof(tmp), initImage);
5066 }
5067 nand_add_dev(tmp);
5068 }
5069
David 'Digit' Turner48a3c662011-03-01 14:03:20 +01005070 /* Init SD-Card stuff. For Android, it is always hda */
5071 /* If the -hda option was used, ignore the Android-provided one */
5072 if (hda_opts == NULL) {
5073 const char* sdPath = android_hw->hw_sdCard_path;
5074 if (sdPath && *sdPath) {
5075 if (!path_exists(sdPath)) {
5076 fprintf(stderr, "WARNING: SD Card image is missing: %s\n", sdPath);
5077 } else if (filelock_create(sdPath) == NULL) {
5078 fprintf(stderr, "WARNING: SD Card image already in use: %s\n", sdPath);
5079 } else {
5080 /* Successful locking */
5081 hda_opts = drive_add(sdPath, HD_ALIAS, 0);
5082 }
5083 }
5084 }
5085
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005086 if (hdb_opts == NULL) {
5087 const char* spath = android_hw->disk_snapStorage_path;
5088 if (spath && *spath) {
5089 if (!path_exists(spath)) {
5090 PANIC("Snapshot storage file does not exist: %s", spath);
5091 }
5092 if (filelock_create(spath) == NULL) {
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005093 PANIC("Snapshot storage already in use: %s", spath);
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005094 }
5095 hdb_opts = drive_add(spath, HD_ALIAS, 1);
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005096 /* VERY IMPORTANT:
5097 * Set this property or the file will be mounted with O_DIRECT,
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02005098 * which will slow down snapshot saving x100 !
David 'Digit' Turner4297b822011-05-04 19:18:15 +02005099 */
David 'Digit' Turner9fb360e2011-05-04 22:01:28 +02005100 qemu_opt_set(hdb_opts, "cache", "unsafe");
David 'Digit' Turner5f64b872011-02-28 23:23:05 +01005101 }
5102 }
5103
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005104 /* Set the VM's max heap size, passed as a boot property */
5105 if (android_hw->vm_heapSize > 0) {
5106 char tmp[64];
5107 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
5108 boot_property_add("dalvik.vm.heapsize",tmp);
5109 }
5110
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005111 /* Initialize net speed and delays stuff. */
5112 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005113 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005114 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005115 }
5116
5117 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005118 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005119 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07005120 }
5121
5122 if (android_op_netfast) {
5123 qemu_net_download_speed = 0;
5124 qemu_net_upload_speed = 0;
5125 qemu_net_min_latency = 0;
5126 qemu_net_max_latency = 0;
5127 }
5128
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005129 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005130 if (android_hw->hw_lcd_density) {
5131 long density = android_hw->hw_lcd_density;
5132 if (density <= 0) {
5133 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07005134 }
5135 hwLcd_setBootProperty(density);
5136 }
5137
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07005138 /* Initialize TCP dump */
5139 if (android_op_tcpdump) {
5140 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
5141 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
5142 }
5143 }
5144
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005145 /* Initialize modem */
5146 if (android_op_radio) {
5147 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
5148 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005149 PANIC("unsupported character device specification: %s\n"
5150 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005151 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005152 }
5153 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
5154 } else if (android_hw->hw_gsmModem != 0 ) {
5155 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005156 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005157 }
5158 }
5159
5160 /* Initialize GPS */
5161 if (android_op_gps) {
5162 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
5163 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005164 PANIC("unsupported character device specification: %s\n"
5165 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005166 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005167 }
5168 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
5169 } else if (android_hw->hw_gps != 0) {
5170 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005171 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005172 }
5173 }
5174
5175 /* Initialize audio. */
5176 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005177 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005178 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005179 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005180 }
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005181 }
5182
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005183 /* Initialize OpenGLES emulation */
David 'Digit' Turnerd4d688e2011-04-26 18:09:17 +02005184 //android_hw_opengles_init();
David 'Digit' Turner5b481492011-04-11 17:37:32 +02005185
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005186 if (android_op_cpu_delay) {
5187 char* end;
5188 long delay = strtol(android_op_cpu_delay, &end, 0);
5189 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005190 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07005191 }
5192 if (delay > 0)
5193 delay = (1000-delay);
5194
5195 qemu_cpu_delay = (int) delay;
5196 }
5197
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005198 if (android_op_dns_server) {
5199 char* x = strchr(android_op_dns_server, ',');
5200 dns_count = 0;
5201 if (x == NULL)
5202 {
5203 if ( add_dns_server( android_op_dns_server ) == 0 )
5204 dns_count = 1;
5205 }
5206 else
5207 {
5208 x = android_op_dns_server;
5209 while (*x) {
5210 char* y = strchr(x, ',');
5211
5212 if (y != NULL) {
5213 *y = 0;
5214 y++;
5215 } else {
5216 y = x + strlen(x);
5217 }
5218
5219 if (y > x && add_dns_server( x ) == 0) {
5220 dns_count += 1;
5221 }
5222 x = y;
5223 }
5224 }
5225 if (dns_count == 0)
5226 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
5227 }
5228
5229 if (dns_count == 0)
5230 dns_count = slirp_get_system_dns_servers();
5231 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005232 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005233 }
5234
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07005235#ifdef CONFIG_MEMCHECK
5236 if (android_op_memcheck) {
5237 memcheck_init(android_op_memcheck);
5238 }
5239#endif // CONFIG_MEMCHECK
5240
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005241 /* Initialize cache partition, if any */
5242 if (android_hw->disk_cachePartition != 0) {
5243 char tmp[PATH_MAX+32];
5244 const char* partPath = android_hw->disk_cachePartition_path;
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005245 uint64_t partSize = android_hw->disk_cachePartition_size;
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005246
David 'Digit' Turnere8ab08c2011-03-15 23:08:50 +01005247 snprintf(tmp,sizeof(tmp),"cache,size=0x%" PRUx64, partSize);
5248
5249 if (partPath && *partPath && strcmp(partPath, "<temp>") != 0) {
5250 if (filelock_create(partPath) == NULL) {
5251 fprintf(stderr, "WARNING: Cache partition already in use. Changes will not persist!\n");
5252 /* Note: if there is no file= parameters, nand_add_dev() will
5253 * create a temporary file to back the partition image. */
5254 } else {
5255 /* Create the file if needed */
5256 if (!path_exists(partPath)) {
5257 if (path_empty_file(partPath) < 0) {
5258 PANIC("Could not create cache image file %s: %s", partPath, strerror(errno));
5259 }
5260 }
5261 pstrcat(tmp, sizeof(tmp), ",file=");
5262 pstrcat(tmp, sizeof(tmp), partPath);
5263 }
David 'Digit' Turnerc480cca2011-02-25 16:43:34 +01005264 }
5265 nand_add_dev(tmp);
5266 }
5267
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005268 /* We always force qemu=1 when running inside QEMU */
5269 stralloc_add_str(kernel_params, " qemu=1");
5270
5271 /* We always initialize the first serial port for the android-kmsg
5272 * character device (used to send kernel messages) */
5273 serial_hds_add_at(0, "android-kmsg");
5274 stralloc_add_str(kernel_params, " console=ttyS0");
5275
5276 /* We always initialize the second serial port for the android-qemud
5277 * character device as well */
5278 serial_hds_add_at(1, "android-qemud");
5279 stralloc_add_str(kernel_params, " android.qemud=ttyS1");
5280
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005281#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5282 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005283 PANIC(
5284 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005285 }
5286#endif
5287
5288 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5289 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005290 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
5291 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005292 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005293 }
5294
5295 if (display_type == DT_NOGRAPHIC) {
5296 if (serial_device_index == 0)
5297 serial_devices[0] = "stdio";
5298 if (parallel_device_index == 0)
5299 parallel_devices[0] = "null";
5300 if (strncmp(monitor_device, "vc", 2) == 0)
5301 monitor_device = "stdio";
5302 }
5303
5304#ifndef _WIN32
5305 if (daemonize) {
5306 pid_t pid;
5307
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005308 if (pipe(fds) == -1) {
5309 PANIC("Unable to aquire pidfile");
5310 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005311
5312 pid = fork();
5313 if (pid > 0) {
5314 uint8_t status;
5315 ssize_t len;
5316
5317 close(fds[1]);
5318
5319 again:
5320 len = read(fds[0], &status, 1);
5321 if (len == -1 && (errno == EINTR))
5322 goto again;
5323
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005324 if (len != 1) {
5325 PANIC("Error when aquiring pidfile");
5326 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005327 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005328 PANIC("Could not acquire pidfile");
5329 } else {
5330 QEMU_EXIT(0);
5331 }
5332 } else if (pid < 0) {
5333 PANIC("Unable to daemonize");
5334 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005335
5336 setsid();
5337
5338 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005339 if (pid > 0) {
5340 QEMU_EXIT(0);
5341 } else if (pid < 0) {
5342 PANIC("Could not acquire pid file");
5343 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005344
5345 umask(027);
5346
5347 signal(SIGTSTP, SIG_IGN);
5348 signal(SIGTTOU, SIG_IGN);
5349 signal(SIGTTIN, SIG_IGN);
5350 }
5351
5352 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5353 if (daemonize) {
5354 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005355 int ret;
5356 do {
5357 ret = write(fds[1], &status, 1);
5358 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005359 PANIC("Could not acquire pid file");
5360 } else {
5361 PANIC("Could not acquire pid file");
5362 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005363 }
5364#endif
5365
5366#ifdef CONFIG_KQEMU
5367 if (smp_cpus > 1)
5368 kqemu_allowed = 0;
5369#endif
5370 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005371 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005372 }
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005373
5374 if (kernel_filename == NULL) {
5375 kernel_filename = android_hw->kernel_path;
5376 }
5377 if (initrd_filename == NULL) {
5378 initrd_filename = android_hw->disk_ramdisk_path;
5379 }
5380
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005381 linux_boot = (kernel_filename != NULL);
5382 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5383
5384 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005385 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005386 }
5387
5388 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005389 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005390 }
5391
5392 /* boot to floppy or the default cd if no hard disk defined yet */
5393 if (!boot_devices[0]) {
5394 boot_devices = "cad";
5395 }
5396 setvbuf(stdout, NULL, _IOLBF, 0);
5397
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005398 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005399 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005400 }
David Turner6a9ef172010-09-09 22:54:36 +02005401 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005402
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005403 /* init network clients */
5404 if (nb_net_clients == 0) {
5405 /* if no clients, we use a default config */
5406 net_clients[nb_net_clients++] = "nic";
5407#ifdef CONFIG_SLIRP
5408 net_clients[nb_net_clients++] = "user";
5409#endif
5410 }
5411
5412 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005413 if (net_client_parse(net_clients[i]) < 0) {
5414 PANIC("Unable to parse net clients");
5415 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005416 }
5417 net_client_check();
5418
5419#ifdef TARGET_I386
5420 /* XXX: this should be moved in the PC machine instantiation code */
5421 if (net_boot != 0) {
5422 int netroms = 0;
5423 for (i = 0; i < nb_nics && i < 4; i++) {
5424 const char *model = nd_table[i].model;
5425 char buf[1024];
5426 char *filename;
5427 if (net_boot & (1 << i)) {
5428 if (model == NULL)
5429 model = "ne2k_pci";
5430 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5431 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5432 if (filename && get_image_size(filename) > 0) {
5433 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005434 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005435 }
5436 option_rom[nb_option_roms] = qemu_strdup(buf);
5437 nb_option_roms++;
5438 netroms++;
5439 }
5440 if (filename) {
5441 qemu_free(filename);
5442 }
5443 }
5444 }
5445 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005446 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005447 }
5448 }
5449#endif
5450
5451 /* init the bluetooth world */
5452 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005453 if (bt_parse(bt_opts[i])) {
5454 PANIC("Unable to parse bluetooth options");
5455 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005456
5457 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005458 if (ram_size == 0) {
5459 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5460 if (ram_size == 0) {
5461 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5462 }
5463 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005464
5465#ifdef CONFIG_KQEMU
5466 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5467 guest ram allocation. It needs to go away. */
5468 if (kqemu_allowed) {
5469 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5470 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5471 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005472 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005473 }
5474 }
5475#endif
5476
rich canningsd952f282011-03-01 15:40:09 -08005477#ifndef _WIN32
5478 init_qemu_clear_logs_sig();
5479#endif
5480
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005481 /* init the dynamic translator */
5482 cpu_exec_init_all(tb_size * 1024 * 1024);
5483
5484 bdrv_init();
5485
5486 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005487#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005488 if (nb_drives_opt < MAX_DRIVES)
5489 drive_add(NULL, CDROM_ALIAS);
5490
5491 /* we always create at least one floppy */
5492
5493 if (nb_drives_opt < MAX_DRIVES)
5494 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005495 /* we always create one sd slot, even if no card is in it */
5496
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005497 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005498 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005499 }
5500#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005501
5502 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005503 if (snapshot)
5504 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5505 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5506 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005507
David Turner6a9ef172010-09-09 22:54:36 +02005508 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005509 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5510
5511#ifndef _WIN32
5512 /* must be after terminal init, SDL library changes signal handlers */
5513 sighandler_setup();
5514#endif
5515
5516 /* Maintain compatibility with multiple stdio monitors */
5517 if (!strcmp(monitor_device,"stdio")) {
5518 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5519 const char *devname = serial_devices[i];
5520 if (devname && !strcmp(devname,"mon:stdio")) {
5521 monitor_device = NULL;
5522 break;
5523 } else if (devname && !strcmp(devname,"stdio")) {
5524 monitor_device = NULL;
5525 serial_devices[i] = "mon:stdio";
5526 break;
5527 }
5528 }
5529 }
5530
5531 if (nb_numa_nodes > 0) {
5532 int i;
5533
5534 if (nb_numa_nodes > smp_cpus) {
5535 nb_numa_nodes = smp_cpus;
5536 }
5537
5538 /* If no memory size if given for any node, assume the default case
5539 * and distribute the available memory equally across all nodes
5540 */
5541 for (i = 0; i < nb_numa_nodes; i++) {
5542 if (node_mem[i] != 0)
5543 break;
5544 }
5545 if (i == nb_numa_nodes) {
5546 uint64_t usedmem = 0;
5547
5548 /* On Linux, the each node's border has to be 8MB aligned,
5549 * the final node gets the rest.
5550 */
5551 for (i = 0; i < nb_numa_nodes - 1; i++) {
5552 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5553 usedmem += node_mem[i];
5554 }
5555 node_mem[i] = ram_size - usedmem;
5556 }
5557
5558 for (i = 0; i < nb_numa_nodes; i++) {
5559 if (node_cpumask[i] != 0)
5560 break;
5561 }
5562 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5563 * must cope with this anyway, because there are BIOSes out there in
5564 * real machines which also use this scheme.
5565 */
5566 if (i == nb_numa_nodes) {
5567 for (i = 0; i < smp_cpus; i++) {
5568 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5569 }
5570 }
5571 }
5572
5573 if (kvm_enabled()) {
5574 int ret;
5575
5576 ret = kvm_init(smp_cpus);
5577 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005578 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005579 }
5580 }
5581
5582 if (monitor_device) {
5583 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5584 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005585 PANIC("qemu: could not open monitor device '%s'",
5586 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005587 }
5588 }
5589
5590 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005591 serial_hds_add(serial_devices[i]);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005592 }
5593
5594 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5595 const char *devname = parallel_devices[i];
5596 if (devname && strcmp(devname, "none")) {
5597 char label[32];
5598 snprintf(label, sizeof(label), "parallel%d", i);
5599 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5600 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005601 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005602 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005603 }
5604 }
5605 }
5606
5607 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5608 const char *devname = virtio_consoles[i];
5609 if (devname && strcmp(devname, "none")) {
5610 char label[32];
5611 snprintf(label, sizeof(label), "virtcon%d", i);
5612 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5613 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005614 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005615 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005616 }
5617 }
5618 }
5619
5620 module_call_init(MODULE_INIT_DEVICE);
5621
5622
5623#ifdef CONFIG_TRACE
5624 if (trace_filename) {
5625 trace_init(trace_filename);
5626#if 0
5627 // We don't need the dcache code until we can get load and store tracing
5628 // working again.
5629 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5630 dcache_replace_policy, dcache_load_miss_penalty,
5631 dcache_store_miss_penalty);
5632#endif
5633 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5634 }
5635#endif
5636
David 'Digit' Turner2d238fd2011-03-25 10:34:47 +01005637 /* Check the CPU Architecture value */
5638#if defined(TARGET_ARM)
5639 if (strcmp(android_hw->hw_cpu_arch,"arm") != 0) {
5640 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'arm'\n",
5641 android_hw->hw_cpu_arch);
5642 exit(1);
5643 }
5644#elif defined(TARGET_X86)
5645 if (strcmp(android_hw->hw_cpu_arch,"x86") != 0) {
5646 fprintf(stderr, "-- Invalid CPU architecture: %s, expected 'x86'\n",
5647 android_hw->hw_cpu_arch);
5648 exit(1);
5649 }
5650#endif
5651
5652 /* Grab CPU model if provided in hardware.ini */
5653 if ( !cpu_model
5654 && android_hw->hw_cpu_model
5655 && android_hw->hw_cpu_model[0] != '\0')
5656 {
5657 cpu_model = android_hw->hw_cpu_model;
5658 }
5659
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005660 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005661 * collected during initialization.
5662 *
5663 * The order is the following:
5664 * - parameters from the hw configuration (kernel.parameters)
5665 * - additionnal parameters from options (e.g. -memcheck)
5666 * - the -append parameters.
5667 */
5668 {
5669 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005670
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005671 if (android_hw->kernel_parameters) {
David 'Digit' Turner62c7ae52011-03-08 15:46:53 +01005672 stralloc_add_c(kernel_params, ' ');
David 'Digit' Turner0b019492011-03-01 14:02:42 +01005673 stralloc_add_str(kernel_params, android_hw->kernel_parameters);
5674 }
5675
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005676 /* If not empty, kernel_config always contains a leading space */
5677 stralloc_append(kernel_params, kernel_config);
5678
5679 if (*kernel_cmdline) {
5680 stralloc_add_c(kernel_params, ' ');
5681 stralloc_add_str(kernel_params, kernel_cmdline);
5682 }
5683
David 'Digit' Turner062dd6a2011-03-01 14:50:07 +01005684 /* Remove any leading/trailing spaces */
5685 stralloc_strip(kernel_params);
5686
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005687 kernel_parameters = stralloc_cstr(kernel_params);
5688 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5689
5690 machine->init(ram_size,
5691 boot_devices,
5692 kernel_filename,
5693 kernel_parameters,
5694 initrd_filename,
5695 cpu_model);
5696
5697 stralloc_reset(kernel_params);
5698 stralloc_reset(kernel_config);
5699 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005700
5701
5702 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5703 for (i = 0; i < nb_numa_nodes; i++) {
5704 if (node_cpumask[i] & (1 << env->cpu_index)) {
5705 env->numa_node = i;
5706 }
5707 }
5708 }
5709
5710 current_machine = machine;
5711
5712 /* Set KVM's vcpu state to qemu's initial CPUState. */
5713 if (kvm_enabled()) {
5714 int ret;
5715
5716 ret = kvm_sync_vcpus();
5717 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005718 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005719 }
5720 }
5721
5722 /* init USB devices */
5723 if (usb_enabled) {
5724 for(i = 0; i < usb_devices_index; i++) {
5725 if (usb_device_add(usb_devices[i], 0) < 0) {
5726 fprintf(stderr, "Warning: could not add USB device %s\n",
5727 usb_devices[i]);
5728 }
5729 }
5730 }
5731
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005732 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005733 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005734
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005735 /* Initialize display from the command line parameters. */
5736 android_display_reset(ds,
5737 android_display_width,
5738 android_display_height,
5739 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005740
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005741 if (display_type == DT_DEFAULT) {
5742#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5743 display_type = DT_SDL;
5744#else
5745 display_type = DT_VNC;
5746 vnc_display = "localhost:0,to=99";
5747 show_vnc_port = 1;
5748#endif
5749 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005750
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005751
5752 switch (display_type) {
5753 case DT_NOGRAPHIC:
5754 break;
5755#if defined(CONFIG_CURSES)
5756 case DT_CURSES:
5757 curses_display_init(ds, full_screen);
5758 break;
5759#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005760#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005761 case DT_SDL:
5762 sdl_display_init(ds, full_screen, no_frame);
5763 break;
5764#elif defined(CONFIG_COCOA)
5765 case DT_SDL:
5766 cocoa_display_init(ds, full_screen);
5767 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005768#elif defined(CONFIG_STANDALONE_CORE)
5769 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005770 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005771 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005772#endif
5773 case DT_VNC:
5774 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005775 if (vnc_display_open(ds, vnc_display) < 0) {
5776 PANIC("Unable to initialize VNC display");
5777 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005778
5779 if (show_vnc_port) {
5780 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5781 }
5782 break;
5783 default:
5784 break;
5785 }
5786 dpy_resize(ds);
5787
5788 dcl = ds->listeners;
5789 while (dcl != NULL) {
5790 if (dcl->dpy_refresh != NULL) {
5791 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5792 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5793 }
5794 dcl = dcl->next;
5795 }
5796
5797 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5798 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5799 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5800 }
5801
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005802 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005803 qemu_chr_initial_reset();
5804
5805 if (monitor_device && monitor_hd)
5806 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5807
5808 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5809 const char *devname = serial_devices[i];
5810 if (devname && strcmp(devname, "none")) {
5811 if (strstart(devname, "vc", 0))
5812 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5813 }
5814 }
5815
5816 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5817 const char *devname = parallel_devices[i];
5818 if (devname && strcmp(devname, "none")) {
5819 if (strstart(devname, "vc", 0))
5820 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5821 }
5822 }
5823
5824 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5825 const char *devname = virtio_consoles[i];
5826 if (virtcon_hds[i] && devname) {
5827 if (strstart(devname, "vc", 0))
5828 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5829 }
5830 }
5831
5832 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005833 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005834 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005835 }
5836
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005837 /* call android-specific setup function */
5838 android_emulation_setup();
5839
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005840#if !defined(CONFIG_STANDALONE_CORE)
5841 // For the standalone emulator (UI+core in one executable) we need to
5842 // set the window title here.
5843 android_emulator_set_base_port(android_base_port);
5844#endif
5845
Ot ten Thije871da2a2010-09-20 10:29:22 +01005846 if (loadvm)
5847 do_loadvm(cur_mon, loadvm);
5848
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005849 if (incoming) {
5850 autostart = 0; /* fixme how to deal with -daemonize */
5851 qemu_start_incoming_migration(incoming);
5852 }
5853
5854 if (autostart)
5855 vm_start();
5856
5857#ifndef _WIN32
5858 if (daemonize) {
5859 uint8_t status = 0;
5860 ssize_t len;
5861
5862 again1:
5863 len = write(fds[1], &status, 1);
5864 if (len == -1 && (errno == EINTR))
5865 goto again1;
5866
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005867 if (len != 1) {
5868 PANIC("Unable to daemonize");
5869 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005870
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005871 if (chdir("/")) {
5872 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005873 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005874 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005875 TFR(fd = open("/dev/null", O_RDWR));
5876 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005877 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005878 }
5879
5880 if (run_as) {
5881 pwd = getpwnam(run_as);
5882 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005883 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005884 }
5885 }
5886
5887 if (chroot_dir) {
5888 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005889 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005890 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005891 if (chdir("/")) {
5892 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005893 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005894 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005895 }
5896
5897 if (run_as) {
5898 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005899 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005900 }
5901 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005902 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005903 }
5904 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005905 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005906 }
5907 }
5908
5909 if (daemonize) {
5910 dup2(fd, 0);
5911 dup2(fd, 1);
5912 dup2(fd, 2);
5913
5914 close(fd);
5915 }
5916#endif
5917
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005918#ifdef CONFIG_ANDROID
5919 // This will notify the UI that the core is successfuly initialized
5920 android_core_init_completed();
5921#endif // CONFIG_ANDROID
5922
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005923 main_loop();
5924 quit_timers();
5925 net_cleanup();
5926 android_emulation_teardown();
5927 return 0;
5928}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005929
5930void
5931android_emulation_teardown(void)
5932{
5933 android_charmap_done();
5934}