blob: ff21db94c7a9fdf7ae7971b7302b836b0fd3b7e6 [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"
54#include "android/hw-qemud.h"
55#include "android/hw-kmsg.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070056#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070057#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070058#include "android/utils/bufprint.h"
David 'Digit' Turner5f824112011-03-01 14:00:26 +010059#include "android/utils/debug.h"
60#include "android/utils/stralloc.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080061#include "android/display-core.h"
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -080062#include "android/utils/timezone.h"
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +010063#include "android/snapshot.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070064#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070065#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070066
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070067#ifdef CONFIG_MEMCHECK
68#include "memcheck/memcheck.h"
69#endif // CONFIG_MEMCHECK
70
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070071#include <unistd.h>
72#include <fcntl.h>
73#include <signal.h>
74#include <time.h>
75#include <errno.h>
76#include <sys/time.h>
77#include <zlib.h>
78
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070079/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070080#include "config-host.h"
81
82#ifndef _WIN32
83#include <libgen.h>
84#include <pwd.h>
85#include <sys/times.h>
86#include <sys/wait.h>
87#include <termios.h>
88#include <sys/mman.h>
89#include <sys/ioctl.h>
90#include <sys/resource.h>
91#include <sys/socket.h>
92#include <netinet/in.h>
93#include <net/if.h>
94#if defined(__NetBSD__)
95#include <net/if_tap.h>
96#endif
97#ifdef __linux__
98#include <linux/if_tun.h>
99#endif
100#include <arpa/inet.h>
101#include <dirent.h>
102#include <netdb.h>
103#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700104#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700105#include <sys/stat.h>
106#if defined(__FreeBSD__) || defined(__DragonFly__)
107#include <libutil.h>
108#else
109#include <util.h>
110#endif
111#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
112#include <freebsd/stdlib.h>
113#else
114#ifdef __linux__
115#include <pty.h>
116#include <malloc.h>
117#include <linux/rtc.h>
118
119/* For the benefit of older linux systems which don't supply it,
120 we use a local copy of hpet.h. */
121/* #include <linux/hpet.h> */
122#include "hpet.h"
123
124#include <linux/ppdev.h>
125#include <linux/parport.h>
126#endif
127#ifdef __sun__
128#include <sys/stat.h>
129#include <sys/ethernet.h>
130#include <sys/sockio.h>
131#include <netinet/arp.h>
132#include <netinet/in.h>
133#include <netinet/in_systm.h>
134#include <netinet/ip.h>
135#include <netinet/ip_icmp.h> // must come after ip.h
136#include <netinet/udp.h>
137#include <netinet/tcp.h>
138#include <net/if.h>
139#include <syslog.h>
140#include <stropts.h>
141#endif
142#endif
143#endif
144
145#if defined(__OpenBSD__)
146#include <util.h>
147#endif
148
149#if defined(CONFIG_VDE)
150#include <libvdeplug.h>
151#endif
152
153#ifdef _WIN32
154#include <windows.h>
155#include <malloc.h>
156#include <sys/timeb.h>
157#include <mmsystem.h>
158#define getopt_long_only getopt_long
159#define memalign(align, size) malloc(size)
160#endif
161
162
163#ifdef CONFIG_COCOA
164#undef main
165#define main qemu_main
166#endif /* CONFIG_COCOA */
167
168#include "hw/hw.h"
169#include "hw/boards.h"
170#include "hw/usb.h"
171#include "hw/pcmcia.h"
172#include "hw/pc.h"
173#include "hw/audiodev.h"
174#include "hw/isa.h"
175#include "hw/baum.h"
176#include "hw/bt.h"
177#include "hw/watchdog.h"
178#include "hw/smbios.h"
179#include "hw/xen.h"
180#include "bt-host.h"
181#include "net.h"
182#include "monitor.h"
183#include "console.h"
184#include "sysemu.h"
185#include "gdbstub.h"
186#include "qemu-timer.h"
187#include "qemu-char.h"
188#include "cache-utils.h"
189#include "block.h"
190#include "dma.h"
191#include "audio/audio.h"
192#include "migration.h"
193#include "kvm.h"
194#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700195#include "android/hw-lcd.h"
196#include "android/boot-properties.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700197#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100198#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700199
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700200#ifdef CONFIG_STANDALONE_CORE
201/* Verbose value used by the standalone emulator core (without UI) */
202unsigned long android_verbose;
203#endif // CONFIG_STANDALONE_CORE
204
Vladimir Chtchetkine57584042011-01-20 16:15:30 -0800205#if !defined(CONFIG_STANDALONE_CORE)
206/* in android/qemulator.c */
207extern void android_emulator_set_base_port(int port);
208#endif
209
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700210#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700211#undef main
212#define main qemu_main
213#endif
214
215#include "disas.h"
216
217#include "exec-all.h"
218
219#ifdef CONFIG_TRACE
220#include "trace.h"
221#include "dcache.h"
222#endif
223
224#include "qemu_socket.h"
225
226#if defined(CONFIG_SLIRP)
227#include "libslirp.h"
228#endif
229
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700230
231
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700232#define DEFAULT_RAM_SIZE 128
233
234/* Max number of USB devices that can be specified on the commandline. */
235#define MAX_USB_CMDLINE 8
236
237/* Max number of bluetooth switches on the commandline. */
238#define MAX_BT_CMDLINE 10
239
240/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700241
242static const char *data_dir;
243const char *bios_name = NULL;
244static void *ioport_opaque[MAX_IOPORTS];
245static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
246static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100247#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700248/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
249 to store the VM snapshots */
250DriveInfo drives_table[MAX_DRIVES+1];
251int nb_drives;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100252#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700253enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700254DisplayType display_type = DT_DEFAULT;
255const char* keyboard_layout = NULL;
256int64_t ticks_per_sec;
257ram_addr_t ram_size;
258int nb_nics;
259NICInfo nd_table[MAX_NICS];
260int vm_running;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100261int autostart;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700262static int rtc_utc = 1;
263static int rtc_date_offset = -1; /* -1 means no change */
264int cirrus_vga_enabled = 1;
265int std_vga_enabled = 0;
266int vmsvga_enabled = 0;
267int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700268QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700269#ifdef TARGET_SPARC
270int graphic_width = 1024;
271int graphic_height = 768;
272int graphic_depth = 8;
273#else
274int graphic_width = 800;
275int graphic_height = 600;
276int graphic_depth = 15;
277#endif
278static int full_screen = 0;
279#ifdef CONFIG_SDL
280static int no_frame = 0;
281#endif
282int no_quit = 0;
283CharDriverState *serial_hds[MAX_SERIAL_PORTS];
284CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
285CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
286#ifdef TARGET_I386
287int win2k_install_hack = 0;
288int rtc_td_hack = 0;
289#endif
290int usb_enabled = 0;
291int singlestep = 0;
292int smp_cpus = 1;
293const char *vnc_display;
294int acpi_enabled = 1;
295int no_hpet = 0;
296int no_virtio_balloon = 0;
297int fd_bootchk = 1;
298int no_reboot = 0;
299int no_shutdown = 0;
300int cursor_hide = 1;
301int graphic_rotate = 0;
302#ifndef _WIN32
303int daemonize = 0;
304#endif
305WatchdogTimerModel *watchdog = NULL;
306int watchdog_action = WDT_RESET;
307const char *option_rom[MAX_OPTION_ROMS];
308int nb_option_roms;
309int semihosting_enabled = 0;
310#ifdef TARGET_ARM
311int old_param = 0;
312#endif
313const char *qemu_name;
314int alt_grab = 0;
315#if defined(TARGET_SPARC) || defined(TARGET_PPC)
316unsigned int nb_prom_envs = 0;
317const char *prom_envs[MAX_PROM_ENVS];
318#endif
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100319#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700320int nb_drives_opt;
321struct drive_opt drives_opt[MAX_DRIVES];
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100322#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700323int nb_numa_nodes;
324uint64_t node_mem[MAX_NODES];
325uint64_t node_cpumask[MAX_NODES];
326
327static CPUState *cur_cpu;
328static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700329static QEMUTimer *nographic_timer;
330
331uint8_t qemu_uuid[16];
332
333
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700334int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700335extern char* audio_input_source;
336
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700337extern char* android_op_ports;
338extern char* android_op_port;
339extern char* android_op_report_console;
340extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700341// Path to the file containing specific key character map.
342char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700343
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700344/* Path to hardware initialization file passed with -android-hw option. */
345char* android_op_hwini = NULL;
346
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700347/* Memory checker options. */
348char* android_op_memcheck = NULL;
349
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700350/* -dns-server option value. */
351char* android_op_dns_server = NULL;
352
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700353/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700354char* android_op_radio = NULL;
355
356/* -gps option value. */
357char* android_op_gps = NULL;
358
359/* -audio option value. */
360char* android_op_audio = NULL;
361
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700362/* -cpu-delay option value. */
363char* android_op_cpu_delay = NULL;
364
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700365#ifdef CONFIG_NAND_LIMITS
366/* -nand-limits option value. */
367char* android_op_nand_limits = NULL;
368#endif // CONFIG_NAND_LIMITS
369
370/* -netspeed option value. */
371char* android_op_netspeed = NULL;
372
373/* -netdelay option value. */
374char* android_op_netdelay = NULL;
375
376/* -netfast option value. */
377int android_op_netfast = 0;
378
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700379/* -tcpdump option value. */
380char* android_op_tcpdump = NULL;
381
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700382/* -lcd-density option value. */
383char* android_op_lcd_density = NULL;
384
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700385/* -ui-port option value. This port will be used to report the core
386 * initialization completion.
387 */
388char* android_op_ui_port = NULL;
389
390/* -ui-settings option value. This value will be passed to the UI when new UI
391 * process is attaching to the core.
392 */
393char* android_op_ui_settings = NULL;
394
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -0800395/* -android-avdname option value. */
396char* android_op_avd_name = "unknown";
397
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700398extern int android_display_width;
399extern int android_display_height;
400extern int android_display_bpp;
401
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700402extern void dprint( const char* format, ... );
403
Tim Baverstock24204cc2010-11-25 11:37:43 +0000404const char* savevm_on_exit = NULL;
Tim Baverstock24204cc2010-11-25 11:37:43 +0000405
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700406#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
407
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700408/* Reports the core initialization failure to the error stdout and to the UI
409 * socket before exiting the application.
410 * Parameters that are passed to this macro are used to format the error
411 * mesage using sprintf routine.
412 */
413#ifdef CONFIG_ANDROID
414#define PANIC(...) android_core_init_failure(__VA_ARGS__)
415#else
416#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
417 exit(1); \
418 } while (0)
419#endif // CONFIG_ANDROID
420
421/* Exits the core during initialization. */
422#ifdef CONFIG_ANDROID
423#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
424#else
425#define QEMU_EXIT(exit_code) exit(exit_code)
426#endif // CONFIG_ANDROID
427
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700428/***********************************************************/
429/* x86 ISA bus support */
430
431target_phys_addr_t isa_mem_base = 0;
432PicState2 *isa_pic;
433
434static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
435static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
436
437static uint32_t ioport_read(int index, uint32_t address)
438{
439 static IOPortReadFunc *default_func[3] = {
440 default_ioport_readb,
441 default_ioport_readw,
442 default_ioport_readl
443 };
444 IOPortReadFunc *func = ioport_read_table[index][address];
445 if (!func)
446 func = default_func[index];
447 return func(ioport_opaque[address], address);
448}
449
450static void ioport_write(int index, uint32_t address, uint32_t data)
451{
452 static IOPortWriteFunc *default_func[3] = {
453 default_ioport_writeb,
454 default_ioport_writew,
455 default_ioport_writel
456 };
457 IOPortWriteFunc *func = ioport_write_table[index][address];
458 if (!func)
459 func = default_func[index];
460 func(ioport_opaque[address], address, data);
461}
462
463static uint32_t default_ioport_readb(void *opaque, uint32_t address)
464{
465#ifdef DEBUG_UNUSED_IOPORT
466 fprintf(stderr, "unused inb: port=0x%04x\n", address);
467#endif
468 return 0xff;
469}
470
471static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
472{
473#ifdef DEBUG_UNUSED_IOPORT
474 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
475#endif
476}
477
478/* default is to make two byte accesses */
479static uint32_t default_ioport_readw(void *opaque, uint32_t address)
480{
481 uint32_t data;
482 data = ioport_read(0, address);
483 address = (address + 1) & (MAX_IOPORTS - 1);
484 data |= ioport_read(0, address) << 8;
485 return data;
486}
487
488static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
489{
490 ioport_write(0, address, data & 0xff);
491 address = (address + 1) & (MAX_IOPORTS - 1);
492 ioport_write(0, address, (data >> 8) & 0xff);
493}
494
495static uint32_t default_ioport_readl(void *opaque, uint32_t address)
496{
497#ifdef DEBUG_UNUSED_IOPORT
498 fprintf(stderr, "unused inl: port=0x%04x\n", address);
499#endif
500 return 0xffffffff;
501}
502
503static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
504{
505#ifdef DEBUG_UNUSED_IOPORT
506 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
507#endif
508}
509
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700510/***********************************************************/
511void hw_error(const char *fmt, ...)
512{
513 va_list ap;
514 CPUState *env;
515
516 va_start(ap, fmt);
517 fprintf(stderr, "qemu: hardware error: ");
518 vfprintf(stderr, fmt, ap);
519 fprintf(stderr, "\n");
520 for(env = first_cpu; env != NULL; env = env->next_cpu) {
521 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
522#ifdef TARGET_I386
523 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
524#else
525 cpu_dump_state(env, stderr, fprintf, 0);
526#endif
527 }
528 va_end(ap);
529 abort();
530}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200531
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700532/***************/
533/* ballooning */
534
535static QEMUBalloonEvent *qemu_balloon_event;
536void *qemu_balloon_event_opaque;
537
538void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
539{
540 qemu_balloon_event = func;
541 qemu_balloon_event_opaque = opaque;
542}
543
544void qemu_balloon(ram_addr_t target)
545{
546 if (qemu_balloon_event)
547 qemu_balloon_event(qemu_balloon_event_opaque, target);
548}
549
550ram_addr_t qemu_balloon_status(void)
551{
552 if (qemu_balloon_event)
553 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
554 return 0;
555}
556
557/***********************************************************/
David Turner025c32f2010-09-10 14:52:42 +0200558/* real time host monotonic timer */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700559
560/* compute with 96 bit intermediate result: (a*b)/c */
561uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
562{
563 union {
564 uint64_t ll;
565 struct {
David 'Digit' Turner20894ae2010-05-10 17:07:36 -0700566#ifdef HOST_WORDS_BIGENDIAN
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700567 uint32_t high, low;
568#else
569 uint32_t low, high;
570#endif
571 } l;
572 } u, res;
573 uint64_t rl, rh;
574
575 u.ll = a;
576 rl = (uint64_t)u.l.low * (uint64_t)b;
577 rh = (uint64_t)u.l.high * (uint64_t)b;
578 rh += (rl >> 32);
579 res.l.high = rh / c;
580 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
581 return res.ll;
582}
583
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700584/***********************************************************/
585/* host time/date access */
586void qemu_get_timedate(struct tm *tm, int offset)
587{
588 time_t ti;
589 struct tm *ret;
590
591 time(&ti);
592 ti += offset;
593 if (rtc_date_offset == -1) {
594 if (rtc_utc)
595 ret = gmtime(&ti);
596 else
597 ret = localtime(&ti);
598 } else {
599 ti -= rtc_date_offset;
600 ret = gmtime(&ti);
601 }
602
603 memcpy(tm, ret, sizeof(struct tm));
604}
605
606int qemu_timedate_diff(struct tm *tm)
607{
608 time_t seconds;
609
610 if (rtc_date_offset == -1)
611 if (rtc_utc)
612 seconds = mktimegm(tm);
613 else
614 seconds = mktime(tm);
615 else
616 seconds = mktimegm(tm) + rtc_date_offset;
617
618 return seconds - time(NULL);
619}
620
621
622#ifdef CONFIG_TRACE
623static int tbflush_requested;
624static int exit_requested;
625
626void start_tracing()
627{
628 if (trace_filename == NULL)
629 return;
630 if (!tracing) {
631 fprintf(stderr,"-- start tracing --\n");
632 start_time = Now();
633 }
634 tracing = 1;
635 tbflush_requested = 1;
636 qemu_notify_event();
637}
638
639void stop_tracing()
640{
641 if (trace_filename == NULL)
642 return;
643 if (tracing) {
644 end_time = Now();
645 elapsed_usecs += end_time - start_time;
646 fprintf(stderr,"-- stop tracing --\n");
647 }
648 tracing = 0;
649 tbflush_requested = 1;
650 qemu_notify_event();
651}
652
653#ifndef _WIN32
654/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
655 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
656 */
657void sigusr_handler(int sig)
658{
659 if (sig == SIGUSR1)
660 start_tracing();
661 else
662 stop_tracing();
663}
664#endif
665
666/* This is the handler to catch control-C so that we can exit cleanly.
667 * This is needed when tracing to flush the buffers to disk.
668 */
669void sigint_handler(int sig)
670{
671 exit_requested = 1;
672 qemu_notify_event();
673}
674#endif /* CONFIG_TRACE */
675
676
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700677/***********************************************************/
678/* Bluetooth support */
679static int nb_hcis;
680static int cur_hci;
681static struct HCIInfo *hci_table[MAX_NICS];
682
683static struct bt_vlan_s {
684 struct bt_scatternet_s net;
685 int id;
686 struct bt_vlan_s *next;
687} *first_bt_vlan;
688
689/* find or alloc a new bluetooth "VLAN" */
690static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
691{
692 struct bt_vlan_s **pvlan, *vlan;
693 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
694 if (vlan->id == id)
695 return &vlan->net;
696 }
697 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
698 vlan->id = id;
699 pvlan = &first_bt_vlan;
700 while (*pvlan != NULL)
701 pvlan = &(*pvlan)->next;
702 *pvlan = vlan;
703 return &vlan->net;
704}
705
706static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
707{
708}
709
710static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
711{
712 return -ENOTSUP;
713}
714
715static struct HCIInfo null_hci = {
716 .cmd_send = null_hci_send,
717 .sco_send = null_hci_send,
718 .acl_send = null_hci_send,
719 .bdaddr_set = null_hci_addr_set,
720};
721
722struct HCIInfo *qemu_next_hci(void)
723{
724 if (cur_hci == nb_hcis)
725 return &null_hci;
726
727 return hci_table[cur_hci++];
728}
729
730static struct HCIInfo *hci_init(const char *str)
731{
732 char *endp;
733 struct bt_scatternet_s *vlan = 0;
734
735 if (!strcmp(str, "null"))
736 /* null */
737 return &null_hci;
738 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
739 /* host[:hciN] */
740 return bt_host_hci(str[4] ? str + 5 : "hci0");
741 else if (!strncmp(str, "hci", 3)) {
742 /* hci[,vlan=n] */
743 if (str[3]) {
744 if (!strncmp(str + 3, ",vlan=", 6)) {
745 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
746 if (*endp)
747 vlan = 0;
748 }
749 } else
750 vlan = qemu_find_bt_vlan(0);
751 if (vlan)
752 return bt_new_hci(vlan);
753 }
754
755 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
756
757 return 0;
758}
759
760static int bt_hci_parse(const char *str)
761{
762 struct HCIInfo *hci;
763 bdaddr_t bdaddr;
764
765 if (nb_hcis >= MAX_NICS) {
766 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
767 return -1;
768 }
769
770 hci = hci_init(str);
771 if (!hci)
772 return -1;
773
774 bdaddr.b[0] = 0x52;
775 bdaddr.b[1] = 0x54;
776 bdaddr.b[2] = 0x00;
777 bdaddr.b[3] = 0x12;
778 bdaddr.b[4] = 0x34;
779 bdaddr.b[5] = 0x56 + nb_hcis;
780 hci->bdaddr_set(hci, bdaddr.b);
781
782 hci_table[nb_hcis++] = hci;
783
784 return 0;
785}
786
787static void bt_vhci_add(int vlan_id)
788{
789 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
790
791 if (!vlan->slave)
792 fprintf(stderr, "qemu: warning: adding a VHCI to "
793 "an empty scatternet %i\n", vlan_id);
794
795 bt_vhci_init(bt_new_hci(vlan));
796}
797
798static struct bt_device_s *bt_device_add(const char *opt)
799{
800 struct bt_scatternet_s *vlan;
801 int vlan_id = 0;
802 char *endp = strstr(opt, ",vlan=");
803 int len = (endp ? endp - opt : strlen(opt)) + 1;
804 char devname[10];
805
806 pstrcpy(devname, MIN(sizeof(devname), len), opt);
807
808 if (endp) {
809 vlan_id = strtol(endp + 6, &endp, 0);
810 if (*endp) {
811 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
812 return 0;
813 }
814 }
815
816 vlan = qemu_find_bt_vlan(vlan_id);
817
818 if (!vlan->slave)
819 fprintf(stderr, "qemu: warning: adding a slave device to "
820 "an empty scatternet %i\n", vlan_id);
821
822 if (!strcmp(devname, "keyboard"))
823 return bt_keyboard_init(vlan);
824
825 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
826 return 0;
827}
828
829static int bt_parse(const char *opt)
830{
831 const char *endp, *p;
832 int vlan;
833
834 if (strstart(opt, "hci", &endp)) {
835 if (!*endp || *endp == ',') {
836 if (*endp)
837 if (!strstart(endp, ",vlan=", 0))
838 opt = endp + 1;
839
840 return bt_hci_parse(opt);
841 }
842 } else if (strstart(opt, "vhci", &endp)) {
843 if (!*endp || *endp == ',') {
844 if (*endp) {
845 if (strstart(endp, ",vlan=", &p)) {
846 vlan = strtol(p, (char **) &endp, 0);
847 if (*endp) {
848 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
849 return 1;
850 }
851 } else {
852 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
853 return 1;
854 }
855 } else
856 vlan = 0;
857
858 bt_vhci_add(vlan);
859 return 0;
860 }
861 } else if (strstart(opt, "device:", &endp))
862 return !bt_device_add(endp);
863
864 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
865 return 1;
866}
867
868/***********************************************************/
869/* QEMU Block devices */
870
871#define HD_ALIAS "index=%d,media=disk"
872#define CDROM_ALIAS "index=2,media=cdrom"
873#define FD_ALIAS "index=%d,if=floppy"
874#define PFLASH_ALIAS "if=pflash"
875#define MTD_ALIAS "if=mtd"
876#define SD_ALIAS "index=0,if=sd"
877
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100878static int drive_init_func(QemuOpts *opts, void *opaque)
879{
880 int *use_scsi = opaque;
881 int fatal_error = 0;
882
883 if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
884 if (fatal_error)
885 return 1;
886 }
887 return 0;
888}
889
890static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
891{
892 if (NULL == qemu_opt_get(opts, "snapshot")) {
893 qemu_opt_set(opts, "snapshot", "on");
894 }
895 return 0;
896}
897
898#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700899static int drive_opt_get_free_idx(void)
900{
901 int index;
902
903 for (index = 0; index < MAX_DRIVES; index++)
904 if (!drives_opt[index].used) {
905 drives_opt[index].used = 1;
906 return index;
907 }
908
909 return -1;
910}
911
912static int drive_get_free_idx(void)
913{
914 int index;
915
916 for (index = 0; index < MAX_DRIVES; index++)
917 if (!drives_table[index].used) {
918 drives_table[index].used = 1;
919 return index;
920 }
921
922 return -1;
923}
924
925int drive_add(const char *file, const char *fmt, ...)
926{
927 va_list ap;
928 int index = drive_opt_get_free_idx();
929
930 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
931 fprintf(stderr, "qemu: too many drives\n");
932 return -1;
933 }
934
935 drives_opt[index].file = file;
936 va_start(ap, fmt);
937 vsnprintf(drives_opt[index].opt,
938 sizeof(drives_opt[0].opt), fmt, ap);
939 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700940
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700941 nb_drives_opt++;
942 return index;
943}
944
945void drive_remove(int index)
946{
947 drives_opt[index].used = 0;
948 nb_drives_opt--;
949}
950
951int drive_get_index(BlockInterfaceType type, int bus, int unit)
952{
953 int index;
954
955 /* seek interface, bus and unit */
956
957 for (index = 0; index < MAX_DRIVES; index++)
958 if (drives_table[index].type == type &&
959 drives_table[index].bus == bus &&
960 drives_table[index].unit == unit &&
961 drives_table[index].used)
962 return index;
963
964 return -1;
965}
966
967int drive_get_max_bus(BlockInterfaceType type)
968{
969 int max_bus;
970 int index;
971
972 max_bus = -1;
973 for (index = 0; index < nb_drives; index++) {
974 if(drives_table[index].type == type &&
975 drives_table[index].bus > max_bus)
976 max_bus = drives_table[index].bus;
977 }
978 return max_bus;
979}
980
981const char *drive_get_serial(BlockDriverState *bdrv)
982{
983 int index;
984
985 for (index = 0; index < nb_drives; index++)
986 if (drives_table[index].bdrv == bdrv)
987 return drives_table[index].serial;
988
989 return "\0";
990}
991
992BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
993{
994 int index;
995
996 for (index = 0; index < nb_drives; index++)
997 if (drives_table[index].bdrv == bdrv)
998 return drives_table[index].onerror;
999
1000 return BLOCK_ERR_STOP_ENOSPC;
1001}
1002
1003static void bdrv_format_print(void *opaque, const char *name)
1004{
1005 fprintf(stderr, " %s", name);
1006}
1007
1008void drive_uninit(BlockDriverState *bdrv)
1009{
1010 int i;
1011
1012 for (i = 0; i < MAX_DRIVES; i++)
1013 if (drives_table[i].bdrv == bdrv) {
1014 drives_table[i].bdrv = NULL;
1015 drives_table[i].used = 0;
1016 drive_remove(drives_table[i].drive_opt_idx);
1017 nb_drives--;
1018 break;
1019 }
1020}
1021
1022int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1023{
1024 char buf[128];
1025 char file[1024];
1026 char devname[128];
1027 char serial[21];
1028 const char *mediastr = "";
1029 BlockInterfaceType type;
1030 enum { MEDIA_DISK, MEDIA_CDROM } media;
1031 int bus_id, unit_id;
1032 int cyls, heads, secs, translation;
1033 BlockDriverState *bdrv;
1034 BlockDriver *drv = NULL;
1035 QEMUMachine *machine = opaque;
1036 int max_devs;
1037 int index;
1038 int cache;
1039 int bdrv_flags, onerror;
1040 int drives_table_idx;
1041 char *str = arg->opt;
1042 static const char * const params[] = { "bus", "unit", "if", "index",
1043 "cyls", "heads", "secs", "trans",
1044 "media", "snapshot", "file",
1045 "cache", "format", "serial", "werror",
1046 NULL };
1047
1048 if (check_params(buf, sizeof(buf), params, str) < 0) {
1049 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1050 buf, str);
1051 return -1;
1052 }
1053
1054 file[0] = 0;
1055 cyls = heads = secs = 0;
1056 bus_id = 0;
1057 unit_id = -1;
1058 translation = BIOS_ATA_TRANSLATION_AUTO;
1059 index = -1;
1060 cache = 3;
1061
1062 if (machine->use_scsi) {
1063 type = IF_SCSI;
1064 max_devs = MAX_SCSI_DEVS;
1065 pstrcpy(devname, sizeof(devname), "scsi");
1066 } else {
1067 type = IF_IDE;
1068 max_devs = MAX_IDE_DEVS;
1069 pstrcpy(devname, sizeof(devname), "ide");
1070 }
1071 media = MEDIA_DISK;
1072
1073 /* extract parameters */
1074
1075 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1076 bus_id = strtol(buf, NULL, 0);
1077 if (bus_id < 0) {
1078 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1079 return -1;
1080 }
1081 }
1082
1083 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1084 unit_id = strtol(buf, NULL, 0);
1085 if (unit_id < 0) {
1086 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1087 return -1;
1088 }
1089 }
1090
1091 if (get_param_value(buf, sizeof(buf), "if", str)) {
1092 pstrcpy(devname, sizeof(devname), buf);
1093 if (!strcmp(buf, "ide")) {
1094 type = IF_IDE;
1095 max_devs = MAX_IDE_DEVS;
1096 } else if (!strcmp(buf, "scsi")) {
1097 type = IF_SCSI;
1098 max_devs = MAX_SCSI_DEVS;
1099 } else if (!strcmp(buf, "floppy")) {
1100 type = IF_FLOPPY;
1101 max_devs = 0;
1102 } else if (!strcmp(buf, "pflash")) {
1103 type = IF_PFLASH;
1104 max_devs = 0;
1105 } else if (!strcmp(buf, "mtd")) {
1106 type = IF_MTD;
1107 max_devs = 0;
1108 } else if (!strcmp(buf, "sd")) {
1109 type = IF_SD;
1110 max_devs = 0;
1111 } else if (!strcmp(buf, "virtio")) {
1112 type = IF_VIRTIO;
1113 max_devs = 0;
1114 } else if (!strcmp(buf, "xen")) {
1115 type = IF_XEN;
1116 max_devs = 0;
1117 } else {
1118 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1119 return -1;
1120 }
1121 }
1122
1123 if (get_param_value(buf, sizeof(buf), "index", str)) {
1124 index = strtol(buf, NULL, 0);
1125 if (index < 0) {
1126 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1127 return -1;
1128 }
1129 }
1130
1131 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1132 cyls = strtol(buf, NULL, 0);
1133 }
1134
1135 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1136 heads = strtol(buf, NULL, 0);
1137 }
1138
1139 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1140 secs = strtol(buf, NULL, 0);
1141 }
1142
1143 if (cyls || heads || secs) {
1144 if (cyls < 1 || cyls > 16383) {
1145 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1146 return -1;
1147 }
1148 if (heads < 1 || heads > 16) {
1149 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1150 return -1;
1151 }
1152 if (secs < 1 || secs > 63) {
1153 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1154 return -1;
1155 }
1156 }
1157
1158 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1159 if (!cyls) {
1160 fprintf(stderr,
1161 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1162 str);
1163 return -1;
1164 }
1165 if (!strcmp(buf, "none"))
1166 translation = BIOS_ATA_TRANSLATION_NONE;
1167 else if (!strcmp(buf, "lba"))
1168 translation = BIOS_ATA_TRANSLATION_LBA;
1169 else if (!strcmp(buf, "auto"))
1170 translation = BIOS_ATA_TRANSLATION_AUTO;
1171 else {
1172 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1173 return -1;
1174 }
1175 }
1176
1177 if (get_param_value(buf, sizeof(buf), "media", str)) {
1178 if (!strcmp(buf, "disk")) {
1179 media = MEDIA_DISK;
1180 } else if (!strcmp(buf, "cdrom")) {
1181 if (cyls || secs || heads) {
1182 fprintf(stderr,
1183 "qemu: '%s' invalid physical CHS format\n", str);
1184 return -1;
1185 }
1186 media = MEDIA_CDROM;
1187 } else {
1188 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1189 return -1;
1190 }
1191 }
1192
1193 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1194 if (!strcmp(buf, "on"))
1195 snapshot = 1;
1196 else if (!strcmp(buf, "off"))
1197 snapshot = 0;
1198 else {
1199 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1200 return -1;
1201 }
1202 }
1203
1204 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1205 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1206 cache = 0;
1207 else if (!strcmp(buf, "writethrough"))
1208 cache = 1;
1209 else if (!strcmp(buf, "writeback"))
1210 cache = 2;
1211 else {
1212 fprintf(stderr, "qemu: invalid cache option\n");
1213 return -1;
1214 }
1215 }
1216
1217 if (get_param_value(buf, sizeof(buf), "format", str)) {
1218 if (strcmp(buf, "?") == 0) {
1219 fprintf(stderr, "qemu: Supported formats:");
1220 bdrv_iterate_format(bdrv_format_print, NULL);
1221 fprintf(stderr, "\n");
1222 return -1;
1223 }
1224 drv = bdrv_find_format(buf);
1225 if (!drv) {
1226 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1227 return -1;
1228 }
1229 }
1230
1231 if (arg->file == NULL)
1232 get_param_value(file, sizeof(file), "file", str);
1233 else
1234 pstrcpy(file, sizeof(file), arg->file);
1235
1236 if (!get_param_value(serial, sizeof(serial), "serial", str))
1237 memset(serial, 0, sizeof(serial));
1238
1239 onerror = BLOCK_ERR_STOP_ENOSPC;
1240 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1241 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1242 fprintf(stderr, "werror is no supported by this format\n");
1243 return -1;
1244 }
1245 if (!strcmp(buf, "ignore"))
1246 onerror = BLOCK_ERR_IGNORE;
1247 else if (!strcmp(buf, "enospc"))
1248 onerror = BLOCK_ERR_STOP_ENOSPC;
1249 else if (!strcmp(buf, "stop"))
1250 onerror = BLOCK_ERR_STOP_ANY;
1251 else if (!strcmp(buf, "report"))
1252 onerror = BLOCK_ERR_REPORT;
1253 else {
1254 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1255 return -1;
1256 }
1257 }
1258
1259 /* compute bus and unit according index */
1260
1261 if (index != -1) {
1262 if (bus_id != 0 || unit_id != -1) {
1263 fprintf(stderr,
1264 "qemu: '%s' index cannot be used with bus and unit\n", str);
1265 return -1;
1266 }
1267 if (max_devs == 0)
1268 {
1269 unit_id = index;
1270 bus_id = 0;
1271 } else {
1272 unit_id = index % max_devs;
1273 bus_id = index / max_devs;
1274 }
1275 }
1276
1277 /* if user doesn't specify a unit_id,
1278 * try to find the first free
1279 */
1280
1281 if (unit_id == -1) {
1282 unit_id = 0;
1283 while (drive_get_index(type, bus_id, unit_id) != -1) {
1284 unit_id++;
1285 if (max_devs && unit_id >= max_devs) {
1286 unit_id -= max_devs;
1287 bus_id++;
1288 }
1289 }
1290 }
1291
1292 /* check unit id */
1293
1294 if (max_devs && unit_id >= max_devs) {
1295 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1296 str, unit_id, max_devs - 1);
1297 return -1;
1298 }
1299
1300 /*
1301 * ignore multiple definitions
1302 */
1303
1304 if (drive_get_index(type, bus_id, unit_id) != -1)
1305 return -2;
1306
1307 /* init */
1308
1309 if (type == IF_IDE || type == IF_SCSI)
1310 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1311 if (max_devs)
1312 snprintf(buf, sizeof(buf), "%s%i%s%i",
1313 devname, bus_id, mediastr, unit_id);
1314 else
1315 snprintf(buf, sizeof(buf), "%s%s%i",
1316 devname, mediastr, unit_id);
1317 bdrv = bdrv_new(buf);
1318 drives_table_idx = drive_get_free_idx();
1319 drives_table[drives_table_idx].bdrv = bdrv;
1320 drives_table[drives_table_idx].type = type;
1321 drives_table[drives_table_idx].bus = bus_id;
1322 drives_table[drives_table_idx].unit = unit_id;
1323 drives_table[drives_table_idx].onerror = onerror;
1324 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1325 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1326 nb_drives++;
1327
1328 switch(type) {
1329 case IF_IDE:
1330 case IF_SCSI:
1331 case IF_XEN:
1332 switch(media) {
1333 case MEDIA_DISK:
1334 if (cyls != 0) {
1335 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1336 bdrv_set_translation_hint(bdrv, translation);
1337 }
1338 break;
1339 case MEDIA_CDROM:
1340 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1341 break;
1342 }
1343 break;
1344 case IF_SD:
1345 /* FIXME: This isn't really a floppy, but it's a reasonable
1346 approximation. */
1347 case IF_FLOPPY:
1348 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1349 break;
1350 case IF_PFLASH:
1351 case IF_MTD:
1352 case IF_VIRTIO:
1353 break;
1354 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001355 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001356 abort();
1357 }
1358 if (!file[0])
1359 return -2;
1360 bdrv_flags = 0;
1361 if (snapshot) {
1362 bdrv_flags |= BDRV_O_SNAPSHOT;
1363 cache = 2; /* always use write-back with snapshot */
1364 }
1365 if (cache == 0) /* no caching */
1366 bdrv_flags |= BDRV_O_NOCACHE;
1367 else if (cache == 2) /* write-back */
1368 bdrv_flags |= BDRV_O_CACHE_WB;
1369 else if (cache == 3) /* not specified */
1370 bdrv_flags |= BDRV_O_CACHE_DEF;
1371 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1372 fprintf(stderr, "qemu: could not open disk image %s\n",
1373 file);
1374 return -1;
1375 }
1376 if (bdrv_key_required(bdrv))
1377 autostart = 0;
1378 return drives_table_idx;
1379}
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001380#endif /* MAX_DRIVES */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001381
1382static void numa_add(const char *optarg)
1383{
1384 char option[128];
1385 char *endptr;
1386 unsigned long long value, endvalue;
1387 int nodenr;
1388
1389 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1390 if (!strcmp(option, "node")) {
1391 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1392 nodenr = nb_numa_nodes;
1393 } else {
1394 nodenr = strtoull(option, NULL, 10);
1395 }
1396
1397 if (get_param_value(option, 128, "mem", optarg) == 0) {
1398 node_mem[nodenr] = 0;
1399 } else {
1400 value = strtoull(option, &endptr, 0);
1401 switch (*endptr) {
1402 case 0: case 'M': case 'm':
1403 value <<= 20;
1404 break;
1405 case 'G': case 'g':
1406 value <<= 30;
1407 break;
1408 }
1409 node_mem[nodenr] = value;
1410 }
1411 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1412 node_cpumask[nodenr] = 0;
1413 } else {
1414 value = strtoull(option, &endptr, 10);
1415 if (value >= 64) {
1416 value = 63;
1417 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1418 } else {
1419 if (*endptr == '-') {
1420 endvalue = strtoull(endptr+1, &endptr, 10);
1421 if (endvalue >= 63) {
1422 endvalue = 62;
1423 fprintf(stderr,
1424 "only 63 CPUs in NUMA mode supported.\n");
1425 }
1426 value = (1 << (endvalue + 1)) - (1 << value);
1427 } else {
1428 value = 1 << value;
1429 }
1430 }
1431 node_cpumask[nodenr] = value;
1432 }
1433 nb_numa_nodes++;
1434 }
1435 return;
1436}
1437
1438/***********************************************************/
1439/* USB devices */
1440
1441static USBPort *used_usb_ports;
1442static USBPort *free_usb_ports;
1443
1444/* ??? Maybe change this to register a hub to keep track of the topology. */
1445void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1446 usb_attachfn attach)
1447{
1448 port->opaque = opaque;
1449 port->index = index;
1450 port->attach = attach;
1451 port->next = free_usb_ports;
1452 free_usb_ports = port;
1453}
1454
1455int usb_device_add_dev(USBDevice *dev)
1456{
1457 USBPort *port;
1458
1459 /* Find a USB port to add the device to. */
1460 port = free_usb_ports;
1461 if (!port->next) {
1462 USBDevice *hub;
1463
1464 /* Create a new hub and chain it on. */
1465 free_usb_ports = NULL;
1466 port->next = used_usb_ports;
1467 used_usb_ports = port;
1468
1469 hub = usb_hub_init(VM_USB_HUB_SIZE);
1470 usb_attach(port, hub);
1471 port = free_usb_ports;
1472 }
1473
1474 free_usb_ports = port->next;
1475 port->next = used_usb_ports;
1476 used_usb_ports = port;
1477 usb_attach(port, dev);
1478 return 0;
1479}
1480
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001481#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001482static void usb_msd_password_cb(void *opaque, int err)
1483{
1484 USBDevice *dev = opaque;
1485
1486 if (!err)
1487 usb_device_add_dev(dev);
1488 else
1489 dev->handle_destroy(dev);
1490}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001491#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001492
1493static int usb_device_add(const char *devname, int is_hotplug)
1494{
1495 const char *p;
1496 USBDevice *dev;
1497
1498 if (!free_usb_ports)
1499 return -1;
1500
1501 if (strstart(devname, "host:", &p)) {
1502 dev = usb_host_device_open(p);
1503 } else if (!strcmp(devname, "mouse")) {
1504 dev = usb_mouse_init();
1505 } else if (!strcmp(devname, "tablet")) {
1506 dev = usb_tablet_init();
1507 } else if (!strcmp(devname, "keyboard")) {
1508 dev = usb_keyboard_init();
1509 } else if (strstart(devname, "disk:", &p)) {
1510#if 0
1511 BlockDriverState *bs;
1512#endif
1513 dev = usb_msd_init(p);
1514 if (!dev)
1515 return -1;
1516#if 0
1517 bs = usb_msd_get_bdrv(dev);
1518 if (bdrv_key_required(bs)) {
1519 autostart = 0;
1520 if (is_hotplug) {
1521 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1522 dev);
1523 return 0;
1524 }
1525 }
1526 } else if (!strcmp(devname, "wacom-tablet")) {
1527 dev = usb_wacom_init();
1528 } else if (strstart(devname, "serial:", &p)) {
1529 dev = usb_serial_init(p);
1530#ifdef CONFIG_BRLAPI
1531 } else if (!strcmp(devname, "braille")) {
1532 dev = usb_baum_init();
1533#endif
1534 } else if (strstart(devname, "net:", &p)) {
1535 int nic = nb_nics;
1536
1537 if (net_client_init("nic", p) < 0)
1538 return -1;
1539 nd_table[nic].model = "usb";
1540 dev = usb_net_init(&nd_table[nic]);
1541 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1542 dev = usb_bt_init(devname[2] ? hci_init(p) :
1543 bt_new_hci(qemu_find_bt_vlan(0)));
1544#endif
1545 } else {
1546 return -1;
1547 }
1548 if (!dev)
1549 return -1;
1550
1551 return usb_device_add_dev(dev);
1552}
1553
1554int usb_device_del_addr(int bus_num, int addr)
1555{
1556 USBPort *port;
1557 USBPort **lastp;
1558 USBDevice *dev;
1559
1560 if (!used_usb_ports)
1561 return -1;
1562
1563 if (bus_num != 0)
1564 return -1;
1565
1566 lastp = &used_usb_ports;
1567 port = used_usb_ports;
1568 while (port && port->dev->addr != addr) {
1569 lastp = &port->next;
1570 port = port->next;
1571 }
1572
1573 if (!port)
1574 return -1;
1575
1576 dev = port->dev;
1577 *lastp = port->next;
1578 usb_attach(port, NULL);
1579 dev->handle_destroy(dev);
1580 port->next = free_usb_ports;
1581 free_usb_ports = port;
1582 return 0;
1583}
1584
1585static int usb_device_del(const char *devname)
1586{
1587 int bus_num, addr;
1588 const char *p;
1589
1590 if (strstart(devname, "host:", &p))
1591 return usb_host_device_close(p);
1592
1593 if (!used_usb_ports)
1594 return -1;
1595
1596 p = strchr(devname, '.');
1597 if (!p)
1598 return -1;
1599 bus_num = strtoul(devname, NULL, 0);
1600 addr = strtoul(p + 1, NULL, 0);
1601
1602 return usb_device_del_addr(bus_num, addr);
1603}
1604
1605void do_usb_add(Monitor *mon, const char *devname)
1606{
1607 usb_device_add(devname, 1);
1608}
1609
1610void do_usb_del(Monitor *mon, const char *devname)
1611{
1612 usb_device_del(devname);
1613}
1614
1615void usb_info(Monitor *mon)
1616{
1617 USBDevice *dev;
1618 USBPort *port;
1619 const char *speed_str;
1620
1621 if (!usb_enabled) {
1622 monitor_printf(mon, "USB support not enabled\n");
1623 return;
1624 }
1625
1626 for (port = used_usb_ports; port; port = port->next) {
1627 dev = port->dev;
1628 if (!dev)
1629 continue;
1630 switch(dev->speed) {
1631 case USB_SPEED_LOW:
1632 speed_str = "1.5";
1633 break;
1634 case USB_SPEED_FULL:
1635 speed_str = "12";
1636 break;
1637 case USB_SPEED_HIGH:
1638 speed_str = "480";
1639 break;
1640 default:
1641 speed_str = "?";
1642 break;
1643 }
1644 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1645 0, dev->addr, speed_str, dev->devname);
1646 }
1647}
1648
1649/***********************************************************/
1650/* PCMCIA/Cardbus */
1651
1652static struct pcmcia_socket_entry_s {
1653 PCMCIASocket *socket;
1654 struct pcmcia_socket_entry_s *next;
1655} *pcmcia_sockets = 0;
1656
1657void pcmcia_socket_register(PCMCIASocket *socket)
1658{
1659 struct pcmcia_socket_entry_s *entry;
1660
1661 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1662 entry->socket = socket;
1663 entry->next = pcmcia_sockets;
1664 pcmcia_sockets = entry;
1665}
1666
1667void pcmcia_socket_unregister(PCMCIASocket *socket)
1668{
1669 struct pcmcia_socket_entry_s *entry, **ptr;
1670
1671 ptr = &pcmcia_sockets;
1672 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1673 if (entry->socket == socket) {
1674 *ptr = entry->next;
1675 qemu_free(entry);
1676 }
1677}
1678
1679void pcmcia_info(Monitor *mon)
1680{
1681 struct pcmcia_socket_entry_s *iter;
1682
1683 if (!pcmcia_sockets)
1684 monitor_printf(mon, "No PCMCIA sockets\n");
1685
1686 for (iter = pcmcia_sockets; iter; iter = iter->next)
1687 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1688 iter->socket->attached ? iter->socket->card_string :
1689 "Empty");
1690}
1691
1692/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001693/* I/O handling */
1694
1695typedef struct IOHandlerRecord {
1696 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001697 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001698 IOHandler *fd_read;
1699 IOHandler *fd_write;
1700 int deleted;
1701 void *opaque;
1702 /* temporary data */
1703 struct pollfd *ufd;
1704 struct IOHandlerRecord *next;
1705} IOHandlerRecord;
1706
1707static IOHandlerRecord *first_io_handler;
1708
1709/* XXX: fd_read_poll should be suppressed, but an API change is
1710 necessary in the character devices to suppress fd_can_read(). */
1711int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001712 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001713 IOHandler *fd_read,
1714 IOHandler *fd_write,
1715 void *opaque)
1716{
1717 IOHandlerRecord **pioh, *ioh;
1718
1719 if (!fd_read && !fd_write) {
1720 pioh = &first_io_handler;
1721 for(;;) {
1722 ioh = *pioh;
1723 if (ioh == NULL)
1724 break;
1725 if (ioh->fd == fd) {
1726 ioh->deleted = 1;
1727 break;
1728 }
1729 pioh = &ioh->next;
1730 }
1731 } else {
1732 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1733 if (ioh->fd == fd)
1734 goto found;
1735 }
1736 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1737 ioh->next = first_io_handler;
1738 first_io_handler = ioh;
1739 found:
1740 ioh->fd = fd;
1741 ioh->fd_read_poll = fd_read_poll;
1742 ioh->fd_read = fd_read;
1743 ioh->fd_write = fd_write;
1744 ioh->opaque = opaque;
1745 ioh->deleted = 0;
1746 }
1747 return 0;
1748}
1749
1750int qemu_set_fd_handler(int fd,
1751 IOHandler *fd_read,
1752 IOHandler *fd_write,
1753 void *opaque)
1754{
1755 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1756}
1757
1758#ifdef _WIN32
1759/***********************************************************/
1760/* Polling handling */
1761
1762typedef struct PollingEntry {
1763 PollingFunc *func;
1764 void *opaque;
1765 struct PollingEntry *next;
1766} PollingEntry;
1767
1768static PollingEntry *first_polling_entry;
1769
1770int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1771{
1772 PollingEntry **ppe, *pe;
1773 pe = qemu_mallocz(sizeof(PollingEntry));
1774 pe->func = func;
1775 pe->opaque = opaque;
1776 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1777 *ppe = pe;
1778 return 0;
1779}
1780
1781void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1782{
1783 PollingEntry **ppe, *pe;
1784 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1785 pe = *ppe;
1786 if (pe->func == func && pe->opaque == opaque) {
1787 *ppe = pe->next;
1788 qemu_free(pe);
1789 break;
1790 }
1791 }
1792}
1793
1794/***********************************************************/
1795/* Wait objects support */
1796typedef struct WaitObjects {
1797 int num;
1798 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1799 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1800 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1801} WaitObjects;
1802
1803static WaitObjects wait_objects = {0};
1804
1805int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1806{
1807 WaitObjects *w = &wait_objects;
1808
1809 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1810 return -1;
1811 w->events[w->num] = handle;
1812 w->func[w->num] = func;
1813 w->opaque[w->num] = opaque;
1814 w->num++;
1815 return 0;
1816}
1817
1818void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1819{
1820 int i, found;
1821 WaitObjects *w = &wait_objects;
1822
1823 found = 0;
1824 for (i = 0; i < w->num; i++) {
1825 if (w->events[i] == handle)
1826 found = 1;
1827 if (found) {
1828 w->events[i] = w->events[i + 1];
1829 w->func[i] = w->func[i + 1];
1830 w->opaque[i] = w->opaque[i + 1];
1831 }
1832 }
1833 if (found)
1834 w->num--;
1835}
1836#endif
1837
1838/***********************************************************/
1839/* ram save/restore */
1840
1841static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1842{
1843 int v;
1844
1845 v = qemu_get_byte(f);
1846 switch(v) {
1847 case 0:
1848 if (qemu_get_buffer(f, buf, len) != len)
1849 return -EIO;
1850 break;
1851 case 1:
1852 v = qemu_get_byte(f);
1853 memset(buf, v, len);
1854 break;
1855 default:
1856 return -EINVAL;
1857 }
1858
1859 if (qemu_file_has_error(f))
1860 return -EIO;
1861
1862 return 0;
1863}
1864
1865static int ram_load_v1(QEMUFile *f, void *opaque)
1866{
1867 int ret;
1868 ram_addr_t i;
1869
1870 if (qemu_get_be32(f) != last_ram_offset)
1871 return -EINVAL;
1872 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1873 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1874 if (ret)
1875 return ret;
1876 }
1877 return 0;
1878}
1879
1880#define BDRV_HASH_BLOCK_SIZE 1024
1881#define IOBUF_SIZE 4096
1882#define RAM_CBLOCK_MAGIC 0xfabe
1883
1884typedef struct RamDecompressState {
1885 z_stream zstream;
1886 QEMUFile *f;
1887 uint8_t buf[IOBUF_SIZE];
1888} RamDecompressState;
1889
1890static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1891{
1892 int ret;
1893 memset(s, 0, sizeof(*s));
1894 s->f = f;
1895 ret = inflateInit(&s->zstream);
1896 if (ret != Z_OK)
1897 return -1;
1898 return 0;
1899}
1900
1901static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1902{
1903 int ret, clen;
1904
1905 s->zstream.avail_out = len;
1906 s->zstream.next_out = buf;
1907 while (s->zstream.avail_out > 0) {
1908 if (s->zstream.avail_in == 0) {
1909 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1910 return -1;
1911 clen = qemu_get_be16(s->f);
1912 if (clen > IOBUF_SIZE)
1913 return -1;
1914 qemu_get_buffer(s->f, s->buf, clen);
1915 s->zstream.avail_in = clen;
1916 s->zstream.next_in = s->buf;
1917 }
1918 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1919 if (ret != Z_OK && ret != Z_STREAM_END) {
1920 return -1;
1921 }
1922 }
1923 return 0;
1924}
1925
1926static void ram_decompress_close(RamDecompressState *s)
1927{
1928 inflateEnd(&s->zstream);
1929}
1930
1931#define RAM_SAVE_FLAG_FULL 0x01
1932#define RAM_SAVE_FLAG_COMPRESS 0x02
1933#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1934#define RAM_SAVE_FLAG_PAGE 0x08
1935#define RAM_SAVE_FLAG_EOS 0x10
1936
1937static int is_dup_page(uint8_t *page, uint8_t ch)
1938{
1939 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1940 uint32_t *array = (uint32_t *)page;
1941 int i;
1942
1943 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1944 if (array[i] != val)
1945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951static int ram_save_block(QEMUFile *f)
1952{
1953 static ram_addr_t current_addr = 0;
1954 ram_addr_t saved_addr = current_addr;
1955 ram_addr_t addr = 0;
1956 int found = 0;
1957
1958 while (addr < last_ram_offset) {
1959 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1960 uint8_t *p;
1961
1962 cpu_physical_memory_reset_dirty(current_addr,
1963 current_addr + TARGET_PAGE_SIZE,
1964 MIGRATION_DIRTY_FLAG);
1965
1966 p = qemu_get_ram_ptr(current_addr);
1967
1968 if (is_dup_page(p, *p)) {
1969 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
1970 qemu_put_byte(f, *p);
1971 } else {
1972 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
1973 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
1974 }
1975
1976 found = 1;
1977 break;
1978 }
1979 addr += TARGET_PAGE_SIZE;
1980 current_addr = (saved_addr + addr) % last_ram_offset;
1981 }
1982
1983 return found;
1984}
1985
1986static uint64_t bytes_transferred = 0;
1987
1988static ram_addr_t ram_save_remaining(void)
1989{
1990 ram_addr_t addr;
1991 ram_addr_t count = 0;
1992
1993 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
1994 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1995 count++;
1996 }
1997
1998 return count;
1999}
2000
2001uint64_t ram_bytes_remaining(void)
2002{
2003 return ram_save_remaining() * TARGET_PAGE_SIZE;
2004}
2005
2006uint64_t ram_bytes_transferred(void)
2007{
2008 return bytes_transferred;
2009}
2010
2011uint64_t ram_bytes_total(void)
2012{
2013 return last_ram_offset;
2014}
2015
2016static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2017{
2018 ram_addr_t addr;
2019 uint64_t bytes_transferred_last;
2020 double bwidth = 0;
2021 uint64_t expected_time = 0;
2022
2023 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2024
2025 if (stage == 1) {
2026 /* Make sure all dirty bits are set */
2027 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2028 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2029 cpu_physical_memory_set_dirty(addr);
2030 }
2031
2032 /* Enable dirty memory tracking */
2033 cpu_physical_memory_set_dirty_tracking(1);
2034
2035 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2036 }
2037
2038 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002039 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002040
2041 while (!qemu_file_rate_limit(f)) {
2042 int ret;
2043
2044 ret = ram_save_block(f);
2045 bytes_transferred += ret * TARGET_PAGE_SIZE;
2046 if (ret == 0) /* no more blocks */
2047 break;
2048 }
2049
David Turner6a9ef172010-09-09 22:54:36 +02002050 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002051 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2052
2053 /* if we haven't transferred anything this round, force expected_time to a
2054 * a very high value, but without crashing */
2055 if (bwidth == 0)
2056 bwidth = 0.000001;
2057
2058 /* try transferring iterative blocks of memory */
2059
2060 if (stage == 3) {
2061
2062 /* flush all remaining blocks regardless of rate limiting */
2063 while (ram_save_block(f) != 0) {
2064 bytes_transferred += TARGET_PAGE_SIZE;
2065 }
2066 cpu_physical_memory_set_dirty_tracking(0);
2067 }
2068
2069 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2070
2071 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2072
2073 return (stage == 2) && (expected_time <= migrate_max_downtime());
2074}
2075
2076static int ram_load_dead(QEMUFile *f, void *opaque)
2077{
2078 RamDecompressState s1, *s = &s1;
2079 uint8_t buf[10];
2080 ram_addr_t i;
2081
2082 if (ram_decompress_open(s, f) < 0)
2083 return -EINVAL;
2084 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2085 if (ram_decompress_buf(s, buf, 1) < 0) {
2086 fprintf(stderr, "Error while reading ram block header\n");
2087 goto error;
2088 }
2089 if (buf[0] == 0) {
2090 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2091 BDRV_HASH_BLOCK_SIZE) < 0) {
2092 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2093 goto error;
2094 }
2095 } else {
2096 error:
2097 printf("Error block header\n");
2098 return -EINVAL;
2099 }
2100 }
2101 ram_decompress_close(s);
2102
2103 return 0;
2104}
2105
2106static int ram_load(QEMUFile *f, void *opaque, int version_id)
2107{
2108 ram_addr_t addr;
2109 int flags;
2110
2111 if (version_id == 1)
2112 return ram_load_v1(f, opaque);
2113
2114 if (version_id == 2) {
2115 if (qemu_get_be32(f) != last_ram_offset)
2116 return -EINVAL;
2117 return ram_load_dead(f, opaque);
2118 }
2119
2120 if (version_id != 3)
2121 return -EINVAL;
2122
2123 do {
2124 addr = qemu_get_be64(f);
2125
2126 flags = addr & ~TARGET_PAGE_MASK;
2127 addr &= TARGET_PAGE_MASK;
2128
2129 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2130 if (addr != last_ram_offset)
2131 return -EINVAL;
2132 }
2133
2134 if (flags & RAM_SAVE_FLAG_FULL) {
2135 if (ram_load_dead(f, opaque) < 0)
2136 return -EINVAL;
2137 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002138
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002139 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2140 uint8_t ch = qemu_get_byte(f);
2141 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2142 } else if (flags & RAM_SAVE_FLAG_PAGE)
2143 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2144 } while (!(flags & RAM_SAVE_FLAG_EOS));
2145
2146 return 0;
2147}
2148
2149void qemu_service_io(void)
2150{
2151 qemu_notify_event();
2152}
2153
2154/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002155/* machine registration */
2156
2157static QEMUMachine *first_machine = NULL;
2158QEMUMachine *current_machine = NULL;
2159
2160int qemu_register_machine(QEMUMachine *m)
2161{
2162 QEMUMachine **pm;
2163 pm = &first_machine;
2164 while (*pm != NULL)
2165 pm = &(*pm)->next;
2166 m->next = NULL;
2167 *pm = m;
2168 return 0;
2169}
2170
2171static QEMUMachine *find_machine(const char *name)
2172{
2173 QEMUMachine *m;
2174
2175 for(m = first_machine; m != NULL; m = m->next) {
2176 if (!strcmp(m->name, name))
2177 return m;
2178 }
2179 return NULL;
2180}
2181
2182static QEMUMachine *find_default_machine(void)
2183{
2184 QEMUMachine *m;
2185
2186 for(m = first_machine; m != NULL; m = m->next) {
2187 if (m->is_default) {
2188 return m;
2189 }
2190 }
2191 return NULL;
2192}
2193
2194/***********************************************************/
2195/* main execution loop */
2196
2197static void gui_update(void *opaque)
2198{
2199 uint64_t interval = GUI_REFRESH_INTERVAL;
2200 DisplayState *ds = opaque;
2201 DisplayChangeListener *dcl = ds->listeners;
2202
2203 dpy_refresh(ds);
2204
2205 while (dcl != NULL) {
2206 if (dcl->gui_timer_interval &&
2207 dcl->gui_timer_interval < interval)
2208 interval = dcl->gui_timer_interval;
2209 dcl = dcl->next;
2210 }
2211 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
2212}
2213
2214static void nographic_update(void *opaque)
2215{
2216 uint64_t interval = GUI_REFRESH_INTERVAL;
2217
2218 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
2219}
2220
2221struct vm_change_state_entry {
2222 VMChangeStateHandler *cb;
2223 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002224 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002225};
2226
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002227static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002228
2229VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2230 void *opaque)
2231{
2232 VMChangeStateEntry *e;
2233
2234 e = qemu_mallocz(sizeof (*e));
2235
2236 e->cb = cb;
2237 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002238 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002239 return e;
2240}
2241
2242void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2243{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002244 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002245 qemu_free (e);
2246}
2247
2248static void vm_state_notify(int running, int reason)
2249{
2250 VMChangeStateEntry *e;
2251
2252 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2253 e->cb(e->opaque, running, reason);
2254 }
2255}
2256
2257static void resume_all_vcpus(void);
2258static void pause_all_vcpus(void);
2259
2260void vm_start(void)
2261{
2262 if (!vm_running) {
2263 cpu_enable_ticks();
2264 vm_running = 1;
2265 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002266 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002267 resume_all_vcpus();
2268 }
2269}
2270
2271/* reset/shutdown handler */
2272
2273typedef struct QEMUResetEntry {
2274 QEMUResetHandler *func;
2275 void *opaque;
2276 int order;
2277 struct QEMUResetEntry *next;
2278} QEMUResetEntry;
2279
2280static QEMUResetEntry *first_reset_entry;
2281static int reset_requested;
2282static int shutdown_requested;
2283static int powerdown_requested;
2284static int debug_requested;
2285static int vmstop_requested;
2286
2287int qemu_shutdown_requested(void)
2288{
2289 int r = shutdown_requested;
2290 shutdown_requested = 0;
2291 return r;
2292}
2293
2294int qemu_reset_requested(void)
2295{
2296 int r = reset_requested;
2297 reset_requested = 0;
2298 return r;
2299}
2300
2301int qemu_powerdown_requested(void)
2302{
2303 int r = powerdown_requested;
2304 powerdown_requested = 0;
2305 return r;
2306}
2307
2308static int qemu_debug_requested(void)
2309{
2310 int r = debug_requested;
2311 debug_requested = 0;
2312 return r;
2313}
2314
2315static int qemu_vmstop_requested(void)
2316{
2317 int r = vmstop_requested;
2318 vmstop_requested = 0;
2319 return r;
2320}
2321
2322static void do_vm_stop(int reason)
2323{
2324 if (vm_running) {
2325 cpu_disable_ticks();
2326 vm_running = 0;
2327 pause_all_vcpus();
2328 vm_state_notify(0, reason);
2329 }
2330}
2331
2332void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2333{
2334 QEMUResetEntry **pre, *re;
2335
2336 pre = &first_reset_entry;
2337 while (*pre != NULL && (*pre)->order >= order) {
2338 pre = &(*pre)->next;
2339 }
2340 re = qemu_mallocz(sizeof(QEMUResetEntry));
2341 re->func = func;
2342 re->opaque = opaque;
2343 re->order = order;
2344 re->next = NULL;
2345 *pre = re;
2346}
2347
2348void qemu_system_reset(void)
2349{
2350 QEMUResetEntry *re;
2351
2352 /* reset all devices */
2353 for(re = first_reset_entry; re != NULL; re = re->next) {
2354 re->func(re->opaque);
2355 }
2356}
2357
2358void qemu_system_reset_request(void)
2359{
2360 if (no_reboot) {
2361 shutdown_requested = 1;
2362 } else {
2363 reset_requested = 1;
2364 }
2365 qemu_notify_event();
2366}
2367
2368void qemu_system_shutdown_request(void)
2369{
2370 shutdown_requested = 1;
2371 qemu_notify_event();
2372}
2373
2374void qemu_system_powerdown_request(void)
2375{
2376 powerdown_requested = 1;
2377 qemu_notify_event();
2378}
2379
2380#ifdef CONFIG_IOTHREAD
2381static void qemu_system_vmstop_request(int reason)
2382{
2383 vmstop_requested = reason;
2384 qemu_notify_event();
2385}
2386#endif
2387
2388#ifndef _WIN32
2389static int io_thread_fd = -1;
2390
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002391#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002392static void qemu_event_increment(void)
2393{
2394 static const char byte = 0;
2395
2396 if (io_thread_fd == -1)
2397 return;
2398
2399 write(io_thread_fd, &byte, sizeof(byte));
2400}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002401#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002402
2403static void qemu_event_read(void *opaque)
2404{
2405 int fd = (unsigned long)opaque;
2406 ssize_t len;
2407
2408 /* Drain the notify pipe */
2409 do {
2410 char buffer[512];
2411 len = read(fd, buffer, sizeof(buffer));
2412 } while ((len == -1 && errno == EINTR) || len > 0);
2413}
2414
2415static int qemu_event_init(void)
2416{
2417 int err;
2418 int fds[2];
2419
2420 err = pipe(fds);
2421 if (err == -1)
2422 return -errno;
2423
2424 err = fcntl_setfl(fds[0], O_NONBLOCK);
2425 if (err < 0)
2426 goto fail;
2427
2428 err = fcntl_setfl(fds[1], O_NONBLOCK);
2429 if (err < 0)
2430 goto fail;
2431
2432 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2433 (void *)(unsigned long)fds[0]);
2434
2435 io_thread_fd = fds[1];
2436 return 0;
2437
2438fail:
2439 close(fds[0]);
2440 close(fds[1]);
2441 return err;
2442}
2443#else
2444HANDLE qemu_event_handle;
2445
2446static void dummy_event_handler(void *opaque)
2447{
2448}
2449
2450static int qemu_event_init(void)
2451{
2452 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2453 if (!qemu_event_handle) {
2454 perror("Failed CreateEvent");
2455 return -1;
2456 }
2457 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2458 return 0;
2459}
2460
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002461#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002462static void qemu_event_increment(void)
2463{
2464 SetEvent(qemu_event_handle);
2465}
2466#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002467#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002468
2469static int cpu_can_run(CPUState *env)
2470{
2471 if (env->stop)
2472 return 0;
2473 if (env->stopped)
2474 return 0;
2475 return 1;
2476}
2477
2478#ifndef CONFIG_IOTHREAD
2479static int qemu_init_main_loop(void)
2480{
2481 return qemu_event_init();
2482}
2483
2484void qemu_init_vcpu(void *_env)
2485{
2486 CPUState *env = _env;
2487
2488 if (kvm_enabled())
2489 kvm_init_vcpu(env);
2490 return;
2491}
2492
2493int qemu_cpu_self(void *env)
2494{
2495 return 1;
2496}
2497
2498static void resume_all_vcpus(void)
2499{
2500}
2501
2502static void pause_all_vcpus(void)
2503{
2504}
2505
2506void qemu_cpu_kick(void *env)
2507{
2508 return;
2509}
2510
2511void qemu_notify_event(void)
2512{
2513 CPUState *env = cpu_single_env;
2514
2515 if (env) {
2516 cpu_exit(env);
2517#ifdef USE_KQEMU
2518 if (env->kqemu_enabled)
2519 kqemu_cpu_interrupt(env);
2520#endif
2521 }
2522}
2523
2524#define qemu_mutex_lock_iothread() do { } while (0)
2525#define qemu_mutex_unlock_iothread() do { } while (0)
2526
2527void vm_stop(int reason)
2528{
2529 do_vm_stop(reason);
2530}
2531
2532#else /* CONFIG_IOTHREAD */
2533
2534#include "qemu-thread.h"
2535
2536QemuMutex qemu_global_mutex;
2537static QemuMutex qemu_fair_mutex;
2538
2539static QemuThread io_thread;
2540
2541static QemuThread *tcg_cpu_thread;
2542static QemuCond *tcg_halt_cond;
2543
2544static int qemu_system_ready;
2545/* cpu creation */
2546static QemuCond qemu_cpu_cond;
2547/* system init */
2548static QemuCond qemu_system_cond;
2549static QemuCond qemu_pause_cond;
2550
2551static void block_io_signals(void);
2552static void unblock_io_signals(void);
2553static int tcg_has_work(void);
2554
2555static int qemu_init_main_loop(void)
2556{
2557 int ret;
2558
2559 ret = qemu_event_init();
2560 if (ret)
2561 return ret;
2562
2563 qemu_cond_init(&qemu_pause_cond);
2564 qemu_mutex_init(&qemu_fair_mutex);
2565 qemu_mutex_init(&qemu_global_mutex);
2566 qemu_mutex_lock(&qemu_global_mutex);
2567
2568 unblock_io_signals();
2569 qemu_thread_self(&io_thread);
2570
2571 return 0;
2572}
2573
2574static void qemu_wait_io_event(CPUState *env)
2575{
2576 while (!tcg_has_work())
2577 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2578
2579 qemu_mutex_unlock(&qemu_global_mutex);
2580
2581 /*
2582 * Users of qemu_global_mutex can be starved, having no chance
2583 * to acquire it since this path will get to it first.
2584 * So use another lock to provide fairness.
2585 */
2586 qemu_mutex_lock(&qemu_fair_mutex);
2587 qemu_mutex_unlock(&qemu_fair_mutex);
2588
2589 qemu_mutex_lock(&qemu_global_mutex);
2590 if (env->stop) {
2591 env->stop = 0;
2592 env->stopped = 1;
2593 qemu_cond_signal(&qemu_pause_cond);
2594 }
2595}
2596
2597static int qemu_cpu_exec(CPUState *env);
2598
2599static void *kvm_cpu_thread_fn(void *arg)
2600{
2601 CPUState *env = arg;
2602
2603 block_io_signals();
2604 qemu_thread_self(env->thread);
2605
2606 /* signal CPU creation */
2607 qemu_mutex_lock(&qemu_global_mutex);
2608 env->created = 1;
2609 qemu_cond_signal(&qemu_cpu_cond);
2610
2611 /* and wait for machine initialization */
2612 while (!qemu_system_ready)
2613 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2614
2615 while (1) {
2616 if (cpu_can_run(env))
2617 qemu_cpu_exec(env);
2618 qemu_wait_io_event(env);
2619 }
2620
2621 return NULL;
2622}
2623
2624static void tcg_cpu_exec(void);
2625
2626static void *tcg_cpu_thread_fn(void *arg)
2627{
2628 CPUState *env = arg;
2629
2630 block_io_signals();
2631 qemu_thread_self(env->thread);
2632
2633 /* signal CPU creation */
2634 qemu_mutex_lock(&qemu_global_mutex);
2635 for (env = first_cpu; env != NULL; env = env->next_cpu)
2636 env->created = 1;
2637 qemu_cond_signal(&qemu_cpu_cond);
2638
2639 /* and wait for machine initialization */
2640 while (!qemu_system_ready)
2641 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2642
2643 while (1) {
2644 tcg_cpu_exec();
2645 qemu_wait_io_event(cur_cpu);
2646 }
2647
2648 return NULL;
2649}
2650
2651void qemu_cpu_kick(void *_env)
2652{
2653 CPUState *env = _env;
2654 qemu_cond_broadcast(env->halt_cond);
2655 if (kvm_enabled())
2656 qemu_thread_signal(env->thread, SIGUSR1);
2657}
2658
2659int qemu_cpu_self(void *env)
2660{
2661 return (cpu_single_env != NULL);
2662}
2663
2664static void cpu_signal(int sig)
2665{
2666 if (cpu_single_env)
2667 cpu_exit(cpu_single_env);
2668}
2669
2670static void block_io_signals(void)
2671{
2672 sigset_t set;
2673 struct sigaction sigact;
2674
2675 sigemptyset(&set);
2676 sigaddset(&set, SIGUSR2);
2677 sigaddset(&set, SIGIO);
2678 sigaddset(&set, SIGALRM);
2679 pthread_sigmask(SIG_BLOCK, &set, NULL);
2680
2681 sigemptyset(&set);
2682 sigaddset(&set, SIGUSR1);
2683 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2684
2685 memset(&sigact, 0, sizeof(sigact));
2686 sigact.sa_handler = cpu_signal;
2687 sigaction(SIGUSR1, &sigact, NULL);
2688}
2689
2690static void unblock_io_signals(void)
2691{
2692 sigset_t set;
2693
2694 sigemptyset(&set);
2695 sigaddset(&set, SIGUSR2);
2696 sigaddset(&set, SIGIO);
2697 sigaddset(&set, SIGALRM);
2698 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2699
2700 sigemptyset(&set);
2701 sigaddset(&set, SIGUSR1);
2702 pthread_sigmask(SIG_BLOCK, &set, NULL);
2703}
2704
2705static void qemu_signal_lock(unsigned int msecs)
2706{
2707 qemu_mutex_lock(&qemu_fair_mutex);
2708
2709 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2710 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2711 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2712 break;
2713 }
2714 qemu_mutex_unlock(&qemu_fair_mutex);
2715}
2716
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002717void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002718{
2719 if (kvm_enabled()) {
2720 qemu_mutex_lock(&qemu_fair_mutex);
2721 qemu_mutex_lock(&qemu_global_mutex);
2722 qemu_mutex_unlock(&qemu_fair_mutex);
2723 } else
2724 qemu_signal_lock(100);
2725}
2726
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002727void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002728{
2729 qemu_mutex_unlock(&qemu_global_mutex);
2730}
2731
2732static int all_vcpus_paused(void)
2733{
2734 CPUState *penv = first_cpu;
2735
2736 while (penv) {
2737 if (!penv->stopped)
2738 return 0;
2739 penv = (CPUState *)penv->next_cpu;
2740 }
2741
2742 return 1;
2743}
2744
2745static void pause_all_vcpus(void)
2746{
2747 CPUState *penv = first_cpu;
2748
2749 while (penv) {
2750 penv->stop = 1;
2751 qemu_thread_signal(penv->thread, SIGUSR1);
2752 qemu_cpu_kick(penv);
2753 penv = (CPUState *)penv->next_cpu;
2754 }
2755
2756 while (!all_vcpus_paused()) {
2757 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2758 penv = first_cpu;
2759 while (penv) {
2760 qemu_thread_signal(penv->thread, SIGUSR1);
2761 penv = (CPUState *)penv->next_cpu;
2762 }
2763 }
2764}
2765
2766static void resume_all_vcpus(void)
2767{
2768 CPUState *penv = first_cpu;
2769
2770 while (penv) {
2771 penv->stop = 0;
2772 penv->stopped = 0;
2773 qemu_thread_signal(penv->thread, SIGUSR1);
2774 qemu_cpu_kick(penv);
2775 penv = (CPUState *)penv->next_cpu;
2776 }
2777}
2778
2779static void tcg_init_vcpu(void *_env)
2780{
2781 CPUState *env = _env;
2782 /* share a single thread for all cpus with TCG */
2783 if (!tcg_cpu_thread) {
2784 env->thread = qemu_mallocz(sizeof(QemuThread));
2785 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2786 qemu_cond_init(env->halt_cond);
2787 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2788 while (env->created == 0)
2789 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2790 tcg_cpu_thread = env->thread;
2791 tcg_halt_cond = env->halt_cond;
2792 } else {
2793 env->thread = tcg_cpu_thread;
2794 env->halt_cond = tcg_halt_cond;
2795 }
2796}
2797
2798static void kvm_start_vcpu(CPUState *env)
2799{
2800#if 0
2801 kvm_init_vcpu(env);
2802 env->thread = qemu_mallocz(sizeof(QemuThread));
2803 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2804 qemu_cond_init(env->halt_cond);
2805 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2806 while (env->created == 0)
2807 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2808#endif
2809}
2810
2811void qemu_init_vcpu(void *_env)
2812{
2813 CPUState *env = _env;
2814
2815 if (kvm_enabled())
2816 kvm_start_vcpu(env);
2817 else
2818 tcg_init_vcpu(env);
2819}
2820
2821void qemu_notify_event(void)
2822{
2823 qemu_event_increment();
2824}
2825
2826void vm_stop(int reason)
2827{
2828 QemuThread me;
2829 qemu_thread_self(&me);
2830
2831 if (!qemu_thread_equal(&me, &io_thread)) {
2832 qemu_system_vmstop_request(reason);
2833 /*
2834 * FIXME: should not return to device code in case
2835 * vm_stop() has been requested.
2836 */
2837 if (cpu_single_env) {
2838 cpu_exit(cpu_single_env);
2839 cpu_single_env->stop = 1;
2840 }
2841 return;
2842 }
2843 do_vm_stop(reason);
2844}
2845
2846#endif
2847
2848
2849#ifdef _WIN32
2850static void host_main_loop_wait(int *timeout)
2851{
2852 int ret, ret2, i;
2853 PollingEntry *pe;
2854
2855
2856 /* XXX: need to suppress polling by better using win32 events */
2857 ret = 0;
2858 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2859 ret |= pe->func(pe->opaque);
2860 }
2861 if (ret == 0) {
2862 int err;
2863 WaitObjects *w = &wait_objects;
2864
2865 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2866 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2867 if (w->func[ret - WAIT_OBJECT_0])
2868 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2869
2870 /* Check for additional signaled events */
2871 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2872
2873 /* Check if event is signaled */
2874 ret2 = WaitForSingleObject(w->events[i], 0);
2875 if(ret2 == WAIT_OBJECT_0) {
2876 if (w->func[i])
2877 w->func[i](w->opaque[i]);
2878 } else if (ret2 == WAIT_TIMEOUT) {
2879 } else {
2880 err = GetLastError();
2881 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2882 }
2883 }
2884 } else if (ret == WAIT_TIMEOUT) {
2885 } else {
2886 err = GetLastError();
2887 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2888 }
2889 }
2890
2891 *timeout = 0;
2892}
2893#else
2894static void host_main_loop_wait(int *timeout)
2895{
2896}
2897#endif
2898
2899void main_loop_wait(int timeout)
2900{
2901 IOHandlerRecord *ioh;
2902 fd_set rfds, wfds, xfds;
2903 int ret, nfds;
2904 struct timeval tv;
2905
2906 qemu_bh_update_timeout(&timeout);
2907
2908 host_main_loop_wait(&timeout);
2909
2910 /* poll any events */
2911 /* XXX: separate device handlers from system ones */
2912 nfds = -1;
2913 FD_ZERO(&rfds);
2914 FD_ZERO(&wfds);
2915 FD_ZERO(&xfds);
2916 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2917 if (ioh->deleted)
2918 continue;
2919 if (ioh->fd_read &&
2920 (!ioh->fd_read_poll ||
2921 ioh->fd_read_poll(ioh->opaque) != 0)) {
2922 FD_SET(ioh->fd, &rfds);
2923 if (ioh->fd > nfds)
2924 nfds = ioh->fd;
2925 }
2926 if (ioh->fd_write) {
2927 FD_SET(ioh->fd, &wfds);
2928 if (ioh->fd > nfds)
2929 nfds = ioh->fd;
2930 }
2931 }
2932
2933 tv.tv_sec = timeout / 1000;
2934 tv.tv_usec = (timeout % 1000) * 1000;
2935
2936#if defined(CONFIG_SLIRP)
2937 if (slirp_is_inited()) {
2938 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2939 }
2940#endif
2941 qemu_mutex_unlock_iothread();
2942 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2943 qemu_mutex_lock_iothread();
2944 if (ret > 0) {
2945 IOHandlerRecord **pioh;
2946
2947 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2948 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2949 ioh->fd_read(ioh->opaque);
2950 }
2951 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2952 ioh->fd_write(ioh->opaque);
2953 }
2954 }
2955
David 'Digit' Turner6b512812010-10-15 15:05:04 +02002956 /* remove deleted IO handlers */
2957 pioh = &first_io_handler;
2958 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002959 ioh = *pioh;
2960 if (ioh->deleted) {
2961 *pioh = ioh->next;
2962 qemu_free(ioh);
2963 } else
2964 pioh = &ioh->next;
2965 }
2966 }
2967#if defined(CONFIG_SLIRP)
2968 if (slirp_is_inited()) {
2969 if (ret < 0) {
2970 FD_ZERO(&rfds);
2971 FD_ZERO(&wfds);
2972 FD_ZERO(&xfds);
2973 }
2974 slirp_select_poll(&rfds, &wfds, &xfds);
2975 }
2976#endif
2977 charpipe_poll();
2978
David Turner6a9ef172010-09-09 22:54:36 +02002979 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07002980
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002981 /* Check bottom-halves last in case any of the earlier events triggered
2982 them. */
2983 qemu_bh_poll();
2984
2985}
2986
2987static int qemu_cpu_exec(CPUState *env)
2988{
2989 int ret;
2990#ifdef CONFIG_PROFILER
2991 int64_t ti;
2992#endif
2993
2994#ifdef CONFIG_PROFILER
2995 ti = profile_getclock();
2996#endif
2997 if (use_icount) {
2998 int64_t count;
2999 int decr;
3000 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3001 env->icount_decr.u16.low = 0;
3002 env->icount_extra = 0;
3003 count = qemu_next_deadline();
3004 count = (count + (1 << icount_time_shift) - 1)
3005 >> icount_time_shift;
3006 qemu_icount += count;
3007 decr = (count > 0xffff) ? 0xffff : count;
3008 count -= decr;
3009 env->icount_decr.u16.low = decr;
3010 env->icount_extra = count;
3011 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003012#ifdef CONFIG_TRACE
3013 if (tbflush_requested) {
3014 tbflush_requested = 0;
3015 tb_flush(env);
3016 return EXCP_INTERRUPT;
3017 }
3018#endif
3019
3020
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003021 ret = cpu_exec(env);
3022#ifdef CONFIG_PROFILER
3023 qemu_time += profile_getclock() - ti;
3024#endif
3025 if (use_icount) {
3026 /* Fold pending instructions back into the
3027 instruction counter, and clear the interrupt flag. */
3028 qemu_icount -= (env->icount_decr.u16.low
3029 + env->icount_extra);
3030 env->icount_decr.u32 = 0;
3031 env->icount_extra = 0;
3032 }
3033 return ret;
3034}
3035
3036static void tcg_cpu_exec(void)
3037{
3038 int ret = 0;
3039
3040 if (next_cpu == NULL)
3041 next_cpu = first_cpu;
3042 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3043 CPUState *env = cur_cpu = next_cpu;
3044
3045 if (!vm_running)
3046 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003047 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003048 break;
3049 }
3050 if (cpu_can_run(env))
3051 ret = qemu_cpu_exec(env);
3052 if (ret == EXCP_DEBUG) {
3053 gdb_set_stop_cpu(env);
3054 debug_requested = 1;
3055 break;
3056 }
3057 }
3058}
3059
3060static int cpu_has_work(CPUState *env)
3061{
3062 if (env->stop)
3063 return 1;
3064 if (env->stopped)
3065 return 0;
3066 if (!env->halted)
3067 return 1;
3068 if (qemu_cpu_has_work(env))
3069 return 1;
3070 return 0;
3071}
3072
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003073int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003074{
3075 CPUState *env;
3076
3077 for (env = first_cpu; env != NULL; env = env->next_cpu)
3078 if (cpu_has_work(env))
3079 return 1;
3080 return 0;
3081}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003082
3083static int vm_can_run(void)
3084{
3085 if (powerdown_requested)
3086 return 0;
3087 if (reset_requested)
3088 return 0;
3089 if (shutdown_requested)
3090 return 0;
3091 if (debug_requested)
3092 return 0;
3093 return 1;
3094}
3095
3096static void main_loop(void)
3097{
3098 int r;
3099
3100#ifdef CONFIG_IOTHREAD
3101 qemu_system_ready = 1;
3102 qemu_cond_broadcast(&qemu_system_cond);
3103#endif
3104
3105 for (;;) {
3106 do {
3107#ifdef CONFIG_PROFILER
3108 int64_t ti;
3109#endif
3110#ifndef CONFIG_IOTHREAD
3111 tcg_cpu_exec();
3112#endif
3113#ifdef CONFIG_PROFILER
3114 ti = profile_getclock();
3115#endif
3116 main_loop_wait(qemu_calculate_timeout());
3117#ifdef CONFIG_PROFILER
3118 dev_time += profile_getclock() - ti;
3119#endif
3120 } while (vm_can_run());
3121
3122 if (qemu_debug_requested())
3123 vm_stop(EXCP_DEBUG);
3124 if (qemu_shutdown_requested()) {
3125 if (no_shutdown) {
3126 vm_stop(0);
3127 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003128 } else {
Tim Baverstock24204cc2010-11-25 11:37:43 +00003129 if (savevm_on_exit != NULL) {
3130 do_savevm(cur_mon, savevm_on_exit);
3131 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003132 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003133 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003134 }
3135 if (qemu_reset_requested()) {
3136 pause_all_vcpus();
3137 qemu_system_reset();
3138 resume_all_vcpus();
3139 }
3140 if (qemu_powerdown_requested())
3141 qemu_system_powerdown();
3142 if ((r = qemu_vmstop_requested()))
3143 vm_stop(r);
3144 }
3145 pause_all_vcpus();
3146}
3147
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003148void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003149{
3150 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3151}
3152
3153void qemu_help(int exitcode)
3154{
3155 version();
3156 printf("usage: %s [options] [disk_image]\n"
3157 "\n"
3158 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3159 "\n"
3160#define DEF(option, opt_arg, opt_enum, opt_help) \
3161 opt_help
3162#define DEFHEADING(text) stringify(text) "\n"
3163#include "qemu-options.h"
3164#undef DEF
3165#undef DEFHEADING
3166#undef GEN_DOCS
3167 "\n"
3168 "During emulation, the following keys are useful:\n"
3169 "ctrl-alt-f toggle full screen\n"
3170 "ctrl-alt-n switch to virtual console 'n'\n"
3171 "ctrl-alt toggle mouse and keyboard grab\n"
3172 "\n"
3173 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3174 ,
3175 "qemu",
3176 DEFAULT_RAM_SIZE,
3177#ifndef _WIN32
3178 DEFAULT_NETWORK_SCRIPT,
3179 DEFAULT_NETWORK_DOWN_SCRIPT,
3180#endif
3181 DEFAULT_GDBSTUB_PORT,
3182 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003183 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003184}
3185
3186#define HAS_ARG 0x0001
3187
3188enum {
3189#define DEF(option, opt_arg, opt_enum, opt_help) \
3190 opt_enum,
3191#define DEFHEADING(text)
3192#include "qemu-options.h"
3193#undef DEF
3194#undef DEFHEADING
3195#undef GEN_DOCS
3196};
3197
3198typedef struct QEMUOption {
3199 const char *name;
3200 int flags;
3201 int index;
3202} QEMUOption;
3203
3204static const QEMUOption qemu_options[] = {
3205 { "h", 0, QEMU_OPTION_h },
3206#define DEF(option, opt_arg, opt_enum, opt_help) \
3207 { option, opt_arg, opt_enum },
3208#define DEFHEADING(text)
3209#include "qemu-options.h"
3210#undef DEF
3211#undef DEFHEADING
3212#undef GEN_DOCS
3213 { NULL, 0, 0 },
3214};
3215
3216#ifdef HAS_AUDIO
3217struct soundhw soundhw[] = {
3218#ifdef HAS_AUDIO_CHOICE
3219#if defined(TARGET_I386) || defined(TARGET_MIPS)
3220 {
3221 "pcspk",
3222 "PC speaker",
3223 0,
3224 1,
3225 { .init_isa = pcspk_audio_init }
3226 },
3227#endif
3228
3229#ifdef CONFIG_SB16
3230 {
3231 "sb16",
3232 "Creative Sound Blaster 16",
3233 0,
3234 1,
3235 { .init_isa = SB16_init }
3236 },
3237#endif
3238
3239#ifdef CONFIG_CS4231A
3240 {
3241 "cs4231a",
3242 "CS4231A",
3243 0,
3244 1,
3245 { .init_isa = cs4231a_init }
3246 },
3247#endif
3248
3249#ifdef CONFIG_ADLIB
3250 {
3251 "adlib",
3252#ifdef HAS_YMF262
3253 "Yamaha YMF262 (OPL3)",
3254#else
3255 "Yamaha YM3812 (OPL2)",
3256#endif
3257 0,
3258 1,
3259 { .init_isa = Adlib_init }
3260 },
3261#endif
3262
3263#ifdef CONFIG_GUS
3264 {
3265 "gus",
3266 "Gravis Ultrasound GF1",
3267 0,
3268 1,
3269 { .init_isa = GUS_init }
3270 },
3271#endif
3272
3273#ifdef CONFIG_AC97
3274 {
3275 "ac97",
3276 "Intel 82801AA AC97 Audio",
3277 0,
3278 0,
3279 { .init_pci = ac97_init }
3280 },
3281#endif
3282
3283#ifdef CONFIG_ES1370
3284 {
3285 "es1370",
3286 "ENSONIQ AudioPCI ES1370",
3287 0,
3288 0,
3289 { .init_pci = es1370_init }
3290 },
3291#endif
3292
3293#endif /* HAS_AUDIO_CHOICE */
3294
3295 { NULL, NULL, 0, 0, { NULL } }
3296};
3297
3298static void select_soundhw (const char *optarg)
3299{
3300 struct soundhw *c;
3301
3302 if (*optarg == '?') {
3303 show_valid_cards:
3304
3305 printf ("Valid sound card names (comma separated):\n");
3306 for (c = soundhw; c->name; ++c) {
3307 printf ("%-11s %s\n", c->name, c->descr);
3308 }
3309 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003310 if (*optarg != '?') {
3311 PANIC("Unknown sound card name: %s", optarg);
3312 } else {
3313 QEMU_EXIT(0);
3314 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003315 }
3316 else {
3317 size_t l;
3318 const char *p;
3319 char *e;
3320 int bad_card = 0;
3321
3322 if (!strcmp (optarg, "all")) {
3323 for (c = soundhw; c->name; ++c) {
3324 c->enabled = 1;
3325 }
3326 return;
3327 }
3328
3329 p = optarg;
3330 while (*p) {
3331 e = strchr (p, ',');
3332 l = !e ? strlen (p) : (size_t) (e - p);
3333
3334 for (c = soundhw; c->name; ++c) {
3335 if (!strncmp (c->name, p, l)) {
3336 c->enabled = 1;
3337 break;
3338 }
3339 }
3340
3341 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003342#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003343 if (l > 80) {
3344 fprintf (stderr,
3345 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003346 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003347 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3348 (int) l, p);
3349 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003350#endif // !CONFIG_ANDROID
3351 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003352 }
3353 p += l + (e != NULL);
3354 }
3355
3356 if (bad_card)
3357 goto show_valid_cards;
3358 }
3359}
3360#endif
3361
3362static void select_vgahw (const char *p)
3363{
3364 const char *opts;
3365
3366 cirrus_vga_enabled = 0;
3367 std_vga_enabled = 0;
3368 vmsvga_enabled = 0;
3369 xenfb_enabled = 0;
3370 if (strstart(p, "std", &opts)) {
3371 std_vga_enabled = 1;
3372 } else if (strstart(p, "cirrus", &opts)) {
3373 cirrus_vga_enabled = 1;
3374 } else if (strstart(p, "vmware", &opts)) {
3375 vmsvga_enabled = 1;
3376 } else if (strstart(p, "xenfb", &opts)) {
3377 xenfb_enabled = 1;
3378 } else if (!strstart(p, "none", &opts)) {
3379 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003380 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003381 }
3382 while (*opts) {
3383 const char *nextopt;
3384
3385 if (strstart(opts, ",retrace=", &nextopt)) {
3386 opts = nextopt;
3387 if (strstart(opts, "dumb", &nextopt))
3388 vga_retrace_method = VGA_RETRACE_DUMB;
3389 else if (strstart(opts, "precise", &nextopt))
3390 vga_retrace_method = VGA_RETRACE_PRECISE;
3391 else goto invalid_vga;
3392 } else goto invalid_vga;
3393 opts = nextopt;
3394 }
3395}
3396
3397#ifdef _WIN32
3398static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3399{
3400 exit(STATUS_CONTROL_C_EXIT);
3401 return TRUE;
3402}
3403#endif
3404
3405int qemu_uuid_parse(const char *str, uint8_t *uuid)
3406{
3407 int ret;
3408
3409 if(strlen(str) != 36)
3410 return -1;
3411
3412 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3413 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3414 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3415
3416 if(ret != 16)
3417 return -1;
3418
3419#ifdef TARGET_I386
3420 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3421#endif
3422
3423 return 0;
3424}
3425
3426#define MAX_NET_CLIENTS 32
3427
3428#ifndef _WIN32
3429
3430static void termsig_handler(int signal)
3431{
3432 qemu_system_shutdown_request();
3433}
3434
3435static void sigchld_handler(int signal)
3436{
3437 waitpid(-1, NULL, WNOHANG);
3438}
3439
3440static void sighandler_setup(void)
3441{
3442 struct sigaction act;
3443
3444 memset(&act, 0, sizeof(act));
3445 act.sa_handler = termsig_handler;
3446 sigaction(SIGINT, &act, NULL);
3447 sigaction(SIGHUP, &act, NULL);
3448 sigaction(SIGTERM, &act, NULL);
3449
3450 act.sa_handler = sigchld_handler;
3451 act.sa_flags = SA_NOCLDSTOP;
3452 sigaction(SIGCHLD, &act, NULL);
3453}
3454
3455#endif
3456
3457#ifdef _WIN32
3458/* Look for support files in the same directory as the executable. */
3459static char *find_datadir(const char *argv0)
3460{
3461 char *p;
3462 char buf[MAX_PATH];
3463 DWORD len;
3464
3465 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3466 if (len == 0) {
3467 return NULL;
3468 }
3469
3470 buf[len] = 0;
3471 p = buf + len - 1;
3472 while (p != buf && *p != '\\')
3473 p--;
3474 *p = 0;
3475 if (access(buf, R_OK) == 0) {
3476 return qemu_strdup(buf);
3477 }
3478 return NULL;
3479}
3480#else /* !_WIN32 */
3481
3482/* Find a likely location for support files using the location of the binary.
3483 For installed binaries this will be "$bindir/../share/qemu". When
3484 running from the build tree this will be "$bindir/../pc-bios". */
3485#define SHARE_SUFFIX "/share/qemu"
3486#define BUILD_SUFFIX "/pc-bios"
3487static char *find_datadir(const char *argv0)
3488{
3489 char *dir;
3490 char *p = NULL;
3491 char *res;
3492#ifdef PATH_MAX
3493 char buf[PATH_MAX];
3494#endif
3495 size_t max_len;
3496
3497#if defined(__linux__)
3498 {
3499 int len;
3500 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3501 if (len > 0) {
3502 buf[len] = 0;
3503 p = buf;
3504 }
3505 }
3506#elif defined(__FreeBSD__)
3507 {
3508 int len;
3509 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3510 if (len > 0) {
3511 buf[len] = 0;
3512 p = buf;
3513 }
3514 }
3515#endif
3516 /* If we don't have any way of figuring out the actual executable
3517 location then try argv[0]. */
3518 if (!p) {
3519#ifdef PATH_MAX
3520 p = buf;
3521#endif
3522 p = realpath(argv0, p);
3523 if (!p) {
3524 return NULL;
3525 }
3526 }
3527 dir = dirname(p);
3528 dir = dirname(dir);
3529
3530 max_len = strlen(dir) +
3531 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
3532 res = qemu_mallocz(max_len);
3533 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
3534 if (access(res, R_OK)) {
3535 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
3536 if (access(res, R_OK)) {
3537 qemu_free(res);
3538 res = NULL;
3539 }
3540 }
3541#ifndef PATH_MAX
3542 free(p);
3543#endif
3544 return res;
3545}
3546#undef SHARE_SUFFIX
3547#undef BUILD_SUFFIX
3548#endif
3549
3550char *qemu_find_file(int type, const char *name)
3551{
3552 int len;
3553 const char *subdir;
3554 char *buf;
3555
3556 /* If name contains path separators then try it as a straight path. */
3557 if ((strchr(name, '/') || strchr(name, '\\'))
3558 && access(name, R_OK) == 0) {
3559 return strdup(name);
3560 }
3561 switch (type) {
3562 case QEMU_FILE_TYPE_BIOS:
3563 subdir = "";
3564 break;
3565 case QEMU_FILE_TYPE_KEYMAP:
3566 subdir = "keymaps/";
3567 break;
3568 default:
3569 abort();
3570 }
3571 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3572 buf = qemu_mallocz(len);
3573 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3574 if (access(buf, R_OK)) {
3575 qemu_free(buf);
3576 return NULL;
3577 }
3578 return buf;
3579}
3580
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003581static int
3582add_dns_server( const char* server_name )
3583{
3584 SockAddress addr;
3585
3586 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3587 fprintf(stdout,
3588 "### WARNING: can't resolve DNS server name '%s'\n",
3589 server_name );
3590 return -1;
3591 }
3592
3593 fprintf(stderr,
3594 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3595
3596 if ( slirp_add_dns_server( &addr ) < 0 ) {
3597 fprintf(stderr,
3598 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3599 return -1;
3600 }
3601 return 0;
3602}
3603
rich cannings7339b552011-02-16 13:43:44 -08003604/* Parses an integer
3605 * Pararm:
3606 * str String containing a number to be parsed.
3607 * result Passes the parsed integer in this argument
3608 * returns 0 if ok, -1 if failed
3609 */
3610int
3611parse_int(const char *str, int *result)
3612{
3613 char* r;
3614 *result = strtol(str, &r, 0);
3615 if (r == NULL || *r != '\0')
3616 return -1;
3617
3618 return 0;
3619}
3620
3621
3622/* parses a null-terminated string specifying a network port (e.g., "80") or
3623 * port range (e.g., "[6666-7000]"). In case of a single port, lport and hport
3624 * are the same. Returns 0 on success, -1 on error. */
3625
3626int parse_port_range(const char *str, unsigned short *lport,
3627 unsigned short *hport) {
3628
3629 unsigned int low = 0, high = 0;
3630 char *p, *arg = strdup(str);
3631
3632 if ((*arg == '[') && ((p = strrchr(arg, ']')) != NULL)) {
3633 p = arg + 1; /* skip '[' */
3634 low = atoi(strtok(p, "-"));
3635 high = atoi(strtok(NULL, "-"));
3636 if ((low > 0) && (high > 0) && (low < high) && (high < 65535)) {
3637 *lport = low;
3638 *hport = high;
3639 }
3640 }
3641 else {
3642 low = atoi(arg);
3643 if ((0 < low) && (low < 65535)) {
3644 *lport = low;
3645 *hport = low;
3646 }
3647 }
3648 free(arg);
3649 if (low != 0)
3650 return 0;
3651 return -1;
3652}
3653
3654/*
3655 * Implements the generic port forwarding option
3656 */
3657void
3658net_slirp_forward(const char *optarg)
3659{
3660 /*
3661 * we expect the following format:
3662 * dst_net:dst_mask:dst_port:redirect_ip:redirect_port OR
3663 * dst_net:dst_mask:[dp_range_start-dp_range_end]:redirect_ip:redirect_port
3664 */
3665 char *argument = strdup(optarg), *p = argument;
3666 char *dst_net, *dst_mask, *dst_port;
3667 char *redirect_ip, *redirect_port;
3668 uint32_t dnet, dmask, rip;
3669 unsigned short dlport, dhport, rport;
3670
3671
3672 dst_net = strtok(p, ":");
3673 dst_mask = strtok(NULL, ":");
3674 dst_port = strtok(NULL, ":");
3675 redirect_ip = strtok(NULL, ":");
3676 redirect_port = strtok(NULL, ":");
3677
3678 if (dst_net == NULL || dst_mask == NULL || dst_port == NULL ||
3679 redirect_ip == NULL || redirect_port == NULL) {
3680 fprintf(stderr,
3681 "Invalid argument for -net-forward, we expect "
3682 "dst_net:dst_mask:dst_port:redirect_ip:redirect_port or "
3683 "dst_net:dst_mask:[dp_range_start-dp_range_end]"
3684 ":redirect_ip:redirect_port: %s\n",
3685 optarg);
3686 exit(1);
3687 }
3688
3689 /* inet_strtoip converts dotted address to host byte order */
3690 if (inet_strtoip(dst_net, &dnet) == -1) {
3691 fprintf(stderr, "Invalid destination IP net: %s\n", dst_net);
3692 exit(1);
3693 }
3694 if (inet_strtoip(dst_mask, &dmask) == -1) {
3695 fprintf(stderr, "Invalid destination IP mask: %s\n", dst_mask);
3696 exit(1);
3697 }
3698 if (inet_strtoip(redirect_ip, &rip) == -1) {
3699 fprintf(stderr, "Invalid redirect IP address: %s\n", redirect_ip);
3700 exit(1);
3701 }
3702
3703 if (parse_port_range(dst_port, &dlport, &dhport) == -1) {
3704 fprintf(stderr, "Invalid destination port or port range\n");
3705 exit(1);
3706 }
3707
3708 rport = atoi(redirect_port);
3709 if (!rport) {
3710 fprintf(stderr, "Invalid redirect port: %s\n", redirect_port);
3711 exit(1);
3712 }
3713
3714 dnet &= dmask;
3715
3716 slirp_add_net_forward(dnet, dmask, dlport, dhport,
3717 rip, rport);
3718
3719 free(argument);
3720}
3721
3722
3723/* Parses an -allow-tcp or -allow-udp argument and inserts a corresponding
3724 * entry in the allows list */
3725void
3726slirp_allow(const char *optarg, u_int8_t proto)
3727{
3728 /*
3729 * we expect the following format:
3730 * dst_ip:dst_port OR dst_ip:[dst_lport-dst_hport]
3731 */
3732 char *argument = strdup(optarg), *p = argument;
3733 char *dst_ip_str, *dst_port_str;
3734 uint32_t dst_ip;
3735 unsigned short dst_lport, dst_hport;
3736
3737 dst_ip_str = strtok(p, ":");
3738 dst_port_str = strtok(NULL, ":");
3739
3740 if (dst_ip_str == NULL || dst_port_str == NULL) {
3741 fprintf(stderr,
3742 "Invalid argument %s for -allow. We expect "
3743 "dst_ip:dst_port or dst_ip:[dst_lport-dst_hport]\n",
3744 optarg);
3745 exit(1);
3746 }
3747
3748 if (inet_strtoip(dst_ip_str, &dst_ip) == -1) {
3749 fprintf(stderr, "Invalid destination IP address: %s\n", dst_ip_str);
3750 exit(1);
3751 }
3752 if (parse_port_range(dst_port_str, &dst_lport, &dst_hport) == -1) {
3753 fprintf(stderr, "Invalid destination port or port range\n");
3754 exit(1);
3755 }
3756
3757 slirp_add_allow(dst_ip, dst_lport, dst_hport, proto);
3758
3759 free(argument);
3760}
3761
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003762int main(int argc, char **argv, char **envp)
3763{
3764 const char *gdbstub_dev = NULL;
3765 uint32_t boot_devices_bitmap = 0;
3766 int i;
3767 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003768 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003769 const char *initrd_filename;
3770 const char *kernel_filename, *kernel_cmdline;
3771 const char *boot_devices = "";
3772 DisplayState *ds;
3773 DisplayChangeListener *dcl;
3774 int cyls, heads, secs, translation;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003775 QemuOpts *hda_opts = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003776 const char *net_clients[MAX_NET_CLIENTS];
3777 int nb_net_clients;
3778 const char *bt_opts[MAX_BT_CMDLINE];
3779 int nb_bt_opts;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003780 int optind;
3781 const char *r, *optarg;
3782 CharDriverState *monitor_hd = NULL;
3783 const char *monitor_device;
3784 const char *serial_devices[MAX_SERIAL_PORTS];
3785 int serial_device_index;
3786 const char *parallel_devices[MAX_PARALLEL_PORTS];
3787 int parallel_device_index;
3788 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3789 int virtio_console_index;
3790 const char *loadvm = NULL;
3791 QEMUMachine *machine;
3792 const char *cpu_model;
3793 const char *usb_devices[MAX_USB_CMDLINE];
3794 int usb_devices_index;
3795#ifndef _WIN32
3796 int fds[2];
3797#endif
3798 int tb_size;
3799 const char *pid_file = NULL;
3800 const char *incoming = NULL;
3801#ifndef _WIN32
3802 int fd = 0;
3803 struct passwd *pwd = NULL;
3804 const char *chroot_dir = NULL;
3805 const char *run_as = NULL;
3806#endif
3807 CPUState *env;
3808 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003809 IniFile* hw_ini = NULL;
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003810 STRALLOC_DEFINE(kernel_params);
3811 STRALLOC_DEFINE(kernel_config);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003812 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003813
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003814 /* Initialize sockets before anything else, so we can properly report
3815 * initialization failures back to the UI. */
3816#ifdef _WIN32
3817 socket_init();
3818#endif
3819
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003820 init_clocks();
3821
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003822 qemu_cache_utils_init(envp);
3823
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003824 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003825#ifndef _WIN32
3826 {
3827 struct sigaction act;
3828 sigfillset(&act.sa_mask);
3829 act.sa_flags = 0;
3830 act.sa_handler = SIG_IGN;
3831 sigaction(SIGPIPE, &act, NULL);
3832 }
3833#else
3834 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3835 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3836 QEMU to run on a single CPU */
3837 {
3838 HANDLE h;
3839 DWORD mask, smask;
3840 int i;
3841 h = GetCurrentProcess();
3842 if (GetProcessAffinityMask(h, &mask, &smask)) {
3843 for(i = 0; i < 32; i++) {
3844 if (mask & (1 << i))
3845 break;
3846 }
3847 if (i != 32) {
3848 mask = 1 << i;
3849 SetProcessAffinityMask(h, mask);
3850 }
3851 }
3852 }
3853#endif
3854
3855 module_call_init(MODULE_INIT_MACHINE);
3856 machine = find_default_machine();
3857 cpu_model = NULL;
3858 initrd_filename = NULL;
3859 ram_size = 0;
3860 snapshot = 0;
3861 kernel_filename = NULL;
3862 kernel_cmdline = "";
David 'Digit' Turner5f824112011-03-01 14:00:26 +01003863
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003864 cyls = heads = secs = 0;
3865 translation = BIOS_ATA_TRANSLATION_AUTO;
3866 monitor_device = "vc:80Cx24C";
3867
3868 serial_devices[0] = "vc:80Cx24C";
3869 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3870 serial_devices[i] = NULL;
3871 serial_device_index = 0;
3872
3873 parallel_devices[0] = "vc:80Cx24C";
3874 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3875 parallel_devices[i] = NULL;
3876 parallel_device_index = 0;
3877
3878 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3879 virtio_consoles[i] = NULL;
3880 virtio_console_index = 0;
3881
3882 for (i = 0; i < MAX_NODES; i++) {
3883 node_mem[i] = 0;
3884 node_cpumask[i] = 0;
3885 }
3886
3887 usb_devices_index = 0;
3888
3889 nb_net_clients = 0;
3890 nb_bt_opts = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003891#ifdef MAX_DRIVES
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003892 nb_drives = 0;
3893 nb_drives_opt = 0;
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003894#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003895 nb_numa_nodes = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003896
3897 nb_nics = 0;
3898
3899 tb_size = 0;
3900 autostart= 1;
3901
3902 register_watchdogs();
3903
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003904 /* Initialize boot properties. */
3905 boot_property_init_service();
3906
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003907 optind = 1;
3908 for(;;) {
3909 if (optind >= argc)
3910 break;
3911 r = argv[optind];
3912 if (r[0] != '-') {
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003913 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003914 } else {
3915 const QEMUOption *popt;
3916
3917 optind++;
3918 /* Treat --foo the same as -foo. */
3919 if (r[1] == '-')
3920 r++;
3921 popt = qemu_options;
3922 for(;;) {
3923 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003924 PANIC("%s: invalid option -- '%s'",
3925 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003926 }
3927 if (!strcmp(popt->name, r + 1))
3928 break;
3929 popt++;
3930 }
3931 if (popt->flags & HAS_ARG) {
3932 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003933 PANIC("%s: option '%s' requires an argument",
3934 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003935 }
3936 optarg = argv[optind++];
3937 } else {
3938 optarg = NULL;
3939 }
3940
3941 switch(popt->index) {
3942 case QEMU_OPTION_M:
3943 machine = find_machine(optarg);
3944 if (!machine) {
3945 QEMUMachine *m;
3946 printf("Supported machines are:\n");
3947 for(m = first_machine; m != NULL; m = m->next) {
3948 printf("%-10s %s%s\n",
3949 m->name, m->desc,
3950 m->is_default ? " (default)" : "");
3951 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003952 if (*optarg != '?') {
3953 PANIC("Invalid machine parameter: %s",
3954 optarg);
3955 } else {
3956 QEMU_EXIT(0);
3957 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003958 }
3959 break;
3960 case QEMU_OPTION_cpu:
3961 /* hw initialization will check this */
3962 if (*optarg == '?') {
3963/* XXX: implement xxx_cpu_list for targets that still miss it */
3964#if defined(cpu_list)
3965 cpu_list(stdout, &fprintf);
3966#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003967 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003968 } else {
3969 cpu_model = optarg;
3970 }
3971 break;
3972 case QEMU_OPTION_initrd:
3973 initrd_filename = optarg;
3974 break;
3975 case QEMU_OPTION_hda:
3976 if (cyls == 0)
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003977 hda_opts = drive_add(optarg, HD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003978 else
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01003979 hda_opts = drive_add(optarg, HD_ALIAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003980 ",cyls=%d,heads=%d,secs=%d%s",
3981 0, cyls, heads, secs,
3982 translation == BIOS_ATA_TRANSLATION_LBA ?
3983 ",trans=lba" :
3984 translation == BIOS_ATA_TRANSLATION_NONE ?
3985 ",trans=none" : "");
3986 break;
3987 case QEMU_OPTION_hdb:
3988 case QEMU_OPTION_hdc:
3989 case QEMU_OPTION_hdd:
3990 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
3991 break;
3992 case QEMU_OPTION_drive:
3993 drive_add(NULL, "%s", optarg);
3994 break;
3995 case QEMU_OPTION_mtdblock:
3996 drive_add(optarg, MTD_ALIAS);
3997 break;
3998 case QEMU_OPTION_sd:
3999 drive_add(optarg, SD_ALIAS);
4000 break;
4001 case QEMU_OPTION_pflash:
4002 drive_add(optarg, PFLASH_ALIAS);
4003 break;
4004 case QEMU_OPTION_snapshot:
4005 snapshot = 1;
4006 break;
4007 case QEMU_OPTION_hdachs:
4008 {
4009 const char *p;
4010 p = optarg;
4011 cyls = strtol(p, (char **)&p, 0);
4012 if (cyls < 1 || cyls > 16383)
4013 goto chs_fail;
4014 if (*p != ',')
4015 goto chs_fail;
4016 p++;
4017 heads = strtol(p, (char **)&p, 0);
4018 if (heads < 1 || heads > 16)
4019 goto chs_fail;
4020 if (*p != ',')
4021 goto chs_fail;
4022 p++;
4023 secs = strtol(p, (char **)&p, 0);
4024 if (secs < 1 || secs > 63)
4025 goto chs_fail;
4026 if (*p == ',') {
4027 p++;
4028 if (!strcmp(p, "none"))
4029 translation = BIOS_ATA_TRANSLATION_NONE;
4030 else if (!strcmp(p, "lba"))
4031 translation = BIOS_ATA_TRANSLATION_LBA;
4032 else if (!strcmp(p, "auto"))
4033 translation = BIOS_ATA_TRANSLATION_AUTO;
4034 else
4035 goto chs_fail;
4036 } else if (*p != '\0') {
4037 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004038 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004039 }
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01004040 if (hda_opts != NULL) {
4041 char num[16];
4042 snprintf(num, sizeof(num), "%d", cyls);
4043 qemu_opt_set(hda_opts, "cyls", num);
4044 snprintf(num, sizeof(num), "%d", heads);
4045 qemu_opt_set(hda_opts, "heads", num);
4046 snprintf(num, sizeof(num), "%d", secs);
4047 qemu_opt_set(hda_opts, "secs", num);
4048 if (translation == BIOS_ATA_TRANSLATION_LBA)
4049 qemu_opt_set(hda_opts, "trans", "lba");
4050 if (translation == BIOS_ATA_TRANSLATION_NONE)
4051 qemu_opt_set(hda_opts, "trans", "none");
4052 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004053 }
4054 break;
4055 case QEMU_OPTION_numa:
4056 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004057 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004058 }
4059 numa_add(optarg);
4060 break;
4061 case QEMU_OPTION_nographic:
4062 display_type = DT_NOGRAPHIC;
4063 break;
4064#ifdef CONFIG_CURSES
4065 case QEMU_OPTION_curses:
4066 display_type = DT_CURSES;
4067 break;
4068#endif
4069 case QEMU_OPTION_portrait:
4070 graphic_rotate = 1;
4071 break;
4072 case QEMU_OPTION_kernel:
4073 kernel_filename = optarg;
4074 break;
4075 case QEMU_OPTION_append:
4076 kernel_cmdline = optarg;
4077 break;
4078 case QEMU_OPTION_cdrom:
4079 drive_add(optarg, CDROM_ALIAS);
4080 break;
4081 case QEMU_OPTION_boot:
4082 boot_devices = optarg;
4083 /* We just do some generic consistency checks */
4084 {
4085 /* Could easily be extended to 64 devices if needed */
4086 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004087
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004088 boot_devices_bitmap = 0;
4089 for (p = boot_devices; *p != '\0'; p++) {
4090 /* Allowed boot devices are:
4091 * a b : floppy disk drives
4092 * c ... f : IDE disk drives
4093 * g ... m : machine implementation dependant drives
4094 * n ... p : network devices
4095 * It's up to each machine implementation to check
4096 * if the given boot devices match the actual hardware
4097 * implementation and firmware features.
4098 */
4099 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004100 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004101 }
4102 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004103 PANIC(
4104 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004105 }
4106 boot_devices_bitmap |= 1 << (*p - 'a');
4107 }
4108 }
4109 break;
4110 case QEMU_OPTION_fda:
4111 case QEMU_OPTION_fdb:
4112 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4113 break;
4114#ifdef TARGET_I386
4115 case QEMU_OPTION_no_fd_bootchk:
4116 fd_bootchk = 0;
4117 break;
4118#endif
4119 case QEMU_OPTION_net:
4120 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004121 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004122 }
4123 net_clients[nb_net_clients] = optarg;
4124 nb_net_clients++;
4125 break;
4126#ifdef CONFIG_SLIRP
4127 case QEMU_OPTION_tftp:
4128 tftp_prefix = optarg;
4129 break;
4130 case QEMU_OPTION_bootp:
4131 bootp_filename = optarg;
4132 break;
4133#if 0 /* ANDROID disabled */
4134#ifndef _WIN32
4135 case QEMU_OPTION_smb:
4136 net_slirp_smb(optarg);
4137 break;
4138#endif
4139#endif /* ANDROID */
4140 case QEMU_OPTION_redir:
4141 net_slirp_redir(NULL, optarg, NULL);
4142 break;
4143#endif
4144 case QEMU_OPTION_bt:
4145 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004146 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004147 }
4148 bt_opts[nb_bt_opts++] = optarg;
4149 break;
4150#ifdef HAS_AUDIO
4151 case QEMU_OPTION_audio_help:
4152 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004153 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004154 break;
4155 case QEMU_OPTION_soundhw:
4156 select_soundhw (optarg);
4157 break;
4158#endif
4159 case QEMU_OPTION_h:
4160 qemu_help(0);
4161 break;
4162 case QEMU_OPTION_version:
4163 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004164 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004165 break;
4166 case QEMU_OPTION_m: {
4167 uint64_t value;
4168 char *ptr;
4169
4170 value = strtoul(optarg, &ptr, 10);
4171 switch (*ptr) {
4172 case 0: case 'M': case 'm':
4173 value <<= 20;
4174 break;
4175 case 'G': case 'g':
4176 value <<= 30;
4177 break;
4178 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004179 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004180 }
4181
4182 /* On 32-bit hosts, QEMU is limited by virtual address space */
4183 if (value > (2047 << 20)
4184#ifndef CONFIG_KQEMU
4185 && HOST_LONG_BITS == 32
4186#endif
4187 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004188 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004189 }
4190 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004191 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004192 }
4193 ram_size = value;
4194 break;
4195 }
4196 case QEMU_OPTION_d:
4197 {
4198 int mask;
4199 const CPULogItem *item;
4200
4201 mask = cpu_str_to_log_mask(optarg);
4202 if (!mask) {
4203 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004204 for(item = cpu_log_items; item->mask != 0; item++) {
4205 printf("%-10s %s\n", item->name, item->help);
4206 }
4207 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004208 }
4209 cpu_set_log(mask);
4210 }
4211 break;
4212 case QEMU_OPTION_s:
4213 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4214 break;
4215 case QEMU_OPTION_gdb:
4216 gdbstub_dev = optarg;
4217 break;
4218 case QEMU_OPTION_L:
4219 data_dir = optarg;
4220 break;
4221 case QEMU_OPTION_bios:
4222 bios_name = optarg;
4223 break;
4224 case QEMU_OPTION_singlestep:
4225 singlestep = 1;
4226 break;
4227 case QEMU_OPTION_S:
4228#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004229 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004230#endif
4231 autostart = 0;
4232 break;
4233#ifndef _WIN32
4234 case QEMU_OPTION_k:
4235 keyboard_layout = optarg;
4236 break;
4237#endif
4238 case QEMU_OPTION_localtime:
4239 rtc_utc = 0;
4240 break;
4241 case QEMU_OPTION_vga:
4242 select_vgahw (optarg);
4243 break;
4244#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4245 case QEMU_OPTION_g:
4246 {
4247 const char *p;
4248 int w, h, depth;
4249 p = optarg;
4250 w = strtol(p, (char **)&p, 10);
4251 if (w <= 0) {
4252 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004253 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004254 }
4255 if (*p != 'x')
4256 goto graphic_error;
4257 p++;
4258 h = strtol(p, (char **)&p, 10);
4259 if (h <= 0)
4260 goto graphic_error;
4261 if (*p == 'x') {
4262 p++;
4263 depth = strtol(p, (char **)&p, 10);
4264 if (depth != 8 && depth != 15 && depth != 16 &&
4265 depth != 24 && depth != 32)
4266 goto graphic_error;
4267 } else if (*p == '\0') {
4268 depth = graphic_depth;
4269 } else {
4270 goto graphic_error;
4271 }
4272
4273 graphic_width = w;
4274 graphic_height = h;
4275 graphic_depth = depth;
4276 }
4277 break;
4278#endif
4279 case QEMU_OPTION_echr:
4280 {
4281 char *r;
4282 term_escape_char = strtol(optarg, &r, 0);
4283 if (r == optarg)
4284 printf("Bad argument to echr\n");
4285 break;
4286 }
4287 case QEMU_OPTION_monitor:
4288 monitor_device = optarg;
4289 break;
4290 case QEMU_OPTION_serial:
4291 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004292 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004293 }
4294 serial_devices[serial_device_index] = optarg;
4295 serial_device_index++;
4296 break;
4297 case QEMU_OPTION_watchdog:
4298 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004299 if (i > 0) {
4300 if (i == 1) {
4301 PANIC("Invalid watchdog parameter: %s",
4302 optarg);
4303 } else {
4304 QEMU_EXIT(0);
4305 }
4306 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004307 break;
4308 case QEMU_OPTION_watchdog_action:
4309 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004310 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004311 }
4312 break;
4313 case QEMU_OPTION_virtiocon:
4314 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004315 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004316 }
4317 virtio_consoles[virtio_console_index] = optarg;
4318 virtio_console_index++;
4319 break;
4320 case QEMU_OPTION_parallel:
4321 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004322 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004323 }
4324 parallel_devices[parallel_device_index] = optarg;
4325 parallel_device_index++;
4326 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004327 case QEMU_OPTION_loadvm:
4328 loadvm = optarg;
4329 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004330 case QEMU_OPTION_savevm_on_exit:
4331 savevm_on_exit = optarg;
4332 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004333 case QEMU_OPTION_full_screen:
4334 full_screen = 1;
4335 break;
4336#ifdef CONFIG_SDL
4337 case QEMU_OPTION_no_frame:
4338 no_frame = 1;
4339 break;
4340 case QEMU_OPTION_alt_grab:
4341 alt_grab = 1;
4342 break;
4343 case QEMU_OPTION_no_quit:
4344 no_quit = 1;
4345 break;
4346 case QEMU_OPTION_sdl:
4347 display_type = DT_SDL;
4348 break;
4349#endif
4350 case QEMU_OPTION_pidfile:
4351 pid_file = optarg;
4352 break;
4353#ifdef TARGET_I386
4354 case QEMU_OPTION_win2k_hack:
4355 win2k_install_hack = 1;
4356 break;
4357 case QEMU_OPTION_rtc_td_hack:
4358 rtc_td_hack = 1;
4359 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004360#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004361 case QEMU_OPTION_acpitable:
4362 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004363 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004364 }
4365 break;
Jun Nakajima334ab472011-02-02 23:49:59 -08004366#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004367 case QEMU_OPTION_smbios:
4368 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004369 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004370 }
4371 break;
4372#endif
4373#ifdef CONFIG_KQEMU
4374 case QEMU_OPTION_no_kqemu:
4375 kqemu_allowed = 0;
4376 break;
4377 case QEMU_OPTION_kernel_kqemu:
4378 kqemu_allowed = 2;
4379 break;
4380#endif
4381#ifdef CONFIG_KVM
4382 case QEMU_OPTION_enable_kvm:
4383 kvm_allowed = 1;
4384#ifdef CONFIG_KQEMU
4385 kqemu_allowed = 0;
4386#endif
4387 break;
4388#endif
4389 case QEMU_OPTION_usb:
4390 usb_enabled = 1;
4391 break;
4392 case QEMU_OPTION_usbdevice:
4393 usb_enabled = 1;
4394 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004395 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004396 }
4397 usb_devices[usb_devices_index] = optarg;
4398 usb_devices_index++;
4399 break;
4400 case QEMU_OPTION_smp:
4401 smp_cpus = atoi(optarg);
4402 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004403 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004404 }
4405 break;
4406 case QEMU_OPTION_vnc:
4407 display_type = DT_VNC;
4408 vnc_display = optarg;
4409 break;
4410#ifdef TARGET_I386
4411 case QEMU_OPTION_no_acpi:
4412 acpi_enabled = 0;
4413 break;
4414 case QEMU_OPTION_no_hpet:
4415 no_hpet = 1;
4416 break;
4417 case QEMU_OPTION_no_virtio_balloon:
4418 no_virtio_balloon = 1;
4419 break;
4420#endif
4421 case QEMU_OPTION_no_reboot:
4422 no_reboot = 1;
4423 break;
4424 case QEMU_OPTION_no_shutdown:
4425 no_shutdown = 1;
4426 break;
4427 case QEMU_OPTION_show_cursor:
4428 cursor_hide = 0;
4429 break;
4430 case QEMU_OPTION_uuid:
4431 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004432 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004433 }
4434 break;
4435#ifndef _WIN32
4436 case QEMU_OPTION_daemonize:
4437 daemonize = 1;
4438 break;
4439#endif
4440 case QEMU_OPTION_option_rom:
4441 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004442 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004443 }
4444 option_rom[nb_option_roms] = optarg;
4445 nb_option_roms++;
4446 break;
4447#if defined(TARGET_ARM) || defined(TARGET_M68K)
4448 case QEMU_OPTION_semihosting:
4449 semihosting_enabled = 1;
4450 break;
4451#endif
4452 case QEMU_OPTION_name:
4453 qemu_name = optarg;
4454 break;
4455#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4456 case QEMU_OPTION_prom_env:
4457 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004458 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004459 }
4460 prom_envs[nb_prom_envs] = optarg;
4461 nb_prom_envs++;
4462 break;
4463#endif
4464#ifdef TARGET_ARM
4465 case QEMU_OPTION_old_param:
4466 old_param = 1;
4467 break;
4468#endif
4469 case QEMU_OPTION_clock:
4470 configure_alarms(optarg);
4471 break;
4472 case QEMU_OPTION_startdate:
4473 {
4474 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004475 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004476 if (!strcmp(optarg, "now")) {
4477 rtc_date_offset = -1;
4478 } else {
4479 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4480 &tm.tm_year,
4481 &tm.tm_mon,
4482 &tm.tm_mday,
4483 &tm.tm_hour,
4484 &tm.tm_min,
4485 &tm.tm_sec) == 6) {
4486 /* OK */
4487 } else if (sscanf(optarg, "%d-%d-%d",
4488 &tm.tm_year,
4489 &tm.tm_mon,
4490 &tm.tm_mday) == 3) {
4491 tm.tm_hour = 0;
4492 tm.tm_min = 0;
4493 tm.tm_sec = 0;
4494 } else {
4495 goto date_fail;
4496 }
4497 tm.tm_year -= 1900;
4498 tm.tm_mon--;
4499 rtc_start_date = mktimegm(&tm);
4500 if (rtc_start_date == -1) {
4501 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004502 PANIC("Invalid date format. Valid format are:\n"
4503 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004504 }
4505 rtc_date_offset = time(NULL) - rtc_start_date;
4506 }
4507 }
4508 break;
rich cannings7339b552011-02-16 13:43:44 -08004509
4510 /* -------------------------------------------------------*/
4511 /* User mode network stack restrictions */
4512 case QEMU_OPTION_drop_udp:
4513 slirp_drop_udp();
4514 break;
4515 case QEMU_OPTION_drop_tcp:
4516 slirp_drop_tcp();
4517 break;
4518 case QEMU_OPTION_allow_tcp:
4519 slirp_allow(optarg, IPPROTO_TCP);
4520 break;
4521 case QEMU_OPTION_allow_udp:
4522 slirp_allow(optarg, IPPROTO_UDP);
4523 break;
4524 case QEMU_OPTION_drop_log:
4525 {
4526 FILE* drop_log_fd;
4527 drop_log_fd = fopen(optarg, "w");
4528
4529 if (!drop_log_fd) {
4530 fprintf(stderr, "Cannot open drop log: %s\n", optarg);
4531 exit(1);
4532 }
4533
4534 slirp_drop_log_fd(drop_log_fd);
4535 }
4536 break;
4537
4538 case QEMU_OPTION_dns_log:
4539 {
4540 FILE* dns_log_fd;
4541 dns_log_fd = fopen(optarg, "wb");
4542
4543 if (dns_log_fd == NULL) {
4544 fprintf(stderr, "Cannot open dns log: %s\n", optarg);
4545 exit(1);
4546 }
4547
4548 slirp_dns_log_fd(dns_log_fd);
4549 }
4550 break;
4551
4552
4553 case QEMU_OPTION_max_dns_conns:
4554 {
4555 int max_dns_conns = 0;
4556 if (parse_int(optarg, &max_dns_conns)) {
4557 fprintf(stderr,
4558 "qemu: syntax: -max-dns-conns max_connections\n");
4559 exit(1);
4560 }
4561 if (max_dns_conns <= 0 || max_dns_conns == LONG_MAX) {
4562 fprintf(stderr,
4563 "Invalid arg for max dns connections: %s\n",
4564 optarg);
4565 exit(1);
4566 }
4567 slirp_set_max_dns_conns(max_dns_conns);
4568 }
4569 break;
4570
4571 case QEMU_OPTION_net_forward:
4572 net_slirp_forward(optarg);
4573 break;
4574 case QEMU_OPTION_net_forward_tcp2sink:
4575 {
4576 SockAddress saddr;
4577
4578 if (parse_host_port(&saddr, optarg)) {
4579 fprintf(stderr,
4580 "Invalid ip/port %s for "
4581 "-forward-dropped-tcp2sink. "
4582 "We expect 'sink_ip:sink_port'\n",
4583 optarg);
4584 exit(1);
4585 }
4586 slirp_forward_dropped_tcp2sink(saddr.u.inet.address,
4587 saddr.u.inet.port);
4588 }
4589 break;
4590 /* -------------------------------------------------------*/
4591
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004592 case QEMU_OPTION_tb_size:
4593 tb_size = strtol(optarg, NULL, 0);
4594 if (tb_size < 0)
4595 tb_size = 0;
4596 break;
4597 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004598 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004599 break;
4600 case QEMU_OPTION_incoming:
4601 incoming = optarg;
4602 break;
4603#ifndef _WIN32
4604 case QEMU_OPTION_chroot:
4605 chroot_dir = optarg;
4606 break;
4607 case QEMU_OPTION_runas:
4608 run_as = optarg;
4609 break;
4610#endif
4611#ifdef CONFIG_XEN
4612 case QEMU_OPTION_xen_domid:
4613 xen_domid = atoi(optarg);
4614 break;
4615 case QEMU_OPTION_xen_create:
4616 xen_mode = XEN_CREATE;
4617 break;
4618 case QEMU_OPTION_xen_attach:
4619 xen_mode = XEN_ATTACH;
4620 break;
4621#endif
4622
4623
4624 case QEMU_OPTION_mic:
4625 audio_input_source = (char*)optarg;
4626 break;
4627#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004628 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004629 trace_filename = optarg;
4630 tracing = 1;
4631 break;
4632#if 0
4633 case QEMU_OPTION_trace_miss:
4634 trace_cache_miss = 1;
4635 break;
4636 case QEMU_OPTION_trace_addr:
4637 trace_all_addr = 1;
4638 break;
4639#endif
4640 case QEMU_OPTION_tracing:
4641 if (strcmp(optarg, "off") == 0)
4642 tracing = 0;
4643 else if (strcmp(optarg, "on") == 0 && trace_filename)
4644 tracing = 1;
4645 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004646 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004647 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004648 }
4649 break;
4650#if 0
4651 case QEMU_OPTION_dcache_load_miss:
4652 dcache_load_miss_penalty = atoi(optarg);
4653 break;
4654 case QEMU_OPTION_dcache_store_miss:
4655 dcache_store_miss_penalty = atoi(optarg);
4656 break;
4657#endif
4658#endif
4659#ifdef CONFIG_NAND
4660 case QEMU_OPTION_nand:
4661 nand_add_dev(optarg);
4662 break;
4663#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004664 case QEMU_OPTION_android_ports:
4665 android_op_ports = (char*)optarg;
4666 break;
4667
4668 case QEMU_OPTION_android_port:
4669 android_op_port = (char*)optarg;
4670 break;
4671
4672 case QEMU_OPTION_android_report_console:
4673 android_op_report_console = (char*)optarg;
4674 break;
4675
4676 case QEMU_OPTION_http_proxy:
4677 op_http_proxy = (char*)optarg;
4678 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004679
4680 case QEMU_OPTION_charmap:
4681 op_charmap_file = (char*)optarg;
4682 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004683
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004684 case QEMU_OPTION_android_hw:
4685 android_op_hwini = (char*)optarg;
4686 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004687
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004688 case QEMU_OPTION_dns_server:
4689 android_op_dns_server = (char*)optarg;
4690 break;
4691
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004692 case QEMU_OPTION_radio:
4693 android_op_radio = (char*)optarg;
4694 break;
4695
4696 case QEMU_OPTION_gps:
4697 android_op_gps = (char*)optarg;
4698 break;
4699
4700 case QEMU_OPTION_audio:
4701 android_op_audio = (char*)optarg;
4702 break;
4703
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004704 case QEMU_OPTION_cpu_delay:
4705 android_op_cpu_delay = (char*)optarg;
4706 break;
4707
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004708 case QEMU_OPTION_show_kernel:
4709 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4710 break;
4711
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004712#ifdef CONFIG_NAND_LIMITS
4713 case QEMU_OPTION_nand_limits:
4714 android_op_nand_limits = (char*)optarg;
4715 break;
4716#endif // CONFIG_NAND_LIMITS
4717
4718 case QEMU_OPTION_netspeed:
4719 android_op_netspeed = (char*)optarg;
4720 break;
4721
4722 case QEMU_OPTION_netdelay:
4723 android_op_netdelay = (char*)optarg;
4724 break;
4725
4726 case QEMU_OPTION_netfast:
4727 android_op_netfast = 1;
4728 break;
4729
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004730 case QEMU_OPTION_tcpdump:
4731 android_op_tcpdump = (char*)optarg;
4732 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004733
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004734 case QEMU_OPTION_boot_property:
4735 boot_property_parse_option((char*)optarg);
4736 break;
4737
4738 case QEMU_OPTION_lcd_density:
4739 android_op_lcd_density = (char*)optarg;
4740 break;
4741
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004742 case QEMU_OPTION_ui_port:
4743 android_op_ui_port = (char*)optarg;
4744 break;
4745
4746 case QEMU_OPTION_ui_settings:
4747 android_op_ui_settings = (char*)optarg;
4748 break;
4749
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004750 case QEMU_OPTION_audio_test_out:
4751 android_audio_test_start_out();
4752 break;
4753
Vladimir Chtchetkine90c62352011-01-13 11:24:07 -08004754 case QEMU_OPTION_android_avdname:
4755 android_op_avd_name = (char*)optarg;
4756 break;
4757
4758 case QEMU_OPTION_timezone:
4759 if (timezone_set((char*)optarg)) {
4760 fprintf(stderr, "emulator: it seems the timezone '%s' is not in zoneinfo format\n",
4761 (char*)optarg);
4762 }
4763 break;
4764
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004765#ifdef CONFIG_MEMCHECK
4766 case QEMU_OPTION_android_memcheck:
4767 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004768 /* This will set ro.kernel.memcheck system property
4769 * to memcheck's tracing flags. */
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004770 stralloc_add_format(kernel_config, " memcheck=%s", android_op_memcheck);
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004771 break;
4772#endif // CONFIG_MEMCHECK
David 'Digit' Turnerbdb6f2d2011-02-23 15:57:25 +01004773
4774 case QEMU_OPTION_snapshot_no_time_update:
4775 android_snapshot_update_time = 0;
4776 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004777 }
4778 }
4779 }
4780
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004781 /* Initialize character map. */
4782 if (android_charmap_setup(op_charmap_file)) {
4783 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004784 PANIC(
4785 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004786 op_charmap_file);
4787 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004788 PANIC(
4789 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004790 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004791 }
4792
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004793 /* If no data_dir is specified then try to find it relative to the
4794 executable path. */
4795 if (!data_dir) {
4796 data_dir = find_datadir(argv[0]);
4797 }
4798 /* If all else fails use the install patch specified when building. */
4799 if (!data_dir) {
4800 data_dir = CONFIG_QEMU_SHAREDIR;
4801 }
4802
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004803 if (!android_op_hwini) {
4804 PANIC("Missing -android-hw <file> option!");
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004805 }
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004806 hw_ini = iniFile_newFromFile(android_op_hwini);
4807 if (hw_ini == NULL) {
4808 PANIC("Could not find %s file.", android_op_hwini);
4809 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004810 androidHwConfig_read(android_hw, hw_ini);
4811 iniFile_free(hw_ini);
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01004812
4813 {
4814 int width = android_hw->hw_lcd_width;
4815 int height = android_hw->hw_lcd_height;
4816 int depth = android_hw->hw_lcd_depth;
4817
4818 /* A bit of sanity checking */
4819 if (width <= 0 || height <= 0 ||
4820 (depth != 16 && depth != 32) ||
4821 (((width|height) & 3) != 0) )
4822 {
4823 PANIC("Invalid display configuration (%d,%d,%d)",
4824 width, height, depth);
4825 }
4826 android_display_width = width;
4827 android_display_height = height;
4828 android_display_bpp = depth;
4829 }
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004830
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004831#ifdef CONFIG_NAND_LIMITS
4832 /* Init nand stuff. */
4833 if (android_op_nand_limits) {
4834 parse_nand_limits(android_op_nand_limits);
4835 }
4836#endif // CONFIG_NAND_LIMITS
4837
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004838 /* Set the VM's max heap size, passed as a boot property */
4839 if (android_hw->vm_heapSize > 0) {
4840 char tmp[64];
4841 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
4842 boot_property_add("dalvik.vm.heapsize",tmp);
4843 }
4844
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004845 /* Initialize net speed and delays stuff. */
4846 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004847 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004848 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004849 }
4850
4851 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004852 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004853 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004854 }
4855
4856 if (android_op_netfast) {
4857 qemu_net_download_speed = 0;
4858 qemu_net_upload_speed = 0;
4859 qemu_net_min_latency = 0;
4860 qemu_net_max_latency = 0;
4861 }
4862
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004863 /* Initialize LCD density */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01004864 if (android_hw->hw_lcd_density) {
4865 long density = android_hw->hw_lcd_density;
4866 if (density <= 0) {
4867 PANIC("Invalid hw.lcd.density value: %ld", density);
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004868 }
4869 hwLcd_setBootProperty(density);
4870 }
4871
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004872 /* Initialize TCP dump */
4873 if (android_op_tcpdump) {
4874 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
4875 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
4876 }
4877 }
4878
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004879 /* Initialize modem */
4880 if (android_op_radio) {
4881 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
4882 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004883 PANIC("unsupported character device specification: %s\n"
4884 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004885 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004886 }
4887 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
4888 } else if (android_hw->hw_gsmModem != 0 ) {
4889 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004890 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004891 }
4892 }
4893
4894 /* Initialize GPS */
4895 if (android_op_gps) {
4896 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
4897 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004898 PANIC("unsupported character device specification: %s\n"
4899 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004900 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004901 }
4902 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
4903 } else if (android_hw->hw_gps != 0) {
4904 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004905 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004906 }
4907 }
4908
4909 /* Initialize audio. */
4910 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004911 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004912 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004913 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004914 }
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004915 }
4916
4917 if (android_op_cpu_delay) {
4918 char* end;
4919 long delay = strtol(android_op_cpu_delay, &end, 0);
4920 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004921 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004922 }
4923 if (delay > 0)
4924 delay = (1000-delay);
4925
4926 qemu_cpu_delay = (int) delay;
4927 }
4928
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004929 if (android_op_dns_server) {
4930 char* x = strchr(android_op_dns_server, ',');
4931 dns_count = 0;
4932 if (x == NULL)
4933 {
4934 if ( add_dns_server( android_op_dns_server ) == 0 )
4935 dns_count = 1;
4936 }
4937 else
4938 {
4939 x = android_op_dns_server;
4940 while (*x) {
4941 char* y = strchr(x, ',');
4942
4943 if (y != NULL) {
4944 *y = 0;
4945 y++;
4946 } else {
4947 y = x + strlen(x);
4948 }
4949
4950 if (y > x && add_dns_server( x ) == 0) {
4951 dns_count += 1;
4952 }
4953 x = y;
4954 }
4955 }
4956 if (dns_count == 0)
4957 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
4958 }
4959
4960 if (dns_count == 0)
4961 dns_count = slirp_get_system_dns_servers();
4962 if (dns_count) {
David 'Digit' Turner5f824112011-03-01 14:00:26 +01004963 stralloc_add_format(kernel_config, " ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004964 }
4965
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004966#ifdef CONFIG_MEMCHECK
4967 if (android_op_memcheck) {
4968 memcheck_init(android_op_memcheck);
4969 }
4970#endif // CONFIG_MEMCHECK
4971
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004972#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
4973 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004974 PANIC(
4975 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004976 }
4977#endif
4978
4979 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
4980 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004981 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
4982 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004983 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004984 }
4985
4986 if (display_type == DT_NOGRAPHIC) {
4987 if (serial_device_index == 0)
4988 serial_devices[0] = "stdio";
4989 if (parallel_device_index == 0)
4990 parallel_devices[0] = "null";
4991 if (strncmp(monitor_device, "vc", 2) == 0)
4992 monitor_device = "stdio";
4993 }
4994
4995#ifndef _WIN32
4996 if (daemonize) {
4997 pid_t pid;
4998
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004999 if (pipe(fds) == -1) {
5000 PANIC("Unable to aquire pidfile");
5001 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005002
5003 pid = fork();
5004 if (pid > 0) {
5005 uint8_t status;
5006 ssize_t len;
5007
5008 close(fds[1]);
5009
5010 again:
5011 len = read(fds[0], &status, 1);
5012 if (len == -1 && (errno == EINTR))
5013 goto again;
5014
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005015 if (len != 1) {
5016 PANIC("Error when aquiring pidfile");
5017 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005018 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005019 PANIC("Could not acquire pidfile");
5020 } else {
5021 QEMU_EXIT(0);
5022 }
5023 } else if (pid < 0) {
5024 PANIC("Unable to daemonize");
5025 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005026
5027 setsid();
5028
5029 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005030 if (pid > 0) {
5031 QEMU_EXIT(0);
5032 } else if (pid < 0) {
5033 PANIC("Could not acquire pid file");
5034 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005035
5036 umask(027);
5037
5038 signal(SIGTSTP, SIG_IGN);
5039 signal(SIGTTOU, SIG_IGN);
5040 signal(SIGTTIN, SIG_IGN);
5041 }
5042
5043 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5044 if (daemonize) {
5045 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02005046 int ret;
5047 do {
5048 ret = write(fds[1], &status, 1);
5049 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005050 PANIC("Could not acquire pid file");
5051 } else {
5052 PANIC("Could not acquire pid file");
5053 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005054 }
5055#endif
5056
5057#ifdef CONFIG_KQEMU
5058 if (smp_cpus > 1)
5059 kqemu_allowed = 0;
5060#endif
5061 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005062 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005063 }
5064 linux_boot = (kernel_filename != NULL);
5065 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5066
5067 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005068 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005069 }
5070
5071 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005072 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005073 }
5074
5075 /* boot to floppy or the default cd if no hard disk defined yet */
5076 if (!boot_devices[0]) {
5077 boot_devices = "cad";
5078 }
5079 setvbuf(stdout, NULL, _IOLBF, 0);
5080
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005081 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005082 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005083 }
David Turner6a9ef172010-09-09 22:54:36 +02005084 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005085
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005086 /* init network clients */
5087 if (nb_net_clients == 0) {
5088 /* if no clients, we use a default config */
5089 net_clients[nb_net_clients++] = "nic";
5090#ifdef CONFIG_SLIRP
5091 net_clients[nb_net_clients++] = "user";
5092#endif
5093 }
5094
5095 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005096 if (net_client_parse(net_clients[i]) < 0) {
5097 PANIC("Unable to parse net clients");
5098 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005099 }
5100 net_client_check();
5101
5102#ifdef TARGET_I386
5103 /* XXX: this should be moved in the PC machine instantiation code */
5104 if (net_boot != 0) {
5105 int netroms = 0;
5106 for (i = 0; i < nb_nics && i < 4; i++) {
5107 const char *model = nd_table[i].model;
5108 char buf[1024];
5109 char *filename;
5110 if (net_boot & (1 << i)) {
5111 if (model == NULL)
5112 model = "ne2k_pci";
5113 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5114 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5115 if (filename && get_image_size(filename) > 0) {
5116 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005117 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005118 }
5119 option_rom[nb_option_roms] = qemu_strdup(buf);
5120 nb_option_roms++;
5121 netroms++;
5122 }
5123 if (filename) {
5124 qemu_free(filename);
5125 }
5126 }
5127 }
5128 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005129 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005130 }
5131 }
5132#endif
5133
5134 /* init the bluetooth world */
5135 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005136 if (bt_parse(bt_opts[i])) {
5137 PANIC("Unable to parse bluetooth options");
5138 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005139
5140 /* init the memory */
David 'Digit' Turner5377c5b2011-02-10 16:52:19 +01005141 if (ram_size == 0) {
5142 ram_size = android_hw->hw_ramSize * 1024LL * 1024;
5143 if (ram_size == 0) {
5144 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5145 }
5146 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005147
5148#ifdef CONFIG_KQEMU
5149 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5150 guest ram allocation. It needs to go away. */
5151 if (kqemu_allowed) {
5152 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5153 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5154 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005155 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005156 }
5157 }
5158#endif
5159
5160 /* init the dynamic translator */
5161 cpu_exec_init_all(tb_size * 1024 * 1024);
5162
5163 bdrv_init();
5164
5165 /* we always create the cdrom drive, even if no disk is there */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005166#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005167 if (nb_drives_opt < MAX_DRIVES)
5168 drive_add(NULL, CDROM_ALIAS);
5169
5170 /* we always create at least one floppy */
5171
5172 if (nb_drives_opt < MAX_DRIVES)
5173 drive_add(NULL, FD_ALIAS, 0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005174 /* we always create one sd slot, even if no card is in it */
5175
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005176 if (1) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005177 drive_add(NULL, SD_ALIAS);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005178 }
5179#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005180
5181 /* open the virtual block devices */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005182 if (snapshot)
5183 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
5184 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
5185 exit(1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005186
David Turner6a9ef172010-09-09 22:54:36 +02005187 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005188 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5189
5190#ifndef _WIN32
5191 /* must be after terminal init, SDL library changes signal handlers */
5192 sighandler_setup();
5193#endif
5194
5195 /* Maintain compatibility with multiple stdio monitors */
5196 if (!strcmp(monitor_device,"stdio")) {
5197 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5198 const char *devname = serial_devices[i];
5199 if (devname && !strcmp(devname,"mon:stdio")) {
5200 monitor_device = NULL;
5201 break;
5202 } else if (devname && !strcmp(devname,"stdio")) {
5203 monitor_device = NULL;
5204 serial_devices[i] = "mon:stdio";
5205 break;
5206 }
5207 }
5208 }
5209
5210 if (nb_numa_nodes > 0) {
5211 int i;
5212
5213 if (nb_numa_nodes > smp_cpus) {
5214 nb_numa_nodes = smp_cpus;
5215 }
5216
5217 /* If no memory size if given for any node, assume the default case
5218 * and distribute the available memory equally across all nodes
5219 */
5220 for (i = 0; i < nb_numa_nodes; i++) {
5221 if (node_mem[i] != 0)
5222 break;
5223 }
5224 if (i == nb_numa_nodes) {
5225 uint64_t usedmem = 0;
5226
5227 /* On Linux, the each node's border has to be 8MB aligned,
5228 * the final node gets the rest.
5229 */
5230 for (i = 0; i < nb_numa_nodes - 1; i++) {
5231 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5232 usedmem += node_mem[i];
5233 }
5234 node_mem[i] = ram_size - usedmem;
5235 }
5236
5237 for (i = 0; i < nb_numa_nodes; i++) {
5238 if (node_cpumask[i] != 0)
5239 break;
5240 }
5241 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5242 * must cope with this anyway, because there are BIOSes out there in
5243 * real machines which also use this scheme.
5244 */
5245 if (i == nb_numa_nodes) {
5246 for (i = 0; i < smp_cpus; i++) {
5247 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5248 }
5249 }
5250 }
5251
5252 if (kvm_enabled()) {
5253 int ret;
5254
5255 ret = kvm_init(smp_cpus);
5256 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005257 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005258 }
5259 }
5260
5261 if (monitor_device) {
5262 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5263 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005264 PANIC("qemu: could not open monitor device '%s'",
5265 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005266 }
5267 }
5268
5269 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5270 const char *devname = serial_devices[i];
5271 if (devname && strcmp(devname, "none")) {
5272 char label[32];
5273 snprintf(label, sizeof(label), "serial%d", i);
5274 serial_hds[i] = qemu_chr_open(label, devname, NULL);
5275 if (!serial_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005276 PANIC("qemu: could not open serial device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005277 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005278 }
5279 }
5280 }
5281
5282 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5283 const char *devname = parallel_devices[i];
5284 if (devname && strcmp(devname, "none")) {
5285 char label[32];
5286 snprintf(label, sizeof(label), "parallel%d", i);
5287 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5288 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005289 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005290 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005291 }
5292 }
5293 }
5294
5295 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5296 const char *devname = virtio_consoles[i];
5297 if (devname && strcmp(devname, "none")) {
5298 char label[32];
5299 snprintf(label, sizeof(label), "virtcon%d", i);
5300 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5301 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005302 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005303 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005304 }
5305 }
5306 }
5307
5308 module_call_init(MODULE_INIT_DEVICE);
5309
5310
5311#ifdef CONFIG_TRACE
5312 if (trace_filename) {
5313 trace_init(trace_filename);
5314#if 0
5315 // We don't need the dcache code until we can get load and store tracing
5316 // working again.
5317 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5318 dcache_replace_policy, dcache_load_miss_penalty,
5319 dcache_store_miss_penalty);
5320#endif
5321 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5322 }
5323#endif
5324
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005325 /* Combine kernel command line passed from the UI with parameters
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005326 * collected during initialization.
5327 *
5328 * The order is the following:
5329 * - parameters from the hw configuration (kernel.parameters)
5330 * - additionnal parameters from options (e.g. -memcheck)
5331 * - the -append parameters.
5332 */
5333 {
5334 const char* kernel_parameters;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005335
David 'Digit' Turner5f824112011-03-01 14:00:26 +01005336 /* If not empty, kernel_config always contains a leading space */
5337 stralloc_append(kernel_params, kernel_config);
5338
5339 if (*kernel_cmdline) {
5340 stralloc_add_c(kernel_params, ' ');
5341 stralloc_add_str(kernel_params, kernel_cmdline);
5342 }
5343
5344 kernel_parameters = stralloc_cstr(kernel_params);
5345 VERBOSE_PRINT(init, "Kernel parameters: %s", kernel_parameters);
5346
5347 machine->init(ram_size,
5348 boot_devices,
5349 kernel_filename,
5350 kernel_parameters,
5351 initrd_filename,
5352 cpu_model);
5353
5354 stralloc_reset(kernel_params);
5355 stralloc_reset(kernel_config);
5356 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005357
5358
5359 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5360 for (i = 0; i < nb_numa_nodes; i++) {
5361 if (node_cpumask[i] & (1 << env->cpu_index)) {
5362 env->numa_node = i;
5363 }
5364 }
5365 }
5366
5367 current_machine = machine;
5368
5369 /* Set KVM's vcpu state to qemu's initial CPUState. */
5370 if (kvm_enabled()) {
5371 int ret;
5372
5373 ret = kvm_sync_vcpus();
5374 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005375 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005376 }
5377 }
5378
5379 /* init USB devices */
5380 if (usb_enabled) {
5381 for(i = 0; i < usb_devices_index; i++) {
5382 if (usb_device_add(usb_devices[i], 0) < 0) {
5383 fprintf(stderr, "Warning: could not add USB device %s\n",
5384 usb_devices[i]);
5385 }
5386 }
5387 }
5388
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005389 /* just use the first displaystate for the moment */
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005390 ds = get_displaystate();
Vladimir Chtchetkinecf755ea2011-01-12 14:38:19 -08005391
David 'Digit' Turner2507cab2011-02-10 16:29:17 +01005392 /* Initialize display from the command line parameters. */
5393 android_display_reset(ds,
5394 android_display_width,
5395 android_display_height,
5396 android_display_bpp);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005397
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005398 if (display_type == DT_DEFAULT) {
5399#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5400 display_type = DT_SDL;
5401#else
5402 display_type = DT_VNC;
5403 vnc_display = "localhost:0,to=99";
5404 show_vnc_port = 1;
5405#endif
5406 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005407
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005408
5409 switch (display_type) {
5410 case DT_NOGRAPHIC:
5411 break;
5412#if defined(CONFIG_CURSES)
5413 case DT_CURSES:
5414 curses_display_init(ds, full_screen);
5415 break;
5416#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005417#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005418 case DT_SDL:
5419 sdl_display_init(ds, full_screen, no_frame);
5420 break;
5421#elif defined(CONFIG_COCOA)
5422 case DT_SDL:
5423 cocoa_display_init(ds, full_screen);
5424 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005425#elif defined(CONFIG_STANDALONE_CORE)
5426 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005427 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005428 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005429#endif
5430 case DT_VNC:
5431 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005432 if (vnc_display_open(ds, vnc_display) < 0) {
5433 PANIC("Unable to initialize VNC display");
5434 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005435
5436 if (show_vnc_port) {
5437 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5438 }
5439 break;
5440 default:
5441 break;
5442 }
5443 dpy_resize(ds);
5444
5445 dcl = ds->listeners;
5446 while (dcl != NULL) {
5447 if (dcl->dpy_refresh != NULL) {
5448 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5449 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5450 }
5451 dcl = dcl->next;
5452 }
5453
5454 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5455 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5456 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5457 }
5458
David 'Digit' Turner94702b02011-01-20 02:46:33 +01005459 text_consoles_set_display(ds);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005460 qemu_chr_initial_reset();
5461
5462 if (monitor_device && monitor_hd)
5463 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5464
5465 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5466 const char *devname = serial_devices[i];
5467 if (devname && strcmp(devname, "none")) {
5468 if (strstart(devname, "vc", 0))
5469 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5470 }
5471 }
5472
5473 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5474 const char *devname = parallel_devices[i];
5475 if (devname && strcmp(devname, "none")) {
5476 if (strstart(devname, "vc", 0))
5477 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5478 }
5479 }
5480
5481 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5482 const char *devname = virtio_consoles[i];
5483 if (virtcon_hds[i] && devname) {
5484 if (strstart(devname, "vc", 0))
5485 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5486 }
5487 }
5488
5489 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005490 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005491 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005492 }
5493
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005494 /* call android-specific setup function */
5495 android_emulation_setup();
5496
Vladimir Chtchetkine57584042011-01-20 16:15:30 -08005497#if !defined(CONFIG_STANDALONE_CORE)
5498 // For the standalone emulator (UI+core in one executable) we need to
5499 // set the window title here.
5500 android_emulator_set_base_port(android_base_port);
5501#endif
5502
Ot ten Thije871da2a2010-09-20 10:29:22 +01005503 if (loadvm)
5504 do_loadvm(cur_mon, loadvm);
5505
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005506 if (incoming) {
5507 autostart = 0; /* fixme how to deal with -daemonize */
5508 qemu_start_incoming_migration(incoming);
5509 }
5510
5511 if (autostart)
5512 vm_start();
5513
5514#ifndef _WIN32
5515 if (daemonize) {
5516 uint8_t status = 0;
5517 ssize_t len;
5518
5519 again1:
5520 len = write(fds[1], &status, 1);
5521 if (len == -1 && (errno == EINTR))
5522 goto again1;
5523
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005524 if (len != 1) {
5525 PANIC("Unable to daemonize");
5526 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005527
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005528 if (chdir("/")) {
5529 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005530 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005531 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005532 TFR(fd = open("/dev/null", O_RDWR));
5533 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005534 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005535 }
5536
5537 if (run_as) {
5538 pwd = getpwnam(run_as);
5539 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005540 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005541 }
5542 }
5543
5544 if (chroot_dir) {
5545 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005546 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005547 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005548 if (chdir("/")) {
5549 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005550 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005551 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005552 }
5553
5554 if (run_as) {
5555 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005556 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005557 }
5558 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005559 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005560 }
5561 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005562 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005563 }
5564 }
5565
5566 if (daemonize) {
5567 dup2(fd, 0);
5568 dup2(fd, 1);
5569 dup2(fd, 2);
5570
5571 close(fd);
5572 }
5573#endif
5574
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005575#ifdef CONFIG_ANDROID
5576 // This will notify the UI that the core is successfuly initialized
5577 android_core_init_completed();
5578#endif // CONFIG_ANDROID
5579
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005580 main_loop();
5581 quit_timers();
5582 net_cleanup();
5583 android_emulation_teardown();
5584 return 0;
5585}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005586
5587void
5588android_emulation_teardown(void)
5589{
5590 android_charmap_done();
5591}