blob: 1b7940483f1f4d4ae19e52351af3a92a84a2d63b [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' Turner5d8f37a2009-09-14 14:32:27 -0700194
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -0700195#ifdef CONFIG_STANDALONE_CORE
196/* Verbose value used by the standalone emulator core (without UI) */
197unsigned long android_verbose;
198#endif // CONFIG_STANDALONE_CORE
199
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700200#if defined(CONFIG_SKINS) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700201#undef main
202#define main qemu_main
203#endif
204
205#include "disas.h"
206
207#include "exec-all.h"
208
209#ifdef CONFIG_TRACE
210#include "trace.h"
211#include "dcache.h"
212#endif
213
214#include "qemu_socket.h"
215
216#if defined(CONFIG_SLIRP)
217#include "libslirp.h"
218#endif
219
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700220
221
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700222#define DEFAULT_RAM_SIZE 128
223
224/* Max number of USB devices that can be specified on the commandline. */
225#define MAX_USB_CMDLINE 8
226
227/* Max number of bluetooth switches on the commandline. */
228#define MAX_BT_CMDLINE 10
229
230/* XXX: use a two level table to limit memory usage */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700231
232static const char *data_dir;
233const char *bios_name = NULL;
234static void *ioport_opaque[MAX_IOPORTS];
235static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
236static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
237/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
238 to store the VM snapshots */
239DriveInfo drives_table[MAX_DRIVES+1];
240int nb_drives;
241enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
242static DisplayState *display_state;
243DisplayType display_type = DT_DEFAULT;
244const char* keyboard_layout = NULL;
245int64_t ticks_per_sec;
246ram_addr_t ram_size;
247int nb_nics;
248NICInfo nd_table[MAX_NICS];
249int vm_running;
250static int autostart;
251static int rtc_utc = 1;
252static int rtc_date_offset = -1; /* -1 means no change */
253int cirrus_vga_enabled = 1;
254int std_vga_enabled = 0;
255int vmsvga_enabled = 0;
256int xenfb_enabled = 0;
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -0700257QEMUClock *rtc_clock;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700258#ifdef TARGET_SPARC
259int graphic_width = 1024;
260int graphic_height = 768;
261int graphic_depth = 8;
262#else
263int graphic_width = 800;
264int graphic_height = 600;
265int graphic_depth = 15;
266#endif
267static int full_screen = 0;
268#ifdef CONFIG_SDL
269static int no_frame = 0;
270#endif
271int no_quit = 0;
272CharDriverState *serial_hds[MAX_SERIAL_PORTS];
273CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
274CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
275#ifdef TARGET_I386
276int win2k_install_hack = 0;
277int rtc_td_hack = 0;
278#endif
279int usb_enabled = 0;
280int singlestep = 0;
281int smp_cpus = 1;
282const char *vnc_display;
283int acpi_enabled = 1;
284int no_hpet = 0;
285int no_virtio_balloon = 0;
286int fd_bootchk = 1;
287int no_reboot = 0;
288int no_shutdown = 0;
289int cursor_hide = 1;
290int graphic_rotate = 0;
291#ifndef _WIN32
292int daemonize = 0;
293#endif
294WatchdogTimerModel *watchdog = NULL;
295int watchdog_action = WDT_RESET;
296const char *option_rom[MAX_OPTION_ROMS];
297int nb_option_roms;
298int semihosting_enabled = 0;
299#ifdef TARGET_ARM
300int old_param = 0;
301#endif
302const char *qemu_name;
303int alt_grab = 0;
304#if defined(TARGET_SPARC) || defined(TARGET_PPC)
305unsigned int nb_prom_envs = 0;
306const char *prom_envs[MAX_PROM_ENVS];
307#endif
308int nb_drives_opt;
309struct drive_opt drives_opt[MAX_DRIVES];
310
311int nb_numa_nodes;
312uint64_t node_mem[MAX_NODES];
313uint64_t node_cpumask[MAX_NODES];
314
315static CPUState *cur_cpu;
316static CPUState *next_cpu;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700317static QEMUTimer *nographic_timer;
318
319uint8_t qemu_uuid[16];
320
321
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700322int qemu_cpu_delay;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700323extern char* audio_input_source;
324
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700325extern char* android_op_ports;
326extern char* android_op_port;
327extern char* android_op_report_console;
328extern char* op_http_proxy;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -0700329// Path to the file containing specific key character map.
330char* op_charmap_file = NULL;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -0700331
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700332/* Framebuffer dimensions, passed with -android-gui option. */
333char* android_op_gui = NULL;
334
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -0700335/* Path to hardware initialization file passed with -android-hw option. */
336char* android_op_hwini = NULL;
337
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -0700338/* Memory checker options. */
339char* android_op_memcheck = NULL;
340
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -0700341/* -dns-server option value. */
342char* android_op_dns_server = NULL;
343
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -0700344/* -radio option value. */
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -0700345char* android_op_radio = NULL;
346
347/* -gps option value. */
348char* android_op_gps = NULL;
349
350/* -audio option value. */
351char* android_op_audio = NULL;
352
353/* -audio-in option value. */
354char* android_op_audio_in = NULL;
355
356/* -audio-out option value. */
357char* android_op_audio_out = NULL;
358
359/* -cpu-delay option value. */
360char* android_op_cpu_delay = NULL;
361
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -0700362#ifdef CONFIG_NAND_LIMITS
363/* -nand-limits option value. */
364char* android_op_nand_limits = NULL;
365#endif // CONFIG_NAND_LIMITS
366
367/* -netspeed option value. */
368char* android_op_netspeed = NULL;
369
370/* -netdelay option value. */
371char* android_op_netdelay = NULL;
372
373/* -netfast option value. */
374int android_op_netfast = 0;
375
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -0700376/* -tcpdump option value. */
377char* android_op_tcpdump = NULL;
378
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -0700379/* -lcd-density option value. */
380char* android_op_lcd_density = NULL;
381
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700382/* -ui-port option value. This port will be used to report the core
383 * initialization completion.
384 */
385char* android_op_ui_port = NULL;
386
387/* -ui-settings option value. This value will be passed to the UI when new UI
388 * process is attaching to the core.
389 */
390char* android_op_ui_settings = NULL;
391
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700392extern int android_display_width;
393extern int android_display_height;
394extern int android_display_bpp;
395
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700396extern void dprint( const char* format, ... );
397
Tim Baverstock24204cc2010-11-25 11:37:43 +0000398#if CONFIG_ANDROID_SNAPSHOTS
399const char* savevm_on_exit = NULL;
400#endif
401
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700402#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
403
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -0700404/* Reports the core initialization failure to the error stdout and to the UI
405 * socket before exiting the application.
406 * Parameters that are passed to this macro are used to format the error
407 * mesage using sprintf routine.
408 */
409#ifdef CONFIG_ANDROID
410#define PANIC(...) android_core_init_failure(__VA_ARGS__)
411#else
412#define PANIC(...) do { fprintf(stderr, __VA_ARGS__); \
413 exit(1); \
414 } while (0)
415#endif // CONFIG_ANDROID
416
417/* Exits the core during initialization. */
418#ifdef CONFIG_ANDROID
419#define QEMU_EXIT(exit_code) android_core_init_exit(exit_code)
420#else
421#define QEMU_EXIT(exit_code) exit(exit_code)
422#endif // CONFIG_ANDROID
423
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700424/***********************************************************/
425/* x86 ISA bus support */
426
427target_phys_addr_t isa_mem_base = 0;
428PicState2 *isa_pic;
429
430static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
431static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
432
433static uint32_t ioport_read(int index, uint32_t address)
434{
435 static IOPortReadFunc *default_func[3] = {
436 default_ioport_readb,
437 default_ioport_readw,
438 default_ioport_readl
439 };
440 IOPortReadFunc *func = ioport_read_table[index][address];
441 if (!func)
442 func = default_func[index];
443 return func(ioport_opaque[address], address);
444}
445
446static void ioport_write(int index, uint32_t address, uint32_t data)
447{
448 static IOPortWriteFunc *default_func[3] = {
449 default_ioport_writeb,
450 default_ioport_writew,
451 default_ioport_writel
452 };
453 IOPortWriteFunc *func = ioport_write_table[index][address];
454 if (!func)
455 func = default_func[index];
456 func(ioport_opaque[address], address, data);
457}
458
459static uint32_t default_ioport_readb(void *opaque, uint32_t address)
460{
461#ifdef DEBUG_UNUSED_IOPORT
462 fprintf(stderr, "unused inb: port=0x%04x\n", address);
463#endif
464 return 0xff;
465}
466
467static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
468{
469#ifdef DEBUG_UNUSED_IOPORT
470 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
471#endif
472}
473
474/* default is to make two byte accesses */
475static uint32_t default_ioport_readw(void *opaque, uint32_t address)
476{
477 uint32_t data;
478 data = ioport_read(0, address);
479 address = (address + 1) & (MAX_IOPORTS - 1);
480 data |= ioport_read(0, address) << 8;
481 return data;
482}
483
484static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
485{
486 ioport_write(0, address, data & 0xff);
487 address = (address + 1) & (MAX_IOPORTS - 1);
488 ioport_write(0, address, (data >> 8) & 0xff);
489}
490
491static uint32_t default_ioport_readl(void *opaque, uint32_t address)
492{
493#ifdef DEBUG_UNUSED_IOPORT
494 fprintf(stderr, "unused inl: port=0x%04x\n", address);
495#endif
496 return 0xffffffff;
497}
498
499static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
500{
501#ifdef DEBUG_UNUSED_IOPORT
502 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
503#endif
504}
505
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -0700506/* Parses -android-gui command line option, extracting width, height and bits
507 * per pixel parameters for the GUI console used in this session of the
508 * emulator. -android-gui option contains exactly three comma-separated positive
509 * integer numbers in strict order: width goes first, width goes next, and bits
510 * per pixel goes third. This routine verifies that format and return 0 if all
511 * three numbers were extracted, or -1 if string format was incorrect for that
512 * option. Note that this routine does not verify that extracted values are
513 * correct!
514 */
515static int
516parse_androig_gui_option(const char* op, int* width, int* height, int* bpp)
517{
518 char val[128];
519
520 if (get_param_value(val, 128, "width", op)) {
521 *width = strtol(val, NULL, 0);
522 } else {
523 fprintf(stderr, "option -android-gui is missing width parameter\n");
524 return -1;
525 }
526 if (get_param_value(val, 128, "height", op)) {
527 *height = strtol(val, NULL, 0);
528 } else {
529 fprintf(stderr, "option -android-gui is missing height parameter\n");
530 return -1;
531 }
532 if (get_param_value(val, 128, "bpp", op)) {
533 *bpp = strtol(val, NULL, 0);
534 } else {
535 fprintf(stderr, "option -android-gui is missing bpp parameter\n");
536 return -1;
537 }
538
539 return 0;
540}
541
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700542/***********************************************************/
543void hw_error(const char *fmt, ...)
544{
545 va_list ap;
546 CPUState *env;
547
548 va_start(ap, fmt);
549 fprintf(stderr, "qemu: hardware error: ");
550 vfprintf(stderr, fmt, ap);
551 fprintf(stderr, "\n");
552 for(env = first_cpu; env != NULL; env = env->next_cpu) {
553 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
554#ifdef TARGET_I386
555 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
556#else
557 cpu_dump_state(env, stderr, fprintf, 0);
558#endif
559 }
560 va_end(ap);
561 abort();
562}
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +0200563
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700564/***************/
565/* ballooning */
566
567static QEMUBalloonEvent *qemu_balloon_event;
568void *qemu_balloon_event_opaque;
569
570void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
571{
572 qemu_balloon_event = func;
573 qemu_balloon_event_opaque = opaque;
574}
575
576void qemu_balloon(ram_addr_t target)
577{
578 if (qemu_balloon_event)
579 qemu_balloon_event(qemu_balloon_event_opaque, target);
580}
581
582ram_addr_t qemu_balloon_status(void)
583{
584 if (qemu_balloon_event)
585 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
586 return 0;
587}
588
589/***********************************************************/
David Turner025c32f2010-09-10 14:52:42 +0200590/* real time host monotonic timer */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700591
592/* compute with 96 bit intermediate result: (a*b)/c */
593uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
594{
595 union {
596 uint64_t ll;
597 struct {
David 'Digit' Turner20894ae2010-05-10 17:07:36 -0700598#ifdef HOST_WORDS_BIGENDIAN
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700599 uint32_t high, low;
600#else
601 uint32_t low, high;
602#endif
603 } l;
604 } u, res;
605 uint64_t rl, rh;
606
607 u.ll = a;
608 rl = (uint64_t)u.l.low * (uint64_t)b;
609 rh = (uint64_t)u.l.high * (uint64_t)b;
610 rh += (rl >> 32);
611 res.l.high = rh / c;
612 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
613 return res.ll;
614}
615
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700616/***********************************************************/
617/* host time/date access */
618void qemu_get_timedate(struct tm *tm, int offset)
619{
620 time_t ti;
621 struct tm *ret;
622
623 time(&ti);
624 ti += offset;
625 if (rtc_date_offset == -1) {
626 if (rtc_utc)
627 ret = gmtime(&ti);
628 else
629 ret = localtime(&ti);
630 } else {
631 ti -= rtc_date_offset;
632 ret = gmtime(&ti);
633 }
634
635 memcpy(tm, ret, sizeof(struct tm));
636}
637
638int qemu_timedate_diff(struct tm *tm)
639{
640 time_t seconds;
641
642 if (rtc_date_offset == -1)
643 if (rtc_utc)
644 seconds = mktimegm(tm);
645 else
646 seconds = mktime(tm);
647 else
648 seconds = mktimegm(tm) + rtc_date_offset;
649
650 return seconds - time(NULL);
651}
652
653
654#ifdef CONFIG_TRACE
655static int tbflush_requested;
656static int exit_requested;
657
658void start_tracing()
659{
660 if (trace_filename == NULL)
661 return;
662 if (!tracing) {
663 fprintf(stderr,"-- start tracing --\n");
664 start_time = Now();
665 }
666 tracing = 1;
667 tbflush_requested = 1;
668 qemu_notify_event();
669}
670
671void stop_tracing()
672{
673 if (trace_filename == NULL)
674 return;
675 if (tracing) {
676 end_time = Now();
677 elapsed_usecs += end_time - start_time;
678 fprintf(stderr,"-- stop tracing --\n");
679 }
680 tracing = 0;
681 tbflush_requested = 1;
682 qemu_notify_event();
683}
684
685#ifndef _WIN32
686/* This is the handler for the SIGUSR1 and SIGUSR2 signals.
687 * SIGUSR1 turns tracing on. SIGUSR2 turns tracing off.
688 */
689void sigusr_handler(int sig)
690{
691 if (sig == SIGUSR1)
692 start_tracing();
693 else
694 stop_tracing();
695}
696#endif
697
698/* This is the handler to catch control-C so that we can exit cleanly.
699 * This is needed when tracing to flush the buffers to disk.
700 */
701void sigint_handler(int sig)
702{
703 exit_requested = 1;
704 qemu_notify_event();
705}
706#endif /* CONFIG_TRACE */
707
708
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700709/***********************************************************/
710/* Bluetooth support */
711static int nb_hcis;
712static int cur_hci;
713static struct HCIInfo *hci_table[MAX_NICS];
714
715static struct bt_vlan_s {
716 struct bt_scatternet_s net;
717 int id;
718 struct bt_vlan_s *next;
719} *first_bt_vlan;
720
721/* find or alloc a new bluetooth "VLAN" */
722static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
723{
724 struct bt_vlan_s **pvlan, *vlan;
725 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
726 if (vlan->id == id)
727 return &vlan->net;
728 }
729 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
730 vlan->id = id;
731 pvlan = &first_bt_vlan;
732 while (*pvlan != NULL)
733 pvlan = &(*pvlan)->next;
734 *pvlan = vlan;
735 return &vlan->net;
736}
737
738static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
739{
740}
741
742static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
743{
744 return -ENOTSUP;
745}
746
747static struct HCIInfo null_hci = {
748 .cmd_send = null_hci_send,
749 .sco_send = null_hci_send,
750 .acl_send = null_hci_send,
751 .bdaddr_set = null_hci_addr_set,
752};
753
754struct HCIInfo *qemu_next_hci(void)
755{
756 if (cur_hci == nb_hcis)
757 return &null_hci;
758
759 return hci_table[cur_hci++];
760}
761
762static struct HCIInfo *hci_init(const char *str)
763{
764 char *endp;
765 struct bt_scatternet_s *vlan = 0;
766
767 if (!strcmp(str, "null"))
768 /* null */
769 return &null_hci;
770 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
771 /* host[:hciN] */
772 return bt_host_hci(str[4] ? str + 5 : "hci0");
773 else if (!strncmp(str, "hci", 3)) {
774 /* hci[,vlan=n] */
775 if (str[3]) {
776 if (!strncmp(str + 3, ",vlan=", 6)) {
777 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
778 if (*endp)
779 vlan = 0;
780 }
781 } else
782 vlan = qemu_find_bt_vlan(0);
783 if (vlan)
784 return bt_new_hci(vlan);
785 }
786
787 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
788
789 return 0;
790}
791
792static int bt_hci_parse(const char *str)
793{
794 struct HCIInfo *hci;
795 bdaddr_t bdaddr;
796
797 if (nb_hcis >= MAX_NICS) {
798 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
799 return -1;
800 }
801
802 hci = hci_init(str);
803 if (!hci)
804 return -1;
805
806 bdaddr.b[0] = 0x52;
807 bdaddr.b[1] = 0x54;
808 bdaddr.b[2] = 0x00;
809 bdaddr.b[3] = 0x12;
810 bdaddr.b[4] = 0x34;
811 bdaddr.b[5] = 0x56 + nb_hcis;
812 hci->bdaddr_set(hci, bdaddr.b);
813
814 hci_table[nb_hcis++] = hci;
815
816 return 0;
817}
818
819static void bt_vhci_add(int vlan_id)
820{
821 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
822
823 if (!vlan->slave)
824 fprintf(stderr, "qemu: warning: adding a VHCI to "
825 "an empty scatternet %i\n", vlan_id);
826
827 bt_vhci_init(bt_new_hci(vlan));
828}
829
830static struct bt_device_s *bt_device_add(const char *opt)
831{
832 struct bt_scatternet_s *vlan;
833 int vlan_id = 0;
834 char *endp = strstr(opt, ",vlan=");
835 int len = (endp ? endp - opt : strlen(opt)) + 1;
836 char devname[10];
837
838 pstrcpy(devname, MIN(sizeof(devname), len), opt);
839
840 if (endp) {
841 vlan_id = strtol(endp + 6, &endp, 0);
842 if (*endp) {
843 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
844 return 0;
845 }
846 }
847
848 vlan = qemu_find_bt_vlan(vlan_id);
849
850 if (!vlan->slave)
851 fprintf(stderr, "qemu: warning: adding a slave device to "
852 "an empty scatternet %i\n", vlan_id);
853
854 if (!strcmp(devname, "keyboard"))
855 return bt_keyboard_init(vlan);
856
857 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
858 return 0;
859}
860
861static int bt_parse(const char *opt)
862{
863 const char *endp, *p;
864 int vlan;
865
866 if (strstart(opt, "hci", &endp)) {
867 if (!*endp || *endp == ',') {
868 if (*endp)
869 if (!strstart(endp, ",vlan=", 0))
870 opt = endp + 1;
871
872 return bt_hci_parse(opt);
873 }
874 } else if (strstart(opt, "vhci", &endp)) {
875 if (!*endp || *endp == ',') {
876 if (*endp) {
877 if (strstart(endp, ",vlan=", &p)) {
878 vlan = strtol(p, (char **) &endp, 0);
879 if (*endp) {
880 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
881 return 1;
882 }
883 } else {
884 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
885 return 1;
886 }
887 } else
888 vlan = 0;
889
890 bt_vhci_add(vlan);
891 return 0;
892 }
893 } else if (strstart(opt, "device:", &endp))
894 return !bt_device_add(endp);
895
896 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
897 return 1;
898}
899
900/***********************************************************/
901/* QEMU Block devices */
902
903#define HD_ALIAS "index=%d,media=disk"
904#define CDROM_ALIAS "index=2,media=cdrom"
905#define FD_ALIAS "index=%d,if=floppy"
906#define PFLASH_ALIAS "if=pflash"
907#define MTD_ALIAS "if=mtd"
908#define SD_ALIAS "index=0,if=sd"
909
910static int drive_opt_get_free_idx(void)
911{
912 int index;
913
914 for (index = 0; index < MAX_DRIVES; index++)
915 if (!drives_opt[index].used) {
916 drives_opt[index].used = 1;
917 return index;
918 }
919
920 return -1;
921}
922
923static int drive_get_free_idx(void)
924{
925 int index;
926
927 for (index = 0; index < MAX_DRIVES; index++)
928 if (!drives_table[index].used) {
929 drives_table[index].used = 1;
930 return index;
931 }
932
933 return -1;
934}
935
936int drive_add(const char *file, const char *fmt, ...)
937{
938 va_list ap;
939 int index = drive_opt_get_free_idx();
940
941 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
942 fprintf(stderr, "qemu: too many drives\n");
943 return -1;
944 }
945
946 drives_opt[index].file = file;
947 va_start(ap, fmt);
948 vsnprintf(drives_opt[index].opt,
949 sizeof(drives_opt[0].opt), fmt, ap);
950 va_end(ap);
David 'Digit' Turner92568952010-04-15 15:04:16 -0700951
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700952 nb_drives_opt++;
953 return index;
954}
955
956void drive_remove(int index)
957{
958 drives_opt[index].used = 0;
959 nb_drives_opt--;
960}
961
962int drive_get_index(BlockInterfaceType type, int bus, int unit)
963{
964 int index;
965
966 /* seek interface, bus and unit */
967
968 for (index = 0; index < MAX_DRIVES; index++)
969 if (drives_table[index].type == type &&
970 drives_table[index].bus == bus &&
971 drives_table[index].unit == unit &&
972 drives_table[index].used)
973 return index;
974
975 return -1;
976}
977
978int drive_get_max_bus(BlockInterfaceType type)
979{
980 int max_bus;
981 int index;
982
983 max_bus = -1;
984 for (index = 0; index < nb_drives; index++) {
985 if(drives_table[index].type == type &&
986 drives_table[index].bus > max_bus)
987 max_bus = drives_table[index].bus;
988 }
989 return max_bus;
990}
991
992const char *drive_get_serial(BlockDriverState *bdrv)
993{
994 int index;
995
996 for (index = 0; index < nb_drives; index++)
997 if (drives_table[index].bdrv == bdrv)
998 return drives_table[index].serial;
999
1000 return "\0";
1001}
1002
1003BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1004{
1005 int index;
1006
1007 for (index = 0; index < nb_drives; index++)
1008 if (drives_table[index].bdrv == bdrv)
1009 return drives_table[index].onerror;
1010
1011 return BLOCK_ERR_STOP_ENOSPC;
1012}
1013
1014static void bdrv_format_print(void *opaque, const char *name)
1015{
1016 fprintf(stderr, " %s", name);
1017}
1018
1019void drive_uninit(BlockDriverState *bdrv)
1020{
1021 int i;
1022
1023 for (i = 0; i < MAX_DRIVES; i++)
1024 if (drives_table[i].bdrv == bdrv) {
1025 drives_table[i].bdrv = NULL;
1026 drives_table[i].used = 0;
1027 drive_remove(drives_table[i].drive_opt_idx);
1028 nb_drives--;
1029 break;
1030 }
1031}
1032
1033int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
1034{
1035 char buf[128];
1036 char file[1024];
1037 char devname[128];
1038 char serial[21];
1039 const char *mediastr = "";
1040 BlockInterfaceType type;
1041 enum { MEDIA_DISK, MEDIA_CDROM } media;
1042 int bus_id, unit_id;
1043 int cyls, heads, secs, translation;
1044 BlockDriverState *bdrv;
1045 BlockDriver *drv = NULL;
1046 QEMUMachine *machine = opaque;
1047 int max_devs;
1048 int index;
1049 int cache;
1050 int bdrv_flags, onerror;
1051 int drives_table_idx;
1052 char *str = arg->opt;
1053 static const char * const params[] = { "bus", "unit", "if", "index",
1054 "cyls", "heads", "secs", "trans",
1055 "media", "snapshot", "file",
1056 "cache", "format", "serial", "werror",
1057 NULL };
1058
1059 if (check_params(buf, sizeof(buf), params, str) < 0) {
1060 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1061 buf, str);
1062 return -1;
1063 }
1064
1065 file[0] = 0;
1066 cyls = heads = secs = 0;
1067 bus_id = 0;
1068 unit_id = -1;
1069 translation = BIOS_ATA_TRANSLATION_AUTO;
1070 index = -1;
1071 cache = 3;
1072
1073 if (machine->use_scsi) {
1074 type = IF_SCSI;
1075 max_devs = MAX_SCSI_DEVS;
1076 pstrcpy(devname, sizeof(devname), "scsi");
1077 } else {
1078 type = IF_IDE;
1079 max_devs = MAX_IDE_DEVS;
1080 pstrcpy(devname, sizeof(devname), "ide");
1081 }
1082 media = MEDIA_DISK;
1083
1084 /* extract parameters */
1085
1086 if (get_param_value(buf, sizeof(buf), "bus", str)) {
1087 bus_id = strtol(buf, NULL, 0);
1088 if (bus_id < 0) {
1089 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
1090 return -1;
1091 }
1092 }
1093
1094 if (get_param_value(buf, sizeof(buf), "unit", str)) {
1095 unit_id = strtol(buf, NULL, 0);
1096 if (unit_id < 0) {
1097 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
1098 return -1;
1099 }
1100 }
1101
1102 if (get_param_value(buf, sizeof(buf), "if", str)) {
1103 pstrcpy(devname, sizeof(devname), buf);
1104 if (!strcmp(buf, "ide")) {
1105 type = IF_IDE;
1106 max_devs = MAX_IDE_DEVS;
1107 } else if (!strcmp(buf, "scsi")) {
1108 type = IF_SCSI;
1109 max_devs = MAX_SCSI_DEVS;
1110 } else if (!strcmp(buf, "floppy")) {
1111 type = IF_FLOPPY;
1112 max_devs = 0;
1113 } else if (!strcmp(buf, "pflash")) {
1114 type = IF_PFLASH;
1115 max_devs = 0;
1116 } else if (!strcmp(buf, "mtd")) {
1117 type = IF_MTD;
1118 max_devs = 0;
1119 } else if (!strcmp(buf, "sd")) {
1120 type = IF_SD;
1121 max_devs = 0;
1122 } else if (!strcmp(buf, "virtio")) {
1123 type = IF_VIRTIO;
1124 max_devs = 0;
1125 } else if (!strcmp(buf, "xen")) {
1126 type = IF_XEN;
1127 max_devs = 0;
1128 } else {
1129 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
1130 return -1;
1131 }
1132 }
1133
1134 if (get_param_value(buf, sizeof(buf), "index", str)) {
1135 index = strtol(buf, NULL, 0);
1136 if (index < 0) {
1137 fprintf(stderr, "qemu: '%s' invalid index\n", str);
1138 return -1;
1139 }
1140 }
1141
1142 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
1143 cyls = strtol(buf, NULL, 0);
1144 }
1145
1146 if (get_param_value(buf, sizeof(buf), "heads", str)) {
1147 heads = strtol(buf, NULL, 0);
1148 }
1149
1150 if (get_param_value(buf, sizeof(buf), "secs", str)) {
1151 secs = strtol(buf, NULL, 0);
1152 }
1153
1154 if (cyls || heads || secs) {
1155 if (cyls < 1 || cyls > 16383) {
1156 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
1157 return -1;
1158 }
1159 if (heads < 1 || heads > 16) {
1160 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
1161 return -1;
1162 }
1163 if (secs < 1 || secs > 63) {
1164 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
1165 return -1;
1166 }
1167 }
1168
1169 if (get_param_value(buf, sizeof(buf), "trans", str)) {
1170 if (!cyls) {
1171 fprintf(stderr,
1172 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1173 str);
1174 return -1;
1175 }
1176 if (!strcmp(buf, "none"))
1177 translation = BIOS_ATA_TRANSLATION_NONE;
1178 else if (!strcmp(buf, "lba"))
1179 translation = BIOS_ATA_TRANSLATION_LBA;
1180 else if (!strcmp(buf, "auto"))
1181 translation = BIOS_ATA_TRANSLATION_AUTO;
1182 else {
1183 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
1184 return -1;
1185 }
1186 }
1187
1188 if (get_param_value(buf, sizeof(buf), "media", str)) {
1189 if (!strcmp(buf, "disk")) {
1190 media = MEDIA_DISK;
1191 } else if (!strcmp(buf, "cdrom")) {
1192 if (cyls || secs || heads) {
1193 fprintf(stderr,
1194 "qemu: '%s' invalid physical CHS format\n", str);
1195 return -1;
1196 }
1197 media = MEDIA_CDROM;
1198 } else {
1199 fprintf(stderr, "qemu: '%s' invalid media\n", str);
1200 return -1;
1201 }
1202 }
1203
1204 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
1205 if (!strcmp(buf, "on"))
1206 snapshot = 1;
1207 else if (!strcmp(buf, "off"))
1208 snapshot = 0;
1209 else {
1210 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
1211 return -1;
1212 }
1213 }
1214
1215 if (get_param_value(buf, sizeof(buf), "cache", str)) {
1216 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
1217 cache = 0;
1218 else if (!strcmp(buf, "writethrough"))
1219 cache = 1;
1220 else if (!strcmp(buf, "writeback"))
1221 cache = 2;
1222 else {
1223 fprintf(stderr, "qemu: invalid cache option\n");
1224 return -1;
1225 }
1226 }
1227
1228 if (get_param_value(buf, sizeof(buf), "format", str)) {
1229 if (strcmp(buf, "?") == 0) {
1230 fprintf(stderr, "qemu: Supported formats:");
1231 bdrv_iterate_format(bdrv_format_print, NULL);
1232 fprintf(stderr, "\n");
1233 return -1;
1234 }
1235 drv = bdrv_find_format(buf);
1236 if (!drv) {
1237 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
1238 return -1;
1239 }
1240 }
1241
1242 if (arg->file == NULL)
1243 get_param_value(file, sizeof(file), "file", str);
1244 else
1245 pstrcpy(file, sizeof(file), arg->file);
1246
1247 if (!get_param_value(serial, sizeof(serial), "serial", str))
1248 memset(serial, 0, sizeof(serial));
1249
1250 onerror = BLOCK_ERR_STOP_ENOSPC;
1251 if (get_param_value(buf, sizeof(serial), "werror", str)) {
1252 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
1253 fprintf(stderr, "werror is no supported by this format\n");
1254 return -1;
1255 }
1256 if (!strcmp(buf, "ignore"))
1257 onerror = BLOCK_ERR_IGNORE;
1258 else if (!strcmp(buf, "enospc"))
1259 onerror = BLOCK_ERR_STOP_ENOSPC;
1260 else if (!strcmp(buf, "stop"))
1261 onerror = BLOCK_ERR_STOP_ANY;
1262 else if (!strcmp(buf, "report"))
1263 onerror = BLOCK_ERR_REPORT;
1264 else {
1265 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
1266 return -1;
1267 }
1268 }
1269
1270 /* compute bus and unit according index */
1271
1272 if (index != -1) {
1273 if (bus_id != 0 || unit_id != -1) {
1274 fprintf(stderr,
1275 "qemu: '%s' index cannot be used with bus and unit\n", str);
1276 return -1;
1277 }
1278 if (max_devs == 0)
1279 {
1280 unit_id = index;
1281 bus_id = 0;
1282 } else {
1283 unit_id = index % max_devs;
1284 bus_id = index / max_devs;
1285 }
1286 }
1287
1288 /* if user doesn't specify a unit_id,
1289 * try to find the first free
1290 */
1291
1292 if (unit_id == -1) {
1293 unit_id = 0;
1294 while (drive_get_index(type, bus_id, unit_id) != -1) {
1295 unit_id++;
1296 if (max_devs && unit_id >= max_devs) {
1297 unit_id -= max_devs;
1298 bus_id++;
1299 }
1300 }
1301 }
1302
1303 /* check unit id */
1304
1305 if (max_devs && unit_id >= max_devs) {
1306 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
1307 str, unit_id, max_devs - 1);
1308 return -1;
1309 }
1310
1311 /*
1312 * ignore multiple definitions
1313 */
1314
1315 if (drive_get_index(type, bus_id, unit_id) != -1)
1316 return -2;
1317
1318 /* init */
1319
1320 if (type == IF_IDE || type == IF_SCSI)
1321 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1322 if (max_devs)
1323 snprintf(buf, sizeof(buf), "%s%i%s%i",
1324 devname, bus_id, mediastr, unit_id);
1325 else
1326 snprintf(buf, sizeof(buf), "%s%s%i",
1327 devname, mediastr, unit_id);
1328 bdrv = bdrv_new(buf);
1329 drives_table_idx = drive_get_free_idx();
1330 drives_table[drives_table_idx].bdrv = bdrv;
1331 drives_table[drives_table_idx].type = type;
1332 drives_table[drives_table_idx].bus = bus_id;
1333 drives_table[drives_table_idx].unit = unit_id;
1334 drives_table[drives_table_idx].onerror = onerror;
1335 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
1336 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
1337 nb_drives++;
1338
1339 switch(type) {
1340 case IF_IDE:
1341 case IF_SCSI:
1342 case IF_XEN:
1343 switch(media) {
1344 case MEDIA_DISK:
1345 if (cyls != 0) {
1346 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
1347 bdrv_set_translation_hint(bdrv, translation);
1348 }
1349 break;
1350 case MEDIA_CDROM:
1351 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
1352 break;
1353 }
1354 break;
1355 case IF_SD:
1356 /* FIXME: This isn't really a floppy, but it's a reasonable
1357 approximation. */
1358 case IF_FLOPPY:
1359 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
1360 break;
1361 case IF_PFLASH:
1362 case IF_MTD:
1363 case IF_VIRTIO:
1364 break;
1365 case IF_COUNT:
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07001366 case IF_NONE:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001367 abort();
1368 }
1369 if (!file[0])
1370 return -2;
1371 bdrv_flags = 0;
1372 if (snapshot) {
1373 bdrv_flags |= BDRV_O_SNAPSHOT;
1374 cache = 2; /* always use write-back with snapshot */
1375 }
1376 if (cache == 0) /* no caching */
1377 bdrv_flags |= BDRV_O_NOCACHE;
1378 else if (cache == 2) /* write-back */
1379 bdrv_flags |= BDRV_O_CACHE_WB;
1380 else if (cache == 3) /* not specified */
1381 bdrv_flags |= BDRV_O_CACHE_DEF;
1382 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
1383 fprintf(stderr, "qemu: could not open disk image %s\n",
1384 file);
1385 return -1;
1386 }
1387 if (bdrv_key_required(bdrv))
1388 autostart = 0;
1389 return drives_table_idx;
1390}
1391
1392static void numa_add(const char *optarg)
1393{
1394 char option[128];
1395 char *endptr;
1396 unsigned long long value, endvalue;
1397 int nodenr;
1398
1399 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1400 if (!strcmp(option, "node")) {
1401 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1402 nodenr = nb_numa_nodes;
1403 } else {
1404 nodenr = strtoull(option, NULL, 10);
1405 }
1406
1407 if (get_param_value(option, 128, "mem", optarg) == 0) {
1408 node_mem[nodenr] = 0;
1409 } else {
1410 value = strtoull(option, &endptr, 0);
1411 switch (*endptr) {
1412 case 0: case 'M': case 'm':
1413 value <<= 20;
1414 break;
1415 case 'G': case 'g':
1416 value <<= 30;
1417 break;
1418 }
1419 node_mem[nodenr] = value;
1420 }
1421 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1422 node_cpumask[nodenr] = 0;
1423 } else {
1424 value = strtoull(option, &endptr, 10);
1425 if (value >= 64) {
1426 value = 63;
1427 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1428 } else {
1429 if (*endptr == '-') {
1430 endvalue = strtoull(endptr+1, &endptr, 10);
1431 if (endvalue >= 63) {
1432 endvalue = 62;
1433 fprintf(stderr,
1434 "only 63 CPUs in NUMA mode supported.\n");
1435 }
1436 value = (1 << (endvalue + 1)) - (1 << value);
1437 } else {
1438 value = 1 << value;
1439 }
1440 }
1441 node_cpumask[nodenr] = value;
1442 }
1443 nb_numa_nodes++;
1444 }
1445 return;
1446}
1447
1448/***********************************************************/
1449/* USB devices */
1450
1451static USBPort *used_usb_ports;
1452static USBPort *free_usb_ports;
1453
1454/* ??? Maybe change this to register a hub to keep track of the topology. */
1455void qemu_register_usb_port(USBPort *port, void *opaque, int index,
1456 usb_attachfn attach)
1457{
1458 port->opaque = opaque;
1459 port->index = index;
1460 port->attach = attach;
1461 port->next = free_usb_ports;
1462 free_usb_ports = port;
1463}
1464
1465int usb_device_add_dev(USBDevice *dev)
1466{
1467 USBPort *port;
1468
1469 /* Find a USB port to add the device to. */
1470 port = free_usb_ports;
1471 if (!port->next) {
1472 USBDevice *hub;
1473
1474 /* Create a new hub and chain it on. */
1475 free_usb_ports = NULL;
1476 port->next = used_usb_ports;
1477 used_usb_ports = port;
1478
1479 hub = usb_hub_init(VM_USB_HUB_SIZE);
1480 usb_attach(port, hub);
1481 port = free_usb_ports;
1482 }
1483
1484 free_usb_ports = port->next;
1485 port->next = used_usb_ports;
1486 used_usb_ports = port;
1487 usb_attach(port, dev);
1488 return 0;
1489}
1490
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001491#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001492static void usb_msd_password_cb(void *opaque, int err)
1493{
1494 USBDevice *dev = opaque;
1495
1496 if (!err)
1497 usb_device_add_dev(dev);
1498 else
1499 dev->handle_destroy(dev);
1500}
David 'Digit' Turner3266b512010-05-10 18:44:56 -07001501#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001502
1503static int usb_device_add(const char *devname, int is_hotplug)
1504{
1505 const char *p;
1506 USBDevice *dev;
1507
1508 if (!free_usb_ports)
1509 return -1;
1510
1511 if (strstart(devname, "host:", &p)) {
1512 dev = usb_host_device_open(p);
1513 } else if (!strcmp(devname, "mouse")) {
1514 dev = usb_mouse_init();
1515 } else if (!strcmp(devname, "tablet")) {
1516 dev = usb_tablet_init();
1517 } else if (!strcmp(devname, "keyboard")) {
1518 dev = usb_keyboard_init();
1519 } else if (strstart(devname, "disk:", &p)) {
1520#if 0
1521 BlockDriverState *bs;
1522#endif
1523 dev = usb_msd_init(p);
1524 if (!dev)
1525 return -1;
1526#if 0
1527 bs = usb_msd_get_bdrv(dev);
1528 if (bdrv_key_required(bs)) {
1529 autostart = 0;
1530 if (is_hotplug) {
1531 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
1532 dev);
1533 return 0;
1534 }
1535 }
1536 } else if (!strcmp(devname, "wacom-tablet")) {
1537 dev = usb_wacom_init();
1538 } else if (strstart(devname, "serial:", &p)) {
1539 dev = usb_serial_init(p);
1540#ifdef CONFIG_BRLAPI
1541 } else if (!strcmp(devname, "braille")) {
1542 dev = usb_baum_init();
1543#endif
1544 } else if (strstart(devname, "net:", &p)) {
1545 int nic = nb_nics;
1546
1547 if (net_client_init("nic", p) < 0)
1548 return -1;
1549 nd_table[nic].model = "usb";
1550 dev = usb_net_init(&nd_table[nic]);
1551 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1552 dev = usb_bt_init(devname[2] ? hci_init(p) :
1553 bt_new_hci(qemu_find_bt_vlan(0)));
1554#endif
1555 } else {
1556 return -1;
1557 }
1558 if (!dev)
1559 return -1;
1560
1561 return usb_device_add_dev(dev);
1562}
1563
1564int usb_device_del_addr(int bus_num, int addr)
1565{
1566 USBPort *port;
1567 USBPort **lastp;
1568 USBDevice *dev;
1569
1570 if (!used_usb_ports)
1571 return -1;
1572
1573 if (bus_num != 0)
1574 return -1;
1575
1576 lastp = &used_usb_ports;
1577 port = used_usb_ports;
1578 while (port && port->dev->addr != addr) {
1579 lastp = &port->next;
1580 port = port->next;
1581 }
1582
1583 if (!port)
1584 return -1;
1585
1586 dev = port->dev;
1587 *lastp = port->next;
1588 usb_attach(port, NULL);
1589 dev->handle_destroy(dev);
1590 port->next = free_usb_ports;
1591 free_usb_ports = port;
1592 return 0;
1593}
1594
1595static int usb_device_del(const char *devname)
1596{
1597 int bus_num, addr;
1598 const char *p;
1599
1600 if (strstart(devname, "host:", &p))
1601 return usb_host_device_close(p);
1602
1603 if (!used_usb_ports)
1604 return -1;
1605
1606 p = strchr(devname, '.');
1607 if (!p)
1608 return -1;
1609 bus_num = strtoul(devname, NULL, 0);
1610 addr = strtoul(p + 1, NULL, 0);
1611
1612 return usb_device_del_addr(bus_num, addr);
1613}
1614
1615void do_usb_add(Monitor *mon, const char *devname)
1616{
1617 usb_device_add(devname, 1);
1618}
1619
1620void do_usb_del(Monitor *mon, const char *devname)
1621{
1622 usb_device_del(devname);
1623}
1624
1625void usb_info(Monitor *mon)
1626{
1627 USBDevice *dev;
1628 USBPort *port;
1629 const char *speed_str;
1630
1631 if (!usb_enabled) {
1632 monitor_printf(mon, "USB support not enabled\n");
1633 return;
1634 }
1635
1636 for (port = used_usb_ports; port; port = port->next) {
1637 dev = port->dev;
1638 if (!dev)
1639 continue;
1640 switch(dev->speed) {
1641 case USB_SPEED_LOW:
1642 speed_str = "1.5";
1643 break;
1644 case USB_SPEED_FULL:
1645 speed_str = "12";
1646 break;
1647 case USB_SPEED_HIGH:
1648 speed_str = "480";
1649 break;
1650 default:
1651 speed_str = "?";
1652 break;
1653 }
1654 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
1655 0, dev->addr, speed_str, dev->devname);
1656 }
1657}
1658
1659/***********************************************************/
1660/* PCMCIA/Cardbus */
1661
1662static struct pcmcia_socket_entry_s {
1663 PCMCIASocket *socket;
1664 struct pcmcia_socket_entry_s *next;
1665} *pcmcia_sockets = 0;
1666
1667void pcmcia_socket_register(PCMCIASocket *socket)
1668{
1669 struct pcmcia_socket_entry_s *entry;
1670
1671 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1672 entry->socket = socket;
1673 entry->next = pcmcia_sockets;
1674 pcmcia_sockets = entry;
1675}
1676
1677void pcmcia_socket_unregister(PCMCIASocket *socket)
1678{
1679 struct pcmcia_socket_entry_s *entry, **ptr;
1680
1681 ptr = &pcmcia_sockets;
1682 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1683 if (entry->socket == socket) {
1684 *ptr = entry->next;
1685 qemu_free(entry);
1686 }
1687}
1688
1689void pcmcia_info(Monitor *mon)
1690{
1691 struct pcmcia_socket_entry_s *iter;
1692
1693 if (!pcmcia_sockets)
1694 monitor_printf(mon, "No PCMCIA sockets\n");
1695
1696 for (iter = pcmcia_sockets; iter; iter = iter->next)
1697 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1698 iter->socket->attached ? iter->socket->card_string :
1699 "Empty");
1700}
1701
1702/***********************************************************/
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001703/* I/O handling */
1704
1705typedef struct IOHandlerRecord {
1706 int fd;
David Turner4143d8f2010-09-10 11:05:02 +02001707 IOCanReadHandler *fd_read_poll;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001708 IOHandler *fd_read;
1709 IOHandler *fd_write;
1710 int deleted;
1711 void *opaque;
1712 /* temporary data */
1713 struct pollfd *ufd;
1714 struct IOHandlerRecord *next;
1715} IOHandlerRecord;
1716
1717static IOHandlerRecord *first_io_handler;
1718
1719/* XXX: fd_read_poll should be suppressed, but an API change is
1720 necessary in the character devices to suppress fd_can_read(). */
1721int qemu_set_fd_handler2(int fd,
David Turner4143d8f2010-09-10 11:05:02 +02001722 IOCanReadHandler *fd_read_poll,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001723 IOHandler *fd_read,
1724 IOHandler *fd_write,
1725 void *opaque)
1726{
1727 IOHandlerRecord **pioh, *ioh;
1728
1729 if (!fd_read && !fd_write) {
1730 pioh = &first_io_handler;
1731 for(;;) {
1732 ioh = *pioh;
1733 if (ioh == NULL)
1734 break;
1735 if (ioh->fd == fd) {
1736 ioh->deleted = 1;
1737 break;
1738 }
1739 pioh = &ioh->next;
1740 }
1741 } else {
1742 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1743 if (ioh->fd == fd)
1744 goto found;
1745 }
1746 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1747 ioh->next = first_io_handler;
1748 first_io_handler = ioh;
1749 found:
1750 ioh->fd = fd;
1751 ioh->fd_read_poll = fd_read_poll;
1752 ioh->fd_read = fd_read;
1753 ioh->fd_write = fd_write;
1754 ioh->opaque = opaque;
1755 ioh->deleted = 0;
1756 }
1757 return 0;
1758}
1759
1760int qemu_set_fd_handler(int fd,
1761 IOHandler *fd_read,
1762 IOHandler *fd_write,
1763 void *opaque)
1764{
1765 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1766}
1767
1768#ifdef _WIN32
1769/***********************************************************/
1770/* Polling handling */
1771
1772typedef struct PollingEntry {
1773 PollingFunc *func;
1774 void *opaque;
1775 struct PollingEntry *next;
1776} PollingEntry;
1777
1778static PollingEntry *first_polling_entry;
1779
1780int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1781{
1782 PollingEntry **ppe, *pe;
1783 pe = qemu_mallocz(sizeof(PollingEntry));
1784 pe->func = func;
1785 pe->opaque = opaque;
1786 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1787 *ppe = pe;
1788 return 0;
1789}
1790
1791void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1792{
1793 PollingEntry **ppe, *pe;
1794 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1795 pe = *ppe;
1796 if (pe->func == func && pe->opaque == opaque) {
1797 *ppe = pe->next;
1798 qemu_free(pe);
1799 break;
1800 }
1801 }
1802}
1803
1804/***********************************************************/
1805/* Wait objects support */
1806typedef struct WaitObjects {
1807 int num;
1808 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1809 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1810 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1811} WaitObjects;
1812
1813static WaitObjects wait_objects = {0};
1814
1815int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1816{
1817 WaitObjects *w = &wait_objects;
1818
1819 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1820 return -1;
1821 w->events[w->num] = handle;
1822 w->func[w->num] = func;
1823 w->opaque[w->num] = opaque;
1824 w->num++;
1825 return 0;
1826}
1827
1828void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1829{
1830 int i, found;
1831 WaitObjects *w = &wait_objects;
1832
1833 found = 0;
1834 for (i = 0; i < w->num; i++) {
1835 if (w->events[i] == handle)
1836 found = 1;
1837 if (found) {
1838 w->events[i] = w->events[i + 1];
1839 w->func[i] = w->func[i + 1];
1840 w->opaque[i] = w->opaque[i + 1];
1841 }
1842 }
1843 if (found)
1844 w->num--;
1845}
1846#endif
1847
1848/***********************************************************/
1849/* ram save/restore */
1850
1851static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1852{
1853 int v;
1854
1855 v = qemu_get_byte(f);
1856 switch(v) {
1857 case 0:
1858 if (qemu_get_buffer(f, buf, len) != len)
1859 return -EIO;
1860 break;
1861 case 1:
1862 v = qemu_get_byte(f);
1863 memset(buf, v, len);
1864 break;
1865 default:
1866 return -EINVAL;
1867 }
1868
1869 if (qemu_file_has_error(f))
1870 return -EIO;
1871
1872 return 0;
1873}
1874
1875static int ram_load_v1(QEMUFile *f, void *opaque)
1876{
1877 int ret;
1878 ram_addr_t i;
1879
1880 if (qemu_get_be32(f) != last_ram_offset)
1881 return -EINVAL;
1882 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
1883 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
1884 if (ret)
1885 return ret;
1886 }
1887 return 0;
1888}
1889
1890#define BDRV_HASH_BLOCK_SIZE 1024
1891#define IOBUF_SIZE 4096
1892#define RAM_CBLOCK_MAGIC 0xfabe
1893
1894typedef struct RamDecompressState {
1895 z_stream zstream;
1896 QEMUFile *f;
1897 uint8_t buf[IOBUF_SIZE];
1898} RamDecompressState;
1899
1900static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
1901{
1902 int ret;
1903 memset(s, 0, sizeof(*s));
1904 s->f = f;
1905 ret = inflateInit(&s->zstream);
1906 if (ret != Z_OK)
1907 return -1;
1908 return 0;
1909}
1910
1911static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
1912{
1913 int ret, clen;
1914
1915 s->zstream.avail_out = len;
1916 s->zstream.next_out = buf;
1917 while (s->zstream.avail_out > 0) {
1918 if (s->zstream.avail_in == 0) {
1919 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
1920 return -1;
1921 clen = qemu_get_be16(s->f);
1922 if (clen > IOBUF_SIZE)
1923 return -1;
1924 qemu_get_buffer(s->f, s->buf, clen);
1925 s->zstream.avail_in = clen;
1926 s->zstream.next_in = s->buf;
1927 }
1928 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
1929 if (ret != Z_OK && ret != Z_STREAM_END) {
1930 return -1;
1931 }
1932 }
1933 return 0;
1934}
1935
1936static void ram_decompress_close(RamDecompressState *s)
1937{
1938 inflateEnd(&s->zstream);
1939}
1940
1941#define RAM_SAVE_FLAG_FULL 0x01
1942#define RAM_SAVE_FLAG_COMPRESS 0x02
1943#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1944#define RAM_SAVE_FLAG_PAGE 0x08
1945#define RAM_SAVE_FLAG_EOS 0x10
1946
1947static int is_dup_page(uint8_t *page, uint8_t ch)
1948{
1949 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1950 uint32_t *array = (uint32_t *)page;
1951 int i;
1952
1953 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1954 if (array[i] != val)
1955 return 0;
1956 }
1957
1958 return 1;
1959}
1960
1961static int ram_save_block(QEMUFile *f)
1962{
1963 static ram_addr_t current_addr = 0;
1964 ram_addr_t saved_addr = current_addr;
1965 ram_addr_t addr = 0;
1966 int found = 0;
1967
1968 while (addr < last_ram_offset) {
1969 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
1970 uint8_t *p;
1971
1972 cpu_physical_memory_reset_dirty(current_addr,
1973 current_addr + TARGET_PAGE_SIZE,
1974 MIGRATION_DIRTY_FLAG);
1975
1976 p = qemu_get_ram_ptr(current_addr);
1977
1978 if (is_dup_page(p, *p)) {
1979 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
1980 qemu_put_byte(f, *p);
1981 } else {
1982 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
1983 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
1984 }
1985
1986 found = 1;
1987 break;
1988 }
1989 addr += TARGET_PAGE_SIZE;
1990 current_addr = (saved_addr + addr) % last_ram_offset;
1991 }
1992
1993 return found;
1994}
1995
1996static uint64_t bytes_transferred = 0;
1997
1998static ram_addr_t ram_save_remaining(void)
1999{
2000 ram_addr_t addr;
2001 ram_addr_t count = 0;
2002
2003 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2004 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2005 count++;
2006 }
2007
2008 return count;
2009}
2010
2011uint64_t ram_bytes_remaining(void)
2012{
2013 return ram_save_remaining() * TARGET_PAGE_SIZE;
2014}
2015
2016uint64_t ram_bytes_transferred(void)
2017{
2018 return bytes_transferred;
2019}
2020
2021uint64_t ram_bytes_total(void)
2022{
2023 return last_ram_offset;
2024}
2025
2026static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2027{
2028 ram_addr_t addr;
2029 uint64_t bytes_transferred_last;
2030 double bwidth = 0;
2031 uint64_t expected_time = 0;
2032
2033 cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX);
2034
2035 if (stage == 1) {
2036 /* Make sure all dirty bits are set */
2037 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
2038 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2039 cpu_physical_memory_set_dirty(addr);
2040 }
2041
2042 /* Enable dirty memory tracking */
2043 cpu_physical_memory_set_dirty_tracking(1);
2044
2045 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
2046 }
2047
2048 bytes_transferred_last = bytes_transferred;
David Turner6a9ef172010-09-09 22:54:36 +02002049 bwidth = qemu_get_clock_ns(rt_clock);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002050
2051 while (!qemu_file_rate_limit(f)) {
2052 int ret;
2053
2054 ret = ram_save_block(f);
2055 bytes_transferred += ret * TARGET_PAGE_SIZE;
2056 if (ret == 0) /* no more blocks */
2057 break;
2058 }
2059
David Turner6a9ef172010-09-09 22:54:36 +02002060 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002061 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2062
2063 /* if we haven't transferred anything this round, force expected_time to a
2064 * a very high value, but without crashing */
2065 if (bwidth == 0)
2066 bwidth = 0.000001;
2067
2068 /* try transferring iterative blocks of memory */
2069
2070 if (stage == 3) {
2071
2072 /* flush all remaining blocks regardless of rate limiting */
2073 while (ram_save_block(f) != 0) {
2074 bytes_transferred += TARGET_PAGE_SIZE;
2075 }
2076 cpu_physical_memory_set_dirty_tracking(0);
2077 }
2078
2079 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2080
2081 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2082
2083 return (stage == 2) && (expected_time <= migrate_max_downtime());
2084}
2085
2086static int ram_load_dead(QEMUFile *f, void *opaque)
2087{
2088 RamDecompressState s1, *s = &s1;
2089 uint8_t buf[10];
2090 ram_addr_t i;
2091
2092 if (ram_decompress_open(s, f) < 0)
2093 return -EINVAL;
2094 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
2095 if (ram_decompress_buf(s, buf, 1) < 0) {
2096 fprintf(stderr, "Error while reading ram block header\n");
2097 goto error;
2098 }
2099 if (buf[0] == 0) {
2100 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
2101 BDRV_HASH_BLOCK_SIZE) < 0) {
2102 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
2103 goto error;
2104 }
2105 } else {
2106 error:
2107 printf("Error block header\n");
2108 return -EINVAL;
2109 }
2110 }
2111 ram_decompress_close(s);
2112
2113 return 0;
2114}
2115
2116static int ram_load(QEMUFile *f, void *opaque, int version_id)
2117{
2118 ram_addr_t addr;
2119 int flags;
2120
2121 if (version_id == 1)
2122 return ram_load_v1(f, opaque);
2123
2124 if (version_id == 2) {
2125 if (qemu_get_be32(f) != last_ram_offset)
2126 return -EINVAL;
2127 return ram_load_dead(f, opaque);
2128 }
2129
2130 if (version_id != 3)
2131 return -EINVAL;
2132
2133 do {
2134 addr = qemu_get_be64(f);
2135
2136 flags = addr & ~TARGET_PAGE_MASK;
2137 addr &= TARGET_PAGE_MASK;
2138
2139 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
2140 if (addr != last_ram_offset)
2141 return -EINVAL;
2142 }
2143
2144 if (flags & RAM_SAVE_FLAG_FULL) {
2145 if (ram_load_dead(f, opaque) < 0)
2146 return -EINVAL;
2147 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07002148
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002149 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2150 uint8_t ch = qemu_get_byte(f);
2151 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2152 } else if (flags & RAM_SAVE_FLAG_PAGE)
2153 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
2154 } while (!(flags & RAM_SAVE_FLAG_EOS));
2155
2156 return 0;
2157}
2158
2159void qemu_service_io(void)
2160{
2161 qemu_notify_event();
2162}
2163
2164/***********************************************************/
2165/* bottom halves (can be seen as timers which expire ASAP) */
2166
2167struct QEMUBH {
2168 QEMUBHFunc *cb;
2169 void *opaque;
2170 int scheduled;
2171 int idle;
2172 int deleted;
2173 QEMUBH *next;
2174};
2175
2176static QEMUBH *first_bh = NULL;
2177
2178QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
2179{
2180 QEMUBH *bh;
2181 bh = qemu_mallocz(sizeof(QEMUBH));
2182 bh->cb = cb;
2183 bh->opaque = opaque;
2184 bh->next = first_bh;
2185 first_bh = bh;
2186 return bh;
2187}
2188
2189int qemu_bh_poll(void)
2190{
2191 QEMUBH *bh, **bhp;
2192 int ret;
2193
2194 ret = 0;
2195 for (bh = first_bh; bh; bh = bh->next) {
2196 if (!bh->deleted && bh->scheduled) {
2197 bh->scheduled = 0;
2198 if (!bh->idle)
2199 ret = 1;
2200 bh->idle = 0;
2201 bh->cb(bh->opaque);
2202 }
2203 }
2204
2205 /* remove deleted bhs */
2206 bhp = &first_bh;
2207 while (*bhp) {
2208 bh = *bhp;
2209 if (bh->deleted) {
2210 *bhp = bh->next;
2211 qemu_free(bh);
2212 } else
2213 bhp = &bh->next;
2214 }
2215
2216 return ret;
2217}
2218
2219void qemu_bh_schedule_idle(QEMUBH *bh)
2220{
2221 if (bh->scheduled)
2222 return;
2223 bh->scheduled = 1;
2224 bh->idle = 1;
2225}
2226
2227void qemu_bh_schedule(QEMUBH *bh)
2228{
2229 if (bh->scheduled)
2230 return;
2231 bh->scheduled = 1;
2232 bh->idle = 0;
2233 /* stop the currently executing CPU to execute the BH ASAP */
2234 qemu_notify_event();
2235}
2236
2237void qemu_bh_cancel(QEMUBH *bh)
2238{
2239 bh->scheduled = 0;
2240}
2241
2242void qemu_bh_delete(QEMUBH *bh)
2243{
2244 bh->scheduled = 0;
2245 bh->deleted = 1;
2246}
2247
David Turnerd0659942010-09-10 10:42:32 +02002248void qemu_bh_update_timeout(int *timeout)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002249{
2250 QEMUBH *bh;
2251
2252 for (bh = first_bh; bh; bh = bh->next) {
2253 if (!bh->deleted && bh->scheduled) {
2254 if (bh->idle) {
2255 /* idle bottom halves will be polled at least
2256 * every 10ms */
2257 *timeout = MIN(10, *timeout);
2258 } else {
2259 /* non-idle bottom halves will be executed
2260 * immediately */
2261 *timeout = 0;
2262 break;
2263 }
2264 }
2265 }
2266}
2267
2268/***********************************************************/
2269/* machine registration */
2270
2271static QEMUMachine *first_machine = NULL;
2272QEMUMachine *current_machine = NULL;
2273
2274int qemu_register_machine(QEMUMachine *m)
2275{
2276 QEMUMachine **pm;
2277 pm = &first_machine;
2278 while (*pm != NULL)
2279 pm = &(*pm)->next;
2280 m->next = NULL;
2281 *pm = m;
2282 return 0;
2283}
2284
2285static QEMUMachine *find_machine(const char *name)
2286{
2287 QEMUMachine *m;
2288
2289 for(m = first_machine; m != NULL; m = m->next) {
2290 if (!strcmp(m->name, name))
2291 return m;
2292 }
2293 return NULL;
2294}
2295
2296static QEMUMachine *find_default_machine(void)
2297{
2298 QEMUMachine *m;
2299
2300 for(m = first_machine; m != NULL; m = m->next) {
2301 if (m->is_default) {
2302 return m;
2303 }
2304 }
2305 return NULL;
2306}
2307
2308/***********************************************************/
2309/* main execution loop */
2310
2311static void gui_update(void *opaque)
2312{
2313 uint64_t interval = GUI_REFRESH_INTERVAL;
2314 DisplayState *ds = opaque;
2315 DisplayChangeListener *dcl = ds->listeners;
2316
2317 dpy_refresh(ds);
2318
2319 while (dcl != NULL) {
2320 if (dcl->gui_timer_interval &&
2321 dcl->gui_timer_interval < interval)
2322 interval = dcl->gui_timer_interval;
2323 dcl = dcl->next;
2324 }
2325 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
2326}
2327
2328static void nographic_update(void *opaque)
2329{
2330 uint64_t interval = GUI_REFRESH_INTERVAL;
2331
2332 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
2333}
2334
2335struct vm_change_state_entry {
2336 VMChangeStateHandler *cb;
2337 void *opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002338 QLIST_ENTRY (vm_change_state_entry) entries;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002339};
2340
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002341static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002342
2343VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
2344 void *opaque)
2345{
2346 VMChangeStateEntry *e;
2347
2348 e = qemu_mallocz(sizeof (*e));
2349
2350 e->cb = cb;
2351 e->opaque = opaque;
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002352 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002353 return e;
2354}
2355
2356void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
2357{
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07002358 QLIST_REMOVE (e, entries);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002359 qemu_free (e);
2360}
2361
2362static void vm_state_notify(int running, int reason)
2363{
2364 VMChangeStateEntry *e;
2365
2366 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
2367 e->cb(e->opaque, running, reason);
2368 }
2369}
2370
2371static void resume_all_vcpus(void);
2372static void pause_all_vcpus(void);
2373
2374void vm_start(void)
2375{
2376 if (!vm_running) {
2377 cpu_enable_ticks();
2378 vm_running = 1;
2379 vm_state_notify(1, 0);
David Turner6a9ef172010-09-09 22:54:36 +02002380 //qemu_rearm_alarm_timer(alarm_timer);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002381 resume_all_vcpus();
2382 }
2383}
2384
2385/* reset/shutdown handler */
2386
2387typedef struct QEMUResetEntry {
2388 QEMUResetHandler *func;
2389 void *opaque;
2390 int order;
2391 struct QEMUResetEntry *next;
2392} QEMUResetEntry;
2393
2394static QEMUResetEntry *first_reset_entry;
2395static int reset_requested;
2396static int shutdown_requested;
2397static int powerdown_requested;
2398static int debug_requested;
2399static int vmstop_requested;
2400
2401int qemu_shutdown_requested(void)
2402{
2403 int r = shutdown_requested;
2404 shutdown_requested = 0;
2405 return r;
2406}
2407
2408int qemu_reset_requested(void)
2409{
2410 int r = reset_requested;
2411 reset_requested = 0;
2412 return r;
2413}
2414
2415int qemu_powerdown_requested(void)
2416{
2417 int r = powerdown_requested;
2418 powerdown_requested = 0;
2419 return r;
2420}
2421
2422static int qemu_debug_requested(void)
2423{
2424 int r = debug_requested;
2425 debug_requested = 0;
2426 return r;
2427}
2428
2429static int qemu_vmstop_requested(void)
2430{
2431 int r = vmstop_requested;
2432 vmstop_requested = 0;
2433 return r;
2434}
2435
2436static void do_vm_stop(int reason)
2437{
2438 if (vm_running) {
2439 cpu_disable_ticks();
2440 vm_running = 0;
2441 pause_all_vcpus();
2442 vm_state_notify(0, reason);
2443 }
2444}
2445
2446void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
2447{
2448 QEMUResetEntry **pre, *re;
2449
2450 pre = &first_reset_entry;
2451 while (*pre != NULL && (*pre)->order >= order) {
2452 pre = &(*pre)->next;
2453 }
2454 re = qemu_mallocz(sizeof(QEMUResetEntry));
2455 re->func = func;
2456 re->opaque = opaque;
2457 re->order = order;
2458 re->next = NULL;
2459 *pre = re;
2460}
2461
2462void qemu_system_reset(void)
2463{
2464 QEMUResetEntry *re;
2465
2466 /* reset all devices */
2467 for(re = first_reset_entry; re != NULL; re = re->next) {
2468 re->func(re->opaque);
2469 }
2470}
2471
2472void qemu_system_reset_request(void)
2473{
2474 if (no_reboot) {
2475 shutdown_requested = 1;
2476 } else {
2477 reset_requested = 1;
2478 }
2479 qemu_notify_event();
2480}
2481
2482void qemu_system_shutdown_request(void)
2483{
2484 shutdown_requested = 1;
2485 qemu_notify_event();
2486}
2487
2488void qemu_system_powerdown_request(void)
2489{
2490 powerdown_requested = 1;
2491 qemu_notify_event();
2492}
2493
2494#ifdef CONFIG_IOTHREAD
2495static void qemu_system_vmstop_request(int reason)
2496{
2497 vmstop_requested = reason;
2498 qemu_notify_event();
2499}
2500#endif
2501
2502#ifndef _WIN32
2503static int io_thread_fd = -1;
2504
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002505#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002506static void qemu_event_increment(void)
2507{
2508 static const char byte = 0;
2509
2510 if (io_thread_fd == -1)
2511 return;
2512
2513 write(io_thread_fd, &byte, sizeof(byte));
2514}
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002515#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002516
2517static void qemu_event_read(void *opaque)
2518{
2519 int fd = (unsigned long)opaque;
2520 ssize_t len;
2521
2522 /* Drain the notify pipe */
2523 do {
2524 char buffer[512];
2525 len = read(fd, buffer, sizeof(buffer));
2526 } while ((len == -1 && errno == EINTR) || len > 0);
2527}
2528
2529static int qemu_event_init(void)
2530{
2531 int err;
2532 int fds[2];
2533
2534 err = pipe(fds);
2535 if (err == -1)
2536 return -errno;
2537
2538 err = fcntl_setfl(fds[0], O_NONBLOCK);
2539 if (err < 0)
2540 goto fail;
2541
2542 err = fcntl_setfl(fds[1], O_NONBLOCK);
2543 if (err < 0)
2544 goto fail;
2545
2546 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
2547 (void *)(unsigned long)fds[0]);
2548
2549 io_thread_fd = fds[1];
2550 return 0;
2551
2552fail:
2553 close(fds[0]);
2554 close(fds[1]);
2555 return err;
2556}
2557#else
2558HANDLE qemu_event_handle;
2559
2560static void dummy_event_handler(void *opaque)
2561{
2562}
2563
2564static int qemu_event_init(void)
2565{
2566 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
2567 if (!qemu_event_handle) {
2568 perror("Failed CreateEvent");
2569 return -1;
2570 }
2571 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
2572 return 0;
2573}
2574
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002575#if 0
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002576static void qemu_event_increment(void)
2577{
2578 SetEvent(qemu_event_handle);
2579}
2580#endif
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02002581#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002582
2583static int cpu_can_run(CPUState *env)
2584{
2585 if (env->stop)
2586 return 0;
2587 if (env->stopped)
2588 return 0;
2589 return 1;
2590}
2591
2592#ifndef CONFIG_IOTHREAD
2593static int qemu_init_main_loop(void)
2594{
2595 return qemu_event_init();
2596}
2597
2598void qemu_init_vcpu(void *_env)
2599{
2600 CPUState *env = _env;
2601
2602 if (kvm_enabled())
2603 kvm_init_vcpu(env);
2604 return;
2605}
2606
2607int qemu_cpu_self(void *env)
2608{
2609 return 1;
2610}
2611
2612static void resume_all_vcpus(void)
2613{
2614}
2615
2616static void pause_all_vcpus(void)
2617{
2618}
2619
2620void qemu_cpu_kick(void *env)
2621{
2622 return;
2623}
2624
2625void qemu_notify_event(void)
2626{
2627 CPUState *env = cpu_single_env;
2628
2629 if (env) {
2630 cpu_exit(env);
2631#ifdef USE_KQEMU
2632 if (env->kqemu_enabled)
2633 kqemu_cpu_interrupt(env);
2634#endif
2635 }
2636}
2637
2638#define qemu_mutex_lock_iothread() do { } while (0)
2639#define qemu_mutex_unlock_iothread() do { } while (0)
2640
2641void vm_stop(int reason)
2642{
2643 do_vm_stop(reason);
2644}
2645
2646#else /* CONFIG_IOTHREAD */
2647
2648#include "qemu-thread.h"
2649
2650QemuMutex qemu_global_mutex;
2651static QemuMutex qemu_fair_mutex;
2652
2653static QemuThread io_thread;
2654
2655static QemuThread *tcg_cpu_thread;
2656static QemuCond *tcg_halt_cond;
2657
2658static int qemu_system_ready;
2659/* cpu creation */
2660static QemuCond qemu_cpu_cond;
2661/* system init */
2662static QemuCond qemu_system_cond;
2663static QemuCond qemu_pause_cond;
2664
2665static void block_io_signals(void);
2666static void unblock_io_signals(void);
2667static int tcg_has_work(void);
2668
2669static int qemu_init_main_loop(void)
2670{
2671 int ret;
2672
2673 ret = qemu_event_init();
2674 if (ret)
2675 return ret;
2676
2677 qemu_cond_init(&qemu_pause_cond);
2678 qemu_mutex_init(&qemu_fair_mutex);
2679 qemu_mutex_init(&qemu_global_mutex);
2680 qemu_mutex_lock(&qemu_global_mutex);
2681
2682 unblock_io_signals();
2683 qemu_thread_self(&io_thread);
2684
2685 return 0;
2686}
2687
2688static void qemu_wait_io_event(CPUState *env)
2689{
2690 while (!tcg_has_work())
2691 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
2692
2693 qemu_mutex_unlock(&qemu_global_mutex);
2694
2695 /*
2696 * Users of qemu_global_mutex can be starved, having no chance
2697 * to acquire it since this path will get to it first.
2698 * So use another lock to provide fairness.
2699 */
2700 qemu_mutex_lock(&qemu_fair_mutex);
2701 qemu_mutex_unlock(&qemu_fair_mutex);
2702
2703 qemu_mutex_lock(&qemu_global_mutex);
2704 if (env->stop) {
2705 env->stop = 0;
2706 env->stopped = 1;
2707 qemu_cond_signal(&qemu_pause_cond);
2708 }
2709}
2710
2711static int qemu_cpu_exec(CPUState *env);
2712
2713static void *kvm_cpu_thread_fn(void *arg)
2714{
2715 CPUState *env = arg;
2716
2717 block_io_signals();
2718 qemu_thread_self(env->thread);
2719
2720 /* signal CPU creation */
2721 qemu_mutex_lock(&qemu_global_mutex);
2722 env->created = 1;
2723 qemu_cond_signal(&qemu_cpu_cond);
2724
2725 /* and wait for machine initialization */
2726 while (!qemu_system_ready)
2727 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2728
2729 while (1) {
2730 if (cpu_can_run(env))
2731 qemu_cpu_exec(env);
2732 qemu_wait_io_event(env);
2733 }
2734
2735 return NULL;
2736}
2737
2738static void tcg_cpu_exec(void);
2739
2740static void *tcg_cpu_thread_fn(void *arg)
2741{
2742 CPUState *env = arg;
2743
2744 block_io_signals();
2745 qemu_thread_self(env->thread);
2746
2747 /* signal CPU creation */
2748 qemu_mutex_lock(&qemu_global_mutex);
2749 for (env = first_cpu; env != NULL; env = env->next_cpu)
2750 env->created = 1;
2751 qemu_cond_signal(&qemu_cpu_cond);
2752
2753 /* and wait for machine initialization */
2754 while (!qemu_system_ready)
2755 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
2756
2757 while (1) {
2758 tcg_cpu_exec();
2759 qemu_wait_io_event(cur_cpu);
2760 }
2761
2762 return NULL;
2763}
2764
2765void qemu_cpu_kick(void *_env)
2766{
2767 CPUState *env = _env;
2768 qemu_cond_broadcast(env->halt_cond);
2769 if (kvm_enabled())
2770 qemu_thread_signal(env->thread, SIGUSR1);
2771}
2772
2773int qemu_cpu_self(void *env)
2774{
2775 return (cpu_single_env != NULL);
2776}
2777
2778static void cpu_signal(int sig)
2779{
2780 if (cpu_single_env)
2781 cpu_exit(cpu_single_env);
2782}
2783
2784static void block_io_signals(void)
2785{
2786 sigset_t set;
2787 struct sigaction sigact;
2788
2789 sigemptyset(&set);
2790 sigaddset(&set, SIGUSR2);
2791 sigaddset(&set, SIGIO);
2792 sigaddset(&set, SIGALRM);
2793 pthread_sigmask(SIG_BLOCK, &set, NULL);
2794
2795 sigemptyset(&set);
2796 sigaddset(&set, SIGUSR1);
2797 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2798
2799 memset(&sigact, 0, sizeof(sigact));
2800 sigact.sa_handler = cpu_signal;
2801 sigaction(SIGUSR1, &sigact, NULL);
2802}
2803
2804static void unblock_io_signals(void)
2805{
2806 sigset_t set;
2807
2808 sigemptyset(&set);
2809 sigaddset(&set, SIGUSR2);
2810 sigaddset(&set, SIGIO);
2811 sigaddset(&set, SIGALRM);
2812 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2813
2814 sigemptyset(&set);
2815 sigaddset(&set, SIGUSR1);
2816 pthread_sigmask(SIG_BLOCK, &set, NULL);
2817}
2818
2819static void qemu_signal_lock(unsigned int msecs)
2820{
2821 qemu_mutex_lock(&qemu_fair_mutex);
2822
2823 while (qemu_mutex_trylock(&qemu_global_mutex)) {
2824 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
2825 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
2826 break;
2827 }
2828 qemu_mutex_unlock(&qemu_fair_mutex);
2829}
2830
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002831void qemu_mutex_lock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002832{
2833 if (kvm_enabled()) {
2834 qemu_mutex_lock(&qemu_fair_mutex);
2835 qemu_mutex_lock(&qemu_global_mutex);
2836 qemu_mutex_unlock(&qemu_fair_mutex);
2837 } else
2838 qemu_signal_lock(100);
2839}
2840
David 'Digit' Turnerc34e8dc2010-09-13 02:47:01 -07002841void qemu_mutex_unlock_iothread(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07002842{
2843 qemu_mutex_unlock(&qemu_global_mutex);
2844}
2845
2846static int all_vcpus_paused(void)
2847{
2848 CPUState *penv = first_cpu;
2849
2850 while (penv) {
2851 if (!penv->stopped)
2852 return 0;
2853 penv = (CPUState *)penv->next_cpu;
2854 }
2855
2856 return 1;
2857}
2858
2859static void pause_all_vcpus(void)
2860{
2861 CPUState *penv = first_cpu;
2862
2863 while (penv) {
2864 penv->stop = 1;
2865 qemu_thread_signal(penv->thread, SIGUSR1);
2866 qemu_cpu_kick(penv);
2867 penv = (CPUState *)penv->next_cpu;
2868 }
2869
2870 while (!all_vcpus_paused()) {
2871 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
2872 penv = first_cpu;
2873 while (penv) {
2874 qemu_thread_signal(penv->thread, SIGUSR1);
2875 penv = (CPUState *)penv->next_cpu;
2876 }
2877 }
2878}
2879
2880static void resume_all_vcpus(void)
2881{
2882 CPUState *penv = first_cpu;
2883
2884 while (penv) {
2885 penv->stop = 0;
2886 penv->stopped = 0;
2887 qemu_thread_signal(penv->thread, SIGUSR1);
2888 qemu_cpu_kick(penv);
2889 penv = (CPUState *)penv->next_cpu;
2890 }
2891}
2892
2893static void tcg_init_vcpu(void *_env)
2894{
2895 CPUState *env = _env;
2896 /* share a single thread for all cpus with TCG */
2897 if (!tcg_cpu_thread) {
2898 env->thread = qemu_mallocz(sizeof(QemuThread));
2899 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2900 qemu_cond_init(env->halt_cond);
2901 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
2902 while (env->created == 0)
2903 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2904 tcg_cpu_thread = env->thread;
2905 tcg_halt_cond = env->halt_cond;
2906 } else {
2907 env->thread = tcg_cpu_thread;
2908 env->halt_cond = tcg_halt_cond;
2909 }
2910}
2911
2912static void kvm_start_vcpu(CPUState *env)
2913{
2914#if 0
2915 kvm_init_vcpu(env);
2916 env->thread = qemu_mallocz(sizeof(QemuThread));
2917 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
2918 qemu_cond_init(env->halt_cond);
2919 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
2920 while (env->created == 0)
2921 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
2922#endif
2923}
2924
2925void qemu_init_vcpu(void *_env)
2926{
2927 CPUState *env = _env;
2928
2929 if (kvm_enabled())
2930 kvm_start_vcpu(env);
2931 else
2932 tcg_init_vcpu(env);
2933}
2934
2935void qemu_notify_event(void)
2936{
2937 qemu_event_increment();
2938}
2939
2940void vm_stop(int reason)
2941{
2942 QemuThread me;
2943 qemu_thread_self(&me);
2944
2945 if (!qemu_thread_equal(&me, &io_thread)) {
2946 qemu_system_vmstop_request(reason);
2947 /*
2948 * FIXME: should not return to device code in case
2949 * vm_stop() has been requested.
2950 */
2951 if (cpu_single_env) {
2952 cpu_exit(cpu_single_env);
2953 cpu_single_env->stop = 1;
2954 }
2955 return;
2956 }
2957 do_vm_stop(reason);
2958}
2959
2960#endif
2961
2962
2963#ifdef _WIN32
2964static void host_main_loop_wait(int *timeout)
2965{
2966 int ret, ret2, i;
2967 PollingEntry *pe;
2968
2969
2970 /* XXX: need to suppress polling by better using win32 events */
2971 ret = 0;
2972 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2973 ret |= pe->func(pe->opaque);
2974 }
2975 if (ret == 0) {
2976 int err;
2977 WaitObjects *w = &wait_objects;
2978
2979 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
2980 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2981 if (w->func[ret - WAIT_OBJECT_0])
2982 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
2983
2984 /* Check for additional signaled events */
2985 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
2986
2987 /* Check if event is signaled */
2988 ret2 = WaitForSingleObject(w->events[i], 0);
2989 if(ret2 == WAIT_OBJECT_0) {
2990 if (w->func[i])
2991 w->func[i](w->opaque[i]);
2992 } else if (ret2 == WAIT_TIMEOUT) {
2993 } else {
2994 err = GetLastError();
2995 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
2996 }
2997 }
2998 } else if (ret == WAIT_TIMEOUT) {
2999 } else {
3000 err = GetLastError();
3001 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
3002 }
3003 }
3004
3005 *timeout = 0;
3006}
3007#else
3008static void host_main_loop_wait(int *timeout)
3009{
3010}
3011#endif
3012
3013void main_loop_wait(int timeout)
3014{
3015 IOHandlerRecord *ioh;
3016 fd_set rfds, wfds, xfds;
3017 int ret, nfds;
3018 struct timeval tv;
3019
3020 qemu_bh_update_timeout(&timeout);
3021
3022 host_main_loop_wait(&timeout);
3023
3024 /* poll any events */
3025 /* XXX: separate device handlers from system ones */
3026 nfds = -1;
3027 FD_ZERO(&rfds);
3028 FD_ZERO(&wfds);
3029 FD_ZERO(&xfds);
3030 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3031 if (ioh->deleted)
3032 continue;
3033 if (ioh->fd_read &&
3034 (!ioh->fd_read_poll ||
3035 ioh->fd_read_poll(ioh->opaque) != 0)) {
3036 FD_SET(ioh->fd, &rfds);
3037 if (ioh->fd > nfds)
3038 nfds = ioh->fd;
3039 }
3040 if (ioh->fd_write) {
3041 FD_SET(ioh->fd, &wfds);
3042 if (ioh->fd > nfds)
3043 nfds = ioh->fd;
3044 }
3045 }
3046
3047 tv.tv_sec = timeout / 1000;
3048 tv.tv_usec = (timeout % 1000) * 1000;
3049
3050#if defined(CONFIG_SLIRP)
3051 if (slirp_is_inited()) {
3052 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3053 }
3054#endif
3055 qemu_mutex_unlock_iothread();
3056 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
3057 qemu_mutex_lock_iothread();
3058 if (ret > 0) {
3059 IOHandlerRecord **pioh;
3060
3061 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
3062 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
3063 ioh->fd_read(ioh->opaque);
3064 }
3065 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
3066 ioh->fd_write(ioh->opaque);
3067 }
3068 }
3069
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003070 /* remove deleted IO handlers */
3071 pioh = &first_io_handler;
3072 while (*pioh) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003073 ioh = *pioh;
3074 if (ioh->deleted) {
3075 *pioh = ioh->next;
3076 qemu_free(ioh);
3077 } else
3078 pioh = &ioh->next;
3079 }
3080 }
3081#if defined(CONFIG_SLIRP)
3082 if (slirp_is_inited()) {
3083 if (ret < 0) {
3084 FD_ZERO(&rfds);
3085 FD_ZERO(&wfds);
3086 FD_ZERO(&xfds);
3087 }
3088 slirp_select_poll(&rfds, &wfds, &xfds);
3089 }
3090#endif
3091 charpipe_poll();
3092
David Turner6a9ef172010-09-09 22:54:36 +02003093 qemu_run_all_timers();
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003094
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003095 /* Check bottom-halves last in case any of the earlier events triggered
3096 them. */
3097 qemu_bh_poll();
3098
3099}
3100
3101static int qemu_cpu_exec(CPUState *env)
3102{
3103 int ret;
3104#ifdef CONFIG_PROFILER
3105 int64_t ti;
3106#endif
3107
3108#ifdef CONFIG_PROFILER
3109 ti = profile_getclock();
3110#endif
3111 if (use_icount) {
3112 int64_t count;
3113 int decr;
3114 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3115 env->icount_decr.u16.low = 0;
3116 env->icount_extra = 0;
3117 count = qemu_next_deadline();
3118 count = (count + (1 << icount_time_shift) - 1)
3119 >> icount_time_shift;
3120 qemu_icount += count;
3121 decr = (count > 0xffff) ? 0xffff : count;
3122 count -= decr;
3123 env->icount_decr.u16.low = decr;
3124 env->icount_extra = count;
3125 }
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07003126#ifdef CONFIG_TRACE
3127 if (tbflush_requested) {
3128 tbflush_requested = 0;
3129 tb_flush(env);
3130 return EXCP_INTERRUPT;
3131 }
3132#endif
3133
3134
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003135 ret = cpu_exec(env);
3136#ifdef CONFIG_PROFILER
3137 qemu_time += profile_getclock() - ti;
3138#endif
3139 if (use_icount) {
3140 /* Fold pending instructions back into the
3141 instruction counter, and clear the interrupt flag. */
3142 qemu_icount -= (env->icount_decr.u16.low
3143 + env->icount_extra);
3144 env->icount_decr.u32 = 0;
3145 env->icount_extra = 0;
3146 }
3147 return ret;
3148}
3149
3150static void tcg_cpu_exec(void)
3151{
3152 int ret = 0;
3153
3154 if (next_cpu == NULL)
3155 next_cpu = first_cpu;
3156 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3157 CPUState *env = cur_cpu = next_cpu;
3158
3159 if (!vm_running)
3160 break;
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003161 if (qemu_timer_alarm_pending()) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003162 break;
3163 }
3164 if (cpu_can_run(env))
3165 ret = qemu_cpu_exec(env);
3166 if (ret == EXCP_DEBUG) {
3167 gdb_set_stop_cpu(env);
3168 debug_requested = 1;
3169 break;
3170 }
3171 }
3172}
3173
3174static int cpu_has_work(CPUState *env)
3175{
3176 if (env->stop)
3177 return 1;
3178 if (env->stopped)
3179 return 0;
3180 if (!env->halted)
3181 return 1;
3182 if (qemu_cpu_has_work(env))
3183 return 1;
3184 return 0;
3185}
3186
David 'Digit' Turner6b512812010-10-15 15:05:04 +02003187int tcg_has_work(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003188{
3189 CPUState *env;
3190
3191 for (env = first_cpu; env != NULL; env = env->next_cpu)
3192 if (cpu_has_work(env))
3193 return 1;
3194 return 0;
3195}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003196
3197static int vm_can_run(void)
3198{
3199 if (powerdown_requested)
3200 return 0;
3201 if (reset_requested)
3202 return 0;
3203 if (shutdown_requested)
3204 return 0;
3205 if (debug_requested)
3206 return 0;
3207 return 1;
3208}
3209
3210static void main_loop(void)
3211{
3212 int r;
3213
3214#ifdef CONFIG_IOTHREAD
3215 qemu_system_ready = 1;
3216 qemu_cond_broadcast(&qemu_system_cond);
3217#endif
3218
3219 for (;;) {
3220 do {
3221#ifdef CONFIG_PROFILER
3222 int64_t ti;
3223#endif
3224#ifndef CONFIG_IOTHREAD
3225 tcg_cpu_exec();
3226#endif
3227#ifdef CONFIG_PROFILER
3228 ti = profile_getclock();
3229#endif
3230 main_loop_wait(qemu_calculate_timeout());
3231#ifdef CONFIG_PROFILER
3232 dev_time += profile_getclock() - ti;
3233#endif
3234 } while (vm_can_run());
3235
3236 if (qemu_debug_requested())
3237 vm_stop(EXCP_DEBUG);
3238 if (qemu_shutdown_requested()) {
3239 if (no_shutdown) {
3240 vm_stop(0);
3241 no_shutdown = 0;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003242 } else {
3243#if CONFIG_ANDROID_SNAPSHOTS
3244 if (savevm_on_exit != NULL) {
3245 do_savevm(cur_mon, savevm_on_exit);
3246 }
3247#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003248 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00003249 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003250 }
3251 if (qemu_reset_requested()) {
3252 pause_all_vcpus();
3253 qemu_system_reset();
3254 resume_all_vcpus();
3255 }
3256 if (qemu_powerdown_requested())
3257 qemu_system_powerdown();
3258 if ((r = qemu_vmstop_requested()))
3259 vm_stop(r);
3260 }
3261 pause_all_vcpus();
3262}
3263
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003264void version(void)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003265{
3266 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
3267}
3268
3269void qemu_help(int exitcode)
3270{
3271 version();
3272 printf("usage: %s [options] [disk_image]\n"
3273 "\n"
3274 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3275 "\n"
3276#define DEF(option, opt_arg, opt_enum, opt_help) \
3277 opt_help
3278#define DEFHEADING(text) stringify(text) "\n"
3279#include "qemu-options.h"
3280#undef DEF
3281#undef DEFHEADING
3282#undef GEN_DOCS
3283 "\n"
3284 "During emulation, the following keys are useful:\n"
3285 "ctrl-alt-f toggle full screen\n"
3286 "ctrl-alt-n switch to virtual console 'n'\n"
3287 "ctrl-alt toggle mouse and keyboard grab\n"
3288 "\n"
3289 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3290 ,
3291 "qemu",
3292 DEFAULT_RAM_SIZE,
3293#ifndef _WIN32
3294 DEFAULT_NETWORK_SCRIPT,
3295 DEFAULT_NETWORK_DOWN_SCRIPT,
3296#endif
3297 DEFAULT_GDBSTUB_PORT,
3298 "/tmp/qemu.log");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003299 QEMU_EXIT(exitcode);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003300}
3301
3302#define HAS_ARG 0x0001
3303
3304enum {
3305#define DEF(option, opt_arg, opt_enum, opt_help) \
3306 opt_enum,
3307#define DEFHEADING(text)
3308#include "qemu-options.h"
3309#undef DEF
3310#undef DEFHEADING
3311#undef GEN_DOCS
3312};
3313
3314typedef struct QEMUOption {
3315 const char *name;
3316 int flags;
3317 int index;
3318} QEMUOption;
3319
3320static const QEMUOption qemu_options[] = {
3321 { "h", 0, QEMU_OPTION_h },
3322#define DEF(option, opt_arg, opt_enum, opt_help) \
3323 { option, opt_arg, opt_enum },
3324#define DEFHEADING(text)
3325#include "qemu-options.h"
3326#undef DEF
3327#undef DEFHEADING
3328#undef GEN_DOCS
3329 { NULL, 0, 0 },
3330};
3331
3332#ifdef HAS_AUDIO
3333struct soundhw soundhw[] = {
3334#ifdef HAS_AUDIO_CHOICE
3335#if defined(TARGET_I386) || defined(TARGET_MIPS)
3336 {
3337 "pcspk",
3338 "PC speaker",
3339 0,
3340 1,
3341 { .init_isa = pcspk_audio_init }
3342 },
3343#endif
3344
3345#ifdef CONFIG_SB16
3346 {
3347 "sb16",
3348 "Creative Sound Blaster 16",
3349 0,
3350 1,
3351 { .init_isa = SB16_init }
3352 },
3353#endif
3354
3355#ifdef CONFIG_CS4231A
3356 {
3357 "cs4231a",
3358 "CS4231A",
3359 0,
3360 1,
3361 { .init_isa = cs4231a_init }
3362 },
3363#endif
3364
3365#ifdef CONFIG_ADLIB
3366 {
3367 "adlib",
3368#ifdef HAS_YMF262
3369 "Yamaha YMF262 (OPL3)",
3370#else
3371 "Yamaha YM3812 (OPL2)",
3372#endif
3373 0,
3374 1,
3375 { .init_isa = Adlib_init }
3376 },
3377#endif
3378
3379#ifdef CONFIG_GUS
3380 {
3381 "gus",
3382 "Gravis Ultrasound GF1",
3383 0,
3384 1,
3385 { .init_isa = GUS_init }
3386 },
3387#endif
3388
3389#ifdef CONFIG_AC97
3390 {
3391 "ac97",
3392 "Intel 82801AA AC97 Audio",
3393 0,
3394 0,
3395 { .init_pci = ac97_init }
3396 },
3397#endif
3398
3399#ifdef CONFIG_ES1370
3400 {
3401 "es1370",
3402 "ENSONIQ AudioPCI ES1370",
3403 0,
3404 0,
3405 { .init_pci = es1370_init }
3406 },
3407#endif
3408
3409#endif /* HAS_AUDIO_CHOICE */
3410
3411 { NULL, NULL, 0, 0, { NULL } }
3412};
3413
3414static void select_soundhw (const char *optarg)
3415{
3416 struct soundhw *c;
3417
3418 if (*optarg == '?') {
3419 show_valid_cards:
3420
3421 printf ("Valid sound card names (comma separated):\n");
3422 for (c = soundhw; c->name; ++c) {
3423 printf ("%-11s %s\n", c->name, c->descr);
3424 }
3425 printf ("\n-soundhw all will enable all of the above\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003426 if (*optarg != '?') {
3427 PANIC("Unknown sound card name: %s", optarg);
3428 } else {
3429 QEMU_EXIT(0);
3430 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003431 }
3432 else {
3433 size_t l;
3434 const char *p;
3435 char *e;
3436 int bad_card = 0;
3437
3438 if (!strcmp (optarg, "all")) {
3439 for (c = soundhw; c->name; ++c) {
3440 c->enabled = 1;
3441 }
3442 return;
3443 }
3444
3445 p = optarg;
3446 while (*p) {
3447 e = strchr (p, ',');
3448 l = !e ? strlen (p) : (size_t) (e - p);
3449
3450 for (c = soundhw; c->name; ++c) {
3451 if (!strncmp (c->name, p, l)) {
3452 c->enabled = 1;
3453 break;
3454 }
3455 }
3456
3457 if (!c->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003458#ifndef CONFIG_ANDROID
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003459 if (l > 80) {
3460 fprintf (stderr,
3461 "Unknown sound card name (too big to show)\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003462 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003463 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3464 (int) l, p);
3465 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003466#endif // !CONFIG_ANDROID
3467 bad_card = 1;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003468 }
3469 p += l + (e != NULL);
3470 }
3471
3472 if (bad_card)
3473 goto show_valid_cards;
3474 }
3475}
3476#endif
3477
3478static void select_vgahw (const char *p)
3479{
3480 const char *opts;
3481
3482 cirrus_vga_enabled = 0;
3483 std_vga_enabled = 0;
3484 vmsvga_enabled = 0;
3485 xenfb_enabled = 0;
3486 if (strstart(p, "std", &opts)) {
3487 std_vga_enabled = 1;
3488 } else if (strstart(p, "cirrus", &opts)) {
3489 cirrus_vga_enabled = 1;
3490 } else if (strstart(p, "vmware", &opts)) {
3491 vmsvga_enabled = 1;
3492 } else if (strstart(p, "xenfb", &opts)) {
3493 xenfb_enabled = 1;
3494 } else if (!strstart(p, "none", &opts)) {
3495 invalid_vga:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003496 PANIC("Unknown vga type: %s", p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003497 }
3498 while (*opts) {
3499 const char *nextopt;
3500
3501 if (strstart(opts, ",retrace=", &nextopt)) {
3502 opts = nextopt;
3503 if (strstart(opts, "dumb", &nextopt))
3504 vga_retrace_method = VGA_RETRACE_DUMB;
3505 else if (strstart(opts, "precise", &nextopt))
3506 vga_retrace_method = VGA_RETRACE_PRECISE;
3507 else goto invalid_vga;
3508 } else goto invalid_vga;
3509 opts = nextopt;
3510 }
3511}
3512
3513#ifdef _WIN32
3514static BOOL WINAPI qemu_ctrl_handler(DWORD type)
3515{
3516 exit(STATUS_CONTROL_C_EXIT);
3517 return TRUE;
3518}
3519#endif
3520
3521int qemu_uuid_parse(const char *str, uint8_t *uuid)
3522{
3523 int ret;
3524
3525 if(strlen(str) != 36)
3526 return -1;
3527
3528 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
3529 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
3530 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
3531
3532 if(ret != 16)
3533 return -1;
3534
3535#ifdef TARGET_I386
3536 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
3537#endif
3538
3539 return 0;
3540}
3541
3542#define MAX_NET_CLIENTS 32
3543
3544#ifndef _WIN32
3545
3546static void termsig_handler(int signal)
3547{
3548 qemu_system_shutdown_request();
3549}
3550
3551static void sigchld_handler(int signal)
3552{
3553 waitpid(-1, NULL, WNOHANG);
3554}
3555
3556static void sighandler_setup(void)
3557{
3558 struct sigaction act;
3559
3560 memset(&act, 0, sizeof(act));
3561 act.sa_handler = termsig_handler;
3562 sigaction(SIGINT, &act, NULL);
3563 sigaction(SIGHUP, &act, NULL);
3564 sigaction(SIGTERM, &act, NULL);
3565
3566 act.sa_handler = sigchld_handler;
3567 act.sa_flags = SA_NOCLDSTOP;
3568 sigaction(SIGCHLD, &act, NULL);
3569}
3570
3571#endif
3572
3573#ifdef _WIN32
3574/* Look for support files in the same directory as the executable. */
3575static char *find_datadir(const char *argv0)
3576{
3577 char *p;
3578 char buf[MAX_PATH];
3579 DWORD len;
3580
3581 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
3582 if (len == 0) {
3583 return NULL;
3584 }
3585
3586 buf[len] = 0;
3587 p = buf + len - 1;
3588 while (p != buf && *p != '\\')
3589 p--;
3590 *p = 0;
3591 if (access(buf, R_OK) == 0) {
3592 return qemu_strdup(buf);
3593 }
3594 return NULL;
3595}
3596#else /* !_WIN32 */
3597
3598/* Find a likely location for support files using the location of the binary.
3599 For installed binaries this will be "$bindir/../share/qemu". When
3600 running from the build tree this will be "$bindir/../pc-bios". */
3601#define SHARE_SUFFIX "/share/qemu"
3602#define BUILD_SUFFIX "/pc-bios"
3603static char *find_datadir(const char *argv0)
3604{
3605 char *dir;
3606 char *p = NULL;
3607 char *res;
3608#ifdef PATH_MAX
3609 char buf[PATH_MAX];
3610#endif
3611 size_t max_len;
3612
3613#if defined(__linux__)
3614 {
3615 int len;
3616 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
3617 if (len > 0) {
3618 buf[len] = 0;
3619 p = buf;
3620 }
3621 }
3622#elif defined(__FreeBSD__)
3623 {
3624 int len;
3625 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
3626 if (len > 0) {
3627 buf[len] = 0;
3628 p = buf;
3629 }
3630 }
3631#endif
3632 /* If we don't have any way of figuring out the actual executable
3633 location then try argv[0]. */
3634 if (!p) {
3635#ifdef PATH_MAX
3636 p = buf;
3637#endif
3638 p = realpath(argv0, p);
3639 if (!p) {
3640 return NULL;
3641 }
3642 }
3643 dir = dirname(p);
3644 dir = dirname(dir);
3645
3646 max_len = strlen(dir) +
3647 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
3648 res = qemu_mallocz(max_len);
3649 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
3650 if (access(res, R_OK)) {
3651 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
3652 if (access(res, R_OK)) {
3653 qemu_free(res);
3654 res = NULL;
3655 }
3656 }
3657#ifndef PATH_MAX
3658 free(p);
3659#endif
3660 return res;
3661}
3662#undef SHARE_SUFFIX
3663#undef BUILD_SUFFIX
3664#endif
3665
3666char *qemu_find_file(int type, const char *name)
3667{
3668 int len;
3669 const char *subdir;
3670 char *buf;
3671
3672 /* If name contains path separators then try it as a straight path. */
3673 if ((strchr(name, '/') || strchr(name, '\\'))
3674 && access(name, R_OK) == 0) {
3675 return strdup(name);
3676 }
3677 switch (type) {
3678 case QEMU_FILE_TYPE_BIOS:
3679 subdir = "";
3680 break;
3681 case QEMU_FILE_TYPE_KEYMAP:
3682 subdir = "keymaps/";
3683 break;
3684 default:
3685 abort();
3686 }
3687 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
3688 buf = qemu_mallocz(len);
3689 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
3690 if (access(buf, R_OK)) {
3691 qemu_free(buf);
3692 return NULL;
3693 }
3694 return buf;
3695}
3696
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003697static int
3698add_dns_server( const char* server_name )
3699{
3700 SockAddress addr;
3701
3702 if (sock_address_init_resolve( &addr, server_name, 55, 0 ) < 0) {
3703 fprintf(stdout,
3704 "### WARNING: can't resolve DNS server name '%s'\n",
3705 server_name );
3706 return -1;
3707 }
3708
3709 fprintf(stderr,
3710 "DNS server name '%s' resolved to %s\n", server_name, sock_address_to_string(&addr) );
3711
3712 if ( slirp_add_dns_server( &addr ) < 0 ) {
3713 fprintf(stderr,
3714 "### WARNING: could not add DNS server '%s' to the network stack\n", server_name);
3715 return -1;
3716 }
3717 return 0;
3718}
3719
3720/* Appends a parameter to a string of parameters separated with space.
3721 * Pararm:
3722 * param_str String containing parameters separated with space.
3723 * param Parameter to append to the string.
3724 * size - Size (in characters) of the buffer addressed by param_str.
3725 */
3726static void
3727append_param(char* param_str, const char* arg, int size)
3728{
3729 if (*param_str) {
3730 strncat(param_str, " ", size);
3731 strncat(param_str, arg, size);
3732 } else {
3733 strncpy(param_str, arg, size);
3734 param_str[size - 1] = '\0';
3735 }
3736}
3737
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003738int main(int argc, char **argv, char **envp)
3739{
3740 const char *gdbstub_dev = NULL;
3741 uint32_t boot_devices_bitmap = 0;
3742 int i;
3743 int snapshot, linux_boot, net_boot;
David Turner6a9ef172010-09-09 22:54:36 +02003744 const char *icount_option = NULL;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003745 const char *initrd_filename;
3746 const char *kernel_filename, *kernel_cmdline;
3747 const char *boot_devices = "";
3748 DisplayState *ds;
3749 DisplayChangeListener *dcl;
3750 int cyls, heads, secs, translation;
3751 const char *net_clients[MAX_NET_CLIENTS];
3752 int nb_net_clients;
3753 const char *bt_opts[MAX_BT_CMDLINE];
3754 int nb_bt_opts;
3755 int hda_index;
3756 int optind;
3757 const char *r, *optarg;
3758 CharDriverState *monitor_hd = NULL;
3759 const char *monitor_device;
3760 const char *serial_devices[MAX_SERIAL_PORTS];
3761 int serial_device_index;
3762 const char *parallel_devices[MAX_PARALLEL_PORTS];
3763 int parallel_device_index;
3764 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
3765 int virtio_console_index;
3766 const char *loadvm = NULL;
3767 QEMUMachine *machine;
3768 const char *cpu_model;
3769 const char *usb_devices[MAX_USB_CMDLINE];
3770 int usb_devices_index;
3771#ifndef _WIN32
3772 int fds[2];
3773#endif
3774 int tb_size;
3775 const char *pid_file = NULL;
3776 const char *incoming = NULL;
3777#ifndef _WIN32
3778 int fd = 0;
3779 struct passwd *pwd = NULL;
3780 const char *chroot_dir = NULL;
3781 const char *run_as = NULL;
3782#endif
3783 CPUState *env;
3784 int show_vnc_port = 0;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07003785#ifdef CONFIG_STANDALONE_CORE
3786 IniFile* hw_ini = NULL;
3787#endif // CONFIG_STANDALONE_CORE
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003788 /* Container for the kernel initialization parameters collected in this
3789 * routine. */
3790 char kernel_cmdline_append[1024];
3791 /* Combines kernel initialization parameters passed from the UI with
3792 * the parameters collected in this routine. */
3793 char kernel_cmdline_full[1024];
3794 char tmp_str[1024];
3795 int dns_count = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003796
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003797 /* Initialize sockets before anything else, so we can properly report
3798 * initialization failures back to the UI. */
3799#ifdef _WIN32
3800 socket_init();
3801#endif
3802
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07003803 init_clocks();
3804
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003805 qemu_cache_utils_init(envp);
3806
David 'Digit' Turnera5d41202010-05-10 18:37:10 -07003807 QLIST_INIT (&vm_change_state_head);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003808#ifndef _WIN32
3809 {
3810 struct sigaction act;
3811 sigfillset(&act.sa_mask);
3812 act.sa_flags = 0;
3813 act.sa_handler = SIG_IGN;
3814 sigaction(SIGPIPE, &act, NULL);
3815 }
3816#else
3817 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
3818 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3819 QEMU to run on a single CPU */
3820 {
3821 HANDLE h;
3822 DWORD mask, smask;
3823 int i;
3824 h = GetCurrentProcess();
3825 if (GetProcessAffinityMask(h, &mask, &smask)) {
3826 for(i = 0; i < 32; i++) {
3827 if (mask & (1 << i))
3828 break;
3829 }
3830 if (i != 32) {
3831 mask = 1 << i;
3832 SetProcessAffinityMask(h, mask);
3833 }
3834 }
3835 }
3836#endif
3837
3838 module_call_init(MODULE_INIT_MACHINE);
3839 machine = find_default_machine();
3840 cpu_model = NULL;
3841 initrd_filename = NULL;
3842 ram_size = 0;
3843 snapshot = 0;
3844 kernel_filename = NULL;
3845 kernel_cmdline = "";
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07003846 kernel_cmdline_append[0] = '\0';
3847 kernel_cmdline_full[0] = '\0';
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003848 cyls = heads = secs = 0;
3849 translation = BIOS_ATA_TRANSLATION_AUTO;
3850 monitor_device = "vc:80Cx24C";
3851
3852 serial_devices[0] = "vc:80Cx24C";
3853 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3854 serial_devices[i] = NULL;
3855 serial_device_index = 0;
3856
3857 parallel_devices[0] = "vc:80Cx24C";
3858 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3859 parallel_devices[i] = NULL;
3860 parallel_device_index = 0;
3861
3862 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
3863 virtio_consoles[i] = NULL;
3864 virtio_console_index = 0;
3865
3866 for (i = 0; i < MAX_NODES; i++) {
3867 node_mem[i] = 0;
3868 node_cpumask[i] = 0;
3869 }
3870
3871 usb_devices_index = 0;
3872
3873 nb_net_clients = 0;
3874 nb_bt_opts = 0;
3875 nb_drives = 0;
3876 nb_drives_opt = 0;
3877 nb_numa_nodes = 0;
3878 hda_index = -1;
3879
3880 nb_nics = 0;
3881
3882 tb_size = 0;
3883 autostart= 1;
3884
3885 register_watchdogs();
3886
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07003887 /* Initialize boot properties. */
3888 boot_property_init_service();
3889
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003890 optind = 1;
3891 for(;;) {
3892 if (optind >= argc)
3893 break;
3894 r = argv[optind];
3895 if (r[0] != '-') {
3896 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
3897 } else {
3898 const QEMUOption *popt;
3899
3900 optind++;
3901 /* Treat --foo the same as -foo. */
3902 if (r[1] == '-')
3903 r++;
3904 popt = qemu_options;
3905 for(;;) {
3906 if (!popt->name) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003907 PANIC("%s: invalid option -- '%s'",
3908 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003909 }
3910 if (!strcmp(popt->name, r + 1))
3911 break;
3912 popt++;
3913 }
3914 if (popt->flags & HAS_ARG) {
3915 if (optind >= argc) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003916 PANIC("%s: option '%s' requires an argument",
3917 argv[0], r);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003918 }
3919 optarg = argv[optind++];
3920 } else {
3921 optarg = NULL;
3922 }
3923
3924 switch(popt->index) {
3925 case QEMU_OPTION_M:
3926 machine = find_machine(optarg);
3927 if (!machine) {
3928 QEMUMachine *m;
3929 printf("Supported machines are:\n");
3930 for(m = first_machine; m != NULL; m = m->next) {
3931 printf("%-10s %s%s\n",
3932 m->name, m->desc,
3933 m->is_default ? " (default)" : "");
3934 }
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003935 if (*optarg != '?') {
3936 PANIC("Invalid machine parameter: %s",
3937 optarg);
3938 } else {
3939 QEMU_EXIT(0);
3940 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003941 }
3942 break;
3943 case QEMU_OPTION_cpu:
3944 /* hw initialization will check this */
3945 if (*optarg == '?') {
3946/* XXX: implement xxx_cpu_list for targets that still miss it */
3947#if defined(cpu_list)
3948 cpu_list(stdout, &fprintf);
3949#endif
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07003950 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07003951 } else {
3952 cpu_model = optarg;
3953 }
3954 break;
3955 case QEMU_OPTION_initrd:
3956 initrd_filename = optarg;
3957 break;
3958 case QEMU_OPTION_hda:
3959 if (cyls == 0)
3960 hda_index = drive_add(optarg, HD_ALIAS, 0);
3961 else
3962 hda_index = drive_add(optarg, HD_ALIAS
3963 ",cyls=%d,heads=%d,secs=%d%s",
3964 0, cyls, heads, secs,
3965 translation == BIOS_ATA_TRANSLATION_LBA ?
3966 ",trans=lba" :
3967 translation == BIOS_ATA_TRANSLATION_NONE ?
3968 ",trans=none" : "");
3969 break;
3970 case QEMU_OPTION_hdb:
3971 case QEMU_OPTION_hdc:
3972 case QEMU_OPTION_hdd:
3973 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
3974 break;
3975 case QEMU_OPTION_drive:
3976 drive_add(NULL, "%s", optarg);
3977 break;
3978 case QEMU_OPTION_mtdblock:
3979 drive_add(optarg, MTD_ALIAS);
3980 break;
3981 case QEMU_OPTION_sd:
3982 drive_add(optarg, SD_ALIAS);
3983 break;
3984 case QEMU_OPTION_pflash:
3985 drive_add(optarg, PFLASH_ALIAS);
3986 break;
3987 case QEMU_OPTION_snapshot:
3988 snapshot = 1;
3989 break;
3990 case QEMU_OPTION_hdachs:
3991 {
3992 const char *p;
3993 p = optarg;
3994 cyls = strtol(p, (char **)&p, 0);
3995 if (cyls < 1 || cyls > 16383)
3996 goto chs_fail;
3997 if (*p != ',')
3998 goto chs_fail;
3999 p++;
4000 heads = strtol(p, (char **)&p, 0);
4001 if (heads < 1 || heads > 16)
4002 goto chs_fail;
4003 if (*p != ',')
4004 goto chs_fail;
4005 p++;
4006 secs = strtol(p, (char **)&p, 0);
4007 if (secs < 1 || secs > 63)
4008 goto chs_fail;
4009 if (*p == ',') {
4010 p++;
4011 if (!strcmp(p, "none"))
4012 translation = BIOS_ATA_TRANSLATION_NONE;
4013 else if (!strcmp(p, "lba"))
4014 translation = BIOS_ATA_TRANSLATION_LBA;
4015 else if (!strcmp(p, "auto"))
4016 translation = BIOS_ATA_TRANSLATION_AUTO;
4017 else
4018 goto chs_fail;
4019 } else if (*p != '\0') {
4020 chs_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004021 PANIC("qemu: invalid physical CHS format");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004022 }
4023 if (hda_index != -1)
4024 snprintf(drives_opt[hda_index].opt,
4025 sizeof(drives_opt[hda_index].opt),
4026 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4027 0, cyls, heads, secs,
4028 translation == BIOS_ATA_TRANSLATION_LBA ?
4029 ",trans=lba" :
4030 translation == BIOS_ATA_TRANSLATION_NONE ?
4031 ",trans=none" : "");
4032 }
4033 break;
4034 case QEMU_OPTION_numa:
4035 if (nb_numa_nodes >= MAX_NODES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004036 PANIC("qemu: too many NUMA nodes");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004037 }
4038 numa_add(optarg);
4039 break;
4040 case QEMU_OPTION_nographic:
4041 display_type = DT_NOGRAPHIC;
4042 break;
4043#ifdef CONFIG_CURSES
4044 case QEMU_OPTION_curses:
4045 display_type = DT_CURSES;
4046 break;
4047#endif
4048 case QEMU_OPTION_portrait:
4049 graphic_rotate = 1;
4050 break;
4051 case QEMU_OPTION_kernel:
4052 kernel_filename = optarg;
4053 break;
4054 case QEMU_OPTION_append:
4055 kernel_cmdline = optarg;
4056 break;
4057 case QEMU_OPTION_cdrom:
4058 drive_add(optarg, CDROM_ALIAS);
4059 break;
4060 case QEMU_OPTION_boot:
4061 boot_devices = optarg;
4062 /* We just do some generic consistency checks */
4063 {
4064 /* Could easily be extended to 64 devices if needed */
4065 const char *p;
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004066
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004067 boot_devices_bitmap = 0;
4068 for (p = boot_devices; *p != '\0'; p++) {
4069 /* Allowed boot devices are:
4070 * a b : floppy disk drives
4071 * c ... f : IDE disk drives
4072 * g ... m : machine implementation dependant drives
4073 * n ... p : network devices
4074 * It's up to each machine implementation to check
4075 * if the given boot devices match the actual hardware
4076 * implementation and firmware features.
4077 */
4078 if (*p < 'a' || *p > 'q') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004079 PANIC("Invalid boot device '%c'", *p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004080 }
4081 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004082 PANIC(
4083 "Boot device '%c' was given twice",*p);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004084 }
4085 boot_devices_bitmap |= 1 << (*p - 'a');
4086 }
4087 }
4088 break;
4089 case QEMU_OPTION_fda:
4090 case QEMU_OPTION_fdb:
4091 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
4092 break;
4093#ifdef TARGET_I386
4094 case QEMU_OPTION_no_fd_bootchk:
4095 fd_bootchk = 0;
4096 break;
4097#endif
4098 case QEMU_OPTION_net:
4099 if (nb_net_clients >= MAX_NET_CLIENTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004100 PANIC("qemu: too many network clients");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004101 }
4102 net_clients[nb_net_clients] = optarg;
4103 nb_net_clients++;
4104 break;
4105#ifdef CONFIG_SLIRP
4106 case QEMU_OPTION_tftp:
4107 tftp_prefix = optarg;
4108 break;
4109 case QEMU_OPTION_bootp:
4110 bootp_filename = optarg;
4111 break;
4112#if 0 /* ANDROID disabled */
4113#ifndef _WIN32
4114 case QEMU_OPTION_smb:
4115 net_slirp_smb(optarg);
4116 break;
4117#endif
4118#endif /* ANDROID */
4119 case QEMU_OPTION_redir:
4120 net_slirp_redir(NULL, optarg, NULL);
4121 break;
4122#endif
4123 case QEMU_OPTION_bt:
4124 if (nb_bt_opts >= MAX_BT_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004125 PANIC("qemu: too many bluetooth options");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004126 }
4127 bt_opts[nb_bt_opts++] = optarg;
4128 break;
4129#ifdef HAS_AUDIO
4130 case QEMU_OPTION_audio_help:
4131 AUD_help ();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004132 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004133 break;
4134 case QEMU_OPTION_soundhw:
4135 select_soundhw (optarg);
4136 break;
4137#endif
4138 case QEMU_OPTION_h:
4139 qemu_help(0);
4140 break;
4141 case QEMU_OPTION_version:
4142 version();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004143 QEMU_EXIT(0);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004144 break;
4145 case QEMU_OPTION_m: {
4146 uint64_t value;
4147 char *ptr;
4148
4149 value = strtoul(optarg, &ptr, 10);
4150 switch (*ptr) {
4151 case 0: case 'M': case 'm':
4152 value <<= 20;
4153 break;
4154 case 'G': case 'g':
4155 value <<= 30;
4156 break;
4157 default:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004158 PANIC("qemu: invalid ram size: %s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004159 }
4160
4161 /* On 32-bit hosts, QEMU is limited by virtual address space */
4162 if (value > (2047 << 20)
4163#ifndef CONFIG_KQEMU
4164 && HOST_LONG_BITS == 32
4165#endif
4166 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004167 PANIC("qemu: at most 2047 MB RAM can be simulated");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004168 }
4169 if (value != (uint64_t)(ram_addr_t)value) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004170 PANIC("qemu: ram size too large");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004171 }
4172 ram_size = value;
4173 break;
4174 }
4175 case QEMU_OPTION_d:
4176 {
4177 int mask;
4178 const CPULogItem *item;
4179
4180 mask = cpu_str_to_log_mask(optarg);
4181 if (!mask) {
4182 printf("Log items (comma separated):\n");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004183 for(item = cpu_log_items; item->mask != 0; item++) {
4184 printf("%-10s %s\n", item->name, item->help);
4185 }
4186 PANIC("Invalid parameter -d=%s", optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004187 }
4188 cpu_set_log(mask);
4189 }
4190 break;
4191 case QEMU_OPTION_s:
4192 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
4193 break;
4194 case QEMU_OPTION_gdb:
4195 gdbstub_dev = optarg;
4196 break;
4197 case QEMU_OPTION_L:
4198 data_dir = optarg;
4199 break;
4200 case QEMU_OPTION_bios:
4201 bios_name = optarg;
4202 break;
4203 case QEMU_OPTION_singlestep:
4204 singlestep = 1;
4205 break;
4206 case QEMU_OPTION_S:
4207#if 0 /* ANDROID */
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004208 PANIC("Sorry, stopped launch is not supported in the Android emulator" );
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004209#endif
4210 autostart = 0;
4211 break;
4212#ifndef _WIN32
4213 case QEMU_OPTION_k:
4214 keyboard_layout = optarg;
4215 break;
4216#endif
4217 case QEMU_OPTION_localtime:
4218 rtc_utc = 0;
4219 break;
4220 case QEMU_OPTION_vga:
4221 select_vgahw (optarg);
4222 break;
4223#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4224 case QEMU_OPTION_g:
4225 {
4226 const char *p;
4227 int w, h, depth;
4228 p = optarg;
4229 w = strtol(p, (char **)&p, 10);
4230 if (w <= 0) {
4231 graphic_error:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004232 PANIC("qemu: invalid resolution or depth");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004233 }
4234 if (*p != 'x')
4235 goto graphic_error;
4236 p++;
4237 h = strtol(p, (char **)&p, 10);
4238 if (h <= 0)
4239 goto graphic_error;
4240 if (*p == 'x') {
4241 p++;
4242 depth = strtol(p, (char **)&p, 10);
4243 if (depth != 8 && depth != 15 && depth != 16 &&
4244 depth != 24 && depth != 32)
4245 goto graphic_error;
4246 } else if (*p == '\0') {
4247 depth = graphic_depth;
4248 } else {
4249 goto graphic_error;
4250 }
4251
4252 graphic_width = w;
4253 graphic_height = h;
4254 graphic_depth = depth;
4255 }
4256 break;
4257#endif
4258 case QEMU_OPTION_echr:
4259 {
4260 char *r;
4261 term_escape_char = strtol(optarg, &r, 0);
4262 if (r == optarg)
4263 printf("Bad argument to echr\n");
4264 break;
4265 }
4266 case QEMU_OPTION_monitor:
4267 monitor_device = optarg;
4268 break;
4269 case QEMU_OPTION_serial:
4270 if (serial_device_index >= MAX_SERIAL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004271 PANIC("qemu: too many serial ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004272 }
4273 serial_devices[serial_device_index] = optarg;
4274 serial_device_index++;
4275 break;
4276 case QEMU_OPTION_watchdog:
4277 i = select_watchdog(optarg);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004278 if (i > 0) {
4279 if (i == 1) {
4280 PANIC("Invalid watchdog parameter: %s",
4281 optarg);
4282 } else {
4283 QEMU_EXIT(0);
4284 }
4285 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004286 break;
4287 case QEMU_OPTION_watchdog_action:
4288 if (select_watchdog_action(optarg) == -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004289 PANIC("Unknown -watchdog-action parameter");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004290 }
4291 break;
4292 case QEMU_OPTION_virtiocon:
4293 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004294 PANIC("qemu: too many virtio consoles");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004295 }
4296 virtio_consoles[virtio_console_index] = optarg;
4297 virtio_console_index++;
4298 break;
4299 case QEMU_OPTION_parallel:
4300 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004301 PANIC("qemu: too many parallel ports");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004302 }
4303 parallel_devices[parallel_device_index] = optarg;
4304 parallel_device_index++;
4305 break;
Tim Baverstock24204cc2010-11-25 11:37:43 +00004306 case QEMU_OPTION_loadvm:
4307 loadvm = optarg;
4308 break;
4309#if CONFIG_ANDROID_SNAPSHOTS
4310 case QEMU_OPTION_savevm_on_exit:
4311 savevm_on_exit = optarg;
4312 break;
4313#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004314 case QEMU_OPTION_full_screen:
4315 full_screen = 1;
4316 break;
4317#ifdef CONFIG_SDL
4318 case QEMU_OPTION_no_frame:
4319 no_frame = 1;
4320 break;
4321 case QEMU_OPTION_alt_grab:
4322 alt_grab = 1;
4323 break;
4324 case QEMU_OPTION_no_quit:
4325 no_quit = 1;
4326 break;
4327 case QEMU_OPTION_sdl:
4328 display_type = DT_SDL;
4329 break;
4330#endif
4331 case QEMU_OPTION_pidfile:
4332 pid_file = optarg;
4333 break;
4334#ifdef TARGET_I386
4335 case QEMU_OPTION_win2k_hack:
4336 win2k_install_hack = 1;
4337 break;
4338 case QEMU_OPTION_rtc_td_hack:
4339 rtc_td_hack = 1;
4340 break;
4341 case QEMU_OPTION_acpitable:
4342 if(acpi_table_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004343 PANIC("Wrong acpi table provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004344 }
4345 break;
4346 case QEMU_OPTION_smbios:
4347 if(smbios_entry_add(optarg) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004348 PANIC("Wrong smbios provided");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004349 }
4350 break;
4351#endif
4352#ifdef CONFIG_KQEMU
4353 case QEMU_OPTION_no_kqemu:
4354 kqemu_allowed = 0;
4355 break;
4356 case QEMU_OPTION_kernel_kqemu:
4357 kqemu_allowed = 2;
4358 break;
4359#endif
4360#ifdef CONFIG_KVM
4361 case QEMU_OPTION_enable_kvm:
4362 kvm_allowed = 1;
4363#ifdef CONFIG_KQEMU
4364 kqemu_allowed = 0;
4365#endif
4366 break;
4367#endif
4368 case QEMU_OPTION_usb:
4369 usb_enabled = 1;
4370 break;
4371 case QEMU_OPTION_usbdevice:
4372 usb_enabled = 1;
4373 if (usb_devices_index >= MAX_USB_CMDLINE) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004374 PANIC("Too many USB devices");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004375 }
4376 usb_devices[usb_devices_index] = optarg;
4377 usb_devices_index++;
4378 break;
4379 case QEMU_OPTION_smp:
4380 smp_cpus = atoi(optarg);
4381 if (smp_cpus < 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004382 PANIC("Invalid number of CPUs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004383 }
4384 break;
4385 case QEMU_OPTION_vnc:
4386 display_type = DT_VNC;
4387 vnc_display = optarg;
4388 break;
4389#ifdef TARGET_I386
4390 case QEMU_OPTION_no_acpi:
4391 acpi_enabled = 0;
4392 break;
4393 case QEMU_OPTION_no_hpet:
4394 no_hpet = 1;
4395 break;
4396 case QEMU_OPTION_no_virtio_balloon:
4397 no_virtio_balloon = 1;
4398 break;
4399#endif
4400 case QEMU_OPTION_no_reboot:
4401 no_reboot = 1;
4402 break;
4403 case QEMU_OPTION_no_shutdown:
4404 no_shutdown = 1;
4405 break;
4406 case QEMU_OPTION_show_cursor:
4407 cursor_hide = 0;
4408 break;
4409 case QEMU_OPTION_uuid:
4410 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004411 PANIC("Fail to parse UUID string. Wrong format.");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004412 }
4413 break;
4414#ifndef _WIN32
4415 case QEMU_OPTION_daemonize:
4416 daemonize = 1;
4417 break;
4418#endif
4419 case QEMU_OPTION_option_rom:
4420 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004421 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004422 }
4423 option_rom[nb_option_roms] = optarg;
4424 nb_option_roms++;
4425 break;
4426#if defined(TARGET_ARM) || defined(TARGET_M68K)
4427 case QEMU_OPTION_semihosting:
4428 semihosting_enabled = 1;
4429 break;
4430#endif
4431 case QEMU_OPTION_name:
4432 qemu_name = optarg;
4433 break;
4434#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4435 case QEMU_OPTION_prom_env:
4436 if (nb_prom_envs >= MAX_PROM_ENVS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004437 PANIC("Too many prom variables");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004438 }
4439 prom_envs[nb_prom_envs] = optarg;
4440 nb_prom_envs++;
4441 break;
4442#endif
4443#ifdef TARGET_ARM
4444 case QEMU_OPTION_old_param:
4445 old_param = 1;
4446 break;
4447#endif
4448 case QEMU_OPTION_clock:
4449 configure_alarms(optarg);
4450 break;
4451 case QEMU_OPTION_startdate:
4452 {
4453 struct tm tm;
David 'Digit' Turnerdc468202010-10-27 02:34:46 +02004454 time_t rtc_start_date = 0;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004455 if (!strcmp(optarg, "now")) {
4456 rtc_date_offset = -1;
4457 } else {
4458 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
4459 &tm.tm_year,
4460 &tm.tm_mon,
4461 &tm.tm_mday,
4462 &tm.tm_hour,
4463 &tm.tm_min,
4464 &tm.tm_sec) == 6) {
4465 /* OK */
4466 } else if (sscanf(optarg, "%d-%d-%d",
4467 &tm.tm_year,
4468 &tm.tm_mon,
4469 &tm.tm_mday) == 3) {
4470 tm.tm_hour = 0;
4471 tm.tm_min = 0;
4472 tm.tm_sec = 0;
4473 } else {
4474 goto date_fail;
4475 }
4476 tm.tm_year -= 1900;
4477 tm.tm_mon--;
4478 rtc_start_date = mktimegm(&tm);
4479 if (rtc_start_date == -1) {
4480 date_fail:
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004481 PANIC("Invalid date format. Valid format are:\n"
4482 "'now' or '2006-06-17T16:01:21' or '2006-06-17'");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004483 }
4484 rtc_date_offset = time(NULL) - rtc_start_date;
4485 }
4486 }
4487 break;
4488 case QEMU_OPTION_tb_size:
4489 tb_size = strtol(optarg, NULL, 0);
4490 if (tb_size < 0)
4491 tb_size = 0;
4492 break;
4493 case QEMU_OPTION_icount:
David Turner6a9ef172010-09-09 22:54:36 +02004494 icount_option = optarg;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004495 break;
4496 case QEMU_OPTION_incoming:
4497 incoming = optarg;
4498 break;
4499#ifndef _WIN32
4500 case QEMU_OPTION_chroot:
4501 chroot_dir = optarg;
4502 break;
4503 case QEMU_OPTION_runas:
4504 run_as = optarg;
4505 break;
4506#endif
4507#ifdef CONFIG_XEN
4508 case QEMU_OPTION_xen_domid:
4509 xen_domid = atoi(optarg);
4510 break;
4511 case QEMU_OPTION_xen_create:
4512 xen_mode = XEN_CREATE;
4513 break;
4514 case QEMU_OPTION_xen_attach:
4515 xen_mode = XEN_ATTACH;
4516 break;
4517#endif
4518
4519
4520 case QEMU_OPTION_mic:
4521 audio_input_source = (char*)optarg;
4522 break;
4523#ifdef CONFIG_TRACE
David 'Digit' Turnera577fca2009-10-15 18:18:09 -07004524 case QEMU_OPTION_trace:
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004525 trace_filename = optarg;
4526 tracing = 1;
4527 break;
4528#if 0
4529 case QEMU_OPTION_trace_miss:
4530 trace_cache_miss = 1;
4531 break;
4532 case QEMU_OPTION_trace_addr:
4533 trace_all_addr = 1;
4534 break;
4535#endif
4536 case QEMU_OPTION_tracing:
4537 if (strcmp(optarg, "off") == 0)
4538 tracing = 0;
4539 else if (strcmp(optarg, "on") == 0 && trace_filename)
4540 tracing = 1;
4541 else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004542 PANIC("Unexpected option to -tracing ('%s')",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004543 optarg);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004544 }
4545 break;
4546#if 0
4547 case QEMU_OPTION_dcache_load_miss:
4548 dcache_load_miss_penalty = atoi(optarg);
4549 break;
4550 case QEMU_OPTION_dcache_store_miss:
4551 dcache_store_miss_penalty = atoi(optarg);
4552 break;
4553#endif
4554#endif
4555#ifdef CONFIG_NAND
4556 case QEMU_OPTION_nand:
4557 nand_add_dev(optarg);
4558 break;
4559#endif
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07004560 case QEMU_OPTION_android_ports:
4561 android_op_ports = (char*)optarg;
4562 break;
4563
4564 case QEMU_OPTION_android_port:
4565 android_op_port = (char*)optarg;
4566 break;
4567
4568 case QEMU_OPTION_android_report_console:
4569 android_op_report_console = (char*)optarg;
4570 break;
4571
4572 case QEMU_OPTION_http_proxy:
4573 op_http_proxy = (char*)optarg;
4574 break;
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004575
4576 case QEMU_OPTION_charmap:
4577 op_charmap_file = (char*)optarg;
4578 break;
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07004579
4580 case QEMU_OPTION_android_gui:
4581 android_op_gui = (char*)optarg;
4582 break;
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004583
4584 case QEMU_OPTION_android_hw:
4585 android_op_hwini = (char*)optarg;
4586 break;
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004587
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004588 case QEMU_OPTION_dns_server:
4589 android_op_dns_server = (char*)optarg;
4590 break;
4591
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004592 case QEMU_OPTION_radio:
4593 android_op_radio = (char*)optarg;
4594 break;
4595
4596 case QEMU_OPTION_gps:
4597 android_op_gps = (char*)optarg;
4598 break;
4599
4600 case QEMU_OPTION_audio:
4601 android_op_audio = (char*)optarg;
4602 break;
4603
4604 case QEMU_OPTION_audio_in:
4605 android_op_audio_in = (char*)optarg;
4606 break;
4607
4608 case QEMU_OPTION_audio_out:
4609 android_op_audio_out = (char*)optarg;
4610 break;
4611
4612 case QEMU_OPTION_cpu_delay:
4613 android_op_cpu_delay = (char*)optarg;
4614 break;
4615
Vladimir Chtchetkine13f3b6c2010-08-25 09:49:25 -07004616 case QEMU_OPTION_show_kernel:
4617 android_kmsg_init(ANDROID_KMSG_PRINT_MESSAGES);
4618 break;
4619
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004620#ifdef CONFIG_NAND_LIMITS
4621 case QEMU_OPTION_nand_limits:
4622 android_op_nand_limits = (char*)optarg;
4623 break;
4624#endif // CONFIG_NAND_LIMITS
4625
4626 case QEMU_OPTION_netspeed:
4627 android_op_netspeed = (char*)optarg;
4628 break;
4629
4630 case QEMU_OPTION_netdelay:
4631 android_op_netdelay = (char*)optarg;
4632 break;
4633
4634 case QEMU_OPTION_netfast:
4635 android_op_netfast = 1;
4636 break;
4637
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004638 case QEMU_OPTION_tcpdump:
4639 android_op_tcpdump = (char*)optarg;
4640 break;
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004641
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004642 case QEMU_OPTION_boot_property:
4643 boot_property_parse_option((char*)optarg);
4644 break;
4645
4646 case QEMU_OPTION_lcd_density:
4647 android_op_lcd_density = (char*)optarg;
4648 break;
4649
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004650 case QEMU_OPTION_ui_port:
4651 android_op_ui_port = (char*)optarg;
4652 break;
4653
4654 case QEMU_OPTION_ui_settings:
4655 android_op_ui_settings = (char*)optarg;
4656 break;
4657
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004658#ifdef CONFIG_MEMCHECK
4659 case QEMU_OPTION_android_memcheck:
4660 android_op_memcheck = (char*)optarg;
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004661 snprintf(tmp_str, sizeof(tmp_str), "memcheck=%s",
4662 android_op_memcheck);
4663 tmp_str[sizeof(tmp_str) - 1] = '\0';
4664 /* This will set ro.kernel.memcheck system property
4665 * to memcheck's tracing flags. */
4666 append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004667 break;
4668#endif // CONFIG_MEMCHECK
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004669 }
4670 }
4671 }
4672
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004673 /* Initialize character map. */
4674 if (android_charmap_setup(op_charmap_file)) {
4675 if (op_charmap_file) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004676 PANIC(
4677 "Unable to initialize character map from file %s.",
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004678 op_charmap_file);
4679 } else {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004680 PANIC(
4681 "Unable to initialize default character map.");
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004682 }
Vladimir Chtchetkine43552dc2010-07-22 11:23:19 -07004683 }
4684
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004685 /* If no data_dir is specified then try to find it relative to the
4686 executable path. */
4687 if (!data_dir) {
4688 data_dir = find_datadir(argv[0]);
4689 }
4690 /* If all else fails use the install patch specified when building. */
4691 if (!data_dir) {
4692 data_dir = CONFIG_QEMU_SHAREDIR;
4693 }
4694
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004695#ifdef CONFIG_STANDALONE_CORE
4696 /* Initialize hardware configuration. */
4697 if (android_op_hwini) {
4698 hw_ini = iniFile_newFromFile(android_op_hwini);
4699 if (hw_ini == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004700 PANIC("Could not find %s file.", android_op_hwini);
Vladimir Chtchetkine074d1f92010-08-06 09:29:50 -07004701 }
4702 } else {
4703 hw_ini = iniFile_newFromMemory("", 0);
4704 }
4705
4706 androidHwConfig_read(android_hw, hw_ini);
4707 iniFile_free(hw_ini);
4708#endif // CONFIG_STANDALONE_CORE
4709
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004710#ifdef CONFIG_NAND_LIMITS
4711 /* Init nand stuff. */
4712 if (android_op_nand_limits) {
4713 parse_nand_limits(android_op_nand_limits);
4714 }
4715#endif // CONFIG_NAND_LIMITS
4716
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004717 /* Set the VM's max heap size, passed as a boot property */
4718 if (android_hw->vm_heapSize > 0) {
4719 char tmp[64];
4720 snprintf(tmp, sizeof(tmp), "%dm", android_hw->vm_heapSize);
4721 boot_property_add("dalvik.vm.heapsize",tmp);
4722 }
4723
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004724 /* Initialize net speed and delays stuff. */
4725 if (android_parse_network_speed(android_op_netspeed) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004726 PANIC("invalid -netspeed parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004727 android_op_netspeed);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004728 }
4729
4730 if ( android_parse_network_latency(android_op_netdelay) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004731 PANIC("invalid -netdelay parameter '%s'",
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004732 android_op_netdelay);
Vladimir Chtchetkinee1316862010-08-26 09:03:54 -07004733 }
4734
4735 if (android_op_netfast) {
4736 qemu_net_download_speed = 0;
4737 qemu_net_upload_speed = 0;
4738 qemu_net_min_latency = 0;
4739 qemu_net_max_latency = 0;
4740 }
4741
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004742 /* Initialize LCD density */
4743 if (android_op_lcd_density) {
4744 char* end;
4745 long density = strtol(android_op_lcd_density, &end, 0);
4746 if (end == NULL || *end || density < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004747 PANIC("option -lcd-density must be a positive integer");
Vladimir Chtchetkineb25bf2a2010-09-08 11:09:23 -07004748 }
4749 hwLcd_setBootProperty(density);
4750 }
4751
Vladimir Chtchetkine318f17a2010-08-27 09:09:45 -07004752 /* Initialize TCP dump */
4753 if (android_op_tcpdump) {
4754 if (qemu_tcpdump_start(android_op_tcpdump) < 0) {
4755 fprintf(stdout, "could not start packet capture: %s\n", strerror(errno));
4756 }
4757 }
4758
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004759 /* Initialize modem */
4760 if (android_op_radio) {
4761 CharDriverState* cs = qemu_chr_open("radio", android_op_radio, NULL);
4762 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004763 PANIC("unsupported character device specification: %s\n"
4764 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004765 android_op_radio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004766 }
4767 android_qemud_set_channel( ANDROID_QEMUD_GSM, cs);
4768 } else if (android_hw->hw_gsmModem != 0 ) {
4769 if ( android_qemud_get_channel( ANDROID_QEMUD_GSM, &android_modem_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004770 PANIC("could not initialize qemud 'gsm' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004771 }
4772 }
4773
4774 /* Initialize GPS */
4775 if (android_op_gps) {
4776 CharDriverState* cs = qemu_chr_open("gps", android_op_gps, NULL);
4777 if (cs == NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004778 PANIC("unsupported character device specification: %s\n"
4779 "used -help-char-devices for list of available formats",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004780 android_op_gps);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004781 }
4782 android_qemud_set_channel( ANDROID_QEMUD_GPS, cs);
4783 } else if (android_hw->hw_gps != 0) {
4784 if ( android_qemud_get_channel( "gps", &android_gps_cs ) < 0 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004785 PANIC("could not initialize qemud 'gps' channel");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004786 }
4787 }
4788
4789 /* Initialize audio. */
4790 if (android_op_audio) {
4791 if (android_op_audio_in || android_op_audio_out) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004792 PANIC("you can't use -audio with -audio-in or -audio-out");
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004793 }
4794 if ( !audio_check_backend_name( 0, android_op_audio ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004795 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004796 android_op_audio);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004797 }
4798 android_op_audio_out = android_op_audio;
4799 android_op_audio_in = android_op_audio;
4800
4801 if ( !audio_check_backend_name( 1, android_op_audio ) ) {
4802 fprintf(stdout,
4803 "emulator: warning: '%s' is not a valid audio input backend. audio record disabled\n",
4804 android_op_audio);
4805 android_op_audio_in = "none";
4806 }
4807 }
4808
4809 if (android_op_audio_in) {
4810 static char env[64]; /* note: putenv needs a static unique string buffer */
4811 if ( !audio_check_backend_name( 1, android_op_audio_in ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004812 PANIC("'%s' is not a valid audio input backend. see -help-audio-in",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004813 android_op_audio_in);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004814 }
4815 bufprint( env, env+sizeof(env), "QEMU_AUDIO_IN_DRV=%s", android_op_audio_in );
4816 putenv( env );
4817
4818 if (!android_hw->hw_audioInput) {
4819 fprintf(stdout, "Emulated hardware doesn't have audio input.\n");
4820 }
4821 }
4822 if (android_op_audio_out) {
4823 static char env[64]; /* note: putenv needs a static unique string buffer */
4824 if ( !audio_check_backend_name( 0, android_op_audio_out ) ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004825 PANIC("'%s' is not a valid audio output backend. see -help-audio-out",
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004826 android_op_audio_out);
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004827 }
4828 bufprint( env, env+sizeof(env), "QEMU_AUDIO_OUT_DRV=%s", android_op_audio_out );
4829 putenv( env );
4830 if (!android_hw->hw_audioOutput) {
4831 fprintf(stdout, "Emulated hardware doesn't have audio output\n");
4832 }
4833 }
4834
4835 if (android_op_cpu_delay) {
4836 char* end;
4837 long delay = strtol(android_op_cpu_delay, &end, 0);
4838 if (end == NULL || *end || delay < 0 || delay > 1000 ) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004839 PANIC("option -cpu-delay must be an integer between 0 and 1000" );
Vladimir Chtchetkineb2438402010-08-24 08:55:33 -07004840 }
4841 if (delay > 0)
4842 delay = (1000-delay);
4843
4844 qemu_cpu_delay = (int) delay;
4845 }
4846
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004847 if (android_op_dns_server) {
4848 char* x = strchr(android_op_dns_server, ',');
4849 dns_count = 0;
4850 if (x == NULL)
4851 {
4852 if ( add_dns_server( android_op_dns_server ) == 0 )
4853 dns_count = 1;
4854 }
4855 else
4856 {
4857 x = android_op_dns_server;
4858 while (*x) {
4859 char* y = strchr(x, ',');
4860
4861 if (y != NULL) {
4862 *y = 0;
4863 y++;
4864 } else {
4865 y = x + strlen(x);
4866 }
4867
4868 if (y > x && add_dns_server( x ) == 0) {
4869 dns_count += 1;
4870 }
4871 x = y;
4872 }
4873 }
4874 if (dns_count == 0)
4875 fprintf( stdout, "### WARNING: will use system default DNS server\n" );
4876 }
4877
4878 if (dns_count == 0)
4879 dns_count = slirp_get_system_dns_servers();
4880 if (dns_count) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004881 snprintf(tmp_str, sizeof(tmp_str), "ndns=%d", dns_count);
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07004882 append_param(kernel_cmdline_append, tmp_str, sizeof(kernel_cmdline_append));
4883 }
4884
Vladimir Chtchetkineb5365f32010-08-09 13:33:57 -07004885#ifdef CONFIG_MEMCHECK
4886 if (android_op_memcheck) {
4887 memcheck_init(android_op_memcheck);
4888 }
4889#endif // CONFIG_MEMCHECK
4890
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004891#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
4892 if (kvm_allowed && kqemu_allowed) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004893 PANIC(
4894 "You can not enable both KVM and kqemu at the same time");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004895 }
4896#endif
4897
4898 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
4899 if (smp_cpus > machine->max_cpus) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004900 PANIC("Number of SMP cpus requested (%d), exceeds max cpus "
4901 "supported by machine `%s' (%d)", smp_cpus, machine->name,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004902 machine->max_cpus);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004903 }
4904
4905 if (display_type == DT_NOGRAPHIC) {
4906 if (serial_device_index == 0)
4907 serial_devices[0] = "stdio";
4908 if (parallel_device_index == 0)
4909 parallel_devices[0] = "null";
4910 if (strncmp(monitor_device, "vc", 2) == 0)
4911 monitor_device = "stdio";
4912 }
4913
4914#ifndef _WIN32
4915 if (daemonize) {
4916 pid_t pid;
4917
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004918 if (pipe(fds) == -1) {
4919 PANIC("Unable to aquire pidfile");
4920 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004921
4922 pid = fork();
4923 if (pid > 0) {
4924 uint8_t status;
4925 ssize_t len;
4926
4927 close(fds[1]);
4928
4929 again:
4930 len = read(fds[0], &status, 1);
4931 if (len == -1 && (errno == EINTR))
4932 goto again;
4933
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004934 if (len != 1) {
4935 PANIC("Error when aquiring pidfile");
4936 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004937 else if (status == 1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004938 PANIC("Could not acquire pidfile");
4939 } else {
4940 QEMU_EXIT(0);
4941 }
4942 } else if (pid < 0) {
4943 PANIC("Unable to daemonize");
4944 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004945
4946 setsid();
4947
4948 pid = fork();
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004949 if (pid > 0) {
4950 QEMU_EXIT(0);
4951 } else if (pid < 0) {
4952 PANIC("Could not acquire pid file");
4953 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004954
4955 umask(027);
4956
4957 signal(SIGTSTP, SIG_IGN);
4958 signal(SIGTTOU, SIG_IGN);
4959 signal(SIGTTIN, SIG_IGN);
4960 }
4961
4962 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4963 if (daemonize) {
4964 uint8_t status = 1;
David 'Digit' Turner4e024bb2010-09-22 14:19:28 +02004965 int ret;
4966 do {
4967 ret = write(fds[1], &status, 1);
4968 } while (ret < 0 && errno == EINTR);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004969 PANIC("Could not acquire pid file");
4970 } else {
4971 PANIC("Could not acquire pid file");
4972 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004973 }
4974#endif
4975
4976#ifdef CONFIG_KQEMU
4977 if (smp_cpus > 1)
4978 kqemu_allowed = 0;
4979#endif
4980 if (qemu_init_main_loop()) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004981 PANIC("qemu_init_main_loop failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004982 }
4983 linux_boot = (kernel_filename != NULL);
4984 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
4985
4986 if (!linux_boot && *kernel_cmdline != '\0') {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004987 PANIC("-append only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004988 }
4989
4990 if (!linux_boot && initrd_filename != NULL) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07004991 PANIC("-initrd only allowed with -kernel option");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004992 }
4993
4994 /* boot to floppy or the default cd if no hard disk defined yet */
4995 if (!boot_devices[0]) {
4996 boot_devices = "cad";
4997 }
4998 setvbuf(stdout, NULL, _IOLBF, 0);
4999
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005000 if (init_timer_alarm() < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005001 PANIC("could not initialize alarm timer");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005002 }
David Turner6a9ef172010-09-09 22:54:36 +02005003 configure_icount(icount_option);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005004
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005005 /* init network clients */
5006 if (nb_net_clients == 0) {
5007 /* if no clients, we use a default config */
5008 net_clients[nb_net_clients++] = "nic";
5009#ifdef CONFIG_SLIRP
5010 net_clients[nb_net_clients++] = "user";
5011#endif
5012 }
5013
5014 for(i = 0;i < nb_net_clients; i++) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005015 if (net_client_parse(net_clients[i]) < 0) {
5016 PANIC("Unable to parse net clients");
5017 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005018 }
5019 net_client_check();
5020
5021#ifdef TARGET_I386
5022 /* XXX: this should be moved in the PC machine instantiation code */
5023 if (net_boot != 0) {
5024 int netroms = 0;
5025 for (i = 0; i < nb_nics && i < 4; i++) {
5026 const char *model = nd_table[i].model;
5027 char buf[1024];
5028 char *filename;
5029 if (net_boot & (1 << i)) {
5030 if (model == NULL)
5031 model = "ne2k_pci";
5032 snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5033 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5034 if (filename && get_image_size(filename) > 0) {
5035 if (nb_option_roms >= MAX_OPTION_ROMS) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005036 PANIC("Too many option ROMs");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005037 }
5038 option_rom[nb_option_roms] = qemu_strdup(buf);
5039 nb_option_roms++;
5040 netroms++;
5041 }
5042 if (filename) {
5043 qemu_free(filename);
5044 }
5045 }
5046 }
5047 if (netroms == 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005048 PANIC("No valid PXE rom found for network device");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005049 }
5050 }
5051#endif
5052
5053 /* init the bluetooth world */
5054 for (i = 0; i < nb_bt_opts; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005055 if (bt_parse(bt_opts[i])) {
5056 PANIC("Unable to parse bluetooth options");
5057 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005058
5059 /* init the memory */
5060 if (ram_size == 0)
5061 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5062
5063#ifdef CONFIG_KQEMU
5064 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5065 guest ram allocation. It needs to go away. */
5066 if (kqemu_allowed) {
5067 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5068 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5069 if (!kqemu_phys_ram_base) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005070 PANIC("Could not allocate physical memory");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005071 }
5072 }
5073#endif
5074
5075 /* init the dynamic translator */
5076 cpu_exec_init_all(tb_size * 1024 * 1024);
5077
5078 bdrv_init();
5079
5080 /* we always create the cdrom drive, even if no disk is there */
5081
5082 if (nb_drives_opt < MAX_DRIVES)
5083 drive_add(NULL, CDROM_ALIAS);
5084
5085 /* we always create at least one floppy */
5086
5087 if (nb_drives_opt < MAX_DRIVES)
5088 drive_add(NULL, FD_ALIAS, 0);
5089
5090 /* we always create one sd slot, even if no card is in it */
5091
5092 if (nb_drives_opt < MAX_DRIVES)
5093 drive_add(NULL, SD_ALIAS);
5094
5095 /* open the virtual block devices */
5096
5097 for(i = 0; i < nb_drives_opt; i++)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005098 if (drive_init(&drives_opt[i], snapshot, machine) == -1) {
5099 PANIC("Could not open the virtual block devices");
5100 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005101
David Turner6a9ef172010-09-09 22:54:36 +02005102 //register_savevm("timer", 0, 2, timer_save, timer_load, &timers_state);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005103 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5104
5105#ifndef _WIN32
5106 /* must be after terminal init, SDL library changes signal handlers */
5107 sighandler_setup();
5108#endif
5109
5110 /* Maintain compatibility with multiple stdio monitors */
5111 if (!strcmp(monitor_device,"stdio")) {
5112 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5113 const char *devname = serial_devices[i];
5114 if (devname && !strcmp(devname,"mon:stdio")) {
5115 monitor_device = NULL;
5116 break;
5117 } else if (devname && !strcmp(devname,"stdio")) {
5118 monitor_device = NULL;
5119 serial_devices[i] = "mon:stdio";
5120 break;
5121 }
5122 }
5123 }
5124
5125 if (nb_numa_nodes > 0) {
5126 int i;
5127
5128 if (nb_numa_nodes > smp_cpus) {
5129 nb_numa_nodes = smp_cpus;
5130 }
5131
5132 /* If no memory size if given for any node, assume the default case
5133 * and distribute the available memory equally across all nodes
5134 */
5135 for (i = 0; i < nb_numa_nodes; i++) {
5136 if (node_mem[i] != 0)
5137 break;
5138 }
5139 if (i == nb_numa_nodes) {
5140 uint64_t usedmem = 0;
5141
5142 /* On Linux, the each node's border has to be 8MB aligned,
5143 * the final node gets the rest.
5144 */
5145 for (i = 0; i < nb_numa_nodes - 1; i++) {
5146 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5147 usedmem += node_mem[i];
5148 }
5149 node_mem[i] = ram_size - usedmem;
5150 }
5151
5152 for (i = 0; i < nb_numa_nodes; i++) {
5153 if (node_cpumask[i] != 0)
5154 break;
5155 }
5156 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5157 * must cope with this anyway, because there are BIOSes out there in
5158 * real machines which also use this scheme.
5159 */
5160 if (i == nb_numa_nodes) {
5161 for (i = 0; i < smp_cpus; i++) {
5162 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5163 }
5164 }
5165 }
5166
5167 if (kvm_enabled()) {
5168 int ret;
5169
5170 ret = kvm_init(smp_cpus);
5171 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005172 PANIC("failed to initialize KVM");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005173 }
5174 }
5175
5176 if (monitor_device) {
5177 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5178 if (!monitor_hd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005179 PANIC("qemu: could not open monitor device '%s'",
5180 monitor_device);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005181 }
5182 }
5183
5184 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5185 const char *devname = serial_devices[i];
5186 if (devname && strcmp(devname, "none")) {
5187 char label[32];
5188 snprintf(label, sizeof(label), "serial%d", i);
5189 serial_hds[i] = qemu_chr_open(label, devname, NULL);
5190 if (!serial_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005191 PANIC("qemu: could not open serial device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005192 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005193 }
5194 }
5195 }
5196
5197 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5198 const char *devname = parallel_devices[i];
5199 if (devname && strcmp(devname, "none")) {
5200 char label[32];
5201 snprintf(label, sizeof(label), "parallel%d", i);
5202 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
5203 if (!parallel_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005204 PANIC("qemu: could not open parallel device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005205 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005206 }
5207 }
5208 }
5209
5210 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5211 const char *devname = virtio_consoles[i];
5212 if (devname && strcmp(devname, "none")) {
5213 char label[32];
5214 snprintf(label, sizeof(label), "virtcon%d", i);
5215 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
5216 if (!virtcon_hds[i]) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005217 PANIC("qemu: could not open virtio console '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005218 devname);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005219 }
5220 }
5221 }
5222
5223 module_call_init(MODULE_INIT_DEVICE);
5224
5225
5226#ifdef CONFIG_TRACE
5227 if (trace_filename) {
5228 trace_init(trace_filename);
5229#if 0
5230 // We don't need the dcache code until we can get load and store tracing
5231 // working again.
5232 dcache_init(dcache_size, dcache_ways, dcache_line_size,
5233 dcache_replace_policy, dcache_load_miss_penalty,
5234 dcache_store_miss_penalty);
5235#endif
5236 fprintf(stderr, "-- When done tracing, exit the emulator. --\n");
5237 }
5238#endif
5239
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005240 /* Combine kernel command line passed from the UI with parameters
5241 * collected during initialization. */
5242 if (*kernel_cmdline) {
5243 if (kernel_cmdline_append[0]) {
5244 snprintf(kernel_cmdline_full, sizeof(kernel_cmdline_full), "%s %s",
5245 kernel_cmdline, kernel_cmdline_append);
5246 } else {
5247 strncpy(kernel_cmdline_full, kernel_cmdline, sizeof(kernel_cmdline_full));
5248 kernel_cmdline_full[sizeof(kernel_cmdline_full) - 1] = '\0';
5249 }
5250 } else if (kernel_cmdline_append[0]) {
5251 strncpy(kernel_cmdline_full, kernel_cmdline_append, sizeof(kernel_cmdline_full));
5252 }
5253
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005254 machine->init(ram_size, boot_devices,
Vladimir Chtchetkine7fbf4972010-08-11 15:30:32 -07005255 kernel_filename, kernel_cmdline_full, initrd_filename, cpu_model);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005256
5257
5258 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5259 for (i = 0; i < nb_numa_nodes; i++) {
5260 if (node_cpumask[i] & (1 << env->cpu_index)) {
5261 env->numa_node = i;
5262 }
5263 }
5264 }
5265
5266 current_machine = machine;
5267
5268 /* Set KVM's vcpu state to qemu's initial CPUState. */
5269 if (kvm_enabled()) {
5270 int ret;
5271
5272 ret = kvm_sync_vcpus();
5273 if (ret < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005274 PANIC("failed to initialize vcpus");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005275 }
5276 }
5277
5278 /* init USB devices */
5279 if (usb_enabled) {
5280 for(i = 0; i < usb_devices_index; i++) {
5281 if (usb_device_add(usb_devices[i], 0) < 0) {
5282 fprintf(stderr, "Warning: could not add USB device %s\n",
5283 usb_devices[i]);
5284 }
5285 }
5286 }
5287
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005288 if (!display_state) {
5289 if (android_op_gui) {
5290 /* Initialize display from the command line parameters. */
5291 if (parse_androig_gui_option(android_op_gui,
5292 &android_display_width,
5293 &android_display_height,
5294 &android_display_bpp)) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005295 PANIC("Unable to parse -android-gui parameter: %s",
5296 android_op_gui);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005297 }
5298 android_display_init_from(android_display_width,
5299 android_display_height, 0,
5300 android_display_bpp);
5301 } else {
David Turnerf52506f2010-09-10 16:11:22 +02005302 ds = get_displaystate(); /* this forces a dumb display init */
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005303 }
5304 } else if (android_op_gui) {
5305 /* Resize display from the command line parameters. */
5306 if (parse_androig_gui_option(android_op_gui,
5307 &android_display_width,
5308 &android_display_height,
5309 &android_display_bpp)) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005310 PANIC("Unable to parse -android-gui parameter: %s",
5311 android_op_gui);
Vladimir Chtchetkinedd50f7d2010-07-30 09:16:41 -07005312 }
5313 display_state->surface = qemu_resize_displaysurface(display_state,
5314 android_display_width,
5315 android_display_height);
5316 }
5317
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005318 /* just use the first displaystate for the moment */
David Turnerf52506f2010-09-10 16:11:22 +02005319 ds = display_state = get_displaystate();
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005320
5321 if (display_type == DT_DEFAULT) {
5322#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5323 display_type = DT_SDL;
5324#else
5325 display_type = DT_VNC;
5326 vnc_display = "localhost:0,to=99";
5327 show_vnc_port = 1;
5328#endif
5329 }
Vladimir Chtchetkined81e6d12010-06-15 16:46:32 -07005330
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005331
5332 switch (display_type) {
5333 case DT_NOGRAPHIC:
5334 break;
5335#if defined(CONFIG_CURSES)
5336 case DT_CURSES:
5337 curses_display_init(ds, full_screen);
5338 break;
5339#endif
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005340#if defined(CONFIG_SDL) && !defined(CONFIG_STANDALONE_CORE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005341 case DT_SDL:
5342 sdl_display_init(ds, full_screen, no_frame);
5343 break;
5344#elif defined(CONFIG_COCOA)
5345 case DT_SDL:
5346 cocoa_display_init(ds, full_screen);
5347 break;
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005348#elif defined(CONFIG_STANDALONE_CORE)
5349 case DT_SDL:
Vladimir Chtchetkinee95660a2010-12-20 08:28:03 -08005350 coredisplay_init(ds);
Vladimir Chtchetkine72d83df2010-12-14 09:24:02 -08005351 break;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005352#endif
5353 case DT_VNC:
5354 vnc_display_init(ds);
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005355 if (vnc_display_open(ds, vnc_display) < 0) {
5356 PANIC("Unable to initialize VNC display");
5357 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005358
5359 if (show_vnc_port) {
5360 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
5361 }
5362 break;
5363 default:
5364 break;
5365 }
5366 dpy_resize(ds);
5367
5368 dcl = ds->listeners;
5369 while (dcl != NULL) {
5370 if (dcl->dpy_refresh != NULL) {
5371 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5372 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
5373 }
5374 dcl = dcl->next;
5375 }
5376
5377 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
5378 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5379 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5380 }
5381
5382 text_consoles_set_display(display_state);
5383 qemu_chr_initial_reset();
5384
5385 if (monitor_device && monitor_hd)
5386 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
5387
5388 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5389 const char *devname = serial_devices[i];
5390 if (devname && strcmp(devname, "none")) {
5391 if (strstart(devname, "vc", 0))
5392 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
5393 }
5394 }
5395
5396 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5397 const char *devname = parallel_devices[i];
5398 if (devname && strcmp(devname, "none")) {
5399 if (strstart(devname, "vc", 0))
5400 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
5401 }
5402 }
5403
5404 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5405 const char *devname = virtio_consoles[i];
5406 if (virtcon_hds[i] && devname) {
5407 if (strstart(devname, "vc", 0))
5408 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5409 }
5410 }
5411
5412 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005413 PANIC("qemu: could not open gdbserver on device '%s'",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005414 gdbstub_dev);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005415 }
5416
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005417 /* call android-specific setup function */
5418 android_emulation_setup();
5419
Ot ten Thije871da2a2010-09-20 10:29:22 +01005420 if (loadvm)
5421 do_loadvm(cur_mon, loadvm);
5422
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005423 if (incoming) {
5424 autostart = 0; /* fixme how to deal with -daemonize */
5425 qemu_start_incoming_migration(incoming);
5426 }
5427
5428 if (autostart)
5429 vm_start();
5430
5431#ifndef _WIN32
5432 if (daemonize) {
5433 uint8_t status = 0;
5434 ssize_t len;
5435
5436 again1:
5437 len = write(fds[1], &status, 1);
5438 if (len == -1 && (errno == EINTR))
5439 goto again1;
5440
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005441 if (len != 1) {
5442 PANIC("Unable to daemonize");
5443 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005444
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005445 if (chdir("/")) {
5446 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005447 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005448 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005449 TFR(fd = open("/dev/null", O_RDWR));
5450 if (fd == -1)
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005451 PANIC("open(\"/dev/null\") failed: %s", errno_str);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005452 }
5453
5454 if (run_as) {
5455 pwd = getpwnam(run_as);
5456 if (!pwd) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005457 PANIC("User \"%s\" doesn't exist", run_as);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005458 }
5459 }
5460
5461 if (chroot_dir) {
5462 if (chroot(chroot_dir) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005463 PANIC("chroot failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005464 }
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005465 if (chdir("/")) {
5466 perror("not able to chdir to /");
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005467 PANIC("not able to chdir to /");
David 'Digit' Turnera7fb77d2010-05-10 23:50:54 -07005468 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005469 }
5470
5471 if (run_as) {
5472 if (setgid(pwd->pw_gid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005473 PANIC("Failed to setgid(%d)", pwd->pw_gid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005474 }
5475 if (setuid(pwd->pw_uid) < 0) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005476 PANIC("Failed to setuid(%d)", pwd->pw_uid);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005477 }
5478 if (setuid(0) != -1) {
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005479 PANIC("Dropping privileges failed");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005480 }
5481 }
5482
5483 if (daemonize) {
5484 dup2(fd, 0);
5485 dup2(fd, 1);
5486 dup2(fd, 2);
5487
5488 close(fd);
5489 }
5490#endif
5491
Vladimir Chtchetkine7746af02010-10-07 05:40:39 -07005492#ifdef CONFIG_ANDROID
5493 // This will notify the UI that the core is successfuly initialized
5494 android_core_init_completed();
5495#endif // CONFIG_ANDROID
5496
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07005497 main_loop();
5498 quit_timers();
5499 net_cleanup();
5500 android_emulation_teardown();
5501 return 0;
5502}
Vladimir Chtchetkineeb838252010-07-15 12:27:56 -07005503
5504void
5505android_emulation_teardown(void)
5506{
5507 android_charmap_done();
5508}