blob: 41042eb8a7bd33db17f79d9c1d6cad9453270a94 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard84f2e8e2007-02-05 20:21:32 +00004 * Copyright (c) 2003-2007 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard67b915a2004-03-31 23:37:16 +000024#include "vl.h"
25
bellard0824d6f2003-06-24 13:42:40 +000026#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000027#include <fcntl.h>
28#include <signal.h>
29#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000030#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000031#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000032#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000033
34#ifndef _WIN32
35#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000036#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <termios.h>
38#include <sys/poll.h>
39#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000040#include <sys/ioctl.h>
41#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000042#include <netinet/in.h>
bellard9d728e82004-09-05 23:09:03 +000043#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000044#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000045#include <sys/select.h>
46#include <arpa/inet.h>
bellard7d3505c2004-05-12 19:32:15 +000047#ifdef _BSD
48#include <sys/stat.h>
bellard83fb7ad2004-07-05 21:25:26 +000049#ifndef __APPLE__
bellard7d3505c2004-05-12 19:32:15 +000050#include <libutil.h>
bellard83fb7ad2004-07-05 21:25:26 +000051#endif
ths5c40d2b2007-06-23 16:03:36 +000052#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
53#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000054#else
bellardec530c82006-04-25 22:36:06 +000055#ifndef __sun__
bellardf1510b22003-06-25 00:07:40 +000056#include <linux/if.h>
57#include <linux/if_tun.h>
bellard7d3505c2004-05-12 19:32:15 +000058#include <pty.h>
59#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000060#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000061
62/* For the benefit of older linux systems which don't supply it,
63 we use a local copy of hpet.h. */
64/* #include <linux/hpet.h> */
65#include "hpet.h"
66
bellarde57a8c02005-11-10 23:58:52 +000067#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000068#include <linux/parport.h>
thsd5d10bc2007-02-17 22:54:49 +000069#else
70#include <sys/stat.h>
71#include <sys/ethernet.h>
72#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000073#include <netinet/arp.h>
74#include <netinet/in.h>
75#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/ip_icmp.h> // must come after ip.h
78#include <netinet/udp.h>
79#include <netinet/tcp.h>
80#include <net/if.h>
81#include <syslog.h>
82#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000083#endif
bellard7d3505c2004-05-12 19:32:15 +000084#endif
thscb4b9762007-09-13 12:39:35 +000085#else
86#include <winsock2.h>
87int inet_aton(const char *cp, struct in_addr *ia);
bellardec530c82006-04-25 22:36:06 +000088#endif
bellard67b915a2004-03-31 23:37:16 +000089
bellardc20709a2004-04-21 23:27:19 +000090#if defined(CONFIG_SLIRP)
91#include "libslirp.h"
92#endif
93
bellard67b915a2004-03-31 23:37:16 +000094#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +000095#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +000096#include <sys/timeb.h>
97#include <windows.h>
98#define getopt_long_only getopt_long
99#define memalign(align, size) malloc(size)
100#endif
101
bellard6ca957f2006-04-30 22:53:25 +0000102#include "qemu_socket.h"
103
bellard73332e52004-04-04 20:22:28 +0000104#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000105#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000106#include <SDL/SDL.h>
bellard96bcd4f2004-07-10 16:26:15 +0000107#endif
bellard73332e52004-04-04 20:22:28 +0000108#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000109
bellard5b0753e2005-03-01 21:37:28 +0000110#ifdef CONFIG_COCOA
111#undef main
112#define main qemu_main
113#endif /* CONFIG_COCOA */
114
bellard0824d6f2003-06-24 13:42:40 +0000115#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000116
bellard8a7ddc32004-03-31 19:00:16 +0000117#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000118
bellard5a671352003-10-01 00:13:48 +0000119#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
thsb46a8902007-10-21 23:20:45 +0000120#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
pbrooka14d6c82006-12-23 15:37:33 +0000121#ifdef __sun__
122#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
123#else
124#define SMBD_COMMAND "/usr/sbin/smbd"
125#endif
bellardf1510b22003-06-25 00:07:40 +0000126
bellard0824d6f2003-06-24 13:42:40 +0000127//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000128//#define DEBUG_IOPORT
bellard330d0412003-07-26 18:11:40 +0000129
bellardbb551fa2004-01-24 13:42:26 +0000130#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
bellard7916e222003-07-01 16:27:45 +0000131
bellard77d4bc32004-05-26 22:13:53 +0000132#ifdef TARGET_PPC
133#define DEFAULT_RAM_SIZE 144
134#else
bellard1bfe8562004-07-08 21:17:50 +0000135#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000136#endif
bellard8a7ddc32004-03-31 19:00:16 +0000137/* in ms */
138#define GUI_REFRESH_INTERVAL 30
bellard313aa562003-08-10 21:52:11 +0000139
pbrook0d92ed32006-05-21 16:30:15 +0000140/* Max number of USB devices that can be specified on the commandline. */
141#define MAX_USB_CMDLINE 8
142
bellard7dea1da2003-11-16 15:59:30 +0000143/* XXX: use a two level table to limit memory usage */
144#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000145
bellard80cabfa2004-03-14 12:20:30 +0000146const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000147const char *bios_name = NULL;
bellard0824d6f2003-06-24 13:42:40 +0000148char phys_ram_file[1024];
bellardc4b1fcc2004-03-14 21:44:30 +0000149void *ioport_opaque[MAX_IOPORTS];
bellardfc01f7e2003-06-30 10:03:06 +0000150IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
151IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
bellardfaea38e2006-08-05 21:31:00 +0000152/* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
153 to store the VM snapshots */
154BlockDriverState *bs_table[MAX_DISKS + 1], *fd_table[MAX_FD];
j_mayer86f55662007-04-24 06:52:59 +0000155BlockDriverState *pflash_table[MAX_PFLASH];
pbrooka1bb27b2007-04-06 16:49:48 +0000156BlockDriverState *sd_bdrv;
balrog3e3d5812007-04-30 02:09:25 +0000157BlockDriverState *mtd_bdrv;
bellardfaea38e2006-08-05 21:31:00 +0000158/* point to the block driver where the snapshots are managed */
159BlockDriverState *bs_snapshots;
bellard313aa562003-08-10 21:52:11 +0000160int vga_ram_size;
161static DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000162int nographic;
bellard3d11d0e2004-12-12 16:56:30 +0000163const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000164int64_t ticks_per_sec;
bellard0ced6582004-05-23 21:06:12 +0000165int ram_size;
bellard80cabfa2004-03-14 12:20:30 +0000166int pit_min_timer_count = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000167int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000168NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000169int vm_running;
bellardee22c2f2004-06-03 12:49:50 +0000170int rtc_utc = 1;
bellard7e0af5d02007-11-07 16:24:33 +0000171int rtc_start_date = -1; /* -1 means now */
bellard1bfe8562004-07-08 21:17:50 +0000172int cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +0000173int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000174#ifdef TARGET_SPARC
175int graphic_width = 1024;
176int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000177int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000178#else
bellard1bfe8562004-07-08 21:17:50 +0000179int graphic_width = 800;
180int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000181int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000182#endif
bellardd63d3072004-10-03 13:29:03 +0000183int full_screen = 0;
ths43523e92007-02-18 18:19:32 +0000184int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000185int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000186CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000187CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000188#ifdef TARGET_I386
189int win2k_install_hack = 0;
190#endif
bellardbb36d472005-11-05 14:22:28 +0000191int usb_enabled = 0;
bellard7c9d8e02005-11-15 22:16:05 +0000192static VLANState *first_vlan;
bellard6a00d602005-11-21 23:25:50 +0000193int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000194const char *vnc_display;
bellardd3e9db92005-12-17 01:27:28 +0000195#if defined(TARGET_SPARC)
bellardba3c64f2005-12-05 20:31:52 +0000196#define MAX_CPUS 16
bellardd3e9db92005-12-17 01:27:28 +0000197#elif defined(TARGET_I386)
198#define MAX_CPUS 255
bellardba3c64f2005-12-05 20:31:52 +0000199#else
bellardd3e9db92005-12-17 01:27:28 +0000200#define MAX_CPUS 1
bellardba3c64f2005-12-05 20:31:52 +0000201#endif
bellard6515b202006-05-03 22:02:44 +0000202int acpi_enabled = 1;
bellard52ca8d62006-06-14 16:03:05 +0000203int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000204int no_reboot = 0;
balrog9467cd42007-05-01 01:34:14 +0000205int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000206int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000207int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000208const char *option_rom[MAX_OPTION_ROMS];
209int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000210int semihosting_enabled = 0;
pbrook3c07f8e2007-01-21 16:47:01 +0000211int autostart = 1;
balrog2b8f2d42007-07-27 22:08:46 +0000212#ifdef TARGET_ARM
213int old_param = 0;
214#endif
thsc35734b2007-03-19 15:17:08 +0000215const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000216int alt_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000217#ifdef TARGET_SPARC
218unsigned int nb_prom_envs = 0;
219const char *prom_envs[MAX_PROM_ENVS];
220#endif
bellard0824d6f2003-06-24 13:42:40 +0000221
balrogaeb30be2007-07-02 15:03:13 +0000222#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
223
bellard0824d6f2003-06-24 13:42:40 +0000224/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000225/* x86 ISA bus support */
226
227target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000228PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000229
bellardc4b1fcc2004-03-14 21:44:30 +0000230uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000231{
232#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000233 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000234#endif
bellardfc01f7e2003-06-30 10:03:06 +0000235 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000236}
237
bellardc4b1fcc2004-03-14 21:44:30 +0000238void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000239{
240#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000241 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000242#endif
243}
244
245/* default is to make two byte accesses */
bellardc4b1fcc2004-03-14 21:44:30 +0000246uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000247{
248 uint32_t data;
bellarddb45c292004-05-12 19:50:26 +0000249 data = ioport_read_table[0][address](ioport_opaque[address], address);
250 address = (address + 1) & (MAX_IOPORTS - 1);
251 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000252 return data;
253}
254
bellardc4b1fcc2004-03-14 21:44:30 +0000255void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000256{
bellarddb45c292004-05-12 19:50:26 +0000257 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
258 address = (address + 1) & (MAX_IOPORTS - 1);
259 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000260}
261
bellardc4b1fcc2004-03-14 21:44:30 +0000262uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000263{
264#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000265 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000266#endif
267 return 0xffffffff;
268}
269
bellardc4b1fcc2004-03-14 21:44:30 +0000270void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000271{
272#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000273 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000274#endif
bellard0824d6f2003-06-24 13:42:40 +0000275}
276
277void init_ioports(void)
278{
279 int i;
280
281 for(i = 0; i < MAX_IOPORTS; i++) {
bellardfc01f7e2003-06-30 10:03:06 +0000282 ioport_read_table[0][i] = default_ioport_readb;
283 ioport_write_table[0][i] = default_ioport_writeb;
284 ioport_read_table[1][i] = default_ioport_readw;
285 ioport_write_table[1][i] = default_ioport_writew;
286 ioport_read_table[2][i] = default_ioport_readl;
287 ioport_write_table[2][i] = default_ioport_writel;
bellard0824d6f2003-06-24 13:42:40 +0000288 }
289}
290
bellardfc01f7e2003-06-30 10:03:06 +0000291/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000292int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000293 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000294{
bellardfc01f7e2003-06-30 10:03:06 +0000295 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000296
bellardc4b1fcc2004-03-14 21:44:30 +0000297 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000298 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000299 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000300 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000301 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000302 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000303 } else {
304 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000305 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000306 }
307 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000308 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000309 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
310 hw_error("register_ioport_read: invalid opaque");
311 ioport_opaque[i] = opaque;
312 }
bellard0824d6f2003-06-24 13:42:40 +0000313 return 0;
314}
315
bellardfc01f7e2003-06-30 10:03:06 +0000316/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000317int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000318 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000319{
bellardfc01f7e2003-06-30 10:03:06 +0000320 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000321
bellardc4b1fcc2004-03-14 21:44:30 +0000322 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000323 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000324 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000325 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000326 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000327 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000328 } else {
329 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000330 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000331 }
332 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000333 ioport_write_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000334 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
ths52f61fd2006-12-22 21:20:52 +0000335 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000336 ioport_opaque[i] = opaque;
337 }
bellardf1510b22003-06-25 00:07:40 +0000338 return 0;
339}
340
bellard69b91032004-05-18 23:05:28 +0000341void isa_unassign_ioport(int start, int length)
342{
343 int i;
344
345 for(i = start; i < start + length; i++) {
346 ioport_read_table[0][i] = default_ioport_readb;
347 ioport_read_table[1][i] = default_ioport_readw;
348 ioport_read_table[2][i] = default_ioport_readl;
349
350 ioport_write_table[0][i] = default_ioport_writeb;
351 ioport_write_table[1][i] = default_ioport_writew;
352 ioport_write_table[2][i] = default_ioport_writel;
353 }
354}
355
bellard20f32282005-01-03 23:36:21 +0000356/***********************************************************/
357
bellardc45886d2004-01-05 00:02:06 +0000358void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000359{
bellardfd872592004-05-12 19:11:15 +0000360#ifdef DEBUG_IOPORT
361 if (loglevel & CPU_LOG_IOPORT)
362 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000363#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000364 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000365#ifdef USE_KQEMU
366 if (env)
367 env->last_io_time = cpu_get_time_fast();
368#endif
bellard0824d6f2003-06-24 13:42:40 +0000369}
370
bellardc45886d2004-01-05 00:02:06 +0000371void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000372{
bellardfd872592004-05-12 19:11:15 +0000373#ifdef DEBUG_IOPORT
374 if (loglevel & CPU_LOG_IOPORT)
375 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000376#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000377 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000378#ifdef USE_KQEMU
379 if (env)
380 env->last_io_time = cpu_get_time_fast();
381#endif
bellard0824d6f2003-06-24 13:42:40 +0000382}
383
bellardc45886d2004-01-05 00:02:06 +0000384void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000385{
bellardfd872592004-05-12 19:11:15 +0000386#ifdef DEBUG_IOPORT
387 if (loglevel & CPU_LOG_IOPORT)
388 fprintf(logfile, "outl: %04x %08x\n", addr, val);
389#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000390 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000391#ifdef USE_KQEMU
392 if (env)
393 env->last_io_time = cpu_get_time_fast();
394#endif
bellard0824d6f2003-06-24 13:42:40 +0000395}
396
bellardc45886d2004-01-05 00:02:06 +0000397int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000398{
bellardfd872592004-05-12 19:11:15 +0000399 int val;
bellardfd872592004-05-12 19:11:15 +0000400 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
401#ifdef DEBUG_IOPORT
402 if (loglevel & CPU_LOG_IOPORT)
403 fprintf(logfile, "inb : %04x %02x\n", addr, val);
404#endif
bellard89bfc102006-02-08 22:46:31 +0000405#ifdef USE_KQEMU
406 if (env)
407 env->last_io_time = cpu_get_time_fast();
408#endif
bellardfd872592004-05-12 19:11:15 +0000409 return val;
bellard0824d6f2003-06-24 13:42:40 +0000410}
411
bellardc45886d2004-01-05 00:02:06 +0000412int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000413{
bellardfd872592004-05-12 19:11:15 +0000414 int val;
bellardfd872592004-05-12 19:11:15 +0000415 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
416#ifdef DEBUG_IOPORT
417 if (loglevel & CPU_LOG_IOPORT)
418 fprintf(logfile, "inw : %04x %04x\n", addr, val);
419#endif
bellard89bfc102006-02-08 22:46:31 +0000420#ifdef USE_KQEMU
421 if (env)
422 env->last_io_time = cpu_get_time_fast();
423#endif
bellardfd872592004-05-12 19:11:15 +0000424 return val;
bellard0824d6f2003-06-24 13:42:40 +0000425}
426
bellardc45886d2004-01-05 00:02:06 +0000427int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000428{
bellardfd872592004-05-12 19:11:15 +0000429 int val;
bellardfd872592004-05-12 19:11:15 +0000430 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
431#ifdef DEBUG_IOPORT
432 if (loglevel & CPU_LOG_IOPORT)
433 fprintf(logfile, "inl : %04x %08x\n", addr, val);
434#endif
bellard89bfc102006-02-08 22:46:31 +0000435#ifdef USE_KQEMU
436 if (env)
437 env->last_io_time = cpu_get_time_fast();
438#endif
bellardfd872592004-05-12 19:11:15 +0000439 return val;
bellard0824d6f2003-06-24 13:42:40 +0000440}
441
442/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000443void hw_error(const char *fmt, ...)
444{
445 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000446 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000447
448 va_start(ap, fmt);
449 fprintf(stderr, "qemu: hardware error: ");
450 vfprintf(stderr, fmt, ap);
451 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000452 for(env = first_cpu; env != NULL; env = env->next_cpu) {
453 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000454#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000455 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000456#else
bellard6a00d602005-11-21 23:25:50 +0000457 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000458#endif
bellard6a00d602005-11-21 23:25:50 +0000459 }
bellard0824d6f2003-06-24 13:42:40 +0000460 va_end(ap);
461 abort();
462}
463
bellard8a7ddc32004-03-31 19:00:16 +0000464/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000465/* keyboard/mouse */
466
467static QEMUPutKBDEvent *qemu_put_kbd_event;
468static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000469static QEMUPutMouseEntry *qemu_put_mouse_event_head;
470static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000471
472void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
473{
474 qemu_put_kbd_event_opaque = opaque;
475 qemu_put_kbd_event = func;
476}
477
ths455204e2007-01-05 16:42:13 +0000478QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
479 void *opaque, int absolute,
480 const char *name)
bellard63066f42004-06-03 18:45:02 +0000481{
ths455204e2007-01-05 16:42:13 +0000482 QEMUPutMouseEntry *s, *cursor;
483
484 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
485 if (!s)
486 return NULL;
487
488 s->qemu_put_mouse_event = func;
489 s->qemu_put_mouse_event_opaque = opaque;
490 s->qemu_put_mouse_event_absolute = absolute;
491 s->qemu_put_mouse_event_name = qemu_strdup(name);
492 s->next = NULL;
493
494 if (!qemu_put_mouse_event_head) {
495 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
496 return s;
497 }
498
499 cursor = qemu_put_mouse_event_head;
500 while (cursor->next != NULL)
501 cursor = cursor->next;
502
503 cursor->next = s;
504 qemu_put_mouse_event_current = s;
505
506 return s;
507}
508
509void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
510{
511 QEMUPutMouseEntry *prev = NULL, *cursor;
512
513 if (!qemu_put_mouse_event_head || entry == NULL)
514 return;
515
516 cursor = qemu_put_mouse_event_head;
517 while (cursor != NULL && cursor != entry) {
518 prev = cursor;
519 cursor = cursor->next;
520 }
521
522 if (cursor == NULL) // does not exist or list empty
523 return;
524 else if (prev == NULL) { // entry is head
525 qemu_put_mouse_event_head = cursor->next;
526 if (qemu_put_mouse_event_current == entry)
527 qemu_put_mouse_event_current = cursor->next;
528 qemu_free(entry->qemu_put_mouse_event_name);
529 qemu_free(entry);
530 return;
531 }
532
533 prev->next = entry->next;
534
535 if (qemu_put_mouse_event_current == entry)
536 qemu_put_mouse_event_current = prev;
537
538 qemu_free(entry->qemu_put_mouse_event_name);
539 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000540}
541
542void kbd_put_keycode(int keycode)
543{
544 if (qemu_put_kbd_event) {
545 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
546 }
547}
548
549void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
550{
ths455204e2007-01-05 16:42:13 +0000551 QEMUPutMouseEvent *mouse_event;
552 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000553 int width;
ths455204e2007-01-05 16:42:13 +0000554
555 if (!qemu_put_mouse_event_current) {
556 return;
557 }
558
559 mouse_event =
560 qemu_put_mouse_event_current->qemu_put_mouse_event;
561 mouse_event_opaque =
562 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
563
564 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000565 if (graphic_rotate) {
566 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
567 width = 0x7fff;
568 else
569 width = graphic_width;
570 mouse_event(mouse_event_opaque,
571 width - dy, dx, dz, buttons_state);
572 } else
573 mouse_event(mouse_event_opaque,
574 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000575 }
576}
577
bellard09b26c52006-04-12 21:09:08 +0000578int kbd_mouse_is_absolute(void)
579{
ths455204e2007-01-05 16:42:13 +0000580 if (!qemu_put_mouse_event_current)
581 return 0;
582
583 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
584}
585
586void do_info_mice(void)
587{
588 QEMUPutMouseEntry *cursor;
589 int index = 0;
590
591 if (!qemu_put_mouse_event_head) {
592 term_printf("No mouse devices connected\n");
593 return;
594 }
595
596 term_printf("Mouse devices available:\n");
597 cursor = qemu_put_mouse_event_head;
598 while (cursor != NULL) {
599 term_printf("%c Mouse #%d: %s\n",
600 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
601 index, cursor->qemu_put_mouse_event_name);
602 index++;
603 cursor = cursor->next;
604 }
605}
606
607void do_mouse_set(int index)
608{
609 QEMUPutMouseEntry *cursor;
610 int i = 0;
611
612 if (!qemu_put_mouse_event_head) {
613 term_printf("No mouse devices connected\n");
614 return;
615 }
616
617 cursor = qemu_put_mouse_event_head;
618 while (cursor != NULL && index != i) {
619 i++;
620 cursor = cursor->next;
621 }
622
623 if (cursor != NULL)
624 qemu_put_mouse_event_current = cursor;
625 else
626 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000627}
628
bellard87858c82003-06-27 12:01:39 +0000629/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000630uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000631{
632 union {
633 uint64_t ll;
634 struct {
635#ifdef WORDS_BIGENDIAN
636 uint32_t high, low;
637#else
638 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000639#endif
bellard87858c82003-06-27 12:01:39 +0000640 } l;
641 } u, res;
642 uint64_t rl, rh;
643
644 u.ll = a;
645 rl = (uint64_t)u.l.low * (uint64_t)b;
646 rh = (uint64_t)u.l.high * (uint64_t)b;
647 rh += (rl >> 32);
648 res.l.high = rh / c;
649 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
650 return res.ll;
651}
652
bellard1dce7c32006-07-13 23:20:22 +0000653/***********************************************************/
654/* real time host monotonic timer */
655
656#define QEMU_TIMER_BASE 1000000000LL
657
658#ifdef WIN32
659
660static int64_t clock_freq;
661
662static void init_get_clock(void)
663{
bellarda8e5ac32006-07-14 09:36:13 +0000664 LARGE_INTEGER freq;
665 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000666 ret = QueryPerformanceFrequency(&freq);
667 if (ret == 0) {
668 fprintf(stderr, "Could not calibrate ticks\n");
669 exit(1);
670 }
671 clock_freq = freq.QuadPart;
672}
673
674static int64_t get_clock(void)
675{
676 LARGE_INTEGER ti;
677 QueryPerformanceCounter(&ti);
678 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
679}
680
681#else
682
683static int use_rt_clock;
684
685static void init_get_clock(void)
686{
687 use_rt_clock = 0;
688#if defined(__linux__)
689 {
690 struct timespec ts;
691 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
692 use_rt_clock = 1;
693 }
694 }
695#endif
696}
697
698static int64_t get_clock(void)
699{
700#if defined(__linux__)
701 if (use_rt_clock) {
702 struct timespec ts;
703 clock_gettime(CLOCK_MONOTONIC, &ts);
704 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000705 } else
bellard1dce7c32006-07-13 23:20:22 +0000706#endif
707 {
708 /* XXX: using gettimeofday leads to problems if the date
709 changes, so it should be avoided. */
710 struct timeval tv;
711 gettimeofday(&tv, NULL);
712 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
713 }
714}
715
716#endif
717
718/***********************************************************/
719/* guest cycle counter */
720
721static int64_t cpu_ticks_prev;
722static int64_t cpu_ticks_offset;
723static int64_t cpu_clock_offset;
724static int cpu_ticks_enabled;
725
726/* return the host CPU cycle counter and handle stop/restart */
727int64_t cpu_get_ticks(void)
728{
729 if (!cpu_ticks_enabled) {
730 return cpu_ticks_offset;
731 } else {
732 int64_t ticks;
733 ticks = cpu_get_real_ticks();
734 if (cpu_ticks_prev > ticks) {
735 /* Note: non increasing ticks may happen if the host uses
736 software suspend */
737 cpu_ticks_offset += cpu_ticks_prev - ticks;
738 }
739 cpu_ticks_prev = ticks;
740 return ticks + cpu_ticks_offset;
741 }
742}
743
744/* return the host CPU monotonic timer and handle stop/restart */
745static int64_t cpu_get_clock(void)
746{
747 int64_t ti;
748 if (!cpu_ticks_enabled) {
749 return cpu_clock_offset;
750 } else {
751 ti = get_clock();
752 return ti + cpu_clock_offset;
753 }
754}
755
756/* enable cpu_get_ticks() */
757void cpu_enable_ticks(void)
758{
759 if (!cpu_ticks_enabled) {
760 cpu_ticks_offset -= cpu_get_real_ticks();
761 cpu_clock_offset -= get_clock();
762 cpu_ticks_enabled = 1;
763 }
764}
765
766/* disable cpu_get_ticks() : the clock is stopped. You must not call
767 cpu_get_ticks() after that. */
768void cpu_disable_ticks(void)
769{
770 if (cpu_ticks_enabled) {
771 cpu_ticks_offset = cpu_get_ticks();
772 cpu_clock_offset = cpu_get_clock();
773 cpu_ticks_enabled = 0;
774 }
775}
776
777/***********************************************************/
778/* timers */
ths5fafdf22007-09-16 21:08:06 +0000779
bellard8a7ddc32004-03-31 19:00:16 +0000780#define QEMU_TIMER_REALTIME 0
781#define QEMU_TIMER_VIRTUAL 1
782
783struct QEMUClock {
784 int type;
785 /* XXX: add frequency */
786};
787
788struct QEMUTimer {
789 QEMUClock *clock;
790 int64_t expire_time;
791 QEMUTimerCB *cb;
792 void *opaque;
793 struct QEMUTimer *next;
794};
795
thsc8994012007-08-19 21:56:03 +0000796struct qemu_alarm_timer {
797 char const *name;
thsefe75412007-08-24 01:36:32 +0000798 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000799
800 int (*start)(struct qemu_alarm_timer *t);
801 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000802 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000803 void *priv;
804};
805
thsefe75412007-08-24 01:36:32 +0000806#define ALARM_FLAG_DYNTICKS 0x1
807
808static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
809{
810 return t->flags & ALARM_FLAG_DYNTICKS;
811}
812
813static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
814{
815 if (!alarm_has_dynticks(t))
816 return;
817
818 t->rearm(t);
819}
820
821/* TODO: MIN_TIMER_REARM_US should be optimized */
822#define MIN_TIMER_REARM_US 250
823
thsc8994012007-08-19 21:56:03 +0000824static struct qemu_alarm_timer *alarm_timer;
825
826#ifdef _WIN32
827
828struct qemu_alarm_win32 {
829 MMRESULT timerId;
830 HANDLE host_alarm;
831 unsigned int period;
832} alarm_win32_data = {0, NULL, -1};
833
834static int win32_start_timer(struct qemu_alarm_timer *t);
835static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000836static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000837
838#else
839
840static int unix_start_timer(struct qemu_alarm_timer *t);
841static void unix_stop_timer(struct qemu_alarm_timer *t);
842
ths231c6582007-08-26 17:29:15 +0000843#ifdef __linux__
844
thsefe75412007-08-24 01:36:32 +0000845static int dynticks_start_timer(struct qemu_alarm_timer *t);
846static void dynticks_stop_timer(struct qemu_alarm_timer *t);
847static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
848
thsc40ec5a2007-08-19 22:09:40 +0000849static int hpet_start_timer(struct qemu_alarm_timer *t);
850static void hpet_stop_timer(struct qemu_alarm_timer *t);
851
thsc8994012007-08-19 21:56:03 +0000852static int rtc_start_timer(struct qemu_alarm_timer *t);
853static void rtc_stop_timer(struct qemu_alarm_timer *t);
854
thsefe75412007-08-24 01:36:32 +0000855#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000856
857#endif /* _WIN32 */
858
859static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000860#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000861#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000862 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
863 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000864 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000865 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000866 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000867 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000868#endif
thsefe75412007-08-24 01:36:32 +0000869 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000870#else
thsefe75412007-08-24 01:36:32 +0000871 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
872 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
873 {"win32", 0, win32_start_timer,
874 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000875#endif
876 {NULL, }
877};
878
thsf3dcfad2007-08-24 01:26:02 +0000879static void show_available_alarms()
880{
881 int i;
882
883 printf("Available alarm timers, in order of precedence:\n");
884 for (i = 0; alarm_timers[i].name; i++)
885 printf("%s\n", alarm_timers[i].name);
886}
887
888static void configure_alarms(char const *opt)
889{
890 int i;
891 int cur = 0;
892 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
893 char *arg;
894 char *name;
895
896 if (!strcmp(opt, "help")) {
897 show_available_alarms();
898 exit(0);
899 }
900
901 arg = strdup(opt);
902
903 /* Reorder the array */
904 name = strtok(arg, ",");
905 while (name) {
906 struct qemu_alarm_timer tmp;
907
balroge2b577e2007-09-17 21:25:20 +0000908 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000909 if (!strcmp(alarm_timers[i].name, name))
910 break;
911 }
912
913 if (i == count) {
914 fprintf(stderr, "Unknown clock %s\n", name);
915 goto next;
916 }
917
918 if (i < cur)
919 /* Ignore */
920 goto next;
921
922 /* Swap */
923 tmp = alarm_timers[i];
924 alarm_timers[i] = alarm_timers[cur];
925 alarm_timers[cur] = tmp;
926
927 cur++;
928next:
929 name = strtok(NULL, ",");
930 }
931
932 free(arg);
933
934 if (cur) {
935 /* Disable remaining timers */
936 for (i = cur; i < count; i++)
937 alarm_timers[i].name = NULL;
938 }
939
940 /* debug */
941 show_available_alarms();
942}
943
bellard8a7ddc32004-03-31 19:00:16 +0000944QEMUClock *rt_clock;
945QEMUClock *vm_clock;
946
947static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000948
949QEMUClock *qemu_new_clock(int type)
950{
951 QEMUClock *clock;
952 clock = qemu_mallocz(sizeof(QEMUClock));
953 if (!clock)
954 return NULL;
955 clock->type = type;
956 return clock;
957}
958
959QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
960{
961 QEMUTimer *ts;
962
963 ts = qemu_mallocz(sizeof(QEMUTimer));
964 ts->clock = clock;
965 ts->cb = cb;
966 ts->opaque = opaque;
967 return ts;
968}
969
970void qemu_free_timer(QEMUTimer *ts)
971{
972 qemu_free(ts);
973}
974
975/* stop a timer, but do not dealloc it */
976void qemu_del_timer(QEMUTimer *ts)
977{
978 QEMUTimer **pt, *t;
979
980 /* NOTE: this code must be signal safe because
981 qemu_timer_expired() can be called from a signal. */
982 pt = &active_timers[ts->clock->type];
983 for(;;) {
984 t = *pt;
985 if (!t)
986 break;
987 if (t == ts) {
988 *pt = t->next;
989 break;
990 }
991 pt = &t->next;
992 }
993}
994
995/* modify the current timer so that it will be fired when current_time
996 >= expire_time. The corresponding callback will be called. */
997void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
998{
999 QEMUTimer **pt, *t;
1000
1001 qemu_del_timer(ts);
1002
1003 /* add the timer in the sorted list */
1004 /* NOTE: this code must be signal safe because
1005 qemu_timer_expired() can be called from a signal. */
1006 pt = &active_timers[ts->clock->type];
1007 for(;;) {
1008 t = *pt;
1009 if (!t)
1010 break;
ths5fafdf22007-09-16 21:08:06 +00001011 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001012 break;
1013 pt = &t->next;
1014 }
1015 ts->expire_time = expire_time;
1016 ts->next = *pt;
1017 *pt = ts;
1018}
1019
1020int qemu_timer_pending(QEMUTimer *ts)
1021{
1022 QEMUTimer *t;
1023 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1024 if (t == ts)
1025 return 1;
1026 }
1027 return 0;
1028}
1029
1030static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1031{
1032 if (!timer_head)
1033 return 0;
1034 return (timer_head->expire_time <= current_time);
1035}
1036
1037static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1038{
1039 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001040
bellard8a7ddc32004-03-31 19:00:16 +00001041 for(;;) {
1042 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001043 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001044 break;
1045 /* remove timer from the list before calling the callback */
1046 *ptimer_head = ts->next;
1047 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001048
bellard8a7ddc32004-03-31 19:00:16 +00001049 /* run the callback (the timer list can be modified) */
1050 ts->cb(ts->opaque);
1051 }
thsefe75412007-08-24 01:36:32 +00001052 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00001053}
1054
1055int64_t qemu_get_clock(QEMUClock *clock)
1056{
1057 switch(clock->type) {
1058 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001059 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001060 default:
1061 case QEMU_TIMER_VIRTUAL:
bellard1dce7c32006-07-13 23:20:22 +00001062 return cpu_get_clock();
bellard8a7ddc32004-03-31 19:00:16 +00001063 }
1064}
1065
bellard1dce7c32006-07-13 23:20:22 +00001066static void init_timers(void)
1067{
1068 init_get_clock();
1069 ticks_per_sec = QEMU_TIMER_BASE;
1070 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1071 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1072}
1073
bellard8a7ddc32004-03-31 19:00:16 +00001074/* save a timer */
1075void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1076{
1077 uint64_t expire_time;
1078
1079 if (qemu_timer_pending(ts)) {
1080 expire_time = ts->expire_time;
1081 } else {
1082 expire_time = -1;
1083 }
1084 qemu_put_be64(f, expire_time);
1085}
1086
1087void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1088{
1089 uint64_t expire_time;
1090
1091 expire_time = qemu_get_be64(f);
1092 if (expire_time != -1) {
1093 qemu_mod_timer(ts, expire_time);
1094 } else {
1095 qemu_del_timer(ts);
1096 }
1097}
1098
1099static void timer_save(QEMUFile *f, void *opaque)
1100{
1101 if (cpu_ticks_enabled) {
1102 hw_error("cannot save state if virtual timers are running");
1103 }
1104 qemu_put_be64s(f, &cpu_ticks_offset);
1105 qemu_put_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001106 qemu_put_be64s(f, &cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001107}
1108
1109static int timer_load(QEMUFile *f, void *opaque, int version_id)
1110{
bellardc88676f2006-08-06 13:36:11 +00001111 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001112 return -EINVAL;
1113 if (cpu_ticks_enabled) {
1114 return -EINVAL;
1115 }
1116 qemu_get_be64s(f, &cpu_ticks_offset);
1117 qemu_get_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001118 if (version_id == 2) {
1119 qemu_get_be64s(f, &cpu_clock_offset);
1120 }
bellard8a7ddc32004-03-31 19:00:16 +00001121 return 0;
1122}
1123
bellard67b915a2004-03-31 23:37:16 +00001124#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001125void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001126 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1127#else
bellard8a7ddc32004-03-31 19:00:16 +00001128static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001129#endif
bellard8a7ddc32004-03-31 19:00:16 +00001130{
bellard02ba45c2004-06-25 14:46:23 +00001131#if 0
1132#define DISP_FREQ 1000
1133 {
1134 static int64_t delta_min = INT64_MAX;
1135 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1136 static int count;
1137 ti = qemu_get_clock(vm_clock);
1138 if (last_clock != 0) {
1139 delta = ti - last_clock;
1140 if (delta < delta_min)
1141 delta_min = delta;
1142 if (delta > delta_max)
1143 delta_max = delta;
1144 delta_cum += delta;
1145 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001146 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001147 muldiv64(delta_min, 1000000, ticks_per_sec),
1148 muldiv64(delta_max, 1000000, ticks_per_sec),
1149 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1150 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1151 count = 0;
1152 delta_min = INT64_MAX;
1153 delta_max = 0;
1154 delta_cum = 0;
1155 }
1156 }
1157 last_clock = ti;
1158 }
1159#endif
thsefe75412007-08-24 01:36:32 +00001160 if (alarm_has_dynticks(alarm_timer) ||
1161 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
bellard8a7ddc32004-03-31 19:00:16 +00001162 qemu_get_clock(vm_clock)) ||
1163 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1164 qemu_get_clock(rt_clock))) {
bellard06d9f2f2006-05-01 13:23:04 +00001165#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001166 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1167 SetEvent(data->host_alarm);
bellard06d9f2f2006-05-01 13:23:04 +00001168#endif
bellard6a00d602005-11-21 23:25:50 +00001169 CPUState *env = cpu_single_env;
1170 if (env) {
1171 /* stop the currently executing cpu because a timer occured */
1172 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001173#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001174 if (env->kqemu_enabled) {
1175 kqemu_cpu_interrupt(env);
1176 }
bellarda332e112005-09-03 17:55:47 +00001177#endif
bellard6a00d602005-11-21 23:25:50 +00001178 }
bellard8a7ddc32004-03-31 19:00:16 +00001179 }
1180}
1181
thsefe75412007-08-24 01:36:32 +00001182static uint64_t qemu_next_deadline(void)
1183{
balrog49a9b722007-10-04 19:47:09 +00001184 int64_t nearest_delta_us = INT64_MAX;
thsefe75412007-08-24 01:36:32 +00001185 int64_t vmdelta_us;
1186
1187 if (active_timers[QEMU_TIMER_REALTIME])
1188 nearest_delta_us = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1189 qemu_get_clock(rt_clock))*1000;
1190
1191 if (active_timers[QEMU_TIMER_VIRTUAL]) {
1192 /* round up */
1193 vmdelta_us = (active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1194 qemu_get_clock(vm_clock)+999)/1000;
1195 if (vmdelta_us < nearest_delta_us)
1196 nearest_delta_us = vmdelta_us;
1197 }
1198
1199 /* Avoid arming the timer to negative, zero, or too low values */
1200 if (nearest_delta_us <= MIN_TIMER_REARM_US)
1201 nearest_delta_us = MIN_TIMER_REARM_US;
1202
1203 return nearest_delta_us;
1204}
1205
bellardfd872592004-05-12 19:11:15 +00001206#ifndef _WIN32
1207
bellard829309c2004-05-20 13:20:12 +00001208#if defined(__linux__)
1209
bellardfd872592004-05-12 19:11:15 +00001210#define RTC_FREQ 1024
1211
thsc8994012007-08-19 21:56:03 +00001212static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001213{
thsc8994012007-08-19 21:56:03 +00001214 struct sigaction act;
1215
1216 /* timer signal */
1217 sigfillset(&act.sa_mask);
1218 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001219 act.sa_handler = host_alarm_handler;
1220
1221 sigaction(SIGIO, &act, NULL);
1222 fcntl(fd, F_SETFL, O_ASYNC);
1223 fcntl(fd, F_SETOWN, getpid());
1224}
1225
thsc40ec5a2007-08-19 22:09:40 +00001226static int hpet_start_timer(struct qemu_alarm_timer *t)
1227{
1228 struct hpet_info info;
1229 int r, fd;
1230
1231 fd = open("/dev/hpet", O_RDONLY);
1232 if (fd < 0)
1233 return -1;
1234
1235 /* Set frequency */
1236 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1237 if (r < 0) {
1238 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1239 "error, but for better emulation accuracy type:\n"
1240 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1241 goto fail;
1242 }
1243
1244 /* Check capabilities */
1245 r = ioctl(fd, HPET_INFO, &info);
1246 if (r < 0)
1247 goto fail;
1248
1249 /* Enable periodic mode */
1250 r = ioctl(fd, HPET_EPI, 0);
1251 if (info.hi_flags && (r < 0))
1252 goto fail;
1253
1254 /* Enable interrupt */
1255 r = ioctl(fd, HPET_IE_ON, 0);
1256 if (r < 0)
1257 goto fail;
1258
1259 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001260 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001261
1262 return 0;
1263fail:
1264 close(fd);
1265 return -1;
1266}
1267
1268static void hpet_stop_timer(struct qemu_alarm_timer *t)
1269{
pbrookfcdc2122007-08-23 20:22:22 +00001270 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001271
1272 close(fd);
1273}
1274
thsc8994012007-08-19 21:56:03 +00001275static int rtc_start_timer(struct qemu_alarm_timer *t)
1276{
1277 int rtc_fd;
1278
balrogaeb30be2007-07-02 15:03:13 +00001279 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001280 if (rtc_fd < 0)
1281 return -1;
1282 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1283 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1284 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1285 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1286 goto fail;
1287 }
1288 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1289 fail:
1290 close(rtc_fd);
1291 return -1;
1292 }
thsc8994012007-08-19 21:56:03 +00001293
1294 enable_sigio_timer(rtc_fd);
1295
pbrookfcdc2122007-08-23 20:22:22 +00001296 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001297
bellardfd872592004-05-12 19:11:15 +00001298 return 0;
1299}
1300
thsc8994012007-08-19 21:56:03 +00001301static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001302{
pbrookfcdc2122007-08-23 20:22:22 +00001303 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001304
1305 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001306}
1307
thsefe75412007-08-24 01:36:32 +00001308static int dynticks_start_timer(struct qemu_alarm_timer *t)
1309{
1310 struct sigevent ev;
1311 timer_t host_timer;
1312 struct sigaction act;
1313
1314 sigfillset(&act.sa_mask);
1315 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001316 act.sa_handler = host_alarm_handler;
1317
1318 sigaction(SIGALRM, &act, NULL);
1319
1320 ev.sigev_value.sival_int = 0;
1321 ev.sigev_notify = SIGEV_SIGNAL;
1322 ev.sigev_signo = SIGALRM;
1323
1324 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1325 perror("timer_create");
1326
1327 /* disable dynticks */
1328 fprintf(stderr, "Dynamic Ticks disabled\n");
1329
1330 return -1;
1331 }
1332
1333 t->priv = (void *)host_timer;
1334
1335 return 0;
1336}
1337
1338static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1339{
1340 timer_t host_timer = (timer_t)t->priv;
1341
1342 timer_delete(host_timer);
1343}
1344
1345static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1346{
1347 timer_t host_timer = (timer_t)t->priv;
1348 struct itimerspec timeout;
1349 int64_t nearest_delta_us = INT64_MAX;
1350 int64_t current_us;
1351
1352 if (!active_timers[QEMU_TIMER_REALTIME] &&
1353 !active_timers[QEMU_TIMER_VIRTUAL])
1354 return;
1355
1356 nearest_delta_us = qemu_next_deadline();
1357
1358 /* check whether a timer is already running */
1359 if (timer_gettime(host_timer, &timeout)) {
1360 perror("gettime");
1361 fprintf(stderr, "Internal timer error: aborting\n");
1362 exit(1);
1363 }
1364 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1365 if (current_us && current_us <= nearest_delta_us)
1366 return;
1367
1368 timeout.it_interval.tv_sec = 0;
1369 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1370 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1371 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1372 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1373 perror("settime");
1374 fprintf(stderr, "Internal timer error: aborting\n");
1375 exit(1);
1376 }
1377}
1378
ths70744b32007-08-26 17:31:30 +00001379#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001380
thsc8994012007-08-19 21:56:03 +00001381static int unix_start_timer(struct qemu_alarm_timer *t)
1382{
1383 struct sigaction act;
1384 struct itimerval itv;
1385 int err;
1386
1387 /* timer signal */
1388 sigfillset(&act.sa_mask);
1389 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001390 act.sa_handler = host_alarm_handler;
1391
1392 sigaction(SIGALRM, &act, NULL);
1393
1394 itv.it_interval.tv_sec = 0;
1395 /* for i386 kernel 2.6 to get 1 ms */
1396 itv.it_interval.tv_usec = 999;
1397 itv.it_value.tv_sec = 0;
1398 itv.it_value.tv_usec = 10 * 1000;
1399
1400 err = setitimer(ITIMER_REAL, &itv, NULL);
1401 if (err)
1402 return -1;
1403
1404 return 0;
1405}
1406
1407static void unix_stop_timer(struct qemu_alarm_timer *t)
1408{
1409 struct itimerval itv;
1410
1411 memset(&itv, 0, sizeof(itv));
1412 setitimer(ITIMER_REAL, &itv, NULL);
1413}
1414
bellard829309c2004-05-20 13:20:12 +00001415#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001416
thsc8994012007-08-19 21:56:03 +00001417#ifdef _WIN32
1418
1419static int win32_start_timer(struct qemu_alarm_timer *t)
1420{
1421 TIMECAPS tc;
1422 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001423 UINT flags;
thsc8994012007-08-19 21:56:03 +00001424
1425 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1426 if (!data->host_alarm) {
1427 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001428 return -1;
thsc8994012007-08-19 21:56:03 +00001429 }
1430
1431 memset(&tc, 0, sizeof(tc));
1432 timeGetDevCaps(&tc, sizeof(tc));
1433
1434 if (data->period < tc.wPeriodMin)
1435 data->period = tc.wPeriodMin;
1436
1437 timeBeginPeriod(data->period);
1438
thsefe75412007-08-24 01:36:32 +00001439 flags = TIME_CALLBACK_FUNCTION;
1440 if (alarm_has_dynticks(t))
1441 flags |= TIME_ONESHOT;
1442 else
1443 flags |= TIME_PERIODIC;
1444
thsc8994012007-08-19 21:56:03 +00001445 data->timerId = timeSetEvent(1, // interval (ms)
1446 data->period, // resolution
1447 host_alarm_handler, // function
1448 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001449 flags);
thsc8994012007-08-19 21:56:03 +00001450
1451 if (!data->timerId) {
1452 perror("Failed to initialize win32 alarm timer");
1453
1454 timeEndPeriod(data->period);
1455 CloseHandle(data->host_alarm);
1456 return -1;
1457 }
1458
1459 qemu_add_wait_object(data->host_alarm, NULL, NULL);
1460
1461 return 0;
1462}
1463
1464static void win32_stop_timer(struct qemu_alarm_timer *t)
1465{
1466 struct qemu_alarm_win32 *data = t->priv;
1467
1468 timeKillEvent(data->timerId);
1469 timeEndPeriod(data->period);
1470
1471 CloseHandle(data->host_alarm);
1472}
1473
thsefe75412007-08-24 01:36:32 +00001474static void win32_rearm_timer(struct qemu_alarm_timer *t)
1475{
1476 struct qemu_alarm_win32 *data = t->priv;
1477 uint64_t nearest_delta_us;
1478
1479 if (!active_timers[QEMU_TIMER_REALTIME] &&
1480 !active_timers[QEMU_TIMER_VIRTUAL])
1481 return;
1482
1483 nearest_delta_us = qemu_next_deadline();
1484 nearest_delta_us /= 1000;
1485
1486 timeKillEvent(data->timerId);
1487
1488 data->timerId = timeSetEvent(1,
1489 data->period,
1490 host_alarm_handler,
1491 (DWORD)t,
1492 TIME_ONESHOT | TIME_PERIODIC);
1493
1494 if (!data->timerId) {
1495 perror("Failed to re-arm win32 alarm timer");
1496
1497 timeEndPeriod(data->period);
1498 CloseHandle(data->host_alarm);
1499 exit(1);
1500 }
1501}
1502
thsc8994012007-08-19 21:56:03 +00001503#endif /* _WIN32 */
1504
bellard1dce7c32006-07-13 23:20:22 +00001505static void init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001506{
thsc8994012007-08-19 21:56:03 +00001507 struct qemu_alarm_timer *t;
1508 int i, err = -1;
bellard06d9f2f2006-05-01 13:23:04 +00001509
thsc8994012007-08-19 21:56:03 +00001510 for (i = 0; alarm_timers[i].name; i++) {
1511 t = &alarm_timers[i];
1512
thsc8994012007-08-19 21:56:03 +00001513 err = t->start(t);
1514 if (!err)
1515 break;
bellard67b915a2004-03-31 23:37:16 +00001516 }
bellardfd872592004-05-12 19:11:15 +00001517
thsc8994012007-08-19 21:56:03 +00001518 if (err) {
1519 fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1520 fprintf(stderr, "Terminating\n");
1521 exit(1);
bellard67b915a2004-03-31 23:37:16 +00001522 }
thsc8994012007-08-19 21:56:03 +00001523
1524 alarm_timer = t;
bellard8a7ddc32004-03-31 19:00:16 +00001525}
1526
bellard40c3bac2004-04-04 12:56:28 +00001527void quit_timers(void)
1528{
thsc8994012007-08-19 21:56:03 +00001529 alarm_timer->stop(alarm_timer);
1530 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001531}
1532
bellardc4b1fcc2004-03-14 21:44:30 +00001533/***********************************************************/
bellard82c643f2004-07-14 17:28:13 +00001534/* character device */
bellardc45886d2004-01-05 00:02:06 +00001535
pbrooke5b0bc42007-01-27 23:46:43 +00001536static void qemu_chr_event(CharDriverState *s, int event)
1537{
1538 if (!s->chr_event)
1539 return;
1540 s->chr_event(s->handler_opaque, event);
1541}
1542
ths86e94de2007-01-05 22:01:59 +00001543static void qemu_chr_reset_bh(void *opaque)
1544{
1545 CharDriverState *s = opaque;
pbrooke5b0bc42007-01-27 23:46:43 +00001546 qemu_chr_event(s, CHR_EVENT_RESET);
ths86e94de2007-01-05 22:01:59 +00001547 qemu_bh_delete(s->bh);
1548 s->bh = NULL;
1549}
1550
1551void qemu_chr_reset(CharDriverState *s)
1552{
1553 if (s->bh == NULL) {
1554 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1555 qemu_bh_schedule(s->bh);
1556 }
1557}
1558
bellard82c643f2004-07-14 17:28:13 +00001559int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
bellard67b915a2004-03-31 23:37:16 +00001560{
bellard82c643f2004-07-14 17:28:13 +00001561 return s->chr_write(s, buf, len);
bellard67b915a2004-03-31 23:37:16 +00001562}
1563
bellarde57a8c02005-11-10 23:58:52 +00001564int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00001565{
bellarde57a8c02005-11-10 23:58:52 +00001566 if (!s->chr_ioctl)
1567 return -ENOTSUP;
1568 return s->chr_ioctl(s, cmd, arg);
bellardf8d179e2005-11-08 22:30:36 +00001569}
1570
pbrooke5b0bc42007-01-27 23:46:43 +00001571int qemu_chr_can_read(CharDriverState *s)
1572{
1573 if (!s->chr_can_read)
1574 return 0;
1575 return s->chr_can_read(s->handler_opaque);
1576}
1577
1578void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1579{
1580 s->chr_read(s->handler_opaque, buf, len);
1581}
1582
1583
bellard82c643f2004-07-14 17:28:13 +00001584void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
bellard0824d6f2003-06-24 13:42:40 +00001585{
bellard82c643f2004-07-14 17:28:13 +00001586 char buf[4096];
1587 va_list ap;
1588 va_start(ap, fmt);
1589 vsnprintf(buf, sizeof(buf), fmt, ap);
1590 qemu_chr_write(s, buf, strlen(buf));
1591 va_end(ap);
1592}
1593
bellard5905b2e2004-08-01 21:53:26 +00001594void qemu_chr_send_event(CharDriverState *s, int event)
1595{
1596 if (s->chr_send_event)
1597 s->chr_send_event(s, event);
1598}
1599
ths5fafdf22007-09-16 21:08:06 +00001600void qemu_chr_add_handlers(CharDriverState *s,
1601 IOCanRWHandler *fd_can_read,
pbrooke5b0bc42007-01-27 23:46:43 +00001602 IOReadHandler *fd_read,
1603 IOEventHandler *fd_event,
1604 void *opaque)
bellard82c643f2004-07-14 17:28:13 +00001605{
pbrooke5b0bc42007-01-27 23:46:43 +00001606 s->chr_can_read = fd_can_read;
1607 s->chr_read = fd_read;
1608 s->chr_event = fd_event;
1609 s->handler_opaque = opaque;
1610 if (s->chr_update_read_handler)
1611 s->chr_update_read_handler(s);
bellard82c643f2004-07-14 17:28:13 +00001612}
ths3b46e622007-09-17 08:09:54 +00001613
bellard82c643f2004-07-14 17:28:13 +00001614static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1615{
1616 return len;
1617}
1618
ths52f61fd2006-12-22 21:20:52 +00001619static CharDriverState *qemu_chr_open_null(void)
bellard82c643f2004-07-14 17:28:13 +00001620{
1621 CharDriverState *chr;
1622
1623 chr = qemu_mallocz(sizeof(CharDriverState));
1624 if (!chr)
1625 return NULL;
1626 chr->chr_write = null_chr_write;
bellard82c643f2004-07-14 17:28:13 +00001627 return chr;
1628}
1629
ths20d8a3e2007-02-18 17:04:49 +00001630/* MUX driver for serial I/O splitting */
1631static int term_timestamps;
1632static int64_t term_timestamps_start;
ths9c1de612007-02-21 17:25:30 +00001633#define MAX_MUX 4
ths20d8a3e2007-02-18 17:04:49 +00001634typedef struct {
1635 IOCanRWHandler *chr_can_read[MAX_MUX];
1636 IOReadHandler *chr_read[MAX_MUX];
1637 IOEventHandler *chr_event[MAX_MUX];
1638 void *ext_opaque[MAX_MUX];
1639 CharDriverState *drv;
1640 int mux_cnt;
1641 int term_got_escape;
1642 int max_size;
1643} MuxDriver;
1644
1645
1646static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1647{
1648 MuxDriver *d = chr->opaque;
1649 int ret;
1650 if (!term_timestamps) {
1651 ret = d->drv->chr_write(d->drv, buf, len);
1652 } else {
1653 int i;
1654
1655 ret = 0;
1656 for(i = 0; i < len; i++) {
1657 ret += d->drv->chr_write(d->drv, buf+i, 1);
1658 if (buf[i] == '\n') {
1659 char buf1[64];
1660 int64_t ti;
1661 int secs;
1662
1663 ti = get_clock();
1664 if (term_timestamps_start == -1)
1665 term_timestamps_start = ti;
1666 ti -= term_timestamps_start;
1667 secs = ti / 1000000000;
1668 snprintf(buf1, sizeof(buf1),
1669 "[%02d:%02d:%02d.%03d] ",
1670 secs / 3600,
1671 (secs / 60) % 60,
1672 secs % 60,
1673 (int)((ti / 1000000) % 1000));
1674 d->drv->chr_write(d->drv, buf1, strlen(buf1));
1675 }
1676 }
1677 }
1678 return ret;
1679}
1680
1681static char *mux_help[] = {
1682 "% h print this help\n\r",
1683 "% x exit emulator\n\r",
1684 "% s save disk data back to file (if -snapshot)\n\r",
1685 "% t toggle console timestamps\n\r"
1686 "% b send break (magic sysrq)\n\r",
1687 "% c switch between console and monitor\n\r",
1688 "% % sends %\n\r",
1689 NULL
1690};
1691
1692static int term_escape_char = 0x01; /* ctrl-a is used for escape */
1693static void mux_print_help(CharDriverState *chr)
1694{
1695 int i, j;
1696 char ebuf[15] = "Escape-Char";
1697 char cbuf[50] = "\n\r";
1698
1699 if (term_escape_char > 0 && term_escape_char < 26) {
1700 sprintf(cbuf,"\n\r");
1701 sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a');
1702 } else {
1703 sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char);
1704 }
1705 chr->chr_write(chr, cbuf, strlen(cbuf));
1706 for (i = 0; mux_help[i] != NULL; i++) {
1707 for (j=0; mux_help[i][j] != '\0'; j++) {
1708 if (mux_help[i][j] == '%')
1709 chr->chr_write(chr, ebuf, strlen(ebuf));
1710 else
1711 chr->chr_write(chr, &mux_help[i][j], 1);
1712 }
1713 }
1714}
1715
1716static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1717{
1718 if (d->term_got_escape) {
1719 d->term_got_escape = 0;
1720 if (ch == term_escape_char)
1721 goto send_char;
1722 switch(ch) {
1723 case '?':
1724 case 'h':
1725 mux_print_help(chr);
1726 break;
1727 case 'x':
1728 {
1729 char *term = "QEMU: Terminated\n\r";
1730 chr->chr_write(chr,term,strlen(term));
1731 exit(0);
1732 break;
1733 }
1734 case 's':
1735 {
1736 int i;
1737 for (i = 0; i < MAX_DISKS; i++) {
1738 if (bs_table[i])
1739 bdrv_commit(bs_table[i]);
1740 }
balrog2dc7b602007-05-24 18:53:22 +00001741 if (mtd_bdrv)
1742 bdrv_commit(mtd_bdrv);
ths20d8a3e2007-02-18 17:04:49 +00001743 }
1744 break;
1745 case 'b':
balrog36ddb832007-05-18 17:46:59 +00001746 qemu_chr_event(chr, CHR_EVENT_BREAK);
ths20d8a3e2007-02-18 17:04:49 +00001747 break;
1748 case 'c':
1749 /* Switch to the next registered device */
1750 chr->focus++;
1751 if (chr->focus >= d->mux_cnt)
1752 chr->focus = 0;
1753 break;
1754 case 't':
1755 term_timestamps = !term_timestamps;
1756 term_timestamps_start = -1;
1757 break;
1758 }
1759 } else if (ch == term_escape_char) {
1760 d->term_got_escape = 1;
1761 } else {
1762 send_char:
1763 return 1;
1764 }
1765 return 0;
1766}
1767
1768static int mux_chr_can_read(void *opaque)
1769{
1770 CharDriverState *chr = opaque;
1771 MuxDriver *d = chr->opaque;
1772 if (d->chr_can_read[chr->focus])
1773 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
1774 return 0;
1775}
1776
1777static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
1778{
1779 CharDriverState *chr = opaque;
1780 MuxDriver *d = chr->opaque;
1781 int i;
1782 for(i = 0; i < size; i++)
1783 if (mux_proc_byte(chr, d, buf[i]))
1784 d->chr_read[chr->focus](d->ext_opaque[chr->focus], &buf[i], 1);
1785}
1786
1787static void mux_chr_event(void *opaque, int event)
1788{
1789 CharDriverState *chr = opaque;
1790 MuxDriver *d = chr->opaque;
1791 int i;
1792
1793 /* Send the event to all registered listeners */
1794 for (i = 0; i < d->mux_cnt; i++)
1795 if (d->chr_event[i])
1796 d->chr_event[i](d->ext_opaque[i], event);
1797}
1798
1799static void mux_chr_update_read_handler(CharDriverState *chr)
1800{
1801 MuxDriver *d = chr->opaque;
1802
1803 if (d->mux_cnt >= MAX_MUX) {
1804 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
1805 return;
1806 }
1807 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
1808 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
1809 d->chr_read[d->mux_cnt] = chr->chr_read;
1810 d->chr_event[d->mux_cnt] = chr->chr_event;
1811 /* Fix up the real driver with mux routines */
1812 if (d->mux_cnt == 0) {
1813 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
1814 mux_chr_event, chr);
1815 }
1816 chr->focus = d->mux_cnt;
1817 d->mux_cnt++;
1818}
1819
1820CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
1821{
1822 CharDriverState *chr;
1823 MuxDriver *d;
1824
1825 chr = qemu_mallocz(sizeof(CharDriverState));
1826 if (!chr)
1827 return NULL;
1828 d = qemu_mallocz(sizeof(MuxDriver));
1829 if (!d) {
1830 free(chr);
1831 return NULL;
1832 }
1833
1834 chr->opaque = d;
1835 d->drv = drv;
1836 chr->focus = -1;
1837 chr->chr_write = mux_chr_write;
1838 chr->chr_update_read_handler = mux_chr_update_read_handler;
1839 return chr;
1840}
1841
1842
bellardfd1dff42006-02-01 21:29:26 +00001843#ifdef _WIN32
bellard82c643f2004-07-14 17:28:13 +00001844
bellardfd1dff42006-02-01 21:29:26 +00001845static void socket_cleanup(void)
1846{
1847 WSACleanup();
1848}
bellard82c643f2004-07-14 17:28:13 +00001849
bellardfd1dff42006-02-01 21:29:26 +00001850static int socket_init(void)
1851{
1852 WSADATA Data;
1853 int ret, err;
1854
1855 ret = WSAStartup(MAKEWORD(2,2), &Data);
1856 if (ret != 0) {
1857 err = WSAGetLastError();
1858 fprintf(stderr, "WSAStartup: %d\n", err);
1859 return -1;
1860 }
1861 atexit(socket_cleanup);
1862 return 0;
1863}
1864
1865static int send_all(int fd, const uint8_t *buf, int len1)
1866{
1867 int ret, len;
ths3b46e622007-09-17 08:09:54 +00001868
bellardfd1dff42006-02-01 21:29:26 +00001869 len = len1;
1870 while (len > 0) {
1871 ret = send(fd, buf, len, 0);
1872 if (ret < 0) {
1873 int errno;
1874 errno = WSAGetLastError();
1875 if (errno != WSAEWOULDBLOCK) {
1876 return -1;
1877 }
1878 } else if (ret == 0) {
1879 break;
1880 } else {
1881 buf += ret;
1882 len -= ret;
1883 }
1884 }
1885 return len1 - len;
1886}
1887
1888void socket_set_nonblock(int fd)
1889{
1890 unsigned long opt = 1;
1891 ioctlsocket(fd, FIONBIO, &opt);
1892}
1893
1894#else
1895
bellard1d969052004-09-18 19:34:39 +00001896static int unix_write(int fd, const uint8_t *buf, int len1)
1897{
1898 int ret, len;
1899
1900 len = len1;
1901 while (len > 0) {
1902 ret = write(fd, buf, len);
1903 if (ret < 0) {
1904 if (errno != EINTR && errno != EAGAIN)
1905 return -1;
1906 } else if (ret == 0) {
1907 break;
1908 } else {
1909 buf += ret;
1910 len -= ret;
1911 }
1912 }
1913 return len1 - len;
1914}
1915
bellardfd1dff42006-02-01 21:29:26 +00001916static inline int send_all(int fd, const uint8_t *buf, int len1)
1917{
1918 return unix_write(fd, buf, len1);
1919}
1920
1921void socket_set_nonblock(int fd)
1922{
1923 fcntl(fd, F_SETFL, O_NONBLOCK);
1924}
1925#endif /* !_WIN32 */
1926
1927#ifndef _WIN32
1928
1929typedef struct {
1930 int fd_in, fd_out;
bellardfd1dff42006-02-01 21:29:26 +00001931 int max_size;
1932} FDCharDriver;
1933
ths20d8a3e2007-02-18 17:04:49 +00001934#define STDIO_MAX_CLIENTS 1
1935static int stdio_nb_clients = 0;
bellardfd1dff42006-02-01 21:29:26 +00001936
bellard82c643f2004-07-14 17:28:13 +00001937static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1938{
1939 FDCharDriver *s = chr->opaque;
bellard1d969052004-09-18 19:34:39 +00001940 return unix_write(s->fd_out, buf, len);
bellard82c643f2004-07-14 17:28:13 +00001941}
1942
bellard7c9d8e02005-11-15 22:16:05 +00001943static int fd_chr_read_poll(void *opaque)
1944{
1945 CharDriverState *chr = opaque;
1946 FDCharDriver *s = chr->opaque;
1947
pbrooke5b0bc42007-01-27 23:46:43 +00001948 s->max_size = qemu_chr_can_read(chr);
bellard7c9d8e02005-11-15 22:16:05 +00001949 return s->max_size;
1950}
1951
1952static void fd_chr_read(void *opaque)
1953{
1954 CharDriverState *chr = opaque;
1955 FDCharDriver *s = chr->opaque;
1956 int size, len;
1957 uint8_t buf[1024];
ths3b46e622007-09-17 08:09:54 +00001958
bellard7c9d8e02005-11-15 22:16:05 +00001959 len = sizeof(buf);
1960 if (len > s->max_size)
1961 len = s->max_size;
1962 if (len == 0)
1963 return;
1964 size = read(s->fd_in, buf, len);
pbrook188157f2006-11-01 01:44:16 +00001965 if (size == 0) {
1966 /* FD has been closed. Remove it from the active list. */
1967 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
1968 return;
1969 }
bellard7c9d8e02005-11-15 22:16:05 +00001970 if (size > 0) {
pbrooke5b0bc42007-01-27 23:46:43 +00001971 qemu_chr_read(chr, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00001972 }
1973}
1974
pbrooke5b0bc42007-01-27 23:46:43 +00001975static void fd_chr_update_read_handler(CharDriverState *chr)
bellard82c643f2004-07-14 17:28:13 +00001976{
1977 FDCharDriver *s = chr->opaque;
1978
bellardf8d179e2005-11-08 22:30:36 +00001979 if (s->fd_in >= 0) {
1980 if (nographic && s->fd_in == 0) {
bellardf8d179e2005-11-08 22:30:36 +00001981 } else {
ths5fafdf22007-09-16 21:08:06 +00001982 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
bellard7c9d8e02005-11-15 22:16:05 +00001983 fd_chr_read, NULL, chr);
bellardf8d179e2005-11-08 22:30:36 +00001984 }
bellard0824d6f2003-06-24 13:42:40 +00001985 }
1986}
1987
bellard82c643f2004-07-14 17:28:13 +00001988/* open a character device to a unix fd */
ths52f61fd2006-12-22 21:20:52 +00001989static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
bellard82c643f2004-07-14 17:28:13 +00001990{
1991 CharDriverState *chr;
1992 FDCharDriver *s;
1993
1994 chr = qemu_mallocz(sizeof(CharDriverState));
1995 if (!chr)
1996 return NULL;
1997 s = qemu_mallocz(sizeof(FDCharDriver));
1998 if (!s) {
1999 free(chr);
2000 return NULL;
2001 }
2002 s->fd_in = fd_in;
2003 s->fd_out = fd_out;
2004 chr->opaque = s;
2005 chr->chr_write = fd_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002006 chr->chr_update_read_handler = fd_chr_update_read_handler;
ths86e94de2007-01-05 22:01:59 +00002007
2008 qemu_chr_reset(chr);
2009
bellard82c643f2004-07-14 17:28:13 +00002010 return chr;
2011}
2012
ths52f61fd2006-12-22 21:20:52 +00002013static CharDriverState *qemu_chr_open_file_out(const char *file_out)
bellardf8d179e2005-11-08 22:30:36 +00002014{
2015 int fd_out;
2016
balrogaeb30be2007-07-02 15:03:13 +00002017 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
bellardf8d179e2005-11-08 22:30:36 +00002018 if (fd_out < 0)
2019 return NULL;
2020 return qemu_chr_open_fd(-1, fd_out);
2021}
2022
ths52f61fd2006-12-22 21:20:52 +00002023static CharDriverState *qemu_chr_open_pipe(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002024{
thsc26c1c42006-12-22 19:25:31 +00002025 int fd_in, fd_out;
2026 char filename_in[256], filename_out[256];
bellardf8d179e2005-11-08 22:30:36 +00002027
thsc26c1c42006-12-22 19:25:31 +00002028 snprintf(filename_in, 256, "%s.in", filename);
2029 snprintf(filename_out, 256, "%s.out", filename);
balrogaeb30be2007-07-02 15:03:13 +00002030 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2031 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002032 if (fd_in < 0 || fd_out < 0) {
2033 if (fd_in >= 0)
2034 close(fd_in);
2035 if (fd_out >= 0)
2036 close(fd_out);
balrogaeb30be2007-07-02 15:03:13 +00002037 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002038 if (fd_in < 0)
2039 return NULL;
2040 }
2041 return qemu_chr_open_fd(fd_in, fd_out);
bellardf8d179e2005-11-08 22:30:36 +00002042}
2043
2044
bellard82c643f2004-07-14 17:28:13 +00002045/* for STDIO, we handle the case where several clients use it
2046 (nographic mode) */
2047
bellardaa0bc6b2005-09-03 15:28:58 +00002048#define TERM_FIFO_MAX_SIZE 1
2049
bellardaa0bc6b2005-09-03 15:28:58 +00002050static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
bellard1dce7c32006-07-13 23:20:22 +00002051static int term_fifo_size;
bellard82c643f2004-07-14 17:28:13 +00002052
bellard7c9d8e02005-11-15 22:16:05 +00002053static int stdio_read_poll(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002054{
ths20d8a3e2007-02-18 17:04:49 +00002055 CharDriverState *chr = opaque;
bellardaa0bc6b2005-09-03 15:28:58 +00002056
ths20d8a3e2007-02-18 17:04:49 +00002057 /* try to flush the queue if needed */
2058 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2059 qemu_chr_read(chr, term_fifo, 1);
2060 term_fifo_size = 0;
bellardaa0bc6b2005-09-03 15:28:58 +00002061 }
ths20d8a3e2007-02-18 17:04:49 +00002062 /* see if we can absorb more chars */
2063 if (term_fifo_size == 0)
2064 return 1;
2065 else
2066 return 0;
bellard82c643f2004-07-14 17:28:13 +00002067}
2068
bellard7c9d8e02005-11-15 22:16:05 +00002069static void stdio_read(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002070{
bellard7c9d8e02005-11-15 22:16:05 +00002071 int size;
2072 uint8_t buf[1];
ths20d8a3e2007-02-18 17:04:49 +00002073 CharDriverState *chr = opaque;
2074
bellard7c9d8e02005-11-15 22:16:05 +00002075 size = read(0, buf, 1);
pbrook519945d2006-09-10 14:39:54 +00002076 if (size == 0) {
2077 /* stdin has been closed. Remove it from the active list. */
2078 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2079 return;
2080 }
ths20d8a3e2007-02-18 17:04:49 +00002081 if (size > 0) {
2082 if (qemu_chr_can_read(chr) > 0) {
2083 qemu_chr_read(chr, buf, 1);
2084 } else if (term_fifo_size == 0) {
2085 term_fifo[term_fifo_size++] = buf[0];
bellard1dce7c32006-07-13 23:20:22 +00002086 }
bellard1dce7c32006-07-13 23:20:22 +00002087 }
2088}
2089
bellard8d11df92004-08-24 21:13:40 +00002090/* init terminal so that we can grab keys */
2091static struct termios oldtty;
2092static int old_fd0_flags;
2093
2094static void term_exit(void)
2095{
2096 tcsetattr (0, TCSANOW, &oldtty);
2097 fcntl(0, F_SETFL, old_fd0_flags);
2098}
2099
2100static void term_init(void)
2101{
2102 struct termios tty;
2103
2104 tcgetattr (0, &tty);
2105 oldtty = tty;
2106 old_fd0_flags = fcntl(0, F_GETFL);
2107
2108 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2109 |INLCR|IGNCR|ICRNL|IXON);
2110 tty.c_oflag |= OPOST;
2111 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2112 /* if graphical mode, we allow Ctrl-C handling */
2113 if (nographic)
2114 tty.c_lflag &= ~ISIG;
2115 tty.c_cflag &= ~(CSIZE|PARENB);
2116 tty.c_cflag |= CS8;
2117 tty.c_cc[VMIN] = 1;
2118 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +00002119
bellard8d11df92004-08-24 21:13:40 +00002120 tcsetattr (0, TCSANOW, &tty);
2121
2122 atexit(term_exit);
2123
2124 fcntl(0, F_SETFL, O_NONBLOCK);
2125}
2126
ths52f61fd2006-12-22 21:20:52 +00002127static CharDriverState *qemu_chr_open_stdio(void)
bellard82c643f2004-07-14 17:28:13 +00002128{
2129 CharDriverState *chr;
2130
ths20d8a3e2007-02-18 17:04:49 +00002131 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2132 return NULL;
2133 chr = qemu_chr_open_fd(0, 1);
2134 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2135 stdio_nb_clients++;
2136 term_init();
2137
bellard82c643f2004-07-14 17:28:13 +00002138 return chr;
2139}
2140
thsaec62502007-06-25 11:48:07 +00002141#if defined(__linux__) || defined(__sun__)
ths52f61fd2006-12-22 21:20:52 +00002142static CharDriverState *qemu_chr_open_pty(void)
bellard82c643f2004-07-14 17:28:13 +00002143{
bellard91fc2112005-12-18 19:09:37 +00002144 struct termios tty;
bellard82c643f2004-07-14 17:28:13 +00002145 char slave_name[1024];
2146 int master_fd, slave_fd;
ths3b46e622007-09-17 08:09:54 +00002147
thsaec62502007-06-25 11:48:07 +00002148#if defined(__linux__)
bellard82c643f2004-07-14 17:28:13 +00002149 /* Not satisfying */
2150 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
2151 return NULL;
2152 }
thsaec62502007-06-25 11:48:07 +00002153#endif
ths3b46e622007-09-17 08:09:54 +00002154
bellard91fc2112005-12-18 19:09:37 +00002155 /* Disabling local echo and line-buffered output */
2156 tcgetattr (master_fd, &tty);
2157 tty.c_lflag &= ~(ECHO|ICANON|ISIG);
2158 tty.c_cc[VMIN] = 1;
2159 tty.c_cc[VTIME] = 0;
2160 tcsetattr (master_fd, TCSAFLUSH, &tty);
2161
bellard82c643f2004-07-14 17:28:13 +00002162 fprintf(stderr, "char device redirected to %s\n", slave_name);
2163 return qemu_chr_open_fd(master_fd, master_fd);
2164}
bellardf8d179e2005-11-08 22:30:36 +00002165
ths5fafdf22007-09-16 21:08:06 +00002166static void tty_serial_init(int fd, int speed,
bellardf8d179e2005-11-08 22:30:36 +00002167 int parity, int data_bits, int stop_bits)
2168{
2169 struct termios tty;
2170 speed_t spd;
2171
bellarde57a8c02005-11-10 23:58:52 +00002172#if 0
ths5fafdf22007-09-16 21:08:06 +00002173 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
bellarde57a8c02005-11-10 23:58:52 +00002174 speed, parity, data_bits, stop_bits);
2175#endif
2176 tcgetattr (fd, &tty);
bellardf8d179e2005-11-08 22:30:36 +00002177
2178 switch(speed) {
2179 case 50:
2180 spd = B50;
2181 break;
2182 case 75:
2183 spd = B75;
2184 break;
2185 case 300:
2186 spd = B300;
2187 break;
2188 case 600:
2189 spd = B600;
2190 break;
2191 case 1200:
2192 spd = B1200;
2193 break;
2194 case 2400:
2195 spd = B2400;
2196 break;
2197 case 4800:
2198 spd = B4800;
2199 break;
2200 case 9600:
2201 spd = B9600;
2202 break;
2203 case 19200:
2204 spd = B19200;
2205 break;
2206 case 38400:
2207 spd = B38400;
2208 break;
2209 case 57600:
2210 spd = B57600;
2211 break;
2212 default:
2213 case 115200:
2214 spd = B115200;
2215 break;
2216 }
2217
2218 cfsetispeed(&tty, spd);
2219 cfsetospeed(&tty, spd);
2220
2221 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2222 |INLCR|IGNCR|ICRNL|IXON);
2223 tty.c_oflag |= OPOST;
2224 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
bellard094eed62006-09-09 11:10:18 +00002225 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
bellardf8d179e2005-11-08 22:30:36 +00002226 switch(data_bits) {
2227 default:
2228 case 8:
2229 tty.c_cflag |= CS8;
2230 break;
2231 case 7:
2232 tty.c_cflag |= CS7;
2233 break;
2234 case 6:
2235 tty.c_cflag |= CS6;
2236 break;
2237 case 5:
2238 tty.c_cflag |= CS5;
2239 break;
2240 }
2241 switch(parity) {
2242 default:
2243 case 'N':
2244 break;
2245 case 'E':
2246 tty.c_cflag |= PARENB;
2247 break;
2248 case 'O':
2249 tty.c_cflag |= PARENB | PARODD;
2250 break;
2251 }
bellard094eed62006-09-09 11:10:18 +00002252 if (stop_bits == 2)
2253 tty.c_cflag |= CSTOPB;
ths3b46e622007-09-17 08:09:54 +00002254
bellardf8d179e2005-11-08 22:30:36 +00002255 tcsetattr (fd, TCSANOW, &tty);
2256}
2257
bellarde57a8c02005-11-10 23:58:52 +00002258static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00002259{
2260 FDCharDriver *s = chr->opaque;
ths3b46e622007-09-17 08:09:54 +00002261
bellarde57a8c02005-11-10 23:58:52 +00002262 switch(cmd) {
2263 case CHR_IOCTL_SERIAL_SET_PARAMS:
2264 {
2265 QEMUSerialSetParams *ssp = arg;
ths5fafdf22007-09-16 21:08:06 +00002266 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
bellarde57a8c02005-11-10 23:58:52 +00002267 ssp->data_bits, ssp->stop_bits);
2268 }
2269 break;
2270 case CHR_IOCTL_SERIAL_SET_BREAK:
2271 {
2272 int enable = *(int *)arg;
2273 if (enable)
2274 tcsendbreak(s->fd_in, 1);
2275 }
2276 break;
2277 default:
2278 return -ENOTSUP;
2279 }
2280 return 0;
bellardf8d179e2005-11-08 22:30:36 +00002281}
2282
ths52f61fd2006-12-22 21:20:52 +00002283static CharDriverState *qemu_chr_open_tty(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002284{
2285 CharDriverState *chr;
2286 int fd;
2287
balrogaeb30be2007-07-02 15:03:13 +00002288 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
bellardf8d179e2005-11-08 22:30:36 +00002289 fcntl(fd, F_SETFL, O_NONBLOCK);
2290 tty_serial_init(fd, 115200, 'N', 8, 1);
2291 chr = qemu_chr_open_fd(fd, fd);
balrogaeb30be2007-07-02 15:03:13 +00002292 if (!chr) {
2293 close(fd);
bellardf8d179e2005-11-08 22:30:36 +00002294 return NULL;
balrogaeb30be2007-07-02 15:03:13 +00002295 }
bellarde57a8c02005-11-10 23:58:52 +00002296 chr->chr_ioctl = tty_serial_ioctl;
ths86e94de2007-01-05 22:01:59 +00002297 qemu_chr_reset(chr);
bellarde57a8c02005-11-10 23:58:52 +00002298 return chr;
2299}
thsaec62502007-06-25 11:48:07 +00002300#else /* ! __linux__ && ! __sun__ */
2301static CharDriverState *qemu_chr_open_pty(void)
2302{
2303 return NULL;
2304}
2305#endif /* __linux__ || __sun__ */
bellarde57a8c02005-11-10 23:58:52 +00002306
thsaec62502007-06-25 11:48:07 +00002307#if defined(__linux__)
ths5867c882007-02-17 23:44:43 +00002308typedef struct {
2309 int fd;
2310 int mode;
2311} ParallelCharDriver;
2312
2313static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2314{
2315 if (s->mode != mode) {
2316 int m = mode;
2317 if (ioctl(s->fd, PPSETMODE, &m) < 0)
2318 return 0;
2319 s->mode = mode;
2320 }
2321 return 1;
2322}
2323
bellarde57a8c02005-11-10 23:58:52 +00002324static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2325{
ths5867c882007-02-17 23:44:43 +00002326 ParallelCharDriver *drv = chr->opaque;
2327 int fd = drv->fd;
bellarde57a8c02005-11-10 23:58:52 +00002328 uint8_t b;
2329
2330 switch(cmd) {
2331 case CHR_IOCTL_PP_READ_DATA:
2332 if (ioctl(fd, PPRDATA, &b) < 0)
2333 return -ENOTSUP;
2334 *(uint8_t *)arg = b;
2335 break;
2336 case CHR_IOCTL_PP_WRITE_DATA:
2337 b = *(uint8_t *)arg;
2338 if (ioctl(fd, PPWDATA, &b) < 0)
2339 return -ENOTSUP;
2340 break;
2341 case CHR_IOCTL_PP_READ_CONTROL:
2342 if (ioctl(fd, PPRCONTROL, &b) < 0)
2343 return -ENOTSUP;
ths5867c882007-02-17 23:44:43 +00002344 /* Linux gives only the lowest bits, and no way to know data
2345 direction! For better compatibility set the fixed upper
2346 bits. */
2347 *(uint8_t *)arg = b | 0xc0;
bellarde57a8c02005-11-10 23:58:52 +00002348 break;
2349 case CHR_IOCTL_PP_WRITE_CONTROL:
2350 b = *(uint8_t *)arg;
2351 if (ioctl(fd, PPWCONTROL, &b) < 0)
2352 return -ENOTSUP;
2353 break;
2354 case CHR_IOCTL_PP_READ_STATUS:
2355 if (ioctl(fd, PPRSTATUS, &b) < 0)
2356 return -ENOTSUP;
2357 *(uint8_t *)arg = b;
2358 break;
ths5867c882007-02-17 23:44:43 +00002359 case CHR_IOCTL_PP_EPP_READ_ADDR:
2360 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2361 struct ParallelIOArg *parg = arg;
2362 int n = read(fd, parg->buffer, parg->count);
2363 if (n != parg->count) {
2364 return -EIO;
2365 }
2366 }
2367 break;
2368 case CHR_IOCTL_PP_EPP_READ:
2369 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2370 struct ParallelIOArg *parg = arg;
2371 int n = read(fd, parg->buffer, parg->count);
2372 if (n != parg->count) {
2373 return -EIO;
2374 }
2375 }
2376 break;
2377 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2378 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2379 struct ParallelIOArg *parg = arg;
2380 int n = write(fd, parg->buffer, parg->count);
2381 if (n != parg->count) {
2382 return -EIO;
2383 }
2384 }
2385 break;
2386 case CHR_IOCTL_PP_EPP_WRITE:
2387 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2388 struct ParallelIOArg *parg = arg;
2389 int n = write(fd, parg->buffer, parg->count);
2390 if (n != parg->count) {
2391 return -EIO;
2392 }
2393 }
2394 break;
bellarde57a8c02005-11-10 23:58:52 +00002395 default:
2396 return -ENOTSUP;
2397 }
2398 return 0;
2399}
2400
ths5867c882007-02-17 23:44:43 +00002401static void pp_close(CharDriverState *chr)
2402{
2403 ParallelCharDriver *drv = chr->opaque;
2404 int fd = drv->fd;
2405
2406 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2407 ioctl(fd, PPRELEASE);
2408 close(fd);
2409 qemu_free(drv);
2410}
2411
ths52f61fd2006-12-22 21:20:52 +00002412static CharDriverState *qemu_chr_open_pp(const char *filename)
bellarde57a8c02005-11-10 23:58:52 +00002413{
2414 CharDriverState *chr;
ths5867c882007-02-17 23:44:43 +00002415 ParallelCharDriver *drv;
bellarde57a8c02005-11-10 23:58:52 +00002416 int fd;
2417
balrogaeb30be2007-07-02 15:03:13 +00002418 TFR(fd = open(filename, O_RDWR));
bellarde57a8c02005-11-10 23:58:52 +00002419 if (fd < 0)
2420 return NULL;
2421
2422 if (ioctl(fd, PPCLAIM) < 0) {
2423 close(fd);
2424 return NULL;
2425 }
2426
ths5867c882007-02-17 23:44:43 +00002427 drv = qemu_mallocz(sizeof(ParallelCharDriver));
2428 if (!drv) {
bellarde57a8c02005-11-10 23:58:52 +00002429 close(fd);
2430 return NULL;
2431 }
ths5867c882007-02-17 23:44:43 +00002432 drv->fd = fd;
2433 drv->mode = IEEE1284_MODE_COMPAT;
2434
2435 chr = qemu_mallocz(sizeof(CharDriverState));
2436 if (!chr) {
2437 qemu_free(drv);
2438 close(fd);
2439 return NULL;
2440 }
bellarde57a8c02005-11-10 23:58:52 +00002441 chr->chr_write = null_chr_write;
bellarde57a8c02005-11-10 23:58:52 +00002442 chr->chr_ioctl = pp_ioctl;
ths5867c882007-02-17 23:44:43 +00002443 chr->chr_close = pp_close;
2444 chr->opaque = drv;
ths86e94de2007-01-05 22:01:59 +00002445
2446 qemu_chr_reset(chr);
2447
bellardf8d179e2005-11-08 22:30:36 +00002448 return chr;
2449}
thsaec62502007-06-25 11:48:07 +00002450#endif /* __linux__ */
bellardf8d179e2005-11-08 22:30:36 +00002451
thsaec62502007-06-25 11:48:07 +00002452#else /* _WIN32 */
bellard67b915a2004-03-31 23:37:16 +00002453
bellardf3311102006-04-12 20:21:17 +00002454typedef struct {
bellardf3311102006-04-12 20:21:17 +00002455 int max_size;
2456 HANDLE hcom, hrecv, hsend;
2457 OVERLAPPED orecv, osend;
2458 BOOL fpipe;
2459 DWORD len;
2460} WinCharState;
2461
2462#define NSENDBUF 2048
2463#define NRECVBUF 2048
2464#define MAXCONNECT 1
2465#define NTIMEOUT 5000
2466
2467static int win_chr_poll(void *opaque);
2468static int win_chr_pipe_poll(void *opaque);
2469
ths087f4ae2007-02-10 21:50:42 +00002470static void win_chr_close(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002471{
ths087f4ae2007-02-10 21:50:42 +00002472 WinCharState *s = chr->opaque;
2473
bellardf3311102006-04-12 20:21:17 +00002474 if (s->hsend) {
2475 CloseHandle(s->hsend);
2476 s->hsend = NULL;
2477 }
2478 if (s->hrecv) {
2479 CloseHandle(s->hrecv);
2480 s->hrecv = NULL;
2481 }
2482 if (s->hcom) {
2483 CloseHandle(s->hcom);
2484 s->hcom = NULL;
2485 }
2486 if (s->fpipe)
ths087f4ae2007-02-10 21:50:42 +00002487 qemu_del_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002488 else
ths087f4ae2007-02-10 21:50:42 +00002489 qemu_del_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002490}
2491
ths087f4ae2007-02-10 21:50:42 +00002492static int win_chr_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002493{
2494 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002495 COMMCONFIG comcfg;
2496 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2497 COMSTAT comstat;
2498 DWORD size;
2499 DWORD err;
ths3b46e622007-09-17 08:09:54 +00002500
bellardf3311102006-04-12 20:21:17 +00002501 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2502 if (!s->hsend) {
2503 fprintf(stderr, "Failed CreateEvent\n");
2504 goto fail;
2505 }
2506 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2507 if (!s->hrecv) {
2508 fprintf(stderr, "Failed CreateEvent\n");
2509 goto fail;
2510 }
2511
2512 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2513 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2514 if (s->hcom == INVALID_HANDLE_VALUE) {
2515 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2516 s->hcom = NULL;
2517 goto fail;
2518 }
ths3b46e622007-09-17 08:09:54 +00002519
bellardf3311102006-04-12 20:21:17 +00002520 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2521 fprintf(stderr, "Failed SetupComm\n");
2522 goto fail;
2523 }
ths3b46e622007-09-17 08:09:54 +00002524
bellardf3311102006-04-12 20:21:17 +00002525 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2526 size = sizeof(COMMCONFIG);
2527 GetDefaultCommConfig(filename, &comcfg, &size);
2528 comcfg.dcb.DCBlength = sizeof(DCB);
2529 CommConfigDialog(filename, NULL, &comcfg);
2530
2531 if (!SetCommState(s->hcom, &comcfg.dcb)) {
2532 fprintf(stderr, "Failed SetCommState\n");
2533 goto fail;
2534 }
2535
2536 if (!SetCommMask(s->hcom, EV_ERR)) {
2537 fprintf(stderr, "Failed SetCommMask\n");
2538 goto fail;
2539 }
2540
2541 cto.ReadIntervalTimeout = MAXDWORD;
2542 if (!SetCommTimeouts(s->hcom, &cto)) {
2543 fprintf(stderr, "Failed SetCommTimeouts\n");
2544 goto fail;
2545 }
ths3b46e622007-09-17 08:09:54 +00002546
bellardf3311102006-04-12 20:21:17 +00002547 if (!ClearCommError(s->hcom, &err, &comstat)) {
2548 fprintf(stderr, "Failed ClearCommError\n");
2549 goto fail;
2550 }
ths087f4ae2007-02-10 21:50:42 +00002551 qemu_add_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002552 return 0;
2553
2554 fail:
ths087f4ae2007-02-10 21:50:42 +00002555 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002556 return -1;
2557}
2558
2559static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2560{
2561 WinCharState *s = chr->opaque;
2562 DWORD len, ret, size, err;
2563
2564 len = len1;
2565 ZeroMemory(&s->osend, sizeof(s->osend));
2566 s->osend.hEvent = s->hsend;
2567 while (len > 0) {
2568 if (s->hsend)
2569 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2570 else
2571 ret = WriteFile(s->hcom, buf, len, &size, NULL);
2572 if (!ret) {
2573 err = GetLastError();
2574 if (err == ERROR_IO_PENDING) {
2575 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2576 if (ret) {
2577 buf += size;
2578 len -= size;
2579 } else {
2580 break;
2581 }
2582 } else {
2583 break;
2584 }
2585 } else {
2586 buf += size;
2587 len -= size;
2588 }
2589 }
2590 return len1 - len;
2591}
2592
ths087f4ae2007-02-10 21:50:42 +00002593static int win_chr_read_poll(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002594{
ths087f4ae2007-02-10 21:50:42 +00002595 WinCharState *s = chr->opaque;
2596
2597 s->max_size = qemu_chr_can_read(chr);
bellardf3311102006-04-12 20:21:17 +00002598 return s->max_size;
2599}
pbrooke5b0bc42007-01-27 23:46:43 +00002600
ths087f4ae2007-02-10 21:50:42 +00002601static void win_chr_readfile(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002602{
ths087f4ae2007-02-10 21:50:42 +00002603 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002604 int ret, err;
2605 uint8_t buf[1024];
2606 DWORD size;
ths3b46e622007-09-17 08:09:54 +00002607
bellardf3311102006-04-12 20:21:17 +00002608 ZeroMemory(&s->orecv, sizeof(s->orecv));
2609 s->orecv.hEvent = s->hrecv;
2610 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2611 if (!ret) {
2612 err = GetLastError();
2613 if (err == ERROR_IO_PENDING) {
2614 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2615 }
2616 }
2617
2618 if (size > 0) {
ths087f4ae2007-02-10 21:50:42 +00002619 qemu_chr_read(chr, buf, size);
bellardf3311102006-04-12 20:21:17 +00002620 }
2621}
2622
ths087f4ae2007-02-10 21:50:42 +00002623static void win_chr_read(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002624{
ths087f4ae2007-02-10 21:50:42 +00002625 WinCharState *s = chr->opaque;
2626
bellardf3311102006-04-12 20:21:17 +00002627 if (s->len > s->max_size)
2628 s->len = s->max_size;
2629 if (s->len == 0)
2630 return;
ths3b46e622007-09-17 08:09:54 +00002631
ths087f4ae2007-02-10 21:50:42 +00002632 win_chr_readfile(chr);
bellardf3311102006-04-12 20:21:17 +00002633}
2634
2635static int win_chr_poll(void *opaque)
2636{
ths087f4ae2007-02-10 21:50:42 +00002637 CharDriverState *chr = opaque;
2638 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002639 COMSTAT status;
2640 DWORD comerr;
ths3b46e622007-09-17 08:09:54 +00002641
bellardf3311102006-04-12 20:21:17 +00002642 ClearCommError(s->hcom, &comerr, &status);
2643 if (status.cbInQue > 0) {
2644 s->len = status.cbInQue;
ths087f4ae2007-02-10 21:50:42 +00002645 win_chr_read_poll(chr);
2646 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002647 return 1;
2648 }
2649 return 0;
2650}
2651
ths52f61fd2006-12-22 21:20:52 +00002652static CharDriverState *qemu_chr_open_win(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002653{
2654 CharDriverState *chr;
2655 WinCharState *s;
ths3b46e622007-09-17 08:09:54 +00002656
bellardf3311102006-04-12 20:21:17 +00002657 chr = qemu_mallocz(sizeof(CharDriverState));
2658 if (!chr)
2659 return NULL;
2660 s = qemu_mallocz(sizeof(WinCharState));
2661 if (!s) {
2662 free(chr);
2663 return NULL;
2664 }
2665 chr->opaque = s;
2666 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002667 chr->chr_close = win_chr_close;
2668
ths087f4ae2007-02-10 21:50:42 +00002669 if (win_chr_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002670 free(s);
2671 free(chr);
2672 return NULL;
2673 }
ths86e94de2007-01-05 22:01:59 +00002674 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002675 return chr;
2676}
2677
2678static int win_chr_pipe_poll(void *opaque)
2679{
ths087f4ae2007-02-10 21:50:42 +00002680 CharDriverState *chr = opaque;
2681 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002682 DWORD size;
2683
2684 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2685 if (size > 0) {
2686 s->len = size;
ths087f4ae2007-02-10 21:50:42 +00002687 win_chr_read_poll(chr);
2688 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002689 return 1;
2690 }
2691 return 0;
2692}
2693
ths087f4ae2007-02-10 21:50:42 +00002694static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002695{
ths087f4ae2007-02-10 21:50:42 +00002696 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002697 OVERLAPPED ov;
2698 int ret;
2699 DWORD size;
2700 char openname[256];
ths3b46e622007-09-17 08:09:54 +00002701
bellardf3311102006-04-12 20:21:17 +00002702 s->fpipe = TRUE;
2703
2704 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2705 if (!s->hsend) {
2706 fprintf(stderr, "Failed CreateEvent\n");
2707 goto fail;
2708 }
2709 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2710 if (!s->hrecv) {
2711 fprintf(stderr, "Failed CreateEvent\n");
2712 goto fail;
2713 }
ths3b46e622007-09-17 08:09:54 +00002714
bellardf3311102006-04-12 20:21:17 +00002715 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2716 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2717 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2718 PIPE_WAIT,
2719 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2720 if (s->hcom == INVALID_HANDLE_VALUE) {
2721 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2722 s->hcom = NULL;
2723 goto fail;
2724 }
2725
2726 ZeroMemory(&ov, sizeof(ov));
2727 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2728 ret = ConnectNamedPipe(s->hcom, &ov);
2729 if (ret) {
2730 fprintf(stderr, "Failed ConnectNamedPipe\n");
2731 goto fail;
2732 }
2733
2734 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2735 if (!ret) {
2736 fprintf(stderr, "Failed GetOverlappedResult\n");
2737 if (ov.hEvent) {
2738 CloseHandle(ov.hEvent);
2739 ov.hEvent = NULL;
2740 }
2741 goto fail;
2742 }
2743
2744 if (ov.hEvent) {
2745 CloseHandle(ov.hEvent);
2746 ov.hEvent = NULL;
2747 }
ths087f4ae2007-02-10 21:50:42 +00002748 qemu_add_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002749 return 0;
2750
2751 fail:
ths087f4ae2007-02-10 21:50:42 +00002752 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002753 return -1;
2754}
2755
2756
ths52f61fd2006-12-22 21:20:52 +00002757static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002758{
2759 CharDriverState *chr;
2760 WinCharState *s;
2761
2762 chr = qemu_mallocz(sizeof(CharDriverState));
2763 if (!chr)
2764 return NULL;
2765 s = qemu_mallocz(sizeof(WinCharState));
2766 if (!s) {
2767 free(chr);
2768 return NULL;
2769 }
2770 chr->opaque = s;
2771 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002772 chr->chr_close = win_chr_close;
ths3b46e622007-09-17 08:09:54 +00002773
ths087f4ae2007-02-10 21:50:42 +00002774 if (win_chr_pipe_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002775 free(s);
2776 free(chr);
2777 return NULL;
2778 }
ths86e94de2007-01-05 22:01:59 +00002779 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002780 return chr;
2781}
2782
ths52f61fd2006-12-22 21:20:52 +00002783static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
bellardf3311102006-04-12 20:21:17 +00002784{
2785 CharDriverState *chr;
2786 WinCharState *s;
2787
2788 chr = qemu_mallocz(sizeof(CharDriverState));
2789 if (!chr)
2790 return NULL;
2791 s = qemu_mallocz(sizeof(WinCharState));
2792 if (!s) {
2793 free(chr);
2794 return NULL;
2795 }
2796 s->hcom = fd_out;
2797 chr->opaque = s;
2798 chr->chr_write = win_chr_write;
ths86e94de2007-01-05 22:01:59 +00002799 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002800 return chr;
2801}
ths72d46472007-05-13 14:54:54 +00002802
2803static CharDriverState *qemu_chr_open_win_con(const char *filename)
2804{
2805 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2806}
2807
ths52f61fd2006-12-22 21:20:52 +00002808static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
bellardf3311102006-04-12 20:21:17 +00002809{
2810 HANDLE fd_out;
ths3b46e622007-09-17 08:09:54 +00002811
bellardf3311102006-04-12 20:21:17 +00002812 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
2813 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2814 if (fd_out == INVALID_HANDLE_VALUE)
2815 return NULL;
2816
2817 return qemu_chr_open_win_file(fd_out);
2818}
thsaec62502007-06-25 11:48:07 +00002819#endif /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00002820
bellard0bab00f2006-06-25 14:49:44 +00002821/***********************************************************/
2822/* UDP Net console */
2823
2824typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002825 int fd;
2826 struct sockaddr_in daddr;
2827 char buf[1024];
2828 int bufcnt;
2829 int bufptr;
2830 int max_size;
2831} NetCharDriver;
2832
2833static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2834{
2835 NetCharDriver *s = chr->opaque;
2836
2837 return sendto(s->fd, buf, len, 0,
2838 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
2839}
2840
2841static int udp_chr_read_poll(void *opaque)
2842{
2843 CharDriverState *chr = opaque;
2844 NetCharDriver *s = chr->opaque;
2845
pbrooke5b0bc42007-01-27 23:46:43 +00002846 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002847
2848 /* If there were any stray characters in the queue process them
2849 * first
2850 */
2851 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002852 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002853 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002854 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002855 }
2856 return s->max_size;
2857}
2858
2859static void udp_chr_read(void *opaque)
2860{
2861 CharDriverState *chr = opaque;
2862 NetCharDriver *s = chr->opaque;
2863
2864 if (s->max_size == 0)
2865 return;
2866 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
2867 s->bufptr = s->bufcnt;
2868 if (s->bufcnt <= 0)
2869 return;
2870
2871 s->bufptr = 0;
2872 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002873 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002874 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002875 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002876 }
2877}
2878
pbrooke5b0bc42007-01-27 23:46:43 +00002879static void udp_chr_update_read_handler(CharDriverState *chr)
bellard0bab00f2006-06-25 14:49:44 +00002880{
2881 NetCharDriver *s = chr->opaque;
2882
2883 if (s->fd >= 0) {
bellard0bab00f2006-06-25 14:49:44 +00002884 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2885 udp_chr_read, NULL, chr);
2886 }
2887}
2888
2889int parse_host_port(struct sockaddr_in *saddr, const char *str);
ths52f61fd2006-12-22 21:20:52 +00002890#ifndef _WIN32
2891static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
2892#endif
bellard951f1352006-06-27 21:02:43 +00002893int parse_host_src_port(struct sockaddr_in *haddr,
2894 struct sockaddr_in *saddr,
2895 const char *str);
bellard0bab00f2006-06-25 14:49:44 +00002896
ths52f61fd2006-12-22 21:20:52 +00002897static CharDriverState *qemu_chr_open_udp(const char *def)
bellard0bab00f2006-06-25 14:49:44 +00002898{
2899 CharDriverState *chr = NULL;
2900 NetCharDriver *s = NULL;
2901 int fd = -1;
bellard951f1352006-06-27 21:02:43 +00002902 struct sockaddr_in saddr;
bellard0bab00f2006-06-25 14:49:44 +00002903
2904 chr = qemu_mallocz(sizeof(CharDriverState));
2905 if (!chr)
2906 goto return_err;
2907 s = qemu_mallocz(sizeof(NetCharDriver));
2908 if (!s)
2909 goto return_err;
2910
2911 fd = socket(PF_INET, SOCK_DGRAM, 0);
2912 if (fd < 0) {
2913 perror("socket(PF_INET, SOCK_DGRAM)");
2914 goto return_err;
2915 }
2916
bellard951f1352006-06-27 21:02:43 +00002917 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
2918 printf("Could not parse: %s\n", def);
2919 goto return_err;
bellard0bab00f2006-06-25 14:49:44 +00002920 }
2921
bellard951f1352006-06-27 21:02:43 +00002922 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
bellard0bab00f2006-06-25 14:49:44 +00002923 {
2924 perror("bind");
2925 goto return_err;
2926 }
2927
2928 s->fd = fd;
2929 s->bufcnt = 0;
2930 s->bufptr = 0;
2931 chr->opaque = s;
2932 chr->chr_write = udp_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002933 chr->chr_update_read_handler = udp_chr_update_read_handler;
bellard0bab00f2006-06-25 14:49:44 +00002934 return chr;
2935
2936return_err:
2937 if (chr)
2938 free(chr);
2939 if (s)
2940 free(s);
2941 if (fd >= 0)
2942 closesocket(fd);
2943 return NULL;
2944}
2945
2946/***********************************************************/
2947/* TCP Net console */
2948
2949typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002950 int fd, listen_fd;
2951 int connected;
2952 int max_size;
bellard951f1352006-06-27 21:02:43 +00002953 int do_telnetopt;
pbrooke5b0bc42007-01-27 23:46:43 +00002954 int do_nodelay;
thsffd843b2006-12-21 19:46:43 +00002955 int is_unix;
bellard0bab00f2006-06-25 14:49:44 +00002956} TCPCharDriver;
2957
2958static void tcp_chr_accept(void *opaque);
2959
2960static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2961{
2962 TCPCharDriver *s = chr->opaque;
2963 if (s->connected) {
2964 return send_all(s->fd, buf, len);
2965 } else {
2966 /* XXX: indicate an error ? */
2967 return len;
2968 }
2969}
2970
2971static int tcp_chr_read_poll(void *opaque)
2972{
2973 CharDriverState *chr = opaque;
2974 TCPCharDriver *s = chr->opaque;
2975 if (!s->connected)
2976 return 0;
pbrooke5b0bc42007-01-27 23:46:43 +00002977 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002978 return s->max_size;
2979}
2980
bellard951f1352006-06-27 21:02:43 +00002981#define IAC 255
2982#define IAC_BREAK 243
2983static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2984 TCPCharDriver *s,
2985 char *buf, int *size)
2986{
2987 /* Handle any telnet client's basic IAC options to satisfy char by
2988 * char mode with no echo. All IAC options will be removed from
2989 * the buf and the do_telnetopt variable will be used to track the
2990 * state of the width of the IAC information.
2991 *
2992 * IAC commands come in sets of 3 bytes with the exception of the
2993 * "IAC BREAK" command and the double IAC.
2994 */
2995
2996 int i;
2997 int j = 0;
2998
2999 for (i = 0; i < *size; i++) {
3000 if (s->do_telnetopt > 1) {
3001 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3002 /* Double IAC means send an IAC */
3003 if (j != i)
3004 buf[j] = buf[i];
3005 j++;
3006 s->do_telnetopt = 1;
3007 } else {
3008 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3009 /* Handle IAC break commands by sending a serial break */
pbrooke5b0bc42007-01-27 23:46:43 +00003010 qemu_chr_event(chr, CHR_EVENT_BREAK);
bellard951f1352006-06-27 21:02:43 +00003011 s->do_telnetopt++;
3012 }
3013 s->do_telnetopt++;
3014 }
3015 if (s->do_telnetopt >= 4) {
3016 s->do_telnetopt = 1;
3017 }
3018 } else {
3019 if ((unsigned char)buf[i] == IAC) {
3020 s->do_telnetopt = 2;
3021 } else {
3022 if (j != i)
3023 buf[j] = buf[i];
3024 j++;
3025 }
3026 }
3027 }
3028 *size = j;
3029}
3030
bellard0bab00f2006-06-25 14:49:44 +00003031static void tcp_chr_read(void *opaque)
3032{
3033 CharDriverState *chr = opaque;
3034 TCPCharDriver *s = chr->opaque;
3035 uint8_t buf[1024];
3036 int len, size;
3037
3038 if (!s->connected || s->max_size <= 0)
3039 return;
3040 len = sizeof(buf);
3041 if (len > s->max_size)
3042 len = s->max_size;
3043 size = recv(s->fd, buf, len, 0);
3044 if (size == 0) {
3045 /* connection closed */
3046 s->connected = 0;
3047 if (s->listen_fd >= 0) {
3048 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3049 }
3050 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3051 closesocket(s->fd);
3052 s->fd = -1;
3053 } else if (size > 0) {
bellard951f1352006-06-27 21:02:43 +00003054 if (s->do_telnetopt)
3055 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3056 if (size > 0)
pbrooke5b0bc42007-01-27 23:46:43 +00003057 qemu_chr_read(chr, buf, size);
bellard0bab00f2006-06-25 14:49:44 +00003058 }
3059}
3060
bellard0bab00f2006-06-25 14:49:44 +00003061static void tcp_chr_connect(void *opaque)
3062{
3063 CharDriverState *chr = opaque;
3064 TCPCharDriver *s = chr->opaque;
3065
3066 s->connected = 1;
3067 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3068 tcp_chr_read, NULL, chr);
ths86e94de2007-01-05 22:01:59 +00003069 qemu_chr_reset(chr);
bellard0bab00f2006-06-25 14:49:44 +00003070}
3071
bellard951f1352006-06-27 21:02:43 +00003072#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3073static void tcp_chr_telnet_init(int fd)
3074{
3075 char buf[3];
3076 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3077 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3078 send(fd, (char *)buf, 3, 0);
3079 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3080 send(fd, (char *)buf, 3, 0);
3081 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3082 send(fd, (char *)buf, 3, 0);
3083 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3084 send(fd, (char *)buf, 3, 0);
3085}
3086
pbrookf7499982007-01-28 00:10:01 +00003087static void socket_set_nodelay(int fd)
3088{
3089 int val = 1;
3090 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3091}
3092
bellard0bab00f2006-06-25 14:49:44 +00003093static void tcp_chr_accept(void *opaque)
3094{
3095 CharDriverState *chr = opaque;
3096 TCPCharDriver *s = chr->opaque;
3097 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003098#ifndef _WIN32
3099 struct sockaddr_un uaddr;
3100#endif
3101 struct sockaddr *addr;
bellard0bab00f2006-06-25 14:49:44 +00003102 socklen_t len;
3103 int fd;
3104
3105 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003106#ifndef _WIN32
3107 if (s->is_unix) {
3108 len = sizeof(uaddr);
3109 addr = (struct sockaddr *)&uaddr;
3110 } else
3111#endif
3112 {
3113 len = sizeof(saddr);
3114 addr = (struct sockaddr *)&saddr;
3115 }
3116 fd = accept(s->listen_fd, addr, &len);
bellard0bab00f2006-06-25 14:49:44 +00003117 if (fd < 0 && errno != EINTR) {
3118 return;
3119 } else if (fd >= 0) {
bellard951f1352006-06-27 21:02:43 +00003120 if (s->do_telnetopt)
3121 tcp_chr_telnet_init(fd);
bellard0bab00f2006-06-25 14:49:44 +00003122 break;
3123 }
3124 }
3125 socket_set_nonblock(fd);
pbrookf7499982007-01-28 00:10:01 +00003126 if (s->do_nodelay)
3127 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003128 s->fd = fd;
3129 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3130 tcp_chr_connect(chr);
3131}
3132
3133static void tcp_chr_close(CharDriverState *chr)
3134{
3135 TCPCharDriver *s = chr->opaque;
3136 if (s->fd >= 0)
3137 closesocket(s->fd);
3138 if (s->listen_fd >= 0)
3139 closesocket(s->listen_fd);
3140 qemu_free(s);
3141}
3142
ths5fafdf22007-09-16 21:08:06 +00003143static CharDriverState *qemu_chr_open_tcp(const char *host_str,
thsffd843b2006-12-21 19:46:43 +00003144 int is_telnet,
3145 int is_unix)
bellard0bab00f2006-06-25 14:49:44 +00003146{
3147 CharDriverState *chr = NULL;
3148 TCPCharDriver *s = NULL;
3149 int fd = -1, ret, err, val;
bellard951f1352006-06-27 21:02:43 +00003150 int is_listen = 0;
3151 int is_waitconnect = 1;
pbrookf7499982007-01-28 00:10:01 +00003152 int do_nodelay = 0;
bellard951f1352006-06-27 21:02:43 +00003153 const char *ptr;
bellard0bab00f2006-06-25 14:49:44 +00003154 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003155#ifndef _WIN32
3156 struct sockaddr_un uaddr;
3157#endif
3158 struct sockaddr *addr;
3159 socklen_t addrlen;
bellard0bab00f2006-06-25 14:49:44 +00003160
thsffd843b2006-12-21 19:46:43 +00003161#ifndef _WIN32
3162 if (is_unix) {
3163 addr = (struct sockaddr *)&uaddr;
3164 addrlen = sizeof(uaddr);
3165 if (parse_unix_path(&uaddr, host_str) < 0)
3166 goto fail;
3167 } else
3168#endif
3169 {
3170 addr = (struct sockaddr *)&saddr;
3171 addrlen = sizeof(saddr);
3172 if (parse_host_port(&saddr, host_str) < 0)
3173 goto fail;
3174 }
bellard0bab00f2006-06-25 14:49:44 +00003175
bellard951f1352006-06-27 21:02:43 +00003176 ptr = host_str;
3177 while((ptr = strchr(ptr,','))) {
3178 ptr++;
3179 if (!strncmp(ptr,"server",6)) {
3180 is_listen = 1;
3181 } else if (!strncmp(ptr,"nowait",6)) {
3182 is_waitconnect = 0;
pbrookf7499982007-01-28 00:10:01 +00003183 } else if (!strncmp(ptr,"nodelay",6)) {
3184 do_nodelay = 1;
bellard951f1352006-06-27 21:02:43 +00003185 } else {
3186 printf("Unknown option: %s\n", ptr);
3187 goto fail;
3188 }
3189 }
3190 if (!is_listen)
3191 is_waitconnect = 0;
3192
bellard0bab00f2006-06-25 14:49:44 +00003193 chr = qemu_mallocz(sizeof(CharDriverState));
3194 if (!chr)
3195 goto fail;
3196 s = qemu_mallocz(sizeof(TCPCharDriver));
3197 if (!s)
3198 goto fail;
thsffd843b2006-12-21 19:46:43 +00003199
3200#ifndef _WIN32
3201 if (is_unix)
3202 fd = socket(PF_UNIX, SOCK_STREAM, 0);
3203 else
3204#endif
3205 fd = socket(PF_INET, SOCK_STREAM, 0);
ths5fafdf22007-09-16 21:08:06 +00003206
3207 if (fd < 0)
bellard0bab00f2006-06-25 14:49:44 +00003208 goto fail;
bellard951f1352006-06-27 21:02:43 +00003209
3210 if (!is_waitconnect)
3211 socket_set_nonblock(fd);
bellard0bab00f2006-06-25 14:49:44 +00003212
3213 s->connected = 0;
3214 s->fd = -1;
3215 s->listen_fd = -1;
thsffd843b2006-12-21 19:46:43 +00003216 s->is_unix = is_unix;
pbrookf7499982007-01-28 00:10:01 +00003217 s->do_nodelay = do_nodelay && !is_unix;
thsffd843b2006-12-21 19:46:43 +00003218
3219 chr->opaque = s;
3220 chr->chr_write = tcp_chr_write;
thsffd843b2006-12-21 19:46:43 +00003221 chr->chr_close = tcp_chr_close;
3222
bellard0bab00f2006-06-25 14:49:44 +00003223 if (is_listen) {
3224 /* allow fast reuse */
thsffd843b2006-12-21 19:46:43 +00003225#ifndef _WIN32
3226 if (is_unix) {
3227 char path[109];
3228 strncpy(path, uaddr.sun_path, 108);
3229 path[108] = 0;
3230 unlink(path);
3231 } else
3232#endif
3233 {
3234 val = 1;
3235 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3236 }
ths3b46e622007-09-17 08:09:54 +00003237
thsffd843b2006-12-21 19:46:43 +00003238 ret = bind(fd, addr, addrlen);
3239 if (ret < 0)
bellard0bab00f2006-06-25 14:49:44 +00003240 goto fail;
thsffd843b2006-12-21 19:46:43 +00003241
bellard0bab00f2006-06-25 14:49:44 +00003242 ret = listen(fd, 0);
3243 if (ret < 0)
3244 goto fail;
thsffd843b2006-12-21 19:46:43 +00003245
bellard0bab00f2006-06-25 14:49:44 +00003246 s->listen_fd = fd;
3247 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
bellard951f1352006-06-27 21:02:43 +00003248 if (is_telnet)
3249 s->do_telnetopt = 1;
bellard0bab00f2006-06-25 14:49:44 +00003250 } else {
3251 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003252 ret = connect(fd, addr, addrlen);
bellard0bab00f2006-06-25 14:49:44 +00003253 if (ret < 0) {
3254 err = socket_error();
3255 if (err == EINTR || err == EWOULDBLOCK) {
3256 } else if (err == EINPROGRESS) {
3257 break;
thsf5b12262007-03-25 15:58:03 +00003258#ifdef _WIN32
3259 } else if (err == WSAEALREADY) {
3260 break;
3261#endif
bellard0bab00f2006-06-25 14:49:44 +00003262 } else {
3263 goto fail;
3264 }
3265 } else {
3266 s->connected = 1;
3267 break;
3268 }
3269 }
3270 s->fd = fd;
pbrookf7499982007-01-28 00:10:01 +00003271 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003272 if (s->connected)
3273 tcp_chr_connect(chr);
3274 else
3275 qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3276 }
ths3b46e622007-09-17 08:09:54 +00003277
bellard951f1352006-06-27 21:02:43 +00003278 if (is_listen && is_waitconnect) {
3279 printf("QEMU waiting for connection on: %s\n", host_str);
3280 tcp_chr_accept(chr);
3281 socket_set_nonblock(s->listen_fd);
3282 }
3283
bellard0bab00f2006-06-25 14:49:44 +00003284 return chr;
3285 fail:
3286 if (fd >= 0)
3287 closesocket(fd);
3288 qemu_free(s);
3289 qemu_free(chr);
3290 return NULL;
3291}
3292
bellard82c643f2004-07-14 17:28:13 +00003293CharDriverState *qemu_chr_open(const char *filename)
3294{
bellardf8d179e2005-11-08 22:30:36 +00003295 const char *p;
bellardfd1dff42006-02-01 21:29:26 +00003296
bellard82c643f2004-07-14 17:28:13 +00003297 if (!strcmp(filename, "vc")) {
thsaf3a9032007-07-11 23:14:59 +00003298 return text_console_init(&display_state, 0);
3299 } else if (strstart(filename, "vc:", &p)) {
3300 return text_console_init(&display_state, p);
bellard82c643f2004-07-14 17:28:13 +00003301 } else if (!strcmp(filename, "null")) {
3302 return qemu_chr_open_null();
ths5fafdf22007-09-16 21:08:06 +00003303 } else
bellard0bab00f2006-06-25 14:49:44 +00003304 if (strstart(filename, "tcp:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003305 return qemu_chr_open_tcp(p, 0, 0);
bellard0bab00f2006-06-25 14:49:44 +00003306 } else
bellard951f1352006-06-27 21:02:43 +00003307 if (strstart(filename, "telnet:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003308 return qemu_chr_open_tcp(p, 1, 0);
bellard0bab00f2006-06-25 14:49:44 +00003309 } else
3310 if (strstart(filename, "udp:", &p)) {
3311 return qemu_chr_open_udp(p);
3312 } else
ths20d8a3e2007-02-18 17:04:49 +00003313 if (strstart(filename, "mon:", &p)) {
3314 CharDriverState *drv = qemu_chr_open(p);
3315 if (drv) {
3316 drv = qemu_chr_open_mux(drv);
3317 monitor_init(drv, !nographic);
3318 return drv;
3319 }
3320 printf("Unable to open driver: %s\n", p);
3321 return 0;
3322 } else
bellard76647282005-11-27 19:10:42 +00003323#ifndef _WIN32
thsffd843b2006-12-21 19:46:43 +00003324 if (strstart(filename, "unix:", &p)) {
3325 return qemu_chr_open_tcp(p, 0, 1);
3326 } else if (strstart(filename, "file:", &p)) {
bellardf8d179e2005-11-08 22:30:36 +00003327 return qemu_chr_open_file_out(p);
3328 } else if (strstart(filename, "pipe:", &p)) {
3329 return qemu_chr_open_pipe(p);
bellard76647282005-11-27 19:10:42 +00003330 } else if (!strcmp(filename, "pty")) {
bellard82c643f2004-07-14 17:28:13 +00003331 return qemu_chr_open_pty();
3332 } else if (!strcmp(filename, "stdio")) {
3333 return qemu_chr_open_stdio();
ths5fafdf22007-09-16 21:08:06 +00003334 } else
bellardf8d179e2005-11-08 22:30:36 +00003335#if defined(__linux__)
bellarde57a8c02005-11-10 23:58:52 +00003336 if (strstart(filename, "/dev/parport", NULL)) {
3337 return qemu_chr_open_pp(filename);
ths5fafdf22007-09-16 21:08:06 +00003338 } else
thsaec62502007-06-25 11:48:07 +00003339#endif
ths3fda3882007-06-28 15:14:49 +00003340#if defined(__linux__) || defined(__sun__)
bellardf8d179e2005-11-08 22:30:36 +00003341 if (strstart(filename, "/dev/", NULL)) {
3342 return qemu_chr_open_tty(filename);
ths3fda3882007-06-28 15:14:49 +00003343 } else
3344#endif
thsaec62502007-06-25 11:48:07 +00003345#else /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00003346 if (strstart(filename, "COM", NULL)) {
3347 return qemu_chr_open_win(filename);
3348 } else
3349 if (strstart(filename, "pipe:", &p)) {
3350 return qemu_chr_open_win_pipe(p);
3351 } else
ths72d46472007-05-13 14:54:54 +00003352 if (strstart(filename, "con:", NULL)) {
3353 return qemu_chr_open_win_con(filename);
3354 } else
bellardf3311102006-04-12 20:21:17 +00003355 if (strstart(filename, "file:", &p)) {
3356 return qemu_chr_open_win_file_out(p);
3357 }
3358#endif
bellard82c643f2004-07-14 17:28:13 +00003359 {
3360 return NULL;
3361 }
3362}
3363
bellardf3311102006-04-12 20:21:17 +00003364void qemu_chr_close(CharDriverState *chr)
3365{
3366 if (chr->chr_close)
3367 chr->chr_close(chr);
3368}
3369
bellardf1510b22003-06-25 00:07:40 +00003370/***********************************************************/
bellard7c9d8e02005-11-15 22:16:05 +00003371/* network device redirectors */
bellardf1510b22003-06-25 00:07:40 +00003372
bellardc20709a2004-04-21 23:27:19 +00003373void hex_dump(FILE *f, const uint8_t *buf, int size)
bellard67b915a2004-03-31 23:37:16 +00003374{
bellardc20709a2004-04-21 23:27:19 +00003375 int len, i, j, c;
3376
3377 for(i=0;i<size;i+=16) {
3378 len = size - i;
3379 if (len > 16)
3380 len = 16;
3381 fprintf(f, "%08x ", i);
3382 for(j=0;j<16;j++) {
3383 if (j < len)
3384 fprintf(f, " %02x", buf[i+j]);
3385 else
3386 fprintf(f, " ");
3387 }
3388 fprintf(f, " ");
3389 for(j=0;j<len;j++) {
3390 c = buf[i+j];
3391 if (c < ' ' || c > '~')
3392 c = '.';
3393 fprintf(f, "%c", c);
3394 }
3395 fprintf(f, "\n");
3396 }
3397}
3398
bellard7c9d8e02005-11-15 22:16:05 +00003399static int parse_macaddr(uint8_t *macaddr, const char *p)
bellardc20709a2004-04-21 23:27:19 +00003400{
bellard7c9d8e02005-11-15 22:16:05 +00003401 int i;
3402 for(i = 0; i < 6; i++) {
3403 macaddr[i] = strtol(p, (char **)&p, 16);
3404 if (i == 5) {
ths5fafdf22007-09-16 21:08:06 +00003405 if (*p != '\0')
bellard7c9d8e02005-11-15 22:16:05 +00003406 return -1;
3407 } else {
ths5fafdf22007-09-16 21:08:06 +00003408 if (*p != ':')
bellard7c9d8e02005-11-15 22:16:05 +00003409 return -1;
3410 p++;
3411 }
bellardc20709a2004-04-21 23:27:19 +00003412 }
bellardc20709a2004-04-21 23:27:19 +00003413 return 0;
3414}
3415
bellard9bf05442004-08-25 22:12:49 +00003416static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3417{
3418 const char *p, *p1;
3419 int len;
3420 p = *pp;
3421 p1 = strchr(p, sep);
3422 if (!p1)
3423 return -1;
3424 len = p1 - p;
3425 p1++;
3426 if (buf_size > 0) {
3427 if (len > buf_size - 1)
3428 len = buf_size - 1;
3429 memcpy(buf, p, len);
3430 buf[len] = '\0';
3431 }
3432 *pp = p1;
3433 return 0;
3434}
3435
bellard951f1352006-06-27 21:02:43 +00003436int parse_host_src_port(struct sockaddr_in *haddr,
3437 struct sockaddr_in *saddr,
3438 const char *input_str)
3439{
3440 char *str = strdup(input_str);
3441 char *host_str = str;
3442 char *src_str;
3443 char *ptr;
3444
3445 /*
3446 * Chop off any extra arguments at the end of the string which
3447 * would start with a comma, then fill in the src port information
3448 * if it was provided else use the "any address" and "any port".
3449 */
3450 if ((ptr = strchr(str,',')))
3451 *ptr = '\0';
3452
3453 if ((src_str = strchr(input_str,'@'))) {
3454 *src_str = '\0';
3455 src_str++;
3456 }
3457
3458 if (parse_host_port(haddr, host_str) < 0)
3459 goto fail;
3460
3461 if (!src_str || *src_str == '\0')
3462 src_str = ":0";
3463
3464 if (parse_host_port(saddr, src_str) < 0)
3465 goto fail;
3466
3467 free(str);
3468 return(0);
3469
3470fail:
3471 free(str);
3472 return -1;
3473}
3474
bellard7c9d8e02005-11-15 22:16:05 +00003475int parse_host_port(struct sockaddr_in *saddr, const char *str)
3476{
3477 char buf[512];
3478 struct hostent *he;
3479 const char *p, *r;
3480 int port;
3481
3482 p = str;
3483 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3484 return -1;
3485 saddr->sin_family = AF_INET;
3486 if (buf[0] == '\0') {
3487 saddr->sin_addr.s_addr = 0;
3488 } else {
3489 if (isdigit(buf[0])) {
3490 if (!inet_aton(buf, &saddr->sin_addr))
3491 return -1;
3492 } else {
bellard7c9d8e02005-11-15 22:16:05 +00003493 if ((he = gethostbyname(buf)) == NULL)
3494 return - 1;
3495 saddr->sin_addr = *(struct in_addr *)he->h_addr;
bellard7c9d8e02005-11-15 22:16:05 +00003496 }
3497 }
3498 port = strtol(p, (char **)&r, 0);
3499 if (r == p)
3500 return -1;
3501 saddr->sin_port = htons(port);
3502 return 0;
3503}
3504
ths52f61fd2006-12-22 21:20:52 +00003505#ifndef _WIN32
3506static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
thsffd843b2006-12-21 19:46:43 +00003507{
3508 const char *p;
3509 int len;
3510
3511 len = MIN(108, strlen(str));
3512 p = strchr(str, ',');
3513 if (p)
3514 len = MIN(len, p - str);
3515
3516 memset(uaddr, 0, sizeof(*uaddr));
3517
3518 uaddr->sun_family = AF_UNIX;
3519 memcpy(uaddr->sun_path, str, len);
3520
3521 return 0;
3522}
ths52f61fd2006-12-22 21:20:52 +00003523#endif
thsffd843b2006-12-21 19:46:43 +00003524
bellard7c9d8e02005-11-15 22:16:05 +00003525/* find or alloc a new VLAN */
3526VLANState *qemu_find_vlan(int id)
3527{
3528 VLANState **pvlan, *vlan;
3529 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3530 if (vlan->id == id)
3531 return vlan;
3532 }
3533 vlan = qemu_mallocz(sizeof(VLANState));
3534 if (!vlan)
3535 return NULL;
3536 vlan->id = id;
3537 vlan->next = NULL;
3538 pvlan = &first_vlan;
3539 while (*pvlan != NULL)
3540 pvlan = &(*pvlan)->next;
3541 *pvlan = vlan;
3542 return vlan;
3543}
3544
3545VLANClientState *qemu_new_vlan_client(VLANState *vlan,
pbrookd861b052006-02-04 22:15:28 +00003546 IOReadHandler *fd_read,
3547 IOCanRWHandler *fd_can_read,
3548 void *opaque)
bellard7c9d8e02005-11-15 22:16:05 +00003549{
3550 VLANClientState *vc, **pvc;
3551 vc = qemu_mallocz(sizeof(VLANClientState));
3552 if (!vc)
3553 return NULL;
3554 vc->fd_read = fd_read;
pbrookd861b052006-02-04 22:15:28 +00003555 vc->fd_can_read = fd_can_read;
bellard7c9d8e02005-11-15 22:16:05 +00003556 vc->opaque = opaque;
3557 vc->vlan = vlan;
3558
3559 vc->next = NULL;
3560 pvc = &vlan->first_client;
3561 while (*pvc != NULL)
3562 pvc = &(*pvc)->next;
3563 *pvc = vc;
3564 return vc;
3565}
3566
pbrookd861b052006-02-04 22:15:28 +00003567int qemu_can_send_packet(VLANClientState *vc1)
3568{
3569 VLANState *vlan = vc1->vlan;
3570 VLANClientState *vc;
3571
3572 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3573 if (vc != vc1) {
balrogfbd17112007-07-02 13:31:53 +00003574 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3575 return 1;
pbrookd861b052006-02-04 22:15:28 +00003576 }
3577 }
balrogfbd17112007-07-02 13:31:53 +00003578 return 0;
pbrookd861b052006-02-04 22:15:28 +00003579}
3580
bellard7c9d8e02005-11-15 22:16:05 +00003581void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3582{
3583 VLANState *vlan = vc1->vlan;
3584 VLANClientState *vc;
3585
3586#if 0
3587 printf("vlan %d send:\n", vlan->id);
3588 hex_dump(stdout, buf, size);
3589#endif
3590 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3591 if (vc != vc1) {
3592 vc->fd_read(vc->opaque, buf, size);
3593 }
3594 }
3595}
3596
3597#if defined(CONFIG_SLIRP)
3598
3599/* slirp network adapter */
3600
3601static int slirp_inited;
3602static VLANClientState *slirp_vc;
3603
3604int slirp_can_output(void)
3605{
pbrook3b7f5d42006-02-10 17:34:02 +00003606 return !slirp_vc || qemu_can_send_packet(slirp_vc);
bellard7c9d8e02005-11-15 22:16:05 +00003607}
3608
3609void slirp_output(const uint8_t *pkt, int pkt_len)
3610{
3611#if 0
3612 printf("slirp output:\n");
3613 hex_dump(stdout, pkt, pkt_len);
3614#endif
pbrook3b7f5d42006-02-10 17:34:02 +00003615 if (!slirp_vc)
3616 return;
bellard7c9d8e02005-11-15 22:16:05 +00003617 qemu_send_packet(slirp_vc, pkt, pkt_len);
3618}
3619
3620static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3621{
3622#if 0
3623 printf("slirp input:\n");
3624 hex_dump(stdout, buf, size);
3625#endif
3626 slirp_input(buf, size);
3627}
3628
3629static int net_slirp_init(VLANState *vlan)
3630{
3631 if (!slirp_inited) {
3632 slirp_inited = 1;
3633 slirp_init();
3634 }
ths5fafdf22007-09-16 21:08:06 +00003635 slirp_vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00003636 slirp_receive, NULL, NULL);
bellard7c9d8e02005-11-15 22:16:05 +00003637 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3638 return 0;
3639}
3640
bellard9bf05442004-08-25 22:12:49 +00003641static void net_slirp_redir(const char *redir_str)
3642{
3643 int is_udp;
3644 char buf[256], *r;
3645 const char *p;
3646 struct in_addr guest_addr;
3647 int host_port, guest_port;
ths3b46e622007-09-17 08:09:54 +00003648
bellard9bf05442004-08-25 22:12:49 +00003649 if (!slirp_inited) {
3650 slirp_inited = 1;
3651 slirp_init();
3652 }
3653
3654 p = redir_str;
3655 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3656 goto fail;
3657 if (!strcmp(buf, "tcp")) {
3658 is_udp = 0;
3659 } else if (!strcmp(buf, "udp")) {
3660 is_udp = 1;
3661 } else {
3662 goto fail;
3663 }
3664
3665 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3666 goto fail;
3667 host_port = strtol(buf, &r, 0);
3668 if (r == buf)
3669 goto fail;
3670
3671 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3672 goto fail;
3673 if (buf[0] == '\0') {
3674 pstrcpy(buf, sizeof(buf), "10.0.2.15");
3675 }
3676 if (!inet_aton(buf, &guest_addr))
3677 goto fail;
ths3b46e622007-09-17 08:09:54 +00003678
bellard9bf05442004-08-25 22:12:49 +00003679 guest_port = strtol(p, &r, 0);
3680 if (r == p)
3681 goto fail;
ths3b46e622007-09-17 08:09:54 +00003682
bellard9bf05442004-08-25 22:12:49 +00003683 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
3684 fprintf(stderr, "qemu: could not set up redirection\n");
3685 exit(1);
3686 }
3687 return;
3688 fail:
3689 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3690 exit(1);
3691}
ths3b46e622007-09-17 08:09:54 +00003692
bellardc94c8d62004-09-13 21:37:34 +00003693#ifndef _WIN32
3694
bellard9d728e82004-09-05 23:09:03 +00003695char smb_dir[1024];
3696
3697static void smb_exit(void)
3698{
3699 DIR *d;
3700 struct dirent *de;
3701 char filename[1024];
3702
3703 /* erase all the files in the directory */
3704 d = opendir(smb_dir);
3705 for(;;) {
3706 de = readdir(d);
3707 if (!de)
3708 break;
3709 if (strcmp(de->d_name, ".") != 0 &&
3710 strcmp(de->d_name, "..") != 0) {
ths5fafdf22007-09-16 21:08:06 +00003711 snprintf(filename, sizeof(filename), "%s/%s",
bellard9d728e82004-09-05 23:09:03 +00003712 smb_dir, de->d_name);
3713 unlink(filename);
3714 }
3715 }
bellard03ffbb62004-09-06 00:14:04 +00003716 closedir(d);
bellard9d728e82004-09-05 23:09:03 +00003717 rmdir(smb_dir);
3718}
3719
3720/* automatic user mode samba server configuration */
3721void net_slirp_smb(const char *exported_dir)
3722{
3723 char smb_conf[1024];
3724 char smb_cmdline[1024];
3725 FILE *f;
3726
3727 if (!slirp_inited) {
3728 slirp_inited = 1;
3729 slirp_init();
3730 }
3731
3732 /* XXX: better tmp dir construction */
3733 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
3734 if (mkdir(smb_dir, 0700) < 0) {
3735 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
3736 exit(1);
3737 }
3738 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
ths3b46e622007-09-17 08:09:54 +00003739
bellard9d728e82004-09-05 23:09:03 +00003740 f = fopen(smb_conf, "w");
3741 if (!f) {
3742 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
3743 exit(1);
3744 }
ths5fafdf22007-09-16 21:08:06 +00003745 fprintf(f,
bellard9d728e82004-09-05 23:09:03 +00003746 "[global]\n"
bellard157777e2005-03-01 21:28:45 +00003747 "private dir=%s\n"
3748 "smb ports=0\n"
3749 "socket address=127.0.0.1\n"
bellard9d728e82004-09-05 23:09:03 +00003750 "pid directory=%s\n"
3751 "lock directory=%s\n"
3752 "log file=%s/log.smbd\n"
3753 "smb passwd file=%s/smbpasswd\n"
bellard03ffbb62004-09-06 00:14:04 +00003754 "security = share\n"
bellard9d728e82004-09-05 23:09:03 +00003755 "[qemu]\n"
3756 "path=%s\n"
3757 "read only=no\n"
3758 "guest ok=yes\n",
3759 smb_dir,
3760 smb_dir,
3761 smb_dir,
3762 smb_dir,
bellard157777e2005-03-01 21:28:45 +00003763 smb_dir,
bellard9d728e82004-09-05 23:09:03 +00003764 exported_dir
3765 );
3766 fclose(f);
3767 atexit(smb_exit);
3768
pbrooka14d6c82006-12-23 15:37:33 +00003769 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
3770 SMBD_COMMAND, smb_conf);
ths3b46e622007-09-17 08:09:54 +00003771
bellard9d728e82004-09-05 23:09:03 +00003772 slirp_add_exec(0, smb_cmdline, 4, 139);
3773}
bellard9bf05442004-08-25 22:12:49 +00003774
bellardc94c8d62004-09-13 21:37:34 +00003775#endif /* !defined(_WIN32) */
blueswir131a60e22007-10-26 18:42:59 +00003776void do_info_slirp(void)
3777{
3778 slirp_stats();
3779}
bellardc94c8d62004-09-13 21:37:34 +00003780
bellardc20709a2004-04-21 23:27:19 +00003781#endif /* CONFIG_SLIRP */
3782
3783#if !defined(_WIN32)
bellard7c9d8e02005-11-15 22:16:05 +00003784
3785typedef struct TAPState {
3786 VLANClientState *vc;
3787 int fd;
thsb46a8902007-10-21 23:20:45 +00003788 char down_script[1024];
bellard7c9d8e02005-11-15 22:16:05 +00003789} TAPState;
3790
3791static void tap_receive(void *opaque, const uint8_t *buf, int size)
3792{
3793 TAPState *s = opaque;
3794 int ret;
3795 for(;;) {
3796 ret = write(s->fd, buf, size);
3797 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
3798 } else {
3799 break;
3800 }
3801 }
3802}
3803
3804static void tap_send(void *opaque)
3805{
3806 TAPState *s = opaque;
3807 uint8_t buf[4096];
3808 int size;
3809
thsd5d10bc2007-02-17 22:54:49 +00003810#ifdef __sun__
3811 struct strbuf sbuf;
3812 int f = 0;
3813 sbuf.maxlen = sizeof(buf);
3814 sbuf.buf = buf;
3815 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
3816#else
bellard7c9d8e02005-11-15 22:16:05 +00003817 size = read(s->fd, buf, sizeof(buf));
thsd5d10bc2007-02-17 22:54:49 +00003818#endif
bellard7c9d8e02005-11-15 22:16:05 +00003819 if (size > 0) {
3820 qemu_send_packet(s->vc, buf, size);
3821 }
3822}
3823
3824/* fd support */
3825
3826static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
3827{
3828 TAPState *s;
3829
3830 s = qemu_mallocz(sizeof(TAPState));
3831 if (!s)
3832 return NULL;
3833 s->fd = fd;
pbrookd861b052006-02-04 22:15:28 +00003834 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00003835 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
3836 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
3837 return s;
3838}
3839
ths5c40d2b2007-06-23 16:03:36 +00003840#if defined (_BSD) || defined (__FreeBSD_kernel__)
bellard7c9d8e02005-11-15 22:16:05 +00003841static int tap_open(char *ifname, int ifname_size)
bellard7d3505c2004-05-12 19:32:15 +00003842{
3843 int fd;
3844 char *dev;
3845 struct stat s;
bellard67b915a2004-03-31 23:37:16 +00003846
balrogaeb30be2007-07-02 15:03:13 +00003847 TFR(fd = open("/dev/tap", O_RDWR));
bellard7d3505c2004-05-12 19:32:15 +00003848 if (fd < 0) {
3849 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
3850 return -1;
3851 }
3852
3853 fstat(fd, &s);
3854 dev = devname(s.st_rdev, S_IFCHR);
3855 pstrcpy(ifname, ifname_size, dev);
3856
3857 fcntl(fd, F_SETFL, O_NONBLOCK);
3858 return fd;
3859}
bellardec530c82006-04-25 22:36:06 +00003860#elif defined(__sun__)
thsd5d10bc2007-02-17 22:54:49 +00003861#define TUNNEWPPA (('T'<<16) | 0x0001)
ths5fafdf22007-09-16 21:08:06 +00003862/*
3863 * Allocate TAP device, returns opened fd.
thsd5d10bc2007-02-17 22:54:49 +00003864 * Stores dev name in the first arg(must be large enough).
ths3b46e622007-09-17 08:09:54 +00003865 */
thsd5d10bc2007-02-17 22:54:49 +00003866int tap_alloc(char *dev)
3867{
3868 int tap_fd, if_fd, ppa = -1;
3869 static int ip_fd = 0;
3870 char *ptr;
3871
3872 static int arp_fd = 0;
3873 int ip_muxid, arp_muxid;
3874 struct strioctl strioc_if, strioc_ppa;
3875 int link_type = I_PLINK;;
3876 struct lifreq ifr;
3877 char actual_name[32] = "";
3878
3879 memset(&ifr, 0x0, sizeof(ifr));
3880
3881 if( *dev ){
ths5fafdf22007-09-16 21:08:06 +00003882 ptr = dev;
3883 while( *ptr && !isdigit((int)*ptr) ) ptr++;
thsd5d10bc2007-02-17 22:54:49 +00003884 ppa = atoi(ptr);
3885 }
3886
3887 /* Check if IP device was opened */
3888 if( ip_fd )
3889 close(ip_fd);
3890
balrogaeb30be2007-07-02 15:03:13 +00003891 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
3892 if (ip_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003893 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
3894 return -1;
3895 }
3896
balrogaeb30be2007-07-02 15:03:13 +00003897 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
3898 if (tap_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003899 syslog(LOG_ERR, "Can't open /dev/tap");
3900 return -1;
3901 }
3902
3903 /* Assign a new PPA and get its unit number. */
3904 strioc_ppa.ic_cmd = TUNNEWPPA;
3905 strioc_ppa.ic_timout = 0;
3906 strioc_ppa.ic_len = sizeof(ppa);
3907 strioc_ppa.ic_dp = (char *)&ppa;
3908 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
3909 syslog (LOG_ERR, "Can't assign new interface");
3910
balrogaeb30be2007-07-02 15:03:13 +00003911 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
3912 if (if_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003913 syslog(LOG_ERR, "Can't open /dev/tap (2)");
3914 return -1;
3915 }
3916 if(ioctl(if_fd, I_PUSH, "ip") < 0){
3917 syslog(LOG_ERR, "Can't push IP module");
3918 return -1;
3919 }
3920
3921 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
3922 syslog(LOG_ERR, "Can't get flags\n");
3923
3924 snprintf (actual_name, 32, "tap%d", ppa);
3925 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3926
3927 ifr.lifr_ppa = ppa;
3928 /* Assign ppa according to the unit number returned by tun device */
3929
3930 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
3931 syslog (LOG_ERR, "Can't set PPA %d", ppa);
3932 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
3933 syslog (LOG_ERR, "Can't get flags\n");
3934 /* Push arp module to if_fd */
3935 if (ioctl (if_fd, I_PUSH, "arp") < 0)
3936 syslog (LOG_ERR, "Can't push ARP module (2)");
3937
3938 /* Push arp module to ip_fd */
3939 if (ioctl (ip_fd, I_POP, NULL) < 0)
3940 syslog (LOG_ERR, "I_POP failed\n");
3941 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
3942 syslog (LOG_ERR, "Can't push ARP module (3)\n");
3943 /* Open arp_fd */
balrogaeb30be2007-07-02 15:03:13 +00003944 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
3945 if (arp_fd < 0)
thsd5d10bc2007-02-17 22:54:49 +00003946 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
3947
3948 /* Set ifname to arp */
3949 strioc_if.ic_cmd = SIOCSLIFNAME;
3950 strioc_if.ic_timout = 0;
3951 strioc_if.ic_len = sizeof(ifr);
3952 strioc_if.ic_dp = (char *)&ifr;
3953 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
3954 syslog (LOG_ERR, "Can't set ifname to arp\n");
3955 }
3956
3957 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
3958 syslog(LOG_ERR, "Can't link TAP device to IP");
3959 return -1;
3960 }
3961
3962 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
3963 syslog (LOG_ERR, "Can't link TAP device to ARP");
3964
3965 close (if_fd);
3966
3967 memset(&ifr, 0x0, sizeof(ifr));
3968 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3969 ifr.lifr_ip_muxid = ip_muxid;
3970 ifr.lifr_arp_muxid = arp_muxid;
3971
3972 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
3973 {
3974 ioctl (ip_fd, I_PUNLINK , arp_muxid);
3975 ioctl (ip_fd, I_PUNLINK, ip_muxid);
3976 syslog (LOG_ERR, "Can't set multiplexor id");
3977 }
3978
3979 sprintf(dev, "tap%d", ppa);
3980 return tap_fd;
3981}
3982
bellardec530c82006-04-25 22:36:06 +00003983static int tap_open(char *ifname, int ifname_size)
3984{
thsd5d10bc2007-02-17 22:54:49 +00003985 char dev[10]="";
3986 int fd;
3987 if( (fd = tap_alloc(dev)) < 0 ){
3988 fprintf(stderr, "Cannot allocate TAP device\n");
3989 return -1;
3990 }
3991 pstrcpy(ifname, ifname_size, dev);
3992 fcntl(fd, F_SETFL, O_NONBLOCK);
3993 return fd;
bellardec530c82006-04-25 22:36:06 +00003994}
bellard7d3505c2004-05-12 19:32:15 +00003995#else
bellard7c9d8e02005-11-15 22:16:05 +00003996static int tap_open(char *ifname, int ifname_size)
bellardf1510b22003-06-25 00:07:40 +00003997{
3998 struct ifreq ifr;
bellardc4b1fcc2004-03-14 21:44:30 +00003999 int fd, ret;
ths3b46e622007-09-17 08:09:54 +00004000
balrogaeb30be2007-07-02 15:03:13 +00004001 TFR(fd = open("/dev/net/tun", O_RDWR));
bellardf1510b22003-06-25 00:07:40 +00004002 if (fd < 0) {
4003 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4004 return -1;
4005 }
4006 memset(&ifr, 0, sizeof(ifr));
4007 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
bellard7c9d8e02005-11-15 22:16:05 +00004008 if (ifname[0] != '\0')
4009 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4010 else
4011 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
bellardf1510b22003-06-25 00:07:40 +00004012 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4013 if (ret != 0) {
4014 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4015 close(fd);
4016 return -1;
4017 }
bellardc4b1fcc2004-03-14 21:44:30 +00004018 pstrcpy(ifname, ifname_size, ifr.ifr_name);
bellardf1510b22003-06-25 00:07:40 +00004019 fcntl(fd, F_SETFL, O_NONBLOCK);
bellardc4b1fcc2004-03-14 21:44:30 +00004020 return fd;
4021}
bellard7d3505c2004-05-12 19:32:15 +00004022#endif
bellardf1510b22003-06-25 00:07:40 +00004023
thsb46a8902007-10-21 23:20:45 +00004024static int launch_script(const char *setup_script, const char *ifname, int fd)
bellardc4b1fcc2004-03-14 21:44:30 +00004025{
thsb46a8902007-10-21 23:20:45 +00004026 int pid, status;
bellardc20709a2004-04-21 23:27:19 +00004027 char *args[3];
4028 char **parg;
4029
thsb46a8902007-10-21 23:20:45 +00004030 /* try to launch network script */
bellard7c9d8e02005-11-15 22:16:05 +00004031 pid = fork();
4032 if (pid >= 0) {
4033 if (pid == 0) {
ths50d3eea2007-03-19 16:36:43 +00004034 int open_max = sysconf (_SC_OPEN_MAX), i;
4035 for (i = 0; i < open_max; i++)
4036 if (i != STDIN_FILENO &&
4037 i != STDOUT_FILENO &&
4038 i != STDERR_FILENO &&
4039 i != fd)
4040 close(i);
4041
bellard7c9d8e02005-11-15 22:16:05 +00004042 parg = args;
4043 *parg++ = (char *)setup_script;
thsb46a8902007-10-21 23:20:45 +00004044 *parg++ = (char *)ifname;
bellard7c9d8e02005-11-15 22:16:05 +00004045 *parg++ = NULL;
4046 execv(setup_script, args);
bellard4a389402005-11-26 20:10:07 +00004047 _exit(1);
bellard7c9d8e02005-11-15 22:16:05 +00004048 }
4049 while (waitpid(pid, &status, 0) != pid);
4050 if (!WIFEXITED(status) ||
4051 WEXITSTATUS(status) != 0) {
4052 fprintf(stderr, "%s: could not launch network script\n",
4053 setup_script);
4054 return -1;
4055 }
bellardc20709a2004-04-21 23:27:19 +00004056 }
thsb46a8902007-10-21 23:20:45 +00004057 return 0;
4058}
4059
4060static int net_tap_init(VLANState *vlan, const char *ifname1,
4061 const char *setup_script, const char *down_script)
4062{
4063 TAPState *s;
4064 int fd;
4065 char ifname[128];
4066
4067 if (ifname1 != NULL)
4068 pstrcpy(ifname, sizeof(ifname), ifname1);
4069 else
4070 ifname[0] = '\0';
4071 TFR(fd = tap_open(ifname, sizeof(ifname)));
4072 if (fd < 0)
4073 return -1;
4074
4075 if (!setup_script || !strcmp(setup_script, "no"))
4076 setup_script = "";
4077 if (setup_script[0] != '\0') {
4078 if (launch_script(setup_script, ifname, fd))
4079 return -1;
bellardc20709a2004-04-21 23:27:19 +00004080 }
bellard7c9d8e02005-11-15 22:16:05 +00004081 s = net_tap_fd_init(vlan, fd);
4082 if (!s)
4083 return -1;
ths5fafdf22007-09-16 21:08:06 +00004084 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
bellard7c9d8e02005-11-15 22:16:05 +00004085 "tap: ifname=%s setup_script=%s", ifname, setup_script);
thsb46a8902007-10-21 23:20:45 +00004086 if (down_script && strcmp(down_script, "no"))
4087 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
bellardc20709a2004-04-21 23:27:19 +00004088 return 0;
4089}
4090
bellardfd1dff42006-02-01 21:29:26 +00004091#endif /* !_WIN32 */
4092
bellard7c9d8e02005-11-15 22:16:05 +00004093/* network connection */
4094typedef struct NetSocketState {
4095 VLANClientState *vc;
4096 int fd;
4097 int state; /* 0 = getting length, 1 = getting data */
4098 int index;
4099 int packet_len;
4100 uint8_t buf[4096];
bellard3d830452005-12-18 16:36:49 +00004101 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
bellard7c9d8e02005-11-15 22:16:05 +00004102} NetSocketState;
4103
4104typedef struct NetSocketListenState {
4105 VLANState *vlan;
4106 int fd;
4107} NetSocketListenState;
4108
4109/* XXX: we consider we can send the whole packet without blocking */
4110static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
bellardc20709a2004-04-21 23:27:19 +00004111{
bellard7c9d8e02005-11-15 22:16:05 +00004112 NetSocketState *s = opaque;
4113 uint32_t len;
4114 len = htonl(size);
4115
bellardfd1dff42006-02-01 21:29:26 +00004116 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4117 send_all(s->fd, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00004118}
4119
bellard3d830452005-12-18 16:36:49 +00004120static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4121{
4122 NetSocketState *s = opaque;
ths5fafdf22007-09-16 21:08:06 +00004123 sendto(s->fd, buf, size, 0,
bellard3d830452005-12-18 16:36:49 +00004124 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4125}
4126
bellard7c9d8e02005-11-15 22:16:05 +00004127static void net_socket_send(void *opaque)
4128{
4129 NetSocketState *s = opaque;
bellardfd1dff42006-02-01 21:29:26 +00004130 int l, size, err;
bellard7c9d8e02005-11-15 22:16:05 +00004131 uint8_t buf1[4096];
4132 const uint8_t *buf;
4133
bellardfd1dff42006-02-01 21:29:26 +00004134 size = recv(s->fd, buf1, sizeof(buf1), 0);
4135 if (size < 0) {
4136 err = socket_error();
ths5fafdf22007-09-16 21:08:06 +00004137 if (err != EWOULDBLOCK)
bellardfd1dff42006-02-01 21:29:26 +00004138 goto eoc;
4139 } else if (size == 0) {
bellard7c9d8e02005-11-15 22:16:05 +00004140 /* end of connection */
bellardfd1dff42006-02-01 21:29:26 +00004141 eoc:
bellard7c9d8e02005-11-15 22:16:05 +00004142 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
bellardfd1dff42006-02-01 21:29:26 +00004143 closesocket(s->fd);
bellard7c9d8e02005-11-15 22:16:05 +00004144 return;
4145 }
4146 buf = buf1;
4147 while (size > 0) {
4148 /* reassemble a packet from the network */
4149 switch(s->state) {
4150 case 0:
4151 l = 4 - s->index;
4152 if (l > size)
4153 l = size;
4154 memcpy(s->buf + s->index, buf, l);
4155 buf += l;
4156 size -= l;
4157 s->index += l;
4158 if (s->index == 4) {
4159 /* got length */
4160 s->packet_len = ntohl(*(uint32_t *)s->buf);
4161 s->index = 0;
4162 s->state = 1;
4163 }
4164 break;
4165 case 1:
4166 l = s->packet_len - s->index;
4167 if (l > size)
4168 l = size;
4169 memcpy(s->buf + s->index, buf, l);
4170 s->index += l;
4171 buf += l;
4172 size -= l;
4173 if (s->index >= s->packet_len) {
4174 qemu_send_packet(s->vc, s->buf, s->packet_len);
4175 s->index = 0;
4176 s->state = 0;
4177 }
4178 break;
4179 }
4180 }
4181}
4182
bellard3d830452005-12-18 16:36:49 +00004183static void net_socket_send_dgram(void *opaque)
4184{
4185 NetSocketState *s = opaque;
4186 int size;
4187
4188 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
ths5fafdf22007-09-16 21:08:06 +00004189 if (size < 0)
bellard3d830452005-12-18 16:36:49 +00004190 return;
4191 if (size == 0) {
4192 /* end of connection */
4193 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4194 return;
4195 }
4196 qemu_send_packet(s->vc, s->buf, size);
4197}
4198
4199static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4200{
4201 struct ip_mreq imr;
4202 int fd;
4203 int val, ret;
4204 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4205 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
ths5fafdf22007-09-16 21:08:06 +00004206 inet_ntoa(mcastaddr->sin_addr),
bellardfd1dff42006-02-01 21:29:26 +00004207 (int)ntohl(mcastaddr->sin_addr.s_addr));
bellard3d830452005-12-18 16:36:49 +00004208 return -1;
4209
4210 }
4211 fd = socket(PF_INET, SOCK_DGRAM, 0);
4212 if (fd < 0) {
4213 perror("socket(PF_INET, SOCK_DGRAM)");
4214 return -1;
4215 }
4216
bellardfd1dff42006-02-01 21:29:26 +00004217 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004218 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
bellardfd1dff42006-02-01 21:29:26 +00004219 (const char *)&val, sizeof(val));
4220 if (ret < 0) {
4221 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4222 goto fail;
4223 }
4224
4225 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4226 if (ret < 0) {
4227 perror("bind");
4228 goto fail;
4229 }
ths3b46e622007-09-17 08:09:54 +00004230
bellard3d830452005-12-18 16:36:49 +00004231 /* Add host to multicast group */
4232 imr.imr_multiaddr = mcastaddr->sin_addr;
4233 imr.imr_interface.s_addr = htonl(INADDR_ANY);
4234
ths5fafdf22007-09-16 21:08:06 +00004235 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
bellardfd1dff42006-02-01 21:29:26 +00004236 (const char *)&imr, sizeof(struct ip_mreq));
bellard3d830452005-12-18 16:36:49 +00004237 if (ret < 0) {
4238 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4239 goto fail;
4240 }
4241
4242 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4243 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004244 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
bellardfd1dff42006-02-01 21:29:26 +00004245 (const char *)&val, sizeof(val));
bellard3d830452005-12-18 16:36:49 +00004246 if (ret < 0) {
4247 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4248 goto fail;
4249 }
4250
bellardfd1dff42006-02-01 21:29:26 +00004251 socket_set_nonblock(fd);
bellard3d830452005-12-18 16:36:49 +00004252 return fd;
4253fail:
ths5fafdf22007-09-16 21:08:06 +00004254 if (fd >= 0)
bellard0bab00f2006-06-25 14:49:44 +00004255 closesocket(fd);
bellard3d830452005-12-18 16:36:49 +00004256 return -1;
4257}
4258
ths5fafdf22007-09-16 21:08:06 +00004259static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004260 int is_connected)
4261{
4262 struct sockaddr_in saddr;
4263 int newfd;
4264 socklen_t saddr_len;
4265 NetSocketState *s;
4266
4267 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
ths5fafdf22007-09-16 21:08:06 +00004268 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
bellard3d830452005-12-18 16:36:49 +00004269 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4270 */
4271
4272 if (is_connected) {
4273 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4274 /* must be bound */
4275 if (saddr.sin_addr.s_addr==0) {
4276 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4277 fd);
4278 return NULL;
4279 }
4280 /* clone dgram socket */
4281 newfd = net_socket_mcast_create(&saddr);
4282 if (newfd < 0) {
4283 /* error already reported by net_socket_mcast_create() */
4284 close(fd);
4285 return NULL;
4286 }
4287 /* clone newfd to fd, close newfd */
4288 dup2(newfd, fd);
4289 close(newfd);
ths5fafdf22007-09-16 21:08:06 +00004290
bellard3d830452005-12-18 16:36:49 +00004291 } else {
4292 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4293 fd, strerror(errno));
4294 return NULL;
4295 }
4296 }
4297
4298 s = qemu_mallocz(sizeof(NetSocketState));
4299 if (!s)
4300 return NULL;
4301 s->fd = fd;
4302
pbrookd861b052006-02-04 22:15:28 +00004303 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
bellard3d830452005-12-18 16:36:49 +00004304 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4305
4306 /* mcast: save bound address as dst */
4307 if (is_connected) s->dgram_dst=saddr;
4308
4309 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004310 "socket: fd=%d (%s mcast=%s:%d)",
bellard3d830452005-12-18 16:36:49 +00004311 fd, is_connected? "cloned" : "",
4312 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4313 return s;
4314}
4315
bellard7c9d8e02005-11-15 22:16:05 +00004316static void net_socket_connect(void *opaque)
4317{
4318 NetSocketState *s = opaque;
4319 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4320}
4321
ths5fafdf22007-09-16 21:08:06 +00004322static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
bellard7c9d8e02005-11-15 22:16:05 +00004323 int is_connected)
4324{
4325 NetSocketState *s;
4326 s = qemu_mallocz(sizeof(NetSocketState));
4327 if (!s)
4328 return NULL;
4329 s->fd = fd;
ths5fafdf22007-09-16 21:08:06 +00004330 s->vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00004331 net_socket_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00004332 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4333 "socket: fd=%d", fd);
4334 if (is_connected) {
4335 net_socket_connect(s);
4336 } else {
4337 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4338 }
4339 return s;
4340}
4341
ths5fafdf22007-09-16 21:08:06 +00004342static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004343 int is_connected)
4344{
4345 int so_type=-1, optlen=sizeof(so_type);
4346
bellardfd1dff42006-02-01 21:29:26 +00004347 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) {
ths931f03e2007-04-28 20:49:36 +00004348 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
bellard3d830452005-12-18 16:36:49 +00004349 return NULL;
4350 }
4351 switch(so_type) {
4352 case SOCK_DGRAM:
4353 return net_socket_fd_init_dgram(vlan, fd, is_connected);
4354 case SOCK_STREAM:
4355 return net_socket_fd_init_stream(vlan, fd, is_connected);
4356 default:
4357 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4358 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4359 return net_socket_fd_init_stream(vlan, fd, is_connected);
4360 }
4361 return NULL;
4362}
4363
bellard7c9d8e02005-11-15 22:16:05 +00004364static void net_socket_accept(void *opaque)
4365{
ths3b46e622007-09-17 08:09:54 +00004366 NetSocketListenState *s = opaque;
bellard7c9d8e02005-11-15 22:16:05 +00004367 NetSocketState *s1;
4368 struct sockaddr_in saddr;
4369 socklen_t len;
4370 int fd;
4371
4372 for(;;) {
4373 len = sizeof(saddr);
4374 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4375 if (fd < 0 && errno != EINTR) {
4376 return;
4377 } else if (fd >= 0) {
4378 break;
4379 }
4380 }
ths5fafdf22007-09-16 21:08:06 +00004381 s1 = net_socket_fd_init(s->vlan, fd, 1);
bellard7c9d8e02005-11-15 22:16:05 +00004382 if (!s1) {
bellard0bab00f2006-06-25 14:49:44 +00004383 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004384 } else {
4385 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004386 "socket: connection from %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004387 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4388 }
4389}
4390
4391static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4392{
4393 NetSocketListenState *s;
4394 int fd, val, ret;
4395 struct sockaddr_in saddr;
4396
4397 if (parse_host_port(&saddr, host_str) < 0)
4398 return -1;
ths3b46e622007-09-17 08:09:54 +00004399
bellard7c9d8e02005-11-15 22:16:05 +00004400 s = qemu_mallocz(sizeof(NetSocketListenState));
4401 if (!s)
4402 return -1;
4403
4404 fd = socket(PF_INET, SOCK_STREAM, 0);
4405 if (fd < 0) {
4406 perror("socket");
4407 return -1;
4408 }
bellardfd1dff42006-02-01 21:29:26 +00004409 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004410
4411 /* allow fast reuse */
4412 val = 1;
bellardfd1dff42006-02-01 21:29:26 +00004413 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00004414
bellard7c9d8e02005-11-15 22:16:05 +00004415 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4416 if (ret < 0) {
4417 perror("bind");
4418 return -1;
4419 }
4420 ret = listen(fd, 0);
4421 if (ret < 0) {
4422 perror("listen");
4423 return -1;
4424 }
4425 s->vlan = vlan;
4426 s->fd = fd;
4427 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4428 return 0;
4429}
4430
4431static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4432{
4433 NetSocketState *s;
bellardfd1dff42006-02-01 21:29:26 +00004434 int fd, connected, ret, err;
bellard7c9d8e02005-11-15 22:16:05 +00004435 struct sockaddr_in saddr;
4436
4437 if (parse_host_port(&saddr, host_str) < 0)
4438 return -1;
4439
4440 fd = socket(PF_INET, SOCK_STREAM, 0);
4441 if (fd < 0) {
4442 perror("socket");
4443 return -1;
4444 }
bellardfd1dff42006-02-01 21:29:26 +00004445 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004446
4447 connected = 0;
4448 for(;;) {
4449 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4450 if (ret < 0) {
bellardfd1dff42006-02-01 21:29:26 +00004451 err = socket_error();
4452 if (err == EINTR || err == EWOULDBLOCK) {
4453 } else if (err == EINPROGRESS) {
bellard7c9d8e02005-11-15 22:16:05 +00004454 break;
thsf5b12262007-03-25 15:58:03 +00004455#ifdef _WIN32
4456 } else if (err == WSAEALREADY) {
4457 break;
4458#endif
bellard7c9d8e02005-11-15 22:16:05 +00004459 } else {
4460 perror("connect");
bellardfd1dff42006-02-01 21:29:26 +00004461 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004462 return -1;
4463 }
4464 } else {
4465 connected = 1;
4466 break;
4467 }
4468 }
4469 s = net_socket_fd_init(vlan, fd, connected);
4470 if (!s)
4471 return -1;
4472 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004473 "socket: connect to %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004474 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
bellardc20709a2004-04-21 23:27:19 +00004475 return 0;
4476}
4477
bellard3d830452005-12-18 16:36:49 +00004478static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4479{
4480 NetSocketState *s;
4481 int fd;
4482 struct sockaddr_in saddr;
4483
4484 if (parse_host_port(&saddr, host_str) < 0)
4485 return -1;
4486
4487
4488 fd = net_socket_mcast_create(&saddr);
4489 if (fd < 0)
4490 return -1;
4491
4492 s = net_socket_fd_init(vlan, fd, 0);
4493 if (!s)
4494 return -1;
4495
4496 s->dgram_dst = saddr;
ths3b46e622007-09-17 08:09:54 +00004497
bellard3d830452005-12-18 16:36:49 +00004498 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004499 "socket: mcast=%s:%d",
bellard3d830452005-12-18 16:36:49 +00004500 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4501 return 0;
4502
4503}
4504
bellard7c9d8e02005-11-15 22:16:05 +00004505static int get_param_value(char *buf, int buf_size,
4506 const char *tag, const char *str)
4507{
4508 const char *p;
4509 char *q;
4510 char option[128];
4511
4512 p = str;
4513 for(;;) {
4514 q = option;
4515 while (*p != '\0' && *p != '=') {
4516 if ((q - option) < sizeof(option) - 1)
4517 *q++ = *p;
4518 p++;
4519 }
4520 *q = '\0';
4521 if (*p != '=')
4522 break;
4523 p++;
4524 if (!strcmp(tag, option)) {
4525 q = buf;
4526 while (*p != '\0' && *p != ',') {
4527 if ((q - buf) < buf_size - 1)
4528 *q++ = *p;
4529 p++;
4530 }
4531 *q = '\0';
4532 return q - buf;
4533 } else {
4534 while (*p != '\0' && *p != ',') {
4535 p++;
4536 }
4537 }
4538 if (*p != ',')
4539 break;
4540 p++;
4541 }
4542 return 0;
4543}
4544
ths52f61fd2006-12-22 21:20:52 +00004545static int net_client_init(const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00004546{
4547 const char *p;
4548 char *q;
4549 char device[64];
4550 char buf[1024];
4551 int vlan_id, ret;
4552 VLANState *vlan;
4553
4554 p = str;
4555 q = device;
4556 while (*p != '\0' && *p != ',') {
4557 if ((q - device) < sizeof(device) - 1)
4558 *q++ = *p;
4559 p++;
4560 }
4561 *q = '\0';
4562 if (*p == ',')
4563 p++;
4564 vlan_id = 0;
4565 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
4566 vlan_id = strtol(buf, NULL, 0);
4567 }
4568 vlan = qemu_find_vlan(vlan_id);
4569 if (!vlan) {
4570 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
4571 return -1;
4572 }
4573 if (!strcmp(device, "nic")) {
4574 NICInfo *nd;
4575 uint8_t *macaddr;
4576
4577 if (nb_nics >= MAX_NICS) {
4578 fprintf(stderr, "Too Many NICs\n");
4579 return -1;
4580 }
4581 nd = &nd_table[nb_nics];
4582 macaddr = nd->macaddr;
4583 macaddr[0] = 0x52;
4584 macaddr[1] = 0x54;
4585 macaddr[2] = 0x00;
4586 macaddr[3] = 0x12;
4587 macaddr[4] = 0x34;
4588 macaddr[5] = 0x56 + nb_nics;
4589
4590 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
4591 if (parse_macaddr(macaddr, buf) < 0) {
4592 fprintf(stderr, "invalid syntax for ethernet address\n");
4593 return -1;
4594 }
4595 }
pbrooka41b2ff2006-02-05 04:14:41 +00004596 if (get_param_value(buf, sizeof(buf), "model", p)) {
4597 nd->model = strdup(buf);
4598 }
bellard7c9d8e02005-11-15 22:16:05 +00004599 nd->vlan = vlan;
4600 nb_nics++;
blueswir1833c7172007-05-27 19:36:43 +00004601 vlan->nb_guest_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004602 ret = 0;
4603 } else
4604 if (!strcmp(device, "none")) {
4605 /* does nothing. It is needed to signal that no network cards
4606 are wanted */
4607 ret = 0;
4608 } else
4609#ifdef CONFIG_SLIRP
4610 if (!strcmp(device, "user")) {
pbrook115defd2006-04-16 11:06:58 +00004611 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
bellard3f423c92006-04-30 21:34:15 +00004612 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
pbrook115defd2006-04-16 11:06:58 +00004613 }
blueswir1833c7172007-05-27 19:36:43 +00004614 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004615 ret = net_slirp_init(vlan);
4616 } else
4617#endif
bellard7fb843f2006-02-01 23:06:55 +00004618#ifdef _WIN32
4619 if (!strcmp(device, "tap")) {
4620 char ifname[64];
4621 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4622 fprintf(stderr, "tap: no interface name\n");
4623 return -1;
4624 }
blueswir1833c7172007-05-27 19:36:43 +00004625 vlan->nb_host_devs++;
bellard7fb843f2006-02-01 23:06:55 +00004626 ret = tap_win32_init(vlan, ifname);
4627 } else
4628#else
bellard7c9d8e02005-11-15 22:16:05 +00004629 if (!strcmp(device, "tap")) {
4630 char ifname[64];
thsb46a8902007-10-21 23:20:45 +00004631 char setup_script[1024], down_script[1024];
bellard7c9d8e02005-11-15 22:16:05 +00004632 int fd;
pbrook4f010352007-05-28 02:29:59 +00004633 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004634 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4635 fd = strtol(buf, NULL, 0);
4636 ret = -1;
4637 if (net_tap_fd_init(vlan, fd))
4638 ret = 0;
4639 } else {
bellardbf8c5342007-01-09 19:44:41 +00004640 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4641 ifname[0] = '\0';
4642 }
bellard7c9d8e02005-11-15 22:16:05 +00004643 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
4644 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
4645 }
thsb46a8902007-10-21 23:20:45 +00004646 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
4647 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
4648 }
4649 ret = net_tap_init(vlan, ifname, setup_script, down_script);
bellard7c9d8e02005-11-15 22:16:05 +00004650 }
4651 } else
bellardfd1dff42006-02-01 21:29:26 +00004652#endif
bellard7c9d8e02005-11-15 22:16:05 +00004653 if (!strcmp(device, "socket")) {
4654 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4655 int fd;
4656 fd = strtol(buf, NULL, 0);
4657 ret = -1;
4658 if (net_socket_fd_init(vlan, fd, 1))
4659 ret = 0;
4660 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
4661 ret = net_socket_listen_init(vlan, buf);
4662 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
4663 ret = net_socket_connect_init(vlan, buf);
bellard3d830452005-12-18 16:36:49 +00004664 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
4665 ret = net_socket_mcast_init(vlan, buf);
bellard7c9d8e02005-11-15 22:16:05 +00004666 } else {
4667 fprintf(stderr, "Unknown socket options: %s\n", p);
4668 return -1;
4669 }
blueswir1833c7172007-05-27 19:36:43 +00004670 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004671 } else
bellard7c9d8e02005-11-15 22:16:05 +00004672 {
4673 fprintf(stderr, "Unknown network device: %s\n", device);
4674 return -1;
4675 }
4676 if (ret < 0) {
4677 fprintf(stderr, "Could not initialize device '%s'\n", device);
4678 }
ths3b46e622007-09-17 08:09:54 +00004679
bellard7c9d8e02005-11-15 22:16:05 +00004680 return ret;
4681}
4682
4683void do_info_network(void)
4684{
4685 VLANState *vlan;
4686 VLANClientState *vc;
4687
4688 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
4689 term_printf("VLAN %d devices:\n", vlan->id);
4690 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
4691 term_printf(" %s\n", vc->info_str);
4692 }
4693}
ths42550fd2006-12-22 16:34:12 +00004694
bellard330d0412003-07-26 18:11:40 +00004695/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00004696/* USB devices */
4697
pbrook0d92ed32006-05-21 16:30:15 +00004698static USBPort *used_usb_ports;
4699static USBPort *free_usb_ports;
4700
4701/* ??? Maybe change this to register a hub to keep track of the topology. */
4702void qemu_register_usb_port(USBPort *port, void *opaque, int index,
4703 usb_attachfn attach)
4704{
4705 port->opaque = opaque;
4706 port->index = index;
4707 port->attach = attach;
4708 port->next = free_usb_ports;
4709 free_usb_ports = port;
4710}
4711
bellarda594cfb2005-11-06 16:13:29 +00004712static int usb_device_add(const char *devname)
4713{
4714 const char *p;
4715 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004716 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00004717
pbrook0d92ed32006-05-21 16:30:15 +00004718 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00004719 return -1;
4720
4721 if (strstart(devname, "host:", &p)) {
4722 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00004723 } else if (!strcmp(devname, "mouse")) {
4724 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00004725 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00004726 dev = usb_tablet_init();
4727 } else if (!strcmp(devname, "keyboard")) {
4728 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00004729 } else if (strstart(devname, "disk:", &p)) {
4730 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00004731 } else if (!strcmp(devname, "wacom-tablet")) {
4732 dev = usb_wacom_init();
bellarda594cfb2005-11-06 16:13:29 +00004733 } else {
4734 return -1;
4735 }
pbrook0d92ed32006-05-21 16:30:15 +00004736 if (!dev)
4737 return -1;
4738
4739 /* Find a USB port to add the device to. */
4740 port = free_usb_ports;
4741 if (!port->next) {
4742 USBDevice *hub;
4743
4744 /* Create a new hub and chain it on. */
4745 free_usb_ports = NULL;
4746 port->next = used_usb_ports;
4747 used_usb_ports = port;
4748
4749 hub = usb_hub_init(VM_USB_HUB_SIZE);
4750 usb_attach(port, hub);
4751 port = free_usb_ports;
4752 }
4753
4754 free_usb_ports = port->next;
4755 port->next = used_usb_ports;
4756 used_usb_ports = port;
4757 usb_attach(port, dev);
bellarda594cfb2005-11-06 16:13:29 +00004758 return 0;
4759}
4760
4761static int usb_device_del(const char *devname)
4762{
pbrook0d92ed32006-05-21 16:30:15 +00004763 USBPort *port;
4764 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00004765 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004766 int bus_num, addr;
bellarda594cfb2005-11-06 16:13:29 +00004767 const char *p;
4768
pbrook0d92ed32006-05-21 16:30:15 +00004769 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00004770 return -1;
4771
4772 p = strchr(devname, '.');
ths5fafdf22007-09-16 21:08:06 +00004773 if (!p)
bellarda594cfb2005-11-06 16:13:29 +00004774 return -1;
4775 bus_num = strtoul(devname, NULL, 0);
4776 addr = strtoul(p + 1, NULL, 0);
4777 if (bus_num != 0)
4778 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00004779
4780 lastp = &used_usb_ports;
4781 port = used_usb_ports;
4782 while (port && port->dev->addr != addr) {
4783 lastp = &port->next;
4784 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00004785 }
pbrook0d92ed32006-05-21 16:30:15 +00004786
4787 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00004788 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00004789
bellard059809e2006-07-19 18:06:15 +00004790 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00004791 *lastp = port->next;
4792 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00004793 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00004794 port->next = free_usb_ports;
4795 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00004796 return 0;
4797}
4798
4799void do_usb_add(const char *devname)
4800{
4801 int ret;
4802 ret = usb_device_add(devname);
ths5fafdf22007-09-16 21:08:06 +00004803 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00004804 term_printf("Could not add USB device '%s'\n", devname);
4805}
4806
4807void do_usb_del(const char *devname)
4808{
4809 int ret;
4810 ret = usb_device_del(devname);
ths5fafdf22007-09-16 21:08:06 +00004811 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00004812 term_printf("Could not remove USB device '%s'\n", devname);
4813}
4814
4815void usb_info(void)
4816{
4817 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004818 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00004819 const char *speed_str;
4820
pbrook0d92ed32006-05-21 16:30:15 +00004821 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00004822 term_printf("USB support not enabled\n");
4823 return;
4824 }
4825
pbrook0d92ed32006-05-21 16:30:15 +00004826 for (port = used_usb_ports; port; port = port->next) {
4827 dev = port->dev;
4828 if (!dev)
4829 continue;
4830 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00004831 case USB_SPEED_LOW:
4832 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00004833 break;
ths5fafdf22007-09-16 21:08:06 +00004834 case USB_SPEED_FULL:
4835 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00004836 break;
ths5fafdf22007-09-16 21:08:06 +00004837 case USB_SPEED_HIGH:
4838 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00004839 break;
4840 default:
ths5fafdf22007-09-16 21:08:06 +00004841 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00004842 break;
bellarda594cfb2005-11-06 16:13:29 +00004843 }
ths5fafdf22007-09-16 21:08:06 +00004844 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00004845 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00004846 }
4847}
4848
bellardf7cce892004-12-08 22:21:25 +00004849/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00004850/* PCMCIA/Cardbus */
4851
4852static struct pcmcia_socket_entry_s {
4853 struct pcmcia_socket_s *socket;
4854 struct pcmcia_socket_entry_s *next;
4855} *pcmcia_sockets = 0;
4856
4857void pcmcia_socket_register(struct pcmcia_socket_s *socket)
4858{
4859 struct pcmcia_socket_entry_s *entry;
4860
4861 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
4862 entry->socket = socket;
4863 entry->next = pcmcia_sockets;
4864 pcmcia_sockets = entry;
4865}
4866
4867void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
4868{
4869 struct pcmcia_socket_entry_s *entry, **ptr;
4870
4871 ptr = &pcmcia_sockets;
4872 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
4873 if (entry->socket == socket) {
4874 *ptr = entry->next;
4875 qemu_free(entry);
4876 }
4877}
4878
4879void pcmcia_info(void)
4880{
4881 struct pcmcia_socket_entry_s *iter;
4882 if (!pcmcia_sockets)
4883 term_printf("No PCMCIA sockets\n");
4884
4885 for (iter = pcmcia_sockets; iter; iter = iter->next)
4886 term_printf("%s: %s\n", iter->socket->slot_string,
4887 iter->socket->attached ? iter->socket->card_string :
4888 "Empty");
4889}
4890
4891/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00004892/* dumb display */
4893
4894static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
4895{
4896}
4897
4898static void dumb_resize(DisplayState *ds, int w, int h)
4899{
4900}
4901
4902static void dumb_refresh(DisplayState *ds)
4903{
4904#if defined(CONFIG_SDL)
4905 vga_hw_update();
4906#endif
4907}
4908
4909static void dumb_display_init(DisplayState *ds)
4910{
4911 ds->data = NULL;
4912 ds->linesize = 0;
4913 ds->depth = 0;
4914 ds->dpy_update = dumb_update;
4915 ds->dpy_resize = dumb_resize;
4916 ds->dpy_refresh = dumb_refresh;
4917}
4918
4919/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00004920/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00004921
bellardc4b1fcc2004-03-14 21:44:30 +00004922#define MAX_IO_HANDLERS 64
4923
4924typedef struct IOHandlerRecord {
4925 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00004926 IOCanRWHandler *fd_read_poll;
4927 IOHandler *fd_read;
4928 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00004929 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00004930 void *opaque;
4931 /* temporary data */
4932 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00004933 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00004934} IOHandlerRecord;
4935
bellard8a7ddc32004-03-31 19:00:16 +00004936static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00004937
bellard7c9d8e02005-11-15 22:16:05 +00004938/* XXX: fd_read_poll should be suppressed, but an API change is
4939 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00004940int qemu_set_fd_handler2(int fd,
4941 IOCanRWHandler *fd_read_poll,
4942 IOHandler *fd_read,
4943 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00004944 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00004945{
bellard8a7ddc32004-03-31 19:00:16 +00004946 IOHandlerRecord **pioh, *ioh;
4947
bellard7c9d8e02005-11-15 22:16:05 +00004948 if (!fd_read && !fd_write) {
4949 pioh = &first_io_handler;
4950 for(;;) {
4951 ioh = *pioh;
4952 if (ioh == NULL)
4953 break;
4954 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00004955 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00004956 break;
4957 }
4958 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00004959 }
bellard7c9d8e02005-11-15 22:16:05 +00004960 } else {
4961 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4962 if (ioh->fd == fd)
4963 goto found;
4964 }
4965 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
4966 if (!ioh)
4967 return -1;
4968 ioh->next = first_io_handler;
4969 first_io_handler = ioh;
4970 found:
4971 ioh->fd = fd;
4972 ioh->fd_read_poll = fd_read_poll;
4973 ioh->fd_read = fd_read;
4974 ioh->fd_write = fd_write;
4975 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00004976 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00004977 }
bellard7c9d8e02005-11-15 22:16:05 +00004978 return 0;
4979}
4980
ths5fafdf22007-09-16 21:08:06 +00004981int qemu_set_fd_handler(int fd,
4982 IOHandler *fd_read,
4983 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00004984 void *opaque)
4985{
4986 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00004987}
4988
bellard8a7ddc32004-03-31 19:00:16 +00004989/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00004990/* Polling handling */
4991
4992typedef struct PollingEntry {
4993 PollingFunc *func;
4994 void *opaque;
4995 struct PollingEntry *next;
4996} PollingEntry;
4997
4998static PollingEntry *first_polling_entry;
4999
5000int qemu_add_polling_cb(PollingFunc *func, void *opaque)
5001{
5002 PollingEntry **ppe, *pe;
5003 pe = qemu_mallocz(sizeof(PollingEntry));
5004 if (!pe)
5005 return -1;
5006 pe->func = func;
5007 pe->opaque = opaque;
5008 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
5009 *ppe = pe;
5010 return 0;
5011}
5012
5013void qemu_del_polling_cb(PollingFunc *func, void *opaque)
5014{
5015 PollingEntry **ppe, *pe;
5016 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
5017 pe = *ppe;
5018 if (pe->func == func && pe->opaque == opaque) {
5019 *ppe = pe->next;
5020 qemu_free(pe);
5021 break;
5022 }
5023 }
5024}
5025
bellarda18e5242006-06-25 17:18:27 +00005026#ifdef _WIN32
5027/***********************************************************/
5028/* Wait objects support */
5029typedef struct WaitObjects {
5030 int num;
5031 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
5032 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
5033 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
5034} WaitObjects;
5035
5036static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00005037
bellarda18e5242006-06-25 17:18:27 +00005038int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5039{
5040 WaitObjects *w = &wait_objects;
5041
5042 if (w->num >= MAXIMUM_WAIT_OBJECTS)
5043 return -1;
5044 w->events[w->num] = handle;
5045 w->func[w->num] = func;
5046 w->opaque[w->num] = opaque;
5047 w->num++;
5048 return 0;
5049}
5050
5051void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5052{
5053 int i, found;
5054 WaitObjects *w = &wait_objects;
5055
5056 found = 0;
5057 for (i = 0; i < w->num; i++) {
5058 if (w->events[i] == handle)
5059 found = 1;
5060 if (found) {
5061 w->events[i] = w->events[i + 1];
5062 w->func[i] = w->func[i + 1];
5063 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00005064 }
bellarda18e5242006-06-25 17:18:27 +00005065 }
5066 if (found)
5067 w->num--;
5068}
5069#endif
5070
bellardf3311102006-04-12 20:21:17 +00005071/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00005072/* savevm/loadvm support */
bellardb4608c02003-06-27 17:34:32 +00005073
bellardfaea38e2006-08-05 21:31:00 +00005074#define IO_BUF_SIZE 32768
5075
5076struct QEMUFile {
5077 FILE *outfile;
5078 BlockDriverState *bs;
5079 int is_file;
5080 int is_writable;
5081 int64_t base_offset;
5082 int64_t buf_offset; /* start of buffer when writing, end of buffer
5083 when reading */
5084 int buf_index;
5085 int buf_size; /* 0 when writing */
5086 uint8_t buf[IO_BUF_SIZE];
5087};
5088
5089QEMUFile *qemu_fopen(const char *filename, const char *mode)
5090{
5091 QEMUFile *f;
5092
5093 f = qemu_mallocz(sizeof(QEMUFile));
5094 if (!f)
5095 return NULL;
5096 if (!strcmp(mode, "wb")) {
5097 f->is_writable = 1;
5098 } else if (!strcmp(mode, "rb")) {
5099 f->is_writable = 0;
5100 } else {
5101 goto fail;
5102 }
5103 f->outfile = fopen(filename, mode);
5104 if (!f->outfile)
5105 goto fail;
5106 f->is_file = 1;
5107 return f;
5108 fail:
5109 if (f->outfile)
5110 fclose(f->outfile);
5111 qemu_free(f);
5112 return NULL;
5113}
5114
5115QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
5116{
5117 QEMUFile *f;
5118
5119 f = qemu_mallocz(sizeof(QEMUFile));
5120 if (!f)
5121 return NULL;
5122 f->is_file = 0;
5123 f->bs = bs;
5124 f->is_writable = is_writable;
5125 f->base_offset = offset;
5126 return f;
5127}
5128
5129void qemu_fflush(QEMUFile *f)
5130{
5131 if (!f->is_writable)
5132 return;
5133 if (f->buf_index > 0) {
5134 if (f->is_file) {
5135 fseek(f->outfile, f->buf_offset, SEEK_SET);
5136 fwrite(f->buf, 1, f->buf_index, f->outfile);
5137 } else {
ths5fafdf22007-09-16 21:08:06 +00005138 bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005139 f->buf, f->buf_index);
5140 }
5141 f->buf_offset += f->buf_index;
5142 f->buf_index = 0;
5143 }
5144}
5145
5146static void qemu_fill_buffer(QEMUFile *f)
5147{
5148 int len;
5149
5150 if (f->is_writable)
5151 return;
5152 if (f->is_file) {
5153 fseek(f->outfile, f->buf_offset, SEEK_SET);
5154 len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
5155 if (len < 0)
5156 len = 0;
5157 } else {
ths5fafdf22007-09-16 21:08:06 +00005158 len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005159 f->buf, IO_BUF_SIZE);
5160 if (len < 0)
5161 len = 0;
5162 }
5163 f->buf_index = 0;
5164 f->buf_size = len;
5165 f->buf_offset += len;
5166}
5167
5168void qemu_fclose(QEMUFile *f)
5169{
5170 if (f->is_writable)
5171 qemu_fflush(f);
5172 if (f->is_file) {
5173 fclose(f->outfile);
5174 }
5175 qemu_free(f);
5176}
5177
bellard8a7ddc32004-03-31 19:00:16 +00005178void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
5179{
bellardfaea38e2006-08-05 21:31:00 +00005180 int l;
5181 while (size > 0) {
5182 l = IO_BUF_SIZE - f->buf_index;
5183 if (l > size)
5184 l = size;
5185 memcpy(f->buf + f->buf_index, buf, l);
5186 f->buf_index += l;
5187 buf += l;
5188 size -= l;
5189 if (f->buf_index >= IO_BUF_SIZE)
5190 qemu_fflush(f);
5191 }
bellard8a7ddc32004-03-31 19:00:16 +00005192}
5193
5194void qemu_put_byte(QEMUFile *f, int v)
5195{
bellardfaea38e2006-08-05 21:31:00 +00005196 f->buf[f->buf_index++] = v;
5197 if (f->buf_index >= IO_BUF_SIZE)
5198 qemu_fflush(f);
5199}
5200
5201int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
5202{
5203 int size, l;
5204
5205 size = size1;
5206 while (size > 0) {
5207 l = f->buf_size - f->buf_index;
5208 if (l == 0) {
5209 qemu_fill_buffer(f);
5210 l = f->buf_size - f->buf_index;
5211 if (l == 0)
5212 break;
5213 }
5214 if (l > size)
5215 l = size;
5216 memcpy(buf, f->buf + f->buf_index, l);
5217 f->buf_index += l;
5218 buf += l;
5219 size -= l;
5220 }
5221 return size1 - size;
5222}
5223
5224int qemu_get_byte(QEMUFile *f)
5225{
5226 if (f->buf_index >= f->buf_size) {
5227 qemu_fill_buffer(f);
5228 if (f->buf_index >= f->buf_size)
5229 return 0;
5230 }
5231 return f->buf[f->buf_index++];
5232}
5233
5234int64_t qemu_ftell(QEMUFile *f)
5235{
5236 return f->buf_offset - f->buf_size + f->buf_index;
5237}
5238
5239int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
5240{
5241 if (whence == SEEK_SET) {
5242 /* nothing to do */
5243 } else if (whence == SEEK_CUR) {
5244 pos += qemu_ftell(f);
5245 } else {
5246 /* SEEK_END not supported */
5247 return -1;
5248 }
5249 if (f->is_writable) {
5250 qemu_fflush(f);
5251 f->buf_offset = pos;
5252 } else {
5253 f->buf_offset = pos;
5254 f->buf_index = 0;
5255 f->buf_size = 0;
5256 }
5257 return pos;
bellard8a7ddc32004-03-31 19:00:16 +00005258}
5259
5260void qemu_put_be16(QEMUFile *f, unsigned int v)
5261{
5262 qemu_put_byte(f, v >> 8);
5263 qemu_put_byte(f, v);
5264}
5265
5266void qemu_put_be32(QEMUFile *f, unsigned int v)
5267{
5268 qemu_put_byte(f, v >> 24);
5269 qemu_put_byte(f, v >> 16);
5270 qemu_put_byte(f, v >> 8);
5271 qemu_put_byte(f, v);
5272}
5273
5274void qemu_put_be64(QEMUFile *f, uint64_t v)
5275{
5276 qemu_put_be32(f, v >> 32);
5277 qemu_put_be32(f, v);
5278}
5279
bellard8a7ddc32004-03-31 19:00:16 +00005280unsigned int qemu_get_be16(QEMUFile *f)
5281{
5282 unsigned int v;
5283 v = qemu_get_byte(f) << 8;
5284 v |= qemu_get_byte(f);
5285 return v;
5286}
5287
5288unsigned int qemu_get_be32(QEMUFile *f)
5289{
5290 unsigned int v;
5291 v = qemu_get_byte(f) << 24;
5292 v |= qemu_get_byte(f) << 16;
5293 v |= qemu_get_byte(f) << 8;
5294 v |= qemu_get_byte(f);
5295 return v;
5296}
5297
5298uint64_t qemu_get_be64(QEMUFile *f)
5299{
5300 uint64_t v;
5301 v = (uint64_t)qemu_get_be32(f) << 32;
5302 v |= qemu_get_be32(f);
5303 return v;
5304}
5305
bellard8a7ddc32004-03-31 19:00:16 +00005306typedef struct SaveStateEntry {
5307 char idstr[256];
5308 int instance_id;
5309 int version_id;
5310 SaveStateHandler *save_state;
5311 LoadStateHandler *load_state;
5312 void *opaque;
5313 struct SaveStateEntry *next;
5314} SaveStateEntry;
5315
5316static SaveStateEntry *first_se;
5317
ths5fafdf22007-09-16 21:08:06 +00005318int register_savevm(const char *idstr,
5319 int instance_id,
bellard8a7ddc32004-03-31 19:00:16 +00005320 int version_id,
5321 SaveStateHandler *save_state,
5322 LoadStateHandler *load_state,
5323 void *opaque)
5324{
5325 SaveStateEntry *se, **pse;
5326
5327 se = qemu_malloc(sizeof(SaveStateEntry));
5328 if (!se)
5329 return -1;
5330 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
5331 se->instance_id = instance_id;
5332 se->version_id = version_id;
5333 se->save_state = save_state;
5334 se->load_state = load_state;
5335 se->opaque = opaque;
5336 se->next = NULL;
5337
5338 /* add at the end of list */
5339 pse = &first_se;
5340 while (*pse != NULL)
5341 pse = &(*pse)->next;
5342 *pse = se;
5343 return 0;
5344}
5345
5346#define QEMU_VM_FILE_MAGIC 0x5145564d
bellardfaea38e2006-08-05 21:31:00 +00005347#define QEMU_VM_FILE_VERSION 0x00000002
bellard8a7ddc32004-03-31 19:00:16 +00005348
bellardfaea38e2006-08-05 21:31:00 +00005349int qemu_savevm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005350{
5351 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005352 int len, ret;
5353 int64_t cur_pos, len_pos, total_len_pos;
bellard313aa562003-08-10 21:52:11 +00005354
bellard8a7ddc32004-03-31 19:00:16 +00005355 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
5356 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
bellardfaea38e2006-08-05 21:31:00 +00005357 total_len_pos = qemu_ftell(f);
5358 qemu_put_be64(f, 0); /* total size */
bellard8a7ddc32004-03-31 19:00:16 +00005359
5360 for(se = first_se; se != NULL; se = se->next) {
5361 /* ID string */
5362 len = strlen(se->idstr);
5363 qemu_put_byte(f, len);
5364 qemu_put_buffer(f, se->idstr, len);
5365
5366 qemu_put_be32(f, se->instance_id);
5367 qemu_put_be32(f, se->version_id);
5368
5369 /* record size: filled later */
bellardfaea38e2006-08-05 21:31:00 +00005370 len_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005371 qemu_put_be32(f, 0);
ths3b46e622007-09-17 08:09:54 +00005372
bellard8a7ddc32004-03-31 19:00:16 +00005373 se->save_state(f, se->opaque);
5374
5375 /* fill record size */
bellardfaea38e2006-08-05 21:31:00 +00005376 cur_pos = qemu_ftell(f);
5377 len = cur_pos - len_pos - 4;
5378 qemu_fseek(f, len_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005379 qemu_put_be32(f, len);
bellardfaea38e2006-08-05 21:31:00 +00005380 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005381 }
bellardfaea38e2006-08-05 21:31:00 +00005382 cur_pos = qemu_ftell(f);
5383 qemu_fseek(f, total_len_pos, SEEK_SET);
5384 qemu_put_be64(f, cur_pos - total_len_pos - 8);
5385 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005386
bellard8a7ddc32004-03-31 19:00:16 +00005387 ret = 0;
bellard8a7ddc32004-03-31 19:00:16 +00005388 return ret;
5389}
5390
5391static SaveStateEntry *find_se(const char *idstr, int instance_id)
5392{
5393 SaveStateEntry *se;
5394
5395 for(se = first_se; se != NULL; se = se->next) {
ths5fafdf22007-09-16 21:08:06 +00005396 if (!strcmp(se->idstr, idstr) &&
bellard8a7ddc32004-03-31 19:00:16 +00005397 instance_id == se->instance_id)
5398 return se;
5399 }
5400 return NULL;
5401}
5402
bellardfaea38e2006-08-05 21:31:00 +00005403int qemu_loadvm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005404{
5405 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005406 int len, ret, instance_id, record_len, version_id;
5407 int64_t total_len, end_pos, cur_pos;
bellard8a7ddc32004-03-31 19:00:16 +00005408 unsigned int v;
5409 char idstr[256];
ths3b46e622007-09-17 08:09:54 +00005410
bellard8a7ddc32004-03-31 19:00:16 +00005411 v = qemu_get_be32(f);
5412 if (v != QEMU_VM_FILE_MAGIC)
5413 goto fail;
5414 v = qemu_get_be32(f);
5415 if (v != QEMU_VM_FILE_VERSION) {
5416 fail:
bellard8a7ddc32004-03-31 19:00:16 +00005417 ret = -1;
5418 goto the_end;
5419 }
bellardfaea38e2006-08-05 21:31:00 +00005420 total_len = qemu_get_be64(f);
5421 end_pos = total_len + qemu_ftell(f);
bellardb4608c02003-06-27 17:34:32 +00005422 for(;;) {
bellardfaea38e2006-08-05 21:31:00 +00005423 if (qemu_ftell(f) >= end_pos)
bellard8a7ddc32004-03-31 19:00:16 +00005424 break;
bellardfaea38e2006-08-05 21:31:00 +00005425 len = qemu_get_byte(f);
bellard8a7ddc32004-03-31 19:00:16 +00005426 qemu_get_buffer(f, idstr, len);
5427 idstr[len] = '\0';
5428 instance_id = qemu_get_be32(f);
5429 version_id = qemu_get_be32(f);
5430 record_len = qemu_get_be32(f);
5431#if 0
ths5fafdf22007-09-16 21:08:06 +00005432 printf("idstr=%s instance=0x%x version=%d len=%d\n",
bellard8a7ddc32004-03-31 19:00:16 +00005433 idstr, instance_id, version_id, record_len);
bellardc45886d2004-01-05 00:02:06 +00005434#endif
bellardfaea38e2006-08-05 21:31:00 +00005435 cur_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005436 se = find_se(idstr, instance_id);
5437 if (!se) {
ths5fafdf22007-09-16 21:08:06 +00005438 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
bellard8a7ddc32004-03-31 19:00:16 +00005439 instance_id, idstr);
5440 } else {
5441 ret = se->load_state(f, se->opaque, version_id);
5442 if (ret < 0) {
ths5fafdf22007-09-16 21:08:06 +00005443 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
bellard8a7ddc32004-03-31 19:00:16 +00005444 instance_id, idstr);
5445 }
bellard34865132003-10-05 14:28:56 +00005446 }
bellard8a7ddc32004-03-31 19:00:16 +00005447 /* always seek to exact end of record */
5448 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
5449 }
bellard8a7ddc32004-03-31 19:00:16 +00005450 ret = 0;
5451 the_end:
bellardfaea38e2006-08-05 21:31:00 +00005452 return ret;
5453}
5454
5455/* device can contain snapshots */
5456static int bdrv_can_snapshot(BlockDriverState *bs)
5457{
5458 return (bs &&
5459 !bdrv_is_removable(bs) &&
5460 !bdrv_is_read_only(bs));
5461}
5462
5463/* device must be snapshots in order to have a reliable snapshot */
5464static int bdrv_has_snapshot(BlockDriverState *bs)
5465{
5466 return (bs &&
5467 !bdrv_is_removable(bs) &&
5468 !bdrv_is_read_only(bs));
5469}
5470
5471static BlockDriverState *get_bs_snapshots(void)
5472{
5473 BlockDriverState *bs;
5474 int i;
5475
5476 if (bs_snapshots)
5477 return bs_snapshots;
5478 for(i = 0; i <= MAX_DISKS; i++) {
5479 bs = bs_table[i];
5480 if (bdrv_can_snapshot(bs))
5481 goto ok;
5482 }
5483 return NULL;
5484 ok:
5485 bs_snapshots = bs;
5486 return bs;
5487}
5488
5489static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
5490 const char *name)
5491{
5492 QEMUSnapshotInfo *sn_tab, *sn;
5493 int nb_sns, i, ret;
ths3b46e622007-09-17 08:09:54 +00005494
bellardfaea38e2006-08-05 21:31:00 +00005495 ret = -ENOENT;
5496 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5497 if (nb_sns < 0)
5498 return ret;
5499 for(i = 0; i < nb_sns; i++) {
5500 sn = &sn_tab[i];
5501 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
5502 *sn_info = *sn;
5503 ret = 0;
5504 break;
5505 }
5506 }
5507 qemu_free(sn_tab);
5508 return ret;
5509}
5510
5511void do_savevm(const char *name)
5512{
5513 BlockDriverState *bs, *bs1;
5514 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
5515 int must_delete, ret, i;
5516 BlockDriverInfo bdi1, *bdi = &bdi1;
5517 QEMUFile *f;
5518 int saved_vm_running;
bellard4c279bd2006-08-17 09:43:50 +00005519#ifdef _WIN32
5520 struct _timeb tb;
5521#else
bellardfaea38e2006-08-05 21:31:00 +00005522 struct timeval tv;
bellard4c279bd2006-08-17 09:43:50 +00005523#endif
bellardfaea38e2006-08-05 21:31:00 +00005524
5525 bs = get_bs_snapshots();
5526 if (!bs) {
5527 term_printf("No block device can accept snapshots\n");
5528 return;
5529 }
5530
pbrook6192bc32006-09-03 12:08:37 +00005531 /* ??? Should this occur after vm_stop? */
5532 qemu_aio_flush();
5533
bellardfaea38e2006-08-05 21:31:00 +00005534 saved_vm_running = vm_running;
5535 vm_stop(0);
ths3b46e622007-09-17 08:09:54 +00005536
bellardfaea38e2006-08-05 21:31:00 +00005537 must_delete = 0;
5538 if (name) {
5539 ret = bdrv_snapshot_find(bs, old_sn, name);
5540 if (ret >= 0) {
5541 must_delete = 1;
5542 }
5543 }
5544 memset(sn, 0, sizeof(*sn));
5545 if (must_delete) {
5546 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
5547 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
5548 } else {
5549 if (name)
5550 pstrcpy(sn->name, sizeof(sn->name), name);
5551 }
5552
5553 /* fill auxiliary fields */
bellard4c279bd2006-08-17 09:43:50 +00005554#ifdef _WIN32
5555 _ftime(&tb);
5556 sn->date_sec = tb.time;
5557 sn->date_nsec = tb.millitm * 1000000;
5558#else
bellardfaea38e2006-08-05 21:31:00 +00005559 gettimeofday(&tv, NULL);
5560 sn->date_sec = tv.tv_sec;
5561 sn->date_nsec = tv.tv_usec * 1000;
bellard4c279bd2006-08-17 09:43:50 +00005562#endif
bellardfaea38e2006-08-05 21:31:00 +00005563 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
ths3b46e622007-09-17 08:09:54 +00005564
bellardfaea38e2006-08-05 21:31:00 +00005565 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5566 term_printf("Device %s does not support VM state snapshots\n",
5567 bdrv_get_device_name(bs));
5568 goto the_end;
5569 }
ths3b46e622007-09-17 08:09:54 +00005570
bellardfaea38e2006-08-05 21:31:00 +00005571 /* save the VM state */
5572 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
5573 if (!f) {
5574 term_printf("Could not open VM state file\n");
5575 goto the_end;
5576 }
5577 ret = qemu_savevm_state(f);
5578 sn->vm_state_size = qemu_ftell(f);
5579 qemu_fclose(f);
5580 if (ret < 0) {
5581 term_printf("Error %d while writing VM\n", ret);
5582 goto the_end;
5583 }
ths3b46e622007-09-17 08:09:54 +00005584
bellardfaea38e2006-08-05 21:31:00 +00005585 /* create the snapshots */
5586
5587 for(i = 0; i < MAX_DISKS; i++) {
5588 bs1 = bs_table[i];
5589 if (bdrv_has_snapshot(bs1)) {
5590 if (must_delete) {
5591 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
5592 if (ret < 0) {
5593 term_printf("Error while deleting snapshot on '%s'\n",
5594 bdrv_get_device_name(bs1));
5595 }
5596 }
5597 ret = bdrv_snapshot_create(bs1, sn);
5598 if (ret < 0) {
5599 term_printf("Error while creating snapshot on '%s'\n",
5600 bdrv_get_device_name(bs1));
5601 }
5602 }
5603 }
5604
5605 the_end:
bellard8a7ddc32004-03-31 19:00:16 +00005606 if (saved_vm_running)
5607 vm_start();
bellardfaea38e2006-08-05 21:31:00 +00005608}
5609
5610void do_loadvm(const char *name)
5611{
5612 BlockDriverState *bs, *bs1;
5613 BlockDriverInfo bdi1, *bdi = &bdi1;
5614 QEMUFile *f;
5615 int i, ret;
5616 int saved_vm_running;
5617
5618 bs = get_bs_snapshots();
5619 if (!bs) {
5620 term_printf("No block device supports snapshots\n");
5621 return;
5622 }
ths3b46e622007-09-17 08:09:54 +00005623
pbrook6192bc32006-09-03 12:08:37 +00005624 /* Flush all IO requests so they don't interfere with the new state. */
5625 qemu_aio_flush();
5626
bellardfaea38e2006-08-05 21:31:00 +00005627 saved_vm_running = vm_running;
5628 vm_stop(0);
5629
5630 for(i = 0; i <= MAX_DISKS; i++) {
5631 bs1 = bs_table[i];
5632 if (bdrv_has_snapshot(bs1)) {
5633 ret = bdrv_snapshot_goto(bs1, name);
5634 if (ret < 0) {
5635 if (bs != bs1)
5636 term_printf("Warning: ");
5637 switch(ret) {
5638 case -ENOTSUP:
5639 term_printf("Snapshots not supported on device '%s'\n",
5640 bdrv_get_device_name(bs1));
5641 break;
5642 case -ENOENT:
5643 term_printf("Could not find snapshot '%s' on device '%s'\n",
5644 name, bdrv_get_device_name(bs1));
5645 break;
5646 default:
5647 term_printf("Error %d while activating snapshot on '%s'\n",
5648 ret, bdrv_get_device_name(bs1));
5649 break;
5650 }
5651 /* fatal on snapshot block device */
5652 if (bs == bs1)
5653 goto the_end;
5654 }
5655 }
5656 }
5657
5658 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5659 term_printf("Device %s does not support VM state snapshots\n",
5660 bdrv_get_device_name(bs));
5661 return;
5662 }
ths3b46e622007-09-17 08:09:54 +00005663
bellardfaea38e2006-08-05 21:31:00 +00005664 /* restore the VM state */
5665 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
5666 if (!f) {
5667 term_printf("Could not open VM state file\n");
5668 goto the_end;
5669 }
5670 ret = qemu_loadvm_state(f);
5671 qemu_fclose(f);
5672 if (ret < 0) {
5673 term_printf("Error %d while loading VM state\n", ret);
5674 }
5675 the_end:
5676 if (saved_vm_running)
5677 vm_start();
5678}
5679
5680void do_delvm(const char *name)
5681{
5682 BlockDriverState *bs, *bs1;
5683 int i, ret;
5684
5685 bs = get_bs_snapshots();
5686 if (!bs) {
5687 term_printf("No block device supports snapshots\n");
5688 return;
5689 }
ths3b46e622007-09-17 08:09:54 +00005690
bellardfaea38e2006-08-05 21:31:00 +00005691 for(i = 0; i <= MAX_DISKS; i++) {
5692 bs1 = bs_table[i];
5693 if (bdrv_has_snapshot(bs1)) {
5694 ret = bdrv_snapshot_delete(bs1, name);
5695 if (ret < 0) {
5696 if (ret == -ENOTSUP)
5697 term_printf("Snapshots not supported on device '%s'\n",
5698 bdrv_get_device_name(bs1));
5699 else
5700 term_printf("Error %d while deleting snapshot on '%s'\n",
5701 ret, bdrv_get_device_name(bs1));
5702 }
5703 }
5704 }
5705}
5706
5707void do_info_snapshots(void)
5708{
5709 BlockDriverState *bs, *bs1;
5710 QEMUSnapshotInfo *sn_tab, *sn;
5711 int nb_sns, i;
5712 char buf[256];
5713
5714 bs = get_bs_snapshots();
5715 if (!bs) {
5716 term_printf("No available block device supports snapshots\n");
5717 return;
5718 }
5719 term_printf("Snapshot devices:");
5720 for(i = 0; i <= MAX_DISKS; i++) {
5721 bs1 = bs_table[i];
5722 if (bdrv_has_snapshot(bs1)) {
5723 if (bs == bs1)
5724 term_printf(" %s", bdrv_get_device_name(bs1));
5725 }
5726 }
5727 term_printf("\n");
5728
5729 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5730 if (nb_sns < 0) {
5731 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
5732 return;
5733 }
5734 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
5735 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
5736 for(i = 0; i < nb_sns; i++) {
5737 sn = &sn_tab[i];
5738 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
5739 }
5740 qemu_free(sn_tab);
bellard8a7ddc32004-03-31 19:00:16 +00005741}
5742
5743/***********************************************************/
5744/* cpu save/restore */
5745
5746#if defined(TARGET_I386)
5747
5748static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
5749{
bellard02ba45c2004-06-25 14:46:23 +00005750 qemu_put_be32(f, dt->selector);
bellard20f32282005-01-03 23:36:21 +00005751 qemu_put_betl(f, dt->base);
bellard8a7ddc32004-03-31 19:00:16 +00005752 qemu_put_be32(f, dt->limit);
5753 qemu_put_be32(f, dt->flags);
5754}
5755
5756static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
5757{
bellard02ba45c2004-06-25 14:46:23 +00005758 dt->selector = qemu_get_be32(f);
bellard20f32282005-01-03 23:36:21 +00005759 dt->base = qemu_get_betl(f);
bellard8a7ddc32004-03-31 19:00:16 +00005760 dt->limit = qemu_get_be32(f);
5761 dt->flags = qemu_get_be32(f);
5762}
5763
5764void cpu_save(QEMUFile *f, void *opaque)
5765{
5766 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00005767 uint16_t fptag, fpus, fpuc, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00005768 uint32_t hflags;
5769 int i;
ths3b46e622007-09-17 08:09:54 +00005770
bellard20f32282005-01-03 23:36:21 +00005771 for(i = 0; i < CPU_NB_REGS; i++)
5772 qemu_put_betls(f, &env->regs[i]);
5773 qemu_put_betls(f, &env->eip);
5774 qemu_put_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00005775 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
5776 qemu_put_be32s(f, &hflags);
ths3b46e622007-09-17 08:09:54 +00005777
bellard8a7ddc32004-03-31 19:00:16 +00005778 /* FPU */
5779 fpuc = env->fpuc;
5780 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
5781 fptag = 0;
bellard664e0f12005-01-08 18:58:29 +00005782 for(i = 0; i < 8; i++) {
5783 fptag |= ((!env->fptags[i]) << i);
bellard8a7ddc32004-03-31 19:00:16 +00005784 }
ths3b46e622007-09-17 08:09:54 +00005785
bellard8a7ddc32004-03-31 19:00:16 +00005786 qemu_put_be16s(f, &fpuc);
5787 qemu_put_be16s(f, &fpus);
5788 qemu_put_be16s(f, &fptag);
5789
bellard664e0f12005-01-08 18:58:29 +00005790#ifdef USE_X86LDOUBLE
5791 fpregs_format = 0;
5792#else
5793 fpregs_format = 1;
5794#endif
5795 qemu_put_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00005796
bellard8a7ddc32004-03-31 19:00:16 +00005797 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00005798#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00005799 {
5800 uint64_t mant;
5801 uint16_t exp;
5802 /* we save the real CPU data (in case of MMX usage only 'mant'
5803 contains the MMX register */
5804 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
5805 qemu_put_be64(f, mant);
5806 qemu_put_be16(f, exp);
5807 }
bellard664e0f12005-01-08 18:58:29 +00005808#else
5809 /* if we use doubles for float emulation, we save the doubles to
5810 avoid losing information in case of MMX usage. It can give
5811 problems if the image is restored on a CPU where long
5812 doubles are used instead. */
bellard8636b5d2005-01-09 00:03:14 +00005813 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
bellard664e0f12005-01-08 18:58:29 +00005814#endif
bellard8a7ddc32004-03-31 19:00:16 +00005815 }
5816
5817 for(i = 0; i < 6; i++)
5818 cpu_put_seg(f, &env->segs[i]);
5819 cpu_put_seg(f, &env->ldt);
5820 cpu_put_seg(f, &env->tr);
5821 cpu_put_seg(f, &env->gdt);
5822 cpu_put_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00005823
bellard8a7ddc32004-03-31 19:00:16 +00005824 qemu_put_be32s(f, &env->sysenter_cs);
5825 qemu_put_be32s(f, &env->sysenter_esp);
5826 qemu_put_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00005827
bellard20f32282005-01-03 23:36:21 +00005828 qemu_put_betls(f, &env->cr[0]);
5829 qemu_put_betls(f, &env->cr[2]);
5830 qemu_put_betls(f, &env->cr[3]);
5831 qemu_put_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00005832
bellard8a7ddc32004-03-31 19:00:16 +00005833 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00005834 qemu_put_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00005835
5836 /* MMU */
5837 qemu_put_be32s(f, &env->a20_mask);
bellard02536f82005-01-06 20:43:38 +00005838
bellard664e0f12005-01-08 18:58:29 +00005839 /* XMM */
5840 qemu_put_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00005841 for(i = 0; i < CPU_NB_REGS; i++) {
5842 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
5843 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
5844 }
5845
bellard664e0f12005-01-08 18:58:29 +00005846#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00005847 qemu_put_be64s(f, &env->efer);
5848 qemu_put_be64s(f, &env->star);
5849 qemu_put_be64s(f, &env->lstar);
5850 qemu_put_be64s(f, &env->cstar);
5851 qemu_put_be64s(f, &env->fmask);
5852 qemu_put_be64s(f, &env->kernelgsbase);
5853#endif
bellard3b21e032006-09-24 18:41:56 +00005854 qemu_put_be32s(f, &env->smbase);
bellard8a7ddc32004-03-31 19:00:16 +00005855}
5856
bellard8636b5d2005-01-09 00:03:14 +00005857#ifdef USE_X86LDOUBLE
bellard664e0f12005-01-08 18:58:29 +00005858/* XXX: add that in a FPU generic layer */
5859union x86_longdouble {
5860 uint64_t mant;
5861 uint16_t exp;
5862};
5863
5864#define MANTD1(fp) (fp & ((1LL << 52) - 1))
5865#define EXPBIAS1 1023
5866#define EXPD1(fp) ((fp >> 52) & 0x7FF)
5867#define SIGND1(fp) ((fp >> 32) & 0x80000000)
5868
5869static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
5870{
5871 int e;
5872 /* mantissa */
5873 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
5874 /* exponent + sign */
5875 e = EXPD1(temp) - EXPBIAS1 + 16383;
5876 e |= SIGND1(temp) >> 16;
5877 p->exp = e;
5878}
bellard8636b5d2005-01-09 00:03:14 +00005879#endif
bellard664e0f12005-01-08 18:58:29 +00005880
bellard8a7ddc32004-03-31 19:00:16 +00005881int cpu_load(QEMUFile *f, void *opaque, int version_id)
5882{
5883 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00005884 int i, guess_mmx;
bellard8a7ddc32004-03-31 19:00:16 +00005885 uint32_t hflags;
bellard664e0f12005-01-08 18:58:29 +00005886 uint16_t fpus, fpuc, fptag, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00005887
bellard3b21e032006-09-24 18:41:56 +00005888 if (version_id != 3 && version_id != 4)
bellard8a7ddc32004-03-31 19:00:16 +00005889 return -EINVAL;
bellard20f32282005-01-03 23:36:21 +00005890 for(i = 0; i < CPU_NB_REGS; i++)
5891 qemu_get_betls(f, &env->regs[i]);
5892 qemu_get_betls(f, &env->eip);
5893 qemu_get_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00005894 qemu_get_be32s(f, &hflags);
5895
5896 qemu_get_be16s(f, &fpuc);
5897 qemu_get_be16s(f, &fpus);
5898 qemu_get_be16s(f, &fptag);
bellard664e0f12005-01-08 18:58:29 +00005899 qemu_get_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00005900
bellard664e0f12005-01-08 18:58:29 +00005901 /* NOTE: we cannot always restore the FPU state if the image come
5902 from a host with a different 'USE_X86LDOUBLE' define. We guess
5903 if we are in an MMX state to restore correctly in that case. */
5904 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
bellard8a7ddc32004-03-31 19:00:16 +00005905 for(i = 0; i < 8; i++) {
5906 uint64_t mant;
5907 uint16_t exp;
ths3b46e622007-09-17 08:09:54 +00005908
bellard664e0f12005-01-08 18:58:29 +00005909 switch(fpregs_format) {
5910 case 0:
5911 mant = qemu_get_be64(f);
5912 exp = qemu_get_be16(f);
5913#ifdef USE_X86LDOUBLE
5914 env->fpregs[i].d = cpu_set_fp80(mant, exp);
5915#else
5916 /* difficult case */
5917 if (guess_mmx)
bellard8636b5d2005-01-09 00:03:14 +00005918 env->fpregs[i].mmx.MMX_Q(0) = mant;
bellard664e0f12005-01-08 18:58:29 +00005919 else
5920 env->fpregs[i].d = cpu_set_fp80(mant, exp);
5921#endif
5922 break;
5923 case 1:
5924 mant = qemu_get_be64(f);
5925#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00005926 {
5927 union x86_longdouble *p;
5928 /* difficult case */
5929 p = (void *)&env->fpregs[i];
5930 if (guess_mmx) {
5931 p->mant = mant;
5932 p->exp = 0xffff;
5933 } else {
5934 fp64_to_fp80(p, mant);
5935 }
bellard664e0f12005-01-08 18:58:29 +00005936 }
5937#else
bellard8636b5d2005-01-09 00:03:14 +00005938 env->fpregs[i].mmx.MMX_Q(0) = mant;
ths3b46e622007-09-17 08:09:54 +00005939#endif
bellard664e0f12005-01-08 18:58:29 +00005940 break;
5941 default:
5942 return -EINVAL;
5943 }
bellard8a7ddc32004-03-31 19:00:16 +00005944 }
5945
5946 env->fpuc = fpuc;
bellard7a0e1f42005-03-13 17:01:47 +00005947 /* XXX: restore FPU round state */
bellard8a7ddc32004-03-31 19:00:16 +00005948 env->fpstt = (fpus >> 11) & 7;
5949 env->fpus = fpus & ~0x3800;
bellard664e0f12005-01-08 18:58:29 +00005950 fptag ^= 0xff;
bellard8a7ddc32004-03-31 19:00:16 +00005951 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00005952 env->fptags[i] = (fptag >> i) & 1;
bellard8a7ddc32004-03-31 19:00:16 +00005953 }
ths3b46e622007-09-17 08:09:54 +00005954
bellard8a7ddc32004-03-31 19:00:16 +00005955 for(i = 0; i < 6; i++)
5956 cpu_get_seg(f, &env->segs[i]);
5957 cpu_get_seg(f, &env->ldt);
5958 cpu_get_seg(f, &env->tr);
5959 cpu_get_seg(f, &env->gdt);
5960 cpu_get_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00005961
bellard8a7ddc32004-03-31 19:00:16 +00005962 qemu_get_be32s(f, &env->sysenter_cs);
5963 qemu_get_be32s(f, &env->sysenter_esp);
5964 qemu_get_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00005965
bellard20f32282005-01-03 23:36:21 +00005966 qemu_get_betls(f, &env->cr[0]);
5967 qemu_get_betls(f, &env->cr[2]);
5968 qemu_get_betls(f, &env->cr[3]);
5969 qemu_get_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00005970
bellard8a7ddc32004-03-31 19:00:16 +00005971 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00005972 qemu_get_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00005973
5974 /* MMU */
5975 qemu_get_be32s(f, &env->a20_mask);
5976
bellard664e0f12005-01-08 18:58:29 +00005977 qemu_get_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00005978 for(i = 0; i < CPU_NB_REGS; i++) {
5979 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
5980 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
5981 }
5982
bellard664e0f12005-01-08 18:58:29 +00005983#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00005984 qemu_get_be64s(f, &env->efer);
5985 qemu_get_be64s(f, &env->star);
5986 qemu_get_be64s(f, &env->lstar);
5987 qemu_get_be64s(f, &env->cstar);
5988 qemu_get_be64s(f, &env->fmask);
5989 qemu_get_be64s(f, &env->kernelgsbase);
5990#endif
ths5fafdf22007-09-16 21:08:06 +00005991 if (version_id >= 4)
bellard3b21e032006-09-24 18:41:56 +00005992 qemu_get_be32s(f, &env->smbase);
bellard02536f82005-01-06 20:43:38 +00005993
bellard8a7ddc32004-03-31 19:00:16 +00005994 /* XXX: compute hflags from scratch, except for CPL and IIF */
5995 env->hflags = hflags;
5996 tlb_flush(env, 1);
5997 return 0;
5998}
5999
bellarda541f292004-04-12 20:39:29 +00006000#elif defined(TARGET_PPC)
6001void cpu_save(QEMUFile *f, void *opaque)
6002{
6003}
6004
6005int cpu_load(QEMUFile *f, void *opaque, int version_id)
6006{
6007 return 0;
6008}
bellard6af0bf92005-07-02 14:58:51 +00006009
6010#elif defined(TARGET_MIPS)
6011void cpu_save(QEMUFile *f, void *opaque)
6012{
6013}
6014
6015int cpu_load(QEMUFile *f, void *opaque, int version_id)
6016{
6017 return 0;
6018}
6019
bellarde95c8d52004-09-30 22:22:08 +00006020#elif defined(TARGET_SPARC)
6021void cpu_save(QEMUFile *f, void *opaque)
6022{
bellarde80cfcf2004-12-19 23:18:01 +00006023 CPUState *env = opaque;
6024 int i;
6025 uint32_t tmp;
6026
bellard4fa5d772005-01-30 22:57:54 +00006027 for(i = 0; i < 8; i++)
6028 qemu_put_betls(f, &env->gregs[i]);
6029 for(i = 0; i < NWINDOWS * 16; i++)
6030 qemu_put_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006031
6032 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006033 for(i = 0; i < TARGET_FPREGS; i++) {
6034 union {
bellard1bdb68e2006-06-21 18:48:01 +00006035 float32 f;
6036 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006037 } u;
6038 u.f = env->fpr[i];
bellard1bdb68e2006-06-21 18:48:01 +00006039 qemu_put_be32(f, u.i);
bellarde80cfcf2004-12-19 23:18:01 +00006040 }
bellard4fa5d772005-01-30 22:57:54 +00006041
6042 qemu_put_betls(f, &env->pc);
6043 qemu_put_betls(f, &env->npc);
6044 qemu_put_betls(f, &env->y);
bellarde80cfcf2004-12-19 23:18:01 +00006045 tmp = GET_PSR(env);
bellard4fa5d772005-01-30 22:57:54 +00006046 qemu_put_be32(f, tmp);
bellard34751872005-07-02 14:31:34 +00006047 qemu_put_betls(f, &env->fsr);
6048 qemu_put_betls(f, &env->tbr);
6049#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006050 qemu_put_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006051 /* MMU */
6052 for(i = 0; i < 16; i++)
6053 qemu_put_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006054#endif
bellarde95c8d52004-09-30 22:22:08 +00006055}
6056
6057int cpu_load(QEMUFile *f, void *opaque, int version_id)
6058{
bellarde80cfcf2004-12-19 23:18:01 +00006059 CPUState *env = opaque;
6060 int i;
6061 uint32_t tmp;
6062
bellard4fa5d772005-01-30 22:57:54 +00006063 for(i = 0; i < 8; i++)
6064 qemu_get_betls(f, &env->gregs[i]);
6065 for(i = 0; i < NWINDOWS * 16; i++)
6066 qemu_get_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006067
6068 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006069 for(i = 0; i < TARGET_FPREGS; i++) {
6070 union {
bellard1bdb68e2006-06-21 18:48:01 +00006071 float32 f;
6072 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006073 } u;
bellard1bdb68e2006-06-21 18:48:01 +00006074 u.i = qemu_get_be32(f);
bellard4fa5d772005-01-30 22:57:54 +00006075 env->fpr[i] = u.f;
bellarde80cfcf2004-12-19 23:18:01 +00006076 }
bellard4fa5d772005-01-30 22:57:54 +00006077
6078 qemu_get_betls(f, &env->pc);
6079 qemu_get_betls(f, &env->npc);
6080 qemu_get_betls(f, &env->y);
6081 tmp = qemu_get_be32(f);
6082 env->cwp = 0; /* needed to ensure that the wrapping registers are
6083 correctly updated */
bellarde80cfcf2004-12-19 23:18:01 +00006084 PUT_PSR(env, tmp);
bellard34751872005-07-02 14:31:34 +00006085 qemu_get_betls(f, &env->fsr);
6086 qemu_get_betls(f, &env->tbr);
6087#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006088 qemu_get_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006089 /* MMU */
6090 for(i = 0; i < 16; i++)
6091 qemu_get_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006092#endif
bellarde80cfcf2004-12-19 23:18:01 +00006093 tlb_flush(env, 1);
bellarde95c8d52004-09-30 22:22:08 +00006094 return 0;
6095}
bellardb5ff1b32005-11-26 10:38:39 +00006096
6097#elif defined(TARGET_ARM)
6098
bellardb5ff1b32005-11-26 10:38:39 +00006099void cpu_save(QEMUFile *f, void *opaque)
6100{
balrogaa941b92007-05-24 18:50:09 +00006101 int i;
6102 CPUARMState *env = (CPUARMState *)opaque;
6103
6104 for (i = 0; i < 16; i++) {
6105 qemu_put_be32(f, env->regs[i]);
6106 }
6107 qemu_put_be32(f, cpsr_read(env));
6108 qemu_put_be32(f, env->spsr);
6109 for (i = 0; i < 6; i++) {
6110 qemu_put_be32(f, env->banked_spsr[i]);
6111 qemu_put_be32(f, env->banked_r13[i]);
6112 qemu_put_be32(f, env->banked_r14[i]);
6113 }
6114 for (i = 0; i < 5; i++) {
6115 qemu_put_be32(f, env->usr_regs[i]);
6116 qemu_put_be32(f, env->fiq_regs[i]);
6117 }
6118 qemu_put_be32(f, env->cp15.c0_cpuid);
6119 qemu_put_be32(f, env->cp15.c0_cachetype);
6120 qemu_put_be32(f, env->cp15.c1_sys);
6121 qemu_put_be32(f, env->cp15.c1_coproc);
balrog610c3c82007-06-24 12:09:48 +00006122 qemu_put_be32(f, env->cp15.c1_xscaleauxcr);
pbrook9ee6e8b2007-11-11 00:04:49 +00006123 qemu_put_be32(f, env->cp15.c2_base0);
6124 qemu_put_be32(f, env->cp15.c2_base1);
6125 qemu_put_be32(f, env->cp15.c2_mask);
balrogaa941b92007-05-24 18:50:09 +00006126 qemu_put_be32(f, env->cp15.c2_data);
6127 qemu_put_be32(f, env->cp15.c2_insn);
6128 qemu_put_be32(f, env->cp15.c3);
6129 qemu_put_be32(f, env->cp15.c5_insn);
6130 qemu_put_be32(f, env->cp15.c5_data);
6131 for (i = 0; i < 8; i++) {
6132 qemu_put_be32(f, env->cp15.c6_region[i]);
6133 }
6134 qemu_put_be32(f, env->cp15.c6_insn);
6135 qemu_put_be32(f, env->cp15.c6_data);
6136 qemu_put_be32(f, env->cp15.c9_insn);
6137 qemu_put_be32(f, env->cp15.c9_data);
6138 qemu_put_be32(f, env->cp15.c13_fcse);
6139 qemu_put_be32(f, env->cp15.c13_context);
pbrook9ee6e8b2007-11-11 00:04:49 +00006140 qemu_put_be32(f, env->cp15.c13_tls1);
6141 qemu_put_be32(f, env->cp15.c13_tls2);
6142 qemu_put_be32(f, env->cp15.c13_tls3);
balrogaa941b92007-05-24 18:50:09 +00006143 qemu_put_be32(f, env->cp15.c15_cpar);
6144
6145 qemu_put_be32(f, env->features);
6146
6147 if (arm_feature(env, ARM_FEATURE_VFP)) {
6148 for (i = 0; i < 16; i++) {
6149 CPU_DoubleU u;
6150 u.d = env->vfp.regs[i];
6151 qemu_put_be32(f, u.l.upper);
6152 qemu_put_be32(f, u.l.lower);
6153 }
6154 for (i = 0; i < 16; i++) {
6155 qemu_put_be32(f, env->vfp.xregs[i]);
6156 }
6157
6158 /* TODO: Should use proper FPSCR access functions. */
6159 qemu_put_be32(f, env->vfp.vec_len);
6160 qemu_put_be32(f, env->vfp.vec_stride);
pbrook9ee6e8b2007-11-11 00:04:49 +00006161
6162 if (arm_feature(env, ARM_FEATURE_VFP3)) {
6163 for (i = 16; i < 32; i++) {
6164 CPU_DoubleU u;
6165 u.d = env->vfp.regs[i];
6166 qemu_put_be32(f, u.l.upper);
6167 qemu_put_be32(f, u.l.lower);
6168 }
6169 }
balrogaa941b92007-05-24 18:50:09 +00006170 }
6171
6172 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6173 for (i = 0; i < 16; i++) {
6174 qemu_put_be64(f, env->iwmmxt.regs[i]);
6175 }
6176 for (i = 0; i < 16; i++) {
6177 qemu_put_be32(f, env->iwmmxt.cregs[i]);
6178 }
6179 }
pbrook9ee6e8b2007-11-11 00:04:49 +00006180
6181 if (arm_feature(env, ARM_FEATURE_M)) {
6182 qemu_put_be32(f, env->v7m.other_sp);
6183 qemu_put_be32(f, env->v7m.vecbase);
6184 qemu_put_be32(f, env->v7m.basepri);
6185 qemu_put_be32(f, env->v7m.control);
6186 qemu_put_be32(f, env->v7m.current_sp);
6187 qemu_put_be32(f, env->v7m.exception);
6188 }
bellardb5ff1b32005-11-26 10:38:39 +00006189}
6190
6191int cpu_load(QEMUFile *f, void *opaque, int version_id)
6192{
balrogaa941b92007-05-24 18:50:09 +00006193 CPUARMState *env = (CPUARMState *)opaque;
6194 int i;
6195
pbrook9ee6e8b2007-11-11 00:04:49 +00006196 if (version_id != ARM_CPU_SAVE_VERSION)
balrogaa941b92007-05-24 18:50:09 +00006197 return -EINVAL;
6198
6199 for (i = 0; i < 16; i++) {
6200 env->regs[i] = qemu_get_be32(f);
6201 }
6202 cpsr_write(env, qemu_get_be32(f), 0xffffffff);
6203 env->spsr = qemu_get_be32(f);
6204 for (i = 0; i < 6; i++) {
6205 env->banked_spsr[i] = qemu_get_be32(f);
6206 env->banked_r13[i] = qemu_get_be32(f);
6207 env->banked_r14[i] = qemu_get_be32(f);
6208 }
6209 for (i = 0; i < 5; i++) {
6210 env->usr_regs[i] = qemu_get_be32(f);
6211 env->fiq_regs[i] = qemu_get_be32(f);
6212 }
6213 env->cp15.c0_cpuid = qemu_get_be32(f);
6214 env->cp15.c0_cachetype = qemu_get_be32(f);
6215 env->cp15.c1_sys = qemu_get_be32(f);
6216 env->cp15.c1_coproc = qemu_get_be32(f);
balrog610c3c82007-06-24 12:09:48 +00006217 env->cp15.c1_xscaleauxcr = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006218 env->cp15.c2_base0 = qemu_get_be32(f);
6219 env->cp15.c2_base1 = qemu_get_be32(f);
6220 env->cp15.c2_mask = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006221 env->cp15.c2_data = qemu_get_be32(f);
6222 env->cp15.c2_insn = qemu_get_be32(f);
6223 env->cp15.c3 = qemu_get_be32(f);
6224 env->cp15.c5_insn = qemu_get_be32(f);
6225 env->cp15.c5_data = qemu_get_be32(f);
6226 for (i = 0; i < 8; i++) {
6227 env->cp15.c6_region[i] = qemu_get_be32(f);
6228 }
6229 env->cp15.c6_insn = qemu_get_be32(f);
6230 env->cp15.c6_data = qemu_get_be32(f);
6231 env->cp15.c9_insn = qemu_get_be32(f);
6232 env->cp15.c9_data = qemu_get_be32(f);
6233 env->cp15.c13_fcse = qemu_get_be32(f);
6234 env->cp15.c13_context = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006235 env->cp15.c13_tls1 = qemu_get_be32(f);
6236 env->cp15.c13_tls2 = qemu_get_be32(f);
6237 env->cp15.c13_tls3 = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006238 env->cp15.c15_cpar = qemu_get_be32(f);
6239
6240 env->features = qemu_get_be32(f);
6241
6242 if (arm_feature(env, ARM_FEATURE_VFP)) {
6243 for (i = 0; i < 16; i++) {
6244 CPU_DoubleU u;
6245 u.l.upper = qemu_get_be32(f);
6246 u.l.lower = qemu_get_be32(f);
6247 env->vfp.regs[i] = u.d;
6248 }
6249 for (i = 0; i < 16; i++) {
6250 env->vfp.xregs[i] = qemu_get_be32(f);
6251 }
6252
6253 /* TODO: Should use proper FPSCR access functions. */
6254 env->vfp.vec_len = qemu_get_be32(f);
6255 env->vfp.vec_stride = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006256
6257 if (arm_feature(env, ARM_FEATURE_VFP3)) {
6258 for (i = 0; i < 16; i++) {
6259 CPU_DoubleU u;
6260 u.l.upper = qemu_get_be32(f);
6261 u.l.lower = qemu_get_be32(f);
6262 env->vfp.regs[i] = u.d;
6263 }
6264 }
balrogaa941b92007-05-24 18:50:09 +00006265 }
6266
6267 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6268 for (i = 0; i < 16; i++) {
6269 env->iwmmxt.regs[i] = qemu_get_be64(f);
6270 }
6271 for (i = 0; i < 16; i++) {
6272 env->iwmmxt.cregs[i] = qemu_get_be32(f);
6273 }
6274 }
6275
pbrook9ee6e8b2007-11-11 00:04:49 +00006276 if (arm_feature(env, ARM_FEATURE_M)) {
6277 env->v7m.other_sp = qemu_get_be32(f);
6278 env->v7m.vecbase = qemu_get_be32(f);
6279 env->v7m.basepri = qemu_get_be32(f);
6280 env->v7m.control = qemu_get_be32(f);
6281 env->v7m.current_sp = qemu_get_be32(f);
6282 env->v7m.exception = qemu_get_be32(f);
6283 }
6284
bellardb5ff1b32005-11-26 10:38:39 +00006285 return 0;
6286}
6287
bellard8a7ddc32004-03-31 19:00:16 +00006288#else
6289
6290#warning No CPU save/restore functions
6291
6292#endif
6293
6294/***********************************************************/
6295/* ram save/restore */
6296
bellard8a7ddc32004-03-31 19:00:16 +00006297static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
6298{
6299 int v;
6300
6301 v = qemu_get_byte(f);
6302 switch(v) {
6303 case 0:
6304 if (qemu_get_buffer(f, buf, len) != len)
6305 return -EIO;
6306 break;
6307 case 1:
6308 v = qemu_get_byte(f);
6309 memset(buf, v, len);
6310 break;
6311 default:
6312 return -EINVAL;
6313 }
6314 return 0;
6315}
6316
bellardc88676f2006-08-06 13:36:11 +00006317static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00006318{
6319 int i, ret;
6320
bellard8a7ddc32004-03-31 19:00:16 +00006321 if (qemu_get_be32(f) != phys_ram_size)
6322 return -EINVAL;
6323 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
6324 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
6325 if (ret)
6326 return ret;
6327 }
6328 return 0;
6329}
6330
bellardc88676f2006-08-06 13:36:11 +00006331#define BDRV_HASH_BLOCK_SIZE 1024
6332#define IOBUF_SIZE 4096
6333#define RAM_CBLOCK_MAGIC 0xfabe
6334
6335typedef struct RamCompressState {
6336 z_stream zstream;
6337 QEMUFile *f;
6338 uint8_t buf[IOBUF_SIZE];
6339} RamCompressState;
6340
6341static int ram_compress_open(RamCompressState *s, QEMUFile *f)
6342{
6343 int ret;
6344 memset(s, 0, sizeof(*s));
6345 s->f = f;
6346 ret = deflateInit2(&s->zstream, 1,
ths5fafdf22007-09-16 21:08:06 +00006347 Z_DEFLATED, 15,
bellardc88676f2006-08-06 13:36:11 +00006348 9, Z_DEFAULT_STRATEGY);
6349 if (ret != Z_OK)
6350 return -1;
6351 s->zstream.avail_out = IOBUF_SIZE;
6352 s->zstream.next_out = s->buf;
6353 return 0;
6354}
6355
6356static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len)
6357{
6358 qemu_put_be16(s->f, RAM_CBLOCK_MAGIC);
6359 qemu_put_be16(s->f, len);
6360 qemu_put_buffer(s->f, buf, len);
6361}
6362
6363static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len)
6364{
6365 int ret;
6366
6367 s->zstream.avail_in = len;
6368 s->zstream.next_in = (uint8_t *)buf;
6369 while (s->zstream.avail_in > 0) {
6370 ret = deflate(&s->zstream, Z_NO_FLUSH);
6371 if (ret != Z_OK)
6372 return -1;
6373 if (s->zstream.avail_out == 0) {
6374 ram_put_cblock(s, s->buf, IOBUF_SIZE);
6375 s->zstream.avail_out = IOBUF_SIZE;
6376 s->zstream.next_out = s->buf;
6377 }
6378 }
6379 return 0;
6380}
6381
6382static void ram_compress_close(RamCompressState *s)
6383{
6384 int len, ret;
6385
6386 /* compress last bytes */
6387 for(;;) {
6388 ret = deflate(&s->zstream, Z_FINISH);
6389 if (ret == Z_OK || ret == Z_STREAM_END) {
6390 len = IOBUF_SIZE - s->zstream.avail_out;
6391 if (len > 0) {
6392 ram_put_cblock(s, s->buf, len);
6393 }
6394 s->zstream.avail_out = IOBUF_SIZE;
6395 s->zstream.next_out = s->buf;
6396 if (ret == Z_STREAM_END)
6397 break;
6398 } else {
6399 goto fail;
6400 }
6401 }
6402fail:
6403 deflateEnd(&s->zstream);
6404}
6405
6406typedef struct RamDecompressState {
6407 z_stream zstream;
6408 QEMUFile *f;
6409 uint8_t buf[IOBUF_SIZE];
6410} RamDecompressState;
6411
6412static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
6413{
6414 int ret;
6415 memset(s, 0, sizeof(*s));
6416 s->f = f;
6417 ret = inflateInit(&s->zstream);
6418 if (ret != Z_OK)
6419 return -1;
6420 return 0;
6421}
6422
6423static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
6424{
6425 int ret, clen;
6426
6427 s->zstream.avail_out = len;
6428 s->zstream.next_out = buf;
6429 while (s->zstream.avail_out > 0) {
6430 if (s->zstream.avail_in == 0) {
6431 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
6432 return -1;
6433 clen = qemu_get_be16(s->f);
6434 if (clen > IOBUF_SIZE)
6435 return -1;
6436 qemu_get_buffer(s->f, s->buf, clen);
6437 s->zstream.avail_in = clen;
6438 s->zstream.next_in = s->buf;
6439 }
6440 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
6441 if (ret != Z_OK && ret != Z_STREAM_END) {
6442 return -1;
6443 }
6444 }
6445 return 0;
6446}
6447
6448static void ram_decompress_close(RamDecompressState *s)
6449{
6450 inflateEnd(&s->zstream);
6451}
6452
6453static void ram_save(QEMUFile *f, void *opaque)
6454{
6455 int i;
6456 RamCompressState s1, *s = &s1;
6457 uint8_t buf[10];
ths3b46e622007-09-17 08:09:54 +00006458
bellardc88676f2006-08-06 13:36:11 +00006459 qemu_put_be32(f, phys_ram_size);
6460 if (ram_compress_open(s, f) < 0)
6461 return;
6462 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6463#if 0
6464 if (tight_savevm_enabled) {
6465 int64_t sector_num;
6466 int j;
6467
6468 /* find if the memory block is available on a virtual
6469 block device */
6470 sector_num = -1;
6471 for(j = 0; j < MAX_DISKS; j++) {
6472 if (bs_table[j]) {
ths5fafdf22007-09-16 21:08:06 +00006473 sector_num = bdrv_hash_find(bs_table[j],
bellardc88676f2006-08-06 13:36:11 +00006474 phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6475 if (sector_num >= 0)
6476 break;
6477 }
6478 }
6479 if (j == MAX_DISKS)
6480 goto normal_compress;
6481 buf[0] = 1;
6482 buf[1] = j;
6483 cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
6484 ram_compress_buf(s, buf, 10);
ths5fafdf22007-09-16 21:08:06 +00006485 } else
bellardc88676f2006-08-06 13:36:11 +00006486#endif
6487 {
6488 // normal_compress:
6489 buf[0] = 0;
6490 ram_compress_buf(s, buf, 1);
6491 ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6492 }
6493 }
6494 ram_compress_close(s);
6495}
6496
6497static int ram_load(QEMUFile *f, void *opaque, int version_id)
6498{
6499 RamDecompressState s1, *s = &s1;
6500 uint8_t buf[10];
6501 int i;
6502
6503 if (version_id == 1)
6504 return ram_load_v1(f, opaque);
6505 if (version_id != 2)
6506 return -EINVAL;
6507 if (qemu_get_be32(f) != phys_ram_size)
6508 return -EINVAL;
6509 if (ram_decompress_open(s, f) < 0)
6510 return -EINVAL;
6511 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6512 if (ram_decompress_buf(s, buf, 1) < 0) {
6513 fprintf(stderr, "Error while reading ram block header\n");
6514 goto error;
6515 }
6516 if (buf[0] == 0) {
6517 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
6518 fprintf(stderr, "Error while reading ram block address=0x%08x", i);
6519 goto error;
6520 }
ths5fafdf22007-09-16 21:08:06 +00006521 } else
bellardc88676f2006-08-06 13:36:11 +00006522#if 0
6523 if (buf[0] == 1) {
6524 int bs_index;
6525 int64_t sector_num;
6526
6527 ram_decompress_buf(s, buf + 1, 9);
6528 bs_index = buf[1];
6529 sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
6530 if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) {
6531 fprintf(stderr, "Invalid block device index %d\n", bs_index);
6532 goto error;
6533 }
ths5fafdf22007-09-16 21:08:06 +00006534 if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i,
bellardc88676f2006-08-06 13:36:11 +00006535 BDRV_HASH_BLOCK_SIZE / 512) < 0) {
ths5fafdf22007-09-16 21:08:06 +00006536 fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
bellardc88676f2006-08-06 13:36:11 +00006537 bs_index, sector_num);
6538 goto error;
6539 }
ths5fafdf22007-09-16 21:08:06 +00006540 } else
bellardc88676f2006-08-06 13:36:11 +00006541#endif
6542 {
6543 error:
6544 printf("Error block header\n");
6545 return -EINVAL;
6546 }
6547 }
6548 ram_decompress_close(s);
6549 return 0;
6550}
6551
bellard8a7ddc32004-03-31 19:00:16 +00006552/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00006553/* bottom halves (can be seen as timers which expire ASAP) */
6554
6555struct QEMUBH {
6556 QEMUBHFunc *cb;
6557 void *opaque;
6558 int scheduled;
6559 QEMUBH *next;
6560};
6561
6562static QEMUBH *first_bh = NULL;
6563
6564QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
6565{
6566 QEMUBH *bh;
6567 bh = qemu_mallocz(sizeof(QEMUBH));
6568 if (!bh)
6569 return NULL;
6570 bh->cb = cb;
6571 bh->opaque = opaque;
6572 return bh;
6573}
6574
bellard6eb57332006-08-06 09:51:25 +00006575int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00006576{
6577 QEMUBH *bh, **pbh;
bellard6eb57332006-08-06 09:51:25 +00006578 int ret;
bellard83f64092006-08-01 16:21:11 +00006579
bellard6eb57332006-08-06 09:51:25 +00006580 ret = 0;
bellard83f64092006-08-01 16:21:11 +00006581 for(;;) {
6582 pbh = &first_bh;
6583 bh = *pbh;
6584 if (!bh)
6585 break;
bellard6eb57332006-08-06 09:51:25 +00006586 ret = 1;
bellard83f64092006-08-01 16:21:11 +00006587 *pbh = bh->next;
6588 bh->scheduled = 0;
6589 bh->cb(bh->opaque);
6590 }
bellard6eb57332006-08-06 09:51:25 +00006591 return ret;
bellard83f64092006-08-01 16:21:11 +00006592}
6593
6594void qemu_bh_schedule(QEMUBH *bh)
6595{
6596 CPUState *env = cpu_single_env;
6597 if (bh->scheduled)
6598 return;
6599 bh->scheduled = 1;
6600 bh->next = first_bh;
6601 first_bh = bh;
6602
6603 /* stop the currently executing CPU to execute the BH ASAP */
6604 if (env) {
6605 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
6606 }
6607}
6608
6609void qemu_bh_cancel(QEMUBH *bh)
6610{
6611 QEMUBH **pbh;
6612 if (bh->scheduled) {
6613 pbh = &first_bh;
6614 while (*pbh != bh)
6615 pbh = &(*pbh)->next;
6616 *pbh = bh->next;
6617 bh->scheduled = 0;
6618 }
6619}
6620
6621void qemu_bh_delete(QEMUBH *bh)
6622{
6623 qemu_bh_cancel(bh);
6624 qemu_free(bh);
6625}
6626
6627/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00006628/* machine registration */
6629
6630QEMUMachine *first_machine = NULL;
6631
6632int qemu_register_machine(QEMUMachine *m)
6633{
6634 QEMUMachine **pm;
6635 pm = &first_machine;
6636 while (*pm != NULL)
6637 pm = &(*pm)->next;
6638 m->next = NULL;
6639 *pm = m;
6640 return 0;
6641}
6642
6643QEMUMachine *find_machine(const char *name)
6644{
6645 QEMUMachine *m;
6646
6647 for(m = first_machine; m != NULL; m = m->next) {
6648 if (!strcmp(m->name, name))
6649 return m;
6650 }
6651 return NULL;
6652}
6653
6654/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00006655/* main execution loop */
6656
6657void gui_update(void *opaque)
6658{
ths740733b2007-06-08 01:57:56 +00006659 DisplayState *ds = opaque;
6660 ds->dpy_refresh(ds);
6661 qemu_mod_timer(ds->gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00006662}
6663
bellard0bd48852005-11-11 00:00:47 +00006664struct vm_change_state_entry {
6665 VMChangeStateHandler *cb;
6666 void *opaque;
6667 LIST_ENTRY (vm_change_state_entry) entries;
6668};
6669
6670static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
6671
6672VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
6673 void *opaque)
6674{
6675 VMChangeStateEntry *e;
6676
6677 e = qemu_mallocz(sizeof (*e));
6678 if (!e)
6679 return NULL;
6680
6681 e->cb = cb;
6682 e->opaque = opaque;
6683 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
6684 return e;
6685}
6686
6687void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
6688{
6689 LIST_REMOVE (e, entries);
6690 qemu_free (e);
6691}
6692
6693static void vm_state_notify(int running)
6694{
6695 VMChangeStateEntry *e;
6696
6697 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
6698 e->cb(e->opaque, running);
6699 }
6700}
6701
bellard8a7ddc32004-03-31 19:00:16 +00006702/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00006703static VMStopHandler *vm_stop_cb;
6704static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00006705
6706int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
6707{
6708 vm_stop_cb = cb;
6709 vm_stop_opaque = opaque;
6710 return 0;
6711}
6712
6713void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
6714{
6715 vm_stop_cb = NULL;
6716}
6717
6718void vm_start(void)
6719{
6720 if (!vm_running) {
6721 cpu_enable_ticks();
6722 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00006723 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00006724 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00006725 }
6726}
6727
ths5fafdf22007-09-16 21:08:06 +00006728void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00006729{
6730 if (vm_running) {
6731 cpu_disable_ticks();
6732 vm_running = 0;
6733 if (reason != 0) {
6734 if (vm_stop_cb) {
6735 vm_stop_cb(vm_stop_opaque, reason);
6736 }
6737 }
bellard0bd48852005-11-11 00:00:47 +00006738 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00006739 }
6740}
6741
bellardbb0c6722004-06-20 12:37:32 +00006742/* reset/shutdown handler */
6743
6744typedef struct QEMUResetEntry {
6745 QEMUResetHandler *func;
6746 void *opaque;
6747 struct QEMUResetEntry *next;
6748} QEMUResetEntry;
6749
6750static QEMUResetEntry *first_reset_entry;
6751static int reset_requested;
6752static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00006753static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00006754
6755void qemu_register_reset(QEMUResetHandler *func, void *opaque)
6756{
6757 QEMUResetEntry **pre, *re;
6758
6759 pre = &first_reset_entry;
6760 while (*pre != NULL)
6761 pre = &(*pre)->next;
6762 re = qemu_mallocz(sizeof(QEMUResetEntry));
6763 re->func = func;
6764 re->opaque = opaque;
6765 re->next = NULL;
6766 *pre = re;
6767}
6768
ths52f61fd2006-12-22 21:20:52 +00006769static void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00006770{
6771 QEMUResetEntry *re;
6772
6773 /* reset all devices */
6774 for(re = first_reset_entry; re != NULL; re = re->next) {
6775 re->func(re->opaque);
6776 }
6777}
6778
6779void qemu_system_reset_request(void)
6780{
bellardd1beab82006-10-02 19:44:22 +00006781 if (no_reboot) {
6782 shutdown_requested = 1;
6783 } else {
6784 reset_requested = 1;
6785 }
bellard6a00d602005-11-21 23:25:50 +00006786 if (cpu_single_env)
6787 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006788}
6789
6790void qemu_system_shutdown_request(void)
6791{
6792 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00006793 if (cpu_single_env)
6794 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006795}
6796
bellard34751872005-07-02 14:31:34 +00006797void qemu_system_powerdown_request(void)
6798{
6799 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00006800 if (cpu_single_env)
6801 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006802}
6803
bellard5905b2e2004-08-01 21:53:26 +00006804void main_loop_wait(int timeout)
bellard8a7ddc32004-03-31 19:00:16 +00006805{
thscafffd42007-02-28 21:59:44 +00006806 IOHandlerRecord *ioh;
bellarde0356492006-05-01 13:33:02 +00006807 fd_set rfds, wfds, xfds;
ths877cf882007-04-18 18:11:47 +00006808 int ret, nfds;
6809#ifdef _WIN32
6810 int ret2, i;
6811#endif
bellardfd1dff42006-02-01 21:29:26 +00006812 struct timeval tv;
bellardf3311102006-04-12 20:21:17 +00006813 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00006814
bellardf3311102006-04-12 20:21:17 +00006815
6816 /* XXX: need to suppress polling by better using win32 events */
6817 ret = 0;
6818 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
6819 ret |= pe->func(pe->opaque);
6820 }
bellard38e205a2004-04-06 19:29:17 +00006821#ifdef _WIN32
thse6b1e552007-04-18 17:56:02 +00006822 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00006823 int err;
6824 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00006825
bellarda18e5242006-06-25 17:18:27 +00006826 ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
6827 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
6828 if (w->func[ret - WAIT_OBJECT_0])
6829 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00006830
ths5fafdf22007-09-16 21:08:06 +00006831 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00006832 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00006833
thse6b1e552007-04-18 17:56:02 +00006834 /* Check if event is signaled */
6835 ret2 = WaitForSingleObject(w->events[i], 0);
6836 if(ret2 == WAIT_OBJECT_0) {
6837 if (w->func[i])
6838 w->func[i](w->opaque[i]);
6839 } else if (ret2 == WAIT_TIMEOUT) {
6840 } else {
6841 err = GetLastError();
6842 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00006843 }
6844 }
bellarda18e5242006-06-25 17:18:27 +00006845 } else if (ret == WAIT_TIMEOUT) {
6846 } else {
6847 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00006848 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00006849 }
bellardf3311102006-04-12 20:21:17 +00006850 }
bellardfd1dff42006-02-01 21:29:26 +00006851#endif
6852 /* poll any events */
6853 /* XXX: separate device handlers from system ones */
6854 nfds = -1;
6855 FD_ZERO(&rfds);
6856 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00006857 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00006858 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00006859 if (ioh->deleted)
6860 continue;
bellardfd1dff42006-02-01 21:29:26 +00006861 if (ioh->fd_read &&
6862 (!ioh->fd_read_poll ||
6863 ioh->fd_read_poll(ioh->opaque) != 0)) {
6864 FD_SET(ioh->fd, &rfds);
6865 if (ioh->fd > nfds)
6866 nfds = ioh->fd;
6867 }
6868 if (ioh->fd_write) {
6869 FD_SET(ioh->fd, &wfds);
6870 if (ioh->fd > nfds)
6871 nfds = ioh->fd;
6872 }
6873 }
ths3b46e622007-09-17 08:09:54 +00006874
bellardfd1dff42006-02-01 21:29:26 +00006875 tv.tv_sec = 0;
6876#ifdef _WIN32
6877 tv.tv_usec = 0;
bellard38e205a2004-04-06 19:29:17 +00006878#else
bellardfd1dff42006-02-01 21:29:26 +00006879 tv.tv_usec = timeout * 1000;
6880#endif
bellarde0356492006-05-01 13:33:02 +00006881#if defined(CONFIG_SLIRP)
6882 if (slirp_inited) {
6883 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
6884 }
6885#endif
6886 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00006887 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00006888 IOHandlerRecord **pioh;
6889
6890 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00006891 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00006892 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00006893 }
ths6ab43fd2007-08-25 01:34:19 +00006894 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00006895 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00006896 }
6897 }
thscafffd42007-02-28 21:59:44 +00006898
6899 /* remove deleted IO handlers */
6900 pioh = &first_io_handler;
6901 while (*pioh) {
6902 ioh = *pioh;
6903 if (ioh->deleted) {
6904 *pioh = ioh->next;
6905 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00006906 } else
thscafffd42007-02-28 21:59:44 +00006907 pioh = &ioh->next;
6908 }
bellardfd1dff42006-02-01 21:29:26 +00006909 }
bellarde0356492006-05-01 13:33:02 +00006910#if defined(CONFIG_SLIRP)
6911 if (slirp_inited) {
6912 if (ret < 0) {
6913 FD_ZERO(&rfds);
6914 FD_ZERO(&wfds);
6915 FD_ZERO(&xfds);
6916 }
6917 slirp_select_poll(&rfds, &wfds, &xfds);
6918 }
6919#endif
bellard83f64092006-08-01 16:21:11 +00006920 qemu_aio_poll();
bellardc20709a2004-04-21 23:27:19 +00006921
bellardfd1dff42006-02-01 21:29:26 +00006922 if (vm_running) {
ths5fafdf22007-09-16 21:08:06 +00006923 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
bellardfd1dff42006-02-01 21:29:26 +00006924 qemu_get_clock(vm_clock));
6925 /* run dma transfers, if any */
6926 DMA_run();
6927 }
pbrook423f0742007-05-23 00:06:54 +00006928
bellardfd1dff42006-02-01 21:29:26 +00006929 /* real time timers */
ths5fafdf22007-09-16 21:08:06 +00006930 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
bellardfd1dff42006-02-01 21:29:26 +00006931 qemu_get_clock(rt_clock));
pbrook423f0742007-05-23 00:06:54 +00006932
6933 /* Check bottom-halves last in case any of the earlier events triggered
6934 them. */
6935 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00006936
bellard5905b2e2004-08-01 21:53:26 +00006937}
6938
bellard6a00d602005-11-21 23:25:50 +00006939static CPUState *cur_cpu;
6940
bellard5905b2e2004-08-01 21:53:26 +00006941int main_loop(void)
6942{
6943 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00006944#ifdef CONFIG_PROFILER
6945 int64_t ti;
6946#endif
bellard6a00d602005-11-21 23:25:50 +00006947 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00006948
bellard6a00d602005-11-21 23:25:50 +00006949 cur_cpu = first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00006950 for(;;) {
6951 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00006952
6953 env = cur_cpu;
6954 for(;;) {
6955 /* get next cpu */
6956 env = env->next_cpu;
6957 if (!env)
6958 env = first_cpu;
bellard89bfc102006-02-08 22:46:31 +00006959#ifdef CONFIG_PROFILER
6960 ti = profile_getclock();
6961#endif
bellard6a00d602005-11-21 23:25:50 +00006962 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00006963#ifdef CONFIG_PROFILER
6964 qemu_time += profile_getclock() - ti;
6965#endif
pbrookbd967e02007-03-11 18:54:57 +00006966 if (ret == EXCP_HLT) {
6967 /* Give the next CPU a chance to run. */
6968 cur_cpu = env;
6969 continue;
6970 }
bellard15a76442005-11-23 21:01:03 +00006971 if (ret != EXCP_HALTED)
6972 break;
6973 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00006974 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00006975 break;
bellard15a76442005-11-23 21:01:03 +00006976 }
6977 cur_cpu = env;
6978
bellard5905b2e2004-08-01 21:53:26 +00006979 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00006980 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00006981 break;
6982 }
6983 if (reset_requested) {
6984 reset_requested = 0;
6985 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00006986 ret = EXCP_INTERRUPT;
6987 }
6988 if (powerdown_requested) {
6989 powerdown_requested = 0;
6990 qemu_system_powerdown();
6991 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00006992 }
6993 if (ret == EXCP_DEBUG) {
6994 vm_stop(EXCP_DEBUG);
6995 }
pbrookbd967e02007-03-11 18:54:57 +00006996 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00006997 /* XXX: use timeout computed from timers */
pbrookbd967e02007-03-11 18:54:57 +00006998 if (ret == EXCP_HALTED)
bellard5905b2e2004-08-01 21:53:26 +00006999 timeout = 10;
7000 else
7001 timeout = 0;
7002 } else {
7003 timeout = 10;
7004 }
bellard89bfc102006-02-08 22:46:31 +00007005#ifdef CONFIG_PROFILER
7006 ti = profile_getclock();
7007#endif
bellard5905b2e2004-08-01 21:53:26 +00007008 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00007009#ifdef CONFIG_PROFILER
7010 dev_time += profile_getclock() - ti;
7011#endif
bellardb4608c02003-06-27 17:34:32 +00007012 }
bellard34865132003-10-05 14:28:56 +00007013 cpu_disable_ticks();
7014 return ret;
bellardb4608c02003-06-27 17:34:32 +00007015}
7016
ths15f82202007-06-29 23:26:08 +00007017static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00007018{
bellard84f2e8e2007-02-05 20:21:32 +00007019 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00007020 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00007021 "\n"
bellarda20dd502003-09-30 21:07:02 +00007022 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00007023 "\n"
bellarda20dd502003-09-30 21:07:02 +00007024 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00007025 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00007026 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00007027 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00007028 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7029 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007030 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
balrog3e3d5812007-04-30 02:09:25 +00007031 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00007032 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00007033 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00007034 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
ths667acca2006-12-11 02:08:05 +00007035 "-snapshot write to temporary files instead of disk image files\n"
7036#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007037 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00007038 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00007039 "-no-quit disable SDL window close capability\n"
7040#endif
bellard52ca8d62006-06-14 16:03:05 +00007041#ifdef TARGET_I386
7042 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7043#endif
bellarda00bad72004-05-22 21:39:06 +00007044 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00007045 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007046 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00007047 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00007048#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00007049 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00007050#endif
bellard1d14ffa2005-10-30 18:58:22 +00007051#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007052 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00007053 "-soundhw c1,... enable audio support\n"
7054 " and only specified sound cards (comma separated list)\n"
7055 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00007056 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00007057#endif
bellard89980282004-06-03 14:04:03 +00007058 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00007059 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00007060#ifdef TARGET_I386
7061 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7062#endif
bellardb389dbf2005-11-06 16:49:55 +00007063 "-usb enable the USB driver (will be the default soon)\n"
7064 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00007065#if defined(TARGET_PPC) || defined(TARGET_SPARC)
7066 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00007067#endif
thsc35734b2007-03-19 15:17:08 +00007068 "-name string set the name of the guest\n"
bellarda20dd502003-09-30 21:07:02 +00007069 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007070 "Network options:\n"
pbrooka41b2ff2006-02-05 04:14:41 +00007071 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007072 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00007073#ifdef CONFIG_SLIRP
pbrook115defd2006-04-16 11:06:58 +00007074 "-net user[,vlan=n][,hostname=host]\n"
7075 " connect the user mode network stack to VLAN 'n' and send\n"
7076 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00007077#endif
bellard7fb843f2006-02-01 23:06:55 +00007078#ifdef _WIN32
7079 "-net tap[,vlan=n],ifname=name\n"
7080 " connect the host TAP network interface to VLAN 'n'\n"
7081#else
thsb46a8902007-10-21 23:20:45 +00007082 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7083 " connect the host TAP network interface to VLAN 'n' and use the\n"
7084 " network scripts 'file' (default=%s)\n"
7085 " and 'dfile' (default=%s);\n"
7086 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00007087 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00007088#endif
bellard6a00d602005-11-21 23:25:50 +00007089 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007090 " connect the vlan 'n' to another VLAN using a socket connection\n"
bellard3d830452005-12-18 16:36:49 +00007091 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7092 " connect the vlan 'n' to multicast maddr and port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007093 "-net none use it alone to have zero network devices; if no -net option\n"
7094 " is provided, the default is '-net nic -net user'\n"
7095 "\n"
7096#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00007097 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00007098 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00007099#ifndef _WIN32
7100 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00007101#endif
bellard9bf05442004-08-25 22:12:49 +00007102 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007103 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00007104#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007105 "\n"
7106 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00007107 "-kernel bzImage use 'bzImage' as kernel image\n"
7108 "-append cmdline use 'cmdline' as kernel command line\n"
7109 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00007110 "\n"
bellard330d0412003-07-26 18:11:40 +00007111 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00007112 "-monitor dev redirect the monitor to char device 'dev'\n"
7113 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00007114 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00007115 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00007116 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00007117 "-s wait gdb connection to port\n"
7118 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00007119 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00007120 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7121 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00007122 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00007123#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00007124 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00007125 "-no-kqemu disable KQEMU kernel module usage\n"
7126#endif
bellardbb0c6722004-06-20 12:37:32 +00007127#ifdef TARGET_I386
bellard1bfe8562004-07-08 21:17:50 +00007128 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7129 " (default is CL-GD5446 PCI VGA)\n"
bellard6515b202006-05-03 22:02:44 +00007130 "-no-acpi disable ACPI\n"
bellardbb0c6722004-06-20 12:37:32 +00007131#endif
bellardd1beab82006-10-02 19:44:22 +00007132 "-no-reboot exit instead of rebooting\n"
bellardd63d3072004-10-03 13:29:03 +00007133 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00007134 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00007135#ifndef _WIN32
7136 "-daemonize daemonize QEMU after initializing\n"
7137#endif
ths9ae02552007-01-05 17:39:04 +00007138 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00007139#ifdef TARGET_SPARC
7140 "-prom-env variable=value set OpenBIOS nvram variables\n"
7141#endif
thsf3dcfad2007-08-24 01:26:02 +00007142 "-clock force the use of the given methods for timer alarm.\n"
7143 " To see what timers are available use -clock help\n"
bellard0824d6f2003-06-24 13:42:40 +00007144 "\n"
bellard82c643f2004-07-14 17:28:13 +00007145 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00007146 "ctrl-alt-f toggle full screen\n"
7147 "ctrl-alt-n switch to virtual console 'n'\n"
7148 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00007149 "\n"
7150 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7151 ,
bellard0db63472003-10-27 21:37:46 +00007152 "qemu",
bellarda00bad72004-05-22 21:39:06 +00007153 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00007154#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00007155 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00007156 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00007157#endif
bellard6e44ba72004-01-18 21:56:49 +00007158 DEFAULT_GDBSTUB_PORT,
7159 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00007160 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00007161}
7162
bellardcd6f1162004-05-13 22:02:20 +00007163#define HAS_ARG 0x0001
7164
7165enum {
7166 QEMU_OPTION_h,
7167
bellardcc1daa42005-06-05 14:49:17 +00007168 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00007169 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00007170 QEMU_OPTION_fda,
7171 QEMU_OPTION_fdb,
7172 QEMU_OPTION_hda,
7173 QEMU_OPTION_hdb,
7174 QEMU_OPTION_hdc,
7175 QEMU_OPTION_hdd,
7176 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00007177 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00007178 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00007179 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00007180 QEMU_OPTION_boot,
7181 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00007182#ifdef TARGET_I386
7183 QEMU_OPTION_no_fd_bootchk,
7184#endif
bellardcd6f1162004-05-13 22:02:20 +00007185 QEMU_OPTION_m,
7186 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00007187 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00007188#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007189 QEMU_OPTION_audio_help,
7190 QEMU_OPTION_soundhw,
7191#endif
bellardcd6f1162004-05-13 22:02:20 +00007192
bellard7c9d8e02005-11-15 22:16:05 +00007193 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00007194 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00007195 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00007196 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00007197 QEMU_OPTION_redir,
bellardcd6f1162004-05-13 22:02:20 +00007198
7199 QEMU_OPTION_kernel,
7200 QEMU_OPTION_append,
7201 QEMU_OPTION_initrd,
7202
7203 QEMU_OPTION_S,
7204 QEMU_OPTION_s,
7205 QEMU_OPTION_p,
7206 QEMU_OPTION_d,
7207 QEMU_OPTION_hdachs,
7208 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00007209 QEMU_OPTION_bios,
bellardcd6f1162004-05-13 22:02:20 +00007210 QEMU_OPTION_no_code_copy,
bellard3d11d0e2004-12-12 16:56:30 +00007211 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00007212 QEMU_OPTION_localtime,
bellard1f042752004-06-05 13:46:47 +00007213 QEMU_OPTION_cirrusvga,
thsd34cab92007-04-02 01:10:46 +00007214 QEMU_OPTION_vmsvga,
bellarde9b137c2004-06-21 16:46:10 +00007215 QEMU_OPTION_g,
bellard1bfe8562004-07-08 21:17:50 +00007216 QEMU_OPTION_std_vga,
ths20d8a3e2007-02-18 17:04:49 +00007217 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00007218 QEMU_OPTION_monitor,
7219 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00007220 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00007221 QEMU_OPTION_loadvm,
7222 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00007223 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00007224 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00007225 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00007226 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00007227 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00007228 QEMU_OPTION_kernel_kqemu,
bellarda09db212005-04-30 16:10:35 +00007229 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00007230 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00007231 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00007232 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00007233 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00007234 QEMU_OPTION_no_acpi,
bellardd1beab82006-10-02 19:44:22 +00007235 QEMU_OPTION_no_reboot,
balrog9467cd42007-05-01 01:34:14 +00007236 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00007237 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00007238 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00007239 QEMU_OPTION_semihosting,
7240 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00007241 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00007242 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00007243 QEMU_OPTION_clock,
bellard7e0af5d02007-11-07 16:24:33 +00007244 QEMU_OPTION_startdate,
bellardcd6f1162004-05-13 22:02:20 +00007245};
7246
7247typedef struct QEMUOption {
7248 const char *name;
7249 int flags;
7250 int index;
7251} QEMUOption;
7252
7253const QEMUOption qemu_options[] = {
7254 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00007255 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00007256
bellardcc1daa42005-06-05 14:49:17 +00007257 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00007258 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00007259 { "fda", HAS_ARG, QEMU_OPTION_fda },
7260 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
7261 { "hda", HAS_ARG, QEMU_OPTION_hda },
7262 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
7263 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
7264 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
7265 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00007266 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00007267 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00007268 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00007269 { "boot", HAS_ARG, QEMU_OPTION_boot },
7270 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00007271#ifdef TARGET_I386
7272 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
7273#endif
bellardcd6f1162004-05-13 22:02:20 +00007274 { "m", HAS_ARG, QEMU_OPTION_m },
7275 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00007276 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00007277 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00007278#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007279 { "audio-help", 0, QEMU_OPTION_audio_help },
7280 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
7281#endif
bellardcd6f1162004-05-13 22:02:20 +00007282
bellard7c9d8e02005-11-15 22:16:05 +00007283 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00007284#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00007285 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00007286 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00007287#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007288 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00007289#endif
bellard9bf05442004-08-25 22:12:49 +00007290 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00007291#endif
bellardcd6f1162004-05-13 22:02:20 +00007292
7293 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
7294 { "append", HAS_ARG, QEMU_OPTION_append },
7295 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
7296
7297 { "S", 0, QEMU_OPTION_S },
7298 { "s", 0, QEMU_OPTION_s },
7299 { "p", HAS_ARG, QEMU_OPTION_p },
7300 { "d", HAS_ARG, QEMU_OPTION_d },
7301 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
7302 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00007303 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardcd6f1162004-05-13 22:02:20 +00007304 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
bellardd993e022005-02-10 22:00:06 +00007305#ifdef USE_KQEMU
7306 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00007307 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00007308#endif
bellard6f7e9ae2005-03-13 09:43:36 +00007309#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00007310 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00007311#endif
bellardee22c2f2004-06-03 12:49:50 +00007312 { "localtime", 0, QEMU_OPTION_localtime },
bellard1bfe8562004-07-08 21:17:50 +00007313 { "std-vga", 0, QEMU_OPTION_std_vga },
balrog8b6e0722007-06-22 08:23:44 +00007314 { "echr", HAS_ARG, QEMU_OPTION_echr },
7315 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
7316 { "serial", HAS_ARG, QEMU_OPTION_serial },
7317 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00007318 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
7319 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00007320#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007321 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00007322 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00007323 { "no-quit", 0, QEMU_OPTION_no_quit },
7324#endif
bellardf7cce892004-12-08 22:21:25 +00007325 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00007326 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00007327 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00007328 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00007329 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
ths96d30e42007-01-07 20:42:14 +00007330
bellard1f042752004-06-05 13:46:47 +00007331 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00007332 { "usb", 0, QEMU_OPTION_usb },
bellard1f042752004-06-05 13:46:47 +00007333 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
thsd34cab92007-04-02 01:10:46 +00007334 { "vmwarevga", 0, QEMU_OPTION_vmsvga },
bellard6515b202006-05-03 22:02:44 +00007335 { "no-acpi", 0, QEMU_OPTION_no_acpi },
bellardd1beab82006-10-02 19:44:22 +00007336 { "no-reboot", 0, QEMU_OPTION_no_reboot },
balrog9467cd42007-05-01 01:34:14 +00007337 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00007338 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00007339 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00007340#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00007341 { "semihosting", 0, QEMU_OPTION_semihosting },
7342#endif
thsc35734b2007-03-19 15:17:08 +00007343 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir166508602007-05-01 14:16:52 +00007344#if defined(TARGET_SPARC)
7345 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
7346#endif
balrog2b8f2d42007-07-27 22:08:46 +00007347#if defined(TARGET_ARM)
7348 { "old-param", 0, QEMU_OPTION_old_param },
7349#endif
thsf3dcfad2007-08-24 01:26:02 +00007350 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellard7e0af5d02007-11-07 16:24:33 +00007351 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
bellardcd6f1162004-05-13 22:02:20 +00007352 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00007353};
7354
bellard5905b2e2004-08-01 21:53:26 +00007355/* password input */
7356
balrog2bac6012007-04-30 01:34:31 +00007357int qemu_key_check(BlockDriverState *bs, const char *name)
7358{
7359 char password[256];
7360 int i;
7361
7362 if (!bdrv_is_encrypted(bs))
7363 return 0;
7364
7365 term_printf("%s is encrypted.\n", name);
7366 for(i = 0; i < 3; i++) {
7367 monitor_readline("Password: ", 1, password, sizeof(password));
7368 if (bdrv_set_key(bs, password) == 0)
7369 return 0;
7370 term_printf("invalid password\n");
7371 }
7372 return -EPERM;
7373}
7374
bellard5905b2e2004-08-01 21:53:26 +00007375static BlockDriverState *get_bdrv(int index)
7376{
7377 BlockDriverState *bs;
7378
7379 if (index < 4) {
7380 bs = bs_table[index];
7381 } else if (index < 6) {
7382 bs = fd_table[index - 4];
7383 } else {
7384 bs = NULL;
7385 }
7386 return bs;
7387}
7388
7389static void read_passwords(void)
7390{
7391 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00007392 int i;
bellard5905b2e2004-08-01 21:53:26 +00007393
7394 for(i = 0; i < 6; i++) {
7395 bs = get_bdrv(i);
balrog2bac6012007-04-30 01:34:31 +00007396 if (bs)
7397 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00007398 }
7399}
7400
bellardcc1daa42005-06-05 14:49:17 +00007401/* XXX: currently we cannot use simultaneously different CPUs */
7402void register_machines(void)
7403{
7404#if defined(TARGET_I386)
7405 qemu_register_machine(&pc_machine);
bellard3dbbdc22005-11-06 18:20:37 +00007406 qemu_register_machine(&isapc_machine);
bellardcc1daa42005-06-05 14:49:17 +00007407#elif defined(TARGET_PPC)
7408 qemu_register_machine(&heathrow_machine);
7409 qemu_register_machine(&core99_machine);
7410 qemu_register_machine(&prep_machine);
j_mayer1a6c0882007-04-24 07:40:49 +00007411 qemu_register_machine(&ref405ep_machine);
7412 qemu_register_machine(&taihu_machine);
bellard6af0bf92005-07-02 14:58:51 +00007413#elif defined(TARGET_MIPS)
7414 qemu_register_machine(&mips_machine);
ths5856de82007-01-15 23:58:11 +00007415 qemu_register_machine(&mips_malta_machine);
thsad6fe1d2007-04-16 17:23:27 +00007416 qemu_register_machine(&mips_pica61_machine);
ths6bf5b4e2007-10-17 13:08:32 +00007417 qemu_register_machine(&mips_mipssim_machine);
bellardcc1daa42005-06-05 14:49:17 +00007418#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +00007419#ifdef TARGET_SPARC64
7420 qemu_register_machine(&sun4u_machine);
7421#else
blueswir136cd9212007-04-01 15:44:43 +00007422 qemu_register_machine(&ss5_machine);
blueswir1e0353fe2007-04-01 15:55:28 +00007423 qemu_register_machine(&ss10_machine);
bellardcc1daa42005-06-05 14:49:17 +00007424#endif
bellardb5ff1b32005-11-26 10:38:39 +00007425#elif defined(TARGET_ARM)
pbrook3371d272007-03-08 03:04:12 +00007426 qemu_register_machine(&integratorcp_machine);
pbrookcdbdb642006-04-09 01:32:52 +00007427 qemu_register_machine(&versatilepb_machine);
pbrook16406952006-04-27 23:15:07 +00007428 qemu_register_machine(&versatileab_machine);
pbrooke69954b2006-09-23 17:40:58 +00007429 qemu_register_machine(&realview_machine);
balrogb00052e2007-04-30 02:22:06 +00007430 qemu_register_machine(&akitapda_machine);
7431 qemu_register_machine(&spitzpda_machine);
7432 qemu_register_machine(&borzoipda_machine);
7433 qemu_register_machine(&terrierpda_machine);
balrogc3d26892007-07-29 17:57:26 +00007434 qemu_register_machine(&palmte_machine);
pbrook9ee6e8b2007-11-11 00:04:49 +00007435 qemu_register_machine(&lm3s811evb_machine);
7436 qemu_register_machine(&lm3s6965evb_machine);
bellard27c7ca72006-04-27 21:32:09 +00007437#elif defined(TARGET_SH4)
7438 qemu_register_machine(&shix_machine);
ths0d78f542007-09-29 19:24:41 +00007439 qemu_register_machine(&r2d_machine);
j_mayereddf68a2007-04-05 07:22:49 +00007440#elif defined(TARGET_ALPHA)
7441 /* XXX: TODO */
pbrook06338792007-05-23 19:58:11 +00007442#elif defined(TARGET_M68K)
pbrook20dcee92007-06-03 11:13:39 +00007443 qemu_register_machine(&mcf5208evb_machine);
pbrook06338792007-05-23 19:58:11 +00007444 qemu_register_machine(&an5206_machine);
pbrookca02f312007-11-10 18:03:23 +00007445 qemu_register_machine(&dummy_m68k_machine);
thsf1ccf902007-10-08 13:16:14 +00007446#elif defined(TARGET_CRIS)
7447 qemu_register_machine(&bareetraxfs_machine);
bellardb5ff1b32005-11-26 10:38:39 +00007448#else
7449#error unsupported CPU
bellard34751872005-07-02 14:31:34 +00007450#endif
bellardcc1daa42005-06-05 14:49:17 +00007451}
7452
bellard1d14ffa2005-10-30 18:58:22 +00007453#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00007454struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00007455#ifdef HAS_AUDIO_CHOICE
bellardfd06c372006-04-24 21:58:30 +00007456#ifdef TARGET_I386
7457 {
7458 "pcspk",
7459 "PC speaker",
7460 0,
7461 1,
7462 { .init_isa = pcspk_audio_init }
7463 },
7464#endif
bellard6a36d842005-12-18 20:34:32 +00007465 {
7466 "sb16",
7467 "Creative Sound Blaster 16",
7468 0,
7469 1,
7470 { .init_isa = SB16_init }
7471 },
7472
7473#ifdef CONFIG_ADLIB
7474 {
7475 "adlib",
7476#ifdef HAS_YMF262
7477 "Yamaha YMF262 (OPL3)",
7478#else
7479 "Yamaha YM3812 (OPL2)",
7480#endif
7481 0,
7482 1,
7483 { .init_isa = Adlib_init }
7484 },
7485#endif
7486
7487#ifdef CONFIG_GUS
7488 {
7489 "gus",
7490 "Gravis Ultrasound GF1",
7491 0,
7492 1,
7493 { .init_isa = GUS_init }
7494 },
7495#endif
7496
7497 {
7498 "es1370",
7499 "ENSONIQ AudioPCI ES1370",
7500 0,
7501 0,
7502 { .init_pci = es1370_init }
7503 },
balrogb00052e2007-04-30 02:22:06 +00007504#endif
bellard6a36d842005-12-18 20:34:32 +00007505
7506 { NULL, NULL, 0, 0, { NULL } }
7507};
7508
bellard1d14ffa2005-10-30 18:58:22 +00007509static void select_soundhw (const char *optarg)
7510{
bellard6a36d842005-12-18 20:34:32 +00007511 struct soundhw *c;
7512
bellard1d14ffa2005-10-30 18:58:22 +00007513 if (*optarg == '?') {
7514 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00007515
bellard1d14ffa2005-10-30 18:58:22 +00007516 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00007517 for (c = soundhw; c->name; ++c) {
7518 printf ("%-11s %s\n", c->name, c->descr);
7519 }
7520 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00007521 exit (*optarg != '?');
7522 }
7523 else {
bellard6a36d842005-12-18 20:34:32 +00007524 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00007525 const char *p;
7526 char *e;
7527 int bad_card = 0;
7528
bellard6a36d842005-12-18 20:34:32 +00007529 if (!strcmp (optarg, "all")) {
7530 for (c = soundhw; c->name; ++c) {
7531 c->enabled = 1;
7532 }
7533 return;
7534 }
bellard1d14ffa2005-10-30 18:58:22 +00007535
bellard6a36d842005-12-18 20:34:32 +00007536 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00007537 while (*p) {
7538 e = strchr (p, ',');
7539 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00007540
7541 for (c = soundhw; c->name; ++c) {
7542 if (!strncmp (c->name, p, l)) {
7543 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00007544 break;
7545 }
7546 }
bellard6a36d842005-12-18 20:34:32 +00007547
7548 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00007549 if (l > 80) {
7550 fprintf (stderr,
7551 "Unknown sound card name (too big to show)\n");
7552 }
7553 else {
7554 fprintf (stderr, "Unknown sound card name `%.*s'\n",
7555 (int) l, p);
7556 }
7557 bad_card = 1;
7558 }
7559 p += l + (e != NULL);
7560 }
7561
7562 if (bad_card)
7563 goto show_valid_cards;
7564 }
7565}
7566#endif
7567
bellard3587d7e2006-06-26 20:03:44 +00007568#ifdef _WIN32
7569static BOOL WINAPI qemu_ctrl_handler(DWORD type)
7570{
7571 exit(STATUS_CONTROL_C_EXIT);
7572 return TRUE;
7573}
7574#endif
7575
bellard7c9d8e02005-11-15 22:16:05 +00007576#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00007577
bellard0824d6f2003-06-24 13:42:40 +00007578int main(int argc, char **argv)
7579{
bellard67b915a2004-03-31 23:37:16 +00007580#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00007581 int use_gdbstub;
7582 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00007583#endif
j_mayer28c5af52007-11-11 01:50:45 +00007584 uint32_t boot_devices_bitmap = 0;
j_mayer86f55662007-04-24 06:52:59 +00007585 int i, cdrom_index, pflash_index;
j_mayer28c5af52007-11-11 01:50:45 +00007586 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00007587 const char *initrd_filename;
ths96d30e42007-01-07 20:42:14 +00007588 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
j_mayer86f55662007-04-24 06:52:59 +00007589 const char *pflash_filename[MAX_PFLASH];
pbrooka1bb27b2007-04-06 16:49:48 +00007590 const char *sd_filename;
balrog3e3d5812007-04-30 02:09:25 +00007591 const char *mtd_filename;
bellarda20dd502003-09-30 21:07:02 +00007592 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00007593 const char *boot_devices = "";
bellard313aa562003-08-10 21:52:11 +00007594 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00007595 int cyls, heads, secs, translation;
bellard7c9d8e02005-11-15 22:16:05 +00007596 char net_clients[MAX_NET_CLIENTS][256];
7597 int nb_net_clients;
bellardcd6f1162004-05-13 22:02:20 +00007598 int optind;
7599 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00007600 CharDriverState *monitor_hd;
7601 char monitor_device[128];
bellard8d11df92004-08-24 21:13:40 +00007602 char serial_devices[MAX_SERIAL_PORTS][128];
7603 int serial_device_index;
bellard6508fe52005-01-15 12:02:56 +00007604 char parallel_devices[MAX_PARALLEL_PORTS][128];
7605 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00007606 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00007607 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00007608 const char *cpu_model;
pbrook0d92ed32006-05-21 16:30:15 +00007609 char usb_devices[MAX_USB_CMDLINE][128];
bellarda594cfb2005-11-06 16:13:29 +00007610 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00007611 int fds[2];
ths93815bc2007-03-19 15:58:31 +00007612 const char *pid_file = NULL;
blueswir1833c7172007-05-27 19:36:43 +00007613 VLANState *vlan;
bellard0bd48852005-11-11 00:00:47 +00007614
7615 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00007616#ifndef _WIN32
7617 {
7618 struct sigaction act;
7619 sigfillset(&act.sa_mask);
7620 act.sa_flags = 0;
7621 act.sa_handler = SIG_IGN;
7622 sigaction(SIGPIPE, &act, NULL);
7623 }
bellard3587d7e2006-06-26 20:03:44 +00007624#else
7625 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00007626 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7627 QEMU to run on a single CPU */
7628 {
7629 HANDLE h;
7630 DWORD mask, smask;
7631 int i;
7632 h = GetCurrentProcess();
7633 if (GetProcessAffinityMask(h, &mask, &smask)) {
7634 for(i = 0; i < 32; i++) {
7635 if (mask & (1 << i))
7636 break;
7637 }
7638 if (i != 32) {
7639 mask = 1 << i;
7640 SetProcessAffinityMask(h, mask);
7641 }
7642 }
7643 }
bellard67b915a2004-03-31 23:37:16 +00007644#endif
bellardbe995c22006-06-25 16:25:21 +00007645
bellardcc1daa42005-06-05 14:49:17 +00007646 register_machines();
7647 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00007648 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00007649 initrd_filename = NULL;
bellardc45886d2004-01-05 00:02:06 +00007650 for(i = 0; i < MAX_FD; i++)
7651 fd_filename[i] = NULL;
ths96d30e42007-01-07 20:42:14 +00007652 for(i = 0; i < MAX_DISKS; i++)
7653 hd_filename[i] = NULL;
j_mayer86f55662007-04-24 06:52:59 +00007654 for(i = 0; i < MAX_PFLASH; i++)
7655 pflash_filename[i] = NULL;
7656 pflash_index = 0;
pbrooka1bb27b2007-04-06 16:49:48 +00007657 sd_filename = NULL;
balrog3e3d5812007-04-30 02:09:25 +00007658 mtd_filename = NULL;
bellarda00bad72004-05-22 21:39:06 +00007659 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
bellard313aa562003-08-10 21:52:11 +00007660 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00007661#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00007662 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00007663 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00007664#endif
bellard33e39632003-07-06 17:15:21 +00007665 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00007666 nographic = 0;
7667 kernel_filename = NULL;
7668 kernel_cmdline = "";
bellardcc1daa42005-06-05 14:49:17 +00007669#ifdef TARGET_PPC
7670 cdrom_index = 1;
7671#else
7672 cdrom_index = 2;
7673#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007674 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00007675 translation = BIOS_ATA_TRANSLATION_AUTO;
bellard82c643f2004-07-14 17:28:13 +00007676 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
bellardc4b1fcc2004-03-14 21:44:30 +00007677
bellard8d11df92004-08-24 21:13:40 +00007678 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
7679 for(i = 1; i < MAX_SERIAL_PORTS; i++)
7680 serial_devices[i][0] = '\0';
7681 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00007682
bellard6508fe52005-01-15 12:02:56 +00007683 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
7684 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
7685 parallel_devices[i][0] = '\0';
7686 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00007687
bellarda594cfb2005-11-06 16:13:29 +00007688 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00007689
bellard7c9d8e02005-11-15 22:16:05 +00007690 nb_net_clients = 0;
7691
7692 nb_nics = 0;
bellard702c6512004-04-02 21:21:32 +00007693 /* default mac address of the first network interface */
ths3b46e622007-09-17 08:09:54 +00007694
bellardcd6f1162004-05-13 22:02:20 +00007695 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00007696 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00007697 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00007698 break;
bellardcd6f1162004-05-13 22:02:20 +00007699 r = argv[optind];
7700 if (r[0] != '-') {
ths96d30e42007-01-07 20:42:14 +00007701 hd_filename[0] = argv[optind++];
bellardcd6f1162004-05-13 22:02:20 +00007702 } else {
7703 const QEMUOption *popt;
7704
7705 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00007706 /* Treat --foo the same as -foo. */
7707 if (r[1] == '-')
7708 r++;
bellardcd6f1162004-05-13 22:02:20 +00007709 popt = qemu_options;
7710 for(;;) {
7711 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00007712 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00007713 argv[0], r);
7714 exit(1);
7715 }
7716 if (!strcmp(popt->name, r + 1))
7717 break;
7718 popt++;
7719 }
7720 if (popt->flags & HAS_ARG) {
7721 if (optind >= argc) {
7722 fprintf(stderr, "%s: option '%s' requires an argument\n",
7723 argv[0], r);
7724 exit(1);
7725 }
7726 optarg = argv[optind++];
7727 } else {
7728 optarg = NULL;
7729 }
7730
7731 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00007732 case QEMU_OPTION_M:
7733 machine = find_machine(optarg);
7734 if (!machine) {
7735 QEMUMachine *m;
7736 printf("Supported machines are:\n");
7737 for(m = first_machine; m != NULL; m = m->next) {
7738 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00007739 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00007740 m == first_machine ? " (default)" : "");
7741 }
ths15f82202007-06-29 23:26:08 +00007742 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00007743 }
7744 break;
j_mayer94fc95c2007-03-05 19:44:02 +00007745 case QEMU_OPTION_cpu:
7746 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00007747 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00007748/* XXX: implement xxx_cpu_list for targets that still miss it */
7749#if defined(cpu_list)
7750 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00007751#endif
ths15f82202007-06-29 23:26:08 +00007752 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00007753 } else {
7754 cpu_model = optarg;
7755 }
7756 break;
bellardcd6f1162004-05-13 22:02:20 +00007757 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00007758 initrd_filename = optarg;
7759 break;
bellardcd6f1162004-05-13 22:02:20 +00007760 case QEMU_OPTION_hda:
bellardcd6f1162004-05-13 22:02:20 +00007761 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00007762 case QEMU_OPTION_hdc:
7763 case QEMU_OPTION_hdd:
7764 {
7765 int hd_index;
7766 hd_index = popt->index - QEMU_OPTION_hda;
ths96d30e42007-01-07 20:42:14 +00007767 hd_filename[hd_index] = optarg;
7768 if (hd_index == cdrom_index)
7769 cdrom_index = -1;
bellardcc1daa42005-06-05 14:49:17 +00007770 }
bellardfc01f7e2003-06-30 10:03:06 +00007771 break;
balrog3e3d5812007-04-30 02:09:25 +00007772 case QEMU_OPTION_mtdblock:
7773 mtd_filename = optarg;
7774 break;
pbrooka1bb27b2007-04-06 16:49:48 +00007775 case QEMU_OPTION_sd:
7776 sd_filename = optarg;
7777 break;
j_mayer86f55662007-04-24 06:52:59 +00007778 case QEMU_OPTION_pflash:
7779 if (pflash_index >= MAX_PFLASH) {
7780 fprintf(stderr, "qemu: too many parallel flash images\n");
7781 exit(1);
7782 }
7783 pflash_filename[pflash_index++] = optarg;
7784 break;
bellardcd6f1162004-05-13 22:02:20 +00007785 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00007786 snapshot = 1;
7787 break;
bellardcd6f1162004-05-13 22:02:20 +00007788 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00007789 {
bellard330d0412003-07-26 18:11:40 +00007790 const char *p;
7791 p = optarg;
7792 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007793 if (cyls < 1 || cyls > 16383)
7794 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00007795 if (*p != ',')
7796 goto chs_fail;
7797 p++;
7798 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007799 if (heads < 1 || heads > 16)
7800 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00007801 if (*p != ',')
7802 goto chs_fail;
7803 p++;
7804 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007805 if (secs < 1 || secs > 63)
7806 goto chs_fail;
7807 if (*p == ',') {
7808 p++;
7809 if (!strcmp(p, "none"))
7810 translation = BIOS_ATA_TRANSLATION_NONE;
7811 else if (!strcmp(p, "lba"))
7812 translation = BIOS_ATA_TRANSLATION_LBA;
7813 else if (!strcmp(p, "auto"))
7814 translation = BIOS_ATA_TRANSLATION_AUTO;
7815 else
7816 goto chs_fail;
7817 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00007818 chs_fail:
bellard46d47672004-11-16 01:45:27 +00007819 fprintf(stderr, "qemu: invalid physical CHS format\n");
7820 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00007821 }
bellard330d0412003-07-26 18:11:40 +00007822 }
7823 break;
bellardcd6f1162004-05-13 22:02:20 +00007824 case QEMU_OPTION_nographic:
bellard8d11df92004-08-24 21:13:40 +00007825 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
thsa39437a2007-03-25 20:27:04 +00007826 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null");
ths20d8a3e2007-02-18 17:04:49 +00007827 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
bellarda20dd502003-09-30 21:07:02 +00007828 nographic = 1;
7829 break;
balroga171fe32007-04-30 01:48:07 +00007830 case QEMU_OPTION_portrait:
7831 graphic_rotate = 1;
7832 break;
bellardcd6f1162004-05-13 22:02:20 +00007833 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00007834 kernel_filename = optarg;
7835 break;
bellardcd6f1162004-05-13 22:02:20 +00007836 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00007837 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00007838 break;
bellardcd6f1162004-05-13 22:02:20 +00007839 case QEMU_OPTION_cdrom:
ths96d30e42007-01-07 20:42:14 +00007840 if (cdrom_index >= 0) {
7841 hd_filename[cdrom_index] = optarg;
bellardcc1daa42005-06-05 14:49:17 +00007842 }
bellard36b486b2003-11-11 13:36:08 +00007843 break;
bellardcd6f1162004-05-13 22:02:20 +00007844 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00007845 boot_devices = optarg;
7846 /* We just do some generic consistency checks */
7847 {
7848 /* Could easily be extended to 64 devices if needed */
7849 const unsigned char *p;
7850
7851 boot_devices_bitmap = 0;
7852 for (p = boot_devices; *p != '\0'; p++) {
7853 /* Allowed boot devices are:
7854 * a b : floppy disk drives
7855 * c ... f : IDE disk drives
7856 * g ... m : machine implementation dependant drives
7857 * n ... p : network devices
7858 * It's up to each machine implementation to check
7859 * if the given boot devices match the actual hardware
7860 * implementation and firmware features.
7861 */
7862 if (*p < 'a' || *p > 'q') {
7863 fprintf(stderr, "Invalid boot device '%c'\n", *p);
7864 exit(1);
7865 }
7866 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
7867 fprintf(stderr,
7868 "Boot device '%c' was given twice\n",*p);
7869 exit(1);
7870 }
7871 boot_devices_bitmap |= 1 << (*p - 'a');
7872 }
bellard36b486b2003-11-11 13:36:08 +00007873 }
7874 break;
bellardcd6f1162004-05-13 22:02:20 +00007875 case QEMU_OPTION_fda:
bellardc45886d2004-01-05 00:02:06 +00007876 fd_filename[0] = optarg;
7877 break;
bellardcd6f1162004-05-13 22:02:20 +00007878 case QEMU_OPTION_fdb:
bellardc45886d2004-01-05 00:02:06 +00007879 fd_filename[1] = optarg;
7880 break;
bellard52ca8d62006-06-14 16:03:05 +00007881#ifdef TARGET_I386
7882 case QEMU_OPTION_no_fd_bootchk:
7883 fd_bootchk = 0;
7884 break;
7885#endif
bellardcd6f1162004-05-13 22:02:20 +00007886 case QEMU_OPTION_no_code_copy:
bellard77fef8c2004-02-16 22:05:46 +00007887 code_copy_enabled = 0;
7888 break;
bellard7c9d8e02005-11-15 22:16:05 +00007889 case QEMU_OPTION_net:
7890 if (nb_net_clients >= MAX_NET_CLIENTS) {
7891 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00007892 exit(1);
7893 }
bellard7c9d8e02005-11-15 22:16:05 +00007894 pstrcpy(net_clients[nb_net_clients],
7895 sizeof(net_clients[0]),
7896 optarg);
7897 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00007898 break;
bellardc7f74642004-08-24 21:57:12 +00007899#ifdef CONFIG_SLIRP
7900 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00007901 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00007902 break;
ths47d5d012007-02-20 00:05:08 +00007903 case QEMU_OPTION_bootp:
7904 bootp_filename = optarg;
7905 break;
bellardc94c8d62004-09-13 21:37:34 +00007906#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007907 case QEMU_OPTION_smb:
7908 net_slirp_smb(optarg);
7909 break;
bellardc94c8d62004-09-13 21:37:34 +00007910#endif
bellard9bf05442004-08-25 22:12:49 +00007911 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00007912 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00007913 break;
bellardc7f74642004-08-24 21:57:12 +00007914#endif
bellard1d14ffa2005-10-30 18:58:22 +00007915#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007916 case QEMU_OPTION_audio_help:
7917 AUD_help ();
7918 exit (0);
7919 break;
7920 case QEMU_OPTION_soundhw:
7921 select_soundhw (optarg);
7922 break;
7923#endif
bellardcd6f1162004-05-13 22:02:20 +00007924 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00007925 help(0);
bellardcd6f1162004-05-13 22:02:20 +00007926 break;
7927 case QEMU_OPTION_m:
7928 ram_size = atoi(optarg) * 1024 * 1024;
7929 if (ram_size <= 0)
ths15f82202007-06-29 23:26:08 +00007930 help(1);
bellardcd6f1162004-05-13 22:02:20 +00007931 if (ram_size > PHYS_RAM_MAX_SIZE) {
7932 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
7933 PHYS_RAM_MAX_SIZE / (1024 * 1024));
7934 exit(1);
7935 }
7936 break;
7937 case QEMU_OPTION_d:
7938 {
7939 int mask;
7940 CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00007941
bellardcd6f1162004-05-13 22:02:20 +00007942 mask = cpu_str_to_log_mask(optarg);
7943 if (!mask) {
7944 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00007945 for(item = cpu_log_items; item->mask != 0; item++) {
7946 printf("%-10s %s\n", item->name, item->help);
7947 }
7948 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00007949 }
7950 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00007951 }
bellardcd6f1162004-05-13 22:02:20 +00007952 break;
bellard67b915a2004-03-31 23:37:16 +00007953#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00007954 case QEMU_OPTION_s:
7955 use_gdbstub = 1;
7956 break;
7957 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00007958 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00007959 break;
bellard67b915a2004-03-31 23:37:16 +00007960#endif
bellardcd6f1162004-05-13 22:02:20 +00007961 case QEMU_OPTION_L:
7962 bios_dir = optarg;
7963 break;
j_mayer1192dad2007-10-05 13:08:35 +00007964 case QEMU_OPTION_bios:
7965 bios_name = optarg;
7966 break;
bellardcd6f1162004-05-13 22:02:20 +00007967 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00007968 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00007969 break;
bellard3d11d0e2004-12-12 16:56:30 +00007970 case QEMU_OPTION_k:
7971 keyboard_layout = optarg;
7972 break;
bellardee22c2f2004-06-03 12:49:50 +00007973 case QEMU_OPTION_localtime:
7974 rtc_utc = 0;
7975 break;
bellard1f042752004-06-05 13:46:47 +00007976 case QEMU_OPTION_cirrusvga:
7977 cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +00007978 vmsvga_enabled = 0;
7979 break;
7980 case QEMU_OPTION_vmsvga:
7981 cirrus_vga_enabled = 0;
7982 vmsvga_enabled = 1;
bellard1f042752004-06-05 13:46:47 +00007983 break;
bellard1bfe8562004-07-08 21:17:50 +00007984 case QEMU_OPTION_std_vga:
7985 cirrus_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +00007986 vmsvga_enabled = 0;
bellard1bfe8562004-07-08 21:17:50 +00007987 break;
bellarde9b137c2004-06-21 16:46:10 +00007988 case QEMU_OPTION_g:
7989 {
7990 const char *p;
7991 int w, h, depth;
7992 p = optarg;
7993 w = strtol(p, (char **)&p, 10);
7994 if (w <= 0) {
7995 graphic_error:
7996 fprintf(stderr, "qemu: invalid resolution or depth\n");
7997 exit(1);
7998 }
7999 if (*p != 'x')
8000 goto graphic_error;
8001 p++;
8002 h = strtol(p, (char **)&p, 10);
8003 if (h <= 0)
8004 goto graphic_error;
8005 if (*p == 'x') {
8006 p++;
8007 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00008008 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00008009 depth != 24 && depth != 32)
8010 goto graphic_error;
8011 } else if (*p == '\0') {
8012 depth = graphic_depth;
8013 } else {
8014 goto graphic_error;
8015 }
ths3b46e622007-09-17 08:09:54 +00008016
bellarde9b137c2004-06-21 16:46:10 +00008017 graphic_width = w;
8018 graphic_height = h;
8019 graphic_depth = depth;
8020 }
8021 break;
ths20d8a3e2007-02-18 17:04:49 +00008022 case QEMU_OPTION_echr:
8023 {
8024 char *r;
8025 term_escape_char = strtol(optarg, &r, 0);
8026 if (r == optarg)
8027 printf("Bad argument to echr\n");
8028 break;
8029 }
bellard82c643f2004-07-14 17:28:13 +00008030 case QEMU_OPTION_monitor:
8031 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
8032 break;
8033 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00008034 if (serial_device_index >= MAX_SERIAL_PORTS) {
8035 fprintf(stderr, "qemu: too many serial ports\n");
8036 exit(1);
8037 }
ths5fafdf22007-09-16 21:08:06 +00008038 pstrcpy(serial_devices[serial_device_index],
bellard8d11df92004-08-24 21:13:40 +00008039 sizeof(serial_devices[0]), optarg);
8040 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00008041 break;
bellard6508fe52005-01-15 12:02:56 +00008042 case QEMU_OPTION_parallel:
8043 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
8044 fprintf(stderr, "qemu: too many parallel ports\n");
8045 exit(1);
8046 }
ths5fafdf22007-09-16 21:08:06 +00008047 pstrcpy(parallel_devices[parallel_device_index],
bellard6508fe52005-01-15 12:02:56 +00008048 sizeof(parallel_devices[0]), optarg);
8049 parallel_device_index++;
8050 break;
bellardd63d3072004-10-03 13:29:03 +00008051 case QEMU_OPTION_loadvm:
8052 loadvm = optarg;
8053 break;
8054 case QEMU_OPTION_full_screen:
8055 full_screen = 1;
8056 break;
ths667acca2006-12-11 02:08:05 +00008057#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00008058 case QEMU_OPTION_no_frame:
8059 no_frame = 1;
8060 break;
ths3780e192007-06-21 21:08:02 +00008061 case QEMU_OPTION_alt_grab:
8062 alt_grab = 1;
8063 break;
ths667acca2006-12-11 02:08:05 +00008064 case QEMU_OPTION_no_quit:
8065 no_quit = 1;
8066 break;
8067#endif
bellardf7cce892004-12-08 22:21:25 +00008068 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00008069 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00008070 break;
bellarda09db212005-04-30 16:10:35 +00008071#ifdef TARGET_I386
8072 case QEMU_OPTION_win2k_hack:
8073 win2k_install_hack = 1;
8074 break;
8075#endif
bellardd993e022005-02-10 22:00:06 +00008076#ifdef USE_KQEMU
8077 case QEMU_OPTION_no_kqemu:
8078 kqemu_allowed = 0;
8079 break;
bellard89bfc102006-02-08 22:46:31 +00008080 case QEMU_OPTION_kernel_kqemu:
8081 kqemu_allowed = 2;
8082 break;
bellardd993e022005-02-10 22:00:06 +00008083#endif
bellardbb36d472005-11-05 14:22:28 +00008084 case QEMU_OPTION_usb:
8085 usb_enabled = 1;
8086 break;
bellarda594cfb2005-11-06 16:13:29 +00008087 case QEMU_OPTION_usbdevice:
8088 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00008089 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00008090 fprintf(stderr, "Too many USB devices\n");
8091 exit(1);
8092 }
8093 pstrcpy(usb_devices[usb_devices_index],
8094 sizeof(usb_devices[usb_devices_index]),
8095 optarg);
8096 usb_devices_index++;
8097 break;
bellard6a00d602005-11-21 23:25:50 +00008098 case QEMU_OPTION_smp:
8099 smp_cpus = atoi(optarg);
bellardba3c64f2005-12-05 20:31:52 +00008100 if (smp_cpus < 1 || smp_cpus > MAX_CPUS) {
bellard6a00d602005-11-21 23:25:50 +00008101 fprintf(stderr, "Invalid number of CPUs\n");
8102 exit(1);
8103 }
8104 break;
bellard24236862006-04-30 21:28:36 +00008105 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00008106 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00008107 break;
bellard6515b202006-05-03 22:02:44 +00008108 case QEMU_OPTION_no_acpi:
8109 acpi_enabled = 0;
8110 break;
bellardd1beab82006-10-02 19:44:22 +00008111 case QEMU_OPTION_no_reboot:
8112 no_reboot = 1;
8113 break;
balrog9467cd42007-05-01 01:34:14 +00008114 case QEMU_OPTION_show_cursor:
8115 cursor_hide = 0;
8116 break;
ths71e3ceb2006-12-22 02:11:31 +00008117 case QEMU_OPTION_daemonize:
8118 daemonize = 1;
8119 break;
ths9ae02552007-01-05 17:39:04 +00008120 case QEMU_OPTION_option_rom:
8121 if (nb_option_roms >= MAX_OPTION_ROMS) {
8122 fprintf(stderr, "Too many option ROMs\n");
8123 exit(1);
8124 }
8125 option_rom[nb_option_roms] = optarg;
8126 nb_option_roms++;
8127 break;
pbrook8e716212007-01-20 17:12:09 +00008128 case QEMU_OPTION_semihosting:
8129 semihosting_enabled = 1;
8130 break;
thsc35734b2007-03-19 15:17:08 +00008131 case QEMU_OPTION_name:
8132 qemu_name = optarg;
8133 break;
blueswir166508602007-05-01 14:16:52 +00008134#ifdef TARGET_SPARC
8135 case QEMU_OPTION_prom_env:
8136 if (nb_prom_envs >= MAX_PROM_ENVS) {
8137 fprintf(stderr, "Too many prom variables\n");
8138 exit(1);
8139 }
8140 prom_envs[nb_prom_envs] = optarg;
8141 nb_prom_envs++;
8142 break;
8143#endif
balrog2b8f2d42007-07-27 22:08:46 +00008144#ifdef TARGET_ARM
8145 case QEMU_OPTION_old_param:
8146 old_param = 1;
8147#endif
thsf3dcfad2007-08-24 01:26:02 +00008148 case QEMU_OPTION_clock:
8149 configure_alarms(optarg);
8150 break;
bellard7e0af5d02007-11-07 16:24:33 +00008151 case QEMU_OPTION_startdate:
8152 {
8153 struct tm tm;
8154 if (!strcmp(optarg, "now")) {
8155 rtc_start_date = -1;
8156 } else {
8157 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
8158 &tm.tm_year,
8159 &tm.tm_mon,
8160 &tm.tm_mday,
8161 &tm.tm_hour,
8162 &tm.tm_min,
8163 &tm.tm_sec) == 6) {
8164 /* OK */
8165 } else if (sscanf(optarg, "%d-%d-%d",
8166 &tm.tm_year,
8167 &tm.tm_mon,
8168 &tm.tm_mday) == 3) {
8169 tm.tm_hour = 0;
8170 tm.tm_min = 0;
8171 tm.tm_sec = 0;
8172 } else {
8173 goto date_fail;
8174 }
8175 tm.tm_year -= 1900;
8176 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00008177 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00008178 if (rtc_start_date == -1) {
8179 date_fail:
8180 fprintf(stderr, "Invalid date format. Valid format are:\n"
8181 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8182 exit(1);
8183 }
8184 }
8185 }
8186 break;
bellardcd6f1162004-05-13 22:02:20 +00008187 }
bellard0824d6f2003-06-24 13:42:40 +00008188 }
8189 }
bellard330d0412003-07-26 18:11:40 +00008190
ths71e3ceb2006-12-22 02:11:31 +00008191#ifndef _WIN32
8192 if (daemonize && !nographic && vnc_display == NULL) {
8193 fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n");
8194 daemonize = 0;
8195 }
8196
8197 if (daemonize) {
8198 pid_t pid;
8199
8200 if (pipe(fds) == -1)
8201 exit(1);
8202
8203 pid = fork();
8204 if (pid > 0) {
8205 uint8_t status;
8206 ssize_t len;
8207
8208 close(fds[1]);
8209
8210 again:
ths93815bc2007-03-19 15:58:31 +00008211 len = read(fds[0], &status, 1);
8212 if (len == -1 && (errno == EINTR))
8213 goto again;
8214
8215 if (len != 1)
8216 exit(1);
8217 else if (status == 1) {
8218 fprintf(stderr, "Could not acquire pidfile\n");
8219 exit(1);
8220 } else
8221 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00008222 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00008223 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00008224
8225 setsid();
8226
8227 pid = fork();
8228 if (pid > 0)
8229 exit(0);
8230 else if (pid < 0)
8231 exit(1);
8232
8233 umask(027);
8234 chdir("/");
8235
8236 signal(SIGTSTP, SIG_IGN);
8237 signal(SIGTTOU, SIG_IGN);
8238 signal(SIGTTIN, SIG_IGN);
8239 }
8240#endif
8241
thsaa26bb22007-03-25 21:33:06 +00008242 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00008243 if (daemonize) {
8244 uint8_t status = 1;
8245 write(fds[1], &status, 1);
8246 } else
8247 fprintf(stderr, "Could not acquire pid file\n");
8248 exit(1);
8249 }
8250
bellardff3fbb32006-01-08 10:53:14 +00008251#ifdef USE_KQEMU
8252 if (smp_cpus > 1)
8253 kqemu_allowed = 0;
8254#endif
bellarda20dd502003-09-30 21:07:02 +00008255 linux_boot = (kernel_filename != NULL);
j_mayer28c5af52007-11-11 01:50:45 +00008256 net_boot = (boot_devices_bitmap >> ('n' - 'a')) && 0xF;
8257
8258 /* XXX: this should not be: some embedded targets just have flash */
8259 if (!linux_boot && net_boot == 0 &&
ths5fafdf22007-09-16 21:08:06 +00008260 hd_filename[0] == '\0' &&
ths96d30e42007-01-07 20:42:14 +00008261 (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
bellardc45886d2004-01-05 00:02:06 +00008262 fd_filename[0] == '\0')
ths15f82202007-06-29 23:26:08 +00008263 help(1);
bellard0824d6f2003-06-24 13:42:40 +00008264
ths96d30e42007-01-07 20:42:14 +00008265 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00008266 if (!boot_devices[0]) {
balrog6ac0e822007-10-31 01:54:04 +00008267 if (hd_filename[0] != '\0')
j_mayer28c5af52007-11-11 01:50:45 +00008268 boot_devices = "c";
balrog6ac0e822007-10-31 01:54:04 +00008269 else if (fd_filename[0] != '\0')
j_mayer28c5af52007-11-11 01:50:45 +00008270 boot_devices = "a";
ths96d30e42007-01-07 20:42:14 +00008271 else
j_mayer28c5af52007-11-11 01:50:45 +00008272 boot_devices = "d";
ths96d30e42007-01-07 20:42:14 +00008273 }
bellardb118d612003-06-30 23:36:21 +00008274 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00008275
pbrook634fce92006-07-15 17:40:09 +00008276 init_timers();
8277 init_timer_alarm();
bellard83f64092006-08-01 16:21:11 +00008278 qemu_aio_init();
pbrook634fce92006-07-15 17:40:09 +00008279
bellardfd1dff42006-02-01 21:29:26 +00008280#ifdef _WIN32
8281 socket_init();
8282#endif
8283
bellard7c9d8e02005-11-15 22:16:05 +00008284 /* init network clients */
8285 if (nb_net_clients == 0) {
8286 /* if no clients, we use a default config */
8287 pstrcpy(net_clients[0], sizeof(net_clients[0]),
8288 "nic");
8289 pstrcpy(net_clients[1], sizeof(net_clients[0]),
8290 "user");
8291 nb_net_clients = 2;
bellardc20709a2004-04-21 23:27:19 +00008292 }
8293
bellard7c9d8e02005-11-15 22:16:05 +00008294 for(i = 0;i < nb_net_clients; i++) {
8295 if (net_client_init(net_clients[i]) < 0)
8296 exit(1);
bellard702c6512004-04-02 21:21:32 +00008297 }
blueswir1833c7172007-05-27 19:36:43 +00008298 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8299 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
8300 continue;
8301 if (vlan->nb_guest_devs == 0) {
8302 fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
8303 exit(1);
8304 }
8305 if (vlan->nb_host_devs == 0)
8306 fprintf(stderr,
8307 "Warning: vlan %d is not connected to host network\n",
8308 vlan->id);
8309 }
bellardf1510b22003-06-25 00:07:40 +00008310
thseec85c22007-01-05 17:41:07 +00008311#ifdef TARGET_I386
j_mayer28c5af52007-11-11 01:50:45 +00008312 /* XXX: this should be moved in the PC machine instanciation code */
8313 if (net_boot != 0) {
8314 int netroms = 0;
8315 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00008316 const char *model = nd_table[i].model;
8317 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00008318 if (net_boot & (1 << i)) {
8319 if (model == NULL)
8320 model = "ne2k_pci";
8321 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
8322 if (get_image_size(buf) > 0) {
8323 if (nb_option_roms >= MAX_OPTION_ROMS) {
8324 fprintf(stderr, "Too many option ROMs\n");
8325 exit(1);
8326 }
8327 option_rom[nb_option_roms] = strdup(buf);
8328 nb_option_roms++;
8329 netroms++;
8330 }
8331 }
thseec85c22007-01-05 17:41:07 +00008332 }
j_mayer28c5af52007-11-11 01:50:45 +00008333 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00008334 fprintf(stderr, "No valid PXE rom found for network device\n");
8335 exit(1);
8336 }
thseec85c22007-01-05 17:41:07 +00008337 }
8338#endif
8339
bellard0824d6f2003-06-24 13:42:40 +00008340 /* init the memory */
bellard970ac5a2007-02-08 23:09:59 +00008341 phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
ths9ae02552007-01-05 17:39:04 +00008342
bellardd993e022005-02-10 22:00:06 +00008343 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00008344 if (!phys_ram_base) {
8345 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00008346 exit(1);
8347 }
8348
ths96d30e42007-01-07 20:42:14 +00008349 /* we always create the cdrom drive, even if no disk is there */
bellard5905b2e2004-08-01 21:53:26 +00008350 bdrv_init();
ths96d30e42007-01-07 20:42:14 +00008351 if (cdrom_index >= 0) {
8352 bs_table[cdrom_index] = bdrv_new("cdrom");
8353 bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM);
bellardc4b1fcc2004-03-14 21:44:30 +00008354 }
8355
ths96d30e42007-01-07 20:42:14 +00008356 /* open the virtual block devices */
8357 for(i = 0; i < MAX_DISKS; i++) {
8358 if (hd_filename[i]) {
8359 if (!bs_table[i]) {
8360 char buf[64];
8361 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
8362 bs_table[i] = bdrv_new(buf);
8363 }
8364 if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8365 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
8366 hd_filename[i]);
8367 exit(1);
8368 }
8369 if (i == 0 && cyls != 0) {
8370 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
8371 bdrv_set_translation_hint(bs_table[i], translation);
8372 }
8373 }
bellardc4b1fcc2004-03-14 21:44:30 +00008374 }
8375
8376 /* we always create at least one floppy disk */
8377 fd_table[0] = bdrv_new("fda");
8378 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
8379
8380 for(i = 0; i < MAX_FD; i++) {
8381 if (fd_filename[i]) {
8382 if (!fd_table[i]) {
8383 char buf[64];
8384 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
8385 fd_table[i] = bdrv_new(buf);
8386 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
8387 }
pbrooka1bb27b2007-04-06 16:49:48 +00008388 if (fd_filename[i][0] != '\0') {
bellard83f64092006-08-01 16:21:11 +00008389 if (bdrv_open(fd_table[i], fd_filename[i],
8390 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
bellardc20709a2004-04-21 23:27:19 +00008391 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
bellardc4b1fcc2004-03-14 21:44:30 +00008392 fd_filename[i]);
8393 exit(1);
8394 }
8395 }
bellard33e39632003-07-06 17:15:21 +00008396 }
8397 }
8398
balrog2bac6012007-04-30 01:34:31 +00008399 /* Open the virtual parallel flash block devices */
j_mayer86f55662007-04-24 06:52:59 +00008400 for(i = 0; i < MAX_PFLASH; i++) {
8401 if (pflash_filename[i]) {
8402 if (!pflash_table[i]) {
8403 char buf[64];
8404 snprintf(buf, sizeof(buf), "fl%c", i + 'a');
8405 pflash_table[i] = bdrv_new(buf);
8406 }
8407 if (bdrv_open(pflash_table[i], pflash_filename[i],
8408 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8409 fprintf(stderr, "qemu: could not open flash image '%s'\n",
8410 pflash_filename[i]);
8411 exit(1);
8412 }
8413 }
8414 }
8415
pbrooka1bb27b2007-04-06 16:49:48 +00008416 sd_bdrv = bdrv_new ("sd");
8417 /* FIXME: This isn't really a floppy, but it's a reasonable
8418 approximation. */
8419 bdrv_set_type_hint(sd_bdrv, BDRV_TYPE_FLOPPY);
8420 if (sd_filename) {
8421 if (bdrv_open(sd_bdrv, sd_filename,
8422 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8423 fprintf(stderr, "qemu: could not open SD card image %s\n",
8424 sd_filename);
balrog2bac6012007-04-30 01:34:31 +00008425 } else
balroga171fe32007-04-30 01:48:07 +00008426 qemu_key_check(sd_bdrv, sd_filename);
pbrooka1bb27b2007-04-06 16:49:48 +00008427 }
8428
balrog3e3d5812007-04-30 02:09:25 +00008429 if (mtd_filename) {
8430 mtd_bdrv = bdrv_new ("mtd");
8431 if (bdrv_open(mtd_bdrv, mtd_filename,
8432 snapshot ? BDRV_O_SNAPSHOT : 0) < 0 ||
8433 qemu_key_check(mtd_bdrv, mtd_filename)) {
8434 fprintf(stderr, "qemu: could not open Flash image %s\n",
8435 mtd_filename);
8436 bdrv_delete(mtd_bdrv);
8437 mtd_bdrv = 0;
8438 }
8439 }
8440
bellardc88676f2006-08-06 13:36:11 +00008441 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
8442 register_savevm("ram", 0, 2, ram_save, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00008443
bellard330d0412003-07-26 18:11:40 +00008444 init_ioports();
bellard0824d6f2003-06-24 13:42:40 +00008445
bellard313aa562003-08-10 21:52:11 +00008446 /* terminal init */
ths740733b2007-06-08 01:57:56 +00008447 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00008448 if (nographic) {
ths2ff89792007-06-21 23:34:19 +00008449 /* nearly nothing to do */
8450 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00008451 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00008452 vnc_display_init(ds);
8453 if (vnc_display_open(ds, vnc_display) < 0)
8454 exit(1);
bellard313aa562003-08-10 21:52:11 +00008455 } else {
bellard5b0753e2005-03-01 21:37:28 +00008456#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00008457 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00008458#elif defined(CONFIG_COCOA)
8459 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00008460#endif
8461 }
bellard0824d6f2003-06-24 13:42:40 +00008462
ths20d8a3e2007-02-18 17:04:49 +00008463 /* Maintain compatibility with multiple stdio monitors */
8464 if (!strcmp(monitor_device,"stdio")) {
8465 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
8466 if (!strcmp(serial_devices[i],"mon:stdio")) {
8467 monitor_device[0] = '\0';
8468 break;
8469 } else if (!strcmp(serial_devices[i],"stdio")) {
8470 monitor_device[0] = '\0';
8471 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio");
8472 break;
8473 }
8474 }
bellard82c643f2004-07-14 17:28:13 +00008475 }
ths20d8a3e2007-02-18 17:04:49 +00008476 if (monitor_device[0] != '\0') {
8477 monitor_hd = qemu_chr_open(monitor_device);
8478 if (!monitor_hd) {
8479 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
8480 exit(1);
8481 }
8482 monitor_init(monitor_hd, !nographic);
8483 }
bellard82c643f2004-07-14 17:28:13 +00008484
bellard8d11df92004-08-24 21:13:40 +00008485 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008486 const char *devname = serial_devices[i];
8487 if (devname[0] != '\0' && strcmp(devname, "none")) {
8488 serial_hds[i] = qemu_chr_open(devname);
bellard8d11df92004-08-24 21:13:40 +00008489 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008490 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008491 devname);
bellard8d11df92004-08-24 21:13:40 +00008492 exit(1);
8493 }
thsaf3a9032007-07-11 23:14:59 +00008494 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008495 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00008496 }
bellard82c643f2004-07-14 17:28:13 +00008497 }
bellard82c643f2004-07-14 17:28:13 +00008498
bellard6508fe52005-01-15 12:02:56 +00008499 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008500 const char *devname = parallel_devices[i];
8501 if (devname[0] != '\0' && strcmp(devname, "none")) {
8502 parallel_hds[i] = qemu_chr_open(devname);
bellard6508fe52005-01-15 12:02:56 +00008503 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008504 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008505 devname);
bellard6508fe52005-01-15 12:02:56 +00008506 exit(1);
8507 }
thsaf3a9032007-07-11 23:14:59 +00008508 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008509 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00008510 }
8511 }
8512
j_mayer28c5af52007-11-11 01:50:45 +00008513 machine->init(ram_size, vga_ram_size, boot_devices,
bellardcc1daa42005-06-05 14:49:17 +00008514 ds, fd_filename, snapshot,
j_mayer94fc95c2007-03-05 19:44:02 +00008515 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00008516
pbrook0d92ed32006-05-21 16:30:15 +00008517 /* init USB devices */
8518 if (usb_enabled) {
8519 for(i = 0; i < usb_devices_index; i++) {
8520 if (usb_device_add(usb_devices[i]) < 0) {
8521 fprintf(stderr, "Warning: could not add USB device %s\n",
8522 usb_devices[i]);
8523 }
8524 }
8525 }
8526
ths740733b2007-06-08 01:57:56 +00008527 if (display_state.dpy_refresh) {
8528 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
8529 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
8530 }
bellard7f7f9872003-10-30 01:11:23 +00008531
bellard67b915a2004-03-31 23:37:16 +00008532#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00008533 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00008534 /* XXX: use standard host:port notation and modify options
8535 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00008536 if (gdbserver_start(gdbstub_port) < 0) {
8537 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00008538 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00008539 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00008540 }
balrog45669e02007-07-02 13:20:17 +00008541 }
bellard67b915a2004-03-31 23:37:16 +00008542#endif
balrog45669e02007-07-02 13:20:17 +00008543
bellardd63d3072004-10-03 13:29:03 +00008544 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00008545 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00008546
bellard67b915a2004-03-31 23:37:16 +00008547 {
bellard5905b2e2004-08-01 21:53:26 +00008548 /* XXX: simplify init */
8549 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00008550 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00008551 vm_start();
8552 }
bellard0824d6f2003-06-24 13:42:40 +00008553 }
thsffd843b2006-12-21 19:46:43 +00008554
ths71e3ceb2006-12-22 02:11:31 +00008555 if (daemonize) {
8556 uint8_t status = 0;
8557 ssize_t len;
8558 int fd;
8559
8560 again1:
8561 len = write(fds[1], &status, 1);
8562 if (len == -1 && (errno == EINTR))
8563 goto again1;
8564
8565 if (len != 1)
8566 exit(1);
8567
balrogaeb30be2007-07-02 15:03:13 +00008568 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00008569 if (fd == -1)
8570 exit(1);
8571
8572 dup2(fd, 0);
8573 dup2(fd, 1);
8574 dup2(fd, 2);
8575
8576 close(fd);
8577 }
8578
bellard8a7ddc32004-03-31 19:00:16 +00008579 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00008580 quit_timers();
thsb46a8902007-10-21 23:20:45 +00008581
ths7d294b62007-10-26 17:21:58 +00008582#if !defined(_WIN32)
thsb46a8902007-10-21 23:20:45 +00008583 /* close network clients */
8584 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8585 VLANClientState *vc;
8586
ths7d294b62007-10-26 17:21:58 +00008587 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
thsb46a8902007-10-21 23:20:45 +00008588 if (vc->fd_read == tap_receive) {
8589 char ifname[64];
8590 TAPState *s = vc->opaque;
8591
8592 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
8593 s->down_script[0])
8594 launch_script(s->down_script, ifname, s->fd);
8595 }
thsb46a8902007-10-21 23:20:45 +00008596 }
ths7d294b62007-10-26 17:21:58 +00008597 }
8598#endif
bellard0824d6f2003-06-24 13:42:40 +00008599 return 0;
8600}