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