Statistics
| Branch: | Revision:

root / vl.c @ 151f7749

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