blob: 3fb50eaf4a1bb3aff915753a5d1d4178db01befc [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"
46#include "block.h"
47#include "audio/audio.h"
48
49#include "qemu_file.h"
50#include "android/android.h"
51#include "charpipe.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070052#include "modem_driver.h"
53#include "android/gps.h"
54#include "android/hw-qemud.h"
55#include "android/hw-kmsg.h"
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -070056#include "android/charmap.h"
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -070057#include "android/globals.h"
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -070058#include "android/utils/bufprint.h"
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -080059#include "android/display-core.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070060#include "targphys.h"
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -070061#include "tcpdump.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070062
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -070063#ifdef CONFIG_MEMCHECK
64#include "memcheck/memcheck.h"
65#endif // CONFIG_MEMCHECK
66
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070067#include <unistd.h>
68#include <fcntl.h>
69#include <signal.h>
70#include <time.h>
71#include <errno.h>
72#include <sys/time.h>
73#include <zlib.h>
74
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070075/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070076#include "config-host.h"
77
78#ifndef _WIN32
79#include <libgen.h>
80#include <pwd.h>
81#include <sys/times.h>
82#include <sys/wait.h>
83#include <termios.h>
84#include <sys/mman.h>
85#include <sys/ioctl.h>
86#include <sys/resource.h>
87#include <sys/socket.h>
88#include <netinet/in.h>
89#include <net/if.h>
90#if defined(__NetBSD__)
91#include <net/if_tap.h>
92#endif
93#ifdef __linux__
94#include <linux/if_tun.h>
95#endif
96#include <arpa/inet.h>
97#include <dirent.h>
98#include <netdb.h>
99#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -0700100#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700101#include <sys/stat.h>
102#if defined(__FreeBSD__) || defined(__DragonFly__)
103#include <libutil.h>
104#else
105#include <util.h>
106#endif
107#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
108#include <freebsd/stdlib.h>
109#else
110#ifdef __linux__
111#include <pty.h>
112#include <malloc.h>
113#include <linux/rtc.h>
114
115/* For the benefit of older linux systems which don't supply it,
116 we use a local copy of hpet.h. */
117/* #include <linux/hpet.h> */
118#include "hpet.h"
119
120#include <linux/ppdev.h>
121#include <linux/parport.h>
122#endif
123#ifdef __sun__
124#include <sys/stat.h>
125#include <sys/ethernet.h>
126#include <sys/sockio.h>
127#include <netinet/arp.h>
128#include <netinet/in.h>
129#include <netinet/in_systm.h>
130#include <netinet/ip.h>
131#include <netinet/ip_icmp.h> // must come after ip.h
132#include <netinet/udp.h>
133#include <netinet/tcp.h>
134#include <net/if.h>
135#include <syslog.h>
136#include <stropts.h>
137#endif
138#endif
139#endif
140
141#if defined(__OpenBSD__)
142#include <util.h>
143#endif
144
145#if defined(CONFIG_VDE)
146#include <libvdeplug.h>
147#endif
148
149#ifdef _WIN32
150#include <windows.h>
151#include <malloc.h>
152#include <sys/timeb.h>
153#include <mmsystem.h>
154#define getopt_long_only getopt_long
155#define memalign(align, size) malloc(size)
156#endif
157
158
159#ifdef CONFIG_COCOA
160#undef main
161#define main qemu_main
162#endif /* CONFIG_COCOA */
163
164#include "hw/hw.h"
165#include "hw/boards.h"
166#include "hw/usb.h"
167#include "hw/pcmcia.h"
168#include "hw/pc.h"
169#include "hw/audiodev.h"
170#include "hw/isa.h"
171#include "hw/baum.h"
172#include "hw/bt.h"
173#include "hw/watchdog.h"
174#include "hw/smbios.h"
175#include "hw/xen.h"
176#include "bt-host.h"
177#include "net.h"
178#include "monitor.h"
179#include "console.h"
180#include "sysemu.h"
181#include "gdbstub.h"
182#include "qemu-timer.h"
183#include "qemu-char.h"
184#include "cache-utils.h"
185#include "block.h"
186#include "dma.h"
187#include "audio/audio.h"
188#include "migration.h"
189#include "kvm.h"
190#include "balloon.h"
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700191#include "android/hw-lcd.h"
192#include "android/boot-properties.h"
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700193#include "android/core-init-utils.h"
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +0100194#include "android/audio-test.h"
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700195
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700196#ifdef CONFIG_STANDALONE_CORE
197/* Verbose value used by the standalone emulator core (without UI) */
198unsigned long android_verbose;
199#endif // CONFIG_STANDALONE_CORE
200
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700201#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700202#undef main
203#define main qemu_main
204#endif
205
206#include "disas.h"
207
208#include "exec-all.h"
209
210#ifdef CONFIG_TRACE
211#include "trace.h"
212#include "dcache.h"
213#endif
214
215#include "qemu_socket.h"
216
217#if defined(CONFIG_SLIRP)
218#include "libslirp.h"
219#endif
220
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700221
222
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700223#define DEFAULT_RAM_SIZE 128
224
225/* Max number of USB devices that can be specified on the commandline. */
226#define MAX_USB_CMDLINE 8
227
228/* Max number of bluetooth switches on the commandline. */
229#define MAX_BT_CMDLINE 10
230
231/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700232
233static const char *data_dir;
234const char *bios_name = NULL;
235static void *ioport_opaque[MAX_IOPORTS];
236static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
237static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
238/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
239 to store the VM snapshots */
240DriveInfo drives_table[MAX_DRIVES+1];
241int nb_drives;
242enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
243static DisplayState *display_state;
244DisplayType display_type = DT_DEFAULT;
245const char* keyboard_layout = NULL;
246int64_t ticks_per_sec;
247ram_addr_t ram_size;
248int nb_nics;
249NICInfo nd_table[MAX_NICS];
250int vm_running;
251static int autostart;
252static int rtc_utc = 1;
253static int rtc_date_offset = -1; /* -1 means no change */
254int cirrus_vga_enabled = 1;
255int std_vga_enabled = 0;
256int vmsvga_enabled = 0;
257int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700258QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700259#ifdef TARGET_SPARC
260int graphic_width = 1024;
261int graphic_height = 768;
262int graphic_depth = 8;
263#else
264int graphic_width = 800;
265int graphic_height = 600;
266int graphic_depth = 15;
267#endif
268static int full_screen = 0;
269#ifdef CONFIG_SDL
270static int no_frame = 0;
271#endif
272int no_quit = 0;
273CharDriverState *serial_hds[MAX_SERIAL_PORTS];
274CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
275CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
276#ifdef TARGET_I386
277int win2k_install_hack = 0;
278int rtc_td_hack = 0;
279#endif
280int usb_enabled = 0;
281int singlestep = 0;
282int smp_cpus = 1;
283const char *vnc_display;
284int acpi_enabled = 1;
285int no_hpet = 0;
286int no_virtio_balloon = 0;
287int fd_bootchk = 1;
288int no_reboot = 0;
289int no_shutdown = 0;
290int cursor_hide = 1;
291int graphic_rotate = 0;
292#ifndef _WIN32
293int daemonize = 0;
294#endif
295WatchdogTimerModel *watchdog = NULL;
296int watchdog_action = WDT_RESET;
297const char *option_rom[MAX_OPTION_ROMS];
298int nb_option_roms;
299int semihosting_enabled = 0;
300#ifdef TARGET_ARM
301int old_param = 0;
302#endif
303const char *qemu_name;
304int alt_grab = 0;
305#if defined(TARGET_SPARC) || defined(TARGET_PPC)
306unsigned int nb_prom_envs = 0;
307const char *prom_envs[MAX_PROM_ENVS];
308#endif
309int nb_drives_opt;
310struct drive_opt drives_opt[MAX_DRIVES];
311
312int nb_numa_nodes;
313uint64_t node_mem[MAX_NODES];
314uint64_t node_cpumask[MAX_NODES];
315
316static CPUState *cur_cpu;
317static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700318static QEMUTimer *nographic_timer;
319
320uint8_t qemu_uuid[16];
321
322
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700323int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700324extern char* audio_input_source;
325
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700326extern char* android_op_ports;
327extern char* android_op_port;
328extern char* android_op_report_console;
329extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700330// Path to the file containing specific key character map.
331char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700332
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700333/* Framebuffer dimensions, passed with -android-gui option. */
334char* android_op_gui = NULL;
335
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700336/* Path to hardware initialization file passed with -android-hw option. */
337char* android_op_hwini = NULL;
338
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700339/* Memory checker options. */
340char* android_op_memcheck = NULL;
341
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700342/* -dns-server option value. */
343char* android_op_dns_server = NULL;
344
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700345/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700346char* android_op_radio = NULL;
347
348/* -gps option value. */
349char* android_op_gps = NULL;
350
351/* -audio option value. */
352char* android_op_audio = NULL;
353
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700354/* -cpu-delay option value. */
355char* android_op_cpu_delay = NULL;
356
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700357#ifdef CONFIG_NAND_LIMITS
358/* -nand-limits option value. */
359char* android_op_nand_limits = NULL;
360#endif // CONFIG_NAND_LIMITS
361
362/* -netspeed option value. */
363char* android_op_netspeed = NULL;
364
365/* -netdelay option value. */
366char* android_op_netdelay = NULL;
367
368/* -netfast option value. */
369int android_op_netfast = 0;
370
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700371/* -tcpdump option value. */
372char* android_op_tcpdump = NULL;
373
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700374/* -lcd-density option value. */
375char* android_op_lcd_density = NULL;
376
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700377/* -ui-port option value. This port will be used to report the core
378 * initialization completion.
379 */
380char* android_op_ui_port = NULL;
381
382/* -ui-settings option value. This value will be passed to the UI when new UI
383 * process is attaching to the core.
384 */
385char* android_op_ui_settings = NULL;
386
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700387extern int android_display_width;
388extern int android_display_height;
389extern int android_display_bpp;
390
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700391extern void dprint( const char* format, ... );
392
Tim Baverstock24204cc2010-11-25 11:37:43 +0000393#if CONFIG_ANDROID_SNAPSHOTS
394const char* savevm_on_exit = NULL;
395#endif
396
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700397#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
398
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700399/* Reports the core initialization failure to the error stdout and to the UI
400 * socket before exiting the application.
401 * Parameters that are passed to this macro are used to format the error
402 * mesage using sprintf routine.
403 */
404#ifdef CONFIG_ANDROID
405#define PANIC(...) android_core_init_failure(__VA_ARGS__)
406#else
407#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
408 exit(1); \
409 } while (0)
410#endif // CONFIG_ANDROID
411
412/* Exits the core during initialization. */
413#ifdef CONFIG_ANDROID
414#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
415#else
416#define QEMU_EXIT(exit_code) exit(exit_code)
417#endif // CONFIG_ANDROID
418
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700419/***********************************************************/
420/* x86 ISA bus support */
421
422target_phys_addr_t isa_mem_base = 0;
423PicState2 *isa_pic;
424
425static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
426static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
427
428static uint32_t ioport_read(int index, uint32_t address)
429{
430 static IOPortReadFunc *default_func[3] = {
431 default_ioport_readb,
432 default_ioport_readw,
433 default_ioport_readl
434 };
435 IOPortReadFunc *func = ioport_read_table[index][address];
436 if (!func)
437 func = default_func[index];
438 return func(ioport_opaque[address], address);
439}
440
441static void ioport_write(int index, uint32_t address, uint32_t data)
442{
443 static IOPortWriteFunc *default_func[3] = {
444 default_ioport_writeb,
445 default_ioport_writew,
446 default_ioport_writel
447 };
448 IOPortWriteFunc *func = ioport_write_table[index][address];
449 if (!func)
450 func = default_func[index];
451 func(ioport_opaque[address], address, data);
452}
453
454static uint32_t default_ioport_readb(void *opaque, uint32_t address)
455{
456#ifdef DEBUG_UNUSED_IOPORT
457 fprintf(stderr, "unused inb: port=0x%04x\n", address);
458#endif
459 return 0xff;
460}
461
462static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
463{
464#ifdef DEBUG_UNUSED_IOPORT
465 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
466#endif
467}
468
469/* default is to make two byte accesses */
470static uint32_t default_ioport_readw(void *opaque, uint32_t address)
471{
472 uint32_t data;
473 data = ioport_read(0, address);
474 address = (address + 1) & (MAX_IOPORTS - 1);
475 data |= ioport_read(0, address) << 8;
476 return data;
477}
478
479static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
480{
481 ioport_write(0, address, data & 0xff);
482 address = (address + 1) & (MAX_IOPORTS - 1);
483 ioport_write(0, address, (data >> 8) & 0xff);
484}
485
486static uint32_t default_ioport_readl(void *opaque, uint32_t address)
487{
488#ifdef DEBUG_UNUSED_IOPORT
489 fprintf(stderr, "unused inl: port=0x%04x\n", address);
490#endif
491 return 0xffffffff;
492}
493
494static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
495{
496#ifdef DEBUG_UNUSED_IOPORT
497 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
498#endif
499}
500
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700501/* Parses -android-gui command line option, extracting width, height and bits
502 * per pixel parameters for the GUI console used in this session of the
503 * emulator. -android-gui option contains exactly three comma-separated positive
504 * integer numbers in strict order: width goes first, width goes next, and bits
505 * per pixel goes third. This routine verifies that format and return 0 if all
506 * three numbers were extracted, or -1 if string format was incorrect for that
507 * option. Note that this routine does not verify that extracted values are
508 * correct!
509 */
510static int
511parse_androig_gui_option(const char* op, int* width, int* height, int* bpp)
512{
513 char val[128];
514
515 if (get_param_value(val, 128, "width", op)) {
516 *width = strtol(val, NULL, 0);
517 } else {
518 fprintf(stderr, "option -android-gui is missing width parameter\n");
519 return -1;
520 }
521 if (get_param_value(val, 128, "height", op)) {
522 *height = strtol(val, NULL, 0);
523 } else {
524 fprintf(stderr, "option -android-gui is missing height parameter\n");
525 return -1;
526 }
527 if (get_param_value(val, 128, "bpp", op)) {
528 *bpp = strtol(val, NULL, 0);
529 } else {
530 fprintf(stderr, "option -android-gui is missing bpp parameter\n");
531 return -1;
532 }
533
534 return 0;
535}
536
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700537/***********************************************************/
538void hw_error(const char *fmt, ...)
539{
540 va_list ap;
541 CPUState *env;
542
543 va_start(ap, fmt);
544 fprintf(stderr, "qemu: hardware error: ");
545 vfprintf(stderr, fmt, ap);
546 fprintf(stderr, "\n");
547 for(env = first_cpu; env != NULL; env = env->next_cpu) {
548 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
549#ifdef TARGET_I386
550 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
551#else
552 cpu_dump_state(env, stderr, fprintf, 0);
553#endif
554 }
555 va_end(ap);
556 abort();
557}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200558
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700559/***************/
560/* ballooning */
561
562static QEMUBalloonEvent *qemu_balloon_event;
563void *qemu_balloon_event_opaque;
564
565void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
566{
567 qemu_balloon_event = func;
568 qemu_balloon_event_opaque = opaque;
569}
570
571void qemu_balloon(ram_addr_t target)
572{
573 if (qemu_balloon_event)
574 qemu_balloon_event(qemu_balloon_event_opaque, target);
575}
576
577ram_addr_t qemu_balloon_status(void)
578{
579 if (qemu_balloon_event)
580 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
581 return 0;
582}
583
584/***********************************************************/
David Turner025c32f2010-09-10 14:52:42 +0200585/* real time host monotonic timer */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700586
587/* compute with 96 bit intermediate result: (a*b)/c */
588uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
589{
590 union {
591 uint64_t ll;
592 struct {
David 'Digit' Turner20894ae2010-05-10 17:07:36 -0700593#ifdef HOST_WORDS_BIGENDIAN
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700594 uint32_t high, low;
595#else
596 uint32_t low, high;
597#endif
598 } l;
599 } u, res;
600 uint64_t rl, rh;
601
602 u.ll = a;
603 rl = (uint64_t)u.l.low * (uint64_t)b;
604 rh = (uint64_t)u.l.high * (uint64_t)b;
605 rh += (rl >> 32);
606 res.l.high = rh / c;
607 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
608 return res.ll;
609}
610
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700611/***********************************************************/
612/* host time/date access */
613void qemu_get_timedate(struct tm *tm, int offset)
614{
615 time_t ti;
616 struct tm *ret;
617
618 time(&ti);
619 ti += offset;
620 if (rtc_date_offset == -1) {
621 if (rtc_utc)
622 ret = gmtime(&ti);
623 else
624 ret = localtime(&ti);
625 } else {
626 ti -= rtc_date_offset;
627 ret = gmtime(&ti);
628 }
629
630 memcpy(tm, ret, sizeof(struct tm));
631}
632
633int qemu_timedate_diff(struct tm *tm)
634{
635 time_t seconds;
636
637 if (rtc_date_offset == -1)
638 if (rtc_utc)
639 seconds = mktimegm(tm);
640 else
641 seconds = mktime(tm);
642 else
643 seconds = mktimegm(tm) + rtc_date_offset;
644
645 return seconds - time(NULL);
646}
647
648
649#ifdef CONFIG_TRACE
650static int tbflush_requested;
651static int exit_requested;
652
653void start_tracing()
654{
655 if (trace_filename == NULL)
656 return;
657 if (!tracing) {
658 fprintf(stderr,"-- start tracing --\n");
659 start_time = Now();
660 }
661 tracing = 1;
662 tbflush_requested = 1;
663 qemu_notify_event();
664}
665
666void stop_tracing()
667{
668 if (trace_filename == NULL)
669 return;
670 if (tracing) {
671 end_time = Now();
672 elapsed_usecs += end_time - start_time;
673 fprintf(stderr,"-- stop tracing --\n");
674 }
675 tracing = 0;
676 tbflush_requested = 1;
677 qemu_notify_event();
678}
679
680#ifndef _WIN32
681/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
682 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
683 */
684void sigusr_handler(int sig)
685{
686 if (sig == SIGUSR1)
687 start_tracing();
688 else
689 stop_tracing();
690}
691#endif
692
693/* This is the handler to catch control-C so that we can exit cleanly.
694 * This is needed when tracing to flush the buffers to disk.
695 */
696void sigint_handler(int sig)
697{
698 exit_requested = 1;
699 qemu_notify_event();
700}
701#endif /* CONFIG_TRACE */
702
703
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700704/***********************************************************/
705/* Bluetooth support */
706static int nb_hcis;
707static int cur_hci;
708static struct HCIInfo *hci_table[MAX_NICS];
709
710static struct bt_vlan_s {
711 struct bt_scatternet_s net;
712 int id;
713 struct bt_vlan_s *next;
714} *first_bt_vlan;
715
716/* find or alloc a new bluetooth "VLAN" */
717static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
718{
719 struct bt_vlan_s **pvlan, *vlan;
720 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
721 if (vlan->id == id)
722 return &vlan->net;
723 }
724 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
725 vlan->id = id;
726 pvlan = &first_bt_vlan;
727 while (*pvlan != NULL)
728 pvlan = &(*pvlan)->next;
729 *pvlan = vlan;
730 return &vlan->net;
731}
732
733static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
734{
735}
736
737static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
738{
739 return -ENOTSUP;
740}
741
742static struct HCIInfo null_hci = {
743 .cmd_send = null_hci_send,
744 .sco_send = null_hci_send,
745 .acl_send = null_hci_send,
746 .bdaddr_set = null_hci_addr_set,
747};
748
749struct HCIInfo *qemu_next_hci(void)
750{
751 if (cur_hci == nb_hcis)
752 return &null_hci;
753
754 return hci_table[cur_hci++];
755}
756
757static struct HCIInfo *hci_init(const char *str)
758{
759 char *endp;
760 struct bt_scatternet_s *vlan = 0;
761
762 if (!strcmp(str, "null"))
763 /* null */
764 return &null_hci;
765 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
766 /* host[:hciN] */
767 return bt_host_hci(str[4] ? str + 5 : "hci0");
768 else if (!strncmp(str, "hci", 3)) {
769 /* hci[,vlan=n] */
770 if (str[3]) {
771 if (!strncmp(str + 3, ",vlan=", 6)) {
772 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
773 if (*endp)
774 vlan = 0;
775 }
776 } else
777 vlan = qemu_find_bt_vlan(0);
778 if (vlan)
779 return bt_new_hci(vlan);
780 }
781
782 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
783
784 return 0;
785}
786
787static int bt_hci_parse(const char *str)
788{
789 struct HCIInfo *hci;
790 bdaddr_t bdaddr;
791
792 if (nb_hcis >= MAX_NICS) {
793 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
794 return -1;
795 }
796
797 hci = hci_init(str);
798 if (!hci)
799 return -1;
800
801 bdaddr.b[0] = 0x52;
802 bdaddr.b[1] = 0x54;
803 bdaddr.b[2] = 0x00;
804 bdaddr.b[3] = 0x12;
805 bdaddr.b[4] = 0x34;
806 bdaddr.b[5] = 0x56 + nb_hcis;
807 hci->bdaddr_set(hci, bdaddr.b);
808
809 hci_table[nb_hcis++] = hci;
810
811 return 0;
812}
813
814static void bt_vhci_add(int vlan_id)
815{
816 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
817
818 if (!vlan->slave)
819 fprintf(stderr, "qemu: warning: adding a VHCI to "
820 "an empty scatternet %i\n", vlan_id);
821
822 bt_vhci_init(bt_new_hci(vlan));
823}
824
825static struct bt_device_s *bt_device_add(const char *opt)
826{
827 struct bt_scatternet_s *vlan;
828 int vlan_id = 0;
829 char *endp = strstr(opt, ",vlan=");
830 int len = (endp ? endp - opt : strlen(opt)) + 1;
831 char devname[10];
832
833 pstrcpy(devname, MIN(sizeof(devname), len), opt);
834
835 if (endp) {
836 vlan_id = strtol(endp + 6, &endp, 0);
837 if (*endp) {
838 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
839 return 0;
840 }
841 }
842
843 vlan = qemu_find_bt_vlan(vlan_id);
844
845 if (!vlan->slave)
846 fprintf(stderr, "qemu: warning: adding a slave device to "
847 "an empty scatternet %i\n", vlan_id);
848
849 if (!strcmp(devname, "keyboard"))
850 return bt_keyboard_init(vlan);
851
852 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
853 return 0;
854}
855
856static int bt_parse(const char *opt)
857{
858 const char *endp, *p;
859 int vlan;
860
861 if (strstart(opt, "hci", &endp)) {
862 if (!*endp || *endp == ',') {
863 if (*endp)
864 if (!strstart(endp, ",vlan=", 0))
865 opt = endp + 1;
866
867 return bt_hci_parse(opt);
868 }
869 } else if (strstart(opt, "vhci", &endp)) {
870 if (!*endp || *endp == ',') {
871 if (*endp) {
872 if (strstart(endp, ",vlan=", &p)) {
873 vlan = strtol(p, (char **) &endp, 0);
874 if (*endp) {
875 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
876 return 1;
877 }
878 } else {
879 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
880 return 1;
881 }
882 } else
883 vlan = 0;
884
885 bt_vhci_add(vlan);
886 return 0;
887 }
888 } else if (strstart(opt, "device:", &endp))
889 return !bt_device_add(endp);
890
891 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
892 return 1;
893}
894
895/***********************************************************/
896/* QEMU Block devices */
897
898#define HD_ALIAS "index=%d,media=disk"
899#define CDROM_ALIAS "index=2,media=cdrom"
900#define FD_ALIAS "index=%d,if=floppy"
901#define PFLASH_ALIAS "if=pflash"
902#define MTD_ALIAS "if=mtd"
903#define SD_ALIAS "index=0,if=sd"
904
905static int drive_opt_get_free_idx(void)
906{
907 int index;
908
909 for (index = 0; index < MAX_DRIVES; index++)
910 if (!drives_opt[index].used) {
911 drives_opt[index].used = 1;
912 return index;
913 }
914
915 return -1;
916}
917
918static int drive_get_free_idx(void)
919{
920 int index;
921
922 for (index = 0; index < MAX_DRIVES; index++)
923 if (!drives_table[index].used) {
924 drives_table[index].used = 1;
925 return index;
926 }
927
928 return -1;
929}
930
931int drive_add(const char *file, const char *fmt, ...)
932{
933 va_list ap;
934 int index = drive_opt_get_free_idx();
935
936 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
937 fprintf(stderr, "qemu: too many drives\n");
938 return -1;
939 }
940
941 drives_opt[index].file = file;
942 va_start(ap, fmt);
943 vsnprintf(drives_opt[index].opt,
944 sizeof(drives_opt[0].opt), fmt, ap);
945 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700946
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700947 nb_drives_opt++;
948 return index;
949}
950
951void drive_remove(int index)
952{
953 drives_opt[index].used = 0;
954 nb_drives_opt--;
955}
956
957int drive_get_index(BlockInterfaceType type, int bus, int unit)
958{
959 int index;
960
961 /* seek interface, bus and unit */
962
963 for (index = 0; index < MAX_DRIVES; index++)
964 if (drives_table[index].type == type &&
965 drives_table[index].bus == bus &&
966 drives_table[index].unit == unit &&
967 drives_table[index].used)
968 return index;
969
970 return -1;
971}
972
973int drive_get_max_bus(BlockInterfaceType type)
974{
975 int max_bus;
976 int index;
977
978 max_bus = -1;
979 for (index = 0; index < nb_drives; index++) {
980 if(drives_table[index].type == type &&
981 drives_table[index].bus > max_bus)
982 max_bus = drives_table[index].bus;
983 }
984 return max_bus;
985}
986
987const char *drive_get_serial(BlockDriverState *bdrv)
988{
989 int index;
990
991 for (index = 0; index < nb_drives; index++)
992 if (drives_table[index].bdrv == bdrv)
993 return drives_table[index].serial;
994
995 return "\0";
996}
997
998BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
999{
1000 int index;
1001
1002 for (index = 0; index < nb_drives; index++)
1003 if (drives_table[index].bdrv == bdrv)
1004 return drives_table[index].onerror;
1005
1006 return BLOCK_ERR_STOP_ENOSPC;
1007}
1008
1009static void bdrv_format_print(void *opaque, const char *name)
1010{
1011 fprintf(stderr, " %s", name);
1012}
1013
1014void drive_uninit(BlockDriverState *bdrv)
1015{
1016 int i;
1017
1018 for (i = 0; i < MAX_DRIVES; i++)
1019 if (drives_table[i].bdrv == bdrv) {
1020 drives_table[i].bdrv = NULL;
1021 drives_table[i].used = 0;
1022 drive_remove(drives_table[i].drive_opt_idx);
1023 nb_drives--;
1024 break;
1025 }
1026}
1027
1028int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1029{
1030 char buf[128];
1031 char file[1024];
1032 char devname[128];
1033 char serial[21];
1034 const char *mediastr = "";
1035 BlockInterfaceType type;
1036 enum { MEDIA_DISK, MEDIA_CDROM } media;
1037 int bus_id, unit_id;
1038 int cyls, heads, secs, translation;
1039 BlockDriverState *bdrv;
1040 BlockDriver *drv = NULL;
1041 QEMUMachine *machine = opaque;
1042 int max_devs;
1043 int index;
1044 int cache;
1045 int bdrv_flags, onerror;
1046 int drives_table_idx;
1047 char *str = arg->opt;
1048 static const char * const params[] = { "bus", "unit", "if", "index",
1049 "cyls", "heads", "secs", "trans",
1050 "media", "snapshot", "file",
1051 "cache", "format", "serial", "werror",
1052 NULL };
1053
1054 if (check_params(buf, sizeof(buf), params, str) < 0) {
1055 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1056 buf, str);
1057 return -1;
1058 }
1059
1060 file[0] = 0;
1061 cyls = heads = secs = 0;
1062 bus_id = 0;
1063 unit_id = -1;
1064 translation = BIOS_ATA_TRANSLATION_AUTO;
1065 index = -1;
1066 cache = 3;
1067
1068 if (machine->use_scsi) {
1069 type = IF_SCSI;
1070 max_devs = MAX_SCSI_DEVS;
1071 pstrcpy(devname, sizeof(devname), "scsi");
1072 } else {
1073 type = IF_IDE;
1074 max_devs = MAX_IDE_DEVS;
1075 pstrcpy(devname, sizeof(devname), "ide");
1076 }
1077 media = MEDIA_DISK;
1078
1079 /* extract parameters */
1080
1081 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1082 bus_id = strtol(buf, NULL, 0);
1083 if (bus_id < 0) {
1084 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1085 return -1;
1086 }
1087 }
1088
1089 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1090 unit_id = strtol(buf, NULL, 0);
1091 if (unit_id < 0) {
1092 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1093 return -1;
1094 }
1095 }
1096
1097 if (get_param_value(buf, sizeof(buf), "if", str)) {
1098 pstrcpy(devname, sizeof(devname), buf);
1099 if (!strcmp(buf, "ide")) {
1100 type = IF_IDE;
1101 max_devs = MAX_IDE_DEVS;
1102 } else if (!strcmp(buf, "scsi")) {
1103 type = IF_SCSI;
1104 max_devs = MAX_SCSI_DEVS;
1105 } else if (!strcmp(buf, "floppy")) {
1106 type = IF_FLOPPY;
1107 max_devs = 0;
1108 } else if (!strcmp(buf, "pflash")) {
1109 type = IF_PFLASH;
1110 max_devs = 0;
1111 } else if (!strcmp(buf, "mtd")) {
1112 type = IF_MTD;
1113 max_devs = 0;
1114 } else if (!strcmp(buf, "sd")) {
1115 type = IF_SD;
1116 max_devs = 0;
1117 } else if (!strcmp(buf, "virtio")) {
1118 type = IF_VIRTIO;
1119 max_devs = 0;
1120 } else if (!strcmp(buf, "xen")) {
1121 type = IF_XEN;
1122 max_devs = 0;
1123 } else {
1124 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1125 return -1;
1126 }
1127 }
1128
1129 if (get_param_value(buf, sizeof(buf), "index", str)) {
1130 index = strtol(buf, NULL, 0);
1131 if (index < 0) {
1132 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1133 return -1;
1134 }
1135 }
1136
1137 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1138 cyls = strtol(buf, NULL, 0);
1139 }
1140
1141 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1142 heads = strtol(buf, NULL, 0);
1143 }
1144
1145 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1146 secs = strtol(buf, NULL, 0);
1147 }
1148
1149 if (cyls || heads || secs) {
1150 if (cyls < 1 || cyls > 16383) {
1151 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1152 return -1;
1153 }
1154 if (heads < 1 || heads > 16) {
1155 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1156 return -1;
1157 }
1158 if (secs < 1 || secs > 63) {
1159 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1160 return -1;
1161 }
1162 }
1163
1164 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1165 if (!cyls) {
1166 fprintf(stderr,
1167 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1168 str);
1169 return -1;
1170 }
1171 if (!strcmp(buf, "none"))
1172 translation = BIOS_ATA_TRANSLATION_NONE;
1173 else if (!strcmp(buf, "lba"))
1174 translation = BIOS_ATA_TRANSLATION_LBA;
1175 else if (!strcmp(buf, "auto"))
1176 translation = BIOS_ATA_TRANSLATION_AUTO;
1177 else {
1178 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1179 return -1;
1180 }
1181 }
1182
1183 if (get_param_value(buf, sizeof(buf), "media", str)) {
1184 if (!strcmp(buf, "disk")) {
1185 media = MEDIA_DISK;
1186 } else if (!strcmp(buf, "cdrom")) {
1187 if (cyls || secs || heads) {
1188 fprintf(stderr,
1189 "qemu: '%s' invalid physical CHS format\n", str);
1190 return -1;
1191 }
1192 media = MEDIA_CDROM;
1193 } else {
1194 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1195 return -1;
1196 }
1197 }
1198
1199 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1200 if (!strcmp(buf, "on"))
1201 snapshot = 1;
1202 else if (!strcmp(buf, "off"))
1203 snapshot = 0;
1204 else {
1205 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1206 return -1;
1207 }
1208 }
1209
1210 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1211 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1212 cache = 0;
1213 else if (!strcmp(buf, "writethrough"))
1214 cache = 1;
1215 else if (!strcmp(buf, "writeback"))
1216 cache = 2;
1217 else {
1218 fprintf(stderr, "qemu: invalid cache option\n");
1219 return -1;
1220 }
1221 }
1222
1223 if (get_param_value(buf, sizeof(buf), "format", str)) {
1224 if (strcmp(buf, "?") == 0) {
1225 fprintf(stderr, "qemu: Supported formats:");
1226 bdrv_iterate_format(bdrv_format_print, NULL);
1227 fprintf(stderr, "\n");
1228 return -1;
1229 }
1230 drv = bdrv_find_format(buf);
1231 if (!drv) {
1232 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1233 return -1;
1234 }
1235 }
1236
1237 if (arg->file == NULL)
1238 get_param_value(file, sizeof(file), "file", str);
1239 else
1240 pstrcpy(file, sizeof(file), arg->file);
1241
1242 if (!get_param_value(serial, sizeof(serial), "serial", str))
1243 memset(serial, 0, sizeof(serial));
1244
1245 onerror = BLOCK_ERR_STOP_ENOSPC;
1246 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1247 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1248 fprintf(stderr, "werror is no supported by this format\n");
1249 return -1;
1250 }
1251 if (!strcmp(buf, "ignore"))
1252 onerror = BLOCK_ERR_IGNORE;
1253 else if (!strcmp(buf, "enospc"))
1254 onerror = BLOCK_ERR_STOP_ENOSPC;
1255 else if (!strcmp(buf, "stop"))
1256 onerror = BLOCK_ERR_STOP_ANY;
1257 else if (!strcmp(buf, "report"))
1258 onerror = BLOCK_ERR_REPORT;
1259 else {
1260 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1261 return -1;
1262 }
1263 }
1264
1265 /* compute bus and unit according index */
1266
1267 if (index != -1) {
1268 if (bus_id != 0 || unit_id != -1) {
1269 fprintf(stderr,
1270 "qemu: '%s' index cannot be used with bus and unit\n", str);
1271 return -1;
1272 }
1273 if (max_devs == 0)
1274 {
1275 unit_id = index;
1276 bus_id = 0;
1277 } else {
1278 unit_id = index % max_devs;
1279 bus_id = index / max_devs;
1280 }
1281 }
1282
1283 /* if user doesn't specify a unit_id,
1284 * try to find the first free
1285 */
1286
1287 if (unit_id == -1) {
1288 unit_id = 0;
1289 while (drive_get_index(type, bus_id, unit_id) != -1) {
1290 unit_id++;
1291 if (max_devs && unit_id >= max_devs) {
1292 unit_id -= max_devs;
1293 bus_id++;
1294 }
1295 }
1296 }
1297
1298 /* check unit id */
1299
1300 if (max_devs && unit_id >= max_devs) {
1301 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1302 str, unit_id, max_devs - 1);
1303 return -1;
1304 }
1305
1306 /*
1307 * ignore multiple definitions
1308 */
1309
1310 if (drive_get_index(type, bus_id, unit_id) != -1)
1311 return -2;
1312
1313 /* init */
1314
1315 if (type == IF_IDE || type == IF_SCSI)
1316 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1317 if (max_devs)
1318 snprintf(buf, sizeof(buf), "%s%i%s%i",
1319 devname, bus_id, mediastr, unit_id);
1320 else
1321 snprintf(buf, sizeof(buf), "%s%s%i",
1322 devname, mediastr, unit_id);
1323 bdrv = bdrv_new(buf);
1324 drives_table_idx = drive_get_free_idx();
1325 drives_table[drives_table_idx].bdrv = bdrv;
1326 drives_table[drives_table_idx].type = type;
1327 drives_table[drives_table_idx].bus = bus_id;
1328 drives_table[drives_table_idx].unit = unit_id;
1329 drives_table[drives_table_idx].onerror = onerror;
1330 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1331 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1332 nb_drives++;
1333
1334 switch(type) {
1335 case IF_IDE:
1336 case IF_SCSI:
1337 case IF_XEN:
1338 switch(media) {
1339 case MEDIA_DISK:
1340 if (cyls != 0) {
1341 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1342 bdrv_set_translation_hint(bdrv, translation);
1343 }
1344 break;
1345 case MEDIA_CDROM:
1346 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1347 break;
1348 }
1349 break;
1350 case IF_SD:
1351 /* FIXME: This isn't really a floppy, but it's a reasonable
1352 approximation. */
1353 case IF_FLOPPY:
1354 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1355 break;
1356 case IF_PFLASH:
1357 case IF_MTD:
1358 case IF_VIRTIO:
1359 break;
1360 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001361 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001362 abort();
1363 }
1364 if (!file[0])
1365 return -2;
1366 bdrv_flags = 0;
1367 if (snapshot) {
1368 bdrv_flags |= BDRV_O_SNAPSHOT;
1369 cache = 2; /* always use write-back with snapshot */
1370 }
1371 if (cache == 0) /* no caching */
1372 bdrv_flags |= BDRV_O_NOCACHE;
1373 else if (cache == 2) /* write-back */
1374 bdrv_flags |= BDRV_O_CACHE_WB;
1375 else if (cache == 3) /* not specified */
1376 bdrv_flags |= BDRV_O_CACHE_DEF;
1377 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1378 fprintf(stderr, "qemu: could not open disk image %s\n",
1379 file);
1380 return -1;
1381 }
1382 if (bdrv_key_required(bdrv))
1383 autostart = 0;
1384 return drives_table_idx;
1385}
1386
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/***********************************************************/
2160/* bottom halves (can be seen as timers which expire ASAP) */
2161
2162struct QEMUBH {
2163 QEMUBHFunc *cb;
2164 void *opaque;
2165 int scheduled;
2166 int idle;
2167 int deleted;
2168 QEMUBH *next;
2169};
2170
2171static QEMUBH *first_bh = NULL;
2172
2173QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
2174{
2175 QEMUBH *bh;
2176 bh = qemu_mallocz(sizeof(QEMUBH));
2177 bh->cb = cb;
2178 bh->opaque = opaque;
2179 bh->next = first_bh;
2180 first_bh = bh;
2181 return bh;
2182}
2183
2184int qemu_bh_poll(void)
2185{
2186 QEMUBH *bh, **bhp;
2187 int ret;
2188
2189 ret = 0;
2190 for (bh = first_bh; bh; bh = bh->next) {
2191 if (!bh->deleted && bh->scheduled) {
2192 bh->scheduled = 0;
2193 if (!bh->idle)
2194 ret = 1;
2195 bh->idle = 0;
2196 bh->cb(bh->opaque);
2197 }
2198 }
2199
2200 /* remove deleted bhs */
2201 bhp = &first_bh;
2202 while (*bhp) {
2203 bh = *bhp;
2204 if (bh->deleted) {
2205 *bhp = bh->next;
2206 qemu_free(bh);
2207 } else
2208 bhp = &bh->next;
2209 }
2210
2211 return ret;
2212}
2213
2214void qemu_bh_schedule_idle(QEMUBH *bh)
2215{
2216 if (bh->scheduled)
2217 return;
2218 bh->scheduled = 1;
2219 bh->idle = 1;
2220}
2221
2222void qemu_bh_schedule(QEMUBH *bh)
2223{
2224 if (bh->scheduled)
2225 return;
2226 bh->scheduled = 1;
2227 bh->idle = 0;
2228 /* stop the currently executing CPU to execute the BH ASAP */
2229 qemu_notify_event();
2230}
2231
2232void qemu_bh_cancel(QEMUBH *bh)
2233{
2234 bh->scheduled = 0;
2235}
2236
2237void qemu_bh_delete(QEMUBH *bh)
2238{
2239 bh->scheduled = 0;
2240 bh->deleted = 1;
2241}
2242
David Turnerd0659942010-09-10 10:42:32 +02002243void qemu_bh_update_timeout(int *timeout)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002244{
2245 QEMUBH *bh;
2246
2247 for (bh = first_bh; bh; bh = bh->next) {
2248 if (!bh->deleted && bh->scheduled) {
2249 if (bh->idle) {
2250 /* idle bottom halves will be polled at least
2251 * every 10ms */
2252 *timeout = MIN(10, *timeout);
2253 } else {
2254 /* non-idle bottom halves will be executed
2255 * immediately */
2256 *timeout = 0;
2257 break;
2258 }
2259 }
2260 }
2261}
2262
2263/***********************************************************/
2264/* machine registration */
2265
2266static QEMUMachine *first_machine = NULL;
2267QEMUMachine *current_machine = NULL;
2268
2269int qemu_register_machine(QEMUMachine *m)
2270{
2271 QEMUMachine **pm;
2272 pm = &first_machine;
2273 while (*pm != NULL)
2274 pm = &(*pm)->next;
2275 m->next = NULL;
2276 *pm = m;
2277 return 0;
2278}
2279
2280static QEMUMachine *find_machine(const char *name)
2281{
2282 QEMUMachine *m;
2283
2284 for(m = first_machine; m != NULL; m = m->next) {
2285 if (!strcmp(m->name, name))
2286 return m;
2287 }
2288 return NULL;
2289}
2290
2291static QEMUMachine *find_default_machine(void)
2292{
2293 QEMUMachine *m;
2294
2295 for(m = first_machine; m != NULL; m = m->next) {
2296 if (m->is_default) {
2297 return m;
2298 }
2299 }
2300 return NULL;
2301}
2302
2303/***********************************************************/
2304/* main execution loop */
2305
2306static void gui_update(void *opaque)
2307{
2308 uint64_t interval = GUI_REFRESH_INTERVAL;
2309 DisplayState *ds = opaque;
2310 DisplayChangeListener *dcl = ds->listeners;
2311
2312 dpy_refresh(ds);
2313
2314 while (dcl != NULL) {
2315 if (dcl->gui_timer_interval &&
2316 dcl->gui_timer_interval < interval)
2317 interval = dcl->gui_timer_interval;
2318 dcl = dcl->next;
2319 }
2320 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
2321}
2322
2323static void nographic_update(void *opaque)
2324{
2325 uint64_t interval = GUI_REFRESH_INTERVAL;
2326
2327 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
2328}
2329
2330struct vm_change_state_entry {
2331 VMChangeStateHandler *cb;
2332 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002333 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002334};
2335
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002336static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002337
2338VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2339 void *opaque)
2340{
2341 VMChangeStateEntry *e;
2342
2343 e = qemu_mallocz(sizeof (*e));
2344
2345 e->cb = cb;
2346 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002347 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002348 return e;
2349}
2350
2351void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2352{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002353 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002354 qemu_free (e);
2355}
2356
2357static void vm_state_notify(int running, int reason)
2358{
2359 VMChangeStateEntry *e;
2360
2361 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2362 e->cb(e->opaque, running, reason);
2363 }
2364}
2365
2366static void resume_all_vcpus(void);
2367static void pause_all_vcpus(void);
2368
2369void vm_start(void)
2370{
2371 if (!vm_running) {
2372 cpu_enable_ticks();
2373 vm_running = 1;
2374 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002375 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002376 resume_all_vcpus();
2377 }
2378}
2379
2380/* reset/shutdown handler */
2381
2382typedef struct QEMUResetEntry {
2383 QEMUResetHandler *func;
2384 void *opaque;
2385 int order;
2386 struct QEMUResetEntry *next;
2387} QEMUResetEntry;
2388
2389static QEMUResetEntry *first_reset_entry;
2390static int reset_requested;
2391static int shutdown_requested;
2392static int powerdown_requested;
2393static int debug_requested;
2394static int vmstop_requested;
2395
2396int qemu_shutdown_requested(void)
2397{
2398 int r = shutdown_requested;
2399 shutdown_requested = 0;
2400 return r;
2401}
2402
2403int qemu_reset_requested(void)
2404{
2405 int r = reset_requested;
2406 reset_requested = 0;
2407 return r;
2408}
2409
2410int qemu_powerdown_requested(void)
2411{
2412 int r = powerdown_requested;
2413 powerdown_requested = 0;
2414 return r;
2415}
2416
2417static int qemu_debug_requested(void)
2418{
2419 int r = debug_requested;
2420 debug_requested = 0;
2421 return r;
2422}
2423
2424static int qemu_vmstop_requested(void)
2425{
2426 int r = vmstop_requested;
2427 vmstop_requested = 0;
2428 return r;
2429}
2430
2431static void do_vm_stop(int reason)
2432{
2433 if (vm_running) {
2434 cpu_disable_ticks();
2435 vm_running = 0;
2436 pause_all_vcpus();
2437 vm_state_notify(0, reason);
2438 }
2439}
2440
2441void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2442{
2443 QEMUResetEntry **pre, *re;
2444
2445 pre = &first_reset_entry;
2446 while (*pre != NULL && (*pre)->order >= order) {
2447 pre = &(*pre)->next;
2448 }
2449 re = qemu_mallocz(sizeof(QEMUResetEntry));
2450 re->func = func;
2451 re->opaque = opaque;
2452 re->order = order;
2453 re->next = NULL;
2454 *pre = re;
2455}
2456
2457void qemu_system_reset(void)
2458{
2459 QEMUResetEntry *re;
2460
2461 /* reset all devices */
2462 for(re = first_reset_entry; re != NULL; re = re->next) {
2463 re->func(re->opaque);
2464 }
2465}
2466
2467void qemu_system_reset_request(void)
2468{
2469 if (no_reboot) {
2470 shutdown_requested = 1;
2471 } else {
2472 reset_requested = 1;
2473 }
2474 qemu_notify_event();
2475}
2476
2477void qemu_system_shutdown_request(void)
2478{
2479 shutdown_requested = 1;
2480 qemu_notify_event();
2481}
2482
2483void qemu_system_powerdown_request(void)
2484{
2485 powerdown_requested = 1;
2486 qemu_notify_event();
2487}
2488
2489#ifdef CONFIG_IOTHREAD
2490static void qemu_system_vmstop_request(int reason)
2491{
2492 vmstop_requested = reason;
2493 qemu_notify_event();
2494}
2495#endif
2496
2497#ifndef _WIN32
2498static int io_thread_fd = -1;
2499
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002500#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002501static void qemu_event_increment(void)
2502{
2503 static const char byte = 0;
2504
2505 if (io_thread_fd == -1)
2506 return;
2507
2508 write(io_thread_fd, &byte, sizeof(byte));
2509}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002510#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002511
2512static void qemu_event_read(void *opaque)
2513{
2514 int fd = (unsigned long)opaque;
2515 ssize_t len;
2516
2517 /* Drain the notify pipe */
2518 do {
2519 char buffer[512];
2520 len = read(fd, buffer, sizeof(buffer));
2521 } while ((len == -1 && errno == EINTR) || len > 0);
2522}
2523
2524static int qemu_event_init(void)
2525{
2526 int err;
2527 int fds[2];
2528
2529 err = pipe(fds);
2530 if (err == -1)
2531 return -errno;
2532
2533 err = fcntl_setfl(fds[0], O_NONBLOCK);
2534 if (err < 0)
2535 goto fail;
2536
2537 err = fcntl_setfl(fds[1], O_NONBLOCK);
2538 if (err < 0)
2539 goto fail;
2540
2541 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2542 (void *)(unsigned long)fds[0]);
2543
2544 io_thread_fd = fds[1];
2545 return 0;
2546
2547fail:
2548 close(fds[0]);
2549 close(fds[1]);
2550 return err;
2551}
2552#else
2553HANDLE qemu_event_handle;
2554
2555static void dummy_event_handler(void *opaque)
2556{
2557}
2558
2559static int qemu_event_init(void)
2560{
2561 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2562 if (!qemu_event_handle) {
2563 perror("Failed CreateEvent");
2564 return -1;
2565 }
2566 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2567 return 0;
2568}
2569
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002570#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002571static void qemu_event_increment(void)
2572{
2573 SetEvent(qemu_event_handle);
2574}
2575#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002576#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002577
2578static int cpu_can_run(CPUState *env)
2579{
2580 if (env->stop)
2581 return 0;
2582 if (env->stopped)
2583 return 0;
2584 return 1;
2585}
2586
2587#ifndef CONFIG_IOTHREAD
2588static int qemu_init_main_loop(void)
2589{
2590 return qemu_event_init();
2591}
2592
2593void qemu_init_vcpu(void *_env)
2594{
2595 CPUState *env = _env;
2596
2597 if (kvm_enabled())
2598 kvm_init_vcpu(env);
2599 return;
2600}
2601
2602int qemu_cpu_self(void *env)
2603{
2604 return 1;
2605}
2606
2607static void resume_all_vcpus(void)
2608{
2609}
2610
2611static void pause_all_vcpus(void)
2612{
2613}
2614
2615void qemu_cpu_kick(void *env)
2616{
2617 return;
2618}
2619
2620void qemu_notify_event(void)
2621{
2622 CPUState *env = cpu_single_env;
2623
2624 if (env) {
2625 cpu_exit(env);
2626#ifdef USE_KQEMU
2627 if (env->kqemu_enabled)
2628 kqemu_cpu_interrupt(env);
2629#endif
2630 }
2631}
2632
2633#define qemu_mutex_lock_iothread() do { } while (0)
2634#define qemu_mutex_unlock_iothread() do { } while (0)
2635
2636void vm_stop(int reason)
2637{
2638 do_vm_stop(reason);
2639}
2640
2641#else /* CONFIG_IOTHREAD */
2642
2643#include "qemu-thread.h"
2644
2645QemuMutex qemu_global_mutex;
2646static QemuMutex qemu_fair_mutex;
2647
2648static QemuThread io_thread;
2649
2650static QemuThread *tcg_cpu_thread;
2651static QemuCond *tcg_halt_cond;
2652
2653static int qemu_system_ready;
2654/* cpu creation */
2655static QemuCond qemu_cpu_cond;
2656/* system init */
2657static QemuCond qemu_system_cond;
2658static QemuCond qemu_pause_cond;
2659
2660static void block_io_signals(void);
2661static void unblock_io_signals(void);
2662static int tcg_has_work(void);
2663
2664static int qemu_init_main_loop(void)
2665{
2666 int ret;
2667
2668 ret = qemu_event_init();
2669 if (ret)
2670 return ret;
2671
2672 qemu_cond_init(&qemu_pause_cond);
2673 qemu_mutex_init(&qemu_fair_mutex);
2674 qemu_mutex_init(&qemu_global_mutex);
2675 qemu_mutex_lock(&qemu_global_mutex);
2676
2677 unblock_io_signals();
2678 qemu_thread_self(&io_thread);
2679
2680 return 0;
2681}
2682
2683static void qemu_wait_io_event(CPUState *env)
2684{
2685 while (!tcg_has_work())
2686 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2687
2688 qemu_mutex_unlock(&qemu_global_mutex);
2689
2690 /*
2691 * Users of qemu_global_mutex can be starved, having no chance
2692 * to acquire it since this path will get to it first.
2693 * So use another lock to provide fairness.
2694 */
2695 qemu_mutex_lock(&qemu_fair_mutex);
2696 qemu_mutex_unlock(&qemu_fair_mutex);
2697
2698 qemu_mutex_lock(&qemu_global_mutex);
2699 if (env->stop) {
2700 env->stop = 0;
2701 env->stopped = 1;
2702 qemu_cond_signal(&qemu_pause_cond);
2703 }
2704}
2705
2706static int qemu_cpu_exec(CPUState *env);
2707
2708static void *kvm_cpu_thread_fn(void *arg)
2709{
2710 CPUState *env = arg;
2711
2712 block_io_signals();
2713 qemu_thread_self(env->thread);
2714
2715 /* signal CPU creation */
2716 qemu_mutex_lock(&qemu_global_mutex);
2717 env->created = 1;
2718 qemu_cond_signal(&qemu_cpu_cond);
2719
2720 /* and wait for machine initialization */
2721 while (!qemu_system_ready)
2722 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2723
2724 while (1) {
2725 if (cpu_can_run(env))
2726 qemu_cpu_exec(env);
2727 qemu_wait_io_event(env);
2728 }
2729
2730 return NULL;
2731}
2732
2733static void tcg_cpu_exec(void);
2734
2735static void *tcg_cpu_thread_fn(void *arg)
2736{
2737 CPUState *env = arg;
2738
2739 block_io_signals();
2740 qemu_thread_self(env->thread);
2741
2742 /* signal CPU creation */
2743 qemu_mutex_lock(&qemu_global_mutex);
2744 for (env = first_cpu; env != NULL; env = env->next_cpu)
2745 env->created = 1;
2746 qemu_cond_signal(&qemu_cpu_cond);
2747
2748 /* and wait for machine initialization */
2749 while (!qemu_system_ready)
2750 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2751
2752 while (1) {
2753 tcg_cpu_exec();
2754 qemu_wait_io_event(cur_cpu);
2755 }
2756
2757 return NULL;
2758}
2759
2760void qemu_cpu_kick(void *_env)
2761{
2762 CPUState *env = _env;
2763 qemu_cond_broadcast(env->halt_cond);
2764 if (kvm_enabled())
2765 qemu_thread_signal(env->thread, SIGUSR1);
2766}
2767
2768int qemu_cpu_self(void *env)
2769{
2770 return (cpu_single_env != NULL);
2771}
2772
2773static void cpu_signal(int sig)
2774{
2775 if (cpu_single_env)
2776 cpu_exit(cpu_single_env);
2777}
2778
2779static void block_io_signals(void)
2780{
2781 sigset_t set;
2782 struct sigaction sigact;
2783
2784 sigemptyset(&set);
2785 sigaddset(&set, SIGUSR2);
2786 sigaddset(&set, SIGIO);
2787 sigaddset(&set, SIGALRM);
2788 pthread_sigmask(SIG_BLOCK, &set, NULL);
2789
2790 sigemptyset(&set);
2791 sigaddset(&set, SIGUSR1);
2792 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2793
2794 memset(&sigact, 0, sizeof(sigact));
2795 sigact.sa_handler = cpu_signal;
2796 sigaction(SIGUSR1, &sigact, NULL);
2797}
2798
2799static void unblock_io_signals(void)
2800{
2801 sigset_t set;
2802
2803 sigemptyset(&set);
2804 sigaddset(&set, SIGUSR2);
2805 sigaddset(&set, SIGIO);
2806 sigaddset(&set, SIGALRM);
2807 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2808
2809 sigemptyset(&set);
2810 sigaddset(&set, SIGUSR1);
2811 pthread_sigmask(SIG_BLOCK, &set, NULL);
2812}
2813
2814static void qemu_signal_lock(unsigned int msecs)
2815{
2816 qemu_mutex_lock(&qemu_fair_mutex);
2817
2818 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2819 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2820 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2821 break;
2822 }
2823 qemu_mutex_unlock(&qemu_fair_mutex);
2824}
2825
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002826void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002827{
2828 if (kvm_enabled()) {
2829 qemu_mutex_lock(&qemu_fair_mutex);
2830 qemu_mutex_lock(&qemu_global_mutex);
2831 qemu_mutex_unlock(&qemu_fair_mutex);
2832 } else
2833 qemu_signal_lock(100);
2834}
2835
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002836void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002837{
2838 qemu_mutex_unlock(&qemu_global_mutex);
2839}
2840
2841static int all_vcpus_paused(void)
2842{
2843 CPUState *penv = first_cpu;
2844
2845 while (penv) {
2846 if (!penv->stopped)
2847 return 0;
2848 penv = (CPUState *)penv->next_cpu;
2849 }
2850
2851 return 1;
2852}
2853
2854static void pause_all_vcpus(void)
2855{
2856 CPUState *penv = first_cpu;
2857
2858 while (penv) {
2859 penv->stop = 1;
2860 qemu_thread_signal(penv->thread, SIGUSR1);
2861 qemu_cpu_kick(penv);
2862 penv = (CPUState *)penv->next_cpu;
2863 }
2864
2865 while (!all_vcpus_paused()) {
2866 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2867 penv = first_cpu;
2868 while (penv) {
2869 qemu_thread_signal(penv->thread, SIGUSR1);
2870 penv = (CPUState *)penv->next_cpu;
2871 }
2872 }
2873}
2874
2875static void resume_all_vcpus(void)
2876{
2877 CPUState *penv = first_cpu;
2878
2879 while (penv) {
2880 penv->stop = 0;
2881 penv->stopped = 0;
2882 qemu_thread_signal(penv->thread, SIGUSR1);
2883 qemu_cpu_kick(penv);
2884 penv = (CPUState *)penv->next_cpu;
2885 }
2886}
2887
2888static void tcg_init_vcpu(void *_env)
2889{
2890 CPUState *env = _env;
2891 /* share a single thread for all cpus with TCG */
2892 if (!tcg_cpu_thread) {
2893 env->thread = qemu_mallocz(sizeof(QemuThread));
2894 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2895 qemu_cond_init(env->halt_cond);
2896 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2897 while (env->created == 0)
2898 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2899 tcg_cpu_thread = env->thread;
2900 tcg_halt_cond = env->halt_cond;
2901 } else {
2902 env->thread = tcg_cpu_thread;
2903 env->halt_cond = tcg_halt_cond;
2904 }
2905}
2906
2907static void kvm_start_vcpu(CPUState *env)
2908{
2909#if 0
2910 kvm_init_vcpu(env);
2911 env->thread = qemu_mallocz(sizeof(QemuThread));
2912 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2913 qemu_cond_init(env->halt_cond);
2914 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2915 while (env->created == 0)
2916 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2917#endif
2918}
2919
2920void qemu_init_vcpu(void *_env)
2921{
2922 CPUState *env = _env;
2923
2924 if (kvm_enabled())
2925 kvm_start_vcpu(env);
2926 else
2927 tcg_init_vcpu(env);
2928}
2929
2930void qemu_notify_event(void)
2931{
2932 qemu_event_increment();
2933}
2934
2935void vm_stop(int reason)
2936{
2937 QemuThread me;
2938 qemu_thread_self(&me);
2939
2940 if (!qemu_thread_equal(&me, &io_thread)) {
2941 qemu_system_vmstop_request(reason);
2942 /*
2943 * FIXME: should not return to device code in case
2944 * vm_stop() has been requested.
2945 */
2946 if (cpu_single_env) {
2947 cpu_exit(cpu_single_env);
2948 cpu_single_env->stop = 1;
2949 }
2950 return;
2951 }
2952 do_vm_stop(reason);
2953}
2954
2955#endif
2956
2957
2958#ifdef _WIN32
2959static void host_main_loop_wait(int *timeout)
2960{
2961 int ret, ret2, i;
2962 PollingEntry *pe;
2963
2964
2965 /* XXX: need to suppress polling by better using win32 events */
2966 ret = 0;
2967 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2968 ret |= pe->func(pe->opaque);
2969 }
2970 if (ret == 0) {
2971 int err;
2972 WaitObjects *w = &wait_objects;
2973
2974 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2975 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2976 if (w->func[ret - WAIT_OBJECT_0])
2977 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2978
2979 /* Check for additional signaled events */
2980 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2981
2982 /* Check if event is signaled */
2983 ret2 = WaitForSingleObject(w->events[i], 0);
2984 if(ret2 == WAIT_OBJECT_0) {
2985 if (w->func[i])
2986 w->func[i](w->opaque[i]);
2987 } else if (ret2 == WAIT_TIMEOUT) {
2988 } else {
2989 err = GetLastError();
2990 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2991 }
2992 }
2993 } else if (ret == WAIT_TIMEOUT) {
2994 } else {
2995 err = GetLastError();
2996 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
2997 }
2998 }
2999
3000 *timeout = 0;
3001}
3002#else
3003static void host_main_loop_wait(int *timeout)
3004{
3005}
3006#endif
3007
3008void main_loop_wait(int timeout)
3009{
3010 IOHandlerRecord *ioh;
3011 fd_set rfds, wfds, xfds;
3012 int ret, nfds;
3013 struct timeval tv;
3014
3015 qemu_bh_update_timeout(&timeout);
3016
3017 host_main_loop_wait(&timeout);
3018
3019 /* poll any events */
3020 /* XXX: separate device handlers from system ones */
3021 nfds = -1;
3022 FD_ZERO(&rfds);
3023 FD_ZERO(&wfds);
3024 FD_ZERO(&xfds);
3025 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3026 if (ioh->deleted)
3027 continue;
3028 if (ioh->fd_read &&
3029 (!ioh->fd_read_poll ||
3030 ioh->fd_read_poll(ioh->opaque) != 0)) {
3031 FD_SET(ioh->fd, &rfds);
3032 if (ioh->fd > nfds)
3033 nfds = ioh->fd;
3034 }
3035 if (ioh->fd_write) {
3036 FD_SET(ioh->fd, &wfds);
3037 if (ioh->fd > nfds)
3038 nfds = ioh->fd;
3039 }
3040 }
3041
3042 tv.tv_sec = timeout / 1000;
3043 tv.tv_usec = (timeout % 1000) * 1000;
3044
3045#if defined(CONFIG_SLIRP)
3046 if (slirp_is_inited()) {
3047 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3048 }
3049#endif
3050 qemu_mutex_unlock_iothread();
3051 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3052 qemu_mutex_lock_iothread();
3053 if (ret > 0) {
3054 IOHandlerRecord **pioh;
3055
3056 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3057 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
3058 ioh->fd_read(ioh->opaque);
3059 }
3060 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
3061 ioh->fd_write(ioh->opaque);
3062 }
3063 }
3064
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003065 /* remove deleted IO handlers */
3066 pioh = &first_io_handler;
3067 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003068 ioh = *pioh;
3069 if (ioh->deleted) {
3070 *pioh = ioh->next;
3071 qemu_free(ioh);
3072 } else
3073 pioh = &ioh->next;
3074 }
3075 }
3076#if defined(CONFIG_SLIRP)
3077 if (slirp_is_inited()) {
3078 if (ret < 0) {
3079 FD_ZERO(&rfds);
3080 FD_ZERO(&wfds);
3081 FD_ZERO(&xfds);
3082 }
3083 slirp_select_poll(&rfds, &wfds, &xfds);
3084 }
3085#endif
3086 charpipe_poll();
3087
David Turner6a9ef172010-09-09 22:54:36 +02003088 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003089
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003090 /* Check bottom-halves last in case any of the earlier events triggered
3091 them. */
3092 qemu_bh_poll();
3093
3094}
3095
3096static int qemu_cpu_exec(CPUState *env)
3097{
3098 int ret;
3099#ifdef CONFIG_PROFILER
3100 int64_t ti;
3101#endif
3102
3103#ifdef CONFIG_PROFILER
3104 ti = profile_getclock();
3105#endif
3106 if (use_icount) {
3107 int64_t count;
3108 int decr;
3109 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3110 env->icount_decr.u16.low = 0;
3111 env->icount_extra = 0;
3112 count = qemu_next_deadline();
3113 count = (count + (1 << icount_time_shift) - 1)
3114 >> icount_time_shift;
3115 qemu_icount += count;
3116 decr = (count > 0xffff) ? 0xffff : count;
3117 count -= decr;
3118 env->icount_decr.u16.low = decr;
3119 env->icount_extra = count;
3120 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003121#ifdef CONFIG_TRACE
3122 if (tbflush_requested) {
3123 tbflush_requested = 0;
3124 tb_flush(env);
3125 return EXCP_INTERRUPT;
3126 }
3127#endif
3128
3129
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003130 ret = cpu_exec(env);
3131#ifdef CONFIG_PROFILER
3132 qemu_time += profile_getclock() - ti;
3133#endif
3134 if (use_icount) {
3135 /* Fold pending instructions back into the
3136 instruction counter, and clear the interrupt flag. */
3137 qemu_icount -= (env->icount_decr.u16.low
3138 + env->icount_extra);
3139 env->icount_decr.u32 = 0;
3140 env->icount_extra = 0;
3141 }
3142 return ret;
3143}
3144
3145static void tcg_cpu_exec(void)
3146{
3147 int ret = 0;
3148
3149 if (next_cpu == NULL)
3150 next_cpu = first_cpu;
3151 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3152 CPUState *env = cur_cpu = next_cpu;
3153
3154 if (!vm_running)
3155 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003156 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003157 break;
3158 }
3159 if (cpu_can_run(env))
3160 ret = qemu_cpu_exec(env);
3161 if (ret == EXCP_DEBUG) {
3162 gdb_set_stop_cpu(env);
3163 debug_requested = 1;
3164 break;
3165 }
3166 }
3167}
3168
3169static int cpu_has_work(CPUState *env)
3170{
3171 if (env->stop)
3172 return 1;
3173 if (env->stopped)
3174 return 0;
3175 if (!env->halted)
3176 return 1;
3177 if (qemu_cpu_has_work(env))
3178 return 1;
3179 return 0;
3180}
3181
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003182int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003183{
3184 CPUState *env;
3185
3186 for (env = first_cpu; env != NULL; env = env->next_cpu)
3187 if (cpu_has_work(env))
3188 return 1;
3189 return 0;
3190}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003191
3192static int vm_can_run(void)
3193{
3194 if (powerdown_requested)
3195 return 0;
3196 if (reset_requested)
3197 return 0;
3198 if (shutdown_requested)
3199 return 0;
3200 if (debug_requested)
3201 return 0;
3202 return 1;
3203}
3204
3205static void main_loop(void)
3206{
3207 int r;
3208
3209#ifdef CONFIG_IOTHREAD
3210 qemu_system_ready = 1;
3211 qemu_cond_broadcast(&qemu_system_cond);
3212#endif
3213
3214 for (;;) {
3215 do {
3216#ifdef CONFIG_PROFILER
3217 int64_t ti;
3218#endif
3219#ifndef CONFIG_IOTHREAD
3220 tcg_cpu_exec();
3221#endif
3222#ifdef CONFIG_PROFILER
3223 ti = profile_getclock();
3224#endif
3225 main_loop_wait(qemu_calculate_timeout());
3226#ifdef CONFIG_PROFILER
3227 dev_time += profile_getclock() - ti;
3228#endif
3229 } while (vm_can_run());
3230
3231 if (qemu_debug_requested())
3232 vm_stop(EXCP_DEBUG);
3233 if (qemu_shutdown_requested()) {
3234 if (no_shutdown) {
3235 vm_stop(0);
3236 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003237 } else {
3238#if CONFIG_ANDROID_SNAPSHOTS
3239 if (savevm_on_exit != NULL) {
3240 do_savevm(cur_mon, savevm_on_exit);
3241 }
3242#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003243 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003244 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003245 }
3246 if (qemu_reset_requested()) {
3247 pause_all_vcpus();
3248 qemu_system_reset();
3249 resume_all_vcpus();
3250 }
3251 if (qemu_powerdown_requested())
3252 qemu_system_powerdown();
3253 if ((r = qemu_vmstop_requested()))
3254 vm_stop(r);
3255 }
3256 pause_all_vcpus();
3257}
3258
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003259void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003260{
3261 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3262}
3263
3264void qemu_help(int exitcode)
3265{
3266 version();
3267 printf("usage: %s [options] [disk_image]\n"
3268 "\n"
3269 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3270 "\n"
3271#define DEF(option, opt_arg, opt_enum, opt_help) \
3272 opt_help
3273#define DEFHEADING(text) stringify(text) "\n"
3274#include "qemu-options.h"
3275#undef DEF
3276#undef DEFHEADING
3277#undef GEN_DOCS
3278 "\n"
3279 "During emulation, the following keys are useful:\n"
3280 "ctrl-alt-f toggle full screen\n"
3281 "ctrl-alt-n switch to virtual console 'n'\n"
3282 "ctrl-alt toggle mouse and keyboard grab\n"
3283 "\n"
3284 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3285 ,
3286 "qemu",
3287 DEFAULT_RAM_SIZE,
3288#ifndef _WIN32
3289 DEFAULT_NETWORK_SCRIPT,
3290 DEFAULT_NETWORK_DOWN_SCRIPT,
3291#endif
3292 DEFAULT_GDBSTUB_PORT,
3293 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003294 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003295}
3296
3297#define HAS_ARG 0x0001
3298
3299enum {
3300#define DEF(option, opt_arg, opt_enum, opt_help) \
3301 opt_enum,
3302#define DEFHEADING(text)
3303#include "qemu-options.h"
3304#undef DEF
3305#undef DEFHEADING
3306#undef GEN_DOCS
3307};
3308
3309typedef struct QEMUOption {
3310 const char *name;
3311 int flags;
3312 int index;
3313} QEMUOption;
3314
3315static const QEMUOption qemu_options[] = {
3316 { "h", 0, QEMU_OPTION_h },
3317#define DEF(option, opt_arg, opt_enum, opt_help) \
3318 { option, opt_arg, opt_enum },
3319#define DEFHEADING(text)
3320#include "qemu-options.h"
3321#undef DEF
3322#undef DEFHEADING
3323#undef GEN_DOCS
3324 { NULL, 0, 0 },
3325};
3326
3327#ifdef HAS_AUDIO
3328struct soundhw soundhw[] = {
3329#ifdef HAS_AUDIO_CHOICE
3330#if defined(TARGET_I386) || defined(TARGET_MIPS)
3331 {
3332 "pcspk",
3333 "PC speaker",
3334 0,
3335 1,
3336 { .init_isa = pcspk_audio_init }
3337 },
3338#endif
3339
3340#ifdef CONFIG_SB16
3341 {
3342 "sb16",
3343 "Creative Sound Blaster 16",
3344 0,
3345 1,
3346 { .init_isa = SB16_init }
3347 },
3348#endif
3349
3350#ifdef CONFIG_CS4231A
3351 {
3352 "cs4231a",
3353 "CS4231A",
3354 0,
3355 1,
3356 { .init_isa = cs4231a_init }
3357 },
3358#endif
3359
3360#ifdef CONFIG_ADLIB
3361 {
3362 "adlib",
3363#ifdef HAS_YMF262
3364 "Yamaha YMF262 (OPL3)",
3365#else
3366 "Yamaha YM3812 (OPL2)",
3367#endif
3368 0,
3369 1,
3370 { .init_isa = Adlib_init }
3371 },
3372#endif
3373
3374#ifdef CONFIG_GUS
3375 {
3376 "gus",
3377 "Gravis Ultrasound GF1",
3378 0,
3379 1,
3380 { .init_isa = GUS_init }
3381 },
3382#endif
3383
3384#ifdef CONFIG_AC97
3385 {
3386 "ac97",
3387 "Intel 82801AA AC97 Audio",
3388 0,
3389 0,
3390 { .init_pci = ac97_init }
3391 },
3392#endif
3393
3394#ifdef CONFIG_ES1370
3395 {
3396 "es1370",
3397 "ENSONIQ AudioPCI ES1370",
3398 0,
3399 0,
3400 { .init_pci = es1370_init }
3401 },
3402#endif
3403
3404#endif /* HAS_AUDIO_CHOICE */
3405
3406 { NULL, NULL, 0, 0, { NULL } }
3407};
3408
3409static void select_soundhw (const char *optarg)
3410{
3411 struct soundhw *c;
3412
3413 if (*optarg == '?') {
3414 show_valid_cards:
3415
3416 printf ("Valid sound card names (comma separated):\n");
3417 for (c = soundhw; c->name; ++c) {
3418 printf ("%-11s %s\n", c->name, c->descr);
3419 }
3420 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003421 if (*optarg != '?') {
3422 PANIC("Unknown sound card name: %s", optarg);
3423 } else {
3424 QEMU_EXIT(0);
3425 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003426 }
3427 else {
3428 size_t l;
3429 const char *p;
3430 char *e;
3431 int bad_card = 0;
3432
3433 if (!strcmp (optarg, "all")) {
3434 for (c = soundhw; c->name; ++c) {
3435 c->enabled = 1;
3436 }
3437 return;
3438 }
3439
3440 p = optarg;
3441 while (*p) {
3442 e = strchr (p, ',');
3443 l = !e ? strlen (p) : (size_t) (e - p);
3444
3445 for (c = soundhw; c->name; ++c) {
3446 if (!strncmp (c->name, p, l)) {
3447 c->enabled = 1;
3448 break;
3449 }
3450 }
3451
3452 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003453#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003454 if (l > 80) {
3455 fprintf (stderr,
3456 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003457 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003458 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3459 (int) l, p);
3460 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003461#endif // !CONFIG_ANDROID
3462 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003463 }
3464 p += l + (e != NULL);
3465 }
3466
3467 if (bad_card)
3468 goto show_valid_cards;
3469 }
3470}
3471#endif
3472
3473static void select_vgahw (const char *p)
3474{
3475 const char *opts;
3476
3477 cirrus_vga_enabled = 0;
3478 std_vga_enabled = 0;
3479 vmsvga_enabled = 0;
3480 xenfb_enabled = 0;
3481 if (strstart(p, "std", &opts)) {
3482 std_vga_enabled = 1;
3483 } else if (strstart(p, "cirrus", &opts)) {
3484 cirrus_vga_enabled = 1;
3485 } else if (strstart(p, "vmware", &opts)) {
3486 vmsvga_enabled = 1;
3487 } else if (strstart(p, "xenfb", &opts)) {
3488 xenfb_enabled = 1;
3489 } else if (!strstart(p, "none", &opts)) {
3490 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003491 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003492 }
3493 while (*opts) {
3494 const char *nextopt;
3495
3496 if (strstart(opts, ",retrace=", &nextopt)) {
3497 opts = nextopt;
3498 if (strstart(opts, "dumb", &nextopt))
3499 vga_retrace_method = VGA_RETRACE_DUMB;
3500 else if (strstart(opts, "precise", &nextopt))
3501 vga_retrace_method = VGA_RETRACE_PRECISE;
3502 else goto invalid_vga;
3503 } else goto invalid_vga;
3504 opts = nextopt;
3505 }
3506}
3507
3508#ifdef _WIN32
3509static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3510{
3511 exit(STATUS_CONTROL_C_EXIT);
3512 return TRUE;
3513}
3514#endif
3515
3516int qemu_uuid_parse(const char *str, uint8_t *uuid)
3517{
3518 int ret;
3519
3520 if(strlen(str) != 36)
3521 return -1;
3522
3523 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3524 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3525 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3526
3527 if(ret != 16)
3528 return -1;
3529
3530#ifdef TARGET_I386
3531 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3532#endif
3533
3534 return 0;
3535}
3536
3537#define MAX_NET_CLIENTS 32
3538
3539#ifndef _WIN32
3540
3541static void termsig_handler(int signal)
3542{
3543 qemu_system_shutdown_request();
3544}
3545
3546static void sigchld_handler(int signal)
3547{
3548 waitpid(-1, NULL, WNOHANG);
3549}
3550
3551static void sighandler_setup(void)
3552{
3553 struct sigaction act;
3554
3555 memset(&act, 0, sizeof(act));
3556 act.sa_handler = termsig_handler;
3557 sigaction(SIGINT, &act, NULL);
3558 sigaction(SIGHUP, &act, NULL);
3559 sigaction(SIGTERM, &act, NULL);
3560
3561 act.sa_handler = sigchld_handler;
3562 act.sa_flags = SA_NOCLDSTOP;
3563 sigaction(SIGCHLD, &act, NULL);
3564}
3565
3566#endif
3567
3568#ifdef _WIN32
3569/* Look for support files in the same directory as the executable. */
3570static char *find_datadir(const char *argv0)
3571{
3572 char *p;
3573 char buf[MAX_PATH];
3574 DWORD len;
3575
3576 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3577 if (len == 0) {
3578 return NULL;
3579 }
3580
3581 buf[len] = 0;
3582 p = buf + len - 1;
3583 while (p != buf && *p != '\\')
3584 p--;
3585 *p = 0;
3586 if (access(buf, R_OK) == 0) {
3587 return qemu_strdup(buf);
3588 }
3589 return NULL;
3590}
3591#else /* !_WIN32 */
3592
3593/* Find a likely location for support files using the location of the binary.
3594 For installed binaries this will be "$bindir/../share/qemu". When
3595 running from the build tree this will be "$bindir/../pc-bios". */
3596#define SHARE_SUFFIX "/share/qemu"
3597#define BUILD_SUFFIX "/pc-bios"
3598static char *find_datadir(const char *argv0)
3599{
3600 char *dir;
3601 char *p = NULL;
3602 char *res;
3603#ifdef PATH_MAX
3604 char buf[PATH_MAX];
3605#endif
3606 size_t max_len;
3607
3608#if defined(__linux__)
3609 {
3610 int len;
3611 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3612 if (len > 0) {
3613 buf[len] = 0;
3614 p = buf;
3615 }
3616 }
3617#elif defined(__FreeBSD__)
3618 {
3619 int len;
3620 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3621 if (len > 0) {
3622 buf[len] = 0;
3623 p = buf;
3624 }
3625 }
3626#endif
3627 /* If we don't have any way of figuring out the actual executable
3628 location then try argv[0]. */
3629 if (!p) {
3630#ifdef PATH_MAX
3631 p = buf;
3632#endif
3633 p = realpath(argv0, p);
3634 if (!p) {
3635 return NULL;
3636 }
3637 }
3638 dir = dirname(p);
3639 dir = dirname(dir);
3640
3641 max_len = strlen(dir) +
3642 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
3643 res = qemu_mallocz(max_len);
3644 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
3645 if (access(res, R_OK)) {
3646 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
3647 if (access(res, R_OK)) {
3648 qemu_free(res);
3649 res = NULL;
3650 }
3651 }
3652#ifndef PATH_MAX
3653 free(p);
3654#endif
3655 return res;
3656}
3657#undef SHARE_SUFFIX
3658#undef BUILD_SUFFIX
3659#endif
3660
3661char *qemu_find_file(int type, const char *name)
3662{
3663 int len;
3664 const char *subdir;
3665 char *buf;
3666
3667 /* If name contains path separators then try it as a straight path. */
3668 if ((strchr(name, '/') || strchr(name, '\\'))
3669 && access(name, R_OK) == 0) {
3670 return strdup(name);
3671 }
3672 switch (type) {
3673 case QEMU_FILE_TYPE_BIOS:
3674 subdir = "";
3675 break;
3676 case QEMU_FILE_TYPE_KEYMAP:
3677 subdir = "keymaps/";
3678 break;
3679 default:
3680 abort();
3681 }
3682 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3683 buf = qemu_mallocz(len);
3684 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3685 if (access(buf, R_OK)) {
3686 qemu_free(buf);
3687 return NULL;
3688 }
3689 return buf;
3690}
3691
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003692static int
3693add_dns_server( const char* server_name )
3694{
3695 SockAddress addr;
3696
3697 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3698 fprintf(stdout,
3699 "### WARNING: can't resolve DNS server name '%s'\n",
3700 server_name );
3701 return -1;
3702 }
3703
3704 fprintf(stderr,
3705 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3706
3707 if ( slirp_add_dns_server( &addr ) < 0 ) {
3708 fprintf(stderr,
3709 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3710 return -1;
3711 }
3712 return 0;
3713}
3714
3715/* Appends a parameter to a string of parameters separated with space.
3716 * Pararm:
3717 * param_str String containing parameters separated with space.
3718 * param Parameter to append to the string.
3719 * size - Size (in characters) of the buffer addressed by param_str.
3720 */
3721static void
3722append_param(char* param_str, const char* arg, int size)
3723{
3724 if (*param_str) {
3725 strncat(param_str, " ", size);
3726 strncat(param_str, arg, size);
3727 } else {
3728 strncpy(param_str, arg, size);
3729 param_str[size - 1] = '\0';
3730 }
3731}
3732
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003733int main(int argc, char **argv, char **envp)
3734{
3735 const char *gdbstub_dev = NULL;
3736 uint32_t boot_devices_bitmap = 0;
3737 int i;
3738 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003739 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003740 const char *initrd_filename;
3741 const char *kernel_filename, *kernel_cmdline;
3742 const char *boot_devices = "";
3743 DisplayState *ds;
3744 DisplayChangeListener *dcl;
3745 int cyls, heads, secs, translation;
3746 const char *net_clients[MAX_NET_CLIENTS];
3747 int nb_net_clients;
3748 const char *bt_opts[MAX_BT_CMDLINE];
3749 int nb_bt_opts;
3750 int hda_index;
3751 int optind;
3752 const char *r, *optarg;
3753 CharDriverState *monitor_hd = NULL;
3754 const char *monitor_device;
3755 const char *serial_devices[MAX_SERIAL_PORTS];
3756 int serial_device_index;
3757 const char *parallel_devices[MAX_PARALLEL_PORTS];
3758 int parallel_device_index;
3759 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3760 int virtio_console_index;
3761 const char *loadvm = NULL;
3762 QEMUMachine *machine;
3763 const char *cpu_model;
3764 const char *usb_devices[MAX_USB_CMDLINE];
3765 int usb_devices_index;
3766#ifndef _WIN32
3767 int fds[2];
3768#endif
3769 int tb_size;
3770 const char *pid_file = NULL;
3771 const char *incoming = NULL;
3772#ifndef _WIN32
3773 int fd = 0;
3774 struct passwd *pwd = NULL;
3775 const char *chroot_dir = NULL;
3776 const char *run_as = NULL;
3777#endif
3778 CPUState *env;
3779 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003780#ifdef CONFIG_STANDALONE_CORE
3781 IniFile* hw_ini = NULL;
3782#endif // CONFIG_STANDALONE_CORE
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003783 /* Container for the kernel initialization parameters collected in this
3784 * routine. */
3785 char kernel_cmdline_append[1024];
3786 /* Combines kernel initialization parameters passed from the UI with
3787 * the parameters collected in this routine. */
3788 char kernel_cmdline_full[1024];
3789 char tmp_str[1024];
3790 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003791
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003792 /* Initialize sockets before anything else, so we can properly report
3793 * initialization failures back to the UI. */
3794#ifdef _WIN32
3795 socket_init();
3796#endif
3797
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003798 init_clocks();
3799
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003800 qemu_cache_utils_init(envp);
3801
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003802 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003803#ifndef _WIN32
3804 {
3805 struct sigaction act;
3806 sigfillset(&act.sa_mask);
3807 act.sa_flags = 0;
3808 act.sa_handler = SIG_IGN;
3809 sigaction(SIGPIPE, &act, NULL);
3810 }
3811#else
3812 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3813 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3814 QEMU to run on a single CPU */
3815 {
3816 HANDLE h;
3817 DWORD mask, smask;
3818 int i;
3819 h = GetCurrentProcess();
3820 if (GetProcessAffinityMask(h, &mask, &smask)) {
3821 for(i = 0; i < 32; i++) {
3822 if (mask & (1 << i))
3823 break;
3824 }
3825 if (i != 32) {
3826 mask = 1 << i;
3827 SetProcessAffinityMask(h, mask);
3828 }
3829 }
3830 }
3831#endif
3832
3833 module_call_init(MODULE_INIT_MACHINE);
3834 machine = find_default_machine();
3835 cpu_model = NULL;
3836 initrd_filename = NULL;
3837 ram_size = 0;
3838 snapshot = 0;
3839 kernel_filename = NULL;
3840 kernel_cmdline = "";
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003841 kernel_cmdline_append[0] = '\0';
3842 kernel_cmdline_full[0] = '\0';
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003843 cyls = heads = secs = 0;
3844 translation = BIOS_ATA_TRANSLATION_AUTO;
3845 monitor_device = "vc:80Cx24C";
3846
3847 serial_devices[0] = "vc:80Cx24C";
3848 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3849 serial_devices[i] = NULL;
3850 serial_device_index = 0;
3851
3852 parallel_devices[0] = "vc:80Cx24C";
3853 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3854 parallel_devices[i] = NULL;
3855 parallel_device_index = 0;
3856
3857 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3858 virtio_consoles[i] = NULL;
3859 virtio_console_index = 0;
3860
3861 for (i = 0; i < MAX_NODES; i++) {
3862 node_mem[i] = 0;
3863 node_cpumask[i] = 0;
3864 }
3865
3866 usb_devices_index = 0;
3867
3868 nb_net_clients = 0;
3869 nb_bt_opts = 0;
3870 nb_drives = 0;
3871 nb_drives_opt = 0;
3872 nb_numa_nodes = 0;
3873 hda_index = -1;
3874
3875 nb_nics = 0;
3876
3877 tb_size = 0;
3878 autostart= 1;
3879
3880 register_watchdogs();
3881
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003882 /* Initialize boot properties. */
3883 boot_property_init_service();
3884
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003885 optind = 1;
3886 for(;;) {
3887 if (optind >= argc)
3888 break;
3889 r = argv[optind];
3890 if (r[0] != '-') {
3891 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
3892 } else {
3893 const QEMUOption *popt;
3894
3895 optind++;
3896 /* Treat --foo the same as -foo. */
3897 if (r[1] == '-')
3898 r++;
3899 popt = qemu_options;
3900 for(;;) {
3901 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003902 PANIC("%s: invalid option -- '%s'",
3903 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003904 }
3905 if (!strcmp(popt->name, r + 1))
3906 break;
3907 popt++;
3908 }
3909 if (popt->flags & HAS_ARG) {
3910 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003911 PANIC("%s: option '%s' requires an argument",
3912 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003913 }
3914 optarg = argv[optind++];
3915 } else {
3916 optarg = NULL;
3917 }
3918
3919 switch(popt->index) {
3920 case QEMU_OPTION_M:
3921 machine = find_machine(optarg);
3922 if (!machine) {
3923 QEMUMachine *m;
3924 printf("Supported machines are:\n");
3925 for(m = first_machine; m != NULL; m = m->next) {
3926 printf("%-10s %s%s\n",
3927 m->name, m->desc,
3928 m->is_default ? " (default)" : "");
3929 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003930 if (*optarg != '?') {
3931 PANIC("Invalid machine parameter: %s",
3932 optarg);
3933 } else {
3934 QEMU_EXIT(0);
3935 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003936 }
3937 break;
3938 case QEMU_OPTION_cpu:
3939 /* hw initialization will check this */
3940 if (*optarg == '?') {
3941/* XXX: implement xxx_cpu_list for targets that still miss it */
3942#if defined(cpu_list)
3943 cpu_list(stdout, &fprintf);
3944#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003945 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003946 } else {
3947 cpu_model = optarg;
3948 }
3949 break;
3950 case QEMU_OPTION_initrd:
3951 initrd_filename = optarg;
3952 break;
3953 case QEMU_OPTION_hda:
3954 if (cyls == 0)
3955 hda_index = drive_add(optarg, HD_ALIAS, 0);
3956 else
3957 hda_index = drive_add(optarg, HD_ALIAS
3958 ",cyls=%d,heads=%d,secs=%d%s",
3959 0, cyls, heads, secs,
3960 translation == BIOS_ATA_TRANSLATION_LBA ?
3961 ",trans=lba" :
3962 translation == BIOS_ATA_TRANSLATION_NONE ?
3963 ",trans=none" : "");
3964 break;
3965 case QEMU_OPTION_hdb:
3966 case QEMU_OPTION_hdc:
3967 case QEMU_OPTION_hdd:
3968 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
3969 break;
3970 case QEMU_OPTION_drive:
3971 drive_add(NULL, "%s", optarg);
3972 break;
3973 case QEMU_OPTION_mtdblock:
3974 drive_add(optarg, MTD_ALIAS);
3975 break;
3976 case QEMU_OPTION_sd:
3977 drive_add(optarg, SD_ALIAS);
3978 break;
3979 case QEMU_OPTION_pflash:
3980 drive_add(optarg, PFLASH_ALIAS);
3981 break;
3982 case QEMU_OPTION_snapshot:
3983 snapshot = 1;
3984 break;
3985 case QEMU_OPTION_hdachs:
3986 {
3987 const char *p;
3988 p = optarg;
3989 cyls = strtol(p, (char **)&p, 0);
3990 if (cyls < 1 || cyls > 16383)
3991 goto chs_fail;
3992 if (*p != ',')
3993 goto chs_fail;
3994 p++;
3995 heads = strtol(p, (char **)&p, 0);
3996 if (heads < 1 || heads > 16)
3997 goto chs_fail;
3998 if (*p != ',')
3999 goto chs_fail;
4000 p++;
4001 secs = strtol(p, (char **)&p, 0);
4002 if (secs < 1 || secs > 63)
4003 goto chs_fail;
4004 if (*p == ',') {
4005 p++;
4006 if (!strcmp(p, "none"))
4007 translation = BIOS_ATA_TRANSLATION_NONE;
4008 else if (!strcmp(p, "lba"))
4009 translation = BIOS_ATA_TRANSLATION_LBA;
4010 else if (!strcmp(p, "auto"))
4011 translation = BIOS_ATA_TRANSLATION_AUTO;
4012 else
4013 goto chs_fail;
4014 } else if (*p != '\0') {
4015 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004016 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004017 }
4018 if (hda_index != -1)
4019 snprintf(drives_opt[hda_index].opt,
4020 sizeof(drives_opt[hda_index].opt),
4021 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4022 0, cyls, heads, secs,
4023 translation == BIOS_ATA_TRANSLATION_LBA ?
4024 ",trans=lba" :
4025 translation == BIOS_ATA_TRANSLATION_NONE ?
4026 ",trans=none" : "");
4027 }
4028 break;
4029 case QEMU_OPTION_numa:
4030 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004031 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004032 }
4033 numa_add(optarg);
4034 break;
4035 case QEMU_OPTION_nographic:
4036 display_type = DT_NOGRAPHIC;
4037 break;
4038#ifdef CONFIG_CURSES
4039 case QEMU_OPTION_curses:
4040 display_type = DT_CURSES;
4041 break;
4042#endif
4043 case QEMU_OPTION_portrait:
4044 graphic_rotate = 1;
4045 break;
4046 case QEMU_OPTION_kernel:
4047 kernel_filename = optarg;
4048 break;
4049 case QEMU_OPTION_append:
4050 kernel_cmdline = optarg;
4051 break;
4052 case QEMU_OPTION_cdrom:
4053 drive_add(optarg, CDROM_ALIAS);
4054 break;
4055 case QEMU_OPTION_boot:
4056 boot_devices = optarg;
4057 /* We just do some generic consistency checks */
4058 {
4059 /* Could easily be extended to 64 devices if needed */
4060 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004061
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004062 boot_devices_bitmap = 0;
4063 for (p = boot_devices; *p != '\0'; p++) {
4064 /* Allowed boot devices are:
4065 * a b : floppy disk drives
4066 * c ... f : IDE disk drives
4067 * g ... m : machine implementation dependant drives
4068 * n ... p : network devices
4069 * It's up to each machine implementation to check
4070 * if the given boot devices match the actual hardware
4071 * implementation and firmware features.
4072 */
4073 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004074 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004075 }
4076 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004077 PANIC(
4078 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004079 }
4080 boot_devices_bitmap |= 1 << (*p - 'a');
4081 }
4082 }
4083 break;
4084 case QEMU_OPTION_fda:
4085 case QEMU_OPTION_fdb:
4086 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4087 break;
4088#ifdef TARGET_I386
4089 case QEMU_OPTION_no_fd_bootchk:
4090 fd_bootchk = 0;
4091 break;
4092#endif
4093 case QEMU_OPTION_net:
4094 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004095 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004096 }
4097 net_clients[nb_net_clients] = optarg;
4098 nb_net_clients++;
4099 break;
4100#ifdef CONFIG_SLIRP
4101 case QEMU_OPTION_tftp:
4102 tftp_prefix = optarg;
4103 break;
4104 case QEMU_OPTION_bootp:
4105 bootp_filename = optarg;
4106 break;
4107#if 0 /* ANDROID disabled */
4108#ifndef _WIN32
4109 case QEMU_OPTION_smb:
4110 net_slirp_smb(optarg);
4111 break;
4112#endif
4113#endif /* ANDROID */
4114 case QEMU_OPTION_redir:
4115 net_slirp_redir(NULL, optarg, NULL);
4116 break;
4117#endif
4118 case QEMU_OPTION_bt:
4119 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004120 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004121 }
4122 bt_opts[nb_bt_opts++] = optarg;
4123 break;
4124#ifdef HAS_AUDIO
4125 case QEMU_OPTION_audio_help:
4126 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004127 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004128 break;
4129 case QEMU_OPTION_soundhw:
4130 select_soundhw (optarg);
4131 break;
4132#endif
4133 case QEMU_OPTION_h:
4134 qemu_help(0);
4135 break;
4136 case QEMU_OPTION_version:
4137 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004138 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004139 break;
4140 case QEMU_OPTION_m: {
4141 uint64_t value;
4142 char *ptr;
4143
4144 value = strtoul(optarg, &ptr, 10);
4145 switch (*ptr) {
4146 case 0: case 'M': case 'm':
4147 value <<= 20;
4148 break;
4149 case 'G': case 'g':
4150 value <<= 30;
4151 break;
4152 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004153 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004154 }
4155
4156 /* On 32-bit hosts, QEMU is limited by virtual address space */
4157 if (value > (2047 << 20)
4158#ifndef CONFIG_KQEMU
4159 && HOST_LONG_BITS == 32
4160#endif
4161 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004162 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004163 }
4164 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004165 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004166 }
4167 ram_size = value;
4168 break;
4169 }
4170 case QEMU_OPTION_d:
4171 {
4172 int mask;
4173 const CPULogItem *item;
4174
4175 mask = cpu_str_to_log_mask(optarg);
4176 if (!mask) {
4177 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004178 for(item = cpu_log_items; item->mask != 0; item++) {
4179 printf("%-10s %s\n", item->name, item->help);
4180 }
4181 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004182 }
4183 cpu_set_log(mask);
4184 }
4185 break;
4186 case QEMU_OPTION_s:
4187 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4188 break;
4189 case QEMU_OPTION_gdb:
4190 gdbstub_dev = optarg;
4191 break;
4192 case QEMU_OPTION_L:
4193 data_dir = optarg;
4194 break;
4195 case QEMU_OPTION_bios:
4196 bios_name = optarg;
4197 break;
4198 case QEMU_OPTION_singlestep:
4199 singlestep = 1;
4200 break;
4201 case QEMU_OPTION_S:
4202#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004203 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004204#endif
4205 autostart = 0;
4206 break;
4207#ifndef _WIN32
4208 case QEMU_OPTION_k:
4209 keyboard_layout = optarg;
4210 break;
4211#endif
4212 case QEMU_OPTION_localtime:
4213 rtc_utc = 0;
4214 break;
4215 case QEMU_OPTION_vga:
4216 select_vgahw (optarg);
4217 break;
4218#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4219 case QEMU_OPTION_g:
4220 {
4221 const char *p;
4222 int w, h, depth;
4223 p = optarg;
4224 w = strtol(p, (char **)&p, 10);
4225 if (w <= 0) {
4226 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004227 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004228 }
4229 if (*p != 'x')
4230 goto graphic_error;
4231 p++;
4232 h = strtol(p, (char **)&p, 10);
4233 if (h <= 0)
4234 goto graphic_error;
4235 if (*p == 'x') {
4236 p++;
4237 depth = strtol(p, (char **)&p, 10);
4238 if (depth != 8 && depth != 15 && depth != 16 &&
4239 depth != 24 && depth != 32)
4240 goto graphic_error;
4241 } else if (*p == '\0') {
4242 depth = graphic_depth;
4243 } else {
4244 goto graphic_error;
4245 }
4246
4247 graphic_width = w;
4248 graphic_height = h;
4249 graphic_depth = depth;
4250 }
4251 break;
4252#endif
4253 case QEMU_OPTION_echr:
4254 {
4255 char *r;
4256 term_escape_char = strtol(optarg, &r, 0);
4257 if (r == optarg)
4258 printf("Bad argument to echr\n");
4259 break;
4260 }
4261 case QEMU_OPTION_monitor:
4262 monitor_device = optarg;
4263 break;
4264 case QEMU_OPTION_serial:
4265 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004266 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004267 }
4268 serial_devices[serial_device_index] = optarg;
4269 serial_device_index++;
4270 break;
4271 case QEMU_OPTION_watchdog:
4272 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004273 if (i > 0) {
4274 if (i == 1) {
4275 PANIC("Invalid watchdog parameter: %s",
4276 optarg);
4277 } else {
4278 QEMU_EXIT(0);
4279 }
4280 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004281 break;
4282 case QEMU_OPTION_watchdog_action:
4283 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004284 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004285 }
4286 break;
4287 case QEMU_OPTION_virtiocon:
4288 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004289 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004290 }
4291 virtio_consoles[virtio_console_index] = optarg;
4292 virtio_console_index++;
4293 break;
4294 case QEMU_OPTION_parallel:
4295 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004296 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004297 }
4298 parallel_devices[parallel_device_index] = optarg;
4299 parallel_device_index++;
4300 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004301 case QEMU_OPTION_loadvm:
4302 loadvm = optarg;
4303 break;
4304#if CONFIG_ANDROID_SNAPSHOTS
4305 case QEMU_OPTION_savevm_on_exit:
4306 savevm_on_exit = optarg;
4307 break;
4308#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004309 case QEMU_OPTION_full_screen:
4310 full_screen = 1;
4311 break;
4312#ifdef CONFIG_SDL
4313 case QEMU_OPTION_no_frame:
4314 no_frame = 1;
4315 break;
4316 case QEMU_OPTION_alt_grab:
4317 alt_grab = 1;
4318 break;
4319 case QEMU_OPTION_no_quit:
4320 no_quit = 1;
4321 break;
4322 case QEMU_OPTION_sdl:
4323 display_type = DT_SDL;
4324 break;
4325#endif
4326 case QEMU_OPTION_pidfile:
4327 pid_file = optarg;
4328 break;
4329#ifdef TARGET_I386
4330 case QEMU_OPTION_win2k_hack:
4331 win2k_install_hack = 1;
4332 break;
4333 case QEMU_OPTION_rtc_td_hack:
4334 rtc_td_hack = 1;
4335 break;
4336 case QEMU_OPTION_acpitable:
4337 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004338 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004339 }
4340 break;
4341 case QEMU_OPTION_smbios:
4342 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004343 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004344 }
4345 break;
4346#endif
4347#ifdef CONFIG_KQEMU
4348 case QEMU_OPTION_no_kqemu:
4349 kqemu_allowed = 0;
4350 break;
4351 case QEMU_OPTION_kernel_kqemu:
4352 kqemu_allowed = 2;
4353 break;
4354#endif
4355#ifdef CONFIG_KVM
4356 case QEMU_OPTION_enable_kvm:
4357 kvm_allowed = 1;
4358#ifdef CONFIG_KQEMU
4359 kqemu_allowed = 0;
4360#endif
4361 break;
4362#endif
4363 case QEMU_OPTION_usb:
4364 usb_enabled = 1;
4365 break;
4366 case QEMU_OPTION_usbdevice:
4367 usb_enabled = 1;
4368 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004369 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004370 }
4371 usb_devices[usb_devices_index] = optarg;
4372 usb_devices_index++;
4373 break;
4374 case QEMU_OPTION_smp:
4375 smp_cpus = atoi(optarg);
4376 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004377 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004378 }
4379 break;
4380 case QEMU_OPTION_vnc:
4381 display_type = DT_VNC;
4382 vnc_display = optarg;
4383 break;
4384#ifdef TARGET_I386
4385 case QEMU_OPTION_no_acpi:
4386 acpi_enabled = 0;
4387 break;
4388 case QEMU_OPTION_no_hpet:
4389 no_hpet = 1;
4390 break;
4391 case QEMU_OPTION_no_virtio_balloon:
4392 no_virtio_balloon = 1;
4393 break;
4394#endif
4395 case QEMU_OPTION_no_reboot:
4396 no_reboot = 1;
4397 break;
4398 case QEMU_OPTION_no_shutdown:
4399 no_shutdown = 1;
4400 break;
4401 case QEMU_OPTION_show_cursor:
4402 cursor_hide = 0;
4403 break;
4404 case QEMU_OPTION_uuid:
4405 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004406 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004407 }
4408 break;
4409#ifndef _WIN32
4410 case QEMU_OPTION_daemonize:
4411 daemonize = 1;
4412 break;
4413#endif
4414 case QEMU_OPTION_option_rom:
4415 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004416 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004417 }
4418 option_rom[nb_option_roms] = optarg;
4419 nb_option_roms++;
4420 break;
4421#if defined(TARGET_ARM) || defined(TARGET_M68K)
4422 case QEMU_OPTION_semihosting:
4423 semihosting_enabled = 1;
4424 break;
4425#endif
4426 case QEMU_OPTION_name:
4427 qemu_name = optarg;
4428 break;
4429#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4430 case QEMU_OPTION_prom_env:
4431 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004432 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004433 }
4434 prom_envs[nb_prom_envs] = optarg;
4435 nb_prom_envs++;
4436 break;
4437#endif
4438#ifdef TARGET_ARM
4439 case QEMU_OPTION_old_param:
4440 old_param = 1;
4441 break;
4442#endif
4443 case QEMU_OPTION_clock:
4444 configure_alarms(optarg);
4445 break;
4446 case QEMU_OPTION_startdate:
4447 {
4448 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004449 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004450 if (!strcmp(optarg, "now")) {
4451 rtc_date_offset = -1;
4452 } else {
4453 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4454 &tm.tm_year,
4455 &tm.tm_mon,
4456 &tm.tm_mday,
4457 &tm.tm_hour,
4458 &tm.tm_min,
4459 &tm.tm_sec) == 6) {
4460 /* OK */
4461 } else if (sscanf(optarg, "%d-%d-%d",
4462 &tm.tm_year,
4463 &tm.tm_mon,
4464 &tm.tm_mday) == 3) {
4465 tm.tm_hour = 0;
4466 tm.tm_min = 0;
4467 tm.tm_sec = 0;
4468 } else {
4469 goto date_fail;
4470 }
4471 tm.tm_year -= 1900;
4472 tm.tm_mon--;
4473 rtc_start_date = mktimegm(&tm);
4474 if (rtc_start_date == -1) {
4475 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004476 PANIC("Invalid date format. Valid format are:\n"
4477 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004478 }
4479 rtc_date_offset = time(NULL) - rtc_start_date;
4480 }
4481 }
4482 break;
4483 case QEMU_OPTION_tb_size:
4484 tb_size = strtol(optarg, NULL, 0);
4485 if (tb_size < 0)
4486 tb_size = 0;
4487 break;
4488 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004489 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004490 break;
4491 case QEMU_OPTION_incoming:
4492 incoming = optarg;
4493 break;
4494#ifndef _WIN32
4495 case QEMU_OPTION_chroot:
4496 chroot_dir = optarg;
4497 break;
4498 case QEMU_OPTION_runas:
4499 run_as = optarg;
4500 break;
4501#endif
4502#ifdef CONFIG_XEN
4503 case QEMU_OPTION_xen_domid:
4504 xen_domid = atoi(optarg);
4505 break;
4506 case QEMU_OPTION_xen_create:
4507 xen_mode = XEN_CREATE;
4508 break;
4509 case QEMU_OPTION_xen_attach:
4510 xen_mode = XEN_ATTACH;
4511 break;
4512#endif
4513
4514
4515 case QEMU_OPTION_mic:
4516 audio_input_source = (char*)optarg;
4517 break;
4518#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004519 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004520 trace_filename = optarg;
4521 tracing = 1;
4522 break;
4523#if 0
4524 case QEMU_OPTION_trace_miss:
4525 trace_cache_miss = 1;
4526 break;
4527 case QEMU_OPTION_trace_addr:
4528 trace_all_addr = 1;
4529 break;
4530#endif
4531 case QEMU_OPTION_tracing:
4532 if (strcmp(optarg, "off") == 0)
4533 tracing = 0;
4534 else if (strcmp(optarg, "on") == 0 && trace_filename)
4535 tracing = 1;
4536 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004537 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004538 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004539 }
4540 break;
4541#if 0
4542 case QEMU_OPTION_dcache_load_miss:
4543 dcache_load_miss_penalty = atoi(optarg);
4544 break;
4545 case QEMU_OPTION_dcache_store_miss:
4546 dcache_store_miss_penalty = atoi(optarg);
4547 break;
4548#endif
4549#endif
4550#ifdef CONFIG_NAND
4551 case QEMU_OPTION_nand:
4552 nand_add_dev(optarg);
4553 break;
4554#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004555 case QEMU_OPTION_android_ports:
4556 android_op_ports = (char*)optarg;
4557 break;
4558
4559 case QEMU_OPTION_android_port:
4560 android_op_port = (char*)optarg;
4561 break;
4562
4563 case QEMU_OPTION_android_report_console:
4564 android_op_report_console = (char*)optarg;
4565 break;
4566
4567 case QEMU_OPTION_http_proxy:
4568 op_http_proxy = (char*)optarg;
4569 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004570
4571 case QEMU_OPTION_charmap:
4572 op_charmap_file = (char*)optarg;
4573 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004574
4575 case QEMU_OPTION_android_gui:
4576 android_op_gui = (char*)optarg;
4577 break;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004578
4579 case QEMU_OPTION_android_hw:
4580 android_op_hwini = (char*)optarg;
4581 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004582
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004583 case QEMU_OPTION_dns_server:
4584 android_op_dns_server = (char*)optarg;
4585 break;
4586
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004587 case QEMU_OPTION_radio:
4588 android_op_radio = (char*)optarg;
4589 break;
4590
4591 case QEMU_OPTION_gps:
4592 android_op_gps = (char*)optarg;
4593 break;
4594
4595 case QEMU_OPTION_audio:
4596 android_op_audio = (char*)optarg;
4597 break;
4598
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004599 case QEMU_OPTION_cpu_delay:
4600 android_op_cpu_delay = (char*)optarg;
4601 break;
4602
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004603 case QEMU_OPTION_show_kernel:
4604 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4605 break;
4606
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004607#ifdef CONFIG_NAND_LIMITS
4608 case QEMU_OPTION_nand_limits:
4609 android_op_nand_limits = (char*)optarg;
4610 break;
4611#endif // CONFIG_NAND_LIMITS
4612
4613 case QEMU_OPTION_netspeed:
4614 android_op_netspeed = (char*)optarg;
4615 break;
4616
4617 case QEMU_OPTION_netdelay:
4618 android_op_netdelay = (char*)optarg;
4619 break;
4620
4621 case QEMU_OPTION_netfast:
4622 android_op_netfast = 1;
4623 break;
4624
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004625 case QEMU_OPTION_tcpdump:
4626 android_op_tcpdump = (char*)optarg;
4627 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004628
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004629 case QEMU_OPTION_boot_property:
4630 boot_property_parse_option((char*)optarg);
4631 break;
4632
4633 case QEMU_OPTION_lcd_density:
4634 android_op_lcd_density = (char*)optarg;
4635 break;
4636
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004637 case QEMU_OPTION_ui_port:
4638 android_op_ui_port = (char*)optarg;
4639 break;
4640
4641 case QEMU_OPTION_ui_settings:
4642 android_op_ui_settings = (char*)optarg;
4643 break;
4644
David 'Digit' Turnerca29fbb2011-01-02 13:17:22 +01004645 case QEMU_OPTION_audio_test_out:
4646 android_audio_test_start_out();
4647 break;
4648
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004649#ifdef CONFIG_MEMCHECK
4650 case QEMU_OPTION_android_memcheck:
4651 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004652 snprintf(tmp_str, sizeof(tmp_str), "memcheck=%s",
4653 android_op_memcheck);
4654 tmp_str[sizeof(tmp_str) - 1] = '\0';
4655 /* This will set ro.kernel.memcheck system property
4656 * to memcheck's tracing flags. */
4657 append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004658 break;
4659#endif // CONFIG_MEMCHECK
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004660 }
4661 }
4662 }
4663
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004664 /* Initialize character map. */
4665 if (android_charmap_setup(op_charmap_file)) {
4666 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004667 PANIC(
4668 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004669 op_charmap_file);
4670 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004671 PANIC(
4672 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004673 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004674 }
4675
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004676 /* If no data_dir is specified then try to find it relative to the
4677 executable path. */
4678 if (!data_dir) {
4679 data_dir = find_datadir(argv[0]);
4680 }
4681 /* If all else fails use the install patch specified when building. */
4682 if (!data_dir) {
4683 data_dir = CONFIG_QEMU_SHAREDIR;
4684 }
4685
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004686#ifdef CONFIG_STANDALONE_CORE
4687 /* Initialize hardware configuration. */
4688 if (android_op_hwini) {
4689 hw_ini = iniFile_newFromFile(android_op_hwini);
4690 if (hw_ini == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004691 PANIC("Could not find %s file.", android_op_hwini);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004692 }
4693 } else {
4694 hw_ini = iniFile_newFromMemory("", 0);
4695 }
4696
4697 androidHwConfig_read(android_hw, hw_ini);
4698 iniFile_free(hw_ini);
4699#endif // CONFIG_STANDALONE_CORE
4700
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004701#ifdef CONFIG_NAND_LIMITS
4702 /* Init nand stuff. */
4703 if (android_op_nand_limits) {
4704 parse_nand_limits(android_op_nand_limits);
4705 }
4706#endif // CONFIG_NAND_LIMITS
4707
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004708 /* Set the VM's max heap size, passed as a boot property */
4709 if (android_hw->vm_heapSize > 0) {
4710 char tmp[64];
4711 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
4712 boot_property_add("dalvik.vm.heapsize",tmp);
4713 }
4714
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004715 /* Initialize net speed and delays stuff. */
4716 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004717 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004718 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004719 }
4720
4721 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004722 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004723 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004724 }
4725
4726 if (android_op_netfast) {
4727 qemu_net_download_speed = 0;
4728 qemu_net_upload_speed = 0;
4729 qemu_net_min_latency = 0;
4730 qemu_net_max_latency = 0;
4731 }
4732
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004733 /* Initialize LCD density */
4734 if (android_op_lcd_density) {
4735 char* end;
4736 long density = strtol(android_op_lcd_density, &end, 0);
4737 if (end == NULL || *end || density < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004738 PANIC("option -lcd-density must be a positive integer");
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004739 }
4740 hwLcd_setBootProperty(density);
4741 }
4742
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004743 /* Initialize TCP dump */
4744 if (android_op_tcpdump) {
4745 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
4746 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
4747 }
4748 }
4749
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004750 /* Initialize modem */
4751 if (android_op_radio) {
4752 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
4753 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004754 PANIC("unsupported character device specification: %s\n"
4755 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004756 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004757 }
4758 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
4759 } else if (android_hw->hw_gsmModem != 0 ) {
4760 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004761 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004762 }
4763 }
4764
4765 /* Initialize GPS */
4766 if (android_op_gps) {
4767 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
4768 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004769 PANIC("unsupported character device specification: %s\n"
4770 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004771 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004772 }
4773 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
4774 } else if (android_hw->hw_gps != 0) {
4775 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004776 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004777 }
4778 }
4779
4780 /* Initialize audio. */
4781 if (android_op_audio) {
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004782 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004783 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004784 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004785 }
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004786 }
4787
4788 if (android_op_cpu_delay) {
4789 char* end;
4790 long delay = strtol(android_op_cpu_delay, &end, 0);
4791 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004792 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004793 }
4794 if (delay > 0)
4795 delay = (1000-delay);
4796
4797 qemu_cpu_delay = (int) delay;
4798 }
4799
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004800 if (android_op_dns_server) {
4801 char* x = strchr(android_op_dns_server, ',');
4802 dns_count = 0;
4803 if (x == NULL)
4804 {
4805 if ( add_dns_server( android_op_dns_server ) == 0 )
4806 dns_count = 1;
4807 }
4808 else
4809 {
4810 x = android_op_dns_server;
4811 while (*x) {
4812 char* y = strchr(x, ',');
4813
4814 if (y != NULL) {
4815 *y = 0;
4816 y++;
4817 } else {
4818 y = x + strlen(x);
4819 }
4820
4821 if (y > x && add_dns_server( x ) == 0) {
4822 dns_count += 1;
4823 }
4824 x = y;
4825 }
4826 }
4827 if (dns_count == 0)
4828 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
4829 }
4830
4831 if (dns_count == 0)
4832 dns_count = slirp_get_system_dns_servers();
4833 if (dns_count) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004834 snprintf(tmp_str, sizeof(tmp_str), "ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004835 append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
4836 }
4837
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004838#ifdef CONFIG_MEMCHECK
4839 if (android_op_memcheck) {
4840 memcheck_init(android_op_memcheck);
4841 }
4842#endif // CONFIG_MEMCHECK
4843
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004844#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
4845 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004846 PANIC(
4847 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004848 }
4849#endif
4850
4851 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
4852 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004853 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
4854 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004855 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004856 }
4857
4858 if (display_type == DT_NOGRAPHIC) {
4859 if (serial_device_index == 0)
4860 serial_devices[0] = "stdio";
4861 if (parallel_device_index == 0)
4862 parallel_devices[0] = "null";
4863 if (strncmp(monitor_device, "vc", 2) == 0)
4864 monitor_device = "stdio";
4865 }
4866
4867#ifndef _WIN32
4868 if (daemonize) {
4869 pid_t pid;
4870
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004871 if (pipe(fds) == -1) {
4872 PANIC("Unable to aquire pidfile");
4873 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004874
4875 pid = fork();
4876 if (pid > 0) {
4877 uint8_t status;
4878 ssize_t len;
4879
4880 close(fds[1]);
4881
4882 again:
4883 len = read(fds[0], &status, 1);
4884 if (len == -1 && (errno == EINTR))
4885 goto again;
4886
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004887 if (len != 1) {
4888 PANIC("Error when aquiring pidfile");
4889 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004890 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004891 PANIC("Could not acquire pidfile");
4892 } else {
4893 QEMU_EXIT(0);
4894 }
4895 } else if (pid < 0) {
4896 PANIC("Unable to daemonize");
4897 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004898
4899 setsid();
4900
4901 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004902 if (pid > 0) {
4903 QEMU_EXIT(0);
4904 } else if (pid < 0) {
4905 PANIC("Could not acquire pid file");
4906 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004907
4908 umask(027);
4909
4910 signal(SIGTSTP, SIG_IGN);
4911 signal(SIGTTOU, SIG_IGN);
4912 signal(SIGTTIN, SIG_IGN);
4913 }
4914
4915 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4916 if (daemonize) {
4917 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02004918 int ret;
4919 do {
4920 ret = write(fds[1], &status, 1);
4921 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004922 PANIC("Could not acquire pid file");
4923 } else {
4924 PANIC("Could not acquire pid file");
4925 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004926 }
4927#endif
4928
4929#ifdef CONFIG_KQEMU
4930 if (smp_cpus > 1)
4931 kqemu_allowed = 0;
4932#endif
4933 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004934 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004935 }
4936 linux_boot = (kernel_filename != NULL);
4937 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
4938
4939 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004940 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004941 }
4942
4943 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004944 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004945 }
4946
4947 /* boot to floppy or the default cd if no hard disk defined yet */
4948 if (!boot_devices[0]) {
4949 boot_devices = "cad";
4950 }
4951 setvbuf(stdout, NULL, _IOLBF, 0);
4952
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004953 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004954 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004955 }
David Turner6a9ef172010-09-09 22:54:36 +02004956 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004957
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004958 /* init network clients */
4959 if (nb_net_clients == 0) {
4960 /* if no clients, we use a default config */
4961 net_clients[nb_net_clients++] = "nic";
4962#ifdef CONFIG_SLIRP
4963 net_clients[nb_net_clients++] = "user";
4964#endif
4965 }
4966
4967 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004968 if (net_client_parse(net_clients[i]) < 0) {
4969 PANIC("Unable to parse net clients");
4970 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004971 }
4972 net_client_check();
4973
4974#ifdef TARGET_I386
4975 /* XXX: this should be moved in the PC machine instantiation code */
4976 if (net_boot != 0) {
4977 int netroms = 0;
4978 for (i = 0; i < nb_nics && i < 4; i++) {
4979 const char *model = nd_table[i].model;
4980 char buf[1024];
4981 char *filename;
4982 if (net_boot & (1 << i)) {
4983 if (model == NULL)
4984 model = "ne2k_pci";
4985 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
4986 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
4987 if (filename && get_image_size(filename) > 0) {
4988 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004989 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004990 }
4991 option_rom[nb_option_roms] = qemu_strdup(buf);
4992 nb_option_roms++;
4993 netroms++;
4994 }
4995 if (filename) {
4996 qemu_free(filename);
4997 }
4998 }
4999 }
5000 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005001 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005002 }
5003 }
5004#endif
5005
5006 /* init the bluetooth world */
5007 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005008 if (bt_parse(bt_opts[i])) {
5009 PANIC("Unable to parse bluetooth options");
5010 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005011
5012 /* init the memory */
5013 if (ram_size == 0)
5014 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5015
5016#ifdef CONFIG_KQEMU
5017 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5018 guest ram allocation. It needs to go away. */
5019 if (kqemu_allowed) {
5020 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5021 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5022 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005023 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005024 }
5025 }
5026#endif
5027
5028 /* init the dynamic translator */
5029 cpu_exec_init_all(tb_size * 1024 * 1024);
5030
5031 bdrv_init();
5032
5033 /* we always create the cdrom drive, even if no disk is there */
5034
5035 if (nb_drives_opt < MAX_DRIVES)
5036 drive_add(NULL, CDROM_ALIAS);
5037
5038 /* we always create at least one floppy */
5039
5040 if (nb_drives_opt < MAX_DRIVES)
5041 drive_add(NULL, FD_ALIAS, 0);
5042
5043 /* we always create one sd slot, even if no card is in it */
5044
5045 if (nb_drives_opt < MAX_DRIVES)
5046 drive_add(NULL, SD_ALIAS);
5047
5048 /* open the virtual block devices */
5049
5050 for(i = 0; i < nb_drives_opt; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005051 if (drive_init(&drives_opt[i], snapshot, machine) == -1) {
5052 PANIC("Could not open the virtual block devices");
5053 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005054
David Turner6a9ef172010-09-09 22:54:36 +02005055 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005056 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5057
5058#ifndef _WIN32
5059 /* must be after terminal init, SDL library changes signal handlers */
5060 sighandler_setup();
5061#endif
5062
5063 /* Maintain compatibility with multiple stdio monitors */
5064 if (!strcmp(monitor_device,"stdio")) {
5065 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5066 const char *devname = serial_devices[i];
5067 if (devname && !strcmp(devname,"mon:stdio")) {
5068 monitor_device = NULL;
5069 break;
5070 } else if (devname && !strcmp(devname,"stdio")) {
5071 monitor_device = NULL;
5072 serial_devices[i] = "mon:stdio";
5073 break;
5074 }
5075 }
5076 }
5077
5078 if (nb_numa_nodes > 0) {
5079 int i;
5080
5081 if (nb_numa_nodes > smp_cpus) {
5082 nb_numa_nodes = smp_cpus;
5083 }
5084
5085 /* If no memory size if given for any node, assume the default case
5086 * and distribute the available memory equally across all nodes
5087 */
5088 for (i = 0; i < nb_numa_nodes; i++) {
5089 if (node_mem[i] != 0)
5090 break;
5091 }
5092 if (i == nb_numa_nodes) {
5093 uint64_t usedmem = 0;
5094
5095 /* On Linux, the each node's border has to be 8MB aligned,
5096 * the final node gets the rest.
5097 */
5098 for (i = 0; i < nb_numa_nodes - 1; i++) {
5099 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5100 usedmem += node_mem[i];
5101 }
5102 node_mem[i] = ram_size - usedmem;
5103 }
5104
5105 for (i = 0; i < nb_numa_nodes; i++) {
5106 if (node_cpumask[i] != 0)
5107 break;
5108 }
5109 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5110 * must cope with this anyway, because there are BIOSes out there in
5111 * real machines which also use this scheme.
5112 */
5113 if (i == nb_numa_nodes) {
5114 for (i = 0; i < smp_cpus; i++) {
5115 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5116 }
5117 }
5118 }
5119
5120 if (kvm_enabled()) {
5121 int ret;
5122
5123 ret = kvm_init(smp_cpus);
5124 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005125 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005126 }
5127 }
5128
5129 if (monitor_device) {
5130 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5131 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005132 PANIC("qemu: could not open monitor device '%s'",
5133 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005134 }
5135 }
5136
5137 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5138 const char *devname = serial_devices[i];
5139 if (devname && strcmp(devname, "none")) {
5140 char label[32];
5141 snprintf(label, sizeof(label), "serial%d", i);
5142 serial_hds[i] = qemu_chr_open(label, devname, NULL);
5143 if (!serial_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005144 PANIC("qemu: could not open serial device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005145 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005146 }
5147 }
5148 }
5149
5150 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5151 const char *devname = parallel_devices[i];
5152 if (devname && strcmp(devname, "none")) {
5153 char label[32];
5154 snprintf(label, sizeof(label), "parallel%d", i);
5155 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5156 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005157 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005158 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005159 }
5160 }
5161 }
5162
5163 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5164 const char *devname = virtio_consoles[i];
5165 if (devname && strcmp(devname, "none")) {
5166 char label[32];
5167 snprintf(label, sizeof(label), "virtcon%d", i);
5168 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5169 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005170 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005171 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005172 }
5173 }
5174 }
5175
5176 module_call_init(MODULE_INIT_DEVICE);
5177
5178
5179#ifdef CONFIG_TRACE
5180 if (trace_filename) {
5181 trace_init(trace_filename);
5182#if 0
5183 // We don't need the dcache code until we can get load and store tracing
5184 // working again.
5185 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5186 dcache_replace_policy, dcache_load_miss_penalty,
5187 dcache_store_miss_penalty);
5188#endif
5189 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5190 }
5191#endif
5192
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005193 /* Combine kernel command line passed from the UI with parameters
5194 * collected during initialization. */
5195 if (*kernel_cmdline) {
5196 if (kernel_cmdline_append[0]) {
5197 snprintf(kernel_cmdline_full, sizeof(kernel_cmdline_full), "%s %s",
5198 kernel_cmdline, kernel_cmdline_append);
5199 } else {
5200 strncpy(kernel_cmdline_full, kernel_cmdline, sizeof(kernel_cmdline_full));
5201 kernel_cmdline_full[sizeof(kernel_cmdline_full) - 1] = '\0';
5202 }
5203 } else if (kernel_cmdline_append[0]) {
5204 strncpy(kernel_cmdline_full, kernel_cmdline_append, sizeof(kernel_cmdline_full));
5205 }
5206
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005207 machine->init(ram_size, boot_devices,
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005208 kernel_filename, kernel_cmdline_full, initrd_filename, cpu_model);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005209
5210
5211 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5212 for (i = 0; i < nb_numa_nodes; i++) {
5213 if (node_cpumask[i] & (1 << env->cpu_index)) {
5214 env->numa_node = i;
5215 }
5216 }
5217 }
5218
5219 current_machine = machine;
5220
5221 /* Set KVM's vcpu state to qemu's initial CPUState. */
5222 if (kvm_enabled()) {
5223 int ret;
5224
5225 ret = kvm_sync_vcpus();
5226 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005227 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005228 }
5229 }
5230
5231 /* init USB devices */
5232 if (usb_enabled) {
5233 for(i = 0; i < usb_devices_index; i++) {
5234 if (usb_device_add(usb_devices[i], 0) < 0) {
5235 fprintf(stderr, "Warning: could not add USB device %s\n",
5236 usb_devices[i]);
5237 }
5238 }
5239 }
5240
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005241 if (!display_state) {
5242 if (android_op_gui) {
5243 /* Initialize display from the command line parameters. */
5244 if (parse_androig_gui_option(android_op_gui,
5245 &android_display_width,
5246 &android_display_height,
5247 &android_display_bpp)) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005248 PANIC("Unable to parse -android-gui parameter: %s",
5249 android_op_gui);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005250 }
5251 android_display_init_from(android_display_width,
5252 android_display_height, 0,
5253 android_display_bpp);
5254 } else {
David Turnerf52506f2010-09-10 16:11:22 +02005255 ds = get_displaystate(); /* this forces a dumb display init */
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005256 }
5257 } else if (android_op_gui) {
5258 /* Resize display from the command line parameters. */
5259 if (parse_androig_gui_option(android_op_gui,
5260 &android_display_width,
5261 &android_display_height,
5262 &android_display_bpp)) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005263 PANIC("Unable to parse -android-gui parameter: %s",
5264 android_op_gui);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005265 }
5266 display_state->surface = qemu_resize_displaysurface(display_state,
5267 android_display_width,
5268 android_display_height);
5269 }
5270
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005271 /* just use the first displaystate for the moment */
David Turnerf52506f2010-09-10 16:11:22 +02005272 ds = display_state = get_displaystate();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005273
5274 if (display_type == DT_DEFAULT) {
5275#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5276 display_type = DT_SDL;
5277#else
5278 display_type = DT_VNC;
5279 vnc_display = "localhost:0,to=99";
5280 show_vnc_port = 1;
5281#endif
5282 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005283
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005284
5285 switch (display_type) {
5286 case DT_NOGRAPHIC:
5287 break;
5288#if defined(CONFIG_CURSES)
5289 case DT_CURSES:
5290 curses_display_init(ds, full_screen);
5291 break;
5292#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005293#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005294 case DT_SDL:
5295 sdl_display_init(ds, full_screen, no_frame);
5296 break;
5297#elif defined(CONFIG_COCOA)
5298 case DT_SDL:
5299 cocoa_display_init(ds, full_screen);
5300 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005301#elif defined(CONFIG_STANDALONE_CORE)
5302 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005303 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005304 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005305#endif
5306 case DT_VNC:
5307 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005308 if (vnc_display_open(ds, vnc_display) < 0) {
5309 PANIC("Unable to initialize VNC display");
5310 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005311
5312 if (show_vnc_port) {
5313 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5314 }
5315 break;
5316 default:
5317 break;
5318 }
5319 dpy_resize(ds);
5320
5321 dcl = ds->listeners;
5322 while (dcl != NULL) {
5323 if (dcl->dpy_refresh != NULL) {
5324 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5325 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5326 }
5327 dcl = dcl->next;
5328 }
5329
5330 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5331 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5332 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5333 }
5334
5335 text_consoles_set_display(display_state);
5336 qemu_chr_initial_reset();
5337
5338 if (monitor_device && monitor_hd)
5339 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5340
5341 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5342 const char *devname = serial_devices[i];
5343 if (devname && strcmp(devname, "none")) {
5344 if (strstart(devname, "vc", 0))
5345 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5346 }
5347 }
5348
5349 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5350 const char *devname = parallel_devices[i];
5351 if (devname && strcmp(devname, "none")) {
5352 if (strstart(devname, "vc", 0))
5353 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5354 }
5355 }
5356
5357 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5358 const char *devname = virtio_consoles[i];
5359 if (virtcon_hds[i] && devname) {
5360 if (strstart(devname, "vc", 0))
5361 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5362 }
5363 }
5364
5365 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005366 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005367 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005368 }
5369
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005370 /* call android-specific setup function */
5371 android_emulation_setup();
5372
Ot ten Thije871da2a2010-09-20 10:29:22 +01005373 if (loadvm)
5374 do_loadvm(cur_mon, loadvm);
5375
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005376 if (incoming) {
5377 autostart = 0; /* fixme how to deal with -daemonize */
5378 qemu_start_incoming_migration(incoming);
5379 }
5380
5381 if (autostart)
5382 vm_start();
5383
5384#ifndef _WIN32
5385 if (daemonize) {
5386 uint8_t status = 0;
5387 ssize_t len;
5388
5389 again1:
5390 len = write(fds[1], &status, 1);
5391 if (len == -1 && (errno == EINTR))
5392 goto again1;
5393
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005394 if (len != 1) {
5395 PANIC("Unable to daemonize");
5396 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005397
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005398 if (chdir("/")) {
5399 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005400 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005401 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005402 TFR(fd = open("/dev/null", O_RDWR));
5403 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005404 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005405 }
5406
5407 if (run_as) {
5408 pwd = getpwnam(run_as);
5409 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005410 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005411 }
5412 }
5413
5414 if (chroot_dir) {
5415 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005416 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005417 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005418 if (chdir("/")) {
5419 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005420 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005421 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005422 }
5423
5424 if (run_as) {
5425 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005426 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005427 }
5428 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005429 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005430 }
5431 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005432 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005433 }
5434 }
5435
5436 if (daemonize) {
5437 dup2(fd, 0);
5438 dup2(fd, 1);
5439 dup2(fd, 2);
5440
5441 close(fd);
5442 }
5443#endif
5444
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005445#ifdef CONFIG_ANDROID
5446 // This will notify the UI that the core is successfuly initialized
5447 android_core_init_completed();
5448#endif // CONFIG_ANDROID
5449
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005450 main_loop();
5451 quit_timers();
5452 net_cleanup();
5453 android_emulation_teardown();
5454 return 0;
5455}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005456
5457void
5458android_emulation_teardown(void)
5459{
5460 android_charmap_done();
5461}