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