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