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