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