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