Statistics
| Branch: | Revision:

root / vl.c @ 59a36a2f

History | View | Annotate | Download (157.7 kB)

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