Statistics
| Branch: | Revision:

root / vl.c @ 3fe5c14e

History | View | Annotate | Download (157.7 kB)

1 0824d6fc bellard
/*
2 80cabfad bellard
 * QEMU System Emulator
3 5fafdf24 ths
 *
4 68d0f70e bellard
 * Copyright (c) 2003-2008 Fabrice Bellard
5 5fafdf24 ths
 *
6 1df912cf bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1df912cf bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1df912cf bellard
 * in the Software without restriction, including without limitation the rights
9 1df912cf bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1df912cf bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1df912cf bellard
 * furnished to do so, subject to the following conditions:
12 1df912cf bellard
 *
13 1df912cf bellard
 * The above copyright notice and this permission notice shall be included in
14 1df912cf bellard
 * all copies or substantial portions of the Software.
15 1df912cf bellard
 *
16 1df912cf bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1df912cf bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1df912cf bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1df912cf bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1df912cf bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1df912cf bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1df912cf bellard
 * THE SOFTWARE.
23 0824d6fc bellard
 */
24 0824d6fc bellard
#include <unistd.h>
25 0824d6fc bellard
#include <fcntl.h>
26 0824d6fc bellard
#include <signal.h>
27 0824d6fc bellard
#include <time.h>
28 0824d6fc bellard
#include <errno.h>
29 67b915a5 bellard
#include <sys/time.h>
30 c88676f8 bellard
#include <zlib.h>
31 67b915a5 bellard
32 179a2c19 blueswir1
/* Needed early for HOST_BSD etc. */
33 d40cdb10 blueswir1
#include "config-host.h"
34 d40cdb10 blueswir1
35 67b915a5 bellard
#ifndef _WIN32
36 5cea8590 Paul Brook
#include <libgen.h>
37 0858532e aliguori
#include <pwd.h>
38 67b915a5 bellard
#include <sys/times.h>
39 f1510b2c bellard
#include <sys/wait.h>
40 67b915a5 bellard
#include <termios.h>
41 67b915a5 bellard
#include <sys/mman.h>
42 f1510b2c bellard
#include <sys/ioctl.h>
43 24646c7e blueswir1
#include <sys/resource.h>
44 f1510b2c bellard
#include <sys/socket.h>
45 c94c8d64 bellard
#include <netinet/in.h>
46 24646c7e blueswir1
#include <net/if.h>
47 24646c7e blueswir1
#if defined(__NetBSD__)
48 24646c7e blueswir1
#include <net/if_tap.h>
49 24646c7e blueswir1
#endif
50 24646c7e blueswir1
#ifdef __linux__
51 24646c7e blueswir1
#include <linux/if_tun.h>
52 24646c7e blueswir1
#endif
53 24646c7e blueswir1
#include <arpa/inet.h>
54 9d728e8c bellard
#include <dirent.h>
55 7c9d8e07 bellard
#include <netdb.h>
56 cb4b976b ths
#include <sys/select.h>
57 179a2c19 blueswir1
#ifdef HOST_BSD
58 7d3505c5 bellard
#include <sys/stat.h>
59 c5e97233 blueswir1
#if defined(__FreeBSD__) || defined(__DragonFly__)
60 7d3505c5 bellard
#include <libutil.h>
61 24646c7e blueswir1
#else
62 24646c7e blueswir1
#include <util.h>
63 128ab2ff blueswir1
#endif
64 5c40d2bd ths
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65 5c40d2bd ths
#include <freebsd/stdlib.h>
66 7d3505c5 bellard
#else
67 223f0d72 blueswir1
#ifdef __linux__
68 7d3505c5 bellard
#include <pty.h>
69 7d3505c5 bellard
#include <malloc.h>
70 fd872598 bellard
#include <linux/rtc.h>
71 bd494f4c ths
72 bd494f4c ths
/* For the benefit of older linux systems which don't supply it,
73 bd494f4c ths
   we use a local copy of hpet.h. */
74 bd494f4c ths
/* #include <linux/hpet.h> */
75 bd494f4c ths
#include "hpet.h"
76 bd494f4c ths
77 e57a8c0e bellard
#include <linux/ppdev.h>
78 5867c88a ths
#include <linux/parport.h>
79 223f0d72 blueswir1
#endif
80 223f0d72 blueswir1
#ifdef __sun__
81 d5d10bc3 ths
#include <sys/stat.h>
82 d5d10bc3 ths
#include <sys/ethernet.h>
83 d5d10bc3 ths
#include <sys/sockio.h>
84 d5d10bc3 ths
#include <netinet/arp.h>
85 d5d10bc3 ths
#include <netinet/in.h>
86 d5d10bc3 ths
#include <netinet/in_systm.h>
87 d5d10bc3 ths
#include <netinet/ip.h>
88 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
89 d5d10bc3 ths
#include <netinet/udp.h>
90 d5d10bc3 ths
#include <netinet/tcp.h>
91 d5d10bc3 ths
#include <net/if.h>
92 d5d10bc3 ths
#include <syslog.h>
93 d5d10bc3 ths
#include <stropts.h>
94 67b915a5 bellard
#endif
95 7d3505c5 bellard
#endif
96 ec530c81 bellard
#endif
97 67b915a5 bellard
98 9892fbfb blueswir1
#if defined(__OpenBSD__)
99 9892fbfb blueswir1
#include <util.h>
100 9892fbfb blueswir1
#endif
101 9892fbfb blueswir1
102 8a16d273 ths
#if defined(CONFIG_VDE)
103 8a16d273 ths
#include <libvdeplug.h>
104 8a16d273 ths
#endif
105 8a16d273 ths
106 67b915a5 bellard
#ifdef _WIN32
107 49dc768d aliguori
#include <windows.h>
108 7d3505c5 bellard
#include <malloc.h>
109 67b915a5 bellard
#include <sys/timeb.h>
110 4fddf62a ths
#include <mmsystem.h>
111 67b915a5 bellard
#define getopt_long_only getopt_long
112 67b915a5 bellard
#define memalign(align, size) malloc(size)
113 67b915a5 bellard
#endif
114 67b915a5 bellard
115 73332e5c bellard
#ifdef CONFIG_SDL
116 96bcd4f8 bellard
#ifdef __APPLE__
117 6693665a Stefan Weil
#include <SDL.h>
118 880fec5d malc
int qemu_main(int argc, char **argv, char **envp);
119 880fec5d malc
int main(int argc, char **argv)
120 880fec5d malc
{
121 880fec5d malc
    qemu_main(argc, argv, NULL);
122 880fec5d malc
}
123 880fec5d malc
#undef main
124 880fec5d malc
#define main qemu_main
125 96bcd4f8 bellard
#endif
126 73332e5c bellard
#endif /* CONFIG_SDL */
127 0824d6fc bellard
128 5b0753e0 bellard
#ifdef CONFIG_COCOA
129 5b0753e0 bellard
#undef main
130 5b0753e0 bellard
#define main qemu_main
131 5b0753e0 bellard
#endif /* CONFIG_COCOA */
132 5b0753e0 bellard
133 511d2b14 blueswir1
#include "hw/hw.h"
134 511d2b14 blueswir1
#include "hw/boards.h"
135 511d2b14 blueswir1
#include "hw/usb.h"
136 511d2b14 blueswir1
#include "hw/pcmcia.h"
137 511d2b14 blueswir1
#include "hw/pc.h"
138 511d2b14 blueswir1
#include "hw/audiodev.h"
139 511d2b14 blueswir1
#include "hw/isa.h"
140 511d2b14 blueswir1
#include "hw/baum.h"
141 511d2b14 blueswir1
#include "hw/bt.h"
142 9dd986cc Richard W.M. Jones
#include "hw/watchdog.h"
143 b6f6e3d3 aliguori
#include "hw/smbios.h"
144 e37630ca aliguori
#include "hw/xen.h"
145 5ef4efa4 aurel32
#include "bt-host.h"
146 511d2b14 blueswir1
#include "net.h"
147 511d2b14 blueswir1
#include "monitor.h"
148 511d2b14 blueswir1
#include "console.h"
149 511d2b14 blueswir1
#include "sysemu.h"
150 511d2b14 blueswir1
#include "gdbstub.h"
151 511d2b14 blueswir1
#include "qemu-timer.h"
152 511d2b14 blueswir1
#include "qemu-char.h"
153 511d2b14 blueswir1
#include "cache-utils.h"
154 511d2b14 blueswir1
#include "block.h"
155 a718acec blueswir1
#include "dma.h"
156 511d2b14 blueswir1
#include "audio/audio.h"
157 511d2b14 blueswir1
#include "migration.h"
158 511d2b14 blueswir1
#include "kvm.h"
159 511d2b14 blueswir1
#include "balloon.h"
160 d3f24367 Kevin Wolf
#include "qemu-option.h"
161 511d2b14 blueswir1
162 0824d6fc bellard
#include "disas.h"
163 fc01f7e7 bellard
164 8a7ddc38 bellard
#include "exec-all.h"
165 0824d6fc bellard
166 511d2b14 blueswir1
#include "qemu_socket.h"
167 511d2b14 blueswir1
168 511d2b14 blueswir1
#if defined(CONFIG_SLIRP)
169 511d2b14 blueswir1
#include "libslirp.h"
170 511d2b14 blueswir1
#endif
171 511d2b14 blueswir1
172 0824d6fc bellard
//#define DEBUG_UNUSED_IOPORT
173 fd872598 bellard
//#define DEBUG_IOPORT
174 9dc63a1e blueswir1
//#define DEBUG_NET
175 9dc63a1e blueswir1
//#define DEBUG_SLIRP
176 330d0414 bellard
177 d12d51d5 aliguori
178 d12d51d5 aliguori
#ifdef DEBUG_IOPORT
179 93fcfe39 aliguori
#  define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
180 d12d51d5 aliguori
#else
181 d12d51d5 aliguori
#  define LOG_IOPORT(...) do { } while (0)
182 d12d51d5 aliguori
#endif
183 d12d51d5 aliguori
184 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
185 313aa567 bellard
186 0d92ed30 pbrook
/* Max number of USB devices that can be specified on the commandline.  */
187 0d92ed30 pbrook
#define MAX_USB_CMDLINE 8
188 0d92ed30 pbrook
189 dc72ac14 balrog
/* Max number of bluetooth switches on the commandline.  */
190 dc72ac14 balrog
#define MAX_BT_CMDLINE 10
191 dc72ac14 balrog
192 7dea1da4 bellard
/* XXX: use a two level table to limit memory usage */
193 7dea1da4 bellard
#define MAX_IOPORTS 65536
194 0824d6fc bellard
195 5cea8590 Paul Brook
static const char *data_dir;
196 1192dad8 j_mayer
const char *bios_name = NULL;
197 dbed7e40 blueswir1
static void *ioport_opaque[MAX_IOPORTS];
198 dbed7e40 blueswir1
static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
199 dbed7e40 blueswir1
static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
200 e4bcb14c ths
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
201 faea38e7 bellard
   to store the VM snapshots */
202 e4bcb14c ths
DriveInfo drives_table[MAX_DRIVES+1];
203 e4bcb14c ths
int nb_drives;
204 cb5a7aa8 malc
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
205 3023f332 aliguori
static DisplayState *display_state;
206 993fbfdb Anthony Liguori
DisplayType display_type = DT_DEFAULT;
207 3d11d0eb bellard
const char* keyboard_layout = NULL;
208 313aa567 bellard
int64_t ticks_per_sec;
209 00f82b8a aurel32
ram_addr_t ram_size;
210 c4b1fcc0 bellard
int nb_nics;
211 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
212 8a7ddc38 bellard
int vm_running;
213 c0f4ce77 aliguori
static int autostart;
214 f6503059 balrog
static int rtc_utc = 1;
215 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
216 1bfe856e bellard
int cirrus_vga_enabled = 1;
217 c2b3b41a aliguori
int std_vga_enabled = 0;
218 d34cab9f ths
int vmsvga_enabled = 0;
219 94909d9f aliguori
int xenfb_enabled = 0;
220 d827220b bellard
#ifdef TARGET_SPARC
221 d827220b bellard
int graphic_width = 1024;
222 d827220b bellard
int graphic_height = 768;
223 eee0b836 blueswir1
int graphic_depth = 8;
224 d827220b bellard
#else
225 1bfe856e bellard
int graphic_width = 800;
226 1bfe856e bellard
int graphic_height = 600;
227 e9b137c2 bellard
int graphic_depth = 15;
228 eee0b836 blueswir1
#endif
229 dbed7e40 blueswir1
static int full_screen = 0;
230 634a21f6 blueswir1
#ifdef CONFIG_SDL
231 dbed7e40 blueswir1
static int no_frame = 0;
232 634a21f6 blueswir1
#endif
233 667accab ths
int no_quit = 0;
234 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
235 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
236 9ede2fde aliguori
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
237 a09db21f bellard
#ifdef TARGET_I386
238 a09db21f bellard
int win2k_install_hack = 0;
239 73822ec8 aliguori
int rtc_td_hack = 0;
240 a09db21f bellard
#endif
241 bb36d470 bellard
int usb_enabled = 0;
242 1b530a6d aurel32
int singlestep = 0;
243 6a00d601 bellard
int smp_cpus = 1;
244 73fc9742 ths
const char *vnc_display;
245 6515b203 bellard
int acpi_enabled = 1;
246 16b29ae1 aliguori
int no_hpet = 0;
247 df97b920 Eduardo Habkost
int no_virtio_balloon = 0;
248 52ca8d6a bellard
int fd_bootchk = 1;
249 d1beab82 bellard
int no_reboot = 0;
250 b2f76161 aurel32
int no_shutdown = 0;
251 9467cd46 balrog
int cursor_hide = 1;
252 a171fe39 balrog
int graphic_rotate = 0;
253 b9e82a59 blueswir1
#ifndef _WIN32
254 71e3ceb8 ths
int daemonize = 0;
255 b9e82a59 blueswir1
#endif
256 9dd986cc Richard W.M. Jones
WatchdogTimerModel *watchdog = NULL;
257 9dd986cc Richard W.M. Jones
int watchdog_action = WDT_RESET;
258 9ae02555 ths
const char *option_rom[MAX_OPTION_ROMS];
259 9ae02555 ths
int nb_option_roms;
260 8e71621f pbrook
int semihosting_enabled = 0;
261 2b8f2d41 balrog
#ifdef TARGET_ARM
262 2b8f2d41 balrog
int old_param = 0;
263 2b8f2d41 balrog
#endif
264 c35734b2 ths
const char *qemu_name;
265 3780e197 ths
int alt_grab = 0;
266 95efd11c blueswir1
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
267 66508601 blueswir1
unsigned int nb_prom_envs = 0;
268 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
269 66508601 blueswir1
#endif
270 ec691c80 aliguori
int nb_drives_opt;
271 ec691c80 aliguori
struct drive_opt drives_opt[MAX_DRIVES];
272 0824d6fc bellard
273 268a362c aliguori
int nb_numa_nodes;
274 268a362c aliguori
uint64_t node_mem[MAX_NODES];
275 268a362c aliguori
uint64_t node_cpumask[MAX_NODES];
276 268a362c aliguori
277 ee5605e5 balrog
static CPUState *cur_cpu;
278 ee5605e5 balrog
static CPUState *next_cpu;
279 43b96858 aliguori
static int timer_alarm_pending = 1;
280 bf20dc07 ths
/* Conversion factor from emulated instructions to virtual clock ticks.  */
281 2e70f6ef pbrook
static int icount_time_shift;
282 bf20dc07 ths
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
283 2e70f6ef pbrook
#define MAX_ICOUNT_SHIFT 10
284 2e70f6ef pbrook
/* Compensate for varying guest execution speed.  */
285 2e70f6ef pbrook
static int64_t qemu_icount_bias;
286 dbed7e40 blueswir1
static QEMUTimer *icount_rt_timer;
287 dbed7e40 blueswir1
static QEMUTimer *icount_vm_timer;
288 9043b62d blueswir1
static QEMUTimer *nographic_timer;
289 ee5605e5 balrog
290 8fcb1b90 blueswir1
uint8_t qemu_uuid[16];
291 8fcb1b90 blueswir1
292 0824d6fc bellard
/***********************************************************/
293 26aa7d72 bellard
/* x86 ISA bus support */
294 26aa7d72 bellard
295 26aa7d72 bellard
target_phys_addr_t isa_mem_base = 0;
296 3de388f6 bellard
PicState2 *isa_pic;
297 0824d6fc bellard
298 477e3edf aliguori
static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
299 477e3edf aliguori
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
300 477e3edf aliguori
301 477e3edf aliguori
static uint32_t ioport_read(int index, uint32_t address)
302 477e3edf aliguori
{
303 477e3edf aliguori
    static IOPortReadFunc *default_func[3] = {
304 477e3edf aliguori
        default_ioport_readb,
305 477e3edf aliguori
        default_ioport_readw,
306 477e3edf aliguori
        default_ioport_readl
307 477e3edf aliguori
    };
308 477e3edf aliguori
    IOPortReadFunc *func = ioport_read_table[index][address];
309 477e3edf aliguori
    if (!func)
310 477e3edf aliguori
        func = default_func[index];
311 477e3edf aliguori
    return func(ioport_opaque[address], address);
312 477e3edf aliguori
}
313 477e3edf aliguori
314 477e3edf aliguori
static void ioport_write(int index, uint32_t address, uint32_t data)
315 477e3edf aliguori
{
316 477e3edf aliguori
    static IOPortWriteFunc *default_func[3] = {
317 477e3edf aliguori
        default_ioport_writeb,
318 477e3edf aliguori
        default_ioport_writew,
319 477e3edf aliguori
        default_ioport_writel
320 477e3edf aliguori
    };
321 477e3edf aliguori
    IOPortWriteFunc *func = ioport_write_table[index][address];
322 477e3edf aliguori
    if (!func)
323 477e3edf aliguori
        func = default_func[index];
324 477e3edf aliguori
    func(ioport_opaque[address], address, data);
325 477e3edf aliguori
}
326 477e3edf aliguori
327 9596ebb7 pbrook
static uint32_t default_ioport_readb(void *opaque, uint32_t address)
328 0824d6fc bellard
{
329 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
330 1196be37 ths
    fprintf(stderr, "unused inb: port=0x%04x\n", address);
331 0824d6fc bellard
#endif
332 fc01f7e7 bellard
    return 0xff;
333 0824d6fc bellard
}
334 0824d6fc bellard
335 9596ebb7 pbrook
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
336 0824d6fc bellard
{
337 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
338 1196be37 ths
    fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
339 0824d6fc bellard
#endif
340 0824d6fc bellard
}
341 0824d6fc bellard
342 0824d6fc bellard
/* default is to make two byte accesses */
343 9596ebb7 pbrook
static uint32_t default_ioport_readw(void *opaque, uint32_t address)
344 0824d6fc bellard
{
345 0824d6fc bellard
    uint32_t data;
346 477e3edf aliguori
    data = ioport_read(0, address);
347 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
348 477e3edf aliguori
    data |= ioport_read(0, address) << 8;
349 0824d6fc bellard
    return data;
350 0824d6fc bellard
}
351 0824d6fc bellard
352 9596ebb7 pbrook
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
353 0824d6fc bellard
{
354 477e3edf aliguori
    ioport_write(0, address, data & 0xff);
355 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
356 477e3edf aliguori
    ioport_write(0, address, (data >> 8) & 0xff);
357 0824d6fc bellard
}
358 0824d6fc bellard
359 9596ebb7 pbrook
static uint32_t default_ioport_readl(void *opaque, uint32_t address)
360 0824d6fc bellard
{
361 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
362 1196be37 ths
    fprintf(stderr, "unused inl: port=0x%04x\n", address);
363 fc01f7e7 bellard
#endif
364 fc01f7e7 bellard
    return 0xffffffff;
365 0824d6fc bellard
}
366 0824d6fc bellard
367 9596ebb7 pbrook
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
368 0824d6fc bellard
{
369 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
370 1196be37 ths
    fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
371 fc01f7e7 bellard
#endif
372 0824d6fc bellard
}
373 0824d6fc bellard
374 fc01f7e7 bellard
/* size is the word size in byte */
375 5fafdf24 ths
int register_ioport_read(int start, int length, int size,
376 c4b1fcc0 bellard
                         IOPortReadFunc *func, void *opaque)
377 f1510b2c bellard
{
378 fc01f7e7 bellard
    int i, bsize;
379 f1510b2c bellard
380 c4b1fcc0 bellard
    if (size == 1) {
381 fc01f7e7 bellard
        bsize = 0;
382 c4b1fcc0 bellard
    } else if (size == 2) {
383 fc01f7e7 bellard
        bsize = 1;
384 c4b1fcc0 bellard
    } else if (size == 4) {
385 fc01f7e7 bellard
        bsize = 2;
386 c4b1fcc0 bellard
    } else {
387 88fdf56f balrog
        hw_error("register_ioport_read: invalid size");
388 fc01f7e7 bellard
        return -1;
389 c4b1fcc0 bellard
    }
390 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
391 fc01f7e7 bellard
        ioport_read_table[bsize][i] = func;
392 c4b1fcc0 bellard
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
393 88fdf56f balrog
            hw_error("register_ioport_read: invalid opaque");
394 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
395 c4b1fcc0 bellard
    }
396 f1510b2c bellard
    return 0;
397 f1510b2c bellard
}
398 f1510b2c bellard
399 fc01f7e7 bellard
/* size is the word size in byte */
400 5fafdf24 ths
int register_ioport_write(int start, int length, int size,
401 c4b1fcc0 bellard
                          IOPortWriteFunc *func, void *opaque)
402 f1510b2c bellard
{
403 fc01f7e7 bellard
    int i, bsize;
404 f1510b2c bellard
405 c4b1fcc0 bellard
    if (size == 1) {
406 fc01f7e7 bellard
        bsize = 0;
407 c4b1fcc0 bellard
    } else if (size == 2) {
408 fc01f7e7 bellard
        bsize = 1;
409 c4b1fcc0 bellard
    } else if (size == 4) {
410 fc01f7e7 bellard
        bsize = 2;
411 c4b1fcc0 bellard
    } else {
412 88fdf56f balrog
        hw_error("register_ioport_write: invalid size");
413 fc01f7e7 bellard
        return -1;
414 c4b1fcc0 bellard
    }
415 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
416 fc01f7e7 bellard
        ioport_write_table[bsize][i] = func;
417 88fdf56f balrog
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
418 88fdf56f balrog
            hw_error("register_ioport_write: invalid opaque");
419 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
420 c4b1fcc0 bellard
    }
421 f1510b2c bellard
    return 0;
422 f1510b2c bellard
}
423 f1510b2c bellard
424 69b91039 bellard
void isa_unassign_ioport(int start, int length)
425 69b91039 bellard
{
426 69b91039 bellard
    int i;
427 69b91039 bellard
428 69b91039 bellard
    for(i = start; i < start + length; i++) {
429 69b91039 bellard
        ioport_read_table[0][i] = default_ioport_readb;
430 69b91039 bellard
        ioport_read_table[1][i] = default_ioport_readw;
431 69b91039 bellard
        ioport_read_table[2][i] = default_ioport_readl;
432 69b91039 bellard
433 69b91039 bellard
        ioport_write_table[0][i] = default_ioport_writeb;
434 69b91039 bellard
        ioport_write_table[1][i] = default_ioport_writew;
435 69b91039 bellard
        ioport_write_table[2][i] = default_ioport_writel;
436 a7607f7e aliguori
437 a7607f7e aliguori
        ioport_opaque[i] = NULL;
438 69b91039 bellard
    }
439 69b91039 bellard
}
440 69b91039 bellard
441 20f32282 bellard
/***********************************************************/
442 20f32282 bellard
443 c45886db bellard
void cpu_outb(CPUState *env, int addr, int val)
444 0824d6fc bellard
{
445 d12d51d5 aliguori
    LOG_IOPORT("outb: %04x %02x\n", addr, val);
446 477e3edf aliguori
    ioport_write(0, addr, val);
447 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
448 89bfc105 bellard
    if (env)
449 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
450 89bfc105 bellard
#endif
451 0824d6fc bellard
}
452 0824d6fc bellard
453 c45886db bellard
void cpu_outw(CPUState *env, int addr, int val)
454 0824d6fc bellard
{
455 d12d51d5 aliguori
    LOG_IOPORT("outw: %04x %04x\n", addr, val);
456 477e3edf aliguori
    ioport_write(1, addr, val);
457 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
458 89bfc105 bellard
    if (env)
459 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
460 89bfc105 bellard
#endif
461 0824d6fc bellard
}
462 0824d6fc bellard
463 c45886db bellard
void cpu_outl(CPUState *env, int addr, int val)
464 0824d6fc bellard
{
465 d12d51d5 aliguori
    LOG_IOPORT("outl: %04x %08x\n", addr, val);
466 477e3edf aliguori
    ioport_write(2, addr, val);
467 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
468 89bfc105 bellard
    if (env)
469 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
470 89bfc105 bellard
#endif
471 0824d6fc bellard
}
472 0824d6fc bellard
473 c45886db bellard
int cpu_inb(CPUState *env, int addr)
474 0824d6fc bellard
{
475 fd872598 bellard
    int val;
476 477e3edf aliguori
    val = ioport_read(0, addr);
477 d12d51d5 aliguori
    LOG_IOPORT("inb : %04x %02x\n", addr, val);
478 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
479 89bfc105 bellard
    if (env)
480 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
481 89bfc105 bellard
#endif
482 fd872598 bellard
    return val;
483 0824d6fc bellard
}
484 0824d6fc bellard
485 c45886db bellard
int cpu_inw(CPUState *env, int addr)
486 0824d6fc bellard
{
487 fd872598 bellard
    int val;
488 477e3edf aliguori
    val = ioport_read(1, addr);
489 d12d51d5 aliguori
    LOG_IOPORT("inw : %04x %04x\n", addr, val);
490 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
491 89bfc105 bellard
    if (env)
492 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
493 89bfc105 bellard
#endif
494 fd872598 bellard
    return val;
495 0824d6fc bellard
}
496 0824d6fc bellard
497 c45886db bellard
int cpu_inl(CPUState *env, int addr)
498 0824d6fc bellard
{
499 fd872598 bellard
    int val;
500 477e3edf aliguori
    val = ioport_read(2, addr);
501 d12d51d5 aliguori
    LOG_IOPORT("inl : %04x %08x\n", addr, val);
502 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
503 89bfc105 bellard
    if (env)
504 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
505 89bfc105 bellard
#endif
506 fd872598 bellard
    return val;
507 0824d6fc bellard
}
508 0824d6fc bellard
509 0824d6fc bellard
/***********************************************************/
510 0824d6fc bellard
void hw_error(const char *fmt, ...)
511 0824d6fc bellard
{
512 0824d6fc bellard
    va_list ap;
513 6a00d601 bellard
    CPUState *env;
514 0824d6fc bellard
515 0824d6fc bellard
    va_start(ap, fmt);
516 0824d6fc bellard
    fprintf(stderr, "qemu: hardware error: ");
517 0824d6fc bellard
    vfprintf(stderr, fmt, ap);
518 0824d6fc bellard
    fprintf(stderr, "\n");
519 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
520 6a00d601 bellard
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
521 0824d6fc bellard
#ifdef TARGET_I386
522 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
523 c45886db bellard
#else
524 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, 0);
525 0824d6fc bellard
#endif
526 6a00d601 bellard
    }
527 0824d6fc bellard
    va_end(ap);
528 0824d6fc bellard
    abort();
529 0824d6fc bellard
}
530 df751fa8 aliguori
 
531 df751fa8 aliguori
/***************/
532 df751fa8 aliguori
/* ballooning */
533 df751fa8 aliguori
534 df751fa8 aliguori
static QEMUBalloonEvent *qemu_balloon_event;
535 df751fa8 aliguori
void *qemu_balloon_event_opaque;
536 df751fa8 aliguori
537 df751fa8 aliguori
void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
538 df751fa8 aliguori
{
539 df751fa8 aliguori
    qemu_balloon_event = func;
540 df751fa8 aliguori
    qemu_balloon_event_opaque = opaque;
541 df751fa8 aliguori
}
542 df751fa8 aliguori
543 df751fa8 aliguori
void qemu_balloon(ram_addr_t target)
544 df751fa8 aliguori
{
545 df751fa8 aliguori
    if (qemu_balloon_event)
546 df751fa8 aliguori
        qemu_balloon_event(qemu_balloon_event_opaque, target);
547 df751fa8 aliguori
}
548 df751fa8 aliguori
549 df751fa8 aliguori
ram_addr_t qemu_balloon_status(void)
550 df751fa8 aliguori
{
551 df751fa8 aliguori
    if (qemu_balloon_event)
552 df751fa8 aliguori
        return qemu_balloon_event(qemu_balloon_event_opaque, 0);
553 df751fa8 aliguori
    return 0;
554 df751fa8 aliguori
}
555 0824d6fc bellard
556 8a7ddc38 bellard
/***********************************************************/
557 63066f4f bellard
/* keyboard/mouse */
558 63066f4f bellard
559 63066f4f bellard
static QEMUPutKBDEvent *qemu_put_kbd_event;
560 63066f4f bellard
static void *qemu_put_kbd_event_opaque;
561 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
562 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
563 63066f4f bellard
564 63066f4f bellard
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
565 63066f4f bellard
{
566 63066f4f bellard
    qemu_put_kbd_event_opaque = opaque;
567 63066f4f bellard
    qemu_put_kbd_event = func;
568 63066f4f bellard
}
569 63066f4f bellard
570 455204eb ths
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
571 455204eb ths
                                                void *opaque, int absolute,
572 455204eb ths
                                                const char *name)
573 63066f4f bellard
{
574 455204eb ths
    QEMUPutMouseEntry *s, *cursor;
575 455204eb ths
576 455204eb ths
    s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
577 455204eb ths
578 455204eb ths
    s->qemu_put_mouse_event = func;
579 455204eb ths
    s->qemu_put_mouse_event_opaque = opaque;
580 455204eb ths
    s->qemu_put_mouse_event_absolute = absolute;
581 455204eb ths
    s->qemu_put_mouse_event_name = qemu_strdup(name);
582 455204eb ths
    s->next = NULL;
583 455204eb ths
584 455204eb ths
    if (!qemu_put_mouse_event_head) {
585 455204eb ths
        qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
586 455204eb ths
        return s;
587 455204eb ths
    }
588 455204eb ths
589 455204eb ths
    cursor = qemu_put_mouse_event_head;
590 455204eb ths
    while (cursor->next != NULL)
591 455204eb ths
        cursor = cursor->next;
592 455204eb ths
593 455204eb ths
    cursor->next = s;
594 455204eb ths
    qemu_put_mouse_event_current = s;
595 455204eb ths
596 455204eb ths
    return s;
597 455204eb ths
}
598 455204eb ths
599 455204eb ths
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
600 455204eb ths
{
601 455204eb ths
    QEMUPutMouseEntry *prev = NULL, *cursor;
602 455204eb ths
603 455204eb ths
    if (!qemu_put_mouse_event_head || entry == NULL)
604 455204eb ths
        return;
605 455204eb ths
606 455204eb ths
    cursor = qemu_put_mouse_event_head;
607 455204eb ths
    while (cursor != NULL && cursor != entry) {
608 455204eb ths
        prev = cursor;
609 455204eb ths
        cursor = cursor->next;
610 455204eb ths
    }
611 455204eb ths
612 455204eb ths
    if (cursor == NULL) // does not exist or list empty
613 455204eb ths
        return;
614 455204eb ths
    else if (prev == NULL) { // entry is head
615 455204eb ths
        qemu_put_mouse_event_head = cursor->next;
616 455204eb ths
        if (qemu_put_mouse_event_current == entry)
617 455204eb ths
            qemu_put_mouse_event_current = cursor->next;
618 455204eb ths
        qemu_free(entry->qemu_put_mouse_event_name);
619 455204eb ths
        qemu_free(entry);
620 455204eb ths
        return;
621 455204eb ths
    }
622 455204eb ths
623 455204eb ths
    prev->next = entry->next;
624 455204eb ths
625 455204eb ths
    if (qemu_put_mouse_event_current == entry)
626 455204eb ths
        qemu_put_mouse_event_current = prev;
627 455204eb ths
628 455204eb ths
    qemu_free(entry->qemu_put_mouse_event_name);
629 455204eb ths
    qemu_free(entry);
630 63066f4f bellard
}
631 63066f4f bellard
632 63066f4f bellard
void kbd_put_keycode(int keycode)
633 63066f4f bellard
{
634 63066f4f bellard
    if (qemu_put_kbd_event) {
635 63066f4f bellard
        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
636 63066f4f bellard
    }
637 63066f4f bellard
}
638 63066f4f bellard
639 63066f4f bellard
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
640 63066f4f bellard
{
641 455204eb ths
    QEMUPutMouseEvent *mouse_event;
642 455204eb ths
    void *mouse_event_opaque;
643 a171fe39 balrog
    int width;
644 455204eb ths
645 455204eb ths
    if (!qemu_put_mouse_event_current) {
646 455204eb ths
        return;
647 455204eb ths
    }
648 455204eb ths
649 455204eb ths
    mouse_event =
650 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event;
651 455204eb ths
    mouse_event_opaque =
652 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
653 455204eb ths
654 455204eb ths
    if (mouse_event) {
655 a171fe39 balrog
        if (graphic_rotate) {
656 a171fe39 balrog
            if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
657 a171fe39 balrog
                width = 0x7fff;
658 a171fe39 balrog
            else
659 b94ed577 aurel32
                width = graphic_width - 1;
660 a171fe39 balrog
            mouse_event(mouse_event_opaque,
661 a171fe39 balrog
                                 width - dy, dx, dz, buttons_state);
662 a171fe39 balrog
        } else
663 a171fe39 balrog
            mouse_event(mouse_event_opaque,
664 a171fe39 balrog
                                 dx, dy, dz, buttons_state);
665 63066f4f bellard
    }
666 63066f4f bellard
}
667 63066f4f bellard
668 09b26c5e bellard
int kbd_mouse_is_absolute(void)
669 09b26c5e bellard
{
670 455204eb ths
    if (!qemu_put_mouse_event_current)
671 455204eb ths
        return 0;
672 455204eb ths
673 455204eb ths
    return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
674 455204eb ths
}
675 455204eb ths
676 376253ec aliguori
void do_info_mice(Monitor *mon)
677 455204eb ths
{
678 455204eb ths
    QEMUPutMouseEntry *cursor;
679 455204eb ths
    int index = 0;
680 455204eb ths
681 455204eb ths
    if (!qemu_put_mouse_event_head) {
682 376253ec aliguori
        monitor_printf(mon, "No mouse devices connected\n");
683 455204eb ths
        return;
684 455204eb ths
    }
685 455204eb ths
686 376253ec aliguori
    monitor_printf(mon, "Mouse devices available:\n");
687 455204eb ths
    cursor = qemu_put_mouse_event_head;
688 455204eb ths
    while (cursor != NULL) {
689 376253ec aliguori
        monitor_printf(mon, "%c Mouse #%d: %s\n",
690 376253ec aliguori
                       (cursor == qemu_put_mouse_event_current ? '*' : ' '),
691 376253ec aliguori
                       index, cursor->qemu_put_mouse_event_name);
692 455204eb ths
        index++;
693 455204eb ths
        cursor = cursor->next;
694 455204eb ths
    }
695 455204eb ths
}
696 455204eb ths
697 376253ec aliguori
void do_mouse_set(Monitor *mon, int index)
698 455204eb ths
{
699 455204eb ths
    QEMUPutMouseEntry *cursor;
700 455204eb ths
    int i = 0;
701 455204eb ths
702 455204eb ths
    if (!qemu_put_mouse_event_head) {
703 376253ec aliguori
        monitor_printf(mon, "No mouse devices connected\n");
704 455204eb ths
        return;
705 455204eb ths
    }
706 455204eb ths
707 455204eb ths
    cursor = qemu_put_mouse_event_head;
708 455204eb ths
    while (cursor != NULL && index != i) {
709 455204eb ths
        i++;
710 455204eb ths
        cursor = cursor->next;
711 455204eb ths
    }
712 455204eb ths
713 455204eb ths
    if (cursor != NULL)
714 455204eb ths
        qemu_put_mouse_event_current = cursor;
715 455204eb ths
    else
716 376253ec aliguori
        monitor_printf(mon, "Mouse at given index not found\n");
717 09b26c5e bellard
}
718 09b26c5e bellard
719 1dce7c3c bellard
/* compute with 96 bit intermediate result: (a*b)/c */
720 1dce7c3c bellard
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
721 0824d6fc bellard
{
722 1dce7c3c bellard
    union {
723 1dce7c3c bellard
        uint64_t ll;
724 1dce7c3c bellard
        struct {
725 1dce7c3c bellard
#ifdef WORDS_BIGENDIAN
726 1dce7c3c bellard
            uint32_t high, low;
727 1dce7c3c bellard
#else
728 1dce7c3c bellard
            uint32_t low, high;
729 3b46e624 ths
#endif
730 1dce7c3c bellard
        } l;
731 1dce7c3c bellard
    } u, res;
732 1dce7c3c bellard
    uint64_t rl, rh;
733 0824d6fc bellard
734 1dce7c3c bellard
    u.ll = a;
735 1dce7c3c bellard
    rl = (uint64_t)u.l.low * (uint64_t)b;
736 1dce7c3c bellard
    rh = (uint64_t)u.l.high * (uint64_t)b;
737 1dce7c3c bellard
    rh += (rl >> 32);
738 1dce7c3c bellard
    res.l.high = rh / c;
739 1dce7c3c bellard
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
740 1dce7c3c bellard
    return res.ll;
741 34865134 bellard
}
742 34865134 bellard
743 1dce7c3c bellard
/***********************************************************/
744 1dce7c3c bellard
/* real time host monotonic timer */
745 34865134 bellard
746 1dce7c3c bellard
#define QEMU_TIMER_BASE 1000000000LL
747 34865134 bellard
748 1dce7c3c bellard
#ifdef WIN32
749 0824d6fc bellard
750 1dce7c3c bellard
static int64_t clock_freq;
751 1115dde7 bellard
752 1dce7c3c bellard
static void init_get_clock(void)
753 1115dde7 bellard
{
754 a8e5ac33 bellard
    LARGE_INTEGER freq;
755 a8e5ac33 bellard
    int ret;
756 1dce7c3c bellard
    ret = QueryPerformanceFrequency(&freq);
757 1dce7c3c bellard
    if (ret == 0) {
758 1dce7c3c bellard
        fprintf(stderr, "Could not calibrate ticks\n");
759 1dce7c3c bellard
        exit(1);
760 1dce7c3c bellard
    }
761 1dce7c3c bellard
    clock_freq = freq.QuadPart;
762 1115dde7 bellard
}
763 1115dde7 bellard
764 1dce7c3c bellard
static int64_t get_clock(void)
765 b8076a74 bellard
{
766 1dce7c3c bellard
    LARGE_INTEGER ti;
767 1dce7c3c bellard
    QueryPerformanceCounter(&ti);
768 1dce7c3c bellard
    return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
769 b8076a74 bellard
}
770 b8076a74 bellard
771 1dce7c3c bellard
#else
772 90cb9493 bellard
773 1dce7c3c bellard
static int use_rt_clock;
774 1dce7c3c bellard
775 1dce7c3c bellard
static void init_get_clock(void)
776 90cb9493 bellard
{
777 1dce7c3c bellard
    use_rt_clock = 0;
778 c5e97233 blueswir1
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
779 c5e97233 blueswir1
    || defined(__DragonFly__)
780 1dce7c3c bellard
    {
781 1dce7c3c bellard
        struct timespec ts;
782 1dce7c3c bellard
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
783 1dce7c3c bellard
            use_rt_clock = 1;
784 1dce7c3c bellard
        }
785 1dce7c3c bellard
    }
786 1dce7c3c bellard
#endif
787 90cb9493 bellard
}
788 90cb9493 bellard
789 1dce7c3c bellard
static int64_t get_clock(void)
790 fdbb4691 bellard
{
791 c5e97233 blueswir1
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
792 c5e97233 blueswir1
        || defined(__DragonFly__)
793 1dce7c3c bellard
    if (use_rt_clock) {
794 1dce7c3c bellard
        struct timespec ts;
795 1dce7c3c bellard
        clock_gettime(CLOCK_MONOTONIC, &ts);
796 1dce7c3c bellard
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
797 5fafdf24 ths
    } else
798 fdbb4691 bellard
#endif
799 1dce7c3c bellard
    {
800 1dce7c3c bellard
        /* XXX: using gettimeofday leads to problems if the date
801 1dce7c3c bellard
           changes, so it should be avoided. */
802 1dce7c3c bellard
        struct timeval tv;
803 1dce7c3c bellard
        gettimeofday(&tv, NULL);
804 1dce7c3c bellard
        return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
805 1dce7c3c bellard
    }
806 fdbb4691 bellard
}
807 34865134 bellard
#endif
808 34865134 bellard
809 2e70f6ef pbrook
/* Return the virtual CPU time, based on the instruction counter.  */
810 2e70f6ef pbrook
static int64_t cpu_get_icount(void)
811 2e70f6ef pbrook
{
812 2e70f6ef pbrook
    int64_t icount;
813 2e70f6ef pbrook
    CPUState *env = cpu_single_env;;
814 2e70f6ef pbrook
    icount = qemu_icount;
815 2e70f6ef pbrook
    if (env) {
816 2e70f6ef pbrook
        if (!can_do_io(env))
817 2e70f6ef pbrook
            fprintf(stderr, "Bad clock read\n");
818 2e70f6ef pbrook
        icount -= (env->icount_decr.u16.low + env->icount_extra);
819 2e70f6ef pbrook
    }
820 2e70f6ef pbrook
    return qemu_icount_bias + (icount << icount_time_shift);
821 2e70f6ef pbrook
}
822 2e70f6ef pbrook
823 1dce7c3c bellard
/***********************************************************/
824 1dce7c3c bellard
/* guest cycle counter */
825 1dce7c3c bellard
826 eade0f19 bellard
static int64_t cpu_ticks_prev;
827 34865134 bellard
static int64_t cpu_ticks_offset;
828 1dce7c3c bellard
static int64_t cpu_clock_offset;
829 8a7ddc38 bellard
static int cpu_ticks_enabled;
830 34865134 bellard
831 1dce7c3c bellard
/* return the host CPU cycle counter and handle stop/restart */
832 1dce7c3c bellard
int64_t cpu_get_ticks(void)
833 34865134 bellard
{
834 2e70f6ef pbrook
    if (use_icount) {
835 2e70f6ef pbrook
        return cpu_get_icount();
836 2e70f6ef pbrook
    }
837 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
838 8a7ddc38 bellard
        return cpu_ticks_offset;
839 8a7ddc38 bellard
    } else {
840 eade0f19 bellard
        int64_t ticks;
841 eade0f19 bellard
        ticks = cpu_get_real_ticks();
842 eade0f19 bellard
        if (cpu_ticks_prev > ticks) {
843 eade0f19 bellard
            /* Note: non increasing ticks may happen if the host uses
844 eade0f19 bellard
               software suspend */
845 eade0f19 bellard
            cpu_ticks_offset += cpu_ticks_prev - ticks;
846 eade0f19 bellard
        }
847 eade0f19 bellard
        cpu_ticks_prev = ticks;
848 eade0f19 bellard
        return ticks + cpu_ticks_offset;
849 8a7ddc38 bellard
    }
850 34865134 bellard
}
851 34865134 bellard
852 1dce7c3c bellard
/* return the host CPU monotonic timer and handle stop/restart */
853 1dce7c3c bellard
static int64_t cpu_get_clock(void)
854 1dce7c3c bellard
{
855 1dce7c3c bellard
    int64_t ti;
856 1dce7c3c bellard
    if (!cpu_ticks_enabled) {
857 1dce7c3c bellard
        return cpu_clock_offset;
858 1dce7c3c bellard
    } else {
859 1dce7c3c bellard
        ti = get_clock();
860 1dce7c3c bellard
        return ti + cpu_clock_offset;
861 1dce7c3c bellard
    }
862 1dce7c3c bellard
}
863 1dce7c3c bellard
864 34865134 bellard
/* enable cpu_get_ticks() */
865 34865134 bellard
void cpu_enable_ticks(void)
866 34865134 bellard
{
867 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
868 8a7ddc38 bellard
        cpu_ticks_offset -= cpu_get_real_ticks();
869 1dce7c3c bellard
        cpu_clock_offset -= get_clock();
870 8a7ddc38 bellard
        cpu_ticks_enabled = 1;
871 8a7ddc38 bellard
    }
872 34865134 bellard
}
873 34865134 bellard
874 34865134 bellard
/* disable cpu_get_ticks() : the clock is stopped. You must not call
875 34865134 bellard
   cpu_get_ticks() after that.  */
876 34865134 bellard
void cpu_disable_ticks(void)
877 34865134 bellard
{
878 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
879 8a7ddc38 bellard
        cpu_ticks_offset = cpu_get_ticks();
880 1dce7c3c bellard
        cpu_clock_offset = cpu_get_clock();
881 8a7ddc38 bellard
        cpu_ticks_enabled = 0;
882 8a7ddc38 bellard
    }
883 34865134 bellard
}
884 34865134 bellard
885 1dce7c3c bellard
/***********************************************************/
886 1dce7c3c bellard
/* timers */
887 5fafdf24 ths
888 8a7ddc38 bellard
#define QEMU_TIMER_REALTIME 0
889 8a7ddc38 bellard
#define QEMU_TIMER_VIRTUAL  1
890 8a7ddc38 bellard
891 8a7ddc38 bellard
struct QEMUClock {
892 8a7ddc38 bellard
    int type;
893 8a7ddc38 bellard
    /* XXX: add frequency */
894 8a7ddc38 bellard
};
895 8a7ddc38 bellard
896 8a7ddc38 bellard
struct QEMUTimer {
897 8a7ddc38 bellard
    QEMUClock *clock;
898 8a7ddc38 bellard
    int64_t expire_time;
899 8a7ddc38 bellard
    QEMUTimerCB *cb;
900 8a7ddc38 bellard
    void *opaque;
901 8a7ddc38 bellard
    struct QEMUTimer *next;
902 8a7ddc38 bellard
};
903 8a7ddc38 bellard
904 c8994013 ths
struct qemu_alarm_timer {
905 c8994013 ths
    char const *name;
906 efe75411 ths
    unsigned int flags;
907 c8994013 ths
908 c8994013 ths
    int (*start)(struct qemu_alarm_timer *t);
909 c8994013 ths
    void (*stop)(struct qemu_alarm_timer *t);
910 efe75411 ths
    void (*rearm)(struct qemu_alarm_timer *t);
911 c8994013 ths
    void *priv;
912 c8994013 ths
};
913 c8994013 ths
914 efe75411 ths
#define ALARM_FLAG_DYNTICKS  0x1
915 d5d08334 balrog
#define ALARM_FLAG_EXPIRED   0x2
916 efe75411 ths
917 efe75411 ths
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
918 efe75411 ths
{
919 e332340a Jean-Christophe Dubois
    return t && (t->flags & ALARM_FLAG_DYNTICKS);
920 efe75411 ths
}
921 efe75411 ths
922 efe75411 ths
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
923 efe75411 ths
{
924 efe75411 ths
    if (!alarm_has_dynticks(t))
925 efe75411 ths
        return;
926 efe75411 ths
927 efe75411 ths
    t->rearm(t);
928 efe75411 ths
}
929 efe75411 ths
930 efe75411 ths
/* TODO: MIN_TIMER_REARM_US should be optimized */
931 efe75411 ths
#define MIN_TIMER_REARM_US 250
932 efe75411 ths
933 c8994013 ths
static struct qemu_alarm_timer *alarm_timer;
934 8a7ddc38 bellard
935 40c3bac3 bellard
#ifdef _WIN32
936 c8994013 ths
937 c8994013 ths
struct qemu_alarm_win32 {
938 c8994013 ths
    MMRESULT timerId;
939 c8994013 ths
    unsigned int period;
940 ef28c4b0 Blue Swirl
} alarm_win32_data = {0, -1};
941 c8994013 ths
942 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t);
943 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t);
944 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t);
945 c8994013 ths
946 40c3bac3 bellard
#else
947 c8994013 ths
948 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t);
949 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t);
950 c8994013 ths
951 231c6586 ths
#ifdef __linux__
952 231c6586 ths
953 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t);
954 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
955 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
956 efe75411 ths
957 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t);
958 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t);
959 c40ec5a9 ths
960 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t);
961 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t);
962 c8994013 ths
963 efe75411 ths
#endif /* __linux__ */
964 8a7ddc38 bellard
965 c8994013 ths
#endif /* _WIN32 */
966 c8994013 ths
967 2e70f6ef pbrook
/* Correlation between real and virtual time is always going to be
968 bf20dc07 ths
   fairly approximate, so ignore small variation.
969 2e70f6ef pbrook
   When the guest is idle real and virtual time will be aligned in
970 2e70f6ef pbrook
   the IO wait loop.  */
971 2e70f6ef pbrook
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
972 2e70f6ef pbrook
973 2e70f6ef pbrook
static void icount_adjust(void)
974 2e70f6ef pbrook
{
975 2e70f6ef pbrook
    int64_t cur_time;
976 2e70f6ef pbrook
    int64_t cur_icount;
977 2e70f6ef pbrook
    int64_t delta;
978 2e70f6ef pbrook
    static int64_t last_delta;
979 2e70f6ef pbrook
    /* If the VM is not running, then do nothing.  */
980 2e70f6ef pbrook
    if (!vm_running)
981 2e70f6ef pbrook
        return;
982 2e70f6ef pbrook
983 2e70f6ef pbrook
    cur_time = cpu_get_clock();
984 2e70f6ef pbrook
    cur_icount = qemu_get_clock(vm_clock);
985 2e70f6ef pbrook
    delta = cur_icount - cur_time;
986 2e70f6ef pbrook
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
987 2e70f6ef pbrook
    if (delta > 0
988 2e70f6ef pbrook
        && last_delta + ICOUNT_WOBBLE < delta * 2
989 2e70f6ef pbrook
        && icount_time_shift > 0) {
990 2e70f6ef pbrook
        /* The guest is getting too far ahead.  Slow time down.  */
991 2e70f6ef pbrook
        icount_time_shift--;
992 2e70f6ef pbrook
    }
993 2e70f6ef pbrook
    if (delta < 0
994 2e70f6ef pbrook
        && last_delta - ICOUNT_WOBBLE > delta * 2
995 2e70f6ef pbrook
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
996 2e70f6ef pbrook
        /* The guest is getting too far behind.  Speed time up.  */
997 2e70f6ef pbrook
        icount_time_shift++;
998 2e70f6ef pbrook
    }
999 2e70f6ef pbrook
    last_delta = delta;
1000 2e70f6ef pbrook
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
1001 2e70f6ef pbrook
}
1002 2e70f6ef pbrook
1003 2e70f6ef pbrook
static void icount_adjust_rt(void * opaque)
1004 2e70f6ef pbrook
{
1005 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
1006 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
1007 2e70f6ef pbrook
    icount_adjust();
1008 2e70f6ef pbrook
}
1009 2e70f6ef pbrook
1010 2e70f6ef pbrook
static void icount_adjust_vm(void * opaque)
1011 2e70f6ef pbrook
{
1012 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
1013 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1014 2e70f6ef pbrook
    icount_adjust();
1015 2e70f6ef pbrook
}
1016 2e70f6ef pbrook
1017 2e70f6ef pbrook
static void init_icount_adjust(void)
1018 2e70f6ef pbrook
{
1019 2e70f6ef pbrook
    /* Have both realtime and virtual time triggers for speed adjustment.
1020 2e70f6ef pbrook
       The realtime trigger catches emulated time passing too slowly,
1021 2e70f6ef pbrook
       the virtual time trigger catches emulated time passing too fast.
1022 2e70f6ef pbrook
       Realtime triggers occur even when idle, so use them less frequently
1023 2e70f6ef pbrook
       than VM triggers.  */
1024 2e70f6ef pbrook
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1025 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
1026 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
1027 2e70f6ef pbrook
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1028 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
1029 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1030 2e70f6ef pbrook
}
1031 2e70f6ef pbrook
1032 c8994013 ths
static struct qemu_alarm_timer alarm_timers[] = {
1033 efe75411 ths
#ifndef _WIN32
1034 231c6586 ths
#ifdef __linux__
1035 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1036 efe75411 ths
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
1037 c40ec5a9 ths
    /* HPET - if available - is preferred */
1038 efe75411 ths
    {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
1039 c40ec5a9 ths
    /* ...otherwise try RTC */
1040 efe75411 ths
    {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
1041 c8994013 ths
#endif
1042 efe75411 ths
    {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
1043 c8994013 ths
#else
1044 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1045 efe75411 ths
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1046 efe75411 ths
    {"win32", 0, win32_start_timer,
1047 efe75411 ths
     win32_stop_timer, NULL, &alarm_win32_data},
1048 c8994013 ths
#endif
1049 c8994013 ths
    {NULL, }
1050 c8994013 ths
};
1051 c8994013 ths
1052 3f47aa8c blueswir1
static void show_available_alarms(void)
1053 f3dcfada ths
{
1054 f3dcfada ths
    int i;
1055 f3dcfada ths
1056 f3dcfada ths
    printf("Available alarm timers, in order of precedence:\n");
1057 f3dcfada ths
    for (i = 0; alarm_timers[i].name; i++)
1058 f3dcfada ths
        printf("%s\n", alarm_timers[i].name);
1059 f3dcfada ths
}
1060 f3dcfada ths
1061 f3dcfada ths
static void configure_alarms(char const *opt)
1062 f3dcfada ths
{
1063 f3dcfada ths
    int i;
1064 f3dcfada ths
    int cur = 0;
1065 b1503cda malc
    int count = ARRAY_SIZE(alarm_timers) - 1;
1066 f3dcfada ths
    char *arg;
1067 f3dcfada ths
    char *name;
1068 2e70f6ef pbrook
    struct qemu_alarm_timer tmp;
1069 f3dcfada ths
1070 3adda04c aurel32
    if (!strcmp(opt, "?")) {
1071 f3dcfada ths
        show_available_alarms();
1072 f3dcfada ths
        exit(0);
1073 f3dcfada ths
    }
1074 f3dcfada ths
1075 f3dcfada ths
    arg = strdup(opt);
1076 f3dcfada ths
1077 f3dcfada ths
    /* Reorder the array */
1078 f3dcfada ths
    name = strtok(arg, ",");
1079 f3dcfada ths
    while (name) {
1080 e2b577e5 balrog
        for (i = 0; i < count && alarm_timers[i].name; i++) {
1081 f3dcfada ths
            if (!strcmp(alarm_timers[i].name, name))
1082 f3dcfada ths
                break;
1083 f3dcfada ths
        }
1084 f3dcfada ths
1085 f3dcfada ths
        if (i == count) {
1086 f3dcfada ths
            fprintf(stderr, "Unknown clock %s\n", name);
1087 f3dcfada ths
            goto next;
1088 f3dcfada ths
        }
1089 f3dcfada ths
1090 f3dcfada ths
        if (i < cur)
1091 f3dcfada ths
            /* Ignore */
1092 f3dcfada ths
            goto next;
1093 f3dcfada ths
1094 f3dcfada ths
        /* Swap */
1095 f3dcfada ths
        tmp = alarm_timers[i];
1096 f3dcfada ths
        alarm_timers[i] = alarm_timers[cur];
1097 f3dcfada ths
        alarm_timers[cur] = tmp;
1098 f3dcfada ths
1099 f3dcfada ths
        cur++;
1100 f3dcfada ths
next:
1101 f3dcfada ths
        name = strtok(NULL, ",");
1102 f3dcfada ths
    }
1103 f3dcfada ths
1104 f3dcfada ths
    free(arg);
1105 f3dcfada ths
1106 f3dcfada ths
    if (cur) {
1107 2e70f6ef pbrook
        /* Disable remaining timers */
1108 f3dcfada ths
        for (i = cur; i < count; i++)
1109 f3dcfada ths
            alarm_timers[i].name = NULL;
1110 3adda04c aurel32
    } else {
1111 3adda04c aurel32
        show_available_alarms();
1112 3adda04c aurel32
        exit(1);
1113 f3dcfada ths
    }
1114 f3dcfada ths
}
1115 f3dcfada ths
1116 c8994013 ths
QEMUClock *rt_clock;
1117 c8994013 ths
QEMUClock *vm_clock;
1118 c8994013 ths
1119 c8994013 ths
static QEMUTimer *active_timers[2];
1120 c8994013 ths
1121 9596ebb7 pbrook
static QEMUClock *qemu_new_clock(int type)
1122 8a7ddc38 bellard
{
1123 8a7ddc38 bellard
    QEMUClock *clock;
1124 8a7ddc38 bellard
    clock = qemu_mallocz(sizeof(QEMUClock));
1125 8a7ddc38 bellard
    clock->type = type;
1126 8a7ddc38 bellard
    return clock;
1127 8a7ddc38 bellard
}
1128 8a7ddc38 bellard
1129 8a7ddc38 bellard
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1130 8a7ddc38 bellard
{
1131 8a7ddc38 bellard
    QEMUTimer *ts;
1132 8a7ddc38 bellard
1133 8a7ddc38 bellard
    ts = qemu_mallocz(sizeof(QEMUTimer));
1134 8a7ddc38 bellard
    ts->clock = clock;
1135 8a7ddc38 bellard
    ts->cb = cb;
1136 8a7ddc38 bellard
    ts->opaque = opaque;
1137 8a7ddc38 bellard
    return ts;
1138 8a7ddc38 bellard
}
1139 8a7ddc38 bellard
1140 8a7ddc38 bellard
void qemu_free_timer(QEMUTimer *ts)
1141 8a7ddc38 bellard
{
1142 8a7ddc38 bellard
    qemu_free(ts);
1143 8a7ddc38 bellard
}
1144 8a7ddc38 bellard
1145 8a7ddc38 bellard
/* stop a timer, but do not dealloc it */
1146 8a7ddc38 bellard
void qemu_del_timer(QEMUTimer *ts)
1147 8a7ddc38 bellard
{
1148 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1149 8a7ddc38 bellard
1150 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1151 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1152 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1153 8a7ddc38 bellard
    for(;;) {
1154 8a7ddc38 bellard
        t = *pt;
1155 8a7ddc38 bellard
        if (!t)
1156 8a7ddc38 bellard
            break;
1157 8a7ddc38 bellard
        if (t == ts) {
1158 8a7ddc38 bellard
            *pt = t->next;
1159 8a7ddc38 bellard
            break;
1160 8a7ddc38 bellard
        }
1161 8a7ddc38 bellard
        pt = &t->next;
1162 8a7ddc38 bellard
    }
1163 8a7ddc38 bellard
}
1164 8a7ddc38 bellard
1165 8a7ddc38 bellard
/* modify the current timer so that it will be fired when current_time
1166 8a7ddc38 bellard
   >= expire_time. The corresponding callback will be called. */
1167 8a7ddc38 bellard
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1168 8a7ddc38 bellard
{
1169 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1170 8a7ddc38 bellard
1171 8a7ddc38 bellard
    qemu_del_timer(ts);
1172 8a7ddc38 bellard
1173 8a7ddc38 bellard
    /* add the timer in the sorted list */
1174 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1175 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1176 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1177 8a7ddc38 bellard
    for(;;) {
1178 8a7ddc38 bellard
        t = *pt;
1179 8a7ddc38 bellard
        if (!t)
1180 8a7ddc38 bellard
            break;
1181 5fafdf24 ths
        if (t->expire_time > expire_time)
1182 8a7ddc38 bellard
            break;
1183 8a7ddc38 bellard
        pt = &t->next;
1184 8a7ddc38 bellard
    }
1185 8a7ddc38 bellard
    ts->expire_time = expire_time;
1186 8a7ddc38 bellard
    ts->next = *pt;
1187 8a7ddc38 bellard
    *pt = ts;
1188 d5d08334 balrog
1189 d5d08334 balrog
    /* Rearm if necessary  */
1190 2e70f6ef pbrook
    if (pt == &active_timers[ts->clock->type]) {
1191 2e70f6ef pbrook
        if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1192 2e70f6ef pbrook
            qemu_rearm_alarm_timer(alarm_timer);
1193 2e70f6ef pbrook
        }
1194 2e70f6ef pbrook
        /* Interrupt execution to force deadline recalculation.  */
1195 d9f75a4e aliguori
        if (use_icount)
1196 d9f75a4e aliguori
            qemu_notify_event();
1197 2e70f6ef pbrook
    }
1198 8a7ddc38 bellard
}
1199 8a7ddc38 bellard
1200 8a7ddc38 bellard
int qemu_timer_pending(QEMUTimer *ts)
1201 8a7ddc38 bellard
{
1202 8a7ddc38 bellard
    QEMUTimer *t;
1203 8a7ddc38 bellard
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1204 8a7ddc38 bellard
        if (t == ts)
1205 8a7ddc38 bellard
            return 1;
1206 8a7ddc38 bellard
    }
1207 8a7ddc38 bellard
    return 0;
1208 8a7ddc38 bellard
}
1209 8a7ddc38 bellard
1210 8a7ddc38 bellard
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1211 8a7ddc38 bellard
{
1212 8a7ddc38 bellard
    if (!timer_head)
1213 8a7ddc38 bellard
        return 0;
1214 8a7ddc38 bellard
    return (timer_head->expire_time <= current_time);
1215 8a7ddc38 bellard
}
1216 8a7ddc38 bellard
1217 8a7ddc38 bellard
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1218 8a7ddc38 bellard
{
1219 8a7ddc38 bellard
    QEMUTimer *ts;
1220 3b46e624 ths
1221 8a7ddc38 bellard
    for(;;) {
1222 8a7ddc38 bellard
        ts = *ptimer_head;
1223 e95c8d51 bellard
        if (!ts || ts->expire_time > current_time)
1224 8a7ddc38 bellard
            break;
1225 8a7ddc38 bellard
        /* remove timer from the list before calling the callback */
1226 8a7ddc38 bellard
        *ptimer_head = ts->next;
1227 8a7ddc38 bellard
        ts->next = NULL;
1228 3b46e624 ths
1229 8a7ddc38 bellard
        /* run the callback (the timer list can be modified) */
1230 8a7ddc38 bellard
        ts->cb(ts->opaque);
1231 8a7ddc38 bellard
    }
1232 8a7ddc38 bellard
}
1233 8a7ddc38 bellard
1234 8a7ddc38 bellard
int64_t qemu_get_clock(QEMUClock *clock)
1235 8a7ddc38 bellard
{
1236 8a7ddc38 bellard
    switch(clock->type) {
1237 8a7ddc38 bellard
    case QEMU_TIMER_REALTIME:
1238 1dce7c3c bellard
        return get_clock() / 1000000;
1239 8a7ddc38 bellard
    default:
1240 8a7ddc38 bellard
    case QEMU_TIMER_VIRTUAL:
1241 2e70f6ef pbrook
        if (use_icount) {
1242 2e70f6ef pbrook
            return cpu_get_icount();
1243 2e70f6ef pbrook
        } else {
1244 2e70f6ef pbrook
            return cpu_get_clock();
1245 2e70f6ef pbrook
        }
1246 8a7ddc38 bellard
    }
1247 8a7ddc38 bellard
}
1248 8a7ddc38 bellard
1249 1dce7c3c bellard
static void init_timers(void)
1250 1dce7c3c bellard
{
1251 1dce7c3c bellard
    init_get_clock();
1252 1dce7c3c bellard
    ticks_per_sec = QEMU_TIMER_BASE;
1253 1dce7c3c bellard
    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1254 1dce7c3c bellard
    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1255 1dce7c3c bellard
}
1256 1dce7c3c bellard
1257 8a7ddc38 bellard
/* save a timer */
1258 8a7ddc38 bellard
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1259 8a7ddc38 bellard
{
1260 8a7ddc38 bellard
    uint64_t expire_time;
1261 8a7ddc38 bellard
1262 8a7ddc38 bellard
    if (qemu_timer_pending(ts)) {
1263 8a7ddc38 bellard
        expire_time = ts->expire_time;
1264 8a7ddc38 bellard
    } else {
1265 8a7ddc38 bellard
        expire_time = -1;
1266 8a7ddc38 bellard
    }
1267 8a7ddc38 bellard
    qemu_put_be64(f, expire_time);
1268 8a7ddc38 bellard
}
1269 8a7ddc38 bellard
1270 8a7ddc38 bellard
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1271 8a7ddc38 bellard
{
1272 8a7ddc38 bellard
    uint64_t expire_time;
1273 8a7ddc38 bellard
1274 8a7ddc38 bellard
    expire_time = qemu_get_be64(f);
1275 8a7ddc38 bellard
    if (expire_time != -1) {
1276 8a7ddc38 bellard
        qemu_mod_timer(ts, expire_time);
1277 8a7ddc38 bellard
    } else {
1278 8a7ddc38 bellard
        qemu_del_timer(ts);
1279 8a7ddc38 bellard
    }
1280 8a7ddc38 bellard
}
1281 8a7ddc38 bellard
1282 8a7ddc38 bellard
static void timer_save(QEMUFile *f, void *opaque)
1283 8a7ddc38 bellard
{
1284 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1285 8a7ddc38 bellard
        hw_error("cannot save state if virtual timers are running");
1286 8a7ddc38 bellard
    }
1287 bee8d684 ths
    qemu_put_be64(f, cpu_ticks_offset);
1288 bee8d684 ths
    qemu_put_be64(f, ticks_per_sec);
1289 bee8d684 ths
    qemu_put_be64(f, cpu_clock_offset);
1290 8a7ddc38 bellard
}
1291 8a7ddc38 bellard
1292 8a7ddc38 bellard
static int timer_load(QEMUFile *f, void *opaque, int version_id)
1293 8a7ddc38 bellard
{
1294 c88676f8 bellard
    if (version_id != 1 && version_id != 2)
1295 8a7ddc38 bellard
        return -EINVAL;
1296 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1297 8a7ddc38 bellard
        return -EINVAL;
1298 8a7ddc38 bellard
    }
1299 bee8d684 ths
    cpu_ticks_offset=qemu_get_be64(f);
1300 bee8d684 ths
    ticks_per_sec=qemu_get_be64(f);
1301 c88676f8 bellard
    if (version_id == 2) {
1302 bee8d684 ths
        cpu_clock_offset=qemu_get_be64(f);
1303 c88676f8 bellard
    }
1304 8a7ddc38 bellard
    return 0;
1305 8a7ddc38 bellard
}
1306 8a7ddc38 bellard
1307 50317c7f aliguori
static void qemu_event_increment(void);
1308 50317c7f aliguori
1309 67b915a5 bellard
#ifdef _WIN32
1310 b9e82a59 blueswir1
static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1311 b9e82a59 blueswir1
                                        DWORD_PTR dwUser, DWORD_PTR dw1,
1312 b9e82a59 blueswir1
                                        DWORD_PTR dw2)
1313 67b915a5 bellard
#else
1314 8a7ddc38 bellard
static void host_alarm_handler(int host_signum)
1315 67b915a5 bellard
#endif
1316 8a7ddc38 bellard
{
1317 02ba45c5 bellard
#if 0
1318 02ba45c5 bellard
#define DISP_FREQ 1000
1319 02ba45c5 bellard
    {
1320 02ba45c5 bellard
        static int64_t delta_min = INT64_MAX;
1321 02ba45c5 bellard
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
1322 02ba45c5 bellard
        static int count;
1323 02ba45c5 bellard
        ti = qemu_get_clock(vm_clock);
1324 02ba45c5 bellard
        if (last_clock != 0) {
1325 02ba45c5 bellard
            delta = ti - last_clock;
1326 02ba45c5 bellard
            if (delta < delta_min)
1327 02ba45c5 bellard
                delta_min = delta;
1328 02ba45c5 bellard
            if (delta > delta_max)
1329 02ba45c5 bellard
                delta_max = delta;
1330 02ba45c5 bellard
            delta_cum += delta;
1331 02ba45c5 bellard
            if (++count == DISP_FREQ) {
1332 26a76461 bellard
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1333 02ba45c5 bellard
                       muldiv64(delta_min, 1000000, ticks_per_sec),
1334 02ba45c5 bellard
                       muldiv64(delta_max, 1000000, ticks_per_sec),
1335 02ba45c5 bellard
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1336 02ba45c5 bellard
                       (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1337 02ba45c5 bellard
                count = 0;
1338 02ba45c5 bellard
                delta_min = INT64_MAX;
1339 02ba45c5 bellard
                delta_max = 0;
1340 02ba45c5 bellard
                delta_cum = 0;
1341 02ba45c5 bellard
            }
1342 02ba45c5 bellard
        }
1343 02ba45c5 bellard
        last_clock = ti;
1344 02ba45c5 bellard
    }
1345 02ba45c5 bellard
#endif
1346 efe75411 ths
    if (alarm_has_dynticks(alarm_timer) ||
1347 2e70f6ef pbrook
        (!use_icount &&
1348 2e70f6ef pbrook
            qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1349 2e70f6ef pbrook
                               qemu_get_clock(vm_clock))) ||
1350 8a7ddc38 bellard
        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1351 8a7ddc38 bellard
                           qemu_get_clock(rt_clock))) {
1352 50317c7f aliguori
        qemu_event_increment();
1353 e332340a Jean-Christophe Dubois
        if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1354 d5d08334 balrog
1355 d6dc3d42 aliguori
#ifndef CONFIG_IOTHREAD
1356 d6dc3d42 aliguori
        if (next_cpu) {
1357 4f8eb8da balrog
            /* stop the currently executing cpu because a timer occured */
1358 d6dc3d42 aliguori
            cpu_exit(next_cpu);
1359 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
1360 d6dc3d42 aliguori
            if (next_cpu->kqemu_enabled) {
1361 d6dc3d42 aliguori
                kqemu_cpu_interrupt(next_cpu);
1362 4f8eb8da balrog
            }
1363 ee5605e5 balrog
#endif
1364 4f8eb8da balrog
        }
1365 d6dc3d42 aliguori
#endif
1366 43b96858 aliguori
        timer_alarm_pending = 1;
1367 d9f75a4e aliguori
        qemu_notify_event();
1368 8a7ddc38 bellard
    }
1369 8a7ddc38 bellard
}
1370 8a7ddc38 bellard
1371 2e70f6ef pbrook
static int64_t qemu_next_deadline(void)
1372 efe75411 ths
{
1373 2e70f6ef pbrook
    int64_t delta;
1374 efe75411 ths
1375 efe75411 ths
    if (active_timers[QEMU_TIMER_VIRTUAL]) {
1376 2e70f6ef pbrook
        delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1377 2e70f6ef pbrook
                     qemu_get_clock(vm_clock);
1378 2e70f6ef pbrook
    } else {
1379 2e70f6ef pbrook
        /* To avoid problems with overflow limit this to 2^32.  */
1380 2e70f6ef pbrook
        delta = INT32_MAX;
1381 efe75411 ths
    }
1382 efe75411 ths
1383 2e70f6ef pbrook
    if (delta < 0)
1384 2e70f6ef pbrook
        delta = 0;
1385 efe75411 ths
1386 2e70f6ef pbrook
    return delta;
1387 2e70f6ef pbrook
}
1388 2e70f6ef pbrook
1389 8632fb9a blueswir1
#if defined(__linux__) || defined(_WIN32)
1390 2e70f6ef pbrook
static uint64_t qemu_next_deadline_dyntick(void)
1391 2e70f6ef pbrook
{
1392 2e70f6ef pbrook
    int64_t delta;
1393 2e70f6ef pbrook
    int64_t rtdelta;
1394 2e70f6ef pbrook
1395 2e70f6ef pbrook
    if (use_icount)
1396 2e70f6ef pbrook
        delta = INT32_MAX;
1397 2e70f6ef pbrook
    else
1398 2e70f6ef pbrook
        delta = (qemu_next_deadline() + 999) / 1000;
1399 2e70f6ef pbrook
1400 2e70f6ef pbrook
    if (active_timers[QEMU_TIMER_REALTIME]) {
1401 2e70f6ef pbrook
        rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1402 2e70f6ef pbrook
                 qemu_get_clock(rt_clock))*1000;
1403 2e70f6ef pbrook
        if (rtdelta < delta)
1404 2e70f6ef pbrook
            delta = rtdelta;
1405 2e70f6ef pbrook
    }
1406 2e70f6ef pbrook
1407 2e70f6ef pbrook
    if (delta < MIN_TIMER_REARM_US)
1408 2e70f6ef pbrook
        delta = MIN_TIMER_REARM_US;
1409 2e70f6ef pbrook
1410 2e70f6ef pbrook
    return delta;
1411 efe75411 ths
}
1412 8632fb9a blueswir1
#endif
1413 efe75411 ths
1414 fd872598 bellard
#ifndef _WIN32
1415 fd872598 bellard
1416 7183b4b4 aliguori
/* Sets a specific flag */
1417 7183b4b4 aliguori
static int fcntl_setfl(int fd, int flag)
1418 7183b4b4 aliguori
{
1419 7183b4b4 aliguori
    int flags;
1420 7183b4b4 aliguori
1421 7183b4b4 aliguori
    flags = fcntl(fd, F_GETFL);
1422 7183b4b4 aliguori
    if (flags == -1)
1423 7183b4b4 aliguori
        return -errno;
1424 7183b4b4 aliguori
1425 7183b4b4 aliguori
    if (fcntl(fd, F_SETFL, flags | flag) == -1)
1426 7183b4b4 aliguori
        return -errno;
1427 7183b4b4 aliguori
1428 7183b4b4 aliguori
    return 0;
1429 7183b4b4 aliguori
}
1430 7183b4b4 aliguori
1431 829309c7 bellard
#if defined(__linux__)
1432 829309c7 bellard
1433 fd872598 bellard
#define RTC_FREQ 1024
1434 fd872598 bellard
1435 de9a95f0 aurel32
static void enable_sigio_timer(int fd)
1436 c8994013 ths
{
1437 c8994013 ths
    struct sigaction act;
1438 c8994013 ths
1439 c8994013 ths
    /* timer signal */
1440 c8994013 ths
    sigfillset(&act.sa_mask);
1441 c8994013 ths
    act.sa_flags = 0;
1442 c8994013 ths
    act.sa_handler = host_alarm_handler;
1443 c8994013 ths
1444 c8994013 ths
    sigaction(SIGIO, &act, NULL);
1445 7183b4b4 aliguori
    fcntl_setfl(fd, O_ASYNC);
1446 c8994013 ths
    fcntl(fd, F_SETOWN, getpid());
1447 c8994013 ths
}
1448 829309c7 bellard
1449 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t)
1450 c40ec5a9 ths
{
1451 c40ec5a9 ths
    struct hpet_info info;
1452 c40ec5a9 ths
    int r, fd;
1453 c40ec5a9 ths
1454 c40ec5a9 ths
    fd = open("/dev/hpet", O_RDONLY);
1455 c40ec5a9 ths
    if (fd < 0)
1456 c40ec5a9 ths
        return -1;
1457 c40ec5a9 ths
1458 c40ec5a9 ths
    /* Set frequency */
1459 c40ec5a9 ths
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1460 c40ec5a9 ths
    if (r < 0) {
1461 c40ec5a9 ths
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1462 c40ec5a9 ths
                "error, but for better emulation accuracy type:\n"
1463 c40ec5a9 ths
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1464 c40ec5a9 ths
        goto fail;
1465 c40ec5a9 ths
    }
1466 c40ec5a9 ths
1467 c40ec5a9 ths
    /* Check capabilities */
1468 c40ec5a9 ths
    r = ioctl(fd, HPET_INFO, &info);
1469 c40ec5a9 ths
    if (r < 0)
1470 c40ec5a9 ths
        goto fail;
1471 c40ec5a9 ths
1472 c40ec5a9 ths
    /* Enable periodic mode */
1473 c40ec5a9 ths
    r = ioctl(fd, HPET_EPI, 0);
1474 c40ec5a9 ths
    if (info.hi_flags && (r < 0))
1475 c40ec5a9 ths
        goto fail;
1476 c40ec5a9 ths
1477 c40ec5a9 ths
    /* Enable interrupt */
1478 c40ec5a9 ths
    r = ioctl(fd, HPET_IE_ON, 0);
1479 c40ec5a9 ths
    if (r < 0)
1480 c40ec5a9 ths
        goto fail;
1481 c40ec5a9 ths
1482 c40ec5a9 ths
    enable_sigio_timer(fd);
1483 fcdc2129 pbrook
    t->priv = (void *)(long)fd;
1484 c40ec5a9 ths
1485 c40ec5a9 ths
    return 0;
1486 c40ec5a9 ths
fail:
1487 c40ec5a9 ths
    close(fd);
1488 c40ec5a9 ths
    return -1;
1489 c40ec5a9 ths
}
1490 c40ec5a9 ths
1491 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t)
1492 c40ec5a9 ths
{
1493 fcdc2129 pbrook
    int fd = (long)t->priv;
1494 c40ec5a9 ths
1495 c40ec5a9 ths
    close(fd);
1496 c40ec5a9 ths
}
1497 c40ec5a9 ths
1498 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t)
1499 fd872598 bellard
{
1500 c8994013 ths
    int rtc_fd;
1501 b5a23ad4 balrog
    unsigned long current_rtc_freq = 0;
1502 c8994013 ths
1503 aeb30be6 balrog
    TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1504 fd872598 bellard
    if (rtc_fd < 0)
1505 fd872598 bellard
        return -1;
1506 b5a23ad4 balrog
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1507 b5a23ad4 balrog
    if (current_rtc_freq != RTC_FREQ &&
1508 b5a23ad4 balrog
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1509 fd872598 bellard
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1510 fd872598 bellard
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1511 fd872598 bellard
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1512 fd872598 bellard
        goto fail;
1513 fd872598 bellard
    }
1514 fd872598 bellard
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1515 fd872598 bellard
    fail:
1516 fd872598 bellard
        close(rtc_fd);
1517 fd872598 bellard
        return -1;
1518 fd872598 bellard
    }
1519 c8994013 ths
1520 c8994013 ths
    enable_sigio_timer(rtc_fd);
1521 c8994013 ths
1522 fcdc2129 pbrook
    t->priv = (void *)(long)rtc_fd;
1523 c8994013 ths
1524 fd872598 bellard
    return 0;
1525 fd872598 bellard
}
1526 fd872598 bellard
1527 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t)
1528 829309c7 bellard
{
1529 fcdc2129 pbrook
    int rtc_fd = (long)t->priv;
1530 c8994013 ths
1531 c8994013 ths
    close(rtc_fd);
1532 829309c7 bellard
}
1533 829309c7 bellard
1534 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t)
1535 efe75411 ths
{
1536 efe75411 ths
    struct sigevent ev;
1537 efe75411 ths
    timer_t host_timer;
1538 efe75411 ths
    struct sigaction act;
1539 efe75411 ths
1540 efe75411 ths
    sigfillset(&act.sa_mask);
1541 efe75411 ths
    act.sa_flags = 0;
1542 efe75411 ths
    act.sa_handler = host_alarm_handler;
1543 efe75411 ths
1544 efe75411 ths
    sigaction(SIGALRM, &act, NULL);
1545 efe75411 ths
1546 9ed415b2 Jean-Christophe Dubois
    /* 
1547 9ed415b2 Jean-Christophe Dubois
     * Initialize ev struct to 0 to avoid valgrind complaining
1548 9ed415b2 Jean-Christophe Dubois
     * about uninitialized data in timer_create call
1549 9ed415b2 Jean-Christophe Dubois
     */
1550 9ed415b2 Jean-Christophe Dubois
    memset(&ev, 0, sizeof(ev));
1551 efe75411 ths
    ev.sigev_value.sival_int = 0;
1552 efe75411 ths
    ev.sigev_notify = SIGEV_SIGNAL;
1553 efe75411 ths
    ev.sigev_signo = SIGALRM;
1554 efe75411 ths
1555 efe75411 ths
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1556 efe75411 ths
        perror("timer_create");
1557 efe75411 ths
1558 efe75411 ths
        /* disable dynticks */
1559 efe75411 ths
        fprintf(stderr, "Dynamic Ticks disabled\n");
1560 efe75411 ths
1561 efe75411 ths
        return -1;
1562 efe75411 ths
    }
1563 efe75411 ths
1564 0399bfe0 blueswir1
    t->priv = (void *)(long)host_timer;
1565 efe75411 ths
1566 efe75411 ths
    return 0;
1567 efe75411 ths
}
1568 efe75411 ths
1569 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1570 efe75411 ths
{
1571 0399bfe0 blueswir1
    timer_t host_timer = (timer_t)(long)t->priv;
1572 efe75411 ths
1573 efe75411 ths
    timer_delete(host_timer);
1574 efe75411 ths
}
1575 efe75411 ths
1576 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1577 efe75411 ths
{
1578 0399bfe0 blueswir1
    timer_t host_timer = (timer_t)(long)t->priv;
1579 efe75411 ths
    struct itimerspec timeout;
1580 efe75411 ths
    int64_t nearest_delta_us = INT64_MAX;
1581 efe75411 ths
    int64_t current_us;
1582 efe75411 ths
1583 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1584 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1585 d5d08334 balrog
        return;
1586 efe75411 ths
1587 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1588 efe75411 ths
1589 efe75411 ths
    /* check whether a timer is already running */
1590 efe75411 ths
    if (timer_gettime(host_timer, &timeout)) {
1591 efe75411 ths
        perror("gettime");
1592 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1593 efe75411 ths
        exit(1);
1594 efe75411 ths
    }
1595 efe75411 ths
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1596 efe75411 ths
    if (current_us && current_us <= nearest_delta_us)
1597 efe75411 ths
        return;
1598 efe75411 ths
1599 efe75411 ths
    timeout.it_interval.tv_sec = 0;
1600 efe75411 ths
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1601 efe75411 ths
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1602 efe75411 ths
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1603 efe75411 ths
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1604 efe75411 ths
        perror("settime");
1605 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1606 efe75411 ths
        exit(1);
1607 efe75411 ths
    }
1608 efe75411 ths
}
1609 efe75411 ths
1610 70744b3a ths
#endif /* defined(__linux__) */
1611 231c6586 ths
1612 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t)
1613 c8994013 ths
{
1614 c8994013 ths
    struct sigaction act;
1615 c8994013 ths
    struct itimerval itv;
1616 c8994013 ths
    int err;
1617 c8994013 ths
1618 c8994013 ths
    /* timer signal */
1619 c8994013 ths
    sigfillset(&act.sa_mask);
1620 c8994013 ths
    act.sa_flags = 0;
1621 c8994013 ths
    act.sa_handler = host_alarm_handler;
1622 c8994013 ths
1623 c8994013 ths
    sigaction(SIGALRM, &act, NULL);
1624 c8994013 ths
1625 c8994013 ths
    itv.it_interval.tv_sec = 0;
1626 c8994013 ths
    /* for i386 kernel 2.6 to get 1 ms */
1627 c8994013 ths
    itv.it_interval.tv_usec = 999;
1628 c8994013 ths
    itv.it_value.tv_sec = 0;
1629 c8994013 ths
    itv.it_value.tv_usec = 10 * 1000;
1630 c8994013 ths
1631 c8994013 ths
    err = setitimer(ITIMER_REAL, &itv, NULL);
1632 c8994013 ths
    if (err)
1633 c8994013 ths
        return -1;
1634 c8994013 ths
1635 c8994013 ths
    return 0;
1636 c8994013 ths
}
1637 c8994013 ths
1638 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t)
1639 c8994013 ths
{
1640 c8994013 ths
    struct itimerval itv;
1641 c8994013 ths
1642 c8994013 ths
    memset(&itv, 0, sizeof(itv));
1643 c8994013 ths
    setitimer(ITIMER_REAL, &itv, NULL);
1644 c8994013 ths
}
1645 c8994013 ths
1646 829309c7 bellard
#endif /* !defined(_WIN32) */
1647 fd872598 bellard
1648 f49e58dc aliguori
1649 c8994013 ths
#ifdef _WIN32
1650 c8994013 ths
1651 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t)
1652 c8994013 ths
{
1653 c8994013 ths
    TIMECAPS tc;
1654 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1655 efe75411 ths
    UINT flags;
1656 c8994013 ths
1657 c8994013 ths
    memset(&tc, 0, sizeof(tc));
1658 c8994013 ths
    timeGetDevCaps(&tc, sizeof(tc));
1659 c8994013 ths
1660 c8994013 ths
    if (data->period < tc.wPeriodMin)
1661 c8994013 ths
        data->period = tc.wPeriodMin;
1662 c8994013 ths
1663 c8994013 ths
    timeBeginPeriod(data->period);
1664 c8994013 ths
1665 efe75411 ths
    flags = TIME_CALLBACK_FUNCTION;
1666 efe75411 ths
    if (alarm_has_dynticks(t))
1667 efe75411 ths
        flags |= TIME_ONESHOT;
1668 efe75411 ths
    else
1669 efe75411 ths
        flags |= TIME_PERIODIC;
1670 efe75411 ths
1671 c8994013 ths
    data->timerId = timeSetEvent(1,         // interval (ms)
1672 c8994013 ths
                        data->period,       // resolution
1673 c8994013 ths
                        host_alarm_handler, // function
1674 c8994013 ths
                        (DWORD)t,           // parameter
1675 efe75411 ths
                        flags);
1676 c8994013 ths
1677 c8994013 ths
    if (!data->timerId) {
1678 c8994013 ths
        perror("Failed to initialize win32 alarm timer");
1679 c8994013 ths
        timeEndPeriod(data->period);
1680 c8994013 ths
        return -1;
1681 c8994013 ths
    }
1682 c8994013 ths
1683 c8994013 ths
    return 0;
1684 c8994013 ths
}
1685 c8994013 ths
1686 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t)
1687 c8994013 ths
{
1688 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1689 c8994013 ths
1690 c8994013 ths
    timeKillEvent(data->timerId);
1691 c8994013 ths
    timeEndPeriod(data->period);
1692 c8994013 ths
}
1693 c8994013 ths
1694 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1695 efe75411 ths
{
1696 efe75411 ths
    struct qemu_alarm_win32 *data = t->priv;
1697 efe75411 ths
    uint64_t nearest_delta_us;
1698 efe75411 ths
1699 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1700 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1701 d5d08334 balrog
        return;
1702 efe75411 ths
1703 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1704 efe75411 ths
    nearest_delta_us /= 1000;
1705 efe75411 ths
1706 efe75411 ths
    timeKillEvent(data->timerId);
1707 efe75411 ths
1708 efe75411 ths
    data->timerId = timeSetEvent(1,
1709 efe75411 ths
                        data->period,
1710 efe75411 ths
                        host_alarm_handler,
1711 efe75411 ths
                        (DWORD)t,
1712 efe75411 ths
                        TIME_ONESHOT | TIME_PERIODIC);
1713 efe75411 ths
1714 efe75411 ths
    if (!data->timerId) {
1715 efe75411 ths
        perror("Failed to re-arm win32 alarm timer");
1716 efe75411 ths
1717 efe75411 ths
        timeEndPeriod(data->period);
1718 efe75411 ths
        exit(1);
1719 efe75411 ths
    }
1720 efe75411 ths
}
1721 efe75411 ths
1722 c8994013 ths
#endif /* _WIN32 */
1723 c8994013 ths
1724 7183b4b4 aliguori
static int init_timer_alarm(void)
1725 8a7ddc38 bellard
{
1726 223f0d72 blueswir1
    struct qemu_alarm_timer *t = NULL;
1727 c8994013 ths
    int i, err = -1;
1728 f49e58dc aliguori
1729 c8994013 ths
    for (i = 0; alarm_timers[i].name; i++) {
1730 c8994013 ths
        t = &alarm_timers[i];
1731 c8994013 ths
1732 c8994013 ths
        err = t->start(t);
1733 c8994013 ths
        if (!err)
1734 c8994013 ths
            break;
1735 67b915a5 bellard
    }
1736 fd872598 bellard
1737 c8994013 ths
    if (err) {
1738 7183b4b4 aliguori
        err = -ENOENT;
1739 7183b4b4 aliguori
        goto fail;
1740 67b915a5 bellard
    }
1741 c8994013 ths
1742 c8994013 ths
    alarm_timer = t;
1743 7183b4b4 aliguori
1744 6abfbd79 aliguori
    return 0;
1745 7183b4b4 aliguori
1746 7183b4b4 aliguori
fail:
1747 7183b4b4 aliguori
    return err;
1748 8a7ddc38 bellard
}
1749 8a7ddc38 bellard
1750 9596ebb7 pbrook
static void quit_timers(void)
1751 40c3bac3 bellard
{
1752 c8994013 ths
    alarm_timer->stop(alarm_timer);
1753 c8994013 ths
    alarm_timer = NULL;
1754 40c3bac3 bellard
}
1755 40c3bac3 bellard
1756 c4b1fcc0 bellard
/***********************************************************/
1757 f6503059 balrog
/* host time/date access */
1758 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
1759 f6503059 balrog
{
1760 f6503059 balrog
    time_t ti;
1761 f6503059 balrog
    struct tm *ret;
1762 f6503059 balrog
1763 f6503059 balrog
    time(&ti);
1764 f6503059 balrog
    ti += offset;
1765 f6503059 balrog
    if (rtc_date_offset == -1) {
1766 f6503059 balrog
        if (rtc_utc)
1767 f6503059 balrog
            ret = gmtime(&ti);
1768 f6503059 balrog
        else
1769 f6503059 balrog
            ret = localtime(&ti);
1770 f6503059 balrog
    } else {
1771 f6503059 balrog
        ti -= rtc_date_offset;
1772 f6503059 balrog
        ret = gmtime(&ti);
1773 f6503059 balrog
    }
1774 f6503059 balrog
1775 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
1776 f6503059 balrog
}
1777 f6503059 balrog
1778 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
1779 f6503059 balrog
{
1780 f6503059 balrog
    time_t seconds;
1781 f6503059 balrog
1782 f6503059 balrog
    if (rtc_date_offset == -1)
1783 f6503059 balrog
        if (rtc_utc)
1784 f6503059 balrog
            seconds = mktimegm(tm);
1785 f6503059 balrog
        else
1786 f6503059 balrog
            seconds = mktime(tm);
1787 f6503059 balrog
    else
1788 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
1789 f6503059 balrog
1790 f6503059 balrog
    return seconds - time(NULL);
1791 f6503059 balrog
}
1792 f6503059 balrog
1793 fd1dff4b bellard
#ifdef _WIN32
1794 fd1dff4b bellard
static void socket_cleanup(void)
1795 fd1dff4b bellard
{
1796 fd1dff4b bellard
    WSACleanup();
1797 fd1dff4b bellard
}
1798 82c643ff bellard
1799 fd1dff4b bellard
static int socket_init(void)
1800 fd1dff4b bellard
{
1801 fd1dff4b bellard
    WSADATA Data;
1802 fd1dff4b bellard
    int ret, err;
1803 fd1dff4b bellard
1804 fd1dff4b bellard
    ret = WSAStartup(MAKEWORD(2,2), &Data);
1805 fd1dff4b bellard
    if (ret != 0) {
1806 fd1dff4b bellard
        err = WSAGetLastError();
1807 fd1dff4b bellard
        fprintf(stderr, "WSAStartup: %d\n", err);
1808 fd1dff4b bellard
        return -1;
1809 fd1dff4b bellard
    }
1810 fd1dff4b bellard
    atexit(socket_cleanup);
1811 fd1dff4b bellard
    return 0;
1812 fd1dff4b bellard
}
1813 64b7b733 aurel32
#endif
1814 64b7b733 aurel32
1815 63a01ef8 aliguori
int get_param_value(char *buf, int buf_size,
1816 63a01ef8 aliguori
                    const char *tag, const char *str)
1817 7c9d8e07 bellard
{
1818 7c9d8e07 bellard
    const char *p;
1819 7c9d8e07 bellard
    char option[128];
1820 7c9d8e07 bellard
1821 7c9d8e07 bellard
    p = str;
1822 7c9d8e07 bellard
    for(;;) {
1823 268a362c aliguori
        p = get_opt_name(option, sizeof(option), p, '=');
1824 7c9d8e07 bellard
        if (*p != '=')
1825 7c9d8e07 bellard
            break;
1826 7c9d8e07 bellard
        p++;
1827 7c9d8e07 bellard
        if (!strcmp(tag, option)) {
1828 609497ab balrog
            (void)get_opt_value(buf, buf_size, p);
1829 e4bcb14c ths
            return strlen(buf);
1830 7c9d8e07 bellard
        } else {
1831 609497ab balrog
            p = get_opt_value(NULL, 0, p);
1832 7c9d8e07 bellard
        }
1833 7c9d8e07 bellard
        if (*p != ',')
1834 7c9d8e07 bellard
            break;
1835 7c9d8e07 bellard
        p++;
1836 7c9d8e07 bellard
    }
1837 7c9d8e07 bellard
    return 0;
1838 7c9d8e07 bellard
}
1839 7c9d8e07 bellard
1840 0aa7a205 Jan Kiszka
int check_params(char *buf, int buf_size,
1841 0aa7a205 Jan Kiszka
                 const char * const *params, const char *str)
1842 e4bcb14c ths
{
1843 e4bcb14c ths
    const char *p;
1844 0aa7a205 Jan Kiszka
    int i;
1845 e4bcb14c ths
1846 e4bcb14c ths
    p = str;
1847 10300216 aliguori
    while (*p != '\0') {
1848 0aa7a205 Jan Kiszka
        p = get_opt_name(buf, buf_size, p, '=');
1849 ffad4116 Jan Kiszka
        if (*p != '=') {
1850 0aa7a205 Jan Kiszka
            return -1;
1851 ffad4116 Jan Kiszka
        }
1852 e4bcb14c ths
        p++;
1853 0aa7a205 Jan Kiszka
        for (i = 0; params[i] != NULL; i++) {
1854 0aa7a205 Jan Kiszka
            if (!strcmp(params[i], buf)) {
1855 e4bcb14c ths
                break;
1856 0aa7a205 Jan Kiszka
            }
1857 0aa7a205 Jan Kiszka
        }
1858 ffad4116 Jan Kiszka
        if (params[i] == NULL) {
1859 0aa7a205 Jan Kiszka
            return -1;
1860 ffad4116 Jan Kiszka
        }
1861 609497ab balrog
        p = get_opt_value(NULL, 0, p);
1862 0aa7a205 Jan Kiszka
        if (*p != ',') {
1863 e4bcb14c ths
            break;
1864 0aa7a205 Jan Kiszka
        }
1865 e4bcb14c ths
        p++;
1866 e4bcb14c ths
    }
1867 0aa7a205 Jan Kiszka
    return 0;
1868 e4bcb14c ths
}
1869 e4bcb14c ths
1870 1ae26a18 balrog
/***********************************************************/
1871 1ae26a18 balrog
/* Bluetooth support */
1872 1ae26a18 balrog
static int nb_hcis;
1873 1ae26a18 balrog
static int cur_hci;
1874 1ae26a18 balrog
static struct HCIInfo *hci_table[MAX_NICS];
1875 dc72ac14 balrog
1876 1ae26a18 balrog
static struct bt_vlan_s {
1877 1ae26a18 balrog
    struct bt_scatternet_s net;
1878 1ae26a18 balrog
    int id;
1879 1ae26a18 balrog
    struct bt_vlan_s *next;
1880 1ae26a18 balrog
} *first_bt_vlan;
1881 1ae26a18 balrog
1882 1ae26a18 balrog
/* find or alloc a new bluetooth "VLAN" */
1883 674bb261 blueswir1
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1884 1ae26a18 balrog
{
1885 1ae26a18 balrog
    struct bt_vlan_s **pvlan, *vlan;
1886 1ae26a18 balrog
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1887 1ae26a18 balrog
        if (vlan->id == id)
1888 1ae26a18 balrog
            return &vlan->net;
1889 1ae26a18 balrog
    }
1890 1ae26a18 balrog
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1891 1ae26a18 balrog
    vlan->id = id;
1892 1ae26a18 balrog
    pvlan = &first_bt_vlan;
1893 1ae26a18 balrog
    while (*pvlan != NULL)
1894 1ae26a18 balrog
        pvlan = &(*pvlan)->next;
1895 1ae26a18 balrog
    *pvlan = vlan;
1896 1ae26a18 balrog
    return &vlan->net;
1897 1ae26a18 balrog
}
1898 1ae26a18 balrog
1899 1ae26a18 balrog
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1900 1ae26a18 balrog
{
1901 1ae26a18 balrog
}
1902 1ae26a18 balrog
1903 1ae26a18 balrog
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1904 1ae26a18 balrog
{
1905 1ae26a18 balrog
    return -ENOTSUP;
1906 1ae26a18 balrog
}
1907 1ae26a18 balrog
1908 1ae26a18 balrog
static struct HCIInfo null_hci = {
1909 1ae26a18 balrog
    .cmd_send = null_hci_send,
1910 1ae26a18 balrog
    .sco_send = null_hci_send,
1911 1ae26a18 balrog
    .acl_send = null_hci_send,
1912 1ae26a18 balrog
    .bdaddr_set = null_hci_addr_set,
1913 1ae26a18 balrog
};
1914 1ae26a18 balrog
1915 1ae26a18 balrog
struct HCIInfo *qemu_next_hci(void)
1916 1ae26a18 balrog
{
1917 1ae26a18 balrog
    if (cur_hci == nb_hcis)
1918 1ae26a18 balrog
        return &null_hci;
1919 1ae26a18 balrog
1920 1ae26a18 balrog
    return hci_table[cur_hci++];
1921 1ae26a18 balrog
}
1922 1ae26a18 balrog
1923 dc72ac14 balrog
static struct HCIInfo *hci_init(const char *str)
1924 dc72ac14 balrog
{
1925 dc72ac14 balrog
    char *endp;
1926 dc72ac14 balrog
    struct bt_scatternet_s *vlan = 0;
1927 dc72ac14 balrog
1928 dc72ac14 balrog
    if (!strcmp(str, "null"))
1929 dc72ac14 balrog
        /* null */
1930 dc72ac14 balrog
        return &null_hci;
1931 dc72ac14 balrog
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1932 dc72ac14 balrog
        /* host[:hciN] */
1933 dc72ac14 balrog
        return bt_host_hci(str[4] ? str + 5 : "hci0");
1934 dc72ac14 balrog
    else if (!strncmp(str, "hci", 3)) {
1935 dc72ac14 balrog
        /* hci[,vlan=n] */
1936 dc72ac14 balrog
        if (str[3]) {
1937 dc72ac14 balrog
            if (!strncmp(str + 3, ",vlan=", 6)) {
1938 dc72ac14 balrog
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1939 dc72ac14 balrog
                if (*endp)
1940 dc72ac14 balrog
                    vlan = 0;
1941 dc72ac14 balrog
            }
1942 dc72ac14 balrog
        } else
1943 dc72ac14 balrog
            vlan = qemu_find_bt_vlan(0);
1944 dc72ac14 balrog
        if (vlan)
1945 dc72ac14 balrog
           return bt_new_hci(vlan);
1946 dc72ac14 balrog
    }
1947 dc72ac14 balrog
1948 dc72ac14 balrog
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1949 dc72ac14 balrog
1950 dc72ac14 balrog
    return 0;
1951 dc72ac14 balrog
}
1952 dc72ac14 balrog
1953 dc72ac14 balrog
static int bt_hci_parse(const char *str)
1954 dc72ac14 balrog
{
1955 dc72ac14 balrog
    struct HCIInfo *hci;
1956 dc72ac14 balrog
    bdaddr_t bdaddr;
1957 dc72ac14 balrog
1958 dc72ac14 balrog
    if (nb_hcis >= MAX_NICS) {
1959 dc72ac14 balrog
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1960 dc72ac14 balrog
        return -1;
1961 dc72ac14 balrog
    }
1962 dc72ac14 balrog
1963 dc72ac14 balrog
    hci = hci_init(str);
1964 dc72ac14 balrog
    if (!hci)
1965 dc72ac14 balrog
        return -1;
1966 dc72ac14 balrog
1967 dc72ac14 balrog
    bdaddr.b[0] = 0x52;
1968 dc72ac14 balrog
    bdaddr.b[1] = 0x54;
1969 dc72ac14 balrog
    bdaddr.b[2] = 0x00;
1970 dc72ac14 balrog
    bdaddr.b[3] = 0x12;
1971 dc72ac14 balrog
    bdaddr.b[4] = 0x34;
1972 dc72ac14 balrog
    bdaddr.b[5] = 0x56 + nb_hcis;
1973 dc72ac14 balrog
    hci->bdaddr_set(hci, bdaddr.b);
1974 dc72ac14 balrog
1975 dc72ac14 balrog
    hci_table[nb_hcis++] = hci;
1976 dc72ac14 balrog
1977 dc72ac14 balrog
    return 0;
1978 dc72ac14 balrog
}
1979 dc72ac14 balrog
1980 dc72ac14 balrog
static void bt_vhci_add(int vlan_id)
1981 dc72ac14 balrog
{
1982 dc72ac14 balrog
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1983 dc72ac14 balrog
1984 dc72ac14 balrog
    if (!vlan->slave)
1985 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a VHCI to "
1986 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
1987 dc72ac14 balrog
1988 dc72ac14 balrog
    bt_vhci_init(bt_new_hci(vlan));
1989 dc72ac14 balrog
}
1990 dc72ac14 balrog
1991 dc72ac14 balrog
static struct bt_device_s *bt_device_add(const char *opt)
1992 dc72ac14 balrog
{
1993 dc72ac14 balrog
    struct bt_scatternet_s *vlan;
1994 dc72ac14 balrog
    int vlan_id = 0;
1995 dc72ac14 balrog
    char *endp = strstr(opt, ",vlan=");
1996 dc72ac14 balrog
    int len = (endp ? endp - opt : strlen(opt)) + 1;
1997 dc72ac14 balrog
    char devname[10];
1998 dc72ac14 balrog
1999 dc72ac14 balrog
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
2000 dc72ac14 balrog
2001 dc72ac14 balrog
    if (endp) {
2002 dc72ac14 balrog
        vlan_id = strtol(endp + 6, &endp, 0);
2003 dc72ac14 balrog
        if (*endp) {
2004 dc72ac14 balrog
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2005 dc72ac14 balrog
            return 0;
2006 dc72ac14 balrog
        }
2007 dc72ac14 balrog
    }
2008 dc72ac14 balrog
2009 dc72ac14 balrog
    vlan = qemu_find_bt_vlan(vlan_id);
2010 dc72ac14 balrog
2011 dc72ac14 balrog
    if (!vlan->slave)
2012 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a slave device to "
2013 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
2014 dc72ac14 balrog
2015 dc72ac14 balrog
    if (!strcmp(devname, "keyboard"))
2016 dc72ac14 balrog
        return bt_keyboard_init(vlan);
2017 dc72ac14 balrog
2018 dc72ac14 balrog
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2019 dc72ac14 balrog
    return 0;
2020 dc72ac14 balrog
}
2021 dc72ac14 balrog
2022 dc72ac14 balrog
static int bt_parse(const char *opt)
2023 dc72ac14 balrog
{
2024 dc72ac14 balrog
    const char *endp, *p;
2025 dc72ac14 balrog
    int vlan;
2026 dc72ac14 balrog
2027 dc72ac14 balrog
    if (strstart(opt, "hci", &endp)) {
2028 dc72ac14 balrog
        if (!*endp || *endp == ',') {
2029 dc72ac14 balrog
            if (*endp)
2030 dc72ac14 balrog
                if (!strstart(endp, ",vlan=", 0))
2031 dc72ac14 balrog
                    opt = endp + 1;
2032 dc72ac14 balrog
2033 dc72ac14 balrog
            return bt_hci_parse(opt);
2034 dc72ac14 balrog
       }
2035 dc72ac14 balrog
    } else if (strstart(opt, "vhci", &endp)) {
2036 dc72ac14 balrog
        if (!*endp || *endp == ',') {
2037 dc72ac14 balrog
            if (*endp) {
2038 dc72ac14 balrog
                if (strstart(endp, ",vlan=", &p)) {
2039 dc72ac14 balrog
                    vlan = strtol(p, (char **) &endp, 0);
2040 dc72ac14 balrog
                    if (*endp) {
2041 dc72ac14 balrog
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2042 dc72ac14 balrog
                        return 1;
2043 dc72ac14 balrog
                    }
2044 dc72ac14 balrog
                } else {
2045 dc72ac14 balrog
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2046 dc72ac14 balrog
                    return 1;
2047 dc72ac14 balrog
                }
2048 dc72ac14 balrog
            } else
2049 dc72ac14 balrog
                vlan = 0;
2050 dc72ac14 balrog
2051 dc72ac14 balrog
            bt_vhci_add(vlan);
2052 dc72ac14 balrog
            return 0;
2053 dc72ac14 balrog
        }
2054 dc72ac14 balrog
    } else if (strstart(opt, "device:", &endp))
2055 dc72ac14 balrog
        return !bt_device_add(endp);
2056 dc72ac14 balrog
2057 dc72ac14 balrog
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2058 dc72ac14 balrog
    return 1;
2059 dc72ac14 balrog
}
2060 dc72ac14 balrog
2061 1ae26a18 balrog
/***********************************************************/
2062 1ae26a18 balrog
/* QEMU Block devices */
2063 1ae26a18 balrog
2064 609497ab balrog
#define HD_ALIAS "index=%d,media=disk"
2065 e4bcb14c ths
#define CDROM_ALIAS "index=2,media=cdrom"
2066 e4bcb14c ths
#define FD_ALIAS "index=%d,if=floppy"
2067 609497ab balrog
#define PFLASH_ALIAS "if=pflash"
2068 609497ab balrog
#define MTD_ALIAS "if=mtd"
2069 9d413d1d balrog
#define SD_ALIAS "index=0,if=sd"
2070 e4bcb14c ths
2071 7d5aca9e aliguori
static int drive_opt_get_free_idx(void)
2072 7d5aca9e aliguori
{
2073 7d5aca9e aliguori
    int index;
2074 7d5aca9e aliguori
2075 7d5aca9e aliguori
    for (index = 0; index < MAX_DRIVES; index++)
2076 7d5aca9e aliguori
        if (!drives_opt[index].used) {
2077 7d5aca9e aliguori
            drives_opt[index].used = 1;
2078 7d5aca9e aliguori
            return index;
2079 7d5aca9e aliguori
        }
2080 7d5aca9e aliguori
2081 7d5aca9e aliguori
    return -1;
2082 7d5aca9e aliguori
}
2083 7d5aca9e aliguori
2084 7d5aca9e aliguori
static int drive_get_free_idx(void)
2085 7d5aca9e aliguori
{
2086 7d5aca9e aliguori
    int index;
2087 7d5aca9e aliguori
2088 7d5aca9e aliguori
    for (index = 0; index < MAX_DRIVES; index++)
2089 7d5aca9e aliguori
        if (!drives_table[index].used) {
2090 7d5aca9e aliguori
            drives_table[index].used = 1;
2091 7d5aca9e aliguori
            return index;
2092 7d5aca9e aliguori
        }
2093 7d5aca9e aliguori
2094 7d5aca9e aliguori
    return -1;
2095 7d5aca9e aliguori
}
2096 7d5aca9e aliguori
2097 4d73cd3b aliguori
int drive_add(const char *file, const char *fmt, ...)
2098 e4bcb14c ths
{
2099 e4bcb14c ths
    va_list ap;
2100 7d5aca9e aliguori
    int index = drive_opt_get_free_idx();
2101 e4bcb14c ths
2102 7d5aca9e aliguori
    if (nb_drives_opt >= MAX_DRIVES || index == -1) {
2103 e4bcb14c ths
        fprintf(stderr, "qemu: too many drives\n");
2104 4d73cd3b aliguori
        return -1;
2105 e4bcb14c ths
    }
2106 e4bcb14c ths
2107 7d5aca9e aliguori
    drives_opt[index].file = file;
2108 e4bcb14c ths
    va_start(ap, fmt);
2109 7d5aca9e aliguori
    vsnprintf(drives_opt[index].opt,
2110 609497ab balrog
              sizeof(drives_opt[0].opt), fmt, ap);
2111 e4bcb14c ths
    va_end(ap);
2112 e4bcb14c ths
2113 7d5aca9e aliguori
    nb_drives_opt++;
2114 7d5aca9e aliguori
    return index;
2115 e4bcb14c ths
}
2116 e4bcb14c ths
2117 b01b1111 aliguori
void drive_remove(int index)
2118 b01b1111 aliguori
{
2119 b01b1111 aliguori
    drives_opt[index].used = 0;
2120 b01b1111 aliguori
    nb_drives_opt--;
2121 b01b1111 aliguori
}
2122 b01b1111 aliguori
2123 f60d39bc ths
int drive_get_index(BlockInterfaceType type, int bus, int unit)
2124 e4bcb14c ths
{
2125 e4bcb14c ths
    int index;
2126 e4bcb14c ths
2127 e4bcb14c ths
    /* seek interface, bus and unit */
2128 e4bcb14c ths
2129 7d5aca9e aliguori
    for (index = 0; index < MAX_DRIVES; index++)
2130 f60d39bc ths
        if (drives_table[index].type == type &&
2131 e4bcb14c ths
            drives_table[index].bus == bus &&
2132 7d5aca9e aliguori
            drives_table[index].unit == unit &&
2133 7d5aca9e aliguori
            drives_table[index].used)
2134 e4bcb14c ths
        return index;
2135 e4bcb14c ths
2136 e4bcb14c ths
    return -1;
2137 e4bcb14c ths
}
2138 e4bcb14c ths
2139 f60d39bc ths
int drive_get_max_bus(BlockInterfaceType type)
2140 e4bcb14c ths
{
2141 e4bcb14c ths
    int max_bus;
2142 e4bcb14c ths
    int index;
2143 e4bcb14c ths
2144 e4bcb14c ths
    max_bus = -1;
2145 e4bcb14c ths
    for (index = 0; index < nb_drives; index++) {
2146 f60d39bc ths
        if(drives_table[index].type == type &&
2147 e4bcb14c ths
           drives_table[index].bus > max_bus)
2148 e4bcb14c ths
            max_bus = drives_table[index].bus;
2149 e4bcb14c ths
    }
2150 e4bcb14c ths
    return max_bus;
2151 e4bcb14c ths
}
2152 e4bcb14c ths
2153 fa879c64 aliguori
const char *drive_get_serial(BlockDriverState *bdrv)
2154 fa879c64 aliguori
{
2155 fa879c64 aliguori
    int index;
2156 fa879c64 aliguori
2157 fa879c64 aliguori
    for (index = 0; index < nb_drives; index++)
2158 fa879c64 aliguori
        if (drives_table[index].bdrv == bdrv)
2159 fa879c64 aliguori
            return drives_table[index].serial;
2160 fa879c64 aliguori
2161 fa879c64 aliguori
    return "\0";
2162 fa879c64 aliguori
}
2163 fa879c64 aliguori
2164 428c5705 aliguori
BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2165 428c5705 aliguori
{
2166 428c5705 aliguori
    int index;
2167 428c5705 aliguori
2168 428c5705 aliguori
    for (index = 0; index < nb_drives; index++)
2169 428c5705 aliguori
        if (drives_table[index].bdrv == bdrv)
2170 428c5705 aliguori
            return drives_table[index].onerror;
2171 428c5705 aliguori
2172 cdad4bd8 aliguori
    return BLOCK_ERR_STOP_ENOSPC;
2173 428c5705 aliguori
}
2174 428c5705 aliguori
2175 a1620fac aurel32
static void bdrv_format_print(void *opaque, const char *name)
2176 a1620fac aurel32
{
2177 a1620fac aurel32
    fprintf(stderr, " %s", name);
2178 a1620fac aurel32
}
2179 a1620fac aurel32
2180 b01b1111 aliguori
void drive_uninit(BlockDriverState *bdrv)
2181 b01b1111 aliguori
{
2182 b01b1111 aliguori
    int i;
2183 b01b1111 aliguori
2184 b01b1111 aliguori
    for (i = 0; i < MAX_DRIVES; i++)
2185 b01b1111 aliguori
        if (drives_table[i].bdrv == bdrv) {
2186 b01b1111 aliguori
            drives_table[i].bdrv = NULL;
2187 b01b1111 aliguori
            drives_table[i].used = 0;
2188 b01b1111 aliguori
            drive_remove(drives_table[i].drive_opt_idx);
2189 b01b1111 aliguori
            nb_drives--;
2190 b01b1111 aliguori
            break;
2191 b01b1111 aliguori
        }
2192 b01b1111 aliguori
}
2193 b01b1111 aliguori
2194 4d73cd3b aliguori
int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
2195 e4bcb14c ths
{
2196 e4bcb14c ths
    char buf[128];
2197 e4bcb14c ths
    char file[1024];
2198 c8522bdf balrog
    char devname[128];
2199 fa879c64 aliguori
    char serial[21];
2200 c8522bdf balrog
    const char *mediastr = "";
2201 f60d39bc ths
    BlockInterfaceType type;
2202 e4bcb14c ths
    enum { MEDIA_DISK, MEDIA_CDROM } media;
2203 e4bcb14c ths
    int bus_id, unit_id;
2204 e4bcb14c ths
    int cyls, heads, secs, translation;
2205 e4bcb14c ths
    BlockDriverState *bdrv;
2206 1e72d3b7 aurel32
    BlockDriver *drv = NULL;
2207 4d73cd3b aliguori
    QEMUMachine *machine = opaque;
2208 e4bcb14c ths
    int max_devs;
2209 e4bcb14c ths
    int index;
2210 33f00271 balrog
    int cache;
2211 428c5705 aliguori
    int bdrv_flags, onerror;
2212 7d5aca9e aliguori
    int drives_table_idx;
2213 609497ab balrog
    char *str = arg->opt;
2214 7ccfb2eb blueswir1
    static const char * const params[] = { "bus", "unit", "if", "index",
2215 7ccfb2eb blueswir1
                                           "cyls", "heads", "secs", "trans",
2216 7ccfb2eb blueswir1
                                           "media", "snapshot", "file",
2217 428c5705 aliguori
                                           "cache", "format", "serial", "werror",
2218 428c5705 aliguori
                                           NULL };
2219 e4bcb14c ths
2220 0aa7a205 Jan Kiszka
    if (check_params(buf, sizeof(buf), params, str) < 0) {
2221 cda94b27 Mark McLoughlin
         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2222 cda94b27 Mark McLoughlin
                         buf, str);
2223 e4bcb14c ths
         return -1;
2224 e4bcb14c ths
    }
2225 e4bcb14c ths
2226 e4bcb14c ths
    file[0] = 0;
2227 e4bcb14c ths
    cyls = heads = secs = 0;
2228 e4bcb14c ths
    bus_id = 0;
2229 e4bcb14c ths
    unit_id = -1;
2230 e4bcb14c ths
    translation = BIOS_ATA_TRANSLATION_AUTO;
2231 e4bcb14c ths
    index = -1;
2232 4dc822d7 aliguori
    cache = 3;
2233 e4bcb14c ths
2234 c9b1ae2c blueswir1
    if (machine->use_scsi) {
2235 f60d39bc ths
        type = IF_SCSI;
2236 e4bcb14c ths
        max_devs = MAX_SCSI_DEVS;
2237 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "scsi");
2238 e4bcb14c ths
    } else {
2239 f60d39bc ths
        type = IF_IDE;
2240 e4bcb14c ths
        max_devs = MAX_IDE_DEVS;
2241 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "ide");
2242 e4bcb14c ths
    }
2243 e4bcb14c ths
    media = MEDIA_DISK;
2244 e4bcb14c ths
2245 e4bcb14c ths
    /* extract parameters */
2246 e4bcb14c ths
2247 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "bus", str)) {
2248 e4bcb14c ths
        bus_id = strtol(buf, NULL, 0);
2249 e4bcb14c ths
        if (bus_id < 0) {
2250 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2251 e4bcb14c ths
            return -1;
2252 e4bcb14c ths
        }
2253 e4bcb14c ths
    }
2254 e4bcb14c ths
2255 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "unit", str)) {
2256 e4bcb14c ths
        unit_id = strtol(buf, NULL, 0);
2257 e4bcb14c ths
        if (unit_id < 0) {
2258 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2259 e4bcb14c ths
            return -1;
2260 e4bcb14c ths
        }
2261 e4bcb14c ths
    }
2262 e4bcb14c ths
2263 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "if", str)) {
2264 ae45d369 bellard
        pstrcpy(devname, sizeof(devname), buf);
2265 e4bcb14c ths
        if (!strcmp(buf, "ide")) {
2266 f60d39bc ths
            type = IF_IDE;
2267 e4bcb14c ths
            max_devs = MAX_IDE_DEVS;
2268 e4bcb14c ths
        } else if (!strcmp(buf, "scsi")) {
2269 f60d39bc ths
            type = IF_SCSI;
2270 e4bcb14c ths
            max_devs = MAX_SCSI_DEVS;
2271 e4bcb14c ths
        } else if (!strcmp(buf, "floppy")) {
2272 f60d39bc ths
            type = IF_FLOPPY;
2273 e4bcb14c ths
            max_devs = 0;
2274 e4bcb14c ths
        } else if (!strcmp(buf, "pflash")) {
2275 f60d39bc ths
            type = IF_PFLASH;
2276 e4bcb14c ths
            max_devs = 0;
2277 e4bcb14c ths
        } else if (!strcmp(buf, "mtd")) {
2278 f60d39bc ths
            type = IF_MTD;
2279 e4bcb14c ths
            max_devs = 0;
2280 e4bcb14c ths
        } else if (!strcmp(buf, "sd")) {
2281 f60d39bc ths
            type = IF_SD;
2282 e4bcb14c ths
            max_devs = 0;
2283 6e02c38d aliguori
        } else if (!strcmp(buf, "virtio")) {
2284 6e02c38d aliguori
            type = IF_VIRTIO;
2285 6e02c38d aliguori
            max_devs = 0;
2286 62d23efa aliguori
        } else if (!strcmp(buf, "xen")) {
2287 62d23efa aliguori
            type = IF_XEN;
2288 62d23efa aliguori
            max_devs = 0;
2289 62d23efa aliguori
        } else {
2290 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2291 e4bcb14c ths
            return -1;
2292 e4bcb14c ths
        }
2293 e4bcb14c ths
    }
2294 e4bcb14c ths
2295 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "index", str)) {
2296 e4bcb14c ths
        index = strtol(buf, NULL, 0);
2297 e4bcb14c ths
        if (index < 0) {
2298 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid index\n", str);
2299 e4bcb14c ths
            return -1;
2300 e4bcb14c ths
        }
2301 e4bcb14c ths
    }
2302 e4bcb14c ths
2303 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2304 e4bcb14c ths
        cyls = strtol(buf, NULL, 0);
2305 e4bcb14c ths
    }
2306 e4bcb14c ths
2307 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "heads", str)) {
2308 e4bcb14c ths
        heads = strtol(buf, NULL, 0);
2309 e4bcb14c ths
    }
2310 e4bcb14c ths
2311 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "secs", str)) {
2312 e4bcb14c ths
        secs = strtol(buf, NULL, 0);
2313 e4bcb14c ths
    }
2314 e4bcb14c ths
2315 e4bcb14c ths
    if (cyls || heads || secs) {
2316 e4bcb14c ths
        if (cyls < 1 || cyls > 16383) {
2317 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2318 e4bcb14c ths
            return -1;
2319 e4bcb14c ths
        }
2320 e4bcb14c ths
        if (heads < 1 || heads > 16) {
2321 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2322 e4bcb14c ths
            return -1;
2323 e4bcb14c ths
        }
2324 e4bcb14c ths
        if (secs < 1 || secs > 63) {
2325 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2326 e4bcb14c ths
            return -1;
2327 e4bcb14c ths
        }
2328 e4bcb14c ths
    }
2329 e4bcb14c ths
2330 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "trans", str)) {
2331 e4bcb14c ths
        if (!cyls) {
2332 e4bcb14c ths
            fprintf(stderr,
2333 e4bcb14c ths
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
2334 e4bcb14c ths
                    str);
2335 e4bcb14c ths
            return -1;
2336 e4bcb14c ths
        }
2337 e4bcb14c ths
        if (!strcmp(buf, "none"))
2338 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_NONE;
2339 e4bcb14c ths
        else if (!strcmp(buf, "lba"))
2340 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_LBA;
2341 e4bcb14c ths
        else if (!strcmp(buf, "auto"))
2342 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_AUTO;
2343 e4bcb14c ths
        else {
2344 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2345 e4bcb14c ths
            return -1;
2346 e4bcb14c ths
        }
2347 e4bcb14c ths
    }
2348 e4bcb14c ths
2349 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "media", str)) {
2350 e4bcb14c ths
        if (!strcmp(buf, "disk")) {
2351 e4bcb14c ths
            media = MEDIA_DISK;
2352 e4bcb14c ths
        } else if (!strcmp(buf, "cdrom")) {
2353 e4bcb14c ths
            if (cyls || secs || heads) {
2354 e4bcb14c ths
                fprintf(stderr,
2355 e4bcb14c ths
                        "qemu: '%s' invalid physical CHS format\n", str);
2356 e4bcb14c ths
                return -1;
2357 e4bcb14c ths
            }
2358 e4bcb14c ths
            media = MEDIA_CDROM;
2359 e4bcb14c ths
        } else {
2360 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid media\n", str);
2361 e4bcb14c ths
            return -1;
2362 e4bcb14c ths
        }
2363 e4bcb14c ths
    }
2364 e4bcb14c ths
2365 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2366 e4bcb14c ths
        if (!strcmp(buf, "on"))
2367 e4bcb14c ths
            snapshot = 1;
2368 e4bcb14c ths
        else if (!strcmp(buf, "off"))
2369 e4bcb14c ths
            snapshot = 0;
2370 e4bcb14c ths
        else {
2371 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2372 e4bcb14c ths
            return -1;
2373 e4bcb14c ths
        }
2374 e4bcb14c ths
    }
2375 e4bcb14c ths
2376 33f00271 balrog
    if (get_param_value(buf, sizeof(buf), "cache", str)) {
2377 9f7965c7 aliguori
        if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2378 33f00271 balrog
            cache = 0;
2379 9f7965c7 aliguori
        else if (!strcmp(buf, "writethrough"))
2380 33f00271 balrog
            cache = 1;
2381 9f7965c7 aliguori
        else if (!strcmp(buf, "writeback"))
2382 9f7965c7 aliguori
            cache = 2;
2383 33f00271 balrog
        else {
2384 33f00271 balrog
           fprintf(stderr, "qemu: invalid cache option\n");
2385 33f00271 balrog
           return -1;
2386 33f00271 balrog
        }
2387 33f00271 balrog
    }
2388 33f00271 balrog
2389 1e72d3b7 aurel32
    if (get_param_value(buf, sizeof(buf), "format", str)) {
2390 a1620fac aurel32
       if (strcmp(buf, "?") == 0) {
2391 a1620fac aurel32
            fprintf(stderr, "qemu: Supported formats:");
2392 a1620fac aurel32
            bdrv_iterate_format(bdrv_format_print, NULL);
2393 a1620fac aurel32
            fprintf(stderr, "\n");
2394 a1620fac aurel32
            return -1;
2395 a1620fac aurel32
        }
2396 1e72d3b7 aurel32
        drv = bdrv_find_format(buf);
2397 1e72d3b7 aurel32
        if (!drv) {
2398 1e72d3b7 aurel32
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2399 1e72d3b7 aurel32
            return -1;
2400 1e72d3b7 aurel32
        }
2401 1e72d3b7 aurel32
    }
2402 1e72d3b7 aurel32
2403 609497ab balrog
    if (arg->file == NULL)
2404 609497ab balrog
        get_param_value(file, sizeof(file), "file", str);
2405 609497ab balrog
    else
2406 609497ab balrog
        pstrcpy(file, sizeof(file), arg->file);
2407 e4bcb14c ths
2408 fa879c64 aliguori
    if (!get_param_value(serial, sizeof(serial), "serial", str))
2409 fa879c64 aliguori
            memset(serial, 0,  sizeof(serial));
2410 fa879c64 aliguori
2411 cdad4bd8 aliguori
    onerror = BLOCK_ERR_STOP_ENOSPC;
2412 428c5705 aliguori
    if (get_param_value(buf, sizeof(serial), "werror", str)) {
2413 869a5c6d aliguori
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
2414 ea8a5d7f aliguori
            fprintf(stderr, "werror is no supported by this format\n");
2415 428c5705 aliguori
            return -1;
2416 428c5705 aliguori
        }
2417 428c5705 aliguori
        if (!strcmp(buf, "ignore"))
2418 428c5705 aliguori
            onerror = BLOCK_ERR_IGNORE;
2419 428c5705 aliguori
        else if (!strcmp(buf, "enospc"))
2420 428c5705 aliguori
            onerror = BLOCK_ERR_STOP_ENOSPC;
2421 428c5705 aliguori
        else if (!strcmp(buf, "stop"))
2422 428c5705 aliguori
            onerror = BLOCK_ERR_STOP_ANY;
2423 428c5705 aliguori
        else if (!strcmp(buf, "report"))
2424 428c5705 aliguori
            onerror = BLOCK_ERR_REPORT;
2425 428c5705 aliguori
        else {
2426 428c5705 aliguori
            fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2427 428c5705 aliguori
            return -1;
2428 428c5705 aliguori
        }
2429 428c5705 aliguori
    }
2430 428c5705 aliguori
2431 e4bcb14c ths
    /* compute bus and unit according index */
2432 e4bcb14c ths
2433 e4bcb14c ths
    if (index != -1) {
2434 e4bcb14c ths
        if (bus_id != 0 || unit_id != -1) {
2435 e4bcb14c ths
            fprintf(stderr,
2436 e4bcb14c ths
                    "qemu: '%s' index cannot be used with bus and unit\n", str);
2437 e4bcb14c ths
            return -1;
2438 e4bcb14c ths
        }
2439 e4bcb14c ths
        if (max_devs == 0)
2440 e4bcb14c ths
        {
2441 e4bcb14c ths
            unit_id = index;
2442 e4bcb14c ths
            bus_id = 0;
2443 e4bcb14c ths
        } else {
2444 e4bcb14c ths
            unit_id = index % max_devs;
2445 e4bcb14c ths
            bus_id = index / max_devs;
2446 e4bcb14c ths
        }
2447 e4bcb14c ths
    }
2448 e4bcb14c ths
2449 e4bcb14c ths
    /* if user doesn't specify a unit_id,
2450 e4bcb14c ths
     * try to find the first free
2451 e4bcb14c ths
     */
2452 e4bcb14c ths
2453 e4bcb14c ths
    if (unit_id == -1) {
2454 e4bcb14c ths
       unit_id = 0;
2455 f60d39bc ths
       while (drive_get_index(type, bus_id, unit_id) != -1) {
2456 e4bcb14c ths
           unit_id++;
2457 e4bcb14c ths
           if (max_devs && unit_id >= max_devs) {
2458 e4bcb14c ths
               unit_id -= max_devs;
2459 e4bcb14c ths
               bus_id++;
2460 e4bcb14c ths
           }
2461 e4bcb14c ths
       }
2462 e4bcb14c ths
    }
2463 e4bcb14c ths
2464 e4bcb14c ths
    /* check unit id */
2465 e4bcb14c ths
2466 e4bcb14c ths
    if (max_devs && unit_id >= max_devs) {
2467 e4bcb14c ths
        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2468 e4bcb14c ths
                        str, unit_id, max_devs - 1);
2469 e4bcb14c ths
        return -1;
2470 e4bcb14c ths
    }
2471 e4bcb14c ths
2472 e4bcb14c ths
    /*
2473 e4bcb14c ths
     * ignore multiple definitions
2474 e4bcb14c ths
     */
2475 e4bcb14c ths
2476 f60d39bc ths
    if (drive_get_index(type, bus_id, unit_id) != -1)
2477 4d73cd3b aliguori
        return -2;
2478 e4bcb14c ths
2479 e4bcb14c ths
    /* init */
2480 e4bcb14c ths
2481 f60d39bc ths
    if (type == IF_IDE || type == IF_SCSI)
2482 c8522bdf balrog
        mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2483 e6198a70 balrog
    if (max_devs)
2484 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%i%s%i",
2485 e6198a70 balrog
                 devname, bus_id, mediastr, unit_id);
2486 e6198a70 balrog
    else
2487 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%s%i",
2488 e6198a70 balrog
                 devname, mediastr, unit_id);
2489 e4bcb14c ths
    bdrv = bdrv_new(buf);
2490 7d5aca9e aliguori
    drives_table_idx = drive_get_free_idx();
2491 7d5aca9e aliguori
    drives_table[drives_table_idx].bdrv = bdrv;
2492 7d5aca9e aliguori
    drives_table[drives_table_idx].type = type;
2493 7d5aca9e aliguori
    drives_table[drives_table_idx].bus = bus_id;
2494 7d5aca9e aliguori
    drives_table[drives_table_idx].unit = unit_id;
2495 7d5aca9e aliguori
    drives_table[drives_table_idx].onerror = onerror;
2496 b01b1111 aliguori
    drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
2497 e6a6dfe4 Anthony Liguori
    strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
2498 e4bcb14c ths
    nb_drives++;
2499 e4bcb14c ths
2500 f60d39bc ths
    switch(type) {
2501 e4bcb14c ths
    case IF_IDE:
2502 e4bcb14c ths
    case IF_SCSI:
2503 62d23efa aliguori
    case IF_XEN:
2504 e4bcb14c ths
        switch(media) {
2505 e4bcb14c ths
        case MEDIA_DISK:
2506 e4bcb14c ths
            if (cyls != 0) {
2507 e4bcb14c ths
                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2508 e4bcb14c ths
                bdrv_set_translation_hint(bdrv, translation);
2509 e4bcb14c ths
            }
2510 e4bcb14c ths
            break;
2511 e4bcb14c ths
        case MEDIA_CDROM:
2512 e4bcb14c ths
            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2513 e4bcb14c ths
            break;
2514 e4bcb14c ths
        }
2515 e4bcb14c ths
        break;
2516 e4bcb14c ths
    case IF_SD:
2517 e4bcb14c ths
        /* FIXME: This isn't really a floppy, but it's a reasonable
2518 e4bcb14c ths
           approximation.  */
2519 e4bcb14c ths
    case IF_FLOPPY:
2520 e4bcb14c ths
        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2521 e4bcb14c ths
        break;
2522 e4bcb14c ths
    case IF_PFLASH:
2523 e4bcb14c ths
    case IF_MTD:
2524 6e02c38d aliguori
    case IF_VIRTIO:
2525 e4bcb14c ths
        break;
2526 aae9460e Paul Brook
    case IF_COUNT:
2527 aae9460e Paul Brook
        abort();
2528 e4bcb14c ths
    }
2529 e4bcb14c ths
    if (!file[0])
2530 4d73cd3b aliguori
        return -2;
2531 33f00271 balrog
    bdrv_flags = 0;
2532 9f7965c7 aliguori
    if (snapshot) {
2533 33f00271 balrog
        bdrv_flags |= BDRV_O_SNAPSHOT;
2534 9f7965c7 aliguori
        cache = 2; /* always use write-back with snapshot */
2535 9f7965c7 aliguori
    }
2536 9f7965c7 aliguori
    if (cache == 0) /* no caching */
2537 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_NOCACHE;
2538 9f7965c7 aliguori
    else if (cache == 2) /* write-back */
2539 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_CACHE_WB;
2540 4dc822d7 aliguori
    else if (cache == 3) /* not specified */
2541 4dc822d7 aliguori
        bdrv_flags |= BDRV_O_CACHE_DEF;
2542 c0f4ce77 aliguori
    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
2543 e4bcb14c ths
        fprintf(stderr, "qemu: could not open disk image %s\n",
2544 e4bcb14c ths
                        file);
2545 e4bcb14c ths
        return -1;
2546 e4bcb14c ths
    }
2547 c0f4ce77 aliguori
    if (bdrv_key_required(bdrv))
2548 c0f4ce77 aliguori
        autostart = 0;
2549 4d73cd3b aliguori
    return drives_table_idx;
2550 e4bcb14c ths
}
2551 e4bcb14c ths
2552 268a362c aliguori
static void numa_add(const char *optarg)
2553 268a362c aliguori
{
2554 268a362c aliguori
    char option[128];
2555 268a362c aliguori
    char *endptr;
2556 268a362c aliguori
    unsigned long long value, endvalue;
2557 268a362c aliguori
    int nodenr;
2558 268a362c aliguori
2559 268a362c aliguori
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
2560 268a362c aliguori
    if (!strcmp(option, "node")) {
2561 268a362c aliguori
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2562 268a362c aliguori
            nodenr = nb_numa_nodes;
2563 268a362c aliguori
        } else {
2564 268a362c aliguori
            nodenr = strtoull(option, NULL, 10);
2565 268a362c aliguori
        }
2566 268a362c aliguori
2567 268a362c aliguori
        if (get_param_value(option, 128, "mem", optarg) == 0) {
2568 268a362c aliguori
            node_mem[nodenr] = 0;
2569 268a362c aliguori
        } else {
2570 268a362c aliguori
            value = strtoull(option, &endptr, 0);
2571 268a362c aliguori
            switch (*endptr) {
2572 268a362c aliguori
            case 0: case 'M': case 'm':
2573 268a362c aliguori
                value <<= 20;
2574 268a362c aliguori
                break;
2575 268a362c aliguori
            case 'G': case 'g':
2576 268a362c aliguori
                value <<= 30;
2577 268a362c aliguori
                break;
2578 268a362c aliguori
            }
2579 268a362c aliguori
            node_mem[nodenr] = value;
2580 268a362c aliguori
        }
2581 268a362c aliguori
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
2582 268a362c aliguori
            node_cpumask[nodenr] = 0;
2583 268a362c aliguori
        } else {
2584 268a362c aliguori
            value = strtoull(option, &endptr, 10);
2585 268a362c aliguori
            if (value >= 64) {
2586 268a362c aliguori
                value = 63;
2587 268a362c aliguori
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2588 268a362c aliguori
            } else {
2589 268a362c aliguori
                if (*endptr == '-') {
2590 268a362c aliguori
                    endvalue = strtoull(endptr+1, &endptr, 10);
2591 268a362c aliguori
                    if (endvalue >= 63) {
2592 268a362c aliguori
                        endvalue = 62;
2593 268a362c aliguori
                        fprintf(stderr,
2594 268a362c aliguori
                            "only 63 CPUs in NUMA mode supported.\n");
2595 268a362c aliguori
                    }
2596 268a362c aliguori
                    value = (1 << (endvalue + 1)) - (1 << value);
2597 268a362c aliguori
                } else {
2598 268a362c aliguori
                    value = 1 << value;
2599 268a362c aliguori
                }
2600 268a362c aliguori
            }
2601 268a362c aliguori
            node_cpumask[nodenr] = value;
2602 268a362c aliguori
        }
2603 268a362c aliguori
        nb_numa_nodes++;
2604 268a362c aliguori
    }
2605 268a362c aliguori
    return;
2606 268a362c aliguori
}
2607 268a362c aliguori
2608 330d0414 bellard
/***********************************************************/
2609 a594cfbf bellard
/* USB devices */
2610 a594cfbf bellard
2611 0d92ed30 pbrook
static USBPort *used_usb_ports;
2612 0d92ed30 pbrook
static USBPort *free_usb_ports;
2613 0d92ed30 pbrook
2614 0d92ed30 pbrook
/* ??? Maybe change this to register a hub to keep track of the topology.  */
2615 0d92ed30 pbrook
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2616 0d92ed30 pbrook
                            usb_attachfn attach)
2617 0d92ed30 pbrook
{
2618 0d92ed30 pbrook
    port->opaque = opaque;
2619 0d92ed30 pbrook
    port->index = index;
2620 0d92ed30 pbrook
    port->attach = attach;
2621 0d92ed30 pbrook
    port->next = free_usb_ports;
2622 0d92ed30 pbrook
    free_usb_ports = port;
2623 0d92ed30 pbrook
}
2624 0d92ed30 pbrook
2625 4b096fc9 aliguori
int usb_device_add_dev(USBDevice *dev)
2626 4b096fc9 aliguori
{
2627 4b096fc9 aliguori
    USBPort *port;
2628 4b096fc9 aliguori
2629 4b096fc9 aliguori
    /* Find a USB port to add the device to.  */
2630 4b096fc9 aliguori
    port = free_usb_ports;
2631 4b096fc9 aliguori
    if (!port->next) {
2632 4b096fc9 aliguori
        USBDevice *hub;
2633 4b096fc9 aliguori
2634 4b096fc9 aliguori
        /* Create a new hub and chain it on.  */
2635 4b096fc9 aliguori
        free_usb_ports = NULL;
2636 4b096fc9 aliguori
        port->next = used_usb_ports;
2637 4b096fc9 aliguori
        used_usb_ports = port;
2638 4b096fc9 aliguori
2639 4b096fc9 aliguori
        hub = usb_hub_init(VM_USB_HUB_SIZE);
2640 4b096fc9 aliguori
        usb_attach(port, hub);
2641 4b096fc9 aliguori
        port = free_usb_ports;
2642 4b096fc9 aliguori
    }
2643 4b096fc9 aliguori
2644 4b096fc9 aliguori
    free_usb_ports = port->next;
2645 4b096fc9 aliguori
    port->next = used_usb_ports;
2646 4b096fc9 aliguori
    used_usb_ports = port;
2647 4b096fc9 aliguori
    usb_attach(port, dev);
2648 4b096fc9 aliguori
    return 0;
2649 4b096fc9 aliguori
}
2650 4b096fc9 aliguori
2651 bb5fc20f aliguori
static void usb_msd_password_cb(void *opaque, int err)
2652 bb5fc20f aliguori
{
2653 bb5fc20f aliguori
    USBDevice *dev = opaque;
2654 bb5fc20f aliguori
2655 bb5fc20f aliguori
    if (!err)
2656 bb5fc20f aliguori
        usb_device_add_dev(dev);
2657 bb5fc20f aliguori
    else
2658 bb5fc20f aliguori
        dev->handle_destroy(dev);
2659 bb5fc20f aliguori
}
2660 bb5fc20f aliguori
2661 c0f4ce77 aliguori
static int usb_device_add(const char *devname, int is_hotplug)
2662 a594cfbf bellard
{
2663 a594cfbf bellard
    const char *p;
2664 a594cfbf bellard
    USBDevice *dev;
2665 a594cfbf bellard
2666 0d92ed30 pbrook
    if (!free_usb_ports)
2667 a594cfbf bellard
        return -1;
2668 a594cfbf bellard
2669 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
2670 a594cfbf bellard
        dev = usb_host_device_open(p);
2671 a594cfbf bellard
    } else if (!strcmp(devname, "mouse")) {
2672 a594cfbf bellard
        dev = usb_mouse_init();
2673 09b26c5e bellard
    } else if (!strcmp(devname, "tablet")) {
2674 47b2d338 balrog
        dev = usb_tablet_init();
2675 47b2d338 balrog
    } else if (!strcmp(devname, "keyboard")) {
2676 47b2d338 balrog
        dev = usb_keyboard_init();
2677 2e5d83bb pbrook
    } else if (strstart(devname, "disk:", &p)) {
2678 c0f4ce77 aliguori
        BlockDriverState *bs;
2679 c0f4ce77 aliguori
2680 bb5fc20f aliguori
        dev = usb_msd_init(p);
2681 c0f4ce77 aliguori
        if (!dev)
2682 c0f4ce77 aliguori
            return -1;
2683 bb5fc20f aliguori
        bs = usb_msd_get_bdrv(dev);
2684 c0f4ce77 aliguori
        if (bdrv_key_required(bs)) {
2685 c0f4ce77 aliguori
            autostart = 0;
2686 bb5fc20f aliguori
            if (is_hotplug) {
2687 376253ec aliguori
                monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2688 376253ec aliguori
                                            dev);
2689 bb5fc20f aliguori
                return 0;
2690 c0f4ce77 aliguori
            }
2691 c0f4ce77 aliguori
        }
2692 f6d2a316 balrog
    } else if (!strcmp(devname, "wacom-tablet")) {
2693 f6d2a316 balrog
        dev = usb_wacom_init();
2694 a7954218 balrog
    } else if (strstart(devname, "serial:", &p)) {
2695 a7954218 balrog
        dev = usb_serial_init(p);
2696 2e4d9fb1 aurel32
#ifdef CONFIG_BRLAPI
2697 2e4d9fb1 aurel32
    } else if (!strcmp(devname, "braille")) {
2698 2e4d9fb1 aurel32
        dev = usb_baum_init();
2699 2e4d9fb1 aurel32
#endif
2700 6c9f886c balrog
    } else if (strstart(devname, "net:", &p)) {
2701 9ad97e65 balrog
        int nic = nb_nics;
2702 6c9f886c balrog
2703 10ae5a7a Jan Kiszka
        if (net_client_init(NULL, "nic", p) < 0)
2704 6c9f886c balrog
            return -1;
2705 9ad97e65 balrog
        nd_table[nic].model = "usb";
2706 9ad97e65 balrog
        dev = usb_net_init(&nd_table[nic]);
2707 dc72ac14 balrog
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2708 dc72ac14 balrog
        dev = usb_bt_init(devname[2] ? hci_init(p) :
2709 dc72ac14 balrog
                        bt_new_hci(qemu_find_bt_vlan(0)));
2710 a594cfbf bellard
    } else {
2711 a594cfbf bellard
        return -1;
2712 a594cfbf bellard
    }
2713 0d92ed30 pbrook
    if (!dev)
2714 0d92ed30 pbrook
        return -1;
2715 0d92ed30 pbrook
2716 4b096fc9 aliguori
    return usb_device_add_dev(dev);
2717 a594cfbf bellard
}
2718 a594cfbf bellard
2719 1f3870ab aliguori
int usb_device_del_addr(int bus_num, int addr)
2720 a594cfbf bellard
{
2721 0d92ed30 pbrook
    USBPort *port;
2722 0d92ed30 pbrook
    USBPort **lastp;
2723 059809e4 bellard
    USBDevice *dev;
2724 a594cfbf bellard
2725 0d92ed30 pbrook
    if (!used_usb_ports)
2726 a594cfbf bellard
        return -1;
2727 a594cfbf bellard
2728 a594cfbf bellard
    if (bus_num != 0)
2729 a594cfbf bellard
        return -1;
2730 0d92ed30 pbrook
2731 0d92ed30 pbrook
    lastp = &used_usb_ports;
2732 0d92ed30 pbrook
    port = used_usb_ports;
2733 0d92ed30 pbrook
    while (port && port->dev->addr != addr) {
2734 0d92ed30 pbrook
        lastp = &port->next;
2735 0d92ed30 pbrook
        port = port->next;
2736 a594cfbf bellard
    }
2737 0d92ed30 pbrook
2738 0d92ed30 pbrook
    if (!port)
2739 a594cfbf bellard
        return -1;
2740 0d92ed30 pbrook
2741 059809e4 bellard
    dev = port->dev;
2742 0d92ed30 pbrook
    *lastp = port->next;
2743 0d92ed30 pbrook
    usb_attach(port, NULL);
2744 059809e4 bellard
    dev->handle_destroy(dev);
2745 0d92ed30 pbrook
    port->next = free_usb_ports;
2746 0d92ed30 pbrook
    free_usb_ports = port;
2747 a594cfbf bellard
    return 0;
2748 a594cfbf bellard
}
2749 a594cfbf bellard
2750 1f3870ab aliguori
static int usb_device_del(const char *devname)
2751 1f3870ab aliguori
{
2752 1f3870ab aliguori
    int bus_num, addr;
2753 1f3870ab aliguori
    const char *p;
2754 1f3870ab aliguori
2755 5d0c5750 aliguori
    if (strstart(devname, "host:", &p))
2756 5d0c5750 aliguori
        return usb_host_device_close(p);
2757 5d0c5750 aliguori
2758 1f3870ab aliguori
    if (!used_usb_ports)
2759 1f3870ab aliguori
        return -1;
2760 1f3870ab aliguori
2761 1f3870ab aliguori
    p = strchr(devname, '.');
2762 1f3870ab aliguori
    if (!p)
2763 1f3870ab aliguori
        return -1;
2764 1f3870ab aliguori
    bus_num = strtoul(devname, NULL, 0);
2765 1f3870ab aliguori
    addr = strtoul(p + 1, NULL, 0);
2766 1f3870ab aliguori
2767 1f3870ab aliguori
    return usb_device_del_addr(bus_num, addr);
2768 1f3870ab aliguori
}
2769 1f3870ab aliguori
2770 376253ec aliguori
void do_usb_add(Monitor *mon, const char *devname)
2771 a594cfbf bellard
{
2772 c0f4ce77 aliguori
    usb_device_add(devname, 1);
2773 a594cfbf bellard
}
2774 a594cfbf bellard
2775 376253ec aliguori
void do_usb_del(Monitor *mon, const char *devname)
2776 a594cfbf bellard
{
2777 4b096fc9 aliguori
    usb_device_del(devname);
2778 a594cfbf bellard
}
2779 a594cfbf bellard
2780 376253ec aliguori
void usb_info(Monitor *mon)
2781 a594cfbf bellard
{
2782 a594cfbf bellard
    USBDevice *dev;
2783 0d92ed30 pbrook
    USBPort *port;
2784 a594cfbf bellard
    const char *speed_str;
2785 a594cfbf bellard
2786 0d92ed30 pbrook
    if (!usb_enabled) {
2787 376253ec aliguori
        monitor_printf(mon, "USB support not enabled\n");
2788 a594cfbf bellard
        return;
2789 a594cfbf bellard
    }
2790 a594cfbf bellard
2791 0d92ed30 pbrook
    for (port = used_usb_ports; port; port = port->next) {
2792 0d92ed30 pbrook
        dev = port->dev;
2793 0d92ed30 pbrook
        if (!dev)
2794 0d92ed30 pbrook
            continue;
2795 0d92ed30 pbrook
        switch(dev->speed) {
2796 5fafdf24 ths
        case USB_SPEED_LOW:
2797 5fafdf24 ths
            speed_str = "1.5";
2798 0d92ed30 pbrook
            break;
2799 5fafdf24 ths
        case USB_SPEED_FULL:
2800 5fafdf24 ths
            speed_str = "12";
2801 0d92ed30 pbrook
            break;
2802 5fafdf24 ths
        case USB_SPEED_HIGH:
2803 5fafdf24 ths
            speed_str = "480";
2804 0d92ed30 pbrook
            break;
2805 0d92ed30 pbrook
        default:
2806 5fafdf24 ths
            speed_str = "?";
2807 0d92ed30 pbrook
            break;
2808 a594cfbf bellard
        }
2809 376253ec aliguori
        monitor_printf(mon, "  Device %d.%d, Speed %s Mb/s, Product %s\n",
2810 376253ec aliguori
                       0, dev->addr, speed_str, dev->devname);
2811 a594cfbf bellard
    }
2812 a594cfbf bellard
}
2813 a594cfbf bellard
2814 f7cce898 bellard
/***********************************************************/
2815 201a51fc balrog
/* PCMCIA/Cardbus */
2816 201a51fc balrog
2817 201a51fc balrog
static struct pcmcia_socket_entry_s {
2818 bc24a225 Paul Brook
    PCMCIASocket *socket;
2819 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
2820 201a51fc balrog
} *pcmcia_sockets = 0;
2821 201a51fc balrog
2822 bc24a225 Paul Brook
void pcmcia_socket_register(PCMCIASocket *socket)
2823 201a51fc balrog
{
2824 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
2825 201a51fc balrog
2826 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2827 201a51fc balrog
    entry->socket = socket;
2828 201a51fc balrog
    entry->next = pcmcia_sockets;
2829 201a51fc balrog
    pcmcia_sockets = entry;
2830 201a51fc balrog
}
2831 201a51fc balrog
2832 bc24a225 Paul Brook
void pcmcia_socket_unregister(PCMCIASocket *socket)
2833 201a51fc balrog
{
2834 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
2835 201a51fc balrog
2836 201a51fc balrog
    ptr = &pcmcia_sockets;
2837 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2838 201a51fc balrog
        if (entry->socket == socket) {
2839 201a51fc balrog
            *ptr = entry->next;
2840 201a51fc balrog
            qemu_free(entry);
2841 201a51fc balrog
        }
2842 201a51fc balrog
}
2843 201a51fc balrog
2844 376253ec aliguori
void pcmcia_info(Monitor *mon)
2845 201a51fc balrog
{
2846 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
2847 376253ec aliguori
2848 201a51fc balrog
    if (!pcmcia_sockets)
2849 376253ec aliguori
        monitor_printf(mon, "No PCMCIA sockets\n");
2850 201a51fc balrog
2851 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
2852 376253ec aliguori
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2853 376253ec aliguori
                       iter->socket->attached ? iter->socket->card_string :
2854 376253ec aliguori
                       "Empty");
2855 201a51fc balrog
}
2856 201a51fc balrog
2857 201a51fc balrog
/***********************************************************/
2858 3023f332 aliguori
/* register display */
2859 3023f332 aliguori
2860 7b5d76da aliguori
struct DisplayAllocator default_allocator = {
2861 7b5d76da aliguori
    defaultallocator_create_displaysurface,
2862 7b5d76da aliguori
    defaultallocator_resize_displaysurface,
2863 7b5d76da aliguori
    defaultallocator_free_displaysurface
2864 7b5d76da aliguori
};
2865 7b5d76da aliguori
2866 3023f332 aliguori
void register_displaystate(DisplayState *ds)
2867 3023f332 aliguori
{
2868 3023f332 aliguori
    DisplayState **s;
2869 3023f332 aliguori
    s = &display_state;
2870 3023f332 aliguori
    while (*s != NULL)
2871 3023f332 aliguori
        s = &(*s)->next;
2872 3023f332 aliguori
    ds->next = NULL;
2873 3023f332 aliguori
    *s = ds;
2874 3023f332 aliguori
}
2875 3023f332 aliguori
2876 3023f332 aliguori
DisplayState *get_displaystate(void)
2877 3023f332 aliguori
{
2878 3023f332 aliguori
    return display_state;
2879 3023f332 aliguori
}
2880 3023f332 aliguori
2881 7b5d76da aliguori
DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2882 7b5d76da aliguori
{
2883 7b5d76da aliguori
    if(ds->allocator ==  &default_allocator) ds->allocator = da;
2884 7b5d76da aliguori
    return ds->allocator;
2885 7b5d76da aliguori
}
2886 7b5d76da aliguori
2887 2ff89790 ths
/* dumb display */
2888 2ff89790 ths
2889 8f391ab4 aliguori
static void dumb_display_init(void)
2890 2ff89790 ths
{
2891 8f391ab4 aliguori
    DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
2892 7b5d76da aliguori
    ds->allocator = &default_allocator;
2893 7b5d76da aliguori
    ds->surface = qemu_create_displaysurface(ds, 640, 480);
2894 8f391ab4 aliguori
    register_displaystate(ds);
2895 2ff89790 ths
}
2896 2ff89790 ths
2897 2ff89790 ths
/***********************************************************/
2898 8a7ddc38 bellard
/* I/O handling */
2899 0824d6fc bellard
2900 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
2901 c4b1fcc0 bellard
    int fd;
2902 7c9d8e07 bellard
    IOCanRWHandler *fd_read_poll;
2903 7c9d8e07 bellard
    IOHandler *fd_read;
2904 7c9d8e07 bellard
    IOHandler *fd_write;
2905 cafffd40 ths
    int deleted;
2906 c4b1fcc0 bellard
    void *opaque;
2907 c4b1fcc0 bellard
    /* temporary data */
2908 c4b1fcc0 bellard
    struct pollfd *ufd;
2909 8a7ddc38 bellard
    struct IOHandlerRecord *next;
2910 c4b1fcc0 bellard
} IOHandlerRecord;
2911 c4b1fcc0 bellard
2912 8a7ddc38 bellard
static IOHandlerRecord *first_io_handler;
2913 c4b1fcc0 bellard
2914 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
2915 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
2916 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
2917 5fafdf24 ths
                         IOCanRWHandler *fd_read_poll,
2918 5fafdf24 ths
                         IOHandler *fd_read,
2919 5fafdf24 ths
                         IOHandler *fd_write,
2920 7c9d8e07 bellard
                         void *opaque)
2921 c4b1fcc0 bellard
{
2922 7c9d8e07 bellard
    IOHandlerRecord **pioh, *ioh;
2923 c4b1fcc0 bellard
2924 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
2925 7c9d8e07 bellard
        pioh = &first_io_handler;
2926 7c9d8e07 bellard
        for(;;) {
2927 7c9d8e07 bellard
            ioh = *pioh;
2928 7c9d8e07 bellard
            if (ioh == NULL)
2929 7c9d8e07 bellard
                break;
2930 7c9d8e07 bellard
            if (ioh->fd == fd) {
2931 cafffd40 ths
                ioh->deleted = 1;
2932 7c9d8e07 bellard
                break;
2933 7c9d8e07 bellard
            }
2934 7c9d8e07 bellard
            pioh = &ioh->next;
2935 7c9d8e07 bellard
        }
2936 7c9d8e07 bellard
    } else {
2937 7c9d8e07 bellard
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2938 7c9d8e07 bellard
            if (ioh->fd == fd)
2939 7c9d8e07 bellard
                goto found;
2940 7c9d8e07 bellard
        }
2941 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2942 7c9d8e07 bellard
        ioh->next = first_io_handler;
2943 7c9d8e07 bellard
        first_io_handler = ioh;
2944 7c9d8e07 bellard
    found:
2945 7c9d8e07 bellard
        ioh->fd = fd;
2946 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
2947 7c9d8e07 bellard
        ioh->fd_read = fd_read;
2948 7c9d8e07 bellard
        ioh->fd_write = fd_write;
2949 7c9d8e07 bellard
        ioh->opaque = opaque;
2950 cafffd40 ths
        ioh->deleted = 0;
2951 7c9d8e07 bellard
    }
2952 c4b1fcc0 bellard
    return 0;
2953 c4b1fcc0 bellard
}
2954 c4b1fcc0 bellard
2955 5fafdf24 ths
int qemu_set_fd_handler(int fd,
2956 5fafdf24 ths
                        IOHandler *fd_read,
2957 5fafdf24 ths
                        IOHandler *fd_write,
2958 7c9d8e07 bellard
                        void *opaque)
2959 8a7ddc38 bellard
{
2960 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2961 8a7ddc38 bellard
}
2962 8a7ddc38 bellard
2963 56f3a5d0 aliguori
#ifdef _WIN32
2964 8a7ddc38 bellard
/***********************************************************/
2965 f331110f bellard
/* Polling handling */
2966 f331110f bellard
2967 f331110f bellard
typedef struct PollingEntry {
2968 f331110f bellard
    PollingFunc *func;
2969 f331110f bellard
    void *opaque;
2970 f331110f bellard
    struct PollingEntry *next;
2971 f331110f bellard
} PollingEntry;
2972 f331110f bellard
2973 f331110f bellard
static PollingEntry *first_polling_entry;
2974 f331110f bellard
2975 f331110f bellard
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2976 f331110f bellard
{
2977 f331110f bellard
    PollingEntry **ppe, *pe;
2978 f331110f bellard
    pe = qemu_mallocz(sizeof(PollingEntry));
2979 f331110f bellard
    pe->func = func;
2980 f331110f bellard
    pe->opaque = opaque;
2981 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2982 f331110f bellard
    *ppe = pe;
2983 f331110f bellard
    return 0;
2984 f331110f bellard
}
2985 f331110f bellard
2986 f331110f bellard
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2987 f331110f bellard
{
2988 f331110f bellard
    PollingEntry **ppe, *pe;
2989 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2990 f331110f bellard
        pe = *ppe;
2991 f331110f bellard
        if (pe->func == func && pe->opaque == opaque) {
2992 f331110f bellard
            *ppe = pe->next;
2993 f331110f bellard
            qemu_free(pe);
2994 f331110f bellard
            break;
2995 f331110f bellard
        }
2996 f331110f bellard
    }
2997 f331110f bellard
}
2998 f331110f bellard
2999 a18e524a bellard
/***********************************************************/
3000 a18e524a bellard
/* Wait objects support */
3001 a18e524a bellard
typedef struct WaitObjects {
3002 a18e524a bellard
    int num;
3003 a18e524a bellard
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
3004 a18e524a bellard
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
3005 a18e524a bellard
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
3006 a18e524a bellard
} WaitObjects;
3007 a18e524a bellard
3008 a18e524a bellard
static WaitObjects wait_objects = {0};
3009 3b46e624 ths
3010 a18e524a bellard
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3011 a18e524a bellard
{
3012 a18e524a bellard
    WaitObjects *w = &wait_objects;
3013 a18e524a bellard
3014 a18e524a bellard
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
3015 a18e524a bellard
        return -1;
3016 a18e524a bellard
    w->events[w->num] = handle;
3017 a18e524a bellard
    w->func[w->num] = func;
3018 a18e524a bellard
    w->opaque[w->num] = opaque;
3019 a18e524a bellard
    w->num++;
3020 a18e524a bellard
    return 0;
3021 a18e524a bellard
}
3022 a18e524a bellard
3023 a18e524a bellard
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
3024 a18e524a bellard
{
3025 a18e524a bellard
    int i, found;
3026 a18e524a bellard
    WaitObjects *w = &wait_objects;
3027 a18e524a bellard
3028 a18e524a bellard
    found = 0;
3029 a18e524a bellard
    for (i = 0; i < w->num; i++) {
3030 a18e524a bellard
        if (w->events[i] == handle)
3031 a18e524a bellard
            found = 1;
3032 a18e524a bellard
        if (found) {
3033 a18e524a bellard
            w->events[i] = w->events[i + 1];
3034 a18e524a bellard
            w->func[i] = w->func[i + 1];
3035 a18e524a bellard
            w->opaque[i] = w->opaque[i + 1];
3036 3b46e624 ths
        }
3037 a18e524a bellard
    }
3038 a18e524a bellard
    if (found)
3039 a18e524a bellard
        w->num--;
3040 a18e524a bellard
}
3041 a18e524a bellard
#endif
3042 a18e524a bellard
3043 8a7ddc38 bellard
/***********************************************************/
3044 8a7ddc38 bellard
/* ram save/restore */
3045 8a7ddc38 bellard
3046 8a7ddc38 bellard
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3047 8a7ddc38 bellard
{
3048 8a7ddc38 bellard
    int v;
3049 8a7ddc38 bellard
3050 8a7ddc38 bellard
    v = qemu_get_byte(f);
3051 8a7ddc38 bellard
    switch(v) {
3052 8a7ddc38 bellard
    case 0:
3053 8a7ddc38 bellard
        if (qemu_get_buffer(f, buf, len) != len)
3054 8a7ddc38 bellard
            return -EIO;
3055 8a7ddc38 bellard
        break;
3056 8a7ddc38 bellard
    case 1:
3057 8a7ddc38 bellard
        v = qemu_get_byte(f);
3058 8a7ddc38 bellard
        memset(buf, v, len);
3059 8a7ddc38 bellard
        break;
3060 8a7ddc38 bellard
    default:
3061 8a7ddc38 bellard
        return -EINVAL;
3062 8a7ddc38 bellard
    }
3063 871d2f07 aliguori
3064 871d2f07 aliguori
    if (qemu_file_has_error(f))
3065 871d2f07 aliguori
        return -EIO;
3066 871d2f07 aliguori
3067 8a7ddc38 bellard
    return 0;
3068 8a7ddc38 bellard
}
3069 8a7ddc38 bellard
3070 c88676f8 bellard
static int ram_load_v1(QEMUFile *f, void *opaque)
3071 c88676f8 bellard
{
3072 00f82b8a aurel32
    int ret;
3073 00f82b8a aurel32
    ram_addr_t i;
3074 c88676f8 bellard
3075 94a6b54f pbrook
    if (qemu_get_be32(f) != last_ram_offset)
3076 c88676f8 bellard
        return -EINVAL;
3077 94a6b54f pbrook
    for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
3078 5579c7f3 pbrook
        ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
3079 c88676f8 bellard
        if (ret)
3080 c88676f8 bellard
            return ret;
3081 c88676f8 bellard
    }
3082 c88676f8 bellard
    return 0;
3083 c88676f8 bellard
}
3084 c88676f8 bellard
3085 c88676f8 bellard
#define BDRV_HASH_BLOCK_SIZE 1024
3086 c88676f8 bellard
#define IOBUF_SIZE 4096
3087 c88676f8 bellard
#define RAM_CBLOCK_MAGIC 0xfabe
3088 c88676f8 bellard
3089 c88676f8 bellard
typedef struct RamDecompressState {
3090 c88676f8 bellard
    z_stream zstream;
3091 c88676f8 bellard
    QEMUFile *f;
3092 c88676f8 bellard
    uint8_t buf[IOBUF_SIZE];
3093 c88676f8 bellard
} RamDecompressState;
3094 c88676f8 bellard
3095 c88676f8 bellard
static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3096 c88676f8 bellard
{
3097 c88676f8 bellard
    int ret;
3098 c88676f8 bellard
    memset(s, 0, sizeof(*s));
3099 c88676f8 bellard
    s->f = f;
3100 c88676f8 bellard
    ret = inflateInit(&s->zstream);
3101 c88676f8 bellard
    if (ret != Z_OK)
3102 c88676f8 bellard
        return -1;
3103 c88676f8 bellard
    return 0;
3104 c88676f8 bellard
}
3105 c88676f8 bellard
3106 c88676f8 bellard
static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3107 c88676f8 bellard
{
3108 c88676f8 bellard
    int ret, clen;
3109 c88676f8 bellard
3110 c88676f8 bellard
    s->zstream.avail_out = len;
3111 c88676f8 bellard
    s->zstream.next_out = buf;
3112 c88676f8 bellard
    while (s->zstream.avail_out > 0) {
3113 c88676f8 bellard
        if (s->zstream.avail_in == 0) {
3114 c88676f8 bellard
            if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3115 c88676f8 bellard
                return -1;
3116 c88676f8 bellard
            clen = qemu_get_be16(s->f);
3117 c88676f8 bellard
            if (clen > IOBUF_SIZE)
3118 c88676f8 bellard
                return -1;
3119 c88676f8 bellard
            qemu_get_buffer(s->f, s->buf, clen);
3120 c88676f8 bellard
            s->zstream.avail_in = clen;
3121 c88676f8 bellard
            s->zstream.next_in = s->buf;
3122 c88676f8 bellard
        }
3123 c88676f8 bellard
        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3124 c88676f8 bellard
        if (ret != Z_OK && ret != Z_STREAM_END) {
3125 c88676f8 bellard
            return -1;
3126 c88676f8 bellard
        }
3127 c88676f8 bellard
    }
3128 c88676f8 bellard
    return 0;
3129 c88676f8 bellard
}
3130 c88676f8 bellard
3131 c88676f8 bellard
static void ram_decompress_close(RamDecompressState *s)
3132 c88676f8 bellard
{
3133 c88676f8 bellard
    inflateEnd(&s->zstream);
3134 c88676f8 bellard
}
3135 c88676f8 bellard
3136 475e4277 aliguori
#define RAM_SAVE_FLAG_FULL        0x01
3137 475e4277 aliguori
#define RAM_SAVE_FLAG_COMPRESS        0x02
3138 475e4277 aliguori
#define RAM_SAVE_FLAG_MEM_SIZE        0x04
3139 475e4277 aliguori
#define RAM_SAVE_FLAG_PAGE        0x08
3140 475e4277 aliguori
#define RAM_SAVE_FLAG_EOS        0x10
3141 475e4277 aliguori
3142 475e4277 aliguori
static int is_dup_page(uint8_t *page, uint8_t ch)
3143 8a7ddc38 bellard
{
3144 475e4277 aliguori
    uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3145 475e4277 aliguori
    uint32_t *array = (uint32_t *)page;
3146 475e4277 aliguori
    int i;
3147 3b46e624 ths
3148 475e4277 aliguori
    for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3149 475e4277 aliguori
        if (array[i] != val)
3150 475e4277 aliguori
            return 0;
3151 475e4277 aliguori
    }
3152 475e4277 aliguori
3153 475e4277 aliguori
    return 1;
3154 475e4277 aliguori
}
3155 475e4277 aliguori
3156 475e4277 aliguori
static int ram_save_block(QEMUFile *f)
3157 475e4277 aliguori
{
3158 475e4277 aliguori
    static ram_addr_t current_addr = 0;
3159 475e4277 aliguori
    ram_addr_t saved_addr = current_addr;
3160 475e4277 aliguori
    ram_addr_t addr = 0;
3161 475e4277 aliguori
    int found = 0;
3162 475e4277 aliguori
3163 94a6b54f pbrook
    while (addr < last_ram_offset) {
3164 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3165 5579c7f3 pbrook
            uint8_t *p;
3166 475e4277 aliguori
3167 475e4277 aliguori
            cpu_physical_memory_reset_dirty(current_addr,
3168 475e4277 aliguori
                                            current_addr + TARGET_PAGE_SIZE,
3169 475e4277 aliguori
                                            MIGRATION_DIRTY_FLAG);
3170 475e4277 aliguori
3171 5579c7f3 pbrook
            p = qemu_get_ram_ptr(current_addr);
3172 475e4277 aliguori
3173 5579c7f3 pbrook
            if (is_dup_page(p, *p)) {
3174 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3175 5579c7f3 pbrook
                qemu_put_byte(f, *p);
3176 475e4277 aliguori
            } else {
3177 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3178 5579c7f3 pbrook
                qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
3179 c88676f8 bellard
            }
3180 475e4277 aliguori
3181 475e4277 aliguori
            found = 1;
3182 475e4277 aliguori
            break;
3183 c88676f8 bellard
        }
3184 475e4277 aliguori
        addr += TARGET_PAGE_SIZE;
3185 94a6b54f pbrook
        current_addr = (saved_addr + addr) % last_ram_offset;
3186 8a7ddc38 bellard
    }
3187 475e4277 aliguori
3188 475e4277 aliguori
    return found;
3189 8a7ddc38 bellard
}
3190 8a7ddc38 bellard
3191 475e4277 aliguori
static ram_addr_t ram_save_threshold = 10;
3192 9f9e28cd Glauber Costa
static uint64_t bytes_transferred = 0;
3193 475e4277 aliguori
3194 475e4277 aliguori
static ram_addr_t ram_save_remaining(void)
3195 475e4277 aliguori
{
3196 475e4277 aliguori
    ram_addr_t addr;
3197 475e4277 aliguori
    ram_addr_t count = 0;
3198 475e4277 aliguori
3199 94a6b54f pbrook
    for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3200 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3201 475e4277 aliguori
            count++;
3202 475e4277 aliguori
    }
3203 475e4277 aliguori
3204 475e4277 aliguori
    return count;
3205 475e4277 aliguori
}
3206 475e4277 aliguori
3207 9f9e28cd Glauber Costa
uint64_t ram_bytes_remaining(void)
3208 9f9e28cd Glauber Costa
{
3209 9f9e28cd Glauber Costa
    return ram_save_remaining() * TARGET_PAGE_SIZE;
3210 9f9e28cd Glauber Costa
}
3211 9f9e28cd Glauber Costa
3212 9f9e28cd Glauber Costa
uint64_t ram_bytes_transferred(void)
3213 9f9e28cd Glauber Costa
{
3214 9f9e28cd Glauber Costa
    return bytes_transferred;
3215 9f9e28cd Glauber Costa
}
3216 9f9e28cd Glauber Costa
3217 9f9e28cd Glauber Costa
uint64_t ram_bytes_total(void)
3218 9f9e28cd Glauber Costa
{
3219 9f9e28cd Glauber Costa
    return last_ram_offset;
3220 9f9e28cd Glauber Costa
}
3221 9f9e28cd Glauber Costa
3222 475e4277 aliguori
static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3223 475e4277 aliguori
{
3224 475e4277 aliguori
    ram_addr_t addr;
3225 475e4277 aliguori
3226 9fa06385 Jan Kiszka
    if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
3227 b0a46a33 Jan Kiszka
        qemu_file_set_error(f);
3228 b0a46a33 Jan Kiszka
        return 0;
3229 b0a46a33 Jan Kiszka
    }
3230 b0a46a33 Jan Kiszka
3231 475e4277 aliguori
    if (stage == 1) {
3232 475e4277 aliguori
        /* Make sure all dirty bits are set */
3233 94a6b54f pbrook
        for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
3234 475e4277 aliguori
            if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3235 475e4277 aliguori
                cpu_physical_memory_set_dirty(addr);
3236 475e4277 aliguori
        }
3237 b0a46a33 Jan Kiszka
3238 475e4277 aliguori
        /* Enable dirty memory tracking */
3239 475e4277 aliguori
        cpu_physical_memory_set_dirty_tracking(1);
3240 475e4277 aliguori
3241 94a6b54f pbrook
        qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
3242 475e4277 aliguori
    }
3243 475e4277 aliguori
3244 475e4277 aliguori
    while (!qemu_file_rate_limit(f)) {
3245 475e4277 aliguori
        int ret;
3246 475e4277 aliguori
3247 475e4277 aliguori
        ret = ram_save_block(f);
3248 9f9e28cd Glauber Costa
        bytes_transferred += ret * TARGET_PAGE_SIZE;
3249 475e4277 aliguori
        if (ret == 0) /* no more blocks */
3250 475e4277 aliguori
            break;
3251 475e4277 aliguori
    }
3252 475e4277 aliguori
3253 475e4277 aliguori
    /* try transferring iterative blocks of memory */
3254 475e4277 aliguori
3255 475e4277 aliguori
    if (stage == 3) {
3256 475e4277 aliguori
3257 475e4277 aliguori
        /* flush all remaining blocks regardless of rate limiting */
3258 9f9e28cd Glauber Costa
        while (ram_save_block(f) != 0) {
3259 9f9e28cd Glauber Costa
            bytes_transferred += TARGET_PAGE_SIZE;
3260 9f9e28cd Glauber Costa
        }
3261 8215e914 aliguori
        cpu_physical_memory_set_dirty_tracking(0);
3262 475e4277 aliguori
    }
3263 475e4277 aliguori
3264 475e4277 aliguori
    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3265 475e4277 aliguori
3266 475e4277 aliguori
    return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3267 475e4277 aliguori
}
3268 475e4277 aliguori
3269 475e4277 aliguori
static int ram_load_dead(QEMUFile *f, void *opaque)
3270 8a7ddc38 bellard
{
3271 c88676f8 bellard
    RamDecompressState s1, *s = &s1;
3272 c88676f8 bellard
    uint8_t buf[10];
3273 00f82b8a aurel32
    ram_addr_t i;
3274 8a7ddc38 bellard
3275 c88676f8 bellard
    if (ram_decompress_open(s, f) < 0)
3276 c88676f8 bellard
        return -EINVAL;
3277 94a6b54f pbrook
    for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
3278 c88676f8 bellard
        if (ram_decompress_buf(s, buf, 1) < 0) {
3279 c88676f8 bellard
            fprintf(stderr, "Error while reading ram block header\n");
3280 c88676f8 bellard
            goto error;
3281 c88676f8 bellard
        }
3282 c88676f8 bellard
        if (buf[0] == 0) {
3283 5579c7f3 pbrook
            if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3284 5579c7f3 pbrook
                                   BDRV_HASH_BLOCK_SIZE) < 0) {
3285 00f82b8a aurel32
                fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
3286 c88676f8 bellard
                goto error;
3287 c88676f8 bellard
            }
3288 475e4277 aliguori
        } else {
3289 c88676f8 bellard
        error:
3290 c88676f8 bellard
            printf("Error block header\n");
3291 c88676f8 bellard
            return -EINVAL;
3292 c88676f8 bellard
        }
3293 8a7ddc38 bellard
    }
3294 c88676f8 bellard
    ram_decompress_close(s);
3295 475e4277 aliguori
3296 475e4277 aliguori
    return 0;
3297 475e4277 aliguori
}
3298 475e4277 aliguori
3299 475e4277 aliguori
static int ram_load(QEMUFile *f, void *opaque, int version_id)
3300 475e4277 aliguori
{
3301 475e4277 aliguori
    ram_addr_t addr;
3302 475e4277 aliguori
    int flags;
3303 475e4277 aliguori
3304 475e4277 aliguori
    if (version_id == 1)
3305 475e4277 aliguori
        return ram_load_v1(f, opaque);
3306 475e4277 aliguori
3307 475e4277 aliguori
    if (version_id == 2) {
3308 94a6b54f pbrook
        if (qemu_get_be32(f) != last_ram_offset)
3309 475e4277 aliguori
            return -EINVAL;
3310 475e4277 aliguori
        return ram_load_dead(f, opaque);
3311 475e4277 aliguori
    }
3312 475e4277 aliguori
3313 475e4277 aliguori
    if (version_id != 3)
3314 475e4277 aliguori
        return -EINVAL;
3315 475e4277 aliguori
3316 475e4277 aliguori
    do {
3317 475e4277 aliguori
        addr = qemu_get_be64(f);
3318 475e4277 aliguori
3319 475e4277 aliguori
        flags = addr & ~TARGET_PAGE_MASK;
3320 475e4277 aliguori
        addr &= TARGET_PAGE_MASK;
3321 475e4277 aliguori
3322 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3323 94a6b54f pbrook
            if (addr != last_ram_offset)
3324 475e4277 aliguori
                return -EINVAL;
3325 475e4277 aliguori
        }
3326 475e4277 aliguori
3327 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_FULL) {
3328 475e4277 aliguori
            if (ram_load_dead(f, opaque) < 0)
3329 475e4277 aliguori
                return -EINVAL;
3330 475e4277 aliguori
        }
3331 475e4277 aliguori
        
3332 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_COMPRESS) {
3333 475e4277 aliguori
            uint8_t ch = qemu_get_byte(f);
3334 5579c7f3 pbrook
            memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3335 475e4277 aliguori
        } else if (flags & RAM_SAVE_FLAG_PAGE)
3336 5579c7f3 pbrook
            qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
3337 475e4277 aliguori
    } while (!(flags & RAM_SAVE_FLAG_EOS));
3338 475e4277 aliguori
3339 8a7ddc38 bellard
    return 0;
3340 8a7ddc38 bellard
}
3341 8a7ddc38 bellard
3342 9e472e10 aliguori
void qemu_service_io(void)
3343 9e472e10 aliguori
{
3344 d9f75a4e aliguori
    qemu_notify_event();
3345 9e472e10 aliguori
}
3346 9e472e10 aliguori
3347 8a7ddc38 bellard
/***********************************************************/
3348 83f64091 bellard
/* bottom halves (can be seen as timers which expire ASAP) */
3349 83f64091 bellard
3350 83f64091 bellard
struct QEMUBH {
3351 83f64091 bellard
    QEMUBHFunc *cb;
3352 83f64091 bellard
    void *opaque;
3353 83f64091 bellard
    int scheduled;
3354 1b435b10 aliguori
    int idle;
3355 1b435b10 aliguori
    int deleted;
3356 83f64091 bellard
    QEMUBH *next;
3357 83f64091 bellard
};
3358 83f64091 bellard
3359 83f64091 bellard
static QEMUBH *first_bh = NULL;
3360 83f64091 bellard
3361 83f64091 bellard
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3362 83f64091 bellard
{
3363 83f64091 bellard
    QEMUBH *bh;
3364 83f64091 bellard
    bh = qemu_mallocz(sizeof(QEMUBH));
3365 83f64091 bellard
    bh->cb = cb;
3366 83f64091 bellard
    bh->opaque = opaque;
3367 1b435b10 aliguori
    bh->next = first_bh;
3368 1b435b10 aliguori
    first_bh = bh;
3369 83f64091 bellard
    return bh;
3370 83f64091 bellard
}
3371 83f64091 bellard
3372 6eb5733a bellard
int qemu_bh_poll(void)
3373 83f64091 bellard
{
3374 1b435b10 aliguori
    QEMUBH *bh, **bhp;
3375 6eb5733a bellard
    int ret;
3376 83f64091 bellard
3377 6eb5733a bellard
    ret = 0;
3378 1b435b10 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
3379 1b435b10 aliguori
        if (!bh->deleted && bh->scheduled) {
3380 1b435b10 aliguori
            bh->scheduled = 0;
3381 1b435b10 aliguori
            if (!bh->idle)
3382 1b435b10 aliguori
                ret = 1;
3383 1b435b10 aliguori
            bh->idle = 0;
3384 1b435b10 aliguori
            bh->cb(bh->opaque);
3385 1b435b10 aliguori
        }
3386 83f64091 bellard
    }
3387 1b435b10 aliguori
3388 1b435b10 aliguori
    /* remove deleted bhs */
3389 1b435b10 aliguori
    bhp = &first_bh;
3390 1b435b10 aliguori
    while (*bhp) {
3391 1b435b10 aliguori
        bh = *bhp;
3392 1b435b10 aliguori
        if (bh->deleted) {
3393 1b435b10 aliguori
            *bhp = bh->next;
3394 1b435b10 aliguori
            qemu_free(bh);
3395 1b435b10 aliguori
        } else
3396 1b435b10 aliguori
            bhp = &bh->next;
3397 1b435b10 aliguori
    }
3398 1b435b10 aliguori
3399 6eb5733a bellard
    return ret;
3400 83f64091 bellard
}
3401 83f64091 bellard
3402 1b435b10 aliguori
void qemu_bh_schedule_idle(QEMUBH *bh)
3403 1b435b10 aliguori
{
3404 1b435b10 aliguori
    if (bh->scheduled)
3405 1b435b10 aliguori
        return;
3406 1b435b10 aliguori
    bh->scheduled = 1;
3407 1b435b10 aliguori
    bh->idle = 1;
3408 1b435b10 aliguori
}
3409 1b435b10 aliguori
3410 83f64091 bellard
void qemu_bh_schedule(QEMUBH *bh)
3411 83f64091 bellard
{
3412 83f64091 bellard
    if (bh->scheduled)
3413 83f64091 bellard
        return;
3414 83f64091 bellard
    bh->scheduled = 1;
3415 1b435b10 aliguori
    bh->idle = 0;
3416 83f64091 bellard
    /* stop the currently executing CPU to execute the BH ASAP */
3417 d9f75a4e aliguori
    qemu_notify_event();
3418 83f64091 bellard
}
3419 83f64091 bellard
3420 83f64091 bellard
void qemu_bh_cancel(QEMUBH *bh)
3421 83f64091 bellard
{
3422 1b435b10 aliguori
    bh->scheduled = 0;
3423 83f64091 bellard
}
3424 83f64091 bellard
3425 83f64091 bellard
void qemu_bh_delete(QEMUBH *bh)
3426 83f64091 bellard
{
3427 1b435b10 aliguori
    bh->scheduled = 0;
3428 1b435b10 aliguori
    bh->deleted = 1;
3429 83f64091 bellard
}
3430 83f64091 bellard
3431 56f3a5d0 aliguori
static void qemu_bh_update_timeout(int *timeout)
3432 56f3a5d0 aliguori
{
3433 56f3a5d0 aliguori
    QEMUBH *bh;
3434 56f3a5d0 aliguori
3435 56f3a5d0 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
3436 56f3a5d0 aliguori
        if (!bh->deleted && bh->scheduled) {
3437 56f3a5d0 aliguori
            if (bh->idle) {
3438 56f3a5d0 aliguori
                /* idle bottom halves will be polled at least
3439 56f3a5d0 aliguori
                 * every 10ms */
3440 56f3a5d0 aliguori
                *timeout = MIN(10, *timeout);
3441 56f3a5d0 aliguori
            } else {
3442 56f3a5d0 aliguori
                /* non-idle bottom halves will be executed
3443 56f3a5d0 aliguori
                 * immediately */
3444 56f3a5d0 aliguori
                *timeout = 0;
3445 56f3a5d0 aliguori
                break;
3446 56f3a5d0 aliguori
            }
3447 56f3a5d0 aliguori
        }
3448 56f3a5d0 aliguori
    }
3449 56f3a5d0 aliguori
}
3450 56f3a5d0 aliguori
3451 83f64091 bellard
/***********************************************************/
3452 cc1daa40 bellard
/* machine registration */
3453 cc1daa40 bellard
3454 bdaf78e0 blueswir1
static QEMUMachine *first_machine = NULL;
3455 6f338c34 aliguori
QEMUMachine *current_machine = NULL;
3456 cc1daa40 bellard
3457 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
3458 cc1daa40 bellard
{
3459 cc1daa40 bellard
    QEMUMachine **pm;
3460 cc1daa40 bellard
    pm = &first_machine;
3461 cc1daa40 bellard
    while (*pm != NULL)
3462 cc1daa40 bellard
        pm = &(*pm)->next;
3463 cc1daa40 bellard
    m->next = NULL;
3464 cc1daa40 bellard
    *pm = m;
3465 cc1daa40 bellard
    return 0;
3466 cc1daa40 bellard
}
3467 cc1daa40 bellard
3468 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
3469 cc1daa40 bellard
{
3470 cc1daa40 bellard
    QEMUMachine *m;
3471 cc1daa40 bellard
3472 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
3473 cc1daa40 bellard
        if (!strcmp(m->name, name))
3474 cc1daa40 bellard
            return m;
3475 cc1daa40 bellard
    }
3476 cc1daa40 bellard
    return NULL;
3477 cc1daa40 bellard
}
3478 cc1daa40 bellard
3479 0c257437 Anthony Liguori
static QEMUMachine *find_default_machine(void)
3480 0c257437 Anthony Liguori
{
3481 0c257437 Anthony Liguori
    QEMUMachine *m;
3482 0c257437 Anthony Liguori
3483 0c257437 Anthony Liguori
    for(m = first_machine; m != NULL; m = m->next) {
3484 0c257437 Anthony Liguori
        if (m->is_default) {
3485 0c257437 Anthony Liguori
            return m;
3486 0c257437 Anthony Liguori
        }
3487 0c257437 Anthony Liguori
    }
3488 0c257437 Anthony Liguori
    return NULL;
3489 0c257437 Anthony Liguori
}
3490 0c257437 Anthony Liguori
3491 cc1daa40 bellard
/***********************************************************/
3492 8a7ddc38 bellard
/* main execution loop */
3493 8a7ddc38 bellard
3494 9596ebb7 pbrook
static void gui_update(void *opaque)
3495 8a7ddc38 bellard
{
3496 7d957bd8 aliguori
    uint64_t interval = GUI_REFRESH_INTERVAL;
3497 740733bb ths
    DisplayState *ds = opaque;
3498 7d957bd8 aliguori
    DisplayChangeListener *dcl = ds->listeners;
3499 7d957bd8 aliguori
3500 7d957bd8 aliguori
    dpy_refresh(ds);
3501 7d957bd8 aliguori
3502 7d957bd8 aliguori
    while (dcl != NULL) {
3503 7d957bd8 aliguori
        if (dcl->gui_timer_interval &&
3504 7d957bd8 aliguori
            dcl->gui_timer_interval < interval)
3505 7d957bd8 aliguori
            interval = dcl->gui_timer_interval;
3506 7d957bd8 aliguori
        dcl = dcl->next;
3507 7d957bd8 aliguori
    }
3508 7d957bd8 aliguori
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
3509 8a7ddc38 bellard
}
3510 8a7ddc38 bellard
3511 9043b62d blueswir1
static void nographic_update(void *opaque)
3512 9043b62d blueswir1
{
3513 9043b62d blueswir1
    uint64_t interval = GUI_REFRESH_INTERVAL;
3514 9043b62d blueswir1
3515 9043b62d blueswir1
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3516 9043b62d blueswir1
}
3517 9043b62d blueswir1
3518 0bd48850 bellard
struct vm_change_state_entry {
3519 0bd48850 bellard
    VMChangeStateHandler *cb;
3520 0bd48850 bellard
    void *opaque;
3521 0bd48850 bellard
    LIST_ENTRY (vm_change_state_entry) entries;
3522 0bd48850 bellard
};
3523 0bd48850 bellard
3524 0bd48850 bellard
static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3525 0bd48850 bellard
3526 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3527 0bd48850 bellard
                                                     void *opaque)
3528 0bd48850 bellard
{
3529 0bd48850 bellard
    VMChangeStateEntry *e;
3530 0bd48850 bellard
3531 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
3532 0bd48850 bellard
3533 0bd48850 bellard
    e->cb = cb;
3534 0bd48850 bellard
    e->opaque = opaque;
3535 0bd48850 bellard
    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3536 0bd48850 bellard
    return e;
3537 0bd48850 bellard
}
3538 0bd48850 bellard
3539 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3540 0bd48850 bellard
{
3541 0bd48850 bellard
    LIST_REMOVE (e, entries);
3542 0bd48850 bellard
    qemu_free (e);
3543 0bd48850 bellard
}
3544 0bd48850 bellard
3545 9781e040 aliguori
static void vm_state_notify(int running, int reason)
3546 0bd48850 bellard
{
3547 0bd48850 bellard
    VMChangeStateEntry *e;
3548 0bd48850 bellard
3549 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3550 9781e040 aliguori
        e->cb(e->opaque, running, reason);
3551 0bd48850 bellard
    }
3552 0bd48850 bellard
}
3553 0bd48850 bellard
3554 d6dc3d42 aliguori
static void resume_all_vcpus(void);
3555 d6dc3d42 aliguori
static void pause_all_vcpus(void);
3556 d6dc3d42 aliguori
3557 8a7ddc38 bellard
void vm_start(void)
3558 8a7ddc38 bellard
{
3559 8a7ddc38 bellard
    if (!vm_running) {
3560 8a7ddc38 bellard
        cpu_enable_ticks();
3561 8a7ddc38 bellard
        vm_running = 1;
3562 9781e040 aliguori
        vm_state_notify(1, 0);
3563 efe75411 ths
        qemu_rearm_alarm_timer(alarm_timer);
3564 d6dc3d42 aliguori
        resume_all_vcpus();
3565 8a7ddc38 bellard
    }
3566 8a7ddc38 bellard
}
3567 8a7ddc38 bellard
3568 bb0c6722 bellard
/* reset/shutdown handler */
3569 bb0c6722 bellard
3570 bb0c6722 bellard
typedef struct QEMUResetEntry {
3571 bb0c6722 bellard
    QEMUResetHandler *func;
3572 bb0c6722 bellard
    void *opaque;
3573 8217606e Jan Kiszka
    int order;
3574 bb0c6722 bellard
    struct QEMUResetEntry *next;
3575 bb0c6722 bellard
} QEMUResetEntry;
3576 bb0c6722 bellard
3577 bb0c6722 bellard
static QEMUResetEntry *first_reset_entry;
3578 bb0c6722 bellard
static int reset_requested;
3579 bb0c6722 bellard
static int shutdown_requested;
3580 3475187d bellard
static int powerdown_requested;
3581 e568902a aliguori
static int debug_requested;
3582 6e29f5da aliguori
static int vmstop_requested;
3583 bb0c6722 bellard
3584 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
3585 cf7a2fe2 aurel32
{
3586 cf7a2fe2 aurel32
    int r = shutdown_requested;
3587 cf7a2fe2 aurel32
    shutdown_requested = 0;
3588 cf7a2fe2 aurel32
    return r;
3589 cf7a2fe2 aurel32
}
3590 cf7a2fe2 aurel32
3591 cf7a2fe2 aurel32
int qemu_reset_requested(void)
3592 cf7a2fe2 aurel32
{
3593 cf7a2fe2 aurel32
    int r = reset_requested;
3594 cf7a2fe2 aurel32
    reset_requested = 0;
3595 cf7a2fe2 aurel32
    return r;
3596 cf7a2fe2 aurel32
}
3597 cf7a2fe2 aurel32
3598 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
3599 cf7a2fe2 aurel32
{
3600 cf7a2fe2 aurel32
    int r = powerdown_requested;
3601 cf7a2fe2 aurel32
    powerdown_requested = 0;
3602 cf7a2fe2 aurel32
    return r;
3603 cf7a2fe2 aurel32
}
3604 cf7a2fe2 aurel32
3605 e568902a aliguori
static int qemu_debug_requested(void)
3606 e568902a aliguori
{
3607 e568902a aliguori
    int r = debug_requested;
3608 e568902a aliguori
    debug_requested = 0;
3609 e568902a aliguori
    return r;
3610 e568902a aliguori
}
3611 e568902a aliguori
3612 6e29f5da aliguori
static int qemu_vmstop_requested(void)
3613 6e29f5da aliguori
{
3614 6e29f5da aliguori
    int r = vmstop_requested;
3615 6e29f5da aliguori
    vmstop_requested = 0;
3616 6e29f5da aliguori
    return r;
3617 6e29f5da aliguori
}
3618 6e29f5da aliguori
3619 6e29f5da aliguori
static void do_vm_stop(int reason)
3620 6e29f5da aliguori
{
3621 6e29f5da aliguori
    if (vm_running) {
3622 6e29f5da aliguori
        cpu_disable_ticks();
3623 6e29f5da aliguori
        vm_running = 0;
3624 d6dc3d42 aliguori
        pause_all_vcpus();
3625 6e29f5da aliguori
        vm_state_notify(0, reason);
3626 6e29f5da aliguori
    }
3627 6e29f5da aliguori
}
3628 6e29f5da aliguori
3629 8217606e Jan Kiszka
void qemu_register_reset(QEMUResetHandler *func, int order, void *opaque)
3630 bb0c6722 bellard
{
3631 bb0c6722 bellard
    QEMUResetEntry **pre, *re;
3632 bb0c6722 bellard
3633 bb0c6722 bellard
    pre = &first_reset_entry;
3634 8217606e Jan Kiszka
    while (*pre != NULL && (*pre)->order >= order) {
3635 bb0c6722 bellard
        pre = &(*pre)->next;
3636 8217606e Jan Kiszka
    }
3637 bb0c6722 bellard
    re = qemu_mallocz(sizeof(QEMUResetEntry));
3638 bb0c6722 bellard
    re->func = func;
3639 bb0c6722 bellard
    re->opaque = opaque;
3640 8217606e Jan Kiszka
    re->order = order;
3641 bb0c6722 bellard
    re->next = NULL;
3642 bb0c6722 bellard
    *pre = re;
3643 bb0c6722 bellard
}
3644 bb0c6722 bellard
3645 cf7a2fe2 aurel32
void qemu_system_reset(void)
3646 bb0c6722 bellard
{
3647 bb0c6722 bellard
    QEMUResetEntry *re;
3648 bb0c6722 bellard
3649 bb0c6722 bellard
    /* reset all devices */
3650 bb0c6722 bellard
    for(re = first_reset_entry; re != NULL; re = re->next) {
3651 bb0c6722 bellard
        re->func(re->opaque);
3652 bb0c6722 bellard
    }
3653 bb0c6722 bellard
}
3654 bb0c6722 bellard
3655 bb0c6722 bellard
void qemu_system_reset_request(void)
3656 bb0c6722 bellard
{
3657 d1beab82 bellard
    if (no_reboot) {
3658 d1beab82 bellard
        shutdown_requested = 1;
3659 d1beab82 bellard
    } else {
3660 d1beab82 bellard
        reset_requested = 1;
3661 d1beab82 bellard
    }
3662 d9f75a4e aliguori
    qemu_notify_event();
3663 bb0c6722 bellard
}
3664 bb0c6722 bellard
3665 bb0c6722 bellard
void qemu_system_shutdown_request(void)
3666 bb0c6722 bellard
{
3667 bb0c6722 bellard
    shutdown_requested = 1;
3668 d9f75a4e aliguori
    qemu_notify_event();
3669 bb0c6722 bellard
}
3670 bb0c6722 bellard
3671 3475187d bellard
void qemu_system_powerdown_request(void)
3672 3475187d bellard
{
3673 3475187d bellard
    powerdown_requested = 1;
3674 d9f75a4e aliguori
    qemu_notify_event();
3675 d9f75a4e aliguori
}
3676 d9f75a4e aliguori
3677 d6dc3d42 aliguori
#ifdef CONFIG_IOTHREAD
3678 d6dc3d42 aliguori
static void qemu_system_vmstop_request(int reason)
3679 d9f75a4e aliguori
{
3680 d6dc3d42 aliguori
    vmstop_requested = reason;
3681 d6dc3d42 aliguori
    qemu_notify_event();
3682 bb0c6722 bellard
}
3683 d6dc3d42 aliguori
#endif
3684 bb0c6722 bellard
3685 50317c7f aliguori
#ifndef _WIN32
3686 50317c7f aliguori
static int io_thread_fd = -1;
3687 50317c7f aliguori
3688 50317c7f aliguori
static void qemu_event_increment(void)
3689 3fcf7b6b aliguori
{
3690 50317c7f aliguori
    static const char byte = 0;
3691 50317c7f aliguori
3692 50317c7f aliguori
    if (io_thread_fd == -1)
3693 50317c7f aliguori
        return;
3694 50317c7f aliguori
3695 50317c7f aliguori
    write(io_thread_fd, &byte, sizeof(byte));
3696 50317c7f aliguori
}
3697 50317c7f aliguori
3698 50317c7f aliguori
static void qemu_event_read(void *opaque)
3699 50317c7f aliguori
{
3700 50317c7f aliguori
    int fd = (unsigned long)opaque;
3701 50317c7f aliguori
    ssize_t len;
3702 50317c7f aliguori
3703 50317c7f aliguori
    /* Drain the notify pipe */
3704 50317c7f aliguori
    do {
3705 50317c7f aliguori
        char buffer[512];
3706 50317c7f aliguori
        len = read(fd, buffer, sizeof(buffer));
3707 50317c7f aliguori
    } while ((len == -1 && errno == EINTR) || len > 0);
3708 50317c7f aliguori
}
3709 50317c7f aliguori
3710 50317c7f aliguori
static int qemu_event_init(void)
3711 50317c7f aliguori
{
3712 50317c7f aliguori
    int err;
3713 50317c7f aliguori
    int fds[2];
3714 50317c7f aliguori
3715 50317c7f aliguori
    err = pipe(fds);
3716 50317c7f aliguori
    if (err == -1)
3717 50317c7f aliguori
        return -errno;
3718 50317c7f aliguori
3719 50317c7f aliguori
    err = fcntl_setfl(fds[0], O_NONBLOCK);
3720 50317c7f aliguori
    if (err < 0)
3721 50317c7f aliguori
        goto fail;
3722 50317c7f aliguori
3723 50317c7f aliguori
    err = fcntl_setfl(fds[1], O_NONBLOCK);
3724 50317c7f aliguori
    if (err < 0)
3725 50317c7f aliguori
        goto fail;
3726 50317c7f aliguori
3727 50317c7f aliguori
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3728 50317c7f aliguori
                         (void *)(unsigned long)fds[0]);
3729 50317c7f aliguori
3730 50317c7f aliguori
    io_thread_fd = fds[1];
3731 a7e21219 Jan Kiszka
    return 0;
3732 a7e21219 Jan Kiszka
3733 50317c7f aliguori
fail:
3734 50317c7f aliguori
    close(fds[0]);
3735 50317c7f aliguori
    close(fds[1]);
3736 50317c7f aliguori
    return err;
3737 50317c7f aliguori
}
3738 50317c7f aliguori
#else
3739 50317c7f aliguori
HANDLE qemu_event_handle;
3740 50317c7f aliguori
3741 50317c7f aliguori
static void dummy_event_handler(void *opaque)
3742 50317c7f aliguori
{
3743 50317c7f aliguori
}
3744 50317c7f aliguori
3745 50317c7f aliguori
static int qemu_event_init(void)
3746 50317c7f aliguori
{
3747 50317c7f aliguori
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3748 50317c7f aliguori
    if (!qemu_event_handle) {
3749 50317c7f aliguori
        perror("Failed CreateEvent");
3750 50317c7f aliguori
        return -1;
3751 50317c7f aliguori
    }
3752 50317c7f aliguori
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3753 3fcf7b6b aliguori
    return 0;
3754 3fcf7b6b aliguori
}
3755 3fcf7b6b aliguori
3756 50317c7f aliguori
static void qemu_event_increment(void)
3757 50317c7f aliguori
{
3758 50317c7f aliguori
    SetEvent(qemu_event_handle);
3759 50317c7f aliguori
}
3760 50317c7f aliguori
#endif
3761 50317c7f aliguori
3762 d6dc3d42 aliguori
static int cpu_can_run(CPUState *env)
3763 d6dc3d42 aliguori
{
3764 d6dc3d42 aliguori
    if (env->stop)
3765 d6dc3d42 aliguori
        return 0;
3766 d6dc3d42 aliguori
    if (env->stopped)
3767 d6dc3d42 aliguori
        return 0;
3768 d6dc3d42 aliguori
    return 1;
3769 d6dc3d42 aliguori
}
3770 d6dc3d42 aliguori
3771 d6dc3d42 aliguori
#ifndef CONFIG_IOTHREAD
3772 50317c7f aliguori
static int qemu_init_main_loop(void)
3773 50317c7f aliguori
{
3774 50317c7f aliguori
    return qemu_event_init();
3775 50317c7f aliguori
}
3776 50317c7f aliguori
3777 0bf46a40 aliguori
void qemu_init_vcpu(void *_env)
3778 0bf46a40 aliguori
{
3779 0bf46a40 aliguori
    CPUState *env = _env;
3780 0bf46a40 aliguori
3781 0bf46a40 aliguori
    if (kvm_enabled())
3782 0bf46a40 aliguori
        kvm_init_vcpu(env);
3783 0bf46a40 aliguori
    return;
3784 0bf46a40 aliguori
}
3785 0bf46a40 aliguori
3786 8edac960 aliguori
int qemu_cpu_self(void *env)
3787 8edac960 aliguori
{
3788 8edac960 aliguori
    return 1;
3789 8edac960 aliguori
}
3790 8edac960 aliguori
3791 d6dc3d42 aliguori
static void resume_all_vcpus(void)
3792 d6dc3d42 aliguori
{
3793 d6dc3d42 aliguori
}
3794 d6dc3d42 aliguori
3795 d6dc3d42 aliguori
static void pause_all_vcpus(void)
3796 d6dc3d42 aliguori
{
3797 d6dc3d42 aliguori
}
3798 d6dc3d42 aliguori
3799 8edac960 aliguori
void qemu_cpu_kick(void *env)
3800 8edac960 aliguori
{
3801 8edac960 aliguori
    return;
3802 8edac960 aliguori
}
3803 8edac960 aliguori
3804 d6dc3d42 aliguori
void qemu_notify_event(void)
3805 d6dc3d42 aliguori
{
3806 d6dc3d42 aliguori
    CPUState *env = cpu_single_env;
3807 d6dc3d42 aliguori
3808 d6dc3d42 aliguori
    if (env) {
3809 d6dc3d42 aliguori
        cpu_exit(env);
3810 d6dc3d42 aliguori
#ifdef USE_KQEMU
3811 d6dc3d42 aliguori
        if (env->kqemu_enabled)
3812 d6dc3d42 aliguori
            kqemu_cpu_interrupt(env);
3813 d6dc3d42 aliguori
#endif
3814 d6dc3d42 aliguori
     }
3815 d6dc3d42 aliguori
}
3816 d6dc3d42 aliguori
3817 4870852c aliguori
#define qemu_mutex_lock_iothread() do { } while (0)
3818 4870852c aliguori
#define qemu_mutex_unlock_iothread() do { } while (0)
3819 4870852c aliguori
3820 6e29f5da aliguori
void vm_stop(int reason)
3821 6e29f5da aliguori
{
3822 6e29f5da aliguori
    do_vm_stop(reason);
3823 6e29f5da aliguori
}
3824 6e29f5da aliguori
3825 d6dc3d42 aliguori
#else /* CONFIG_IOTHREAD */
3826 d6dc3d42 aliguori
3827 d6dc3d42 aliguori
#include "qemu-thread.h"
3828 d6dc3d42 aliguori
3829 d6dc3d42 aliguori
QemuMutex qemu_global_mutex;
3830 d6dc3d42 aliguori
static QemuMutex qemu_fair_mutex;
3831 d6dc3d42 aliguori
3832 d6dc3d42 aliguori
static QemuThread io_thread;
3833 d6dc3d42 aliguori
3834 d6dc3d42 aliguori
static QemuThread *tcg_cpu_thread;
3835 d6dc3d42 aliguori
static QemuCond *tcg_halt_cond;
3836 d6dc3d42 aliguori
3837 d6dc3d42 aliguori
static int qemu_system_ready;
3838 d6dc3d42 aliguori
/* cpu creation */
3839 d6dc3d42 aliguori
static QemuCond qemu_cpu_cond;
3840 d6dc3d42 aliguori
/* system init */
3841 d6dc3d42 aliguori
static QemuCond qemu_system_cond;
3842 d6dc3d42 aliguori
static QemuCond qemu_pause_cond;
3843 d6dc3d42 aliguori
3844 d6dc3d42 aliguori
static void block_io_signals(void);
3845 d6dc3d42 aliguori
static void unblock_io_signals(void);
3846 d6dc3d42 aliguori
static int tcg_has_work(void);
3847 d6dc3d42 aliguori
3848 d6dc3d42 aliguori
static int qemu_init_main_loop(void)
3849 d6dc3d42 aliguori
{
3850 d6dc3d42 aliguori
    int ret;
3851 d6dc3d42 aliguori
3852 d6dc3d42 aliguori
    ret = qemu_event_init();
3853 d6dc3d42 aliguori
    if (ret)
3854 d6dc3d42 aliguori
        return ret;
3855 d6dc3d42 aliguori
3856 d6dc3d42 aliguori
    qemu_cond_init(&qemu_pause_cond);
3857 d6dc3d42 aliguori
    qemu_mutex_init(&qemu_fair_mutex);
3858 d6dc3d42 aliguori
    qemu_mutex_init(&qemu_global_mutex);
3859 d6dc3d42 aliguori
    qemu_mutex_lock(&qemu_global_mutex);
3860 d6dc3d42 aliguori
3861 d6dc3d42 aliguori
    unblock_io_signals();
3862 d6dc3d42 aliguori
    qemu_thread_self(&io_thread);
3863 d6dc3d42 aliguori
3864 d6dc3d42 aliguori
    return 0;
3865 d6dc3d42 aliguori
}
3866 d6dc3d42 aliguori
3867 d6dc3d42 aliguori
static void qemu_wait_io_event(CPUState *env)
3868 d6dc3d42 aliguori
{
3869 d6dc3d42 aliguori
    while (!tcg_has_work())
3870 d6dc3d42 aliguori
        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3871 d6dc3d42 aliguori
3872 d6dc3d42 aliguori
    qemu_mutex_unlock(&qemu_global_mutex);
3873 d6dc3d42 aliguori
3874 d6dc3d42 aliguori
    /*
3875 d6dc3d42 aliguori
     * Users of qemu_global_mutex can be starved, having no chance
3876 d6dc3d42 aliguori
     * to acquire it since this path will get to it first.
3877 d6dc3d42 aliguori
     * So use another lock to provide fairness.
3878 d6dc3d42 aliguori
     */
3879 d6dc3d42 aliguori
    qemu_mutex_lock(&qemu_fair_mutex);
3880 d6dc3d42 aliguori
    qemu_mutex_unlock(&qemu_fair_mutex);
3881 d6dc3d42 aliguori
3882 d6dc3d42 aliguori
    qemu_mutex_lock(&qemu_global_mutex);
3883 d6dc3d42 aliguori
    if (env->stop) {
3884 d6dc3d42 aliguori
        env->stop = 0;
3885 d6dc3d42 aliguori
        env->stopped = 1;
3886 d6dc3d42 aliguori
        qemu_cond_signal(&qemu_pause_cond);
3887 d6dc3d42 aliguori
    }
3888 d6dc3d42 aliguori
}
3889 d6dc3d42 aliguori
3890 d6dc3d42 aliguori
static int qemu_cpu_exec(CPUState *env);
3891 d6dc3d42 aliguori
3892 d6dc3d42 aliguori
static void *kvm_cpu_thread_fn(void *arg)
3893 d6dc3d42 aliguori
{
3894 d6dc3d42 aliguori
    CPUState *env = arg;
3895 d6dc3d42 aliguori
3896 d6dc3d42 aliguori
    block_io_signals();
3897 d6dc3d42 aliguori
    qemu_thread_self(env->thread);
3898 d6dc3d42 aliguori
3899 d6dc3d42 aliguori
    /* signal CPU creation */
3900 d6dc3d42 aliguori
    qemu_mutex_lock(&qemu_global_mutex);
3901 d6dc3d42 aliguori
    env->created = 1;
3902 d6dc3d42 aliguori
    qemu_cond_signal(&qemu_cpu_cond);
3903 d6dc3d42 aliguori
3904 d6dc3d42 aliguori
    /* and wait for machine initialization */
3905 d6dc3d42 aliguori
    while (!qemu_system_ready)
3906 d6dc3d42 aliguori
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3907 d6dc3d42 aliguori
3908 d6dc3d42 aliguori
    while (1) {
3909 d6dc3d42 aliguori
        if (cpu_can_run(env))
3910 d6dc3d42 aliguori
            qemu_cpu_exec(env);
3911 d6dc3d42 aliguori
        qemu_wait_io_event(env);
3912 d6dc3d42 aliguori
    }
3913 d6dc3d42 aliguori
3914 d6dc3d42 aliguori
    return NULL;
3915 d6dc3d42 aliguori
}
3916 d6dc3d42 aliguori
3917 d6dc3d42 aliguori
static void tcg_cpu_exec(void);
3918 d6dc3d42 aliguori
3919 d6dc3d42 aliguori
static void *tcg_cpu_thread_fn(void *arg)
3920 d6dc3d42 aliguori
{
3921 d6dc3d42 aliguori
    CPUState *env = arg;
3922 d6dc3d42 aliguori
3923 d6dc3d42 aliguori
    block_io_signals();
3924 d6dc3d42 aliguori
    qemu_thread_self(env->thread);
3925 d6dc3d42 aliguori
3926 d6dc3d42 aliguori
    /* signal CPU creation */
3927 d6dc3d42 aliguori
    qemu_mutex_lock(&qemu_global_mutex);
3928 d6dc3d42 aliguori
    for (env = first_cpu; env != NULL; env = env->next_cpu)
3929 d6dc3d42 aliguori
        env->created = 1;
3930 d6dc3d42 aliguori
    qemu_cond_signal(&qemu_cpu_cond);
3931 d6dc3d42 aliguori
3932 d6dc3d42 aliguori
    /* and wait for machine initialization */
3933 d6dc3d42 aliguori
    while (!qemu_system_ready)
3934 d6dc3d42 aliguori
        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3935 d6dc3d42 aliguori
3936 d6dc3d42 aliguori
    while (1) {
3937 d6dc3d42 aliguori
        tcg_cpu_exec();
3938 d6dc3d42 aliguori
        qemu_wait_io_event(cur_cpu);
3939 d6dc3d42 aliguori
    }
3940 d6dc3d42 aliguori
3941 d6dc3d42 aliguori
    return NULL;
3942 d6dc3d42 aliguori
}
3943 d6dc3d42 aliguori
3944 d6dc3d42 aliguori
void qemu_cpu_kick(void *_env)
3945 d6dc3d42 aliguori
{
3946 d6dc3d42 aliguori
    CPUState *env = _env;
3947 d6dc3d42 aliguori
    qemu_cond_broadcast(env->halt_cond);
3948 d6dc3d42 aliguori
    if (kvm_enabled())
3949 d6dc3d42 aliguori
        qemu_thread_signal(env->thread, SIGUSR1);
3950 d6dc3d42 aliguori
}
3951 d6dc3d42 aliguori
3952 d6dc3d42 aliguori
int qemu_cpu_self(void *env)
3953 d6dc3d42 aliguori
{
3954 d6dc3d42 aliguori
    return (cpu_single_env != NULL);
3955 d6dc3d42 aliguori
}
3956 d6dc3d42 aliguori
3957 d6dc3d42 aliguori
static void cpu_signal(int sig)
3958 d6dc3d42 aliguori
{
3959 d6dc3d42 aliguori
    if (cpu_single_env)
3960 d6dc3d42 aliguori
        cpu_exit(cpu_single_env);
3961 d6dc3d42 aliguori
}
3962 d6dc3d42 aliguori
3963 d6dc3d42 aliguori
static void block_io_signals(void)
3964 d6dc3d42 aliguori
{
3965 d6dc3d42 aliguori
    sigset_t set;
3966 d6dc3d42 aliguori
    struct sigaction sigact;
3967 d6dc3d42 aliguori
3968 d6dc3d42 aliguori
    sigemptyset(&set);
3969 d6dc3d42 aliguori
    sigaddset(&set, SIGUSR2);
3970 d6dc3d42 aliguori
    sigaddset(&set, SIGIO);
3971 d6dc3d42 aliguori
    sigaddset(&set, SIGALRM);
3972 d6dc3d42 aliguori
    pthread_sigmask(SIG_BLOCK, &set, NULL);
3973 d6dc3d42 aliguori
3974 d6dc3d42 aliguori
    sigemptyset(&set);
3975 d6dc3d42 aliguori
    sigaddset(&set, SIGUSR1);
3976 d6dc3d42 aliguori
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3977 d6dc3d42 aliguori
3978 d6dc3d42 aliguori
    memset(&sigact, 0, sizeof(sigact));
3979 d6dc3d42 aliguori
    sigact.sa_handler = cpu_signal;
3980 d6dc3d42 aliguori
    sigaction(SIGUSR1, &sigact, NULL);
3981 d6dc3d42 aliguori
}
3982 d6dc3d42 aliguori
3983 d6dc3d42 aliguori
static void unblock_io_signals(void)
3984 d6dc3d42 aliguori
{
3985 d6dc3d42 aliguori
    sigset_t set;
3986 d6dc3d42 aliguori
3987 d6dc3d42 aliguori
    sigemptyset(&set);
3988 d6dc3d42 aliguori
    sigaddset(&set, SIGUSR2);
3989 d6dc3d42 aliguori
    sigaddset(&set, SIGIO);
3990 d6dc3d42 aliguori
    sigaddset(&set, SIGALRM);
3991 d6dc3d42 aliguori
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3992 d6dc3d42 aliguori
3993 d6dc3d42 aliguori
    sigemptyset(&set);
3994 d6dc3d42 aliguori
    sigaddset(&set, SIGUSR1);
3995 d6dc3d42 aliguori
    pthread_sigmask(SIG_BLOCK, &set, NULL);
3996 d6dc3d42 aliguori
}
3997 d6dc3d42 aliguori
3998 d6dc3d42 aliguori
static void qemu_signal_lock(unsigned int msecs)
3999 d6dc3d42 aliguori
{
4000 d6dc3d42 aliguori
    qemu_mutex_lock(&qemu_fair_mutex);
4001 d6dc3d42 aliguori
4002 d6dc3d42 aliguori
    while (qemu_mutex_trylock(&qemu_global_mutex)) {
4003 d6dc3d42 aliguori
        qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
4004 d6dc3d42 aliguori
        if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
4005 d6dc3d42 aliguori
            break;
4006 d6dc3d42 aliguori
    }
4007 d6dc3d42 aliguori
    qemu_mutex_unlock(&qemu_fair_mutex);
4008 d6dc3d42 aliguori
}
4009 d6dc3d42 aliguori
4010 d6dc3d42 aliguori
static void qemu_mutex_lock_iothread(void)
4011 d6dc3d42 aliguori
{
4012 d6dc3d42 aliguori
    if (kvm_enabled()) {
4013 d6dc3d42 aliguori
        qemu_mutex_lock(&qemu_fair_mutex);
4014 d6dc3d42 aliguori
        qemu_mutex_lock(&qemu_global_mutex);
4015 d6dc3d42 aliguori
        qemu_mutex_unlock(&qemu_fair_mutex);
4016 d6dc3d42 aliguori
    } else
4017 d6dc3d42 aliguori
        qemu_signal_lock(100);
4018 d6dc3d42 aliguori
}
4019 d6dc3d42 aliguori
4020 d6dc3d42 aliguori
static void qemu_mutex_unlock_iothread(void)
4021 d6dc3d42 aliguori
{
4022 d6dc3d42 aliguori
    qemu_mutex_unlock(&qemu_global_mutex);
4023 d6dc3d42 aliguori
}
4024 d6dc3d42 aliguori
4025 d6dc3d42 aliguori
static int all_vcpus_paused(void)
4026 d6dc3d42 aliguori
{
4027 d6dc3d42 aliguori
    CPUState *penv = first_cpu;
4028 d6dc3d42 aliguori
4029 d6dc3d42 aliguori
    while (penv) {
4030 d6dc3d42 aliguori
        if (!penv->stopped)
4031 d6dc3d42 aliguori
            return 0;
4032 d6dc3d42 aliguori
        penv = (CPUState *)penv->next_cpu;
4033 d6dc3d42 aliguori
    }
4034 d6dc3d42 aliguori
4035 d6dc3d42 aliguori
    return 1;
4036 d6dc3d42 aliguori
}
4037 d6dc3d42 aliguori
4038 d6dc3d42 aliguori
static void pause_all_vcpus(void)
4039 d6dc3d42 aliguori
{
4040 d6dc3d42 aliguori
    CPUState *penv = first_cpu;
4041 d6dc3d42 aliguori
4042 d6dc3d42 aliguori
    while (penv) {
4043 d6dc3d42 aliguori
        penv->stop = 1;
4044 d6dc3d42 aliguori
        qemu_thread_signal(penv->thread, SIGUSR1);
4045 d6dc3d42 aliguori
        qemu_cpu_kick(penv);
4046 d6dc3d42 aliguori
        penv = (CPUState *)penv->next_cpu;
4047 d6dc3d42 aliguori
    }
4048 d6dc3d42 aliguori
4049 d6dc3d42 aliguori
    while (!all_vcpus_paused()) {
4050 d6dc3d42 aliguori
        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
4051 d6dc3d42 aliguori
        penv = first_cpu;
4052 d6dc3d42 aliguori
        while (penv) {
4053 d6dc3d42 aliguori
            qemu_thread_signal(penv->thread, SIGUSR1);
4054 d6dc3d42 aliguori
            penv = (CPUState *)penv->next_cpu;
4055 d6dc3d42 aliguori
        }
4056 d6dc3d42 aliguori
    }
4057 d6dc3d42 aliguori
}
4058 d6dc3d42 aliguori
4059 d6dc3d42 aliguori
static void resume_all_vcpus(void)
4060 d6dc3d42 aliguori
{
4061 d6dc3d42 aliguori
    CPUState *penv = first_cpu;
4062 d6dc3d42 aliguori
4063 d6dc3d42 aliguori
    while (penv) {
4064 d6dc3d42 aliguori
        penv->stop = 0;
4065 d6dc3d42 aliguori
        penv->stopped = 0;
4066 d6dc3d42 aliguori
        qemu_thread_signal(penv->thread, SIGUSR1);
4067 d6dc3d42 aliguori
        qemu_cpu_kick(penv);
4068 d6dc3d42 aliguori
        penv = (CPUState *)penv->next_cpu;
4069 d6dc3d42 aliguori
    }
4070 d6dc3d42 aliguori
}
4071 d6dc3d42 aliguori
4072 d6dc3d42 aliguori
static void tcg_init_vcpu(void *_env)
4073 d6dc3d42 aliguori
{
4074 d6dc3d42 aliguori
    CPUState *env = _env;
4075 d6dc3d42 aliguori
    /* share a single thread for all cpus with TCG */
4076 d6dc3d42 aliguori
    if (!tcg_cpu_thread) {
4077 d6dc3d42 aliguori
        env->thread = qemu_mallocz(sizeof(QemuThread));
4078 d6dc3d42 aliguori
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
4079 d6dc3d42 aliguori
        qemu_cond_init(env->halt_cond);
4080 d6dc3d42 aliguori
        qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
4081 d6dc3d42 aliguori
        while (env->created == 0)
4082 d6dc3d42 aliguori
            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
4083 d6dc3d42 aliguori
        tcg_cpu_thread = env->thread;
4084 d6dc3d42 aliguori
        tcg_halt_cond = env->halt_cond;
4085 d6dc3d42 aliguori
    } else {
4086 d6dc3d42 aliguori
        env->thread = tcg_cpu_thread;
4087 d6dc3d42 aliguori
        env->halt_cond = tcg_halt_cond;
4088 d6dc3d42 aliguori
    }
4089 d6dc3d42 aliguori
}
4090 d6dc3d42 aliguori
4091 d6dc3d42 aliguori
static void kvm_start_vcpu(CPUState *env)
4092 d6dc3d42 aliguori
{
4093 d6dc3d42 aliguori
    kvm_init_vcpu(env);
4094 d6dc3d42 aliguori
    env->thread = qemu_mallocz(sizeof(QemuThread));
4095 d6dc3d42 aliguori
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
4096 d6dc3d42 aliguori
    qemu_cond_init(env->halt_cond);
4097 d6dc3d42 aliguori
    qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
4098 d6dc3d42 aliguori
    while (env->created == 0)
4099 d6dc3d42 aliguori
        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
4100 d6dc3d42 aliguori
}
4101 d6dc3d42 aliguori
4102 d6dc3d42 aliguori
void qemu_init_vcpu(void *_env)
4103 d6dc3d42 aliguori
{
4104 d6dc3d42 aliguori
    CPUState *env = _env;
4105 d6dc3d42 aliguori
4106 d6dc3d42 aliguori
    if (kvm_enabled())
4107 d6dc3d42 aliguori
        kvm_start_vcpu(env);
4108 d6dc3d42 aliguori
    else
4109 d6dc3d42 aliguori
        tcg_init_vcpu(env);
4110 d6dc3d42 aliguori
}
4111 d6dc3d42 aliguori
4112 d6dc3d42 aliguori
void qemu_notify_event(void)
4113 d6dc3d42 aliguori
{
4114 d6dc3d42 aliguori
    qemu_event_increment();
4115 d6dc3d42 aliguori
}
4116 d6dc3d42 aliguori
4117 d6dc3d42 aliguori
void vm_stop(int reason)
4118 d6dc3d42 aliguori
{
4119 d6dc3d42 aliguori
    QemuThread me;
4120 d6dc3d42 aliguori
    qemu_thread_self(&me);
4121 d6dc3d42 aliguori
4122 d6dc3d42 aliguori
    if (!qemu_thread_equal(&me, &io_thread)) {
4123 d6dc3d42 aliguori
        qemu_system_vmstop_request(reason);
4124 d6dc3d42 aliguori
        /*
4125 d6dc3d42 aliguori
         * FIXME: should not return to device code in case
4126 d6dc3d42 aliguori
         * vm_stop() has been requested.
4127 d6dc3d42 aliguori
         */
4128 d6dc3d42 aliguori
        if (cpu_single_env) {
4129 d6dc3d42 aliguori
            cpu_exit(cpu_single_env);
4130 d6dc3d42 aliguori
            cpu_single_env->stop = 1;
4131 d6dc3d42 aliguori
        }
4132 d6dc3d42 aliguori
        return;
4133 d6dc3d42 aliguori
    }
4134 d6dc3d42 aliguori
    do_vm_stop(reason);
4135 d6dc3d42 aliguori
}
4136 d6dc3d42 aliguori
4137 d6dc3d42 aliguori
#endif
4138 d6dc3d42 aliguori
4139 d6dc3d42 aliguori
4140 877cf882 ths
#ifdef _WIN32
4141 69d6451c blueswir1
static void host_main_loop_wait(int *timeout)
4142 56f3a5d0 aliguori
{
4143 56f3a5d0 aliguori
    int ret, ret2, i;
4144 f331110f bellard
    PollingEntry *pe;
4145 f331110f bellard
4146 c4b1fcc0 bellard
4147 f331110f bellard
    /* XXX: need to suppress polling by better using win32 events */
4148 f331110f bellard
    ret = 0;
4149 f331110f bellard
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4150 f331110f bellard
        ret |= pe->func(pe->opaque);
4151 f331110f bellard
    }
4152 e6b1e558 ths
    if (ret == 0) {
4153 a18e524a bellard
        int err;
4154 a18e524a bellard
        WaitObjects *w = &wait_objects;
4155 3b46e624 ths
4156 56f3a5d0 aliguori
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
4157 a18e524a bellard
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4158 a18e524a bellard
            if (w->func[ret - WAIT_OBJECT_0])
4159 a18e524a bellard
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
4160 3b46e624 ths
4161 5fafdf24 ths
            /* Check for additional signaled events */
4162 e6b1e558 ths
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
4163 3b46e624 ths
4164 e6b1e558 ths
                /* Check if event is signaled */
4165 e6b1e558 ths
                ret2 = WaitForSingleObject(w->events[i], 0);
4166 e6b1e558 ths
                if(ret2 == WAIT_OBJECT_0) {
4167 e6b1e558 ths
                    if (w->func[i])
4168 e6b1e558 ths
                        w->func[i](w->opaque[i]);
4169 e6b1e558 ths
                } else if (ret2 == WAIT_TIMEOUT) {
4170 e6b1e558 ths
                } else {
4171 e6b1e558 ths
                    err = GetLastError();
4172 e6b1e558 ths
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
4173 3b46e624 ths
                }
4174 3b46e624 ths
            }
4175 a18e524a bellard
        } else if (ret == WAIT_TIMEOUT) {
4176 a18e524a bellard
        } else {
4177 a18e524a bellard
            err = GetLastError();
4178 e6b1e558 ths
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
4179 a18e524a bellard
        }
4180 f331110f bellard
    }
4181 56f3a5d0 aliguori
4182 56f3a5d0 aliguori
    *timeout = 0;
4183 56f3a5d0 aliguori
}
4184 56f3a5d0 aliguori
#else
4185 69d6451c blueswir1
static void host_main_loop_wait(int *timeout)
4186 56f3a5d0 aliguori
{
4187 56f3a5d0 aliguori
}
4188 fd1dff4b bellard
#endif
4189 56f3a5d0 aliguori
4190 56f3a5d0 aliguori
void main_loop_wait(int timeout)
4191 56f3a5d0 aliguori
{
4192 56f3a5d0 aliguori
    IOHandlerRecord *ioh;
4193 56f3a5d0 aliguori
    fd_set rfds, wfds, xfds;
4194 56f3a5d0 aliguori
    int ret, nfds;
4195 56f3a5d0 aliguori
    struct timeval tv;
4196 56f3a5d0 aliguori
4197 56f3a5d0 aliguori
    qemu_bh_update_timeout(&timeout);
4198 56f3a5d0 aliguori
4199 56f3a5d0 aliguori
    host_main_loop_wait(&timeout);
4200 56f3a5d0 aliguori
4201 fd1dff4b bellard
    /* poll any events */
4202 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
4203 6abfbd79 aliguori
    nfds = -1;
4204 fd1dff4b bellard
    FD_ZERO(&rfds);
4205 fd1dff4b bellard
    FD_ZERO(&wfds);
4206 e035649e bellard
    FD_ZERO(&xfds);
4207 fd1dff4b bellard
    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4208 cafffd40 ths
        if (ioh->deleted)
4209 cafffd40 ths
            continue;
4210 fd1dff4b bellard
        if (ioh->fd_read &&
4211 fd1dff4b bellard
            (!ioh->fd_read_poll ||
4212 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
4213 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
4214 fd1dff4b bellard
            if (ioh->fd > nfds)
4215 fd1dff4b bellard
                nfds = ioh->fd;
4216 fd1dff4b bellard
        }
4217 fd1dff4b bellard
        if (ioh->fd_write) {
4218 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
4219 fd1dff4b bellard
            if (ioh->fd > nfds)
4220 fd1dff4b bellard
                nfds = ioh->fd;
4221 fd1dff4b bellard
        }
4222 fd1dff4b bellard
    }
4223 3b46e624 ths
4224 56f3a5d0 aliguori
    tv.tv_sec = timeout / 1000;
4225 56f3a5d0 aliguori
    tv.tv_usec = (timeout % 1000) * 1000;
4226 56f3a5d0 aliguori
4227 e035649e bellard
#if defined(CONFIG_SLIRP)
4228 63a01ef8 aliguori
    if (slirp_is_inited()) {
4229 e035649e bellard
        slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4230 e035649e bellard
    }
4231 e035649e bellard
#endif
4232 4870852c aliguori
    qemu_mutex_unlock_iothread();
4233 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4234 4870852c aliguori
    qemu_mutex_lock_iothread();
4235 fd1dff4b bellard
    if (ret > 0) {
4236 cafffd40 ths
        IOHandlerRecord **pioh;
4237 cafffd40 ths
4238 cafffd40 ths
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4239 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
4240 fd1dff4b bellard
                ioh->fd_read(ioh->opaque);
4241 7c9d8e07 bellard
            }
4242 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
4243 fd1dff4b bellard
                ioh->fd_write(ioh->opaque);
4244 c4b1fcc0 bellard
            }
4245 b4608c04 bellard
        }
4246 cafffd40 ths
4247 cafffd40 ths
        /* remove deleted IO handlers */
4248 cafffd40 ths
        pioh = &first_io_handler;
4249 cafffd40 ths
        while (*pioh) {
4250 cafffd40 ths
            ioh = *pioh;
4251 cafffd40 ths
            if (ioh->deleted) {
4252 cafffd40 ths
                *pioh = ioh->next;
4253 cafffd40 ths
                qemu_free(ioh);
4254 5fafdf24 ths
            } else
4255 cafffd40 ths
                pioh = &ioh->next;
4256 cafffd40 ths
        }
4257 fd1dff4b bellard
    }
4258 c20709aa bellard
#if defined(CONFIG_SLIRP)
4259 63a01ef8 aliguori
    if (slirp_is_inited()) {
4260 e035649e bellard
        if (ret < 0) {
4261 e035649e bellard
            FD_ZERO(&rfds);
4262 e035649e bellard
            FD_ZERO(&wfds);
4263 e035649e bellard
            FD_ZERO(&xfds);
4264 c20709aa bellard
        }
4265 e035649e bellard
        slirp_select_poll(&rfds, &wfds, &xfds);
4266 fd1dff4b bellard
    }
4267 c20709aa bellard
#endif
4268 c20709aa bellard
4269 50317c7f aliguori
    /* rearm timer, if not periodic */
4270 50317c7f aliguori
    if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4271 50317c7f aliguori
        alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4272 50317c7f aliguori
        qemu_rearm_alarm_timer(alarm_timer);
4273 50317c7f aliguori
    }
4274 50317c7f aliguori
4275 357c692c aliguori
    /* vm time timers */
4276 d6dc3d42 aliguori
    if (vm_running) {
4277 d6dc3d42 aliguori
        if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4278 d6dc3d42 aliguori
            qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4279 d6dc3d42 aliguori
                qemu_get_clock(vm_clock));
4280 d6dc3d42 aliguori
    }
4281 357c692c aliguori
4282 357c692c aliguori
    /* real time timers */
4283 357c692c aliguori
    qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4284 357c692c aliguori
                    qemu_get_clock(rt_clock));
4285 357c692c aliguori
4286 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
4287 423f0742 pbrook
       them.  */
4288 423f0742 pbrook
    qemu_bh_poll();
4289 3b46e624 ths
4290 5905b2e5 bellard
}
4291 5905b2e5 bellard
4292 43b96858 aliguori
static int qemu_cpu_exec(CPUState *env)
4293 5905b2e5 bellard
{
4294 43b96858 aliguori
    int ret;
4295 89bfc105 bellard
#ifdef CONFIG_PROFILER
4296 89bfc105 bellard
    int64_t ti;
4297 89bfc105 bellard
#endif
4298 5905b2e5 bellard
4299 89bfc105 bellard
#ifdef CONFIG_PROFILER
4300 43b96858 aliguori
    ti = profile_getclock();
4301 89bfc105 bellard
#endif
4302 43b96858 aliguori
    if (use_icount) {
4303 43b96858 aliguori
        int64_t count;
4304 43b96858 aliguori
        int decr;
4305 43b96858 aliguori
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4306 43b96858 aliguori
        env->icount_decr.u16.low = 0;
4307 43b96858 aliguori
        env->icount_extra = 0;
4308 43b96858 aliguori
        count = qemu_next_deadline();
4309 43b96858 aliguori
        count = (count + (1 << icount_time_shift) - 1)
4310 43b96858 aliguori
                >> icount_time_shift;
4311 43b96858 aliguori
        qemu_icount += count;
4312 43b96858 aliguori
        decr = (count > 0xffff) ? 0xffff : count;
4313 43b96858 aliguori
        count -= decr;
4314 43b96858 aliguori
        env->icount_decr.u16.low = decr;
4315 43b96858 aliguori
        env->icount_extra = count;
4316 43b96858 aliguori
    }
4317 43b96858 aliguori
    ret = cpu_exec(env);
4318 89bfc105 bellard
#ifdef CONFIG_PROFILER
4319 43b96858 aliguori
    qemu_time += profile_getclock() - ti;
4320 89bfc105 bellard
#endif
4321 43b96858 aliguori
    if (use_icount) {
4322 43b96858 aliguori
        /* Fold pending instructions back into the
4323 43b96858 aliguori
           instruction counter, and clear the interrupt flag.  */
4324 43b96858 aliguori
        qemu_icount -= (env->icount_decr.u16.low
4325 43b96858 aliguori
                        + env->icount_extra);
4326 43b96858 aliguori
        env->icount_decr.u32 = 0;
4327 43b96858 aliguori
        env->icount_extra = 0;
4328 43b96858 aliguori
    }
4329 43b96858 aliguori
    return ret;
4330 43b96858 aliguori
}
4331 43b96858 aliguori
4332 e6e35b1e aliguori
static void tcg_cpu_exec(void)
4333 e6e35b1e aliguori
{
4334 d6dc3d42 aliguori
    int ret = 0;
4335 e6e35b1e aliguori
4336 e6e35b1e aliguori
    if (next_cpu == NULL)
4337 e6e35b1e aliguori
        next_cpu = first_cpu;
4338 e6e35b1e aliguori
    for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4339 e6e35b1e aliguori
        CPUState *env = cur_cpu = next_cpu;
4340 e6e35b1e aliguori
4341 e6e35b1e aliguori
        if (!vm_running)
4342 e6e35b1e aliguori
            break;
4343 e6e35b1e aliguori
        if (timer_alarm_pending) {
4344 e6e35b1e aliguori
            timer_alarm_pending = 0;
4345 e6e35b1e aliguori
            break;
4346 e6e35b1e aliguori
        }
4347 d6dc3d42 aliguori
        if (cpu_can_run(env))
4348 d6dc3d42 aliguori
            ret = qemu_cpu_exec(env);
4349 e6e35b1e aliguori
        if (ret == EXCP_DEBUG) {
4350 e6e35b1e aliguori
            gdb_set_stop_cpu(env);
4351 e6e35b1e aliguori
            debug_requested = 1;
4352 e6e35b1e aliguori
            break;
4353 e6e35b1e aliguori
        }
4354 e6e35b1e aliguori
    }
4355 e6e35b1e aliguori
}
4356 e6e35b1e aliguori
4357 43b96858 aliguori
static int cpu_has_work(CPUState *env)
4358 43b96858 aliguori
{
4359 d6dc3d42 aliguori
    if (env->stop)
4360 d6dc3d42 aliguori
        return 1;
4361 d6dc3d42 aliguori
    if (env->stopped)
4362 d6dc3d42 aliguori
        return 0;
4363 43b96858 aliguori
    if (!env->halted)
4364 43b96858 aliguori
        return 1;
4365 43b96858 aliguori
    if (qemu_cpu_has_work(env))
4366 43b96858 aliguori
        return 1;
4367 43b96858 aliguori
    return 0;
4368 43b96858 aliguori
}
4369 43b96858 aliguori
4370 43b96858 aliguori
static int tcg_has_work(void)
4371 43b96858 aliguori
{
4372 43b96858 aliguori
    CPUState *env;
4373 43b96858 aliguori
4374 43b96858 aliguori
    for (env = first_cpu; env != NULL; env = env->next_cpu)
4375 43b96858 aliguori
        if (cpu_has_work(env))
4376 43b96858 aliguori
            return 1;
4377 43b96858 aliguori
    return 0;
4378 43b96858 aliguori
}
4379 43b96858 aliguori
4380 43b96858 aliguori
static int qemu_calculate_timeout(void)
4381 43b96858 aliguori
{
4382 b319820d Luiz Capitulino
#ifndef CONFIG_IOTHREAD
4383 43b96858 aliguori
    int timeout;
4384 43b96858 aliguori
4385 43b96858 aliguori
    if (!vm_running)
4386 43b96858 aliguori
        timeout = 5000;
4387 43b96858 aliguori
    else if (tcg_has_work())
4388 43b96858 aliguori
        timeout = 0;
4389 43b96858 aliguori
    else if (!use_icount)
4390 43b96858 aliguori
        timeout = 5000;
4391 43b96858 aliguori
    else {
4392 43b96858 aliguori
     /* XXX: use timeout computed from timers */
4393 43b96858 aliguori
        int64_t add;
4394 43b96858 aliguori
        int64_t delta;
4395 43b96858 aliguori
        /* Advance virtual time to the next event.  */
4396 43b96858 aliguori
        if (use_icount == 1) {
4397 43b96858 aliguori
            /* When not using an adaptive execution frequency
4398 43b96858 aliguori
               we tend to get badly out of sync with real time,
4399 43b96858 aliguori
               so just delay for a reasonable amount of time.  */
4400 43b96858 aliguori
            delta = 0;
4401 43b96858 aliguori
        } else {
4402 43b96858 aliguori
            delta = cpu_get_icount() - cpu_get_clock();
4403 43b96858 aliguori
        }
4404 43b96858 aliguori
        if (delta > 0) {
4405 43b96858 aliguori
            /* If virtual time is ahead of real time then just
4406 43b96858 aliguori
               wait for IO.  */
4407 43b96858 aliguori
            timeout = (delta / 1000000) + 1;
4408 43b96858 aliguori
        } else {
4409 43b96858 aliguori
            /* Wait for either IO to occur or the next
4410 43b96858 aliguori
               timer event.  */
4411 43b96858 aliguori
            add = qemu_next_deadline();
4412 43b96858 aliguori
            /* We advance the timer before checking for IO.
4413 43b96858 aliguori
               Limit the amount we advance so that early IO
4414 43b96858 aliguori
               activity won't get the guest too far ahead.  */
4415 43b96858 aliguori
            if (add > 10000000)
4416 43b96858 aliguori
                add = 10000000;
4417 43b96858 aliguori
            delta += add;
4418 43b96858 aliguori
            add = (add + (1 << icount_time_shift) - 1)
4419 43b96858 aliguori
                  >> icount_time_shift;
4420 43b96858 aliguori
            qemu_icount += add;
4421 43b96858 aliguori
            timeout = delta / 1000000;
4422 43b96858 aliguori
            if (timeout < 0)
4423 43b96858 aliguori
                timeout = 0;
4424 43b96858 aliguori
        }
4425 43b96858 aliguori
    }
4426 43b96858 aliguori
4427 43b96858 aliguori
    return timeout;
4428 b319820d Luiz Capitulino
#else /* CONFIG_IOTHREAD */
4429 b319820d Luiz Capitulino
    return 1000;
4430 b319820d Luiz Capitulino
#endif
4431 43b96858 aliguori
}
4432 43b96858 aliguori
4433 43b96858 aliguori
static int vm_can_run(void)
4434 43b96858 aliguori
{
4435 43b96858 aliguori
    if (powerdown_requested)
4436 43b96858 aliguori
        return 0;
4437 43b96858 aliguori
    if (reset_requested)
4438 43b96858 aliguori
        return 0;
4439 43b96858 aliguori
    if (shutdown_requested)
4440 43b96858 aliguori
        return 0;
4441 e568902a aliguori
    if (debug_requested)
4442 e568902a aliguori
        return 0;
4443 43b96858 aliguori
    return 1;
4444 43b96858 aliguori
}
4445 43b96858 aliguori
4446 43b96858 aliguori
static void main_loop(void)
4447 43b96858 aliguori
{
4448 6e29f5da aliguori
    int r;
4449 e6e35b1e aliguori
4450 d6dc3d42 aliguori
#ifdef CONFIG_IOTHREAD
4451 d6dc3d42 aliguori
    qemu_system_ready = 1;
4452 d6dc3d42 aliguori
    qemu_cond_broadcast(&qemu_system_cond);
4453 d6dc3d42 aliguori
#endif
4454 d6dc3d42 aliguori
4455 6e29f5da aliguori
    for (;;) {
4456 43b96858 aliguori
        do {
4457 e6e35b1e aliguori
#ifdef CONFIG_PROFILER
4458 e6e35b1e aliguori
            int64_t ti;
4459 e6e35b1e aliguori
#endif
4460 d6dc3d42 aliguori
#ifndef CONFIG_IOTHREAD
4461 e6e35b1e aliguori
            tcg_cpu_exec();
4462 d6dc3d42 aliguori
#endif
4463 89bfc105 bellard
#ifdef CONFIG_PROFILER
4464 43b96858 aliguori
            ti = profile_getclock();
4465 89bfc105 bellard
#endif
4466 43b96858 aliguori
            main_loop_wait(qemu_calculate_timeout());
4467 89bfc105 bellard
#ifdef CONFIG_PROFILER
4468 43b96858 aliguori
            dev_time += profile_getclock() - ti;
4469 89bfc105 bellard
#endif
4470 e568902a aliguori
        } while (vm_can_run());
4471 43b96858 aliguori
4472 e568902a aliguori
        if (qemu_debug_requested())
4473 e568902a aliguori
            vm_stop(EXCP_DEBUG);
4474 43b96858 aliguori
        if (qemu_shutdown_requested()) {
4475 43b96858 aliguori
            if (no_shutdown) {
4476 43b96858 aliguori
                vm_stop(0);
4477 43b96858 aliguori
                no_shutdown = 0;
4478 43b96858 aliguori
            } else
4479 43b96858 aliguori
                break;
4480 43b96858 aliguori
        }
4481 d6dc3d42 aliguori
        if (qemu_reset_requested()) {
4482 d6dc3d42 aliguori
            pause_all_vcpus();
4483 43b96858 aliguori
            qemu_system_reset();
4484 d6dc3d42 aliguori
            resume_all_vcpus();
4485 d6dc3d42 aliguori
        }
4486 43b96858 aliguori
        if (qemu_powerdown_requested())
4487 43b96858 aliguori
            qemu_system_powerdown();
4488 6e29f5da aliguori
        if ((r = qemu_vmstop_requested()))
4489 6e29f5da aliguori
            vm_stop(r);
4490 b4608c04 bellard
    }
4491 d6dc3d42 aliguori
    pause_all_vcpus();
4492 b4608c04 bellard
}
4493 b4608c04 bellard
4494 9bd7e6d9 pbrook
static void version(void)
4495 9bd7e6d9 pbrook
{
4496 4a19f1ec pbrook
    printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
4497 9bd7e6d9 pbrook
}
4498 9bd7e6d9 pbrook
4499 15f82208 ths
static void help(int exitcode)
4500 0824d6fc bellard
{
4501 9bd7e6d9 pbrook
    version();
4502 9bd7e6d9 pbrook
    printf("usage: %s [options] [disk_image]\n"
4503 0824d6fc bellard
           "\n"
4504 a20dd508 bellard
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4505 fc01f7e7 bellard
           "\n"
4506 5824d651 blueswir1
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4507 5824d651 blueswir1
           opt_help
4508 5824d651 blueswir1
#define DEFHEADING(text) stringify(text) "\n"
4509 5824d651 blueswir1
#include "qemu-options.h"
4510 5824d651 blueswir1
#undef DEF
4511 5824d651 blueswir1
#undef DEFHEADING
4512 5824d651 blueswir1
#undef GEN_DOCS
4513 0824d6fc bellard
           "\n"
4514 82c643ff bellard
           "During emulation, the following keys are useful:\n"
4515 032a8c9e bellard
           "ctrl-alt-f      toggle full screen\n"
4516 032a8c9e bellard
           "ctrl-alt-n      switch to virtual console 'n'\n"
4517 032a8c9e bellard
           "ctrl-alt        toggle mouse and keyboard grab\n"
4518 82c643ff bellard
           "\n"
4519 82c643ff bellard
           "When using -nographic, press 'ctrl-a h' to get some help.\n"
4520 82c643ff bellard
           ,
4521 0db63474 bellard
           "qemu",
4522 a00bad7e bellard
           DEFAULT_RAM_SIZE,
4523 7c9d8e07 bellard
#ifndef _WIN32
4524 a00bad7e bellard
           DEFAULT_NETWORK_SCRIPT,
4525 b46a8906 ths
           DEFAULT_NETWORK_DOWN_SCRIPT,
4526 7c9d8e07 bellard
#endif
4527 6e44ba7f bellard
           DEFAULT_GDBSTUB_PORT,
4528 bce61846 bellard
           "/tmp/qemu.log");
4529 15f82208 ths
    exit(exitcode);
4530 0824d6fc bellard
}
4531 0824d6fc bellard
4532 cd6f1169 bellard
#define HAS_ARG 0x0001
4533 cd6f1169 bellard
4534 cd6f1169 bellard
enum {
4535 5824d651 blueswir1
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4536 5824d651 blueswir1
    opt_enum,
4537 5824d651 blueswir1
#define DEFHEADING(text)
4538 5824d651 blueswir1
#include "qemu-options.h"
4539 5824d651 blueswir1
#undef DEF
4540 5824d651 blueswir1
#undef DEFHEADING
4541 5824d651 blueswir1
#undef GEN_DOCS
4542 cd6f1169 bellard
};
4543 cd6f1169 bellard
4544 cd6f1169 bellard
typedef struct QEMUOption {
4545 cd6f1169 bellard
    const char *name;
4546 cd6f1169 bellard
    int flags;
4547 cd6f1169 bellard
    int index;
4548 cd6f1169 bellard
} QEMUOption;
4549 cd6f1169 bellard
4550 dbed7e40 blueswir1
static const QEMUOption qemu_options[] = {
4551 cd6f1169 bellard
    { "h", 0, QEMU_OPTION_h },
4552 5824d651 blueswir1
#define DEF(option, opt_arg, opt_enum, opt_help)        \
4553 5824d651 blueswir1
    { option, opt_arg, opt_enum },
4554 5824d651 blueswir1
#define DEFHEADING(text)
4555 5824d651 blueswir1
#include "qemu-options.h"
4556 5824d651 blueswir1
#undef DEF
4557 5824d651 blueswir1
#undef DEFHEADING
4558 5824d651 blueswir1
#undef GEN_DOCS
4559 cd6f1169 bellard
    { NULL },
4560 fc01f7e7 bellard
};
4561 fc01f7e7 bellard
4562 1d14ffa9 bellard
#ifdef HAS_AUDIO
4563 6a36d84e bellard
struct soundhw soundhw[] = {
4564 b00052e4 balrog
#ifdef HAS_AUDIO_CHOICE
4565 4ce7ff6e aurel32
#if defined(TARGET_I386) || defined(TARGET_MIPS)
4566 fd06c375 bellard
    {
4567 fd06c375 bellard
        "pcspk",
4568 fd06c375 bellard
        "PC speaker",
4569 fd06c375 bellard
        0,
4570 fd06c375 bellard
        1,
4571 fd06c375 bellard
        { .init_isa = pcspk_audio_init }
4572 fd06c375 bellard
    },
4573 fd06c375 bellard
#endif
4574 4c9b53e3 malc
4575 4c9b53e3 malc
#ifdef CONFIG_SB16
4576 6a36d84e bellard
    {
4577 6a36d84e bellard
        "sb16",
4578 6a36d84e bellard
        "Creative Sound Blaster 16",
4579 6a36d84e bellard
        0,
4580 6a36d84e bellard
        1,
4581 6a36d84e bellard
        { .init_isa = SB16_init }
4582 6a36d84e bellard
    },
4583 4c9b53e3 malc
#endif
4584 6a36d84e bellard
4585 cc53d26d malc
#ifdef CONFIG_CS4231A
4586 cc53d26d malc
    {
4587 cc53d26d malc
        "cs4231a",
4588 cc53d26d malc
        "CS4231A",
4589 cc53d26d malc
        0,
4590 cc53d26d malc
        1,
4591 cc53d26d malc
        { .init_isa = cs4231a_init }
4592 cc53d26d malc
    },
4593 cc53d26d malc
#endif
4594 cc53d26d malc
4595 1d14ffa9 bellard
#ifdef CONFIG_ADLIB
4596 6a36d84e bellard
    {
4597 6a36d84e bellard
        "adlib",
4598 1d14ffa9 bellard
#ifdef HAS_YMF262
4599 6a36d84e bellard
        "Yamaha YMF262 (OPL3)",
4600 1d14ffa9 bellard
#else
4601 6a36d84e bellard
        "Yamaha YM3812 (OPL2)",
4602 1d14ffa9 bellard
#endif
4603 6a36d84e bellard
        0,
4604 6a36d84e bellard
        1,
4605 6a36d84e bellard
        { .init_isa = Adlib_init }
4606 6a36d84e bellard
    },
4607 1d14ffa9 bellard
#endif
4608 6a36d84e bellard
4609 1d14ffa9 bellard
#ifdef CONFIG_GUS
4610 6a36d84e bellard
    {
4611 6a36d84e bellard
        "gus",
4612 6a36d84e bellard
        "Gravis Ultrasound GF1",
4613 6a36d84e bellard
        0,
4614 6a36d84e bellard
        1,
4615 6a36d84e bellard
        { .init_isa = GUS_init }
4616 6a36d84e bellard
    },
4617 1d14ffa9 bellard
#endif
4618 6a36d84e bellard
4619 4c9b53e3 malc
#ifdef CONFIG_AC97
4620 e5c9a13e balrog
    {
4621 e5c9a13e balrog
        "ac97",
4622 e5c9a13e balrog
        "Intel 82801AA AC97 Audio",
4623 e5c9a13e balrog
        0,
4624 e5c9a13e balrog
        0,
4625 e5c9a13e balrog
        { .init_pci = ac97_init }
4626 e5c9a13e balrog
    },
4627 4c9b53e3 malc
#endif
4628 e5c9a13e balrog
4629 4c9b53e3 malc
#ifdef CONFIG_ES1370
4630 6a36d84e bellard
    {
4631 6a36d84e bellard
        "es1370",
4632 6a36d84e bellard
        "ENSONIQ AudioPCI ES1370",
4633 6a36d84e bellard
        0,
4634 6a36d84e bellard
        0,
4635 6a36d84e bellard
        { .init_pci = es1370_init }
4636 6a36d84e bellard
    },
4637 b00052e4 balrog
#endif
4638 6a36d84e bellard
4639 4c9b53e3 malc
#endif /* HAS_AUDIO_CHOICE */
4640 4c9b53e3 malc
4641 6a36d84e bellard
    { NULL, NULL, 0, 0, { NULL } }
4642 6a36d84e bellard
};
4643 6a36d84e bellard
4644 6a36d84e bellard
static void select_soundhw (const char *optarg)
4645 6a36d84e bellard
{
4646 6a36d84e bellard
    struct soundhw *c;
4647 6a36d84e bellard
4648 6a36d84e bellard
    if (*optarg == '?') {
4649 6a36d84e bellard
    show_valid_cards:
4650 6a36d84e bellard
4651 6a36d84e bellard
        printf ("Valid sound card names (comma separated):\n");
4652 6a36d84e bellard
        for (c = soundhw; c->name; ++c) {
4653 6a36d84e bellard
            printf ("%-11s %s\n", c->name, c->descr);
4654 6a36d84e bellard
        }
4655 6a36d84e bellard
        printf ("\n-soundhw all will enable all of the above\n");
4656 1d14ffa9 bellard
        exit (*optarg != '?');
4657 1d14ffa9 bellard
    }
4658 1d14ffa9 bellard
    else {
4659 6a36d84e bellard
        size_t l;
4660 1d14ffa9 bellard
        const char *p;
4661 1d14ffa9 bellard
        char *e;
4662 1d14ffa9 bellard
        int bad_card = 0;
4663 1d14ffa9 bellard
4664 6a36d84e bellard
        if (!strcmp (optarg, "all")) {
4665 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
4666 6a36d84e bellard
                c->enabled = 1;
4667 6a36d84e bellard
            }
4668 6a36d84e bellard
            return;
4669 6a36d84e bellard
        }
4670 1d14ffa9 bellard
4671 6a36d84e bellard
        p = optarg;
4672 1d14ffa9 bellard
        while (*p) {
4673 1d14ffa9 bellard
            e = strchr (p, ',');
4674 1d14ffa9 bellard
            l = !e ? strlen (p) : (size_t) (e - p);
4675 6a36d84e bellard
4676 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
4677 6a36d84e bellard
                if (!strncmp (c->name, p, l)) {
4678 6a36d84e bellard
                    c->enabled = 1;
4679 1d14ffa9 bellard
                    break;
4680 1d14ffa9 bellard
                }
4681 1d14ffa9 bellard
            }
4682 6a36d84e bellard
4683 6a36d84e bellard
            if (!c->name) {
4684 1d14ffa9 bellard
                if (l > 80) {
4685 1d14ffa9 bellard
                    fprintf (stderr,
4686 1d14ffa9 bellard
                             "Unknown sound card name (too big to show)\n");
4687 1d14ffa9 bellard
                }
4688 1d14ffa9 bellard
                else {
4689 1d14ffa9 bellard
                    fprintf (stderr, "Unknown sound card name `%.*s'\n",
4690 1d14ffa9 bellard
                             (int) l, p);
4691 1d14ffa9 bellard
                }
4692 1d14ffa9 bellard
                bad_card = 1;
4693 1d14ffa9 bellard
            }
4694 1d14ffa9 bellard
            p += l + (e != NULL);
4695 1d14ffa9 bellard
        }
4696 1d14ffa9 bellard
4697 1d14ffa9 bellard
        if (bad_card)
4698 1d14ffa9 bellard
            goto show_valid_cards;
4699 1d14ffa9 bellard
    }
4700 1d14ffa9 bellard
}
4701 1d14ffa9 bellard
#endif
4702 1d14ffa9 bellard
4703 3893c124 malc
static void select_vgahw (const char *p)
4704 3893c124 malc
{
4705 3893c124 malc
    const char *opts;
4706 3893c124 malc
4707 28b85ed8 aliguori
    cirrus_vga_enabled = 0;
4708 28b85ed8 aliguori
    std_vga_enabled = 0;
4709 28b85ed8 aliguori
    vmsvga_enabled = 0;
4710 94909d9f aliguori
    xenfb_enabled = 0;
4711 3893c124 malc
    if (strstart(p, "std", &opts)) {
4712 c2b3b41a aliguori
        std_vga_enabled = 1;
4713 3893c124 malc
    } else if (strstart(p, "cirrus", &opts)) {
4714 3893c124 malc
        cirrus_vga_enabled = 1;
4715 3893c124 malc
    } else if (strstart(p, "vmware", &opts)) {
4716 3893c124 malc
        vmsvga_enabled = 1;
4717 94909d9f aliguori
    } else if (strstart(p, "xenfb", &opts)) {
4718 94909d9f aliguori
        xenfb_enabled = 1;
4719 28b85ed8 aliguori
    } else if (!strstart(p, "none", &opts)) {
4720 3893c124 malc
    invalid_vga:
4721 3893c124 malc
        fprintf(stderr, "Unknown vga type: %s\n", p);
4722 3893c124 malc
        exit(1);
4723 3893c124 malc
    }
4724 cb5a7aa8 malc
    while (*opts) {
4725 cb5a7aa8 malc
        const char *nextopt;
4726 cb5a7aa8 malc
4727 cb5a7aa8 malc
        if (strstart(opts, ",retrace=", &nextopt)) {
4728 cb5a7aa8 malc
            opts = nextopt;
4729 cb5a7aa8 malc
            if (strstart(opts, "dumb", &nextopt))
4730 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_DUMB;
4731 cb5a7aa8 malc
            else if (strstart(opts, "precise", &nextopt))
4732 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_PRECISE;
4733 cb5a7aa8 malc
            else goto invalid_vga;
4734 cb5a7aa8 malc
        } else goto invalid_vga;
4735 cb5a7aa8 malc
        opts = nextopt;
4736 cb5a7aa8 malc
    }
4737 3893c124 malc
}
4738 3893c124 malc
4739 3587d7e6 bellard
#ifdef _WIN32
4740 3587d7e6 bellard
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4741 3587d7e6 bellard
{
4742 3587d7e6 bellard
    exit(STATUS_CONTROL_C_EXIT);
4743 3587d7e6 bellard
    return TRUE;
4744 3587d7e6 bellard
}
4745 3587d7e6 bellard
#endif
4746 3587d7e6 bellard
4747 c4be29ff aliguori
int qemu_uuid_parse(const char *str, uint8_t *uuid)
4748 8fcb1b90 blueswir1
{
4749 8fcb1b90 blueswir1
    int ret;
4750 8fcb1b90 blueswir1
4751 8fcb1b90 blueswir1
    if(strlen(str) != 36)
4752 8fcb1b90 blueswir1
        return -1;
4753 8fcb1b90 blueswir1
4754 8fcb1b90 blueswir1
    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4755 8fcb1b90 blueswir1
            &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4756 8fcb1b90 blueswir1
            &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4757 8fcb1b90 blueswir1
4758 8fcb1b90 blueswir1
    if(ret != 16)
4759 8fcb1b90 blueswir1
        return -1;
4760 8fcb1b90 blueswir1
4761 b6f6e3d3 aliguori
#ifdef TARGET_I386
4762 b6f6e3d3 aliguori
    smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4763 b6f6e3d3 aliguori
#endif
4764 b6f6e3d3 aliguori
4765 8fcb1b90 blueswir1
    return 0;
4766 8fcb1b90 blueswir1
}
4767 8fcb1b90 blueswir1
4768 7c9d8e07 bellard
#define MAX_NET_CLIENTS 32
4769 c20709aa bellard
4770 5b08fc10 aliguori
#ifndef _WIN32
4771 5b08fc10 aliguori
4772 5b08fc10 aliguori
static void termsig_handler(int signal)
4773 5b08fc10 aliguori
{
4774 5b08fc10 aliguori
    qemu_system_shutdown_request();
4775 5b08fc10 aliguori
}
4776 5b08fc10 aliguori
4777 7c3370d4 Jan Kiszka
static void sigchld_handler(int signal)
4778 7c3370d4 Jan Kiszka
{
4779 7c3370d4 Jan Kiszka
    waitpid(-1, NULL, WNOHANG);
4780 7c3370d4 Jan Kiszka
}
4781 7c3370d4 Jan Kiszka
4782 7c3370d4 Jan Kiszka
static void sighandler_setup(void)
4783 5b08fc10 aliguori
{
4784 5b08fc10 aliguori
    struct sigaction act;
4785 5b08fc10 aliguori
4786 5b08fc10 aliguori
    memset(&act, 0, sizeof(act));
4787 5b08fc10 aliguori
    act.sa_handler = termsig_handler;
4788 5b08fc10 aliguori
    sigaction(SIGINT,  &act, NULL);
4789 5b08fc10 aliguori
    sigaction(SIGHUP,  &act, NULL);
4790 5b08fc10 aliguori
    sigaction(SIGTERM, &act, NULL);
4791 7c3370d4 Jan Kiszka
4792 7c3370d4 Jan Kiszka
    act.sa_handler = sigchld_handler;
4793 7c3370d4 Jan Kiszka
    act.sa_flags = SA_NOCLDSTOP;
4794 7c3370d4 Jan Kiszka
    sigaction(SIGCHLD, &act, NULL);
4795 5b08fc10 aliguori
}
4796 5b08fc10 aliguori
4797 5b08fc10 aliguori
#endif
4798 5b08fc10 aliguori
4799 5cea8590 Paul Brook
#ifdef _WIN32
4800 5cea8590 Paul Brook
/* Look for support files in the same directory as the executable.  */
4801 5cea8590 Paul Brook
static char *find_datadir(const char *argv0)
4802 5cea8590 Paul Brook
{
4803 5cea8590 Paul Brook
    char *p;
4804 5cea8590 Paul Brook
    char buf[MAX_PATH];
4805 5cea8590 Paul Brook
    DWORD len;
4806 5cea8590 Paul Brook
4807 5cea8590 Paul Brook
    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4808 5cea8590 Paul Brook
    if (len == 0) {
4809 c5947808 Blue Swirl
        return NULL;
4810 5cea8590 Paul Brook
    }
4811 5cea8590 Paul Brook
4812 5cea8590 Paul Brook
    buf[len] = 0;
4813 5cea8590 Paul Brook
    p = buf + len - 1;
4814 5cea8590 Paul Brook
    while (p != buf && *p != '\\')
4815 5cea8590 Paul Brook
        p--;
4816 5cea8590 Paul Brook
    *p = 0;
4817 5cea8590 Paul Brook
    if (access(buf, R_OK) == 0) {
4818 5cea8590 Paul Brook
        return qemu_strdup(buf);
4819 5cea8590 Paul Brook
    }
4820 5cea8590 Paul Brook
    return NULL;
4821 5cea8590 Paul Brook
}
4822 5cea8590 Paul Brook
#else /* !_WIN32 */
4823 5cea8590 Paul Brook
4824 5cea8590 Paul Brook
/* Find a likely location for support files using the location of the binary.
4825 5cea8590 Paul Brook
   For installed binaries this will be "$bindir/../share/qemu".  When
4826 5cea8590 Paul Brook
   running from the build tree this will be "$bindir/../pc-bios".  */
4827 5cea8590 Paul Brook
#define SHARE_SUFFIX "/share/qemu"
4828 5cea8590 Paul Brook
#define BUILD_SUFFIX "/pc-bios"
4829 5cea8590 Paul Brook
static char *find_datadir(const char *argv0)
4830 5cea8590 Paul Brook
{
4831 5cea8590 Paul Brook
    char *dir;
4832 5cea8590 Paul Brook
    char *p = NULL;
4833 5cea8590 Paul Brook
    char *res;
4834 5cea8590 Paul Brook
#ifdef PATH_MAX
4835 5cea8590 Paul Brook
    char buf[PATH_MAX];
4836 5cea8590 Paul Brook
#endif
4837 3a41759d Blue Swirl
    size_t max_len;
4838 5cea8590 Paul Brook
4839 5cea8590 Paul Brook
#if defined(__linux__)
4840 5cea8590 Paul Brook
    {
4841 5cea8590 Paul Brook
        int len;
4842 5cea8590 Paul Brook
        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4843 5cea8590 Paul Brook
        if (len > 0) {
4844 5cea8590 Paul Brook
            buf[len] = 0;
4845 5cea8590 Paul Brook
            p = buf;
4846 5cea8590 Paul Brook
        }
4847 5cea8590 Paul Brook
    }
4848 5cea8590 Paul Brook
#elif defined(__FreeBSD__)
4849 5cea8590 Paul Brook
    {
4850 5cea8590 Paul Brook
        int len;
4851 5cea8590 Paul Brook
        len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4852 5cea8590 Paul Brook
        if (len > 0) {
4853 5cea8590 Paul Brook
            buf[len] = 0;
4854 5cea8590 Paul Brook
            p = buf;
4855 5cea8590 Paul Brook
        }
4856 5cea8590 Paul Brook
    }
4857 5cea8590 Paul Brook
#endif
4858 5cea8590 Paul Brook
    /* If we don't have any way of figuring out the actual executable
4859 5cea8590 Paul Brook
       location then try argv[0].  */
4860 5cea8590 Paul Brook
    if (!p) {
4861 5cea8590 Paul Brook
#ifdef PATH_MAX
4862 5cea8590 Paul Brook
        p = buf;
4863 5cea8590 Paul Brook
#endif
4864 5cea8590 Paul Brook
        p = realpath(argv0, p);
4865 5cea8590 Paul Brook
        if (!p) {
4866 5cea8590 Paul Brook
            return NULL;
4867 5cea8590 Paul Brook
        }
4868 5cea8590 Paul Brook
    }
4869 5cea8590 Paul Brook
    dir = dirname(p);
4870 5cea8590 Paul Brook
    dir = dirname(dir);
4871 5cea8590 Paul Brook
4872 3a41759d Blue Swirl
    max_len = strlen(dir) +
4873 3a41759d Blue Swirl
        MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4874 3a41759d Blue Swirl
    res = qemu_mallocz(max_len);
4875 3a41759d Blue Swirl
    snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
4876 5cea8590 Paul Brook
    if (access(res, R_OK)) {
4877 3a41759d Blue Swirl
        snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
4878 5cea8590 Paul Brook
        if (access(res, R_OK)) {
4879 5cea8590 Paul Brook
            qemu_free(res);
4880 5cea8590 Paul Brook
            res = NULL;
4881 5cea8590 Paul Brook
        }
4882 5cea8590 Paul Brook
    }
4883 5cea8590 Paul Brook
#ifndef PATH_MAX
4884 5cea8590 Paul Brook
    free(p);
4885 5cea8590 Paul Brook
#endif
4886 5cea8590 Paul Brook
    return res;
4887 5cea8590 Paul Brook
}
4888 5cea8590 Paul Brook
#undef SHARE_SUFFIX
4889 5cea8590 Paul Brook
#undef BUILD_SUFFIX
4890 5cea8590 Paul Brook
#endif
4891 5cea8590 Paul Brook
4892 5cea8590 Paul Brook
char *qemu_find_file(int type, const char *name)
4893 5cea8590 Paul Brook
{
4894 5cea8590 Paul Brook
    int len;
4895 5cea8590 Paul Brook
    const char *subdir;
4896 5cea8590 Paul Brook
    char *buf;
4897 5cea8590 Paul Brook
4898 5cea8590 Paul Brook
    /* If name contains path separators then try it as a straight path.  */
4899 5cea8590 Paul Brook
    if ((strchr(name, '/') || strchr(name, '\\'))
4900 5cea8590 Paul Brook
        && access(name, R_OK) == 0) {
4901 5cea8590 Paul Brook
        return strdup(name);
4902 5cea8590 Paul Brook
    }
4903 5cea8590 Paul Brook
    switch (type) {
4904 5cea8590 Paul Brook
    case QEMU_FILE_TYPE_BIOS:
4905 5cea8590 Paul Brook
        subdir = "";
4906 5cea8590 Paul Brook
        break;
4907 5cea8590 Paul Brook
    case QEMU_FILE_TYPE_KEYMAP:
4908 5cea8590 Paul Brook
        subdir = "keymaps/";
4909 5cea8590 Paul Brook
        break;
4910 5cea8590 Paul Brook
    default:
4911 5cea8590 Paul Brook
        abort();
4912 5cea8590 Paul Brook
    }
4913 5cea8590 Paul Brook
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4914 5cea8590 Paul Brook
    buf = qemu_mallocz(len);
4915 3a41759d Blue Swirl
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
4916 5cea8590 Paul Brook
    if (access(buf, R_OK)) {
4917 5cea8590 Paul Brook
        qemu_free(buf);
4918 5cea8590 Paul Brook
        return NULL;
4919 5cea8590 Paul Brook
    }
4920 5cea8590 Paul Brook
    return buf;
4921 5cea8590 Paul Brook
}
4922 5cea8590 Paul Brook
4923 902b3d5c malc
int main(int argc, char **argv, char **envp)
4924 0824d6fc bellard
{
4925 59030a8c aliguori
    const char *gdbstub_dev = NULL;
4926 28c5af54 j_mayer
    uint32_t boot_devices_bitmap = 0;
4927 e4bcb14c ths
    int i;
4928 28c5af54 j_mayer
    int snapshot, linux_boot, net_boot;
4929 7f7f9873 bellard
    const char *initrd_filename;
4930 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
4931 28c5af54 j_mayer
    const char *boot_devices = "";
4932 3023f332 aliguori
    DisplayState *ds;
4933 7d957bd8 aliguori
    DisplayChangeListener *dcl;
4934 46d4767d bellard
    int cyls, heads, secs, translation;
4935 fd5f393a pbrook
    const char *net_clients[MAX_NET_CLIENTS];
4936 7c9d8e07 bellard
    int nb_net_clients;
4937 dc72ac14 balrog
    const char *bt_opts[MAX_BT_CMDLINE];
4938 dc72ac14 balrog
    int nb_bt_opts;
4939 e4bcb14c ths
    int hda_index;
4940 cd6f1169 bellard
    int optind;
4941 cd6f1169 bellard
    const char *r, *optarg;
4942 4c621805 aliguori
    CharDriverState *monitor_hd = NULL;
4943 fd5f393a pbrook
    const char *monitor_device;
4944 fd5f393a pbrook
    const char *serial_devices[MAX_SERIAL_PORTS];
4945 8d11df9e bellard
    int serial_device_index;
4946 fd5f393a pbrook
    const char *parallel_devices[MAX_PARALLEL_PORTS];
4947 6508fe59 bellard
    int parallel_device_index;
4948 9ede2fde aliguori
    const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4949 9ede2fde aliguori
    int virtio_console_index;
4950 d63d307f bellard
    const char *loadvm = NULL;
4951 cc1daa40 bellard
    QEMUMachine *machine;
4952 94fc95cd j_mayer
    const char *cpu_model;
4953 fd5f393a pbrook
    const char *usb_devices[MAX_USB_CMDLINE];
4954 a594cfbf bellard
    int usb_devices_index;
4955 b9e82a59 blueswir1
#ifndef _WIN32
4956 71e3ceb8 ths
    int fds[2];
4957 b9e82a59 blueswir1
#endif
4958 26a5f13b bellard
    int tb_size;
4959 93815bc2 ths
    const char *pid_file = NULL;
4960 5bb7910a aliguori
    const char *incoming = NULL;
4961 b9e82a59 blueswir1
#ifndef _WIN32
4962 54042bcf aliguori
    int fd = 0;
4963 54042bcf aliguori
    struct passwd *pwd = NULL;
4964 0858532e aliguori
    const char *chroot_dir = NULL;
4965 0858532e aliguori
    const char *run_as = NULL;
4966 b9e82a59 blueswir1
#endif
4967 268a362c aliguori
    CPUState *env;
4968 993fbfdb Anthony Liguori
    int show_vnc_port = 0;
4969 0bd48850 bellard
4970 902b3d5c malc
    qemu_cache_utils_init(envp);
4971 902b3d5c malc
4972 0bd48850 bellard
    LIST_INIT (&vm_change_state_head);
4973 be995c27 bellard
#ifndef _WIN32
4974 be995c27 bellard
    {
4975 be995c27 bellard
        struct sigaction act;
4976 be995c27 bellard
        sigfillset(&act.sa_mask);
4977 be995c27 bellard
        act.sa_flags = 0;
4978 be995c27 bellard
        act.sa_handler = SIG_IGN;
4979 be995c27 bellard
        sigaction(SIGPIPE, &act, NULL);
4980 be995c27 bellard
    }
4981 3587d7e6 bellard
#else
4982 3587d7e6 bellard
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
4983 a8e5ac33 bellard
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
4984 a8e5ac33 bellard
       QEMU to run on a single CPU */
4985 a8e5ac33 bellard
    {
4986 a8e5ac33 bellard
        HANDLE h;
4987 a8e5ac33 bellard
        DWORD mask, smask;
4988 a8e5ac33 bellard
        int i;
4989 a8e5ac33 bellard
        h = GetCurrentProcess();
4990 a8e5ac33 bellard
        if (GetProcessAffinityMask(h, &mask, &smask)) {
4991 a8e5ac33 bellard
            for(i = 0; i < 32; i++) {
4992 a8e5ac33 bellard
                if (mask & (1 << i))
4993 a8e5ac33 bellard
                    break;
4994 a8e5ac33 bellard
            }
4995 a8e5ac33 bellard
            if (i != 32) {
4996 a8e5ac33 bellard
                mask = 1 << i;
4997 a8e5ac33 bellard
                SetProcessAffinityMask(h, mask);
4998 a8e5ac33 bellard
            }
4999 a8e5ac33 bellard
        }
5000 a8e5ac33 bellard
    }
5001 67b915a5 bellard
#endif
5002 be995c27 bellard
5003 f80f9ec9 Anthony Liguori
    module_call_init(MODULE_INIT_MACHINE);
5004 0c257437 Anthony Liguori
    machine = find_default_machine();
5005 94fc95cd j_mayer
    cpu_model = NULL;
5006 fc01f7e7 bellard
    initrd_filename = NULL;
5007 4fc5d071 aurel32
    ram_size = 0;
5008 33e3963e bellard
    snapshot = 0;
5009 a20dd508 bellard
    kernel_filename = NULL;
5010 a20dd508 bellard
    kernel_cmdline = "";
5011 c4b1fcc0 bellard
    cyls = heads = secs = 0;
5012 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
5013 d47d13b9 aliguori
    monitor_device = "vc:80Cx24C";
5014 c4b1fcc0 bellard
5015 c75a823c aurel32
    serial_devices[0] = "vc:80Cx24C";
5016 8d11df9e bellard
    for(i = 1; i < MAX_SERIAL_PORTS; i++)
5017 fd5f393a pbrook
        serial_devices[i] = NULL;
5018 8d11df9e bellard
    serial_device_index = 0;
5019 3b46e624 ths
5020 8290edda aliguori
    parallel_devices[0] = "vc:80Cx24C";
5021 6508fe59 bellard
    for(i = 1; i < MAX_PARALLEL_PORTS; i++)
5022 fd5f393a pbrook
        parallel_devices[i] = NULL;
5023 6508fe59 bellard
    parallel_device_index = 0;
5024 3b46e624 ths
5025 1b8fc811 aliguori
    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
5026 9ede2fde aliguori
        virtio_consoles[i] = NULL;
5027 9ede2fde aliguori
    virtio_console_index = 0;
5028 9ede2fde aliguori
5029 268a362c aliguori
    for (i = 0; i < MAX_NODES; i++) {
5030 268a362c aliguori
        node_mem[i] = 0;
5031 268a362c aliguori
        node_cpumask[i] = 0;
5032 268a362c aliguori
    }
5033 268a362c aliguori
5034 a594cfbf bellard
    usb_devices_index = 0;
5035 3b46e624 ths
5036 7c9d8e07 bellard
    nb_net_clients = 0;
5037 dc72ac14 balrog
    nb_bt_opts = 0;
5038 e4bcb14c ths
    nb_drives = 0;
5039 e4bcb14c ths
    nb_drives_opt = 0;
5040 268a362c aliguori
    nb_numa_nodes = 0;
5041 e4bcb14c ths
    hda_index = -1;
5042 7c9d8e07 bellard
5043 7c9d8e07 bellard
    nb_nics = 0;
5044 3b46e624 ths
5045 26a5f13b bellard
    tb_size = 0;
5046 41bd639b blueswir1
    autostart= 1;
5047 41bd639b blueswir1
5048 9dd986cc Richard W.M. Jones
    register_watchdogs();
5049 9dd986cc Richard W.M. Jones
5050 cd6f1169 bellard
    optind = 1;
5051 0824d6fc bellard
    for(;;) {
5052 cd6f1169 bellard
        if (optind >= argc)
5053 0824d6fc bellard
            break;
5054 cd6f1169 bellard
        r = argv[optind];
5055 cd6f1169 bellard
        if (r[0] != '-') {
5056 609497ab balrog
            hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
5057 cd6f1169 bellard
        } else {
5058 cd6f1169 bellard
            const QEMUOption *popt;
5059 cd6f1169 bellard
5060 cd6f1169 bellard
            optind++;
5061 dff5efc8 pbrook
            /* Treat --foo the same as -foo.  */
5062 dff5efc8 pbrook
            if (r[1] == '-')
5063 dff5efc8 pbrook
                r++;
5064 cd6f1169 bellard
            popt = qemu_options;
5065 cd6f1169 bellard
            for(;;) {
5066 cd6f1169 bellard
                if (!popt->name) {
5067 5fafdf24 ths
                    fprintf(stderr, "%s: invalid option -- '%s'\n",
5068 cd6f1169 bellard
                            argv[0], r);
5069 cd6f1169 bellard
                    exit(1);
5070 cd6f1169 bellard
                }
5071 cd6f1169 bellard
                if (!strcmp(popt->name, r + 1))
5072 cd6f1169 bellard
                    break;
5073 cd6f1169 bellard
                popt++;
5074 cd6f1169 bellard
            }
5075 cd6f1169 bellard
            if (popt->flags & HAS_ARG) {
5076 cd6f1169 bellard
                if (optind >= argc) {
5077 cd6f1169 bellard
                    fprintf(stderr, "%s: option '%s' requires an argument\n",
5078 cd6f1169 bellard
                            argv[0], r);
5079 cd6f1169 bellard
                    exit(1);
5080 cd6f1169 bellard
                }
5081 cd6f1169 bellard
                optarg = argv[optind++];
5082 cd6f1169 bellard
            } else {
5083 cd6f1169 bellard
                optarg = NULL;
5084 cd6f1169 bellard
            }
5085 cd6f1169 bellard
5086 cd6f1169 bellard
            switch(popt->index) {
5087 cc1daa40 bellard
            case QEMU_OPTION_M:
5088 cc1daa40 bellard
                machine = find_machine(optarg);
5089 cc1daa40 bellard
                if (!machine) {
5090 cc1daa40 bellard
                    QEMUMachine *m;
5091 cc1daa40 bellard
                    printf("Supported machines are:\n");
5092 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
5093 cc1daa40 bellard
                        printf("%-10s %s%s\n",
5094 5fafdf24 ths
                               m->name, m->desc,
5095 0c257437 Anthony Liguori
                               m->is_default ? " (default)" : "");
5096 cc1daa40 bellard
                    }
5097 15f82208 ths
                    exit(*optarg != '?');
5098 cc1daa40 bellard
                }
5099 cc1daa40 bellard
                break;
5100 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
5101 94fc95cd j_mayer
                /* hw initialization will check this */
5102 15f82208 ths
                if (*optarg == '?') {
5103 c732abe2 j_mayer
/* XXX: implement xxx_cpu_list for targets that still miss it */
5104 c732abe2 j_mayer
#if defined(cpu_list)
5105 c732abe2 j_mayer
                    cpu_list(stdout, &fprintf);
5106 94fc95cd j_mayer
#endif
5107 15f82208 ths
                    exit(0);
5108 94fc95cd j_mayer
                } else {
5109 94fc95cd j_mayer
                    cpu_model = optarg;
5110 94fc95cd j_mayer
                }
5111 94fc95cd j_mayer
                break;
5112 cd6f1169 bellard
            case QEMU_OPTION_initrd:
5113 fc01f7e7 bellard
                initrd_filename = optarg;
5114 fc01f7e7 bellard
                break;
5115 cd6f1169 bellard
            case QEMU_OPTION_hda:
5116 e4bcb14c ths
                if (cyls == 0)
5117 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS, 0);
5118 e4bcb14c ths
                else
5119 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS
5120 e4bcb14c ths
                             ",cyls=%d,heads=%d,secs=%d%s",
5121 609497ab balrog
                             0, cyls, heads, secs,
5122 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_LBA ?
5123 e4bcb14c ths
                                 ",trans=lba" :
5124 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_NONE ?
5125 e4bcb14c ths
                                 ",trans=none" : "");
5126 e4bcb14c ths
                 break;
5127 cd6f1169 bellard
            case QEMU_OPTION_hdb:
5128 cc1daa40 bellard
            case QEMU_OPTION_hdc:
5129 cc1daa40 bellard
            case QEMU_OPTION_hdd:
5130 609497ab balrog
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
5131 fc01f7e7 bellard
                break;
5132 e4bcb14c ths
            case QEMU_OPTION_drive:
5133 609497ab balrog
                drive_add(NULL, "%s", optarg);
5134 e4bcb14c ths
                break;
5135 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
5136 609497ab balrog
                drive_add(optarg, MTD_ALIAS);
5137 3e3d5815 balrog
                break;
5138 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
5139 609497ab balrog
                drive_add(optarg, SD_ALIAS);
5140 a1bb27b1 pbrook
                break;
5141 86f55663 j_mayer
            case QEMU_OPTION_pflash:
5142 609497ab balrog
                drive_add(optarg, PFLASH_ALIAS);
5143 86f55663 j_mayer
                break;
5144 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
5145 33e3963e bellard
                snapshot = 1;
5146 33e3963e bellard
                break;
5147 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
5148 330d0414 bellard
                {
5149 330d0414 bellard
                    const char *p;
5150 330d0414 bellard
                    p = optarg;
5151 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
5152 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
5153 46d4767d bellard
                        goto chs_fail;
5154 330d0414 bellard
                    if (*p != ',')
5155 330d0414 bellard
                        goto chs_fail;
5156 330d0414 bellard
                    p++;
5157 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
5158 46d4767d bellard
                    if (heads < 1 || heads > 16)
5159 46d4767d bellard
                        goto chs_fail;
5160 330d0414 bellard
                    if (*p != ',')
5161 330d0414 bellard
                        goto chs_fail;
5162 330d0414 bellard
                    p++;
5163 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
5164 46d4767d bellard
                    if (secs < 1 || secs > 63)
5165 46d4767d bellard
                        goto chs_fail;
5166 46d4767d bellard
                    if (*p == ',') {
5167 46d4767d bellard
                        p++;
5168 46d4767d bellard
                        if (!strcmp(p, "none"))
5169 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
5170 46d4767d bellard
                        else if (!strcmp(p, "lba"))
5171 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
5172 46d4767d bellard
                        else if (!strcmp(p, "auto"))
5173 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
5174 46d4767d bellard
                        else
5175 46d4767d bellard
                            goto chs_fail;
5176 46d4767d bellard
                    } else if (*p != '\0') {
5177 c4b1fcc0 bellard
                    chs_fail:
5178 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
5179 46d4767d bellard
                        exit(1);
5180 c4b1fcc0 bellard
                    }
5181 e4bcb14c ths
                    if (hda_index != -1)
5182 609497ab balrog
                        snprintf(drives_opt[hda_index].opt,
5183 609497ab balrog
                                 sizeof(drives_opt[hda_index].opt),
5184 609497ab balrog
                                 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5185 609497ab balrog
                                 0, cyls, heads, secs,
5186 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
5187 e4bcb14c ths
                                         ",trans=lba" :
5188 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
5189 e4bcb14c ths
                                     ",trans=none" : "");
5190 330d0414 bellard
                }
5191 330d0414 bellard
                break;
5192 268a362c aliguori
            case QEMU_OPTION_numa:
5193 268a362c aliguori
                if (nb_numa_nodes >= MAX_NODES) {
5194 268a362c aliguori
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
5195 268a362c aliguori
                    exit(1);
5196 268a362c aliguori
                }
5197 268a362c aliguori
                numa_add(optarg);
5198 268a362c aliguori
                break;
5199 cd6f1169 bellard
            case QEMU_OPTION_nographic:
5200 993fbfdb Anthony Liguori
                display_type = DT_NOGRAPHIC;
5201 a20dd508 bellard
                break;
5202 4d3b6f6e balrog
#ifdef CONFIG_CURSES
5203 4d3b6f6e balrog
            case QEMU_OPTION_curses:
5204 993fbfdb Anthony Liguori
                display_type = DT_CURSES;
5205 4d3b6f6e balrog
                break;
5206 4d3b6f6e balrog
#endif
5207 a171fe39 balrog
            case QEMU_OPTION_portrait:
5208 a171fe39 balrog
                graphic_rotate = 1;
5209 a171fe39 balrog
                break;
5210 cd6f1169 bellard
            case QEMU_OPTION_kernel:
5211 a20dd508 bellard
                kernel_filename = optarg;
5212 a20dd508 bellard
                break;
5213 cd6f1169 bellard
            case QEMU_OPTION_append:
5214 a20dd508 bellard
                kernel_cmdline = optarg;
5215 313aa567 bellard
                break;
5216 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
5217 609497ab balrog
                drive_add(optarg, CDROM_ALIAS);
5218 36b486bb bellard
                break;
5219 cd6f1169 bellard
            case QEMU_OPTION_boot:
5220 28c5af54 j_mayer
                boot_devices = optarg;
5221 28c5af54 j_mayer
                /* We just do some generic consistency checks */
5222 28c5af54 j_mayer
                {
5223 28c5af54 j_mayer
                    /* Could easily be extended to 64 devices if needed */
5224 60fe76f3 ths
                    const char *p;
5225 28c5af54 j_mayer
                    
5226 28c5af54 j_mayer
                    boot_devices_bitmap = 0;
5227 28c5af54 j_mayer
                    for (p = boot_devices; *p != '\0'; p++) {
5228 28c5af54 j_mayer
                        /* Allowed boot devices are:
5229 28c5af54 j_mayer
                         * a b     : floppy disk drives
5230 28c5af54 j_mayer
                         * c ... f : IDE disk drives
5231 28c5af54 j_mayer
                         * g ... m : machine implementation dependant drives
5232 28c5af54 j_mayer
                         * n ... p : network devices
5233 28c5af54 j_mayer
                         * It's up to each machine implementation to check
5234 28c5af54 j_mayer
                         * if the given boot devices match the actual hardware
5235 28c5af54 j_mayer
                         * implementation and firmware features.
5236 28c5af54 j_mayer
                         */
5237 28c5af54 j_mayer
                        if (*p < 'a' || *p > 'q') {
5238 28c5af54 j_mayer
                            fprintf(stderr, "Invalid boot device '%c'\n", *p);
5239 28c5af54 j_mayer
                            exit(1);
5240 28c5af54 j_mayer
                        }
5241 28c5af54 j_mayer
                        if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5242 28c5af54 j_mayer
                            fprintf(stderr,
5243 28c5af54 j_mayer
                                    "Boot device '%c' was given twice\n",*p);
5244 28c5af54 j_mayer
                            exit(1);
5245 28c5af54 j_mayer
                        }
5246 28c5af54 j_mayer
                        boot_devices_bitmap |= 1 << (*p - 'a');
5247 28c5af54 j_mayer
                    }
5248 36b486bb bellard
                }
5249 36b486bb bellard
                break;
5250 cd6f1169 bellard
            case QEMU_OPTION_fda:
5251 cd6f1169 bellard
            case QEMU_OPTION_fdb:
5252 609497ab balrog
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
5253 c45886db bellard
                break;
5254 52ca8d6a bellard
#ifdef TARGET_I386
5255 52ca8d6a bellard
            case QEMU_OPTION_no_fd_bootchk:
5256 52ca8d6a bellard
                fd_bootchk = 0;
5257 52ca8d6a bellard
                break;
5258 52ca8d6a bellard
#endif
5259 7c9d8e07 bellard
            case QEMU_OPTION_net:
5260 7c9d8e07 bellard
                if (nb_net_clients >= MAX_NET_CLIENTS) {
5261 7c9d8e07 bellard
                    fprintf(stderr, "qemu: too many network clients\n");
5262 c4b1fcc0 bellard
                    exit(1);
5263 c4b1fcc0 bellard
                }
5264 fd5f393a pbrook
                net_clients[nb_net_clients] = optarg;
5265 7c9d8e07 bellard
                nb_net_clients++;
5266 702c651c bellard
                break;
5267 c7f74643 bellard
#ifdef CONFIG_SLIRP
5268 c7f74643 bellard
            case QEMU_OPTION_tftp:
5269 c7f74643 bellard
                tftp_prefix = optarg;
5270 9bf05444 bellard
                break;
5271 47d5d01a ths
            case QEMU_OPTION_bootp:
5272 47d5d01a ths
                bootp_filename = optarg;
5273 47d5d01a ths
                break;
5274 c94c8d64 bellard
#ifndef _WIN32
5275 9d728e8c bellard
            case QEMU_OPTION_smb:
5276 9d728e8c bellard
                net_slirp_smb(optarg);
5277 9d728e8c bellard
                break;
5278 c94c8d64 bellard
#endif
5279 9bf05444 bellard
            case QEMU_OPTION_redir:
5280 c1261d8d Alexander Graf
                net_slirp_redir(NULL, optarg, NULL);
5281 9bf05444 bellard
                break;
5282 c7f74643 bellard
#endif
5283 dc72ac14 balrog
            case QEMU_OPTION_bt:
5284 dc72ac14 balrog
                if (nb_bt_opts >= MAX_BT_CMDLINE) {
5285 dc72ac14 balrog
                    fprintf(stderr, "qemu: too many bluetooth options\n");
5286 dc72ac14 balrog
                    exit(1);
5287 dc72ac14 balrog
                }
5288 dc72ac14 balrog
                bt_opts[nb_bt_opts++] = optarg;
5289 dc72ac14 balrog
                break;
5290 1d14ffa9 bellard
#ifdef HAS_AUDIO
5291 1d14ffa9 bellard
            case QEMU_OPTION_audio_help:
5292 1d14ffa9 bellard
                AUD_help ();
5293 1d14ffa9 bellard
                exit (0);
5294 1d14ffa9 bellard
                break;
5295 1d14ffa9 bellard
            case QEMU_OPTION_soundhw:
5296 1d14ffa9 bellard
                select_soundhw (optarg);
5297 1d14ffa9 bellard
                break;
5298 1d14ffa9 bellard
#endif
5299 cd6f1169 bellard
            case QEMU_OPTION_h:
5300 15f82208 ths
                help(0);
5301 cd6f1169 bellard
                break;
5302 9bd7e6d9 pbrook
            case QEMU_OPTION_version:
5303 9bd7e6d9 pbrook
                version();
5304 9bd7e6d9 pbrook
                exit(0);
5305 9bd7e6d9 pbrook
                break;
5306 00f82b8a aurel32
            case QEMU_OPTION_m: {
5307 00f82b8a aurel32
                uint64_t value;
5308 00f82b8a aurel32
                char *ptr;
5309 00f82b8a aurel32
5310 00f82b8a aurel32
                value = strtoul(optarg, &ptr, 10);
5311 00f82b8a aurel32
                switch (*ptr) {
5312 00f82b8a aurel32
                case 0: case 'M': case 'm':
5313 00f82b8a aurel32
                    value <<= 20;
5314 00f82b8a aurel32
                    break;
5315 00f82b8a aurel32
                case 'G': case 'g':
5316 00f82b8a aurel32
                    value <<= 30;
5317 00f82b8a aurel32
                    break;
5318 00f82b8a aurel32
                default:
5319 00f82b8a aurel32
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
5320 cd6f1169 bellard
                    exit(1);
5321 cd6f1169 bellard
                }
5322 00f82b8a aurel32
5323 00f82b8a aurel32
                /* On 32-bit hosts, QEMU is limited by virtual address space */
5324 00f82b8a aurel32
                if (value > (2047 << 20)
5325 640f42e4 blueswir1
#ifndef CONFIG_KQEMU
5326 00f82b8a aurel32
                    && HOST_LONG_BITS == 32
5327 00f82b8a aurel32
#endif
5328 00f82b8a aurel32
                    ) {
5329 00f82b8a aurel32
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5330 00f82b8a aurel32
                    exit(1);
5331 00f82b8a aurel32
                }
5332 00f82b8a aurel32
                if (value != (uint64_t)(ram_addr_t)value) {
5333 00f82b8a aurel32
                    fprintf(stderr, "qemu: ram size too large\n");
5334 00f82b8a aurel32
                    exit(1);
5335 00f82b8a aurel32
                }
5336 00f82b8a aurel32
                ram_size = value;
5337 cd6f1169 bellard
                break;
5338 00f82b8a aurel32
            }
5339 cd6f1169 bellard
            case QEMU_OPTION_d:
5340 cd6f1169 bellard
                {
5341 cd6f1169 bellard
                    int mask;
5342 c7cd6a37 blueswir1
                    const CPULogItem *item;
5343 3b46e624 ths
5344 cd6f1169 bellard
                    mask = cpu_str_to_log_mask(optarg);
5345 cd6f1169 bellard
                    if (!mask) {
5346 cd6f1169 bellard
                        printf("Log items (comma separated):\n");
5347 f193c797 bellard
                    for(item = cpu_log_items; item->mask != 0; item++) {
5348 f193c797 bellard
                        printf("%-10s %s\n", item->name, item->help);
5349 f193c797 bellard
                    }
5350 f193c797 bellard
                    exit(1);
5351 cd6f1169 bellard
                    }
5352 cd6f1169 bellard
                    cpu_set_log(mask);
5353 f193c797 bellard
                }
5354 cd6f1169 bellard
                break;
5355 cd6f1169 bellard
            case QEMU_OPTION_s:
5356 59030a8c aliguori
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
5357 cd6f1169 bellard
                break;
5358 59030a8c aliguori
            case QEMU_OPTION_gdb:
5359 59030a8c aliguori
                gdbstub_dev = optarg;
5360 cd6f1169 bellard
                break;
5361 cd6f1169 bellard
            case QEMU_OPTION_L:
5362 5cea8590 Paul Brook
                data_dir = optarg;
5363 cd6f1169 bellard
                break;
5364 1192dad8 j_mayer
            case QEMU_OPTION_bios:
5365 1192dad8 j_mayer
                bios_name = optarg;
5366 1192dad8 j_mayer
                break;
5367 1b530a6d aurel32
            case QEMU_OPTION_singlestep:
5368 1b530a6d aurel32
                singlestep = 1;
5369 1b530a6d aurel32
                break;
5370 cd6f1169 bellard
            case QEMU_OPTION_S:
5371 3c07f8e8 pbrook
                autostart = 0;
5372 cd6f1169 bellard
                break;
5373 5824d651 blueswir1
#ifndef _WIN32
5374 3d11d0eb bellard
            case QEMU_OPTION_k:
5375 3d11d0eb bellard
                keyboard_layout = optarg;
5376 3d11d0eb bellard
                break;
5377 5824d651 blueswir1
#endif
5378 ee22c2f7 bellard
            case QEMU_OPTION_localtime:
5379 ee22c2f7 bellard
                rtc_utc = 0;
5380 ee22c2f7 bellard
                break;
5381 3893c124 malc
            case QEMU_OPTION_vga:
5382 3893c124 malc
                select_vgahw (optarg);
5383 1bfe856e bellard
                break;
5384 5824d651 blueswir1
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
5385 e9b137c2 bellard
            case QEMU_OPTION_g:
5386 e9b137c2 bellard
                {
5387 e9b137c2 bellard
                    const char *p;
5388 e9b137c2 bellard
                    int w, h, depth;
5389 e9b137c2 bellard
                    p = optarg;
5390 e9b137c2 bellard
                    w = strtol(p, (char **)&p, 10);
5391 e9b137c2 bellard
                    if (w <= 0) {
5392 e9b137c2 bellard
                    graphic_error:
5393 e9b137c2 bellard
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
5394 e9b137c2 bellard
                        exit(1);
5395 e9b137c2 bellard
                    }
5396 e9b137c2 bellard
                    if (*p != 'x')
5397 e9b137c2 bellard
                        goto graphic_error;
5398 e9b137c2 bellard
                    p++;
5399 e9b137c2 bellard
                    h = strtol(p, (char **)&p, 10);
5400 e9b137c2 bellard
                    if (h <= 0)
5401 e9b137c2 bellard
                        goto graphic_error;
5402 e9b137c2 bellard
                    if (*p == 'x') {
5403 e9b137c2 bellard
                        p++;
5404 e9b137c2 bellard
                        depth = strtol(p, (char **)&p, 10);
5405 5fafdf24 ths
                        if (depth != 8 && depth != 15 && depth != 16 &&
5406 e9b137c2 bellard
                            depth != 24 && depth != 32)
5407 e9b137c2 bellard
                            goto graphic_error;
5408 e9b137c2 bellard
                    } else if (*p == '\0') {
5409 e9b137c2 bellard
                        depth = graphic_depth;
5410 e9b137c2 bellard
                    } else {
5411 e9b137c2 bellard
                        goto graphic_error;
5412 e9b137c2 bellard
                    }
5413 3b46e624 ths
5414 e9b137c2 bellard
                    graphic_width = w;
5415 e9b137c2 bellard
                    graphic_height = h;
5416 e9b137c2 bellard
                    graphic_depth = depth;
5417 e9b137c2 bellard
                }
5418 e9b137c2 bellard
                break;
5419 5824d651 blueswir1
#endif
5420 20d8a3ed ths
            case QEMU_OPTION_echr:
5421 20d8a3ed ths
                {
5422 20d8a3ed ths
                    char *r;
5423 20d8a3ed ths
                    term_escape_char = strtol(optarg, &r, 0);
5424 20d8a3ed ths
                    if (r == optarg)
5425 20d8a3ed ths
                        printf("Bad argument to echr\n");
5426 20d8a3ed ths
                    break;
5427 20d8a3ed ths
                }
5428 82c643ff bellard
            case QEMU_OPTION_monitor:
5429 fd5f393a pbrook
                monitor_device = optarg;
5430 82c643ff bellard
                break;
5431 82c643ff bellard
            case QEMU_OPTION_serial:
5432 8d11df9e bellard
                if (serial_device_index >= MAX_SERIAL_PORTS) {
5433 8d11df9e bellard
                    fprintf(stderr, "qemu: too many serial ports\n");
5434 8d11df9e bellard
                    exit(1);
5435 8d11df9e bellard
                }
5436 fd5f393a pbrook
                serial_devices[serial_device_index] = optarg;
5437 8d11df9e bellard
                serial_device_index++;
5438 82c643ff bellard
                break;
5439 9dd986cc Richard W.M. Jones
            case QEMU_OPTION_watchdog:
5440 9dd986cc Richard W.M. Jones
                i = select_watchdog(optarg);
5441 9dd986cc Richard W.M. Jones
                if (i > 0)
5442 9dd986cc Richard W.M. Jones
                    exit (i == 1 ? 1 : 0);
5443 9dd986cc Richard W.M. Jones
                break;
5444 9dd986cc Richard W.M. Jones
            case QEMU_OPTION_watchdog_action:
5445 9dd986cc Richard W.M. Jones
                if (select_watchdog_action(optarg) == -1) {
5446 9dd986cc Richard W.M. Jones
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
5447 9dd986cc Richard W.M. Jones
                    exit(1);
5448 9dd986cc Richard W.M. Jones
                }
5449 9dd986cc Richard W.M. Jones
                break;
5450 51ecf136 aliguori
            case QEMU_OPTION_virtiocon:
5451 51ecf136 aliguori
                if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5452 51ecf136 aliguori
                    fprintf(stderr, "qemu: too many virtio consoles\n");
5453 51ecf136 aliguori
                    exit(1);
5454 51ecf136 aliguori
                }
5455 51ecf136 aliguori
                virtio_consoles[virtio_console_index] = optarg;
5456 51ecf136 aliguori
                virtio_console_index++;
5457 51ecf136 aliguori
                break;
5458 6508fe59 bellard
            case QEMU_OPTION_parallel:
5459 6508fe59 bellard
                if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5460 6508fe59 bellard
                    fprintf(stderr, "qemu: too many parallel ports\n");
5461 6508fe59 bellard
                    exit(1);
5462 6508fe59 bellard
                }
5463 fd5f393a pbrook
                parallel_devices[parallel_device_index] = optarg;
5464 6508fe59 bellard
                parallel_device_index++;
5465 6508fe59 bellard
                break;
5466 d63d307f bellard
            case QEMU_OPTION_loadvm:
5467 d63d307f bellard
                loadvm = optarg;
5468 d63d307f bellard
                break;
5469 d63d307f bellard
            case QEMU_OPTION_full_screen:
5470 d63d307f bellard
                full_screen = 1;
5471 d63d307f bellard
                break;
5472 667accab ths
#ifdef CONFIG_SDL
5473 43523e93 ths
            case QEMU_OPTION_no_frame:
5474 43523e93 ths
                no_frame = 1;
5475 43523e93 ths
                break;
5476 3780e197 ths
            case QEMU_OPTION_alt_grab:
5477 3780e197 ths
                alt_grab = 1;
5478 3780e197 ths
                break;
5479 667accab ths
            case QEMU_OPTION_no_quit:
5480 667accab ths
                no_quit = 1;
5481 667accab ths
                break;
5482 7d957bd8 aliguori
            case QEMU_OPTION_sdl:
5483 993fbfdb Anthony Liguori
                display_type = DT_SDL;
5484 7d957bd8 aliguori
                break;
5485 667accab ths
#endif
5486 f7cce898 bellard
            case QEMU_OPTION_pidfile:
5487 93815bc2 ths
                pid_file = optarg;
5488 f7cce898 bellard
                break;
5489 a09db21f bellard
#ifdef TARGET_I386
5490 a09db21f bellard
            case QEMU_OPTION_win2k_hack:
5491 a09db21f bellard
                win2k_install_hack = 1;
5492 a09db21f bellard
                break;
5493 73822ec8 aliguori
            case QEMU_OPTION_rtc_td_hack:
5494 73822ec8 aliguori
                rtc_td_hack = 1;
5495 73822ec8 aliguori
                break;
5496 8a92ea2f aliguori
            case QEMU_OPTION_acpitable:
5497 8a92ea2f aliguori
                if(acpi_table_add(optarg) < 0) {
5498 8a92ea2f aliguori
                    fprintf(stderr, "Wrong acpi table provided\n");
5499 8a92ea2f aliguori
                    exit(1);
5500 8a92ea2f aliguori
                }
5501 8a92ea2f aliguori
                break;
5502 b6f6e3d3 aliguori
            case QEMU_OPTION_smbios:
5503 b6f6e3d3 aliguori
                if(smbios_entry_add(optarg) < 0) {
5504 b6f6e3d3 aliguori
                    fprintf(stderr, "Wrong smbios provided\n");
5505 b6f6e3d3 aliguori
                    exit(1);
5506 b6f6e3d3 aliguori
                }
5507 b6f6e3d3 aliguori
                break;
5508 a09db21f bellard
#endif
5509 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
5510 d993e026 bellard
            case QEMU_OPTION_no_kqemu:
5511 d993e026 bellard
                kqemu_allowed = 0;
5512 d993e026 bellard
                break;
5513 89bfc105 bellard
            case QEMU_OPTION_kernel_kqemu:
5514 89bfc105 bellard
                kqemu_allowed = 2;
5515 89bfc105 bellard
                break;
5516 d993e026 bellard
#endif
5517 7ba1e619 aliguori
#ifdef CONFIG_KVM
5518 7ba1e619 aliguori
            case QEMU_OPTION_enable_kvm:
5519 7ba1e619 aliguori
                kvm_allowed = 1;
5520 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
5521 7ba1e619 aliguori
                kqemu_allowed = 0;
5522 7ba1e619 aliguori
#endif
5523 7ba1e619 aliguori
                break;
5524 7ba1e619 aliguori
#endif
5525 bb36d470 bellard
            case QEMU_OPTION_usb:
5526 bb36d470 bellard
                usb_enabled = 1;
5527 bb36d470 bellard
                break;
5528 a594cfbf bellard
            case QEMU_OPTION_usbdevice:
5529 a594cfbf bellard
                usb_enabled = 1;
5530 0d92ed30 pbrook
                if (usb_devices_index >= MAX_USB_CMDLINE) {
5531 a594cfbf bellard
                    fprintf(stderr, "Too many USB devices\n");
5532 a594cfbf bellard
                    exit(1);
5533 a594cfbf bellard
                }
5534 fd5f393a pbrook
                usb_devices[usb_devices_index] = optarg;
5535 a594cfbf bellard
                usb_devices_index++;
5536 a594cfbf bellard
                break;
5537 6a00d601 bellard
            case QEMU_OPTION_smp:
5538 6a00d601 bellard
                smp_cpus = atoi(optarg);
5539 b2097003 aliguori
                if (smp_cpus < 1) {
5540 6a00d601 bellard
                    fprintf(stderr, "Invalid number of CPUs\n");
5541 6a00d601 bellard
                    exit(1);
5542 6a00d601 bellard
                }
5543 6a00d601 bellard
                break;
5544 24236869 bellard
            case QEMU_OPTION_vnc:
5545 993fbfdb Anthony Liguori
                display_type = DT_VNC;
5546 73fc9742 ths
                vnc_display = optarg;
5547 24236869 bellard
                break;
5548 5824d651 blueswir1
#ifdef TARGET_I386
5549 6515b203 bellard
            case QEMU_OPTION_no_acpi:
5550 6515b203 bellard
                acpi_enabled = 0;
5551 6515b203 bellard
                break;
5552 16b29ae1 aliguori
            case QEMU_OPTION_no_hpet:
5553 16b29ae1 aliguori
                no_hpet = 1;
5554 16b29ae1 aliguori
                break;
5555 df97b920 Eduardo Habkost
            case QEMU_OPTION_no_virtio_balloon:
5556 df97b920 Eduardo Habkost
                no_virtio_balloon = 1;
5557 df97b920 Eduardo Habkost
                break;
5558 5824d651 blueswir1
#endif
5559 d1beab82 bellard
            case QEMU_OPTION_no_reboot:
5560 d1beab82 bellard
                no_reboot = 1;
5561 d1beab82 bellard
                break;
5562 b2f76161 aurel32
            case QEMU_OPTION_no_shutdown:
5563 b2f76161 aurel32
                no_shutdown = 1;
5564 b2f76161 aurel32
                break;
5565 9467cd46 balrog
            case QEMU_OPTION_show_cursor:
5566 9467cd46 balrog
                cursor_hide = 0;
5567 9467cd46 balrog
                break;
5568 8fcb1b90 blueswir1
            case QEMU_OPTION_uuid:
5569 8fcb1b90 blueswir1
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5570 8fcb1b90 blueswir1
                    fprintf(stderr, "Fail to parse UUID string."
5571 8fcb1b90 blueswir1
                            " Wrong format.\n");
5572 8fcb1b90 blueswir1
                    exit(1);
5573 8fcb1b90 blueswir1
                }
5574 8fcb1b90 blueswir1
                break;
5575 5824d651 blueswir1
#ifndef _WIN32
5576 71e3ceb8 ths
            case QEMU_OPTION_daemonize:
5577 71e3ceb8 ths
                daemonize = 1;
5578 71e3ceb8 ths
                break;
5579 5824d651 blueswir1
#endif
5580 9ae02555 ths
            case QEMU_OPTION_option_rom:
5581 9ae02555 ths
                if (nb_option_roms >= MAX_OPTION_ROMS) {
5582 9ae02555 ths
                    fprintf(stderr, "Too many option ROMs\n");
5583 9ae02555 ths
                    exit(1);
5584 9ae02555 ths
                }
5585 9ae02555 ths
                option_rom[nb_option_roms] = optarg;
5586 9ae02555 ths
                nb_option_roms++;
5587 9ae02555 ths
                break;
5588 5824d651 blueswir1
#if defined(TARGET_ARM) || defined(TARGET_M68K)
5589 8e71621f pbrook
            case QEMU_OPTION_semihosting:
5590 8e71621f pbrook
                semihosting_enabled = 1;
5591 8e71621f pbrook
                break;
5592 5824d651 blueswir1
#endif
5593 c35734b2 ths
            case QEMU_OPTION_name:
5594 c35734b2 ths
                qemu_name = optarg;
5595 c35734b2 ths
                break;
5596 95efd11c blueswir1
#if defined(TARGET_SPARC) || defined(TARGET_PPC)
5597 66508601 blueswir1
            case QEMU_OPTION_prom_env:
5598 66508601 blueswir1
                if (nb_prom_envs >= MAX_PROM_ENVS) {
5599 66508601 blueswir1
                    fprintf(stderr, "Too many prom variables\n");
5600 66508601 blueswir1
                    exit(1);
5601 66508601 blueswir1
                }
5602 66508601 blueswir1
                prom_envs[nb_prom_envs] = optarg;
5603 66508601 blueswir1
                nb_prom_envs++;
5604 66508601 blueswir1
                break;
5605 66508601 blueswir1
#endif
5606 2b8f2d41 balrog
#ifdef TARGET_ARM
5607 2b8f2d41 balrog
            case QEMU_OPTION_old_param:
5608 2b8f2d41 balrog
                old_param = 1;
5609 05ebd537 ths
                break;
5610 2b8f2d41 balrog
#endif
5611 f3dcfada ths
            case QEMU_OPTION_clock:
5612 f3dcfada ths
                configure_alarms(optarg);
5613 f3dcfada ths
                break;
5614 7e0af5d0 bellard
            case QEMU_OPTION_startdate:
5615 7e0af5d0 bellard
                {
5616 7e0af5d0 bellard
                    struct tm tm;
5617 f6503059 balrog
                    time_t rtc_start_date;
5618 7e0af5d0 bellard
                    if (!strcmp(optarg, "now")) {
5619 f6503059 balrog
                        rtc_date_offset = -1;
5620 7e0af5d0 bellard
                    } else {
5621 7e0af5d0 bellard
                        if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5622 7e0af5d0 bellard
                               &tm.tm_year,
5623 7e0af5d0 bellard
                               &tm.tm_mon,
5624 7e0af5d0 bellard
                               &tm.tm_mday,
5625 7e0af5d0 bellard
                               &tm.tm_hour,
5626 7e0af5d0 bellard
                               &tm.tm_min,
5627 7e0af5d0 bellard
                               &tm.tm_sec) == 6) {
5628 7e0af5d0 bellard
                            /* OK */
5629 7e0af5d0 bellard
                        } else if (sscanf(optarg, "%d-%d-%d",
5630 7e0af5d0 bellard
                                          &tm.tm_year,
5631 7e0af5d0 bellard
                                          &tm.tm_mon,
5632 7e0af5d0 bellard
                                          &tm.tm_mday) == 3) {
5633 7e0af5d0 bellard
                            tm.tm_hour = 0;
5634 7e0af5d0 bellard
                            tm.tm_min = 0;
5635 7e0af5d0 bellard
                            tm.tm_sec = 0;
5636 7e0af5d0 bellard
                        } else {
5637 7e0af5d0 bellard
                            goto date_fail;
5638 7e0af5d0 bellard
                        }
5639 7e0af5d0 bellard
                        tm.tm_year -= 1900;
5640 7e0af5d0 bellard
                        tm.tm_mon--;
5641 3c6b2088 bellard
                        rtc_start_date = mktimegm(&tm);
5642 7e0af5d0 bellard
                        if (rtc_start_date == -1) {
5643 7e0af5d0 bellard
                        date_fail:
5644 7e0af5d0 bellard
                            fprintf(stderr, "Invalid date format. Valid format are:\n"
5645 7e0af5d0 bellard
                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5646 7e0af5d0 bellard
                            exit(1);
5647 7e0af5d0 bellard
                        }
5648 f6503059 balrog
                        rtc_date_offset = time(NULL) - rtc_start_date;
5649 7e0af5d0 bellard
                    }
5650 7e0af5d0 bellard
                }
5651 7e0af5d0 bellard
                break;
5652 26a5f13b bellard
            case QEMU_OPTION_tb_size:
5653 26a5f13b bellard
                tb_size = strtol(optarg, NULL, 0);
5654 26a5f13b bellard
                if (tb_size < 0)
5655 26a5f13b bellard
                    tb_size = 0;
5656 26a5f13b bellard
                break;
5657 2e70f6ef pbrook
            case QEMU_OPTION_icount:
5658 2e70f6ef pbrook
                use_icount = 1;
5659 2e70f6ef pbrook
                if (strcmp(optarg, "auto") == 0) {
5660 2e70f6ef pbrook
                    icount_time_shift = -1;
5661 2e70f6ef pbrook
                } else {
5662 2e70f6ef pbrook
                    icount_time_shift = strtol(optarg, NULL, 0);
5663 2e70f6ef pbrook
                }
5664 2e70f6ef pbrook
                break;
5665 5bb7910a aliguori
            case QEMU_OPTION_incoming:
5666 5bb7910a aliguori
                incoming = optarg;
5667 5bb7910a aliguori
                break;
5668 5824d651 blueswir1
#ifndef _WIN32
5669 0858532e aliguori
            case QEMU_OPTION_chroot:
5670 0858532e aliguori
                chroot_dir = optarg;
5671 0858532e aliguori
                break;
5672 0858532e aliguori
            case QEMU_OPTION_runas:
5673 0858532e aliguori
                run_as = optarg;
5674 0858532e aliguori
                break;
5675 5824d651 blueswir1
#endif
5676 e37630ca aliguori
#ifdef CONFIG_XEN
5677 e37630ca aliguori
            case QEMU_OPTION_xen_domid:
5678 e37630ca aliguori
                xen_domid = atoi(optarg);
5679 e37630ca aliguori
                break;
5680 e37630ca aliguori
            case QEMU_OPTION_xen_create:
5681 e37630ca aliguori
                xen_mode = XEN_CREATE;
5682 e37630ca aliguori
                break;
5683 e37630ca aliguori
            case QEMU_OPTION_xen_attach:
5684 e37630ca aliguori
                xen_mode = XEN_ATTACH;
5685 e37630ca aliguori
                break;
5686 e37630ca aliguori
#endif
5687 cd6f1169 bellard
            }
5688 0824d6fc bellard
        }
5689 0824d6fc bellard
    }
5690 330d0414 bellard
5691 5cea8590 Paul Brook
    /* If no data_dir is specified then try to find it relative to the
5692 5cea8590 Paul Brook
       executable path.  */
5693 5cea8590 Paul Brook
    if (!data_dir) {
5694 5cea8590 Paul Brook
        data_dir = find_datadir(argv[0]);
5695 5cea8590 Paul Brook
    }
5696 5cea8590 Paul Brook
    /* If all else fails use the install patch specified when building.  */
5697 5cea8590 Paul Brook
    if (!data_dir) {
5698 5cea8590 Paul Brook
        data_dir = CONFIG_QEMU_SHAREDIR;
5699 5cea8590 Paul Brook
    }
5700 5cea8590 Paul Brook
5701 640f42e4 blueswir1
#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5702 7ba1e619 aliguori
    if (kvm_allowed && kqemu_allowed) {
5703 7ba1e619 aliguori
        fprintf(stderr,
5704 7ba1e619 aliguori
                "You can not enable both KVM and kqemu at the same time\n");
5705 7ba1e619 aliguori
        exit(1);
5706 7ba1e619 aliguori
    }
5707 7ba1e619 aliguori
#endif
5708 7ba1e619 aliguori
5709 3d878caa balrog
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5710 b2097003 aliguori
    if (smp_cpus > machine->max_cpus) {
5711 b2097003 aliguori
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5712 b2097003 aliguori
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
5713 b2097003 aliguori
                machine->max_cpus);
5714 b2097003 aliguori
        exit(1);
5715 b2097003 aliguori
    }
5716 b2097003 aliguori
5717 993fbfdb Anthony Liguori
    if (display_type == DT_NOGRAPHIC) {
5718 bc0129d9 aliguori
       if (serial_device_index == 0)
5719 bc0129d9 aliguori
           serial_devices[0] = "stdio";
5720 bc0129d9 aliguori
       if (parallel_device_index == 0)
5721 bc0129d9 aliguori
           parallel_devices[0] = "null";
5722 bc0129d9 aliguori
       if (strncmp(monitor_device, "vc", 2) == 0)
5723 bc0129d9 aliguori
           monitor_device = "stdio";
5724 bc0129d9 aliguori
    }
5725 bc0129d9 aliguori
5726 71e3ceb8 ths
#ifndef _WIN32
5727 71e3ceb8 ths
    if (daemonize) {
5728 71e3ceb8 ths
        pid_t pid;
5729 71e3ceb8 ths
5730 71e3ceb8 ths
        if (pipe(fds) == -1)
5731 71e3ceb8 ths
            exit(1);
5732 71e3ceb8 ths
5733 71e3ceb8 ths
        pid = fork();
5734 71e3ceb8 ths
        if (pid > 0) {
5735 71e3ceb8 ths
            uint8_t status;
5736 71e3ceb8 ths
            ssize_t len;
5737 71e3ceb8 ths
5738 71e3ceb8 ths
            close(fds[1]);
5739 71e3ceb8 ths
5740 71e3ceb8 ths
        again:
5741 93815bc2 ths
            len = read(fds[0], &status, 1);
5742 93815bc2 ths
            if (len == -1 && (errno == EINTR))
5743 93815bc2 ths
                goto again;
5744 93815bc2 ths
5745 93815bc2 ths
            if (len != 1)
5746 93815bc2 ths
                exit(1);
5747 93815bc2 ths
            else if (status == 1) {
5748 93815bc2 ths
                fprintf(stderr, "Could not acquire pidfile\n");
5749 93815bc2 ths
                exit(1);
5750 93815bc2 ths
            } else
5751 93815bc2 ths
                exit(0);
5752 71e3ceb8 ths
        } else if (pid < 0)
5753 93815bc2 ths
            exit(1);
5754 71e3ceb8 ths
5755 71e3ceb8 ths
        setsid();
5756 71e3ceb8 ths
5757 71e3ceb8 ths
        pid = fork();
5758 71e3ceb8 ths
        if (pid > 0)
5759 71e3ceb8 ths
            exit(0);
5760 71e3ceb8 ths
        else if (pid < 0)
5761 71e3ceb8 ths
            exit(1);
5762 71e3ceb8 ths
5763 71e3ceb8 ths
        umask(027);
5764 71e3ceb8 ths
5765 71e3ceb8 ths
        signal(SIGTSTP, SIG_IGN);
5766 71e3ceb8 ths
        signal(SIGTTOU, SIG_IGN);
5767 71e3ceb8 ths
        signal(SIGTTIN, SIG_IGN);
5768 71e3ceb8 ths
    }
5769 71e3ceb8 ths
5770 aa26bb2d ths
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
5771 93815bc2 ths
        if (daemonize) {
5772 93815bc2 ths
            uint8_t status = 1;
5773 93815bc2 ths
            write(fds[1], &status, 1);
5774 93815bc2 ths
        } else
5775 93815bc2 ths
            fprintf(stderr, "Could not acquire pid file\n");
5776 93815bc2 ths
        exit(1);
5777 93815bc2 ths
    }
5778 b9e82a59 blueswir1
#endif
5779 93815bc2 ths
5780 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
5781 ff3fbb30 bellard
    if (smp_cpus > 1)
5782 ff3fbb30 bellard
        kqemu_allowed = 0;
5783 ff3fbb30 bellard
#endif
5784 3fcf7b6b aliguori
    if (qemu_init_main_loop()) {
5785 3fcf7b6b aliguori
        fprintf(stderr, "qemu_init_main_loop failed\n");
5786 3fcf7b6b aliguori
        exit(1);
5787 3fcf7b6b aliguori
    }
5788 a20dd508 bellard
    linux_boot = (kernel_filename != NULL);
5789 7317b8ca balrog
    net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5790 6c41b272 balrog
5791 f8d39c01 ths
    if (!linux_boot && *kernel_cmdline != '\0') {
5792 f8d39c01 ths
        fprintf(stderr, "-append only allowed with -kernel option\n");
5793 f8d39c01 ths
        exit(1);
5794 f8d39c01 ths
    }
5795 f8d39c01 ths
5796 f8d39c01 ths
    if (!linux_boot && initrd_filename != NULL) {
5797 f8d39c01 ths
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
5798 f8d39c01 ths
        exit(1);
5799 f8d39c01 ths
    }
5800 f8d39c01 ths
5801 96d30e48 ths
    /* boot to floppy or the default cd if no hard disk defined yet */
5802 28c5af54 j_mayer
    if (!boot_devices[0]) {
5803 e4bcb14c ths
        boot_devices = "cad";
5804 96d30e48 ths
    }
5805 b118d61e bellard
    setvbuf(stdout, NULL, _IOLBF, 0);
5806 3b46e624 ths
5807 634fce96 pbrook
    init_timers();
5808 7183b4b4 aliguori
    if (init_timer_alarm() < 0) {
5809 7183b4b4 aliguori
        fprintf(stderr, "could not initialize alarm timer\n");
5810 7183b4b4 aliguori
        exit(1);
5811 7183b4b4 aliguori
    }
5812 2e70f6ef pbrook
    if (use_icount && icount_time_shift < 0) {
5813 2e70f6ef pbrook
        use_icount = 2;
5814 2e70f6ef pbrook
        /* 125MIPS seems a reasonable initial guess at the guest speed.
5815 2e70f6ef pbrook
           It will be corrected fairly quickly anyway.  */
5816 2e70f6ef pbrook
        icount_time_shift = 3;
5817 2e70f6ef pbrook
        init_icount_adjust();
5818 2e70f6ef pbrook
    }
5819 634fce96 pbrook
5820 fd1dff4b bellard
#ifdef _WIN32
5821 fd1dff4b bellard
    socket_init();
5822 fd1dff4b bellard
#endif
5823 fd1dff4b bellard
5824 7c9d8e07 bellard
    /* init network clients */
5825 7c9d8e07 bellard
    if (nb_net_clients == 0) {
5826 7c9d8e07 bellard
        /* if no clients, we use a default config */
5827 f441b28b aliguori
        net_clients[nb_net_clients++] = "nic";
5828 f441b28b aliguori
#ifdef CONFIG_SLIRP
5829 f441b28b aliguori
        net_clients[nb_net_clients++] = "user";
5830 f441b28b aliguori
#endif
5831 c20709aa bellard
    }
5832 c20709aa bellard
5833 7c9d8e07 bellard
    for(i = 0;i < nb_net_clients; i++) {
5834 9ad97e65 balrog
        if (net_client_parse(net_clients[i]) < 0)
5835 7c9d8e07 bellard
            exit(1);
5836 702c651c bellard
    }
5837 63a01ef8 aliguori
    net_client_check();
5838 f1510b2c bellard
5839 eec85c2a ths
#ifdef TARGET_I386
5840 ed494d87 balrog
    /* XXX: this should be moved in the PC machine instantiation code */
5841 28c5af54 j_mayer
    if (net_boot != 0) {
5842 28c5af54 j_mayer
        int netroms = 0;
5843 28c5af54 j_mayer
        for (i = 0; i < nb_nics && i < 4; i++) {
5844 eec85c2a ths
            const char *model = nd_table[i].model;
5845 eec85c2a ths
            char buf[1024];
5846 5cea8590 Paul Brook
            char *filename;
5847 28c5af54 j_mayer
            if (net_boot & (1 << i)) {
5848 28c5af54 j_mayer
                if (model == NULL)
5849 28c5af54 j_mayer
                    model = "ne2k_pci";
5850 5cea8590 Paul Brook
                snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
5851 5cea8590 Paul Brook
                filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
5852 5cea8590 Paul Brook
                if (filename && get_image_size(filename) > 0) {
5853 28c5af54 j_mayer
                    if (nb_option_roms >= MAX_OPTION_ROMS) {
5854 28c5af54 j_mayer
                        fprintf(stderr, "Too many option ROMs\n");
5855 28c5af54 j_mayer
                        exit(1);
5856 28c5af54 j_mayer
                    }
5857 5cea8590 Paul Brook
                    option_rom[nb_option_roms] = qemu_strdup(buf);
5858 28c5af54 j_mayer
                    nb_option_roms++;
5859 28c5af54 j_mayer
                    netroms++;
5860 28c5af54 j_mayer
                }
5861 5cea8590 Paul Brook
                if (filename) {
5862 5cea8590 Paul Brook
                    qemu_free(filename);
5863 5cea8590 Paul Brook
                }
5864 28c5af54 j_mayer
            }
5865 eec85c2a ths
        }
5866 28c5af54 j_mayer
        if (netroms == 0) {
5867 eec85c2a ths
            fprintf(stderr, "No valid PXE rom found for network device\n");
5868 eec85c2a ths
            exit(1);
5869 eec85c2a ths
        }
5870 eec85c2a ths
    }
5871 eec85c2a ths
#endif
5872 eec85c2a ths
5873 dc72ac14 balrog
    /* init the bluetooth world */
5874 dc72ac14 balrog
    for (i = 0; i < nb_bt_opts; i++)
5875 dc72ac14 balrog
        if (bt_parse(bt_opts[i]))
5876 dc72ac14 balrog
            exit(1);
5877 dc72ac14 balrog
5878 0824d6fc bellard
    /* init the memory */
5879 94a6b54f pbrook
    if (ram_size == 0)
5880 94a6b54f pbrook
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5881 9ae02555 ths
5882 640f42e4 blueswir1
#ifdef CONFIG_KQEMU
5883 94a6b54f pbrook
    /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5884 94a6b54f pbrook
       guest ram allocation.  It needs to go away.  */
5885 94a6b54f pbrook
    if (kqemu_allowed) {
5886 4cfce484 Paul Brook
        kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
5887 94a6b54f pbrook
        kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5888 94a6b54f pbrook
        if (!kqemu_phys_ram_base) {
5889 94a6b54f pbrook
            fprintf(stderr, "Could not allocate physical memory\n");
5890 94a6b54f pbrook
            exit(1);
5891 94a6b54f pbrook
        }
5892 0824d6fc bellard
    }
5893 94a6b54f pbrook
#endif
5894 0824d6fc bellard
5895 26a5f13b bellard
    /* init the dynamic translator */
5896 26a5f13b bellard
    cpu_exec_init_all(tb_size * 1024 * 1024);
5897 26a5f13b bellard
5898 5905b2e5 bellard
    bdrv_init();
5899 c4b1fcc0 bellard
5900 e4bcb14c ths
    /* we always create the cdrom drive, even if no disk is there */
5901 c4b1fcc0 bellard
5902 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
5903 609497ab balrog
        drive_add(NULL, CDROM_ALIAS);
5904 c4b1fcc0 bellard
5905 9d413d1d balrog
    /* we always create at least one floppy */
5906 33e3963e bellard
5907 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
5908 609497ab balrog
        drive_add(NULL, FD_ALIAS, 0);
5909 86f55663 j_mayer
5910 9d413d1d balrog
    /* we always create one sd slot, even if no card is in it */
5911 9d413d1d balrog
5912 9d413d1d balrog
    if (nb_drives_opt < MAX_DRIVES)
5913 609497ab balrog
        drive_add(NULL, SD_ALIAS);
5914 9d413d1d balrog
5915 e4bcb14c ths
    /* open the virtual block devices */
5916 e4bcb14c ths
5917 e4bcb14c ths
    for(i = 0; i < nb_drives_opt; i++)
5918 609497ab balrog
        if (drive_init(&drives_opt[i], snapshot, machine) == -1)
5919 e4bcb14c ths
            exit(1);
5920 3e3d5815 balrog
5921 c88676f8 bellard
    register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
5922 475e4277 aliguori
    register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
5923 8a7ddc38 bellard
5924 3023f332 aliguori
#ifndef _WIN32
5925 3023f332 aliguori
    /* must be after terminal init, SDL library changes signal handlers */
5926 7c3370d4 Jan Kiszka
    sighandler_setup();
5927 3023f332 aliguori
#endif
5928 3023f332 aliguori
5929 3023f332 aliguori
    /* Maintain compatibility with multiple stdio monitors */
5930 3023f332 aliguori
    if (!strcmp(monitor_device,"stdio")) {
5931 3023f332 aliguori
        for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5932 3023f332 aliguori
            const char *devname = serial_devices[i];
5933 3023f332 aliguori
            if (devname && !strcmp(devname,"mon:stdio")) {
5934 3023f332 aliguori
                monitor_device = NULL;
5935 3023f332 aliguori
                break;
5936 3023f332 aliguori
            } else if (devname && !strcmp(devname,"stdio")) {
5937 3023f332 aliguori
                monitor_device = NULL;
5938 3023f332 aliguori
                serial_devices[i] = "mon:stdio";
5939 3023f332 aliguori
                break;
5940 3023f332 aliguori
            }
5941 3023f332 aliguori
        }
5942 3023f332 aliguori
    }
5943 3023f332 aliguori
5944 268a362c aliguori
    if (nb_numa_nodes > 0) {
5945 268a362c aliguori
        int i;
5946 268a362c aliguori
5947 268a362c aliguori
        if (nb_numa_nodes > smp_cpus) {
5948 268a362c aliguori
            nb_numa_nodes = smp_cpus;
5949 268a362c aliguori
        }
5950 268a362c aliguori
5951 268a362c aliguori
        /* If no memory size if given for any node, assume the default case
5952 268a362c aliguori
         * and distribute the available memory equally across all nodes
5953 268a362c aliguori
         */
5954 268a362c aliguori
        for (i = 0; i < nb_numa_nodes; i++) {
5955 268a362c aliguori
            if (node_mem[i] != 0)
5956 268a362c aliguori
                break;
5957 268a362c aliguori
        }
5958 268a362c aliguori
        if (i == nb_numa_nodes) {
5959 268a362c aliguori
            uint64_t usedmem = 0;
5960 268a362c aliguori
5961 268a362c aliguori
            /* On Linux, the each node's border has to be 8MB aligned,
5962 268a362c aliguori
             * the final node gets the rest.
5963 268a362c aliguori
             */
5964 268a362c aliguori
            for (i = 0; i < nb_numa_nodes - 1; i++) {
5965 268a362c aliguori
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5966 268a362c aliguori
                usedmem += node_mem[i];
5967 268a362c aliguori
            }
5968 268a362c aliguori
            node_mem[i] = ram_size - usedmem;
5969 268a362c aliguori
        }
5970 268a362c aliguori
5971 268a362c aliguori
        for (i = 0; i < nb_numa_nodes; i++) {
5972 268a362c aliguori
            if (node_cpumask[i] != 0)
5973 268a362c aliguori
                break;
5974 268a362c aliguori
        }
5975 268a362c aliguori
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
5976 268a362c aliguori
         * must cope with this anyway, because there are BIOSes out there in
5977 268a362c aliguori
         * real machines which also use this scheme.
5978 268a362c aliguori
         */
5979 268a362c aliguori
        if (i == nb_numa_nodes) {
5980 268a362c aliguori
            for (i = 0; i < smp_cpus; i++) {
5981 268a362c aliguori
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
5982 268a362c aliguori
            }
5983 268a362c aliguori
        }
5984 268a362c aliguori
    }
5985 268a362c aliguori
5986 3023f332 aliguori
    if (kvm_enabled()) {
5987 3023f332 aliguori
        int ret;
5988 3023f332 aliguori
5989 3023f332 aliguori
        ret = kvm_init(smp_cpus);
5990 3023f332 aliguori
        if (ret < 0) {
5991 3023f332 aliguori
            fprintf(stderr, "failed to initialize KVM\n");
5992 3023f332 aliguori
            exit(1);
5993 3023f332 aliguori
        }
5994 3023f332 aliguori
    }
5995 3023f332 aliguori
5996 4c621805 aliguori
    if (monitor_device) {
5997 ceecf1d1 aurel32
        monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
5998 4c621805 aliguori
        if (!monitor_hd) {
5999 4c621805 aliguori
            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
6000 4c621805 aliguori
            exit(1);
6001 4c621805 aliguori
        }
6002 4c621805 aliguori
    }
6003 4c621805 aliguori
6004 2796dae0 aliguori
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6005 2796dae0 aliguori
        const char *devname = serial_devices[i];
6006 2796dae0 aliguori
        if (devname && strcmp(devname, "none")) {
6007 2796dae0 aliguori
            char label[32];
6008 2796dae0 aliguori
            snprintf(label, sizeof(label), "serial%d", i);
6009 ceecf1d1 aurel32
            serial_hds[i] = qemu_chr_open(label, devname, NULL);
6010 2796dae0 aliguori
            if (!serial_hds[i]) {
6011 2796dae0 aliguori
                fprintf(stderr, "qemu: could not open serial device '%s'\n",
6012 2796dae0 aliguori
                        devname);
6013 2796dae0 aliguori
                exit(1);
6014 2796dae0 aliguori
            }
6015 2796dae0 aliguori
        }
6016 2796dae0 aliguori
    }
6017 2796dae0 aliguori
6018 2796dae0 aliguori
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6019 2796dae0 aliguori
        const char *devname = parallel_devices[i];
6020 2796dae0 aliguori
        if (devname && strcmp(devname, "none")) {
6021 2796dae0 aliguori
            char label[32];
6022 2796dae0 aliguori
            snprintf(label, sizeof(label), "parallel%d", i);
6023 ceecf1d1 aurel32
            parallel_hds[i] = qemu_chr_open(label, devname, NULL);
6024 2796dae0 aliguori
            if (!parallel_hds[i]) {
6025 2796dae0 aliguori
                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
6026 2796dae0 aliguori
                        devname);
6027 2796dae0 aliguori
                exit(1);
6028 2796dae0 aliguori
            }
6029 2796dae0 aliguori
        }
6030 2796dae0 aliguori
    }
6031 2796dae0 aliguori
6032 2796dae0 aliguori
    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6033 2796dae0 aliguori
        const char *devname = virtio_consoles[i];
6034 2796dae0 aliguori
        if (devname && strcmp(devname, "none")) {
6035 2796dae0 aliguori
            char label[32];
6036 2796dae0 aliguori
            snprintf(label, sizeof(label), "virtcon%d", i);
6037 ceecf1d1 aurel32
            virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
6038 2796dae0 aliguori
            if (!virtcon_hds[i]) {
6039 2796dae0 aliguori
                fprintf(stderr, "qemu: could not open virtio console '%s'\n",
6040 2796dae0 aliguori
                        devname);
6041 2796dae0 aliguori
                exit(1);
6042 2796dae0 aliguori
            }
6043 2796dae0 aliguori
        }
6044 2796dae0 aliguori
    }
6045 2796dae0 aliguori
6046 aae9460e Paul Brook
    module_call_init(MODULE_INIT_DEVICE);
6047 aae9460e Paul Brook
6048 fbe1b595 Paul Brook
    machine->init(ram_size, boot_devices,
6049 3023f332 aliguori
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6050 3023f332 aliguori
6051 268a362c aliguori
6052 268a362c aliguori
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
6053 268a362c aliguori
        for (i = 0; i < nb_numa_nodes; i++) {
6054 268a362c aliguori
            if (node_cpumask[i] & (1 << env->cpu_index)) {
6055 268a362c aliguori
                env->numa_node = i;
6056 268a362c aliguori
            }
6057 268a362c aliguori
        }
6058 268a362c aliguori
    }
6059 268a362c aliguori
6060 6f338c34 aliguori
    current_machine = machine;
6061 6f338c34 aliguori
6062 3023f332 aliguori
    /* Set KVM's vcpu state to qemu's initial CPUState. */
6063 3023f332 aliguori
    if (kvm_enabled()) {
6064 3023f332 aliguori
        int ret;
6065 3023f332 aliguori
6066 3023f332 aliguori
        ret = kvm_sync_vcpus();
6067 3023f332 aliguori
        if (ret < 0) {
6068 3023f332 aliguori
            fprintf(stderr, "failed to initialize vcpus\n");
6069 3023f332 aliguori
            exit(1);
6070 3023f332 aliguori
        }
6071 3023f332 aliguori
    }
6072 3023f332 aliguori
6073 3023f332 aliguori
    /* init USB devices */
6074 3023f332 aliguori
    if (usb_enabled) {
6075 3023f332 aliguori
        for(i = 0; i < usb_devices_index; i++) {
6076 c0f4ce77 aliguori
            if (usb_device_add(usb_devices[i], 0) < 0) {
6077 3023f332 aliguori
                fprintf(stderr, "Warning: could not add USB device %s\n",
6078 3023f332 aliguori
                        usb_devices[i]);
6079 3023f332 aliguori
            }
6080 3023f332 aliguori
        }
6081 3023f332 aliguori
    }
6082 3023f332 aliguori
6083 8f391ab4 aliguori
    if (!display_state)
6084 8f391ab4 aliguori
        dumb_display_init();
6085 3023f332 aliguori
    /* just use the first displaystate for the moment */
6086 3023f332 aliguori
    ds = display_state;
6087 993fbfdb Anthony Liguori
6088 993fbfdb Anthony Liguori
    if (display_type == DT_DEFAULT) {
6089 993fbfdb Anthony Liguori
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6090 993fbfdb Anthony Liguori
        display_type = DT_SDL;
6091 993fbfdb Anthony Liguori
#else
6092 993fbfdb Anthony Liguori
        display_type = DT_VNC;
6093 993fbfdb Anthony Liguori
        vnc_display = "localhost:0,to=99";
6094 993fbfdb Anthony Liguori
        show_vnc_port = 1;
6095 993fbfdb Anthony Liguori
#endif
6096 993fbfdb Anthony Liguori
    }
6097 993fbfdb Anthony Liguori
        
6098 993fbfdb Anthony Liguori
6099 993fbfdb Anthony Liguori
    switch (display_type) {
6100 993fbfdb Anthony Liguori
    case DT_NOGRAPHIC:
6101 993fbfdb Anthony Liguori
        break;
6102 4d3b6f6e balrog
#if defined(CONFIG_CURSES)
6103 993fbfdb Anthony Liguori
    case DT_CURSES:
6104 993fbfdb Anthony Liguori
        curses_display_init(ds, full_screen);
6105 993fbfdb Anthony Liguori
        break;
6106 4d3b6f6e balrog
#endif
6107 5b0753e0 bellard
#if defined(CONFIG_SDL)
6108 993fbfdb Anthony Liguori
    case DT_SDL:
6109 993fbfdb Anthony Liguori
        sdl_display_init(ds, full_screen, no_frame);
6110 993fbfdb Anthony Liguori
        break;
6111 5b0753e0 bellard
#elif defined(CONFIG_COCOA)
6112 993fbfdb Anthony Liguori
    case DT_SDL:
6113 993fbfdb Anthony Liguori
        cocoa_display_init(ds, full_screen);
6114 993fbfdb Anthony Liguori
        break;
6115 313aa567 bellard
#endif
6116 993fbfdb Anthony Liguori
    case DT_VNC:
6117 993fbfdb Anthony Liguori
        vnc_display_init(ds);
6118 993fbfdb Anthony Liguori
        if (vnc_display_open(ds, vnc_display) < 0)
6119 993fbfdb Anthony Liguori
            exit(1);
6120 f92f8afe Anthony Liguori
6121 993fbfdb Anthony Liguori
        if (show_vnc_port) {
6122 993fbfdb Anthony Liguori
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
6123 f92f8afe Anthony Liguori
        }
6124 993fbfdb Anthony Liguori
        break;
6125 993fbfdb Anthony Liguori
    default:
6126 993fbfdb Anthony Liguori
        break;
6127 313aa567 bellard
    }
6128 7d957bd8 aliguori
    dpy_resize(ds);
6129 5b08fc10 aliguori
6130 3023f332 aliguori
    dcl = ds->listeners;
6131 3023f332 aliguori
    while (dcl != NULL) {
6132 3023f332 aliguori
        if (dcl->dpy_refresh != NULL) {
6133 3023f332 aliguori
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6134 3023f332 aliguori
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
6135 20d8a3ed ths
        }
6136 3023f332 aliguori
        dcl = dcl->next;
6137 20d8a3ed ths
    }
6138 3023f332 aliguori
6139 993fbfdb Anthony Liguori
    if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
6140 9043b62d blueswir1
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6141 9043b62d blueswir1
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6142 9043b62d blueswir1
    }
6143 9043b62d blueswir1
6144 2796dae0 aliguori
    text_consoles_set_display(display_state);
6145 2970a6c9 aliguori
    qemu_chr_initial_reset();
6146 2796dae0 aliguori
6147 4c621805 aliguori
    if (monitor_device && monitor_hd)
6148 cde76ee1 aliguori
        monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
6149 82c643ff bellard
6150 8d11df9e bellard
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6151 c03b0f0f bellard
        const char *devname = serial_devices[i];
6152 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
6153 af3a9031 ths
            if (strstart(devname, "vc", 0))
6154 7ba1260a bellard
                qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
6155 8d11df9e bellard
        }
6156 82c643ff bellard
    }
6157 82c643ff bellard
6158 6508fe59 bellard
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6159 c03b0f0f bellard
        const char *devname = parallel_devices[i];
6160 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
6161 af3a9031 ths
            if (strstart(devname, "vc", 0))
6162 7ba1260a bellard
                qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
6163 6508fe59 bellard
        }
6164 6508fe59 bellard
    }
6165 6508fe59 bellard
6166 9ede2fde aliguori
    for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6167 9ede2fde aliguori
        const char *devname = virtio_consoles[i];
6168 2796dae0 aliguori
        if (virtcon_hds[i] && devname) {
6169 9ede2fde aliguori
            if (strstart(devname, "vc", 0))
6170 9ede2fde aliguori
                qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6171 9ede2fde aliguori
        }
6172 9ede2fde aliguori
    }
6173 9ede2fde aliguori
6174 59030a8c aliguori
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6175 59030a8c aliguori
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6176 59030a8c aliguori
                gdbstub_dev);
6177 59030a8c aliguori
        exit(1);
6178 45669e00 balrog
    }
6179 45669e00 balrog
6180 d63d307f bellard
    if (loadvm)
6181 376253ec aliguori
        do_loadvm(cur_mon, loadvm);
6182 d63d307f bellard
6183 5bb7910a aliguori
    if (incoming) {
6184 5bb7910a aliguori
        autostart = 0; /* fixme how to deal with -daemonize */
6185 5bb7910a aliguori
        qemu_start_incoming_migration(incoming);
6186 5bb7910a aliguori
    }
6187 5bb7910a aliguori
6188 c0f4ce77 aliguori
    if (autostart)
6189 c0f4ce77 aliguori
        vm_start();
6190 ffd843bc ths
6191 b9e82a59 blueswir1
#ifndef _WIN32
6192 71e3ceb8 ths
    if (daemonize) {
6193 71e3ceb8 ths
        uint8_t status = 0;
6194 71e3ceb8 ths
        ssize_t len;
6195 71e3ceb8 ths
6196 71e3ceb8 ths
    again1:
6197 71e3ceb8 ths
        len = write(fds[1], &status, 1);
6198 71e3ceb8 ths
        if (len == -1 && (errno == EINTR))
6199 71e3ceb8 ths
            goto again1;
6200 71e3ceb8 ths
6201 71e3ceb8 ths
        if (len != 1)
6202 71e3ceb8 ths
            exit(1);
6203 71e3ceb8 ths
6204 bd54b863 aliguori
        chdir("/");
6205 aeb30be6 balrog
        TFR(fd = open("/dev/null", O_RDWR));
6206 71e3ceb8 ths
        if (fd == -1)
6207 71e3ceb8 ths
            exit(1);
6208 0858532e aliguori
    }
6209 71e3ceb8 ths
6210 0858532e aliguori
    if (run_as) {
6211 0858532e aliguori
        pwd = getpwnam(run_as);
6212 0858532e aliguori
        if (!pwd) {
6213 0858532e aliguori
            fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6214 0858532e aliguori
            exit(1);
6215 0858532e aliguori
        }
6216 0858532e aliguori
    }
6217 0858532e aliguori
6218 0858532e aliguori
    if (chroot_dir) {
6219 0858532e aliguori
        if (chroot(chroot_dir) < 0) {
6220 0858532e aliguori
            fprintf(stderr, "chroot failed\n");
6221 0858532e aliguori
            exit(1);
6222 0858532e aliguori
        }
6223 0858532e aliguori
        chdir("/");
6224 0858532e aliguori
    }
6225 0858532e aliguori
6226 0858532e aliguori
    if (run_as) {
6227 0858532e aliguori
        if (setgid(pwd->pw_gid) < 0) {
6228 0858532e aliguori
            fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6229 0858532e aliguori
            exit(1);
6230 0858532e aliguori
        }
6231 0858532e aliguori
        if (setuid(pwd->pw_uid) < 0) {
6232 0858532e aliguori
            fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6233 0858532e aliguori
            exit(1);
6234 0858532e aliguori
        }
6235 0858532e aliguori
        if (setuid(0) != -1) {
6236 0858532e aliguori
            fprintf(stderr, "Dropping privileges failed\n");
6237 0858532e aliguori
            exit(1);
6238 0858532e aliguori
        }
6239 0858532e aliguori
    }
6240 0858532e aliguori
6241 0858532e aliguori
    if (daemonize) {
6242 0858532e aliguori
        dup2(fd, 0);
6243 0858532e aliguori
        dup2(fd, 1);
6244 0858532e aliguori
        dup2(fd, 2);
6245 71e3ceb8 ths
6246 0858532e aliguori
        close(fd);
6247 71e3ceb8 ths
    }
6248 b9e82a59 blueswir1
#endif
6249 71e3ceb8 ths
6250 8a7ddc38 bellard
    main_loop();
6251 40c3bac3 bellard
    quit_timers();
6252 63a01ef8 aliguori
    net_cleanup();
6253 b46a8906 ths
6254 0824d6fc bellard
    return 0;
6255 0824d6fc bellard
}