Statistics
| Branch: | Revision:

root / vl.c @ c5947808

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