blob: b01947fbb1059249ba9715e3cab1efc9203b67c8 [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"
pbrooka14d6c82006-12-23 15:37:33 +0000120#ifdef __sun__
121#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
122#else
123#define SMBD_COMMAND "/usr/sbin/smbd"
124#endif
bellardf1510b22003-06-25 00:07:40 +0000125
bellard0824d6f2003-06-24 13:42:40 +0000126//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000127//#define DEBUG_IOPORT
bellard330d0412003-07-26 18:11:40 +0000128
bellardbb551fa2004-01-24 13:42:26 +0000129#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
bellard7916e222003-07-01 16:27:45 +0000130
bellard77d4bc32004-05-26 22:13:53 +0000131#ifdef TARGET_PPC
132#define DEFAULT_RAM_SIZE 144
133#else
bellard1bfe8562004-07-08 21:17:50 +0000134#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000135#endif
bellard8a7ddc32004-03-31 19:00:16 +0000136/* in ms */
137#define GUI_REFRESH_INTERVAL 30
bellard313aa562003-08-10 21:52:11 +0000138
pbrook0d92ed32006-05-21 16:30:15 +0000139/* Max number of USB devices that can be specified on the commandline. */
140#define MAX_USB_CMDLINE 8
141
bellard7dea1da2003-11-16 15:59:30 +0000142/* XXX: use a two level table to limit memory usage */
143#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000144
bellard80cabfa2004-03-14 12:20:30 +0000145const char *bios_dir = CONFIG_QEMU_SHAREDIR;
bellard0824d6f2003-06-24 13:42:40 +0000146char phys_ram_file[1024];
bellardc4b1fcc2004-03-14 21:44:30 +0000147void *ioport_opaque[MAX_IOPORTS];
bellardfc01f7e2003-06-30 10:03:06 +0000148IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
149IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
bellardfaea38e2006-08-05 21:31:00 +0000150/* Note: bs_table[MAX_DISKS] is a dummy block driver if none available
151 to store the VM snapshots */
152BlockDriverState *bs_table[MAX_DISKS + 1], *fd_table[MAX_FD];
j_mayer86f55662007-04-24 06:52:59 +0000153BlockDriverState *pflash_table[MAX_PFLASH];
pbrooka1bb27b2007-04-06 16:49:48 +0000154BlockDriverState *sd_bdrv;
balrog3e3d5812007-04-30 02:09:25 +0000155BlockDriverState *mtd_bdrv;
bellardfaea38e2006-08-05 21:31:00 +0000156/* point to the block driver where the snapshots are managed */
157BlockDriverState *bs_snapshots;
bellard313aa562003-08-10 21:52:11 +0000158int vga_ram_size;
159static DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000160int nographic;
bellard3d11d0e2004-12-12 16:56:30 +0000161const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000162int64_t ticks_per_sec;
bellard36b486b2003-11-11 13:36:08 +0000163int boot_device = 'c';
bellard0ced6582004-05-23 21:06:12 +0000164int ram_size;
bellard80cabfa2004-03-14 12:20:30 +0000165int pit_min_timer_count = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000166int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000167NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000168int vm_running;
bellardee22c2f2004-06-03 12:49:50 +0000169int rtc_utc = 1;
bellard1bfe8562004-07-08 21:17:50 +0000170int cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +0000171int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000172#ifdef TARGET_SPARC
173int graphic_width = 1024;
174int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000175int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000176#else
bellard1bfe8562004-07-08 21:17:50 +0000177int graphic_width = 800;
178int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000179int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000180#endif
bellardd63d3072004-10-03 13:29:03 +0000181int full_screen = 0;
ths43523e92007-02-18 18:19:32 +0000182int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000183int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000184CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000185CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000186#ifdef TARGET_I386
187int win2k_install_hack = 0;
188#endif
bellardbb36d472005-11-05 14:22:28 +0000189int usb_enabled = 0;
bellard7c9d8e02005-11-15 22:16:05 +0000190static VLANState *first_vlan;
bellard6a00d602005-11-21 23:25:50 +0000191int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000192const char *vnc_display;
bellardd3e9db92005-12-17 01:27:28 +0000193#if defined(TARGET_SPARC)
bellardba3c64f2005-12-05 20:31:52 +0000194#define MAX_CPUS 16
bellardd3e9db92005-12-17 01:27:28 +0000195#elif defined(TARGET_I386)
196#define MAX_CPUS 255
bellardba3c64f2005-12-05 20:31:52 +0000197#else
bellardd3e9db92005-12-17 01:27:28 +0000198#define MAX_CPUS 1
bellardba3c64f2005-12-05 20:31:52 +0000199#endif
bellard6515b202006-05-03 22:02:44 +0000200int acpi_enabled = 1;
bellard52ca8d62006-06-14 16:03:05 +0000201int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000202int no_reboot = 0;
balrog9467cd42007-05-01 01:34:14 +0000203int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000204int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000205int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000206const char *option_rom[MAX_OPTION_ROMS];
207int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000208int semihosting_enabled = 0;
pbrook3c07f8e2007-01-21 16:47:01 +0000209int autostart = 1;
balrog2b8f2d42007-07-27 22:08:46 +0000210#ifdef TARGET_ARM
211int old_param = 0;
212#endif
thsc35734b2007-03-19 15:17:08 +0000213const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000214int alt_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000215#ifdef TARGET_SPARC
216unsigned int nb_prom_envs = 0;
217const char *prom_envs[MAX_PROM_ENVS];
218#endif
bellard0824d6f2003-06-24 13:42:40 +0000219
balrogaeb30be2007-07-02 15:03:13 +0000220#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
221
bellard0824d6f2003-06-24 13:42:40 +0000222/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000223/* x86 ISA bus support */
224
225target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000226PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000227
bellardc4b1fcc2004-03-14 21:44:30 +0000228uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000229{
230#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000231 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000232#endif
bellardfc01f7e2003-06-30 10:03:06 +0000233 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000234}
235
bellardc4b1fcc2004-03-14 21:44:30 +0000236void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000237{
238#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000239 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000240#endif
241}
242
243/* default is to make two byte accesses */
bellardc4b1fcc2004-03-14 21:44:30 +0000244uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000245{
246 uint32_t data;
bellarddb45c292004-05-12 19:50:26 +0000247 data = ioport_read_table[0][address](ioport_opaque[address], address);
248 address = (address + 1) & (MAX_IOPORTS - 1);
249 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000250 return data;
251}
252
bellardc4b1fcc2004-03-14 21:44:30 +0000253void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000254{
bellarddb45c292004-05-12 19:50:26 +0000255 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
256 address = (address + 1) & (MAX_IOPORTS - 1);
257 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000258}
259
bellardc4b1fcc2004-03-14 21:44:30 +0000260uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000261{
262#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000263 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000264#endif
265 return 0xffffffff;
266}
267
bellardc4b1fcc2004-03-14 21:44:30 +0000268void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000269{
270#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000271 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000272#endif
bellard0824d6f2003-06-24 13:42:40 +0000273}
274
275void init_ioports(void)
276{
277 int i;
278
279 for(i = 0; i < MAX_IOPORTS; i++) {
bellardfc01f7e2003-06-30 10:03:06 +0000280 ioport_read_table[0][i] = default_ioport_readb;
281 ioport_write_table[0][i] = default_ioport_writeb;
282 ioport_read_table[1][i] = default_ioport_readw;
283 ioport_write_table[1][i] = default_ioport_writew;
284 ioport_read_table[2][i] = default_ioport_readl;
285 ioport_write_table[2][i] = default_ioport_writel;
bellard0824d6f2003-06-24 13:42:40 +0000286 }
287}
288
bellardfc01f7e2003-06-30 10:03:06 +0000289/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000290int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000291 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000292{
bellardfc01f7e2003-06-30 10:03:06 +0000293 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000294
bellardc4b1fcc2004-03-14 21:44:30 +0000295 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000296 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000297 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000298 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000299 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000300 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000301 } else {
302 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000303 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000304 }
305 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000306 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000307 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
308 hw_error("register_ioport_read: invalid opaque");
309 ioport_opaque[i] = opaque;
310 }
bellard0824d6f2003-06-24 13:42:40 +0000311 return 0;
312}
313
bellardfc01f7e2003-06-30 10:03:06 +0000314/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000315int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000316 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000317{
bellardfc01f7e2003-06-30 10:03:06 +0000318 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000319
bellardc4b1fcc2004-03-14 21:44:30 +0000320 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000321 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000322 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000323 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000324 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000325 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000326 } else {
327 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000328 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000329 }
330 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000331 ioport_write_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000332 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
ths52f61fd2006-12-22 21:20:52 +0000333 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000334 ioport_opaque[i] = opaque;
335 }
bellardf1510b22003-06-25 00:07:40 +0000336 return 0;
337}
338
bellard69b91032004-05-18 23:05:28 +0000339void isa_unassign_ioport(int start, int length)
340{
341 int i;
342
343 for(i = start; i < start + length; i++) {
344 ioport_read_table[0][i] = default_ioport_readb;
345 ioport_read_table[1][i] = default_ioport_readw;
346 ioport_read_table[2][i] = default_ioport_readl;
347
348 ioport_write_table[0][i] = default_ioport_writeb;
349 ioport_write_table[1][i] = default_ioport_writew;
350 ioport_write_table[2][i] = default_ioport_writel;
351 }
352}
353
bellard20f32282005-01-03 23:36:21 +0000354/***********************************************************/
355
bellardc45886d2004-01-05 00:02:06 +0000356void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000357{
bellardfd872592004-05-12 19:11:15 +0000358#ifdef DEBUG_IOPORT
359 if (loglevel & CPU_LOG_IOPORT)
360 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000361#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000362 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000363#ifdef USE_KQEMU
364 if (env)
365 env->last_io_time = cpu_get_time_fast();
366#endif
bellard0824d6f2003-06-24 13:42:40 +0000367}
368
bellardc45886d2004-01-05 00:02:06 +0000369void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000370{
bellardfd872592004-05-12 19:11:15 +0000371#ifdef DEBUG_IOPORT
372 if (loglevel & CPU_LOG_IOPORT)
373 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000374#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000375 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000376#ifdef USE_KQEMU
377 if (env)
378 env->last_io_time = cpu_get_time_fast();
379#endif
bellard0824d6f2003-06-24 13:42:40 +0000380}
381
bellardc45886d2004-01-05 00:02:06 +0000382void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000383{
bellardfd872592004-05-12 19:11:15 +0000384#ifdef DEBUG_IOPORT
385 if (loglevel & CPU_LOG_IOPORT)
386 fprintf(logfile, "outl: %04x %08x\n", addr, val);
387#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000388 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000389#ifdef USE_KQEMU
390 if (env)
391 env->last_io_time = cpu_get_time_fast();
392#endif
bellard0824d6f2003-06-24 13:42:40 +0000393}
394
bellardc45886d2004-01-05 00:02:06 +0000395int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000396{
bellardfd872592004-05-12 19:11:15 +0000397 int val;
bellardfd872592004-05-12 19:11:15 +0000398 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
399#ifdef DEBUG_IOPORT
400 if (loglevel & CPU_LOG_IOPORT)
401 fprintf(logfile, "inb : %04x %02x\n", addr, val);
402#endif
bellard89bfc102006-02-08 22:46:31 +0000403#ifdef USE_KQEMU
404 if (env)
405 env->last_io_time = cpu_get_time_fast();
406#endif
bellardfd872592004-05-12 19:11:15 +0000407 return val;
bellard0824d6f2003-06-24 13:42:40 +0000408}
409
bellardc45886d2004-01-05 00:02:06 +0000410int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000411{
bellardfd872592004-05-12 19:11:15 +0000412 int val;
bellardfd872592004-05-12 19:11:15 +0000413 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
414#ifdef DEBUG_IOPORT
415 if (loglevel & CPU_LOG_IOPORT)
416 fprintf(logfile, "inw : %04x %04x\n", addr, val);
417#endif
bellard89bfc102006-02-08 22:46:31 +0000418#ifdef USE_KQEMU
419 if (env)
420 env->last_io_time = cpu_get_time_fast();
421#endif
bellardfd872592004-05-12 19:11:15 +0000422 return val;
bellard0824d6f2003-06-24 13:42:40 +0000423}
424
bellardc45886d2004-01-05 00:02:06 +0000425int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000426{
bellardfd872592004-05-12 19:11:15 +0000427 int val;
bellardfd872592004-05-12 19:11:15 +0000428 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
429#ifdef DEBUG_IOPORT
430 if (loglevel & CPU_LOG_IOPORT)
431 fprintf(logfile, "inl : %04x %08x\n", addr, val);
432#endif
bellard89bfc102006-02-08 22:46:31 +0000433#ifdef USE_KQEMU
434 if (env)
435 env->last_io_time = cpu_get_time_fast();
436#endif
bellardfd872592004-05-12 19:11:15 +0000437 return val;
bellard0824d6f2003-06-24 13:42:40 +0000438}
439
440/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000441void hw_error(const char *fmt, ...)
442{
443 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000444 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000445
446 va_start(ap, fmt);
447 fprintf(stderr, "qemu: hardware error: ");
448 vfprintf(stderr, fmt, ap);
449 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000450 for(env = first_cpu; env != NULL; env = env->next_cpu) {
451 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000452#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000453 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000454#else
bellard6a00d602005-11-21 23:25:50 +0000455 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000456#endif
bellard6a00d602005-11-21 23:25:50 +0000457 }
bellard0824d6f2003-06-24 13:42:40 +0000458 va_end(ap);
459 abort();
460}
461
bellard8a7ddc32004-03-31 19:00:16 +0000462/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000463/* keyboard/mouse */
464
465static QEMUPutKBDEvent *qemu_put_kbd_event;
466static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000467static QEMUPutMouseEntry *qemu_put_mouse_event_head;
468static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000469
470void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
471{
472 qemu_put_kbd_event_opaque = opaque;
473 qemu_put_kbd_event = func;
474}
475
ths455204e2007-01-05 16:42:13 +0000476QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
477 void *opaque, int absolute,
478 const char *name)
bellard63066f42004-06-03 18:45:02 +0000479{
ths455204e2007-01-05 16:42:13 +0000480 QEMUPutMouseEntry *s, *cursor;
481
482 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
483 if (!s)
484 return NULL;
485
486 s->qemu_put_mouse_event = func;
487 s->qemu_put_mouse_event_opaque = opaque;
488 s->qemu_put_mouse_event_absolute = absolute;
489 s->qemu_put_mouse_event_name = qemu_strdup(name);
490 s->next = NULL;
491
492 if (!qemu_put_mouse_event_head) {
493 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
494 return s;
495 }
496
497 cursor = qemu_put_mouse_event_head;
498 while (cursor->next != NULL)
499 cursor = cursor->next;
500
501 cursor->next = s;
502 qemu_put_mouse_event_current = s;
503
504 return s;
505}
506
507void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
508{
509 QEMUPutMouseEntry *prev = NULL, *cursor;
510
511 if (!qemu_put_mouse_event_head || entry == NULL)
512 return;
513
514 cursor = qemu_put_mouse_event_head;
515 while (cursor != NULL && cursor != entry) {
516 prev = cursor;
517 cursor = cursor->next;
518 }
519
520 if (cursor == NULL) // does not exist or list empty
521 return;
522 else if (prev == NULL) { // entry is head
523 qemu_put_mouse_event_head = cursor->next;
524 if (qemu_put_mouse_event_current == entry)
525 qemu_put_mouse_event_current = cursor->next;
526 qemu_free(entry->qemu_put_mouse_event_name);
527 qemu_free(entry);
528 return;
529 }
530
531 prev->next = entry->next;
532
533 if (qemu_put_mouse_event_current == entry)
534 qemu_put_mouse_event_current = prev;
535
536 qemu_free(entry->qemu_put_mouse_event_name);
537 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000538}
539
540void kbd_put_keycode(int keycode)
541{
542 if (qemu_put_kbd_event) {
543 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
544 }
545}
546
547void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
548{
ths455204e2007-01-05 16:42:13 +0000549 QEMUPutMouseEvent *mouse_event;
550 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000551 int width;
ths455204e2007-01-05 16:42:13 +0000552
553 if (!qemu_put_mouse_event_current) {
554 return;
555 }
556
557 mouse_event =
558 qemu_put_mouse_event_current->qemu_put_mouse_event;
559 mouse_event_opaque =
560 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
561
562 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000563 if (graphic_rotate) {
564 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
565 width = 0x7fff;
566 else
567 width = graphic_width;
568 mouse_event(mouse_event_opaque,
569 width - dy, dx, dz, buttons_state);
570 } else
571 mouse_event(mouse_event_opaque,
572 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000573 }
574}
575
bellard09b26c52006-04-12 21:09:08 +0000576int kbd_mouse_is_absolute(void)
577{
ths455204e2007-01-05 16:42:13 +0000578 if (!qemu_put_mouse_event_current)
579 return 0;
580
581 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
582}
583
584void do_info_mice(void)
585{
586 QEMUPutMouseEntry *cursor;
587 int index = 0;
588
589 if (!qemu_put_mouse_event_head) {
590 term_printf("No mouse devices connected\n");
591 return;
592 }
593
594 term_printf("Mouse devices available:\n");
595 cursor = qemu_put_mouse_event_head;
596 while (cursor != NULL) {
597 term_printf("%c Mouse #%d: %s\n",
598 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
599 index, cursor->qemu_put_mouse_event_name);
600 index++;
601 cursor = cursor->next;
602 }
603}
604
605void do_mouse_set(int index)
606{
607 QEMUPutMouseEntry *cursor;
608 int i = 0;
609
610 if (!qemu_put_mouse_event_head) {
611 term_printf("No mouse devices connected\n");
612 return;
613 }
614
615 cursor = qemu_put_mouse_event_head;
616 while (cursor != NULL && index != i) {
617 i++;
618 cursor = cursor->next;
619 }
620
621 if (cursor != NULL)
622 qemu_put_mouse_event_current = cursor;
623 else
624 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000625}
626
bellard87858c82003-06-27 12:01:39 +0000627/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000628uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000629{
630 union {
631 uint64_t ll;
632 struct {
633#ifdef WORDS_BIGENDIAN
634 uint32_t high, low;
635#else
636 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000637#endif
bellard87858c82003-06-27 12:01:39 +0000638 } l;
639 } u, res;
640 uint64_t rl, rh;
641
642 u.ll = a;
643 rl = (uint64_t)u.l.low * (uint64_t)b;
644 rh = (uint64_t)u.l.high * (uint64_t)b;
645 rh += (rl >> 32);
646 res.l.high = rh / c;
647 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
648 return res.ll;
649}
650
bellard1dce7c32006-07-13 23:20:22 +0000651/***********************************************************/
652/* real time host monotonic timer */
653
654#define QEMU_TIMER_BASE 1000000000LL
655
656#ifdef WIN32
657
658static int64_t clock_freq;
659
660static void init_get_clock(void)
661{
bellarda8e5ac32006-07-14 09:36:13 +0000662 LARGE_INTEGER freq;
663 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000664 ret = QueryPerformanceFrequency(&freq);
665 if (ret == 0) {
666 fprintf(stderr, "Could not calibrate ticks\n");
667 exit(1);
668 }
669 clock_freq = freq.QuadPart;
670}
671
672static int64_t get_clock(void)
673{
674 LARGE_INTEGER ti;
675 QueryPerformanceCounter(&ti);
676 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
677}
678
679#else
680
681static int use_rt_clock;
682
683static void init_get_clock(void)
684{
685 use_rt_clock = 0;
686#if defined(__linux__)
687 {
688 struct timespec ts;
689 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
690 use_rt_clock = 1;
691 }
692 }
693#endif
694}
695
696static int64_t get_clock(void)
697{
698#if defined(__linux__)
699 if (use_rt_clock) {
700 struct timespec ts;
701 clock_gettime(CLOCK_MONOTONIC, &ts);
702 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000703 } else
bellard1dce7c32006-07-13 23:20:22 +0000704#endif
705 {
706 /* XXX: using gettimeofday leads to problems if the date
707 changes, so it should be avoided. */
708 struct timeval tv;
709 gettimeofday(&tv, NULL);
710 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
711 }
712}
713
714#endif
715
716/***********************************************************/
717/* guest cycle counter */
718
719static int64_t cpu_ticks_prev;
720static int64_t cpu_ticks_offset;
721static int64_t cpu_clock_offset;
722static int cpu_ticks_enabled;
723
724/* return the host CPU cycle counter and handle stop/restart */
725int64_t cpu_get_ticks(void)
726{
727 if (!cpu_ticks_enabled) {
728 return cpu_ticks_offset;
729 } else {
730 int64_t ticks;
731 ticks = cpu_get_real_ticks();
732 if (cpu_ticks_prev > ticks) {
733 /* Note: non increasing ticks may happen if the host uses
734 software suspend */
735 cpu_ticks_offset += cpu_ticks_prev - ticks;
736 }
737 cpu_ticks_prev = ticks;
738 return ticks + cpu_ticks_offset;
739 }
740}
741
742/* return the host CPU monotonic timer and handle stop/restart */
743static int64_t cpu_get_clock(void)
744{
745 int64_t ti;
746 if (!cpu_ticks_enabled) {
747 return cpu_clock_offset;
748 } else {
749 ti = get_clock();
750 return ti + cpu_clock_offset;
751 }
752}
753
754/* enable cpu_get_ticks() */
755void cpu_enable_ticks(void)
756{
757 if (!cpu_ticks_enabled) {
758 cpu_ticks_offset -= cpu_get_real_ticks();
759 cpu_clock_offset -= get_clock();
760 cpu_ticks_enabled = 1;
761 }
762}
763
764/* disable cpu_get_ticks() : the clock is stopped. You must not call
765 cpu_get_ticks() after that. */
766void cpu_disable_ticks(void)
767{
768 if (cpu_ticks_enabled) {
769 cpu_ticks_offset = cpu_get_ticks();
770 cpu_clock_offset = cpu_get_clock();
771 cpu_ticks_enabled = 0;
772 }
773}
774
775/***********************************************************/
776/* timers */
ths5fafdf22007-09-16 21:08:06 +0000777
bellard8a7ddc32004-03-31 19:00:16 +0000778#define QEMU_TIMER_REALTIME 0
779#define QEMU_TIMER_VIRTUAL 1
780
781struct QEMUClock {
782 int type;
783 /* XXX: add frequency */
784};
785
786struct QEMUTimer {
787 QEMUClock *clock;
788 int64_t expire_time;
789 QEMUTimerCB *cb;
790 void *opaque;
791 struct QEMUTimer *next;
792};
793
thsc8994012007-08-19 21:56:03 +0000794struct qemu_alarm_timer {
795 char const *name;
thsefe75412007-08-24 01:36:32 +0000796 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000797
798 int (*start)(struct qemu_alarm_timer *t);
799 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000800 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000801 void *priv;
802};
803
thsefe75412007-08-24 01:36:32 +0000804#define ALARM_FLAG_DYNTICKS 0x1
805
806static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
807{
808 return t->flags & ALARM_FLAG_DYNTICKS;
809}
810
811static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
812{
813 if (!alarm_has_dynticks(t))
814 return;
815
816 t->rearm(t);
817}
818
819/* TODO: MIN_TIMER_REARM_US should be optimized */
820#define MIN_TIMER_REARM_US 250
821
thsc8994012007-08-19 21:56:03 +0000822static struct qemu_alarm_timer *alarm_timer;
823
824#ifdef _WIN32
825
826struct qemu_alarm_win32 {
827 MMRESULT timerId;
828 HANDLE host_alarm;
829 unsigned int period;
830} alarm_win32_data = {0, NULL, -1};
831
832static int win32_start_timer(struct qemu_alarm_timer *t);
833static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000834static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000835
836#else
837
838static int unix_start_timer(struct qemu_alarm_timer *t);
839static void unix_stop_timer(struct qemu_alarm_timer *t);
840
ths231c6582007-08-26 17:29:15 +0000841#ifdef __linux__
842
thsefe75412007-08-24 01:36:32 +0000843static int dynticks_start_timer(struct qemu_alarm_timer *t);
844static void dynticks_stop_timer(struct qemu_alarm_timer *t);
845static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
846
thsc40ec5a2007-08-19 22:09:40 +0000847static int hpet_start_timer(struct qemu_alarm_timer *t);
848static void hpet_stop_timer(struct qemu_alarm_timer *t);
849
thsc8994012007-08-19 21:56:03 +0000850static int rtc_start_timer(struct qemu_alarm_timer *t);
851static void rtc_stop_timer(struct qemu_alarm_timer *t);
852
thsefe75412007-08-24 01:36:32 +0000853#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000854
855#endif /* _WIN32 */
856
857static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000858#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000859#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000860 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
861 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000862 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000863 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000864 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000865 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000866#endif
thsefe75412007-08-24 01:36:32 +0000867 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000868#else
thsefe75412007-08-24 01:36:32 +0000869 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
870 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
871 {"win32", 0, win32_start_timer,
872 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000873#endif
874 {NULL, }
875};
876
thsf3dcfad2007-08-24 01:26:02 +0000877static void show_available_alarms()
878{
879 int i;
880
881 printf("Available alarm timers, in order of precedence:\n");
882 for (i = 0; alarm_timers[i].name; i++)
883 printf("%s\n", alarm_timers[i].name);
884}
885
886static void configure_alarms(char const *opt)
887{
888 int i;
889 int cur = 0;
890 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
891 char *arg;
892 char *name;
893
894 if (!strcmp(opt, "help")) {
895 show_available_alarms();
896 exit(0);
897 }
898
899 arg = strdup(opt);
900
901 /* Reorder the array */
902 name = strtok(arg, ",");
903 while (name) {
904 struct qemu_alarm_timer tmp;
905
906 for (i = 0; i < count; i++) {
907 if (!strcmp(alarm_timers[i].name, name))
908 break;
909 }
910
911 if (i == count) {
912 fprintf(stderr, "Unknown clock %s\n", name);
913 goto next;
914 }
915
916 if (i < cur)
917 /* Ignore */
918 goto next;
919
920 /* Swap */
921 tmp = alarm_timers[i];
922 alarm_timers[i] = alarm_timers[cur];
923 alarm_timers[cur] = tmp;
924
925 cur++;
926next:
927 name = strtok(NULL, ",");
928 }
929
930 free(arg);
931
932 if (cur) {
933 /* Disable remaining timers */
934 for (i = cur; i < count; i++)
935 alarm_timers[i].name = NULL;
936 }
937
938 /* debug */
939 show_available_alarms();
940}
941
bellard8a7ddc32004-03-31 19:00:16 +0000942QEMUClock *rt_clock;
943QEMUClock *vm_clock;
944
945static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000946
947QEMUClock *qemu_new_clock(int type)
948{
949 QEMUClock *clock;
950 clock = qemu_mallocz(sizeof(QEMUClock));
951 if (!clock)
952 return NULL;
953 clock->type = type;
954 return clock;
955}
956
957QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
958{
959 QEMUTimer *ts;
960
961 ts = qemu_mallocz(sizeof(QEMUTimer));
962 ts->clock = clock;
963 ts->cb = cb;
964 ts->opaque = opaque;
965 return ts;
966}
967
968void qemu_free_timer(QEMUTimer *ts)
969{
970 qemu_free(ts);
971}
972
973/* stop a timer, but do not dealloc it */
974void qemu_del_timer(QEMUTimer *ts)
975{
976 QEMUTimer **pt, *t;
977
978 /* NOTE: this code must be signal safe because
979 qemu_timer_expired() can be called from a signal. */
980 pt = &active_timers[ts->clock->type];
981 for(;;) {
982 t = *pt;
983 if (!t)
984 break;
985 if (t == ts) {
986 *pt = t->next;
987 break;
988 }
989 pt = &t->next;
990 }
thsefe75412007-08-24 01:36:32 +0000991
992 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +0000993}
994
995/* modify the current timer so that it will be fired when current_time
996 >= expire_time. The corresponding callback will be called. */
997void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
998{
999 QEMUTimer **pt, *t;
1000
1001 qemu_del_timer(ts);
1002
1003 /* add the timer in the sorted list */
1004 /* NOTE: this code must be signal safe because
1005 qemu_timer_expired() can be called from a signal. */
1006 pt = &active_timers[ts->clock->type];
1007 for(;;) {
1008 t = *pt;
1009 if (!t)
1010 break;
ths5fafdf22007-09-16 21:08:06 +00001011 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001012 break;
1013 pt = &t->next;
1014 }
1015 ts->expire_time = expire_time;
1016 ts->next = *pt;
1017 *pt = ts;
1018}
1019
1020int qemu_timer_pending(QEMUTimer *ts)
1021{
1022 QEMUTimer *t;
1023 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1024 if (t == ts)
1025 return 1;
1026 }
1027 return 0;
1028}
1029
1030static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1031{
1032 if (!timer_head)
1033 return 0;
1034 return (timer_head->expire_time <= current_time);
1035}
1036
1037static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1038{
1039 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001040
bellard8a7ddc32004-03-31 19:00:16 +00001041 for(;;) {
1042 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001043 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001044 break;
1045 /* remove timer from the list before calling the callback */
1046 *ptimer_head = ts->next;
1047 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001048
bellard8a7ddc32004-03-31 19:00:16 +00001049 /* run the callback (the timer list can be modified) */
1050 ts->cb(ts->opaque);
1051 }
thsefe75412007-08-24 01:36:32 +00001052 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00001053}
1054
1055int64_t qemu_get_clock(QEMUClock *clock)
1056{
1057 switch(clock->type) {
1058 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001059 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001060 default:
1061 case QEMU_TIMER_VIRTUAL:
bellard1dce7c32006-07-13 23:20:22 +00001062 return cpu_get_clock();
bellard8a7ddc32004-03-31 19:00:16 +00001063 }
1064}
1065
bellard1dce7c32006-07-13 23:20:22 +00001066static void init_timers(void)
1067{
1068 init_get_clock();
1069 ticks_per_sec = QEMU_TIMER_BASE;
1070 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1071 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1072}
1073
bellard8a7ddc32004-03-31 19:00:16 +00001074/* save a timer */
1075void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1076{
1077 uint64_t expire_time;
1078
1079 if (qemu_timer_pending(ts)) {
1080 expire_time = ts->expire_time;
1081 } else {
1082 expire_time = -1;
1083 }
1084 qemu_put_be64(f, expire_time);
1085}
1086
1087void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1088{
1089 uint64_t expire_time;
1090
1091 expire_time = qemu_get_be64(f);
1092 if (expire_time != -1) {
1093 qemu_mod_timer(ts, expire_time);
1094 } else {
1095 qemu_del_timer(ts);
1096 }
1097}
1098
1099static void timer_save(QEMUFile *f, void *opaque)
1100{
1101 if (cpu_ticks_enabled) {
1102 hw_error("cannot save state if virtual timers are running");
1103 }
1104 qemu_put_be64s(f, &cpu_ticks_offset);
1105 qemu_put_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001106 qemu_put_be64s(f, &cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001107}
1108
1109static int timer_load(QEMUFile *f, void *opaque, int version_id)
1110{
bellardc88676f2006-08-06 13:36:11 +00001111 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001112 return -EINVAL;
1113 if (cpu_ticks_enabled) {
1114 return -EINVAL;
1115 }
1116 qemu_get_be64s(f, &cpu_ticks_offset);
1117 qemu_get_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001118 if (version_id == 2) {
1119 qemu_get_be64s(f, &cpu_clock_offset);
1120 }
bellard8a7ddc32004-03-31 19:00:16 +00001121 return 0;
1122}
1123
bellard67b915a2004-03-31 23:37:16 +00001124#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001125void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001126 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1127#else
bellard8a7ddc32004-03-31 19:00:16 +00001128static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001129#endif
bellard8a7ddc32004-03-31 19:00:16 +00001130{
bellard02ba45c2004-06-25 14:46:23 +00001131#if 0
1132#define DISP_FREQ 1000
1133 {
1134 static int64_t delta_min = INT64_MAX;
1135 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1136 static int count;
1137 ti = qemu_get_clock(vm_clock);
1138 if (last_clock != 0) {
1139 delta = ti - last_clock;
1140 if (delta < delta_min)
1141 delta_min = delta;
1142 if (delta > delta_max)
1143 delta_max = delta;
1144 delta_cum += delta;
1145 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001146 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001147 muldiv64(delta_min, 1000000, ticks_per_sec),
1148 muldiv64(delta_max, 1000000, ticks_per_sec),
1149 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1150 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1151 count = 0;
1152 delta_min = INT64_MAX;
1153 delta_max = 0;
1154 delta_cum = 0;
1155 }
1156 }
1157 last_clock = ti;
1158 }
1159#endif
thsefe75412007-08-24 01:36:32 +00001160 if (alarm_has_dynticks(alarm_timer) ||
1161 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
bellard8a7ddc32004-03-31 19:00:16 +00001162 qemu_get_clock(vm_clock)) ||
1163 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1164 qemu_get_clock(rt_clock))) {
bellard06d9f2f2006-05-01 13:23:04 +00001165#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001166 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1167 SetEvent(data->host_alarm);
bellard06d9f2f2006-05-01 13:23:04 +00001168#endif
bellard6a00d602005-11-21 23:25:50 +00001169 CPUState *env = cpu_single_env;
1170 if (env) {
1171 /* stop the currently executing cpu because a timer occured */
1172 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001173#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001174 if (env->kqemu_enabled) {
1175 kqemu_cpu_interrupt(env);
1176 }
bellarda332e112005-09-03 17:55:47 +00001177#endif
bellard6a00d602005-11-21 23:25:50 +00001178 }
bellard8a7ddc32004-03-31 19:00:16 +00001179 }
1180}
1181
thsefe75412007-08-24 01:36:32 +00001182static uint64_t qemu_next_deadline(void)
1183{
thsfe4d8f62007-09-16 19:53:43 +00001184 int64_t nearest_delta_us = UINT64_MAX;
thsefe75412007-08-24 01:36:32 +00001185 int64_t vmdelta_us;
1186
1187 if (active_timers[QEMU_TIMER_REALTIME])
1188 nearest_delta_us = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1189 qemu_get_clock(rt_clock))*1000;
1190
1191 if (active_timers[QEMU_TIMER_VIRTUAL]) {
1192 /* round up */
1193 vmdelta_us = (active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1194 qemu_get_clock(vm_clock)+999)/1000;
1195 if (vmdelta_us < nearest_delta_us)
1196 nearest_delta_us = vmdelta_us;
1197 }
1198
1199 /* Avoid arming the timer to negative, zero, or too low values */
1200 if (nearest_delta_us <= MIN_TIMER_REARM_US)
1201 nearest_delta_us = MIN_TIMER_REARM_US;
1202
1203 return nearest_delta_us;
1204}
1205
bellardfd872592004-05-12 19:11:15 +00001206#ifndef _WIN32
1207
bellard829309c2004-05-20 13:20:12 +00001208#if defined(__linux__)
1209
bellardfd872592004-05-12 19:11:15 +00001210#define RTC_FREQ 1024
1211
thsc8994012007-08-19 21:56:03 +00001212static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001213{
thsc8994012007-08-19 21:56:03 +00001214 struct sigaction act;
1215
1216 /* timer signal */
1217 sigfillset(&act.sa_mask);
1218 act.sa_flags = 0;
1219#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1220 act.sa_flags |= SA_ONSTACK;
1221#endif
1222 act.sa_handler = host_alarm_handler;
1223
1224 sigaction(SIGIO, &act, NULL);
1225 fcntl(fd, F_SETFL, O_ASYNC);
1226 fcntl(fd, F_SETOWN, getpid());
1227}
1228
thsc40ec5a2007-08-19 22:09:40 +00001229static int hpet_start_timer(struct qemu_alarm_timer *t)
1230{
1231 struct hpet_info info;
1232 int r, fd;
1233
1234 fd = open("/dev/hpet", O_RDONLY);
1235 if (fd < 0)
1236 return -1;
1237
1238 /* Set frequency */
1239 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1240 if (r < 0) {
1241 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1242 "error, but for better emulation accuracy type:\n"
1243 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1244 goto fail;
1245 }
1246
1247 /* Check capabilities */
1248 r = ioctl(fd, HPET_INFO, &info);
1249 if (r < 0)
1250 goto fail;
1251
1252 /* Enable periodic mode */
1253 r = ioctl(fd, HPET_EPI, 0);
1254 if (info.hi_flags && (r < 0))
1255 goto fail;
1256
1257 /* Enable interrupt */
1258 r = ioctl(fd, HPET_IE_ON, 0);
1259 if (r < 0)
1260 goto fail;
1261
1262 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001263 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001264
1265 return 0;
1266fail:
1267 close(fd);
1268 return -1;
1269}
1270
1271static void hpet_stop_timer(struct qemu_alarm_timer *t)
1272{
pbrookfcdc2122007-08-23 20:22:22 +00001273 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001274
1275 close(fd);
1276}
1277
thsc8994012007-08-19 21:56:03 +00001278static int rtc_start_timer(struct qemu_alarm_timer *t)
1279{
1280 int rtc_fd;
1281
balrogaeb30be2007-07-02 15:03:13 +00001282 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001283 if (rtc_fd < 0)
1284 return -1;
1285 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1286 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1287 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1288 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1289 goto fail;
1290 }
1291 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1292 fail:
1293 close(rtc_fd);
1294 return -1;
1295 }
thsc8994012007-08-19 21:56:03 +00001296
1297 enable_sigio_timer(rtc_fd);
1298
pbrookfcdc2122007-08-23 20:22:22 +00001299 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001300
bellardfd872592004-05-12 19:11:15 +00001301 return 0;
1302}
1303
thsc8994012007-08-19 21:56:03 +00001304static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001305{
pbrookfcdc2122007-08-23 20:22:22 +00001306 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001307
1308 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001309}
1310
thsefe75412007-08-24 01:36:32 +00001311static int dynticks_start_timer(struct qemu_alarm_timer *t)
1312{
1313 struct sigevent ev;
1314 timer_t host_timer;
1315 struct sigaction act;
1316
1317 sigfillset(&act.sa_mask);
1318 act.sa_flags = 0;
1319#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1320 act.sa_flags |= SA_ONSTACK;
1321#endif
1322 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;
1396#if defined(TARGET_I386) && defined(USE_CODE_COPY)
1397 act.sa_flags |= SA_ONSTACK;
1398#endif
1399 act.sa_handler = host_alarm_handler;
1400
1401 sigaction(SIGALRM, &act, NULL);
1402
1403 itv.it_interval.tv_sec = 0;
1404 /* for i386 kernel 2.6 to get 1 ms */
1405 itv.it_interval.tv_usec = 999;
1406 itv.it_value.tv_sec = 0;
1407 itv.it_value.tv_usec = 10 * 1000;
1408
1409 err = setitimer(ITIMER_REAL, &itv, NULL);
1410 if (err)
1411 return -1;
1412
1413 return 0;
1414}
1415
1416static void unix_stop_timer(struct qemu_alarm_timer *t)
1417{
1418 struct itimerval itv;
1419
1420 memset(&itv, 0, sizeof(itv));
1421 setitimer(ITIMER_REAL, &itv, NULL);
1422}
1423
bellard829309c2004-05-20 13:20:12 +00001424#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001425
thsc8994012007-08-19 21:56:03 +00001426#ifdef _WIN32
1427
1428static int win32_start_timer(struct qemu_alarm_timer *t)
1429{
1430 TIMECAPS tc;
1431 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001432 UINT flags;
thsc8994012007-08-19 21:56:03 +00001433
1434 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1435 if (!data->host_alarm) {
1436 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001437 return -1;
thsc8994012007-08-19 21:56:03 +00001438 }
1439
1440 memset(&tc, 0, sizeof(tc));
1441 timeGetDevCaps(&tc, sizeof(tc));
1442
1443 if (data->period < tc.wPeriodMin)
1444 data->period = tc.wPeriodMin;
1445
1446 timeBeginPeriod(data->period);
1447
thsefe75412007-08-24 01:36:32 +00001448 flags = TIME_CALLBACK_FUNCTION;
1449 if (alarm_has_dynticks(t))
1450 flags |= TIME_ONESHOT;
1451 else
1452 flags |= TIME_PERIODIC;
1453
thsc8994012007-08-19 21:56:03 +00001454 data->timerId = timeSetEvent(1, // interval (ms)
1455 data->period, // resolution
1456 host_alarm_handler, // function
1457 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001458 flags);
thsc8994012007-08-19 21:56:03 +00001459
1460 if (!data->timerId) {
1461 perror("Failed to initialize win32 alarm timer");
1462
1463 timeEndPeriod(data->period);
1464 CloseHandle(data->host_alarm);
1465 return -1;
1466 }
1467
1468 qemu_add_wait_object(data->host_alarm, NULL, NULL);
1469
1470 return 0;
1471}
1472
1473static void win32_stop_timer(struct qemu_alarm_timer *t)
1474{
1475 struct qemu_alarm_win32 *data = t->priv;
1476
1477 timeKillEvent(data->timerId);
1478 timeEndPeriod(data->period);
1479
1480 CloseHandle(data->host_alarm);
1481}
1482
thsefe75412007-08-24 01:36:32 +00001483static void win32_rearm_timer(struct qemu_alarm_timer *t)
1484{
1485 struct qemu_alarm_win32 *data = t->priv;
1486 uint64_t nearest_delta_us;
1487
1488 if (!active_timers[QEMU_TIMER_REALTIME] &&
1489 !active_timers[QEMU_TIMER_VIRTUAL])
1490 return;
1491
1492 nearest_delta_us = qemu_next_deadline();
1493 nearest_delta_us /= 1000;
1494
1495 timeKillEvent(data->timerId);
1496
1497 data->timerId = timeSetEvent(1,
1498 data->period,
1499 host_alarm_handler,
1500 (DWORD)t,
1501 TIME_ONESHOT | TIME_PERIODIC);
1502
1503 if (!data->timerId) {
1504 perror("Failed to re-arm win32 alarm timer");
1505
1506 timeEndPeriod(data->period);
1507 CloseHandle(data->host_alarm);
1508 exit(1);
1509 }
1510}
1511
thsc8994012007-08-19 21:56:03 +00001512#endif /* _WIN32 */
1513
bellard1dce7c32006-07-13 23:20:22 +00001514static void init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001515{
thsc8994012007-08-19 21:56:03 +00001516 struct qemu_alarm_timer *t;
1517 int i, err = -1;
bellard06d9f2f2006-05-01 13:23:04 +00001518
thsc8994012007-08-19 21:56:03 +00001519 for (i = 0; alarm_timers[i].name; i++) {
1520 t = &alarm_timers[i];
1521
thsc8994012007-08-19 21:56:03 +00001522 err = t->start(t);
1523 if (!err)
1524 break;
bellard67b915a2004-03-31 23:37:16 +00001525 }
bellardfd872592004-05-12 19:11:15 +00001526
thsc8994012007-08-19 21:56:03 +00001527 if (err) {
1528 fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1529 fprintf(stderr, "Terminating\n");
1530 exit(1);
bellard67b915a2004-03-31 23:37:16 +00001531 }
thsc8994012007-08-19 21:56:03 +00001532
1533 alarm_timer = t;
bellard8a7ddc32004-03-31 19:00:16 +00001534}
1535
bellard40c3bac2004-04-04 12:56:28 +00001536void quit_timers(void)
1537{
thsc8994012007-08-19 21:56:03 +00001538 alarm_timer->stop(alarm_timer);
1539 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001540}
1541
bellardc4b1fcc2004-03-14 21:44:30 +00001542/***********************************************************/
bellard82c643f2004-07-14 17:28:13 +00001543/* character device */
bellardc45886d2004-01-05 00:02:06 +00001544
pbrooke5b0bc42007-01-27 23:46:43 +00001545static void qemu_chr_event(CharDriverState *s, int event)
1546{
1547 if (!s->chr_event)
1548 return;
1549 s->chr_event(s->handler_opaque, event);
1550}
1551
ths86e94de2007-01-05 22:01:59 +00001552static void qemu_chr_reset_bh(void *opaque)
1553{
1554 CharDriverState *s = opaque;
pbrooke5b0bc42007-01-27 23:46:43 +00001555 qemu_chr_event(s, CHR_EVENT_RESET);
ths86e94de2007-01-05 22:01:59 +00001556 qemu_bh_delete(s->bh);
1557 s->bh = NULL;
1558}
1559
1560void qemu_chr_reset(CharDriverState *s)
1561{
1562 if (s->bh == NULL) {
1563 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1564 qemu_bh_schedule(s->bh);
1565 }
1566}
1567
bellard82c643f2004-07-14 17:28:13 +00001568int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
bellard67b915a2004-03-31 23:37:16 +00001569{
bellard82c643f2004-07-14 17:28:13 +00001570 return s->chr_write(s, buf, len);
bellard67b915a2004-03-31 23:37:16 +00001571}
1572
bellarde57a8c02005-11-10 23:58:52 +00001573int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00001574{
bellarde57a8c02005-11-10 23:58:52 +00001575 if (!s->chr_ioctl)
1576 return -ENOTSUP;
1577 return s->chr_ioctl(s, cmd, arg);
bellardf8d179e2005-11-08 22:30:36 +00001578}
1579
pbrooke5b0bc42007-01-27 23:46:43 +00001580int qemu_chr_can_read(CharDriverState *s)
1581{
1582 if (!s->chr_can_read)
1583 return 0;
1584 return s->chr_can_read(s->handler_opaque);
1585}
1586
1587void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1588{
1589 s->chr_read(s->handler_opaque, buf, len);
1590}
1591
1592
bellard82c643f2004-07-14 17:28:13 +00001593void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
bellard0824d6f2003-06-24 13:42:40 +00001594{
bellard82c643f2004-07-14 17:28:13 +00001595 char buf[4096];
1596 va_list ap;
1597 va_start(ap, fmt);
1598 vsnprintf(buf, sizeof(buf), fmt, ap);
1599 qemu_chr_write(s, buf, strlen(buf));
1600 va_end(ap);
1601}
1602
bellard5905b2e2004-08-01 21:53:26 +00001603void qemu_chr_send_event(CharDriverState *s, int event)
1604{
1605 if (s->chr_send_event)
1606 s->chr_send_event(s, event);
1607}
1608
ths5fafdf22007-09-16 21:08:06 +00001609void qemu_chr_add_handlers(CharDriverState *s,
1610 IOCanRWHandler *fd_can_read,
pbrooke5b0bc42007-01-27 23:46:43 +00001611 IOReadHandler *fd_read,
1612 IOEventHandler *fd_event,
1613 void *opaque)
bellard82c643f2004-07-14 17:28:13 +00001614{
pbrooke5b0bc42007-01-27 23:46:43 +00001615 s->chr_can_read = fd_can_read;
1616 s->chr_read = fd_read;
1617 s->chr_event = fd_event;
1618 s->handler_opaque = opaque;
1619 if (s->chr_update_read_handler)
1620 s->chr_update_read_handler(s);
bellard82c643f2004-07-14 17:28:13 +00001621}
ths3b46e622007-09-17 08:09:54 +00001622
bellard82c643f2004-07-14 17:28:13 +00001623static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1624{
1625 return len;
1626}
1627
ths52f61fd2006-12-22 21:20:52 +00001628static CharDriverState *qemu_chr_open_null(void)
bellard82c643f2004-07-14 17:28:13 +00001629{
1630 CharDriverState *chr;
1631
1632 chr = qemu_mallocz(sizeof(CharDriverState));
1633 if (!chr)
1634 return NULL;
1635 chr->chr_write = null_chr_write;
bellard82c643f2004-07-14 17:28:13 +00001636 return chr;
1637}
1638
ths20d8a3e2007-02-18 17:04:49 +00001639/* MUX driver for serial I/O splitting */
1640static int term_timestamps;
1641static int64_t term_timestamps_start;
ths9c1de612007-02-21 17:25:30 +00001642#define MAX_MUX 4
ths20d8a3e2007-02-18 17:04:49 +00001643typedef struct {
1644 IOCanRWHandler *chr_can_read[MAX_MUX];
1645 IOReadHandler *chr_read[MAX_MUX];
1646 IOEventHandler *chr_event[MAX_MUX];
1647 void *ext_opaque[MAX_MUX];
1648 CharDriverState *drv;
1649 int mux_cnt;
1650 int term_got_escape;
1651 int max_size;
1652} MuxDriver;
1653
1654
1655static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1656{
1657 MuxDriver *d = chr->opaque;
1658 int ret;
1659 if (!term_timestamps) {
1660 ret = d->drv->chr_write(d->drv, buf, len);
1661 } else {
1662 int i;
1663
1664 ret = 0;
1665 for(i = 0; i < len; i++) {
1666 ret += d->drv->chr_write(d->drv, buf+i, 1);
1667 if (buf[i] == '\n') {
1668 char buf1[64];
1669 int64_t ti;
1670 int secs;
1671
1672 ti = get_clock();
1673 if (term_timestamps_start == -1)
1674 term_timestamps_start = ti;
1675 ti -= term_timestamps_start;
1676 secs = ti / 1000000000;
1677 snprintf(buf1, sizeof(buf1),
1678 "[%02d:%02d:%02d.%03d] ",
1679 secs / 3600,
1680 (secs / 60) % 60,
1681 secs % 60,
1682 (int)((ti / 1000000) % 1000));
1683 d->drv->chr_write(d->drv, buf1, strlen(buf1));
1684 }
1685 }
1686 }
1687 return ret;
1688}
1689
1690static char *mux_help[] = {
1691 "% h print this help\n\r",
1692 "% x exit emulator\n\r",
1693 "% s save disk data back to file (if -snapshot)\n\r",
1694 "% t toggle console timestamps\n\r"
1695 "% b send break (magic sysrq)\n\r",
1696 "% c switch between console and monitor\n\r",
1697 "% % sends %\n\r",
1698 NULL
1699};
1700
1701static int term_escape_char = 0x01; /* ctrl-a is used for escape */
1702static void mux_print_help(CharDriverState *chr)
1703{
1704 int i, j;
1705 char ebuf[15] = "Escape-Char";
1706 char cbuf[50] = "\n\r";
1707
1708 if (term_escape_char > 0 && term_escape_char < 26) {
1709 sprintf(cbuf,"\n\r");
1710 sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a');
1711 } else {
1712 sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char);
1713 }
1714 chr->chr_write(chr, cbuf, strlen(cbuf));
1715 for (i = 0; mux_help[i] != NULL; i++) {
1716 for (j=0; mux_help[i][j] != '\0'; j++) {
1717 if (mux_help[i][j] == '%')
1718 chr->chr_write(chr, ebuf, strlen(ebuf));
1719 else
1720 chr->chr_write(chr, &mux_help[i][j], 1);
1721 }
1722 }
1723}
1724
1725static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1726{
1727 if (d->term_got_escape) {
1728 d->term_got_escape = 0;
1729 if (ch == term_escape_char)
1730 goto send_char;
1731 switch(ch) {
1732 case '?':
1733 case 'h':
1734 mux_print_help(chr);
1735 break;
1736 case 'x':
1737 {
1738 char *term = "QEMU: Terminated\n\r";
1739 chr->chr_write(chr,term,strlen(term));
1740 exit(0);
1741 break;
1742 }
1743 case 's':
1744 {
1745 int i;
1746 for (i = 0; i < MAX_DISKS; i++) {
1747 if (bs_table[i])
1748 bdrv_commit(bs_table[i]);
1749 }
balrog2dc7b602007-05-24 18:53:22 +00001750 if (mtd_bdrv)
1751 bdrv_commit(mtd_bdrv);
ths20d8a3e2007-02-18 17:04:49 +00001752 }
1753 break;
1754 case 'b':
balrog36ddb832007-05-18 17:46:59 +00001755 qemu_chr_event(chr, CHR_EVENT_BREAK);
ths20d8a3e2007-02-18 17:04:49 +00001756 break;
1757 case 'c':
1758 /* Switch to the next registered device */
1759 chr->focus++;
1760 if (chr->focus >= d->mux_cnt)
1761 chr->focus = 0;
1762 break;
1763 case 't':
1764 term_timestamps = !term_timestamps;
1765 term_timestamps_start = -1;
1766 break;
1767 }
1768 } else if (ch == term_escape_char) {
1769 d->term_got_escape = 1;
1770 } else {
1771 send_char:
1772 return 1;
1773 }
1774 return 0;
1775}
1776
1777static int mux_chr_can_read(void *opaque)
1778{
1779 CharDriverState *chr = opaque;
1780 MuxDriver *d = chr->opaque;
1781 if (d->chr_can_read[chr->focus])
1782 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
1783 return 0;
1784}
1785
1786static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
1787{
1788 CharDriverState *chr = opaque;
1789 MuxDriver *d = chr->opaque;
1790 int i;
1791 for(i = 0; i < size; i++)
1792 if (mux_proc_byte(chr, d, buf[i]))
1793 d->chr_read[chr->focus](d->ext_opaque[chr->focus], &buf[i], 1);
1794}
1795
1796static void mux_chr_event(void *opaque, int event)
1797{
1798 CharDriverState *chr = opaque;
1799 MuxDriver *d = chr->opaque;
1800 int i;
1801
1802 /* Send the event to all registered listeners */
1803 for (i = 0; i < d->mux_cnt; i++)
1804 if (d->chr_event[i])
1805 d->chr_event[i](d->ext_opaque[i], event);
1806}
1807
1808static void mux_chr_update_read_handler(CharDriverState *chr)
1809{
1810 MuxDriver *d = chr->opaque;
1811
1812 if (d->mux_cnt >= MAX_MUX) {
1813 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
1814 return;
1815 }
1816 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
1817 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
1818 d->chr_read[d->mux_cnt] = chr->chr_read;
1819 d->chr_event[d->mux_cnt] = chr->chr_event;
1820 /* Fix up the real driver with mux routines */
1821 if (d->mux_cnt == 0) {
1822 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
1823 mux_chr_event, chr);
1824 }
1825 chr->focus = d->mux_cnt;
1826 d->mux_cnt++;
1827}
1828
1829CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
1830{
1831 CharDriverState *chr;
1832 MuxDriver *d;
1833
1834 chr = qemu_mallocz(sizeof(CharDriverState));
1835 if (!chr)
1836 return NULL;
1837 d = qemu_mallocz(sizeof(MuxDriver));
1838 if (!d) {
1839 free(chr);
1840 return NULL;
1841 }
1842
1843 chr->opaque = d;
1844 d->drv = drv;
1845 chr->focus = -1;
1846 chr->chr_write = mux_chr_write;
1847 chr->chr_update_read_handler = mux_chr_update_read_handler;
1848 return chr;
1849}
1850
1851
bellardfd1dff42006-02-01 21:29:26 +00001852#ifdef _WIN32
bellard82c643f2004-07-14 17:28:13 +00001853
bellardfd1dff42006-02-01 21:29:26 +00001854static void socket_cleanup(void)
1855{
1856 WSACleanup();
1857}
bellard82c643f2004-07-14 17:28:13 +00001858
bellardfd1dff42006-02-01 21:29:26 +00001859static int socket_init(void)
1860{
1861 WSADATA Data;
1862 int ret, err;
1863
1864 ret = WSAStartup(MAKEWORD(2,2), &Data);
1865 if (ret != 0) {
1866 err = WSAGetLastError();
1867 fprintf(stderr, "WSAStartup: %d\n", err);
1868 return -1;
1869 }
1870 atexit(socket_cleanup);
1871 return 0;
1872}
1873
1874static int send_all(int fd, const uint8_t *buf, int len1)
1875{
1876 int ret, len;
ths3b46e622007-09-17 08:09:54 +00001877
bellardfd1dff42006-02-01 21:29:26 +00001878 len = len1;
1879 while (len > 0) {
1880 ret = send(fd, buf, len, 0);
1881 if (ret < 0) {
1882 int errno;
1883 errno = WSAGetLastError();
1884 if (errno != WSAEWOULDBLOCK) {
1885 return -1;
1886 }
1887 } else if (ret == 0) {
1888 break;
1889 } else {
1890 buf += ret;
1891 len -= ret;
1892 }
1893 }
1894 return len1 - len;
1895}
1896
1897void socket_set_nonblock(int fd)
1898{
1899 unsigned long opt = 1;
1900 ioctlsocket(fd, FIONBIO, &opt);
1901}
1902
1903#else
1904
bellard1d969052004-09-18 19:34:39 +00001905static int unix_write(int fd, const uint8_t *buf, int len1)
1906{
1907 int ret, len;
1908
1909 len = len1;
1910 while (len > 0) {
1911 ret = write(fd, buf, len);
1912 if (ret < 0) {
1913 if (errno != EINTR && errno != EAGAIN)
1914 return -1;
1915 } else if (ret == 0) {
1916 break;
1917 } else {
1918 buf += ret;
1919 len -= ret;
1920 }
1921 }
1922 return len1 - len;
1923}
1924
bellardfd1dff42006-02-01 21:29:26 +00001925static inline int send_all(int fd, const uint8_t *buf, int len1)
1926{
1927 return unix_write(fd, buf, len1);
1928}
1929
1930void socket_set_nonblock(int fd)
1931{
1932 fcntl(fd, F_SETFL, O_NONBLOCK);
1933}
1934#endif /* !_WIN32 */
1935
1936#ifndef _WIN32
1937
1938typedef struct {
1939 int fd_in, fd_out;
bellardfd1dff42006-02-01 21:29:26 +00001940 int max_size;
1941} FDCharDriver;
1942
ths20d8a3e2007-02-18 17:04:49 +00001943#define STDIO_MAX_CLIENTS 1
1944static int stdio_nb_clients = 0;
bellardfd1dff42006-02-01 21:29:26 +00001945
bellard82c643f2004-07-14 17:28:13 +00001946static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1947{
1948 FDCharDriver *s = chr->opaque;
bellard1d969052004-09-18 19:34:39 +00001949 return unix_write(s->fd_out, buf, len);
bellard82c643f2004-07-14 17:28:13 +00001950}
1951
bellard7c9d8e02005-11-15 22:16:05 +00001952static int fd_chr_read_poll(void *opaque)
1953{
1954 CharDriverState *chr = opaque;
1955 FDCharDriver *s = chr->opaque;
1956
pbrooke5b0bc42007-01-27 23:46:43 +00001957 s->max_size = qemu_chr_can_read(chr);
bellard7c9d8e02005-11-15 22:16:05 +00001958 return s->max_size;
1959}
1960
1961static void fd_chr_read(void *opaque)
1962{
1963 CharDriverState *chr = opaque;
1964 FDCharDriver *s = chr->opaque;
1965 int size, len;
1966 uint8_t buf[1024];
ths3b46e622007-09-17 08:09:54 +00001967
bellard7c9d8e02005-11-15 22:16:05 +00001968 len = sizeof(buf);
1969 if (len > s->max_size)
1970 len = s->max_size;
1971 if (len == 0)
1972 return;
1973 size = read(s->fd_in, buf, len);
pbrook188157f2006-11-01 01:44:16 +00001974 if (size == 0) {
1975 /* FD has been closed. Remove it from the active list. */
1976 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
1977 return;
1978 }
bellard7c9d8e02005-11-15 22:16:05 +00001979 if (size > 0) {
pbrooke5b0bc42007-01-27 23:46:43 +00001980 qemu_chr_read(chr, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00001981 }
1982}
1983
pbrooke5b0bc42007-01-27 23:46:43 +00001984static void fd_chr_update_read_handler(CharDriverState *chr)
bellard82c643f2004-07-14 17:28:13 +00001985{
1986 FDCharDriver *s = chr->opaque;
1987
bellardf8d179e2005-11-08 22:30:36 +00001988 if (s->fd_in >= 0) {
1989 if (nographic && s->fd_in == 0) {
bellardf8d179e2005-11-08 22:30:36 +00001990 } else {
ths5fafdf22007-09-16 21:08:06 +00001991 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
bellard7c9d8e02005-11-15 22:16:05 +00001992 fd_chr_read, NULL, chr);
bellardf8d179e2005-11-08 22:30:36 +00001993 }
bellard0824d6f2003-06-24 13:42:40 +00001994 }
1995}
1996
bellard82c643f2004-07-14 17:28:13 +00001997/* open a character device to a unix fd */
ths52f61fd2006-12-22 21:20:52 +00001998static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
bellard82c643f2004-07-14 17:28:13 +00001999{
2000 CharDriverState *chr;
2001 FDCharDriver *s;
2002
2003 chr = qemu_mallocz(sizeof(CharDriverState));
2004 if (!chr)
2005 return NULL;
2006 s = qemu_mallocz(sizeof(FDCharDriver));
2007 if (!s) {
2008 free(chr);
2009 return NULL;
2010 }
2011 s->fd_in = fd_in;
2012 s->fd_out = fd_out;
2013 chr->opaque = s;
2014 chr->chr_write = fd_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002015 chr->chr_update_read_handler = fd_chr_update_read_handler;
ths86e94de2007-01-05 22:01:59 +00002016
2017 qemu_chr_reset(chr);
2018
bellard82c643f2004-07-14 17:28:13 +00002019 return chr;
2020}
2021
ths52f61fd2006-12-22 21:20:52 +00002022static CharDriverState *qemu_chr_open_file_out(const char *file_out)
bellardf8d179e2005-11-08 22:30:36 +00002023{
2024 int fd_out;
2025
balrogaeb30be2007-07-02 15:03:13 +00002026 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
bellardf8d179e2005-11-08 22:30:36 +00002027 if (fd_out < 0)
2028 return NULL;
2029 return qemu_chr_open_fd(-1, fd_out);
2030}
2031
ths52f61fd2006-12-22 21:20:52 +00002032static CharDriverState *qemu_chr_open_pipe(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002033{
thsc26c1c42006-12-22 19:25:31 +00002034 int fd_in, fd_out;
2035 char filename_in[256], filename_out[256];
bellardf8d179e2005-11-08 22:30:36 +00002036
thsc26c1c42006-12-22 19:25:31 +00002037 snprintf(filename_in, 256, "%s.in", filename);
2038 snprintf(filename_out, 256, "%s.out", filename);
balrogaeb30be2007-07-02 15:03:13 +00002039 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2040 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002041 if (fd_in < 0 || fd_out < 0) {
2042 if (fd_in >= 0)
2043 close(fd_in);
2044 if (fd_out >= 0)
2045 close(fd_out);
balrogaeb30be2007-07-02 15:03:13 +00002046 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002047 if (fd_in < 0)
2048 return NULL;
2049 }
2050 return qemu_chr_open_fd(fd_in, fd_out);
bellardf8d179e2005-11-08 22:30:36 +00002051}
2052
2053
bellard82c643f2004-07-14 17:28:13 +00002054/* for STDIO, we handle the case where several clients use it
2055 (nographic mode) */
2056
bellardaa0bc6b2005-09-03 15:28:58 +00002057#define TERM_FIFO_MAX_SIZE 1
2058
bellardaa0bc6b2005-09-03 15:28:58 +00002059static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
bellard1dce7c32006-07-13 23:20:22 +00002060static int term_fifo_size;
bellard82c643f2004-07-14 17:28:13 +00002061
bellard7c9d8e02005-11-15 22:16:05 +00002062static int stdio_read_poll(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002063{
ths20d8a3e2007-02-18 17:04:49 +00002064 CharDriverState *chr = opaque;
bellardaa0bc6b2005-09-03 15:28:58 +00002065
ths20d8a3e2007-02-18 17:04:49 +00002066 /* try to flush the queue if needed */
2067 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2068 qemu_chr_read(chr, term_fifo, 1);
2069 term_fifo_size = 0;
bellardaa0bc6b2005-09-03 15:28:58 +00002070 }
ths20d8a3e2007-02-18 17:04:49 +00002071 /* see if we can absorb more chars */
2072 if (term_fifo_size == 0)
2073 return 1;
2074 else
2075 return 0;
bellard82c643f2004-07-14 17:28:13 +00002076}
2077
bellard7c9d8e02005-11-15 22:16:05 +00002078static void stdio_read(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002079{
bellard7c9d8e02005-11-15 22:16:05 +00002080 int size;
2081 uint8_t buf[1];
ths20d8a3e2007-02-18 17:04:49 +00002082 CharDriverState *chr = opaque;
2083
bellard7c9d8e02005-11-15 22:16:05 +00002084 size = read(0, buf, 1);
pbrook519945d2006-09-10 14:39:54 +00002085 if (size == 0) {
2086 /* stdin has been closed. Remove it from the active list. */
2087 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2088 return;
2089 }
ths20d8a3e2007-02-18 17:04:49 +00002090 if (size > 0) {
2091 if (qemu_chr_can_read(chr) > 0) {
2092 qemu_chr_read(chr, buf, 1);
2093 } else if (term_fifo_size == 0) {
2094 term_fifo[term_fifo_size++] = buf[0];
bellard1dce7c32006-07-13 23:20:22 +00002095 }
bellard1dce7c32006-07-13 23:20:22 +00002096 }
2097}
2098
bellard8d11df92004-08-24 21:13:40 +00002099/* init terminal so that we can grab keys */
2100static struct termios oldtty;
2101static int old_fd0_flags;
2102
2103static void term_exit(void)
2104{
2105 tcsetattr (0, TCSANOW, &oldtty);
2106 fcntl(0, F_SETFL, old_fd0_flags);
2107}
2108
2109static void term_init(void)
2110{
2111 struct termios tty;
2112
2113 tcgetattr (0, &tty);
2114 oldtty = tty;
2115 old_fd0_flags = fcntl(0, F_GETFL);
2116
2117 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2118 |INLCR|IGNCR|ICRNL|IXON);
2119 tty.c_oflag |= OPOST;
2120 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2121 /* if graphical mode, we allow Ctrl-C handling */
2122 if (nographic)
2123 tty.c_lflag &= ~ISIG;
2124 tty.c_cflag &= ~(CSIZE|PARENB);
2125 tty.c_cflag |= CS8;
2126 tty.c_cc[VMIN] = 1;
2127 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +00002128
bellard8d11df92004-08-24 21:13:40 +00002129 tcsetattr (0, TCSANOW, &tty);
2130
2131 atexit(term_exit);
2132
2133 fcntl(0, F_SETFL, O_NONBLOCK);
2134}
2135
ths52f61fd2006-12-22 21:20:52 +00002136static CharDriverState *qemu_chr_open_stdio(void)
bellard82c643f2004-07-14 17:28:13 +00002137{
2138 CharDriverState *chr;
2139
ths20d8a3e2007-02-18 17:04:49 +00002140 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2141 return NULL;
2142 chr = qemu_chr_open_fd(0, 1);
2143 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2144 stdio_nb_clients++;
2145 term_init();
2146
bellard82c643f2004-07-14 17:28:13 +00002147 return chr;
2148}
2149
thsaec62502007-06-25 11:48:07 +00002150#if defined(__linux__) || defined(__sun__)
ths52f61fd2006-12-22 21:20:52 +00002151static CharDriverState *qemu_chr_open_pty(void)
bellard82c643f2004-07-14 17:28:13 +00002152{
bellard91fc2112005-12-18 19:09:37 +00002153 struct termios tty;
bellard82c643f2004-07-14 17:28:13 +00002154 char slave_name[1024];
2155 int master_fd, slave_fd;
ths3b46e622007-09-17 08:09:54 +00002156
thsaec62502007-06-25 11:48:07 +00002157#if defined(__linux__)
bellard82c643f2004-07-14 17:28:13 +00002158 /* Not satisfying */
2159 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
2160 return NULL;
2161 }
thsaec62502007-06-25 11:48:07 +00002162#endif
ths3b46e622007-09-17 08:09:54 +00002163
bellard91fc2112005-12-18 19:09:37 +00002164 /* Disabling local echo and line-buffered output */
2165 tcgetattr (master_fd, &tty);
2166 tty.c_lflag &= ~(ECHO|ICANON|ISIG);
2167 tty.c_cc[VMIN] = 1;
2168 tty.c_cc[VTIME] = 0;
2169 tcsetattr (master_fd, TCSAFLUSH, &tty);
2170
bellard82c643f2004-07-14 17:28:13 +00002171 fprintf(stderr, "char device redirected to %s\n", slave_name);
2172 return qemu_chr_open_fd(master_fd, master_fd);
2173}
bellardf8d179e2005-11-08 22:30:36 +00002174
ths5fafdf22007-09-16 21:08:06 +00002175static void tty_serial_init(int fd, int speed,
bellardf8d179e2005-11-08 22:30:36 +00002176 int parity, int data_bits, int stop_bits)
2177{
2178 struct termios tty;
2179 speed_t spd;
2180
bellarde57a8c02005-11-10 23:58:52 +00002181#if 0
ths5fafdf22007-09-16 21:08:06 +00002182 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
bellarde57a8c02005-11-10 23:58:52 +00002183 speed, parity, data_bits, stop_bits);
2184#endif
2185 tcgetattr (fd, &tty);
bellardf8d179e2005-11-08 22:30:36 +00002186
2187 switch(speed) {
2188 case 50:
2189 spd = B50;
2190 break;
2191 case 75:
2192 spd = B75;
2193 break;
2194 case 300:
2195 spd = B300;
2196 break;
2197 case 600:
2198 spd = B600;
2199 break;
2200 case 1200:
2201 spd = B1200;
2202 break;
2203 case 2400:
2204 spd = B2400;
2205 break;
2206 case 4800:
2207 spd = B4800;
2208 break;
2209 case 9600:
2210 spd = B9600;
2211 break;
2212 case 19200:
2213 spd = B19200;
2214 break;
2215 case 38400:
2216 spd = B38400;
2217 break;
2218 case 57600:
2219 spd = B57600;
2220 break;
2221 default:
2222 case 115200:
2223 spd = B115200;
2224 break;
2225 }
2226
2227 cfsetispeed(&tty, spd);
2228 cfsetospeed(&tty, spd);
2229
2230 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2231 |INLCR|IGNCR|ICRNL|IXON);
2232 tty.c_oflag |= OPOST;
2233 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
bellard094eed62006-09-09 11:10:18 +00002234 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
bellardf8d179e2005-11-08 22:30:36 +00002235 switch(data_bits) {
2236 default:
2237 case 8:
2238 tty.c_cflag |= CS8;
2239 break;
2240 case 7:
2241 tty.c_cflag |= CS7;
2242 break;
2243 case 6:
2244 tty.c_cflag |= CS6;
2245 break;
2246 case 5:
2247 tty.c_cflag |= CS5;
2248 break;
2249 }
2250 switch(parity) {
2251 default:
2252 case 'N':
2253 break;
2254 case 'E':
2255 tty.c_cflag |= PARENB;
2256 break;
2257 case 'O':
2258 tty.c_cflag |= PARENB | PARODD;
2259 break;
2260 }
bellard094eed62006-09-09 11:10:18 +00002261 if (stop_bits == 2)
2262 tty.c_cflag |= CSTOPB;
ths3b46e622007-09-17 08:09:54 +00002263
bellardf8d179e2005-11-08 22:30:36 +00002264 tcsetattr (fd, TCSANOW, &tty);
2265}
2266
bellarde57a8c02005-11-10 23:58:52 +00002267static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00002268{
2269 FDCharDriver *s = chr->opaque;
ths3b46e622007-09-17 08:09:54 +00002270
bellarde57a8c02005-11-10 23:58:52 +00002271 switch(cmd) {
2272 case CHR_IOCTL_SERIAL_SET_PARAMS:
2273 {
2274 QEMUSerialSetParams *ssp = arg;
ths5fafdf22007-09-16 21:08:06 +00002275 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
bellarde57a8c02005-11-10 23:58:52 +00002276 ssp->data_bits, ssp->stop_bits);
2277 }
2278 break;
2279 case CHR_IOCTL_SERIAL_SET_BREAK:
2280 {
2281 int enable = *(int *)arg;
2282 if (enable)
2283 tcsendbreak(s->fd_in, 1);
2284 }
2285 break;
2286 default:
2287 return -ENOTSUP;
2288 }
2289 return 0;
bellardf8d179e2005-11-08 22:30:36 +00002290}
2291
ths52f61fd2006-12-22 21:20:52 +00002292static CharDriverState *qemu_chr_open_tty(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002293{
2294 CharDriverState *chr;
2295 int fd;
2296
balrogaeb30be2007-07-02 15:03:13 +00002297 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
bellardf8d179e2005-11-08 22:30:36 +00002298 fcntl(fd, F_SETFL, O_NONBLOCK);
2299 tty_serial_init(fd, 115200, 'N', 8, 1);
2300 chr = qemu_chr_open_fd(fd, fd);
balrogaeb30be2007-07-02 15:03:13 +00002301 if (!chr) {
2302 close(fd);
bellardf8d179e2005-11-08 22:30:36 +00002303 return NULL;
balrogaeb30be2007-07-02 15:03:13 +00002304 }
bellarde57a8c02005-11-10 23:58:52 +00002305 chr->chr_ioctl = tty_serial_ioctl;
ths86e94de2007-01-05 22:01:59 +00002306 qemu_chr_reset(chr);
bellarde57a8c02005-11-10 23:58:52 +00002307 return chr;
2308}
thsaec62502007-06-25 11:48:07 +00002309#else /* ! __linux__ && ! __sun__ */
2310static CharDriverState *qemu_chr_open_pty(void)
2311{
2312 return NULL;
2313}
2314#endif /* __linux__ || __sun__ */
bellarde57a8c02005-11-10 23:58:52 +00002315
thsaec62502007-06-25 11:48:07 +00002316#if defined(__linux__)
ths5867c882007-02-17 23:44:43 +00002317typedef struct {
2318 int fd;
2319 int mode;
2320} ParallelCharDriver;
2321
2322static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2323{
2324 if (s->mode != mode) {
2325 int m = mode;
2326 if (ioctl(s->fd, PPSETMODE, &m) < 0)
2327 return 0;
2328 s->mode = mode;
2329 }
2330 return 1;
2331}
2332
bellarde57a8c02005-11-10 23:58:52 +00002333static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2334{
ths5867c882007-02-17 23:44:43 +00002335 ParallelCharDriver *drv = chr->opaque;
2336 int fd = drv->fd;
bellarde57a8c02005-11-10 23:58:52 +00002337 uint8_t b;
2338
2339 switch(cmd) {
2340 case CHR_IOCTL_PP_READ_DATA:
2341 if (ioctl(fd, PPRDATA, &b) < 0)
2342 return -ENOTSUP;
2343 *(uint8_t *)arg = b;
2344 break;
2345 case CHR_IOCTL_PP_WRITE_DATA:
2346 b = *(uint8_t *)arg;
2347 if (ioctl(fd, PPWDATA, &b) < 0)
2348 return -ENOTSUP;
2349 break;
2350 case CHR_IOCTL_PP_READ_CONTROL:
2351 if (ioctl(fd, PPRCONTROL, &b) < 0)
2352 return -ENOTSUP;
ths5867c882007-02-17 23:44:43 +00002353 /* Linux gives only the lowest bits, and no way to know data
2354 direction! For better compatibility set the fixed upper
2355 bits. */
2356 *(uint8_t *)arg = b | 0xc0;
bellarde57a8c02005-11-10 23:58:52 +00002357 break;
2358 case CHR_IOCTL_PP_WRITE_CONTROL:
2359 b = *(uint8_t *)arg;
2360 if (ioctl(fd, PPWCONTROL, &b) < 0)
2361 return -ENOTSUP;
2362 break;
2363 case CHR_IOCTL_PP_READ_STATUS:
2364 if (ioctl(fd, PPRSTATUS, &b) < 0)
2365 return -ENOTSUP;
2366 *(uint8_t *)arg = b;
2367 break;
ths5867c882007-02-17 23:44:43 +00002368 case CHR_IOCTL_PP_EPP_READ_ADDR:
2369 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2370 struct ParallelIOArg *parg = arg;
2371 int n = read(fd, parg->buffer, parg->count);
2372 if (n != parg->count) {
2373 return -EIO;
2374 }
2375 }
2376 break;
2377 case CHR_IOCTL_PP_EPP_READ:
2378 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2379 struct ParallelIOArg *parg = arg;
2380 int n = read(fd, parg->buffer, parg->count);
2381 if (n != parg->count) {
2382 return -EIO;
2383 }
2384 }
2385 break;
2386 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2387 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2388 struct ParallelIOArg *parg = arg;
2389 int n = write(fd, parg->buffer, parg->count);
2390 if (n != parg->count) {
2391 return -EIO;
2392 }
2393 }
2394 break;
2395 case CHR_IOCTL_PP_EPP_WRITE:
2396 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2397 struct ParallelIOArg *parg = arg;
2398 int n = write(fd, parg->buffer, parg->count);
2399 if (n != parg->count) {
2400 return -EIO;
2401 }
2402 }
2403 break;
bellarde57a8c02005-11-10 23:58:52 +00002404 default:
2405 return -ENOTSUP;
2406 }
2407 return 0;
2408}
2409
ths5867c882007-02-17 23:44:43 +00002410static void pp_close(CharDriverState *chr)
2411{
2412 ParallelCharDriver *drv = chr->opaque;
2413 int fd = drv->fd;
2414
2415 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2416 ioctl(fd, PPRELEASE);
2417 close(fd);
2418 qemu_free(drv);
2419}
2420
ths52f61fd2006-12-22 21:20:52 +00002421static CharDriverState *qemu_chr_open_pp(const char *filename)
bellarde57a8c02005-11-10 23:58:52 +00002422{
2423 CharDriverState *chr;
ths5867c882007-02-17 23:44:43 +00002424 ParallelCharDriver *drv;
bellarde57a8c02005-11-10 23:58:52 +00002425 int fd;
2426
balrogaeb30be2007-07-02 15:03:13 +00002427 TFR(fd = open(filename, O_RDWR));
bellarde57a8c02005-11-10 23:58:52 +00002428 if (fd < 0)
2429 return NULL;
2430
2431 if (ioctl(fd, PPCLAIM) < 0) {
2432 close(fd);
2433 return NULL;
2434 }
2435
ths5867c882007-02-17 23:44:43 +00002436 drv = qemu_mallocz(sizeof(ParallelCharDriver));
2437 if (!drv) {
bellarde57a8c02005-11-10 23:58:52 +00002438 close(fd);
2439 return NULL;
2440 }
ths5867c882007-02-17 23:44:43 +00002441 drv->fd = fd;
2442 drv->mode = IEEE1284_MODE_COMPAT;
2443
2444 chr = qemu_mallocz(sizeof(CharDriverState));
2445 if (!chr) {
2446 qemu_free(drv);
2447 close(fd);
2448 return NULL;
2449 }
bellarde57a8c02005-11-10 23:58:52 +00002450 chr->chr_write = null_chr_write;
bellarde57a8c02005-11-10 23:58:52 +00002451 chr->chr_ioctl = pp_ioctl;
ths5867c882007-02-17 23:44:43 +00002452 chr->chr_close = pp_close;
2453 chr->opaque = drv;
ths86e94de2007-01-05 22:01:59 +00002454
2455 qemu_chr_reset(chr);
2456
bellardf8d179e2005-11-08 22:30:36 +00002457 return chr;
2458}
thsaec62502007-06-25 11:48:07 +00002459#endif /* __linux__ */
bellardf8d179e2005-11-08 22:30:36 +00002460
thsaec62502007-06-25 11:48:07 +00002461#else /* _WIN32 */
bellard67b915a2004-03-31 23:37:16 +00002462
bellardf3311102006-04-12 20:21:17 +00002463typedef struct {
bellardf3311102006-04-12 20:21:17 +00002464 int max_size;
2465 HANDLE hcom, hrecv, hsend;
2466 OVERLAPPED orecv, osend;
2467 BOOL fpipe;
2468 DWORD len;
2469} WinCharState;
2470
2471#define NSENDBUF 2048
2472#define NRECVBUF 2048
2473#define MAXCONNECT 1
2474#define NTIMEOUT 5000
2475
2476static int win_chr_poll(void *opaque);
2477static int win_chr_pipe_poll(void *opaque);
2478
ths087f4ae2007-02-10 21:50:42 +00002479static void win_chr_close(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002480{
ths087f4ae2007-02-10 21:50:42 +00002481 WinCharState *s = chr->opaque;
2482
bellardf3311102006-04-12 20:21:17 +00002483 if (s->hsend) {
2484 CloseHandle(s->hsend);
2485 s->hsend = NULL;
2486 }
2487 if (s->hrecv) {
2488 CloseHandle(s->hrecv);
2489 s->hrecv = NULL;
2490 }
2491 if (s->hcom) {
2492 CloseHandle(s->hcom);
2493 s->hcom = NULL;
2494 }
2495 if (s->fpipe)
ths087f4ae2007-02-10 21:50:42 +00002496 qemu_del_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002497 else
ths087f4ae2007-02-10 21:50:42 +00002498 qemu_del_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002499}
2500
ths087f4ae2007-02-10 21:50:42 +00002501static int win_chr_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002502{
2503 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002504 COMMCONFIG comcfg;
2505 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2506 COMSTAT comstat;
2507 DWORD size;
2508 DWORD err;
ths3b46e622007-09-17 08:09:54 +00002509
bellardf3311102006-04-12 20:21:17 +00002510 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2511 if (!s->hsend) {
2512 fprintf(stderr, "Failed CreateEvent\n");
2513 goto fail;
2514 }
2515 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2516 if (!s->hrecv) {
2517 fprintf(stderr, "Failed CreateEvent\n");
2518 goto fail;
2519 }
2520
2521 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2522 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2523 if (s->hcom == INVALID_HANDLE_VALUE) {
2524 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2525 s->hcom = NULL;
2526 goto fail;
2527 }
ths3b46e622007-09-17 08:09:54 +00002528
bellardf3311102006-04-12 20:21:17 +00002529 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2530 fprintf(stderr, "Failed SetupComm\n");
2531 goto fail;
2532 }
ths3b46e622007-09-17 08:09:54 +00002533
bellardf3311102006-04-12 20:21:17 +00002534 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2535 size = sizeof(COMMCONFIG);
2536 GetDefaultCommConfig(filename, &comcfg, &size);
2537 comcfg.dcb.DCBlength = sizeof(DCB);
2538 CommConfigDialog(filename, NULL, &comcfg);
2539
2540 if (!SetCommState(s->hcom, &comcfg.dcb)) {
2541 fprintf(stderr, "Failed SetCommState\n");
2542 goto fail;
2543 }
2544
2545 if (!SetCommMask(s->hcom, EV_ERR)) {
2546 fprintf(stderr, "Failed SetCommMask\n");
2547 goto fail;
2548 }
2549
2550 cto.ReadIntervalTimeout = MAXDWORD;
2551 if (!SetCommTimeouts(s->hcom, &cto)) {
2552 fprintf(stderr, "Failed SetCommTimeouts\n");
2553 goto fail;
2554 }
ths3b46e622007-09-17 08:09:54 +00002555
bellardf3311102006-04-12 20:21:17 +00002556 if (!ClearCommError(s->hcom, &err, &comstat)) {
2557 fprintf(stderr, "Failed ClearCommError\n");
2558 goto fail;
2559 }
ths087f4ae2007-02-10 21:50:42 +00002560 qemu_add_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002561 return 0;
2562
2563 fail:
ths087f4ae2007-02-10 21:50:42 +00002564 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002565 return -1;
2566}
2567
2568static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2569{
2570 WinCharState *s = chr->opaque;
2571 DWORD len, ret, size, err;
2572
2573 len = len1;
2574 ZeroMemory(&s->osend, sizeof(s->osend));
2575 s->osend.hEvent = s->hsend;
2576 while (len > 0) {
2577 if (s->hsend)
2578 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2579 else
2580 ret = WriteFile(s->hcom, buf, len, &size, NULL);
2581 if (!ret) {
2582 err = GetLastError();
2583 if (err == ERROR_IO_PENDING) {
2584 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2585 if (ret) {
2586 buf += size;
2587 len -= size;
2588 } else {
2589 break;
2590 }
2591 } else {
2592 break;
2593 }
2594 } else {
2595 buf += size;
2596 len -= size;
2597 }
2598 }
2599 return len1 - len;
2600}
2601
ths087f4ae2007-02-10 21:50:42 +00002602static int win_chr_read_poll(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002603{
ths087f4ae2007-02-10 21:50:42 +00002604 WinCharState *s = chr->opaque;
2605
2606 s->max_size = qemu_chr_can_read(chr);
bellardf3311102006-04-12 20:21:17 +00002607 return s->max_size;
2608}
pbrooke5b0bc42007-01-27 23:46:43 +00002609
ths087f4ae2007-02-10 21:50:42 +00002610static void win_chr_readfile(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002611{
ths087f4ae2007-02-10 21:50:42 +00002612 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002613 int ret, err;
2614 uint8_t buf[1024];
2615 DWORD size;
ths3b46e622007-09-17 08:09:54 +00002616
bellardf3311102006-04-12 20:21:17 +00002617 ZeroMemory(&s->orecv, sizeof(s->orecv));
2618 s->orecv.hEvent = s->hrecv;
2619 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2620 if (!ret) {
2621 err = GetLastError();
2622 if (err == ERROR_IO_PENDING) {
2623 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2624 }
2625 }
2626
2627 if (size > 0) {
ths087f4ae2007-02-10 21:50:42 +00002628 qemu_chr_read(chr, buf, size);
bellardf3311102006-04-12 20:21:17 +00002629 }
2630}
2631
ths087f4ae2007-02-10 21:50:42 +00002632static void win_chr_read(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002633{
ths087f4ae2007-02-10 21:50:42 +00002634 WinCharState *s = chr->opaque;
2635
bellardf3311102006-04-12 20:21:17 +00002636 if (s->len > s->max_size)
2637 s->len = s->max_size;
2638 if (s->len == 0)
2639 return;
ths3b46e622007-09-17 08:09:54 +00002640
ths087f4ae2007-02-10 21:50:42 +00002641 win_chr_readfile(chr);
bellardf3311102006-04-12 20:21:17 +00002642}
2643
2644static int win_chr_poll(void *opaque)
2645{
ths087f4ae2007-02-10 21:50:42 +00002646 CharDriverState *chr = opaque;
2647 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002648 COMSTAT status;
2649 DWORD comerr;
ths3b46e622007-09-17 08:09:54 +00002650
bellardf3311102006-04-12 20:21:17 +00002651 ClearCommError(s->hcom, &comerr, &status);
2652 if (status.cbInQue > 0) {
2653 s->len = status.cbInQue;
ths087f4ae2007-02-10 21:50:42 +00002654 win_chr_read_poll(chr);
2655 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002656 return 1;
2657 }
2658 return 0;
2659}
2660
ths52f61fd2006-12-22 21:20:52 +00002661static CharDriverState *qemu_chr_open_win(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002662{
2663 CharDriverState *chr;
2664 WinCharState *s;
ths3b46e622007-09-17 08:09:54 +00002665
bellardf3311102006-04-12 20:21:17 +00002666 chr = qemu_mallocz(sizeof(CharDriverState));
2667 if (!chr)
2668 return NULL;
2669 s = qemu_mallocz(sizeof(WinCharState));
2670 if (!s) {
2671 free(chr);
2672 return NULL;
2673 }
2674 chr->opaque = s;
2675 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002676 chr->chr_close = win_chr_close;
2677
ths087f4ae2007-02-10 21:50:42 +00002678 if (win_chr_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002679 free(s);
2680 free(chr);
2681 return NULL;
2682 }
ths86e94de2007-01-05 22:01:59 +00002683 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002684 return chr;
2685}
2686
2687static int win_chr_pipe_poll(void *opaque)
2688{
ths087f4ae2007-02-10 21:50:42 +00002689 CharDriverState *chr = opaque;
2690 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002691 DWORD size;
2692
2693 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2694 if (size > 0) {
2695 s->len = size;
ths087f4ae2007-02-10 21:50:42 +00002696 win_chr_read_poll(chr);
2697 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002698 return 1;
2699 }
2700 return 0;
2701}
2702
ths087f4ae2007-02-10 21:50:42 +00002703static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002704{
ths087f4ae2007-02-10 21:50:42 +00002705 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002706 OVERLAPPED ov;
2707 int ret;
2708 DWORD size;
2709 char openname[256];
ths3b46e622007-09-17 08:09:54 +00002710
bellardf3311102006-04-12 20:21:17 +00002711 s->fpipe = TRUE;
2712
2713 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2714 if (!s->hsend) {
2715 fprintf(stderr, "Failed CreateEvent\n");
2716 goto fail;
2717 }
2718 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2719 if (!s->hrecv) {
2720 fprintf(stderr, "Failed CreateEvent\n");
2721 goto fail;
2722 }
ths3b46e622007-09-17 08:09:54 +00002723
bellardf3311102006-04-12 20:21:17 +00002724 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2725 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2726 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2727 PIPE_WAIT,
2728 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2729 if (s->hcom == INVALID_HANDLE_VALUE) {
2730 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2731 s->hcom = NULL;
2732 goto fail;
2733 }
2734
2735 ZeroMemory(&ov, sizeof(ov));
2736 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2737 ret = ConnectNamedPipe(s->hcom, &ov);
2738 if (ret) {
2739 fprintf(stderr, "Failed ConnectNamedPipe\n");
2740 goto fail;
2741 }
2742
2743 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2744 if (!ret) {
2745 fprintf(stderr, "Failed GetOverlappedResult\n");
2746 if (ov.hEvent) {
2747 CloseHandle(ov.hEvent);
2748 ov.hEvent = NULL;
2749 }
2750 goto fail;
2751 }
2752
2753 if (ov.hEvent) {
2754 CloseHandle(ov.hEvent);
2755 ov.hEvent = NULL;
2756 }
ths087f4ae2007-02-10 21:50:42 +00002757 qemu_add_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002758 return 0;
2759
2760 fail:
ths087f4ae2007-02-10 21:50:42 +00002761 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002762 return -1;
2763}
2764
2765
ths52f61fd2006-12-22 21:20:52 +00002766static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002767{
2768 CharDriverState *chr;
2769 WinCharState *s;
2770
2771 chr = qemu_mallocz(sizeof(CharDriverState));
2772 if (!chr)
2773 return NULL;
2774 s = qemu_mallocz(sizeof(WinCharState));
2775 if (!s) {
2776 free(chr);
2777 return NULL;
2778 }
2779 chr->opaque = s;
2780 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002781 chr->chr_close = win_chr_close;
ths3b46e622007-09-17 08:09:54 +00002782
ths087f4ae2007-02-10 21:50:42 +00002783 if (win_chr_pipe_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002784 free(s);
2785 free(chr);
2786 return NULL;
2787 }
ths86e94de2007-01-05 22:01:59 +00002788 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002789 return chr;
2790}
2791
ths52f61fd2006-12-22 21:20:52 +00002792static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
bellardf3311102006-04-12 20:21:17 +00002793{
2794 CharDriverState *chr;
2795 WinCharState *s;
2796
2797 chr = qemu_mallocz(sizeof(CharDriverState));
2798 if (!chr)
2799 return NULL;
2800 s = qemu_mallocz(sizeof(WinCharState));
2801 if (!s) {
2802 free(chr);
2803 return NULL;
2804 }
2805 s->hcom = fd_out;
2806 chr->opaque = s;
2807 chr->chr_write = win_chr_write;
ths86e94de2007-01-05 22:01:59 +00002808 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002809 return chr;
2810}
ths72d46472007-05-13 14:54:54 +00002811
2812static CharDriverState *qemu_chr_open_win_con(const char *filename)
2813{
2814 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2815}
2816
ths52f61fd2006-12-22 21:20:52 +00002817static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
bellardf3311102006-04-12 20:21:17 +00002818{
2819 HANDLE fd_out;
ths3b46e622007-09-17 08:09:54 +00002820
bellardf3311102006-04-12 20:21:17 +00002821 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
2822 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2823 if (fd_out == INVALID_HANDLE_VALUE)
2824 return NULL;
2825
2826 return qemu_chr_open_win_file(fd_out);
2827}
thsaec62502007-06-25 11:48:07 +00002828#endif /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00002829
bellard0bab00f2006-06-25 14:49:44 +00002830/***********************************************************/
2831/* UDP Net console */
2832
2833typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002834 int fd;
2835 struct sockaddr_in daddr;
2836 char buf[1024];
2837 int bufcnt;
2838 int bufptr;
2839 int max_size;
2840} NetCharDriver;
2841
2842static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2843{
2844 NetCharDriver *s = chr->opaque;
2845
2846 return sendto(s->fd, buf, len, 0,
2847 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
2848}
2849
2850static int udp_chr_read_poll(void *opaque)
2851{
2852 CharDriverState *chr = opaque;
2853 NetCharDriver *s = chr->opaque;
2854
pbrooke5b0bc42007-01-27 23:46:43 +00002855 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002856
2857 /* If there were any stray characters in the queue process them
2858 * first
2859 */
2860 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002861 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002862 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002863 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002864 }
2865 return s->max_size;
2866}
2867
2868static void udp_chr_read(void *opaque)
2869{
2870 CharDriverState *chr = opaque;
2871 NetCharDriver *s = chr->opaque;
2872
2873 if (s->max_size == 0)
2874 return;
2875 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
2876 s->bufptr = s->bufcnt;
2877 if (s->bufcnt <= 0)
2878 return;
2879
2880 s->bufptr = 0;
2881 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002882 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002883 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002884 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002885 }
2886}
2887
pbrooke5b0bc42007-01-27 23:46:43 +00002888static void udp_chr_update_read_handler(CharDriverState *chr)
bellard0bab00f2006-06-25 14:49:44 +00002889{
2890 NetCharDriver *s = chr->opaque;
2891
2892 if (s->fd >= 0) {
bellard0bab00f2006-06-25 14:49:44 +00002893 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2894 udp_chr_read, NULL, chr);
2895 }
2896}
2897
2898int parse_host_port(struct sockaddr_in *saddr, const char *str);
ths52f61fd2006-12-22 21:20:52 +00002899#ifndef _WIN32
2900static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
2901#endif
bellard951f1352006-06-27 21:02:43 +00002902int parse_host_src_port(struct sockaddr_in *haddr,
2903 struct sockaddr_in *saddr,
2904 const char *str);
bellard0bab00f2006-06-25 14:49:44 +00002905
ths52f61fd2006-12-22 21:20:52 +00002906static CharDriverState *qemu_chr_open_udp(const char *def)
bellard0bab00f2006-06-25 14:49:44 +00002907{
2908 CharDriverState *chr = NULL;
2909 NetCharDriver *s = NULL;
2910 int fd = -1;
bellard951f1352006-06-27 21:02:43 +00002911 struct sockaddr_in saddr;
bellard0bab00f2006-06-25 14:49:44 +00002912
2913 chr = qemu_mallocz(sizeof(CharDriverState));
2914 if (!chr)
2915 goto return_err;
2916 s = qemu_mallocz(sizeof(NetCharDriver));
2917 if (!s)
2918 goto return_err;
2919
2920 fd = socket(PF_INET, SOCK_DGRAM, 0);
2921 if (fd < 0) {
2922 perror("socket(PF_INET, SOCK_DGRAM)");
2923 goto return_err;
2924 }
2925
bellard951f1352006-06-27 21:02:43 +00002926 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
2927 printf("Could not parse: %s\n", def);
2928 goto return_err;
bellard0bab00f2006-06-25 14:49:44 +00002929 }
2930
bellard951f1352006-06-27 21:02:43 +00002931 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
bellard0bab00f2006-06-25 14:49:44 +00002932 {
2933 perror("bind");
2934 goto return_err;
2935 }
2936
2937 s->fd = fd;
2938 s->bufcnt = 0;
2939 s->bufptr = 0;
2940 chr->opaque = s;
2941 chr->chr_write = udp_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002942 chr->chr_update_read_handler = udp_chr_update_read_handler;
bellard0bab00f2006-06-25 14:49:44 +00002943 return chr;
2944
2945return_err:
2946 if (chr)
2947 free(chr);
2948 if (s)
2949 free(s);
2950 if (fd >= 0)
2951 closesocket(fd);
2952 return NULL;
2953}
2954
2955/***********************************************************/
2956/* TCP Net console */
2957
2958typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002959 int fd, listen_fd;
2960 int connected;
2961 int max_size;
bellard951f1352006-06-27 21:02:43 +00002962 int do_telnetopt;
pbrooke5b0bc42007-01-27 23:46:43 +00002963 int do_nodelay;
thsffd843b2006-12-21 19:46:43 +00002964 int is_unix;
bellard0bab00f2006-06-25 14:49:44 +00002965} TCPCharDriver;
2966
2967static void tcp_chr_accept(void *opaque);
2968
2969static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2970{
2971 TCPCharDriver *s = chr->opaque;
2972 if (s->connected) {
2973 return send_all(s->fd, buf, len);
2974 } else {
2975 /* XXX: indicate an error ? */
2976 return len;
2977 }
2978}
2979
2980static int tcp_chr_read_poll(void *opaque)
2981{
2982 CharDriverState *chr = opaque;
2983 TCPCharDriver *s = chr->opaque;
2984 if (!s->connected)
2985 return 0;
pbrooke5b0bc42007-01-27 23:46:43 +00002986 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002987 return s->max_size;
2988}
2989
bellard951f1352006-06-27 21:02:43 +00002990#define IAC 255
2991#define IAC_BREAK 243
2992static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
2993 TCPCharDriver *s,
2994 char *buf, int *size)
2995{
2996 /* Handle any telnet client's basic IAC options to satisfy char by
2997 * char mode with no echo. All IAC options will be removed from
2998 * the buf and the do_telnetopt variable will be used to track the
2999 * state of the width of the IAC information.
3000 *
3001 * IAC commands come in sets of 3 bytes with the exception of the
3002 * "IAC BREAK" command and the double IAC.
3003 */
3004
3005 int i;
3006 int j = 0;
3007
3008 for (i = 0; i < *size; i++) {
3009 if (s->do_telnetopt > 1) {
3010 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3011 /* Double IAC means send an IAC */
3012 if (j != i)
3013 buf[j] = buf[i];
3014 j++;
3015 s->do_telnetopt = 1;
3016 } else {
3017 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3018 /* Handle IAC break commands by sending a serial break */
pbrooke5b0bc42007-01-27 23:46:43 +00003019 qemu_chr_event(chr, CHR_EVENT_BREAK);
bellard951f1352006-06-27 21:02:43 +00003020 s->do_telnetopt++;
3021 }
3022 s->do_telnetopt++;
3023 }
3024 if (s->do_telnetopt >= 4) {
3025 s->do_telnetopt = 1;
3026 }
3027 } else {
3028 if ((unsigned char)buf[i] == IAC) {
3029 s->do_telnetopt = 2;
3030 } else {
3031 if (j != i)
3032 buf[j] = buf[i];
3033 j++;
3034 }
3035 }
3036 }
3037 *size = j;
3038}
3039
bellard0bab00f2006-06-25 14:49:44 +00003040static void tcp_chr_read(void *opaque)
3041{
3042 CharDriverState *chr = opaque;
3043 TCPCharDriver *s = chr->opaque;
3044 uint8_t buf[1024];
3045 int len, size;
3046
3047 if (!s->connected || s->max_size <= 0)
3048 return;
3049 len = sizeof(buf);
3050 if (len > s->max_size)
3051 len = s->max_size;
3052 size = recv(s->fd, buf, len, 0);
3053 if (size == 0) {
3054 /* connection closed */
3055 s->connected = 0;
3056 if (s->listen_fd >= 0) {
3057 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3058 }
3059 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3060 closesocket(s->fd);
3061 s->fd = -1;
3062 } else if (size > 0) {
bellard951f1352006-06-27 21:02:43 +00003063 if (s->do_telnetopt)
3064 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3065 if (size > 0)
pbrooke5b0bc42007-01-27 23:46:43 +00003066 qemu_chr_read(chr, buf, size);
bellard0bab00f2006-06-25 14:49:44 +00003067 }
3068}
3069
bellard0bab00f2006-06-25 14:49:44 +00003070static void tcp_chr_connect(void *opaque)
3071{
3072 CharDriverState *chr = opaque;
3073 TCPCharDriver *s = chr->opaque;
3074
3075 s->connected = 1;
3076 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3077 tcp_chr_read, NULL, chr);
ths86e94de2007-01-05 22:01:59 +00003078 qemu_chr_reset(chr);
bellard0bab00f2006-06-25 14:49:44 +00003079}
3080
bellard951f1352006-06-27 21:02:43 +00003081#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3082static void tcp_chr_telnet_init(int fd)
3083{
3084 char buf[3];
3085 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3086 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3087 send(fd, (char *)buf, 3, 0);
3088 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3089 send(fd, (char *)buf, 3, 0);
3090 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3091 send(fd, (char *)buf, 3, 0);
3092 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3093 send(fd, (char *)buf, 3, 0);
3094}
3095
pbrookf7499982007-01-28 00:10:01 +00003096static void socket_set_nodelay(int fd)
3097{
3098 int val = 1;
3099 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3100}
3101
bellard0bab00f2006-06-25 14:49:44 +00003102static void tcp_chr_accept(void *opaque)
3103{
3104 CharDriverState *chr = opaque;
3105 TCPCharDriver *s = chr->opaque;
3106 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003107#ifndef _WIN32
3108 struct sockaddr_un uaddr;
3109#endif
3110 struct sockaddr *addr;
bellard0bab00f2006-06-25 14:49:44 +00003111 socklen_t len;
3112 int fd;
3113
3114 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003115#ifndef _WIN32
3116 if (s->is_unix) {
3117 len = sizeof(uaddr);
3118 addr = (struct sockaddr *)&uaddr;
3119 } else
3120#endif
3121 {
3122 len = sizeof(saddr);
3123 addr = (struct sockaddr *)&saddr;
3124 }
3125 fd = accept(s->listen_fd, addr, &len);
bellard0bab00f2006-06-25 14:49:44 +00003126 if (fd < 0 && errno != EINTR) {
3127 return;
3128 } else if (fd >= 0) {
bellard951f1352006-06-27 21:02:43 +00003129 if (s->do_telnetopt)
3130 tcp_chr_telnet_init(fd);
bellard0bab00f2006-06-25 14:49:44 +00003131 break;
3132 }
3133 }
3134 socket_set_nonblock(fd);
pbrookf7499982007-01-28 00:10:01 +00003135 if (s->do_nodelay)
3136 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003137 s->fd = fd;
3138 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3139 tcp_chr_connect(chr);
3140}
3141
3142static void tcp_chr_close(CharDriverState *chr)
3143{
3144 TCPCharDriver *s = chr->opaque;
3145 if (s->fd >= 0)
3146 closesocket(s->fd);
3147 if (s->listen_fd >= 0)
3148 closesocket(s->listen_fd);
3149 qemu_free(s);
3150}
3151
ths5fafdf22007-09-16 21:08:06 +00003152static CharDriverState *qemu_chr_open_tcp(const char *host_str,
thsffd843b2006-12-21 19:46:43 +00003153 int is_telnet,
3154 int is_unix)
bellard0bab00f2006-06-25 14:49:44 +00003155{
3156 CharDriverState *chr = NULL;
3157 TCPCharDriver *s = NULL;
3158 int fd = -1, ret, err, val;
bellard951f1352006-06-27 21:02:43 +00003159 int is_listen = 0;
3160 int is_waitconnect = 1;
pbrookf7499982007-01-28 00:10:01 +00003161 int do_nodelay = 0;
bellard951f1352006-06-27 21:02:43 +00003162 const char *ptr;
bellard0bab00f2006-06-25 14:49:44 +00003163 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003164#ifndef _WIN32
3165 struct sockaddr_un uaddr;
3166#endif
3167 struct sockaddr *addr;
3168 socklen_t addrlen;
bellard0bab00f2006-06-25 14:49:44 +00003169
thsffd843b2006-12-21 19:46:43 +00003170#ifndef _WIN32
3171 if (is_unix) {
3172 addr = (struct sockaddr *)&uaddr;
3173 addrlen = sizeof(uaddr);
3174 if (parse_unix_path(&uaddr, host_str) < 0)
3175 goto fail;
3176 } else
3177#endif
3178 {
3179 addr = (struct sockaddr *)&saddr;
3180 addrlen = sizeof(saddr);
3181 if (parse_host_port(&saddr, host_str) < 0)
3182 goto fail;
3183 }
bellard0bab00f2006-06-25 14:49:44 +00003184
bellard951f1352006-06-27 21:02:43 +00003185 ptr = host_str;
3186 while((ptr = strchr(ptr,','))) {
3187 ptr++;
3188 if (!strncmp(ptr,"server",6)) {
3189 is_listen = 1;
3190 } else if (!strncmp(ptr,"nowait",6)) {
3191 is_waitconnect = 0;
pbrookf7499982007-01-28 00:10:01 +00003192 } else if (!strncmp(ptr,"nodelay",6)) {
3193 do_nodelay = 1;
bellard951f1352006-06-27 21:02:43 +00003194 } else {
3195 printf("Unknown option: %s\n", ptr);
3196 goto fail;
3197 }
3198 }
3199 if (!is_listen)
3200 is_waitconnect = 0;
3201
bellard0bab00f2006-06-25 14:49:44 +00003202 chr = qemu_mallocz(sizeof(CharDriverState));
3203 if (!chr)
3204 goto fail;
3205 s = qemu_mallocz(sizeof(TCPCharDriver));
3206 if (!s)
3207 goto fail;
thsffd843b2006-12-21 19:46:43 +00003208
3209#ifndef _WIN32
3210 if (is_unix)
3211 fd = socket(PF_UNIX, SOCK_STREAM, 0);
3212 else
3213#endif
3214 fd = socket(PF_INET, SOCK_STREAM, 0);
ths5fafdf22007-09-16 21:08:06 +00003215
3216 if (fd < 0)
bellard0bab00f2006-06-25 14:49:44 +00003217 goto fail;
bellard951f1352006-06-27 21:02:43 +00003218
3219 if (!is_waitconnect)
3220 socket_set_nonblock(fd);
bellard0bab00f2006-06-25 14:49:44 +00003221
3222 s->connected = 0;
3223 s->fd = -1;
3224 s->listen_fd = -1;
thsffd843b2006-12-21 19:46:43 +00003225 s->is_unix = is_unix;
pbrookf7499982007-01-28 00:10:01 +00003226 s->do_nodelay = do_nodelay && !is_unix;
thsffd843b2006-12-21 19:46:43 +00003227
3228 chr->opaque = s;
3229 chr->chr_write = tcp_chr_write;
thsffd843b2006-12-21 19:46:43 +00003230 chr->chr_close = tcp_chr_close;
3231
bellard0bab00f2006-06-25 14:49:44 +00003232 if (is_listen) {
3233 /* allow fast reuse */
thsffd843b2006-12-21 19:46:43 +00003234#ifndef _WIN32
3235 if (is_unix) {
3236 char path[109];
3237 strncpy(path, uaddr.sun_path, 108);
3238 path[108] = 0;
3239 unlink(path);
3240 } else
3241#endif
3242 {
3243 val = 1;
3244 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3245 }
ths3b46e622007-09-17 08:09:54 +00003246
thsffd843b2006-12-21 19:46:43 +00003247 ret = bind(fd, addr, addrlen);
3248 if (ret < 0)
bellard0bab00f2006-06-25 14:49:44 +00003249 goto fail;
thsffd843b2006-12-21 19:46:43 +00003250
bellard0bab00f2006-06-25 14:49:44 +00003251 ret = listen(fd, 0);
3252 if (ret < 0)
3253 goto fail;
thsffd843b2006-12-21 19:46:43 +00003254
bellard0bab00f2006-06-25 14:49:44 +00003255 s->listen_fd = fd;
3256 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
bellard951f1352006-06-27 21:02:43 +00003257 if (is_telnet)
3258 s->do_telnetopt = 1;
bellard0bab00f2006-06-25 14:49:44 +00003259 } else {
3260 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003261 ret = connect(fd, addr, addrlen);
bellard0bab00f2006-06-25 14:49:44 +00003262 if (ret < 0) {
3263 err = socket_error();
3264 if (err == EINTR || err == EWOULDBLOCK) {
3265 } else if (err == EINPROGRESS) {
3266 break;
thsf5b12262007-03-25 15:58:03 +00003267#ifdef _WIN32
3268 } else if (err == WSAEALREADY) {
3269 break;
3270#endif
bellard0bab00f2006-06-25 14:49:44 +00003271 } else {
3272 goto fail;
3273 }
3274 } else {
3275 s->connected = 1;
3276 break;
3277 }
3278 }
3279 s->fd = fd;
pbrookf7499982007-01-28 00:10:01 +00003280 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003281 if (s->connected)
3282 tcp_chr_connect(chr);
3283 else
3284 qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3285 }
ths3b46e622007-09-17 08:09:54 +00003286
bellard951f1352006-06-27 21:02:43 +00003287 if (is_listen && is_waitconnect) {
3288 printf("QEMU waiting for connection on: %s\n", host_str);
3289 tcp_chr_accept(chr);
3290 socket_set_nonblock(s->listen_fd);
3291 }
3292
bellard0bab00f2006-06-25 14:49:44 +00003293 return chr;
3294 fail:
3295 if (fd >= 0)
3296 closesocket(fd);
3297 qemu_free(s);
3298 qemu_free(chr);
3299 return NULL;
3300}
3301
bellard82c643f2004-07-14 17:28:13 +00003302CharDriverState *qemu_chr_open(const char *filename)
3303{
bellardf8d179e2005-11-08 22:30:36 +00003304 const char *p;
bellardfd1dff42006-02-01 21:29:26 +00003305
bellard82c643f2004-07-14 17:28:13 +00003306 if (!strcmp(filename, "vc")) {
thsaf3a9032007-07-11 23:14:59 +00003307 return text_console_init(&display_state, 0);
3308 } else if (strstart(filename, "vc:", &p)) {
3309 return text_console_init(&display_state, p);
bellard82c643f2004-07-14 17:28:13 +00003310 } else if (!strcmp(filename, "null")) {
3311 return qemu_chr_open_null();
ths5fafdf22007-09-16 21:08:06 +00003312 } else
bellard0bab00f2006-06-25 14:49:44 +00003313 if (strstart(filename, "tcp:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003314 return qemu_chr_open_tcp(p, 0, 0);
bellard0bab00f2006-06-25 14:49:44 +00003315 } else
bellard951f1352006-06-27 21:02:43 +00003316 if (strstart(filename, "telnet:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003317 return qemu_chr_open_tcp(p, 1, 0);
bellard0bab00f2006-06-25 14:49:44 +00003318 } else
3319 if (strstart(filename, "udp:", &p)) {
3320 return qemu_chr_open_udp(p);
3321 } else
ths20d8a3e2007-02-18 17:04:49 +00003322 if (strstart(filename, "mon:", &p)) {
3323 CharDriverState *drv = qemu_chr_open(p);
3324 if (drv) {
3325 drv = qemu_chr_open_mux(drv);
3326 monitor_init(drv, !nographic);
3327 return drv;
3328 }
3329 printf("Unable to open driver: %s\n", p);
3330 return 0;
3331 } else
bellard76647282005-11-27 19:10:42 +00003332#ifndef _WIN32
thsffd843b2006-12-21 19:46:43 +00003333 if (strstart(filename, "unix:", &p)) {
3334 return qemu_chr_open_tcp(p, 0, 1);
3335 } else if (strstart(filename, "file:", &p)) {
bellardf8d179e2005-11-08 22:30:36 +00003336 return qemu_chr_open_file_out(p);
3337 } else if (strstart(filename, "pipe:", &p)) {
3338 return qemu_chr_open_pipe(p);
bellard76647282005-11-27 19:10:42 +00003339 } else if (!strcmp(filename, "pty")) {
bellard82c643f2004-07-14 17:28:13 +00003340 return qemu_chr_open_pty();
3341 } else if (!strcmp(filename, "stdio")) {
3342 return qemu_chr_open_stdio();
ths5fafdf22007-09-16 21:08:06 +00003343 } else
bellardf8d179e2005-11-08 22:30:36 +00003344#if defined(__linux__)
bellarde57a8c02005-11-10 23:58:52 +00003345 if (strstart(filename, "/dev/parport", NULL)) {
3346 return qemu_chr_open_pp(filename);
ths5fafdf22007-09-16 21:08:06 +00003347 } else
thsaec62502007-06-25 11:48:07 +00003348#endif
ths3fda3882007-06-28 15:14:49 +00003349#if defined(__linux__) || defined(__sun__)
bellardf8d179e2005-11-08 22:30:36 +00003350 if (strstart(filename, "/dev/", NULL)) {
3351 return qemu_chr_open_tty(filename);
ths3fda3882007-06-28 15:14:49 +00003352 } else
3353#endif
thsaec62502007-06-25 11:48:07 +00003354#else /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00003355 if (strstart(filename, "COM", NULL)) {
3356 return qemu_chr_open_win(filename);
3357 } else
3358 if (strstart(filename, "pipe:", &p)) {
3359 return qemu_chr_open_win_pipe(p);
3360 } else
ths72d46472007-05-13 14:54:54 +00003361 if (strstart(filename, "con:", NULL)) {
3362 return qemu_chr_open_win_con(filename);
3363 } else
bellardf3311102006-04-12 20:21:17 +00003364 if (strstart(filename, "file:", &p)) {
3365 return qemu_chr_open_win_file_out(p);
3366 }
3367#endif
bellard82c643f2004-07-14 17:28:13 +00003368 {
3369 return NULL;
3370 }
3371}
3372
bellardf3311102006-04-12 20:21:17 +00003373void qemu_chr_close(CharDriverState *chr)
3374{
3375 if (chr->chr_close)
3376 chr->chr_close(chr);
3377}
3378
bellardf1510b22003-06-25 00:07:40 +00003379/***********************************************************/
bellard7c9d8e02005-11-15 22:16:05 +00003380/* network device redirectors */
bellardf1510b22003-06-25 00:07:40 +00003381
bellardc20709a2004-04-21 23:27:19 +00003382void hex_dump(FILE *f, const uint8_t *buf, int size)
bellard67b915a2004-03-31 23:37:16 +00003383{
bellardc20709a2004-04-21 23:27:19 +00003384 int len, i, j, c;
3385
3386 for(i=0;i<size;i+=16) {
3387 len = size - i;
3388 if (len > 16)
3389 len = 16;
3390 fprintf(f, "%08x ", i);
3391 for(j=0;j<16;j++) {
3392 if (j < len)
3393 fprintf(f, " %02x", buf[i+j]);
3394 else
3395 fprintf(f, " ");
3396 }
3397 fprintf(f, " ");
3398 for(j=0;j<len;j++) {
3399 c = buf[i+j];
3400 if (c < ' ' || c > '~')
3401 c = '.';
3402 fprintf(f, "%c", c);
3403 }
3404 fprintf(f, "\n");
3405 }
3406}
3407
bellard7c9d8e02005-11-15 22:16:05 +00003408static int parse_macaddr(uint8_t *macaddr, const char *p)
bellardc20709a2004-04-21 23:27:19 +00003409{
bellard7c9d8e02005-11-15 22:16:05 +00003410 int i;
3411 for(i = 0; i < 6; i++) {
3412 macaddr[i] = strtol(p, (char **)&p, 16);
3413 if (i == 5) {
ths5fafdf22007-09-16 21:08:06 +00003414 if (*p != '\0')
bellard7c9d8e02005-11-15 22:16:05 +00003415 return -1;
3416 } else {
ths5fafdf22007-09-16 21:08:06 +00003417 if (*p != ':')
bellard7c9d8e02005-11-15 22:16:05 +00003418 return -1;
3419 p++;
3420 }
bellardc20709a2004-04-21 23:27:19 +00003421 }
bellardc20709a2004-04-21 23:27:19 +00003422 return 0;
3423}
3424
bellard9bf05442004-08-25 22:12:49 +00003425static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3426{
3427 const char *p, *p1;
3428 int len;
3429 p = *pp;
3430 p1 = strchr(p, sep);
3431 if (!p1)
3432 return -1;
3433 len = p1 - p;
3434 p1++;
3435 if (buf_size > 0) {
3436 if (len > buf_size - 1)
3437 len = buf_size - 1;
3438 memcpy(buf, p, len);
3439 buf[len] = '\0';
3440 }
3441 *pp = p1;
3442 return 0;
3443}
3444
bellard951f1352006-06-27 21:02:43 +00003445int parse_host_src_port(struct sockaddr_in *haddr,
3446 struct sockaddr_in *saddr,
3447 const char *input_str)
3448{
3449 char *str = strdup(input_str);
3450 char *host_str = str;
3451 char *src_str;
3452 char *ptr;
3453
3454 /*
3455 * Chop off any extra arguments at the end of the string which
3456 * would start with a comma, then fill in the src port information
3457 * if it was provided else use the "any address" and "any port".
3458 */
3459 if ((ptr = strchr(str,',')))
3460 *ptr = '\0';
3461
3462 if ((src_str = strchr(input_str,'@'))) {
3463 *src_str = '\0';
3464 src_str++;
3465 }
3466
3467 if (parse_host_port(haddr, host_str) < 0)
3468 goto fail;
3469
3470 if (!src_str || *src_str == '\0')
3471 src_str = ":0";
3472
3473 if (parse_host_port(saddr, src_str) < 0)
3474 goto fail;
3475
3476 free(str);
3477 return(0);
3478
3479fail:
3480 free(str);
3481 return -1;
3482}
3483
bellard7c9d8e02005-11-15 22:16:05 +00003484int parse_host_port(struct sockaddr_in *saddr, const char *str)
3485{
3486 char buf[512];
3487 struct hostent *he;
3488 const char *p, *r;
3489 int port;
3490
3491 p = str;
3492 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3493 return -1;
3494 saddr->sin_family = AF_INET;
3495 if (buf[0] == '\0') {
3496 saddr->sin_addr.s_addr = 0;
3497 } else {
3498 if (isdigit(buf[0])) {
3499 if (!inet_aton(buf, &saddr->sin_addr))
3500 return -1;
3501 } else {
bellard7c9d8e02005-11-15 22:16:05 +00003502 if ((he = gethostbyname(buf)) == NULL)
3503 return - 1;
3504 saddr->sin_addr = *(struct in_addr *)he->h_addr;
bellard7c9d8e02005-11-15 22:16:05 +00003505 }
3506 }
3507 port = strtol(p, (char **)&r, 0);
3508 if (r == p)
3509 return -1;
3510 saddr->sin_port = htons(port);
3511 return 0;
3512}
3513
ths52f61fd2006-12-22 21:20:52 +00003514#ifndef _WIN32
3515static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
thsffd843b2006-12-21 19:46:43 +00003516{
3517 const char *p;
3518 int len;
3519
3520 len = MIN(108, strlen(str));
3521 p = strchr(str, ',');
3522 if (p)
3523 len = MIN(len, p - str);
3524
3525 memset(uaddr, 0, sizeof(*uaddr));
3526
3527 uaddr->sun_family = AF_UNIX;
3528 memcpy(uaddr->sun_path, str, len);
3529
3530 return 0;
3531}
ths52f61fd2006-12-22 21:20:52 +00003532#endif
thsffd843b2006-12-21 19:46:43 +00003533
bellard7c9d8e02005-11-15 22:16:05 +00003534/* find or alloc a new VLAN */
3535VLANState *qemu_find_vlan(int id)
3536{
3537 VLANState **pvlan, *vlan;
3538 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3539 if (vlan->id == id)
3540 return vlan;
3541 }
3542 vlan = qemu_mallocz(sizeof(VLANState));
3543 if (!vlan)
3544 return NULL;
3545 vlan->id = id;
3546 vlan->next = NULL;
3547 pvlan = &first_vlan;
3548 while (*pvlan != NULL)
3549 pvlan = &(*pvlan)->next;
3550 *pvlan = vlan;
3551 return vlan;
3552}
3553
3554VLANClientState *qemu_new_vlan_client(VLANState *vlan,
pbrookd861b052006-02-04 22:15:28 +00003555 IOReadHandler *fd_read,
3556 IOCanRWHandler *fd_can_read,
3557 void *opaque)
bellard7c9d8e02005-11-15 22:16:05 +00003558{
3559 VLANClientState *vc, **pvc;
3560 vc = qemu_mallocz(sizeof(VLANClientState));
3561 if (!vc)
3562 return NULL;
3563 vc->fd_read = fd_read;
pbrookd861b052006-02-04 22:15:28 +00003564 vc->fd_can_read = fd_can_read;
bellard7c9d8e02005-11-15 22:16:05 +00003565 vc->opaque = opaque;
3566 vc->vlan = vlan;
3567
3568 vc->next = NULL;
3569 pvc = &vlan->first_client;
3570 while (*pvc != NULL)
3571 pvc = &(*pvc)->next;
3572 *pvc = vc;
3573 return vc;
3574}
3575
pbrookd861b052006-02-04 22:15:28 +00003576int qemu_can_send_packet(VLANClientState *vc1)
3577{
3578 VLANState *vlan = vc1->vlan;
3579 VLANClientState *vc;
3580
3581 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3582 if (vc != vc1) {
balrogfbd17112007-07-02 13:31:53 +00003583 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3584 return 1;
pbrookd861b052006-02-04 22:15:28 +00003585 }
3586 }
balrogfbd17112007-07-02 13:31:53 +00003587 return 0;
pbrookd861b052006-02-04 22:15:28 +00003588}
3589
bellard7c9d8e02005-11-15 22:16:05 +00003590void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3591{
3592 VLANState *vlan = vc1->vlan;
3593 VLANClientState *vc;
3594
3595#if 0
3596 printf("vlan %d send:\n", vlan->id);
3597 hex_dump(stdout, buf, size);
3598#endif
3599 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3600 if (vc != vc1) {
3601 vc->fd_read(vc->opaque, buf, size);
3602 }
3603 }
3604}
3605
3606#if defined(CONFIG_SLIRP)
3607
3608/* slirp network adapter */
3609
3610static int slirp_inited;
3611static VLANClientState *slirp_vc;
3612
3613int slirp_can_output(void)
3614{
pbrook3b7f5d42006-02-10 17:34:02 +00003615 return !slirp_vc || qemu_can_send_packet(slirp_vc);
bellard7c9d8e02005-11-15 22:16:05 +00003616}
3617
3618void slirp_output(const uint8_t *pkt, int pkt_len)
3619{
3620#if 0
3621 printf("slirp output:\n");
3622 hex_dump(stdout, pkt, pkt_len);
3623#endif
pbrook3b7f5d42006-02-10 17:34:02 +00003624 if (!slirp_vc)
3625 return;
bellard7c9d8e02005-11-15 22:16:05 +00003626 qemu_send_packet(slirp_vc, pkt, pkt_len);
3627}
3628
3629static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3630{
3631#if 0
3632 printf("slirp input:\n");
3633 hex_dump(stdout, buf, size);
3634#endif
3635 slirp_input(buf, size);
3636}
3637
3638static int net_slirp_init(VLANState *vlan)
3639{
3640 if (!slirp_inited) {
3641 slirp_inited = 1;
3642 slirp_init();
3643 }
ths5fafdf22007-09-16 21:08:06 +00003644 slirp_vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00003645 slirp_receive, NULL, NULL);
bellard7c9d8e02005-11-15 22:16:05 +00003646 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3647 return 0;
3648}
3649
bellard9bf05442004-08-25 22:12:49 +00003650static void net_slirp_redir(const char *redir_str)
3651{
3652 int is_udp;
3653 char buf[256], *r;
3654 const char *p;
3655 struct in_addr guest_addr;
3656 int host_port, guest_port;
ths3b46e622007-09-17 08:09:54 +00003657
bellard9bf05442004-08-25 22:12:49 +00003658 if (!slirp_inited) {
3659 slirp_inited = 1;
3660 slirp_init();
3661 }
3662
3663 p = redir_str;
3664 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3665 goto fail;
3666 if (!strcmp(buf, "tcp")) {
3667 is_udp = 0;
3668 } else if (!strcmp(buf, "udp")) {
3669 is_udp = 1;
3670 } else {
3671 goto fail;
3672 }
3673
3674 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3675 goto fail;
3676 host_port = strtol(buf, &r, 0);
3677 if (r == buf)
3678 goto fail;
3679
3680 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3681 goto fail;
3682 if (buf[0] == '\0') {
3683 pstrcpy(buf, sizeof(buf), "10.0.2.15");
3684 }
3685 if (!inet_aton(buf, &guest_addr))
3686 goto fail;
ths3b46e622007-09-17 08:09:54 +00003687
bellard9bf05442004-08-25 22:12:49 +00003688 guest_port = strtol(p, &r, 0);
3689 if (r == p)
3690 goto fail;
ths3b46e622007-09-17 08:09:54 +00003691
bellard9bf05442004-08-25 22:12:49 +00003692 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
3693 fprintf(stderr, "qemu: could not set up redirection\n");
3694 exit(1);
3695 }
3696 return;
3697 fail:
3698 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3699 exit(1);
3700}
ths3b46e622007-09-17 08:09:54 +00003701
bellardc94c8d62004-09-13 21:37:34 +00003702#ifndef _WIN32
3703
bellard9d728e82004-09-05 23:09:03 +00003704char smb_dir[1024];
3705
3706static void smb_exit(void)
3707{
3708 DIR *d;
3709 struct dirent *de;
3710 char filename[1024];
3711
3712 /* erase all the files in the directory */
3713 d = opendir(smb_dir);
3714 for(;;) {
3715 de = readdir(d);
3716 if (!de)
3717 break;
3718 if (strcmp(de->d_name, ".") != 0 &&
3719 strcmp(de->d_name, "..") != 0) {
ths5fafdf22007-09-16 21:08:06 +00003720 snprintf(filename, sizeof(filename), "%s/%s",
bellard9d728e82004-09-05 23:09:03 +00003721 smb_dir, de->d_name);
3722 unlink(filename);
3723 }
3724 }
bellard03ffbb62004-09-06 00:14:04 +00003725 closedir(d);
bellard9d728e82004-09-05 23:09:03 +00003726 rmdir(smb_dir);
3727}
3728
3729/* automatic user mode samba server configuration */
3730void net_slirp_smb(const char *exported_dir)
3731{
3732 char smb_conf[1024];
3733 char smb_cmdline[1024];
3734 FILE *f;
3735
3736 if (!slirp_inited) {
3737 slirp_inited = 1;
3738 slirp_init();
3739 }
3740
3741 /* XXX: better tmp dir construction */
3742 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
3743 if (mkdir(smb_dir, 0700) < 0) {
3744 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
3745 exit(1);
3746 }
3747 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
ths3b46e622007-09-17 08:09:54 +00003748
bellard9d728e82004-09-05 23:09:03 +00003749 f = fopen(smb_conf, "w");
3750 if (!f) {
3751 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
3752 exit(1);
3753 }
ths5fafdf22007-09-16 21:08:06 +00003754 fprintf(f,
bellard9d728e82004-09-05 23:09:03 +00003755 "[global]\n"
bellard157777e2005-03-01 21:28:45 +00003756 "private dir=%s\n"
3757 "smb ports=0\n"
3758 "socket address=127.0.0.1\n"
bellard9d728e82004-09-05 23:09:03 +00003759 "pid directory=%s\n"
3760 "lock directory=%s\n"
3761 "log file=%s/log.smbd\n"
3762 "smb passwd file=%s/smbpasswd\n"
bellard03ffbb62004-09-06 00:14:04 +00003763 "security = share\n"
bellard9d728e82004-09-05 23:09:03 +00003764 "[qemu]\n"
3765 "path=%s\n"
3766 "read only=no\n"
3767 "guest ok=yes\n",
3768 smb_dir,
3769 smb_dir,
3770 smb_dir,
3771 smb_dir,
bellard157777e2005-03-01 21:28:45 +00003772 smb_dir,
bellard9d728e82004-09-05 23:09:03 +00003773 exported_dir
3774 );
3775 fclose(f);
3776 atexit(smb_exit);
3777
pbrooka14d6c82006-12-23 15:37:33 +00003778 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
3779 SMBD_COMMAND, smb_conf);
ths3b46e622007-09-17 08:09:54 +00003780
bellard9d728e82004-09-05 23:09:03 +00003781 slirp_add_exec(0, smb_cmdline, 4, 139);
3782}
bellard9bf05442004-08-25 22:12:49 +00003783
bellardc94c8d62004-09-13 21:37:34 +00003784#endif /* !defined(_WIN32) */
3785
bellardc20709a2004-04-21 23:27:19 +00003786#endif /* CONFIG_SLIRP */
3787
3788#if !defined(_WIN32)
bellard7c9d8e02005-11-15 22:16:05 +00003789
3790typedef struct TAPState {
3791 VLANClientState *vc;
3792 int fd;
3793} TAPState;
3794
3795static void tap_receive(void *opaque, const uint8_t *buf, int size)
3796{
3797 TAPState *s = opaque;
3798 int ret;
3799 for(;;) {
3800 ret = write(s->fd, buf, size);
3801 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
3802 } else {
3803 break;
3804 }
3805 }
3806}
3807
3808static void tap_send(void *opaque)
3809{
3810 TAPState *s = opaque;
3811 uint8_t buf[4096];
3812 int size;
3813
thsd5d10bc2007-02-17 22:54:49 +00003814#ifdef __sun__
3815 struct strbuf sbuf;
3816 int f = 0;
3817 sbuf.maxlen = sizeof(buf);
3818 sbuf.buf = buf;
3819 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
3820#else
bellard7c9d8e02005-11-15 22:16:05 +00003821 size = read(s->fd, buf, sizeof(buf));
thsd5d10bc2007-02-17 22:54:49 +00003822#endif
bellard7c9d8e02005-11-15 22:16:05 +00003823 if (size > 0) {
3824 qemu_send_packet(s->vc, buf, size);
3825 }
3826}
3827
3828/* fd support */
3829
3830static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
3831{
3832 TAPState *s;
3833
3834 s = qemu_mallocz(sizeof(TAPState));
3835 if (!s)
3836 return NULL;
3837 s->fd = fd;
pbrookd861b052006-02-04 22:15:28 +00003838 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00003839 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
3840 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
3841 return s;
3842}
3843
ths5c40d2b2007-06-23 16:03:36 +00003844#if defined (_BSD) || defined (__FreeBSD_kernel__)
bellard7c9d8e02005-11-15 22:16:05 +00003845static int tap_open(char *ifname, int ifname_size)
bellard7d3505c2004-05-12 19:32:15 +00003846{
3847 int fd;
3848 char *dev;
3849 struct stat s;
bellard67b915a2004-03-31 23:37:16 +00003850
balrogaeb30be2007-07-02 15:03:13 +00003851 TFR(fd = open("/dev/tap", O_RDWR));
bellard7d3505c2004-05-12 19:32:15 +00003852 if (fd < 0) {
3853 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
3854 return -1;
3855 }
3856
3857 fstat(fd, &s);
3858 dev = devname(s.st_rdev, S_IFCHR);
3859 pstrcpy(ifname, ifname_size, dev);
3860
3861 fcntl(fd, F_SETFL, O_NONBLOCK);
3862 return fd;
3863}
bellardec530c82006-04-25 22:36:06 +00003864#elif defined(__sun__)
thsd5d10bc2007-02-17 22:54:49 +00003865#define TUNNEWPPA (('T'<<16) | 0x0001)
ths5fafdf22007-09-16 21:08:06 +00003866/*
3867 * Allocate TAP device, returns opened fd.
thsd5d10bc2007-02-17 22:54:49 +00003868 * Stores dev name in the first arg(must be large enough).
ths3b46e622007-09-17 08:09:54 +00003869 */
thsd5d10bc2007-02-17 22:54:49 +00003870int tap_alloc(char *dev)
3871{
3872 int tap_fd, if_fd, ppa = -1;
3873 static int ip_fd = 0;
3874 char *ptr;
3875
3876 static int arp_fd = 0;
3877 int ip_muxid, arp_muxid;
3878 struct strioctl strioc_if, strioc_ppa;
3879 int link_type = I_PLINK;;
3880 struct lifreq ifr;
3881 char actual_name[32] = "";
3882
3883 memset(&ifr, 0x0, sizeof(ifr));
3884
3885 if( *dev ){
ths5fafdf22007-09-16 21:08:06 +00003886 ptr = dev;
3887 while( *ptr && !isdigit((int)*ptr) ) ptr++;
thsd5d10bc2007-02-17 22:54:49 +00003888 ppa = atoi(ptr);
3889 }
3890
3891 /* Check if IP device was opened */
3892 if( ip_fd )
3893 close(ip_fd);
3894
balrogaeb30be2007-07-02 15:03:13 +00003895 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
3896 if (ip_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003897 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
3898 return -1;
3899 }
3900
balrogaeb30be2007-07-02 15:03:13 +00003901 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
3902 if (tap_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003903 syslog(LOG_ERR, "Can't open /dev/tap");
3904 return -1;
3905 }
3906
3907 /* Assign a new PPA and get its unit number. */
3908 strioc_ppa.ic_cmd = TUNNEWPPA;
3909 strioc_ppa.ic_timout = 0;
3910 strioc_ppa.ic_len = sizeof(ppa);
3911 strioc_ppa.ic_dp = (char *)&ppa;
3912 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
3913 syslog (LOG_ERR, "Can't assign new interface");
3914
balrogaeb30be2007-07-02 15:03:13 +00003915 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
3916 if (if_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003917 syslog(LOG_ERR, "Can't open /dev/tap (2)");
3918 return -1;
3919 }
3920 if(ioctl(if_fd, I_PUSH, "ip") < 0){
3921 syslog(LOG_ERR, "Can't push IP module");
3922 return -1;
3923 }
3924
3925 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
3926 syslog(LOG_ERR, "Can't get flags\n");
3927
3928 snprintf (actual_name, 32, "tap%d", ppa);
3929 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3930
3931 ifr.lifr_ppa = ppa;
3932 /* Assign ppa according to the unit number returned by tun device */
3933
3934 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
3935 syslog (LOG_ERR, "Can't set PPA %d", ppa);
3936 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
3937 syslog (LOG_ERR, "Can't get flags\n");
3938 /* Push arp module to if_fd */
3939 if (ioctl (if_fd, I_PUSH, "arp") < 0)
3940 syslog (LOG_ERR, "Can't push ARP module (2)");
3941
3942 /* Push arp module to ip_fd */
3943 if (ioctl (ip_fd, I_POP, NULL) < 0)
3944 syslog (LOG_ERR, "I_POP failed\n");
3945 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
3946 syslog (LOG_ERR, "Can't push ARP module (3)\n");
3947 /* Open arp_fd */
balrogaeb30be2007-07-02 15:03:13 +00003948 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
3949 if (arp_fd < 0)
thsd5d10bc2007-02-17 22:54:49 +00003950 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
3951
3952 /* Set ifname to arp */
3953 strioc_if.ic_cmd = SIOCSLIFNAME;
3954 strioc_if.ic_timout = 0;
3955 strioc_if.ic_len = sizeof(ifr);
3956 strioc_if.ic_dp = (char *)&ifr;
3957 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
3958 syslog (LOG_ERR, "Can't set ifname to arp\n");
3959 }
3960
3961 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
3962 syslog(LOG_ERR, "Can't link TAP device to IP");
3963 return -1;
3964 }
3965
3966 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
3967 syslog (LOG_ERR, "Can't link TAP device to ARP");
3968
3969 close (if_fd);
3970
3971 memset(&ifr, 0x0, sizeof(ifr));
3972 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3973 ifr.lifr_ip_muxid = ip_muxid;
3974 ifr.lifr_arp_muxid = arp_muxid;
3975
3976 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
3977 {
3978 ioctl (ip_fd, I_PUNLINK , arp_muxid);
3979 ioctl (ip_fd, I_PUNLINK, ip_muxid);
3980 syslog (LOG_ERR, "Can't set multiplexor id");
3981 }
3982
3983 sprintf(dev, "tap%d", ppa);
3984 return tap_fd;
3985}
3986
bellardec530c82006-04-25 22:36:06 +00003987static int tap_open(char *ifname, int ifname_size)
3988{
thsd5d10bc2007-02-17 22:54:49 +00003989 char dev[10]="";
3990 int fd;
3991 if( (fd = tap_alloc(dev)) < 0 ){
3992 fprintf(stderr, "Cannot allocate TAP device\n");
3993 return -1;
3994 }
3995 pstrcpy(ifname, ifname_size, dev);
3996 fcntl(fd, F_SETFL, O_NONBLOCK);
3997 return fd;
bellardec530c82006-04-25 22:36:06 +00003998}
bellard7d3505c2004-05-12 19:32:15 +00003999#else
bellard7c9d8e02005-11-15 22:16:05 +00004000static int tap_open(char *ifname, int ifname_size)
bellardf1510b22003-06-25 00:07:40 +00004001{
4002 struct ifreq ifr;
bellardc4b1fcc2004-03-14 21:44:30 +00004003 int fd, ret;
ths3b46e622007-09-17 08:09:54 +00004004
balrogaeb30be2007-07-02 15:03:13 +00004005 TFR(fd = open("/dev/net/tun", O_RDWR));
bellardf1510b22003-06-25 00:07:40 +00004006 if (fd < 0) {
4007 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4008 return -1;
4009 }
4010 memset(&ifr, 0, sizeof(ifr));
4011 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
bellard7c9d8e02005-11-15 22:16:05 +00004012 if (ifname[0] != '\0')
4013 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4014 else
4015 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
bellardf1510b22003-06-25 00:07:40 +00004016 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4017 if (ret != 0) {
4018 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4019 close(fd);
4020 return -1;
4021 }
bellardc4b1fcc2004-03-14 21:44:30 +00004022 pstrcpy(ifname, ifname_size, ifr.ifr_name);
bellardf1510b22003-06-25 00:07:40 +00004023 fcntl(fd, F_SETFL, O_NONBLOCK);
bellardc4b1fcc2004-03-14 21:44:30 +00004024 return fd;
4025}
bellard7d3505c2004-05-12 19:32:15 +00004026#endif
bellardf1510b22003-06-25 00:07:40 +00004027
bellard7c9d8e02005-11-15 22:16:05 +00004028static int net_tap_init(VLANState *vlan, const char *ifname1,
4029 const char *setup_script)
bellardc4b1fcc2004-03-14 21:44:30 +00004030{
bellard7c9d8e02005-11-15 22:16:05 +00004031 TAPState *s;
4032 int pid, status, fd;
bellardc20709a2004-04-21 23:27:19 +00004033 char *args[3];
4034 char **parg;
bellard7c9d8e02005-11-15 22:16:05 +00004035 char ifname[128];
bellardc20709a2004-04-21 23:27:19 +00004036
bellard7c9d8e02005-11-15 22:16:05 +00004037 if (ifname1 != NULL)
4038 pstrcpy(ifname, sizeof(ifname), ifname1);
4039 else
4040 ifname[0] = '\0';
balrogaeb30be2007-07-02 15:03:13 +00004041 TFR(fd = tap_open(ifname, sizeof(ifname)));
bellard7c9d8e02005-11-15 22:16:05 +00004042 if (fd < 0)
bellardc20709a2004-04-21 23:27:19 +00004043 return -1;
4044
ths6a1cbf62007-02-02 00:37:56 +00004045 if (!setup_script || !strcmp(setup_script, "no"))
bellard7c9d8e02005-11-15 22:16:05 +00004046 setup_script = "";
4047 if (setup_script[0] != '\0') {
4048 /* try to launch network init script */
4049 pid = fork();
4050 if (pid >= 0) {
4051 if (pid == 0) {
ths50d3eea2007-03-19 16:36:43 +00004052 int open_max = sysconf (_SC_OPEN_MAX), i;
4053 for (i = 0; i < open_max; i++)
4054 if (i != STDIN_FILENO &&
4055 i != STDOUT_FILENO &&
4056 i != STDERR_FILENO &&
4057 i != fd)
4058 close(i);
4059
bellard7c9d8e02005-11-15 22:16:05 +00004060 parg = args;
4061 *parg++ = (char *)setup_script;
4062 *parg++ = ifname;
4063 *parg++ = NULL;
4064 execv(setup_script, args);
bellard4a389402005-11-26 20:10:07 +00004065 _exit(1);
bellard7c9d8e02005-11-15 22:16:05 +00004066 }
4067 while (waitpid(pid, &status, 0) != pid);
4068 if (!WIFEXITED(status) ||
4069 WEXITSTATUS(status) != 0) {
4070 fprintf(stderr, "%s: could not launch network script\n",
4071 setup_script);
4072 return -1;
4073 }
bellardc20709a2004-04-21 23:27:19 +00004074 }
4075 }
bellard7c9d8e02005-11-15 22:16:05 +00004076 s = net_tap_fd_init(vlan, fd);
4077 if (!s)
4078 return -1;
ths5fafdf22007-09-16 21:08:06 +00004079 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
bellard7c9d8e02005-11-15 22:16:05 +00004080 "tap: ifname=%s setup_script=%s", ifname, setup_script);
bellardc20709a2004-04-21 23:27:19 +00004081 return 0;
4082}
4083
bellardfd1dff42006-02-01 21:29:26 +00004084#endif /* !_WIN32 */
4085
bellard7c9d8e02005-11-15 22:16:05 +00004086/* network connection */
4087typedef struct NetSocketState {
4088 VLANClientState *vc;
4089 int fd;
4090 int state; /* 0 = getting length, 1 = getting data */
4091 int index;
4092 int packet_len;
4093 uint8_t buf[4096];
bellard3d830452005-12-18 16:36:49 +00004094 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
bellard7c9d8e02005-11-15 22:16:05 +00004095} NetSocketState;
4096
4097typedef struct NetSocketListenState {
4098 VLANState *vlan;
4099 int fd;
4100} NetSocketListenState;
4101
4102/* XXX: we consider we can send the whole packet without blocking */
4103static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
bellardc20709a2004-04-21 23:27:19 +00004104{
bellard7c9d8e02005-11-15 22:16:05 +00004105 NetSocketState *s = opaque;
4106 uint32_t len;
4107 len = htonl(size);
4108
bellardfd1dff42006-02-01 21:29:26 +00004109 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4110 send_all(s->fd, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00004111}
4112
bellard3d830452005-12-18 16:36:49 +00004113static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4114{
4115 NetSocketState *s = opaque;
ths5fafdf22007-09-16 21:08:06 +00004116 sendto(s->fd, buf, size, 0,
bellard3d830452005-12-18 16:36:49 +00004117 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4118}
4119
bellard7c9d8e02005-11-15 22:16:05 +00004120static void net_socket_send(void *opaque)
4121{
4122 NetSocketState *s = opaque;
bellardfd1dff42006-02-01 21:29:26 +00004123 int l, size, err;
bellard7c9d8e02005-11-15 22:16:05 +00004124 uint8_t buf1[4096];
4125 const uint8_t *buf;
4126
bellardfd1dff42006-02-01 21:29:26 +00004127 size = recv(s->fd, buf1, sizeof(buf1), 0);
4128 if (size < 0) {
4129 err = socket_error();
ths5fafdf22007-09-16 21:08:06 +00004130 if (err != EWOULDBLOCK)
bellardfd1dff42006-02-01 21:29:26 +00004131 goto eoc;
4132 } else if (size == 0) {
bellard7c9d8e02005-11-15 22:16:05 +00004133 /* end of connection */
bellardfd1dff42006-02-01 21:29:26 +00004134 eoc:
bellard7c9d8e02005-11-15 22:16:05 +00004135 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
bellardfd1dff42006-02-01 21:29:26 +00004136 closesocket(s->fd);
bellard7c9d8e02005-11-15 22:16:05 +00004137 return;
4138 }
4139 buf = buf1;
4140 while (size > 0) {
4141 /* reassemble a packet from the network */
4142 switch(s->state) {
4143 case 0:
4144 l = 4 - s->index;
4145 if (l > size)
4146 l = size;
4147 memcpy(s->buf + s->index, buf, l);
4148 buf += l;
4149 size -= l;
4150 s->index += l;
4151 if (s->index == 4) {
4152 /* got length */
4153 s->packet_len = ntohl(*(uint32_t *)s->buf);
4154 s->index = 0;
4155 s->state = 1;
4156 }
4157 break;
4158 case 1:
4159 l = s->packet_len - s->index;
4160 if (l > size)
4161 l = size;
4162 memcpy(s->buf + s->index, buf, l);
4163 s->index += l;
4164 buf += l;
4165 size -= l;
4166 if (s->index >= s->packet_len) {
4167 qemu_send_packet(s->vc, s->buf, s->packet_len);
4168 s->index = 0;
4169 s->state = 0;
4170 }
4171 break;
4172 }
4173 }
4174}
4175
bellard3d830452005-12-18 16:36:49 +00004176static void net_socket_send_dgram(void *opaque)
4177{
4178 NetSocketState *s = opaque;
4179 int size;
4180
4181 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
ths5fafdf22007-09-16 21:08:06 +00004182 if (size < 0)
bellard3d830452005-12-18 16:36:49 +00004183 return;
4184 if (size == 0) {
4185 /* end of connection */
4186 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4187 return;
4188 }
4189 qemu_send_packet(s->vc, s->buf, size);
4190}
4191
4192static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4193{
4194 struct ip_mreq imr;
4195 int fd;
4196 int val, ret;
4197 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4198 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
ths5fafdf22007-09-16 21:08:06 +00004199 inet_ntoa(mcastaddr->sin_addr),
bellardfd1dff42006-02-01 21:29:26 +00004200 (int)ntohl(mcastaddr->sin_addr.s_addr));
bellard3d830452005-12-18 16:36:49 +00004201 return -1;
4202
4203 }
4204 fd = socket(PF_INET, SOCK_DGRAM, 0);
4205 if (fd < 0) {
4206 perror("socket(PF_INET, SOCK_DGRAM)");
4207 return -1;
4208 }
4209
bellardfd1dff42006-02-01 21:29:26 +00004210 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004211 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
bellardfd1dff42006-02-01 21:29:26 +00004212 (const char *)&val, sizeof(val));
4213 if (ret < 0) {
4214 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4215 goto fail;
4216 }
4217
4218 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4219 if (ret < 0) {
4220 perror("bind");
4221 goto fail;
4222 }
ths3b46e622007-09-17 08:09:54 +00004223
bellard3d830452005-12-18 16:36:49 +00004224 /* Add host to multicast group */
4225 imr.imr_multiaddr = mcastaddr->sin_addr;
4226 imr.imr_interface.s_addr = htonl(INADDR_ANY);
4227
ths5fafdf22007-09-16 21:08:06 +00004228 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
bellardfd1dff42006-02-01 21:29:26 +00004229 (const char *)&imr, sizeof(struct ip_mreq));
bellard3d830452005-12-18 16:36:49 +00004230 if (ret < 0) {
4231 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4232 goto fail;
4233 }
4234
4235 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4236 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004237 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
bellardfd1dff42006-02-01 21:29:26 +00004238 (const char *)&val, sizeof(val));
bellard3d830452005-12-18 16:36:49 +00004239 if (ret < 0) {
4240 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4241 goto fail;
4242 }
4243
bellardfd1dff42006-02-01 21:29:26 +00004244 socket_set_nonblock(fd);
bellard3d830452005-12-18 16:36:49 +00004245 return fd;
4246fail:
ths5fafdf22007-09-16 21:08:06 +00004247 if (fd >= 0)
bellard0bab00f2006-06-25 14:49:44 +00004248 closesocket(fd);
bellard3d830452005-12-18 16:36:49 +00004249 return -1;
4250}
4251
ths5fafdf22007-09-16 21:08:06 +00004252static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004253 int is_connected)
4254{
4255 struct sockaddr_in saddr;
4256 int newfd;
4257 socklen_t saddr_len;
4258 NetSocketState *s;
4259
4260 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
ths5fafdf22007-09-16 21:08:06 +00004261 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
bellard3d830452005-12-18 16:36:49 +00004262 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4263 */
4264
4265 if (is_connected) {
4266 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4267 /* must be bound */
4268 if (saddr.sin_addr.s_addr==0) {
4269 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4270 fd);
4271 return NULL;
4272 }
4273 /* clone dgram socket */
4274 newfd = net_socket_mcast_create(&saddr);
4275 if (newfd < 0) {
4276 /* error already reported by net_socket_mcast_create() */
4277 close(fd);
4278 return NULL;
4279 }
4280 /* clone newfd to fd, close newfd */
4281 dup2(newfd, fd);
4282 close(newfd);
ths5fafdf22007-09-16 21:08:06 +00004283
bellard3d830452005-12-18 16:36:49 +00004284 } else {
4285 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4286 fd, strerror(errno));
4287 return NULL;
4288 }
4289 }
4290
4291 s = qemu_mallocz(sizeof(NetSocketState));
4292 if (!s)
4293 return NULL;
4294 s->fd = fd;
4295
pbrookd861b052006-02-04 22:15:28 +00004296 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
bellard3d830452005-12-18 16:36:49 +00004297 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4298
4299 /* mcast: save bound address as dst */
4300 if (is_connected) s->dgram_dst=saddr;
4301
4302 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004303 "socket: fd=%d (%s mcast=%s:%d)",
bellard3d830452005-12-18 16:36:49 +00004304 fd, is_connected? "cloned" : "",
4305 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4306 return s;
4307}
4308
bellard7c9d8e02005-11-15 22:16:05 +00004309static void net_socket_connect(void *opaque)
4310{
4311 NetSocketState *s = opaque;
4312 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4313}
4314
ths5fafdf22007-09-16 21:08:06 +00004315static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
bellard7c9d8e02005-11-15 22:16:05 +00004316 int is_connected)
4317{
4318 NetSocketState *s;
4319 s = qemu_mallocz(sizeof(NetSocketState));
4320 if (!s)
4321 return NULL;
4322 s->fd = fd;
ths5fafdf22007-09-16 21:08:06 +00004323 s->vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00004324 net_socket_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00004325 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4326 "socket: fd=%d", fd);
4327 if (is_connected) {
4328 net_socket_connect(s);
4329 } else {
4330 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4331 }
4332 return s;
4333}
4334
ths5fafdf22007-09-16 21:08:06 +00004335static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004336 int is_connected)
4337{
4338 int so_type=-1, optlen=sizeof(so_type);
4339
bellardfd1dff42006-02-01 21:29:26 +00004340 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) {
ths931f03e2007-04-28 20:49:36 +00004341 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
bellard3d830452005-12-18 16:36:49 +00004342 return NULL;
4343 }
4344 switch(so_type) {
4345 case SOCK_DGRAM:
4346 return net_socket_fd_init_dgram(vlan, fd, is_connected);
4347 case SOCK_STREAM:
4348 return net_socket_fd_init_stream(vlan, fd, is_connected);
4349 default:
4350 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4351 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4352 return net_socket_fd_init_stream(vlan, fd, is_connected);
4353 }
4354 return NULL;
4355}
4356
bellard7c9d8e02005-11-15 22:16:05 +00004357static void net_socket_accept(void *opaque)
4358{
ths3b46e622007-09-17 08:09:54 +00004359 NetSocketListenState *s = opaque;
bellard7c9d8e02005-11-15 22:16:05 +00004360 NetSocketState *s1;
4361 struct sockaddr_in saddr;
4362 socklen_t len;
4363 int fd;
4364
4365 for(;;) {
4366 len = sizeof(saddr);
4367 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4368 if (fd < 0 && errno != EINTR) {
4369 return;
4370 } else if (fd >= 0) {
4371 break;
4372 }
4373 }
ths5fafdf22007-09-16 21:08:06 +00004374 s1 = net_socket_fd_init(s->vlan, fd, 1);
bellard7c9d8e02005-11-15 22:16:05 +00004375 if (!s1) {
bellard0bab00f2006-06-25 14:49:44 +00004376 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004377 } else {
4378 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004379 "socket: connection from %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004380 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4381 }
4382}
4383
4384static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4385{
4386 NetSocketListenState *s;
4387 int fd, val, ret;
4388 struct sockaddr_in saddr;
4389
4390 if (parse_host_port(&saddr, host_str) < 0)
4391 return -1;
ths3b46e622007-09-17 08:09:54 +00004392
bellard7c9d8e02005-11-15 22:16:05 +00004393 s = qemu_mallocz(sizeof(NetSocketListenState));
4394 if (!s)
4395 return -1;
4396
4397 fd = socket(PF_INET, SOCK_STREAM, 0);
4398 if (fd < 0) {
4399 perror("socket");
4400 return -1;
4401 }
bellardfd1dff42006-02-01 21:29:26 +00004402 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004403
4404 /* allow fast reuse */
4405 val = 1;
bellardfd1dff42006-02-01 21:29:26 +00004406 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00004407
bellard7c9d8e02005-11-15 22:16:05 +00004408 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4409 if (ret < 0) {
4410 perror("bind");
4411 return -1;
4412 }
4413 ret = listen(fd, 0);
4414 if (ret < 0) {
4415 perror("listen");
4416 return -1;
4417 }
4418 s->vlan = vlan;
4419 s->fd = fd;
4420 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4421 return 0;
4422}
4423
4424static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4425{
4426 NetSocketState *s;
bellardfd1dff42006-02-01 21:29:26 +00004427 int fd, connected, ret, err;
bellard7c9d8e02005-11-15 22:16:05 +00004428 struct sockaddr_in saddr;
4429
4430 if (parse_host_port(&saddr, host_str) < 0)
4431 return -1;
4432
4433 fd = socket(PF_INET, SOCK_STREAM, 0);
4434 if (fd < 0) {
4435 perror("socket");
4436 return -1;
4437 }
bellardfd1dff42006-02-01 21:29:26 +00004438 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004439
4440 connected = 0;
4441 for(;;) {
4442 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4443 if (ret < 0) {
bellardfd1dff42006-02-01 21:29:26 +00004444 err = socket_error();
4445 if (err == EINTR || err == EWOULDBLOCK) {
4446 } else if (err == EINPROGRESS) {
bellard7c9d8e02005-11-15 22:16:05 +00004447 break;
thsf5b12262007-03-25 15:58:03 +00004448#ifdef _WIN32
4449 } else if (err == WSAEALREADY) {
4450 break;
4451#endif
bellard7c9d8e02005-11-15 22:16:05 +00004452 } else {
4453 perror("connect");
bellardfd1dff42006-02-01 21:29:26 +00004454 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004455 return -1;
4456 }
4457 } else {
4458 connected = 1;
4459 break;
4460 }
4461 }
4462 s = net_socket_fd_init(vlan, fd, connected);
4463 if (!s)
4464 return -1;
4465 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004466 "socket: connect to %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004467 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
bellardc20709a2004-04-21 23:27:19 +00004468 return 0;
4469}
4470
bellard3d830452005-12-18 16:36:49 +00004471static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4472{
4473 NetSocketState *s;
4474 int fd;
4475 struct sockaddr_in saddr;
4476
4477 if (parse_host_port(&saddr, host_str) < 0)
4478 return -1;
4479
4480
4481 fd = net_socket_mcast_create(&saddr);
4482 if (fd < 0)
4483 return -1;
4484
4485 s = net_socket_fd_init(vlan, fd, 0);
4486 if (!s)
4487 return -1;
4488
4489 s->dgram_dst = saddr;
ths3b46e622007-09-17 08:09:54 +00004490
bellard3d830452005-12-18 16:36:49 +00004491 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004492 "socket: mcast=%s:%d",
bellard3d830452005-12-18 16:36:49 +00004493 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4494 return 0;
4495
4496}
4497
bellard7c9d8e02005-11-15 22:16:05 +00004498static int get_param_value(char *buf, int buf_size,
4499 const char *tag, const char *str)
4500{
4501 const char *p;
4502 char *q;
4503 char option[128];
4504
4505 p = str;
4506 for(;;) {
4507 q = option;
4508 while (*p != '\0' && *p != '=') {
4509 if ((q - option) < sizeof(option) - 1)
4510 *q++ = *p;
4511 p++;
4512 }
4513 *q = '\0';
4514 if (*p != '=')
4515 break;
4516 p++;
4517 if (!strcmp(tag, option)) {
4518 q = buf;
4519 while (*p != '\0' && *p != ',') {
4520 if ((q - buf) < buf_size - 1)
4521 *q++ = *p;
4522 p++;
4523 }
4524 *q = '\0';
4525 return q - buf;
4526 } else {
4527 while (*p != '\0' && *p != ',') {
4528 p++;
4529 }
4530 }
4531 if (*p != ',')
4532 break;
4533 p++;
4534 }
4535 return 0;
4536}
4537
ths52f61fd2006-12-22 21:20:52 +00004538static int net_client_init(const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00004539{
4540 const char *p;
4541 char *q;
4542 char device[64];
4543 char buf[1024];
4544 int vlan_id, ret;
4545 VLANState *vlan;
4546
4547 p = str;
4548 q = device;
4549 while (*p != '\0' && *p != ',') {
4550 if ((q - device) < sizeof(device) - 1)
4551 *q++ = *p;
4552 p++;
4553 }
4554 *q = '\0';
4555 if (*p == ',')
4556 p++;
4557 vlan_id = 0;
4558 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
4559 vlan_id = strtol(buf, NULL, 0);
4560 }
4561 vlan = qemu_find_vlan(vlan_id);
4562 if (!vlan) {
4563 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
4564 return -1;
4565 }
4566 if (!strcmp(device, "nic")) {
4567 NICInfo *nd;
4568 uint8_t *macaddr;
4569
4570 if (nb_nics >= MAX_NICS) {
4571 fprintf(stderr, "Too Many NICs\n");
4572 return -1;
4573 }
4574 nd = &nd_table[nb_nics];
4575 macaddr = nd->macaddr;
4576 macaddr[0] = 0x52;
4577 macaddr[1] = 0x54;
4578 macaddr[2] = 0x00;
4579 macaddr[3] = 0x12;
4580 macaddr[4] = 0x34;
4581 macaddr[5] = 0x56 + nb_nics;
4582
4583 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
4584 if (parse_macaddr(macaddr, buf) < 0) {
4585 fprintf(stderr, "invalid syntax for ethernet address\n");
4586 return -1;
4587 }
4588 }
pbrooka41b2ff2006-02-05 04:14:41 +00004589 if (get_param_value(buf, sizeof(buf), "model", p)) {
4590 nd->model = strdup(buf);
4591 }
bellard7c9d8e02005-11-15 22:16:05 +00004592 nd->vlan = vlan;
4593 nb_nics++;
blueswir1833c7172007-05-27 19:36:43 +00004594 vlan->nb_guest_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004595 ret = 0;
4596 } else
4597 if (!strcmp(device, "none")) {
4598 /* does nothing. It is needed to signal that no network cards
4599 are wanted */
4600 ret = 0;
4601 } else
4602#ifdef CONFIG_SLIRP
4603 if (!strcmp(device, "user")) {
pbrook115defd2006-04-16 11:06:58 +00004604 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
bellard3f423c92006-04-30 21:34:15 +00004605 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
pbrook115defd2006-04-16 11:06:58 +00004606 }
blueswir1833c7172007-05-27 19:36:43 +00004607 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004608 ret = net_slirp_init(vlan);
4609 } else
4610#endif
bellard7fb843f2006-02-01 23:06:55 +00004611#ifdef _WIN32
4612 if (!strcmp(device, "tap")) {
4613 char ifname[64];
4614 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4615 fprintf(stderr, "tap: no interface name\n");
4616 return -1;
4617 }
blueswir1833c7172007-05-27 19:36:43 +00004618 vlan->nb_host_devs++;
bellard7fb843f2006-02-01 23:06:55 +00004619 ret = tap_win32_init(vlan, ifname);
4620 } else
4621#else
bellard7c9d8e02005-11-15 22:16:05 +00004622 if (!strcmp(device, "tap")) {
4623 char ifname[64];
4624 char setup_script[1024];
4625 int fd;
pbrook4f010352007-05-28 02:29:59 +00004626 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004627 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4628 fd = strtol(buf, NULL, 0);
4629 ret = -1;
4630 if (net_tap_fd_init(vlan, fd))
4631 ret = 0;
4632 } else {
bellardbf8c5342007-01-09 19:44:41 +00004633 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4634 ifname[0] = '\0';
4635 }
bellard7c9d8e02005-11-15 22:16:05 +00004636 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
4637 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
4638 }
4639 ret = net_tap_init(vlan, ifname, setup_script);
4640 }
4641 } else
bellardfd1dff42006-02-01 21:29:26 +00004642#endif
bellard7c9d8e02005-11-15 22:16:05 +00004643 if (!strcmp(device, "socket")) {
4644 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4645 int fd;
4646 fd = strtol(buf, NULL, 0);
4647 ret = -1;
4648 if (net_socket_fd_init(vlan, fd, 1))
4649 ret = 0;
4650 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
4651 ret = net_socket_listen_init(vlan, buf);
4652 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
4653 ret = net_socket_connect_init(vlan, buf);
bellard3d830452005-12-18 16:36:49 +00004654 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
4655 ret = net_socket_mcast_init(vlan, buf);
bellard7c9d8e02005-11-15 22:16:05 +00004656 } else {
4657 fprintf(stderr, "Unknown socket options: %s\n", p);
4658 return -1;
4659 }
blueswir1833c7172007-05-27 19:36:43 +00004660 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004661 } else
bellard7c9d8e02005-11-15 22:16:05 +00004662 {
4663 fprintf(stderr, "Unknown network device: %s\n", device);
4664 return -1;
4665 }
4666 if (ret < 0) {
4667 fprintf(stderr, "Could not initialize device '%s'\n", device);
4668 }
ths3b46e622007-09-17 08:09:54 +00004669
bellard7c9d8e02005-11-15 22:16:05 +00004670 return ret;
4671}
4672
4673void do_info_network(void)
4674{
4675 VLANState *vlan;
4676 VLANClientState *vc;
4677
4678 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
4679 term_printf("VLAN %d devices:\n", vlan->id);
4680 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
4681 term_printf(" %s\n", vc->info_str);
4682 }
4683}
ths42550fd2006-12-22 16:34:12 +00004684
bellard330d0412003-07-26 18:11:40 +00004685/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00004686/* USB devices */
4687
pbrook0d92ed32006-05-21 16:30:15 +00004688static USBPort *used_usb_ports;
4689static USBPort *free_usb_ports;
4690
4691/* ??? Maybe change this to register a hub to keep track of the topology. */
4692void qemu_register_usb_port(USBPort *port, void *opaque, int index,
4693 usb_attachfn attach)
4694{
4695 port->opaque = opaque;
4696 port->index = index;
4697 port->attach = attach;
4698 port->next = free_usb_ports;
4699 free_usb_ports = port;
4700}
4701
bellarda594cfb2005-11-06 16:13:29 +00004702static int usb_device_add(const char *devname)
4703{
4704 const char *p;
4705 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004706 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00004707
pbrook0d92ed32006-05-21 16:30:15 +00004708 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00004709 return -1;
4710
4711 if (strstart(devname, "host:", &p)) {
4712 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00004713 } else if (!strcmp(devname, "mouse")) {
4714 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00004715 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00004716 dev = usb_tablet_init();
4717 } else if (!strcmp(devname, "keyboard")) {
4718 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00004719 } else if (strstart(devname, "disk:", &p)) {
4720 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00004721 } else if (!strcmp(devname, "wacom-tablet")) {
4722 dev = usb_wacom_init();
bellarda594cfb2005-11-06 16:13:29 +00004723 } else {
4724 return -1;
4725 }
pbrook0d92ed32006-05-21 16:30:15 +00004726 if (!dev)
4727 return -1;
4728
4729 /* Find a USB port to add the device to. */
4730 port = free_usb_ports;
4731 if (!port->next) {
4732 USBDevice *hub;
4733
4734 /* Create a new hub and chain it on. */
4735 free_usb_ports = NULL;
4736 port->next = used_usb_ports;
4737 used_usb_ports = port;
4738
4739 hub = usb_hub_init(VM_USB_HUB_SIZE);
4740 usb_attach(port, hub);
4741 port = free_usb_ports;
4742 }
4743
4744 free_usb_ports = port->next;
4745 port->next = used_usb_ports;
4746 used_usb_ports = port;
4747 usb_attach(port, dev);
bellarda594cfb2005-11-06 16:13:29 +00004748 return 0;
4749}
4750
4751static int usb_device_del(const char *devname)
4752{
pbrook0d92ed32006-05-21 16:30:15 +00004753 USBPort *port;
4754 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00004755 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004756 int bus_num, addr;
bellarda594cfb2005-11-06 16:13:29 +00004757 const char *p;
4758
pbrook0d92ed32006-05-21 16:30:15 +00004759 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00004760 return -1;
4761
4762 p = strchr(devname, '.');
ths5fafdf22007-09-16 21:08:06 +00004763 if (!p)
bellarda594cfb2005-11-06 16:13:29 +00004764 return -1;
4765 bus_num = strtoul(devname, NULL, 0);
4766 addr = strtoul(p + 1, NULL, 0);
4767 if (bus_num != 0)
4768 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00004769
4770 lastp = &used_usb_ports;
4771 port = used_usb_ports;
4772 while (port && port->dev->addr != addr) {
4773 lastp = &port->next;
4774 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00004775 }
pbrook0d92ed32006-05-21 16:30:15 +00004776
4777 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00004778 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00004779
bellard059809e2006-07-19 18:06:15 +00004780 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00004781 *lastp = port->next;
4782 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00004783 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00004784 port->next = free_usb_ports;
4785 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00004786 return 0;
4787}
4788
4789void do_usb_add(const char *devname)
4790{
4791 int ret;
4792 ret = usb_device_add(devname);
ths5fafdf22007-09-16 21:08:06 +00004793 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00004794 term_printf("Could not add USB device '%s'\n", devname);
4795}
4796
4797void do_usb_del(const char *devname)
4798{
4799 int ret;
4800 ret = usb_device_del(devname);
ths5fafdf22007-09-16 21:08:06 +00004801 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00004802 term_printf("Could not remove USB device '%s'\n", devname);
4803}
4804
4805void usb_info(void)
4806{
4807 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00004808 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00004809 const char *speed_str;
4810
pbrook0d92ed32006-05-21 16:30:15 +00004811 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00004812 term_printf("USB support not enabled\n");
4813 return;
4814 }
4815
pbrook0d92ed32006-05-21 16:30:15 +00004816 for (port = used_usb_ports; port; port = port->next) {
4817 dev = port->dev;
4818 if (!dev)
4819 continue;
4820 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00004821 case USB_SPEED_LOW:
4822 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00004823 break;
ths5fafdf22007-09-16 21:08:06 +00004824 case USB_SPEED_FULL:
4825 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00004826 break;
ths5fafdf22007-09-16 21:08:06 +00004827 case USB_SPEED_HIGH:
4828 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00004829 break;
4830 default:
ths5fafdf22007-09-16 21:08:06 +00004831 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00004832 break;
bellarda594cfb2005-11-06 16:13:29 +00004833 }
ths5fafdf22007-09-16 21:08:06 +00004834 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00004835 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00004836 }
4837}
4838
bellardf7cce892004-12-08 22:21:25 +00004839/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00004840/* PCMCIA/Cardbus */
4841
4842static struct pcmcia_socket_entry_s {
4843 struct pcmcia_socket_s *socket;
4844 struct pcmcia_socket_entry_s *next;
4845} *pcmcia_sockets = 0;
4846
4847void pcmcia_socket_register(struct pcmcia_socket_s *socket)
4848{
4849 struct pcmcia_socket_entry_s *entry;
4850
4851 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
4852 entry->socket = socket;
4853 entry->next = pcmcia_sockets;
4854 pcmcia_sockets = entry;
4855}
4856
4857void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
4858{
4859 struct pcmcia_socket_entry_s *entry, **ptr;
4860
4861 ptr = &pcmcia_sockets;
4862 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
4863 if (entry->socket == socket) {
4864 *ptr = entry->next;
4865 qemu_free(entry);
4866 }
4867}
4868
4869void pcmcia_info(void)
4870{
4871 struct pcmcia_socket_entry_s *iter;
4872 if (!pcmcia_sockets)
4873 term_printf("No PCMCIA sockets\n");
4874
4875 for (iter = pcmcia_sockets; iter; iter = iter->next)
4876 term_printf("%s: %s\n", iter->socket->slot_string,
4877 iter->socket->attached ? iter->socket->card_string :
4878 "Empty");
4879}
4880
4881/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00004882/* dumb display */
4883
4884static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
4885{
4886}
4887
4888static void dumb_resize(DisplayState *ds, int w, int h)
4889{
4890}
4891
4892static void dumb_refresh(DisplayState *ds)
4893{
4894#if defined(CONFIG_SDL)
4895 vga_hw_update();
4896#endif
4897}
4898
4899static void dumb_display_init(DisplayState *ds)
4900{
4901 ds->data = NULL;
4902 ds->linesize = 0;
4903 ds->depth = 0;
4904 ds->dpy_update = dumb_update;
4905 ds->dpy_resize = dumb_resize;
4906 ds->dpy_refresh = dumb_refresh;
4907}
4908
4909/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00004910/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00004911
bellardc4b1fcc2004-03-14 21:44:30 +00004912#define MAX_IO_HANDLERS 64
4913
4914typedef struct IOHandlerRecord {
4915 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00004916 IOCanRWHandler *fd_read_poll;
4917 IOHandler *fd_read;
4918 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00004919 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00004920 void *opaque;
4921 /* temporary data */
4922 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00004923 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00004924} IOHandlerRecord;
4925
bellard8a7ddc32004-03-31 19:00:16 +00004926static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00004927
bellard7c9d8e02005-11-15 22:16:05 +00004928/* XXX: fd_read_poll should be suppressed, but an API change is
4929 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00004930int qemu_set_fd_handler2(int fd,
4931 IOCanRWHandler *fd_read_poll,
4932 IOHandler *fd_read,
4933 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00004934 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00004935{
bellard8a7ddc32004-03-31 19:00:16 +00004936 IOHandlerRecord **pioh, *ioh;
4937
bellard7c9d8e02005-11-15 22:16:05 +00004938 if (!fd_read && !fd_write) {
4939 pioh = &first_io_handler;
4940 for(;;) {
4941 ioh = *pioh;
4942 if (ioh == NULL)
4943 break;
4944 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00004945 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00004946 break;
4947 }
4948 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00004949 }
bellard7c9d8e02005-11-15 22:16:05 +00004950 } else {
4951 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4952 if (ioh->fd == fd)
4953 goto found;
4954 }
4955 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
4956 if (!ioh)
4957 return -1;
4958 ioh->next = first_io_handler;
4959 first_io_handler = ioh;
4960 found:
4961 ioh->fd = fd;
4962 ioh->fd_read_poll = fd_read_poll;
4963 ioh->fd_read = fd_read;
4964 ioh->fd_write = fd_write;
4965 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00004966 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00004967 }
bellard7c9d8e02005-11-15 22:16:05 +00004968 return 0;
4969}
4970
ths5fafdf22007-09-16 21:08:06 +00004971int qemu_set_fd_handler(int fd,
4972 IOHandler *fd_read,
4973 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00004974 void *opaque)
4975{
4976 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00004977}
4978
bellard8a7ddc32004-03-31 19:00:16 +00004979/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00004980/* Polling handling */
4981
4982typedef struct PollingEntry {
4983 PollingFunc *func;
4984 void *opaque;
4985 struct PollingEntry *next;
4986} PollingEntry;
4987
4988static PollingEntry *first_polling_entry;
4989
4990int qemu_add_polling_cb(PollingFunc *func, void *opaque)
4991{
4992 PollingEntry **ppe, *pe;
4993 pe = qemu_mallocz(sizeof(PollingEntry));
4994 if (!pe)
4995 return -1;
4996 pe->func = func;
4997 pe->opaque = opaque;
4998 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
4999 *ppe = pe;
5000 return 0;
5001}
5002
5003void qemu_del_polling_cb(PollingFunc *func, void *opaque)
5004{
5005 PollingEntry **ppe, *pe;
5006 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
5007 pe = *ppe;
5008 if (pe->func == func && pe->opaque == opaque) {
5009 *ppe = pe->next;
5010 qemu_free(pe);
5011 break;
5012 }
5013 }
5014}
5015
bellarda18e5242006-06-25 17:18:27 +00005016#ifdef _WIN32
5017/***********************************************************/
5018/* Wait objects support */
5019typedef struct WaitObjects {
5020 int num;
5021 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
5022 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
5023 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
5024} WaitObjects;
5025
5026static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00005027
bellarda18e5242006-06-25 17:18:27 +00005028int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5029{
5030 WaitObjects *w = &wait_objects;
5031
5032 if (w->num >= MAXIMUM_WAIT_OBJECTS)
5033 return -1;
5034 w->events[w->num] = handle;
5035 w->func[w->num] = func;
5036 w->opaque[w->num] = opaque;
5037 w->num++;
5038 return 0;
5039}
5040
5041void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5042{
5043 int i, found;
5044 WaitObjects *w = &wait_objects;
5045
5046 found = 0;
5047 for (i = 0; i < w->num; i++) {
5048 if (w->events[i] == handle)
5049 found = 1;
5050 if (found) {
5051 w->events[i] = w->events[i + 1];
5052 w->func[i] = w->func[i + 1];
5053 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00005054 }
bellarda18e5242006-06-25 17:18:27 +00005055 }
5056 if (found)
5057 w->num--;
5058}
5059#endif
5060
bellardf3311102006-04-12 20:21:17 +00005061/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00005062/* savevm/loadvm support */
bellardb4608c02003-06-27 17:34:32 +00005063
bellardfaea38e2006-08-05 21:31:00 +00005064#define IO_BUF_SIZE 32768
5065
5066struct QEMUFile {
5067 FILE *outfile;
5068 BlockDriverState *bs;
5069 int is_file;
5070 int is_writable;
5071 int64_t base_offset;
5072 int64_t buf_offset; /* start of buffer when writing, end of buffer
5073 when reading */
5074 int buf_index;
5075 int buf_size; /* 0 when writing */
5076 uint8_t buf[IO_BUF_SIZE];
5077};
5078
5079QEMUFile *qemu_fopen(const char *filename, const char *mode)
5080{
5081 QEMUFile *f;
5082
5083 f = qemu_mallocz(sizeof(QEMUFile));
5084 if (!f)
5085 return NULL;
5086 if (!strcmp(mode, "wb")) {
5087 f->is_writable = 1;
5088 } else if (!strcmp(mode, "rb")) {
5089 f->is_writable = 0;
5090 } else {
5091 goto fail;
5092 }
5093 f->outfile = fopen(filename, mode);
5094 if (!f->outfile)
5095 goto fail;
5096 f->is_file = 1;
5097 return f;
5098 fail:
5099 if (f->outfile)
5100 fclose(f->outfile);
5101 qemu_free(f);
5102 return NULL;
5103}
5104
5105QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
5106{
5107 QEMUFile *f;
5108
5109 f = qemu_mallocz(sizeof(QEMUFile));
5110 if (!f)
5111 return NULL;
5112 f->is_file = 0;
5113 f->bs = bs;
5114 f->is_writable = is_writable;
5115 f->base_offset = offset;
5116 return f;
5117}
5118
5119void qemu_fflush(QEMUFile *f)
5120{
5121 if (!f->is_writable)
5122 return;
5123 if (f->buf_index > 0) {
5124 if (f->is_file) {
5125 fseek(f->outfile, f->buf_offset, SEEK_SET);
5126 fwrite(f->buf, 1, f->buf_index, f->outfile);
5127 } else {
ths5fafdf22007-09-16 21:08:06 +00005128 bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005129 f->buf, f->buf_index);
5130 }
5131 f->buf_offset += f->buf_index;
5132 f->buf_index = 0;
5133 }
5134}
5135
5136static void qemu_fill_buffer(QEMUFile *f)
5137{
5138 int len;
5139
5140 if (f->is_writable)
5141 return;
5142 if (f->is_file) {
5143 fseek(f->outfile, f->buf_offset, SEEK_SET);
5144 len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
5145 if (len < 0)
5146 len = 0;
5147 } else {
ths5fafdf22007-09-16 21:08:06 +00005148 len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005149 f->buf, IO_BUF_SIZE);
5150 if (len < 0)
5151 len = 0;
5152 }
5153 f->buf_index = 0;
5154 f->buf_size = len;
5155 f->buf_offset += len;
5156}
5157
5158void qemu_fclose(QEMUFile *f)
5159{
5160 if (f->is_writable)
5161 qemu_fflush(f);
5162 if (f->is_file) {
5163 fclose(f->outfile);
5164 }
5165 qemu_free(f);
5166}
5167
bellard8a7ddc32004-03-31 19:00:16 +00005168void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
5169{
bellardfaea38e2006-08-05 21:31:00 +00005170 int l;
5171 while (size > 0) {
5172 l = IO_BUF_SIZE - f->buf_index;
5173 if (l > size)
5174 l = size;
5175 memcpy(f->buf + f->buf_index, buf, l);
5176 f->buf_index += l;
5177 buf += l;
5178 size -= l;
5179 if (f->buf_index >= IO_BUF_SIZE)
5180 qemu_fflush(f);
5181 }
bellard8a7ddc32004-03-31 19:00:16 +00005182}
5183
5184void qemu_put_byte(QEMUFile *f, int v)
5185{
bellardfaea38e2006-08-05 21:31:00 +00005186 f->buf[f->buf_index++] = v;
5187 if (f->buf_index >= IO_BUF_SIZE)
5188 qemu_fflush(f);
5189}
5190
5191int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
5192{
5193 int size, l;
5194
5195 size = size1;
5196 while (size > 0) {
5197 l = f->buf_size - f->buf_index;
5198 if (l == 0) {
5199 qemu_fill_buffer(f);
5200 l = f->buf_size - f->buf_index;
5201 if (l == 0)
5202 break;
5203 }
5204 if (l > size)
5205 l = size;
5206 memcpy(buf, f->buf + f->buf_index, l);
5207 f->buf_index += l;
5208 buf += l;
5209 size -= l;
5210 }
5211 return size1 - size;
5212}
5213
5214int qemu_get_byte(QEMUFile *f)
5215{
5216 if (f->buf_index >= f->buf_size) {
5217 qemu_fill_buffer(f);
5218 if (f->buf_index >= f->buf_size)
5219 return 0;
5220 }
5221 return f->buf[f->buf_index++];
5222}
5223
5224int64_t qemu_ftell(QEMUFile *f)
5225{
5226 return f->buf_offset - f->buf_size + f->buf_index;
5227}
5228
5229int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
5230{
5231 if (whence == SEEK_SET) {
5232 /* nothing to do */
5233 } else if (whence == SEEK_CUR) {
5234 pos += qemu_ftell(f);
5235 } else {
5236 /* SEEK_END not supported */
5237 return -1;
5238 }
5239 if (f->is_writable) {
5240 qemu_fflush(f);
5241 f->buf_offset = pos;
5242 } else {
5243 f->buf_offset = pos;
5244 f->buf_index = 0;
5245 f->buf_size = 0;
5246 }
5247 return pos;
bellard8a7ddc32004-03-31 19:00:16 +00005248}
5249
5250void qemu_put_be16(QEMUFile *f, unsigned int v)
5251{
5252 qemu_put_byte(f, v >> 8);
5253 qemu_put_byte(f, v);
5254}
5255
5256void qemu_put_be32(QEMUFile *f, unsigned int v)
5257{
5258 qemu_put_byte(f, v >> 24);
5259 qemu_put_byte(f, v >> 16);
5260 qemu_put_byte(f, v >> 8);
5261 qemu_put_byte(f, v);
5262}
5263
5264void qemu_put_be64(QEMUFile *f, uint64_t v)
5265{
5266 qemu_put_be32(f, v >> 32);
5267 qemu_put_be32(f, v);
5268}
5269
bellard8a7ddc32004-03-31 19:00:16 +00005270unsigned int qemu_get_be16(QEMUFile *f)
5271{
5272 unsigned int v;
5273 v = qemu_get_byte(f) << 8;
5274 v |= qemu_get_byte(f);
5275 return v;
5276}
5277
5278unsigned int qemu_get_be32(QEMUFile *f)
5279{
5280 unsigned int v;
5281 v = qemu_get_byte(f) << 24;
5282 v |= qemu_get_byte(f) << 16;
5283 v |= qemu_get_byte(f) << 8;
5284 v |= qemu_get_byte(f);
5285 return v;
5286}
5287
5288uint64_t qemu_get_be64(QEMUFile *f)
5289{
5290 uint64_t v;
5291 v = (uint64_t)qemu_get_be32(f) << 32;
5292 v |= qemu_get_be32(f);
5293 return v;
5294}
5295
bellard8a7ddc32004-03-31 19:00:16 +00005296typedef struct SaveStateEntry {
5297 char idstr[256];
5298 int instance_id;
5299 int version_id;
5300 SaveStateHandler *save_state;
5301 LoadStateHandler *load_state;
5302 void *opaque;
5303 struct SaveStateEntry *next;
5304} SaveStateEntry;
5305
5306static SaveStateEntry *first_se;
5307
ths5fafdf22007-09-16 21:08:06 +00005308int register_savevm(const char *idstr,
5309 int instance_id,
bellard8a7ddc32004-03-31 19:00:16 +00005310 int version_id,
5311 SaveStateHandler *save_state,
5312 LoadStateHandler *load_state,
5313 void *opaque)
5314{
5315 SaveStateEntry *se, **pse;
5316
5317 se = qemu_malloc(sizeof(SaveStateEntry));
5318 if (!se)
5319 return -1;
5320 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
5321 se->instance_id = instance_id;
5322 se->version_id = version_id;
5323 se->save_state = save_state;
5324 se->load_state = load_state;
5325 se->opaque = opaque;
5326 se->next = NULL;
5327
5328 /* add at the end of list */
5329 pse = &first_se;
5330 while (*pse != NULL)
5331 pse = &(*pse)->next;
5332 *pse = se;
5333 return 0;
5334}
5335
5336#define QEMU_VM_FILE_MAGIC 0x5145564d
bellardfaea38e2006-08-05 21:31:00 +00005337#define QEMU_VM_FILE_VERSION 0x00000002
bellard8a7ddc32004-03-31 19:00:16 +00005338
bellardfaea38e2006-08-05 21:31:00 +00005339int qemu_savevm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005340{
5341 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005342 int len, ret;
5343 int64_t cur_pos, len_pos, total_len_pos;
bellard313aa562003-08-10 21:52:11 +00005344
bellard8a7ddc32004-03-31 19:00:16 +00005345 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
5346 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
bellardfaea38e2006-08-05 21:31:00 +00005347 total_len_pos = qemu_ftell(f);
5348 qemu_put_be64(f, 0); /* total size */
bellard8a7ddc32004-03-31 19:00:16 +00005349
5350 for(se = first_se; se != NULL; se = se->next) {
5351 /* ID string */
5352 len = strlen(se->idstr);
5353 qemu_put_byte(f, len);
5354 qemu_put_buffer(f, se->idstr, len);
5355
5356 qemu_put_be32(f, se->instance_id);
5357 qemu_put_be32(f, se->version_id);
5358
5359 /* record size: filled later */
bellardfaea38e2006-08-05 21:31:00 +00005360 len_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005361 qemu_put_be32(f, 0);
ths3b46e622007-09-17 08:09:54 +00005362
bellard8a7ddc32004-03-31 19:00:16 +00005363 se->save_state(f, se->opaque);
5364
5365 /* fill record size */
bellardfaea38e2006-08-05 21:31:00 +00005366 cur_pos = qemu_ftell(f);
5367 len = cur_pos - len_pos - 4;
5368 qemu_fseek(f, len_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005369 qemu_put_be32(f, len);
bellardfaea38e2006-08-05 21:31:00 +00005370 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005371 }
bellardfaea38e2006-08-05 21:31:00 +00005372 cur_pos = qemu_ftell(f);
5373 qemu_fseek(f, total_len_pos, SEEK_SET);
5374 qemu_put_be64(f, cur_pos - total_len_pos - 8);
5375 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005376
bellard8a7ddc32004-03-31 19:00:16 +00005377 ret = 0;
bellard8a7ddc32004-03-31 19:00:16 +00005378 return ret;
5379}
5380
5381static SaveStateEntry *find_se(const char *idstr, int instance_id)
5382{
5383 SaveStateEntry *se;
5384
5385 for(se = first_se; se != NULL; se = se->next) {
ths5fafdf22007-09-16 21:08:06 +00005386 if (!strcmp(se->idstr, idstr) &&
bellard8a7ddc32004-03-31 19:00:16 +00005387 instance_id == se->instance_id)
5388 return se;
5389 }
5390 return NULL;
5391}
5392
bellardfaea38e2006-08-05 21:31:00 +00005393int qemu_loadvm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005394{
5395 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005396 int len, ret, instance_id, record_len, version_id;
5397 int64_t total_len, end_pos, cur_pos;
bellard8a7ddc32004-03-31 19:00:16 +00005398 unsigned int v;
5399 char idstr[256];
ths3b46e622007-09-17 08:09:54 +00005400
bellard8a7ddc32004-03-31 19:00:16 +00005401 v = qemu_get_be32(f);
5402 if (v != QEMU_VM_FILE_MAGIC)
5403 goto fail;
5404 v = qemu_get_be32(f);
5405 if (v != QEMU_VM_FILE_VERSION) {
5406 fail:
bellard8a7ddc32004-03-31 19:00:16 +00005407 ret = -1;
5408 goto the_end;
5409 }
bellardfaea38e2006-08-05 21:31:00 +00005410 total_len = qemu_get_be64(f);
5411 end_pos = total_len + qemu_ftell(f);
bellardb4608c02003-06-27 17:34:32 +00005412 for(;;) {
bellardfaea38e2006-08-05 21:31:00 +00005413 if (qemu_ftell(f) >= end_pos)
bellard8a7ddc32004-03-31 19:00:16 +00005414 break;
bellardfaea38e2006-08-05 21:31:00 +00005415 len = qemu_get_byte(f);
bellard8a7ddc32004-03-31 19:00:16 +00005416 qemu_get_buffer(f, idstr, len);
5417 idstr[len] = '\0';
5418 instance_id = qemu_get_be32(f);
5419 version_id = qemu_get_be32(f);
5420 record_len = qemu_get_be32(f);
5421#if 0
ths5fafdf22007-09-16 21:08:06 +00005422 printf("idstr=%s instance=0x%x version=%d len=%d\n",
bellard8a7ddc32004-03-31 19:00:16 +00005423 idstr, instance_id, version_id, record_len);
bellardc45886d2004-01-05 00:02:06 +00005424#endif
bellardfaea38e2006-08-05 21:31:00 +00005425 cur_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005426 se = find_se(idstr, instance_id);
5427 if (!se) {
ths5fafdf22007-09-16 21:08:06 +00005428 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
bellard8a7ddc32004-03-31 19:00:16 +00005429 instance_id, idstr);
5430 } else {
5431 ret = se->load_state(f, se->opaque, version_id);
5432 if (ret < 0) {
ths5fafdf22007-09-16 21:08:06 +00005433 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
bellard8a7ddc32004-03-31 19:00:16 +00005434 instance_id, idstr);
5435 }
bellard34865132003-10-05 14:28:56 +00005436 }
bellard8a7ddc32004-03-31 19:00:16 +00005437 /* always seek to exact end of record */
5438 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
5439 }
bellard8a7ddc32004-03-31 19:00:16 +00005440 ret = 0;
5441 the_end:
bellardfaea38e2006-08-05 21:31:00 +00005442 return ret;
5443}
5444
5445/* device can contain snapshots */
5446static int bdrv_can_snapshot(BlockDriverState *bs)
5447{
5448 return (bs &&
5449 !bdrv_is_removable(bs) &&
5450 !bdrv_is_read_only(bs));
5451}
5452
5453/* device must be snapshots in order to have a reliable snapshot */
5454static int bdrv_has_snapshot(BlockDriverState *bs)
5455{
5456 return (bs &&
5457 !bdrv_is_removable(bs) &&
5458 !bdrv_is_read_only(bs));
5459}
5460
5461static BlockDriverState *get_bs_snapshots(void)
5462{
5463 BlockDriverState *bs;
5464 int i;
5465
5466 if (bs_snapshots)
5467 return bs_snapshots;
5468 for(i = 0; i <= MAX_DISKS; i++) {
5469 bs = bs_table[i];
5470 if (bdrv_can_snapshot(bs))
5471 goto ok;
5472 }
5473 return NULL;
5474 ok:
5475 bs_snapshots = bs;
5476 return bs;
5477}
5478
5479static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
5480 const char *name)
5481{
5482 QEMUSnapshotInfo *sn_tab, *sn;
5483 int nb_sns, i, ret;
ths3b46e622007-09-17 08:09:54 +00005484
bellardfaea38e2006-08-05 21:31:00 +00005485 ret = -ENOENT;
5486 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5487 if (nb_sns < 0)
5488 return ret;
5489 for(i = 0; i < nb_sns; i++) {
5490 sn = &sn_tab[i];
5491 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
5492 *sn_info = *sn;
5493 ret = 0;
5494 break;
5495 }
5496 }
5497 qemu_free(sn_tab);
5498 return ret;
5499}
5500
5501void do_savevm(const char *name)
5502{
5503 BlockDriverState *bs, *bs1;
5504 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
5505 int must_delete, ret, i;
5506 BlockDriverInfo bdi1, *bdi = &bdi1;
5507 QEMUFile *f;
5508 int saved_vm_running;
bellard4c279bd2006-08-17 09:43:50 +00005509#ifdef _WIN32
5510 struct _timeb tb;
5511#else
bellardfaea38e2006-08-05 21:31:00 +00005512 struct timeval tv;
bellard4c279bd2006-08-17 09:43:50 +00005513#endif
bellardfaea38e2006-08-05 21:31:00 +00005514
5515 bs = get_bs_snapshots();
5516 if (!bs) {
5517 term_printf("No block device can accept snapshots\n");
5518 return;
5519 }
5520
pbrook6192bc32006-09-03 12:08:37 +00005521 /* ??? Should this occur after vm_stop? */
5522 qemu_aio_flush();
5523
bellardfaea38e2006-08-05 21:31:00 +00005524 saved_vm_running = vm_running;
5525 vm_stop(0);
ths3b46e622007-09-17 08:09:54 +00005526
bellardfaea38e2006-08-05 21:31:00 +00005527 must_delete = 0;
5528 if (name) {
5529 ret = bdrv_snapshot_find(bs, old_sn, name);
5530 if (ret >= 0) {
5531 must_delete = 1;
5532 }
5533 }
5534 memset(sn, 0, sizeof(*sn));
5535 if (must_delete) {
5536 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
5537 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
5538 } else {
5539 if (name)
5540 pstrcpy(sn->name, sizeof(sn->name), name);
5541 }
5542
5543 /* fill auxiliary fields */
bellard4c279bd2006-08-17 09:43:50 +00005544#ifdef _WIN32
5545 _ftime(&tb);
5546 sn->date_sec = tb.time;
5547 sn->date_nsec = tb.millitm * 1000000;
5548#else
bellardfaea38e2006-08-05 21:31:00 +00005549 gettimeofday(&tv, NULL);
5550 sn->date_sec = tv.tv_sec;
5551 sn->date_nsec = tv.tv_usec * 1000;
bellard4c279bd2006-08-17 09:43:50 +00005552#endif
bellardfaea38e2006-08-05 21:31:00 +00005553 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
ths3b46e622007-09-17 08:09:54 +00005554
bellardfaea38e2006-08-05 21:31:00 +00005555 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5556 term_printf("Device %s does not support VM state snapshots\n",
5557 bdrv_get_device_name(bs));
5558 goto the_end;
5559 }
ths3b46e622007-09-17 08:09:54 +00005560
bellardfaea38e2006-08-05 21:31:00 +00005561 /* save the VM state */
5562 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
5563 if (!f) {
5564 term_printf("Could not open VM state file\n");
5565 goto the_end;
5566 }
5567 ret = qemu_savevm_state(f);
5568 sn->vm_state_size = qemu_ftell(f);
5569 qemu_fclose(f);
5570 if (ret < 0) {
5571 term_printf("Error %d while writing VM\n", ret);
5572 goto the_end;
5573 }
ths3b46e622007-09-17 08:09:54 +00005574
bellardfaea38e2006-08-05 21:31:00 +00005575 /* create the snapshots */
5576
5577 for(i = 0; i < MAX_DISKS; i++) {
5578 bs1 = bs_table[i];
5579 if (bdrv_has_snapshot(bs1)) {
5580 if (must_delete) {
5581 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
5582 if (ret < 0) {
5583 term_printf("Error while deleting snapshot on '%s'\n",
5584 bdrv_get_device_name(bs1));
5585 }
5586 }
5587 ret = bdrv_snapshot_create(bs1, sn);
5588 if (ret < 0) {
5589 term_printf("Error while creating snapshot on '%s'\n",
5590 bdrv_get_device_name(bs1));
5591 }
5592 }
5593 }
5594
5595 the_end:
bellard8a7ddc32004-03-31 19:00:16 +00005596 if (saved_vm_running)
5597 vm_start();
bellardfaea38e2006-08-05 21:31:00 +00005598}
5599
5600void do_loadvm(const char *name)
5601{
5602 BlockDriverState *bs, *bs1;
5603 BlockDriverInfo bdi1, *bdi = &bdi1;
5604 QEMUFile *f;
5605 int i, ret;
5606 int saved_vm_running;
5607
5608 bs = get_bs_snapshots();
5609 if (!bs) {
5610 term_printf("No block device supports snapshots\n");
5611 return;
5612 }
ths3b46e622007-09-17 08:09:54 +00005613
pbrook6192bc32006-09-03 12:08:37 +00005614 /* Flush all IO requests so they don't interfere with the new state. */
5615 qemu_aio_flush();
5616
bellardfaea38e2006-08-05 21:31:00 +00005617 saved_vm_running = vm_running;
5618 vm_stop(0);
5619
5620 for(i = 0; i <= MAX_DISKS; i++) {
5621 bs1 = bs_table[i];
5622 if (bdrv_has_snapshot(bs1)) {
5623 ret = bdrv_snapshot_goto(bs1, name);
5624 if (ret < 0) {
5625 if (bs != bs1)
5626 term_printf("Warning: ");
5627 switch(ret) {
5628 case -ENOTSUP:
5629 term_printf("Snapshots not supported on device '%s'\n",
5630 bdrv_get_device_name(bs1));
5631 break;
5632 case -ENOENT:
5633 term_printf("Could not find snapshot '%s' on device '%s'\n",
5634 name, bdrv_get_device_name(bs1));
5635 break;
5636 default:
5637 term_printf("Error %d while activating snapshot on '%s'\n",
5638 ret, bdrv_get_device_name(bs1));
5639 break;
5640 }
5641 /* fatal on snapshot block device */
5642 if (bs == bs1)
5643 goto the_end;
5644 }
5645 }
5646 }
5647
5648 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5649 term_printf("Device %s does not support VM state snapshots\n",
5650 bdrv_get_device_name(bs));
5651 return;
5652 }
ths3b46e622007-09-17 08:09:54 +00005653
bellardfaea38e2006-08-05 21:31:00 +00005654 /* restore the VM state */
5655 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
5656 if (!f) {
5657 term_printf("Could not open VM state file\n");
5658 goto the_end;
5659 }
5660 ret = qemu_loadvm_state(f);
5661 qemu_fclose(f);
5662 if (ret < 0) {
5663 term_printf("Error %d while loading VM state\n", ret);
5664 }
5665 the_end:
5666 if (saved_vm_running)
5667 vm_start();
5668}
5669
5670void do_delvm(const char *name)
5671{
5672 BlockDriverState *bs, *bs1;
5673 int i, ret;
5674
5675 bs = get_bs_snapshots();
5676 if (!bs) {
5677 term_printf("No block device supports snapshots\n");
5678 return;
5679 }
ths3b46e622007-09-17 08:09:54 +00005680
bellardfaea38e2006-08-05 21:31:00 +00005681 for(i = 0; i <= MAX_DISKS; i++) {
5682 bs1 = bs_table[i];
5683 if (bdrv_has_snapshot(bs1)) {
5684 ret = bdrv_snapshot_delete(bs1, name);
5685 if (ret < 0) {
5686 if (ret == -ENOTSUP)
5687 term_printf("Snapshots not supported on device '%s'\n",
5688 bdrv_get_device_name(bs1));
5689 else
5690 term_printf("Error %d while deleting snapshot on '%s'\n",
5691 ret, bdrv_get_device_name(bs1));
5692 }
5693 }
5694 }
5695}
5696
5697void do_info_snapshots(void)
5698{
5699 BlockDriverState *bs, *bs1;
5700 QEMUSnapshotInfo *sn_tab, *sn;
5701 int nb_sns, i;
5702 char buf[256];
5703
5704 bs = get_bs_snapshots();
5705 if (!bs) {
5706 term_printf("No available block device supports snapshots\n");
5707 return;
5708 }
5709 term_printf("Snapshot devices:");
5710 for(i = 0; i <= MAX_DISKS; i++) {
5711 bs1 = bs_table[i];
5712 if (bdrv_has_snapshot(bs1)) {
5713 if (bs == bs1)
5714 term_printf(" %s", bdrv_get_device_name(bs1));
5715 }
5716 }
5717 term_printf("\n");
5718
5719 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5720 if (nb_sns < 0) {
5721 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
5722 return;
5723 }
5724 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
5725 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
5726 for(i = 0; i < nb_sns; i++) {
5727 sn = &sn_tab[i];
5728 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
5729 }
5730 qemu_free(sn_tab);
bellard8a7ddc32004-03-31 19:00:16 +00005731}
5732
5733/***********************************************************/
5734/* cpu save/restore */
5735
5736#if defined(TARGET_I386)
5737
5738static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
5739{
bellard02ba45c2004-06-25 14:46:23 +00005740 qemu_put_be32(f, dt->selector);
bellard20f32282005-01-03 23:36:21 +00005741 qemu_put_betl(f, dt->base);
bellard8a7ddc32004-03-31 19:00:16 +00005742 qemu_put_be32(f, dt->limit);
5743 qemu_put_be32(f, dt->flags);
5744}
5745
5746static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
5747{
bellard02ba45c2004-06-25 14:46:23 +00005748 dt->selector = qemu_get_be32(f);
bellard20f32282005-01-03 23:36:21 +00005749 dt->base = qemu_get_betl(f);
bellard8a7ddc32004-03-31 19:00:16 +00005750 dt->limit = qemu_get_be32(f);
5751 dt->flags = qemu_get_be32(f);
5752}
5753
5754void cpu_save(QEMUFile *f, void *opaque)
5755{
5756 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00005757 uint16_t fptag, fpus, fpuc, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00005758 uint32_t hflags;
5759 int i;
ths3b46e622007-09-17 08:09:54 +00005760
bellard20f32282005-01-03 23:36:21 +00005761 for(i = 0; i < CPU_NB_REGS; i++)
5762 qemu_put_betls(f, &env->regs[i]);
5763 qemu_put_betls(f, &env->eip);
5764 qemu_put_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00005765 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
5766 qemu_put_be32s(f, &hflags);
ths3b46e622007-09-17 08:09:54 +00005767
bellard8a7ddc32004-03-31 19:00:16 +00005768 /* FPU */
5769 fpuc = env->fpuc;
5770 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
5771 fptag = 0;
bellard664e0f12005-01-08 18:58:29 +00005772 for(i = 0; i < 8; i++) {
5773 fptag |= ((!env->fptags[i]) << i);
bellard8a7ddc32004-03-31 19:00:16 +00005774 }
ths3b46e622007-09-17 08:09:54 +00005775
bellard8a7ddc32004-03-31 19:00:16 +00005776 qemu_put_be16s(f, &fpuc);
5777 qemu_put_be16s(f, &fpus);
5778 qemu_put_be16s(f, &fptag);
5779
bellard664e0f12005-01-08 18:58:29 +00005780#ifdef USE_X86LDOUBLE
5781 fpregs_format = 0;
5782#else
5783 fpregs_format = 1;
5784#endif
5785 qemu_put_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00005786
bellard8a7ddc32004-03-31 19:00:16 +00005787 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00005788#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00005789 {
5790 uint64_t mant;
5791 uint16_t exp;
5792 /* we save the real CPU data (in case of MMX usage only 'mant'
5793 contains the MMX register */
5794 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
5795 qemu_put_be64(f, mant);
5796 qemu_put_be16(f, exp);
5797 }
bellard664e0f12005-01-08 18:58:29 +00005798#else
5799 /* if we use doubles for float emulation, we save the doubles to
5800 avoid losing information in case of MMX usage. It can give
5801 problems if the image is restored on a CPU where long
5802 doubles are used instead. */
bellard8636b5d2005-01-09 00:03:14 +00005803 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
bellard664e0f12005-01-08 18:58:29 +00005804#endif
bellard8a7ddc32004-03-31 19:00:16 +00005805 }
5806
5807 for(i = 0; i < 6; i++)
5808 cpu_put_seg(f, &env->segs[i]);
5809 cpu_put_seg(f, &env->ldt);
5810 cpu_put_seg(f, &env->tr);
5811 cpu_put_seg(f, &env->gdt);
5812 cpu_put_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00005813
bellard8a7ddc32004-03-31 19:00:16 +00005814 qemu_put_be32s(f, &env->sysenter_cs);
5815 qemu_put_be32s(f, &env->sysenter_esp);
5816 qemu_put_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00005817
bellard20f32282005-01-03 23:36:21 +00005818 qemu_put_betls(f, &env->cr[0]);
5819 qemu_put_betls(f, &env->cr[2]);
5820 qemu_put_betls(f, &env->cr[3]);
5821 qemu_put_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00005822
bellard8a7ddc32004-03-31 19:00:16 +00005823 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00005824 qemu_put_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00005825
5826 /* MMU */
5827 qemu_put_be32s(f, &env->a20_mask);
bellard02536f82005-01-06 20:43:38 +00005828
bellard664e0f12005-01-08 18:58:29 +00005829 /* XMM */
5830 qemu_put_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00005831 for(i = 0; i < CPU_NB_REGS; i++) {
5832 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
5833 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
5834 }
5835
bellard664e0f12005-01-08 18:58:29 +00005836#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00005837 qemu_put_be64s(f, &env->efer);
5838 qemu_put_be64s(f, &env->star);
5839 qemu_put_be64s(f, &env->lstar);
5840 qemu_put_be64s(f, &env->cstar);
5841 qemu_put_be64s(f, &env->fmask);
5842 qemu_put_be64s(f, &env->kernelgsbase);
5843#endif
bellard3b21e032006-09-24 18:41:56 +00005844 qemu_put_be32s(f, &env->smbase);
bellard8a7ddc32004-03-31 19:00:16 +00005845}
5846
bellard8636b5d2005-01-09 00:03:14 +00005847#ifdef USE_X86LDOUBLE
bellard664e0f12005-01-08 18:58:29 +00005848/* XXX: add that in a FPU generic layer */
5849union x86_longdouble {
5850 uint64_t mant;
5851 uint16_t exp;
5852};
5853
5854#define MANTD1(fp) (fp & ((1LL << 52) - 1))
5855#define EXPBIAS1 1023
5856#define EXPD1(fp) ((fp >> 52) & 0x7FF)
5857#define SIGND1(fp) ((fp >> 32) & 0x80000000)
5858
5859static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
5860{
5861 int e;
5862 /* mantissa */
5863 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
5864 /* exponent + sign */
5865 e = EXPD1(temp) - EXPBIAS1 + 16383;
5866 e |= SIGND1(temp) >> 16;
5867 p->exp = e;
5868}
bellard8636b5d2005-01-09 00:03:14 +00005869#endif
bellard664e0f12005-01-08 18:58:29 +00005870
bellard8a7ddc32004-03-31 19:00:16 +00005871int cpu_load(QEMUFile *f, void *opaque, int version_id)
5872{
5873 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00005874 int i, guess_mmx;
bellard8a7ddc32004-03-31 19:00:16 +00005875 uint32_t hflags;
bellard664e0f12005-01-08 18:58:29 +00005876 uint16_t fpus, fpuc, fptag, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00005877
bellard3b21e032006-09-24 18:41:56 +00005878 if (version_id != 3 && version_id != 4)
bellard8a7ddc32004-03-31 19:00:16 +00005879 return -EINVAL;
bellard20f32282005-01-03 23:36:21 +00005880 for(i = 0; i < CPU_NB_REGS; i++)
5881 qemu_get_betls(f, &env->regs[i]);
5882 qemu_get_betls(f, &env->eip);
5883 qemu_get_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00005884 qemu_get_be32s(f, &hflags);
5885
5886 qemu_get_be16s(f, &fpuc);
5887 qemu_get_be16s(f, &fpus);
5888 qemu_get_be16s(f, &fptag);
bellard664e0f12005-01-08 18:58:29 +00005889 qemu_get_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00005890
bellard664e0f12005-01-08 18:58:29 +00005891 /* NOTE: we cannot always restore the FPU state if the image come
5892 from a host with a different 'USE_X86LDOUBLE' define. We guess
5893 if we are in an MMX state to restore correctly in that case. */
5894 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
bellard8a7ddc32004-03-31 19:00:16 +00005895 for(i = 0; i < 8; i++) {
5896 uint64_t mant;
5897 uint16_t exp;
ths3b46e622007-09-17 08:09:54 +00005898
bellard664e0f12005-01-08 18:58:29 +00005899 switch(fpregs_format) {
5900 case 0:
5901 mant = qemu_get_be64(f);
5902 exp = qemu_get_be16(f);
5903#ifdef USE_X86LDOUBLE
5904 env->fpregs[i].d = cpu_set_fp80(mant, exp);
5905#else
5906 /* difficult case */
5907 if (guess_mmx)
bellard8636b5d2005-01-09 00:03:14 +00005908 env->fpregs[i].mmx.MMX_Q(0) = mant;
bellard664e0f12005-01-08 18:58:29 +00005909 else
5910 env->fpregs[i].d = cpu_set_fp80(mant, exp);
5911#endif
5912 break;
5913 case 1:
5914 mant = qemu_get_be64(f);
5915#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00005916 {
5917 union x86_longdouble *p;
5918 /* difficult case */
5919 p = (void *)&env->fpregs[i];
5920 if (guess_mmx) {
5921 p->mant = mant;
5922 p->exp = 0xffff;
5923 } else {
5924 fp64_to_fp80(p, mant);
5925 }
bellard664e0f12005-01-08 18:58:29 +00005926 }
5927#else
bellard8636b5d2005-01-09 00:03:14 +00005928 env->fpregs[i].mmx.MMX_Q(0) = mant;
ths3b46e622007-09-17 08:09:54 +00005929#endif
bellard664e0f12005-01-08 18:58:29 +00005930 break;
5931 default:
5932 return -EINVAL;
5933 }
bellard8a7ddc32004-03-31 19:00:16 +00005934 }
5935
5936 env->fpuc = fpuc;
bellard7a0e1f42005-03-13 17:01:47 +00005937 /* XXX: restore FPU round state */
bellard8a7ddc32004-03-31 19:00:16 +00005938 env->fpstt = (fpus >> 11) & 7;
5939 env->fpus = fpus & ~0x3800;
bellard664e0f12005-01-08 18:58:29 +00005940 fptag ^= 0xff;
bellard8a7ddc32004-03-31 19:00:16 +00005941 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00005942 env->fptags[i] = (fptag >> i) & 1;
bellard8a7ddc32004-03-31 19:00:16 +00005943 }
ths3b46e622007-09-17 08:09:54 +00005944
bellard8a7ddc32004-03-31 19:00:16 +00005945 for(i = 0; i < 6; i++)
5946 cpu_get_seg(f, &env->segs[i]);
5947 cpu_get_seg(f, &env->ldt);
5948 cpu_get_seg(f, &env->tr);
5949 cpu_get_seg(f, &env->gdt);
5950 cpu_get_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00005951
bellard8a7ddc32004-03-31 19:00:16 +00005952 qemu_get_be32s(f, &env->sysenter_cs);
5953 qemu_get_be32s(f, &env->sysenter_esp);
5954 qemu_get_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00005955
bellard20f32282005-01-03 23:36:21 +00005956 qemu_get_betls(f, &env->cr[0]);
5957 qemu_get_betls(f, &env->cr[2]);
5958 qemu_get_betls(f, &env->cr[3]);
5959 qemu_get_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00005960
bellard8a7ddc32004-03-31 19:00:16 +00005961 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00005962 qemu_get_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00005963
5964 /* MMU */
5965 qemu_get_be32s(f, &env->a20_mask);
5966
bellard664e0f12005-01-08 18:58:29 +00005967 qemu_get_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00005968 for(i = 0; i < CPU_NB_REGS; i++) {
5969 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
5970 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
5971 }
5972
bellard664e0f12005-01-08 18:58:29 +00005973#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00005974 qemu_get_be64s(f, &env->efer);
5975 qemu_get_be64s(f, &env->star);
5976 qemu_get_be64s(f, &env->lstar);
5977 qemu_get_be64s(f, &env->cstar);
5978 qemu_get_be64s(f, &env->fmask);
5979 qemu_get_be64s(f, &env->kernelgsbase);
5980#endif
ths5fafdf22007-09-16 21:08:06 +00005981 if (version_id >= 4)
bellard3b21e032006-09-24 18:41:56 +00005982 qemu_get_be32s(f, &env->smbase);
bellard02536f82005-01-06 20:43:38 +00005983
bellard8a7ddc32004-03-31 19:00:16 +00005984 /* XXX: compute hflags from scratch, except for CPL and IIF */
5985 env->hflags = hflags;
5986 tlb_flush(env, 1);
5987 return 0;
5988}
5989
bellarda541f292004-04-12 20:39:29 +00005990#elif defined(TARGET_PPC)
5991void cpu_save(QEMUFile *f, void *opaque)
5992{
5993}
5994
5995int cpu_load(QEMUFile *f, void *opaque, int version_id)
5996{
5997 return 0;
5998}
bellard6af0bf92005-07-02 14:58:51 +00005999
6000#elif defined(TARGET_MIPS)
6001void cpu_save(QEMUFile *f, void *opaque)
6002{
6003}
6004
6005int cpu_load(QEMUFile *f, void *opaque, int version_id)
6006{
6007 return 0;
6008}
6009
bellarde95c8d52004-09-30 22:22:08 +00006010#elif defined(TARGET_SPARC)
6011void cpu_save(QEMUFile *f, void *opaque)
6012{
bellarde80cfcf2004-12-19 23:18:01 +00006013 CPUState *env = opaque;
6014 int i;
6015 uint32_t tmp;
6016
bellard4fa5d772005-01-30 22:57:54 +00006017 for(i = 0; i < 8; i++)
6018 qemu_put_betls(f, &env->gregs[i]);
6019 for(i = 0; i < NWINDOWS * 16; i++)
6020 qemu_put_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006021
6022 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006023 for(i = 0; i < TARGET_FPREGS; i++) {
6024 union {
bellard1bdb68e2006-06-21 18:48:01 +00006025 float32 f;
6026 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006027 } u;
6028 u.f = env->fpr[i];
bellard1bdb68e2006-06-21 18:48:01 +00006029 qemu_put_be32(f, u.i);
bellarde80cfcf2004-12-19 23:18:01 +00006030 }
bellard4fa5d772005-01-30 22:57:54 +00006031
6032 qemu_put_betls(f, &env->pc);
6033 qemu_put_betls(f, &env->npc);
6034 qemu_put_betls(f, &env->y);
bellarde80cfcf2004-12-19 23:18:01 +00006035 tmp = GET_PSR(env);
bellard4fa5d772005-01-30 22:57:54 +00006036 qemu_put_be32(f, tmp);
bellard34751872005-07-02 14:31:34 +00006037 qemu_put_betls(f, &env->fsr);
6038 qemu_put_betls(f, &env->tbr);
6039#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006040 qemu_put_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006041 /* MMU */
6042 for(i = 0; i < 16; i++)
6043 qemu_put_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006044#endif
bellarde95c8d52004-09-30 22:22:08 +00006045}
6046
6047int cpu_load(QEMUFile *f, void *opaque, int version_id)
6048{
bellarde80cfcf2004-12-19 23:18:01 +00006049 CPUState *env = opaque;
6050 int i;
6051 uint32_t tmp;
6052
bellard4fa5d772005-01-30 22:57:54 +00006053 for(i = 0; i < 8; i++)
6054 qemu_get_betls(f, &env->gregs[i]);
6055 for(i = 0; i < NWINDOWS * 16; i++)
6056 qemu_get_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006057
6058 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006059 for(i = 0; i < TARGET_FPREGS; i++) {
6060 union {
bellard1bdb68e2006-06-21 18:48:01 +00006061 float32 f;
6062 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006063 } u;
bellard1bdb68e2006-06-21 18:48:01 +00006064 u.i = qemu_get_be32(f);
bellard4fa5d772005-01-30 22:57:54 +00006065 env->fpr[i] = u.f;
bellarde80cfcf2004-12-19 23:18:01 +00006066 }
bellard4fa5d772005-01-30 22:57:54 +00006067
6068 qemu_get_betls(f, &env->pc);
6069 qemu_get_betls(f, &env->npc);
6070 qemu_get_betls(f, &env->y);
6071 tmp = qemu_get_be32(f);
6072 env->cwp = 0; /* needed to ensure that the wrapping registers are
6073 correctly updated */
bellarde80cfcf2004-12-19 23:18:01 +00006074 PUT_PSR(env, tmp);
bellard34751872005-07-02 14:31:34 +00006075 qemu_get_betls(f, &env->fsr);
6076 qemu_get_betls(f, &env->tbr);
6077#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006078 qemu_get_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006079 /* MMU */
6080 for(i = 0; i < 16; i++)
6081 qemu_get_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006082#endif
bellarde80cfcf2004-12-19 23:18:01 +00006083 tlb_flush(env, 1);
bellarde95c8d52004-09-30 22:22:08 +00006084 return 0;
6085}
bellardb5ff1b32005-11-26 10:38:39 +00006086
6087#elif defined(TARGET_ARM)
6088
bellardb5ff1b32005-11-26 10:38:39 +00006089void cpu_save(QEMUFile *f, void *opaque)
6090{
balrogaa941b92007-05-24 18:50:09 +00006091 int i;
6092 CPUARMState *env = (CPUARMState *)opaque;
6093
6094 for (i = 0; i < 16; i++) {
6095 qemu_put_be32(f, env->regs[i]);
6096 }
6097 qemu_put_be32(f, cpsr_read(env));
6098 qemu_put_be32(f, env->spsr);
6099 for (i = 0; i < 6; i++) {
6100 qemu_put_be32(f, env->banked_spsr[i]);
6101 qemu_put_be32(f, env->banked_r13[i]);
6102 qemu_put_be32(f, env->banked_r14[i]);
6103 }
6104 for (i = 0; i < 5; i++) {
6105 qemu_put_be32(f, env->usr_regs[i]);
6106 qemu_put_be32(f, env->fiq_regs[i]);
6107 }
6108 qemu_put_be32(f, env->cp15.c0_cpuid);
6109 qemu_put_be32(f, env->cp15.c0_cachetype);
6110 qemu_put_be32(f, env->cp15.c1_sys);
6111 qemu_put_be32(f, env->cp15.c1_coproc);
balrog610c3c82007-06-24 12:09:48 +00006112 qemu_put_be32(f, env->cp15.c1_xscaleauxcr);
balrogaa941b92007-05-24 18:50:09 +00006113 qemu_put_be32(f, env->cp15.c2_base);
6114 qemu_put_be32(f, env->cp15.c2_data);
6115 qemu_put_be32(f, env->cp15.c2_insn);
6116 qemu_put_be32(f, env->cp15.c3);
6117 qemu_put_be32(f, env->cp15.c5_insn);
6118 qemu_put_be32(f, env->cp15.c5_data);
6119 for (i = 0; i < 8; i++) {
6120 qemu_put_be32(f, env->cp15.c6_region[i]);
6121 }
6122 qemu_put_be32(f, env->cp15.c6_insn);
6123 qemu_put_be32(f, env->cp15.c6_data);
6124 qemu_put_be32(f, env->cp15.c9_insn);
6125 qemu_put_be32(f, env->cp15.c9_data);
6126 qemu_put_be32(f, env->cp15.c13_fcse);
6127 qemu_put_be32(f, env->cp15.c13_context);
6128 qemu_put_be32(f, env->cp15.c15_cpar);
6129
6130 qemu_put_be32(f, env->features);
6131
6132 if (arm_feature(env, ARM_FEATURE_VFP)) {
6133 for (i = 0; i < 16; i++) {
6134 CPU_DoubleU u;
6135 u.d = env->vfp.regs[i];
6136 qemu_put_be32(f, u.l.upper);
6137 qemu_put_be32(f, u.l.lower);
6138 }
6139 for (i = 0; i < 16; i++) {
6140 qemu_put_be32(f, env->vfp.xregs[i]);
6141 }
6142
6143 /* TODO: Should use proper FPSCR access functions. */
6144 qemu_put_be32(f, env->vfp.vec_len);
6145 qemu_put_be32(f, env->vfp.vec_stride);
6146 }
6147
6148 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6149 for (i = 0; i < 16; i++) {
6150 qemu_put_be64(f, env->iwmmxt.regs[i]);
6151 }
6152 for (i = 0; i < 16; i++) {
6153 qemu_put_be32(f, env->iwmmxt.cregs[i]);
6154 }
6155 }
bellardb5ff1b32005-11-26 10:38:39 +00006156}
6157
6158int cpu_load(QEMUFile *f, void *opaque, int version_id)
6159{
balrogaa941b92007-05-24 18:50:09 +00006160 CPUARMState *env = (CPUARMState *)opaque;
6161 int i;
6162
6163 if (version_id != 0)
6164 return -EINVAL;
6165
6166 for (i = 0; i < 16; i++) {
6167 env->regs[i] = qemu_get_be32(f);
6168 }
6169 cpsr_write(env, qemu_get_be32(f), 0xffffffff);
6170 env->spsr = qemu_get_be32(f);
6171 for (i = 0; i < 6; i++) {
6172 env->banked_spsr[i] = qemu_get_be32(f);
6173 env->banked_r13[i] = qemu_get_be32(f);
6174 env->banked_r14[i] = qemu_get_be32(f);
6175 }
6176 for (i = 0; i < 5; i++) {
6177 env->usr_regs[i] = qemu_get_be32(f);
6178 env->fiq_regs[i] = qemu_get_be32(f);
6179 }
6180 env->cp15.c0_cpuid = qemu_get_be32(f);
6181 env->cp15.c0_cachetype = qemu_get_be32(f);
6182 env->cp15.c1_sys = qemu_get_be32(f);
6183 env->cp15.c1_coproc = qemu_get_be32(f);
balrog610c3c82007-06-24 12:09:48 +00006184 env->cp15.c1_xscaleauxcr = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006185 env->cp15.c2_base = qemu_get_be32(f);
6186 env->cp15.c2_data = qemu_get_be32(f);
6187 env->cp15.c2_insn = qemu_get_be32(f);
6188 env->cp15.c3 = qemu_get_be32(f);
6189 env->cp15.c5_insn = qemu_get_be32(f);
6190 env->cp15.c5_data = qemu_get_be32(f);
6191 for (i = 0; i < 8; i++) {
6192 env->cp15.c6_region[i] = qemu_get_be32(f);
6193 }
6194 env->cp15.c6_insn = qemu_get_be32(f);
6195 env->cp15.c6_data = qemu_get_be32(f);
6196 env->cp15.c9_insn = qemu_get_be32(f);
6197 env->cp15.c9_data = qemu_get_be32(f);
6198 env->cp15.c13_fcse = qemu_get_be32(f);
6199 env->cp15.c13_context = qemu_get_be32(f);
6200 env->cp15.c15_cpar = qemu_get_be32(f);
6201
6202 env->features = qemu_get_be32(f);
6203
6204 if (arm_feature(env, ARM_FEATURE_VFP)) {
6205 for (i = 0; i < 16; i++) {
6206 CPU_DoubleU u;
6207 u.l.upper = qemu_get_be32(f);
6208 u.l.lower = qemu_get_be32(f);
6209 env->vfp.regs[i] = u.d;
6210 }
6211 for (i = 0; i < 16; i++) {
6212 env->vfp.xregs[i] = qemu_get_be32(f);
6213 }
6214
6215 /* TODO: Should use proper FPSCR access functions. */
6216 env->vfp.vec_len = qemu_get_be32(f);
6217 env->vfp.vec_stride = qemu_get_be32(f);
6218 }
6219
6220 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6221 for (i = 0; i < 16; i++) {
6222 env->iwmmxt.regs[i] = qemu_get_be64(f);
6223 }
6224 for (i = 0; i < 16; i++) {
6225 env->iwmmxt.cregs[i] = qemu_get_be32(f);
6226 }
6227 }
6228
bellardb5ff1b32005-11-26 10:38:39 +00006229 return 0;
6230}
6231
bellard8a7ddc32004-03-31 19:00:16 +00006232#else
6233
6234#warning No CPU save/restore functions
6235
6236#endif
6237
6238/***********************************************************/
6239/* ram save/restore */
6240
bellard8a7ddc32004-03-31 19:00:16 +00006241static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
6242{
6243 int v;
6244
6245 v = qemu_get_byte(f);
6246 switch(v) {
6247 case 0:
6248 if (qemu_get_buffer(f, buf, len) != len)
6249 return -EIO;
6250 break;
6251 case 1:
6252 v = qemu_get_byte(f);
6253 memset(buf, v, len);
6254 break;
6255 default:
6256 return -EINVAL;
6257 }
6258 return 0;
6259}
6260
bellardc88676f2006-08-06 13:36:11 +00006261static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00006262{
6263 int i, ret;
6264
bellard8a7ddc32004-03-31 19:00:16 +00006265 if (qemu_get_be32(f) != phys_ram_size)
6266 return -EINVAL;
6267 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
6268 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
6269 if (ret)
6270 return ret;
6271 }
6272 return 0;
6273}
6274
bellardc88676f2006-08-06 13:36:11 +00006275#define BDRV_HASH_BLOCK_SIZE 1024
6276#define IOBUF_SIZE 4096
6277#define RAM_CBLOCK_MAGIC 0xfabe
6278
6279typedef struct RamCompressState {
6280 z_stream zstream;
6281 QEMUFile *f;
6282 uint8_t buf[IOBUF_SIZE];
6283} RamCompressState;
6284
6285static int ram_compress_open(RamCompressState *s, QEMUFile *f)
6286{
6287 int ret;
6288 memset(s, 0, sizeof(*s));
6289 s->f = f;
6290 ret = deflateInit2(&s->zstream, 1,
ths5fafdf22007-09-16 21:08:06 +00006291 Z_DEFLATED, 15,
bellardc88676f2006-08-06 13:36:11 +00006292 9, Z_DEFAULT_STRATEGY);
6293 if (ret != Z_OK)
6294 return -1;
6295 s->zstream.avail_out = IOBUF_SIZE;
6296 s->zstream.next_out = s->buf;
6297 return 0;
6298}
6299
6300static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len)
6301{
6302 qemu_put_be16(s->f, RAM_CBLOCK_MAGIC);
6303 qemu_put_be16(s->f, len);
6304 qemu_put_buffer(s->f, buf, len);
6305}
6306
6307static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len)
6308{
6309 int ret;
6310
6311 s->zstream.avail_in = len;
6312 s->zstream.next_in = (uint8_t *)buf;
6313 while (s->zstream.avail_in > 0) {
6314 ret = deflate(&s->zstream, Z_NO_FLUSH);
6315 if (ret != Z_OK)
6316 return -1;
6317 if (s->zstream.avail_out == 0) {
6318 ram_put_cblock(s, s->buf, IOBUF_SIZE);
6319 s->zstream.avail_out = IOBUF_SIZE;
6320 s->zstream.next_out = s->buf;
6321 }
6322 }
6323 return 0;
6324}
6325
6326static void ram_compress_close(RamCompressState *s)
6327{
6328 int len, ret;
6329
6330 /* compress last bytes */
6331 for(;;) {
6332 ret = deflate(&s->zstream, Z_FINISH);
6333 if (ret == Z_OK || ret == Z_STREAM_END) {
6334 len = IOBUF_SIZE - s->zstream.avail_out;
6335 if (len > 0) {
6336 ram_put_cblock(s, s->buf, len);
6337 }
6338 s->zstream.avail_out = IOBUF_SIZE;
6339 s->zstream.next_out = s->buf;
6340 if (ret == Z_STREAM_END)
6341 break;
6342 } else {
6343 goto fail;
6344 }
6345 }
6346fail:
6347 deflateEnd(&s->zstream);
6348}
6349
6350typedef struct RamDecompressState {
6351 z_stream zstream;
6352 QEMUFile *f;
6353 uint8_t buf[IOBUF_SIZE];
6354} RamDecompressState;
6355
6356static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
6357{
6358 int ret;
6359 memset(s, 0, sizeof(*s));
6360 s->f = f;
6361 ret = inflateInit(&s->zstream);
6362 if (ret != Z_OK)
6363 return -1;
6364 return 0;
6365}
6366
6367static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
6368{
6369 int ret, clen;
6370
6371 s->zstream.avail_out = len;
6372 s->zstream.next_out = buf;
6373 while (s->zstream.avail_out > 0) {
6374 if (s->zstream.avail_in == 0) {
6375 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
6376 return -1;
6377 clen = qemu_get_be16(s->f);
6378 if (clen > IOBUF_SIZE)
6379 return -1;
6380 qemu_get_buffer(s->f, s->buf, clen);
6381 s->zstream.avail_in = clen;
6382 s->zstream.next_in = s->buf;
6383 }
6384 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
6385 if (ret != Z_OK && ret != Z_STREAM_END) {
6386 return -1;
6387 }
6388 }
6389 return 0;
6390}
6391
6392static void ram_decompress_close(RamDecompressState *s)
6393{
6394 inflateEnd(&s->zstream);
6395}
6396
6397static void ram_save(QEMUFile *f, void *opaque)
6398{
6399 int i;
6400 RamCompressState s1, *s = &s1;
6401 uint8_t buf[10];
ths3b46e622007-09-17 08:09:54 +00006402
bellardc88676f2006-08-06 13:36:11 +00006403 qemu_put_be32(f, phys_ram_size);
6404 if (ram_compress_open(s, f) < 0)
6405 return;
6406 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6407#if 0
6408 if (tight_savevm_enabled) {
6409 int64_t sector_num;
6410 int j;
6411
6412 /* find if the memory block is available on a virtual
6413 block device */
6414 sector_num = -1;
6415 for(j = 0; j < MAX_DISKS; j++) {
6416 if (bs_table[j]) {
ths5fafdf22007-09-16 21:08:06 +00006417 sector_num = bdrv_hash_find(bs_table[j],
bellardc88676f2006-08-06 13:36:11 +00006418 phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6419 if (sector_num >= 0)
6420 break;
6421 }
6422 }
6423 if (j == MAX_DISKS)
6424 goto normal_compress;
6425 buf[0] = 1;
6426 buf[1] = j;
6427 cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
6428 ram_compress_buf(s, buf, 10);
ths5fafdf22007-09-16 21:08:06 +00006429 } else
bellardc88676f2006-08-06 13:36:11 +00006430#endif
6431 {
6432 // normal_compress:
6433 buf[0] = 0;
6434 ram_compress_buf(s, buf, 1);
6435 ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6436 }
6437 }
6438 ram_compress_close(s);
6439}
6440
6441static int ram_load(QEMUFile *f, void *opaque, int version_id)
6442{
6443 RamDecompressState s1, *s = &s1;
6444 uint8_t buf[10];
6445 int i;
6446
6447 if (version_id == 1)
6448 return ram_load_v1(f, opaque);
6449 if (version_id != 2)
6450 return -EINVAL;
6451 if (qemu_get_be32(f) != phys_ram_size)
6452 return -EINVAL;
6453 if (ram_decompress_open(s, f) < 0)
6454 return -EINVAL;
6455 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6456 if (ram_decompress_buf(s, buf, 1) < 0) {
6457 fprintf(stderr, "Error while reading ram block header\n");
6458 goto error;
6459 }
6460 if (buf[0] == 0) {
6461 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
6462 fprintf(stderr, "Error while reading ram block address=0x%08x", i);
6463 goto error;
6464 }
ths5fafdf22007-09-16 21:08:06 +00006465 } else
bellardc88676f2006-08-06 13:36:11 +00006466#if 0
6467 if (buf[0] == 1) {
6468 int bs_index;
6469 int64_t sector_num;
6470
6471 ram_decompress_buf(s, buf + 1, 9);
6472 bs_index = buf[1];
6473 sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
6474 if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) {
6475 fprintf(stderr, "Invalid block device index %d\n", bs_index);
6476 goto error;
6477 }
ths5fafdf22007-09-16 21:08:06 +00006478 if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i,
bellardc88676f2006-08-06 13:36:11 +00006479 BDRV_HASH_BLOCK_SIZE / 512) < 0) {
ths5fafdf22007-09-16 21:08:06 +00006480 fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
bellardc88676f2006-08-06 13:36:11 +00006481 bs_index, sector_num);
6482 goto error;
6483 }
ths5fafdf22007-09-16 21:08:06 +00006484 } else
bellardc88676f2006-08-06 13:36:11 +00006485#endif
6486 {
6487 error:
6488 printf("Error block header\n");
6489 return -EINVAL;
6490 }
6491 }
6492 ram_decompress_close(s);
6493 return 0;
6494}
6495
bellard8a7ddc32004-03-31 19:00:16 +00006496/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00006497/* bottom halves (can be seen as timers which expire ASAP) */
6498
6499struct QEMUBH {
6500 QEMUBHFunc *cb;
6501 void *opaque;
6502 int scheduled;
6503 QEMUBH *next;
6504};
6505
6506static QEMUBH *first_bh = NULL;
6507
6508QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
6509{
6510 QEMUBH *bh;
6511 bh = qemu_mallocz(sizeof(QEMUBH));
6512 if (!bh)
6513 return NULL;
6514 bh->cb = cb;
6515 bh->opaque = opaque;
6516 return bh;
6517}
6518
bellard6eb57332006-08-06 09:51:25 +00006519int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00006520{
6521 QEMUBH *bh, **pbh;
bellard6eb57332006-08-06 09:51:25 +00006522 int ret;
bellard83f64092006-08-01 16:21:11 +00006523
bellard6eb57332006-08-06 09:51:25 +00006524 ret = 0;
bellard83f64092006-08-01 16:21:11 +00006525 for(;;) {
6526 pbh = &first_bh;
6527 bh = *pbh;
6528 if (!bh)
6529 break;
bellard6eb57332006-08-06 09:51:25 +00006530 ret = 1;
bellard83f64092006-08-01 16:21:11 +00006531 *pbh = bh->next;
6532 bh->scheduled = 0;
6533 bh->cb(bh->opaque);
6534 }
bellard6eb57332006-08-06 09:51:25 +00006535 return ret;
bellard83f64092006-08-01 16:21:11 +00006536}
6537
6538void qemu_bh_schedule(QEMUBH *bh)
6539{
6540 CPUState *env = cpu_single_env;
6541 if (bh->scheduled)
6542 return;
6543 bh->scheduled = 1;
6544 bh->next = first_bh;
6545 first_bh = bh;
6546
6547 /* stop the currently executing CPU to execute the BH ASAP */
6548 if (env) {
6549 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
6550 }
6551}
6552
6553void qemu_bh_cancel(QEMUBH *bh)
6554{
6555 QEMUBH **pbh;
6556 if (bh->scheduled) {
6557 pbh = &first_bh;
6558 while (*pbh != bh)
6559 pbh = &(*pbh)->next;
6560 *pbh = bh->next;
6561 bh->scheduled = 0;
6562 }
6563}
6564
6565void qemu_bh_delete(QEMUBH *bh)
6566{
6567 qemu_bh_cancel(bh);
6568 qemu_free(bh);
6569}
6570
6571/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00006572/* machine registration */
6573
6574QEMUMachine *first_machine = NULL;
6575
6576int qemu_register_machine(QEMUMachine *m)
6577{
6578 QEMUMachine **pm;
6579 pm = &first_machine;
6580 while (*pm != NULL)
6581 pm = &(*pm)->next;
6582 m->next = NULL;
6583 *pm = m;
6584 return 0;
6585}
6586
6587QEMUMachine *find_machine(const char *name)
6588{
6589 QEMUMachine *m;
6590
6591 for(m = first_machine; m != NULL; m = m->next) {
6592 if (!strcmp(m->name, name))
6593 return m;
6594 }
6595 return NULL;
6596}
6597
6598/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00006599/* main execution loop */
6600
6601void gui_update(void *opaque)
6602{
ths740733b2007-06-08 01:57:56 +00006603 DisplayState *ds = opaque;
6604 ds->dpy_refresh(ds);
6605 qemu_mod_timer(ds->gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00006606}
6607
bellard0bd48852005-11-11 00:00:47 +00006608struct vm_change_state_entry {
6609 VMChangeStateHandler *cb;
6610 void *opaque;
6611 LIST_ENTRY (vm_change_state_entry) entries;
6612};
6613
6614static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
6615
6616VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
6617 void *opaque)
6618{
6619 VMChangeStateEntry *e;
6620
6621 e = qemu_mallocz(sizeof (*e));
6622 if (!e)
6623 return NULL;
6624
6625 e->cb = cb;
6626 e->opaque = opaque;
6627 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
6628 return e;
6629}
6630
6631void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
6632{
6633 LIST_REMOVE (e, entries);
6634 qemu_free (e);
6635}
6636
6637static void vm_state_notify(int running)
6638{
6639 VMChangeStateEntry *e;
6640
6641 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
6642 e->cb(e->opaque, running);
6643 }
6644}
6645
bellard8a7ddc32004-03-31 19:00:16 +00006646/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00006647static VMStopHandler *vm_stop_cb;
6648static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00006649
6650int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
6651{
6652 vm_stop_cb = cb;
6653 vm_stop_opaque = opaque;
6654 return 0;
6655}
6656
6657void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
6658{
6659 vm_stop_cb = NULL;
6660}
6661
6662void vm_start(void)
6663{
6664 if (!vm_running) {
6665 cpu_enable_ticks();
6666 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00006667 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00006668 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00006669 }
6670}
6671
ths5fafdf22007-09-16 21:08:06 +00006672void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00006673{
6674 if (vm_running) {
6675 cpu_disable_ticks();
6676 vm_running = 0;
6677 if (reason != 0) {
6678 if (vm_stop_cb) {
6679 vm_stop_cb(vm_stop_opaque, reason);
6680 }
6681 }
bellard0bd48852005-11-11 00:00:47 +00006682 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00006683 }
6684}
6685
bellardbb0c6722004-06-20 12:37:32 +00006686/* reset/shutdown handler */
6687
6688typedef struct QEMUResetEntry {
6689 QEMUResetHandler *func;
6690 void *opaque;
6691 struct QEMUResetEntry *next;
6692} QEMUResetEntry;
6693
6694static QEMUResetEntry *first_reset_entry;
6695static int reset_requested;
6696static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00006697static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00006698
6699void qemu_register_reset(QEMUResetHandler *func, void *opaque)
6700{
6701 QEMUResetEntry **pre, *re;
6702
6703 pre = &first_reset_entry;
6704 while (*pre != NULL)
6705 pre = &(*pre)->next;
6706 re = qemu_mallocz(sizeof(QEMUResetEntry));
6707 re->func = func;
6708 re->opaque = opaque;
6709 re->next = NULL;
6710 *pre = re;
6711}
6712
ths52f61fd2006-12-22 21:20:52 +00006713static void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00006714{
6715 QEMUResetEntry *re;
6716
6717 /* reset all devices */
6718 for(re = first_reset_entry; re != NULL; re = re->next) {
6719 re->func(re->opaque);
6720 }
6721}
6722
6723void qemu_system_reset_request(void)
6724{
bellardd1beab82006-10-02 19:44:22 +00006725 if (no_reboot) {
6726 shutdown_requested = 1;
6727 } else {
6728 reset_requested = 1;
6729 }
bellard6a00d602005-11-21 23:25:50 +00006730 if (cpu_single_env)
6731 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006732}
6733
6734void qemu_system_shutdown_request(void)
6735{
6736 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00006737 if (cpu_single_env)
6738 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006739}
6740
bellard34751872005-07-02 14:31:34 +00006741void qemu_system_powerdown_request(void)
6742{
6743 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00006744 if (cpu_single_env)
6745 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00006746}
6747
bellard5905b2e2004-08-01 21:53:26 +00006748void main_loop_wait(int timeout)
bellard8a7ddc32004-03-31 19:00:16 +00006749{
thscafffd42007-02-28 21:59:44 +00006750 IOHandlerRecord *ioh;
bellarde0356492006-05-01 13:33:02 +00006751 fd_set rfds, wfds, xfds;
ths877cf882007-04-18 18:11:47 +00006752 int ret, nfds;
6753#ifdef _WIN32
6754 int ret2, i;
6755#endif
bellardfd1dff42006-02-01 21:29:26 +00006756 struct timeval tv;
bellardf3311102006-04-12 20:21:17 +00006757 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00006758
bellardf3311102006-04-12 20:21:17 +00006759
6760 /* XXX: need to suppress polling by better using win32 events */
6761 ret = 0;
6762 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
6763 ret |= pe->func(pe->opaque);
6764 }
bellard38e205a2004-04-06 19:29:17 +00006765#ifdef _WIN32
thse6b1e552007-04-18 17:56:02 +00006766 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00006767 int err;
6768 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00006769
bellarda18e5242006-06-25 17:18:27 +00006770 ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
6771 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
6772 if (w->func[ret - WAIT_OBJECT_0])
6773 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00006774
ths5fafdf22007-09-16 21:08:06 +00006775 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00006776 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00006777
thse6b1e552007-04-18 17:56:02 +00006778 /* Check if event is signaled */
6779 ret2 = WaitForSingleObject(w->events[i], 0);
6780 if(ret2 == WAIT_OBJECT_0) {
6781 if (w->func[i])
6782 w->func[i](w->opaque[i]);
6783 } else if (ret2 == WAIT_TIMEOUT) {
6784 } else {
6785 err = GetLastError();
6786 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00006787 }
6788 }
bellarda18e5242006-06-25 17:18:27 +00006789 } else if (ret == WAIT_TIMEOUT) {
6790 } else {
6791 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00006792 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00006793 }
bellardf3311102006-04-12 20:21:17 +00006794 }
bellardfd1dff42006-02-01 21:29:26 +00006795#endif
6796 /* poll any events */
6797 /* XXX: separate device handlers from system ones */
6798 nfds = -1;
6799 FD_ZERO(&rfds);
6800 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00006801 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00006802 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00006803 if (ioh->deleted)
6804 continue;
bellardfd1dff42006-02-01 21:29:26 +00006805 if (ioh->fd_read &&
6806 (!ioh->fd_read_poll ||
6807 ioh->fd_read_poll(ioh->opaque) != 0)) {
6808 FD_SET(ioh->fd, &rfds);
6809 if (ioh->fd > nfds)
6810 nfds = ioh->fd;
6811 }
6812 if (ioh->fd_write) {
6813 FD_SET(ioh->fd, &wfds);
6814 if (ioh->fd > nfds)
6815 nfds = ioh->fd;
6816 }
6817 }
ths3b46e622007-09-17 08:09:54 +00006818
bellardfd1dff42006-02-01 21:29:26 +00006819 tv.tv_sec = 0;
6820#ifdef _WIN32
6821 tv.tv_usec = 0;
bellard38e205a2004-04-06 19:29:17 +00006822#else
bellardfd1dff42006-02-01 21:29:26 +00006823 tv.tv_usec = timeout * 1000;
6824#endif
bellarde0356492006-05-01 13:33:02 +00006825#if defined(CONFIG_SLIRP)
6826 if (slirp_inited) {
6827 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
6828 }
6829#endif
6830 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00006831 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00006832 IOHandlerRecord **pioh;
6833
6834 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00006835 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00006836 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00006837 }
ths6ab43fd2007-08-25 01:34:19 +00006838 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00006839 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00006840 }
6841 }
thscafffd42007-02-28 21:59:44 +00006842
6843 /* remove deleted IO handlers */
6844 pioh = &first_io_handler;
6845 while (*pioh) {
6846 ioh = *pioh;
6847 if (ioh->deleted) {
6848 *pioh = ioh->next;
6849 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00006850 } else
thscafffd42007-02-28 21:59:44 +00006851 pioh = &ioh->next;
6852 }
bellardfd1dff42006-02-01 21:29:26 +00006853 }
bellarde0356492006-05-01 13:33:02 +00006854#if defined(CONFIG_SLIRP)
6855 if (slirp_inited) {
6856 if (ret < 0) {
6857 FD_ZERO(&rfds);
6858 FD_ZERO(&wfds);
6859 FD_ZERO(&xfds);
6860 }
6861 slirp_select_poll(&rfds, &wfds, &xfds);
6862 }
6863#endif
bellard83f64092006-08-01 16:21:11 +00006864 qemu_aio_poll();
bellardc20709a2004-04-21 23:27:19 +00006865
bellardfd1dff42006-02-01 21:29:26 +00006866 if (vm_running) {
ths5fafdf22007-09-16 21:08:06 +00006867 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
bellardfd1dff42006-02-01 21:29:26 +00006868 qemu_get_clock(vm_clock));
6869 /* run dma transfers, if any */
6870 DMA_run();
6871 }
pbrook423f0742007-05-23 00:06:54 +00006872
bellardfd1dff42006-02-01 21:29:26 +00006873 /* real time timers */
ths5fafdf22007-09-16 21:08:06 +00006874 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
bellardfd1dff42006-02-01 21:29:26 +00006875 qemu_get_clock(rt_clock));
pbrook423f0742007-05-23 00:06:54 +00006876
6877 /* Check bottom-halves last in case any of the earlier events triggered
6878 them. */
6879 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00006880
bellard5905b2e2004-08-01 21:53:26 +00006881}
6882
bellard6a00d602005-11-21 23:25:50 +00006883static CPUState *cur_cpu;
6884
bellard5905b2e2004-08-01 21:53:26 +00006885int main_loop(void)
6886{
6887 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00006888#ifdef CONFIG_PROFILER
6889 int64_t ti;
6890#endif
bellard6a00d602005-11-21 23:25:50 +00006891 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00006892
bellard6a00d602005-11-21 23:25:50 +00006893 cur_cpu = first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00006894 for(;;) {
6895 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00006896
6897 env = cur_cpu;
6898 for(;;) {
6899 /* get next cpu */
6900 env = env->next_cpu;
6901 if (!env)
6902 env = first_cpu;
bellard89bfc102006-02-08 22:46:31 +00006903#ifdef CONFIG_PROFILER
6904 ti = profile_getclock();
6905#endif
bellard6a00d602005-11-21 23:25:50 +00006906 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00006907#ifdef CONFIG_PROFILER
6908 qemu_time += profile_getclock() - ti;
6909#endif
pbrookbd967e02007-03-11 18:54:57 +00006910 if (ret == EXCP_HLT) {
6911 /* Give the next CPU a chance to run. */
6912 cur_cpu = env;
6913 continue;
6914 }
bellard15a76442005-11-23 21:01:03 +00006915 if (ret != EXCP_HALTED)
6916 break;
6917 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00006918 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00006919 break;
bellard15a76442005-11-23 21:01:03 +00006920 }
6921 cur_cpu = env;
6922
bellard5905b2e2004-08-01 21:53:26 +00006923 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00006924 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00006925 break;
6926 }
6927 if (reset_requested) {
6928 reset_requested = 0;
6929 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00006930 ret = EXCP_INTERRUPT;
6931 }
6932 if (powerdown_requested) {
6933 powerdown_requested = 0;
6934 qemu_system_powerdown();
6935 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00006936 }
6937 if (ret == EXCP_DEBUG) {
6938 vm_stop(EXCP_DEBUG);
6939 }
pbrookbd967e02007-03-11 18:54:57 +00006940 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00006941 /* XXX: use timeout computed from timers */
pbrookbd967e02007-03-11 18:54:57 +00006942 if (ret == EXCP_HALTED)
bellard5905b2e2004-08-01 21:53:26 +00006943 timeout = 10;
6944 else
6945 timeout = 0;
6946 } else {
6947 timeout = 10;
6948 }
bellard89bfc102006-02-08 22:46:31 +00006949#ifdef CONFIG_PROFILER
6950 ti = profile_getclock();
6951#endif
bellard5905b2e2004-08-01 21:53:26 +00006952 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00006953#ifdef CONFIG_PROFILER
6954 dev_time += profile_getclock() - ti;
6955#endif
bellardb4608c02003-06-27 17:34:32 +00006956 }
bellard34865132003-10-05 14:28:56 +00006957 cpu_disable_ticks();
6958 return ret;
bellardb4608c02003-06-27 17:34:32 +00006959}
6960
ths15f82202007-06-29 23:26:08 +00006961static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00006962{
bellard84f2e8e2007-02-05 20:21:32 +00006963 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00006964 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00006965 "\n"
bellarda20dd502003-09-30 21:07:02 +00006966 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00006967 "\n"
bellarda20dd502003-09-30 21:07:02 +00006968 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00006969 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00006970 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00006971 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00006972 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
6973 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00006974 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
balrog3e3d5812007-04-30 02:09:25 +00006975 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00006976 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00006977 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00006978 "-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 +00006979 "-snapshot write to temporary files instead of disk image files\n"
6980#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00006981 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00006982 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00006983 "-no-quit disable SDL window close capability\n"
6984#endif
bellard52ca8d62006-06-14 16:03:05 +00006985#ifdef TARGET_I386
6986 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
6987#endif
bellarda00bad72004-05-22 21:39:06 +00006988 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00006989 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00006990 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00006991 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00006992#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00006993 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00006994#endif
bellard1d14ffa2005-10-30 18:58:22 +00006995#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00006996 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00006997 "-soundhw c1,... enable audio support\n"
6998 " and only specified sound cards (comma separated list)\n"
6999 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00007000 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00007001#endif
bellard89980282004-06-03 14:04:03 +00007002 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00007003 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00007004#ifdef TARGET_I386
7005 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7006#endif
bellardb389dbf2005-11-06 16:49:55 +00007007 "-usb enable the USB driver (will be the default soon)\n"
7008 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00007009#if defined(TARGET_PPC) || defined(TARGET_SPARC)
7010 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00007011#endif
thsc35734b2007-03-19 15:17:08 +00007012 "-name string set the name of the guest\n"
bellarda20dd502003-09-30 21:07:02 +00007013 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007014 "Network options:\n"
pbrooka41b2ff2006-02-05 04:14:41 +00007015 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007016 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00007017#ifdef CONFIG_SLIRP
pbrook115defd2006-04-16 11:06:58 +00007018 "-net user[,vlan=n][,hostname=host]\n"
7019 " connect the user mode network stack to VLAN 'n' and send\n"
7020 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00007021#endif
bellard7fb843f2006-02-01 23:06:55 +00007022#ifdef _WIN32
7023 "-net tap[,vlan=n],ifname=name\n"
7024 " connect the host TAP network interface to VLAN 'n'\n"
7025#else
bellard7c9d8e02005-11-15 22:16:05 +00007026 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n"
7027 " connect the host TAP network interface to VLAN 'n' and use\n"
7028 " the network script 'file' (default=%s);\n"
ths6a1cbf62007-02-02 00:37:56 +00007029 " use 'script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00007030 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00007031#endif
bellard6a00d602005-11-21 23:25:50 +00007032 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007033 " connect the vlan 'n' to another VLAN using a socket connection\n"
bellard3d830452005-12-18 16:36:49 +00007034 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7035 " connect the vlan 'n' to multicast maddr and port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007036 "-net none use it alone to have zero network devices; if no -net option\n"
7037 " is provided, the default is '-net nic -net user'\n"
7038 "\n"
7039#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00007040 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00007041 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00007042#ifndef _WIN32
7043 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00007044#endif
bellard9bf05442004-08-25 22:12:49 +00007045 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007046 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00007047#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007048 "\n"
7049 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00007050 "-kernel bzImage use 'bzImage' as kernel image\n"
7051 "-append cmdline use 'cmdline' as kernel command line\n"
7052 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00007053 "\n"
bellard330d0412003-07-26 18:11:40 +00007054 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00007055 "-monitor dev redirect the monitor to char device 'dev'\n"
7056 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00007057 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00007058 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00007059 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00007060 "-s wait gdb connection to port\n"
7061 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00007062 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00007063 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7064 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00007065 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00007066#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00007067 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00007068 "-no-kqemu disable KQEMU kernel module usage\n"
7069#endif
bellard77fef8c2004-02-16 22:05:46 +00007070#ifdef USE_CODE_COPY
7071 "-no-code-copy disable code copy acceleration\n"
7072#endif
bellardbb0c6722004-06-20 12:37:32 +00007073#ifdef TARGET_I386
bellard1bfe8562004-07-08 21:17:50 +00007074 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7075 " (default is CL-GD5446 PCI VGA)\n"
bellard6515b202006-05-03 22:02:44 +00007076 "-no-acpi disable ACPI\n"
bellardbb0c6722004-06-20 12:37:32 +00007077#endif
bellardd1beab82006-10-02 19:44:22 +00007078 "-no-reboot exit instead of rebooting\n"
bellardd63d3072004-10-03 13:29:03 +00007079 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00007080 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00007081#ifndef _WIN32
7082 "-daemonize daemonize QEMU after initializing\n"
7083#endif
ths9ae02552007-01-05 17:39:04 +00007084 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00007085#ifdef TARGET_SPARC
7086 "-prom-env variable=value set OpenBIOS nvram variables\n"
7087#endif
thsf3dcfad2007-08-24 01:26:02 +00007088 "-clock force the use of the given methods for timer alarm.\n"
7089 " To see what timers are available use -clock help\n"
bellard0824d6f2003-06-24 13:42:40 +00007090 "\n"
bellard82c643f2004-07-14 17:28:13 +00007091 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00007092 "ctrl-alt-f toggle full screen\n"
7093 "ctrl-alt-n switch to virtual console 'n'\n"
7094 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00007095 "\n"
7096 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7097 ,
bellard0db63472003-10-27 21:37:46 +00007098 "qemu",
bellarda00bad72004-05-22 21:39:06 +00007099 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00007100#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00007101 DEFAULT_NETWORK_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00007102#endif
bellard6e44ba72004-01-18 21:56:49 +00007103 DEFAULT_GDBSTUB_PORT,
7104 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00007105 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00007106}
7107
bellardcd6f1162004-05-13 22:02:20 +00007108#define HAS_ARG 0x0001
7109
7110enum {
7111 QEMU_OPTION_h,
7112
bellardcc1daa42005-06-05 14:49:17 +00007113 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00007114 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00007115 QEMU_OPTION_fda,
7116 QEMU_OPTION_fdb,
7117 QEMU_OPTION_hda,
7118 QEMU_OPTION_hdb,
7119 QEMU_OPTION_hdc,
7120 QEMU_OPTION_hdd,
7121 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00007122 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00007123 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00007124 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00007125 QEMU_OPTION_boot,
7126 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00007127#ifdef TARGET_I386
7128 QEMU_OPTION_no_fd_bootchk,
7129#endif
bellardcd6f1162004-05-13 22:02:20 +00007130 QEMU_OPTION_m,
7131 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00007132 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00007133#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007134 QEMU_OPTION_audio_help,
7135 QEMU_OPTION_soundhw,
7136#endif
bellardcd6f1162004-05-13 22:02:20 +00007137
bellard7c9d8e02005-11-15 22:16:05 +00007138 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00007139 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00007140 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00007141 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00007142 QEMU_OPTION_redir,
bellardcd6f1162004-05-13 22:02:20 +00007143
7144 QEMU_OPTION_kernel,
7145 QEMU_OPTION_append,
7146 QEMU_OPTION_initrd,
7147
7148 QEMU_OPTION_S,
7149 QEMU_OPTION_s,
7150 QEMU_OPTION_p,
7151 QEMU_OPTION_d,
7152 QEMU_OPTION_hdachs,
7153 QEMU_OPTION_L,
7154 QEMU_OPTION_no_code_copy,
bellard3d11d0e2004-12-12 16:56:30 +00007155 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00007156 QEMU_OPTION_localtime,
bellard1f042752004-06-05 13:46:47 +00007157 QEMU_OPTION_cirrusvga,
thsd34cab92007-04-02 01:10:46 +00007158 QEMU_OPTION_vmsvga,
bellarde9b137c2004-06-21 16:46:10 +00007159 QEMU_OPTION_g,
bellard1bfe8562004-07-08 21:17:50 +00007160 QEMU_OPTION_std_vga,
ths20d8a3e2007-02-18 17:04:49 +00007161 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00007162 QEMU_OPTION_monitor,
7163 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00007164 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00007165 QEMU_OPTION_loadvm,
7166 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00007167 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00007168 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00007169 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00007170 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00007171 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00007172 QEMU_OPTION_kernel_kqemu,
bellarda09db212005-04-30 16:10:35 +00007173 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00007174 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00007175 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00007176 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00007177 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00007178 QEMU_OPTION_no_acpi,
bellardd1beab82006-10-02 19:44:22 +00007179 QEMU_OPTION_no_reboot,
balrog9467cd42007-05-01 01:34:14 +00007180 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00007181 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00007182 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00007183 QEMU_OPTION_semihosting,
7184 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00007185 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00007186 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00007187 QEMU_OPTION_clock,
bellardcd6f1162004-05-13 22:02:20 +00007188};
7189
7190typedef struct QEMUOption {
7191 const char *name;
7192 int flags;
7193 int index;
7194} QEMUOption;
7195
7196const QEMUOption qemu_options[] = {
7197 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00007198 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00007199
bellardcc1daa42005-06-05 14:49:17 +00007200 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00007201 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00007202 { "fda", HAS_ARG, QEMU_OPTION_fda },
7203 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
7204 { "hda", HAS_ARG, QEMU_OPTION_hda },
7205 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
7206 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
7207 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
7208 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00007209 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00007210 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00007211 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00007212 { "boot", HAS_ARG, QEMU_OPTION_boot },
7213 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00007214#ifdef TARGET_I386
7215 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
7216#endif
bellardcd6f1162004-05-13 22:02:20 +00007217 { "m", HAS_ARG, QEMU_OPTION_m },
7218 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00007219 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00007220 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00007221#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007222 { "audio-help", 0, QEMU_OPTION_audio_help },
7223 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
7224#endif
bellardcd6f1162004-05-13 22:02:20 +00007225
bellard7c9d8e02005-11-15 22:16:05 +00007226 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00007227#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00007228 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00007229 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00007230#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007231 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00007232#endif
bellard9bf05442004-08-25 22:12:49 +00007233 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00007234#endif
bellardcd6f1162004-05-13 22:02:20 +00007235
7236 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
7237 { "append", HAS_ARG, QEMU_OPTION_append },
7238 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
7239
7240 { "S", 0, QEMU_OPTION_S },
7241 { "s", 0, QEMU_OPTION_s },
7242 { "p", HAS_ARG, QEMU_OPTION_p },
7243 { "d", HAS_ARG, QEMU_OPTION_d },
7244 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
7245 { "L", HAS_ARG, QEMU_OPTION_L },
7246 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
bellardd993e022005-02-10 22:00:06 +00007247#ifdef USE_KQEMU
7248 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00007249 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00007250#endif
bellard6f7e9ae2005-03-13 09:43:36 +00007251#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00007252 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00007253#endif
bellardee22c2f2004-06-03 12:49:50 +00007254 { "localtime", 0, QEMU_OPTION_localtime },
bellard1bfe8562004-07-08 21:17:50 +00007255 { "std-vga", 0, QEMU_OPTION_std_vga },
balrog8b6e0722007-06-22 08:23:44 +00007256 { "echr", HAS_ARG, QEMU_OPTION_echr },
7257 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
7258 { "serial", HAS_ARG, QEMU_OPTION_serial },
7259 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00007260 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
7261 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00007262#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007263 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00007264 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00007265 { "no-quit", 0, QEMU_OPTION_no_quit },
7266#endif
bellardf7cce892004-12-08 22:21:25 +00007267 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00007268 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00007269 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00007270 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00007271 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
ths96d30e42007-01-07 20:42:14 +00007272
bellard1f042752004-06-05 13:46:47 +00007273 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00007274 { "usb", 0, QEMU_OPTION_usb },
bellard1f042752004-06-05 13:46:47 +00007275 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
thsd34cab92007-04-02 01:10:46 +00007276 { "vmwarevga", 0, QEMU_OPTION_vmsvga },
bellard6515b202006-05-03 22:02:44 +00007277 { "no-acpi", 0, QEMU_OPTION_no_acpi },
bellardd1beab82006-10-02 19:44:22 +00007278 { "no-reboot", 0, QEMU_OPTION_no_reboot },
balrog9467cd42007-05-01 01:34:14 +00007279 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00007280 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00007281 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00007282#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00007283 { "semihosting", 0, QEMU_OPTION_semihosting },
7284#endif
thsc35734b2007-03-19 15:17:08 +00007285 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir166508602007-05-01 14:16:52 +00007286#if defined(TARGET_SPARC)
7287 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
7288#endif
balrog2b8f2d42007-07-27 22:08:46 +00007289#if defined(TARGET_ARM)
7290 { "old-param", 0, QEMU_OPTION_old_param },
7291#endif
thsf3dcfad2007-08-24 01:26:02 +00007292 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellardcd6f1162004-05-13 22:02:20 +00007293 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00007294};
7295
bellard77fef8c2004-02-16 22:05:46 +00007296#if defined (TARGET_I386) && defined(USE_CODE_COPY)
7297
7298/* this stack is only used during signal handling */
7299#define SIGNAL_STACK_SIZE 32768
7300
7301static uint8_t *signal_stack;
7302
7303#endif
7304
bellard5905b2e2004-08-01 21:53:26 +00007305/* password input */
7306
balrog2bac6012007-04-30 01:34:31 +00007307int qemu_key_check(BlockDriverState *bs, const char *name)
7308{
7309 char password[256];
7310 int i;
7311
7312 if (!bdrv_is_encrypted(bs))
7313 return 0;
7314
7315 term_printf("%s is encrypted.\n", name);
7316 for(i = 0; i < 3; i++) {
7317 monitor_readline("Password: ", 1, password, sizeof(password));
7318 if (bdrv_set_key(bs, password) == 0)
7319 return 0;
7320 term_printf("invalid password\n");
7321 }
7322 return -EPERM;
7323}
7324
bellard5905b2e2004-08-01 21:53:26 +00007325static BlockDriverState *get_bdrv(int index)
7326{
7327 BlockDriverState *bs;
7328
7329 if (index < 4) {
7330 bs = bs_table[index];
7331 } else if (index < 6) {
7332 bs = fd_table[index - 4];
7333 } else {
7334 bs = NULL;
7335 }
7336 return bs;
7337}
7338
7339static void read_passwords(void)
7340{
7341 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00007342 int i;
bellard5905b2e2004-08-01 21:53:26 +00007343
7344 for(i = 0; i < 6; i++) {
7345 bs = get_bdrv(i);
balrog2bac6012007-04-30 01:34:31 +00007346 if (bs)
7347 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00007348 }
7349}
7350
bellardcc1daa42005-06-05 14:49:17 +00007351/* XXX: currently we cannot use simultaneously different CPUs */
7352void register_machines(void)
7353{
7354#if defined(TARGET_I386)
7355 qemu_register_machine(&pc_machine);
bellard3dbbdc22005-11-06 18:20:37 +00007356 qemu_register_machine(&isapc_machine);
bellardcc1daa42005-06-05 14:49:17 +00007357#elif defined(TARGET_PPC)
7358 qemu_register_machine(&heathrow_machine);
7359 qemu_register_machine(&core99_machine);
7360 qemu_register_machine(&prep_machine);
j_mayer1a6c0882007-04-24 07:40:49 +00007361 qemu_register_machine(&ref405ep_machine);
7362 qemu_register_machine(&taihu_machine);
bellard6af0bf92005-07-02 14:58:51 +00007363#elif defined(TARGET_MIPS)
7364 qemu_register_machine(&mips_machine);
ths5856de82007-01-15 23:58:11 +00007365 qemu_register_machine(&mips_malta_machine);
thsad6fe1d2007-04-16 17:23:27 +00007366 qemu_register_machine(&mips_pica61_machine);
bellardcc1daa42005-06-05 14:49:17 +00007367#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +00007368#ifdef TARGET_SPARC64
7369 qemu_register_machine(&sun4u_machine);
7370#else
blueswir136cd9212007-04-01 15:44:43 +00007371 qemu_register_machine(&ss5_machine);
blueswir1e0353fe2007-04-01 15:55:28 +00007372 qemu_register_machine(&ss10_machine);
bellardcc1daa42005-06-05 14:49:17 +00007373#endif
bellardb5ff1b32005-11-26 10:38:39 +00007374#elif defined(TARGET_ARM)
pbrook3371d272007-03-08 03:04:12 +00007375 qemu_register_machine(&integratorcp_machine);
pbrookcdbdb642006-04-09 01:32:52 +00007376 qemu_register_machine(&versatilepb_machine);
pbrook16406952006-04-27 23:15:07 +00007377 qemu_register_machine(&versatileab_machine);
pbrooke69954b2006-09-23 17:40:58 +00007378 qemu_register_machine(&realview_machine);
balrogb00052e2007-04-30 02:22:06 +00007379 qemu_register_machine(&akitapda_machine);
7380 qemu_register_machine(&spitzpda_machine);
7381 qemu_register_machine(&borzoipda_machine);
7382 qemu_register_machine(&terrierpda_machine);
balrogc3d26892007-07-29 17:57:26 +00007383 qemu_register_machine(&palmte_machine);
bellard27c7ca72006-04-27 21:32:09 +00007384#elif defined(TARGET_SH4)
7385 qemu_register_machine(&shix_machine);
j_mayereddf68a2007-04-05 07:22:49 +00007386#elif defined(TARGET_ALPHA)
7387 /* XXX: TODO */
pbrook06338792007-05-23 19:58:11 +00007388#elif defined(TARGET_M68K)
pbrook20dcee92007-06-03 11:13:39 +00007389 qemu_register_machine(&mcf5208evb_machine);
pbrook06338792007-05-23 19:58:11 +00007390 qemu_register_machine(&an5206_machine);
bellardb5ff1b32005-11-26 10:38:39 +00007391#else
7392#error unsupported CPU
bellard34751872005-07-02 14:31:34 +00007393#endif
bellardcc1daa42005-06-05 14:49:17 +00007394}
7395
bellard1d14ffa2005-10-30 18:58:22 +00007396#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00007397struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00007398#ifdef HAS_AUDIO_CHOICE
bellardfd06c372006-04-24 21:58:30 +00007399#ifdef TARGET_I386
7400 {
7401 "pcspk",
7402 "PC speaker",
7403 0,
7404 1,
7405 { .init_isa = pcspk_audio_init }
7406 },
7407#endif
bellard6a36d842005-12-18 20:34:32 +00007408 {
7409 "sb16",
7410 "Creative Sound Blaster 16",
7411 0,
7412 1,
7413 { .init_isa = SB16_init }
7414 },
7415
7416#ifdef CONFIG_ADLIB
7417 {
7418 "adlib",
7419#ifdef HAS_YMF262
7420 "Yamaha YMF262 (OPL3)",
7421#else
7422 "Yamaha YM3812 (OPL2)",
7423#endif
7424 0,
7425 1,
7426 { .init_isa = Adlib_init }
7427 },
7428#endif
7429
7430#ifdef CONFIG_GUS
7431 {
7432 "gus",
7433 "Gravis Ultrasound GF1",
7434 0,
7435 1,
7436 { .init_isa = GUS_init }
7437 },
7438#endif
7439
7440 {
7441 "es1370",
7442 "ENSONIQ AudioPCI ES1370",
7443 0,
7444 0,
7445 { .init_pci = es1370_init }
7446 },
balrogb00052e2007-04-30 02:22:06 +00007447#endif
bellard6a36d842005-12-18 20:34:32 +00007448
7449 { NULL, NULL, 0, 0, { NULL } }
7450};
7451
bellard1d14ffa2005-10-30 18:58:22 +00007452static void select_soundhw (const char *optarg)
7453{
bellard6a36d842005-12-18 20:34:32 +00007454 struct soundhw *c;
7455
bellard1d14ffa2005-10-30 18:58:22 +00007456 if (*optarg == '?') {
7457 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00007458
bellard1d14ffa2005-10-30 18:58:22 +00007459 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00007460 for (c = soundhw; c->name; ++c) {
7461 printf ("%-11s %s\n", c->name, c->descr);
7462 }
7463 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00007464 exit (*optarg != '?');
7465 }
7466 else {
bellard6a36d842005-12-18 20:34:32 +00007467 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00007468 const char *p;
7469 char *e;
7470 int bad_card = 0;
7471
bellard6a36d842005-12-18 20:34:32 +00007472 if (!strcmp (optarg, "all")) {
7473 for (c = soundhw; c->name; ++c) {
7474 c->enabled = 1;
7475 }
7476 return;
7477 }
bellard1d14ffa2005-10-30 18:58:22 +00007478
bellard6a36d842005-12-18 20:34:32 +00007479 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00007480 while (*p) {
7481 e = strchr (p, ',');
7482 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00007483
7484 for (c = soundhw; c->name; ++c) {
7485 if (!strncmp (c->name, p, l)) {
7486 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00007487 break;
7488 }
7489 }
bellard6a36d842005-12-18 20:34:32 +00007490
7491 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00007492 if (l > 80) {
7493 fprintf (stderr,
7494 "Unknown sound card name (too big to show)\n");
7495 }
7496 else {
7497 fprintf (stderr, "Unknown sound card name `%.*s'\n",
7498 (int) l, p);
7499 }
7500 bad_card = 1;
7501 }
7502 p += l + (e != NULL);
7503 }
7504
7505 if (bad_card)
7506 goto show_valid_cards;
7507 }
7508}
7509#endif
7510
bellard3587d7e2006-06-26 20:03:44 +00007511#ifdef _WIN32
7512static BOOL WINAPI qemu_ctrl_handler(DWORD type)
7513{
7514 exit(STATUS_CONTROL_C_EXIT);
7515 return TRUE;
7516}
7517#endif
7518
bellard7c9d8e02005-11-15 22:16:05 +00007519#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00007520
bellard0824d6f2003-06-24 13:42:40 +00007521int main(int argc, char **argv)
7522{
bellard67b915a2004-03-31 23:37:16 +00007523#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00007524 int use_gdbstub;
7525 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00007526#endif
j_mayer86f55662007-04-24 06:52:59 +00007527 int i, cdrom_index, pflash_index;
bellard1ccde1c2004-02-06 19:46:14 +00007528 int snapshot, linux_boot;
bellard7f7f9872003-10-30 01:11:23 +00007529 const char *initrd_filename;
ths96d30e42007-01-07 20:42:14 +00007530 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
j_mayer86f55662007-04-24 06:52:59 +00007531 const char *pflash_filename[MAX_PFLASH];
pbrooka1bb27b2007-04-06 16:49:48 +00007532 const char *sd_filename;
balrog3e3d5812007-04-30 02:09:25 +00007533 const char *mtd_filename;
bellarda20dd502003-09-30 21:07:02 +00007534 const char *kernel_filename, *kernel_cmdline;
bellard313aa562003-08-10 21:52:11 +00007535 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00007536 int cyls, heads, secs, translation;
bellard7c9d8e02005-11-15 22:16:05 +00007537 char net_clients[MAX_NET_CLIENTS][256];
7538 int nb_net_clients;
bellardcd6f1162004-05-13 22:02:20 +00007539 int optind;
7540 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00007541 CharDriverState *monitor_hd;
7542 char monitor_device[128];
bellard8d11df92004-08-24 21:13:40 +00007543 char serial_devices[MAX_SERIAL_PORTS][128];
7544 int serial_device_index;
bellard6508fe52005-01-15 12:02:56 +00007545 char parallel_devices[MAX_PARALLEL_PORTS][128];
7546 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00007547 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00007548 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00007549 const char *cpu_model;
pbrook0d92ed32006-05-21 16:30:15 +00007550 char usb_devices[MAX_USB_CMDLINE][128];
bellarda594cfb2005-11-06 16:13:29 +00007551 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00007552 int fds[2];
ths93815bc2007-03-19 15:58:31 +00007553 const char *pid_file = NULL;
blueswir1833c7172007-05-27 19:36:43 +00007554 VLANState *vlan;
bellard0bd48852005-11-11 00:00:47 +00007555
7556 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00007557#ifndef _WIN32
7558 {
7559 struct sigaction act;
7560 sigfillset(&act.sa_mask);
7561 act.sa_flags = 0;
7562 act.sa_handler = SIG_IGN;
7563 sigaction(SIGPIPE, &act, NULL);
7564 }
bellard3587d7e2006-06-26 20:03:44 +00007565#else
7566 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00007567 /* Note: cpu_interrupt() is currently not SMP safe, so we force
7568 QEMU to run on a single CPU */
7569 {
7570 HANDLE h;
7571 DWORD mask, smask;
7572 int i;
7573 h = GetCurrentProcess();
7574 if (GetProcessAffinityMask(h, &mask, &smask)) {
7575 for(i = 0; i < 32; i++) {
7576 if (mask & (1 << i))
7577 break;
7578 }
7579 if (i != 32) {
7580 mask = 1 << i;
7581 SetProcessAffinityMask(h, mask);
7582 }
7583 }
7584 }
bellard67b915a2004-03-31 23:37:16 +00007585#endif
bellardbe995c22006-06-25 16:25:21 +00007586
bellardcc1daa42005-06-05 14:49:17 +00007587 register_machines();
7588 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00007589 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00007590 initrd_filename = NULL;
bellardc45886d2004-01-05 00:02:06 +00007591 for(i = 0; i < MAX_FD; i++)
7592 fd_filename[i] = NULL;
ths96d30e42007-01-07 20:42:14 +00007593 for(i = 0; i < MAX_DISKS; i++)
7594 hd_filename[i] = NULL;
j_mayer86f55662007-04-24 06:52:59 +00007595 for(i = 0; i < MAX_PFLASH; i++)
7596 pflash_filename[i] = NULL;
7597 pflash_index = 0;
pbrooka1bb27b2007-04-06 16:49:48 +00007598 sd_filename = NULL;
balrog3e3d5812007-04-30 02:09:25 +00007599 mtd_filename = NULL;
bellarda00bad72004-05-22 21:39:06 +00007600 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
bellard313aa562003-08-10 21:52:11 +00007601 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00007602#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00007603 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00007604 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00007605#endif
bellard33e39632003-07-06 17:15:21 +00007606 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00007607 nographic = 0;
7608 kernel_filename = NULL;
7609 kernel_cmdline = "";
bellardcc1daa42005-06-05 14:49:17 +00007610#ifdef TARGET_PPC
7611 cdrom_index = 1;
7612#else
7613 cdrom_index = 2;
7614#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007615 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00007616 translation = BIOS_ATA_TRANSLATION_AUTO;
bellard82c643f2004-07-14 17:28:13 +00007617 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
bellardc4b1fcc2004-03-14 21:44:30 +00007618
bellard8d11df92004-08-24 21:13:40 +00007619 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
7620 for(i = 1; i < MAX_SERIAL_PORTS; i++)
7621 serial_devices[i][0] = '\0';
7622 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00007623
bellard6508fe52005-01-15 12:02:56 +00007624 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
7625 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
7626 parallel_devices[i][0] = '\0';
7627 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00007628
bellarda594cfb2005-11-06 16:13:29 +00007629 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00007630
bellard7c9d8e02005-11-15 22:16:05 +00007631 nb_net_clients = 0;
7632
7633 nb_nics = 0;
bellard702c6512004-04-02 21:21:32 +00007634 /* default mac address of the first network interface */
ths3b46e622007-09-17 08:09:54 +00007635
bellardcd6f1162004-05-13 22:02:20 +00007636 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00007637 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00007638 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00007639 break;
bellardcd6f1162004-05-13 22:02:20 +00007640 r = argv[optind];
7641 if (r[0] != '-') {
ths96d30e42007-01-07 20:42:14 +00007642 hd_filename[0] = argv[optind++];
bellardcd6f1162004-05-13 22:02:20 +00007643 } else {
7644 const QEMUOption *popt;
7645
7646 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00007647 /* Treat --foo the same as -foo. */
7648 if (r[1] == '-')
7649 r++;
bellardcd6f1162004-05-13 22:02:20 +00007650 popt = qemu_options;
7651 for(;;) {
7652 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00007653 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00007654 argv[0], r);
7655 exit(1);
7656 }
7657 if (!strcmp(popt->name, r + 1))
7658 break;
7659 popt++;
7660 }
7661 if (popt->flags & HAS_ARG) {
7662 if (optind >= argc) {
7663 fprintf(stderr, "%s: option '%s' requires an argument\n",
7664 argv[0], r);
7665 exit(1);
7666 }
7667 optarg = argv[optind++];
7668 } else {
7669 optarg = NULL;
7670 }
7671
7672 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00007673 case QEMU_OPTION_M:
7674 machine = find_machine(optarg);
7675 if (!machine) {
7676 QEMUMachine *m;
7677 printf("Supported machines are:\n");
7678 for(m = first_machine; m != NULL; m = m->next) {
7679 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00007680 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00007681 m == first_machine ? " (default)" : "");
7682 }
ths15f82202007-06-29 23:26:08 +00007683 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00007684 }
7685 break;
j_mayer94fc95c2007-03-05 19:44:02 +00007686 case QEMU_OPTION_cpu:
7687 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00007688 if (*optarg == '?') {
j_mayer94fc95c2007-03-05 19:44:02 +00007689#if defined(TARGET_PPC)
7690 ppc_cpu_list(stdout, &fprintf);
pbrook5adb4832007-03-08 03:15:18 +00007691#elif defined(TARGET_ARM)
7692 arm_cpu_list();
ths33d68b52007-03-18 00:30:29 +00007693#elif defined(TARGET_MIPS)
7694 mips_cpu_list(stdout, &fprintf);
blueswir162724a32007-03-25 07:55:52 +00007695#elif defined(TARGET_SPARC)
7696 sparc_cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00007697#endif
ths15f82202007-06-29 23:26:08 +00007698 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00007699 } else {
7700 cpu_model = optarg;
7701 }
7702 break;
bellardcd6f1162004-05-13 22:02:20 +00007703 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00007704 initrd_filename = optarg;
7705 break;
bellardcd6f1162004-05-13 22:02:20 +00007706 case QEMU_OPTION_hda:
bellardcd6f1162004-05-13 22:02:20 +00007707 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00007708 case QEMU_OPTION_hdc:
7709 case QEMU_OPTION_hdd:
7710 {
7711 int hd_index;
7712 hd_index = popt->index - QEMU_OPTION_hda;
ths96d30e42007-01-07 20:42:14 +00007713 hd_filename[hd_index] = optarg;
7714 if (hd_index == cdrom_index)
7715 cdrom_index = -1;
bellardcc1daa42005-06-05 14:49:17 +00007716 }
bellardfc01f7e2003-06-30 10:03:06 +00007717 break;
balrog3e3d5812007-04-30 02:09:25 +00007718 case QEMU_OPTION_mtdblock:
7719 mtd_filename = optarg;
7720 break;
pbrooka1bb27b2007-04-06 16:49:48 +00007721 case QEMU_OPTION_sd:
7722 sd_filename = optarg;
7723 break;
j_mayer86f55662007-04-24 06:52:59 +00007724 case QEMU_OPTION_pflash:
7725 if (pflash_index >= MAX_PFLASH) {
7726 fprintf(stderr, "qemu: too many parallel flash images\n");
7727 exit(1);
7728 }
7729 pflash_filename[pflash_index++] = optarg;
7730 break;
bellardcd6f1162004-05-13 22:02:20 +00007731 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00007732 snapshot = 1;
7733 break;
bellardcd6f1162004-05-13 22:02:20 +00007734 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00007735 {
bellard330d0412003-07-26 18:11:40 +00007736 const char *p;
7737 p = optarg;
7738 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007739 if (cyls < 1 || cyls > 16383)
7740 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00007741 if (*p != ',')
7742 goto chs_fail;
7743 p++;
7744 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007745 if (heads < 1 || heads > 16)
7746 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00007747 if (*p != ',')
7748 goto chs_fail;
7749 p++;
7750 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00007751 if (secs < 1 || secs > 63)
7752 goto chs_fail;
7753 if (*p == ',') {
7754 p++;
7755 if (!strcmp(p, "none"))
7756 translation = BIOS_ATA_TRANSLATION_NONE;
7757 else if (!strcmp(p, "lba"))
7758 translation = BIOS_ATA_TRANSLATION_LBA;
7759 else if (!strcmp(p, "auto"))
7760 translation = BIOS_ATA_TRANSLATION_AUTO;
7761 else
7762 goto chs_fail;
7763 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00007764 chs_fail:
bellard46d47672004-11-16 01:45:27 +00007765 fprintf(stderr, "qemu: invalid physical CHS format\n");
7766 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00007767 }
bellard330d0412003-07-26 18:11:40 +00007768 }
7769 break;
bellardcd6f1162004-05-13 22:02:20 +00007770 case QEMU_OPTION_nographic:
bellard8d11df92004-08-24 21:13:40 +00007771 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
thsa39437a2007-03-25 20:27:04 +00007772 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null");
ths20d8a3e2007-02-18 17:04:49 +00007773 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
bellarda20dd502003-09-30 21:07:02 +00007774 nographic = 1;
7775 break;
balroga171fe32007-04-30 01:48:07 +00007776 case QEMU_OPTION_portrait:
7777 graphic_rotate = 1;
7778 break;
bellardcd6f1162004-05-13 22:02:20 +00007779 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00007780 kernel_filename = optarg;
7781 break;
bellardcd6f1162004-05-13 22:02:20 +00007782 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00007783 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00007784 break;
bellardcd6f1162004-05-13 22:02:20 +00007785 case QEMU_OPTION_cdrom:
ths96d30e42007-01-07 20:42:14 +00007786 if (cdrom_index >= 0) {
7787 hd_filename[cdrom_index] = optarg;
bellardcc1daa42005-06-05 14:49:17 +00007788 }
bellard36b486b2003-11-11 13:36:08 +00007789 break;
bellardcd6f1162004-05-13 22:02:20 +00007790 case QEMU_OPTION_boot:
bellard36b486b2003-11-11 13:36:08 +00007791 boot_device = optarg[0];
ths5fafdf22007-09-16 21:08:06 +00007792 if (boot_device != 'a' &&
thseec85c22007-01-05 17:41:07 +00007793#if defined(TARGET_SPARC) || defined(TARGET_I386)
bellard6f7e9ae2005-03-13 09:43:36 +00007794 // Network boot
7795 boot_device != 'n' &&
7796#endif
bellardc45886d2004-01-05 00:02:06 +00007797 boot_device != 'c' && boot_device != 'd') {
bellard36b486b2003-11-11 13:36:08 +00007798 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
7799 exit(1);
7800 }
7801 break;
bellardcd6f1162004-05-13 22:02:20 +00007802 case QEMU_OPTION_fda:
bellardc45886d2004-01-05 00:02:06 +00007803 fd_filename[0] = optarg;
7804 break;
bellardcd6f1162004-05-13 22:02:20 +00007805 case QEMU_OPTION_fdb:
bellardc45886d2004-01-05 00:02:06 +00007806 fd_filename[1] = optarg;
7807 break;
bellard52ca8d62006-06-14 16:03:05 +00007808#ifdef TARGET_I386
7809 case QEMU_OPTION_no_fd_bootchk:
7810 fd_bootchk = 0;
7811 break;
7812#endif
bellardcd6f1162004-05-13 22:02:20 +00007813 case QEMU_OPTION_no_code_copy:
bellard77fef8c2004-02-16 22:05:46 +00007814 code_copy_enabled = 0;
7815 break;
bellard7c9d8e02005-11-15 22:16:05 +00007816 case QEMU_OPTION_net:
7817 if (nb_net_clients >= MAX_NET_CLIENTS) {
7818 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00007819 exit(1);
7820 }
bellard7c9d8e02005-11-15 22:16:05 +00007821 pstrcpy(net_clients[nb_net_clients],
7822 sizeof(net_clients[0]),
7823 optarg);
7824 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00007825 break;
bellardc7f74642004-08-24 21:57:12 +00007826#ifdef CONFIG_SLIRP
7827 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00007828 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00007829 break;
ths47d5d012007-02-20 00:05:08 +00007830 case QEMU_OPTION_bootp:
7831 bootp_filename = optarg;
7832 break;
bellardc94c8d62004-09-13 21:37:34 +00007833#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007834 case QEMU_OPTION_smb:
7835 net_slirp_smb(optarg);
7836 break;
bellardc94c8d62004-09-13 21:37:34 +00007837#endif
bellard9bf05442004-08-25 22:12:49 +00007838 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00007839 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00007840 break;
bellardc7f74642004-08-24 21:57:12 +00007841#endif
bellard1d14ffa2005-10-30 18:58:22 +00007842#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007843 case QEMU_OPTION_audio_help:
7844 AUD_help ();
7845 exit (0);
7846 break;
7847 case QEMU_OPTION_soundhw:
7848 select_soundhw (optarg);
7849 break;
7850#endif
bellardcd6f1162004-05-13 22:02:20 +00007851 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00007852 help(0);
bellardcd6f1162004-05-13 22:02:20 +00007853 break;
7854 case QEMU_OPTION_m:
7855 ram_size = atoi(optarg) * 1024 * 1024;
7856 if (ram_size <= 0)
ths15f82202007-06-29 23:26:08 +00007857 help(1);
bellardcd6f1162004-05-13 22:02:20 +00007858 if (ram_size > PHYS_RAM_MAX_SIZE) {
7859 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
7860 PHYS_RAM_MAX_SIZE / (1024 * 1024));
7861 exit(1);
7862 }
7863 break;
7864 case QEMU_OPTION_d:
7865 {
7866 int mask;
7867 CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00007868
bellardcd6f1162004-05-13 22:02:20 +00007869 mask = cpu_str_to_log_mask(optarg);
7870 if (!mask) {
7871 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00007872 for(item = cpu_log_items; item->mask != 0; item++) {
7873 printf("%-10s %s\n", item->name, item->help);
7874 }
7875 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00007876 }
7877 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00007878 }
bellardcd6f1162004-05-13 22:02:20 +00007879 break;
bellard67b915a2004-03-31 23:37:16 +00007880#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00007881 case QEMU_OPTION_s:
7882 use_gdbstub = 1;
7883 break;
7884 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00007885 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00007886 break;
bellard67b915a2004-03-31 23:37:16 +00007887#endif
bellardcd6f1162004-05-13 22:02:20 +00007888 case QEMU_OPTION_L:
7889 bios_dir = optarg;
7890 break;
7891 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00007892 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00007893 break;
bellard3d11d0e2004-12-12 16:56:30 +00007894 case QEMU_OPTION_k:
7895 keyboard_layout = optarg;
7896 break;
bellardee22c2f2004-06-03 12:49:50 +00007897 case QEMU_OPTION_localtime:
7898 rtc_utc = 0;
7899 break;
bellard1f042752004-06-05 13:46:47 +00007900 case QEMU_OPTION_cirrusvga:
7901 cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +00007902 vmsvga_enabled = 0;
7903 break;
7904 case QEMU_OPTION_vmsvga:
7905 cirrus_vga_enabled = 0;
7906 vmsvga_enabled = 1;
bellard1f042752004-06-05 13:46:47 +00007907 break;
bellard1bfe8562004-07-08 21:17:50 +00007908 case QEMU_OPTION_std_vga:
7909 cirrus_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +00007910 vmsvga_enabled = 0;
bellard1bfe8562004-07-08 21:17:50 +00007911 break;
bellarde9b137c2004-06-21 16:46:10 +00007912 case QEMU_OPTION_g:
7913 {
7914 const char *p;
7915 int w, h, depth;
7916 p = optarg;
7917 w = strtol(p, (char **)&p, 10);
7918 if (w <= 0) {
7919 graphic_error:
7920 fprintf(stderr, "qemu: invalid resolution or depth\n");
7921 exit(1);
7922 }
7923 if (*p != 'x')
7924 goto graphic_error;
7925 p++;
7926 h = strtol(p, (char **)&p, 10);
7927 if (h <= 0)
7928 goto graphic_error;
7929 if (*p == 'x') {
7930 p++;
7931 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00007932 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00007933 depth != 24 && depth != 32)
7934 goto graphic_error;
7935 } else if (*p == '\0') {
7936 depth = graphic_depth;
7937 } else {
7938 goto graphic_error;
7939 }
ths3b46e622007-09-17 08:09:54 +00007940
bellarde9b137c2004-06-21 16:46:10 +00007941 graphic_width = w;
7942 graphic_height = h;
7943 graphic_depth = depth;
7944 }
7945 break;
ths20d8a3e2007-02-18 17:04:49 +00007946 case QEMU_OPTION_echr:
7947 {
7948 char *r;
7949 term_escape_char = strtol(optarg, &r, 0);
7950 if (r == optarg)
7951 printf("Bad argument to echr\n");
7952 break;
7953 }
bellard82c643f2004-07-14 17:28:13 +00007954 case QEMU_OPTION_monitor:
7955 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
7956 break;
7957 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00007958 if (serial_device_index >= MAX_SERIAL_PORTS) {
7959 fprintf(stderr, "qemu: too many serial ports\n");
7960 exit(1);
7961 }
ths5fafdf22007-09-16 21:08:06 +00007962 pstrcpy(serial_devices[serial_device_index],
bellard8d11df92004-08-24 21:13:40 +00007963 sizeof(serial_devices[0]), optarg);
7964 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00007965 break;
bellard6508fe52005-01-15 12:02:56 +00007966 case QEMU_OPTION_parallel:
7967 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
7968 fprintf(stderr, "qemu: too many parallel ports\n");
7969 exit(1);
7970 }
ths5fafdf22007-09-16 21:08:06 +00007971 pstrcpy(parallel_devices[parallel_device_index],
bellard6508fe52005-01-15 12:02:56 +00007972 sizeof(parallel_devices[0]), optarg);
7973 parallel_device_index++;
7974 break;
bellardd63d3072004-10-03 13:29:03 +00007975 case QEMU_OPTION_loadvm:
7976 loadvm = optarg;
7977 break;
7978 case QEMU_OPTION_full_screen:
7979 full_screen = 1;
7980 break;
ths667acca2006-12-11 02:08:05 +00007981#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007982 case QEMU_OPTION_no_frame:
7983 no_frame = 1;
7984 break;
ths3780e192007-06-21 21:08:02 +00007985 case QEMU_OPTION_alt_grab:
7986 alt_grab = 1;
7987 break;
ths667acca2006-12-11 02:08:05 +00007988 case QEMU_OPTION_no_quit:
7989 no_quit = 1;
7990 break;
7991#endif
bellardf7cce892004-12-08 22:21:25 +00007992 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00007993 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00007994 break;
bellarda09db212005-04-30 16:10:35 +00007995#ifdef TARGET_I386
7996 case QEMU_OPTION_win2k_hack:
7997 win2k_install_hack = 1;
7998 break;
7999#endif
bellardd993e022005-02-10 22:00:06 +00008000#ifdef USE_KQEMU
8001 case QEMU_OPTION_no_kqemu:
8002 kqemu_allowed = 0;
8003 break;
bellard89bfc102006-02-08 22:46:31 +00008004 case QEMU_OPTION_kernel_kqemu:
8005 kqemu_allowed = 2;
8006 break;
bellardd993e022005-02-10 22:00:06 +00008007#endif
bellardbb36d472005-11-05 14:22:28 +00008008 case QEMU_OPTION_usb:
8009 usb_enabled = 1;
8010 break;
bellarda594cfb2005-11-06 16:13:29 +00008011 case QEMU_OPTION_usbdevice:
8012 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00008013 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00008014 fprintf(stderr, "Too many USB devices\n");
8015 exit(1);
8016 }
8017 pstrcpy(usb_devices[usb_devices_index],
8018 sizeof(usb_devices[usb_devices_index]),
8019 optarg);
8020 usb_devices_index++;
8021 break;
bellard6a00d602005-11-21 23:25:50 +00008022 case QEMU_OPTION_smp:
8023 smp_cpus = atoi(optarg);
bellardba3c64f2005-12-05 20:31:52 +00008024 if (smp_cpus < 1 || smp_cpus > MAX_CPUS) {
bellard6a00d602005-11-21 23:25:50 +00008025 fprintf(stderr, "Invalid number of CPUs\n");
8026 exit(1);
8027 }
8028 break;
bellard24236862006-04-30 21:28:36 +00008029 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00008030 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00008031 break;
bellard6515b202006-05-03 22:02:44 +00008032 case QEMU_OPTION_no_acpi:
8033 acpi_enabled = 0;
8034 break;
bellardd1beab82006-10-02 19:44:22 +00008035 case QEMU_OPTION_no_reboot:
8036 no_reboot = 1;
8037 break;
balrog9467cd42007-05-01 01:34:14 +00008038 case QEMU_OPTION_show_cursor:
8039 cursor_hide = 0;
8040 break;
ths71e3ceb2006-12-22 02:11:31 +00008041 case QEMU_OPTION_daemonize:
8042 daemonize = 1;
8043 break;
ths9ae02552007-01-05 17:39:04 +00008044 case QEMU_OPTION_option_rom:
8045 if (nb_option_roms >= MAX_OPTION_ROMS) {
8046 fprintf(stderr, "Too many option ROMs\n");
8047 exit(1);
8048 }
8049 option_rom[nb_option_roms] = optarg;
8050 nb_option_roms++;
8051 break;
pbrook8e716212007-01-20 17:12:09 +00008052 case QEMU_OPTION_semihosting:
8053 semihosting_enabled = 1;
8054 break;
thsc35734b2007-03-19 15:17:08 +00008055 case QEMU_OPTION_name:
8056 qemu_name = optarg;
8057 break;
blueswir166508602007-05-01 14:16:52 +00008058#ifdef TARGET_SPARC
8059 case QEMU_OPTION_prom_env:
8060 if (nb_prom_envs >= MAX_PROM_ENVS) {
8061 fprintf(stderr, "Too many prom variables\n");
8062 exit(1);
8063 }
8064 prom_envs[nb_prom_envs] = optarg;
8065 nb_prom_envs++;
8066 break;
8067#endif
balrog2b8f2d42007-07-27 22:08:46 +00008068#ifdef TARGET_ARM
8069 case QEMU_OPTION_old_param:
8070 old_param = 1;
8071#endif
thsf3dcfad2007-08-24 01:26:02 +00008072 case QEMU_OPTION_clock:
8073 configure_alarms(optarg);
8074 break;
bellardcd6f1162004-05-13 22:02:20 +00008075 }
bellard0824d6f2003-06-24 13:42:40 +00008076 }
8077 }
bellard330d0412003-07-26 18:11:40 +00008078
ths71e3ceb2006-12-22 02:11:31 +00008079#ifndef _WIN32
8080 if (daemonize && !nographic && vnc_display == NULL) {
8081 fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n");
8082 daemonize = 0;
8083 }
8084
8085 if (daemonize) {
8086 pid_t pid;
8087
8088 if (pipe(fds) == -1)
8089 exit(1);
8090
8091 pid = fork();
8092 if (pid > 0) {
8093 uint8_t status;
8094 ssize_t len;
8095
8096 close(fds[1]);
8097
8098 again:
ths93815bc2007-03-19 15:58:31 +00008099 len = read(fds[0], &status, 1);
8100 if (len == -1 && (errno == EINTR))
8101 goto again;
8102
8103 if (len != 1)
8104 exit(1);
8105 else if (status == 1) {
8106 fprintf(stderr, "Could not acquire pidfile\n");
8107 exit(1);
8108 } else
8109 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00008110 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00008111 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00008112
8113 setsid();
8114
8115 pid = fork();
8116 if (pid > 0)
8117 exit(0);
8118 else if (pid < 0)
8119 exit(1);
8120
8121 umask(027);
8122 chdir("/");
8123
8124 signal(SIGTSTP, SIG_IGN);
8125 signal(SIGTTOU, SIG_IGN);
8126 signal(SIGTTIN, SIG_IGN);
8127 }
8128#endif
8129
thsaa26bb22007-03-25 21:33:06 +00008130 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00008131 if (daemonize) {
8132 uint8_t status = 1;
8133 write(fds[1], &status, 1);
8134 } else
8135 fprintf(stderr, "Could not acquire pid file\n");
8136 exit(1);
8137 }
8138
bellardff3fbb32006-01-08 10:53:14 +00008139#ifdef USE_KQEMU
8140 if (smp_cpus > 1)
8141 kqemu_allowed = 0;
8142#endif
bellarda20dd502003-09-30 21:07:02 +00008143 linux_boot = (kernel_filename != NULL);
ths42550fd2006-12-22 16:34:12 +00008144
8145 if (!linux_boot &&
thsd84fe7a2007-02-19 01:10:26 +00008146 boot_device != 'n' &&
ths5fafdf22007-09-16 21:08:06 +00008147 hd_filename[0] == '\0' &&
ths96d30e42007-01-07 20:42:14 +00008148 (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
bellardc45886d2004-01-05 00:02:06 +00008149 fd_filename[0] == '\0')
ths15f82202007-06-29 23:26:08 +00008150 help(1);
bellard0824d6f2003-06-24 13:42:40 +00008151
ths96d30e42007-01-07 20:42:14 +00008152 /* boot to floppy or the default cd if no hard disk defined yet */
8153 if (hd_filename[0] == '\0' && boot_device == 'c') {
8154 if (fd_filename[0] != '\0')
8155 boot_device = 'a';
8156 else
8157 boot_device = 'd';
8158 }
8159
bellardb118d612003-06-30 23:36:21 +00008160 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00008161
pbrook634fce92006-07-15 17:40:09 +00008162 init_timers();
8163 init_timer_alarm();
bellard83f64092006-08-01 16:21:11 +00008164 qemu_aio_init();
pbrook634fce92006-07-15 17:40:09 +00008165
bellardfd1dff42006-02-01 21:29:26 +00008166#ifdef _WIN32
8167 socket_init();
8168#endif
8169
bellard7c9d8e02005-11-15 22:16:05 +00008170 /* init network clients */
8171 if (nb_net_clients == 0) {
8172 /* if no clients, we use a default config */
8173 pstrcpy(net_clients[0], sizeof(net_clients[0]),
8174 "nic");
8175 pstrcpy(net_clients[1], sizeof(net_clients[0]),
8176 "user");
8177 nb_net_clients = 2;
bellardc20709a2004-04-21 23:27:19 +00008178 }
8179
bellard7c9d8e02005-11-15 22:16:05 +00008180 for(i = 0;i < nb_net_clients; i++) {
8181 if (net_client_init(net_clients[i]) < 0)
8182 exit(1);
bellard702c6512004-04-02 21:21:32 +00008183 }
blueswir1833c7172007-05-27 19:36:43 +00008184 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8185 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
8186 continue;
8187 if (vlan->nb_guest_devs == 0) {
8188 fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
8189 exit(1);
8190 }
8191 if (vlan->nb_host_devs == 0)
8192 fprintf(stderr,
8193 "Warning: vlan %d is not connected to host network\n",
8194 vlan->id);
8195 }
bellardf1510b22003-06-25 00:07:40 +00008196
thseec85c22007-01-05 17:41:07 +00008197#ifdef TARGET_I386
8198 if (boot_device == 'n') {
8199 for (i = 0; i < nb_nics; i++) {
8200 const char *model = nd_table[i].model;
8201 char buf[1024];
8202 if (model == NULL)
8203 model = "ne2k_pci";
8204 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
8205 if (get_image_size(buf) > 0) {
8206 option_rom[nb_option_roms] = strdup(buf);
8207 nb_option_roms++;
8208 break;
8209 }
8210 }
8211 if (i == nb_nics) {
8212 fprintf(stderr, "No valid PXE rom found for network device\n");
8213 exit(1);
8214 }
thseec85c22007-01-05 17:41:07 +00008215 }
8216#endif
8217
bellard0824d6f2003-06-24 13:42:40 +00008218 /* init the memory */
bellard970ac5a2007-02-08 23:09:59 +00008219 phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
ths9ae02552007-01-05 17:39:04 +00008220
bellardd993e022005-02-10 22:00:06 +00008221 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00008222 if (!phys_ram_base) {
8223 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00008224 exit(1);
8225 }
8226
ths96d30e42007-01-07 20:42:14 +00008227 /* we always create the cdrom drive, even if no disk is there */
bellard5905b2e2004-08-01 21:53:26 +00008228 bdrv_init();
ths96d30e42007-01-07 20:42:14 +00008229 if (cdrom_index >= 0) {
8230 bs_table[cdrom_index] = bdrv_new("cdrom");
8231 bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM);
bellardc4b1fcc2004-03-14 21:44:30 +00008232 }
8233
ths96d30e42007-01-07 20:42:14 +00008234 /* open the virtual block devices */
8235 for(i = 0; i < MAX_DISKS; i++) {
8236 if (hd_filename[i]) {
8237 if (!bs_table[i]) {
8238 char buf[64];
8239 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
8240 bs_table[i] = bdrv_new(buf);
8241 }
8242 if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8243 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
8244 hd_filename[i]);
8245 exit(1);
8246 }
8247 if (i == 0 && cyls != 0) {
8248 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
8249 bdrv_set_translation_hint(bs_table[i], translation);
8250 }
8251 }
bellardc4b1fcc2004-03-14 21:44:30 +00008252 }
8253
8254 /* we always create at least one floppy disk */
8255 fd_table[0] = bdrv_new("fda");
8256 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
8257
8258 for(i = 0; i < MAX_FD; i++) {
8259 if (fd_filename[i]) {
8260 if (!fd_table[i]) {
8261 char buf[64];
8262 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
8263 fd_table[i] = bdrv_new(buf);
8264 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
8265 }
pbrooka1bb27b2007-04-06 16:49:48 +00008266 if (fd_filename[i][0] != '\0') {
bellard83f64092006-08-01 16:21:11 +00008267 if (bdrv_open(fd_table[i], fd_filename[i],
8268 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
bellardc20709a2004-04-21 23:27:19 +00008269 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
bellardc4b1fcc2004-03-14 21:44:30 +00008270 fd_filename[i]);
8271 exit(1);
8272 }
8273 }
bellard33e39632003-07-06 17:15:21 +00008274 }
8275 }
8276
balrog2bac6012007-04-30 01:34:31 +00008277 /* Open the virtual parallel flash block devices */
j_mayer86f55662007-04-24 06:52:59 +00008278 for(i = 0; i < MAX_PFLASH; i++) {
8279 if (pflash_filename[i]) {
8280 if (!pflash_table[i]) {
8281 char buf[64];
8282 snprintf(buf, sizeof(buf), "fl%c", i + 'a');
8283 pflash_table[i] = bdrv_new(buf);
8284 }
8285 if (bdrv_open(pflash_table[i], pflash_filename[i],
8286 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8287 fprintf(stderr, "qemu: could not open flash image '%s'\n",
8288 pflash_filename[i]);
8289 exit(1);
8290 }
8291 }
8292 }
8293
pbrooka1bb27b2007-04-06 16:49:48 +00008294 sd_bdrv = bdrv_new ("sd");
8295 /* FIXME: This isn't really a floppy, but it's a reasonable
8296 approximation. */
8297 bdrv_set_type_hint(sd_bdrv, BDRV_TYPE_FLOPPY);
8298 if (sd_filename) {
8299 if (bdrv_open(sd_bdrv, sd_filename,
8300 snapshot ? BDRV_O_SNAPSHOT : 0) < 0) {
8301 fprintf(stderr, "qemu: could not open SD card image %s\n",
8302 sd_filename);
balrog2bac6012007-04-30 01:34:31 +00008303 } else
balroga171fe32007-04-30 01:48:07 +00008304 qemu_key_check(sd_bdrv, sd_filename);
pbrooka1bb27b2007-04-06 16:49:48 +00008305 }
8306
balrog3e3d5812007-04-30 02:09:25 +00008307 if (mtd_filename) {
8308 mtd_bdrv = bdrv_new ("mtd");
8309 if (bdrv_open(mtd_bdrv, mtd_filename,
8310 snapshot ? BDRV_O_SNAPSHOT : 0) < 0 ||
8311 qemu_key_check(mtd_bdrv, mtd_filename)) {
8312 fprintf(stderr, "qemu: could not open Flash image %s\n",
8313 mtd_filename);
8314 bdrv_delete(mtd_bdrv);
8315 mtd_bdrv = 0;
8316 }
8317 }
8318
bellardc88676f2006-08-06 13:36:11 +00008319 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
8320 register_savevm("ram", 0, 2, ram_save, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00008321
bellard330d0412003-07-26 18:11:40 +00008322 init_ioports();
bellard0824d6f2003-06-24 13:42:40 +00008323
bellard313aa562003-08-10 21:52:11 +00008324 /* terminal init */
ths740733b2007-06-08 01:57:56 +00008325 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00008326 if (nographic) {
ths2ff89792007-06-21 23:34:19 +00008327 /* nearly nothing to do */
8328 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00008329 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00008330 vnc_display_init(ds);
8331 if (vnc_display_open(ds, vnc_display) < 0)
8332 exit(1);
bellard313aa562003-08-10 21:52:11 +00008333 } else {
bellard5b0753e2005-03-01 21:37:28 +00008334#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00008335 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00008336#elif defined(CONFIG_COCOA)
8337 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00008338#endif
8339 }
bellard0824d6f2003-06-24 13:42:40 +00008340
ths20d8a3e2007-02-18 17:04:49 +00008341 /* Maintain compatibility with multiple stdio monitors */
8342 if (!strcmp(monitor_device,"stdio")) {
8343 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
8344 if (!strcmp(serial_devices[i],"mon:stdio")) {
8345 monitor_device[0] = '\0';
8346 break;
8347 } else if (!strcmp(serial_devices[i],"stdio")) {
8348 monitor_device[0] = '\0';
8349 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio");
8350 break;
8351 }
8352 }
bellard82c643f2004-07-14 17:28:13 +00008353 }
ths20d8a3e2007-02-18 17:04:49 +00008354 if (monitor_device[0] != '\0') {
8355 monitor_hd = qemu_chr_open(monitor_device);
8356 if (!monitor_hd) {
8357 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
8358 exit(1);
8359 }
8360 monitor_init(monitor_hd, !nographic);
8361 }
bellard82c643f2004-07-14 17:28:13 +00008362
bellard8d11df92004-08-24 21:13:40 +00008363 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008364 const char *devname = serial_devices[i];
8365 if (devname[0] != '\0' && strcmp(devname, "none")) {
8366 serial_hds[i] = qemu_chr_open(devname);
bellard8d11df92004-08-24 21:13:40 +00008367 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008368 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008369 devname);
bellard8d11df92004-08-24 21:13:40 +00008370 exit(1);
8371 }
thsaf3a9032007-07-11 23:14:59 +00008372 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008373 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00008374 }
bellard82c643f2004-07-14 17:28:13 +00008375 }
bellard82c643f2004-07-14 17:28:13 +00008376
bellard6508fe52005-01-15 12:02:56 +00008377 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008378 const char *devname = parallel_devices[i];
8379 if (devname[0] != '\0' && strcmp(devname, "none")) {
8380 parallel_hds[i] = qemu_chr_open(devname);
bellard6508fe52005-01-15 12:02:56 +00008381 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008382 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008383 devname);
bellard6508fe52005-01-15 12:02:56 +00008384 exit(1);
8385 }
thsaf3a9032007-07-11 23:14:59 +00008386 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008387 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00008388 }
8389 }
8390
bellardcc1daa42005-06-05 14:49:17 +00008391 machine->init(ram_size, vga_ram_size, boot_device,
8392 ds, fd_filename, snapshot,
j_mayer94fc95c2007-03-05 19:44:02 +00008393 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00008394
pbrook0d92ed32006-05-21 16:30:15 +00008395 /* init USB devices */
8396 if (usb_enabled) {
8397 for(i = 0; i < usb_devices_index; i++) {
8398 if (usb_device_add(usb_devices[i]) < 0) {
8399 fprintf(stderr, "Warning: could not add USB device %s\n",
8400 usb_devices[i]);
8401 }
8402 }
8403 }
8404
ths740733b2007-06-08 01:57:56 +00008405 if (display_state.dpy_refresh) {
8406 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
8407 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
8408 }
bellard7f7f9872003-10-30 01:11:23 +00008409
bellard67b915a2004-03-31 23:37:16 +00008410#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00008411 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00008412 /* XXX: use standard host:port notation and modify options
8413 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00008414 if (gdbserver_start(gdbstub_port) < 0) {
8415 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00008416 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00008417 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00008418 }
balrog45669e02007-07-02 13:20:17 +00008419 }
bellard67b915a2004-03-31 23:37:16 +00008420#endif
balrog45669e02007-07-02 13:20:17 +00008421
bellardd63d3072004-10-03 13:29:03 +00008422 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00008423 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00008424
bellard67b915a2004-03-31 23:37:16 +00008425 {
bellard5905b2e2004-08-01 21:53:26 +00008426 /* XXX: simplify init */
8427 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00008428 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00008429 vm_start();
8430 }
bellard0824d6f2003-06-24 13:42:40 +00008431 }
thsffd843b2006-12-21 19:46:43 +00008432
ths71e3ceb2006-12-22 02:11:31 +00008433 if (daemonize) {
8434 uint8_t status = 0;
8435 ssize_t len;
8436 int fd;
8437
8438 again1:
8439 len = write(fds[1], &status, 1);
8440 if (len == -1 && (errno == EINTR))
8441 goto again1;
8442
8443 if (len != 1)
8444 exit(1);
8445
balrogaeb30be2007-07-02 15:03:13 +00008446 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00008447 if (fd == -1)
8448 exit(1);
8449
8450 dup2(fd, 0);
8451 dup2(fd, 1);
8452 dup2(fd, 2);
8453
8454 close(fd);
8455 }
8456
bellard8a7ddc32004-03-31 19:00:16 +00008457 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00008458 quit_timers();
bellard0824d6f2003-06-24 13:42:40 +00008459 return 0;
8460}