blob: 7ffc904e05548daf4a3fb964907c8aecf877e68e [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;
balrog6ac0e822007-10-31 01:54:04 +0000165#if defined(TARGET_I386)
166#define MAX_BOOT_DEVICES 3
167#else
168#define MAX_BOOT_DEVICES 1
169#endif
170static char boot_device[MAX_BOOT_DEVICES + 1];
bellard0ced6582004-05-23 21:06:12 +0000171int ram_size;
bellard80cabfa2004-03-14 12:20:30 +0000172int pit_min_timer_count = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000173int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000174NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000175int vm_running;
bellardee22c2f2004-06-03 12:49:50 +0000176int rtc_utc = 1;
bellard7e0af5d02007-11-07 16:24:33 +0000177int rtc_start_date = -1; /* -1 means now */
bellard1bfe8562004-07-08 21:17:50 +0000178int cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +0000179int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000180#ifdef TARGET_SPARC
181int graphic_width = 1024;
182int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000183int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000184#else
bellard1bfe8562004-07-08 21:17:50 +0000185int graphic_width = 800;
186int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000187int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000188#endif
bellardd63d3072004-10-03 13:29:03 +0000189int full_screen = 0;
ths43523e92007-02-18 18:19:32 +0000190int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000191int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000192CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000193CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000194#ifdef TARGET_I386
195int win2k_install_hack = 0;
196#endif
bellardbb36d472005-11-05 14:22:28 +0000197int usb_enabled = 0;
bellard7c9d8e02005-11-15 22:16:05 +0000198static VLANState *first_vlan;
bellard6a00d602005-11-21 23:25:50 +0000199int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000200const char *vnc_display;
bellardd3e9db92005-12-17 01:27:28 +0000201#if defined(TARGET_SPARC)
bellardba3c64f2005-12-05 20:31:52 +0000202#define MAX_CPUS 16
bellardd3e9db92005-12-17 01:27:28 +0000203#elif defined(TARGET_I386)
204#define MAX_CPUS 255
bellardba3c64f2005-12-05 20:31:52 +0000205#else
bellardd3e9db92005-12-17 01:27:28 +0000206#define MAX_CPUS 1
bellardba3c64f2005-12-05 20:31:52 +0000207#endif
bellard6515b202006-05-03 22:02:44 +0000208int acpi_enabled = 1;
bellard52ca8d62006-06-14 16:03:05 +0000209int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000210int no_reboot = 0;
balrog9467cd42007-05-01 01:34:14 +0000211int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000212int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000213int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000214const char *option_rom[MAX_OPTION_ROMS];
215int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000216int semihosting_enabled = 0;
pbrook3c07f8e2007-01-21 16:47:01 +0000217int autostart = 1;
balrog2b8f2d42007-07-27 22:08:46 +0000218#ifdef TARGET_ARM
219int old_param = 0;
220#endif
thsc35734b2007-03-19 15:17:08 +0000221const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000222int alt_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000223#ifdef TARGET_SPARC
224unsigned int nb_prom_envs = 0;
225const char *prom_envs[MAX_PROM_ENVS];
226#endif
bellard0824d6f2003-06-24 13:42:40 +0000227
balrogaeb30be2007-07-02 15:03:13 +0000228#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
229
bellard0824d6f2003-06-24 13:42:40 +0000230/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000231/* x86 ISA bus support */
232
233target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000234PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000235
bellardc4b1fcc2004-03-14 21:44:30 +0000236uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000237{
238#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000239 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000240#endif
bellardfc01f7e2003-06-30 10:03:06 +0000241 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000242}
243
bellardc4b1fcc2004-03-14 21:44:30 +0000244void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000245{
246#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000247 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000248#endif
249}
250
251/* default is to make two byte accesses */
bellardc4b1fcc2004-03-14 21:44:30 +0000252uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000253{
254 uint32_t data;
bellarddb45c292004-05-12 19:50:26 +0000255 data = ioport_read_table[0][address](ioport_opaque[address], address);
256 address = (address + 1) & (MAX_IOPORTS - 1);
257 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000258 return data;
259}
260
bellardc4b1fcc2004-03-14 21:44:30 +0000261void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000262{
bellarddb45c292004-05-12 19:50:26 +0000263 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
264 address = (address + 1) & (MAX_IOPORTS - 1);
265 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000266}
267
bellardc4b1fcc2004-03-14 21:44:30 +0000268uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000269{
270#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000271 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000272#endif
273 return 0xffffffff;
274}
275
bellardc4b1fcc2004-03-14 21:44:30 +0000276void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000277{
278#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000279 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000280#endif
bellard0824d6f2003-06-24 13:42:40 +0000281}
282
283void init_ioports(void)
284{
285 int i;
286
287 for(i = 0; i < MAX_IOPORTS; i++) {
bellardfc01f7e2003-06-30 10:03:06 +0000288 ioport_read_table[0][i] = default_ioport_readb;
289 ioport_write_table[0][i] = default_ioport_writeb;
290 ioport_read_table[1][i] = default_ioport_readw;
291 ioport_write_table[1][i] = default_ioport_writew;
292 ioport_read_table[2][i] = default_ioport_readl;
293 ioport_write_table[2][i] = default_ioport_writel;
bellard0824d6f2003-06-24 13:42:40 +0000294 }
295}
296
bellardfc01f7e2003-06-30 10:03:06 +0000297/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000298int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000299 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000300{
bellardfc01f7e2003-06-30 10:03:06 +0000301 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000302
bellardc4b1fcc2004-03-14 21:44:30 +0000303 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000304 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000305 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000306 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000307 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000308 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000309 } else {
310 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000311 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000312 }
313 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000314 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000315 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
316 hw_error("register_ioport_read: invalid opaque");
317 ioport_opaque[i] = opaque;
318 }
bellard0824d6f2003-06-24 13:42:40 +0000319 return 0;
320}
321
bellardfc01f7e2003-06-30 10:03:06 +0000322/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000323int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000324 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000325{
bellardfc01f7e2003-06-30 10:03:06 +0000326 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000327
bellardc4b1fcc2004-03-14 21:44:30 +0000328 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000329 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000330 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000331 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000332 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000333 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000334 } else {
335 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000336 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000337 }
338 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000339 ioport_write_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000340 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
ths52f61fd2006-12-22 21:20:52 +0000341 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000342 ioport_opaque[i] = opaque;
343 }
bellardf1510b22003-06-25 00:07:40 +0000344 return 0;
345}
346
bellard69b91032004-05-18 23:05:28 +0000347void isa_unassign_ioport(int start, int length)
348{
349 int i;
350
351 for(i = start; i < start + length; i++) {
352 ioport_read_table[0][i] = default_ioport_readb;
353 ioport_read_table[1][i] = default_ioport_readw;
354 ioport_read_table[2][i] = default_ioport_readl;
355
356 ioport_write_table[0][i] = default_ioport_writeb;
357 ioport_write_table[1][i] = default_ioport_writew;
358 ioport_write_table[2][i] = default_ioport_writel;
359 }
360}
361
bellard20f32282005-01-03 23:36:21 +0000362/***********************************************************/
363
bellardc45886d2004-01-05 00:02:06 +0000364void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000365{
bellardfd872592004-05-12 19:11:15 +0000366#ifdef DEBUG_IOPORT
367 if (loglevel & CPU_LOG_IOPORT)
368 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000369#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000370 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000371#ifdef USE_KQEMU
372 if (env)
373 env->last_io_time = cpu_get_time_fast();
374#endif
bellard0824d6f2003-06-24 13:42:40 +0000375}
376
bellardc45886d2004-01-05 00:02:06 +0000377void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000378{
bellardfd872592004-05-12 19:11:15 +0000379#ifdef DEBUG_IOPORT
380 if (loglevel & CPU_LOG_IOPORT)
381 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000382#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000383 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000384#ifdef USE_KQEMU
385 if (env)
386 env->last_io_time = cpu_get_time_fast();
387#endif
bellard0824d6f2003-06-24 13:42:40 +0000388}
389
bellardc45886d2004-01-05 00:02:06 +0000390void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000391{
bellardfd872592004-05-12 19:11:15 +0000392#ifdef DEBUG_IOPORT
393 if (loglevel & CPU_LOG_IOPORT)
394 fprintf(logfile, "outl: %04x %08x\n", addr, val);
395#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000396 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000397#ifdef USE_KQEMU
398 if (env)
399 env->last_io_time = cpu_get_time_fast();
400#endif
bellard0824d6f2003-06-24 13:42:40 +0000401}
402
bellardc45886d2004-01-05 00:02:06 +0000403int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000404{
bellardfd872592004-05-12 19:11:15 +0000405 int val;
bellardfd872592004-05-12 19:11:15 +0000406 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
407#ifdef DEBUG_IOPORT
408 if (loglevel & CPU_LOG_IOPORT)
409 fprintf(logfile, "inb : %04x %02x\n", addr, val);
410#endif
bellard89bfc102006-02-08 22:46:31 +0000411#ifdef USE_KQEMU
412 if (env)
413 env->last_io_time = cpu_get_time_fast();
414#endif
bellardfd872592004-05-12 19:11:15 +0000415 return val;
bellard0824d6f2003-06-24 13:42:40 +0000416}
417
bellardc45886d2004-01-05 00:02:06 +0000418int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000419{
bellardfd872592004-05-12 19:11:15 +0000420 int val;
bellardfd872592004-05-12 19:11:15 +0000421 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
422#ifdef DEBUG_IOPORT
423 if (loglevel & CPU_LOG_IOPORT)
424 fprintf(logfile, "inw : %04x %04x\n", addr, val);
425#endif
bellard89bfc102006-02-08 22:46:31 +0000426#ifdef USE_KQEMU
427 if (env)
428 env->last_io_time = cpu_get_time_fast();
429#endif
bellardfd872592004-05-12 19:11:15 +0000430 return val;
bellard0824d6f2003-06-24 13:42:40 +0000431}
432
bellardc45886d2004-01-05 00:02:06 +0000433int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000434{
bellardfd872592004-05-12 19:11:15 +0000435 int val;
bellardfd872592004-05-12 19:11:15 +0000436 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
437#ifdef DEBUG_IOPORT
438 if (loglevel & CPU_LOG_IOPORT)
439 fprintf(logfile, "inl : %04x %08x\n", addr, val);
440#endif
bellard89bfc102006-02-08 22:46:31 +0000441#ifdef USE_KQEMU
442 if (env)
443 env->last_io_time = cpu_get_time_fast();
444#endif
bellardfd872592004-05-12 19:11:15 +0000445 return val;
bellard0824d6f2003-06-24 13:42:40 +0000446}
447
448/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000449void hw_error(const char *fmt, ...)
450{
451 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000452 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000453
454 va_start(ap, fmt);
455 fprintf(stderr, "qemu: hardware error: ");
456 vfprintf(stderr, fmt, ap);
457 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000458 for(env = first_cpu; env != NULL; env = env->next_cpu) {
459 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000460#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000461 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000462#else
bellard6a00d602005-11-21 23:25:50 +0000463 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000464#endif
bellard6a00d602005-11-21 23:25:50 +0000465 }
bellard0824d6f2003-06-24 13:42:40 +0000466 va_end(ap);
467 abort();
468}
469
bellard8a7ddc32004-03-31 19:00:16 +0000470/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000471/* keyboard/mouse */
472
473static QEMUPutKBDEvent *qemu_put_kbd_event;
474static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000475static QEMUPutMouseEntry *qemu_put_mouse_event_head;
476static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000477
478void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
479{
480 qemu_put_kbd_event_opaque = opaque;
481 qemu_put_kbd_event = func;
482}
483
ths455204e2007-01-05 16:42:13 +0000484QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
485 void *opaque, int absolute,
486 const char *name)
bellard63066f42004-06-03 18:45:02 +0000487{
ths455204e2007-01-05 16:42:13 +0000488 QEMUPutMouseEntry *s, *cursor;
489
490 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
491 if (!s)
492 return NULL;
493
494 s->qemu_put_mouse_event = func;
495 s->qemu_put_mouse_event_opaque = opaque;
496 s->qemu_put_mouse_event_absolute = absolute;
497 s->qemu_put_mouse_event_name = qemu_strdup(name);
498 s->next = NULL;
499
500 if (!qemu_put_mouse_event_head) {
501 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
502 return s;
503 }
504
505 cursor = qemu_put_mouse_event_head;
506 while (cursor->next != NULL)
507 cursor = cursor->next;
508
509 cursor->next = s;
510 qemu_put_mouse_event_current = s;
511
512 return s;
513}
514
515void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
516{
517 QEMUPutMouseEntry *prev = NULL, *cursor;
518
519 if (!qemu_put_mouse_event_head || entry == NULL)
520 return;
521
522 cursor = qemu_put_mouse_event_head;
523 while (cursor != NULL && cursor != entry) {
524 prev = cursor;
525 cursor = cursor->next;
526 }
527
528 if (cursor == NULL) // does not exist or list empty
529 return;
530 else if (prev == NULL) { // entry is head
531 qemu_put_mouse_event_head = cursor->next;
532 if (qemu_put_mouse_event_current == entry)
533 qemu_put_mouse_event_current = cursor->next;
534 qemu_free(entry->qemu_put_mouse_event_name);
535 qemu_free(entry);
536 return;
537 }
538
539 prev->next = entry->next;
540
541 if (qemu_put_mouse_event_current == entry)
542 qemu_put_mouse_event_current = prev;
543
544 qemu_free(entry->qemu_put_mouse_event_name);
545 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000546}
547
548void kbd_put_keycode(int keycode)
549{
550 if (qemu_put_kbd_event) {
551 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
552 }
553}
554
555void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
556{
ths455204e2007-01-05 16:42:13 +0000557 QEMUPutMouseEvent *mouse_event;
558 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000559 int width;
ths455204e2007-01-05 16:42:13 +0000560
561 if (!qemu_put_mouse_event_current) {
562 return;
563 }
564
565 mouse_event =
566 qemu_put_mouse_event_current->qemu_put_mouse_event;
567 mouse_event_opaque =
568 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
569
570 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000571 if (graphic_rotate) {
572 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
573 width = 0x7fff;
574 else
575 width = graphic_width;
576 mouse_event(mouse_event_opaque,
577 width - dy, dx, dz, buttons_state);
578 } else
579 mouse_event(mouse_event_opaque,
580 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000581 }
582}
583
bellard09b26c52006-04-12 21:09:08 +0000584int kbd_mouse_is_absolute(void)
585{
ths455204e2007-01-05 16:42:13 +0000586 if (!qemu_put_mouse_event_current)
587 return 0;
588
589 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
590}
591
592void do_info_mice(void)
593{
594 QEMUPutMouseEntry *cursor;
595 int index = 0;
596
597 if (!qemu_put_mouse_event_head) {
598 term_printf("No mouse devices connected\n");
599 return;
600 }
601
602 term_printf("Mouse devices available:\n");
603 cursor = qemu_put_mouse_event_head;
604 while (cursor != NULL) {
605 term_printf("%c Mouse #%d: %s\n",
606 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
607 index, cursor->qemu_put_mouse_event_name);
608 index++;
609 cursor = cursor->next;
610 }
611}
612
613void do_mouse_set(int index)
614{
615 QEMUPutMouseEntry *cursor;
616 int i = 0;
617
618 if (!qemu_put_mouse_event_head) {
619 term_printf("No mouse devices connected\n");
620 return;
621 }
622
623 cursor = qemu_put_mouse_event_head;
624 while (cursor != NULL && index != i) {
625 i++;
626 cursor = cursor->next;
627 }
628
629 if (cursor != NULL)
630 qemu_put_mouse_event_current = cursor;
631 else
632 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000633}
634
bellard87858c82003-06-27 12:01:39 +0000635/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000636uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000637{
638 union {
639 uint64_t ll;
640 struct {
641#ifdef WORDS_BIGENDIAN
642 uint32_t high, low;
643#else
644 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000645#endif
bellard87858c82003-06-27 12:01:39 +0000646 } l;
647 } u, res;
648 uint64_t rl, rh;
649
650 u.ll = a;
651 rl = (uint64_t)u.l.low * (uint64_t)b;
652 rh = (uint64_t)u.l.high * (uint64_t)b;
653 rh += (rl >> 32);
654 res.l.high = rh / c;
655 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
656 return res.ll;
657}
658
bellard1dce7c32006-07-13 23:20:22 +0000659/***********************************************************/
660/* real time host monotonic timer */
661
662#define QEMU_TIMER_BASE 1000000000LL
663
664#ifdef WIN32
665
666static int64_t clock_freq;
667
668static void init_get_clock(void)
669{
bellarda8e5ac32006-07-14 09:36:13 +0000670 LARGE_INTEGER freq;
671 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000672 ret = QueryPerformanceFrequency(&freq);
673 if (ret == 0) {
674 fprintf(stderr, "Could not calibrate ticks\n");
675 exit(1);
676 }
677 clock_freq = freq.QuadPart;
678}
679
680static int64_t get_clock(void)
681{
682 LARGE_INTEGER ti;
683 QueryPerformanceCounter(&ti);
684 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
685}
686
687#else
688
689static int use_rt_clock;
690
691static void init_get_clock(void)
692{
693 use_rt_clock = 0;
694#if defined(__linux__)
695 {
696 struct timespec ts;
697 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
698 use_rt_clock = 1;
699 }
700 }
701#endif
702}
703
704static int64_t get_clock(void)
705{
706#if defined(__linux__)
707 if (use_rt_clock) {
708 struct timespec ts;
709 clock_gettime(CLOCK_MONOTONIC, &ts);
710 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000711 } else
bellard1dce7c32006-07-13 23:20:22 +0000712#endif
713 {
714 /* XXX: using gettimeofday leads to problems if the date
715 changes, so it should be avoided. */
716 struct timeval tv;
717 gettimeofday(&tv, NULL);
718 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
719 }
720}
721
722#endif
723
724/***********************************************************/
725/* guest cycle counter */
726
727static int64_t cpu_ticks_prev;
728static int64_t cpu_ticks_offset;
729static int64_t cpu_clock_offset;
730static int cpu_ticks_enabled;
731
732/* return the host CPU cycle counter and handle stop/restart */
733int64_t cpu_get_ticks(void)
734{
735 if (!cpu_ticks_enabled) {
736 return cpu_ticks_offset;
737 } else {
738 int64_t ticks;
739 ticks = cpu_get_real_ticks();
740 if (cpu_ticks_prev > ticks) {
741 /* Note: non increasing ticks may happen if the host uses
742 software suspend */
743 cpu_ticks_offset += cpu_ticks_prev - ticks;
744 }
745 cpu_ticks_prev = ticks;
746 return ticks + cpu_ticks_offset;
747 }
748}
749
750/* return the host CPU monotonic timer and handle stop/restart */
751static int64_t cpu_get_clock(void)
752{
753 int64_t ti;
754 if (!cpu_ticks_enabled) {
755 return cpu_clock_offset;
756 } else {
757 ti = get_clock();
758 return ti + cpu_clock_offset;
759 }
760}
761
762/* enable cpu_get_ticks() */
763void cpu_enable_ticks(void)
764{
765 if (!cpu_ticks_enabled) {
766 cpu_ticks_offset -= cpu_get_real_ticks();
767 cpu_clock_offset -= get_clock();
768 cpu_ticks_enabled = 1;
769 }
770}
771
772/* disable cpu_get_ticks() : the clock is stopped. You must not call
773 cpu_get_ticks() after that. */
774void cpu_disable_ticks(void)
775{
776 if (cpu_ticks_enabled) {
777 cpu_ticks_offset = cpu_get_ticks();
778 cpu_clock_offset = cpu_get_clock();
779 cpu_ticks_enabled = 0;
780 }
781}
782
783/***********************************************************/
784/* timers */
ths5fafdf22007-09-16 21:08:06 +0000785
bellard8a7ddc32004-03-31 19:00:16 +0000786#define QEMU_TIMER_REALTIME 0
787#define QEMU_TIMER_VIRTUAL 1
788
789struct QEMUClock {
790 int type;
791 /* XXX: add frequency */
792};
793
794struct QEMUTimer {
795 QEMUClock *clock;
796 int64_t expire_time;
797 QEMUTimerCB *cb;
798 void *opaque;
799 struct QEMUTimer *next;
800};
801
thsc8994012007-08-19 21:56:03 +0000802struct qemu_alarm_timer {
803 char const *name;
thsefe75412007-08-24 01:36:32 +0000804 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000805
806 int (*start)(struct qemu_alarm_timer *t);
807 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000808 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000809 void *priv;
810};
811
thsefe75412007-08-24 01:36:32 +0000812#define ALARM_FLAG_DYNTICKS 0x1
813
814static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
815{
816 return t->flags & ALARM_FLAG_DYNTICKS;
817}
818
819static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
820{
821 if (!alarm_has_dynticks(t))
822 return;
823
824 t->rearm(t);
825}
826
827/* TODO: MIN_TIMER_REARM_US should be optimized */
828#define MIN_TIMER_REARM_US 250
829
thsc8994012007-08-19 21:56:03 +0000830static struct qemu_alarm_timer *alarm_timer;
831
832#ifdef _WIN32
833
834struct qemu_alarm_win32 {
835 MMRESULT timerId;
836 HANDLE host_alarm;
837 unsigned int period;
838} alarm_win32_data = {0, NULL, -1};
839
840static int win32_start_timer(struct qemu_alarm_timer *t);
841static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000842static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000843
844#else
845
846static int unix_start_timer(struct qemu_alarm_timer *t);
847static void unix_stop_timer(struct qemu_alarm_timer *t);
848
ths231c6582007-08-26 17:29:15 +0000849#ifdef __linux__
850
thsefe75412007-08-24 01:36:32 +0000851static int dynticks_start_timer(struct qemu_alarm_timer *t);
852static void dynticks_stop_timer(struct qemu_alarm_timer *t);
853static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
854
thsc40ec5a2007-08-19 22:09:40 +0000855static int hpet_start_timer(struct qemu_alarm_timer *t);
856static void hpet_stop_timer(struct qemu_alarm_timer *t);
857
thsc8994012007-08-19 21:56:03 +0000858static int rtc_start_timer(struct qemu_alarm_timer *t);
859static void rtc_stop_timer(struct qemu_alarm_timer *t);
860
thsefe75412007-08-24 01:36:32 +0000861#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000862
863#endif /* _WIN32 */
864
865static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000866#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000867#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000868 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
869 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000870 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000871 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000872 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000873 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000874#endif
thsefe75412007-08-24 01:36:32 +0000875 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000876#else
thsefe75412007-08-24 01:36:32 +0000877 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
878 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
879 {"win32", 0, win32_start_timer,
880 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000881#endif
882 {NULL, }
883};
884
thsf3dcfad2007-08-24 01:26:02 +0000885static void show_available_alarms()
886{
887 int i;
888
889 printf("Available alarm timers, in order of precedence:\n");
890 for (i = 0; alarm_timers[i].name; i++)
891 printf("%s\n", alarm_timers[i].name);
892}
893
894static void configure_alarms(char const *opt)
895{
896 int i;
897 int cur = 0;
898 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
899 char *arg;
900 char *name;
901
902 if (!strcmp(opt, "help")) {
903 show_available_alarms();
904 exit(0);
905 }
906
907 arg = strdup(opt);
908
909 /* Reorder the array */
910 name = strtok(arg, ",");
911 while (name) {
912 struct qemu_alarm_timer tmp;
913
balroge2b577e2007-09-17 21:25:20 +0000914 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000915 if (!strcmp(alarm_timers[i].name, name))
916 break;
917 }
918
919 if (i == count) {
920 fprintf(stderr, "Unknown clock %s\n", name);
921 goto next;
922 }
923
924 if (i < cur)
925 /* Ignore */
926 goto next;
927
928 /* Swap */
929 tmp = alarm_timers[i];
930 alarm_timers[i] = alarm_timers[cur];
931 alarm_timers[cur] = tmp;
932
933 cur++;
934next:
935 name = strtok(NULL, ",");
936 }
937
938 free(arg);
939
940 if (cur) {
941 /* Disable remaining timers */
942 for (i = cur; i < count; i++)
943 alarm_timers[i].name = NULL;
944 }
945
946 /* debug */
947 show_available_alarms();
948}
949
bellard8a7ddc32004-03-31 19:00:16 +0000950QEMUClock *rt_clock;
951QEMUClock *vm_clock;
952
953static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000954
955QEMUClock *qemu_new_clock(int type)
956{
957 QEMUClock *clock;
958 clock = qemu_mallocz(sizeof(QEMUClock));
959 if (!clock)
960 return NULL;
961 clock->type = type;
962 return clock;
963}
964
965QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
966{
967 QEMUTimer *ts;
968
969 ts = qemu_mallocz(sizeof(QEMUTimer));
970 ts->clock = clock;
971 ts->cb = cb;
972 ts->opaque = opaque;
973 return ts;
974}
975
976void qemu_free_timer(QEMUTimer *ts)
977{
978 qemu_free(ts);
979}
980
981/* stop a timer, but do not dealloc it */
982void qemu_del_timer(QEMUTimer *ts)
983{
984 QEMUTimer **pt, *t;
985
986 /* NOTE: this code must be signal safe because
987 qemu_timer_expired() can be called from a signal. */
988 pt = &active_timers[ts->clock->type];
989 for(;;) {
990 t = *pt;
991 if (!t)
992 break;
993 if (t == ts) {
994 *pt = t->next;
995 break;
996 }
997 pt = &t->next;
998 }
999}
1000
1001/* modify the current timer so that it will be fired when current_time
1002 >= expire_time. The corresponding callback will be called. */
1003void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1004{
1005 QEMUTimer **pt, *t;
1006
1007 qemu_del_timer(ts);
1008
1009 /* add the timer in the sorted list */
1010 /* NOTE: this code must be signal safe because
1011 qemu_timer_expired() can be called from a signal. */
1012 pt = &active_timers[ts->clock->type];
1013 for(;;) {
1014 t = *pt;
1015 if (!t)
1016 break;
ths5fafdf22007-09-16 21:08:06 +00001017 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001018 break;
1019 pt = &t->next;
1020 }
1021 ts->expire_time = expire_time;
1022 ts->next = *pt;
1023 *pt = ts;
1024}
1025
1026int qemu_timer_pending(QEMUTimer *ts)
1027{
1028 QEMUTimer *t;
1029 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1030 if (t == ts)
1031 return 1;
1032 }
1033 return 0;
1034}
1035
1036static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1037{
1038 if (!timer_head)
1039 return 0;
1040 return (timer_head->expire_time <= current_time);
1041}
1042
1043static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1044{
1045 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001046
bellard8a7ddc32004-03-31 19:00:16 +00001047 for(;;) {
1048 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001049 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001050 break;
1051 /* remove timer from the list before calling the callback */
1052 *ptimer_head = ts->next;
1053 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001054
bellard8a7ddc32004-03-31 19:00:16 +00001055 /* run the callback (the timer list can be modified) */
1056 ts->cb(ts->opaque);
1057 }
thsefe75412007-08-24 01:36:32 +00001058 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00001059}
1060
1061int64_t qemu_get_clock(QEMUClock *clock)
1062{
1063 switch(clock->type) {
1064 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001065 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001066 default:
1067 case QEMU_TIMER_VIRTUAL:
bellard1dce7c32006-07-13 23:20:22 +00001068 return cpu_get_clock();
bellard8a7ddc32004-03-31 19:00:16 +00001069 }
1070}
1071
bellard1dce7c32006-07-13 23:20:22 +00001072static void init_timers(void)
1073{
1074 init_get_clock();
1075 ticks_per_sec = QEMU_TIMER_BASE;
1076 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1077 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1078}
1079
bellard8a7ddc32004-03-31 19:00:16 +00001080/* save a timer */
1081void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1082{
1083 uint64_t expire_time;
1084
1085 if (qemu_timer_pending(ts)) {
1086 expire_time = ts->expire_time;
1087 } else {
1088 expire_time = -1;
1089 }
1090 qemu_put_be64(f, expire_time);
1091}
1092
1093void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1094{
1095 uint64_t expire_time;
1096
1097 expire_time = qemu_get_be64(f);
1098 if (expire_time != -1) {
1099 qemu_mod_timer(ts, expire_time);
1100 } else {
1101 qemu_del_timer(ts);
1102 }
1103}
1104
1105static void timer_save(QEMUFile *f, void *opaque)
1106{
1107 if (cpu_ticks_enabled) {
1108 hw_error("cannot save state if virtual timers are running");
1109 }
1110 qemu_put_be64s(f, &cpu_ticks_offset);
1111 qemu_put_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001112 qemu_put_be64s(f, &cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001113}
1114
1115static int timer_load(QEMUFile *f, void *opaque, int version_id)
1116{
bellardc88676f2006-08-06 13:36:11 +00001117 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001118 return -EINVAL;
1119 if (cpu_ticks_enabled) {
1120 return -EINVAL;
1121 }
1122 qemu_get_be64s(f, &cpu_ticks_offset);
1123 qemu_get_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001124 if (version_id == 2) {
1125 qemu_get_be64s(f, &cpu_clock_offset);
1126 }
bellard8a7ddc32004-03-31 19:00:16 +00001127 return 0;
1128}
1129
bellard67b915a2004-03-31 23:37:16 +00001130#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001131void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001132 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1133#else
bellard8a7ddc32004-03-31 19:00:16 +00001134static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001135#endif
bellard8a7ddc32004-03-31 19:00:16 +00001136{
bellard02ba45c2004-06-25 14:46:23 +00001137#if 0
1138#define DISP_FREQ 1000
1139 {
1140 static int64_t delta_min = INT64_MAX;
1141 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1142 static int count;
1143 ti = qemu_get_clock(vm_clock);
1144 if (last_clock != 0) {
1145 delta = ti - last_clock;
1146 if (delta < delta_min)
1147 delta_min = delta;
1148 if (delta > delta_max)
1149 delta_max = delta;
1150 delta_cum += delta;
1151 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001152 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001153 muldiv64(delta_min, 1000000, ticks_per_sec),
1154 muldiv64(delta_max, 1000000, ticks_per_sec),
1155 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1156 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1157 count = 0;
1158 delta_min = INT64_MAX;
1159 delta_max = 0;
1160 delta_cum = 0;
1161 }
1162 }
1163 last_clock = ti;
1164 }
1165#endif
thsefe75412007-08-24 01:36:32 +00001166 if (alarm_has_dynticks(alarm_timer) ||
1167 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
bellard8a7ddc32004-03-31 19:00:16 +00001168 qemu_get_clock(vm_clock)) ||
1169 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1170 qemu_get_clock(rt_clock))) {
bellard06d9f2f2006-05-01 13:23:04 +00001171#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001172 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1173 SetEvent(data->host_alarm);
bellard06d9f2f2006-05-01 13:23:04 +00001174#endif
bellard6a00d602005-11-21 23:25:50 +00001175 CPUState *env = cpu_single_env;
1176 if (env) {
1177 /* stop the currently executing cpu because a timer occured */
1178 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001179#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001180 if (env->kqemu_enabled) {
1181 kqemu_cpu_interrupt(env);
1182 }
bellarda332e112005-09-03 17:55:47 +00001183#endif
bellard6a00d602005-11-21 23:25:50 +00001184 }
bellard8a7ddc32004-03-31 19:00:16 +00001185 }
1186}
1187
thsefe75412007-08-24 01:36:32 +00001188static uint64_t qemu_next_deadline(void)
1189{
balrog49a9b722007-10-04 19:47:09 +00001190 int64_t nearest_delta_us = INT64_MAX;
thsefe75412007-08-24 01:36:32 +00001191 int64_t vmdelta_us;
1192
1193 if (active_timers[QEMU_TIMER_REALTIME])
1194 nearest_delta_us = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1195 qemu_get_clock(rt_clock))*1000;
1196
1197 if (active_timers[QEMU_TIMER_VIRTUAL]) {
1198 /* round up */
1199 vmdelta_us = (active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1200 qemu_get_clock(vm_clock)+999)/1000;
1201 if (vmdelta_us < nearest_delta_us)
1202 nearest_delta_us = vmdelta_us;
1203 }
1204
1205 /* Avoid arming the timer to negative, zero, or too low values */
1206 if (nearest_delta_us <= MIN_TIMER_REARM_US)
1207 nearest_delta_us = MIN_TIMER_REARM_US;
1208
1209 return nearest_delta_us;
1210}
1211
bellardfd872592004-05-12 19:11:15 +00001212#ifndef _WIN32
1213
bellard829309c2004-05-20 13:20:12 +00001214#if defined(__linux__)
1215
bellardfd872592004-05-12 19:11:15 +00001216#define RTC_FREQ 1024
1217
thsc8994012007-08-19 21:56:03 +00001218static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001219{
thsc8994012007-08-19 21:56:03 +00001220 struct sigaction act;
1221
1222 /* timer signal */
1223 sigfillset(&act.sa_mask);
1224 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001225 act.sa_handler = host_alarm_handler;
1226
1227 sigaction(SIGIO, &act, NULL);
1228 fcntl(fd, F_SETFL, O_ASYNC);
1229 fcntl(fd, F_SETOWN, getpid());
1230}
1231
thsc40ec5a2007-08-19 22:09:40 +00001232static int hpet_start_timer(struct qemu_alarm_timer *t)
1233{
1234 struct hpet_info info;
1235 int r, fd;
1236
1237 fd = open("/dev/hpet", O_RDONLY);
1238 if (fd < 0)
1239 return -1;
1240
1241 /* Set frequency */
1242 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1243 if (r < 0) {
1244 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1245 "error, but for better emulation accuracy type:\n"
1246 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1247 goto fail;
1248 }
1249
1250 /* Check capabilities */
1251 r = ioctl(fd, HPET_INFO, &info);
1252 if (r < 0)
1253 goto fail;
1254
1255 /* Enable periodic mode */
1256 r = ioctl(fd, HPET_EPI, 0);
1257 if (info.hi_flags && (r < 0))
1258 goto fail;
1259
1260 /* Enable interrupt */
1261 r = ioctl(fd, HPET_IE_ON, 0);
1262 if (r < 0)
1263 goto fail;
1264
1265 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001266 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001267
1268 return 0;
1269fail:
1270 close(fd);
1271 return -1;
1272}
1273
1274static void hpet_stop_timer(struct qemu_alarm_timer *t)
1275{
pbrookfcdc2122007-08-23 20:22:22 +00001276 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001277
1278 close(fd);
1279}
1280
thsc8994012007-08-19 21:56:03 +00001281static int rtc_start_timer(struct qemu_alarm_timer *t)
1282{
1283 int rtc_fd;
1284
balrogaeb30be2007-07-02 15:03:13 +00001285 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001286 if (rtc_fd < 0)
1287 return -1;
1288 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1289 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1290 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1291 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1292 goto fail;
1293 }
1294 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1295 fail:
1296 close(rtc_fd);
1297 return -1;
1298 }
thsc8994012007-08-19 21:56:03 +00001299
1300 enable_sigio_timer(rtc_fd);
1301
pbrookfcdc2122007-08-23 20:22:22 +00001302 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001303
bellardfd872592004-05-12 19:11:15 +00001304 return 0;
1305}
1306
thsc8994012007-08-19 21:56:03 +00001307static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001308{
pbrookfcdc2122007-08-23 20:22:22 +00001309 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001310
1311 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001312}
1313
thsefe75412007-08-24 01:36:32 +00001314static int dynticks_start_timer(struct qemu_alarm_timer *t)
1315{
1316 struct sigevent ev;
1317 timer_t host_timer;
1318 struct sigaction act;
1319
1320 sigfillset(&act.sa_mask);
1321 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001322 act.sa_handler = host_alarm_handler;
1323
1324 sigaction(SIGALRM, &act, NULL);
1325
1326 ev.sigev_value.sival_int = 0;
1327 ev.sigev_notify = SIGEV_SIGNAL;
1328 ev.sigev_signo = SIGALRM;
1329
1330 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1331 perror("timer_create");
1332
1333 /* disable dynticks */
1334 fprintf(stderr, "Dynamic Ticks disabled\n");
1335
1336 return -1;
1337 }
1338
1339 t->priv = (void *)host_timer;
1340
1341 return 0;
1342}
1343
1344static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1345{
1346 timer_t host_timer = (timer_t)t->priv;
1347
1348 timer_delete(host_timer);
1349}
1350
1351static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1352{
1353 timer_t host_timer = (timer_t)t->priv;
1354 struct itimerspec timeout;
1355 int64_t nearest_delta_us = INT64_MAX;
1356 int64_t current_us;
1357
1358 if (!active_timers[QEMU_TIMER_REALTIME] &&
1359 !active_timers[QEMU_TIMER_VIRTUAL])
1360 return;
1361
1362 nearest_delta_us = qemu_next_deadline();
1363
1364 /* check whether a timer is already running */
1365 if (timer_gettime(host_timer, &timeout)) {
1366 perror("gettime");
1367 fprintf(stderr, "Internal timer error: aborting\n");
1368 exit(1);
1369 }
1370 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1371 if (current_us && current_us <= nearest_delta_us)
1372 return;
1373
1374 timeout.it_interval.tv_sec = 0;
1375 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1376 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1377 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1378 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1379 perror("settime");
1380 fprintf(stderr, "Internal timer error: aborting\n");
1381 exit(1);
1382 }
1383}
1384
ths70744b32007-08-26 17:31:30 +00001385#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001386
thsc8994012007-08-19 21:56:03 +00001387static int unix_start_timer(struct qemu_alarm_timer *t)
1388{
1389 struct sigaction act;
1390 struct itimerval itv;
1391 int err;
1392
1393 /* timer signal */
1394 sigfillset(&act.sa_mask);
1395 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001396 act.sa_handler = host_alarm_handler;
1397
1398 sigaction(SIGALRM, &act, NULL);
1399
1400 itv.it_interval.tv_sec = 0;
1401 /* for i386 kernel 2.6 to get 1 ms */
1402 itv.it_interval.tv_usec = 999;
1403 itv.it_value.tv_sec = 0;
1404 itv.it_value.tv_usec = 10 * 1000;
1405
1406 err = setitimer(ITIMER_REAL, &itv, NULL);
1407 if (err)
1408 return -1;
1409
1410 return 0;
1411}
1412
1413static void unix_stop_timer(struct qemu_alarm_timer *t)
1414{
1415 struct itimerval itv;
1416
1417 memset(&itv, 0, sizeof(itv));
1418 setitimer(ITIMER_REAL, &itv, NULL);
1419}
1420
bellard829309c2004-05-20 13:20:12 +00001421#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001422
thsc8994012007-08-19 21:56:03 +00001423#ifdef _WIN32
1424
1425static int win32_start_timer(struct qemu_alarm_timer *t)
1426{
1427 TIMECAPS tc;
1428 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001429 UINT flags;
thsc8994012007-08-19 21:56:03 +00001430
1431 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1432 if (!data->host_alarm) {
1433 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001434 return -1;
thsc8994012007-08-19 21:56:03 +00001435 }
1436
1437 memset(&tc, 0, sizeof(tc));
1438 timeGetDevCaps(&tc, sizeof(tc));
1439
1440 if (data->period < tc.wPeriodMin)
1441 data->period = tc.wPeriodMin;
1442
1443 timeBeginPeriod(data->period);
1444
thsefe75412007-08-24 01:36:32 +00001445 flags = TIME_CALLBACK_FUNCTION;
1446 if (alarm_has_dynticks(t))
1447 flags |= TIME_ONESHOT;
1448 else
1449 flags |= TIME_PERIODIC;
1450
thsc8994012007-08-19 21:56:03 +00001451 data->timerId = timeSetEvent(1, // interval (ms)
1452 data->period, // resolution
1453 host_alarm_handler, // function
1454 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001455 flags);
thsc8994012007-08-19 21:56:03 +00001456
1457 if (!data->timerId) {
1458 perror("Failed to initialize win32 alarm timer");
1459
1460 timeEndPeriod(data->period);
1461 CloseHandle(data->host_alarm);
1462 return -1;
1463 }
1464
1465 qemu_add_wait_object(data->host_alarm, NULL, NULL);
1466
1467 return 0;
1468}
1469
1470static void win32_stop_timer(struct qemu_alarm_timer *t)
1471{
1472 struct qemu_alarm_win32 *data = t->priv;
1473
1474 timeKillEvent(data->timerId);
1475 timeEndPeriod(data->period);
1476
1477 CloseHandle(data->host_alarm);
1478}
1479
thsefe75412007-08-24 01:36:32 +00001480static void win32_rearm_timer(struct qemu_alarm_timer *t)
1481{
1482 struct qemu_alarm_win32 *data = t->priv;
1483 uint64_t nearest_delta_us;
1484
1485 if (!active_timers[QEMU_TIMER_REALTIME] &&
1486 !active_timers[QEMU_TIMER_VIRTUAL])
1487 return;
1488
1489 nearest_delta_us = qemu_next_deadline();
1490 nearest_delta_us /= 1000;
1491
1492 timeKillEvent(data->timerId);
1493
1494 data->timerId = timeSetEvent(1,
1495 data->period,
1496 host_alarm_handler,
1497 (DWORD)t,
1498 TIME_ONESHOT | TIME_PERIODIC);
1499
1500 if (!data->timerId) {
1501 perror("Failed to re-arm win32 alarm timer");
1502
1503 timeEndPeriod(data->period);
1504 CloseHandle(data->host_alarm);
1505 exit(1);
1506 }
1507}
1508
thsc8994012007-08-19 21:56:03 +00001509#endif /* _WIN32 */
1510
bellard1dce7c32006-07-13 23:20:22 +00001511static void init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001512{
thsc8994012007-08-19 21:56:03 +00001513 struct qemu_alarm_timer *t;
1514 int i, err = -1;
bellard06d9f2f2006-05-01 13:23:04 +00001515
thsc8994012007-08-19 21:56:03 +00001516 for (i = 0; alarm_timers[i].name; i++) {
1517 t = &alarm_timers[i];
1518
thsc8994012007-08-19 21:56:03 +00001519 err = t->start(t);
1520 if (!err)
1521 break;
bellard67b915a2004-03-31 23:37:16 +00001522 }
bellardfd872592004-05-12 19:11:15 +00001523
thsc8994012007-08-19 21:56:03 +00001524 if (err) {
1525 fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1526 fprintf(stderr, "Terminating\n");
1527 exit(1);
bellard67b915a2004-03-31 23:37:16 +00001528 }
thsc8994012007-08-19 21:56:03 +00001529
1530 alarm_timer = t;
bellard8a7ddc32004-03-31 19:00:16 +00001531}
1532
bellard40c3bac2004-04-04 12:56:28 +00001533void quit_timers(void)
1534{
thsc8994012007-08-19 21:56:03 +00001535 alarm_timer->stop(alarm_timer);
1536 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001537}
1538
bellardc4b1fcc2004-03-14 21:44:30 +00001539/***********************************************************/
bellard82c643f2004-07-14 17:28:13 +00001540/* character device */
bellardc45886d2004-01-05 00:02:06 +00001541
pbrooke5b0bc42007-01-27 23:46:43 +00001542static void qemu_chr_event(CharDriverState *s, int event)
1543{
1544 if (!s->chr_event)
1545 return;
1546 s->chr_event(s->handler_opaque, event);
1547}
1548
ths86e94de2007-01-05 22:01:59 +00001549static void qemu_chr_reset_bh(void *opaque)
1550{
1551 CharDriverState *s = opaque;
pbrooke5b0bc42007-01-27 23:46:43 +00001552 qemu_chr_event(s, CHR_EVENT_RESET);
ths86e94de2007-01-05 22:01:59 +00001553 qemu_bh_delete(s->bh);
1554 s->bh = NULL;
1555}
1556
1557void qemu_chr_reset(CharDriverState *s)
1558{
1559 if (s->bh == NULL) {
1560 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1561 qemu_bh_schedule(s->bh);
1562 }
1563}
1564
bellard82c643f2004-07-14 17:28:13 +00001565int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
bellard67b915a2004-03-31 23:37:16 +00001566{
bellard82c643f2004-07-14 17:28:13 +00001567 return s->chr_write(s, buf, len);
bellard67b915a2004-03-31 23:37:16 +00001568}
1569
bellarde57a8c02005-11-10 23:58:52 +00001570int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00001571{
bellarde57a8c02005-11-10 23:58:52 +00001572 if (!s->chr_ioctl)
1573 return -ENOTSUP;
1574 return s->chr_ioctl(s, cmd, arg);
bellardf8d179e2005-11-08 22:30:36 +00001575}
1576
pbrooke5b0bc42007-01-27 23:46:43 +00001577int qemu_chr_can_read(CharDriverState *s)
1578{
1579 if (!s->chr_can_read)
1580 return 0;
1581 return s->chr_can_read(s->handler_opaque);
1582}
1583
1584void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1585{
1586 s->chr_read(s->handler_opaque, buf, len);
1587}
1588
1589
bellard82c643f2004-07-14 17:28:13 +00001590void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
bellard0824d6f2003-06-24 13:42:40 +00001591{
bellard82c643f2004-07-14 17:28:13 +00001592 char buf[4096];
1593 va_list ap;
1594 va_start(ap, fmt);
1595 vsnprintf(buf, sizeof(buf), fmt, ap);
1596 qemu_chr_write(s, buf, strlen(buf));
1597 va_end(ap);
1598}
1599
bellard5905b2e2004-08-01 21:53:26 +00001600void qemu_chr_send_event(CharDriverState *s, int event)
1601{
1602 if (s->chr_send_event)
1603 s->chr_send_event(s, event);
1604}
1605
ths5fafdf22007-09-16 21:08:06 +00001606void qemu_chr_add_handlers(CharDriverState *s,
1607 IOCanRWHandler *fd_can_read,
pbrooke5b0bc42007-01-27 23:46:43 +00001608 IOReadHandler *fd_read,
1609 IOEventHandler *fd_event,
1610 void *opaque)
bellard82c643f2004-07-14 17:28:13 +00001611{
pbrooke5b0bc42007-01-27 23:46:43 +00001612 s->chr_can_read = fd_can_read;
1613 s->chr_read = fd_read;
1614 s->chr_event = fd_event;
1615 s->handler_opaque = opaque;
1616 if (s->chr_update_read_handler)
1617 s->chr_update_read_handler(s);
bellard82c643f2004-07-14 17:28:13 +00001618}
ths3b46e622007-09-17 08:09:54 +00001619
bellard82c643f2004-07-14 17:28:13 +00001620static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1621{
1622 return len;
1623}
1624
ths52f61fd2006-12-22 21:20:52 +00001625static CharDriverState *qemu_chr_open_null(void)
bellard82c643f2004-07-14 17:28:13 +00001626{
1627 CharDriverState *chr;
1628
1629 chr = qemu_mallocz(sizeof(CharDriverState));
1630 if (!chr)
1631 return NULL;
1632 chr->chr_write = null_chr_write;
bellard82c643f2004-07-14 17:28:13 +00001633 return chr;
1634}
1635
ths20d8a3e2007-02-18 17:04:49 +00001636/* MUX driver for serial I/O splitting */
1637static int term_timestamps;
1638static int64_t term_timestamps_start;
ths9c1de612007-02-21 17:25:30 +00001639#define MAX_MUX 4
ths20d8a3e2007-02-18 17:04:49 +00001640typedef struct {
1641 IOCanRWHandler *chr_can_read[MAX_MUX];
1642 IOReadHandler *chr_read[MAX_MUX];
1643 IOEventHandler *chr_event[MAX_MUX];
1644 void *ext_opaque[MAX_MUX];
1645 CharDriverState *drv;
1646 int mux_cnt;
1647 int term_got_escape;
1648 int max_size;
1649} MuxDriver;
1650
1651
1652static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1653{
1654 MuxDriver *d = chr->opaque;
1655 int ret;
1656 if (!term_timestamps) {
1657 ret = d->drv->chr_write(d->drv, buf, len);
1658 } else {
1659 int i;
1660
1661 ret = 0;
1662 for(i = 0; i < len; i++) {
1663 ret += d->drv->chr_write(d->drv, buf+i, 1);
1664 if (buf[i] == '\n') {
1665 char buf1[64];
1666 int64_t ti;
1667 int secs;
1668
1669 ti = get_clock();
1670 if (term_timestamps_start == -1)
1671 term_timestamps_start = ti;
1672 ti -= term_timestamps_start;
1673 secs = ti / 1000000000;
1674 snprintf(buf1, sizeof(buf1),
1675 "[%02d:%02d:%02d.%03d] ",
1676 secs / 3600,
1677 (secs / 60) % 60,
1678 secs % 60,
1679 (int)((ti / 1000000) % 1000));
1680 d->drv->chr_write(d->drv, buf1, strlen(buf1));
1681 }
1682 }
1683 }
1684 return ret;
1685}
1686
1687static char *mux_help[] = {
1688 "% h print this help\n\r",
1689 "% x exit emulator\n\r",
1690 "% s save disk data back to file (if -snapshot)\n\r",
1691 "% t toggle console timestamps\n\r"
1692 "% b send break (magic sysrq)\n\r",
1693 "% c switch between console and monitor\n\r",
1694 "% % sends %\n\r",
1695 NULL
1696};
1697
1698static int term_escape_char = 0x01; /* ctrl-a is used for escape */
1699static void mux_print_help(CharDriverState *chr)
1700{
1701 int i, j;
1702 char ebuf[15] = "Escape-Char";
1703 char cbuf[50] = "\n\r";
1704
1705 if (term_escape_char > 0 && term_escape_char < 26) {
1706 sprintf(cbuf,"\n\r");
1707 sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a');
1708 } else {
1709 sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char);
1710 }
1711 chr->chr_write(chr, cbuf, strlen(cbuf));
1712 for (i = 0; mux_help[i] != NULL; i++) {
1713 for (j=0; mux_help[i][j] != '\0'; j++) {
1714 if (mux_help[i][j] == '%')
1715 chr->chr_write(chr, ebuf, strlen(ebuf));
1716 else
1717 chr->chr_write(chr, &mux_help[i][j], 1);
1718 }
1719 }
1720}
1721
1722static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1723{
1724 if (d->term_got_escape) {
1725 d->term_got_escape = 0;
1726 if (ch == term_escape_char)
1727 goto send_char;
1728 switch(ch) {
1729 case '?':
1730 case 'h':
1731 mux_print_help(chr);
1732 break;
1733 case 'x':
1734 {
1735 char *term = "QEMU: Terminated\n\r";
1736 chr->chr_write(chr,term,strlen(term));
1737 exit(0);
1738 break;
1739 }
1740 case 's':
1741 {
1742 int i;
1743 for (i = 0; i < MAX_DISKS; i++) {
1744 if (bs_table[i])
1745 bdrv_commit(bs_table[i]);
1746 }
balrog2dc7b602007-05-24 18:53:22 +00001747 if (mtd_bdrv)
1748 bdrv_commit(mtd_bdrv);
ths20d8a3e2007-02-18 17:04:49 +00001749 }
1750 break;
1751 case 'b':
balrog36ddb832007-05-18 17:46:59 +00001752 qemu_chr_event(chr, CHR_EVENT_BREAK);
ths20d8a3e2007-02-18 17:04:49 +00001753 break;
1754 case 'c':
1755 /* Switch to the next registered device */
1756 chr->focus++;
1757 if (chr->focus >= d->mux_cnt)
1758 chr->focus = 0;
1759 break;
1760 case 't':
1761 term_timestamps = !term_timestamps;
1762 term_timestamps_start = -1;
1763 break;
1764 }
1765 } else if (ch == term_escape_char) {
1766 d->term_got_escape = 1;
1767 } else {
1768 send_char:
1769 return 1;
1770 }
1771 return 0;
1772}
1773
1774static int mux_chr_can_read(void *opaque)
1775{
1776 CharDriverState *chr = opaque;
1777 MuxDriver *d = chr->opaque;
1778 if (d->chr_can_read[chr->focus])
1779 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
1780 return 0;
1781}
1782
1783static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
1784{
1785 CharDriverState *chr = opaque;
1786 MuxDriver *d = chr->opaque;
1787 int i;
1788 for(i = 0; i < size; i++)
1789 if (mux_proc_byte(chr, d, buf[i]))
1790 d->chr_read[chr->focus](d->ext_opaque[chr->focus], &buf[i], 1);
1791}
1792
1793static void mux_chr_event(void *opaque, int event)
1794{
1795 CharDriverState *chr = opaque;
1796 MuxDriver *d = chr->opaque;
1797 int i;
1798
1799 /* Send the event to all registered listeners */
1800 for (i = 0; i < d->mux_cnt; i++)
1801 if (d->chr_event[i])
1802 d->chr_event[i](d->ext_opaque[i], event);
1803}
1804
1805static void mux_chr_update_read_handler(CharDriverState *chr)
1806{
1807 MuxDriver *d = chr->opaque;
1808
1809 if (d->mux_cnt >= MAX_MUX) {
1810 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
1811 return;
1812 }
1813 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
1814 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
1815 d->chr_read[d->mux_cnt] = chr->chr_read;
1816 d->chr_event[d->mux_cnt] = chr->chr_event;
1817 /* Fix up the real driver with mux routines */
1818 if (d->mux_cnt == 0) {
1819 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
1820 mux_chr_event, chr);
1821 }
1822 chr->focus = d->mux_cnt;
1823 d->mux_cnt++;
1824}
1825
1826CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
1827{
1828 CharDriverState *chr;
1829 MuxDriver *d;
1830
1831 chr = qemu_mallocz(sizeof(CharDriverState));
1832 if (!chr)
1833 return NULL;
1834 d = qemu_mallocz(sizeof(MuxDriver));
1835 if (!d) {
1836 free(chr);
1837 return NULL;
1838 }
1839
1840 chr->opaque = d;
1841 d->drv = drv;
1842 chr->focus = -1;
1843 chr->chr_write = mux_chr_write;
1844 chr->chr_update_read_handler = mux_chr_update_read_handler;
1845 return chr;
1846}
1847
1848
bellardfd1dff42006-02-01 21:29:26 +00001849#ifdef _WIN32
bellard82c643f2004-07-14 17:28:13 +00001850
bellardfd1dff42006-02-01 21:29:26 +00001851static void socket_cleanup(void)
1852{
1853 WSACleanup();
1854}
bellard82c643f2004-07-14 17:28:13 +00001855
bellardfd1dff42006-02-01 21:29:26 +00001856static int socket_init(void)
1857{
1858 WSADATA Data;
1859 int ret, err;
1860
1861 ret = WSAStartup(MAKEWORD(2,2), &Data);
1862 if (ret != 0) {
1863 err = WSAGetLastError();
1864 fprintf(stderr, "WSAStartup: %d\n", err);
1865 return -1;
1866 }
1867 atexit(socket_cleanup);
1868 return 0;
1869}
1870
1871static int send_all(int fd, const uint8_t *buf, int len1)
1872{
1873 int ret, len;
ths3b46e622007-09-17 08:09:54 +00001874
bellardfd1dff42006-02-01 21:29:26 +00001875 len = len1;
1876 while (len > 0) {
1877 ret = send(fd, buf, len, 0);
1878 if (ret < 0) {
1879 int errno;
1880 errno = WSAGetLastError();
1881 if (errno != WSAEWOULDBLOCK) {
1882 return -1;
1883 }
1884 } else if (ret == 0) {
1885 break;
1886 } else {
1887 buf += ret;
1888 len -= ret;
1889 }
1890 }
1891 return len1 - len;
1892}
1893
1894void socket_set_nonblock(int fd)
1895{
1896 unsigned long opt = 1;
1897 ioctlsocket(fd, FIONBIO, &opt);
1898}
1899
1900#else
1901
bellard1d969052004-09-18 19:34:39 +00001902static int unix_write(int fd, const uint8_t *buf, int len1)
1903{
1904 int ret, len;
1905
1906 len = len1;
1907 while (len > 0) {
1908 ret = write(fd, buf, len);
1909 if (ret < 0) {
1910 if (errno != EINTR && errno != EAGAIN)
1911 return -1;
1912 } else if (ret == 0) {
1913 break;
1914 } else {
1915 buf += ret;
1916 len -= ret;
1917 }
1918 }
1919 return len1 - len;
1920}
1921
bellardfd1dff42006-02-01 21:29:26 +00001922static inline int send_all(int fd, const uint8_t *buf, int len1)
1923{
1924 return unix_write(fd, buf, len1);
1925}
1926
1927void socket_set_nonblock(int fd)
1928{
1929 fcntl(fd, F_SETFL, O_NONBLOCK);
1930}
1931#endif /* !_WIN32 */
1932
1933#ifndef _WIN32
1934
1935typedef struct {
1936 int fd_in, fd_out;
bellardfd1dff42006-02-01 21:29:26 +00001937 int max_size;
1938} FDCharDriver;
1939
ths20d8a3e2007-02-18 17:04:49 +00001940#define STDIO_MAX_CLIENTS 1
1941static int stdio_nb_clients = 0;
bellardfd1dff42006-02-01 21:29:26 +00001942
bellard82c643f2004-07-14 17:28:13 +00001943static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1944{
1945 FDCharDriver *s = chr->opaque;
bellard1d969052004-09-18 19:34:39 +00001946 return unix_write(s->fd_out, buf, len);
bellard82c643f2004-07-14 17:28:13 +00001947}
1948
bellard7c9d8e02005-11-15 22:16:05 +00001949static int fd_chr_read_poll(void *opaque)
1950{
1951 CharDriverState *chr = opaque;
1952 FDCharDriver *s = chr->opaque;
1953
pbrooke5b0bc42007-01-27 23:46:43 +00001954 s->max_size = qemu_chr_can_read(chr);
bellard7c9d8e02005-11-15 22:16:05 +00001955 return s->max_size;
1956}
1957
1958static void fd_chr_read(void *opaque)
1959{
1960 CharDriverState *chr = opaque;
1961 FDCharDriver *s = chr->opaque;
1962 int size, len;
1963 uint8_t buf[1024];
ths3b46e622007-09-17 08:09:54 +00001964
bellard7c9d8e02005-11-15 22:16:05 +00001965 len = sizeof(buf);
1966 if (len > s->max_size)
1967 len = s->max_size;
1968 if (len == 0)
1969 return;
1970 size = read(s->fd_in, buf, len);
pbrook188157f2006-11-01 01:44:16 +00001971 if (size == 0) {
1972 /* FD has been closed. Remove it from the active list. */
1973 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
1974 return;
1975 }
bellard7c9d8e02005-11-15 22:16:05 +00001976 if (size > 0) {
pbrooke5b0bc42007-01-27 23:46:43 +00001977 qemu_chr_read(chr, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00001978 }
1979}
1980
pbrooke5b0bc42007-01-27 23:46:43 +00001981static void fd_chr_update_read_handler(CharDriverState *chr)
bellard82c643f2004-07-14 17:28:13 +00001982{
1983 FDCharDriver *s = chr->opaque;
1984
bellardf8d179e2005-11-08 22:30:36 +00001985 if (s->fd_in >= 0) {
1986 if (nographic && s->fd_in == 0) {
bellardf8d179e2005-11-08 22:30:36 +00001987 } else {
ths5fafdf22007-09-16 21:08:06 +00001988 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
bellard7c9d8e02005-11-15 22:16:05 +00001989 fd_chr_read, NULL, chr);
bellardf8d179e2005-11-08 22:30:36 +00001990 }
bellard0824d6f2003-06-24 13:42:40 +00001991 }
1992}
1993
bellard82c643f2004-07-14 17:28:13 +00001994/* open a character device to a unix fd */
ths52f61fd2006-12-22 21:20:52 +00001995static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
bellard82c643f2004-07-14 17:28:13 +00001996{
1997 CharDriverState *chr;
1998 FDCharDriver *s;
1999
2000 chr = qemu_mallocz(sizeof(CharDriverState));
2001 if (!chr)
2002 return NULL;
2003 s = qemu_mallocz(sizeof(FDCharDriver));
2004 if (!s) {
2005 free(chr);
2006 return NULL;
2007 }
2008 s->fd_in = fd_in;
2009 s->fd_out = fd_out;
2010 chr->opaque = s;
2011 chr->chr_write = fd_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002012 chr->chr_update_read_handler = fd_chr_update_read_handler;
ths86e94de2007-01-05 22:01:59 +00002013
2014 qemu_chr_reset(chr);
2015
bellard82c643f2004-07-14 17:28:13 +00002016 return chr;
2017}
2018
ths52f61fd2006-12-22 21:20:52 +00002019static CharDriverState *qemu_chr_open_file_out(const char *file_out)
bellardf8d179e2005-11-08 22:30:36 +00002020{
2021 int fd_out;
2022
balrogaeb30be2007-07-02 15:03:13 +00002023 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
bellardf8d179e2005-11-08 22:30:36 +00002024 if (fd_out < 0)
2025 return NULL;
2026 return qemu_chr_open_fd(-1, fd_out);
2027}
2028
ths52f61fd2006-12-22 21:20:52 +00002029static CharDriverState *qemu_chr_open_pipe(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002030{
thsc26c1c42006-12-22 19:25:31 +00002031 int fd_in, fd_out;
2032 char filename_in[256], filename_out[256];
bellardf8d179e2005-11-08 22:30:36 +00002033
thsc26c1c42006-12-22 19:25:31 +00002034 snprintf(filename_in, 256, "%s.in", filename);
2035 snprintf(filename_out, 256, "%s.out", filename);
balrogaeb30be2007-07-02 15:03:13 +00002036 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2037 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002038 if (fd_in < 0 || fd_out < 0) {
2039 if (fd_in >= 0)
2040 close(fd_in);
2041 if (fd_out >= 0)
2042 close(fd_out);
balrogaeb30be2007-07-02 15:03:13 +00002043 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002044 if (fd_in < 0)
2045 return NULL;
2046 }
2047 return qemu_chr_open_fd(fd_in, fd_out);
bellardf8d179e2005-11-08 22:30:36 +00002048}
2049
2050
bellard82c643f2004-07-14 17:28:13 +00002051/* for STDIO, we handle the case where several clients use it
2052 (nographic mode) */
2053
bellardaa0bc6b2005-09-03 15:28:58 +00002054#define TERM_FIFO_MAX_SIZE 1
2055
bellardaa0bc6b2005-09-03 15:28:58 +00002056static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
bellard1dce7c32006-07-13 23:20:22 +00002057static int term_fifo_size;
bellard82c643f2004-07-14 17:28:13 +00002058
bellard7c9d8e02005-11-15 22:16:05 +00002059static int stdio_read_poll(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002060{
ths20d8a3e2007-02-18 17:04:49 +00002061 CharDriverState *chr = opaque;
bellardaa0bc6b2005-09-03 15:28:58 +00002062
ths20d8a3e2007-02-18 17:04:49 +00002063 /* try to flush the queue if needed */
2064 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2065 qemu_chr_read(chr, term_fifo, 1);
2066 term_fifo_size = 0;
bellardaa0bc6b2005-09-03 15:28:58 +00002067 }
ths20d8a3e2007-02-18 17:04:49 +00002068 /* see if we can absorb more chars */
2069 if (term_fifo_size == 0)
2070 return 1;
2071 else
2072 return 0;
bellard82c643f2004-07-14 17:28:13 +00002073}
2074
bellard7c9d8e02005-11-15 22:16:05 +00002075static void stdio_read(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002076{
bellard7c9d8e02005-11-15 22:16:05 +00002077 int size;
2078 uint8_t buf[1];
ths20d8a3e2007-02-18 17:04:49 +00002079 CharDriverState *chr = opaque;
2080
bellard7c9d8e02005-11-15 22:16:05 +00002081 size = read(0, buf, 1);
pbrook519945d2006-09-10 14:39:54 +00002082 if (size == 0) {
2083 /* stdin has been closed. Remove it from the active list. */
2084 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2085 return;
2086 }
ths20d8a3e2007-02-18 17:04:49 +00002087 if (size > 0) {
2088 if (qemu_chr_can_read(chr) > 0) {
2089 qemu_chr_read(chr, buf, 1);
2090 } else if (term_fifo_size == 0) {
2091 term_fifo[term_fifo_size++] = buf[0];
bellard1dce7c32006-07-13 23:20:22 +00002092 }
bellard1dce7c32006-07-13 23:20:22 +00002093 }
2094}
2095
bellard8d11df92004-08-24 21:13:40 +00002096/* init terminal so that we can grab keys */
2097static struct termios oldtty;
2098static int old_fd0_flags;
2099
2100static void term_exit(void)
2101{
2102 tcsetattr (0, TCSANOW, &oldtty);
2103 fcntl(0, F_SETFL, old_fd0_flags);
2104}
2105
2106static void term_init(void)
2107{
2108 struct termios tty;
2109
2110 tcgetattr (0, &tty);
2111 oldtty = tty;
2112 old_fd0_flags = fcntl(0, F_GETFL);
2113
2114 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2115 |INLCR|IGNCR|ICRNL|IXON);
2116 tty.c_oflag |= OPOST;
2117 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2118 /* if graphical mode, we allow Ctrl-C handling */
2119 if (nographic)
2120 tty.c_lflag &= ~ISIG;
2121 tty.c_cflag &= ~(CSIZE|PARENB);
2122 tty.c_cflag |= CS8;
2123 tty.c_cc[VMIN] = 1;
2124 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +00002125
bellard8d11df92004-08-24 21:13:40 +00002126 tcsetattr (0, TCSANOW, &tty);
2127
2128 atexit(term_exit);
2129
2130 fcntl(0, F_SETFL, O_NONBLOCK);
2131}
2132
ths52f61fd2006-12-22 21:20:52 +00002133static CharDriverState *qemu_chr_open_stdio(void)
bellard82c643f2004-07-14 17:28:13 +00002134{
2135 CharDriverState *chr;
2136
ths20d8a3e2007-02-18 17:04:49 +00002137 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2138 return NULL;
2139 chr = qemu_chr_open_fd(0, 1);
2140 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2141 stdio_nb_clients++;
2142 term_init();
2143
bellard82c643f2004-07-14 17:28:13 +00002144 return chr;
2145}
2146
thsaec62502007-06-25 11:48:07 +00002147#if defined(__linux__) || defined(__sun__)
ths52f61fd2006-12-22 21:20:52 +00002148static CharDriverState *qemu_chr_open_pty(void)
bellard82c643f2004-07-14 17:28:13 +00002149{
bellard91fc2112005-12-18 19:09:37 +00002150 struct termios tty;
bellard82c643f2004-07-14 17:28:13 +00002151 char slave_name[1024];
2152 int master_fd, slave_fd;
ths3b46e622007-09-17 08:09:54 +00002153
thsaec62502007-06-25 11:48:07 +00002154#if defined(__linux__)
bellard82c643f2004-07-14 17:28:13 +00002155 /* Not satisfying */
2156 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
2157 return NULL;
2158 }
thsaec62502007-06-25 11:48:07 +00002159#endif
ths3b46e622007-09-17 08:09:54 +00002160
bellard91fc2112005-12-18 19:09:37 +00002161 /* Disabling local echo and line-buffered output */
2162 tcgetattr (master_fd, &tty);
2163 tty.c_lflag &= ~(ECHO|ICANON|ISIG);
2164 tty.c_cc[VMIN] = 1;
2165 tty.c_cc[VTIME] = 0;
2166 tcsetattr (master_fd, TCSAFLUSH, &tty);
2167
bellard82c643f2004-07-14 17:28:13 +00002168 fprintf(stderr, "char device redirected to %s\n", slave_name);
2169 return qemu_chr_open_fd(master_fd, master_fd);
2170}
bellardf8d179e2005-11-08 22:30:36 +00002171
ths5fafdf22007-09-16 21:08:06 +00002172static void tty_serial_init(int fd, int speed,
bellardf8d179e2005-11-08 22:30:36 +00002173 int parity, int data_bits, int stop_bits)
2174{
2175 struct termios tty;
2176 speed_t spd;
2177
bellarde57a8c02005-11-10 23:58:52 +00002178#if 0
ths5fafdf22007-09-16 21:08:06 +00002179 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
bellarde57a8c02005-11-10 23:58:52 +00002180 speed, parity, data_bits, stop_bits);
2181#endif
2182 tcgetattr (fd, &tty);
bellardf8d179e2005-11-08 22:30:36 +00002183
2184 switch(speed) {
2185 case 50:
2186 spd = B50;
2187 break;
2188 case 75:
2189 spd = B75;
2190 break;
2191 case 300:
2192 spd = B300;
2193 break;
2194 case 600:
2195 spd = B600;
2196 break;
2197 case 1200:
2198 spd = B1200;
2199 break;
2200 case 2400:
2201 spd = B2400;
2202 break;
2203 case 4800:
2204 spd = B4800;
2205 break;
2206 case 9600:
2207 spd = B9600;
2208 break;
2209 case 19200:
2210 spd = B19200;
2211 break;
2212 case 38400:
2213 spd = B38400;
2214 break;
2215 case 57600:
2216 spd = B57600;
2217 break;
2218 default:
2219 case 115200:
2220 spd = B115200;
2221 break;
2222 }
2223
2224 cfsetispeed(&tty, spd);
2225 cfsetospeed(&tty, spd);
2226
2227 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2228 |INLCR|IGNCR|ICRNL|IXON);
2229 tty.c_oflag |= OPOST;
2230 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
bellard094eed62006-09-09 11:10:18 +00002231 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
bellardf8d179e2005-11-08 22:30:36 +00002232 switch(data_bits) {
2233 default:
2234 case 8:
2235 tty.c_cflag |= CS8;
2236 break;
2237 case 7:
2238 tty.c_cflag |= CS7;
2239 break;
2240 case 6:
2241 tty.c_cflag |= CS6;
2242 break;
2243 case 5:
2244 tty.c_cflag |= CS5;
2245 break;
2246 }
2247 switch(parity) {
2248 default:
2249 case 'N':
2250 break;
2251 case 'E':
2252 tty.c_cflag |= PARENB;
2253 break;
2254 case 'O':
2255 tty.c_cflag |= PARENB | PARODD;
2256 break;
2257 }
bellard094eed62006-09-09 11:10:18 +00002258 if (stop_bits == 2)
2259 tty.c_cflag |= CSTOPB;
ths3b46e622007-09-17 08:09:54 +00002260
bellardf8d179e2005-11-08 22:30:36 +00002261 tcsetattr (fd, TCSANOW, &tty);
2262}
2263
bellarde57a8c02005-11-10 23:58:52 +00002264static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00002265{
2266 FDCharDriver *s = chr->opaque;
ths3b46e622007-09-17 08:09:54 +00002267
bellarde57a8c02005-11-10 23:58:52 +00002268 switch(cmd) {
2269 case CHR_IOCTL_SERIAL_SET_PARAMS:
2270 {
2271 QEMUSerialSetParams *ssp = arg;
ths5fafdf22007-09-16 21:08:06 +00002272 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
bellarde57a8c02005-11-10 23:58:52 +00002273 ssp->data_bits, ssp->stop_bits);
2274 }
2275 break;
2276 case CHR_IOCTL_SERIAL_SET_BREAK:
2277 {
2278 int enable = *(int *)arg;
2279 if (enable)
2280 tcsendbreak(s->fd_in, 1);
2281 }
2282 break;
2283 default:
2284 return -ENOTSUP;
2285 }
2286 return 0;
bellardf8d179e2005-11-08 22:30:36 +00002287}
2288
ths52f61fd2006-12-22 21:20:52 +00002289static CharDriverState *qemu_chr_open_tty(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002290{
2291 CharDriverState *chr;
2292 int fd;
2293
balrogaeb30be2007-07-02 15:03:13 +00002294 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
bellardf8d179e2005-11-08 22:30:36 +00002295 fcntl(fd, F_SETFL, O_NONBLOCK);
2296 tty_serial_init(fd, 115200, 'N', 8, 1);
2297 chr = qemu_chr_open_fd(fd, fd);
balrogaeb30be2007-07-02 15:03:13 +00002298 if (!chr) {
2299 close(fd);
bellardf8d179e2005-11-08 22:30:36 +00002300 return NULL;
balrogaeb30be2007-07-02 15:03:13 +00002301 }
bellarde57a8c02005-11-10 23:58:52 +00002302 chr->chr_ioctl = tty_serial_ioctl;
ths86e94de2007-01-05 22:01:59 +00002303 qemu_chr_reset(chr);
bellarde57a8c02005-11-10 23:58:52 +00002304 return chr;
2305}
thsaec62502007-06-25 11:48:07 +00002306#else /* ! __linux__ && ! __sun__ */
2307static CharDriverState *qemu_chr_open_pty(void)
2308{
2309 return NULL;
2310}
2311#endif /* __linux__ || __sun__ */
bellarde57a8c02005-11-10 23:58:52 +00002312
thsaec62502007-06-25 11:48:07 +00002313#if defined(__linux__)
ths5867c882007-02-17 23:44:43 +00002314typedef struct {
2315 int fd;
2316 int mode;
2317} ParallelCharDriver;
2318
2319static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2320{
2321 if (s->mode != mode) {
2322 int m = mode;
2323 if (ioctl(s->fd, PPSETMODE, &m) < 0)
2324 return 0;
2325 s->mode = mode;
2326 }
2327 return 1;
2328}
2329
bellarde57a8c02005-11-10 23:58:52 +00002330static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2331{
ths5867c882007-02-17 23:44:43 +00002332 ParallelCharDriver *drv = chr->opaque;
2333 int fd = drv->fd;
bellarde57a8c02005-11-10 23:58:52 +00002334 uint8_t b;
2335
2336 switch(cmd) {
2337 case CHR_IOCTL_PP_READ_DATA:
2338 if (ioctl(fd, PPRDATA, &b) < 0)
2339 return -ENOTSUP;
2340 *(uint8_t *)arg = b;
2341 break;
2342 case CHR_IOCTL_PP_WRITE_DATA:
2343 b = *(uint8_t *)arg;
2344 if (ioctl(fd, PPWDATA, &b) < 0)
2345 return -ENOTSUP;
2346 break;
2347 case CHR_IOCTL_PP_READ_CONTROL:
2348 if (ioctl(fd, PPRCONTROL, &b) < 0)
2349 return -ENOTSUP;
ths5867c882007-02-17 23:44:43 +00002350 /* Linux gives only the lowest bits, and no way to know data
2351 direction! For better compatibility set the fixed upper
2352 bits. */
2353 *(uint8_t *)arg = b | 0xc0;
bellarde57a8c02005-11-10 23:58:52 +00002354 break;
2355 case CHR_IOCTL_PP_WRITE_CONTROL:
2356 b = *(uint8_t *)arg;
2357 if (ioctl(fd, PPWCONTROL, &b) < 0)
2358 return -ENOTSUP;
2359 break;
2360 case CHR_IOCTL_PP_READ_STATUS:
2361 if (ioctl(fd, PPRSTATUS, &b) < 0)
2362 return -ENOTSUP;
2363 *(uint8_t *)arg = b;
2364 break;
ths5867c882007-02-17 23:44:43 +00002365 case CHR_IOCTL_PP_EPP_READ_ADDR:
2366 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2367 struct ParallelIOArg *parg = arg;
2368 int n = read(fd, parg->buffer, parg->count);
2369 if (n != parg->count) {
2370 return -EIO;
2371 }
2372 }
2373 break;
2374 case CHR_IOCTL_PP_EPP_READ:
2375 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2376 struct ParallelIOArg *parg = arg;
2377 int n = read(fd, parg->buffer, parg->count);
2378 if (n != parg->count) {
2379 return -EIO;
2380 }
2381 }
2382 break;
2383 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2384 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2385 struct ParallelIOArg *parg = arg;
2386 int n = write(fd, parg->buffer, parg->count);
2387 if (n != parg->count) {
2388 return -EIO;
2389 }
2390 }
2391 break;
2392 case CHR_IOCTL_PP_EPP_WRITE:
2393 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2394 struct ParallelIOArg *parg = arg;
2395 int n = write(fd, parg->buffer, parg->count);
2396 if (n != parg->count) {
2397 return -EIO;
2398 }
2399 }
2400 break;
bellarde57a8c02005-11-10 23:58:52 +00002401 default:
2402 return -ENOTSUP;
2403 }
2404 return 0;
2405}
2406
ths5867c882007-02-17 23:44:43 +00002407static void pp_close(CharDriverState *chr)
2408{
2409 ParallelCharDriver *drv = chr->opaque;
2410 int fd = drv->fd;
2411
2412 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2413 ioctl(fd, PPRELEASE);
2414 close(fd);
2415 qemu_free(drv);
2416}
2417
ths52f61fd2006-12-22 21:20:52 +00002418static CharDriverState *qemu_chr_open_pp(const char *filename)
bellarde57a8c02005-11-10 23:58:52 +00002419{
2420 CharDriverState *chr;
ths5867c882007-02-17 23:44:43 +00002421 ParallelCharDriver *drv;
bellarde57a8c02005-11-10 23:58:52 +00002422 int fd;
2423
balrogaeb30be2007-07-02 15:03:13 +00002424 TFR(fd = open(filename, O_RDWR));
bellarde57a8c02005-11-10 23:58:52 +00002425 if (fd < 0)
2426 return NULL;
2427
2428 if (ioctl(fd, PPCLAIM) < 0) {
2429 close(fd);
2430 return NULL;
2431 }
2432
ths5867c882007-02-17 23:44:43 +00002433 drv = qemu_mallocz(sizeof(ParallelCharDriver));
2434 if (!drv) {
bellarde57a8c02005-11-10 23:58:52 +00002435 close(fd);
2436 return NULL;
2437 }
ths5867c882007-02-17 23:44:43 +00002438 drv->fd = fd;
2439 drv->mode = IEEE1284_MODE_COMPAT;
2440
2441 chr = qemu_mallocz(sizeof(CharDriverState));
2442 if (!chr) {
2443 qemu_free(drv);
2444 close(fd);
2445 return NULL;
2446 }
bellarde57a8c02005-11-10 23:58:52 +00002447 chr->chr_write = null_chr_write;
bellarde57a8c02005-11-10 23:58:52 +00002448 chr->chr_ioctl = pp_ioctl;
ths5867c882007-02-17 23:44:43 +00002449 chr->chr_close = pp_close;
2450 chr->opaque = drv;
ths86e94de2007-01-05 22:01:59 +00002451
2452 qemu_chr_reset(chr);
2453
bellardf8d179e2005-11-08 22:30:36 +00002454 return chr;
2455}
thsaec62502007-06-25 11:48:07 +00002456#endif /* __linux__ */
bellardf8d179e2005-11-08 22:30:36 +00002457
thsaec62502007-06-25 11:48:07 +00002458#else /* _WIN32 */
bellard67b915a2004-03-31 23:37:16 +00002459
bellardf3311102006-04-12 20:21:17 +00002460typedef struct {
bellardf3311102006-04-12 20:21:17 +00002461 int max_size;
2462 HANDLE hcom, hrecv, hsend;
2463 OVERLAPPED orecv, osend;
2464 BOOL fpipe;
2465 DWORD len;
2466} WinCharState;
2467
2468#define NSENDBUF 2048
2469#define NRECVBUF 2048
2470#define MAXCONNECT 1
2471#define NTIMEOUT 5000
2472
2473static int win_chr_poll(void *opaque);
2474static int win_chr_pipe_poll(void *opaque);
2475
ths087f4ae2007-02-10 21:50:42 +00002476static void win_chr_close(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002477{
ths087f4ae2007-02-10 21:50:42 +00002478 WinCharState *s = chr->opaque;
2479
bellardf3311102006-04-12 20:21:17 +00002480 if (s->hsend) {
2481 CloseHandle(s->hsend);
2482 s->hsend = NULL;
2483 }
2484 if (s->hrecv) {
2485 CloseHandle(s->hrecv);
2486 s->hrecv = NULL;
2487 }
2488 if (s->hcom) {
2489 CloseHandle(s->hcom);
2490 s->hcom = NULL;
2491 }
2492 if (s->fpipe)
ths087f4ae2007-02-10 21:50:42 +00002493 qemu_del_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002494 else
ths087f4ae2007-02-10 21:50:42 +00002495 qemu_del_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002496}
2497
ths087f4ae2007-02-10 21:50:42 +00002498static int win_chr_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002499{
2500 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002501 COMMCONFIG comcfg;
2502 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2503 COMSTAT comstat;
2504 DWORD size;
2505 DWORD err;
ths3b46e622007-09-17 08:09:54 +00002506
bellardf3311102006-04-12 20:21:17 +00002507 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2508 if (!s->hsend) {
2509 fprintf(stderr, "Failed CreateEvent\n");
2510 goto fail;
2511 }
2512 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2513 if (!s->hrecv) {
2514 fprintf(stderr, "Failed CreateEvent\n");
2515 goto fail;
2516 }
2517
2518 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2519 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2520 if (s->hcom == INVALID_HANDLE_VALUE) {
2521 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2522 s->hcom = NULL;
2523 goto fail;
2524 }
ths3b46e622007-09-17 08:09:54 +00002525
bellardf3311102006-04-12 20:21:17 +00002526 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2527 fprintf(stderr, "Failed SetupComm\n");
2528 goto fail;
2529 }
ths3b46e622007-09-17 08:09:54 +00002530
bellardf3311102006-04-12 20:21:17 +00002531 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2532 size = sizeof(COMMCONFIG);
2533 GetDefaultCommConfig(filename, &comcfg, &size);
2534 comcfg.dcb.DCBlength = sizeof(DCB);
2535 CommConfigDialog(filename, NULL, &comcfg);
2536
2537 if (!SetCommState(s->hcom, &comcfg.dcb)) {
2538 fprintf(stderr, "Failed SetCommState\n");
2539 goto fail;
2540 }
2541
2542 if (!SetCommMask(s->hcom, EV_ERR)) {
2543 fprintf(stderr, "Failed SetCommMask\n");
2544 goto fail;
2545 }
2546
2547 cto.ReadIntervalTimeout = MAXDWORD;
2548 if (!SetCommTimeouts(s->hcom, &cto)) {
2549 fprintf(stderr, "Failed SetCommTimeouts\n");
2550 goto fail;
2551 }
ths3b46e622007-09-17 08:09:54 +00002552
bellardf3311102006-04-12 20:21:17 +00002553 if (!ClearCommError(s->hcom, &err, &comstat)) {
2554 fprintf(stderr, "Failed ClearCommError\n");
2555 goto fail;
2556 }
ths087f4ae2007-02-10 21:50:42 +00002557 qemu_add_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002558 return 0;
2559
2560 fail:
ths087f4ae2007-02-10 21:50:42 +00002561 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002562 return -1;
2563}
2564
2565static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2566{
2567 WinCharState *s = chr->opaque;
2568 DWORD len, ret, size, err;
2569
2570 len = len1;
2571 ZeroMemory(&s->osend, sizeof(s->osend));
2572 s->osend.hEvent = s->hsend;
2573 while (len > 0) {
2574 if (s->hsend)
2575 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2576 else
2577 ret = WriteFile(s->hcom, buf, len, &size, NULL);
2578 if (!ret) {
2579 err = GetLastError();
2580 if (err == ERROR_IO_PENDING) {
2581 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2582 if (ret) {
2583 buf += size;
2584 len -= size;
2585 } else {
2586 break;
2587 }
2588 } else {
2589 break;
2590 }
2591 } else {
2592 buf += size;
2593 len -= size;
2594 }
2595 }
2596 return len1 - len;
2597}
2598
ths087f4ae2007-02-10 21:50:42 +00002599static int win_chr_read_poll(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002600{
ths087f4ae2007-02-10 21:50:42 +00002601 WinCharState *s = chr->opaque;
2602
2603 s->max_size = qemu_chr_can_read(chr);
bellardf3311102006-04-12 20:21:17 +00002604 return s->max_size;
2605}
pbrooke5b0bc42007-01-27 23:46:43 +00002606
ths087f4ae2007-02-10 21:50:42 +00002607static void win_chr_readfile(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002608{
ths087f4ae2007-02-10 21:50:42 +00002609 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002610 int ret, err;
2611 uint8_t buf[1024];
2612 DWORD size;
ths3b46e622007-09-17 08:09:54 +00002613
bellardf3311102006-04-12 20:21:17 +00002614 ZeroMemory(&s->orecv, sizeof(s->orecv));
2615 s->orecv.hEvent = s->hrecv;
2616 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2617 if (!ret) {
2618 err = GetLastError();
2619 if (err == ERROR_IO_PENDING) {
2620 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2621 }
2622 }
2623
2624 if (size > 0) {
ths087f4ae2007-02-10 21:50:42 +00002625 qemu_chr_read(chr, buf, size);
bellardf3311102006-04-12 20:21:17 +00002626 }
2627}
2628
ths087f4ae2007-02-10 21:50:42 +00002629static void win_chr_read(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002630{
ths087f4ae2007-02-10 21:50:42 +00002631 WinCharState *s = chr->opaque;
2632
bellardf3311102006-04-12 20:21:17 +00002633 if (s->len > s->max_size)
2634 s->len = s->max_size;
2635 if (s->len == 0)
2636 return;
ths3b46e622007-09-17 08:09:54 +00002637
ths087f4ae2007-02-10 21:50:42 +00002638 win_chr_readfile(chr);
bellardf3311102006-04-12 20:21:17 +00002639}
2640
2641static int win_chr_poll(void *opaque)
2642{
ths087f4ae2007-02-10 21:50:42 +00002643 CharDriverState *chr = opaque;
2644 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002645 COMSTAT status;
2646 DWORD comerr;
ths3b46e622007-09-17 08:09:54 +00002647
bellardf3311102006-04-12 20:21:17 +00002648 ClearCommError(s->hcom, &comerr, &status);
2649 if (status.cbInQue > 0) {
2650 s->len = status.cbInQue;
ths087f4ae2007-02-10 21:50:42 +00002651 win_chr_read_poll(chr);
2652 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002653 return 1;
2654 }
2655 return 0;
2656}
2657
ths52f61fd2006-12-22 21:20:52 +00002658static CharDriverState *qemu_chr_open_win(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002659{
2660 CharDriverState *chr;
2661 WinCharState *s;
ths3b46e622007-09-17 08:09:54 +00002662
bellardf3311102006-04-12 20:21:17 +00002663 chr = qemu_mallocz(sizeof(CharDriverState));
2664 if (!chr)
2665 return NULL;
2666 s = qemu_mallocz(sizeof(WinCharState));
2667 if (!s) {
2668 free(chr);
2669 return NULL;
2670 }
2671 chr->opaque = s;
2672 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002673 chr->chr_close = win_chr_close;
2674
ths087f4ae2007-02-10 21:50:42 +00002675 if (win_chr_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002676 free(s);
2677 free(chr);
2678 return NULL;
2679 }
ths86e94de2007-01-05 22:01:59 +00002680 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002681 return chr;
2682}
2683
2684static int win_chr_pipe_poll(void *opaque)
2685{
ths087f4ae2007-02-10 21:50:42 +00002686 CharDriverState *chr = opaque;
2687 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002688 DWORD size;
2689
2690 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2691 if (size > 0) {
2692 s->len = size;
ths087f4ae2007-02-10 21:50:42 +00002693 win_chr_read_poll(chr);
2694 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002695 return 1;
2696 }
2697 return 0;
2698}
2699
ths087f4ae2007-02-10 21:50:42 +00002700static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002701{
ths087f4ae2007-02-10 21:50:42 +00002702 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002703 OVERLAPPED ov;
2704 int ret;
2705 DWORD size;
2706 char openname[256];
ths3b46e622007-09-17 08:09:54 +00002707
bellardf3311102006-04-12 20:21:17 +00002708 s->fpipe = TRUE;
2709
2710 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2711 if (!s->hsend) {
2712 fprintf(stderr, "Failed CreateEvent\n");
2713 goto fail;
2714 }
2715 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2716 if (!s->hrecv) {
2717 fprintf(stderr, "Failed CreateEvent\n");
2718 goto fail;
2719 }
ths3b46e622007-09-17 08:09:54 +00002720
bellardf3311102006-04-12 20:21:17 +00002721 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2722 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2723 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2724 PIPE_WAIT,
2725 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2726 if (s->hcom == INVALID_HANDLE_VALUE) {
2727 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2728 s->hcom = NULL;
2729 goto fail;
2730 }
2731
2732 ZeroMemory(&ov, sizeof(ov));
2733 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2734 ret = ConnectNamedPipe(s->hcom, &ov);
2735 if (ret) {
2736 fprintf(stderr, "Failed ConnectNamedPipe\n");
2737 goto fail;
2738 }
2739
2740 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2741 if (!ret) {
2742 fprintf(stderr, "Failed GetOverlappedResult\n");
2743 if (ov.hEvent) {
2744 CloseHandle(ov.hEvent);
2745 ov.hEvent = NULL;
2746 }
2747 goto fail;
2748 }
2749
2750 if (ov.hEvent) {
2751 CloseHandle(ov.hEvent);
2752 ov.hEvent = NULL;
2753 }
ths087f4ae2007-02-10 21:50:42 +00002754 qemu_add_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002755 return 0;
2756
2757 fail:
ths087f4ae2007-02-10 21:50:42 +00002758 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002759 return -1;
2760}
2761
2762
ths52f61fd2006-12-22 21:20:52 +00002763static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002764{
2765 CharDriverState *chr;
2766 WinCharState *s;
2767
2768 chr = qemu_mallocz(sizeof(CharDriverState));
2769 if (!chr)
2770 return NULL;
2771 s = qemu_mallocz(sizeof(WinCharState));
2772 if (!s) {
2773 free(chr);
2774 return NULL;
2775 }
2776 chr->opaque = s;
2777 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002778 chr->chr_close = win_chr_close;
ths3b46e622007-09-17 08:09:54 +00002779
ths087f4ae2007-02-10 21:50:42 +00002780 if (win_chr_pipe_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002781 free(s);
2782 free(chr);
2783 return NULL;
2784 }
ths86e94de2007-01-05 22:01:59 +00002785 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002786 return chr;
2787}
2788
ths52f61fd2006-12-22 21:20:52 +00002789static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
bellardf3311102006-04-12 20:21:17 +00002790{
2791 CharDriverState *chr;
2792 WinCharState *s;
2793
2794 chr = qemu_mallocz(sizeof(CharDriverState));
2795 if (!chr)
2796 return NULL;
2797 s = qemu_mallocz(sizeof(WinCharState));
2798 if (!s) {
2799 free(chr);
2800 return NULL;
2801 }
2802 s->hcom = fd_out;
2803 chr->opaque = s;
2804 chr->chr_write = win_chr_write;
ths86e94de2007-01-05 22:01:59 +00002805 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002806 return chr;
2807}
ths72d46472007-05-13 14:54:54 +00002808
2809static CharDriverState *qemu_chr_open_win_con(const char *filename)
2810{
2811 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2812}
2813
ths52f61fd2006-12-22 21:20:52 +00002814static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
bellardf3311102006-04-12 20:21:17 +00002815{
2816 HANDLE fd_out;
ths3b46e622007-09-17 08:09:54 +00002817
bellardf3311102006-04-12 20:21:17 +00002818 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
2819 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2820 if (fd_out == INVALID_HANDLE_VALUE)
2821 return NULL;
2822
2823 return qemu_chr_open_win_file(fd_out);
2824}
thsaec62502007-06-25 11:48:07 +00002825#endif /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00002826
bellard0bab00f2006-06-25 14:49:44 +00002827/***********************************************************/
2828/* UDP Net console */
2829
2830typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002831 int fd;
2832 struct sockaddr_in daddr;
2833 char buf[1024];
2834 int bufcnt;
2835 int bufptr;
2836 int max_size;
2837} NetCharDriver;
2838
2839static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2840{
2841 NetCharDriver *s = chr->opaque;
2842
2843 return sendto(s->fd, buf, len, 0,
2844 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
2845}
2846
2847static int udp_chr_read_poll(void *opaque)
2848{
2849 CharDriverState *chr = opaque;
2850 NetCharDriver *s = chr->opaque;
2851
pbrooke5b0bc42007-01-27 23:46:43 +00002852 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002853
2854 /* If there were any stray characters in the queue process them
2855 * first
2856 */
2857 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002858 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002859 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002860 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002861 }
2862 return s->max_size;
2863}
2864
2865static void udp_chr_read(void *opaque)
2866{
2867 CharDriverState *chr = opaque;
2868 NetCharDriver *s = chr->opaque;
2869
2870 if (s->max_size == 0)
2871 return;
2872 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
2873 s->bufptr = s->bufcnt;
2874 if (s->bufcnt <= 0)
2875 return;
2876
2877 s->bufptr = 0;
2878 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002879 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002880 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002881 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002882 }
2883}
2884
pbrooke5b0bc42007-01-27 23:46:43 +00002885static void udp_chr_update_read_handler(CharDriverState *chr)
bellard0bab00f2006-06-25 14:49:44 +00002886{
2887 NetCharDriver *s = chr->opaque;
2888
2889 if (s->fd >= 0) {
bellard0bab00f2006-06-25 14:49:44 +00002890 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2891 udp_chr_read, NULL, chr);
2892 }
2893}
2894
2895int parse_host_port(struct sockaddr_in *saddr, const char *str);
ths52f61fd2006-12-22 21:20:52 +00002896#ifndef _WIN32
2897static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
2898#endif
bellard951f1352006-06-27 21:02:43 +00002899int parse_host_src_port(struct sockaddr_in *haddr,
2900 struct sockaddr_in *saddr,
2901 const char *str);
bellard0bab00f2006-06-25 14:49:44 +00002902
ths52f61fd2006-12-22 21:20:52 +00002903static CharDriverState *qemu_chr_open_udp(const char *def)
bellard0bab00f2006-06-25 14:49:44 +00002904{
2905 CharDriverState *chr = NULL;
2906 NetCharDriver *s = NULL;
2907 int fd = -1;
bellard951f1352006-06-27 21:02:43 +00002908 struct sockaddr_in saddr;
bellard0bab00f2006-06-25 14:49:44 +00002909
2910 chr = qemu_mallocz(sizeof(CharDriverState));
2911 if (!chr)
2912 goto return_err;
2913 s = qemu_mallocz(sizeof(NetCharDriver));
2914 if (!s)
2915 goto return_err;
2916
2917 fd = socket(PF_INET, SOCK_DGRAM, 0);
2918 if (fd < 0) {
2919 perror("socket(PF_INET, SOCK_DGRAM)");
2920 goto return_err;
2921 }
2922
bellard951f1352006-06-27 21:02:43 +00002923 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
2924 printf("Could not parse: %s\n", def);
2925 goto return_err;
bellard0bab00f2006-06-25 14:49:44 +00002926 }
2927
bellard951f1352006-06-27 21:02:43 +00002928 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
bellard0bab00f2006-06-25 14:49:44 +00002929 {
2930 perror("bind");
2931 goto return_err;
2932 }
2933
2934 s->fd = fd;
2935 s->bufcnt = 0;
2936 s->bufptr = 0;
2937 chr->opaque = s;
2938 chr->chr_write = udp_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002939 chr->chr_update_read_handler = udp_chr_update_read_handler;
bellard0bab00f2006-06-25 14:49:44 +00002940 return chr;
2941
2942return_err:
2943 if (chr)
2944 free(chr);
2945 if (s)
2946 free(s);
2947 if (fd >= 0)
2948 closesocket(fd);
2949 return NULL;
2950}
2951
2952/***********************************************************/
2953/* TCP Net console */
2954
2955typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002956 int fd, listen_fd;
2957 int connected;
2958 int max_size;
bellard951f1352006-06-27 21:02:43 +00002959 int do_telnetopt;
pbrooke5b0bc42007-01-27 23:46:43 +00002960 int do_nodelay;
thsffd843b2006-12-21 19:46:43 +00002961 int is_unix;
bellard0bab00f2006-06-25 14:49:44 +00002962} TCPCharDriver;
2963
2964static void tcp_chr_accept(void *opaque);
2965
2966static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2967{
2968 TCPCharDriver *s = chr->opaque;
2969 if (s->connected) {
2970 return send_all(s->fd, buf, len);
2971 } else {
2972 /* XXX: indicate an error ? */
2973 return len;
2974 }
2975}
2976
2977static int tcp_chr_read_poll(void *opaque)
2978{
2979 CharDriverState *chr = opaque;
2980 TCPCharDriver *s = chr->opaque;
2981 if (!s->connected)
2982 return 0;
pbrooke5b0bc42007-01-27 23:46:43 +00002983 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002984 return s->max_size;
2985}
2986
bellard951f1352006-06-27 21:02:43 +00002987#define IAC 255
2988#define IAC_BREAK 243
2989static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2990 TCPCharDriver *s,
2991 char *buf, int *size)
2992{
2993 /* Handle any telnet client's basic IAC options to satisfy char by
2994 * char mode with no echo. All IAC options will be removed from
2995 * the buf and the do_telnetopt variable will be used to track the
2996 * state of the width of the IAC information.
2997 *
2998 * IAC commands come in sets of 3 bytes with the exception of the
2999 * "IAC BREAK" command and the double IAC.
3000 */
3001
3002 int i;
3003 int j = 0;
3004
3005 for (i = 0; i < *size; i++) {
3006 if (s->do_telnetopt > 1) {
3007 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3008 /* Double IAC means send an IAC */
3009 if (j != i)
3010 buf[j] = buf[i];
3011 j++;
3012 s->do_telnetopt = 1;
3013 } else {
3014 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3015 /* Handle IAC break commands by sending a serial break */
pbrooke5b0bc42007-01-27 23:46:43 +00003016 qemu_chr_event(chr, CHR_EVENT_BREAK);
bellard951f1352006-06-27 21:02:43 +00003017 s->do_telnetopt++;
3018 }
3019 s->do_telnetopt++;
3020 }
3021 if (s->do_telnetopt >= 4) {
3022 s->do_telnetopt = 1;
3023 }
3024 } else {
3025 if ((unsigned char)buf[i] == IAC) {
3026 s->do_telnetopt = 2;
3027 } else {
3028 if (j != i)
3029 buf[j] = buf[i];
3030 j++;
3031 }
3032 }
3033 }
3034 *size = j;
3035}
3036
bellard0bab00f2006-06-25 14:49:44 +00003037static void tcp_chr_read(void *opaque)
3038{
3039 CharDriverState *chr = opaque;
3040 TCPCharDriver *s = chr->opaque;
3041 uint8_t buf[1024];
3042 int len, size;
3043
3044 if (!s->connected || s->max_size <= 0)
3045 return;
3046 len = sizeof(buf);
3047 if (len > s->max_size)
3048 len = s->max_size;
3049 size = recv(s->fd, buf, len, 0);
3050 if (size == 0) {
3051 /* connection closed */
3052 s->connected = 0;
3053 if (s->listen_fd >= 0) {
3054 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3055 }
3056 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3057 closesocket(s->fd);
3058 s->fd = -1;
3059 } else if (size > 0) {
bellard951f1352006-06-27 21:02:43 +00003060 if (s->do_telnetopt)
3061 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3062 if (size > 0)
pbrooke5b0bc42007-01-27 23:46:43 +00003063 qemu_chr_read(chr, buf, size);
bellard0bab00f2006-06-25 14:49:44 +00003064 }
3065}
3066
bellard0bab00f2006-06-25 14:49:44 +00003067static void tcp_chr_connect(void *opaque)
3068{
3069 CharDriverState *chr = opaque;
3070 TCPCharDriver *s = chr->opaque;
3071
3072 s->connected = 1;
3073 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3074 tcp_chr_read, NULL, chr);
ths86e94de2007-01-05 22:01:59 +00003075 qemu_chr_reset(chr);
bellard0bab00f2006-06-25 14:49:44 +00003076}
3077
bellard951f1352006-06-27 21:02:43 +00003078#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3079static void tcp_chr_telnet_init(int fd)
3080{
3081 char buf[3];
3082 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3083 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3084 send(fd, (char *)buf, 3, 0);
3085 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3086 send(fd, (char *)buf, 3, 0);
3087 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3088 send(fd, (char *)buf, 3, 0);
3089 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3090 send(fd, (char *)buf, 3, 0);
3091}
3092
pbrookf7499982007-01-28 00:10:01 +00003093static void socket_set_nodelay(int fd)
3094{
3095 int val = 1;
3096 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3097}
3098
bellard0bab00f2006-06-25 14:49:44 +00003099static void tcp_chr_accept(void *opaque)
3100{
3101 CharDriverState *chr = opaque;
3102 TCPCharDriver *s = chr->opaque;
3103 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003104#ifndef _WIN32
3105 struct sockaddr_un uaddr;
3106#endif
3107 struct sockaddr *addr;
bellard0bab00f2006-06-25 14:49:44 +00003108 socklen_t len;
3109 int fd;
3110
3111 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003112#ifndef _WIN32
3113 if (s->is_unix) {
3114 len = sizeof(uaddr);
3115 addr = (struct sockaddr *)&uaddr;
3116 } else
3117#endif
3118 {
3119 len = sizeof(saddr);
3120 addr = (struct sockaddr *)&saddr;
3121 }
3122 fd = accept(s->listen_fd, addr, &len);
bellard0bab00f2006-06-25 14:49:44 +00003123 if (fd < 0 && errno != EINTR) {
3124 return;
3125 } else if (fd >= 0) {
bellard951f1352006-06-27 21:02:43 +00003126 if (s->do_telnetopt)
3127 tcp_chr_telnet_init(fd);
bellard0bab00f2006-06-25 14:49:44 +00003128 break;
3129 }
3130 }
3131 socket_set_nonblock(fd);
pbrookf7499982007-01-28 00:10:01 +00003132 if (s->do_nodelay)
3133 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003134 s->fd = fd;
3135 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3136 tcp_chr_connect(chr);
3137}
3138
3139static void tcp_chr_close(CharDriverState *chr)
3140{
3141 TCPCharDriver *s = chr->opaque;
3142 if (s->fd >= 0)
3143 closesocket(s->fd);
3144 if (s->listen_fd >= 0)
3145 closesocket(s->listen_fd);
3146 qemu_free(s);
3147}
3148
ths5fafdf22007-09-16 21:08:06 +00003149static CharDriverState *qemu_chr_open_tcp(const char *host_str,
thsffd843b2006-12-21 19:46:43 +00003150 int is_telnet,
3151 int is_unix)
bellard0bab00f2006-06-25 14:49:44 +00003152{
3153 CharDriverState *chr = NULL;
3154 TCPCharDriver *s = NULL;
3155 int fd = -1, ret, err, val;
bellard951f1352006-06-27 21:02:43 +00003156 int is_listen = 0;
3157 int is_waitconnect = 1;
pbrookf7499982007-01-28 00:10:01 +00003158 int do_nodelay = 0;
bellard951f1352006-06-27 21:02:43 +00003159 const char *ptr;
bellard0bab00f2006-06-25 14:49:44 +00003160 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003161#ifndef _WIN32
3162 struct sockaddr_un uaddr;
3163#endif
3164 struct sockaddr *addr;
3165 socklen_t addrlen;
bellard0bab00f2006-06-25 14:49:44 +00003166
thsffd843b2006-12-21 19:46:43 +00003167#ifndef _WIN32
3168 if (is_unix) {
3169 addr = (struct sockaddr *)&uaddr;
3170 addrlen = sizeof(uaddr);
3171 if (parse_unix_path(&uaddr, host_str) < 0)
3172 goto fail;
3173 } else
3174#endif
3175 {
3176 addr = (struct sockaddr *)&saddr;
3177 addrlen = sizeof(saddr);
3178 if (parse_host_port(&saddr, host_str) < 0)
3179 goto fail;
3180 }
bellard0bab00f2006-06-25 14:49:44 +00003181
bellard951f1352006-06-27 21:02:43 +00003182 ptr = host_str;
3183 while((ptr = strchr(ptr,','))) {
3184 ptr++;
3185 if (!strncmp(ptr,"server",6)) {
3186 is_listen = 1;
3187 } else if (!strncmp(ptr,"nowait",6)) {
3188 is_waitconnect = 0;
pbrookf7499982007-01-28 00:10:01 +00003189 } else if (!strncmp(ptr,"nodelay",6)) {
3190 do_nodelay = 1;
bellard951f1352006-06-27 21:02:43 +00003191 } else {
3192 printf("Unknown option: %s\n", ptr);
3193 goto fail;
3194 }
3195 }
3196 if (!is_listen)
3197 is_waitconnect = 0;
3198
bellard0bab00f2006-06-25 14:49:44 +00003199 chr = qemu_mallocz(sizeof(CharDriverState));
3200 if (!chr)
3201 goto fail;
3202 s = qemu_mallocz(sizeof(TCPCharDriver));
3203 if (!s)
3204 goto fail;
thsffd843b2006-12-21 19:46:43 +00003205
3206#ifndef _WIN32
3207 if (is_unix)
3208 fd = socket(PF_UNIX, SOCK_STREAM, 0);
3209 else
3210#endif
3211 fd = socket(PF_INET, SOCK_STREAM, 0);
ths5fafdf22007-09-16 21:08:06 +00003212
3213 if (fd < 0)
bellard0bab00f2006-06-25 14:49:44 +00003214 goto fail;
bellard951f1352006-06-27 21:02:43 +00003215
3216 if (!is_waitconnect)
3217 socket_set_nonblock(fd);
bellard0bab00f2006-06-25 14:49:44 +00003218
3219 s->connected = 0;
3220 s->fd = -1;
3221 s->listen_fd = -1;
thsffd843b2006-12-21 19:46:43 +00003222 s->is_unix = is_unix;
pbrookf7499982007-01-28 00:10:01 +00003223 s->do_nodelay = do_nodelay && !is_unix;
thsffd843b2006-12-21 19:46:43 +00003224
3225 chr->opaque = s;
3226 chr->chr_write = tcp_chr_write;
thsffd843b2006-12-21 19:46:43 +00003227 chr->chr_close = tcp_chr_close;
3228
bellard0bab00f2006-06-25 14:49:44 +00003229 if (is_listen) {
3230 /* allow fast reuse */
thsffd843b2006-12-21 19:46:43 +00003231#ifndef _WIN32
3232 if (is_unix) {
3233 char path[109];
3234 strncpy(path, uaddr.sun_path, 108);
3235 path[108] = 0;
3236 unlink(path);
3237 } else
3238#endif
3239 {
3240 val = 1;
3241 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3242 }
ths3b46e622007-09-17 08:09:54 +00003243
thsffd843b2006-12-21 19:46:43 +00003244 ret = bind(fd, addr, addrlen);
3245 if (ret < 0)
bellard0bab00f2006-06-25 14:49:44 +00003246 goto fail;
thsffd843b2006-12-21 19:46:43 +00003247
bellard0bab00f2006-06-25 14:49:44 +00003248 ret = listen(fd, 0);
3249 if (ret < 0)
3250 goto fail;
thsffd843b2006-12-21 19:46:43 +00003251
bellard0bab00f2006-06-25 14:49:44 +00003252 s->listen_fd = fd;
3253 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
bellard951f1352006-06-27 21:02:43 +00003254 if (is_telnet)
3255 s->do_telnetopt = 1;
bellard0bab00f2006-06-25 14:49:44 +00003256 } else {
3257 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003258 ret = connect(fd, addr, addrlen);
bellard0bab00f2006-06-25 14:49:44 +00003259 if (ret < 0) {
3260 err = socket_error();
3261 if (err == EINTR || err == EWOULDBLOCK) {
3262 } else if (err == EINPROGRESS) {
3263 break;
thsf5b12262007-03-25 15:58:03 +00003264#ifdef _WIN32
3265 } else if (err == WSAEALREADY) {
3266 break;
3267#endif
bellard0bab00f2006-06-25 14:49:44 +00003268 } else {
3269 goto fail;
3270 }
3271 } else {
3272 s->connected = 1;
3273 break;
3274 }
3275 }
3276 s->fd = fd;
pbrookf7499982007-01-28 00:10:01 +00003277 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003278 if (s->connected)
3279 tcp_chr_connect(chr);
3280 else
3281 qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3282 }
ths3b46e622007-09-17 08:09:54 +00003283
bellard951f1352006-06-27 21:02:43 +00003284 if (is_listen && is_waitconnect) {
3285 printf("QEMU waiting for connection on: %s\n", host_str);
3286 tcp_chr_accept(chr);
3287 socket_set_nonblock(s->listen_fd);
3288 }
3289
bellard0bab00f2006-06-25 14:49:44 +00003290 return chr;
3291 fail:
3292 if (fd >= 0)
3293 closesocket(fd);
3294 qemu_free(s);
3295 qemu_free(chr);
3296 return NULL;
3297}
3298
bellard82c643f2004-07-14 17:28:13 +00003299CharDriverState *qemu_chr_open(const char *filename)
3300{
bellardf8d179e2005-11-08 22:30:36 +00003301 const char *p;
bellardfd1dff42006-02-01 21:29:26 +00003302
bellard82c643f2004-07-14 17:28:13 +00003303 if (!strcmp(filename, "vc")) {
thsaf3a9032007-07-11 23:14:59 +00003304 return text_console_init(&display_state, 0);
3305 } else if (strstart(filename, "vc:", &p)) {
3306 return text_console_init(&display_state, p);
bellard82c643f2004-07-14 17:28:13 +00003307 } else if (!strcmp(filename, "null")) {
3308 return qemu_chr_open_null();
ths5fafdf22007-09-16 21:08:06 +00003309 } else
bellard0bab00f2006-06-25 14:49:44 +00003310 if (strstart(filename, "tcp:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003311 return qemu_chr_open_tcp(p, 0, 0);
bellard0bab00f2006-06-25 14:49:44 +00003312 } else
bellard951f1352006-06-27 21:02:43 +00003313 if (strstart(filename, "telnet:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003314 return qemu_chr_open_tcp(p, 1, 0);
bellard0bab00f2006-06-25 14:49:44 +00003315 } else
3316 if (strstart(filename, "udp:", &p)) {
3317 return qemu_chr_open_udp(p);
3318 } else
ths20d8a3e2007-02-18 17:04:49 +00003319 if (strstart(filename, "mon:", &p)) {
3320 CharDriverState *drv = qemu_chr_open(p);
3321 if (drv) {
3322 drv = qemu_chr_open_mux(drv);
3323 monitor_init(drv, !nographic);
3324 return drv;
3325 }
3326 printf("Unable to open driver: %s\n", p);
3327 return 0;
3328 } else
bellard76647282005-11-27 19:10:42 +00003329#ifndef _WIN32
thsffd843b2006-12-21 19:46:43 +00003330 if (strstart(filename, "unix:", &p)) {
3331 return qemu_chr_open_tcp(p, 0, 1);
3332 } else if (strstart(filename, "file:", &p)) {
bellardf8d179e2005-11-08 22:30:36 +00003333 return qemu_chr_open_file_out(p);
3334 } else if (strstart(filename, "pipe:", &p)) {
3335 return qemu_chr_open_pipe(p);
bellard76647282005-11-27 19:10:42 +00003336 } else if (!strcmp(filename, "pty")) {
bellard82c643f2004-07-14 17:28:13 +00003337 return qemu_chr_open_pty();
3338 } else if (!strcmp(filename, "stdio")) {
3339 return qemu_chr_open_stdio();
ths5fafdf22007-09-16 21:08:06 +00003340 } else
bellardf8d179e2005-11-08 22:30:36 +00003341#if defined(__linux__)
bellarde57a8c02005-11-10 23:58:52 +00003342 if (strstart(filename, "/dev/parport", NULL)) {
3343 return qemu_chr_open_pp(filename);
ths5fafdf22007-09-16 21:08:06 +00003344 } else
thsaec62502007-06-25 11:48:07 +00003345#endif
ths3fda3882007-06-28 15:14:49 +00003346#if defined(__linux__) || defined(__sun__)
bellardf8d179e2005-11-08 22:30:36 +00003347 if (strstart(filename, "/dev/", NULL)) {
3348 return qemu_chr_open_tty(filename);
ths3fda3882007-06-28 15:14:49 +00003349 } else
3350#endif
thsaec62502007-06-25 11:48:07 +00003351#else /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00003352 if (strstart(filename, "COM", NULL)) {
3353 return qemu_chr_open_win(filename);
3354 } else
3355 if (strstart(filename, "pipe:", &p)) {
3356 return qemu_chr_open_win_pipe(p);
3357 } else
ths72d46472007-05-13 14:54:54 +00003358 if (strstart(filename, "con:", NULL)) {
3359 return qemu_chr_open_win_con(filename);
3360 } else
bellardf3311102006-04-12 20:21:17 +00003361 if (strstart(filename, "file:", &p)) {
3362 return qemu_chr_open_win_file_out(p);
3363 }
3364#endif
bellard82c643f2004-07-14 17:28:13 +00003365 {
3366 return NULL;
3367 }
3368}
3369
bellardf3311102006-04-12 20:21:17 +00003370void qemu_chr_close(CharDriverState *chr)
3371{
3372 if (chr->chr_close)
3373 chr->chr_close(chr);
3374}
3375
bellardf1510b22003-06-25 00:07:40 +00003376/***********************************************************/
bellard7c9d8e02005-11-15 22:16:05 +00003377/* network device redirectors */
bellardf1510b22003-06-25 00:07:40 +00003378
bellardc20709a2004-04-21 23:27:19 +00003379void hex_dump(FILE *f, const uint8_t *buf, int size)
bellard67b915a2004-03-31 23:37:16 +00003380{
bellardc20709a2004-04-21 23:27:19 +00003381 int len, i, j, c;
3382
3383 for(i=0;i<size;i+=16) {
3384 len = size - i;
3385 if (len > 16)
3386 len = 16;
3387 fprintf(f, "%08x ", i);
3388 for(j=0;j<16;j++) {
3389 if (j < len)
3390 fprintf(f, " %02x", buf[i+j]);
3391 else
3392 fprintf(f, " ");
3393 }
3394 fprintf(f, " ");
3395 for(j=0;j<len;j++) {
3396 c = buf[i+j];
3397 if (c < ' ' || c > '~')
3398 c = '.';
3399 fprintf(f, "%c", c);
3400 }
3401 fprintf(f, "\n");
3402 }
3403}
3404
bellard7c9d8e02005-11-15 22:16:05 +00003405static int parse_macaddr(uint8_t *macaddr, const char *p)
bellardc20709a2004-04-21 23:27:19 +00003406{
bellard7c9d8e02005-11-15 22:16:05 +00003407 int i;
3408 for(i = 0; i < 6; i++) {
3409 macaddr[i] = strtol(p, (char **)&p, 16);
3410 if (i == 5) {
ths5fafdf22007-09-16 21:08:06 +00003411 if (*p != '\0')
bellard7c9d8e02005-11-15 22:16:05 +00003412 return -1;
3413 } else {
ths5fafdf22007-09-16 21:08:06 +00003414 if (*p != ':')
bellard7c9d8e02005-11-15 22:16:05 +00003415 return -1;
3416 p++;
3417 }
bellardc20709a2004-04-21 23:27:19 +00003418 }
bellardc20709a2004-04-21 23:27:19 +00003419 return 0;
3420}
3421
bellard9bf05442004-08-25 22:12:49 +00003422static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3423{
3424 const char *p, *p1;
3425 int len;
3426 p = *pp;
3427 p1 = strchr(p, sep);
3428 if (!p1)
3429 return -1;
3430 len = p1 - p;
3431 p1++;
3432 if (buf_size > 0) {
3433 if (len > buf_size - 1)
3434 len = buf_size - 1;
3435 memcpy(buf, p, len);
3436 buf[len] = '\0';
3437 }
3438 *pp = p1;
3439 return 0;
3440}
3441
bellard951f1352006-06-27 21:02:43 +00003442int parse_host_src_port(struct sockaddr_in *haddr,
3443 struct sockaddr_in *saddr,
3444 const char *input_str)
3445{
3446 char *str = strdup(input_str);
3447 char *host_str = str;
3448 char *src_str;
3449 char *ptr;
3450
3451 /*
3452 * Chop off any extra arguments at the end of the string which
3453 * would start with a comma, then fill in the src port information
3454 * if it was provided else use the "any address" and "any port".
3455 */
3456 if ((ptr = strchr(str,',')))
3457 *ptr = '\0';
3458
3459 if ((src_str = strchr(input_str,'@'))) {
3460 *src_str = '\0';
3461 src_str++;
3462 }
3463
3464 if (parse_host_port(haddr, host_str) < 0)
3465 goto fail;
3466
3467 if (!src_str || *src_str == '\0')
3468 src_str = ":0";
3469
3470 if (parse_host_port(saddr, src_str) < 0)
3471 goto fail;
3472
3473 free(str);
3474 return(0);
3475
3476fail:
3477 free(str);
3478 return -1;
3479}
3480
bellard7c9d8e02005-11-15 22:16:05 +00003481int parse_host_port(struct sockaddr_in *saddr, const char *str)
3482{
3483 char buf[512];
3484 struct hostent *he;
3485 const char *p, *r;
3486 int port;
3487
3488 p = str;
3489 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3490 return -1;
3491 saddr->sin_family = AF_INET;
3492 if (buf[0] == '\0') {
3493 saddr->sin_addr.s_addr = 0;
3494 } else {
3495 if (isdigit(buf[0])) {
3496 if (!inet_aton(buf, &saddr->sin_addr))
3497 return -1;
3498 } else {
bellard7c9d8e02005-11-15 22:16:05 +00003499 if ((he = gethostbyname(buf)) == NULL)
3500 return - 1;
3501 saddr->sin_addr = *(struct in_addr *)he->h_addr;
bellard7c9d8e02005-11-15 22:16:05 +00003502 }
3503 }
3504 port = strtol(p, (char **)&r, 0);
3505 if (r == p)
3506 return -1;
3507 saddr->sin_port = htons(port);
3508 return 0;
3509}
3510
ths52f61fd2006-12-22 21:20:52 +00003511#ifndef _WIN32
3512static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
thsffd843b2006-12-21 19:46:43 +00003513{
3514 const char *p;
3515 int len;
3516
3517 len = MIN(108, strlen(str));
3518 p = strchr(str, ',');
3519 if (p)
3520 len = MIN(len, p - str);
3521
3522 memset(uaddr, 0, sizeof(*uaddr));
3523
3524 uaddr->sun_family = AF_UNIX;
3525 memcpy(uaddr->sun_path, str, len);
3526
3527 return 0;
3528}
ths52f61fd2006-12-22 21:20:52 +00003529#endif
thsffd843b2006-12-21 19:46:43 +00003530
bellard7c9d8e02005-11-15 22:16:05 +00003531/* find or alloc a new VLAN */
3532VLANState *qemu_find_vlan(int id)
3533{
3534 VLANState **pvlan, *vlan;
3535 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3536 if (vlan->id == id)
3537 return vlan;
3538 }
3539 vlan = qemu_mallocz(sizeof(VLANState));
3540 if (!vlan)
3541 return NULL;
3542 vlan->id = id;
3543 vlan->next = NULL;
3544 pvlan = &first_vlan;
3545 while (*pvlan != NULL)
3546 pvlan = &(*pvlan)->next;
3547 *pvlan = vlan;
3548 return vlan;
3549}
3550
3551VLANClientState *qemu_new_vlan_client(VLANState *vlan,
pbrookd861b052006-02-04 22:15:28 +00003552 IOReadHandler *fd_read,
3553 IOCanRWHandler *fd_can_read,
3554 void *opaque)
bellard7c9d8e02005-11-15 22:16:05 +00003555{
3556 VLANClientState *vc, **pvc;
3557 vc = qemu_mallocz(sizeof(VLANClientState));
3558 if (!vc)
3559 return NULL;
3560 vc->fd_read = fd_read;
pbrookd861b052006-02-04 22:15:28 +00003561 vc->fd_can_read = fd_can_read;
bellard7c9d8e02005-11-15 22:16:05 +00003562 vc->opaque = opaque;
3563 vc->vlan = vlan;
3564
3565 vc->next = NULL;
3566 pvc = &vlan->first_client;
3567 while (*pvc != NULL)
3568 pvc = &(*pvc)->next;
3569 *pvc = vc;
3570 return vc;
3571}
3572
pbrookd861b052006-02-04 22:15:28 +00003573int qemu_can_send_packet(VLANClientState *vc1)
3574{
3575 VLANState *vlan = vc1->vlan;
3576 VLANClientState *vc;
3577
3578 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3579 if (vc != vc1) {
balrogfbd17112007-07-02 13:31:53 +00003580 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3581 return 1;
pbrookd861b052006-02-04 22:15:28 +00003582 }
3583 }
balrogfbd17112007-07-02 13:31:53 +00003584 return 0;
pbrookd861b052006-02-04 22:15:28 +00003585}
3586
bellard7c9d8e02005-11-15 22:16:05 +00003587void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3588{
3589 VLANState *vlan = vc1->vlan;
3590 VLANClientState *vc;
3591
3592#if 0
3593 printf("vlan %d send:\n", vlan->id);
3594 hex_dump(stdout, buf, size);
3595#endif
3596 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3597 if (vc != vc1) {
3598 vc->fd_read(vc->opaque, buf, size);
3599 }
3600 }
3601}
3602
3603#if defined(CONFIG_SLIRP)
3604
3605/* slirp network adapter */
3606
3607static int slirp_inited;
3608static VLANClientState *slirp_vc;
3609
3610int slirp_can_output(void)
3611{
pbrook3b7f5d42006-02-10 17:34:02 +00003612 return !slirp_vc || qemu_can_send_packet(slirp_vc);
bellard7c9d8e02005-11-15 22:16:05 +00003613}
3614
3615void slirp_output(const uint8_t *pkt, int pkt_len)
3616{
3617#if 0
3618 printf("slirp output:\n");
3619 hex_dump(stdout, pkt, pkt_len);
3620#endif
pbrook3b7f5d42006-02-10 17:34:02 +00003621 if (!slirp_vc)
3622 return;
bellard7c9d8e02005-11-15 22:16:05 +00003623 qemu_send_packet(slirp_vc, pkt, pkt_len);
3624}
3625
3626static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3627{
3628#if 0
3629 printf("slirp input:\n");
3630 hex_dump(stdout, buf, size);
3631#endif
3632 slirp_input(buf, size);
3633}
3634
3635static int net_slirp_init(VLANState *vlan)
3636{
3637 if (!slirp_inited) {
3638 slirp_inited = 1;
3639 slirp_init();
3640 }
ths5fafdf22007-09-16 21:08:06 +00003641 slirp_vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00003642 slirp_receive, NULL, NULL);
bellard7c9d8e02005-11-15 22:16:05 +00003643 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3644 return 0;
3645}
3646
bellard9bf05442004-08-25 22:12:49 +00003647static void net_slirp_redir(const char *redir_str)
3648{
3649 int is_udp;
3650 char buf[256], *r;
3651 const char *p;
3652 struct in_addr guest_addr;
3653 int host_port, guest_port;
ths3b46e622007-09-17 08:09:54 +00003654
bellard9bf05442004-08-25 22:12:49 +00003655 if (!slirp_inited) {
3656 slirp_inited = 1;
3657 slirp_init();
3658 }
3659
3660 p = redir_str;
3661 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3662 goto fail;
3663 if (!strcmp(buf, "tcp")) {
3664 is_udp = 0;
3665 } else if (!strcmp(buf, "udp")) {
3666 is_udp = 1;
3667 } else {
3668 goto fail;
3669 }
3670
3671 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3672 goto fail;
3673 host_port = strtol(buf, &r, 0);
3674 if (r == buf)
3675 goto fail;
3676
3677 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3678 goto fail;
3679 if (buf[0] == '\0') {
3680 pstrcpy(buf, sizeof(buf), "10.0.2.15");
3681 }
3682 if (!inet_aton(buf, &guest_addr))
3683 goto fail;
ths3b46e622007-09-17 08:09:54 +00003684
bellard9bf05442004-08-25 22:12:49 +00003685 guest_port = strtol(p, &r, 0);
3686 if (r == p)
3687 goto fail;
ths3b46e622007-09-17 08:09:54 +00003688
bellard9bf05442004-08-25 22:12:49 +00003689 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
3690 fprintf(stderr, "qemu: could not set up redirection\n");
3691 exit(1);
3692 }
3693 return;
3694 fail:
3695 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3696 exit(1);
3697}
ths3b46e622007-09-17 08:09:54 +00003698
bellardc94c8d62004-09-13 21:37:34 +00003699#ifndef _WIN32
3700
bellard9d728e82004-09-05 23:09:03 +00003701char smb_dir[1024];
3702
3703static void smb_exit(void)
3704{
3705 DIR *d;
3706 struct dirent *de;
3707 char filename[1024];
3708
3709 /* erase all the files in the directory */
3710 d = opendir(smb_dir);
3711 for(;;) {
3712 de = readdir(d);
3713 if (!de)
3714 break;
3715 if (strcmp(de->d_name, ".") != 0 &&
3716 strcmp(de->d_name, "..") != 0) {
ths5fafdf22007-09-16 21:08:06 +00003717 snprintf(filename, sizeof(filename), "%s/%s",
bellard9d728e82004-09-05 23:09:03 +00003718 smb_dir, de->d_name);
3719 unlink(filename);
3720 }
3721 }
bellard03ffbb62004-09-06 00:14:04 +00003722 closedir(d);
bellard9d728e82004-09-05 23:09:03 +00003723 rmdir(smb_dir);
3724}
3725
3726/* automatic user mode samba server configuration */
3727void net_slirp_smb(const char *exported_dir)
3728{
3729 char smb_conf[1024];
3730 char smb_cmdline[1024];
3731 FILE *f;
3732
3733 if (!slirp_inited) {
3734 slirp_inited = 1;
3735 slirp_init();
3736 }
3737
3738 /* XXX: better tmp dir construction */
3739 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
3740 if (mkdir(smb_dir, 0700) < 0) {
3741 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
3742 exit(1);
3743 }
3744 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
ths3b46e622007-09-17 08:09:54 +00003745
bellard9d728e82004-09-05 23:09:03 +00003746 f = fopen(smb_conf, "w");
3747 if (!f) {
3748 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
3749 exit(1);
3750 }
ths5fafdf22007-09-16 21:08:06 +00003751 fprintf(f,
bellard9d728e82004-09-05 23:09:03 +00003752 "[global]\n"
bellard157777e2005-03-01 21:28:45 +00003753 "private dir=%s\n"
3754 "smb ports=0\n"
3755 "socket address=127.0.0.1\n"
bellard9d728e82004-09-05 23:09:03 +00003756 "pid directory=%s\n"
3757 "lock directory=%s\n"
3758 "log file=%s/log.smbd\n"
3759 "smb passwd file=%s/smbpasswd\n"
bellard03ffbb62004-09-06 00:14:04 +00003760 "security = share\n"
bellard9d728e82004-09-05 23:09:03 +00003761 "[qemu]\n"
3762 "path=%s\n"
3763 "read only=no\n"
3764 "guest ok=yes\n",
3765 smb_dir,
3766 smb_dir,
3767 smb_dir,
3768 smb_dir,
bellard157777e2005-03-01 21:28:45 +00003769 smb_dir,
bellard9d728e82004-09-05 23:09:03 +00003770 exported_dir
3771 );
3772 fclose(f);
3773 atexit(smb_exit);
3774
pbrooka14d6c82006-12-23 15:37:33 +00003775 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
3776 SMBD_COMMAND, smb_conf);
ths3b46e622007-09-17 08:09:54 +00003777
bellard9d728e82004-09-05 23:09:03 +00003778 slirp_add_exec(0, smb_cmdline, 4, 139);
3779}
bellard9bf05442004-08-25 22:12:49 +00003780
bellardc94c8d62004-09-13 21:37:34 +00003781#endif /* !defined(_WIN32) */
blueswir131a60e22007-10-26 18:42:59 +00003782void do_info_slirp(void)
3783{
3784 slirp_stats();
3785}
bellardc94c8d62004-09-13 21:37:34 +00003786
bellardc20709a2004-04-21 23:27:19 +00003787#endif /* CONFIG_SLIRP */
3788
3789#if !defined(_WIN32)
bellard7c9d8e02005-11-15 22:16:05 +00003790
3791typedef struct TAPState {
3792 VLANClientState *vc;
3793 int fd;
thsb46a8902007-10-21 23:20:45 +00003794 char down_script[1024];
bellard7c9d8e02005-11-15 22:16:05 +00003795} TAPState;
3796
3797static void tap_receive(void *opaque, const uint8_t *buf, int size)
3798{
3799 TAPState *s = opaque;
3800 int ret;
3801 for(;;) {
3802 ret = write(s->fd, buf, size);
3803 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
3804 } else {
3805 break;
3806 }
3807 }
3808}
3809
3810static void tap_send(void *opaque)
3811{
3812 TAPState *s = opaque;
3813 uint8_t buf[4096];
3814 int size;
3815
thsd5d10bc2007-02-17 22:54:49 +00003816#ifdef __sun__
3817 struct strbuf sbuf;
3818 int f = 0;
3819 sbuf.maxlen = sizeof(buf);
3820 sbuf.buf = buf;
3821 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
3822#else
bellard7c9d8e02005-11-15 22:16:05 +00003823 size = read(s->fd, buf, sizeof(buf));
thsd5d10bc2007-02-17 22:54:49 +00003824#endif
bellard7c9d8e02005-11-15 22:16:05 +00003825 if (size > 0) {
3826 qemu_send_packet(s->vc, buf, size);
3827 }
3828}
3829
3830/* fd support */
3831
3832static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
3833{
3834 TAPState *s;
3835
3836 s = qemu_mallocz(sizeof(TAPState));
3837 if (!s)
3838 return NULL;
3839 s->fd = fd;
pbrookd861b052006-02-04 22:15:28 +00003840 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00003841 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
3842 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
3843 return s;
3844}
3845
ths5c40d2b2007-06-23 16:03:36 +00003846#if defined (_BSD) || defined (__FreeBSD_kernel__)
bellard7c9d8e02005-11-15 22:16:05 +00003847static int tap_open(char *ifname, int ifname_size)
bellard7d3505c2004-05-12 19:32:15 +00003848{
3849 int fd;
3850 char *dev;
3851 struct stat s;
bellard67b915a2004-03-31 23:37:16 +00003852
balrogaeb30be2007-07-02 15:03:13 +00003853 TFR(fd = open("/dev/tap", O_RDWR));
bellard7d3505c2004-05-12 19:32:15 +00003854 if (fd < 0) {
3855 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
3856 return -1;
3857 }
3858
3859 fstat(fd, &s);
3860 dev = devname(s.st_rdev, S_IFCHR);
3861 pstrcpy(ifname, ifname_size, dev);
3862
3863 fcntl(fd, F_SETFL, O_NONBLOCK);
3864 return fd;
3865}
bellardec530c82006-04-25 22:36:06 +00003866#elif defined(__sun__)
thsd5d10bc2007-02-17 22:54:49 +00003867#define TUNNEWPPA (('T'<<16) | 0x0001)
ths5fafdf22007-09-16 21:08:06 +00003868/*
3869 * Allocate TAP device, returns opened fd.
thsd5d10bc2007-02-17 22:54:49 +00003870 * Stores dev name in the first arg(must be large enough).
ths3b46e622007-09-17 08:09:54 +00003871 */
thsd5d10bc2007-02-17 22:54:49 +00003872int tap_alloc(char *dev)
3873{
3874 int tap_fd, if_fd, ppa = -1;
3875 static int ip_fd = 0;
3876 char *ptr;
3877
3878 static int arp_fd = 0;
3879 int ip_muxid, arp_muxid;
3880 struct strioctl strioc_if, strioc_ppa;
3881 int link_type = I_PLINK;;
3882 struct lifreq ifr;
3883 char actual_name[32] = "";
3884
3885 memset(&ifr, 0x0, sizeof(ifr));
3886
3887 if( *dev ){
ths5fafdf22007-09-16 21:08:06 +00003888 ptr = dev;
3889 while( *ptr && !isdigit((int)*ptr) ) ptr++;
thsd5d10bc2007-02-17 22:54:49 +00003890 ppa = atoi(ptr);
3891 }
3892
3893 /* Check if IP device was opened */
3894 if( ip_fd )
3895 close(ip_fd);
3896
balrogaeb30be2007-07-02 15:03:13 +00003897 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
3898 if (ip_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003899 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
3900 return -1;
3901 }
3902
balrogaeb30be2007-07-02 15:03:13 +00003903 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
3904 if (tap_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003905 syslog(LOG_ERR, "Can't open /dev/tap");
3906 return -1;
3907 }
3908
3909 /* Assign a new PPA and get its unit number. */
3910 strioc_ppa.ic_cmd = TUNNEWPPA;
3911 strioc_ppa.ic_timout = 0;
3912 strioc_ppa.ic_len = sizeof(ppa);
3913 strioc_ppa.ic_dp = (char *)&ppa;
3914 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
3915 syslog (LOG_ERR, "Can't assign new interface");
3916
balrogaeb30be2007-07-02 15:03:13 +00003917 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
3918 if (if_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003919 syslog(LOG_ERR, "Can't open /dev/tap (2)");
3920 return -1;
3921 }
3922 if(ioctl(if_fd, I_PUSH, "ip") < 0){
3923 syslog(LOG_ERR, "Can't push IP module");
3924 return -1;
3925 }
3926
3927 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
3928 syslog(LOG_ERR, "Can't get flags\n");
3929
3930 snprintf (actual_name, 32, "tap%d", ppa);
3931 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3932
3933 ifr.lifr_ppa = ppa;
3934 /* Assign ppa according to the unit number returned by tun device */
3935
3936 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
3937 syslog (LOG_ERR, "Can't set PPA %d", ppa);
3938 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
3939 syslog (LOG_ERR, "Can't get flags\n");
3940 /* Push arp module to if_fd */
3941 if (ioctl (if_fd, I_PUSH, "arp") < 0)
3942 syslog (LOG_ERR, "Can't push ARP module (2)");
3943
3944 /* Push arp module to ip_fd */
3945 if (ioctl (ip_fd, I_POP, NULL) < 0)
3946 syslog (LOG_ERR, "I_POP failed\n");
3947 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
3948 syslog (LOG_ERR, "Can't push ARP module (3)\n");
3949 /* Open arp_fd */
balrogaeb30be2007-07-02 15:03:13 +00003950 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
3951 if (arp_fd < 0)
thsd5d10bc2007-02-17 22:54:49 +00003952 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
3953
3954 /* Set ifname to arp */
3955 strioc_if.ic_cmd = SIOCSLIFNAME;
3956 strioc_if.ic_timout = 0;
3957 strioc_if.ic_len = sizeof(ifr);
3958 strioc_if.ic_dp = (char *)&ifr;
3959 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
3960 syslog (LOG_ERR, "Can't set ifname to arp\n");
3961 }
3962
3963 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
3964 syslog(LOG_ERR, "Can't link TAP device to IP");
3965 return -1;
3966 }
3967
3968 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
3969 syslog (LOG_ERR, "Can't link TAP device to ARP");
3970
3971 close (if_fd);
3972
3973 memset(&ifr, 0x0, sizeof(ifr));
3974 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3975 ifr.lifr_ip_muxid = ip_muxid;
3976 ifr.lifr_arp_muxid = arp_muxid;
3977
3978 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
3979 {
3980 ioctl (ip_fd, I_PUNLINK , arp_muxid);
3981 ioctl (ip_fd, I_PUNLINK, ip_muxid);
3982 syslog (LOG_ERR, "Can't set multiplexor id");
3983 }
3984
3985 sprintf(dev, "tap%d", ppa);
3986 return tap_fd;
3987}
3988
bellardec530c82006-04-25 22:36:06 +00003989static int tap_open(char *ifname, int ifname_size)
3990{
thsd5d10bc2007-02-17 22:54:49 +00003991 char dev[10]="";
3992 int fd;
3993 if( (fd = tap_alloc(dev)) < 0 ){
3994 fprintf(stderr, "Cannot allocate TAP device\n");
3995 return -1;
3996 }
3997 pstrcpy(ifname, ifname_size, dev);
3998 fcntl(fd, F_SETFL, O_NONBLOCK);
3999 return fd;
bellardec530c82006-04-25 22:36:06 +00004000}
bellard7d3505c2004-05-12 19:32:15 +00004001#else
bellard7c9d8e02005-11-15 22:16:05 +00004002static int tap_open(char *ifname, int ifname_size)
bellardf1510b22003-06-25 00:07:40 +00004003{
4004 struct ifreq ifr;
bellardc4b1fcc2004-03-14 21:44:30 +00004005 int fd, ret;
ths3b46e622007-09-17 08:09:54 +00004006
balrogaeb30be2007-07-02 15:03:13 +00004007 TFR(fd = open("/dev/net/tun", O_RDWR));
bellardf1510b22003-06-25 00:07:40 +00004008 if (fd < 0) {
4009 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4010 return -1;
4011 }
4012 memset(&ifr, 0, sizeof(ifr));
4013 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
bellard7c9d8e02005-11-15 22:16:05 +00004014 if (ifname[0] != '\0')
4015 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4016 else
4017 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
bellardf1510b22003-06-25 00:07:40 +00004018 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4019 if (ret != 0) {
4020 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4021 close(fd);
4022 return -1;
4023 }
bellardc4b1fcc2004-03-14 21:44:30 +00004024 pstrcpy(ifname, ifname_size, ifr.ifr_name);
bellardf1510b22003-06-25 00:07:40 +00004025 fcntl(fd, F_SETFL, O_NONBLOCK);
bellardc4b1fcc2004-03-14 21:44:30 +00004026 return fd;
4027}
bellard7d3505c2004-05-12 19:32:15 +00004028#endif
bellardf1510b22003-06-25 00:07:40 +00004029
thsb46a8902007-10-21 23:20:45 +00004030static int launch_script(const char *setup_script, const char *ifname, int fd)
bellardc4b1fcc2004-03-14 21:44:30 +00004031{
thsb46a8902007-10-21 23:20:45 +00004032 int pid, status;
bellardc20709a2004-04-21 23:27:19 +00004033 char *args[3];
4034 char **parg;
4035
thsb46a8902007-10-21 23:20:45 +00004036 /* try to launch network script */
bellard7c9d8e02005-11-15 22:16:05 +00004037 pid = fork();
4038 if (pid >= 0) {
4039 if (pid == 0) {
ths50d3eea2007-03-19 16:36:43 +00004040 int open_max = sysconf (_SC_OPEN_MAX), i;
4041 for (i = 0; i < open_max; i++)
4042 if (i != STDIN_FILENO &&
4043 i != STDOUT_FILENO &&
4044 i != STDERR_FILENO &&
4045 i != fd)
4046 close(i);
4047
bellard7c9d8e02005-11-15 22:16:05 +00004048 parg = args;
4049 *parg++ = (char *)setup_script;
thsb46a8902007-10-21 23:20:45 +00004050 *parg++ = (char *)ifname;
bellard7c9d8e02005-11-15 22:16:05 +00004051 *parg++ = NULL;
4052 execv(setup_script, args);
bellard4a389402005-11-26 20:10:07 +00004053 _exit(1);
bellard7c9d8e02005-11-15 22:16:05 +00004054 }
4055 while (waitpid(pid, &status, 0) != pid);
4056 if (!WIFEXITED(status) ||
4057 WEXITSTATUS(status) != 0) {
4058 fprintf(stderr, "%s: could not launch network script\n",
4059 setup_script);
4060 return -1;
4061 }
bellardc20709a2004-04-21 23:27:19 +00004062 }
thsb46a8902007-10-21 23:20:45 +00004063 return 0;
4064}
4065
4066static int net_tap_init(VLANState *vlan, const char *ifname1,
4067 const char *setup_script, const char *down_script)
4068{
4069 TAPState *s;
4070 int fd;
4071 char ifname[128];
4072
4073 if (ifname1 != NULL)
4074 pstrcpy(ifname, sizeof(ifname), ifname1);
4075 else
4076 ifname[0] = '\0';
4077 TFR(fd = tap_open(ifname, sizeof(ifname)));
4078 if (fd < 0)
4079 return -1;
4080
4081 if (!setup_script || !strcmp(setup_script, "no"))
4082 setup_script = "";
4083 if (setup_script[0] != '\0') {
4084 if (launch_script(setup_script, ifname, fd))
4085 return -1;
bellardc20709a2004-04-21 23:27:19 +00004086 }
bellard7c9d8e02005-11-15 22:16:05 +00004087 s = net_tap_fd_init(vlan, fd);
4088 if (!s)
4089 return -1;
ths5fafdf22007-09-16 21:08:06 +00004090 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
bellard7c9d8e02005-11-15 22:16:05 +00004091 "tap: ifname=%s setup_script=%s", ifname, setup_script);
thsb46a8902007-10-21 23:20:45 +00004092 if (down_script && strcmp(down_script, "no"))
4093 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
bellardc20709a2004-04-21 23:27:19 +00004094 return 0;
4095}
4096
bellardfd1dff42006-02-01 21:29:26 +00004097#endif /* !_WIN32 */
4098
bellard7c9d8e02005-11-15 22:16:05 +00004099/* network connection */
4100typedef struct NetSocketState {
4101 VLANClientState *vc;
4102 int fd;
4103 int state; /* 0 = getting length, 1 = getting data */
4104 int index;
4105 int packet_len;
4106 uint8_t buf[4096];
bellard3d830452005-12-18 16:36:49 +00004107 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
bellard7c9d8e02005-11-15 22:16:05 +00004108} NetSocketState;
4109
4110typedef struct NetSocketListenState {
4111 VLANState *vlan;
4112 int fd;
4113} NetSocketListenState;
4114
4115/* XXX: we consider we can send the whole packet without blocking */
4116static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
bellardc20709a2004-04-21 23:27:19 +00004117{
bellard7c9d8e02005-11-15 22:16:05 +00004118 NetSocketState *s = opaque;
4119 uint32_t len;
4120 len = htonl(size);
4121
bellardfd1dff42006-02-01 21:29:26 +00004122 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4123 send_all(s->fd, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00004124}
4125
bellard3d830452005-12-18 16:36:49 +00004126static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4127{
4128 NetSocketState *s = opaque;
ths5fafdf22007-09-16 21:08:06 +00004129 sendto(s->fd, buf, size, 0,
bellard3d830452005-12-18 16:36:49 +00004130 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4131}
4132
bellard7c9d8e02005-11-15 22:16:05 +00004133static void net_socket_send(void *opaque)
4134{
4135 NetSocketState *s = opaque;
bellardfd1dff42006-02-01 21:29:26 +00004136 int l, size, err;
bellard7c9d8e02005-11-15 22:16:05 +00004137 uint8_t buf1[4096];
4138 const uint8_t *buf;
4139
bellardfd1dff42006-02-01 21:29:26 +00004140 size = recv(s->fd, buf1, sizeof(buf1), 0);
4141 if (size < 0) {
4142 err = socket_error();
ths5fafdf22007-09-16 21:08:06 +00004143 if (err != EWOULDBLOCK)
bellardfd1dff42006-02-01 21:29:26 +00004144 goto eoc;
4145 } else if (size == 0) {
bellard7c9d8e02005-11-15 22:16:05 +00004146 /* end of connection */
bellardfd1dff42006-02-01 21:29:26 +00004147 eoc:
bellard7c9d8e02005-11-15 22:16:05 +00004148 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
bellardfd1dff42006-02-01 21:29:26 +00004149 closesocket(s->fd);
bellard7c9d8e02005-11-15 22:16:05 +00004150 return;
4151 }
4152 buf = buf1;
4153 while (size > 0) {
4154 /* reassemble a packet from the network */
4155 switch(s->state) {
4156 case 0:
4157 l = 4 - s->index;
4158 if (l > size)
4159 l = size;
4160 memcpy(s->buf + s->index, buf, l);
4161 buf += l;
4162 size -= l;
4163 s->index += l;
4164 if (s->index == 4) {
4165 /* got length */
4166 s->packet_len = ntohl(*(uint32_t *)s->buf);
4167 s->index = 0;
4168 s->state = 1;
4169 }
4170 break;
4171 case 1:
4172 l = s->packet_len - s->index;
4173 if (l > size)
4174 l = size;
4175 memcpy(s->buf + s->index, buf, l);
4176 s->index += l;
4177 buf += l;
4178 size -= l;
4179 if (s->index >= s->packet_len) {
4180 qemu_send_packet(s->vc, s->buf, s->packet_len);
4181 s->index = 0;
4182 s->state = 0;
4183 }
4184 break;
4185 }
4186 }
4187}
4188
bellard3d830452005-12-18 16:36:49 +00004189static void net_socket_send_dgram(void *opaque)
4190{
4191 NetSocketState *s = opaque;
4192 int size;
4193
4194 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
ths5fafdf22007-09-16 21:08:06 +00004195 if (size < 0)
bellard3d830452005-12-18 16:36:49 +00004196 return;
4197 if (size == 0) {
4198 /* end of connection */
4199 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4200 return;
4201 }
4202 qemu_send_packet(s->vc, s->buf, size);
4203}
4204
4205static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4206{
4207 struct ip_mreq imr;
4208 int fd;
4209 int val, ret;
4210 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4211 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
ths5fafdf22007-09-16 21:08:06 +00004212 inet_ntoa(mcastaddr->sin_addr),
bellardfd1dff42006-02-01 21:29:26 +00004213 (int)ntohl(mcastaddr->sin_addr.s_addr));
bellard3d830452005-12-18 16:36:49 +00004214 return -1;
4215
4216 }
4217 fd = socket(PF_INET, SOCK_DGRAM, 0);
4218 if (fd < 0) {
4219 perror("socket(PF_INET, SOCK_DGRAM)");
4220 return -1;
4221 }
4222
bellardfd1dff42006-02-01 21:29:26 +00004223 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004224 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
bellardfd1dff42006-02-01 21:29:26 +00004225 (const char *)&val, sizeof(val));
4226 if (ret < 0) {
4227 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4228 goto fail;
4229 }
4230
4231 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4232 if (ret < 0) {
4233 perror("bind");
4234 goto fail;
4235 }
ths3b46e622007-09-17 08:09:54 +00004236
bellard3d830452005-12-18 16:36:49 +00004237 /* Add host to multicast group */
4238 imr.imr_multiaddr = mcastaddr->sin_addr;
4239 imr.imr_interface.s_addr = htonl(INADDR_ANY);
4240
ths5fafdf22007-09-16 21:08:06 +00004241 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
bellardfd1dff42006-02-01 21:29:26 +00004242 (const char *)&imr, sizeof(struct ip_mreq));
bellard3d830452005-12-18 16:36:49 +00004243 if (ret < 0) {
4244 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4245 goto fail;
4246 }
4247
4248 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4249 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004250 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
bellardfd1dff42006-02-01 21:29:26 +00004251 (const char *)&val, sizeof(val));
bellard3d830452005-12-18 16:36:49 +00004252 if (ret < 0) {
4253 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4254 goto fail;
4255 }
4256
bellardfd1dff42006-02-01 21:29:26 +00004257 socket_set_nonblock(fd);
bellard3d830452005-12-18 16:36:49 +00004258 return fd;
4259fail:
ths5fafdf22007-09-16 21:08:06 +00004260 if (fd >= 0)
bellard0bab00f2006-06-25 14:49:44 +00004261 closesocket(fd);
bellard3d830452005-12-18 16:36:49 +00004262 return -1;
4263}
4264
ths5fafdf22007-09-16 21:08:06 +00004265static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004266 int is_connected)
4267{
4268 struct sockaddr_in saddr;
4269 int newfd;
4270 socklen_t saddr_len;
4271 NetSocketState *s;
4272
4273 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
ths5fafdf22007-09-16 21:08:06 +00004274 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
bellard3d830452005-12-18 16:36:49 +00004275 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4276 */
4277
4278 if (is_connected) {
4279 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4280 /* must be bound */
4281 if (saddr.sin_addr.s_addr==0) {
4282 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4283 fd);
4284 return NULL;
4285 }
4286 /* clone dgram socket */
4287 newfd = net_socket_mcast_create(&saddr);
4288 if (newfd < 0) {
4289 /* error already reported by net_socket_mcast_create() */
4290 close(fd);
4291 return NULL;
4292 }
4293 /* clone newfd to fd, close newfd */
4294 dup2(newfd, fd);
4295 close(newfd);
ths5fafdf22007-09-16 21:08:06 +00004296
bellard3d830452005-12-18 16:36:49 +00004297 } else {
4298 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4299 fd, strerror(errno));
4300 return NULL;
4301 }
4302 }
4303
4304 s = qemu_mallocz(sizeof(NetSocketState));
4305 if (!s)
4306 return NULL;
4307 s->fd = fd;
4308
pbrookd861b052006-02-04 22:15:28 +00004309 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
bellard3d830452005-12-18 16:36:49 +00004310 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4311
4312 /* mcast: save bound address as dst */
4313 if (is_connected) s->dgram_dst=saddr;
4314
4315 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004316 "socket: fd=%d (%s mcast=%s:%d)",
bellard3d830452005-12-18 16:36:49 +00004317 fd, is_connected? "cloned" : "",
4318 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4319 return s;
4320}
4321
bellard7c9d8e02005-11-15 22:16:05 +00004322static void net_socket_connect(void *opaque)
4323{
4324 NetSocketState *s = opaque;
4325 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4326}
4327
ths5fafdf22007-09-16 21:08:06 +00004328static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
bellard7c9d8e02005-11-15 22:16:05 +00004329 int is_connected)
4330{
4331 NetSocketState *s;
4332 s = qemu_mallocz(sizeof(NetSocketState));
4333 if (!s)
4334 return NULL;
4335 s->fd = fd;
ths5fafdf22007-09-16 21:08:06 +00004336 s->vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00004337 net_socket_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00004338 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4339 "socket: fd=%d", fd);
4340 if (is_connected) {
4341 net_socket_connect(s);
4342 } else {
4343 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4344 }
4345 return s;
4346}
4347
ths5fafdf22007-09-16 21:08:06 +00004348static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004349 int is_connected)
4350{
4351 int so_type=-1, optlen=sizeof(so_type);
4352
bellardfd1dff42006-02-01 21:29:26 +00004353 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) {
ths931f03e2007-04-28 20:49:36 +00004354 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
bellard3d830452005-12-18 16:36:49 +00004355 return NULL;
4356 }
4357 switch(so_type) {
4358 case SOCK_DGRAM:
4359 return net_socket_fd_init_dgram(vlan, fd, is_connected);
4360 case SOCK_STREAM:
4361 return net_socket_fd_init_stream(vlan, fd, is_connected);
4362 default:
4363 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4364 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4365 return net_socket_fd_init_stream(vlan, fd, is_connected);
4366 }
4367 return NULL;
4368}
4369
bellard7c9d8e02005-11-15 22:16:05 +00004370static void net_socket_accept(void *opaque)
4371{
ths3b46e622007-09-17 08:09:54 +00004372 NetSocketListenState *s = opaque;
bellard7c9d8e02005-11-15 22:16:05 +00004373 NetSocketState *s1;
4374 struct sockaddr_in saddr;
4375 socklen_t len;
4376 int fd;
4377
4378 for(;;) {
4379 len = sizeof(saddr);
4380 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4381 if (fd < 0 && errno != EINTR) {
4382 return;
4383 } else if (fd >= 0) {
4384 break;
4385 }
4386 }
ths5fafdf22007-09-16 21:08:06 +00004387 s1 = net_socket_fd_init(s->vlan, fd, 1);
bellard7c9d8e02005-11-15 22:16:05 +00004388 if (!s1) {
bellard0bab00f2006-06-25 14:49:44 +00004389 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004390 } else {
4391 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004392 "socket: connection from %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004393 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4394 }
4395}
4396
4397static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4398{
4399 NetSocketListenState *s;
4400 int fd, val, ret;
4401 struct sockaddr_in saddr;
4402
4403 if (parse_host_port(&saddr, host_str) < 0)
4404 return -1;
ths3b46e622007-09-17 08:09:54 +00004405
bellard7c9d8e02005-11-15 22:16:05 +00004406 s = qemu_mallocz(sizeof(NetSocketListenState));
4407 if (!s)
4408 return -1;
4409
4410 fd = socket(PF_INET, SOCK_STREAM, 0);
4411 if (fd < 0) {
4412 perror("socket");
4413 return -1;
4414 }
bellardfd1dff42006-02-01 21:29:26 +00004415 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004416
4417 /* allow fast reuse */
4418 val = 1;
bellardfd1dff42006-02-01 21:29:26 +00004419 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00004420
bellard7c9d8e02005-11-15 22:16:05 +00004421 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4422 if (ret < 0) {
4423 perror("bind");
4424 return -1;
4425 }
4426 ret = listen(fd, 0);
4427 if (ret < 0) {
4428 perror("listen");
4429 return -1;
4430 }
4431 s->vlan = vlan;
4432 s->fd = fd;
4433 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4434 return 0;
4435}
4436
4437static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4438{
4439 NetSocketState *s;
bellardfd1dff42006-02-01 21:29:26 +00004440 int fd, connected, ret, err;
bellard7c9d8e02005-11-15 22:16:05 +00004441 struct sockaddr_in saddr;
4442
4443 if (parse_host_port(&saddr, host_str) < 0)
4444 return -1;
4445
4446 fd = socket(PF_INET, SOCK_STREAM, 0);
4447 if (fd < 0) {
4448 perror("socket");
4449 return -1;
4450 }
bellardfd1dff42006-02-01 21:29:26 +00004451 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004452
4453 connected = 0;
4454 for(;;) {
4455 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4456 if (ret < 0) {
bellardfd1dff42006-02-01 21:29:26 +00004457 err = socket_error();
4458 if (err == EINTR || err == EWOULDBLOCK) {
4459 } else if (err == EINPROGRESS) {
bellard7c9d8e02005-11-15 22:16:05 +00004460 break;
thsf5b12262007-03-25 15:58:03 +00004461#ifdef _WIN32
4462 } else if (err == WSAEALREADY) {
4463 break;
4464#endif
bellard7c9d8e02005-11-15 22:16:05 +00004465 } else {
4466 perror("connect");
bellardfd1dff42006-02-01 21:29:26 +00004467 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004468 return -1;
4469 }
4470 } else {
4471 connected = 1;
4472 break;
4473 }
4474 }
4475 s = net_socket_fd_init(vlan, fd, connected);
4476 if (!s)
4477 return -1;
4478 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004479 "socket: connect to %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004480 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
bellardc20709a2004-04-21 23:27:19 +00004481 return 0;
4482}
4483
bellard3d830452005-12-18 16:36:49 +00004484static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4485{
4486 NetSocketState *s;
4487 int fd;
4488 struct sockaddr_in saddr;
4489
4490 if (parse_host_port(&saddr, host_str) < 0)
4491 return -1;
4492
4493
4494 fd = net_socket_mcast_create(&saddr);
4495 if (fd < 0)
4496 return -1;
4497
4498 s = net_socket_fd_init(vlan, fd, 0);
4499 if (!s)
4500 return -1;
4501
4502 s->dgram_dst = saddr;
ths3b46e622007-09-17 08:09:54 +00004503
bellard3d830452005-12-18 16:36:49 +00004504 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004505 "socket: mcast=%s:%d",
bellard3d830452005-12-18 16:36:49 +00004506 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4507 return 0;
4508
4509}
4510
bellard7c9d8e02005-11-15 22:16:05 +00004511static int get_param_value(char *buf, int buf_size,
4512 const char *tag, const char *str)
4513{
4514 const char *p;
4515 char *q;
4516 char option[128];
4517
4518 p = str;
4519 for(;;) {
4520 q = option;
4521 while (*p != '\0' && *p != '=') {
4522 if ((q - option) < sizeof(option) - 1)
4523 *q++ = *p;
4524 p++;
4525 }
4526 *q = '\0';
4527 if (*p != '=')
4528 break;
4529 p++;
4530 if (!strcmp(tag, option)) {
4531 q = buf;
4532 while (*p != '\0' && *p != ',') {
4533 if ((q - buf) < buf_size - 1)
4534 *q++ = *p;
4535 p++;
4536 }
4537 *q = '\0';
4538 return q - buf;
4539 } else {
4540 while (*p != '\0' && *p != ',') {
4541 p++;
4542 }
4543 }
4544 if (*p != ',')
4545 break;
4546 p++;
4547 }
4548 return 0;
4549}
4550
ths52f61fd2006-12-22 21:20:52 +00004551static int net_client_init(const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00004552{
4553 const char *p;
4554 char *q;
4555 char device[64];
4556 char buf[1024];
4557 int vlan_id, ret;
4558 VLANState *vlan;
4559
4560 p = str;
4561 q = device;
4562 while (*p != '\0' && *p != ',') {
4563 if ((q - device) < sizeof(device) - 1)
4564 *q++ = *p;
4565 p++;
4566 }
4567 *q = '\0';
4568 if (*p == ',')
4569 p++;
4570 vlan_id = 0;
4571 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
4572 vlan_id = strtol(buf, NULL, 0);
4573 }
4574 vlan = qemu_find_vlan(vlan_id);
4575 if (!vlan) {
4576 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
4577 return -1;
4578 }
4579 if (!strcmp(device, "nic")) {
4580 NICInfo *nd;
4581 uint8_t *macaddr;
4582
4583 if (nb_nics >= MAX_NICS) {
4584 fprintf(stderr, "Too Many NICs\n");
4585 return -1;
4586 }
4587 nd = &nd_table[nb_nics];
4588 macaddr = nd->macaddr;
4589 macaddr[0] = 0x52;
4590 macaddr[1] = 0x54;
4591 macaddr[2] = 0x00;
4592 macaddr[3] = 0x12;
4593 macaddr[4] = 0x34;
4594 macaddr[5] = 0x56 + nb_nics;
4595
4596 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
4597 if (parse_macaddr(macaddr, buf) < 0) {
4598 fprintf(stderr, "invalid syntax for ethernet address\n");
4599 return -1;
4600 }
4601 }
pbrooka41b2ff2006-02-05 04:14:41 +00004602 if (get_param_value(buf, sizeof(buf), "model", p)) {
4603 nd->model = strdup(buf);
4604 }
bellard7c9d8e02005-11-15 22:16:05 +00004605 nd->vlan = vlan;
4606 nb_nics++;
blueswir1833c7172007-05-27 19:36:43 +00004607 vlan->nb_guest_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004608 ret = 0;
4609 } else
4610 if (!strcmp(device, "none")) {
4611 /* does nothing. It is needed to signal that no network cards
4612 are wanted */
4613 ret = 0;
4614 } else
4615#ifdef CONFIG_SLIRP
4616 if (!strcmp(device, "user")) {
pbrook115defd2006-04-16 11:06:58 +00004617 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
bellard3f423c92006-04-30 21:34:15 +00004618 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
pbrook115defd2006-04-16 11:06:58 +00004619 }
blueswir1833c7172007-05-27 19:36:43 +00004620 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004621 ret = net_slirp_init(vlan);
4622 } else
4623#endif
bellard7fb843f2006-02-01 23:06:55 +00004624#ifdef _WIN32
4625 if (!strcmp(device, "tap")) {
4626 char ifname[64];
4627 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4628 fprintf(stderr, "tap: no interface name\n");
4629 return -1;
4630 }
blueswir1833c7172007-05-27 19:36:43 +00004631 vlan->nb_host_devs++;
bellard7fb843f2006-02-01 23:06:55 +00004632 ret = tap_win32_init(vlan, ifname);
4633 } else
4634#else
bellard7c9d8e02005-11-15 22:16:05 +00004635 if (!strcmp(device, "tap")) {
4636 char ifname[64];
thsb46a8902007-10-21 23:20:45 +00004637 char setup_script[1024], down_script[1024];
bellard7c9d8e02005-11-15 22:16:05 +00004638 int fd;
pbrook4f010352007-05-28 02:29:59 +00004639 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004640 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4641 fd = strtol(buf, NULL, 0);
4642 ret = -1;
4643 if (net_tap_fd_init(vlan, fd))
4644 ret = 0;
4645 } else {
bellardbf8c5342007-01-09 19:44:41 +00004646 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4647 ifname[0] = '\0';
4648 }
bellard7c9d8e02005-11-15 22:16:05 +00004649 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
4650 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
4651 }
thsb46a8902007-10-21 23:20:45 +00004652 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
4653 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
4654 }
4655 ret = net_tap_init(vlan, ifname, setup_script, down_script);
bellard7c9d8e02005-11-15 22:16:05 +00004656 }
4657 } else
bellardfd1dff42006-02-01 21:29:26 +00004658#endif
bellard7c9d8e02005-11-15 22:16:05 +00004659 if (!strcmp(device, "socket")) {
4660 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4661 int fd;
4662 fd = strtol(buf, NULL, 0);
4663 ret = -1;
4664 if (net_socket_fd_init(vlan, fd, 1))
4665 ret = 0;
4666 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
4667 ret = net_socket_listen_init(vlan, buf);
4668 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
4669 ret = net_socket_connect_init(vlan, buf);
bellard3d830452005-12-18 16:36:49 +00004670 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
4671 ret = net_socket_mcast_init(vlan, buf);
bellard7c9d8e02005-11-15 22:16:05 +00004672 } else {
4673 fprintf(stderr, "Unknown socket options: %s\n", p);
4674 return -1;
4675 }
blueswir1833c7172007-05-27 19:36:43 +00004676 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004677 } else
bellard7c9d8e02005-11-15 22:16:05 +00004678 {
4679 fprintf(stderr, "Unknown network device: %s\n", device);
4680 return -1;
4681 }
4682 if (ret < 0) {
4683 fprintf(stderr, "Could not initialize device '%s'\n", device);
4684 }
ths3b46e622007-09-17 08:09:54 +00004685
bellard7c9d8e02005-11-15 22:16:05 +00004686 return ret;
4687}
4688
4689void do_info_network(void)
4690{
4691 VLANState *vlan;
4692 VLANClientState *vc;
4693
4694 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
4695 term_printf("VLAN %d devices:\n", vlan->id);
4696 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
4697 term_printf(" %s\n", vc->info_str);
4698 }
4699}
ths42550fd2006-12-22 16:34:12 +00004700
bellard330d0412003-07-26 18:11:40 +00004701/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00004702/* USB devices */
4703
pbrook0d92ed32006-05-21 16:30:15 +00004704static USBPort *used_usb_ports;
4705static USBPort *free_usb_ports;
4706
4707/* ??? Maybe change this to register a hub to keep track of the topology. */
4708void qemu_register_usb_port(USBPort *port, void *opaque, int index,
4709 usb_attachfn attach)
4710{
4711 port->opaque = opaque;
4712 port->index = index;
4713 port->attach = attach;
4714 port->next = free_usb_ports;
4715 free_usb_ports = port;
4716}
4717
bellarda594cfb2005-11-06 16:13:29 +00004718static int usb_device_add(const char *devname)
4719{
4720 const char *p;
4721 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004722 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00004723
pbrook0d92ed32006-05-21 16:30:15 +00004724 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00004725 return -1;
4726
4727 if (strstart(devname, "host:", &p)) {
4728 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00004729 } else if (!strcmp(devname, "mouse")) {
4730 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00004731 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00004732 dev = usb_tablet_init();
4733 } else if (!strcmp(devname, "keyboard")) {
4734 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00004735 } else if (strstart(devname, "disk:", &p)) {
4736 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00004737 } else if (!strcmp(devname, "wacom-tablet")) {
4738 dev = usb_wacom_init();
bellarda594cfb2005-11-06 16:13:29 +00004739 } else {
4740 return -1;
4741 }
pbrook0d92ed32006-05-21 16:30:15 +00004742 if (!dev)
4743 return -1;
4744
4745 /* Find a USB port to add the device to. */
4746 port = free_usb_ports;
4747 if (!port->next) {
4748 USBDevice *hub;
4749
4750 /* Create a new hub and chain it on. */
4751 free_usb_ports = NULL;
4752 port->next = used_usb_ports;
4753 used_usb_ports = port;
4754
4755 hub = usb_hub_init(VM_USB_HUB_SIZE);
4756 usb_attach(port, hub);
4757 port = free_usb_ports;
4758 }
4759
4760 free_usb_ports = port->next;
4761 port->next = used_usb_ports;
4762 used_usb_ports = port;
4763 usb_attach(port, dev);
bellarda594cfb2005-11-06 16:13:29 +00004764 return 0;
4765}
4766
4767static int usb_device_del(const char *devname)
4768{
pbrook0d92ed32006-05-21 16:30:15 +00004769 USBPort *port;
4770 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00004771 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004772 int bus_num, addr;
bellarda594cfb2005-11-06 16:13:29 +00004773 const char *p;
4774
pbrook0d92ed32006-05-21 16:30:15 +00004775 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00004776 return -1;
4777
4778 p = strchr(devname, '.');
ths5fafdf22007-09-16 21:08:06 +00004779 if (!p)
bellarda594cfb2005-11-06 16:13:29 +00004780 return -1;
4781 bus_num = strtoul(devname, NULL, 0);
4782 addr = strtoul(p + 1, NULL, 0);
4783 if (bus_num != 0)
4784 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00004785
4786 lastp = &used_usb_ports;
4787 port = used_usb_ports;
4788 while (port && port->dev->addr != addr) {
4789 lastp = &port->next;
4790 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00004791 }
pbrook0d92ed32006-05-21 16:30:15 +00004792
4793 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00004794 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00004795
bellard059809e2006-07-19 18:06:15 +00004796 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00004797 *lastp = port->next;
4798 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00004799 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00004800 port->next = free_usb_ports;
4801 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00004802 return 0;
4803}
4804
4805void do_usb_add(const char *devname)
4806{
4807 int ret;
4808 ret = usb_device_add(devname);
ths5fafdf22007-09-16 21:08:06 +00004809 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00004810 term_printf("Could not add USB device '%s'\n", devname);
4811}
4812
4813void do_usb_del(const char *devname)
4814{
4815 int ret;
4816 ret = usb_device_del(devname);
ths5fafdf22007-09-16 21:08:06 +00004817 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00004818 term_printf("Could not remove USB device '%s'\n", devname);
4819}
4820
4821void usb_info(void)
4822{
4823 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004824 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00004825 const char *speed_str;
4826
pbrook0d92ed32006-05-21 16:30:15 +00004827 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00004828 term_printf("USB support not enabled\n");
4829 return;
4830 }
4831
pbrook0d92ed32006-05-21 16:30:15 +00004832 for (port = used_usb_ports; port; port = port->next) {
4833 dev = port->dev;
4834 if (!dev)
4835 continue;
4836 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00004837 case USB_SPEED_LOW:
4838 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00004839 break;
ths5fafdf22007-09-16 21:08:06 +00004840 case USB_SPEED_FULL:
4841 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00004842 break;
ths5fafdf22007-09-16 21:08:06 +00004843 case USB_SPEED_HIGH:
4844 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00004845 break;
4846 default:
ths5fafdf22007-09-16 21:08:06 +00004847 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00004848 break;
bellarda594cfb2005-11-06 16:13:29 +00004849 }
ths5fafdf22007-09-16 21:08:06 +00004850 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00004851 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00004852 }
4853}
4854
bellardf7cce892004-12-08 22:21:25 +00004855/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00004856/* PCMCIA/Cardbus */
4857
4858static struct pcmcia_socket_entry_s {
4859 struct pcmcia_socket_s *socket;
4860 struct pcmcia_socket_entry_s *next;
4861} *pcmcia_sockets = 0;
4862
4863void pcmcia_socket_register(struct pcmcia_socket_s *socket)
4864{
4865 struct pcmcia_socket_entry_s *entry;
4866
4867 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
4868 entry->socket = socket;
4869 entry->next = pcmcia_sockets;
4870 pcmcia_sockets = entry;
4871}
4872
4873void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
4874{
4875 struct pcmcia_socket_entry_s *entry, **ptr;
4876
4877 ptr = &pcmcia_sockets;
4878 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
4879 if (entry->socket == socket) {
4880 *ptr = entry->next;
4881 qemu_free(entry);
4882 }
4883}
4884
4885void pcmcia_info(void)
4886{
4887 struct pcmcia_socket_entry_s *iter;
4888 if (!pcmcia_sockets)
4889 term_printf("No PCMCIA sockets\n");
4890
4891 for (iter = pcmcia_sockets; iter; iter = iter->next)
4892 term_printf("%s: %s\n", iter->socket->slot_string,
4893 iter->socket->attached ? iter->socket->card_string :
4894 "Empty");
4895}
4896
4897/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00004898/* dumb display */
4899
4900static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
4901{
4902}
4903
4904static void dumb_resize(DisplayState *ds, int w, int h)
4905{
4906}
4907
4908static void dumb_refresh(DisplayState *ds)
4909{
4910#if defined(CONFIG_SDL)
4911 vga_hw_update();
4912#endif
4913}
4914
4915static void dumb_display_init(DisplayState *ds)
4916{
4917 ds->data = NULL;
4918 ds->linesize = 0;
4919 ds->depth = 0;
4920 ds->dpy_update = dumb_update;
4921 ds->dpy_resize = dumb_resize;
4922 ds->dpy_refresh = dumb_refresh;
4923}
4924
4925/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00004926/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00004927
bellardc4b1fcc2004-03-14 21:44:30 +00004928#define MAX_IO_HANDLERS 64
4929
4930typedef struct IOHandlerRecord {
4931 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00004932 IOCanRWHandler *fd_read_poll;
4933 IOHandler *fd_read;
4934 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00004935 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00004936 void *opaque;
4937 /* temporary data */
4938 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00004939 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00004940} IOHandlerRecord;
4941
bellard8a7ddc32004-03-31 19:00:16 +00004942static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00004943
bellard7c9d8e02005-11-15 22:16:05 +00004944/* XXX: fd_read_poll should be suppressed, but an API change is
4945 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00004946int qemu_set_fd_handler2(int fd,
4947 IOCanRWHandler *fd_read_poll,
4948 IOHandler *fd_read,
4949 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00004950 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00004951{
bellard8a7ddc32004-03-31 19:00:16 +00004952 IOHandlerRecord **pioh, *ioh;
4953
bellard7c9d8e02005-11-15 22:16:05 +00004954 if (!fd_read && !fd_write) {
4955 pioh = &first_io_handler;
4956 for(;;) {
4957 ioh = *pioh;
4958 if (ioh == NULL)
4959 break;
4960 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00004961 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00004962 break;
4963 }
4964 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00004965 }
bellard7c9d8e02005-11-15 22:16:05 +00004966 } else {
4967 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4968 if (ioh->fd == fd)
4969 goto found;
4970 }
4971 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
4972 if (!ioh)
4973 return -1;
4974 ioh->next = first_io_handler;
4975 first_io_handler = ioh;
4976 found:
4977 ioh->fd = fd;
4978 ioh->fd_read_poll = fd_read_poll;
4979 ioh->fd_read = fd_read;
4980 ioh->fd_write = fd_write;
4981 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00004982 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00004983 }
bellard7c9d8e02005-11-15 22:16:05 +00004984 return 0;
4985}
4986
ths5fafdf22007-09-16 21:08:06 +00004987int qemu_set_fd_handler(int fd,
4988 IOHandler *fd_read,
4989 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00004990 void *opaque)
4991{
4992 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00004993}
4994
bellard8a7ddc32004-03-31 19:00:16 +00004995/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00004996/* Polling handling */
4997
4998typedef struct PollingEntry {
4999 PollingFunc *func;
5000 void *opaque;
5001 struct PollingEntry *next;
5002} PollingEntry;
5003
5004static PollingEntry *first_polling_entry;
5005
5006int qemu_add_polling_cb(PollingFunc *func, void *opaque)
5007{
5008 PollingEntry **ppe, *pe;
5009 pe = qemu_mallocz(sizeof(PollingEntry));
5010 if (!pe)
5011 return -1;
5012 pe->func = func;
5013 pe->opaque = opaque;
5014 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
5015 *ppe = pe;
5016 return 0;
5017}
5018
5019void qemu_del_polling_cb(PollingFunc *func, void *opaque)
5020{
5021 PollingEntry **ppe, *pe;
5022 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
5023 pe = *ppe;
5024 if (pe->func == func && pe->opaque == opaque) {
5025 *ppe = pe->next;
5026 qemu_free(pe);
5027 break;
5028 }
5029 }
5030}
5031
bellarda18e5242006-06-25 17:18:27 +00005032#ifdef _WIN32
5033/***********************************************************/
5034/* Wait objects support */
5035typedef struct WaitObjects {
5036 int num;
5037 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
5038 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
5039 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
5040} WaitObjects;
5041
5042static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00005043
bellarda18e5242006-06-25 17:18:27 +00005044int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5045{
5046 WaitObjects *w = &wait_objects;
5047
5048 if (w->num >= MAXIMUM_WAIT_OBJECTS)
5049 return -1;
5050 w->events[w->num] = handle;
5051 w->func[w->num] = func;
5052 w->opaque[w->num] = opaque;
5053 w->num++;
5054 return 0;
5055}
5056
5057void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5058{
5059 int i, found;
5060 WaitObjects *w = &wait_objects;
5061
5062 found = 0;
5063 for (i = 0; i < w->num; i++) {
5064 if (w->events[i] == handle)
5065 found = 1;
5066 if (found) {
5067 w->events[i] = w->events[i + 1];
5068 w->func[i] = w->func[i + 1];
5069 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00005070 }
bellarda18e5242006-06-25 17:18:27 +00005071 }
5072 if (found)
5073 w->num--;
5074}
5075#endif
5076
bellardf3311102006-04-12 20:21:17 +00005077/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00005078/* savevm/loadvm support */
bellardb4608c02003-06-27 17:34:32 +00005079
bellardfaea38e2006-08-05 21:31:00 +00005080#define IO_BUF_SIZE 32768
5081
5082struct QEMUFile {
5083 FILE *outfile;
5084 BlockDriverState *bs;
5085 int is_file;
5086 int is_writable;
5087 int64_t base_offset;
5088 int64_t buf_offset; /* start of buffer when writing, end of buffer
5089 when reading */
5090 int buf_index;
5091 int buf_size; /* 0 when writing */
5092 uint8_t buf[IO_BUF_SIZE];
5093};
5094
5095QEMUFile *qemu_fopen(const char *filename, const char *mode)
5096{
5097 QEMUFile *f;
5098
5099 f = qemu_mallocz(sizeof(QEMUFile));
5100 if (!f)
5101 return NULL;
5102 if (!strcmp(mode, "wb")) {
5103 f->is_writable = 1;
5104 } else if (!strcmp(mode, "rb")) {
5105 f->is_writable = 0;
5106 } else {
5107 goto fail;
5108 }
5109 f->outfile = fopen(filename, mode);
5110 if (!f->outfile)
5111 goto fail;
5112 f->is_file = 1;
5113 return f;
5114 fail:
5115 if (f->outfile)
5116 fclose(f->outfile);
5117 qemu_free(f);
5118 return NULL;
5119}
5120
5121QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
5122{
5123 QEMUFile *f;
5124
5125 f = qemu_mallocz(sizeof(QEMUFile));
5126 if (!f)
5127 return NULL;
5128 f->is_file = 0;
5129 f->bs = bs;
5130 f->is_writable = is_writable;
5131 f->base_offset = offset;
5132 return f;
5133}
5134
5135void qemu_fflush(QEMUFile *f)
5136{
5137 if (!f->is_writable)
5138 return;
5139 if (f->buf_index > 0) {
5140 if (f->is_file) {
5141 fseek(f->outfile, f->buf_offset, SEEK_SET);
5142 fwrite(f->buf, 1, f->buf_index, f->outfile);
5143 } else {
ths5fafdf22007-09-16 21:08:06 +00005144 bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005145 f->buf, f->buf_index);
5146 }
5147 f->buf_offset += f->buf_index;
5148 f->buf_index = 0;
5149 }
5150}
5151
5152static void qemu_fill_buffer(QEMUFile *f)
5153{
5154 int len;
5155
5156 if (f->is_writable)
5157 return;
5158 if (f->is_file) {
5159 fseek(f->outfile, f->buf_offset, SEEK_SET);
5160 len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
5161 if (len < 0)
5162 len = 0;
5163 } else {
ths5fafdf22007-09-16 21:08:06 +00005164 len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005165 f->buf, IO_BUF_SIZE);
5166 if (len < 0)
5167 len = 0;
5168 }
5169 f->buf_index = 0;
5170 f->buf_size = len;
5171 f->buf_offset += len;
5172}
5173
5174void qemu_fclose(QEMUFile *f)
5175{
5176 if (f->is_writable)
5177 qemu_fflush(f);
5178 if (f->is_file) {
5179 fclose(f->outfile);
5180 }
5181 qemu_free(f);
5182}
5183
bellard8a7ddc32004-03-31 19:00:16 +00005184void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
5185{
bellardfaea38e2006-08-05 21:31:00 +00005186 int l;
5187 while (size > 0) {
5188 l = IO_BUF_SIZE - f->buf_index;
5189 if (l > size)
5190 l = size;
5191 memcpy(f->buf + f->buf_index, buf, l);
5192 f->buf_index += l;
5193 buf += l;
5194 size -= l;
5195 if (f->buf_index >= IO_BUF_SIZE)
5196 qemu_fflush(f);
5197 }
bellard8a7ddc32004-03-31 19:00:16 +00005198}
5199
5200void qemu_put_byte(QEMUFile *f, int v)
5201{
bellardfaea38e2006-08-05 21:31:00 +00005202 f->buf[f->buf_index++] = v;
5203 if (f->buf_index >= IO_BUF_SIZE)
5204 qemu_fflush(f);
5205}
5206
5207int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
5208{
5209 int size, l;
5210
5211 size = size1;
5212 while (size > 0) {
5213 l = f->buf_size - f->buf_index;
5214 if (l == 0) {
5215 qemu_fill_buffer(f);
5216 l = f->buf_size - f->buf_index;
5217 if (l == 0)
5218 break;
5219 }
5220 if (l > size)
5221 l = size;
5222 memcpy(buf, f->buf + f->buf_index, l);
5223 f->buf_index += l;
5224 buf += l;
5225 size -= l;
5226 }
5227 return size1 - size;
5228}
5229
5230int qemu_get_byte(QEMUFile *f)
5231{
5232 if (f->buf_index >= f->buf_size) {
5233 qemu_fill_buffer(f);
5234 if (f->buf_index >= f->buf_size)
5235 return 0;
5236 }
5237 return f->buf[f->buf_index++];
5238}
5239
5240int64_t qemu_ftell(QEMUFile *f)
5241{
5242 return f->buf_offset - f->buf_size + f->buf_index;
5243}
5244
5245int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
5246{
5247 if (whence == SEEK_SET) {
5248 /* nothing to do */
5249 } else if (whence == SEEK_CUR) {
5250 pos += qemu_ftell(f);
5251 } else {
5252 /* SEEK_END not supported */
5253 return -1;
5254 }
5255 if (f->is_writable) {
5256 qemu_fflush(f);
5257 f->buf_offset = pos;
5258 } else {
5259 f->buf_offset = pos;
5260 f->buf_index = 0;
5261 f->buf_size = 0;
5262 }
5263 return pos;
bellard8a7ddc32004-03-31 19:00:16 +00005264}
5265
5266void qemu_put_be16(QEMUFile *f, unsigned int v)
5267{
5268 qemu_put_byte(f, v >> 8);
5269 qemu_put_byte(f, v);
5270}
5271
5272void qemu_put_be32(QEMUFile *f, unsigned int v)
5273{
5274 qemu_put_byte(f, v >> 24);
5275 qemu_put_byte(f, v >> 16);
5276 qemu_put_byte(f, v >> 8);
5277 qemu_put_byte(f, v);
5278}
5279
5280void qemu_put_be64(QEMUFile *f, uint64_t v)
5281{
5282 qemu_put_be32(f, v >> 32);
5283 qemu_put_be32(f, v);
5284}
5285
bellard8a7ddc32004-03-31 19:00:16 +00005286unsigned int qemu_get_be16(QEMUFile *f)
5287{
5288 unsigned int v;
5289 v = qemu_get_byte(f) << 8;
5290 v |= qemu_get_byte(f);
5291 return v;
5292}
5293
5294unsigned int qemu_get_be32(QEMUFile *f)
5295{
5296 unsigned int v;
5297 v = qemu_get_byte(f) << 24;
5298 v |= qemu_get_byte(f) << 16;
5299 v |= qemu_get_byte(f) << 8;
5300 v |= qemu_get_byte(f);
5301 return v;
5302}
5303
5304uint64_t qemu_get_be64(QEMUFile *f)
5305{
5306 uint64_t v;
5307 v = (uint64_t)qemu_get_be32(f) << 32;
5308 v |= qemu_get_be32(f);
5309 return v;
5310}
5311
bellard8a7ddc32004-03-31 19:00:16 +00005312typedef struct SaveStateEntry {
5313 char idstr[256];
5314 int instance_id;
5315 int version_id;
5316 SaveStateHandler *save_state;
5317 LoadStateHandler *load_state;
5318 void *opaque;
5319 struct SaveStateEntry *next;
5320} SaveStateEntry;
5321
5322static SaveStateEntry *first_se;
5323
ths5fafdf22007-09-16 21:08:06 +00005324int register_savevm(const char *idstr,
5325 int instance_id,
bellard8a7ddc32004-03-31 19:00:16 +00005326 int version_id,
5327 SaveStateHandler *save_state,
5328 LoadStateHandler *load_state,
5329 void *opaque)
5330{
5331 SaveStateEntry *se, **pse;
5332
5333 se = qemu_malloc(sizeof(SaveStateEntry));
5334 if (!se)
5335 return -1;
5336 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
5337 se->instance_id = instance_id;
5338 se->version_id = version_id;
5339 se->save_state = save_state;
5340 se->load_state = load_state;
5341 se->opaque = opaque;
5342 se->next = NULL;
5343
5344 /* add at the end of list */
5345 pse = &first_se;
5346 while (*pse != NULL)
5347 pse = &(*pse)->next;
5348 *pse = se;
5349 return 0;
5350}
5351
5352#define QEMU_VM_FILE_MAGIC 0x5145564d
bellardfaea38e2006-08-05 21:31:00 +00005353#define QEMU_VM_FILE_VERSION 0x00000002
bellard8a7ddc32004-03-31 19:00:16 +00005354
bellardfaea38e2006-08-05 21:31:00 +00005355int qemu_savevm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005356{
5357 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005358 int len, ret;
5359 int64_t cur_pos, len_pos, total_len_pos;
bellard313aa562003-08-10 21:52:11 +00005360
bellard8a7ddc32004-03-31 19:00:16 +00005361 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
5362 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
bellardfaea38e2006-08-05 21:31:00 +00005363 total_len_pos = qemu_ftell(f);
5364 qemu_put_be64(f, 0); /* total size */
bellard8a7ddc32004-03-31 19:00:16 +00005365
5366 for(se = first_se; se != NULL; se = se->next) {
5367 /* ID string */
5368 len = strlen(se->idstr);
5369 qemu_put_byte(f, len);
5370 qemu_put_buffer(f, se->idstr, len);
5371
5372 qemu_put_be32(f, se->instance_id);
5373 qemu_put_be32(f, se->version_id);
5374
5375 /* record size: filled later */
bellardfaea38e2006-08-05 21:31:00 +00005376 len_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005377 qemu_put_be32(f, 0);
ths3b46e622007-09-17 08:09:54 +00005378
bellard8a7ddc32004-03-31 19:00:16 +00005379 se->save_state(f, se->opaque);
5380
5381 /* fill record size */
bellardfaea38e2006-08-05 21:31:00 +00005382 cur_pos = qemu_ftell(f);
5383 len = cur_pos - len_pos - 4;
5384 qemu_fseek(f, len_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005385 qemu_put_be32(f, len);
bellardfaea38e2006-08-05 21:31:00 +00005386 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005387 }
bellardfaea38e2006-08-05 21:31:00 +00005388 cur_pos = qemu_ftell(f);
5389 qemu_fseek(f, total_len_pos, SEEK_SET);
5390 qemu_put_be64(f, cur_pos - total_len_pos - 8);
5391 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005392
bellard8a7ddc32004-03-31 19:00:16 +00005393 ret = 0;
bellard8a7ddc32004-03-31 19:00:16 +00005394 return ret;
5395}
5396
5397static SaveStateEntry *find_se(const char *idstr, int instance_id)
5398{
5399 SaveStateEntry *se;
5400
5401 for(se = first_se; se != NULL; se = se->next) {
ths5fafdf22007-09-16 21:08:06 +00005402 if (!strcmp(se->idstr, idstr) &&
bellard8a7ddc32004-03-31 19:00:16 +00005403 instance_id == se->instance_id)
5404 return se;
5405 }
5406 return NULL;
5407}
5408
bellardfaea38e2006-08-05 21:31:00 +00005409int qemu_loadvm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005410{
5411 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005412 int len, ret, instance_id, record_len, version_id;
5413 int64_t total_len, end_pos, cur_pos;
bellard8a7ddc32004-03-31 19:00:16 +00005414 unsigned int v;
5415 char idstr[256];
ths3b46e622007-09-17 08:09:54 +00005416
bellard8a7ddc32004-03-31 19:00:16 +00005417 v = qemu_get_be32(f);
5418 if (v != QEMU_VM_FILE_MAGIC)
5419 goto fail;
5420 v = qemu_get_be32(f);
5421 if (v != QEMU_VM_FILE_VERSION) {
5422 fail:
bellard8a7ddc32004-03-31 19:00:16 +00005423 ret = -1;
5424 goto the_end;
5425 }
bellardfaea38e2006-08-05 21:31:00 +00005426 total_len = qemu_get_be64(f);
5427 end_pos = total_len + qemu_ftell(f);
bellardb4608c02003-06-27 17:34:32 +00005428 for(;;) {
bellardfaea38e2006-08-05 21:31:00 +00005429 if (qemu_ftell(f) >= end_pos)
bellard8a7ddc32004-03-31 19:00:16 +00005430 break;
bellardfaea38e2006-08-05 21:31:00 +00005431 len = qemu_get_byte(f);
bellard8a7ddc32004-03-31 19:00:16 +00005432 qemu_get_buffer(f, idstr, len);
5433 idstr[len] = '\0';
5434 instance_id = qemu_get_be32(f);
5435 version_id = qemu_get_be32(f);
5436 record_len = qemu_get_be32(f);
5437#if 0
ths5fafdf22007-09-16 21:08:06 +00005438 printf("idstr=%s instance=0x%x version=%d len=%d\n",
bellard8a7ddc32004-03-31 19:00:16 +00005439 idstr, instance_id, version_id, record_len);
bellardc45886d2004-01-05 00:02:06 +00005440#endif
bellardfaea38e2006-08-05 21:31:00 +00005441 cur_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005442 se = find_se(idstr, instance_id);
5443 if (!se) {
ths5fafdf22007-09-16 21:08:06 +00005444 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
bellard8a7ddc32004-03-31 19:00:16 +00005445 instance_id, idstr);
5446 } else {
5447 ret = se->load_state(f, se->opaque, version_id);
5448 if (ret < 0) {
ths5fafdf22007-09-16 21:08:06 +00005449 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
bellard8a7ddc32004-03-31 19:00:16 +00005450 instance_id, idstr);
5451 }
bellard34865132003-10-05 14:28:56 +00005452 }
bellard8a7ddc32004-03-31 19:00:16 +00005453 /* always seek to exact end of record */
5454 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
5455 }
bellard8a7ddc32004-03-31 19:00:16 +00005456 ret = 0;
5457 the_end:
bellardfaea38e2006-08-05 21:31:00 +00005458 return ret;
5459}
5460
5461/* device can contain snapshots */
5462static int bdrv_can_snapshot(BlockDriverState *bs)
5463{
5464 return (bs &&
5465 !bdrv_is_removable(bs) &&
5466 !bdrv_is_read_only(bs));
5467}
5468
5469/* device must be snapshots in order to have a reliable snapshot */
5470static int bdrv_has_snapshot(BlockDriverState *bs)
5471{
5472 return (bs &&
5473 !bdrv_is_removable(bs) &&
5474 !bdrv_is_read_only(bs));
5475}
5476
5477static BlockDriverState *get_bs_snapshots(void)
5478{
5479 BlockDriverState *bs;
5480 int i;
5481
5482 if (bs_snapshots)
5483 return bs_snapshots;
5484 for(i = 0; i <= MAX_DISKS; i++) {
5485 bs = bs_table[i];
5486 if (bdrv_can_snapshot(bs))
5487 goto ok;
5488 }
5489 return NULL;
5490 ok:
5491 bs_snapshots = bs;
5492 return bs;
5493}
5494
5495static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
5496 const char *name)
5497{
5498 QEMUSnapshotInfo *sn_tab, *sn;
5499 int nb_sns, i, ret;
ths3b46e622007-09-17 08:09:54 +00005500
bellardfaea38e2006-08-05 21:31:00 +00005501 ret = -ENOENT;
5502 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5503 if (nb_sns < 0)
5504 return ret;
5505 for(i = 0; i < nb_sns; i++) {
5506 sn = &sn_tab[i];
5507 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
5508 *sn_info = *sn;
5509 ret = 0;
5510 break;
5511 }
5512 }
5513 qemu_free(sn_tab);
5514 return ret;
5515}
5516
5517void do_savevm(const char *name)
5518{
5519 BlockDriverState *bs, *bs1;
5520 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
5521 int must_delete, ret, i;
5522 BlockDriverInfo bdi1, *bdi = &bdi1;
5523 QEMUFile *f;
5524 int saved_vm_running;
bellard4c279bd2006-08-17 09:43:50 +00005525#ifdef _WIN32
5526 struct _timeb tb;
5527#else
bellardfaea38e2006-08-05 21:31:00 +00005528 struct timeval tv;
bellard4c279bd2006-08-17 09:43:50 +00005529#endif
bellardfaea38e2006-08-05 21:31:00 +00005530
5531 bs = get_bs_snapshots();
5532 if (!bs) {
5533 term_printf("No block device can accept snapshots\n");
5534 return;
5535 }
5536
pbrook6192bc32006-09-03 12:08:37 +00005537 /* ??? Should this occur after vm_stop? */
5538 qemu_aio_flush();
5539
bellardfaea38e2006-08-05 21:31:00 +00005540 saved_vm_running = vm_running;
5541 vm_stop(0);
ths3b46e622007-09-17 08:09:54 +00005542
bellardfaea38e2006-08-05 21:31:00 +00005543 must_delete = 0;
5544 if (name) {
5545 ret = bdrv_snapshot_find(bs, old_sn, name);
5546 if (ret >= 0) {
5547 must_delete = 1;
5548 }
5549 }
5550 memset(sn, 0, sizeof(*sn));
5551 if (must_delete) {
5552 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
5553 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
5554 } else {
5555 if (name)
5556 pstrcpy(sn->name, sizeof(sn->name), name);
5557 }
5558
5559 /* fill auxiliary fields */
bellard4c279bd2006-08-17 09:43:50 +00005560#ifdef _WIN32
5561 _ftime(&tb);
5562 sn->date_sec = tb.time;
5563 sn->date_nsec = tb.millitm * 1000000;
5564#else
bellardfaea38e2006-08-05 21:31:00 +00005565 gettimeofday(&tv, NULL);
5566 sn->date_sec = tv.tv_sec;
5567 sn->date_nsec = tv.tv_usec * 1000;
bellard4c279bd2006-08-17 09:43:50 +00005568#endif
bellardfaea38e2006-08-05 21:31:00 +00005569 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
ths3b46e622007-09-17 08:09:54 +00005570
bellardfaea38e2006-08-05 21:31:00 +00005571 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5572 term_printf("Device %s does not support VM state snapshots\n",
5573 bdrv_get_device_name(bs));
5574 goto the_end;
5575 }
ths3b46e622007-09-17 08:09:54 +00005576
bellardfaea38e2006-08-05 21:31:00 +00005577 /* save the VM state */
5578 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
5579 if (!f) {
5580 term_printf("Could not open VM state file\n");
5581 goto the_end;
5582 }
5583 ret = qemu_savevm_state(f);
5584 sn->vm_state_size = qemu_ftell(f);
5585 qemu_fclose(f);
5586 if (ret < 0) {
5587 term_printf("Error %d while writing VM\n", ret);
5588 goto the_end;
5589 }
ths3b46e622007-09-17 08:09:54 +00005590
bellardfaea38e2006-08-05 21:31:00 +00005591 /* create the snapshots */
5592
5593 for(i = 0; i < MAX_DISKS; i++) {
5594 bs1 = bs_table[i];
5595 if (bdrv_has_snapshot(bs1)) {
5596 if (must_delete) {
5597 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
5598 if (ret < 0) {
5599 term_printf("Error while deleting snapshot on '%s'\n",
5600 bdrv_get_device_name(bs1));
5601 }
5602 }
5603 ret = bdrv_snapshot_create(bs1, sn);
5604 if (ret < 0) {
5605 term_printf("Error while creating snapshot on '%s'\n",
5606 bdrv_get_device_name(bs1));
5607 }
5608 }
5609 }
5610
5611 the_end:
bellard8a7ddc32004-03-31 19:00:16 +00005612 if (saved_vm_running)
5613 vm_start();
bellardfaea38e2006-08-05 21:31:00 +00005614}
5615
5616void do_loadvm(const char *name)
5617{
5618 BlockDriverState *bs, *bs1;
5619 BlockDriverInfo bdi1, *bdi = &bdi1;
5620 QEMUFile *f;
5621 int i, ret;
5622 int saved_vm_running;
5623
5624 bs = get_bs_snapshots();
5625 if (!bs) {
5626 term_printf("No block device supports snapshots\n");
5627 return;
5628 }
ths3b46e622007-09-17 08:09:54 +00005629
pbrook6192bc32006-09-03 12:08:37 +00005630 /* Flush all IO requests so they don't interfere with the new state. */
5631 qemu_aio_flush();
5632
bellardfaea38e2006-08-05 21:31:00 +00005633 saved_vm_running = vm_running;
5634 vm_stop(0);
5635
5636 for(i = 0; i <= MAX_DISKS; i++) {
5637 bs1 = bs_table[i];
5638 if (bdrv_has_snapshot(bs1)) {
5639 ret = bdrv_snapshot_goto(bs1, name);
5640 if (ret < 0) {
5641 if (bs != bs1)
5642 term_printf("Warning: ");
5643 switch(ret) {
5644 case -ENOTSUP:
5645 term_printf("Snapshots not supported on device '%s'\n",
5646 bdrv_get_device_name(bs1));
5647 break;
5648 case -ENOENT:
5649 term_printf("Could not find snapshot '%s' on device '%s'\n",
5650 name, bdrv_get_device_name(bs1));
5651 break;
5652 default:
5653 term_printf("Error %d while activating snapshot on '%s'\n",
5654 ret, bdrv_get_device_name(bs1));
5655 break;
5656 }
5657 /* fatal on snapshot block device */
5658 if (bs == bs1)
5659 goto the_end;
5660 }
5661 }
5662 }
5663
5664 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5665 term_printf("Device %s does not support VM state snapshots\n",
5666 bdrv_get_device_name(bs));
5667 return;
5668 }
ths3b46e622007-09-17 08:09:54 +00005669
bellardfaea38e2006-08-05 21:31:00 +00005670 /* restore the VM state */
5671 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
5672 if (!f) {
5673 term_printf("Could not open VM state file\n");
5674 goto the_end;
5675 }
5676 ret = qemu_loadvm_state(f);
5677 qemu_fclose(f);
5678 if (ret < 0) {
5679 term_printf("Error %d while loading VM state\n", ret);
5680 }
5681 the_end:
5682 if (saved_vm_running)
5683 vm_start();
5684}
5685
5686void do_delvm(const char *name)
5687{
5688 BlockDriverState *bs, *bs1;
5689 int i, ret;
5690
5691 bs = get_bs_snapshots();
5692 if (!bs) {
5693 term_printf("No block device supports snapshots\n");
5694 return;
5695 }
ths3b46e622007-09-17 08:09:54 +00005696
bellardfaea38e2006-08-05 21:31:00 +00005697 for(i = 0; i <= MAX_DISKS; i++) {
5698 bs1 = bs_table[i];
5699 if (bdrv_has_snapshot(bs1)) {
5700 ret = bdrv_snapshot_delete(bs1, name);
5701 if (ret < 0) {
5702 if (ret == -ENOTSUP)
5703 term_printf("Snapshots not supported on device '%s'\n",
5704 bdrv_get_device_name(bs1));
5705 else
5706 term_printf("Error %d while deleting snapshot on '%s'\n",
5707 ret, bdrv_get_device_name(bs1));
5708 }
5709 }
5710 }
5711}
5712
5713void do_info_snapshots(void)
5714{
5715 BlockDriverState *bs, *bs1;
5716 QEMUSnapshotInfo *sn_tab, *sn;
5717 int nb_sns, i;
5718 char buf[256];
5719
5720 bs = get_bs_snapshots();
5721 if (!bs) {
5722 term_printf("No available block device supports snapshots\n");
5723 return;
5724 }
5725 term_printf("Snapshot devices:");
5726 for(i = 0; i <= MAX_DISKS; i++) {
5727 bs1 = bs_table[i];
5728 if (bdrv_has_snapshot(bs1)) {
5729 if (bs == bs1)
5730 term_printf(" %s", bdrv_get_device_name(bs1));
5731 }
5732 }
5733 term_printf("\n");
5734
5735 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5736 if (nb_sns < 0) {
5737 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
5738 return;
5739 }
5740 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
5741 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
5742 for(i = 0; i < nb_sns; i++) {
5743 sn = &sn_tab[i];
5744 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
5745 }
5746 qemu_free(sn_tab);
bellard8a7ddc32004-03-31 19:00:16 +00005747}
5748
5749/***********************************************************/
5750/* cpu save/restore */
5751
5752#if defined(TARGET_I386)
5753
5754static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
5755{
bellard02ba45c2004-06-25 14:46:23 +00005756 qemu_put_be32(f, dt->selector);
bellard20f32282005-01-03 23:36:21 +00005757 qemu_put_betl(f, dt->base);
bellard8a7ddc32004-03-31 19:00:16 +00005758 qemu_put_be32(f, dt->limit);
5759 qemu_put_be32(f, dt->flags);
5760}
5761
5762static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
5763{
bellard02ba45c2004-06-25 14:46:23 +00005764 dt->selector = qemu_get_be32(f);
bellard20f32282005-01-03 23:36:21 +00005765 dt->base = qemu_get_betl(f);
bellard8a7ddc32004-03-31 19:00:16 +00005766 dt->limit = qemu_get_be32(f);
5767 dt->flags = qemu_get_be32(f);
5768}
5769
5770void cpu_save(QEMUFile *f, void *opaque)
5771{
5772 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00005773 uint16_t fptag, fpus, fpuc, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00005774 uint32_t hflags;
5775 int i;
ths3b46e622007-09-17 08:09:54 +00005776
bellard20f32282005-01-03 23:36:21 +00005777 for(i = 0; i < CPU_NB_REGS; i++)
5778 qemu_put_betls(f, &env->regs[i]);
5779 qemu_put_betls(f, &env->eip);
5780 qemu_put_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00005781 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
5782 qemu_put_be32s(f, &hflags);
ths3b46e622007-09-17 08:09:54 +00005783
bellard8a7ddc32004-03-31 19:00:16 +00005784 /* FPU */
5785 fpuc = env->fpuc;
5786 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
5787 fptag = 0;
bellard664e0f12005-01-08 18:58:29 +00005788 for(i = 0; i < 8; i++) {
5789 fptag |= ((!env->fptags[i]) << i);
bellard8a7ddc32004-03-31 19:00:16 +00005790 }
ths3b46e622007-09-17 08:09:54 +00005791
bellard8a7ddc32004-03-31 19:00:16 +00005792 qemu_put_be16s(f, &fpuc);
5793 qemu_put_be16s(f, &fpus);
5794 qemu_put_be16s(f, &fptag);
5795
bellard664e0f12005-01-08 18:58:29 +00005796#ifdef USE_X86LDOUBLE
5797 fpregs_format = 0;
5798#else
5799 fpregs_format = 1;
5800#endif
5801 qemu_put_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00005802
bellard8a7ddc32004-03-31 19:00:16 +00005803 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00005804#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00005805 {
5806 uint64_t mant;
5807 uint16_t exp;
5808 /* we save the real CPU data (in case of MMX usage only 'mant'
5809 contains the MMX register */
5810 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
5811 qemu_put_be64(f, mant);
5812 qemu_put_be16(f, exp);
5813 }
bellard664e0f12005-01-08 18:58:29 +00005814#else
5815 /* if we use doubles for float emulation, we save the doubles to
5816 avoid losing information in case of MMX usage. It can give
5817 problems if the image is restored on a CPU where long
5818 doubles are used instead. */
bellard8636b5d2005-01-09 00:03:14 +00005819 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
bellard664e0f12005-01-08 18:58:29 +00005820#endif
bellard8a7ddc32004-03-31 19:00:16 +00005821 }
5822
5823 for(i = 0; i < 6; i++)
5824 cpu_put_seg(f, &env->segs[i]);
5825 cpu_put_seg(f, &env->ldt);
5826 cpu_put_seg(f, &env->tr);
5827 cpu_put_seg(f, &env->gdt);
5828 cpu_put_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00005829
bellard8a7ddc32004-03-31 19:00:16 +00005830 qemu_put_be32s(f, &env->sysenter_cs);
5831 qemu_put_be32s(f, &env->sysenter_esp);
5832 qemu_put_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00005833
bellard20f32282005-01-03 23:36:21 +00005834 qemu_put_betls(f, &env->cr[0]);
5835 qemu_put_betls(f, &env->cr[2]);
5836 qemu_put_betls(f, &env->cr[3]);
5837 qemu_put_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00005838
bellard8a7ddc32004-03-31 19:00:16 +00005839 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00005840 qemu_put_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00005841
5842 /* MMU */
5843 qemu_put_be32s(f, &env->a20_mask);
bellard02536f82005-01-06 20:43:38 +00005844
bellard664e0f12005-01-08 18:58:29 +00005845 /* XMM */
5846 qemu_put_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00005847 for(i = 0; i < CPU_NB_REGS; i++) {
5848 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
5849 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
5850 }
5851
bellard664e0f12005-01-08 18:58:29 +00005852#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00005853 qemu_put_be64s(f, &env->efer);
5854 qemu_put_be64s(f, &env->star);
5855 qemu_put_be64s(f, &env->lstar);
5856 qemu_put_be64s(f, &env->cstar);
5857 qemu_put_be64s(f, &env->fmask);
5858 qemu_put_be64s(f, &env->kernelgsbase);
5859#endif
bellard3b21e032006-09-24 18:41:56 +00005860 qemu_put_be32s(f, &env->smbase);
bellard8a7ddc32004-03-31 19:00:16 +00005861}
5862
bellard8636b5d2005-01-09 00:03:14 +00005863#ifdef USE_X86LDOUBLE
bellard664e0f12005-01-08 18:58:29 +00005864/* XXX: add that in a FPU generic layer */
5865union x86_longdouble {
5866 uint64_t mant;
5867 uint16_t exp;
5868};
5869
5870#define MANTD1(fp) (fp & ((1LL << 52) - 1))
5871#define EXPBIAS1 1023
5872#define EXPD1(fp) ((fp >> 52) & 0x7FF)
5873#define SIGND1(fp) ((fp >> 32) & 0x80000000)
5874
5875static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
5876{
5877 int e;
5878 /* mantissa */
5879 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
5880 /* exponent + sign */
5881 e = EXPD1(temp) - EXPBIAS1 + 16383;
5882 e |= SIGND1(temp) >> 16;
5883 p->exp = e;
5884}
bellard8636b5d2005-01-09 00:03:14 +00005885#endif
bellard664e0f12005-01-08 18:58:29 +00005886
bellard8a7ddc32004-03-31 19:00:16 +00005887int cpu_load(QEMUFile *f, void *opaque, int version_id)
5888{
5889 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00005890 int i, guess_mmx;
bellard8a7ddc32004-03-31 19:00:16 +00005891 uint32_t hflags;
bellard664e0f12005-01-08 18:58:29 +00005892 uint16_t fpus, fpuc, fptag, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00005893
bellard3b21e032006-09-24 18:41:56 +00005894 if (version_id != 3 && version_id != 4)
bellard8a7ddc32004-03-31 19:00:16 +00005895 return -EINVAL;
bellard20f32282005-01-03 23:36:21 +00005896 for(i = 0; i < CPU_NB_REGS; i++)
5897 qemu_get_betls(f, &env->regs[i]);
5898 qemu_get_betls(f, &env->eip);
5899 qemu_get_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00005900 qemu_get_be32s(f, &hflags);
5901
5902 qemu_get_be16s(f, &fpuc);
5903 qemu_get_be16s(f, &fpus);
5904 qemu_get_be16s(f, &fptag);
bellard664e0f12005-01-08 18:58:29 +00005905 qemu_get_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00005906
bellard664e0f12005-01-08 18:58:29 +00005907 /* NOTE: we cannot always restore the FPU state if the image come
5908 from a host with a different 'USE_X86LDOUBLE' define. We guess
5909 if we are in an MMX state to restore correctly in that case. */
5910 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
bellard8a7ddc32004-03-31 19:00:16 +00005911 for(i = 0; i < 8; i++) {
5912 uint64_t mant;
5913 uint16_t exp;
ths3b46e622007-09-17 08:09:54 +00005914
bellard664e0f12005-01-08 18:58:29 +00005915 switch(fpregs_format) {
5916 case 0:
5917 mant = qemu_get_be64(f);
5918 exp = qemu_get_be16(f);
5919#ifdef USE_X86LDOUBLE
5920 env->fpregs[i].d = cpu_set_fp80(mant, exp);
5921#else
5922 /* difficult case */
5923 if (guess_mmx)
bellard8636b5d2005-01-09 00:03:14 +00005924 env->fpregs[i].mmx.MMX_Q(0) = mant;
bellard664e0f12005-01-08 18:58:29 +00005925 else
5926 env->fpregs[i].d = cpu_set_fp80(mant, exp);
5927#endif
5928 break;
5929 case 1:
5930 mant = qemu_get_be64(f);
5931#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00005932 {
5933 union x86_longdouble *p;
5934 /* difficult case */
5935 p = (void *)&env->fpregs[i];
5936 if (guess_mmx) {
5937 p->mant = mant;
5938 p->exp = 0xffff;
5939 } else {
5940 fp64_to_fp80(p, mant);
5941 }
bellard664e0f12005-01-08 18:58:29 +00005942 }
5943#else
bellard8636b5d2005-01-09 00:03:14 +00005944 env->fpregs[i].mmx.MMX_Q(0) = mant;
ths3b46e622007-09-17 08:09:54 +00005945#endif
bellard664e0f12005-01-08 18:58:29 +00005946 break;
5947 default:
5948 return -EINVAL;
5949 }
bellard8a7ddc32004-03-31 19:00:16 +00005950 }
5951
5952 env->fpuc = fpuc;
bellard7a0e1f42005-03-13 17:01:47 +00005953 /* XXX: restore FPU round state */
bellard8a7ddc32004-03-31 19:00:16 +00005954 env->fpstt = (fpus >> 11) & 7;
5955 env->fpus = fpus & ~0x3800;
bellard664e0f12005-01-08 18:58:29 +00005956 fptag ^= 0xff;
bellard8a7ddc32004-03-31 19:00:16 +00005957 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00005958 env->fptags[i] = (fptag >> i) & 1;
bellard8a7ddc32004-03-31 19:00:16 +00005959 }
ths3b46e622007-09-17 08:09:54 +00005960
bellard8a7ddc32004-03-31 19:00:16 +00005961 for(i = 0; i < 6; i++)
5962 cpu_get_seg(f, &env->segs[i]);
5963 cpu_get_seg(f, &env->ldt);
5964 cpu_get_seg(f, &env->tr);
5965 cpu_get_seg(f, &env->gdt);
5966 cpu_get_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00005967
bellard8a7ddc32004-03-31 19:00:16 +00005968 qemu_get_be32s(f, &env->sysenter_cs);
5969 qemu_get_be32s(f, &env->sysenter_esp);
5970 qemu_get_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00005971
bellard20f32282005-01-03 23:36:21 +00005972 qemu_get_betls(f, &env->cr[0]);
5973 qemu_get_betls(f, &env->cr[2]);
5974 qemu_get_betls(f, &env->cr[3]);
5975 qemu_get_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00005976
bellard8a7ddc32004-03-31 19:00:16 +00005977 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00005978 qemu_get_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00005979
5980 /* MMU */
5981 qemu_get_be32s(f, &env->a20_mask);
5982
bellard664e0f12005-01-08 18:58:29 +00005983 qemu_get_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00005984 for(i = 0; i < CPU_NB_REGS; i++) {
5985 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
5986 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
5987 }
5988
bellard664e0f12005-01-08 18:58:29 +00005989#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00005990 qemu_get_be64s(f, &env->efer);
5991 qemu_get_be64s(f, &env->star);
5992 qemu_get_be64s(f, &env->lstar);
5993 qemu_get_be64s(f, &env->cstar);
5994 qemu_get_be64s(f, &env->fmask);
5995 qemu_get_be64s(f, &env->kernelgsbase);
5996#endif
ths5fafdf22007-09-16 21:08:06 +00005997 if (version_id >= 4)
bellard3b21e032006-09-24 18:41:56 +00005998 qemu_get_be32s(f, &env->smbase);
bellard02536f82005-01-06 20:43:38 +00005999
bellard8a7ddc32004-03-31 19:00:16 +00006000 /* XXX: compute hflags from scratch, except for CPL and IIF */
6001 env->hflags = hflags;
6002 tlb_flush(env, 1);
6003 return 0;
6004}
6005
bellarda541f292004-04-12 20:39:29 +00006006#elif defined(TARGET_PPC)
6007void cpu_save(QEMUFile *f, void *opaque)
6008{
6009}
6010
6011int cpu_load(QEMUFile *f, void *opaque, int version_id)
6012{
6013 return 0;
6014}
bellard6af0bf92005-07-02 14:58:51 +00006015
6016#elif defined(TARGET_MIPS)
6017void cpu_save(QEMUFile *f, void *opaque)
6018{
6019}
6020
6021int cpu_load(QEMUFile *f, void *opaque, int version_id)
6022{
6023 return 0;
6024}
6025
bellarde95c8d52004-09-30 22:22:08 +00006026#elif defined(TARGET_SPARC)
6027void cpu_save(QEMUFile *f, void *opaque)
6028{
bellarde80cfcf2004-12-19 23:18:01 +00006029 CPUState *env = opaque;
6030 int i;
6031 uint32_t tmp;
6032
bellard4fa5d772005-01-30 22:57:54 +00006033 for(i = 0; i < 8; i++)
6034 qemu_put_betls(f, &env->gregs[i]);
6035 for(i = 0; i < NWINDOWS * 16; i++)
6036 qemu_put_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006037
6038 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006039 for(i = 0; i < TARGET_FPREGS; i++) {
6040 union {
bellard1bdb68e2006-06-21 18:48:01 +00006041 float32 f;
6042 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006043 } u;
6044 u.f = env->fpr[i];
bellard1bdb68e2006-06-21 18:48:01 +00006045 qemu_put_be32(f, u.i);
bellarde80cfcf2004-12-19 23:18:01 +00006046 }
bellard4fa5d772005-01-30 22:57:54 +00006047
6048 qemu_put_betls(f, &env->pc);
6049 qemu_put_betls(f, &env->npc);
6050 qemu_put_betls(f, &env->y);
bellarde80cfcf2004-12-19 23:18:01 +00006051 tmp = GET_PSR(env);
bellard4fa5d772005-01-30 22:57:54 +00006052 qemu_put_be32(f, tmp);
bellard34751872005-07-02 14:31:34 +00006053 qemu_put_betls(f, &env->fsr);
6054 qemu_put_betls(f, &env->tbr);
6055#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006056 qemu_put_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006057 /* MMU */
6058 for(i = 0; i < 16; i++)
6059 qemu_put_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006060#endif
bellarde95c8d52004-09-30 22:22:08 +00006061}
6062
6063int cpu_load(QEMUFile *f, void *opaque, int version_id)
6064{
bellarde80cfcf2004-12-19 23:18:01 +00006065 CPUState *env = opaque;
6066 int i;
6067 uint32_t tmp;
6068
bellard4fa5d772005-01-30 22:57:54 +00006069 for(i = 0; i < 8; i++)
6070 qemu_get_betls(f, &env->gregs[i]);
6071 for(i = 0; i < NWINDOWS * 16; i++)
6072 qemu_get_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006073
6074 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006075 for(i = 0; i < TARGET_FPREGS; i++) {
6076 union {
bellard1bdb68e2006-06-21 18:48:01 +00006077 float32 f;
6078 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006079 } u;
bellard1bdb68e2006-06-21 18:48:01 +00006080 u.i = qemu_get_be32(f);
bellard4fa5d772005-01-30 22:57:54 +00006081 env->fpr[i] = u.f;
bellarde80cfcf2004-12-19 23:18:01 +00006082 }
bellard4fa5d772005-01-30 22:57:54 +00006083
6084 qemu_get_betls(f, &env->pc);
6085 qemu_get_betls(f, &env->npc);
6086 qemu_get_betls(f, &env->y);
6087 tmp = qemu_get_be32(f);
6088 env->cwp = 0; /* needed to ensure that the wrapping registers are
6089 correctly updated */
bellarde80cfcf2004-12-19 23:18:01 +00006090 PUT_PSR(env, tmp);
bellard34751872005-07-02 14:31:34 +00006091 qemu_get_betls(f, &env->fsr);
6092 qemu_get_betls(f, &env->tbr);
6093#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006094 qemu_get_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006095 /* MMU */
6096 for(i = 0; i < 16; i++)
6097 qemu_get_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006098#endif
bellarde80cfcf2004-12-19 23:18:01 +00006099 tlb_flush(env, 1);
bellarde95c8d52004-09-30 22:22:08 +00006100 return 0;
6101}
bellardb5ff1b32005-11-26 10:38:39 +00006102
6103#elif defined(TARGET_ARM)
6104
bellardb5ff1b32005-11-26 10:38:39 +00006105void cpu_save(QEMUFile *f, void *opaque)
6106{
balrogaa941b92007-05-24 18:50:09 +00006107 int i;
6108 CPUARMState *env = (CPUARMState *)opaque;
6109
6110 for (i = 0; i < 16; i++) {
6111 qemu_put_be32(f, env->regs[i]);
6112 }
6113 qemu_put_be32(f, cpsr_read(env));
6114 qemu_put_be32(f, env->spsr);
6115 for (i = 0; i < 6; i++) {
6116 qemu_put_be32(f, env->banked_spsr[i]);
6117 qemu_put_be32(f, env->banked_r13[i]);
6118 qemu_put_be32(f, env->banked_r14[i]);
6119 }
6120 for (i = 0; i < 5; i++) {
6121 qemu_put_be32(f, env->usr_regs[i]);
6122 qemu_put_be32(f, env->fiq_regs[i]);
6123 }
6124 qemu_put_be32(f, env->cp15.c0_cpuid);
6125 qemu_put_be32(f, env->cp15.c0_cachetype);
6126 qemu_put_be32(f, env->cp15.c1_sys);
6127 qemu_put_be32(f, env->cp15.c1_coproc);
balrog610c3c82007-06-24 12:09:48 +00006128 qemu_put_be32(f, env->cp15.c1_xscaleauxcr);
pbrook9ee6e8b2007-11-11 00:04:49 +00006129 qemu_put_be32(f, env->cp15.c2_base0);
6130 qemu_put_be32(f, env->cp15.c2_base1);
6131 qemu_put_be32(f, env->cp15.c2_mask);
balrogaa941b92007-05-24 18:50:09 +00006132 qemu_put_be32(f, env->cp15.c2_data);
6133 qemu_put_be32(f, env->cp15.c2_insn);
6134 qemu_put_be32(f, env->cp15.c3);
6135 qemu_put_be32(f, env->cp15.c5_insn);
6136 qemu_put_be32(f, env->cp15.c5_data);
6137 for (i = 0; i < 8; i++) {
6138 qemu_put_be32(f, env->cp15.c6_region[i]);
6139 }
6140 qemu_put_be32(f, env->cp15.c6_insn);
6141 qemu_put_be32(f, env->cp15.c6_data);
6142 qemu_put_be32(f, env->cp15.c9_insn);
6143 qemu_put_be32(f, env->cp15.c9_data);
6144 qemu_put_be32(f, env->cp15.c13_fcse);
6145 qemu_put_be32(f, env->cp15.c13_context);
pbrook9ee6e8b2007-11-11 00:04:49 +00006146 qemu_put_be32(f, env->cp15.c13_tls1);
6147 qemu_put_be32(f, env->cp15.c13_tls2);
6148 qemu_put_be32(f, env->cp15.c13_tls3);
balrogaa941b92007-05-24 18:50:09 +00006149 qemu_put_be32(f, env->cp15.c15_cpar);
6150
6151 qemu_put_be32(f, env->features);
6152
6153 if (arm_feature(env, ARM_FEATURE_VFP)) {
6154 for (i = 0; i < 16; i++) {
6155 CPU_DoubleU u;
6156 u.d = env->vfp.regs[i];
6157 qemu_put_be32(f, u.l.upper);
6158 qemu_put_be32(f, u.l.lower);
6159 }
6160 for (i = 0; i < 16; i++) {
6161 qemu_put_be32(f, env->vfp.xregs[i]);
6162 }
6163
6164 /* TODO: Should use proper FPSCR access functions. */
6165 qemu_put_be32(f, env->vfp.vec_len);
6166 qemu_put_be32(f, env->vfp.vec_stride);
pbrook9ee6e8b2007-11-11 00:04:49 +00006167
6168 if (arm_feature(env, ARM_FEATURE_VFP3)) {
6169 for (i = 16; i < 32; i++) {
6170 CPU_DoubleU u;
6171 u.d = env->vfp.regs[i];
6172 qemu_put_be32(f, u.l.upper);
6173 qemu_put_be32(f, u.l.lower);
6174 }
6175 }
balrogaa941b92007-05-24 18:50:09 +00006176 }
6177
6178 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6179 for (i = 0; i < 16; i++) {
6180 qemu_put_be64(f, env->iwmmxt.regs[i]);
6181 }
6182 for (i = 0; i < 16; i++) {
6183 qemu_put_be32(f, env->iwmmxt.cregs[i]);
6184 }
6185 }
pbrook9ee6e8b2007-11-11 00:04:49 +00006186
6187 if (arm_feature(env, ARM_FEATURE_M)) {
6188 qemu_put_be32(f, env->v7m.other_sp);
6189 qemu_put_be32(f, env->v7m.vecbase);
6190 qemu_put_be32(f, env->v7m.basepri);
6191 qemu_put_be32(f, env->v7m.control);
6192 qemu_put_be32(f, env->v7m.current_sp);
6193 qemu_put_be32(f, env->v7m.exception);
6194 }
bellardb5ff1b32005-11-26 10:38:39 +00006195}
6196
6197int cpu_load(QEMUFile *f, void *opaque, int version_id)
6198{
balrogaa941b92007-05-24 18:50:09 +00006199 CPUARMState *env = (CPUARMState *)opaque;
6200 int i;
6201
pbrook9ee6e8b2007-11-11 00:04:49 +00006202 if (version_id != ARM_CPU_SAVE_VERSION)
balrogaa941b92007-05-24 18:50:09 +00006203 return -EINVAL;
6204
6205 for (i = 0; i < 16; i++) {
6206 env->regs[i] = qemu_get_be32(f);
6207 }
6208 cpsr_write(env, qemu_get_be32(f), 0xffffffff);
6209 env->spsr = qemu_get_be32(f);
6210 for (i = 0; i < 6; i++) {
6211 env->banked_spsr[i] = qemu_get_be32(f);
6212 env->banked_r13[i] = qemu_get_be32(f);
6213 env->banked_r14[i] = qemu_get_be32(f);
6214 }
6215 for (i = 0; i < 5; i++) {
6216 env->usr_regs[i] = qemu_get_be32(f);
6217 env->fiq_regs[i] = qemu_get_be32(f);
6218 }
6219 env->cp15.c0_cpuid = qemu_get_be32(f);
6220 env->cp15.c0_cachetype = qemu_get_be32(f);
6221 env->cp15.c1_sys = qemu_get_be32(f);
6222 env->cp15.c1_coproc = qemu_get_be32(f);
balrog610c3c82007-06-24 12:09:48 +00006223 env->cp15.c1_xscaleauxcr = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006224 env->cp15.c2_base0 = qemu_get_be32(f);
6225 env->cp15.c2_base1 = qemu_get_be32(f);
6226 env->cp15.c2_mask = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006227 env->cp15.c2_data = qemu_get_be32(f);
6228 env->cp15.c2_insn = qemu_get_be32(f);
6229 env->cp15.c3 = qemu_get_be32(f);
6230 env->cp15.c5_insn = qemu_get_be32(f);
6231 env->cp15.c5_data = qemu_get_be32(f);
6232 for (i = 0; i < 8; i++) {
6233 env->cp15.c6_region[i] = qemu_get_be32(f);
6234 }
6235 env->cp15.c6_insn = qemu_get_be32(f);
6236 env->cp15.c6_data = qemu_get_be32(f);
6237 env->cp15.c9_insn = qemu_get_be32(f);
6238 env->cp15.c9_data = qemu_get_be32(f);
6239 env->cp15.c13_fcse = qemu_get_be32(f);
6240 env->cp15.c13_context = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006241 env->cp15.c13_tls1 = qemu_get_be32(f);
6242 env->cp15.c13_tls2 = qemu_get_be32(f);
6243 env->cp15.c13_tls3 = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006244 env->cp15.c15_cpar = qemu_get_be32(f);
6245
6246 env->features = qemu_get_be32(f);
6247
6248 if (arm_feature(env, ARM_FEATURE_VFP)) {
6249 for (i = 0; i < 16; i++) {
6250 CPU_DoubleU u;
6251 u.l.upper = qemu_get_be32(f);
6252 u.l.lower = qemu_get_be32(f);
6253 env->vfp.regs[i] = u.d;
6254 }
6255 for (i = 0; i < 16; i++) {
6256 env->vfp.xregs[i] = qemu_get_be32(f);
6257 }
6258
6259 /* TODO: Should use proper FPSCR access functions. */
6260 env->vfp.vec_len = qemu_get_be32(f);
6261 env->vfp.vec_stride = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006262
6263 if (arm_feature(env, ARM_FEATURE_VFP3)) {
6264 for (i = 0; i < 16; i++) {
6265 CPU_DoubleU u;
6266 u.l.upper = qemu_get_be32(f);
6267 u.l.lower = qemu_get_be32(f);
6268 env->vfp.regs[i] = u.d;
6269 }
6270 }
balrogaa941b92007-05-24 18:50:09 +00006271 }
6272
6273 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6274 for (i = 0; i < 16; i++) {
6275 env->iwmmxt.regs[i] = qemu_get_be64(f);
6276 }
6277 for (i = 0; i < 16; i++) {
6278 env->iwmmxt.cregs[i] = qemu_get_be32(f);
6279 }
6280 }
6281
pbrook9ee6e8b2007-11-11 00:04:49 +00006282 if (arm_feature(env, ARM_FEATURE_M)) {
6283 env->v7m.other_sp = qemu_get_be32(f);
6284 env->v7m.vecbase = qemu_get_be32(f);
6285 env->v7m.basepri = qemu_get_be32(f);
6286 env->v7m.control = qemu_get_be32(f);
6287 env->v7m.current_sp = qemu_get_be32(f);
6288 env->v7m.exception = qemu_get_be32(f);
6289 }
6290
bellardb5ff1b32005-11-26 10:38:39 +00006291 return 0;
6292}
6293
bellard8a7ddc32004-03-31 19:00:16 +00006294#else
6295
6296#warning No CPU save/restore functions
6297
6298#endif
6299
6300/***********************************************************/
6301/* ram save/restore */
6302
bellard8a7ddc32004-03-31 19:00:16 +00006303static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
6304{
6305 int v;
6306
6307 v = qemu_get_byte(f);
6308 switch(v) {
6309 case 0:
6310 if (qemu_get_buffer(f, buf, len) != len)
6311 return -EIO;
6312 break;
6313 case 1:
6314 v = qemu_get_byte(f);
6315 memset(buf, v, len);
6316 break;
6317 default:
6318 return -EINVAL;
6319 }
6320 return 0;
6321}
6322
bellardc88676f2006-08-06 13:36:11 +00006323static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00006324{
6325 int i, ret;
6326
bellard8a7ddc32004-03-31 19:00:16 +00006327 if (qemu_get_be32(f) != phys_ram_size)
6328 return -EINVAL;
6329 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
6330 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
6331 if (ret)
6332 return ret;
6333 }
6334 return 0;
6335}
6336
bellardc88676f2006-08-06 13:36:11 +00006337#define BDRV_HASH_BLOCK_SIZE 1024
6338#define IOBUF_SIZE 4096
6339#define RAM_CBLOCK_MAGIC 0xfabe
6340
6341typedef struct RamCompressState {
6342 z_stream zstream;
6343 QEMUFile *f;
6344 uint8_t buf[IOBUF_SIZE];
6345} RamCompressState;
6346
6347static int ram_compress_open(RamCompressState *s, QEMUFile *f)
6348{
6349 int ret;
6350 memset(s, 0, sizeof(*s));
6351 s->f = f;
6352 ret = deflateInit2(&s->zstream, 1,
ths5fafdf22007-09-16 21:08:06 +00006353 Z_DEFLATED, 15,
bellardc88676f2006-08-06 13:36:11 +00006354 9, Z_DEFAULT_STRATEGY);
6355 if (ret != Z_OK)
6356 return -1;
6357 s->zstream.avail_out = IOBUF_SIZE;
6358 s->zstream.next_out = s->buf;
6359 return 0;
6360}
6361
6362static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len)
6363{
6364 qemu_put_be16(s->f, RAM_CBLOCK_MAGIC);
6365 qemu_put_be16(s->f, len);
6366 qemu_put_buffer(s->f, buf, len);
6367}
6368
6369static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len)
6370{
6371 int ret;
6372
6373 s->zstream.avail_in = len;
6374 s->zstream.next_in = (uint8_t *)buf;
6375 while (s->zstream.avail_in > 0) {
6376 ret = deflate(&s->zstream, Z_NO_FLUSH);
6377 if (ret != Z_OK)
6378 return -1;
6379 if (s->zstream.avail_out == 0) {
6380 ram_put_cblock(s, s->buf, IOBUF_SIZE);
6381 s->zstream.avail_out = IOBUF_SIZE;
6382 s->zstream.next_out = s->buf;
6383 }
6384 }
6385 return 0;
6386}
6387
6388static void ram_compress_close(RamCompressState *s)
6389{
6390 int len, ret;
6391
6392 /* compress last bytes */
6393 for(;;) {
6394 ret = deflate(&s->zstream, Z_FINISH);
6395 if (ret == Z_OK || ret == Z_STREAM_END) {
6396 len = IOBUF_SIZE - s->zstream.avail_out;
6397 if (len > 0) {
6398 ram_put_cblock(s, s->buf, len);
6399 }
6400 s->zstream.avail_out = IOBUF_SIZE;
6401 s->zstream.next_out = s->buf;
6402 if (ret == Z_STREAM_END)
6403 break;
6404 } else {
6405 goto fail;
6406 }
6407 }
6408fail:
6409 deflateEnd(&s->zstream);
6410}
6411
6412typedef struct RamDecompressState {
6413 z_stream zstream;
6414 QEMUFile *f;
6415 uint8_t buf[IOBUF_SIZE];
6416} RamDecompressState;
6417
6418static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
6419{
6420 int ret;
6421 memset(s, 0, sizeof(*s));
6422 s->f = f;
6423 ret = inflateInit(&s->zstream);
6424 if (ret != Z_OK)
6425 return -1;
6426 return 0;
6427}
6428
6429static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
6430{
6431 int ret, clen;
6432
6433 s->zstream.avail_out = len;
6434 s->zstream.next_out = buf;
6435 while (s->zstream.avail_out > 0) {
6436 if (s->zstream.avail_in == 0) {
6437 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
6438 return -1;
6439 clen = qemu_get_be16(s->f);
6440 if (clen > IOBUF_SIZE)
6441 return -1;
6442 qemu_get_buffer(s->f, s->buf, clen);
6443 s->zstream.avail_in = clen;
6444 s->zstream.next_in = s->buf;
6445 }
6446 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
6447 if (ret != Z_OK && ret != Z_STREAM_END) {
6448 return -1;
6449 }
6450 }
6451 return 0;
6452}
6453
6454static void ram_decompress_close(RamDecompressState *s)
6455{
6456 inflateEnd(&s->zstream);
6457}
6458
6459static void ram_save(QEMUFile *f, void *opaque)
6460{
6461 int i;
6462 RamCompressState s1, *s = &s1;
6463 uint8_t buf[10];
ths3b46e622007-09-17 08:09:54 +00006464
bellardc88676f2006-08-06 13:36:11 +00006465 qemu_put_be32(f, phys_ram_size);
6466 if (ram_compress_open(s, f) < 0)
6467 return;
6468 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6469#if 0
6470 if (tight_savevm_enabled) {
6471 int64_t sector_num;
6472 int j;
6473
6474 /* find if the memory block is available on a virtual
6475 block device */
6476 sector_num = -1;
6477 for(j = 0; j < MAX_DISKS; j++) {
6478 if (bs_table[j]) {
ths5fafdf22007-09-16 21:08:06 +00006479 sector_num = bdrv_hash_find(bs_table[j],
bellardc88676f2006-08-06 13:36:11 +00006480 phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6481 if (sector_num >= 0)
6482 break;
6483 }
6484 }
6485 if (j == MAX_DISKS)
6486 goto normal_compress;
6487 buf[0] = 1;
6488 buf[1] = j;
6489 cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
6490 ram_compress_buf(s, buf, 10);
ths5fafdf22007-09-16 21:08:06 +00006491 } else
bellardc88676f2006-08-06 13:36:11 +00006492#endif
6493 {
6494 // normal_compress:
6495 buf[0] = 0;
6496 ram_compress_buf(s, buf, 1);
6497 ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6498 }
6499 }
6500 ram_compress_close(s);
6501}
6502
6503static int ram_load(QEMUFile *f, void *opaque, int version_id)
6504{
6505 RamDecompressState s1, *s = &s1;
6506 uint8_t buf[10];
6507 int i;
6508
6509 if (version_id == 1)
6510 return ram_load_v1(f, opaque);
6511 if (version_id != 2)
6512 return -EINVAL;
6513 if (qemu_get_be32(f) != phys_ram_size)
6514 return -EINVAL;
6515 if (ram_decompress_open(s, f) < 0)
6516 return -EINVAL;
6517 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6518 if (ram_decompress_buf(s, buf, 1) < 0) {
6519 fprintf(stderr, "Error while reading ram block header\n");
6520 goto error;
6521 }
6522 if (buf[0] == 0) {
6523 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
6524 fprintf(stderr, "Error while reading ram block address=0x%08x", i);
6525 goto error;
6526 }
ths5fafdf22007-09-16 21:08:06 +00006527 } else
bellardc88676f2006-08-06 13:36:11 +00006528#if 0
6529 if (buf[0] == 1) {
6530 int bs_index;
6531 int64_t sector_num;
6532
6533 ram_decompress_buf(s, buf + 1, 9);
6534 bs_index = buf[1];
6535 sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
6536 if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) {
6537 fprintf(stderr, "Invalid block device index %d\n", bs_index);
6538 goto error;
6539 }
ths5fafdf22007-09-16 21:08:06 +00006540 if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i,
bellardc88676f2006-08-06 13:36:11 +00006541 BDRV_HASH_BLOCK_SIZE / 512) < 0) {
ths5fafdf22007-09-16 21:08:06 +00006542 fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
bellardc88676f2006-08-06 13:36:11 +00006543 bs_index, sector_num);
6544 goto error;
6545 }
ths5fafdf22007-09-16 21:08:06 +00006546 } else
bellardc88676f2006-08-06 13:36:11 +00006547#endif
6548 {
6549 error:
6550 printf("Error block header\n");
6551 return -EINVAL;
6552 }
6553 }
6554 ram_decompress_close(s);
6555 return 0;
6556}
6557
bellard8a7ddc32004-03-31 19:00:16 +00006558/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00006559/* bottom halves (can be seen as timers which expire ASAP) */
6560
6561struct QEMUBH {
6562 QEMUBHFunc *cb;
6563 void *opaque;
6564 int scheduled;
6565 QEMUBH *next;
6566};
6567
6568static QEMUBH *first_bh = NULL;
6569
6570QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
6571{
6572 QEMUBH *bh;
6573 bh = qemu_mallocz(sizeof(QEMUBH));
6574 if (!bh)
6575 return NULL;
6576 bh->cb = cb;
6577 bh->opaque = opaque;
6578 return bh;
6579}
6580
bellard6eb57332006-08-06 09:51:25 +00006581int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00006582{
6583 QEMUBH *bh, **pbh;
bellard6eb57332006-08-06 09:51:25 +00006584 int ret;
bellard83f64092006-08-01 16:21:11 +00006585
bellard6eb57332006-08-06 09:51:25 +00006586 ret = 0;
bellard83f64092006-08-01 16:21:11 +00006587 for(;;) {
6588 pbh = &first_bh;
6589 bh = *pbh;
6590 if (!bh)
6591 break;
bellard6eb57332006-08-06 09:51:25 +00006592 ret = 1;
bellard83f64092006-08-01 16:21:11 +00006593 *pbh = bh->next;
6594 bh->scheduled = 0;
6595 bh->cb(bh->opaque);
6596 }
bellard6eb57332006-08-06 09:51:25 +00006597 return ret;
bellard83f64092006-08-01 16:21:11 +00006598}
6599
6600void qemu_bh_schedule(QEMUBH *bh)
6601{
6602 CPUState *env = cpu_single_env;
6603 if (bh->scheduled)
6604 return;
6605 bh->scheduled = 1;
6606 bh->next = first_bh;
6607 first_bh = bh;
6608
6609 /* stop the currently executing CPU to execute the BH ASAP */
6610 if (env) {
6611 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
6612 }
6613}
6614
6615void qemu_bh_cancel(QEMUBH *bh)
6616{
6617 QEMUBH **pbh;
6618 if (bh->scheduled) {
6619 pbh = &first_bh;
6620 while (*pbh != bh)
6621 pbh = &(*pbh)->next;
6622 *pbh = bh->next;
6623 bh->scheduled = 0;
6624 }
6625}
6626
6627void qemu_bh_delete(QEMUBH *bh)
6628{
6629 qemu_bh_cancel(bh);
6630 qemu_free(bh);
6631}
6632
6633/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00006634/* machine registration */
6635
6636QEMUMachine *first_machine = NULL;
6637
6638int qemu_register_machine(QEMUMachine *m)
6639{
6640 QEMUMachine **pm;
6641 pm = &first_machine;
6642 while (*pm != NULL)
6643 pm = &(*pm)->next;
6644 m->next = NULL;
6645 *pm = m;
6646 return 0;
6647}
6648
6649QEMUMachine *find_machine(const char *name)
6650{
6651 QEMUMachine *m;
6652
6653 for(m = first_machine; m != NULL; m = m->next) {
6654 if (!strcmp(m->name, name))
6655 return m;
6656 }
6657 return NULL;
6658}
6659
6660/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00006661/* main execution loop */
6662
6663void gui_update(void *opaque)
6664{
ths740733b2007-06-08 01:57:56 +00006665 DisplayState *ds = opaque;
6666 ds->dpy_refresh(ds);
6667 qemu_mod_timer(ds->gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00006668}
6669
bellard0bd48852005-11-11 00:00:47 +00006670struct vm_change_state_entry {
6671 VMChangeStateHandler *cb;
6672 void *opaque;
6673 LIST_ENTRY (vm_change_state_entry) entries;
6674};
6675
6676static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
6677
6678VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
6679 void *opaque)
6680{
6681 VMChangeStateEntry *e;
6682
6683 e = qemu_mallocz(sizeof (*e));
6684 if (!e)
6685 return NULL;
6686
6687 e->cb = cb;
6688 e->opaque = opaque;
6689 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
6690 return e;
6691}
6692
6693void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
6694{
6695 LIST_REMOVE (e, entries);
6696 qemu_free (e);
6697}
6698
6699static void vm_state_notify(int running)
6700{
6701 VMChangeStateEntry *e;
6702
6703 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
6704 e->cb(e->opaque, running);
6705 }
6706}
6707
bellard8a7ddc32004-03-31 19:00:16 +00006708/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00006709static VMStopHandler *vm_stop_cb;
6710static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00006711
6712int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
6713{
6714 vm_stop_cb = cb;
6715 vm_stop_opaque = opaque;
6716 return 0;
6717}
6718
6719void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
6720{
6721 vm_stop_cb = NULL;
6722}
6723
6724void vm_start(void)
6725{
6726 if (!vm_running) {
6727 cpu_enable_ticks();
6728 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00006729 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00006730 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00006731 }
6732}
6733
ths5fafdf22007-09-16 21:08:06 +00006734void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00006735{
6736 if (vm_running) {
6737 cpu_disable_ticks();
6738 vm_running = 0;
6739 if (reason != 0) {
6740 if (vm_stop_cb) {
6741 vm_stop_cb(vm_stop_opaque, reason);
6742 }
6743 }
bellard0bd48852005-11-11 00:00:47 +00006744 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00006745 }
6746}
6747
bellardbb0c6722004-06-20 12:37:32 +00006748/* reset/shutdown handler */
6749
6750typedef struct QEMUResetEntry {
6751 QEMUResetHandler *func;
6752 void *opaque;
6753 struct QEMUResetEntry *next;
6754} QEMUResetEntry;
6755
6756static QEMUResetEntry *first_reset_entry;
6757static int reset_requested;
6758static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00006759static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00006760
6761void qemu_register_reset(QEMUResetHandler *func, void *opaque)
6762{
6763 QEMUResetEntry **pre, *re;
6764
6765 pre = &first_reset_entry;
6766 while (*pre != NULL)
6767 pre = &(*pre)->next;
6768 re = qemu_mallocz(sizeof(QEMUResetEntry));
6769 re->func = func;
6770 re->opaque = opaque;
6771 re->next = NULL;
6772 *pre = re;
6773}
6774
ths52f61fd2006-12-22 21:20:52 +00006775static void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00006776{
6777 QEMUResetEntry *re;
6778
6779 /* reset all devices */
6780 for(re = first_reset_entry; re != NULL; re = re->next) {
6781 re->func(re->opaque);
6782 }
6783}
6784
6785void qemu_system_reset_request(void)
6786{
bellardd1beab82006-10-02 19:44:22 +00006787 if (no_reboot) {
6788 shutdown_requested = 1;
6789 } else {
6790 reset_requested = 1;
6791 }
bellard6a00d602005-11-21 23:25:50 +00006792 if (cpu_single_env)
6793 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006794}
6795
6796void qemu_system_shutdown_request(void)
6797{
6798 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00006799 if (cpu_single_env)
6800 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006801}
6802
bellard34751872005-07-02 14:31:34 +00006803void qemu_system_powerdown_request(void)
6804{
6805 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00006806 if (cpu_single_env)
6807 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006808}
6809
bellard5905b2e2004-08-01 21:53:26 +00006810void main_loop_wait(int timeout)
bellard8a7ddc32004-03-31 19:00:16 +00006811{
thscafffd42007-02-28 21:59:44 +00006812 IOHandlerRecord *ioh;
bellarde0356492006-05-01 13:33:02 +00006813 fd_set rfds, wfds, xfds;
ths877cf882007-04-18 18:11:47 +00006814 int ret, nfds;
6815#ifdef _WIN32
6816 int ret2, i;
6817#endif
bellardfd1dff42006-02-01 21:29:26 +00006818 struct timeval tv;
bellardf3311102006-04-12 20:21:17 +00006819 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00006820
bellardf3311102006-04-12 20:21:17 +00006821
6822 /* XXX: need to suppress polling by better using win32 events */
6823 ret = 0;
6824 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
6825 ret |= pe->func(pe->opaque);
6826 }
bellard38e205a2004-04-06 19:29:17 +00006827#ifdef _WIN32
thse6b1e552007-04-18 17:56:02 +00006828 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00006829 int err;
6830 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00006831
bellarda18e5242006-06-25 17:18:27 +00006832 ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
6833 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
6834 if (w->func[ret - WAIT_OBJECT_0])
6835 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00006836
ths5fafdf22007-09-16 21:08:06 +00006837 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00006838 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00006839
thse6b1e552007-04-18 17:56:02 +00006840 /* Check if event is signaled */
6841 ret2 = WaitForSingleObject(w->events[i], 0);
6842 if(ret2 == WAIT_OBJECT_0) {
6843 if (w->func[i])
6844 w->func[i](w->opaque[i]);
6845 } else if (ret2 == WAIT_TIMEOUT) {
6846 } else {
6847 err = GetLastError();
6848 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00006849 }
6850 }
bellarda18e5242006-06-25 17:18:27 +00006851 } else if (ret == WAIT_TIMEOUT) {
6852 } else {
6853 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00006854 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00006855 }
bellardf3311102006-04-12 20:21:17 +00006856 }
bellardfd1dff42006-02-01 21:29:26 +00006857#endif
6858 /* poll any events */
6859 /* XXX: separate device handlers from system ones */
6860 nfds = -1;
6861 FD_ZERO(&rfds);
6862 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00006863 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00006864 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00006865 if (ioh->deleted)
6866 continue;
bellardfd1dff42006-02-01 21:29:26 +00006867 if (ioh->fd_read &&
6868 (!ioh->fd_read_poll ||
6869 ioh->fd_read_poll(ioh->opaque) != 0)) {
6870 FD_SET(ioh->fd, &rfds);
6871 if (ioh->fd > nfds)
6872 nfds = ioh->fd;
6873 }
6874 if (ioh->fd_write) {
6875 FD_SET(ioh->fd, &wfds);
6876 if (ioh->fd > nfds)
6877 nfds = ioh->fd;
6878 }
6879 }
ths3b46e622007-09-17 08:09:54 +00006880
bellardfd1dff42006-02-01 21:29:26 +00006881 tv.tv_sec = 0;
6882#ifdef _WIN32
6883 tv.tv_usec = 0;
bellard38e205a2004-04-06 19:29:17 +00006884#else
bellardfd1dff42006-02-01 21:29:26 +00006885 tv.tv_usec = timeout * 1000;
6886#endif
bellarde0356492006-05-01 13:33:02 +00006887#if defined(CONFIG_SLIRP)
6888 if (slirp_inited) {
6889 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
6890 }
6891#endif
6892 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00006893 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00006894 IOHandlerRecord **pioh;
6895
6896 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00006897 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00006898 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00006899 }
ths6ab43fd2007-08-25 01:34:19 +00006900 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00006901 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00006902 }
6903 }
thscafffd42007-02-28 21:59:44 +00006904
6905 /* remove deleted IO handlers */
6906 pioh = &first_io_handler;
6907 while (*pioh) {
6908 ioh = *pioh;
6909 if (ioh->deleted) {
6910 *pioh = ioh->next;
6911 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00006912 } else
thscafffd42007-02-28 21:59:44 +00006913 pioh = &ioh->next;
6914 }
bellardfd1dff42006-02-01 21:29:26 +00006915 }
bellarde0356492006-05-01 13:33:02 +00006916#if defined(CONFIG_SLIRP)
6917 if (slirp_inited) {
6918 if (ret < 0) {
6919 FD_ZERO(&rfds);
6920 FD_ZERO(&wfds);
6921 FD_ZERO(&xfds);
6922 }
6923 slirp_select_poll(&rfds, &wfds, &xfds);
6924 }
6925#endif
bellard83f64092006-08-01 16:21:11 +00006926 qemu_aio_poll();
bellardc20709a2004-04-21 23:27:19 +00006927
bellardfd1dff42006-02-01 21:29:26 +00006928 if (vm_running) {
ths5fafdf22007-09-16 21:08:06 +00006929 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
bellardfd1dff42006-02-01 21:29:26 +00006930 qemu_get_clock(vm_clock));
6931 /* run dma transfers, if any */
6932 DMA_run();
6933 }
pbrook423f0742007-05-23 00:06:54 +00006934
bellardfd1dff42006-02-01 21:29:26 +00006935 /* real time timers */
ths5fafdf22007-09-16 21:08:06 +00006936 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
bellardfd1dff42006-02-01 21:29:26 +00006937 qemu_get_clock(rt_clock));
pbrook423f0742007-05-23 00:06:54 +00006938
6939 /* Check bottom-halves last in case any of the earlier events triggered
6940 them. */
6941 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00006942
bellard5905b2e2004-08-01 21:53:26 +00006943}
6944
bellard6a00d602005-11-21 23:25:50 +00006945static CPUState *cur_cpu;
6946
bellard5905b2e2004-08-01 21:53:26 +00006947int main_loop(void)
6948{
6949 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00006950#ifdef CONFIG_PROFILER
6951 int64_t ti;
6952#endif
bellard6a00d602005-11-21 23:25:50 +00006953 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00006954
bellard6a00d602005-11-21 23:25:50 +00006955 cur_cpu = first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00006956 for(;;) {
6957 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00006958
6959 env = cur_cpu;
6960 for(;;) {
6961 /* get next cpu */
6962 env = env->next_cpu;
6963 if (!env)
6964 env = first_cpu;
bellard89bfc102006-02-08 22:46:31 +00006965#ifdef CONFIG_PROFILER
6966 ti = profile_getclock();
6967#endif
bellard6a00d602005-11-21 23:25:50 +00006968 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00006969#ifdef CONFIG_PROFILER
6970 qemu_time += profile_getclock() - ti;
6971#endif
pbrookbd967e02007-03-11 18:54:57 +00006972 if (ret == EXCP_HLT) {
6973 /* Give the next CPU a chance to run. */
6974 cur_cpu = env;
6975 continue;
6976 }
bellard15a76442005-11-23 21:01:03 +00006977 if (ret != EXCP_HALTED)
6978 break;
6979 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00006980 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00006981 break;
bellard15a76442005-11-23 21:01:03 +00006982 }
6983 cur_cpu = env;
6984
bellard5905b2e2004-08-01 21:53:26 +00006985 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00006986 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00006987 break;
6988 }
6989 if (reset_requested) {
6990 reset_requested = 0;
6991 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00006992 ret = EXCP_INTERRUPT;
6993 }
6994 if (powerdown_requested) {
6995 powerdown_requested = 0;
6996 qemu_system_powerdown();
6997 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00006998 }
6999 if (ret == EXCP_DEBUG) {
7000 vm_stop(EXCP_DEBUG);
7001 }
pbrookbd967e02007-03-11 18:54:57 +00007002 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00007003 /* XXX: use timeout computed from timers */
pbrookbd967e02007-03-11 18:54:57 +00007004 if (ret == EXCP_HALTED)
bellard5905b2e2004-08-01 21:53:26 +00007005 timeout = 10;
7006 else
7007 timeout = 0;
7008 } else {
7009 timeout = 10;
7010 }
bellard89bfc102006-02-08 22:46:31 +00007011#ifdef CONFIG_PROFILER
7012 ti = profile_getclock();
7013#endif
bellard5905b2e2004-08-01 21:53:26 +00007014 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00007015#ifdef CONFIG_PROFILER
7016 dev_time += profile_getclock() - ti;
7017#endif
bellardb4608c02003-06-27 17:34:32 +00007018 }
bellard34865132003-10-05 14:28:56 +00007019 cpu_disable_ticks();
7020 return ret;
bellardb4608c02003-06-27 17:34:32 +00007021}
7022
ths15f82202007-06-29 23:26:08 +00007023static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00007024{
bellard84f2e8e2007-02-05 20:21:32 +00007025 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00007026 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00007027 "\n"
bellarda20dd502003-09-30 21:07:02 +00007028 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00007029 "\n"
bellarda20dd502003-09-30 21:07:02 +00007030 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00007031 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00007032 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00007033 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00007034 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7035 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007036 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
balrog3e3d5812007-04-30 02:09:25 +00007037 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00007038 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00007039 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00007040 "-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 +00007041 "-snapshot write to temporary files instead of disk image files\n"
7042#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007043 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00007044 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00007045 "-no-quit disable SDL window close capability\n"
7046#endif
bellard52ca8d62006-06-14 16:03:05 +00007047#ifdef TARGET_I386
7048 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7049#endif
bellarda00bad72004-05-22 21:39:06 +00007050 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00007051 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007052 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00007053 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00007054#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00007055 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00007056#endif
bellard1d14ffa2005-10-30 18:58:22 +00007057#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007058 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00007059 "-soundhw c1,... enable audio support\n"
7060 " and only specified sound cards (comma separated list)\n"
7061 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00007062 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00007063#endif
bellard89980282004-06-03 14:04:03 +00007064 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00007065 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00007066#ifdef TARGET_I386
7067 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7068#endif
bellardb389dbf2005-11-06 16:49:55 +00007069 "-usb enable the USB driver (will be the default soon)\n"
7070 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00007071#if defined(TARGET_PPC) || defined(TARGET_SPARC)
7072 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00007073#endif
thsc35734b2007-03-19 15:17:08 +00007074 "-name string set the name of the guest\n"
bellarda20dd502003-09-30 21:07:02 +00007075 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007076 "Network options:\n"
pbrooka41b2ff2006-02-05 04:14:41 +00007077 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007078 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00007079#ifdef CONFIG_SLIRP
pbrook115defd2006-04-16 11:06:58 +00007080 "-net user[,vlan=n][,hostname=host]\n"
7081 " connect the user mode network stack to VLAN 'n' and send\n"
7082 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00007083#endif
bellard7fb843f2006-02-01 23:06:55 +00007084#ifdef _WIN32
7085 "-net tap[,vlan=n],ifname=name\n"
7086 " connect the host TAP network interface to VLAN 'n'\n"
7087#else
thsb46a8902007-10-21 23:20:45 +00007088 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7089 " connect the host TAP network interface to VLAN 'n' and use the\n"
7090 " network scripts 'file' (default=%s)\n"
7091 " and 'dfile' (default=%s);\n"
7092 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00007093 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00007094#endif
bellard6a00d602005-11-21 23:25:50 +00007095 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007096 " connect the vlan 'n' to another VLAN using a socket connection\n"
bellard3d830452005-12-18 16:36:49 +00007097 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7098 " connect the vlan 'n' to multicast maddr and port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007099 "-net none use it alone to have zero network devices; if no -net option\n"
7100 " is provided, the default is '-net nic -net user'\n"
7101 "\n"
7102#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00007103 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00007104 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00007105#ifndef _WIN32
7106 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00007107#endif
bellard9bf05442004-08-25 22:12:49 +00007108 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007109 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00007110#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007111 "\n"
7112 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00007113 "-kernel bzImage use 'bzImage' as kernel image\n"
7114 "-append cmdline use 'cmdline' as kernel command line\n"
7115 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00007116 "\n"
bellard330d0412003-07-26 18:11:40 +00007117 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00007118 "-monitor dev redirect the monitor to char device 'dev'\n"
7119 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00007120 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00007121 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00007122 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00007123 "-s wait gdb connection to port\n"
7124 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00007125 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00007126 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7127 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00007128 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00007129#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00007130 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00007131 "-no-kqemu disable KQEMU kernel module usage\n"
7132#endif
bellardbb0c6722004-06-20 12:37:32 +00007133#ifdef TARGET_I386
bellard1bfe8562004-07-08 21:17:50 +00007134 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7135 " (default is CL-GD5446 PCI VGA)\n"
bellard6515b202006-05-03 22:02:44 +00007136 "-no-acpi disable ACPI\n"
bellardbb0c6722004-06-20 12:37:32 +00007137#endif
bellardd1beab82006-10-02 19:44:22 +00007138 "-no-reboot exit instead of rebooting\n"
bellardd63d3072004-10-03 13:29:03 +00007139 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00007140 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00007141#ifndef _WIN32
7142 "-daemonize daemonize QEMU after initializing\n"
7143#endif
ths9ae02552007-01-05 17:39:04 +00007144 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00007145#ifdef TARGET_SPARC
7146 "-prom-env variable=value set OpenBIOS nvram variables\n"
7147#endif
thsf3dcfad2007-08-24 01:26:02 +00007148 "-clock force the use of the given methods for timer alarm.\n"
7149 " To see what timers are available use -clock help\n"
bellard0824d6f2003-06-24 13:42:40 +00007150 "\n"
bellard82c643f2004-07-14 17:28:13 +00007151 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00007152 "ctrl-alt-f toggle full screen\n"
7153 "ctrl-alt-n switch to virtual console 'n'\n"
7154 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00007155 "\n"
7156 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7157 ,
bellard0db63472003-10-27 21:37:46 +00007158 "qemu",
bellarda00bad72004-05-22 21:39:06 +00007159 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00007160#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00007161 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00007162 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00007163#endif
bellard6e44ba72004-01-18 21:56:49 +00007164 DEFAULT_GDBSTUB_PORT,
7165 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00007166 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00007167}
7168
bellardcd6f1162004-05-13 22:02:20 +00007169#define HAS_ARG 0x0001
7170
7171enum {
7172 QEMU_OPTION_h,
7173
bellardcc1daa42005-06-05 14:49:17 +00007174 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00007175 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00007176 QEMU_OPTION_fda,
7177 QEMU_OPTION_fdb,
7178 QEMU_OPTION_hda,
7179 QEMU_OPTION_hdb,
7180 QEMU_OPTION_hdc,
7181 QEMU_OPTION_hdd,
7182 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00007183 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00007184 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00007185 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00007186 QEMU_OPTION_boot,
7187 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00007188#ifdef TARGET_I386
7189 QEMU_OPTION_no_fd_bootchk,
7190#endif
bellardcd6f1162004-05-13 22:02:20 +00007191 QEMU_OPTION_m,
7192 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00007193 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00007194#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007195 QEMU_OPTION_audio_help,
7196 QEMU_OPTION_soundhw,
7197#endif
bellardcd6f1162004-05-13 22:02:20 +00007198
bellard7c9d8e02005-11-15 22:16:05 +00007199 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00007200 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00007201 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00007202 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00007203 QEMU_OPTION_redir,
bellardcd6f1162004-05-13 22:02:20 +00007204
7205 QEMU_OPTION_kernel,
7206 QEMU_OPTION_append,
7207 QEMU_OPTION_initrd,
7208
7209 QEMU_OPTION_S,
7210 QEMU_OPTION_s,
7211 QEMU_OPTION_p,
7212 QEMU_OPTION_d,
7213 QEMU_OPTION_hdachs,
7214 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00007215 QEMU_OPTION_bios,
bellardcd6f1162004-05-13 22:02:20 +00007216 QEMU_OPTION_no_code_copy,
bellard3d11d0e2004-12-12 16:56:30 +00007217 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00007218 QEMU_OPTION_localtime,
bellard1f042752004-06-05 13:46:47 +00007219 QEMU_OPTION_cirrusvga,
thsd34cab92007-04-02 01:10:46 +00007220 QEMU_OPTION_vmsvga,
bellarde9b137c2004-06-21 16:46:10 +00007221 QEMU_OPTION_g,
bellard1bfe8562004-07-08 21:17:50 +00007222 QEMU_OPTION_std_vga,
ths20d8a3e2007-02-18 17:04:49 +00007223 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00007224 QEMU_OPTION_monitor,
7225 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00007226 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00007227 QEMU_OPTION_loadvm,
7228 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00007229 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00007230 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00007231 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00007232 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00007233 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00007234 QEMU_OPTION_kernel_kqemu,
bellarda09db212005-04-30 16:10:35 +00007235 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00007236 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00007237 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00007238 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00007239 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00007240 QEMU_OPTION_no_acpi,
bellardd1beab82006-10-02 19:44:22 +00007241 QEMU_OPTION_no_reboot,
balrog9467cd42007-05-01 01:34:14 +00007242 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00007243 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00007244 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00007245 QEMU_OPTION_semihosting,
7246 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00007247 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00007248 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00007249 QEMU_OPTION_clock,
bellard7e0af5d02007-11-07 16:24:33 +00007250 QEMU_OPTION_startdate,
bellardcd6f1162004-05-13 22:02:20 +00007251};
7252
7253typedef struct QEMUOption {
7254 const char *name;
7255 int flags;
7256 int index;
7257} QEMUOption;
7258
7259const QEMUOption qemu_options[] = {
7260 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00007261 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00007262
bellardcc1daa42005-06-05 14:49:17 +00007263 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00007264 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00007265 { "fda", HAS_ARG, QEMU_OPTION_fda },
7266 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
7267 { "hda", HAS_ARG, QEMU_OPTION_hda },
7268 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
7269 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
7270 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
7271 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00007272 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00007273 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00007274 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00007275 { "boot", HAS_ARG, QEMU_OPTION_boot },
7276 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00007277#ifdef TARGET_I386
7278 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
7279#endif
bellardcd6f1162004-05-13 22:02:20 +00007280 { "m", HAS_ARG, QEMU_OPTION_m },
7281 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00007282 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00007283 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00007284#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007285 { "audio-help", 0, QEMU_OPTION_audio_help },
7286 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
7287#endif
bellardcd6f1162004-05-13 22:02:20 +00007288
bellard7c9d8e02005-11-15 22:16:05 +00007289 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00007290#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00007291 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00007292 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00007293#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007294 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00007295#endif
bellard9bf05442004-08-25 22:12:49 +00007296 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00007297#endif
bellardcd6f1162004-05-13 22:02:20 +00007298
7299 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
7300 { "append", HAS_ARG, QEMU_OPTION_append },
7301 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
7302
7303 { "S", 0, QEMU_OPTION_S },
7304 { "s", 0, QEMU_OPTION_s },
7305 { "p", HAS_ARG, QEMU_OPTION_p },
7306 { "d", HAS_ARG, QEMU_OPTION_d },
7307 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
7308 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00007309 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardcd6f1162004-05-13 22:02:20 +00007310 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
bellardd993e022005-02-10 22:00:06 +00007311#ifdef USE_KQEMU
7312 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00007313 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00007314#endif
bellard6f7e9ae2005-03-13 09:43:36 +00007315#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00007316 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00007317#endif
bellardee22c2f2004-06-03 12:49:50 +00007318 { "localtime", 0, QEMU_OPTION_localtime },
bellard1bfe8562004-07-08 21:17:50 +00007319 { "std-vga", 0, QEMU_OPTION_std_vga },
balrog8b6e0722007-06-22 08:23:44 +00007320 { "echr", HAS_ARG, QEMU_OPTION_echr },
7321 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
7322 { "serial", HAS_ARG, QEMU_OPTION_serial },
7323 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00007324 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
7325 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00007326#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007327 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00007328 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00007329 { "no-quit", 0, QEMU_OPTION_no_quit },
7330#endif
bellardf7cce892004-12-08 22:21:25 +00007331 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00007332 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00007333 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00007334 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00007335 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
ths96d30e42007-01-07 20:42:14 +00007336
bellard1f042752004-06-05 13:46:47 +00007337 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00007338 { "usb", 0, QEMU_OPTION_usb },
bellard1f042752004-06-05 13:46:47 +00007339 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
thsd34cab92007-04-02 01:10:46 +00007340 { "vmwarevga", 0, QEMU_OPTION_vmsvga },
bellard6515b202006-05-03 22:02:44 +00007341 { "no-acpi", 0, QEMU_OPTION_no_acpi },
bellardd1beab82006-10-02 19:44:22 +00007342 { "no-reboot", 0, QEMU_OPTION_no_reboot },
balrog9467cd42007-05-01 01:34:14 +00007343 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00007344 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00007345 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00007346#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00007347 { "semihosting", 0, QEMU_OPTION_semihosting },
7348#endif
thsc35734b2007-03-19 15:17:08 +00007349 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir166508602007-05-01 14:16:52 +00007350#if defined(TARGET_SPARC)
7351 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
7352#endif
balrog2b8f2d42007-07-27 22:08:46 +00007353#if defined(TARGET_ARM)
7354 { "old-param", 0, QEMU_OPTION_old_param },
7355#endif
thsf3dcfad2007-08-24 01:26:02 +00007356 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellard7e0af5d02007-11-07 16:24:33 +00007357 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
bellardcd6f1162004-05-13 22:02:20 +00007358 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00007359};
7360
bellard5905b2e2004-08-01 21:53:26 +00007361/* password input */
7362
balrog2bac6012007-04-30 01:34:31 +00007363int qemu_key_check(BlockDriverState *bs, const char *name)
7364{
7365 char password[256];
7366 int i;
7367
7368 if (!bdrv_is_encrypted(bs))
7369 return 0;
7370
7371 term_printf("%s is encrypted.\n", name);
7372 for(i = 0; i < 3; i++) {
7373 monitor_readline("Password: ", 1, password, sizeof(password));
7374 if (bdrv_set_key(bs, password) == 0)
7375 return 0;
7376 term_printf("invalid password\n");
7377 }
7378 return -EPERM;
7379}
7380
bellard5905b2e2004-08-01 21:53:26 +00007381static BlockDriverState *get_bdrv(int index)
7382{
7383 BlockDriverState *bs;
7384
7385 if (index < 4) {
7386 bs = bs_table[index];
7387 } else if (index < 6) {
7388 bs = fd_table[index - 4];
7389 } else {
7390 bs = NULL;
7391 }
7392 return bs;
7393}
7394
7395static void read_passwords(void)
7396{
7397 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00007398 int i;
bellard5905b2e2004-08-01 21:53:26 +00007399
7400 for(i = 0; i < 6; i++) {
7401 bs = get_bdrv(i);
balrog2bac6012007-04-30 01:34:31 +00007402 if (bs)
7403 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00007404 }
7405}
7406
bellardcc1daa42005-06-05 14:49:17 +00007407/* XXX: currently we cannot use simultaneously different CPUs */
7408void register_machines(void)
7409{
7410#if defined(TARGET_I386)
7411 qemu_register_machine(&pc_machine);
bellard3dbbdc22005-11-06 18:20:37 +00007412 qemu_register_machine(&isapc_machine);
bellardcc1daa42005-06-05 14:49:17 +00007413#elif defined(TARGET_PPC)
7414 qemu_register_machine(&heathrow_machine);
7415 qemu_register_machine(&core99_machine);
7416 qemu_register_machine(&prep_machine);
j_mayer1a6c0882007-04-24 07:40:49 +00007417 qemu_register_machine(&ref405ep_machine);
7418 qemu_register_machine(&taihu_machine);
bellard6af0bf92005-07-02 14:58:51 +00007419#elif defined(TARGET_MIPS)
7420 qemu_register_machine(&mips_machine);
ths5856de82007-01-15 23:58:11 +00007421 qemu_register_machine(&mips_malta_machine);
thsad6fe1d2007-04-16 17:23:27 +00007422 qemu_register_machine(&mips_pica61_machine);
ths6bf5b4e2007-10-17 13:08:32 +00007423 qemu_register_machine(&mips_mipssim_machine);
bellardcc1daa42005-06-05 14:49:17 +00007424#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +00007425#ifdef TARGET_SPARC64
7426 qemu_register_machine(&sun4u_machine);
7427#else
blueswir136cd9212007-04-01 15:44:43 +00007428 qemu_register_machine(&ss5_machine);
blueswir1e0353fe2007-04-01 15:55:28 +00007429 qemu_register_machine(&ss10_machine);
bellardcc1daa42005-06-05 14:49:17 +00007430#endif
bellardb5ff1b32005-11-26 10:38:39 +00007431#elif defined(TARGET_ARM)
pbrook3371d272007-03-08 03:04:12 +00007432 qemu_register_machine(&integratorcp_machine);
pbrookcdbdb642006-04-09 01:32:52 +00007433 qemu_register_machine(&versatilepb_machine);
pbrook16406952006-04-27 23:15:07 +00007434 qemu_register_machine(&versatileab_machine);
pbrooke69954b2006-09-23 17:40:58 +00007435 qemu_register_machine(&realview_machine);
balrogb00052e2007-04-30 02:22:06 +00007436 qemu_register_machine(&akitapda_machine);
7437 qemu_register_machine(&spitzpda_machine);
7438 qemu_register_machine(&borzoipda_machine);
7439 qemu_register_machine(&terrierpda_machine);
balrogc3d26892007-07-29 17:57:26 +00007440 qemu_register_machine(&palmte_machine);
pbrook9ee6e8b2007-11-11 00:04:49 +00007441 qemu_register_machine(&lm3s811evb_machine);
7442 qemu_register_machine(&lm3s6965evb_machine);
bellard27c7ca72006-04-27 21:32:09 +00007443#elif defined(TARGET_SH4)
7444 qemu_register_machine(&shix_machine);
ths0d78f542007-09-29 19:24:41 +00007445 qemu_register_machine(&r2d_machine);
j_mayereddf68a2007-04-05 07:22:49 +00007446#elif defined(TARGET_ALPHA)
7447 /* XXX: TODO */
pbrook06338792007-05-23 19:58:11 +00007448#elif defined(TARGET_M68K)
pbrook20dcee92007-06-03 11:13:39 +00007449 qemu_register_machine(&mcf5208evb_machine);
pbrook06338792007-05-23 19:58:11 +00007450 qemu_register_machine(&an5206_machine);
pbrookca02f312007-11-10 18:03:23 +00007451 qemu_register_machine(&dummy_m68k_machine);
thsf1ccf902007-10-08 13:16:14 +00007452#elif defined(TARGET_CRIS)
7453 qemu_register_machine(&bareetraxfs_machine);
bellardb5ff1b32005-11-26 10:38:39 +00007454#else
7455#error unsupported CPU
bellard34751872005-07-02 14:31:34 +00007456#endif
bellardcc1daa42005-06-05 14:49:17 +00007457}
7458
bellard1d14ffa2005-10-30 18:58:22 +00007459#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00007460struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00007461#ifdef HAS_AUDIO_CHOICE
bellardfd06c372006-04-24 21:58:30 +00007462#ifdef TARGET_I386
7463 {
7464 "pcspk",
7465 "PC speaker",
7466 0,
7467 1,
7468 { .init_isa = pcspk_audio_init }
7469 },
7470#endif
bellard6a36d842005-12-18 20:34:32 +00007471 {
7472 "sb16",
7473 "Creative Sound Blaster 16",
7474 0,
7475 1,
7476 { .init_isa = SB16_init }
7477 },
7478
7479#ifdef CONFIG_ADLIB
7480 {
7481 "adlib",
7482#ifdef HAS_YMF262
7483 "Yamaha YMF262 (OPL3)",
7484#else
7485 "Yamaha YM3812 (OPL2)",
7486#endif
7487 0,
7488 1,
7489 { .init_isa = Adlib_init }
7490 },
7491#endif
7492
7493#ifdef CONFIG_GUS
7494 {
7495 "gus",
7496 "Gravis Ultrasound GF1",
7497 0,
7498 1,
7499 { .init_isa = GUS_init }
7500 },
7501#endif
7502
7503 {
7504 "es1370",
7505 "ENSONIQ AudioPCI ES1370",
7506 0,
7507 0,
7508 { .init_pci = es1370_init }
7509 },
balrogb00052e2007-04-30 02:22:06 +00007510#endif
bellard6a36d842005-12-18 20:34:32 +00007511
7512 { NULL, NULL, 0, 0, { NULL } }
7513};
7514
bellard1d14ffa2005-10-30 18:58:22 +00007515static void select_soundhw (const char *optarg)
7516{
bellard6a36d842005-12-18 20:34:32 +00007517 struct soundhw *c;
7518
bellard1d14ffa2005-10-30 18:58:22 +00007519 if (*optarg == '?') {
7520 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00007521
bellard1d14ffa2005-10-30 18:58:22 +00007522 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00007523 for (c = soundhw; c->name; ++c) {
7524 printf ("%-11s %s\n", c->name, c->descr);
7525 }
7526 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00007527 exit (*optarg != '?');
7528 }
7529 else {
bellard6a36d842005-12-18 20:34:32 +00007530 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00007531 const char *p;
7532 char *e;
7533 int bad_card = 0;
7534
bellard6a36d842005-12-18 20:34:32 +00007535 if (!strcmp (optarg, "all")) {
7536 for (c = soundhw; c->name; ++c) {
7537 c->enabled = 1;
7538 }
7539 return;
7540 }
bellard1d14ffa2005-10-30 18:58:22 +00007541
bellard6a36d842005-12-18 20:34:32 +00007542 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00007543 while (*p) {
7544 e = strchr (p, ',');
7545 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00007546
7547 for (c = soundhw; c->name; ++c) {
7548 if (!strncmp (c->name, p, l)) {
7549 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00007550 break;
7551 }
7552 }
bellard6a36d842005-12-18 20:34:32 +00007553
7554 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00007555 if (l > 80) {
7556 fprintf (stderr,
7557 "Unknown sound card name (too big to show)\n");
7558 }
7559 else {
7560 fprintf (stderr, "Unknown sound card name `%.*s'\n",
7561 (int) l, p);
7562 }
7563 bad_card = 1;
7564 }
7565 p += l + (e != NULL);
7566 }
7567
7568 if (bad_card)
7569 goto show_valid_cards;
7570 }
7571}
7572#endif
7573
bellard3587d7e2006-06-26 20:03:44 +00007574#ifdef _WIN32
7575static BOOL WINAPI qemu_ctrl_handler(DWORD type)
7576{
7577 exit(STATUS_CONTROL_C_EXIT);
7578 return TRUE;
7579}
7580#endif
7581
bellard7c9d8e02005-11-15 22:16:05 +00007582#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00007583
bellard0824d6f2003-06-24 13:42:40 +00007584int main(int argc, char **argv)
7585{
bellard67b915a2004-03-31 23:37:16 +00007586#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00007587 int use_gdbstub;
7588 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00007589#endif
j_mayer86f55662007-04-24 06:52:59 +00007590 int i, cdrom_index, pflash_index;
bellard1ccde1c2004-02-06 19:46:14 +00007591 int snapshot, linux_boot;
bellard7f7f9872003-10-30 01:11:23 +00007592 const char *initrd_filename;
ths96d30e42007-01-07 20:42:14 +00007593 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
j_mayer86f55662007-04-24 06:52:59 +00007594 const char *pflash_filename[MAX_PFLASH];
pbrooka1bb27b2007-04-06 16:49:48 +00007595 const char *sd_filename;
balrog3e3d5812007-04-30 02:09:25 +00007596 const char *mtd_filename;
bellarda20dd502003-09-30 21:07:02 +00007597 const char *kernel_filename, *kernel_cmdline;
bellard313aa562003-08-10 21:52:11 +00007598 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00007599 int cyls, heads, secs, translation;
bellard7c9d8e02005-11-15 22:16:05 +00007600 char net_clients[MAX_NET_CLIENTS][256];
7601 int nb_net_clients;
bellardcd6f1162004-05-13 22:02:20 +00007602 int optind;
7603 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00007604 CharDriverState *monitor_hd;
7605 char monitor_device[128];
bellard8d11df92004-08-24 21:13:40 +00007606 char serial_devices[MAX_SERIAL_PORTS][128];
7607 int serial_device_index;
bellard6508fe52005-01-15 12:02:56 +00007608 char parallel_devices[MAX_PARALLEL_PORTS][128];
7609 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00007610 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00007611 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00007612 const char *cpu_model;
pbrook0d92ed32006-05-21 16:30:15 +00007613 char usb_devices[MAX_USB_CMDLINE][128];
bellarda594cfb2005-11-06 16:13:29 +00007614 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00007615 int fds[2];
ths93815bc2007-03-19 15:58:31 +00007616 const char *pid_file = NULL;
blueswir1833c7172007-05-27 19:36:43 +00007617 VLANState *vlan;
bellard0bd48852005-11-11 00:00:47 +00007618
7619 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00007620#ifndef _WIN32
7621 {
7622 struct sigaction act;
7623 sigfillset(&act.sa_mask);
7624 act.sa_flags = 0;
7625 act.sa_handler = SIG_IGN;
7626 sigaction(SIGPIPE, &act, NULL);
7627 }
bellard3587d7e2006-06-26 20:03:44 +00007628#else
7629 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00007630 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7631 QEMU to run on a single CPU */
7632 {
7633 HANDLE h;
7634 DWORD mask, smask;
7635 int i;
7636 h = GetCurrentProcess();
7637 if (GetProcessAffinityMask(h, &mask, &smask)) {
7638 for(i = 0; i < 32; i++) {
7639 if (mask & (1 << i))
7640 break;
7641 }
7642 if (i != 32) {
7643 mask = 1 << i;
7644 SetProcessAffinityMask(h, mask);
7645 }
7646 }
7647 }
bellard67b915a2004-03-31 23:37:16 +00007648#endif
bellardbe995c22006-06-25 16:25:21 +00007649
bellardcc1daa42005-06-05 14:49:17 +00007650 register_machines();
7651 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00007652 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00007653 initrd_filename = NULL;
bellardc45886d2004-01-05 00:02:06 +00007654 for(i = 0; i < MAX_FD; i++)
7655 fd_filename[i] = NULL;
ths96d30e42007-01-07 20:42:14 +00007656 for(i = 0; i < MAX_DISKS; i++)
7657 hd_filename[i] = NULL;
j_mayer86f55662007-04-24 06:52:59 +00007658 for(i = 0; i < MAX_PFLASH; i++)
7659 pflash_filename[i] = NULL;
7660 pflash_index = 0;
pbrooka1bb27b2007-04-06 16:49:48 +00007661 sd_filename = NULL;
balrog3e3d5812007-04-30 02:09:25 +00007662 mtd_filename = NULL;
bellarda00bad72004-05-22 21:39:06 +00007663 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
bellard313aa562003-08-10 21:52:11 +00007664 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00007665#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00007666 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00007667 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00007668#endif
bellard33e39632003-07-06 17:15:21 +00007669 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00007670 nographic = 0;
7671 kernel_filename = NULL;
7672 kernel_cmdline = "";
bellardcc1daa42005-06-05 14:49:17 +00007673#ifdef TARGET_PPC
7674 cdrom_index = 1;
7675#else
7676 cdrom_index = 2;
7677#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007678 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00007679 translation = BIOS_ATA_TRANSLATION_AUTO;
bellard82c643f2004-07-14 17:28:13 +00007680 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
bellardc4b1fcc2004-03-14 21:44:30 +00007681
bellard8d11df92004-08-24 21:13:40 +00007682 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
7683 for(i = 1; i < MAX_SERIAL_PORTS; i++)
7684 serial_devices[i][0] = '\0';
7685 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00007686
bellard6508fe52005-01-15 12:02:56 +00007687 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
7688 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
7689 parallel_devices[i][0] = '\0';
7690 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00007691
bellarda594cfb2005-11-06 16:13:29 +00007692 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00007693
bellard7c9d8e02005-11-15 22:16:05 +00007694 nb_net_clients = 0;
7695
7696 nb_nics = 0;
bellard702c6512004-04-02 21:21:32 +00007697 /* default mac address of the first network interface */
ths3b46e622007-09-17 08:09:54 +00007698
bellardcd6f1162004-05-13 22:02:20 +00007699 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00007700 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00007701 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00007702 break;
bellardcd6f1162004-05-13 22:02:20 +00007703 r = argv[optind];
7704 if (r[0] != '-') {
ths96d30e42007-01-07 20:42:14 +00007705 hd_filename[0] = argv[optind++];
bellardcd6f1162004-05-13 22:02:20 +00007706 } else {
7707 const QEMUOption *popt;
7708
7709 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00007710 /* Treat --foo the same as -foo. */
7711 if (r[1] == '-')
7712 r++;
bellardcd6f1162004-05-13 22:02:20 +00007713 popt = qemu_options;
7714 for(;;) {
7715 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00007716 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00007717 argv[0], r);
7718 exit(1);
7719 }
7720 if (!strcmp(popt->name, r + 1))
7721 break;
7722 popt++;
7723 }
7724 if (popt->flags & HAS_ARG) {
7725 if (optind >= argc) {
7726 fprintf(stderr, "%s: option '%s' requires an argument\n",
7727 argv[0], r);
7728 exit(1);
7729 }
7730 optarg = argv[optind++];
7731 } else {
7732 optarg = NULL;
7733 }
7734
7735 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00007736 case QEMU_OPTION_M:
7737 machine = find_machine(optarg);
7738 if (!machine) {
7739 QEMUMachine *m;
7740 printf("Supported machines are:\n");
7741 for(m = first_machine; m != NULL; m = m->next) {
7742 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00007743 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00007744 m == first_machine ? " (default)" : "");
7745 }
ths15f82202007-06-29 23:26:08 +00007746 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00007747 }
7748 break;
j_mayer94fc95c2007-03-05 19:44:02 +00007749 case QEMU_OPTION_cpu:
7750 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00007751 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00007752/* XXX: implement xxx_cpu_list for targets that still miss it */
7753#if defined(cpu_list)
7754 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00007755#endif
ths15f82202007-06-29 23:26:08 +00007756 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00007757 } else {
7758 cpu_model = optarg;
7759 }
7760 break;
bellardcd6f1162004-05-13 22:02:20 +00007761 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00007762 initrd_filename = optarg;
7763 break;
bellardcd6f1162004-05-13 22:02:20 +00007764 case QEMU_OPTION_hda:
bellardcd6f1162004-05-13 22:02:20 +00007765 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00007766 case QEMU_OPTION_hdc:
7767 case QEMU_OPTION_hdd:
7768 {
7769 int hd_index;
7770 hd_index = popt->index - QEMU_OPTION_hda;
ths96d30e42007-01-07 20:42:14 +00007771 hd_filename[hd_index] = optarg;
7772 if (hd_index == cdrom_index)
7773 cdrom_index = -1;
bellardcc1daa42005-06-05 14:49:17 +00007774 }
bellardfc01f7e2003-06-30 10:03:06 +00007775 break;
balrog3e3d5812007-04-30 02:09:25 +00007776 case QEMU_OPTION_mtdblock:
7777 mtd_filename = optarg;
7778 break;
pbrooka1bb27b2007-04-06 16:49:48 +00007779 case QEMU_OPTION_sd:
7780 sd_filename = optarg;
7781 break;
j_mayer86f55662007-04-24 06:52:59 +00007782 case QEMU_OPTION_pflash:
7783 if (pflash_index >= MAX_PFLASH) {
7784 fprintf(stderr, "qemu: too many parallel flash images\n");
7785 exit(1);
7786 }
7787 pflash_filename[pflash_index++] = optarg;
7788 break;
bellardcd6f1162004-05-13 22:02:20 +00007789 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00007790 snapshot = 1;
7791 break;
bellardcd6f1162004-05-13 22:02:20 +00007792 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00007793 {
bellard330d0412003-07-26 18:11:40 +00007794 const char *p;
7795 p = optarg;
7796 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007797 if (cyls < 1 || cyls > 16383)
7798 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00007799 if (*p != ',')
7800 goto chs_fail;
7801 p++;
7802 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007803 if (heads < 1 || heads > 16)
7804 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00007805 if (*p != ',')
7806 goto chs_fail;
7807 p++;
7808 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007809 if (secs < 1 || secs > 63)
7810 goto chs_fail;
7811 if (*p == ',') {
7812 p++;
7813 if (!strcmp(p, "none"))
7814 translation = BIOS_ATA_TRANSLATION_NONE;
7815 else if (!strcmp(p, "lba"))
7816 translation = BIOS_ATA_TRANSLATION_LBA;
7817 else if (!strcmp(p, "auto"))
7818 translation = BIOS_ATA_TRANSLATION_AUTO;
7819 else
7820 goto chs_fail;
7821 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00007822 chs_fail:
bellard46d47672004-11-16 01:45:27 +00007823 fprintf(stderr, "qemu: invalid physical CHS format\n");
7824 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00007825 }
bellard330d0412003-07-26 18:11:40 +00007826 }
7827 break;
bellardcd6f1162004-05-13 22:02:20 +00007828 case QEMU_OPTION_nographic:
bellard8d11df92004-08-24 21:13:40 +00007829 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
thsa39437a2007-03-25 20:27:04 +00007830 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null");
ths20d8a3e2007-02-18 17:04:49 +00007831 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
bellarda20dd502003-09-30 21:07:02 +00007832 nographic = 1;
7833 break;
balroga171fe32007-04-30 01:48:07 +00007834 case QEMU_OPTION_portrait:
7835 graphic_rotate = 1;
7836 break;
bellardcd6f1162004-05-13 22:02:20 +00007837 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00007838 kernel_filename = optarg;
7839 break;
bellardcd6f1162004-05-13 22:02:20 +00007840 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00007841 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00007842 break;
bellardcd6f1162004-05-13 22:02:20 +00007843 case QEMU_OPTION_cdrom:
ths96d30e42007-01-07 20:42:14 +00007844 if (cdrom_index >= 0) {
7845 hd_filename[cdrom_index] = optarg;
bellardcc1daa42005-06-05 14:49:17 +00007846 }
bellard36b486b2003-11-11 13:36:08 +00007847 break;
bellardcd6f1162004-05-13 22:02:20 +00007848 case QEMU_OPTION_boot:
balrog6ac0e822007-10-31 01:54:04 +00007849 if (strlen(optarg) > MAX_BOOT_DEVICES) {
7850 fprintf(stderr, "qemu: too many boot devices\n");
7851 exit(1);
7852 }
7853 strncpy(boot_device, optarg, MAX_BOOT_DEVICES);
thseec85c22007-01-05 17:41:07 +00007854#if defined(TARGET_SPARC) || defined(TARGET_I386)
balrog6ac0e822007-10-31 01:54:04 +00007855#define BOOTCHARS "acdn"
7856#else
7857#define BOOTCHARS "acd"
bellard6f7e9ae2005-03-13 09:43:36 +00007858#endif
balrog6ac0e822007-10-31 01:54:04 +00007859 if (strlen(boot_device) != strspn(boot_device, BOOTCHARS)) {
7860 fprintf(stderr, "qemu: invalid boot device "
7861 "sequence '%s'\n", boot_device);
bellard36b486b2003-11-11 13:36:08 +00007862 exit(1);
7863 }
7864 break;
bellardcd6f1162004-05-13 22:02:20 +00007865 case QEMU_OPTION_fda:
bellardc45886d2004-01-05 00:02:06 +00007866 fd_filename[0] = optarg;
7867 break;
bellardcd6f1162004-05-13 22:02:20 +00007868 case QEMU_OPTION_fdb:
bellardc45886d2004-01-05 00:02:06 +00007869 fd_filename[1] = optarg;
7870 break;
bellard52ca8d62006-06-14 16:03:05 +00007871#ifdef TARGET_I386
7872 case QEMU_OPTION_no_fd_bootchk:
7873 fd_bootchk = 0;
7874 break;
7875#endif
bellardcd6f1162004-05-13 22:02:20 +00007876 case QEMU_OPTION_no_code_copy:
bellard77fef8c2004-02-16 22:05:46 +00007877 code_copy_enabled = 0;
7878 break;
bellard7c9d8e02005-11-15 22:16:05 +00007879 case QEMU_OPTION_net:
7880 if (nb_net_clients >= MAX_NET_CLIENTS) {
7881 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00007882 exit(1);
7883 }
bellard7c9d8e02005-11-15 22:16:05 +00007884 pstrcpy(net_clients[nb_net_clients],
7885 sizeof(net_clients[0]),
7886 optarg);
7887 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00007888 break;
bellardc7f74642004-08-24 21:57:12 +00007889#ifdef CONFIG_SLIRP
7890 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00007891 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00007892 break;
ths47d5d012007-02-20 00:05:08 +00007893 case QEMU_OPTION_bootp:
7894 bootp_filename = optarg;
7895 break;
bellardc94c8d62004-09-13 21:37:34 +00007896#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007897 case QEMU_OPTION_smb:
7898 net_slirp_smb(optarg);
7899 break;
bellardc94c8d62004-09-13 21:37:34 +00007900#endif
bellard9bf05442004-08-25 22:12:49 +00007901 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00007902 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00007903 break;
bellardc7f74642004-08-24 21:57:12 +00007904#endif
bellard1d14ffa2005-10-30 18:58:22 +00007905#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007906 case QEMU_OPTION_audio_help:
7907 AUD_help ();
7908 exit (0);
7909 break;
7910 case QEMU_OPTION_soundhw:
7911 select_soundhw (optarg);
7912 break;
7913#endif
bellardcd6f1162004-05-13 22:02:20 +00007914 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00007915 help(0);
bellardcd6f1162004-05-13 22:02:20 +00007916 break;
7917 case QEMU_OPTION_m:
7918 ram_size = atoi(optarg) * 1024 * 1024;
7919 if (ram_size <= 0)
ths15f82202007-06-29 23:26:08 +00007920 help(1);
bellardcd6f1162004-05-13 22:02:20 +00007921 if (ram_size > PHYS_RAM_MAX_SIZE) {
7922 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
7923 PHYS_RAM_MAX_SIZE / (1024 * 1024));
7924 exit(1);
7925 }
7926 break;
7927 case QEMU_OPTION_d:
7928 {
7929 int mask;
7930 CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00007931
bellardcd6f1162004-05-13 22:02:20 +00007932 mask = cpu_str_to_log_mask(optarg);
7933 if (!mask) {
7934 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00007935 for(item = cpu_log_items; item->mask != 0; item++) {
7936 printf("%-10s %s\n", item->name, item->help);
7937 }
7938 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00007939 }
7940 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00007941 }
bellardcd6f1162004-05-13 22:02:20 +00007942 break;
bellard67b915a2004-03-31 23:37:16 +00007943#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00007944 case QEMU_OPTION_s:
7945 use_gdbstub = 1;
7946 break;
7947 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00007948 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00007949 break;
bellard67b915a2004-03-31 23:37:16 +00007950#endif
bellardcd6f1162004-05-13 22:02:20 +00007951 case QEMU_OPTION_L:
7952 bios_dir = optarg;
7953 break;
j_mayer1192dad2007-10-05 13:08:35 +00007954 case QEMU_OPTION_bios:
7955 bios_name = optarg;
7956 break;
bellardcd6f1162004-05-13 22:02:20 +00007957 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00007958 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00007959 break;
bellard3d11d0e2004-12-12 16:56:30 +00007960 case QEMU_OPTION_k:
7961 keyboard_layout = optarg;
7962 break;
bellardee22c2f2004-06-03 12:49:50 +00007963 case QEMU_OPTION_localtime:
7964 rtc_utc = 0;
7965 break;
bellard1f042752004-06-05 13:46:47 +00007966 case QEMU_OPTION_cirrusvga:
7967 cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +00007968 vmsvga_enabled = 0;
7969 break;
7970 case QEMU_OPTION_vmsvga:
7971 cirrus_vga_enabled = 0;
7972 vmsvga_enabled = 1;
bellard1f042752004-06-05 13:46:47 +00007973 break;
bellard1bfe8562004-07-08 21:17:50 +00007974 case QEMU_OPTION_std_vga:
7975 cirrus_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +00007976 vmsvga_enabled = 0;
bellard1bfe8562004-07-08 21:17:50 +00007977 break;
bellarde9b137c2004-06-21 16:46:10 +00007978 case QEMU_OPTION_g:
7979 {
7980 const char *p;
7981 int w, h, depth;
7982 p = optarg;
7983 w = strtol(p, (char **)&p, 10);
7984 if (w <= 0) {
7985 graphic_error:
7986 fprintf(stderr, "qemu: invalid resolution or depth\n");
7987 exit(1);
7988 }
7989 if (*p != 'x')
7990 goto graphic_error;
7991 p++;
7992 h = strtol(p, (char **)&p, 10);
7993 if (h <= 0)
7994 goto graphic_error;
7995 if (*p == 'x') {
7996 p++;
7997 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00007998 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00007999 depth != 24 && depth != 32)
8000 goto graphic_error;
8001 } else if (*p == '\0') {
8002 depth = graphic_depth;
8003 } else {
8004 goto graphic_error;
8005 }
ths3b46e622007-09-17 08:09:54 +00008006
bellarde9b137c2004-06-21 16:46:10 +00008007 graphic_width = w;
8008 graphic_height = h;
8009 graphic_depth = depth;
8010 }
8011 break;
ths20d8a3e2007-02-18 17:04:49 +00008012 case QEMU_OPTION_echr:
8013 {
8014 char *r;
8015 term_escape_char = strtol(optarg, &r, 0);
8016 if (r == optarg)
8017 printf("Bad argument to echr\n");
8018 break;
8019 }
bellard82c643f2004-07-14 17:28:13 +00008020 case QEMU_OPTION_monitor:
8021 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
8022 break;
8023 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00008024 if (serial_device_index >= MAX_SERIAL_PORTS) {
8025 fprintf(stderr, "qemu: too many serial ports\n");
8026 exit(1);
8027 }
ths5fafdf22007-09-16 21:08:06 +00008028 pstrcpy(serial_devices[serial_device_index],
bellard8d11df92004-08-24 21:13:40 +00008029 sizeof(serial_devices[0]), optarg);
8030 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00008031 break;
bellard6508fe52005-01-15 12:02:56 +00008032 case QEMU_OPTION_parallel:
8033 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
8034 fprintf(stderr, "qemu: too many parallel ports\n");
8035 exit(1);
8036 }
ths5fafdf22007-09-16 21:08:06 +00008037 pstrcpy(parallel_devices[parallel_device_index],
bellard6508fe52005-01-15 12:02:56 +00008038 sizeof(parallel_devices[0]), optarg);
8039 parallel_device_index++;
8040 break;
bellardd63d3072004-10-03 13:29:03 +00008041 case QEMU_OPTION_loadvm:
8042 loadvm = optarg;
8043 break;
8044 case QEMU_OPTION_full_screen:
8045 full_screen = 1;
8046 break;
ths667acca2006-12-11 02:08:05 +00008047#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00008048 case QEMU_OPTION_no_frame:
8049 no_frame = 1;
8050 break;
ths3780e192007-06-21 21:08:02 +00008051 case QEMU_OPTION_alt_grab:
8052 alt_grab = 1;
8053 break;
ths667acca2006-12-11 02:08:05 +00008054 case QEMU_OPTION_no_quit:
8055 no_quit = 1;
8056 break;
8057#endif
bellardf7cce892004-12-08 22:21:25 +00008058 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00008059 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00008060 break;
bellarda09db212005-04-30 16:10:35 +00008061#ifdef TARGET_I386
8062 case QEMU_OPTION_win2k_hack:
8063 win2k_install_hack = 1;
8064 break;
8065#endif
bellardd993e022005-02-10 22:00:06 +00008066#ifdef USE_KQEMU
8067 case QEMU_OPTION_no_kqemu:
8068 kqemu_allowed = 0;
8069 break;
bellard89bfc102006-02-08 22:46:31 +00008070 case QEMU_OPTION_kernel_kqemu:
8071 kqemu_allowed = 2;
8072 break;
bellardd993e022005-02-10 22:00:06 +00008073#endif
bellardbb36d472005-11-05 14:22:28 +00008074 case QEMU_OPTION_usb:
8075 usb_enabled = 1;
8076 break;
bellarda594cfb2005-11-06 16:13:29 +00008077 case QEMU_OPTION_usbdevice:
8078 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00008079 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00008080 fprintf(stderr, "Too many USB devices\n");
8081 exit(1);
8082 }
8083 pstrcpy(usb_devices[usb_devices_index],
8084 sizeof(usb_devices[usb_devices_index]),
8085 optarg);
8086 usb_devices_index++;
8087 break;
bellard6a00d602005-11-21 23:25:50 +00008088 case QEMU_OPTION_smp:
8089 smp_cpus = atoi(optarg);
bellardba3c64f2005-12-05 20:31:52 +00008090 if (smp_cpus < 1 || smp_cpus > MAX_CPUS) {
bellard6a00d602005-11-21 23:25:50 +00008091 fprintf(stderr, "Invalid number of CPUs\n");
8092 exit(1);
8093 }
8094 break;
bellard24236862006-04-30 21:28:36 +00008095 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00008096 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00008097 break;
bellard6515b202006-05-03 22:02:44 +00008098 case QEMU_OPTION_no_acpi:
8099 acpi_enabled = 0;
8100 break;
bellardd1beab82006-10-02 19:44:22 +00008101 case QEMU_OPTION_no_reboot:
8102 no_reboot = 1;
8103 break;
balrog9467cd42007-05-01 01:34:14 +00008104 case QEMU_OPTION_show_cursor:
8105 cursor_hide = 0;
8106 break;
ths71e3ceb2006-12-22 02:11:31 +00008107 case QEMU_OPTION_daemonize:
8108 daemonize = 1;
8109 break;
ths9ae02552007-01-05 17:39:04 +00008110 case QEMU_OPTION_option_rom:
8111 if (nb_option_roms >= MAX_OPTION_ROMS) {
8112 fprintf(stderr, "Too many option ROMs\n");
8113 exit(1);
8114 }
8115 option_rom[nb_option_roms] = optarg;
8116 nb_option_roms++;
8117 break;
pbrook8e716212007-01-20 17:12:09 +00008118 case QEMU_OPTION_semihosting:
8119 semihosting_enabled = 1;
8120 break;
thsc35734b2007-03-19 15:17:08 +00008121 case QEMU_OPTION_name:
8122 qemu_name = optarg;
8123 break;
blueswir166508602007-05-01 14:16:52 +00008124#ifdef TARGET_SPARC
8125 case QEMU_OPTION_prom_env:
8126 if (nb_prom_envs >= MAX_PROM_ENVS) {
8127 fprintf(stderr, "Too many prom variables\n");
8128 exit(1);
8129 }
8130 prom_envs[nb_prom_envs] = optarg;
8131 nb_prom_envs++;
8132 break;
8133#endif
balrog2b8f2d42007-07-27 22:08:46 +00008134#ifdef TARGET_ARM
8135 case QEMU_OPTION_old_param:
8136 old_param = 1;
8137#endif
thsf3dcfad2007-08-24 01:26:02 +00008138 case QEMU_OPTION_clock:
8139 configure_alarms(optarg);
8140 break;
bellard7e0af5d02007-11-07 16:24:33 +00008141 case QEMU_OPTION_startdate:
8142 {
8143 struct tm tm;
8144 if (!strcmp(optarg, "now")) {
8145 rtc_start_date = -1;
8146 } else {
8147 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
8148 &tm.tm_year,
8149 &tm.tm_mon,
8150 &tm.tm_mday,
8151 &tm.tm_hour,
8152 &tm.tm_min,
8153 &tm.tm_sec) == 6) {
8154 /* OK */
8155 } else if (sscanf(optarg, "%d-%d-%d",
8156 &tm.tm_year,
8157 &tm.tm_mon,
8158 &tm.tm_mday) == 3) {
8159 tm.tm_hour = 0;
8160 tm.tm_min = 0;
8161 tm.tm_sec = 0;
8162 } else {
8163 goto date_fail;
8164 }
8165 tm.tm_year -= 1900;
8166 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00008167 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00008168 if (rtc_start_date == -1) {
8169 date_fail:
8170 fprintf(stderr, "Invalid date format. Valid format are:\n"
8171 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8172 exit(1);
8173 }
8174 }
8175 }
8176 break;
bellardcd6f1162004-05-13 22:02:20 +00008177 }
bellard0824d6f2003-06-24 13:42:40 +00008178 }
8179 }
bellard330d0412003-07-26 18:11:40 +00008180
ths71e3ceb2006-12-22 02:11:31 +00008181#ifndef _WIN32
8182 if (daemonize && !nographic && vnc_display == NULL) {
8183 fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n");
8184 daemonize = 0;
8185 }
8186
8187 if (daemonize) {
8188 pid_t pid;
8189
8190 if (pipe(fds) == -1)
8191 exit(1);
8192
8193 pid = fork();
8194 if (pid > 0) {
8195 uint8_t status;
8196 ssize_t len;
8197
8198 close(fds[1]);
8199
8200 again:
ths93815bc2007-03-19 15:58:31 +00008201 len = read(fds[0], &status, 1);
8202 if (len == -1 && (errno == EINTR))
8203 goto again;
8204
8205 if (len != 1)
8206 exit(1);
8207 else if (status == 1) {
8208 fprintf(stderr, "Could not acquire pidfile\n");
8209 exit(1);
8210 } else
8211 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00008212 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00008213 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00008214
8215 setsid();
8216
8217 pid = fork();
8218 if (pid > 0)
8219 exit(0);
8220 else if (pid < 0)
8221 exit(1);
8222
8223 umask(027);
8224 chdir("/");
8225
8226 signal(SIGTSTP, SIG_IGN);
8227 signal(SIGTTOU, SIG_IGN);
8228 signal(SIGTTIN, SIG_IGN);
8229 }
8230#endif
8231
thsaa26bb22007-03-25 21:33:06 +00008232 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00008233 if (daemonize) {
8234 uint8_t status = 1;
8235 write(fds[1], &status, 1);
8236 } else
8237 fprintf(stderr, "Could not acquire pid file\n");
8238 exit(1);
8239 }
8240
bellardff3fbb32006-01-08 10:53:14 +00008241#ifdef USE_KQEMU
8242 if (smp_cpus > 1)
8243 kqemu_allowed = 0;
8244#endif
bellarda20dd502003-09-30 21:07:02 +00008245 linux_boot = (kernel_filename != NULL);
ths42550fd2006-12-22 16:34:12 +00008246
8247 if (!linux_boot &&
balrog6ac0e822007-10-31 01:54:04 +00008248 (!strchr(boot_device, 'n')) &&
ths5fafdf22007-09-16 21:08:06 +00008249 hd_filename[0] == '\0' &&
ths96d30e42007-01-07 20:42:14 +00008250 (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
bellardc45886d2004-01-05 00:02:06 +00008251 fd_filename[0] == '\0')
ths15f82202007-06-29 23:26:08 +00008252 help(1);
bellard0824d6f2003-06-24 13:42:40 +00008253
ths96d30e42007-01-07 20:42:14 +00008254 /* boot to floppy or the default cd if no hard disk defined yet */
balrog6ac0e822007-10-31 01:54:04 +00008255 if (!boot_device[0]) {
8256 if (hd_filename[0] != '\0')
8257 boot_device[0] = 'c';
8258 else if (fd_filename[0] != '\0')
8259 boot_device[0] = 'a';
ths96d30e42007-01-07 20:42:14 +00008260 else
balrog6ac0e822007-10-31 01:54:04 +00008261 boot_device[0] = 'd';
8262 boot_device[1] = 0;
ths96d30e42007-01-07 20:42:14 +00008263 }
bellardb118d612003-06-30 23:36:21 +00008264 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00008265
pbrook634fce92006-07-15 17:40:09 +00008266 init_timers();
8267 init_timer_alarm();
bellard83f64092006-08-01 16:21:11 +00008268 qemu_aio_init();
pbrook634fce92006-07-15 17:40:09 +00008269
bellardfd1dff42006-02-01 21:29:26 +00008270#ifdef _WIN32
8271 socket_init();
8272#endif
8273
bellard7c9d8e02005-11-15 22:16:05 +00008274 /* init network clients */
8275 if (nb_net_clients == 0) {
8276 /* if no clients, we use a default config */
8277 pstrcpy(net_clients[0], sizeof(net_clients[0]),
8278 "nic");
8279 pstrcpy(net_clients[1], sizeof(net_clients[0]),
8280 "user");
8281 nb_net_clients = 2;
bellardc20709a2004-04-21 23:27:19 +00008282 }
8283
bellard7c9d8e02005-11-15 22:16:05 +00008284 for(i = 0;i < nb_net_clients; i++) {
8285 if (net_client_init(net_clients[i]) < 0)
8286 exit(1);
bellard702c6512004-04-02 21:21:32 +00008287 }
blueswir1833c7172007-05-27 19:36:43 +00008288 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8289 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
8290 continue;
8291 if (vlan->nb_guest_devs == 0) {
8292 fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
8293 exit(1);
8294 }
8295 if (vlan->nb_host_devs == 0)
8296 fprintf(stderr,
8297 "Warning: vlan %d is not connected to host network\n",
8298 vlan->id);
8299 }
bellardf1510b22003-06-25 00:07:40 +00008300
thseec85c22007-01-05 17:41:07 +00008301#ifdef TARGET_I386
balrog6ac0e822007-10-31 01:54:04 +00008302 if (strchr(boot_device, 'n')) {
thseec85c22007-01-05 17:41:07 +00008303 for (i = 0; i < nb_nics; i++) {
8304 const char *model = nd_table[i].model;
8305 char buf[1024];
8306 if (model == NULL)
8307 model = "ne2k_pci";
8308 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
8309 if (get_image_size(buf) > 0) {
8310 option_rom[nb_option_roms] = strdup(buf);
8311 nb_option_roms++;
8312 break;
8313 }
8314 }
8315 if (i == nb_nics) {
8316 fprintf(stderr, "No valid PXE rom found for network device\n");
8317 exit(1);
8318 }
thseec85c22007-01-05 17:41:07 +00008319 }
8320#endif
8321
bellard0824d6f2003-06-24 13:42:40 +00008322 /* init the memory */
bellard970ac5a2007-02-08 23:09:59 +00008323 phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
ths9ae02552007-01-05 17:39:04 +00008324
bellardd993e022005-02-10 22:00:06 +00008325 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00008326 if (!phys_ram_base) {
8327 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00008328 exit(1);
8329 }
8330
ths96d30e42007-01-07 20:42:14 +00008331 /* we always create the cdrom drive, even if no disk is there */
bellard5905b2e2004-08-01 21:53:26 +00008332 bdrv_init();
ths96d30e42007-01-07 20:42:14 +00008333 if (cdrom_index >= 0) {
8334 bs_table[cdrom_index] = bdrv_new("cdrom");
8335 bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM);
bellardc4b1fcc2004-03-14 21:44:30 +00008336 }
8337
ths96d30e42007-01-07 20:42:14 +00008338 /* open the virtual block devices */
8339 for(i = 0; i < MAX_DISKS; i++) {
8340 if (hd_filename[i]) {
8341 if (!bs_table[i]) {
8342 char buf[64];
8343 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
8344 bs_table[i] = bdrv_new(buf);
8345 }
8346 if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8347 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
8348 hd_filename[i]);
8349 exit(1);
8350 }
8351 if (i == 0 && cyls != 0) {
8352 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
8353 bdrv_set_translation_hint(bs_table[i], translation);
8354 }
8355 }
bellardc4b1fcc2004-03-14 21:44:30 +00008356 }
8357
8358 /* we always create at least one floppy disk */
8359 fd_table[0] = bdrv_new("fda");
8360 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
8361
8362 for(i = 0; i < MAX_FD; i++) {
8363 if (fd_filename[i]) {
8364 if (!fd_table[i]) {
8365 char buf[64];
8366 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
8367 fd_table[i] = bdrv_new(buf);
8368 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
8369 }
pbrooka1bb27b2007-04-06 16:49:48 +00008370 if (fd_filename[i][0] != '\0') {
bellard83f64092006-08-01 16:21:11 +00008371 if (bdrv_open(fd_table[i], fd_filename[i],
8372 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
bellardc20709a2004-04-21 23:27:19 +00008373 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
bellardc4b1fcc2004-03-14 21:44:30 +00008374 fd_filename[i]);
8375 exit(1);
8376 }
8377 }
bellard33e39632003-07-06 17:15:21 +00008378 }
8379 }
8380
balrog2bac6012007-04-30 01:34:31 +00008381 /* Open the virtual parallel flash block devices */
j_mayer86f55662007-04-24 06:52:59 +00008382 for(i = 0; i < MAX_PFLASH; i++) {
8383 if (pflash_filename[i]) {
8384 if (!pflash_table[i]) {
8385 char buf[64];
8386 snprintf(buf, sizeof(buf), "fl%c", i + 'a');
8387 pflash_table[i] = bdrv_new(buf);
8388 }
8389 if (bdrv_open(pflash_table[i], pflash_filename[i],
8390 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8391 fprintf(stderr, "qemu: could not open flash image '%s'\n",
8392 pflash_filename[i]);
8393 exit(1);
8394 }
8395 }
8396 }
8397
pbrooka1bb27b2007-04-06 16:49:48 +00008398 sd_bdrv = bdrv_new ("sd");
8399 /* FIXME: This isn't really a floppy, but it's a reasonable
8400 approximation. */
8401 bdrv_set_type_hint(sd_bdrv, BDRV_TYPE_FLOPPY);
8402 if (sd_filename) {
8403 if (bdrv_open(sd_bdrv, sd_filename,
8404 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8405 fprintf(stderr, "qemu: could not open SD card image %s\n",
8406 sd_filename);
balrog2bac6012007-04-30 01:34:31 +00008407 } else
balroga171fe32007-04-30 01:48:07 +00008408 qemu_key_check(sd_bdrv, sd_filename);
pbrooka1bb27b2007-04-06 16:49:48 +00008409 }
8410
balrog3e3d5812007-04-30 02:09:25 +00008411 if (mtd_filename) {
8412 mtd_bdrv = bdrv_new ("mtd");
8413 if (bdrv_open(mtd_bdrv, mtd_filename,
8414 snapshot ? BDRV_O_SNAPSHOT : 0) < 0 ||
8415 qemu_key_check(mtd_bdrv, mtd_filename)) {
8416 fprintf(stderr, "qemu: could not open Flash image %s\n",
8417 mtd_filename);
8418 bdrv_delete(mtd_bdrv);
8419 mtd_bdrv = 0;
8420 }
8421 }
8422
bellardc88676f2006-08-06 13:36:11 +00008423 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
8424 register_savevm("ram", 0, 2, ram_save, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00008425
bellard330d0412003-07-26 18:11:40 +00008426 init_ioports();
bellard0824d6f2003-06-24 13:42:40 +00008427
bellard313aa562003-08-10 21:52:11 +00008428 /* terminal init */
ths740733b2007-06-08 01:57:56 +00008429 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00008430 if (nographic) {
ths2ff89792007-06-21 23:34:19 +00008431 /* nearly nothing to do */
8432 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00008433 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00008434 vnc_display_init(ds);
8435 if (vnc_display_open(ds, vnc_display) < 0)
8436 exit(1);
bellard313aa562003-08-10 21:52:11 +00008437 } else {
bellard5b0753e2005-03-01 21:37:28 +00008438#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00008439 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00008440#elif defined(CONFIG_COCOA)
8441 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00008442#endif
8443 }
bellard0824d6f2003-06-24 13:42:40 +00008444
ths20d8a3e2007-02-18 17:04:49 +00008445 /* Maintain compatibility with multiple stdio monitors */
8446 if (!strcmp(monitor_device,"stdio")) {
8447 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
8448 if (!strcmp(serial_devices[i],"mon:stdio")) {
8449 monitor_device[0] = '\0';
8450 break;
8451 } else if (!strcmp(serial_devices[i],"stdio")) {
8452 monitor_device[0] = '\0';
8453 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio");
8454 break;
8455 }
8456 }
bellard82c643f2004-07-14 17:28:13 +00008457 }
ths20d8a3e2007-02-18 17:04:49 +00008458 if (monitor_device[0] != '\0') {
8459 monitor_hd = qemu_chr_open(monitor_device);
8460 if (!monitor_hd) {
8461 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
8462 exit(1);
8463 }
8464 monitor_init(monitor_hd, !nographic);
8465 }
bellard82c643f2004-07-14 17:28:13 +00008466
bellard8d11df92004-08-24 21:13:40 +00008467 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008468 const char *devname = serial_devices[i];
8469 if (devname[0] != '\0' && strcmp(devname, "none")) {
8470 serial_hds[i] = qemu_chr_open(devname);
bellard8d11df92004-08-24 21:13:40 +00008471 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008472 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008473 devname);
bellard8d11df92004-08-24 21:13:40 +00008474 exit(1);
8475 }
thsaf3a9032007-07-11 23:14:59 +00008476 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008477 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00008478 }
bellard82c643f2004-07-14 17:28:13 +00008479 }
bellard82c643f2004-07-14 17:28:13 +00008480
bellard6508fe52005-01-15 12:02:56 +00008481 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008482 const char *devname = parallel_devices[i];
8483 if (devname[0] != '\0' && strcmp(devname, "none")) {
8484 parallel_hds[i] = qemu_chr_open(devname);
bellard6508fe52005-01-15 12:02:56 +00008485 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008486 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008487 devname);
bellard6508fe52005-01-15 12:02:56 +00008488 exit(1);
8489 }
thsaf3a9032007-07-11 23:14:59 +00008490 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008491 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00008492 }
8493 }
8494
bellardcc1daa42005-06-05 14:49:17 +00008495 machine->init(ram_size, vga_ram_size, boot_device,
8496 ds, fd_filename, snapshot,
j_mayer94fc95c2007-03-05 19:44:02 +00008497 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00008498
pbrook0d92ed32006-05-21 16:30:15 +00008499 /* init USB devices */
8500 if (usb_enabled) {
8501 for(i = 0; i < usb_devices_index; i++) {
8502 if (usb_device_add(usb_devices[i]) < 0) {
8503 fprintf(stderr, "Warning: could not add USB device %s\n",
8504 usb_devices[i]);
8505 }
8506 }
8507 }
8508
ths740733b2007-06-08 01:57:56 +00008509 if (display_state.dpy_refresh) {
8510 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
8511 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
8512 }
bellard7f7f9872003-10-30 01:11:23 +00008513
bellard67b915a2004-03-31 23:37:16 +00008514#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00008515 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00008516 /* XXX: use standard host:port notation and modify options
8517 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00008518 if (gdbserver_start(gdbstub_port) < 0) {
8519 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00008520 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00008521 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00008522 }
balrog45669e02007-07-02 13:20:17 +00008523 }
bellard67b915a2004-03-31 23:37:16 +00008524#endif
balrog45669e02007-07-02 13:20:17 +00008525
bellardd63d3072004-10-03 13:29:03 +00008526 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00008527 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00008528
bellard67b915a2004-03-31 23:37:16 +00008529 {
bellard5905b2e2004-08-01 21:53:26 +00008530 /* XXX: simplify init */
8531 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00008532 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00008533 vm_start();
8534 }
bellard0824d6f2003-06-24 13:42:40 +00008535 }
thsffd843b2006-12-21 19:46:43 +00008536
ths71e3ceb2006-12-22 02:11:31 +00008537 if (daemonize) {
8538 uint8_t status = 0;
8539 ssize_t len;
8540 int fd;
8541
8542 again1:
8543 len = write(fds[1], &status, 1);
8544 if (len == -1 && (errno == EINTR))
8545 goto again1;
8546
8547 if (len != 1)
8548 exit(1);
8549
balrogaeb30be2007-07-02 15:03:13 +00008550 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00008551 if (fd == -1)
8552 exit(1);
8553
8554 dup2(fd, 0);
8555 dup2(fd, 1);
8556 dup2(fd, 2);
8557
8558 close(fd);
8559 }
8560
bellard8a7ddc32004-03-31 19:00:16 +00008561 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00008562 quit_timers();
thsb46a8902007-10-21 23:20:45 +00008563
ths7d294b62007-10-26 17:21:58 +00008564#if !defined(_WIN32)
thsb46a8902007-10-21 23:20:45 +00008565 /* close network clients */
8566 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8567 VLANClientState *vc;
8568
ths7d294b62007-10-26 17:21:58 +00008569 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
thsb46a8902007-10-21 23:20:45 +00008570 if (vc->fd_read == tap_receive) {
8571 char ifname[64];
8572 TAPState *s = vc->opaque;
8573
8574 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
8575 s->down_script[0])
8576 launch_script(s->down_script, ifname, s->fd);
8577 }
thsb46a8902007-10-21 23:20:45 +00008578 }
ths7d294b62007-10-26 17:21:58 +00008579 }
8580#endif
bellard0824d6f2003-06-24 13:42:40 +00008581 return 0;
8582}