blob: 5b7e97e6de72b967f6bc5d34d6cd9786067a6ac0 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/boards.h"
26#include "hw/usb.h"
27#include "hw/pcmcia.h"
28#include "hw/pc.h"
29#include "hw/fdc.h"
30#include "hw/audiodev.h"
31#include "hw/isa.h"
32#include "net.h"
33#include "console.h"
34#include "sysemu.h"
35#include "gdbstub.h"
36#include "qemu-timer.h"
37#include "qemu-char.h"
38#include "block.h"
39#include "audio/audio.h"
bellard67b915a2004-03-31 23:37:16 +000040
bellard0824d6f2003-06-24 13:42:40 +000041#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000042#include <fcntl.h>
43#include <signal.h>
44#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000045#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000046#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000047#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000048
49#ifndef _WIN32
50#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000051#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000052#include <termios.h>
53#include <sys/poll.h>
54#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000055#include <sys/ioctl.h>
56#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000057#include <netinet/in.h>
bellard9d728e82004-09-05 23:09:03 +000058#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000059#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000060#include <sys/select.h>
61#include <arpa/inet.h>
bellard7d3505c2004-05-12 19:32:15 +000062#ifdef _BSD
63#include <sys/stat.h>
bellard83fb7ad2004-07-05 21:25:26 +000064#ifndef __APPLE__
bellard7d3505c2004-05-12 19:32:15 +000065#include <libutil.h>
bellard83fb7ad2004-07-05 21:25:26 +000066#endif
ths5c40d2b2007-06-23 16:03:36 +000067#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000069#else
bellardec530c82006-04-25 22:36:06 +000070#ifndef __sun__
bellardf1510b22003-06-25 00:07:40 +000071#include <linux/if.h>
72#include <linux/if_tun.h>
bellard7d3505c2004-05-12 19:32:15 +000073#include <pty.h>
74#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000075#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000076
77/* For the benefit of older linux systems which don't supply it,
78 we use a local copy of hpet.h. */
79/* #include <linux/hpet.h> */
80#include "hpet.h"
81
bellarde57a8c02005-11-10 23:58:52 +000082#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000083#include <linux/parport.h>
thsd5d10bc2007-02-17 22:54:49 +000084#else
85#include <sys/stat.h>
86#include <sys/ethernet.h>
87#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000088#include <netinet/arp.h>
89#include <netinet/in.h>
90#include <netinet/in_systm.h>
91#include <netinet/ip.h>
92#include <netinet/ip_icmp.h> // must come after ip.h
93#include <netinet/udp.h>
94#include <netinet/tcp.h>
95#include <net/if.h>
96#include <syslog.h>
97#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000098#endif
bellard7d3505c2004-05-12 19:32:15 +000099#endif
thscb4b9762007-09-13 12:39:35 +0000100#else
101#include <winsock2.h>
102int inet_aton(const char *cp, struct in_addr *ia);
bellardec530c82006-04-25 22:36:06 +0000103#endif
bellard67b915a2004-03-31 23:37:16 +0000104
bellardc20709a2004-04-21 23:27:19 +0000105#if defined(CONFIG_SLIRP)
106#include "libslirp.h"
107#endif
108
bellard67b915a2004-03-31 23:37:16 +0000109#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +0000110#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000111#include <sys/timeb.h>
112#include <windows.h>
113#define getopt_long_only getopt_long
114#define memalign(align, size) malloc(size)
115#endif
116
bellard6ca957f2006-04-30 22:53:25 +0000117#include "qemu_socket.h"
118
bellard73332e52004-04-04 20:22:28 +0000119#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000120#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000121#include <SDL/SDL.h>
bellard96bcd4f2004-07-10 16:26:15 +0000122#endif
bellard73332e52004-04-04 20:22:28 +0000123#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000124
bellard5b0753e2005-03-01 21:37:28 +0000125#ifdef CONFIG_COCOA
126#undef main
127#define main qemu_main
128#endif /* CONFIG_COCOA */
129
bellard0824d6f2003-06-24 13:42:40 +0000130#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000131
bellard8a7ddc32004-03-31 19:00:16 +0000132#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000133
bellard5a671352003-10-01 00:13:48 +0000134#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
thsb46a8902007-10-21 23:20:45 +0000135#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
pbrooka14d6c82006-12-23 15:37:33 +0000136#ifdef __sun__
137#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
138#else
139#define SMBD_COMMAND "/usr/sbin/smbd"
140#endif
bellardf1510b22003-06-25 00:07:40 +0000141
bellard0824d6f2003-06-24 13:42:40 +0000142//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000143//#define DEBUG_IOPORT
bellard330d0412003-07-26 18:11:40 +0000144
bellardbb551fa2004-01-24 13:42:26 +0000145#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
bellard7916e222003-07-01 16:27:45 +0000146
bellard77d4bc32004-05-26 22:13:53 +0000147#ifdef TARGET_PPC
148#define DEFAULT_RAM_SIZE 144
149#else
bellard1bfe8562004-07-08 21:17:50 +0000150#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000151#endif
bellard8a7ddc32004-03-31 19:00:16 +0000152/* in ms */
153#define GUI_REFRESH_INTERVAL 30
bellard313aa562003-08-10 21:52:11 +0000154
pbrook0d92ed32006-05-21 16:30:15 +0000155/* Max number of USB devices that can be specified on the commandline. */
156#define MAX_USB_CMDLINE 8
157
bellard7dea1da2003-11-16 15:59:30 +0000158/* XXX: use a two level table to limit memory usage */
159#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000160
bellard80cabfa2004-03-14 12:20:30 +0000161const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000162const char *bios_name = NULL;
bellardc4b1fcc2004-03-14 21:44:30 +0000163void *ioport_opaque[MAX_IOPORTS];
bellardfc01f7e2003-06-30 10:03:06 +0000164IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
165IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
thse4bcb142007-12-02 04:51:10 +0000166/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000167 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000168DriveInfo drives_table[MAX_DRIVES+1];
169int nb_drives;
bellardfaea38e2006-08-05 21:31:00 +0000170/* point to the block driver where the snapshots are managed */
171BlockDriverState *bs_snapshots;
bellard313aa562003-08-10 21:52:11 +0000172int vga_ram_size;
173static DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000174int nographic;
bellard3d11d0e2004-12-12 16:56:30 +0000175const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000176int64_t ticks_per_sec;
bellard0ced6582004-05-23 21:06:12 +0000177int ram_size;
bellard80cabfa2004-03-14 12:20:30 +0000178int pit_min_timer_count = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000179int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000180NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000181int vm_running;
bellardee22c2f2004-06-03 12:49:50 +0000182int rtc_utc = 1;
bellard7e0af5d02007-11-07 16:24:33 +0000183int rtc_start_date = -1; /* -1 means now */
bellard1bfe8562004-07-08 21:17:50 +0000184int cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +0000185int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000186#ifdef TARGET_SPARC
187int graphic_width = 1024;
188int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000189int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000190#else
bellard1bfe8562004-07-08 21:17:50 +0000191int graphic_width = 800;
192int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000193int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000194#endif
bellardd63d3072004-10-03 13:29:03 +0000195int full_screen = 0;
ths43523e92007-02-18 18:19:32 +0000196int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000197int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000200#ifdef TARGET_I386
201int win2k_install_hack = 0;
202#endif
bellardbb36d472005-11-05 14:22:28 +0000203int usb_enabled = 0;
bellard7c9d8e02005-11-15 22:16:05 +0000204static VLANState *first_vlan;
bellard6a00d602005-11-21 23:25:50 +0000205int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000206const char *vnc_display;
bellardd3e9db92005-12-17 01:27:28 +0000207#if defined(TARGET_SPARC)
bellardba3c64f2005-12-05 20:31:52 +0000208#define MAX_CPUS 16
bellardd3e9db92005-12-17 01:27:28 +0000209#elif defined(TARGET_I386)
210#define MAX_CPUS 255
bellardba3c64f2005-12-05 20:31:52 +0000211#else
bellardd3e9db92005-12-17 01:27:28 +0000212#define MAX_CPUS 1
bellardba3c64f2005-12-05 20:31:52 +0000213#endif
bellard6515b202006-05-03 22:02:44 +0000214int acpi_enabled = 1;
bellard52ca8d62006-06-14 16:03:05 +0000215int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000216int no_reboot = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000219int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000220const char *option_rom[MAX_OPTION_ROMS];
221int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000222int semihosting_enabled = 0;
pbrook3c07f8e2007-01-21 16:47:01 +0000223int autostart = 1;
balrog2b8f2d42007-07-27 22:08:46 +0000224#ifdef TARGET_ARM
225int old_param = 0;
226#endif
thsc35734b2007-03-19 15:17:08 +0000227const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000228int alt_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000229#ifdef TARGET_SPARC
230unsigned int nb_prom_envs = 0;
231const char *prom_envs[MAX_PROM_ENVS];
232#endif
thse4bcb142007-12-02 04:51:10 +0000233int nb_drives_opt;
234char drives_opt[MAX_DRIVES][1024];
bellard0824d6f2003-06-24 13:42:40 +0000235
balrogee5605e2007-12-03 03:01:40 +0000236static CPUState *cur_cpu;
237static CPUState *next_cpu;
238static int event_pending;
239
balrogaeb30be2007-07-02 15:03:13 +0000240#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
241
bellard0824d6f2003-06-24 13:42:40 +0000242/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000243/* x86 ISA bus support */
244
245target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000246PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000247
pbrook9596ebb2007-11-18 01:44:38 +0000248static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000249{
250#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000251 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000252#endif
bellardfc01f7e2003-06-30 10:03:06 +0000253 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000254}
255
pbrook9596ebb2007-11-18 01:44:38 +0000256static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000257{
258#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000259 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000260#endif
261}
262
263/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000264static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000265{
266 uint32_t data;
bellarddb45c292004-05-12 19:50:26 +0000267 data = ioport_read_table[0][address](ioport_opaque[address], address);
268 address = (address + 1) & (MAX_IOPORTS - 1);
269 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000270 return data;
271}
272
pbrook9596ebb2007-11-18 01:44:38 +0000273static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000274{
bellarddb45c292004-05-12 19:50:26 +0000275 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
276 address = (address + 1) & (MAX_IOPORTS - 1);
277 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000278}
279
pbrook9596ebb2007-11-18 01:44:38 +0000280static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000281{
282#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000283 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000284#endif
285 return 0xffffffff;
286}
287
pbrook9596ebb2007-11-18 01:44:38 +0000288static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000289{
290#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000291 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000292#endif
bellard0824d6f2003-06-24 13:42:40 +0000293}
294
pbrook9596ebb2007-11-18 01:44:38 +0000295static void init_ioports(void)
bellard0824d6f2003-06-24 13:42:40 +0000296{
297 int i;
298
299 for(i = 0; i < MAX_IOPORTS; i++) {
bellardfc01f7e2003-06-30 10:03:06 +0000300 ioport_read_table[0][i] = default_ioport_readb;
301 ioport_write_table[0][i] = default_ioport_writeb;
302 ioport_read_table[1][i] = default_ioport_readw;
303 ioport_write_table[1][i] = default_ioport_writew;
304 ioport_read_table[2][i] = default_ioport_readl;
305 ioport_write_table[2][i] = default_ioport_writel;
bellard0824d6f2003-06-24 13:42:40 +0000306 }
307}
308
bellardfc01f7e2003-06-30 10:03:06 +0000309/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000310int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000311 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000312{
bellardfc01f7e2003-06-30 10:03:06 +0000313 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000314
bellardc4b1fcc2004-03-14 21:44:30 +0000315 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000316 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000317 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000318 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000319 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000320 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000321 } else {
322 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000323 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000324 }
325 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000326 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000327 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
328 hw_error("register_ioport_read: invalid opaque");
329 ioport_opaque[i] = opaque;
330 }
bellard0824d6f2003-06-24 13:42:40 +0000331 return 0;
332}
333
bellardfc01f7e2003-06-30 10:03:06 +0000334/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000335int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000336 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000337{
bellardfc01f7e2003-06-30 10:03:06 +0000338 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000339
bellardc4b1fcc2004-03-14 21:44:30 +0000340 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000341 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000342 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000343 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000344 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000345 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000346 } else {
347 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000348 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000349 }
350 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000351 ioport_write_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000352 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
ths52f61fd2006-12-22 21:20:52 +0000353 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000354 ioport_opaque[i] = opaque;
355 }
bellardf1510b22003-06-25 00:07:40 +0000356 return 0;
357}
358
bellard69b91032004-05-18 23:05:28 +0000359void isa_unassign_ioport(int start, int length)
360{
361 int i;
362
363 for(i = start; i < start + length; i++) {
364 ioport_read_table[0][i] = default_ioport_readb;
365 ioport_read_table[1][i] = default_ioport_readw;
366 ioport_read_table[2][i] = default_ioport_readl;
367
368 ioport_write_table[0][i] = default_ioport_writeb;
369 ioport_write_table[1][i] = default_ioport_writew;
370 ioport_write_table[2][i] = default_ioport_writel;
371 }
372}
373
bellard20f32282005-01-03 23:36:21 +0000374/***********************************************************/
375
bellardc45886d2004-01-05 00:02:06 +0000376void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000377{
bellardfd872592004-05-12 19:11:15 +0000378#ifdef DEBUG_IOPORT
379 if (loglevel & CPU_LOG_IOPORT)
380 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000381#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000382 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000383#ifdef USE_KQEMU
384 if (env)
385 env->last_io_time = cpu_get_time_fast();
386#endif
bellard0824d6f2003-06-24 13:42:40 +0000387}
388
bellardc45886d2004-01-05 00:02:06 +0000389void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000390{
bellardfd872592004-05-12 19:11:15 +0000391#ifdef DEBUG_IOPORT
392 if (loglevel & CPU_LOG_IOPORT)
393 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000394#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000395 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000396#ifdef USE_KQEMU
397 if (env)
398 env->last_io_time = cpu_get_time_fast();
399#endif
bellard0824d6f2003-06-24 13:42:40 +0000400}
401
bellardc45886d2004-01-05 00:02:06 +0000402void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000403{
bellardfd872592004-05-12 19:11:15 +0000404#ifdef DEBUG_IOPORT
405 if (loglevel & CPU_LOG_IOPORT)
406 fprintf(logfile, "outl: %04x %08x\n", addr, val);
407#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000408 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
bellard89bfc102006-02-08 22:46:31 +0000409#ifdef USE_KQEMU
410 if (env)
411 env->last_io_time = cpu_get_time_fast();
412#endif
bellard0824d6f2003-06-24 13:42:40 +0000413}
414
bellardc45886d2004-01-05 00:02:06 +0000415int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000416{
bellardfd872592004-05-12 19:11:15 +0000417 int val;
bellardfd872592004-05-12 19:11:15 +0000418 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
419#ifdef DEBUG_IOPORT
420 if (loglevel & CPU_LOG_IOPORT)
421 fprintf(logfile, "inb : %04x %02x\n", addr, val);
422#endif
bellard89bfc102006-02-08 22:46:31 +0000423#ifdef USE_KQEMU
424 if (env)
425 env->last_io_time = cpu_get_time_fast();
426#endif
bellardfd872592004-05-12 19:11:15 +0000427 return val;
bellard0824d6f2003-06-24 13:42:40 +0000428}
429
bellardc45886d2004-01-05 00:02:06 +0000430int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000431{
bellardfd872592004-05-12 19:11:15 +0000432 int val;
bellardfd872592004-05-12 19:11:15 +0000433 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
434#ifdef DEBUG_IOPORT
435 if (loglevel & CPU_LOG_IOPORT)
436 fprintf(logfile, "inw : %04x %04x\n", addr, val);
437#endif
bellard89bfc102006-02-08 22:46:31 +0000438#ifdef USE_KQEMU
439 if (env)
440 env->last_io_time = cpu_get_time_fast();
441#endif
bellardfd872592004-05-12 19:11:15 +0000442 return val;
bellard0824d6f2003-06-24 13:42:40 +0000443}
444
bellardc45886d2004-01-05 00:02:06 +0000445int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000446{
bellardfd872592004-05-12 19:11:15 +0000447 int val;
bellardfd872592004-05-12 19:11:15 +0000448 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
449#ifdef DEBUG_IOPORT
450 if (loglevel & CPU_LOG_IOPORT)
451 fprintf(logfile, "inl : %04x %08x\n", addr, val);
452#endif
bellard89bfc102006-02-08 22:46:31 +0000453#ifdef USE_KQEMU
454 if (env)
455 env->last_io_time = cpu_get_time_fast();
456#endif
bellardfd872592004-05-12 19:11:15 +0000457 return val;
bellard0824d6f2003-06-24 13:42:40 +0000458}
459
460/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000461void hw_error(const char *fmt, ...)
462{
463 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000464 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000465
466 va_start(ap, fmt);
467 fprintf(stderr, "qemu: hardware error: ");
468 vfprintf(stderr, fmt, ap);
469 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000470 for(env = first_cpu; env != NULL; env = env->next_cpu) {
471 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000472#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000473 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000474#else
bellard6a00d602005-11-21 23:25:50 +0000475 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000476#endif
bellard6a00d602005-11-21 23:25:50 +0000477 }
bellard0824d6f2003-06-24 13:42:40 +0000478 va_end(ap);
479 abort();
480}
481
bellard8a7ddc32004-03-31 19:00:16 +0000482/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000483/* keyboard/mouse */
484
485static QEMUPutKBDEvent *qemu_put_kbd_event;
486static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000487static QEMUPutMouseEntry *qemu_put_mouse_event_head;
488static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000489
490void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
491{
492 qemu_put_kbd_event_opaque = opaque;
493 qemu_put_kbd_event = func;
494}
495
ths455204e2007-01-05 16:42:13 +0000496QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
497 void *opaque, int absolute,
498 const char *name)
bellard63066f42004-06-03 18:45:02 +0000499{
ths455204e2007-01-05 16:42:13 +0000500 QEMUPutMouseEntry *s, *cursor;
501
502 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
503 if (!s)
504 return NULL;
505
506 s->qemu_put_mouse_event = func;
507 s->qemu_put_mouse_event_opaque = opaque;
508 s->qemu_put_mouse_event_absolute = absolute;
509 s->qemu_put_mouse_event_name = qemu_strdup(name);
510 s->next = NULL;
511
512 if (!qemu_put_mouse_event_head) {
513 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
514 return s;
515 }
516
517 cursor = qemu_put_mouse_event_head;
518 while (cursor->next != NULL)
519 cursor = cursor->next;
520
521 cursor->next = s;
522 qemu_put_mouse_event_current = s;
523
524 return s;
525}
526
527void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
528{
529 QEMUPutMouseEntry *prev = NULL, *cursor;
530
531 if (!qemu_put_mouse_event_head || entry == NULL)
532 return;
533
534 cursor = qemu_put_mouse_event_head;
535 while (cursor != NULL && cursor != entry) {
536 prev = cursor;
537 cursor = cursor->next;
538 }
539
540 if (cursor == NULL) // does not exist or list empty
541 return;
542 else if (prev == NULL) { // entry is head
543 qemu_put_mouse_event_head = cursor->next;
544 if (qemu_put_mouse_event_current == entry)
545 qemu_put_mouse_event_current = cursor->next;
546 qemu_free(entry->qemu_put_mouse_event_name);
547 qemu_free(entry);
548 return;
549 }
550
551 prev->next = entry->next;
552
553 if (qemu_put_mouse_event_current == entry)
554 qemu_put_mouse_event_current = prev;
555
556 qemu_free(entry->qemu_put_mouse_event_name);
557 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000558}
559
560void kbd_put_keycode(int keycode)
561{
562 if (qemu_put_kbd_event) {
563 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
564 }
565}
566
567void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
568{
ths455204e2007-01-05 16:42:13 +0000569 QEMUPutMouseEvent *mouse_event;
570 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000571 int width;
ths455204e2007-01-05 16:42:13 +0000572
573 if (!qemu_put_mouse_event_current) {
574 return;
575 }
576
577 mouse_event =
578 qemu_put_mouse_event_current->qemu_put_mouse_event;
579 mouse_event_opaque =
580 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
581
582 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000583 if (graphic_rotate) {
584 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
585 width = 0x7fff;
586 else
587 width = graphic_width;
588 mouse_event(mouse_event_opaque,
589 width - dy, dx, dz, buttons_state);
590 } else
591 mouse_event(mouse_event_opaque,
592 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000593 }
594}
595
bellard09b26c52006-04-12 21:09:08 +0000596int kbd_mouse_is_absolute(void)
597{
ths455204e2007-01-05 16:42:13 +0000598 if (!qemu_put_mouse_event_current)
599 return 0;
600
601 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
602}
603
604void do_info_mice(void)
605{
606 QEMUPutMouseEntry *cursor;
607 int index = 0;
608
609 if (!qemu_put_mouse_event_head) {
610 term_printf("No mouse devices connected\n");
611 return;
612 }
613
614 term_printf("Mouse devices available:\n");
615 cursor = qemu_put_mouse_event_head;
616 while (cursor != NULL) {
617 term_printf("%c Mouse #%d: %s\n",
618 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
619 index, cursor->qemu_put_mouse_event_name);
620 index++;
621 cursor = cursor->next;
622 }
623}
624
625void do_mouse_set(int index)
626{
627 QEMUPutMouseEntry *cursor;
628 int i = 0;
629
630 if (!qemu_put_mouse_event_head) {
631 term_printf("No mouse devices connected\n");
632 return;
633 }
634
635 cursor = qemu_put_mouse_event_head;
636 while (cursor != NULL && index != i) {
637 i++;
638 cursor = cursor->next;
639 }
640
641 if (cursor != NULL)
642 qemu_put_mouse_event_current = cursor;
643 else
644 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000645}
646
bellard87858c82003-06-27 12:01:39 +0000647/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000648uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000649{
650 union {
651 uint64_t ll;
652 struct {
653#ifdef WORDS_BIGENDIAN
654 uint32_t high, low;
655#else
656 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000657#endif
bellard87858c82003-06-27 12:01:39 +0000658 } l;
659 } u, res;
660 uint64_t rl, rh;
661
662 u.ll = a;
663 rl = (uint64_t)u.l.low * (uint64_t)b;
664 rh = (uint64_t)u.l.high * (uint64_t)b;
665 rh += (rl >> 32);
666 res.l.high = rh / c;
667 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
668 return res.ll;
669}
670
bellard1dce7c32006-07-13 23:20:22 +0000671/***********************************************************/
672/* real time host monotonic timer */
673
674#define QEMU_TIMER_BASE 1000000000LL
675
676#ifdef WIN32
677
678static int64_t clock_freq;
679
680static void init_get_clock(void)
681{
bellarda8e5ac32006-07-14 09:36:13 +0000682 LARGE_INTEGER freq;
683 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000684 ret = QueryPerformanceFrequency(&freq);
685 if (ret == 0) {
686 fprintf(stderr, "Could not calibrate ticks\n");
687 exit(1);
688 }
689 clock_freq = freq.QuadPart;
690}
691
692static int64_t get_clock(void)
693{
694 LARGE_INTEGER ti;
695 QueryPerformanceCounter(&ti);
696 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
697}
698
699#else
700
701static int use_rt_clock;
702
703static void init_get_clock(void)
704{
705 use_rt_clock = 0;
706#if defined(__linux__)
707 {
708 struct timespec ts;
709 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
710 use_rt_clock = 1;
711 }
712 }
713#endif
714}
715
716static int64_t get_clock(void)
717{
718#if defined(__linux__)
719 if (use_rt_clock) {
720 struct timespec ts;
721 clock_gettime(CLOCK_MONOTONIC, &ts);
722 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000723 } else
bellard1dce7c32006-07-13 23:20:22 +0000724#endif
725 {
726 /* XXX: using gettimeofday leads to problems if the date
727 changes, so it should be avoided. */
728 struct timeval tv;
729 gettimeofday(&tv, NULL);
730 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
731 }
732}
733
734#endif
735
736/***********************************************************/
737/* guest cycle counter */
738
739static int64_t cpu_ticks_prev;
740static int64_t cpu_ticks_offset;
741static int64_t cpu_clock_offset;
742static int cpu_ticks_enabled;
743
744/* return the host CPU cycle counter and handle stop/restart */
745int64_t cpu_get_ticks(void)
746{
747 if (!cpu_ticks_enabled) {
748 return cpu_ticks_offset;
749 } else {
750 int64_t ticks;
751 ticks = cpu_get_real_ticks();
752 if (cpu_ticks_prev > ticks) {
753 /* Note: non increasing ticks may happen if the host uses
754 software suspend */
755 cpu_ticks_offset += cpu_ticks_prev - ticks;
756 }
757 cpu_ticks_prev = ticks;
758 return ticks + cpu_ticks_offset;
759 }
760}
761
762/* return the host CPU monotonic timer and handle stop/restart */
763static int64_t cpu_get_clock(void)
764{
765 int64_t ti;
766 if (!cpu_ticks_enabled) {
767 return cpu_clock_offset;
768 } else {
769 ti = get_clock();
770 return ti + cpu_clock_offset;
771 }
772}
773
774/* enable cpu_get_ticks() */
775void cpu_enable_ticks(void)
776{
777 if (!cpu_ticks_enabled) {
778 cpu_ticks_offset -= cpu_get_real_ticks();
779 cpu_clock_offset -= get_clock();
780 cpu_ticks_enabled = 1;
781 }
782}
783
784/* disable cpu_get_ticks() : the clock is stopped. You must not call
785 cpu_get_ticks() after that. */
786void cpu_disable_ticks(void)
787{
788 if (cpu_ticks_enabled) {
789 cpu_ticks_offset = cpu_get_ticks();
790 cpu_clock_offset = cpu_get_clock();
791 cpu_ticks_enabled = 0;
792 }
793}
794
795/***********************************************************/
796/* timers */
ths5fafdf22007-09-16 21:08:06 +0000797
bellard8a7ddc32004-03-31 19:00:16 +0000798#define QEMU_TIMER_REALTIME 0
799#define QEMU_TIMER_VIRTUAL 1
800
801struct QEMUClock {
802 int type;
803 /* XXX: add frequency */
804};
805
806struct QEMUTimer {
807 QEMUClock *clock;
808 int64_t expire_time;
809 QEMUTimerCB *cb;
810 void *opaque;
811 struct QEMUTimer *next;
812};
813
thsc8994012007-08-19 21:56:03 +0000814struct qemu_alarm_timer {
815 char const *name;
thsefe75412007-08-24 01:36:32 +0000816 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000817
818 int (*start)(struct qemu_alarm_timer *t);
819 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000820 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000821 void *priv;
822};
823
thsefe75412007-08-24 01:36:32 +0000824#define ALARM_FLAG_DYNTICKS 0x1
825
826static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
827{
828 return t->flags & ALARM_FLAG_DYNTICKS;
829}
830
831static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
832{
833 if (!alarm_has_dynticks(t))
834 return;
835
836 t->rearm(t);
837}
838
839/* TODO: MIN_TIMER_REARM_US should be optimized */
840#define MIN_TIMER_REARM_US 250
841
thsc8994012007-08-19 21:56:03 +0000842static struct qemu_alarm_timer *alarm_timer;
843
844#ifdef _WIN32
845
846struct qemu_alarm_win32 {
847 MMRESULT timerId;
848 HANDLE host_alarm;
849 unsigned int period;
850} alarm_win32_data = {0, NULL, -1};
851
852static int win32_start_timer(struct qemu_alarm_timer *t);
853static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000854static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000855
856#else
857
858static int unix_start_timer(struct qemu_alarm_timer *t);
859static void unix_stop_timer(struct qemu_alarm_timer *t);
860
ths231c6582007-08-26 17:29:15 +0000861#ifdef __linux__
862
thsefe75412007-08-24 01:36:32 +0000863static int dynticks_start_timer(struct qemu_alarm_timer *t);
864static void dynticks_stop_timer(struct qemu_alarm_timer *t);
865static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
866
thsc40ec5a2007-08-19 22:09:40 +0000867static int hpet_start_timer(struct qemu_alarm_timer *t);
868static void hpet_stop_timer(struct qemu_alarm_timer *t);
869
thsc8994012007-08-19 21:56:03 +0000870static int rtc_start_timer(struct qemu_alarm_timer *t);
871static void rtc_stop_timer(struct qemu_alarm_timer *t);
872
thsefe75412007-08-24 01:36:32 +0000873#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000874
875#endif /* _WIN32 */
876
877static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000878#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000879#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000880 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
881 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000882 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000883 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000884 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000885 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000886#endif
thsefe75412007-08-24 01:36:32 +0000887 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000888#else
thsefe75412007-08-24 01:36:32 +0000889 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
890 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
891 {"win32", 0, win32_start_timer,
892 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000893#endif
894 {NULL, }
895};
896
thsf3dcfad2007-08-24 01:26:02 +0000897static void show_available_alarms()
898{
899 int i;
900
901 printf("Available alarm timers, in order of precedence:\n");
902 for (i = 0; alarm_timers[i].name; i++)
903 printf("%s\n", alarm_timers[i].name);
904}
905
906static void configure_alarms(char const *opt)
907{
908 int i;
909 int cur = 0;
910 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
911 char *arg;
912 char *name;
913
914 if (!strcmp(opt, "help")) {
915 show_available_alarms();
916 exit(0);
917 }
918
919 arg = strdup(opt);
920
921 /* Reorder the array */
922 name = strtok(arg, ",");
923 while (name) {
924 struct qemu_alarm_timer tmp;
925
balroge2b577e2007-09-17 21:25:20 +0000926 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000927 if (!strcmp(alarm_timers[i].name, name))
928 break;
929 }
930
931 if (i == count) {
932 fprintf(stderr, "Unknown clock %s\n", name);
933 goto next;
934 }
935
936 if (i < cur)
937 /* Ignore */
938 goto next;
939
940 /* Swap */
941 tmp = alarm_timers[i];
942 alarm_timers[i] = alarm_timers[cur];
943 alarm_timers[cur] = tmp;
944
945 cur++;
946next:
947 name = strtok(NULL, ",");
948 }
949
950 free(arg);
951
952 if (cur) {
953 /* Disable remaining timers */
954 for (i = cur; i < count; i++)
955 alarm_timers[i].name = NULL;
956 }
957
958 /* debug */
959 show_available_alarms();
960}
961
bellard8a7ddc32004-03-31 19:00:16 +0000962QEMUClock *rt_clock;
963QEMUClock *vm_clock;
964
965static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000966
pbrook9596ebb2007-11-18 01:44:38 +0000967static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000968{
969 QEMUClock *clock;
970 clock = qemu_mallocz(sizeof(QEMUClock));
971 if (!clock)
972 return NULL;
973 clock->type = type;
974 return clock;
975}
976
977QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
978{
979 QEMUTimer *ts;
980
981 ts = qemu_mallocz(sizeof(QEMUTimer));
982 ts->clock = clock;
983 ts->cb = cb;
984 ts->opaque = opaque;
985 return ts;
986}
987
988void qemu_free_timer(QEMUTimer *ts)
989{
990 qemu_free(ts);
991}
992
993/* stop a timer, but do not dealloc it */
994void qemu_del_timer(QEMUTimer *ts)
995{
996 QEMUTimer **pt, *t;
997
998 /* NOTE: this code must be signal safe because
999 qemu_timer_expired() can be called from a signal. */
1000 pt = &active_timers[ts->clock->type];
1001 for(;;) {
1002 t = *pt;
1003 if (!t)
1004 break;
1005 if (t == ts) {
1006 *pt = t->next;
1007 break;
1008 }
1009 pt = &t->next;
1010 }
1011}
1012
1013/* modify the current timer so that it will be fired when current_time
1014 >= expire_time. The corresponding callback will be called. */
1015void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1016{
1017 QEMUTimer **pt, *t;
1018
1019 qemu_del_timer(ts);
1020
1021 /* add the timer in the sorted list */
1022 /* NOTE: this code must be signal safe because
1023 qemu_timer_expired() can be called from a signal. */
1024 pt = &active_timers[ts->clock->type];
1025 for(;;) {
1026 t = *pt;
1027 if (!t)
1028 break;
ths5fafdf22007-09-16 21:08:06 +00001029 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001030 break;
1031 pt = &t->next;
1032 }
1033 ts->expire_time = expire_time;
1034 ts->next = *pt;
1035 *pt = ts;
1036}
1037
1038int qemu_timer_pending(QEMUTimer *ts)
1039{
1040 QEMUTimer *t;
1041 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1042 if (t == ts)
1043 return 1;
1044 }
1045 return 0;
1046}
1047
1048static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1049{
1050 if (!timer_head)
1051 return 0;
1052 return (timer_head->expire_time <= current_time);
1053}
1054
1055static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1056{
1057 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001058
bellard8a7ddc32004-03-31 19:00:16 +00001059 for(;;) {
1060 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001061 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001062 break;
1063 /* remove timer from the list before calling the callback */
1064 *ptimer_head = ts->next;
1065 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001066
bellard8a7ddc32004-03-31 19:00:16 +00001067 /* run the callback (the timer list can be modified) */
1068 ts->cb(ts->opaque);
1069 }
thsefe75412007-08-24 01:36:32 +00001070 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00001071}
1072
1073int64_t qemu_get_clock(QEMUClock *clock)
1074{
1075 switch(clock->type) {
1076 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001077 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001078 default:
1079 case QEMU_TIMER_VIRTUAL:
bellard1dce7c32006-07-13 23:20:22 +00001080 return cpu_get_clock();
bellard8a7ddc32004-03-31 19:00:16 +00001081 }
1082}
1083
bellard1dce7c32006-07-13 23:20:22 +00001084static void init_timers(void)
1085{
1086 init_get_clock();
1087 ticks_per_sec = QEMU_TIMER_BASE;
1088 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1089 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1090}
1091
bellard8a7ddc32004-03-31 19:00:16 +00001092/* save a timer */
1093void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1094{
1095 uint64_t expire_time;
1096
1097 if (qemu_timer_pending(ts)) {
1098 expire_time = ts->expire_time;
1099 } else {
1100 expire_time = -1;
1101 }
1102 qemu_put_be64(f, expire_time);
1103}
1104
1105void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1106{
1107 uint64_t expire_time;
1108
1109 expire_time = qemu_get_be64(f);
1110 if (expire_time != -1) {
1111 qemu_mod_timer(ts, expire_time);
1112 } else {
1113 qemu_del_timer(ts);
1114 }
1115}
1116
1117static void timer_save(QEMUFile *f, void *opaque)
1118{
1119 if (cpu_ticks_enabled) {
1120 hw_error("cannot save state if virtual timers are running");
1121 }
1122 qemu_put_be64s(f, &cpu_ticks_offset);
1123 qemu_put_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001124 qemu_put_be64s(f, &cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001125}
1126
1127static int timer_load(QEMUFile *f, void *opaque, int version_id)
1128{
bellardc88676f2006-08-06 13:36:11 +00001129 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001130 return -EINVAL;
1131 if (cpu_ticks_enabled) {
1132 return -EINVAL;
1133 }
1134 qemu_get_be64s(f, &cpu_ticks_offset);
1135 qemu_get_be64s(f, &ticks_per_sec);
bellardc88676f2006-08-06 13:36:11 +00001136 if (version_id == 2) {
1137 qemu_get_be64s(f, &cpu_clock_offset);
1138 }
bellard8a7ddc32004-03-31 19:00:16 +00001139 return 0;
1140}
1141
bellard67b915a2004-03-31 23:37:16 +00001142#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001143void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001144 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1145#else
bellard8a7ddc32004-03-31 19:00:16 +00001146static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001147#endif
bellard8a7ddc32004-03-31 19:00:16 +00001148{
bellard02ba45c2004-06-25 14:46:23 +00001149#if 0
1150#define DISP_FREQ 1000
1151 {
1152 static int64_t delta_min = INT64_MAX;
1153 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1154 static int count;
1155 ti = qemu_get_clock(vm_clock);
1156 if (last_clock != 0) {
1157 delta = ti - last_clock;
1158 if (delta < delta_min)
1159 delta_min = delta;
1160 if (delta > delta_max)
1161 delta_max = delta;
1162 delta_cum += delta;
1163 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001164 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001165 muldiv64(delta_min, 1000000, ticks_per_sec),
1166 muldiv64(delta_max, 1000000, ticks_per_sec),
1167 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1168 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1169 count = 0;
1170 delta_min = INT64_MAX;
1171 delta_max = 0;
1172 delta_cum = 0;
1173 }
1174 }
1175 last_clock = ti;
1176 }
1177#endif
thsefe75412007-08-24 01:36:32 +00001178 if (alarm_has_dynticks(alarm_timer) ||
1179 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
bellard8a7ddc32004-03-31 19:00:16 +00001180 qemu_get_clock(vm_clock)) ||
1181 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1182 qemu_get_clock(rt_clock))) {
bellard06d9f2f2006-05-01 13:23:04 +00001183#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001184 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1185 SetEvent(data->host_alarm);
bellard06d9f2f2006-05-01 13:23:04 +00001186#endif
balrogee5605e2007-12-03 03:01:40 +00001187 CPUState *env = next_cpu;
1188
1189 /* stop the currently executing cpu because a timer occured */
1190 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001191#ifdef USE_KQEMU
balrogee5605e2007-12-03 03:01:40 +00001192 if (env->kqemu_enabled) {
1193 kqemu_cpu_interrupt(env);
bellard6a00d602005-11-21 23:25:50 +00001194 }
balrogee5605e2007-12-03 03:01:40 +00001195#endif
1196 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001197 }
1198}
1199
thsefe75412007-08-24 01:36:32 +00001200static uint64_t qemu_next_deadline(void)
1201{
balrog49a9b722007-10-04 19:47:09 +00001202 int64_t nearest_delta_us = INT64_MAX;
thsefe75412007-08-24 01:36:32 +00001203 int64_t vmdelta_us;
1204
1205 if (active_timers[QEMU_TIMER_REALTIME])
1206 nearest_delta_us = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1207 qemu_get_clock(rt_clock))*1000;
1208
1209 if (active_timers[QEMU_TIMER_VIRTUAL]) {
1210 /* round up */
1211 vmdelta_us = (active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1212 qemu_get_clock(vm_clock)+999)/1000;
1213 if (vmdelta_us < nearest_delta_us)
1214 nearest_delta_us = vmdelta_us;
1215 }
1216
1217 /* Avoid arming the timer to negative, zero, or too low values */
1218 if (nearest_delta_us <= MIN_TIMER_REARM_US)
1219 nearest_delta_us = MIN_TIMER_REARM_US;
1220
1221 return nearest_delta_us;
1222}
1223
bellardfd872592004-05-12 19:11:15 +00001224#ifndef _WIN32
1225
bellard829309c2004-05-20 13:20:12 +00001226#if defined(__linux__)
1227
bellardfd872592004-05-12 19:11:15 +00001228#define RTC_FREQ 1024
1229
thsc8994012007-08-19 21:56:03 +00001230static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001231{
thsc8994012007-08-19 21:56:03 +00001232 struct sigaction act;
1233
1234 /* timer signal */
1235 sigfillset(&act.sa_mask);
1236 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001237 act.sa_handler = host_alarm_handler;
1238
1239 sigaction(SIGIO, &act, NULL);
1240 fcntl(fd, F_SETFL, O_ASYNC);
1241 fcntl(fd, F_SETOWN, getpid());
1242}
1243
thsc40ec5a2007-08-19 22:09:40 +00001244static int hpet_start_timer(struct qemu_alarm_timer *t)
1245{
1246 struct hpet_info info;
1247 int r, fd;
1248
1249 fd = open("/dev/hpet", O_RDONLY);
1250 if (fd < 0)
1251 return -1;
1252
1253 /* Set frequency */
1254 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1255 if (r < 0) {
1256 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1257 "error, but for better emulation accuracy type:\n"
1258 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1259 goto fail;
1260 }
1261
1262 /* Check capabilities */
1263 r = ioctl(fd, HPET_INFO, &info);
1264 if (r < 0)
1265 goto fail;
1266
1267 /* Enable periodic mode */
1268 r = ioctl(fd, HPET_EPI, 0);
1269 if (info.hi_flags && (r < 0))
1270 goto fail;
1271
1272 /* Enable interrupt */
1273 r = ioctl(fd, HPET_IE_ON, 0);
1274 if (r < 0)
1275 goto fail;
1276
1277 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001278 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001279
1280 return 0;
1281fail:
1282 close(fd);
1283 return -1;
1284}
1285
1286static void hpet_stop_timer(struct qemu_alarm_timer *t)
1287{
pbrookfcdc2122007-08-23 20:22:22 +00001288 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001289
1290 close(fd);
1291}
1292
thsc8994012007-08-19 21:56:03 +00001293static int rtc_start_timer(struct qemu_alarm_timer *t)
1294{
1295 int rtc_fd;
1296
balrogaeb30be2007-07-02 15:03:13 +00001297 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001298 if (rtc_fd < 0)
1299 return -1;
1300 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1301 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1302 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1303 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1304 goto fail;
1305 }
1306 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1307 fail:
1308 close(rtc_fd);
1309 return -1;
1310 }
thsc8994012007-08-19 21:56:03 +00001311
1312 enable_sigio_timer(rtc_fd);
1313
pbrookfcdc2122007-08-23 20:22:22 +00001314 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001315
bellardfd872592004-05-12 19:11:15 +00001316 return 0;
1317}
1318
thsc8994012007-08-19 21:56:03 +00001319static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001320{
pbrookfcdc2122007-08-23 20:22:22 +00001321 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001322
1323 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001324}
1325
thsefe75412007-08-24 01:36:32 +00001326static int dynticks_start_timer(struct qemu_alarm_timer *t)
1327{
1328 struct sigevent ev;
1329 timer_t host_timer;
1330 struct sigaction act;
1331
1332 sigfillset(&act.sa_mask);
1333 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001334 act.sa_handler = host_alarm_handler;
1335
1336 sigaction(SIGALRM, &act, NULL);
1337
1338 ev.sigev_value.sival_int = 0;
1339 ev.sigev_notify = SIGEV_SIGNAL;
1340 ev.sigev_signo = SIGALRM;
1341
1342 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1343 perror("timer_create");
1344
1345 /* disable dynticks */
1346 fprintf(stderr, "Dynamic Ticks disabled\n");
1347
1348 return -1;
1349 }
1350
1351 t->priv = (void *)host_timer;
1352
1353 return 0;
1354}
1355
1356static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1357{
1358 timer_t host_timer = (timer_t)t->priv;
1359
1360 timer_delete(host_timer);
1361}
1362
1363static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1364{
1365 timer_t host_timer = (timer_t)t->priv;
1366 struct itimerspec timeout;
1367 int64_t nearest_delta_us = INT64_MAX;
1368 int64_t current_us;
1369
1370 if (!active_timers[QEMU_TIMER_REALTIME] &&
1371 !active_timers[QEMU_TIMER_VIRTUAL])
1372 return;
1373
1374 nearest_delta_us = qemu_next_deadline();
1375
1376 /* check whether a timer is already running */
1377 if (timer_gettime(host_timer, &timeout)) {
1378 perror("gettime");
1379 fprintf(stderr, "Internal timer error: aborting\n");
1380 exit(1);
1381 }
1382 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1383 if (current_us && current_us <= nearest_delta_us)
1384 return;
1385
1386 timeout.it_interval.tv_sec = 0;
1387 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1388 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1389 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1390 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1391 perror("settime");
1392 fprintf(stderr, "Internal timer error: aborting\n");
1393 exit(1);
1394 }
1395}
1396
ths70744b32007-08-26 17:31:30 +00001397#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001398
thsc8994012007-08-19 21:56:03 +00001399static int unix_start_timer(struct qemu_alarm_timer *t)
1400{
1401 struct sigaction act;
1402 struct itimerval itv;
1403 int err;
1404
1405 /* timer signal */
1406 sigfillset(&act.sa_mask);
1407 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001408 act.sa_handler = host_alarm_handler;
1409
1410 sigaction(SIGALRM, &act, NULL);
1411
1412 itv.it_interval.tv_sec = 0;
1413 /* for i386 kernel 2.6 to get 1 ms */
1414 itv.it_interval.tv_usec = 999;
1415 itv.it_value.tv_sec = 0;
1416 itv.it_value.tv_usec = 10 * 1000;
1417
1418 err = setitimer(ITIMER_REAL, &itv, NULL);
1419 if (err)
1420 return -1;
1421
1422 return 0;
1423}
1424
1425static void unix_stop_timer(struct qemu_alarm_timer *t)
1426{
1427 struct itimerval itv;
1428
1429 memset(&itv, 0, sizeof(itv));
1430 setitimer(ITIMER_REAL, &itv, NULL);
1431}
1432
bellard829309c2004-05-20 13:20:12 +00001433#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001434
thsc8994012007-08-19 21:56:03 +00001435#ifdef _WIN32
1436
1437static int win32_start_timer(struct qemu_alarm_timer *t)
1438{
1439 TIMECAPS tc;
1440 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001441 UINT flags;
thsc8994012007-08-19 21:56:03 +00001442
1443 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1444 if (!data->host_alarm) {
1445 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001446 return -1;
thsc8994012007-08-19 21:56:03 +00001447 }
1448
1449 memset(&tc, 0, sizeof(tc));
1450 timeGetDevCaps(&tc, sizeof(tc));
1451
1452 if (data->period < tc.wPeriodMin)
1453 data->period = tc.wPeriodMin;
1454
1455 timeBeginPeriod(data->period);
1456
thsefe75412007-08-24 01:36:32 +00001457 flags = TIME_CALLBACK_FUNCTION;
1458 if (alarm_has_dynticks(t))
1459 flags |= TIME_ONESHOT;
1460 else
1461 flags |= TIME_PERIODIC;
1462
thsc8994012007-08-19 21:56:03 +00001463 data->timerId = timeSetEvent(1, // interval (ms)
1464 data->period, // resolution
1465 host_alarm_handler, // function
1466 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001467 flags);
thsc8994012007-08-19 21:56:03 +00001468
1469 if (!data->timerId) {
1470 perror("Failed to initialize win32 alarm timer");
1471
1472 timeEndPeriod(data->period);
1473 CloseHandle(data->host_alarm);
1474 return -1;
1475 }
1476
1477 qemu_add_wait_object(data->host_alarm, NULL, NULL);
1478
1479 return 0;
1480}
1481
1482static void win32_stop_timer(struct qemu_alarm_timer *t)
1483{
1484 struct qemu_alarm_win32 *data = t->priv;
1485
1486 timeKillEvent(data->timerId);
1487 timeEndPeriod(data->period);
1488
1489 CloseHandle(data->host_alarm);
1490}
1491
thsefe75412007-08-24 01:36:32 +00001492static void win32_rearm_timer(struct qemu_alarm_timer *t)
1493{
1494 struct qemu_alarm_win32 *data = t->priv;
1495 uint64_t nearest_delta_us;
1496
1497 if (!active_timers[QEMU_TIMER_REALTIME] &&
1498 !active_timers[QEMU_TIMER_VIRTUAL])
1499 return;
1500
1501 nearest_delta_us = qemu_next_deadline();
1502 nearest_delta_us /= 1000;
1503
1504 timeKillEvent(data->timerId);
1505
1506 data->timerId = timeSetEvent(1,
1507 data->period,
1508 host_alarm_handler,
1509 (DWORD)t,
1510 TIME_ONESHOT | TIME_PERIODIC);
1511
1512 if (!data->timerId) {
1513 perror("Failed to re-arm win32 alarm timer");
1514
1515 timeEndPeriod(data->period);
1516 CloseHandle(data->host_alarm);
1517 exit(1);
1518 }
1519}
1520
thsc8994012007-08-19 21:56:03 +00001521#endif /* _WIN32 */
1522
bellard1dce7c32006-07-13 23:20:22 +00001523static void init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001524{
thsc8994012007-08-19 21:56:03 +00001525 struct qemu_alarm_timer *t;
1526 int i, err = -1;
bellard06d9f2f2006-05-01 13:23:04 +00001527
thsc8994012007-08-19 21:56:03 +00001528 for (i = 0; alarm_timers[i].name; i++) {
1529 t = &alarm_timers[i];
1530
thsc8994012007-08-19 21:56:03 +00001531 err = t->start(t);
1532 if (!err)
1533 break;
bellard67b915a2004-03-31 23:37:16 +00001534 }
bellardfd872592004-05-12 19:11:15 +00001535
thsc8994012007-08-19 21:56:03 +00001536 if (err) {
1537 fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1538 fprintf(stderr, "Terminating\n");
1539 exit(1);
bellard67b915a2004-03-31 23:37:16 +00001540 }
thsc8994012007-08-19 21:56:03 +00001541
1542 alarm_timer = t;
bellard8a7ddc32004-03-31 19:00:16 +00001543}
1544
pbrook9596ebb2007-11-18 01:44:38 +00001545static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001546{
thsc8994012007-08-19 21:56:03 +00001547 alarm_timer->stop(alarm_timer);
1548 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001549}
1550
bellardc4b1fcc2004-03-14 21:44:30 +00001551/***********************************************************/
bellard82c643f2004-07-14 17:28:13 +00001552/* character device */
bellardc45886d2004-01-05 00:02:06 +00001553
pbrooke5b0bc42007-01-27 23:46:43 +00001554static void qemu_chr_event(CharDriverState *s, int event)
1555{
1556 if (!s->chr_event)
1557 return;
1558 s->chr_event(s->handler_opaque, event);
1559}
1560
ths86e94de2007-01-05 22:01:59 +00001561static void qemu_chr_reset_bh(void *opaque)
1562{
1563 CharDriverState *s = opaque;
pbrooke5b0bc42007-01-27 23:46:43 +00001564 qemu_chr_event(s, CHR_EVENT_RESET);
ths86e94de2007-01-05 22:01:59 +00001565 qemu_bh_delete(s->bh);
1566 s->bh = NULL;
1567}
1568
1569void qemu_chr_reset(CharDriverState *s)
1570{
1571 if (s->bh == NULL) {
1572 s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1573 qemu_bh_schedule(s->bh);
1574 }
1575}
1576
bellard82c643f2004-07-14 17:28:13 +00001577int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
bellard67b915a2004-03-31 23:37:16 +00001578{
bellard82c643f2004-07-14 17:28:13 +00001579 return s->chr_write(s, buf, len);
bellard67b915a2004-03-31 23:37:16 +00001580}
1581
bellarde57a8c02005-11-10 23:58:52 +00001582int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00001583{
bellarde57a8c02005-11-10 23:58:52 +00001584 if (!s->chr_ioctl)
1585 return -ENOTSUP;
1586 return s->chr_ioctl(s, cmd, arg);
bellardf8d179e2005-11-08 22:30:36 +00001587}
1588
pbrooke5b0bc42007-01-27 23:46:43 +00001589int qemu_chr_can_read(CharDriverState *s)
1590{
1591 if (!s->chr_can_read)
1592 return 0;
1593 return s->chr_can_read(s->handler_opaque);
1594}
1595
1596void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1597{
1598 s->chr_read(s->handler_opaque, buf, len);
1599}
1600
balrogbd9bdce2007-11-25 00:55:06 +00001601void qemu_chr_accept_input(CharDriverState *s)
1602{
1603 if (s->chr_accept_input)
1604 s->chr_accept_input(s);
1605}
pbrooke5b0bc42007-01-27 23:46:43 +00001606
bellard82c643f2004-07-14 17:28:13 +00001607void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
bellard0824d6f2003-06-24 13:42:40 +00001608{
bellard82c643f2004-07-14 17:28:13 +00001609 char buf[4096];
1610 va_list ap;
1611 va_start(ap, fmt);
1612 vsnprintf(buf, sizeof(buf), fmt, ap);
1613 qemu_chr_write(s, buf, strlen(buf));
1614 va_end(ap);
1615}
1616
bellard5905b2e2004-08-01 21:53:26 +00001617void qemu_chr_send_event(CharDriverState *s, int event)
1618{
1619 if (s->chr_send_event)
1620 s->chr_send_event(s, event);
1621}
1622
ths5fafdf22007-09-16 21:08:06 +00001623void qemu_chr_add_handlers(CharDriverState *s,
1624 IOCanRWHandler *fd_can_read,
pbrooke5b0bc42007-01-27 23:46:43 +00001625 IOReadHandler *fd_read,
1626 IOEventHandler *fd_event,
1627 void *opaque)
bellard82c643f2004-07-14 17:28:13 +00001628{
pbrooke5b0bc42007-01-27 23:46:43 +00001629 s->chr_can_read = fd_can_read;
1630 s->chr_read = fd_read;
1631 s->chr_event = fd_event;
1632 s->handler_opaque = opaque;
1633 if (s->chr_update_read_handler)
1634 s->chr_update_read_handler(s);
bellard82c643f2004-07-14 17:28:13 +00001635}
ths3b46e622007-09-17 08:09:54 +00001636
bellard82c643f2004-07-14 17:28:13 +00001637static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1638{
1639 return len;
1640}
1641
ths52f61fd2006-12-22 21:20:52 +00001642static CharDriverState *qemu_chr_open_null(void)
bellard82c643f2004-07-14 17:28:13 +00001643{
1644 CharDriverState *chr;
1645
1646 chr = qemu_mallocz(sizeof(CharDriverState));
1647 if (!chr)
1648 return NULL;
1649 chr->chr_write = null_chr_write;
bellard82c643f2004-07-14 17:28:13 +00001650 return chr;
1651}
1652
ths20d8a3e2007-02-18 17:04:49 +00001653/* MUX driver for serial I/O splitting */
1654static int term_timestamps;
1655static int64_t term_timestamps_start;
ths9c1de612007-02-21 17:25:30 +00001656#define MAX_MUX 4
balrogbd9bdce2007-11-25 00:55:06 +00001657#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
1658#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
ths20d8a3e2007-02-18 17:04:49 +00001659typedef struct {
1660 IOCanRWHandler *chr_can_read[MAX_MUX];
1661 IOReadHandler *chr_read[MAX_MUX];
1662 IOEventHandler *chr_event[MAX_MUX];
1663 void *ext_opaque[MAX_MUX];
1664 CharDriverState *drv;
balrogbd9bdce2007-11-25 00:55:06 +00001665 unsigned char buffer[MUX_BUFFER_SIZE];
1666 int prod;
1667 int cons;
ths20d8a3e2007-02-18 17:04:49 +00001668 int mux_cnt;
1669 int term_got_escape;
1670 int max_size;
1671} MuxDriver;
1672
1673
1674static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1675{
1676 MuxDriver *d = chr->opaque;
1677 int ret;
1678 if (!term_timestamps) {
1679 ret = d->drv->chr_write(d->drv, buf, len);
1680 } else {
1681 int i;
1682
1683 ret = 0;
1684 for(i = 0; i < len; i++) {
1685 ret += d->drv->chr_write(d->drv, buf+i, 1);
1686 if (buf[i] == '\n') {
1687 char buf1[64];
1688 int64_t ti;
1689 int secs;
1690
1691 ti = get_clock();
1692 if (term_timestamps_start == -1)
1693 term_timestamps_start = ti;
1694 ti -= term_timestamps_start;
1695 secs = ti / 1000000000;
1696 snprintf(buf1, sizeof(buf1),
1697 "[%02d:%02d:%02d.%03d] ",
1698 secs / 3600,
1699 (secs / 60) % 60,
1700 secs % 60,
1701 (int)((ti / 1000000) % 1000));
1702 d->drv->chr_write(d->drv, buf1, strlen(buf1));
1703 }
1704 }
1705 }
1706 return ret;
1707}
1708
1709static char *mux_help[] = {
1710 "% h print this help\n\r",
1711 "% x exit emulator\n\r",
1712 "% s save disk data back to file (if -snapshot)\n\r",
1713 "% t toggle console timestamps\n\r"
1714 "% b send break (magic sysrq)\n\r",
1715 "% c switch between console and monitor\n\r",
1716 "% % sends %\n\r",
1717 NULL
1718};
1719
1720static int term_escape_char = 0x01; /* ctrl-a is used for escape */
1721static void mux_print_help(CharDriverState *chr)
1722{
1723 int i, j;
1724 char ebuf[15] = "Escape-Char";
1725 char cbuf[50] = "\n\r";
1726
1727 if (term_escape_char > 0 && term_escape_char < 26) {
1728 sprintf(cbuf,"\n\r");
1729 sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a');
1730 } else {
1731 sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char);
1732 }
1733 chr->chr_write(chr, cbuf, strlen(cbuf));
1734 for (i = 0; mux_help[i] != NULL; i++) {
1735 for (j=0; mux_help[i][j] != '\0'; j++) {
1736 if (mux_help[i][j] == '%')
1737 chr->chr_write(chr, ebuf, strlen(ebuf));
1738 else
1739 chr->chr_write(chr, &mux_help[i][j], 1);
1740 }
1741 }
1742}
1743
1744static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1745{
1746 if (d->term_got_escape) {
1747 d->term_got_escape = 0;
1748 if (ch == term_escape_char)
1749 goto send_char;
1750 switch(ch) {
1751 case '?':
1752 case 'h':
1753 mux_print_help(chr);
1754 break;
1755 case 'x':
1756 {
1757 char *term = "QEMU: Terminated\n\r";
1758 chr->chr_write(chr,term,strlen(term));
1759 exit(0);
1760 break;
1761 }
1762 case 's':
1763 {
1764 int i;
thse4bcb142007-12-02 04:51:10 +00001765 for (i = 0; i < nb_drives; i++) {
1766 bdrv_commit(drives_table[i].bdrv);
ths20d8a3e2007-02-18 17:04:49 +00001767 }
1768 }
1769 break;
1770 case 'b':
balrog36ddb832007-05-18 17:46:59 +00001771 qemu_chr_event(chr, CHR_EVENT_BREAK);
ths20d8a3e2007-02-18 17:04:49 +00001772 break;
1773 case 'c':
1774 /* Switch to the next registered device */
1775 chr->focus++;
1776 if (chr->focus >= d->mux_cnt)
1777 chr->focus = 0;
1778 break;
1779 case 't':
1780 term_timestamps = !term_timestamps;
1781 term_timestamps_start = -1;
1782 break;
1783 }
1784 } else if (ch == term_escape_char) {
1785 d->term_got_escape = 1;
1786 } else {
1787 send_char:
1788 return 1;
1789 }
1790 return 0;
1791}
1792
balrogbd9bdce2007-11-25 00:55:06 +00001793static void mux_chr_accept_input(CharDriverState *chr)
1794{
1795 int m = chr->focus;
1796 MuxDriver *d = chr->opaque;
1797
1798 while (d->prod != d->cons &&
1799 d->chr_can_read[m] &&
1800 d->chr_can_read[m](d->ext_opaque[m])) {
1801 d->chr_read[m](d->ext_opaque[m],
1802 &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
1803 }
1804}
1805
ths20d8a3e2007-02-18 17:04:49 +00001806static int mux_chr_can_read(void *opaque)
1807{
1808 CharDriverState *chr = opaque;
1809 MuxDriver *d = chr->opaque;
balrogbd9bdce2007-11-25 00:55:06 +00001810
1811 if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
1812 return 1;
ths20d8a3e2007-02-18 17:04:49 +00001813 if (d->chr_can_read[chr->focus])
balrogbd9bdce2007-11-25 00:55:06 +00001814 return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
ths20d8a3e2007-02-18 17:04:49 +00001815 return 0;
1816}
1817
1818static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
1819{
1820 CharDriverState *chr = opaque;
1821 MuxDriver *d = chr->opaque;
balrogbd9bdce2007-11-25 00:55:06 +00001822 int m = chr->focus;
ths20d8a3e2007-02-18 17:04:49 +00001823 int i;
balrogbd9bdce2007-11-25 00:55:06 +00001824
1825 mux_chr_accept_input (opaque);
1826
ths20d8a3e2007-02-18 17:04:49 +00001827 for(i = 0; i < size; i++)
balrogbd9bdce2007-11-25 00:55:06 +00001828 if (mux_proc_byte(chr, d, buf[i])) {
1829 if (d->prod == d->cons &&
1830 d->chr_can_read[m] &&
1831 d->chr_can_read[m](d->ext_opaque[m]))
1832 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
1833 else
1834 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
1835 }
ths20d8a3e2007-02-18 17:04:49 +00001836}
1837
1838static void mux_chr_event(void *opaque, int event)
1839{
1840 CharDriverState *chr = opaque;
1841 MuxDriver *d = chr->opaque;
1842 int i;
1843
1844 /* Send the event to all registered listeners */
1845 for (i = 0; i < d->mux_cnt; i++)
1846 if (d->chr_event[i])
1847 d->chr_event[i](d->ext_opaque[i], event);
1848}
1849
1850static void mux_chr_update_read_handler(CharDriverState *chr)
1851{
1852 MuxDriver *d = chr->opaque;
1853
1854 if (d->mux_cnt >= MAX_MUX) {
1855 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
1856 return;
1857 }
1858 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
1859 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
1860 d->chr_read[d->mux_cnt] = chr->chr_read;
1861 d->chr_event[d->mux_cnt] = chr->chr_event;
1862 /* Fix up the real driver with mux routines */
1863 if (d->mux_cnt == 0) {
1864 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
1865 mux_chr_event, chr);
1866 }
1867 chr->focus = d->mux_cnt;
1868 d->mux_cnt++;
1869}
1870
pbrook9596ebb2007-11-18 01:44:38 +00001871static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
ths20d8a3e2007-02-18 17:04:49 +00001872{
1873 CharDriverState *chr;
1874 MuxDriver *d;
1875
1876 chr = qemu_mallocz(sizeof(CharDriverState));
1877 if (!chr)
1878 return NULL;
1879 d = qemu_mallocz(sizeof(MuxDriver));
1880 if (!d) {
1881 free(chr);
1882 return NULL;
1883 }
1884
1885 chr->opaque = d;
1886 d->drv = drv;
1887 chr->focus = -1;
1888 chr->chr_write = mux_chr_write;
1889 chr->chr_update_read_handler = mux_chr_update_read_handler;
balrogbd9bdce2007-11-25 00:55:06 +00001890 chr->chr_accept_input = mux_chr_accept_input;
ths20d8a3e2007-02-18 17:04:49 +00001891 return chr;
1892}
1893
1894
bellardfd1dff42006-02-01 21:29:26 +00001895#ifdef _WIN32
bellard82c643f2004-07-14 17:28:13 +00001896
bellardfd1dff42006-02-01 21:29:26 +00001897static void socket_cleanup(void)
1898{
1899 WSACleanup();
1900}
bellard82c643f2004-07-14 17:28:13 +00001901
bellardfd1dff42006-02-01 21:29:26 +00001902static int socket_init(void)
1903{
1904 WSADATA Data;
1905 int ret, err;
1906
1907 ret = WSAStartup(MAKEWORD(2,2), &Data);
1908 if (ret != 0) {
1909 err = WSAGetLastError();
1910 fprintf(stderr, "WSAStartup: %d\n", err);
1911 return -1;
1912 }
1913 atexit(socket_cleanup);
1914 return 0;
1915}
1916
1917static int send_all(int fd, const uint8_t *buf, int len1)
1918{
1919 int ret, len;
ths3b46e622007-09-17 08:09:54 +00001920
bellardfd1dff42006-02-01 21:29:26 +00001921 len = len1;
1922 while (len > 0) {
1923 ret = send(fd, buf, len, 0);
1924 if (ret < 0) {
1925 int errno;
1926 errno = WSAGetLastError();
1927 if (errno != WSAEWOULDBLOCK) {
1928 return -1;
1929 }
1930 } else if (ret == 0) {
1931 break;
1932 } else {
1933 buf += ret;
1934 len -= ret;
1935 }
1936 }
1937 return len1 - len;
1938}
1939
1940void socket_set_nonblock(int fd)
1941{
1942 unsigned long opt = 1;
1943 ioctlsocket(fd, FIONBIO, &opt);
1944}
1945
1946#else
1947
bellard1d969052004-09-18 19:34:39 +00001948static int unix_write(int fd, const uint8_t *buf, int len1)
1949{
1950 int ret, len;
1951
1952 len = len1;
1953 while (len > 0) {
1954 ret = write(fd, buf, len);
1955 if (ret < 0) {
1956 if (errno != EINTR && errno != EAGAIN)
1957 return -1;
1958 } else if (ret == 0) {
1959 break;
1960 } else {
1961 buf += ret;
1962 len -= ret;
1963 }
1964 }
1965 return len1 - len;
1966}
1967
bellardfd1dff42006-02-01 21:29:26 +00001968static inline int send_all(int fd, const uint8_t *buf, int len1)
1969{
1970 return unix_write(fd, buf, len1);
1971}
1972
1973void socket_set_nonblock(int fd)
1974{
1975 fcntl(fd, F_SETFL, O_NONBLOCK);
1976}
1977#endif /* !_WIN32 */
1978
1979#ifndef _WIN32
1980
1981typedef struct {
1982 int fd_in, fd_out;
bellardfd1dff42006-02-01 21:29:26 +00001983 int max_size;
1984} FDCharDriver;
1985
ths20d8a3e2007-02-18 17:04:49 +00001986#define STDIO_MAX_CLIENTS 1
1987static int stdio_nb_clients = 0;
bellardfd1dff42006-02-01 21:29:26 +00001988
bellard82c643f2004-07-14 17:28:13 +00001989static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1990{
1991 FDCharDriver *s = chr->opaque;
bellard1d969052004-09-18 19:34:39 +00001992 return unix_write(s->fd_out, buf, len);
bellard82c643f2004-07-14 17:28:13 +00001993}
1994
bellard7c9d8e02005-11-15 22:16:05 +00001995static int fd_chr_read_poll(void *opaque)
1996{
1997 CharDriverState *chr = opaque;
1998 FDCharDriver *s = chr->opaque;
1999
pbrooke5b0bc42007-01-27 23:46:43 +00002000 s->max_size = qemu_chr_can_read(chr);
bellard7c9d8e02005-11-15 22:16:05 +00002001 return s->max_size;
2002}
2003
2004static void fd_chr_read(void *opaque)
2005{
2006 CharDriverState *chr = opaque;
2007 FDCharDriver *s = chr->opaque;
2008 int size, len;
2009 uint8_t buf[1024];
ths3b46e622007-09-17 08:09:54 +00002010
bellard7c9d8e02005-11-15 22:16:05 +00002011 len = sizeof(buf);
2012 if (len > s->max_size)
2013 len = s->max_size;
2014 if (len == 0)
2015 return;
2016 size = read(s->fd_in, buf, len);
pbrook188157f2006-11-01 01:44:16 +00002017 if (size == 0) {
2018 /* FD has been closed. Remove it from the active list. */
2019 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2020 return;
2021 }
bellard7c9d8e02005-11-15 22:16:05 +00002022 if (size > 0) {
pbrooke5b0bc42007-01-27 23:46:43 +00002023 qemu_chr_read(chr, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00002024 }
2025}
2026
pbrooke5b0bc42007-01-27 23:46:43 +00002027static void fd_chr_update_read_handler(CharDriverState *chr)
bellard82c643f2004-07-14 17:28:13 +00002028{
2029 FDCharDriver *s = chr->opaque;
2030
bellardf8d179e2005-11-08 22:30:36 +00002031 if (s->fd_in >= 0) {
2032 if (nographic && s->fd_in == 0) {
bellardf8d179e2005-11-08 22:30:36 +00002033 } else {
ths5fafdf22007-09-16 21:08:06 +00002034 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
bellard7c9d8e02005-11-15 22:16:05 +00002035 fd_chr_read, NULL, chr);
bellardf8d179e2005-11-08 22:30:36 +00002036 }
bellard0824d6f2003-06-24 13:42:40 +00002037 }
2038}
2039
bellard82c643f2004-07-14 17:28:13 +00002040/* open a character device to a unix fd */
ths52f61fd2006-12-22 21:20:52 +00002041static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
bellard82c643f2004-07-14 17:28:13 +00002042{
2043 CharDriverState *chr;
2044 FDCharDriver *s;
2045
2046 chr = qemu_mallocz(sizeof(CharDriverState));
2047 if (!chr)
2048 return NULL;
2049 s = qemu_mallocz(sizeof(FDCharDriver));
2050 if (!s) {
2051 free(chr);
2052 return NULL;
2053 }
2054 s->fd_in = fd_in;
2055 s->fd_out = fd_out;
2056 chr->opaque = s;
2057 chr->chr_write = fd_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002058 chr->chr_update_read_handler = fd_chr_update_read_handler;
ths86e94de2007-01-05 22:01:59 +00002059
2060 qemu_chr_reset(chr);
2061
bellard82c643f2004-07-14 17:28:13 +00002062 return chr;
2063}
2064
ths52f61fd2006-12-22 21:20:52 +00002065static CharDriverState *qemu_chr_open_file_out(const char *file_out)
bellardf8d179e2005-11-08 22:30:36 +00002066{
2067 int fd_out;
2068
balrogaeb30be2007-07-02 15:03:13 +00002069 TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
bellardf8d179e2005-11-08 22:30:36 +00002070 if (fd_out < 0)
2071 return NULL;
2072 return qemu_chr_open_fd(-1, fd_out);
2073}
2074
ths52f61fd2006-12-22 21:20:52 +00002075static CharDriverState *qemu_chr_open_pipe(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002076{
thsc26c1c42006-12-22 19:25:31 +00002077 int fd_in, fd_out;
2078 char filename_in[256], filename_out[256];
bellardf8d179e2005-11-08 22:30:36 +00002079
thsc26c1c42006-12-22 19:25:31 +00002080 snprintf(filename_in, 256, "%s.in", filename);
2081 snprintf(filename_out, 256, "%s.out", filename);
balrogaeb30be2007-07-02 15:03:13 +00002082 TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2083 TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002084 if (fd_in < 0 || fd_out < 0) {
2085 if (fd_in >= 0)
2086 close(fd_in);
2087 if (fd_out >= 0)
2088 close(fd_out);
balrogaeb30be2007-07-02 15:03:13 +00002089 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
thsc26c1c42006-12-22 19:25:31 +00002090 if (fd_in < 0)
2091 return NULL;
2092 }
2093 return qemu_chr_open_fd(fd_in, fd_out);
bellardf8d179e2005-11-08 22:30:36 +00002094}
2095
2096
bellard82c643f2004-07-14 17:28:13 +00002097/* for STDIO, we handle the case where several clients use it
2098 (nographic mode) */
2099
bellardaa0bc6b2005-09-03 15:28:58 +00002100#define TERM_FIFO_MAX_SIZE 1
2101
bellardaa0bc6b2005-09-03 15:28:58 +00002102static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
bellard1dce7c32006-07-13 23:20:22 +00002103static int term_fifo_size;
bellard82c643f2004-07-14 17:28:13 +00002104
bellard7c9d8e02005-11-15 22:16:05 +00002105static int stdio_read_poll(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002106{
ths20d8a3e2007-02-18 17:04:49 +00002107 CharDriverState *chr = opaque;
bellardaa0bc6b2005-09-03 15:28:58 +00002108
ths20d8a3e2007-02-18 17:04:49 +00002109 /* try to flush the queue if needed */
2110 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2111 qemu_chr_read(chr, term_fifo, 1);
2112 term_fifo_size = 0;
bellardaa0bc6b2005-09-03 15:28:58 +00002113 }
ths20d8a3e2007-02-18 17:04:49 +00002114 /* see if we can absorb more chars */
2115 if (term_fifo_size == 0)
2116 return 1;
2117 else
2118 return 0;
bellard82c643f2004-07-14 17:28:13 +00002119}
2120
bellard7c9d8e02005-11-15 22:16:05 +00002121static void stdio_read(void *opaque)
bellard82c643f2004-07-14 17:28:13 +00002122{
bellard7c9d8e02005-11-15 22:16:05 +00002123 int size;
2124 uint8_t buf[1];
ths20d8a3e2007-02-18 17:04:49 +00002125 CharDriverState *chr = opaque;
2126
bellard7c9d8e02005-11-15 22:16:05 +00002127 size = read(0, buf, 1);
pbrook519945d2006-09-10 14:39:54 +00002128 if (size == 0) {
2129 /* stdin has been closed. Remove it from the active list. */
2130 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2131 return;
2132 }
ths20d8a3e2007-02-18 17:04:49 +00002133 if (size > 0) {
2134 if (qemu_chr_can_read(chr) > 0) {
2135 qemu_chr_read(chr, buf, 1);
2136 } else if (term_fifo_size == 0) {
2137 term_fifo[term_fifo_size++] = buf[0];
bellard1dce7c32006-07-13 23:20:22 +00002138 }
bellard1dce7c32006-07-13 23:20:22 +00002139 }
2140}
2141
bellard8d11df92004-08-24 21:13:40 +00002142/* init terminal so that we can grab keys */
2143static struct termios oldtty;
2144static int old_fd0_flags;
2145
2146static void term_exit(void)
2147{
2148 tcsetattr (0, TCSANOW, &oldtty);
2149 fcntl(0, F_SETFL, old_fd0_flags);
2150}
2151
2152static void term_init(void)
2153{
2154 struct termios tty;
2155
2156 tcgetattr (0, &tty);
2157 oldtty = tty;
2158 old_fd0_flags = fcntl(0, F_GETFL);
2159
2160 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2161 |INLCR|IGNCR|ICRNL|IXON);
2162 tty.c_oflag |= OPOST;
2163 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2164 /* if graphical mode, we allow Ctrl-C handling */
2165 if (nographic)
2166 tty.c_lflag &= ~ISIG;
2167 tty.c_cflag &= ~(CSIZE|PARENB);
2168 tty.c_cflag |= CS8;
2169 tty.c_cc[VMIN] = 1;
2170 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +00002171
bellard8d11df92004-08-24 21:13:40 +00002172 tcsetattr (0, TCSANOW, &tty);
2173
2174 atexit(term_exit);
2175
2176 fcntl(0, F_SETFL, O_NONBLOCK);
2177}
2178
ths52f61fd2006-12-22 21:20:52 +00002179static CharDriverState *qemu_chr_open_stdio(void)
bellard82c643f2004-07-14 17:28:13 +00002180{
2181 CharDriverState *chr;
2182
ths20d8a3e2007-02-18 17:04:49 +00002183 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2184 return NULL;
2185 chr = qemu_chr_open_fd(0, 1);
2186 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2187 stdio_nb_clients++;
2188 term_init();
2189
bellard82c643f2004-07-14 17:28:13 +00002190 return chr;
2191}
2192
thsaec62502007-06-25 11:48:07 +00002193#if defined(__linux__) || defined(__sun__)
ths52f61fd2006-12-22 21:20:52 +00002194static CharDriverState *qemu_chr_open_pty(void)
bellard82c643f2004-07-14 17:28:13 +00002195{
bellard91fc2112005-12-18 19:09:37 +00002196 struct termios tty;
bellard82c643f2004-07-14 17:28:13 +00002197 char slave_name[1024];
2198 int master_fd, slave_fd;
ths3b46e622007-09-17 08:09:54 +00002199
thsaec62502007-06-25 11:48:07 +00002200#if defined(__linux__)
bellard82c643f2004-07-14 17:28:13 +00002201 /* Not satisfying */
2202 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
2203 return NULL;
2204 }
thsaec62502007-06-25 11:48:07 +00002205#endif
ths3b46e622007-09-17 08:09:54 +00002206
bellard91fc2112005-12-18 19:09:37 +00002207 /* Disabling local echo and line-buffered output */
2208 tcgetattr (master_fd, &tty);
2209 tty.c_lflag &= ~(ECHO|ICANON|ISIG);
2210 tty.c_cc[VMIN] = 1;
2211 tty.c_cc[VTIME] = 0;
2212 tcsetattr (master_fd, TCSAFLUSH, &tty);
2213
bellard82c643f2004-07-14 17:28:13 +00002214 fprintf(stderr, "char device redirected to %s\n", slave_name);
2215 return qemu_chr_open_fd(master_fd, master_fd);
2216}
bellardf8d179e2005-11-08 22:30:36 +00002217
ths5fafdf22007-09-16 21:08:06 +00002218static void tty_serial_init(int fd, int speed,
bellardf8d179e2005-11-08 22:30:36 +00002219 int parity, int data_bits, int stop_bits)
2220{
2221 struct termios tty;
2222 speed_t spd;
2223
bellarde57a8c02005-11-10 23:58:52 +00002224#if 0
ths5fafdf22007-09-16 21:08:06 +00002225 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
bellarde57a8c02005-11-10 23:58:52 +00002226 speed, parity, data_bits, stop_bits);
2227#endif
2228 tcgetattr (fd, &tty);
bellardf8d179e2005-11-08 22:30:36 +00002229
2230 switch(speed) {
2231 case 50:
2232 spd = B50;
2233 break;
2234 case 75:
2235 spd = B75;
2236 break;
2237 case 300:
2238 spd = B300;
2239 break;
2240 case 600:
2241 spd = B600;
2242 break;
2243 case 1200:
2244 spd = B1200;
2245 break;
2246 case 2400:
2247 spd = B2400;
2248 break;
2249 case 4800:
2250 spd = B4800;
2251 break;
2252 case 9600:
2253 spd = B9600;
2254 break;
2255 case 19200:
2256 spd = B19200;
2257 break;
2258 case 38400:
2259 spd = B38400;
2260 break;
2261 case 57600:
2262 spd = B57600;
2263 break;
2264 default:
2265 case 115200:
2266 spd = B115200;
2267 break;
2268 }
2269
2270 cfsetispeed(&tty, spd);
2271 cfsetospeed(&tty, spd);
2272
2273 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2274 |INLCR|IGNCR|ICRNL|IXON);
2275 tty.c_oflag |= OPOST;
2276 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
bellard094eed62006-09-09 11:10:18 +00002277 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
bellardf8d179e2005-11-08 22:30:36 +00002278 switch(data_bits) {
2279 default:
2280 case 8:
2281 tty.c_cflag |= CS8;
2282 break;
2283 case 7:
2284 tty.c_cflag |= CS7;
2285 break;
2286 case 6:
2287 tty.c_cflag |= CS6;
2288 break;
2289 case 5:
2290 tty.c_cflag |= CS5;
2291 break;
2292 }
2293 switch(parity) {
2294 default:
2295 case 'N':
2296 break;
2297 case 'E':
2298 tty.c_cflag |= PARENB;
2299 break;
2300 case 'O':
2301 tty.c_cflag |= PARENB | PARODD;
2302 break;
2303 }
bellard094eed62006-09-09 11:10:18 +00002304 if (stop_bits == 2)
2305 tty.c_cflag |= CSTOPB;
ths3b46e622007-09-17 08:09:54 +00002306
bellardf8d179e2005-11-08 22:30:36 +00002307 tcsetattr (fd, TCSANOW, &tty);
2308}
2309
bellarde57a8c02005-11-10 23:58:52 +00002310static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
bellardf8d179e2005-11-08 22:30:36 +00002311{
2312 FDCharDriver *s = chr->opaque;
ths3b46e622007-09-17 08:09:54 +00002313
bellarde57a8c02005-11-10 23:58:52 +00002314 switch(cmd) {
2315 case CHR_IOCTL_SERIAL_SET_PARAMS:
2316 {
2317 QEMUSerialSetParams *ssp = arg;
ths5fafdf22007-09-16 21:08:06 +00002318 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
bellarde57a8c02005-11-10 23:58:52 +00002319 ssp->data_bits, ssp->stop_bits);
2320 }
2321 break;
2322 case CHR_IOCTL_SERIAL_SET_BREAK:
2323 {
2324 int enable = *(int *)arg;
2325 if (enable)
2326 tcsendbreak(s->fd_in, 1);
2327 }
2328 break;
2329 default:
2330 return -ENOTSUP;
2331 }
2332 return 0;
bellardf8d179e2005-11-08 22:30:36 +00002333}
2334
ths52f61fd2006-12-22 21:20:52 +00002335static CharDriverState *qemu_chr_open_tty(const char *filename)
bellardf8d179e2005-11-08 22:30:36 +00002336{
2337 CharDriverState *chr;
2338 int fd;
2339
balrogaeb30be2007-07-02 15:03:13 +00002340 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
bellardf8d179e2005-11-08 22:30:36 +00002341 fcntl(fd, F_SETFL, O_NONBLOCK);
2342 tty_serial_init(fd, 115200, 'N', 8, 1);
2343 chr = qemu_chr_open_fd(fd, fd);
balrogaeb30be2007-07-02 15:03:13 +00002344 if (!chr) {
2345 close(fd);
bellardf8d179e2005-11-08 22:30:36 +00002346 return NULL;
balrogaeb30be2007-07-02 15:03:13 +00002347 }
bellarde57a8c02005-11-10 23:58:52 +00002348 chr->chr_ioctl = tty_serial_ioctl;
ths86e94de2007-01-05 22:01:59 +00002349 qemu_chr_reset(chr);
bellarde57a8c02005-11-10 23:58:52 +00002350 return chr;
2351}
thsaec62502007-06-25 11:48:07 +00002352#else /* ! __linux__ && ! __sun__ */
2353static CharDriverState *qemu_chr_open_pty(void)
2354{
2355 return NULL;
2356}
2357#endif /* __linux__ || __sun__ */
bellarde57a8c02005-11-10 23:58:52 +00002358
thsaec62502007-06-25 11:48:07 +00002359#if defined(__linux__)
ths5867c882007-02-17 23:44:43 +00002360typedef struct {
2361 int fd;
2362 int mode;
2363} ParallelCharDriver;
2364
2365static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2366{
2367 if (s->mode != mode) {
2368 int m = mode;
2369 if (ioctl(s->fd, PPSETMODE, &m) < 0)
2370 return 0;
2371 s->mode = mode;
2372 }
2373 return 1;
2374}
2375
bellarde57a8c02005-11-10 23:58:52 +00002376static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2377{
ths5867c882007-02-17 23:44:43 +00002378 ParallelCharDriver *drv = chr->opaque;
2379 int fd = drv->fd;
bellarde57a8c02005-11-10 23:58:52 +00002380 uint8_t b;
2381
2382 switch(cmd) {
2383 case CHR_IOCTL_PP_READ_DATA:
2384 if (ioctl(fd, PPRDATA, &b) < 0)
2385 return -ENOTSUP;
2386 *(uint8_t *)arg = b;
2387 break;
2388 case CHR_IOCTL_PP_WRITE_DATA:
2389 b = *(uint8_t *)arg;
2390 if (ioctl(fd, PPWDATA, &b) < 0)
2391 return -ENOTSUP;
2392 break;
2393 case CHR_IOCTL_PP_READ_CONTROL:
2394 if (ioctl(fd, PPRCONTROL, &b) < 0)
2395 return -ENOTSUP;
ths5867c882007-02-17 23:44:43 +00002396 /* Linux gives only the lowest bits, and no way to know data
2397 direction! For better compatibility set the fixed upper
2398 bits. */
2399 *(uint8_t *)arg = b | 0xc0;
bellarde57a8c02005-11-10 23:58:52 +00002400 break;
2401 case CHR_IOCTL_PP_WRITE_CONTROL:
2402 b = *(uint8_t *)arg;
2403 if (ioctl(fd, PPWCONTROL, &b) < 0)
2404 return -ENOTSUP;
2405 break;
2406 case CHR_IOCTL_PP_READ_STATUS:
2407 if (ioctl(fd, PPRSTATUS, &b) < 0)
2408 return -ENOTSUP;
2409 *(uint8_t *)arg = b;
2410 break;
ths5867c882007-02-17 23:44:43 +00002411 case CHR_IOCTL_PP_EPP_READ_ADDR:
2412 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2413 struct ParallelIOArg *parg = arg;
2414 int n = read(fd, parg->buffer, parg->count);
2415 if (n != parg->count) {
2416 return -EIO;
2417 }
2418 }
2419 break;
2420 case CHR_IOCTL_PP_EPP_READ:
2421 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2422 struct ParallelIOArg *parg = arg;
2423 int n = read(fd, parg->buffer, parg->count);
2424 if (n != parg->count) {
2425 return -EIO;
2426 }
2427 }
2428 break;
2429 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2430 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2431 struct ParallelIOArg *parg = arg;
2432 int n = write(fd, parg->buffer, parg->count);
2433 if (n != parg->count) {
2434 return -EIO;
2435 }
2436 }
2437 break;
2438 case CHR_IOCTL_PP_EPP_WRITE:
2439 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2440 struct ParallelIOArg *parg = arg;
2441 int n = write(fd, parg->buffer, parg->count);
2442 if (n != parg->count) {
2443 return -EIO;
2444 }
2445 }
2446 break;
bellarde57a8c02005-11-10 23:58:52 +00002447 default:
2448 return -ENOTSUP;
2449 }
2450 return 0;
2451}
2452
ths5867c882007-02-17 23:44:43 +00002453static void pp_close(CharDriverState *chr)
2454{
2455 ParallelCharDriver *drv = chr->opaque;
2456 int fd = drv->fd;
2457
2458 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2459 ioctl(fd, PPRELEASE);
2460 close(fd);
2461 qemu_free(drv);
2462}
2463
ths52f61fd2006-12-22 21:20:52 +00002464static CharDriverState *qemu_chr_open_pp(const char *filename)
bellarde57a8c02005-11-10 23:58:52 +00002465{
2466 CharDriverState *chr;
ths5867c882007-02-17 23:44:43 +00002467 ParallelCharDriver *drv;
bellarde57a8c02005-11-10 23:58:52 +00002468 int fd;
2469
balrogaeb30be2007-07-02 15:03:13 +00002470 TFR(fd = open(filename, O_RDWR));
bellarde57a8c02005-11-10 23:58:52 +00002471 if (fd < 0)
2472 return NULL;
2473
2474 if (ioctl(fd, PPCLAIM) < 0) {
2475 close(fd);
2476 return NULL;
2477 }
2478
ths5867c882007-02-17 23:44:43 +00002479 drv = qemu_mallocz(sizeof(ParallelCharDriver));
2480 if (!drv) {
bellarde57a8c02005-11-10 23:58:52 +00002481 close(fd);
2482 return NULL;
2483 }
ths5867c882007-02-17 23:44:43 +00002484 drv->fd = fd;
2485 drv->mode = IEEE1284_MODE_COMPAT;
2486
2487 chr = qemu_mallocz(sizeof(CharDriverState));
2488 if (!chr) {
2489 qemu_free(drv);
2490 close(fd);
2491 return NULL;
2492 }
bellarde57a8c02005-11-10 23:58:52 +00002493 chr->chr_write = null_chr_write;
bellarde57a8c02005-11-10 23:58:52 +00002494 chr->chr_ioctl = pp_ioctl;
ths5867c882007-02-17 23:44:43 +00002495 chr->chr_close = pp_close;
2496 chr->opaque = drv;
ths86e94de2007-01-05 22:01:59 +00002497
2498 qemu_chr_reset(chr);
2499
bellardf8d179e2005-11-08 22:30:36 +00002500 return chr;
2501}
thsaec62502007-06-25 11:48:07 +00002502#endif /* __linux__ */
bellardf8d179e2005-11-08 22:30:36 +00002503
thsaec62502007-06-25 11:48:07 +00002504#else /* _WIN32 */
bellard67b915a2004-03-31 23:37:16 +00002505
bellardf3311102006-04-12 20:21:17 +00002506typedef struct {
bellardf3311102006-04-12 20:21:17 +00002507 int max_size;
2508 HANDLE hcom, hrecv, hsend;
2509 OVERLAPPED orecv, osend;
2510 BOOL fpipe;
2511 DWORD len;
2512} WinCharState;
2513
2514#define NSENDBUF 2048
2515#define NRECVBUF 2048
2516#define MAXCONNECT 1
2517#define NTIMEOUT 5000
2518
2519static int win_chr_poll(void *opaque);
2520static int win_chr_pipe_poll(void *opaque);
2521
ths087f4ae2007-02-10 21:50:42 +00002522static void win_chr_close(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002523{
ths087f4ae2007-02-10 21:50:42 +00002524 WinCharState *s = chr->opaque;
2525
bellardf3311102006-04-12 20:21:17 +00002526 if (s->hsend) {
2527 CloseHandle(s->hsend);
2528 s->hsend = NULL;
2529 }
2530 if (s->hrecv) {
2531 CloseHandle(s->hrecv);
2532 s->hrecv = NULL;
2533 }
2534 if (s->hcom) {
2535 CloseHandle(s->hcom);
2536 s->hcom = NULL;
2537 }
2538 if (s->fpipe)
ths087f4ae2007-02-10 21:50:42 +00002539 qemu_del_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002540 else
ths087f4ae2007-02-10 21:50:42 +00002541 qemu_del_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002542}
2543
ths087f4ae2007-02-10 21:50:42 +00002544static int win_chr_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002545{
2546 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002547 COMMCONFIG comcfg;
2548 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2549 COMSTAT comstat;
2550 DWORD size;
2551 DWORD err;
ths3b46e622007-09-17 08:09:54 +00002552
bellardf3311102006-04-12 20:21:17 +00002553 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2554 if (!s->hsend) {
2555 fprintf(stderr, "Failed CreateEvent\n");
2556 goto fail;
2557 }
2558 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2559 if (!s->hrecv) {
2560 fprintf(stderr, "Failed CreateEvent\n");
2561 goto fail;
2562 }
2563
2564 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2565 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2566 if (s->hcom == INVALID_HANDLE_VALUE) {
2567 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2568 s->hcom = NULL;
2569 goto fail;
2570 }
ths3b46e622007-09-17 08:09:54 +00002571
bellardf3311102006-04-12 20:21:17 +00002572 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2573 fprintf(stderr, "Failed SetupComm\n");
2574 goto fail;
2575 }
ths3b46e622007-09-17 08:09:54 +00002576
bellardf3311102006-04-12 20:21:17 +00002577 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2578 size = sizeof(COMMCONFIG);
2579 GetDefaultCommConfig(filename, &comcfg, &size);
2580 comcfg.dcb.DCBlength = sizeof(DCB);
2581 CommConfigDialog(filename, NULL, &comcfg);
2582
2583 if (!SetCommState(s->hcom, &comcfg.dcb)) {
2584 fprintf(stderr, "Failed SetCommState\n");
2585 goto fail;
2586 }
2587
2588 if (!SetCommMask(s->hcom, EV_ERR)) {
2589 fprintf(stderr, "Failed SetCommMask\n");
2590 goto fail;
2591 }
2592
2593 cto.ReadIntervalTimeout = MAXDWORD;
2594 if (!SetCommTimeouts(s->hcom, &cto)) {
2595 fprintf(stderr, "Failed SetCommTimeouts\n");
2596 goto fail;
2597 }
ths3b46e622007-09-17 08:09:54 +00002598
bellardf3311102006-04-12 20:21:17 +00002599 if (!ClearCommError(s->hcom, &err, &comstat)) {
2600 fprintf(stderr, "Failed ClearCommError\n");
2601 goto fail;
2602 }
ths087f4ae2007-02-10 21:50:42 +00002603 qemu_add_polling_cb(win_chr_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002604 return 0;
2605
2606 fail:
ths087f4ae2007-02-10 21:50:42 +00002607 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002608 return -1;
2609}
2610
2611static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2612{
2613 WinCharState *s = chr->opaque;
2614 DWORD len, ret, size, err;
2615
2616 len = len1;
2617 ZeroMemory(&s->osend, sizeof(s->osend));
2618 s->osend.hEvent = s->hsend;
2619 while (len > 0) {
2620 if (s->hsend)
2621 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2622 else
2623 ret = WriteFile(s->hcom, buf, len, &size, NULL);
2624 if (!ret) {
2625 err = GetLastError();
2626 if (err == ERROR_IO_PENDING) {
2627 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2628 if (ret) {
2629 buf += size;
2630 len -= size;
2631 } else {
2632 break;
2633 }
2634 } else {
2635 break;
2636 }
2637 } else {
2638 buf += size;
2639 len -= size;
2640 }
2641 }
2642 return len1 - len;
2643}
2644
ths087f4ae2007-02-10 21:50:42 +00002645static int win_chr_read_poll(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002646{
ths087f4ae2007-02-10 21:50:42 +00002647 WinCharState *s = chr->opaque;
2648
2649 s->max_size = qemu_chr_can_read(chr);
bellardf3311102006-04-12 20:21:17 +00002650 return s->max_size;
2651}
pbrooke5b0bc42007-01-27 23:46:43 +00002652
ths087f4ae2007-02-10 21:50:42 +00002653static void win_chr_readfile(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002654{
ths087f4ae2007-02-10 21:50:42 +00002655 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002656 int ret, err;
2657 uint8_t buf[1024];
2658 DWORD size;
ths3b46e622007-09-17 08:09:54 +00002659
bellardf3311102006-04-12 20:21:17 +00002660 ZeroMemory(&s->orecv, sizeof(s->orecv));
2661 s->orecv.hEvent = s->hrecv;
2662 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2663 if (!ret) {
2664 err = GetLastError();
2665 if (err == ERROR_IO_PENDING) {
2666 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2667 }
2668 }
2669
2670 if (size > 0) {
ths087f4ae2007-02-10 21:50:42 +00002671 qemu_chr_read(chr, buf, size);
bellardf3311102006-04-12 20:21:17 +00002672 }
2673}
2674
ths087f4ae2007-02-10 21:50:42 +00002675static void win_chr_read(CharDriverState *chr)
bellardf3311102006-04-12 20:21:17 +00002676{
ths087f4ae2007-02-10 21:50:42 +00002677 WinCharState *s = chr->opaque;
2678
bellardf3311102006-04-12 20:21:17 +00002679 if (s->len > s->max_size)
2680 s->len = s->max_size;
2681 if (s->len == 0)
2682 return;
ths3b46e622007-09-17 08:09:54 +00002683
ths087f4ae2007-02-10 21:50:42 +00002684 win_chr_readfile(chr);
bellardf3311102006-04-12 20:21:17 +00002685}
2686
2687static int win_chr_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 COMSTAT status;
2692 DWORD comerr;
ths3b46e622007-09-17 08:09:54 +00002693
bellardf3311102006-04-12 20:21:17 +00002694 ClearCommError(s->hcom, &comerr, &status);
2695 if (status.cbInQue > 0) {
2696 s->len = status.cbInQue;
ths087f4ae2007-02-10 21:50:42 +00002697 win_chr_read_poll(chr);
2698 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002699 return 1;
2700 }
2701 return 0;
2702}
2703
ths52f61fd2006-12-22 21:20:52 +00002704static CharDriverState *qemu_chr_open_win(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002705{
2706 CharDriverState *chr;
2707 WinCharState *s;
ths3b46e622007-09-17 08:09:54 +00002708
bellardf3311102006-04-12 20:21:17 +00002709 chr = qemu_mallocz(sizeof(CharDriverState));
2710 if (!chr)
2711 return NULL;
2712 s = qemu_mallocz(sizeof(WinCharState));
2713 if (!s) {
2714 free(chr);
2715 return NULL;
2716 }
2717 chr->opaque = s;
2718 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002719 chr->chr_close = win_chr_close;
2720
ths087f4ae2007-02-10 21:50:42 +00002721 if (win_chr_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002722 free(s);
2723 free(chr);
2724 return NULL;
2725 }
ths86e94de2007-01-05 22:01:59 +00002726 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002727 return chr;
2728}
2729
2730static int win_chr_pipe_poll(void *opaque)
2731{
ths087f4ae2007-02-10 21:50:42 +00002732 CharDriverState *chr = opaque;
2733 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002734 DWORD size;
2735
2736 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2737 if (size > 0) {
2738 s->len = size;
ths087f4ae2007-02-10 21:50:42 +00002739 win_chr_read_poll(chr);
2740 win_chr_read(chr);
bellardf3311102006-04-12 20:21:17 +00002741 return 1;
2742 }
2743 return 0;
2744}
2745
ths087f4ae2007-02-10 21:50:42 +00002746static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
bellardf3311102006-04-12 20:21:17 +00002747{
ths087f4ae2007-02-10 21:50:42 +00002748 WinCharState *s = chr->opaque;
bellardf3311102006-04-12 20:21:17 +00002749 OVERLAPPED ov;
2750 int ret;
2751 DWORD size;
2752 char openname[256];
ths3b46e622007-09-17 08:09:54 +00002753
bellardf3311102006-04-12 20:21:17 +00002754 s->fpipe = TRUE;
2755
2756 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2757 if (!s->hsend) {
2758 fprintf(stderr, "Failed CreateEvent\n");
2759 goto fail;
2760 }
2761 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2762 if (!s->hrecv) {
2763 fprintf(stderr, "Failed CreateEvent\n");
2764 goto fail;
2765 }
ths3b46e622007-09-17 08:09:54 +00002766
bellardf3311102006-04-12 20:21:17 +00002767 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2768 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2769 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2770 PIPE_WAIT,
2771 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2772 if (s->hcom == INVALID_HANDLE_VALUE) {
2773 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2774 s->hcom = NULL;
2775 goto fail;
2776 }
2777
2778 ZeroMemory(&ov, sizeof(ov));
2779 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2780 ret = ConnectNamedPipe(s->hcom, &ov);
2781 if (ret) {
2782 fprintf(stderr, "Failed ConnectNamedPipe\n");
2783 goto fail;
2784 }
2785
2786 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2787 if (!ret) {
2788 fprintf(stderr, "Failed GetOverlappedResult\n");
2789 if (ov.hEvent) {
2790 CloseHandle(ov.hEvent);
2791 ov.hEvent = NULL;
2792 }
2793 goto fail;
2794 }
2795
2796 if (ov.hEvent) {
2797 CloseHandle(ov.hEvent);
2798 ov.hEvent = NULL;
2799 }
ths087f4ae2007-02-10 21:50:42 +00002800 qemu_add_polling_cb(win_chr_pipe_poll, chr);
bellardf3311102006-04-12 20:21:17 +00002801 return 0;
2802
2803 fail:
ths087f4ae2007-02-10 21:50:42 +00002804 win_chr_close(chr);
bellardf3311102006-04-12 20:21:17 +00002805 return -1;
2806}
2807
2808
ths52f61fd2006-12-22 21:20:52 +00002809static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
bellardf3311102006-04-12 20:21:17 +00002810{
2811 CharDriverState *chr;
2812 WinCharState *s;
2813
2814 chr = qemu_mallocz(sizeof(CharDriverState));
2815 if (!chr)
2816 return NULL;
2817 s = qemu_mallocz(sizeof(WinCharState));
2818 if (!s) {
2819 free(chr);
2820 return NULL;
2821 }
2822 chr->opaque = s;
2823 chr->chr_write = win_chr_write;
bellardf3311102006-04-12 20:21:17 +00002824 chr->chr_close = win_chr_close;
ths3b46e622007-09-17 08:09:54 +00002825
ths087f4ae2007-02-10 21:50:42 +00002826 if (win_chr_pipe_init(chr, filename) < 0) {
bellardf3311102006-04-12 20:21:17 +00002827 free(s);
2828 free(chr);
2829 return NULL;
2830 }
ths86e94de2007-01-05 22:01:59 +00002831 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002832 return chr;
2833}
2834
ths52f61fd2006-12-22 21:20:52 +00002835static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
bellardf3311102006-04-12 20:21:17 +00002836{
2837 CharDriverState *chr;
2838 WinCharState *s;
2839
2840 chr = qemu_mallocz(sizeof(CharDriverState));
2841 if (!chr)
2842 return NULL;
2843 s = qemu_mallocz(sizeof(WinCharState));
2844 if (!s) {
2845 free(chr);
2846 return NULL;
2847 }
2848 s->hcom = fd_out;
2849 chr->opaque = s;
2850 chr->chr_write = win_chr_write;
ths86e94de2007-01-05 22:01:59 +00002851 qemu_chr_reset(chr);
bellardf3311102006-04-12 20:21:17 +00002852 return chr;
2853}
ths72d46472007-05-13 14:54:54 +00002854
2855static CharDriverState *qemu_chr_open_win_con(const char *filename)
2856{
2857 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2858}
2859
ths52f61fd2006-12-22 21:20:52 +00002860static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
bellardf3311102006-04-12 20:21:17 +00002861{
2862 HANDLE fd_out;
ths3b46e622007-09-17 08:09:54 +00002863
bellardf3311102006-04-12 20:21:17 +00002864 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
2865 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2866 if (fd_out == INVALID_HANDLE_VALUE)
2867 return NULL;
2868
2869 return qemu_chr_open_win_file(fd_out);
2870}
thsaec62502007-06-25 11:48:07 +00002871#endif /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00002872
bellard0bab00f2006-06-25 14:49:44 +00002873/***********************************************************/
2874/* UDP Net console */
2875
2876typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00002877 int fd;
2878 struct sockaddr_in daddr;
2879 char buf[1024];
2880 int bufcnt;
2881 int bufptr;
2882 int max_size;
2883} NetCharDriver;
2884
2885static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2886{
2887 NetCharDriver *s = chr->opaque;
2888
2889 return sendto(s->fd, buf, len, 0,
2890 (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
2891}
2892
2893static int udp_chr_read_poll(void *opaque)
2894{
2895 CharDriverState *chr = opaque;
2896 NetCharDriver *s = chr->opaque;
2897
pbrooke5b0bc42007-01-27 23:46:43 +00002898 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002899
2900 /* If there were any stray characters in the queue process them
2901 * first
2902 */
2903 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002904 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002905 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002906 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002907 }
2908 return s->max_size;
2909}
2910
2911static void udp_chr_read(void *opaque)
2912{
2913 CharDriverState *chr = opaque;
2914 NetCharDriver *s = chr->opaque;
2915
2916 if (s->max_size == 0)
2917 return;
2918 s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
2919 s->bufptr = s->bufcnt;
2920 if (s->bufcnt <= 0)
2921 return;
2922
2923 s->bufptr = 0;
2924 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
pbrooke5b0bc42007-01-27 23:46:43 +00002925 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
bellard0bab00f2006-06-25 14:49:44 +00002926 s->bufptr++;
pbrooke5b0bc42007-01-27 23:46:43 +00002927 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00002928 }
2929}
2930
pbrooke5b0bc42007-01-27 23:46:43 +00002931static void udp_chr_update_read_handler(CharDriverState *chr)
bellard0bab00f2006-06-25 14:49:44 +00002932{
2933 NetCharDriver *s = chr->opaque;
2934
2935 if (s->fd >= 0) {
bellard0bab00f2006-06-25 14:49:44 +00002936 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
2937 udp_chr_read, NULL, chr);
2938 }
2939}
2940
2941int parse_host_port(struct sockaddr_in *saddr, const char *str);
ths52f61fd2006-12-22 21:20:52 +00002942#ifndef _WIN32
2943static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
2944#endif
bellard951f1352006-06-27 21:02:43 +00002945int parse_host_src_port(struct sockaddr_in *haddr,
2946 struct sockaddr_in *saddr,
2947 const char *str);
bellard0bab00f2006-06-25 14:49:44 +00002948
ths52f61fd2006-12-22 21:20:52 +00002949static CharDriverState *qemu_chr_open_udp(const char *def)
bellard0bab00f2006-06-25 14:49:44 +00002950{
2951 CharDriverState *chr = NULL;
2952 NetCharDriver *s = NULL;
2953 int fd = -1;
bellard951f1352006-06-27 21:02:43 +00002954 struct sockaddr_in saddr;
bellard0bab00f2006-06-25 14:49:44 +00002955
2956 chr = qemu_mallocz(sizeof(CharDriverState));
2957 if (!chr)
2958 goto return_err;
2959 s = qemu_mallocz(sizeof(NetCharDriver));
2960 if (!s)
2961 goto return_err;
2962
2963 fd = socket(PF_INET, SOCK_DGRAM, 0);
2964 if (fd < 0) {
2965 perror("socket(PF_INET, SOCK_DGRAM)");
2966 goto return_err;
2967 }
2968
bellard951f1352006-06-27 21:02:43 +00002969 if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
2970 printf("Could not parse: %s\n", def);
2971 goto return_err;
bellard0bab00f2006-06-25 14:49:44 +00002972 }
2973
bellard951f1352006-06-27 21:02:43 +00002974 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
bellard0bab00f2006-06-25 14:49:44 +00002975 {
2976 perror("bind");
2977 goto return_err;
2978 }
2979
2980 s->fd = fd;
2981 s->bufcnt = 0;
2982 s->bufptr = 0;
2983 chr->opaque = s;
2984 chr->chr_write = udp_chr_write;
pbrooke5b0bc42007-01-27 23:46:43 +00002985 chr->chr_update_read_handler = udp_chr_update_read_handler;
bellard0bab00f2006-06-25 14:49:44 +00002986 return chr;
2987
2988return_err:
2989 if (chr)
2990 free(chr);
2991 if (s)
2992 free(s);
2993 if (fd >= 0)
2994 closesocket(fd);
2995 return NULL;
2996}
2997
2998/***********************************************************/
2999/* TCP Net console */
3000
3001typedef struct {
bellard0bab00f2006-06-25 14:49:44 +00003002 int fd, listen_fd;
3003 int connected;
3004 int max_size;
bellard951f1352006-06-27 21:02:43 +00003005 int do_telnetopt;
pbrooke5b0bc42007-01-27 23:46:43 +00003006 int do_nodelay;
thsffd843b2006-12-21 19:46:43 +00003007 int is_unix;
bellard0bab00f2006-06-25 14:49:44 +00003008} TCPCharDriver;
3009
3010static void tcp_chr_accept(void *opaque);
3011
3012static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3013{
3014 TCPCharDriver *s = chr->opaque;
3015 if (s->connected) {
3016 return send_all(s->fd, buf, len);
3017 } else {
3018 /* XXX: indicate an error ? */
3019 return len;
3020 }
3021}
3022
3023static int tcp_chr_read_poll(void *opaque)
3024{
3025 CharDriverState *chr = opaque;
3026 TCPCharDriver *s = chr->opaque;
3027 if (!s->connected)
3028 return 0;
pbrooke5b0bc42007-01-27 23:46:43 +00003029 s->max_size = qemu_chr_can_read(chr);
bellard0bab00f2006-06-25 14:49:44 +00003030 return s->max_size;
3031}
3032
bellard951f1352006-06-27 21:02:43 +00003033#define IAC 255
3034#define IAC_BREAK 243
3035static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
3036 TCPCharDriver *s,
3037 char *buf, int *size)
3038{
3039 /* Handle any telnet client's basic IAC options to satisfy char by
3040 * char mode with no echo. All IAC options will be removed from
3041 * the buf and the do_telnetopt variable will be used to track the
3042 * state of the width of the IAC information.
3043 *
3044 * IAC commands come in sets of 3 bytes with the exception of the
3045 * "IAC BREAK" command and the double IAC.
3046 */
3047
3048 int i;
3049 int j = 0;
3050
3051 for (i = 0; i < *size; i++) {
3052 if (s->do_telnetopt > 1) {
3053 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3054 /* Double IAC means send an IAC */
3055 if (j != i)
3056 buf[j] = buf[i];
3057 j++;
3058 s->do_telnetopt = 1;
3059 } else {
3060 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3061 /* Handle IAC break commands by sending a serial break */
pbrooke5b0bc42007-01-27 23:46:43 +00003062 qemu_chr_event(chr, CHR_EVENT_BREAK);
bellard951f1352006-06-27 21:02:43 +00003063 s->do_telnetopt++;
3064 }
3065 s->do_telnetopt++;
3066 }
3067 if (s->do_telnetopt >= 4) {
3068 s->do_telnetopt = 1;
3069 }
3070 } else {
3071 if ((unsigned char)buf[i] == IAC) {
3072 s->do_telnetopt = 2;
3073 } else {
3074 if (j != i)
3075 buf[j] = buf[i];
3076 j++;
3077 }
3078 }
3079 }
3080 *size = j;
3081}
3082
bellard0bab00f2006-06-25 14:49:44 +00003083static void tcp_chr_read(void *opaque)
3084{
3085 CharDriverState *chr = opaque;
3086 TCPCharDriver *s = chr->opaque;
3087 uint8_t buf[1024];
3088 int len, size;
3089
3090 if (!s->connected || s->max_size <= 0)
3091 return;
3092 len = sizeof(buf);
3093 if (len > s->max_size)
3094 len = s->max_size;
3095 size = recv(s->fd, buf, len, 0);
3096 if (size == 0) {
3097 /* connection closed */
3098 s->connected = 0;
3099 if (s->listen_fd >= 0) {
3100 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3101 }
3102 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3103 closesocket(s->fd);
3104 s->fd = -1;
3105 } else if (size > 0) {
bellard951f1352006-06-27 21:02:43 +00003106 if (s->do_telnetopt)
3107 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3108 if (size > 0)
pbrooke5b0bc42007-01-27 23:46:43 +00003109 qemu_chr_read(chr, buf, size);
bellard0bab00f2006-06-25 14:49:44 +00003110 }
3111}
3112
bellard0bab00f2006-06-25 14:49:44 +00003113static void tcp_chr_connect(void *opaque)
3114{
3115 CharDriverState *chr = opaque;
3116 TCPCharDriver *s = chr->opaque;
3117
3118 s->connected = 1;
3119 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3120 tcp_chr_read, NULL, chr);
ths86e94de2007-01-05 22:01:59 +00003121 qemu_chr_reset(chr);
bellard0bab00f2006-06-25 14:49:44 +00003122}
3123
bellard951f1352006-06-27 21:02:43 +00003124#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3125static void tcp_chr_telnet_init(int fd)
3126{
3127 char buf[3];
3128 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3129 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3130 send(fd, (char *)buf, 3, 0);
3131 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3132 send(fd, (char *)buf, 3, 0);
3133 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3134 send(fd, (char *)buf, 3, 0);
3135 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3136 send(fd, (char *)buf, 3, 0);
3137}
3138
pbrookf7499982007-01-28 00:10:01 +00003139static void socket_set_nodelay(int fd)
3140{
3141 int val = 1;
3142 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3143}
3144
bellard0bab00f2006-06-25 14:49:44 +00003145static void tcp_chr_accept(void *opaque)
3146{
3147 CharDriverState *chr = opaque;
3148 TCPCharDriver *s = chr->opaque;
3149 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003150#ifndef _WIN32
3151 struct sockaddr_un uaddr;
3152#endif
3153 struct sockaddr *addr;
bellard0bab00f2006-06-25 14:49:44 +00003154 socklen_t len;
3155 int fd;
3156
3157 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003158#ifndef _WIN32
3159 if (s->is_unix) {
3160 len = sizeof(uaddr);
3161 addr = (struct sockaddr *)&uaddr;
3162 } else
3163#endif
3164 {
3165 len = sizeof(saddr);
3166 addr = (struct sockaddr *)&saddr;
3167 }
3168 fd = accept(s->listen_fd, addr, &len);
bellard0bab00f2006-06-25 14:49:44 +00003169 if (fd < 0 && errno != EINTR) {
3170 return;
3171 } else if (fd >= 0) {
bellard951f1352006-06-27 21:02:43 +00003172 if (s->do_telnetopt)
3173 tcp_chr_telnet_init(fd);
bellard0bab00f2006-06-25 14:49:44 +00003174 break;
3175 }
3176 }
3177 socket_set_nonblock(fd);
pbrookf7499982007-01-28 00:10:01 +00003178 if (s->do_nodelay)
3179 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003180 s->fd = fd;
3181 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3182 tcp_chr_connect(chr);
3183}
3184
3185static void tcp_chr_close(CharDriverState *chr)
3186{
3187 TCPCharDriver *s = chr->opaque;
3188 if (s->fd >= 0)
3189 closesocket(s->fd);
3190 if (s->listen_fd >= 0)
3191 closesocket(s->listen_fd);
3192 qemu_free(s);
3193}
3194
ths5fafdf22007-09-16 21:08:06 +00003195static CharDriverState *qemu_chr_open_tcp(const char *host_str,
thsffd843b2006-12-21 19:46:43 +00003196 int is_telnet,
3197 int is_unix)
bellard0bab00f2006-06-25 14:49:44 +00003198{
3199 CharDriverState *chr = NULL;
3200 TCPCharDriver *s = NULL;
3201 int fd = -1, ret, err, val;
bellard951f1352006-06-27 21:02:43 +00003202 int is_listen = 0;
3203 int is_waitconnect = 1;
pbrookf7499982007-01-28 00:10:01 +00003204 int do_nodelay = 0;
bellard951f1352006-06-27 21:02:43 +00003205 const char *ptr;
bellard0bab00f2006-06-25 14:49:44 +00003206 struct sockaddr_in saddr;
thsffd843b2006-12-21 19:46:43 +00003207#ifndef _WIN32
3208 struct sockaddr_un uaddr;
3209#endif
3210 struct sockaddr *addr;
3211 socklen_t addrlen;
bellard0bab00f2006-06-25 14:49:44 +00003212
thsffd843b2006-12-21 19:46:43 +00003213#ifndef _WIN32
3214 if (is_unix) {
3215 addr = (struct sockaddr *)&uaddr;
3216 addrlen = sizeof(uaddr);
3217 if (parse_unix_path(&uaddr, host_str) < 0)
3218 goto fail;
3219 } else
3220#endif
3221 {
3222 addr = (struct sockaddr *)&saddr;
3223 addrlen = sizeof(saddr);
3224 if (parse_host_port(&saddr, host_str) < 0)
3225 goto fail;
3226 }
bellard0bab00f2006-06-25 14:49:44 +00003227
bellard951f1352006-06-27 21:02:43 +00003228 ptr = host_str;
3229 while((ptr = strchr(ptr,','))) {
3230 ptr++;
3231 if (!strncmp(ptr,"server",6)) {
3232 is_listen = 1;
3233 } else if (!strncmp(ptr,"nowait",6)) {
3234 is_waitconnect = 0;
pbrookf7499982007-01-28 00:10:01 +00003235 } else if (!strncmp(ptr,"nodelay",6)) {
3236 do_nodelay = 1;
bellard951f1352006-06-27 21:02:43 +00003237 } else {
3238 printf("Unknown option: %s\n", ptr);
3239 goto fail;
3240 }
3241 }
3242 if (!is_listen)
3243 is_waitconnect = 0;
3244
bellard0bab00f2006-06-25 14:49:44 +00003245 chr = qemu_mallocz(sizeof(CharDriverState));
3246 if (!chr)
3247 goto fail;
3248 s = qemu_mallocz(sizeof(TCPCharDriver));
3249 if (!s)
3250 goto fail;
thsffd843b2006-12-21 19:46:43 +00003251
3252#ifndef _WIN32
3253 if (is_unix)
3254 fd = socket(PF_UNIX, SOCK_STREAM, 0);
3255 else
3256#endif
3257 fd = socket(PF_INET, SOCK_STREAM, 0);
ths5fafdf22007-09-16 21:08:06 +00003258
3259 if (fd < 0)
bellard0bab00f2006-06-25 14:49:44 +00003260 goto fail;
bellard951f1352006-06-27 21:02:43 +00003261
3262 if (!is_waitconnect)
3263 socket_set_nonblock(fd);
bellard0bab00f2006-06-25 14:49:44 +00003264
3265 s->connected = 0;
3266 s->fd = -1;
3267 s->listen_fd = -1;
thsffd843b2006-12-21 19:46:43 +00003268 s->is_unix = is_unix;
pbrookf7499982007-01-28 00:10:01 +00003269 s->do_nodelay = do_nodelay && !is_unix;
thsffd843b2006-12-21 19:46:43 +00003270
3271 chr->opaque = s;
3272 chr->chr_write = tcp_chr_write;
thsffd843b2006-12-21 19:46:43 +00003273 chr->chr_close = tcp_chr_close;
3274
bellard0bab00f2006-06-25 14:49:44 +00003275 if (is_listen) {
3276 /* allow fast reuse */
thsffd843b2006-12-21 19:46:43 +00003277#ifndef _WIN32
3278 if (is_unix) {
3279 char path[109];
3280 strncpy(path, uaddr.sun_path, 108);
3281 path[108] = 0;
3282 unlink(path);
3283 } else
3284#endif
3285 {
3286 val = 1;
3287 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3288 }
ths3b46e622007-09-17 08:09:54 +00003289
thsffd843b2006-12-21 19:46:43 +00003290 ret = bind(fd, addr, addrlen);
3291 if (ret < 0)
bellard0bab00f2006-06-25 14:49:44 +00003292 goto fail;
thsffd843b2006-12-21 19:46:43 +00003293
bellard0bab00f2006-06-25 14:49:44 +00003294 ret = listen(fd, 0);
3295 if (ret < 0)
3296 goto fail;
thsffd843b2006-12-21 19:46:43 +00003297
bellard0bab00f2006-06-25 14:49:44 +00003298 s->listen_fd = fd;
3299 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
bellard951f1352006-06-27 21:02:43 +00003300 if (is_telnet)
3301 s->do_telnetopt = 1;
bellard0bab00f2006-06-25 14:49:44 +00003302 } else {
3303 for(;;) {
thsffd843b2006-12-21 19:46:43 +00003304 ret = connect(fd, addr, addrlen);
bellard0bab00f2006-06-25 14:49:44 +00003305 if (ret < 0) {
3306 err = socket_error();
3307 if (err == EINTR || err == EWOULDBLOCK) {
3308 } else if (err == EINPROGRESS) {
3309 break;
thsf5b12262007-03-25 15:58:03 +00003310#ifdef _WIN32
3311 } else if (err == WSAEALREADY) {
3312 break;
3313#endif
bellard0bab00f2006-06-25 14:49:44 +00003314 } else {
3315 goto fail;
3316 }
3317 } else {
3318 s->connected = 1;
3319 break;
3320 }
3321 }
3322 s->fd = fd;
pbrookf7499982007-01-28 00:10:01 +00003323 socket_set_nodelay(fd);
bellard0bab00f2006-06-25 14:49:44 +00003324 if (s->connected)
3325 tcp_chr_connect(chr);
3326 else
3327 qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3328 }
ths3b46e622007-09-17 08:09:54 +00003329
bellard951f1352006-06-27 21:02:43 +00003330 if (is_listen && is_waitconnect) {
3331 printf("QEMU waiting for connection on: %s\n", host_str);
3332 tcp_chr_accept(chr);
3333 socket_set_nonblock(s->listen_fd);
3334 }
3335
bellard0bab00f2006-06-25 14:49:44 +00003336 return chr;
3337 fail:
3338 if (fd >= 0)
3339 closesocket(fd);
3340 qemu_free(s);
3341 qemu_free(chr);
3342 return NULL;
3343}
3344
bellard82c643f2004-07-14 17:28:13 +00003345CharDriverState *qemu_chr_open(const char *filename)
3346{
bellardf8d179e2005-11-08 22:30:36 +00003347 const char *p;
bellardfd1dff42006-02-01 21:29:26 +00003348
bellard82c643f2004-07-14 17:28:13 +00003349 if (!strcmp(filename, "vc")) {
thsaf3a9032007-07-11 23:14:59 +00003350 return text_console_init(&display_state, 0);
3351 } else if (strstart(filename, "vc:", &p)) {
3352 return text_console_init(&display_state, p);
bellard82c643f2004-07-14 17:28:13 +00003353 } else if (!strcmp(filename, "null")) {
3354 return qemu_chr_open_null();
ths5fafdf22007-09-16 21:08:06 +00003355 } else
bellard0bab00f2006-06-25 14:49:44 +00003356 if (strstart(filename, "tcp:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003357 return qemu_chr_open_tcp(p, 0, 0);
bellard0bab00f2006-06-25 14:49:44 +00003358 } else
bellard951f1352006-06-27 21:02:43 +00003359 if (strstart(filename, "telnet:", &p)) {
thsffd843b2006-12-21 19:46:43 +00003360 return qemu_chr_open_tcp(p, 1, 0);
bellard0bab00f2006-06-25 14:49:44 +00003361 } else
3362 if (strstart(filename, "udp:", &p)) {
3363 return qemu_chr_open_udp(p);
3364 } else
ths20d8a3e2007-02-18 17:04:49 +00003365 if (strstart(filename, "mon:", &p)) {
3366 CharDriverState *drv = qemu_chr_open(p);
3367 if (drv) {
3368 drv = qemu_chr_open_mux(drv);
3369 monitor_init(drv, !nographic);
3370 return drv;
3371 }
3372 printf("Unable to open driver: %s\n", p);
3373 return 0;
3374 } else
bellard76647282005-11-27 19:10:42 +00003375#ifndef _WIN32
thsffd843b2006-12-21 19:46:43 +00003376 if (strstart(filename, "unix:", &p)) {
3377 return qemu_chr_open_tcp(p, 0, 1);
3378 } else if (strstart(filename, "file:", &p)) {
bellardf8d179e2005-11-08 22:30:36 +00003379 return qemu_chr_open_file_out(p);
3380 } else if (strstart(filename, "pipe:", &p)) {
3381 return qemu_chr_open_pipe(p);
bellard76647282005-11-27 19:10:42 +00003382 } else if (!strcmp(filename, "pty")) {
bellard82c643f2004-07-14 17:28:13 +00003383 return qemu_chr_open_pty();
3384 } else if (!strcmp(filename, "stdio")) {
3385 return qemu_chr_open_stdio();
ths5fafdf22007-09-16 21:08:06 +00003386 } else
bellardf8d179e2005-11-08 22:30:36 +00003387#if defined(__linux__)
bellarde57a8c02005-11-10 23:58:52 +00003388 if (strstart(filename, "/dev/parport", NULL)) {
3389 return qemu_chr_open_pp(filename);
ths5fafdf22007-09-16 21:08:06 +00003390 } else
thsaec62502007-06-25 11:48:07 +00003391#endif
ths3fda3882007-06-28 15:14:49 +00003392#if defined(__linux__) || defined(__sun__)
bellardf8d179e2005-11-08 22:30:36 +00003393 if (strstart(filename, "/dev/", NULL)) {
3394 return qemu_chr_open_tty(filename);
ths3fda3882007-06-28 15:14:49 +00003395 } else
3396#endif
thsaec62502007-06-25 11:48:07 +00003397#else /* !_WIN32 */
bellardf3311102006-04-12 20:21:17 +00003398 if (strstart(filename, "COM", NULL)) {
3399 return qemu_chr_open_win(filename);
3400 } else
3401 if (strstart(filename, "pipe:", &p)) {
3402 return qemu_chr_open_win_pipe(p);
3403 } else
ths72d46472007-05-13 14:54:54 +00003404 if (strstart(filename, "con:", NULL)) {
3405 return qemu_chr_open_win_con(filename);
3406 } else
bellardf3311102006-04-12 20:21:17 +00003407 if (strstart(filename, "file:", &p)) {
3408 return qemu_chr_open_win_file_out(p);
3409 }
3410#endif
bellard82c643f2004-07-14 17:28:13 +00003411 {
3412 return NULL;
3413 }
3414}
3415
bellardf3311102006-04-12 20:21:17 +00003416void qemu_chr_close(CharDriverState *chr)
3417{
3418 if (chr->chr_close)
3419 chr->chr_close(chr);
3420}
3421
bellardf1510b22003-06-25 00:07:40 +00003422/***********************************************************/
bellard7c9d8e02005-11-15 22:16:05 +00003423/* network device redirectors */
bellardf1510b22003-06-25 00:07:40 +00003424
j_mayer3f4afa12007-11-19 01:05:22 +00003425__attribute__ (( unused ))
pbrook9596ebb2007-11-18 01:44:38 +00003426static void hex_dump(FILE *f, const uint8_t *buf, int size)
bellard67b915a2004-03-31 23:37:16 +00003427{
bellardc20709a2004-04-21 23:27:19 +00003428 int len, i, j, c;
3429
3430 for(i=0;i<size;i+=16) {
3431 len = size - i;
3432 if (len > 16)
3433 len = 16;
3434 fprintf(f, "%08x ", i);
3435 for(j=0;j<16;j++) {
3436 if (j < len)
3437 fprintf(f, " %02x", buf[i+j]);
3438 else
3439 fprintf(f, " ");
3440 }
3441 fprintf(f, " ");
3442 for(j=0;j<len;j++) {
3443 c = buf[i+j];
3444 if (c < ' ' || c > '~')
3445 c = '.';
3446 fprintf(f, "%c", c);
3447 }
3448 fprintf(f, "\n");
3449 }
3450}
3451
bellard7c9d8e02005-11-15 22:16:05 +00003452static int parse_macaddr(uint8_t *macaddr, const char *p)
bellardc20709a2004-04-21 23:27:19 +00003453{
bellard7c9d8e02005-11-15 22:16:05 +00003454 int i;
3455 for(i = 0; i < 6; i++) {
3456 macaddr[i] = strtol(p, (char **)&p, 16);
3457 if (i == 5) {
ths5fafdf22007-09-16 21:08:06 +00003458 if (*p != '\0')
bellard7c9d8e02005-11-15 22:16:05 +00003459 return -1;
3460 } else {
ths5fafdf22007-09-16 21:08:06 +00003461 if (*p != ':')
bellard7c9d8e02005-11-15 22:16:05 +00003462 return -1;
3463 p++;
3464 }
bellardc20709a2004-04-21 23:27:19 +00003465 }
bellardc20709a2004-04-21 23:27:19 +00003466 return 0;
3467}
3468
bellard9bf05442004-08-25 22:12:49 +00003469static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3470{
3471 const char *p, *p1;
3472 int len;
3473 p = *pp;
3474 p1 = strchr(p, sep);
3475 if (!p1)
3476 return -1;
3477 len = p1 - p;
3478 p1++;
3479 if (buf_size > 0) {
3480 if (len > buf_size - 1)
3481 len = buf_size - 1;
3482 memcpy(buf, p, len);
3483 buf[len] = '\0';
3484 }
3485 *pp = p1;
3486 return 0;
3487}
3488
bellard951f1352006-06-27 21:02:43 +00003489int parse_host_src_port(struct sockaddr_in *haddr,
3490 struct sockaddr_in *saddr,
3491 const char *input_str)
3492{
3493 char *str = strdup(input_str);
3494 char *host_str = str;
3495 char *src_str;
3496 char *ptr;
3497
3498 /*
3499 * Chop off any extra arguments at the end of the string which
3500 * would start with a comma, then fill in the src port information
3501 * if it was provided else use the "any address" and "any port".
3502 */
3503 if ((ptr = strchr(str,',')))
3504 *ptr = '\0';
3505
3506 if ((src_str = strchr(input_str,'@'))) {
3507 *src_str = '\0';
3508 src_str++;
3509 }
3510
3511 if (parse_host_port(haddr, host_str) < 0)
3512 goto fail;
3513
3514 if (!src_str || *src_str == '\0')
3515 src_str = ":0";
3516
3517 if (parse_host_port(saddr, src_str) < 0)
3518 goto fail;
3519
3520 free(str);
3521 return(0);
3522
3523fail:
3524 free(str);
3525 return -1;
3526}
3527
bellard7c9d8e02005-11-15 22:16:05 +00003528int parse_host_port(struct sockaddr_in *saddr, const char *str)
3529{
3530 char buf[512];
3531 struct hostent *he;
3532 const char *p, *r;
3533 int port;
3534
3535 p = str;
3536 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3537 return -1;
3538 saddr->sin_family = AF_INET;
3539 if (buf[0] == '\0') {
3540 saddr->sin_addr.s_addr = 0;
3541 } else {
3542 if (isdigit(buf[0])) {
3543 if (!inet_aton(buf, &saddr->sin_addr))
3544 return -1;
3545 } else {
bellard7c9d8e02005-11-15 22:16:05 +00003546 if ((he = gethostbyname(buf)) == NULL)
3547 return - 1;
3548 saddr->sin_addr = *(struct in_addr *)he->h_addr;
bellard7c9d8e02005-11-15 22:16:05 +00003549 }
3550 }
3551 port = strtol(p, (char **)&r, 0);
3552 if (r == p)
3553 return -1;
3554 saddr->sin_port = htons(port);
3555 return 0;
3556}
3557
ths52f61fd2006-12-22 21:20:52 +00003558#ifndef _WIN32
3559static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
thsffd843b2006-12-21 19:46:43 +00003560{
3561 const char *p;
3562 int len;
3563
3564 len = MIN(108, strlen(str));
3565 p = strchr(str, ',');
3566 if (p)
3567 len = MIN(len, p - str);
3568
3569 memset(uaddr, 0, sizeof(*uaddr));
3570
3571 uaddr->sun_family = AF_UNIX;
3572 memcpy(uaddr->sun_path, str, len);
3573
3574 return 0;
3575}
ths52f61fd2006-12-22 21:20:52 +00003576#endif
thsffd843b2006-12-21 19:46:43 +00003577
bellard7c9d8e02005-11-15 22:16:05 +00003578/* find or alloc a new VLAN */
3579VLANState *qemu_find_vlan(int id)
3580{
3581 VLANState **pvlan, *vlan;
3582 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3583 if (vlan->id == id)
3584 return vlan;
3585 }
3586 vlan = qemu_mallocz(sizeof(VLANState));
3587 if (!vlan)
3588 return NULL;
3589 vlan->id = id;
3590 vlan->next = NULL;
3591 pvlan = &first_vlan;
3592 while (*pvlan != NULL)
3593 pvlan = &(*pvlan)->next;
3594 *pvlan = vlan;
3595 return vlan;
3596}
3597
3598VLANClientState *qemu_new_vlan_client(VLANState *vlan,
pbrookd861b052006-02-04 22:15:28 +00003599 IOReadHandler *fd_read,
3600 IOCanRWHandler *fd_can_read,
3601 void *opaque)
bellard7c9d8e02005-11-15 22:16:05 +00003602{
3603 VLANClientState *vc, **pvc;
3604 vc = qemu_mallocz(sizeof(VLANClientState));
3605 if (!vc)
3606 return NULL;
3607 vc->fd_read = fd_read;
pbrookd861b052006-02-04 22:15:28 +00003608 vc->fd_can_read = fd_can_read;
bellard7c9d8e02005-11-15 22:16:05 +00003609 vc->opaque = opaque;
3610 vc->vlan = vlan;
3611
3612 vc->next = NULL;
3613 pvc = &vlan->first_client;
3614 while (*pvc != NULL)
3615 pvc = &(*pvc)->next;
3616 *pvc = vc;
3617 return vc;
3618}
3619
pbrookd861b052006-02-04 22:15:28 +00003620int qemu_can_send_packet(VLANClientState *vc1)
3621{
3622 VLANState *vlan = vc1->vlan;
3623 VLANClientState *vc;
3624
3625 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3626 if (vc != vc1) {
balrogfbd17112007-07-02 13:31:53 +00003627 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3628 return 1;
pbrookd861b052006-02-04 22:15:28 +00003629 }
3630 }
balrogfbd17112007-07-02 13:31:53 +00003631 return 0;
pbrookd861b052006-02-04 22:15:28 +00003632}
3633
bellard7c9d8e02005-11-15 22:16:05 +00003634void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3635{
3636 VLANState *vlan = vc1->vlan;
3637 VLANClientState *vc;
3638
3639#if 0
3640 printf("vlan %d send:\n", vlan->id);
3641 hex_dump(stdout, buf, size);
3642#endif
3643 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3644 if (vc != vc1) {
3645 vc->fd_read(vc->opaque, buf, size);
3646 }
3647 }
3648}
3649
3650#if defined(CONFIG_SLIRP)
3651
3652/* slirp network adapter */
3653
3654static int slirp_inited;
3655static VLANClientState *slirp_vc;
3656
3657int slirp_can_output(void)
3658{
pbrook3b7f5d42006-02-10 17:34:02 +00003659 return !slirp_vc || qemu_can_send_packet(slirp_vc);
bellard7c9d8e02005-11-15 22:16:05 +00003660}
3661
3662void slirp_output(const uint8_t *pkt, int pkt_len)
3663{
3664#if 0
3665 printf("slirp output:\n");
3666 hex_dump(stdout, pkt, pkt_len);
3667#endif
pbrook3b7f5d42006-02-10 17:34:02 +00003668 if (!slirp_vc)
3669 return;
bellard7c9d8e02005-11-15 22:16:05 +00003670 qemu_send_packet(slirp_vc, pkt, pkt_len);
3671}
3672
3673static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3674{
3675#if 0
3676 printf("slirp input:\n");
3677 hex_dump(stdout, buf, size);
3678#endif
3679 slirp_input(buf, size);
3680}
3681
3682static int net_slirp_init(VLANState *vlan)
3683{
3684 if (!slirp_inited) {
3685 slirp_inited = 1;
3686 slirp_init();
3687 }
ths5fafdf22007-09-16 21:08:06 +00003688 slirp_vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00003689 slirp_receive, NULL, NULL);
bellard7c9d8e02005-11-15 22:16:05 +00003690 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3691 return 0;
3692}
3693
bellard9bf05442004-08-25 22:12:49 +00003694static void net_slirp_redir(const char *redir_str)
3695{
3696 int is_udp;
3697 char buf[256], *r;
3698 const char *p;
3699 struct in_addr guest_addr;
3700 int host_port, guest_port;
ths3b46e622007-09-17 08:09:54 +00003701
bellard9bf05442004-08-25 22:12:49 +00003702 if (!slirp_inited) {
3703 slirp_inited = 1;
3704 slirp_init();
3705 }
3706
3707 p = redir_str;
3708 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3709 goto fail;
3710 if (!strcmp(buf, "tcp")) {
3711 is_udp = 0;
3712 } else if (!strcmp(buf, "udp")) {
3713 is_udp = 1;
3714 } else {
3715 goto fail;
3716 }
3717
3718 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3719 goto fail;
3720 host_port = strtol(buf, &r, 0);
3721 if (r == buf)
3722 goto fail;
3723
3724 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3725 goto fail;
3726 if (buf[0] == '\0') {
3727 pstrcpy(buf, sizeof(buf), "10.0.2.15");
3728 }
3729 if (!inet_aton(buf, &guest_addr))
3730 goto fail;
ths3b46e622007-09-17 08:09:54 +00003731
bellard9bf05442004-08-25 22:12:49 +00003732 guest_port = strtol(p, &r, 0);
3733 if (r == p)
3734 goto fail;
ths3b46e622007-09-17 08:09:54 +00003735
bellard9bf05442004-08-25 22:12:49 +00003736 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
3737 fprintf(stderr, "qemu: could not set up redirection\n");
3738 exit(1);
3739 }
3740 return;
3741 fail:
3742 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3743 exit(1);
3744}
ths3b46e622007-09-17 08:09:54 +00003745
bellardc94c8d62004-09-13 21:37:34 +00003746#ifndef _WIN32
3747
bellard9d728e82004-09-05 23:09:03 +00003748char smb_dir[1024];
3749
3750static void smb_exit(void)
3751{
3752 DIR *d;
3753 struct dirent *de;
3754 char filename[1024];
3755
3756 /* erase all the files in the directory */
3757 d = opendir(smb_dir);
3758 for(;;) {
3759 de = readdir(d);
3760 if (!de)
3761 break;
3762 if (strcmp(de->d_name, ".") != 0 &&
3763 strcmp(de->d_name, "..") != 0) {
ths5fafdf22007-09-16 21:08:06 +00003764 snprintf(filename, sizeof(filename), "%s/%s",
bellard9d728e82004-09-05 23:09:03 +00003765 smb_dir, de->d_name);
3766 unlink(filename);
3767 }
3768 }
bellard03ffbb62004-09-06 00:14:04 +00003769 closedir(d);
bellard9d728e82004-09-05 23:09:03 +00003770 rmdir(smb_dir);
3771}
3772
3773/* automatic user mode samba server configuration */
pbrook9596ebb2007-11-18 01:44:38 +00003774static void net_slirp_smb(const char *exported_dir)
bellard9d728e82004-09-05 23:09:03 +00003775{
3776 char smb_conf[1024];
3777 char smb_cmdline[1024];
3778 FILE *f;
3779
3780 if (!slirp_inited) {
3781 slirp_inited = 1;
3782 slirp_init();
3783 }
3784
3785 /* XXX: better tmp dir construction */
3786 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
3787 if (mkdir(smb_dir, 0700) < 0) {
3788 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
3789 exit(1);
3790 }
3791 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
ths3b46e622007-09-17 08:09:54 +00003792
bellard9d728e82004-09-05 23:09:03 +00003793 f = fopen(smb_conf, "w");
3794 if (!f) {
3795 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
3796 exit(1);
3797 }
ths5fafdf22007-09-16 21:08:06 +00003798 fprintf(f,
bellard9d728e82004-09-05 23:09:03 +00003799 "[global]\n"
bellard157777e2005-03-01 21:28:45 +00003800 "private dir=%s\n"
3801 "smb ports=0\n"
3802 "socket address=127.0.0.1\n"
bellard9d728e82004-09-05 23:09:03 +00003803 "pid directory=%s\n"
3804 "lock directory=%s\n"
3805 "log file=%s/log.smbd\n"
3806 "smb passwd file=%s/smbpasswd\n"
bellard03ffbb62004-09-06 00:14:04 +00003807 "security = share\n"
bellard9d728e82004-09-05 23:09:03 +00003808 "[qemu]\n"
3809 "path=%s\n"
3810 "read only=no\n"
3811 "guest ok=yes\n",
3812 smb_dir,
3813 smb_dir,
3814 smb_dir,
3815 smb_dir,
bellard157777e2005-03-01 21:28:45 +00003816 smb_dir,
bellard9d728e82004-09-05 23:09:03 +00003817 exported_dir
3818 );
3819 fclose(f);
3820 atexit(smb_exit);
3821
pbrooka14d6c82006-12-23 15:37:33 +00003822 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
3823 SMBD_COMMAND, smb_conf);
ths3b46e622007-09-17 08:09:54 +00003824
bellard9d728e82004-09-05 23:09:03 +00003825 slirp_add_exec(0, smb_cmdline, 4, 139);
3826}
bellard9bf05442004-08-25 22:12:49 +00003827
bellardc94c8d62004-09-13 21:37:34 +00003828#endif /* !defined(_WIN32) */
blueswir131a60e22007-10-26 18:42:59 +00003829void do_info_slirp(void)
3830{
3831 slirp_stats();
3832}
bellardc94c8d62004-09-13 21:37:34 +00003833
bellardc20709a2004-04-21 23:27:19 +00003834#endif /* CONFIG_SLIRP */
3835
3836#if !defined(_WIN32)
bellard7c9d8e02005-11-15 22:16:05 +00003837
3838typedef struct TAPState {
3839 VLANClientState *vc;
3840 int fd;
thsb46a8902007-10-21 23:20:45 +00003841 char down_script[1024];
bellard7c9d8e02005-11-15 22:16:05 +00003842} TAPState;
3843
3844static void tap_receive(void *opaque, const uint8_t *buf, int size)
3845{
3846 TAPState *s = opaque;
3847 int ret;
3848 for(;;) {
3849 ret = write(s->fd, buf, size);
3850 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
3851 } else {
3852 break;
3853 }
3854 }
3855}
3856
3857static void tap_send(void *opaque)
3858{
3859 TAPState *s = opaque;
3860 uint8_t buf[4096];
3861 int size;
3862
thsd5d10bc2007-02-17 22:54:49 +00003863#ifdef __sun__
3864 struct strbuf sbuf;
3865 int f = 0;
3866 sbuf.maxlen = sizeof(buf);
3867 sbuf.buf = buf;
3868 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
3869#else
bellard7c9d8e02005-11-15 22:16:05 +00003870 size = read(s->fd, buf, sizeof(buf));
thsd5d10bc2007-02-17 22:54:49 +00003871#endif
bellard7c9d8e02005-11-15 22:16:05 +00003872 if (size > 0) {
3873 qemu_send_packet(s->vc, buf, size);
3874 }
3875}
3876
3877/* fd support */
3878
3879static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
3880{
3881 TAPState *s;
3882
3883 s = qemu_mallocz(sizeof(TAPState));
3884 if (!s)
3885 return NULL;
3886 s->fd = fd;
pbrookd861b052006-02-04 22:15:28 +00003887 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00003888 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
3889 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
3890 return s;
3891}
3892
ths5c40d2b2007-06-23 16:03:36 +00003893#if defined (_BSD) || defined (__FreeBSD_kernel__)
bellard7c9d8e02005-11-15 22:16:05 +00003894static int tap_open(char *ifname, int ifname_size)
bellard7d3505c2004-05-12 19:32:15 +00003895{
3896 int fd;
3897 char *dev;
3898 struct stat s;
bellard67b915a2004-03-31 23:37:16 +00003899
balrogaeb30be2007-07-02 15:03:13 +00003900 TFR(fd = open("/dev/tap", O_RDWR));
bellard7d3505c2004-05-12 19:32:15 +00003901 if (fd < 0) {
3902 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
3903 return -1;
3904 }
3905
3906 fstat(fd, &s);
3907 dev = devname(s.st_rdev, S_IFCHR);
3908 pstrcpy(ifname, ifname_size, dev);
3909
3910 fcntl(fd, F_SETFL, O_NONBLOCK);
3911 return fd;
3912}
bellardec530c82006-04-25 22:36:06 +00003913#elif defined(__sun__)
thsd5d10bc2007-02-17 22:54:49 +00003914#define TUNNEWPPA (('T'<<16) | 0x0001)
ths5fafdf22007-09-16 21:08:06 +00003915/*
3916 * Allocate TAP device, returns opened fd.
thsd5d10bc2007-02-17 22:54:49 +00003917 * Stores dev name in the first arg(must be large enough).
ths3b46e622007-09-17 08:09:54 +00003918 */
thsd5d10bc2007-02-17 22:54:49 +00003919int tap_alloc(char *dev)
3920{
3921 int tap_fd, if_fd, ppa = -1;
3922 static int ip_fd = 0;
3923 char *ptr;
3924
3925 static int arp_fd = 0;
3926 int ip_muxid, arp_muxid;
3927 struct strioctl strioc_if, strioc_ppa;
3928 int link_type = I_PLINK;;
3929 struct lifreq ifr;
3930 char actual_name[32] = "";
3931
3932 memset(&ifr, 0x0, sizeof(ifr));
3933
3934 if( *dev ){
ths5fafdf22007-09-16 21:08:06 +00003935 ptr = dev;
3936 while( *ptr && !isdigit((int)*ptr) ) ptr++;
thsd5d10bc2007-02-17 22:54:49 +00003937 ppa = atoi(ptr);
3938 }
3939
3940 /* Check if IP device was opened */
3941 if( ip_fd )
3942 close(ip_fd);
3943
balrogaeb30be2007-07-02 15:03:13 +00003944 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
3945 if (ip_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003946 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
3947 return -1;
3948 }
3949
balrogaeb30be2007-07-02 15:03:13 +00003950 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
3951 if (tap_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003952 syslog(LOG_ERR, "Can't open /dev/tap");
3953 return -1;
3954 }
3955
3956 /* Assign a new PPA and get its unit number. */
3957 strioc_ppa.ic_cmd = TUNNEWPPA;
3958 strioc_ppa.ic_timout = 0;
3959 strioc_ppa.ic_len = sizeof(ppa);
3960 strioc_ppa.ic_dp = (char *)&ppa;
3961 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
3962 syslog (LOG_ERR, "Can't assign new interface");
3963
balrogaeb30be2007-07-02 15:03:13 +00003964 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
3965 if (if_fd < 0) {
thsd5d10bc2007-02-17 22:54:49 +00003966 syslog(LOG_ERR, "Can't open /dev/tap (2)");
3967 return -1;
3968 }
3969 if(ioctl(if_fd, I_PUSH, "ip") < 0){
3970 syslog(LOG_ERR, "Can't push IP module");
3971 return -1;
3972 }
3973
3974 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
3975 syslog(LOG_ERR, "Can't get flags\n");
3976
3977 snprintf (actual_name, 32, "tap%d", ppa);
3978 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
3979
3980 ifr.lifr_ppa = ppa;
3981 /* Assign ppa according to the unit number returned by tun device */
3982
3983 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
3984 syslog (LOG_ERR, "Can't set PPA %d", ppa);
3985 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
3986 syslog (LOG_ERR, "Can't get flags\n");
3987 /* Push arp module to if_fd */
3988 if (ioctl (if_fd, I_PUSH, "arp") < 0)
3989 syslog (LOG_ERR, "Can't push ARP module (2)");
3990
3991 /* Push arp module to ip_fd */
3992 if (ioctl (ip_fd, I_POP, NULL) < 0)
3993 syslog (LOG_ERR, "I_POP failed\n");
3994 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
3995 syslog (LOG_ERR, "Can't push ARP module (3)\n");
3996 /* Open arp_fd */
balrogaeb30be2007-07-02 15:03:13 +00003997 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
3998 if (arp_fd < 0)
thsd5d10bc2007-02-17 22:54:49 +00003999 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
4000
4001 /* Set ifname to arp */
4002 strioc_if.ic_cmd = SIOCSLIFNAME;
4003 strioc_if.ic_timout = 0;
4004 strioc_if.ic_len = sizeof(ifr);
4005 strioc_if.ic_dp = (char *)&ifr;
4006 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
4007 syslog (LOG_ERR, "Can't set ifname to arp\n");
4008 }
4009
4010 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
4011 syslog(LOG_ERR, "Can't link TAP device to IP");
4012 return -1;
4013 }
4014
4015 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
4016 syslog (LOG_ERR, "Can't link TAP device to ARP");
4017
4018 close (if_fd);
4019
4020 memset(&ifr, 0x0, sizeof(ifr));
4021 strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4022 ifr.lifr_ip_muxid = ip_muxid;
4023 ifr.lifr_arp_muxid = arp_muxid;
4024
4025 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
4026 {
4027 ioctl (ip_fd, I_PUNLINK , arp_muxid);
4028 ioctl (ip_fd, I_PUNLINK, ip_muxid);
4029 syslog (LOG_ERR, "Can't set multiplexor id");
4030 }
4031
4032 sprintf(dev, "tap%d", ppa);
4033 return tap_fd;
4034}
4035
bellardec530c82006-04-25 22:36:06 +00004036static int tap_open(char *ifname, int ifname_size)
4037{
thsd5d10bc2007-02-17 22:54:49 +00004038 char dev[10]="";
4039 int fd;
4040 if( (fd = tap_alloc(dev)) < 0 ){
4041 fprintf(stderr, "Cannot allocate TAP device\n");
4042 return -1;
4043 }
4044 pstrcpy(ifname, ifname_size, dev);
4045 fcntl(fd, F_SETFL, O_NONBLOCK);
4046 return fd;
bellardec530c82006-04-25 22:36:06 +00004047}
bellard7d3505c2004-05-12 19:32:15 +00004048#else
bellard7c9d8e02005-11-15 22:16:05 +00004049static int tap_open(char *ifname, int ifname_size)
bellardf1510b22003-06-25 00:07:40 +00004050{
4051 struct ifreq ifr;
bellardc4b1fcc2004-03-14 21:44:30 +00004052 int fd, ret;
ths3b46e622007-09-17 08:09:54 +00004053
balrogaeb30be2007-07-02 15:03:13 +00004054 TFR(fd = open("/dev/net/tun", O_RDWR));
bellardf1510b22003-06-25 00:07:40 +00004055 if (fd < 0) {
4056 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4057 return -1;
4058 }
4059 memset(&ifr, 0, sizeof(ifr));
4060 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
bellard7c9d8e02005-11-15 22:16:05 +00004061 if (ifname[0] != '\0')
4062 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4063 else
4064 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
bellardf1510b22003-06-25 00:07:40 +00004065 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4066 if (ret != 0) {
4067 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4068 close(fd);
4069 return -1;
4070 }
bellardc4b1fcc2004-03-14 21:44:30 +00004071 pstrcpy(ifname, ifname_size, ifr.ifr_name);
bellardf1510b22003-06-25 00:07:40 +00004072 fcntl(fd, F_SETFL, O_NONBLOCK);
bellardc4b1fcc2004-03-14 21:44:30 +00004073 return fd;
4074}
bellard7d3505c2004-05-12 19:32:15 +00004075#endif
bellardf1510b22003-06-25 00:07:40 +00004076
thsb46a8902007-10-21 23:20:45 +00004077static int launch_script(const char *setup_script, const char *ifname, int fd)
bellardc4b1fcc2004-03-14 21:44:30 +00004078{
thsb46a8902007-10-21 23:20:45 +00004079 int pid, status;
bellardc20709a2004-04-21 23:27:19 +00004080 char *args[3];
4081 char **parg;
4082
thsb46a8902007-10-21 23:20:45 +00004083 /* try to launch network script */
bellard7c9d8e02005-11-15 22:16:05 +00004084 pid = fork();
4085 if (pid >= 0) {
4086 if (pid == 0) {
ths50d3eea2007-03-19 16:36:43 +00004087 int open_max = sysconf (_SC_OPEN_MAX), i;
4088 for (i = 0; i < open_max; i++)
4089 if (i != STDIN_FILENO &&
4090 i != STDOUT_FILENO &&
4091 i != STDERR_FILENO &&
4092 i != fd)
4093 close(i);
4094
bellard7c9d8e02005-11-15 22:16:05 +00004095 parg = args;
4096 *parg++ = (char *)setup_script;
thsb46a8902007-10-21 23:20:45 +00004097 *parg++ = (char *)ifname;
bellard7c9d8e02005-11-15 22:16:05 +00004098 *parg++ = NULL;
4099 execv(setup_script, args);
bellard4a389402005-11-26 20:10:07 +00004100 _exit(1);
bellard7c9d8e02005-11-15 22:16:05 +00004101 }
4102 while (waitpid(pid, &status, 0) != pid);
4103 if (!WIFEXITED(status) ||
4104 WEXITSTATUS(status) != 0) {
4105 fprintf(stderr, "%s: could not launch network script\n",
4106 setup_script);
4107 return -1;
4108 }
bellardc20709a2004-04-21 23:27:19 +00004109 }
thsb46a8902007-10-21 23:20:45 +00004110 return 0;
4111}
4112
4113static int net_tap_init(VLANState *vlan, const char *ifname1,
4114 const char *setup_script, const char *down_script)
4115{
4116 TAPState *s;
4117 int fd;
4118 char ifname[128];
4119
4120 if (ifname1 != NULL)
4121 pstrcpy(ifname, sizeof(ifname), ifname1);
4122 else
4123 ifname[0] = '\0';
4124 TFR(fd = tap_open(ifname, sizeof(ifname)));
4125 if (fd < 0)
4126 return -1;
4127
4128 if (!setup_script || !strcmp(setup_script, "no"))
4129 setup_script = "";
4130 if (setup_script[0] != '\0') {
4131 if (launch_script(setup_script, ifname, fd))
4132 return -1;
bellardc20709a2004-04-21 23:27:19 +00004133 }
bellard7c9d8e02005-11-15 22:16:05 +00004134 s = net_tap_fd_init(vlan, fd);
4135 if (!s)
4136 return -1;
ths5fafdf22007-09-16 21:08:06 +00004137 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
bellard7c9d8e02005-11-15 22:16:05 +00004138 "tap: ifname=%s setup_script=%s", ifname, setup_script);
thsb46a8902007-10-21 23:20:45 +00004139 if (down_script && strcmp(down_script, "no"))
4140 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
bellardc20709a2004-04-21 23:27:19 +00004141 return 0;
4142}
4143
bellardfd1dff42006-02-01 21:29:26 +00004144#endif /* !_WIN32 */
4145
bellard7c9d8e02005-11-15 22:16:05 +00004146/* network connection */
4147typedef struct NetSocketState {
4148 VLANClientState *vc;
4149 int fd;
4150 int state; /* 0 = getting length, 1 = getting data */
4151 int index;
4152 int packet_len;
4153 uint8_t buf[4096];
bellard3d830452005-12-18 16:36:49 +00004154 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
bellard7c9d8e02005-11-15 22:16:05 +00004155} NetSocketState;
4156
4157typedef struct NetSocketListenState {
4158 VLANState *vlan;
4159 int fd;
4160} NetSocketListenState;
4161
4162/* XXX: we consider we can send the whole packet without blocking */
4163static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
bellardc20709a2004-04-21 23:27:19 +00004164{
bellard7c9d8e02005-11-15 22:16:05 +00004165 NetSocketState *s = opaque;
4166 uint32_t len;
4167 len = htonl(size);
4168
bellardfd1dff42006-02-01 21:29:26 +00004169 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4170 send_all(s->fd, buf, size);
bellard7c9d8e02005-11-15 22:16:05 +00004171}
4172
bellard3d830452005-12-18 16:36:49 +00004173static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4174{
4175 NetSocketState *s = opaque;
ths5fafdf22007-09-16 21:08:06 +00004176 sendto(s->fd, buf, size, 0,
bellard3d830452005-12-18 16:36:49 +00004177 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4178}
4179
bellard7c9d8e02005-11-15 22:16:05 +00004180static void net_socket_send(void *opaque)
4181{
4182 NetSocketState *s = opaque;
bellardfd1dff42006-02-01 21:29:26 +00004183 int l, size, err;
bellard7c9d8e02005-11-15 22:16:05 +00004184 uint8_t buf1[4096];
4185 const uint8_t *buf;
4186
bellardfd1dff42006-02-01 21:29:26 +00004187 size = recv(s->fd, buf1, sizeof(buf1), 0);
4188 if (size < 0) {
4189 err = socket_error();
ths5fafdf22007-09-16 21:08:06 +00004190 if (err != EWOULDBLOCK)
bellardfd1dff42006-02-01 21:29:26 +00004191 goto eoc;
4192 } else if (size == 0) {
bellard7c9d8e02005-11-15 22:16:05 +00004193 /* end of connection */
bellardfd1dff42006-02-01 21:29:26 +00004194 eoc:
bellard7c9d8e02005-11-15 22:16:05 +00004195 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
bellardfd1dff42006-02-01 21:29:26 +00004196 closesocket(s->fd);
bellard7c9d8e02005-11-15 22:16:05 +00004197 return;
4198 }
4199 buf = buf1;
4200 while (size > 0) {
4201 /* reassemble a packet from the network */
4202 switch(s->state) {
4203 case 0:
4204 l = 4 - s->index;
4205 if (l > size)
4206 l = size;
4207 memcpy(s->buf + s->index, buf, l);
4208 buf += l;
4209 size -= l;
4210 s->index += l;
4211 if (s->index == 4) {
4212 /* got length */
4213 s->packet_len = ntohl(*(uint32_t *)s->buf);
4214 s->index = 0;
4215 s->state = 1;
4216 }
4217 break;
4218 case 1:
4219 l = s->packet_len - s->index;
4220 if (l > size)
4221 l = size;
4222 memcpy(s->buf + s->index, buf, l);
4223 s->index += l;
4224 buf += l;
4225 size -= l;
4226 if (s->index >= s->packet_len) {
4227 qemu_send_packet(s->vc, s->buf, s->packet_len);
4228 s->index = 0;
4229 s->state = 0;
4230 }
4231 break;
4232 }
4233 }
4234}
4235
bellard3d830452005-12-18 16:36:49 +00004236static void net_socket_send_dgram(void *opaque)
4237{
4238 NetSocketState *s = opaque;
4239 int size;
4240
4241 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
ths5fafdf22007-09-16 21:08:06 +00004242 if (size < 0)
bellard3d830452005-12-18 16:36:49 +00004243 return;
4244 if (size == 0) {
4245 /* end of connection */
4246 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4247 return;
4248 }
4249 qemu_send_packet(s->vc, s->buf, size);
4250}
4251
4252static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4253{
4254 struct ip_mreq imr;
4255 int fd;
4256 int val, ret;
4257 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4258 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
ths5fafdf22007-09-16 21:08:06 +00004259 inet_ntoa(mcastaddr->sin_addr),
bellardfd1dff42006-02-01 21:29:26 +00004260 (int)ntohl(mcastaddr->sin_addr.s_addr));
bellard3d830452005-12-18 16:36:49 +00004261 return -1;
4262
4263 }
4264 fd = socket(PF_INET, SOCK_DGRAM, 0);
4265 if (fd < 0) {
4266 perror("socket(PF_INET, SOCK_DGRAM)");
4267 return -1;
4268 }
4269
bellardfd1dff42006-02-01 21:29:26 +00004270 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004271 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
bellardfd1dff42006-02-01 21:29:26 +00004272 (const char *)&val, sizeof(val));
4273 if (ret < 0) {
4274 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4275 goto fail;
4276 }
4277
4278 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4279 if (ret < 0) {
4280 perror("bind");
4281 goto fail;
4282 }
ths3b46e622007-09-17 08:09:54 +00004283
bellard3d830452005-12-18 16:36:49 +00004284 /* Add host to multicast group */
4285 imr.imr_multiaddr = mcastaddr->sin_addr;
4286 imr.imr_interface.s_addr = htonl(INADDR_ANY);
4287
ths5fafdf22007-09-16 21:08:06 +00004288 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
bellardfd1dff42006-02-01 21:29:26 +00004289 (const char *)&imr, sizeof(struct ip_mreq));
bellard3d830452005-12-18 16:36:49 +00004290 if (ret < 0) {
4291 perror("setsockopt(IP_ADD_MEMBERSHIP)");
4292 goto fail;
4293 }
4294
4295 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4296 val = 1;
ths5fafdf22007-09-16 21:08:06 +00004297 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
bellardfd1dff42006-02-01 21:29:26 +00004298 (const char *)&val, sizeof(val));
bellard3d830452005-12-18 16:36:49 +00004299 if (ret < 0) {
4300 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4301 goto fail;
4302 }
4303
bellardfd1dff42006-02-01 21:29:26 +00004304 socket_set_nonblock(fd);
bellard3d830452005-12-18 16:36:49 +00004305 return fd;
4306fail:
ths5fafdf22007-09-16 21:08:06 +00004307 if (fd >= 0)
bellard0bab00f2006-06-25 14:49:44 +00004308 closesocket(fd);
bellard3d830452005-12-18 16:36:49 +00004309 return -1;
4310}
4311
ths5fafdf22007-09-16 21:08:06 +00004312static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004313 int is_connected)
4314{
4315 struct sockaddr_in saddr;
4316 int newfd;
4317 socklen_t saddr_len;
4318 NetSocketState *s;
4319
4320 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
ths5fafdf22007-09-16 21:08:06 +00004321 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
bellard3d830452005-12-18 16:36:49 +00004322 * by ONLY ONE process: we must "clone" this dgram socket --jjo
4323 */
4324
4325 if (is_connected) {
4326 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4327 /* must be bound */
4328 if (saddr.sin_addr.s_addr==0) {
4329 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4330 fd);
4331 return NULL;
4332 }
4333 /* clone dgram socket */
4334 newfd = net_socket_mcast_create(&saddr);
4335 if (newfd < 0) {
4336 /* error already reported by net_socket_mcast_create() */
4337 close(fd);
4338 return NULL;
4339 }
4340 /* clone newfd to fd, close newfd */
4341 dup2(newfd, fd);
4342 close(newfd);
ths5fafdf22007-09-16 21:08:06 +00004343
bellard3d830452005-12-18 16:36:49 +00004344 } else {
4345 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4346 fd, strerror(errno));
4347 return NULL;
4348 }
4349 }
4350
4351 s = qemu_mallocz(sizeof(NetSocketState));
4352 if (!s)
4353 return NULL;
4354 s->fd = fd;
4355
pbrookd861b052006-02-04 22:15:28 +00004356 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
bellard3d830452005-12-18 16:36:49 +00004357 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4358
4359 /* mcast: save bound address as dst */
4360 if (is_connected) s->dgram_dst=saddr;
4361
4362 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004363 "socket: fd=%d (%s mcast=%s:%d)",
bellard3d830452005-12-18 16:36:49 +00004364 fd, is_connected? "cloned" : "",
4365 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4366 return s;
4367}
4368
bellard7c9d8e02005-11-15 22:16:05 +00004369static void net_socket_connect(void *opaque)
4370{
4371 NetSocketState *s = opaque;
4372 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4373}
4374
ths5fafdf22007-09-16 21:08:06 +00004375static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
bellard7c9d8e02005-11-15 22:16:05 +00004376 int is_connected)
4377{
4378 NetSocketState *s;
4379 s = qemu_mallocz(sizeof(NetSocketState));
4380 if (!s)
4381 return NULL;
4382 s->fd = fd;
ths5fafdf22007-09-16 21:08:06 +00004383 s->vc = qemu_new_vlan_client(vlan,
pbrookd861b052006-02-04 22:15:28 +00004384 net_socket_receive, NULL, s);
bellard7c9d8e02005-11-15 22:16:05 +00004385 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4386 "socket: fd=%d", fd);
4387 if (is_connected) {
4388 net_socket_connect(s);
4389 } else {
4390 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4391 }
4392 return s;
4393}
4394
ths5fafdf22007-09-16 21:08:06 +00004395static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
bellard3d830452005-12-18 16:36:49 +00004396 int is_connected)
4397{
4398 int so_type=-1, optlen=sizeof(so_type);
4399
bellardfd1dff42006-02-01 21:29:26 +00004400 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) {
ths931f03e2007-04-28 20:49:36 +00004401 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
bellard3d830452005-12-18 16:36:49 +00004402 return NULL;
4403 }
4404 switch(so_type) {
4405 case SOCK_DGRAM:
4406 return net_socket_fd_init_dgram(vlan, fd, is_connected);
4407 case SOCK_STREAM:
4408 return net_socket_fd_init_stream(vlan, fd, is_connected);
4409 default:
4410 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4411 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4412 return net_socket_fd_init_stream(vlan, fd, is_connected);
4413 }
4414 return NULL;
4415}
4416
bellard7c9d8e02005-11-15 22:16:05 +00004417static void net_socket_accept(void *opaque)
4418{
ths3b46e622007-09-17 08:09:54 +00004419 NetSocketListenState *s = opaque;
bellard7c9d8e02005-11-15 22:16:05 +00004420 NetSocketState *s1;
4421 struct sockaddr_in saddr;
4422 socklen_t len;
4423 int fd;
4424
4425 for(;;) {
4426 len = sizeof(saddr);
4427 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4428 if (fd < 0 && errno != EINTR) {
4429 return;
4430 } else if (fd >= 0) {
4431 break;
4432 }
4433 }
ths5fafdf22007-09-16 21:08:06 +00004434 s1 = net_socket_fd_init(s->vlan, fd, 1);
bellard7c9d8e02005-11-15 22:16:05 +00004435 if (!s1) {
bellard0bab00f2006-06-25 14:49:44 +00004436 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004437 } else {
4438 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004439 "socket: connection from %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004440 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4441 }
4442}
4443
4444static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4445{
4446 NetSocketListenState *s;
4447 int fd, val, ret;
4448 struct sockaddr_in saddr;
4449
4450 if (parse_host_port(&saddr, host_str) < 0)
4451 return -1;
ths3b46e622007-09-17 08:09:54 +00004452
bellard7c9d8e02005-11-15 22:16:05 +00004453 s = qemu_mallocz(sizeof(NetSocketListenState));
4454 if (!s)
4455 return -1;
4456
4457 fd = socket(PF_INET, SOCK_STREAM, 0);
4458 if (fd < 0) {
4459 perror("socket");
4460 return -1;
4461 }
bellardfd1dff42006-02-01 21:29:26 +00004462 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004463
4464 /* allow fast reuse */
4465 val = 1;
bellardfd1dff42006-02-01 21:29:26 +00004466 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00004467
bellard7c9d8e02005-11-15 22:16:05 +00004468 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4469 if (ret < 0) {
4470 perror("bind");
4471 return -1;
4472 }
4473 ret = listen(fd, 0);
4474 if (ret < 0) {
4475 perror("listen");
4476 return -1;
4477 }
4478 s->vlan = vlan;
4479 s->fd = fd;
4480 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4481 return 0;
4482}
4483
4484static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4485{
4486 NetSocketState *s;
bellardfd1dff42006-02-01 21:29:26 +00004487 int fd, connected, ret, err;
bellard7c9d8e02005-11-15 22:16:05 +00004488 struct sockaddr_in saddr;
4489
4490 if (parse_host_port(&saddr, host_str) < 0)
4491 return -1;
4492
4493 fd = socket(PF_INET, SOCK_STREAM, 0);
4494 if (fd < 0) {
4495 perror("socket");
4496 return -1;
4497 }
bellardfd1dff42006-02-01 21:29:26 +00004498 socket_set_nonblock(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004499
4500 connected = 0;
4501 for(;;) {
4502 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4503 if (ret < 0) {
bellardfd1dff42006-02-01 21:29:26 +00004504 err = socket_error();
4505 if (err == EINTR || err == EWOULDBLOCK) {
4506 } else if (err == EINPROGRESS) {
bellard7c9d8e02005-11-15 22:16:05 +00004507 break;
thsf5b12262007-03-25 15:58:03 +00004508#ifdef _WIN32
4509 } else if (err == WSAEALREADY) {
4510 break;
4511#endif
bellard7c9d8e02005-11-15 22:16:05 +00004512 } else {
4513 perror("connect");
bellardfd1dff42006-02-01 21:29:26 +00004514 closesocket(fd);
bellard7c9d8e02005-11-15 22:16:05 +00004515 return -1;
4516 }
4517 } else {
4518 connected = 1;
4519 break;
4520 }
4521 }
4522 s = net_socket_fd_init(vlan, fd, connected);
4523 if (!s)
4524 return -1;
4525 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004526 "socket: connect to %s:%d",
bellard7c9d8e02005-11-15 22:16:05 +00004527 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
bellardc20709a2004-04-21 23:27:19 +00004528 return 0;
4529}
4530
bellard3d830452005-12-18 16:36:49 +00004531static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4532{
4533 NetSocketState *s;
4534 int fd;
4535 struct sockaddr_in saddr;
4536
4537 if (parse_host_port(&saddr, host_str) < 0)
4538 return -1;
4539
4540
4541 fd = net_socket_mcast_create(&saddr);
4542 if (fd < 0)
4543 return -1;
4544
4545 s = net_socket_fd_init(vlan, fd, 0);
4546 if (!s)
4547 return -1;
4548
4549 s->dgram_dst = saddr;
ths3b46e622007-09-17 08:09:54 +00004550
bellard3d830452005-12-18 16:36:49 +00004551 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
ths5fafdf22007-09-16 21:08:06 +00004552 "socket: mcast=%s:%d",
bellard3d830452005-12-18 16:36:49 +00004553 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4554 return 0;
4555
4556}
4557
thse4bcb142007-12-02 04:51:10 +00004558static const char *get_word(char *buf, int buf_size, const char *p)
4559{
4560 char *q;
4561 int substring;
4562
4563 substring = 0;
4564 q = buf;
4565 while (*p != '\0') {
4566 if (*p == '\\') {
4567 p++;
4568 if (*p == '\0')
4569 break;
4570 } else if (*p == '\"') {
4571 substring = !substring;
4572 p++;
4573 continue;
4574 } else if (!substring && (*p == ',' || *p == '='))
4575 break;
4576 if (q && (q - buf) < buf_size - 1)
4577 *q++ = *p;
4578 p++;
4579 }
4580 if (q)
4581 *q = '\0';
4582
4583 return p;
4584}
4585
bellard7c9d8e02005-11-15 22:16:05 +00004586static int get_param_value(char *buf, int buf_size,
4587 const char *tag, const char *str)
4588{
4589 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00004590 char option[128];
4591
4592 p = str;
4593 for(;;) {
thse4bcb142007-12-02 04:51:10 +00004594 p = get_word(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00004595 if (*p != '=')
4596 break;
4597 p++;
4598 if (!strcmp(tag, option)) {
thse4bcb142007-12-02 04:51:10 +00004599 (void)get_word(buf, buf_size, p);
4600 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00004601 } else {
thse4bcb142007-12-02 04:51:10 +00004602 p = get_word(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00004603 }
4604 if (*p != ',')
4605 break;
4606 p++;
4607 }
4608 return 0;
4609}
4610
thse4bcb142007-12-02 04:51:10 +00004611static int check_params(char *buf, int buf_size,
4612 char **params, const char *str)
4613{
4614 const char *p;
4615 int i;
4616
4617 p = str;
4618 for(;;) {
4619 p = get_word(buf, buf_size, p);
4620 if (*p != '=')
4621 return -1;
4622 p++;
4623 for(i = 0; params[i] != NULL; i++)
4624 if (!strcmp(params[i], buf))
4625 break;
4626 if (params[i] == NULL)
4627 return -1;
4628 p = get_word(NULL, 0, p);
4629 if (*p != ',')
4630 break;
4631 p++;
4632 }
4633 return 0;
4634}
4635
4636
ths52f61fd2006-12-22 21:20:52 +00004637static int net_client_init(const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00004638{
4639 const char *p;
4640 char *q;
4641 char device[64];
4642 char buf[1024];
4643 int vlan_id, ret;
4644 VLANState *vlan;
4645
4646 p = str;
4647 q = device;
4648 while (*p != '\0' && *p != ',') {
4649 if ((q - device) < sizeof(device) - 1)
4650 *q++ = *p;
4651 p++;
4652 }
4653 *q = '\0';
4654 if (*p == ',')
4655 p++;
4656 vlan_id = 0;
4657 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
4658 vlan_id = strtol(buf, NULL, 0);
4659 }
4660 vlan = qemu_find_vlan(vlan_id);
4661 if (!vlan) {
4662 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
4663 return -1;
4664 }
4665 if (!strcmp(device, "nic")) {
4666 NICInfo *nd;
4667 uint8_t *macaddr;
4668
4669 if (nb_nics >= MAX_NICS) {
4670 fprintf(stderr, "Too Many NICs\n");
4671 return -1;
4672 }
4673 nd = &nd_table[nb_nics];
4674 macaddr = nd->macaddr;
4675 macaddr[0] = 0x52;
4676 macaddr[1] = 0x54;
4677 macaddr[2] = 0x00;
4678 macaddr[3] = 0x12;
4679 macaddr[4] = 0x34;
4680 macaddr[5] = 0x56 + nb_nics;
4681
4682 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
4683 if (parse_macaddr(macaddr, buf) < 0) {
4684 fprintf(stderr, "invalid syntax for ethernet address\n");
4685 return -1;
4686 }
4687 }
pbrooka41b2ff2006-02-05 04:14:41 +00004688 if (get_param_value(buf, sizeof(buf), "model", p)) {
4689 nd->model = strdup(buf);
4690 }
bellard7c9d8e02005-11-15 22:16:05 +00004691 nd->vlan = vlan;
4692 nb_nics++;
blueswir1833c7172007-05-27 19:36:43 +00004693 vlan->nb_guest_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004694 ret = 0;
4695 } else
4696 if (!strcmp(device, "none")) {
4697 /* does nothing. It is needed to signal that no network cards
4698 are wanted */
4699 ret = 0;
4700 } else
4701#ifdef CONFIG_SLIRP
4702 if (!strcmp(device, "user")) {
pbrook115defd2006-04-16 11:06:58 +00004703 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
bellard3f423c92006-04-30 21:34:15 +00004704 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
pbrook115defd2006-04-16 11:06:58 +00004705 }
blueswir1833c7172007-05-27 19:36:43 +00004706 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004707 ret = net_slirp_init(vlan);
4708 } else
4709#endif
bellard7fb843f2006-02-01 23:06:55 +00004710#ifdef _WIN32
4711 if (!strcmp(device, "tap")) {
4712 char ifname[64];
4713 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4714 fprintf(stderr, "tap: no interface name\n");
4715 return -1;
4716 }
blueswir1833c7172007-05-27 19:36:43 +00004717 vlan->nb_host_devs++;
bellard7fb843f2006-02-01 23:06:55 +00004718 ret = tap_win32_init(vlan, ifname);
4719 } else
4720#else
bellard7c9d8e02005-11-15 22:16:05 +00004721 if (!strcmp(device, "tap")) {
4722 char ifname[64];
thsb46a8902007-10-21 23:20:45 +00004723 char setup_script[1024], down_script[1024];
bellard7c9d8e02005-11-15 22:16:05 +00004724 int fd;
pbrook4f010352007-05-28 02:29:59 +00004725 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004726 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4727 fd = strtol(buf, NULL, 0);
4728 ret = -1;
4729 if (net_tap_fd_init(vlan, fd))
4730 ret = 0;
4731 } else {
bellardbf8c5342007-01-09 19:44:41 +00004732 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4733 ifname[0] = '\0';
4734 }
bellard7c9d8e02005-11-15 22:16:05 +00004735 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
4736 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
4737 }
thsb46a8902007-10-21 23:20:45 +00004738 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
4739 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
4740 }
4741 ret = net_tap_init(vlan, ifname, setup_script, down_script);
bellard7c9d8e02005-11-15 22:16:05 +00004742 }
4743 } else
bellardfd1dff42006-02-01 21:29:26 +00004744#endif
bellard7c9d8e02005-11-15 22:16:05 +00004745 if (!strcmp(device, "socket")) {
4746 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4747 int fd;
4748 fd = strtol(buf, NULL, 0);
4749 ret = -1;
4750 if (net_socket_fd_init(vlan, fd, 1))
4751 ret = 0;
4752 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
4753 ret = net_socket_listen_init(vlan, buf);
4754 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
4755 ret = net_socket_connect_init(vlan, buf);
bellard3d830452005-12-18 16:36:49 +00004756 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
4757 ret = net_socket_mcast_init(vlan, buf);
bellard7c9d8e02005-11-15 22:16:05 +00004758 } else {
4759 fprintf(stderr, "Unknown socket options: %s\n", p);
4760 return -1;
4761 }
blueswir1833c7172007-05-27 19:36:43 +00004762 vlan->nb_host_devs++;
bellard7c9d8e02005-11-15 22:16:05 +00004763 } else
bellard7c9d8e02005-11-15 22:16:05 +00004764 {
4765 fprintf(stderr, "Unknown network device: %s\n", device);
4766 return -1;
4767 }
4768 if (ret < 0) {
4769 fprintf(stderr, "Could not initialize device '%s'\n", device);
4770 }
ths3b46e622007-09-17 08:09:54 +00004771
bellard7c9d8e02005-11-15 22:16:05 +00004772 return ret;
4773}
4774
4775void do_info_network(void)
4776{
4777 VLANState *vlan;
4778 VLANClientState *vc;
4779
4780 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
4781 term_printf("VLAN %d devices:\n", vlan->id);
4782 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
4783 term_printf(" %s\n", vc->info_str);
4784 }
4785}
ths42550fd2006-12-22 16:34:12 +00004786
thse4bcb142007-12-02 04:51:10 +00004787#define HD_ALIAS "file=\"%s\",index=%d,media=disk"
4788#ifdef TARGET_PPC
4789#define CDROM_ALIAS "index=1,media=cdrom"
4790#else
4791#define CDROM_ALIAS "index=2,media=cdrom"
4792#endif
4793#define FD_ALIAS "index=%d,if=floppy"
4794#define PFLASH_ALIAS "file=\"%s\",if=pflash"
4795#define MTD_ALIAS "file=\"%s\",if=mtd"
balrog9d413d12007-12-04 00:10:34 +00004796#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00004797
4798static int drive_add(const char *fmt, ...)
4799{
4800 va_list ap;
4801
4802 if (nb_drives_opt >= MAX_DRIVES) {
4803 fprintf(stderr, "qemu: too many drives\n");
4804 exit(1);
4805 }
4806
4807 va_start(ap, fmt);
4808 vsnprintf(drives_opt[nb_drives_opt], sizeof(drives_opt[0]), fmt, ap);
4809 va_end(ap);
4810
4811 return nb_drives_opt++;
4812}
4813
4814int drive_get_index(BlockInterfaceType interface, int bus, int unit)
4815{
4816 int index;
4817
4818 /* seek interface, bus and unit */
4819
4820 for (index = 0; index < nb_drives; index++)
4821 if (drives_table[index].interface == interface &&
4822 drives_table[index].bus == bus &&
4823 drives_table[index].unit == unit)
4824 return index;
4825
4826 return -1;
4827}
4828
4829int drive_get_max_bus(BlockInterfaceType interface)
4830{
4831 int max_bus;
4832 int index;
4833
4834 max_bus = -1;
4835 for (index = 0; index < nb_drives; index++) {
4836 if(drives_table[index].interface == interface &&
4837 drives_table[index].bus > max_bus)
4838 max_bus = drives_table[index].bus;
4839 }
4840 return max_bus;
4841}
4842
4843static int drive_init(const char *str, int snapshot, QEMUMachine *machine)
4844{
4845 char buf[128];
4846 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00004847 char devname[128];
4848 const char *mediastr = "";
thse4bcb142007-12-02 04:51:10 +00004849 BlockInterfaceType interface;
4850 enum { MEDIA_DISK, MEDIA_CDROM } media;
4851 int bus_id, unit_id;
4852 int cyls, heads, secs, translation;
4853 BlockDriverState *bdrv;
4854 int max_devs;
4855 int index;
4856 char *params[] = { "bus", "unit", "if", "index", "cyls", "heads",
4857 "secs", "trans", "media", "snapshot", "file", NULL };
4858
4859 if (check_params(buf, sizeof(buf), params, str) < 0) {
4860 fprintf(stderr, "qemu: unknowm parameter '%s' in '%s'\n",
4861 buf, str);
4862 return -1;
4863 }
4864
4865 file[0] = 0;
4866 cyls = heads = secs = 0;
4867 bus_id = 0;
4868 unit_id = -1;
4869 translation = BIOS_ATA_TRANSLATION_AUTO;
4870 index = -1;
4871
4872 if (!strcmp(machine->name, "realview") ||
4873 !strcmp(machine->name, "SS-5") ||
4874 !strcmp(machine->name, "SS-10") ||
4875 !strcmp(machine->name, "SS-600MP") ||
4876 !strcmp(machine->name, "versatilepb") ||
4877 !strcmp(machine->name, "versatileab")) {
4878 interface = IF_SCSI;
4879 max_devs = MAX_SCSI_DEVS;
balrogc8522bd2007-12-06 22:11:20 +00004880 strcpy(devname, "scsi");
thse4bcb142007-12-02 04:51:10 +00004881 } else {
4882 interface = IF_IDE;
4883 max_devs = MAX_IDE_DEVS;
balrogc8522bd2007-12-06 22:11:20 +00004884 strcpy(devname, "ide");
thse4bcb142007-12-02 04:51:10 +00004885 }
4886 media = MEDIA_DISK;
4887
4888 /* extract parameters */
4889
4890 if (get_param_value(buf, sizeof(buf), "bus", str)) {
4891 bus_id = strtol(buf, NULL, 0);
4892 if (bus_id < 0) {
4893 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
4894 return -1;
4895 }
4896 }
4897
4898 if (get_param_value(buf, sizeof(buf), "unit", str)) {
4899 unit_id = strtol(buf, NULL, 0);
4900 if (unit_id < 0) {
4901 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
4902 return -1;
4903 }
4904 }
4905
4906 if (get_param_value(buf, sizeof(buf), "if", str)) {
balrogc8522bd2007-12-06 22:11:20 +00004907 strncpy(devname, buf, sizeof(devname));
thse4bcb142007-12-02 04:51:10 +00004908 if (!strcmp(buf, "ide")) {
4909 interface = IF_IDE;
4910 max_devs = MAX_IDE_DEVS;
4911 } else if (!strcmp(buf, "scsi")) {
4912 interface = IF_SCSI;
4913 max_devs = MAX_SCSI_DEVS;
4914 } else if (!strcmp(buf, "floppy")) {
4915 interface = IF_FLOPPY;
4916 max_devs = 0;
4917 } else if (!strcmp(buf, "pflash")) {
4918 interface = IF_PFLASH;
4919 max_devs = 0;
4920 } else if (!strcmp(buf, "mtd")) {
4921 interface = IF_MTD;
4922 max_devs = 0;
4923 } else if (!strcmp(buf, "sd")) {
4924 interface = IF_SD;
4925 max_devs = 0;
4926 } else {
4927 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
4928 return -1;
4929 }
4930 }
4931
4932 if (get_param_value(buf, sizeof(buf), "index", str)) {
4933 index = strtol(buf, NULL, 0);
4934 if (index < 0) {
4935 fprintf(stderr, "qemu: '%s' invalid index\n", str);
4936 return -1;
4937 }
4938 }
4939
4940 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
4941 cyls = strtol(buf, NULL, 0);
4942 }
4943
4944 if (get_param_value(buf, sizeof(buf), "heads", str)) {
4945 heads = strtol(buf, NULL, 0);
4946 }
4947
4948 if (get_param_value(buf, sizeof(buf), "secs", str)) {
4949 secs = strtol(buf, NULL, 0);
4950 }
4951
4952 if (cyls || heads || secs) {
4953 if (cyls < 1 || cyls > 16383) {
4954 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
4955 return -1;
4956 }
4957 if (heads < 1 || heads > 16) {
4958 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
4959 return -1;
4960 }
4961 if (secs < 1 || secs > 63) {
4962 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
4963 return -1;
4964 }
4965 }
4966
4967 if (get_param_value(buf, sizeof(buf), "trans", str)) {
4968 if (!cyls) {
4969 fprintf(stderr,
4970 "qemu: '%s' trans must be used with cyls,heads and secs\n",
4971 str);
4972 return -1;
4973 }
4974 if (!strcmp(buf, "none"))
4975 translation = BIOS_ATA_TRANSLATION_NONE;
4976 else if (!strcmp(buf, "lba"))
4977 translation = BIOS_ATA_TRANSLATION_LBA;
4978 else if (!strcmp(buf, "auto"))
4979 translation = BIOS_ATA_TRANSLATION_AUTO;
4980 else {
4981 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
4982 return -1;
4983 }
4984 }
4985
4986 if (get_param_value(buf, sizeof(buf), "media", str)) {
4987 if (!strcmp(buf, "disk")) {
4988 media = MEDIA_DISK;
4989 } else if (!strcmp(buf, "cdrom")) {
4990 if (cyls || secs || heads) {
4991 fprintf(stderr,
4992 "qemu: '%s' invalid physical CHS format\n", str);
4993 return -1;
4994 }
4995 media = MEDIA_CDROM;
4996 } else {
4997 fprintf(stderr, "qemu: '%s' invalid media\n", str);
4998 return -1;
4999 }
5000 }
5001
5002 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
5003 if (!strcmp(buf, "on"))
5004 snapshot = 1;
5005 else if (!strcmp(buf, "off"))
5006 snapshot = 0;
5007 else {
5008 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
5009 return -1;
5010 }
5011 }
5012
5013 get_param_value(file, sizeof(file), "file", str);
5014
5015 /* compute bus and unit according index */
5016
5017 if (index != -1) {
5018 if (bus_id != 0 || unit_id != -1) {
5019 fprintf(stderr,
5020 "qemu: '%s' index cannot be used with bus and unit\n", str);
5021 return -1;
5022 }
5023 if (max_devs == 0)
5024 {
5025 unit_id = index;
5026 bus_id = 0;
5027 } else {
5028 unit_id = index % max_devs;
5029 bus_id = index / max_devs;
5030 }
5031 }
5032
5033 /* if user doesn't specify a unit_id,
5034 * try to find the first free
5035 */
5036
5037 if (unit_id == -1) {
5038 unit_id = 0;
5039 while (drive_get_index(interface, bus_id, unit_id) != -1) {
5040 unit_id++;
5041 if (max_devs && unit_id >= max_devs) {
5042 unit_id -= max_devs;
5043 bus_id++;
5044 }
5045 }
5046 }
5047
5048 /* check unit id */
5049
5050 if (max_devs && unit_id >= max_devs) {
5051 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
5052 str, unit_id, max_devs - 1);
5053 return -1;
5054 }
5055
5056 /*
5057 * ignore multiple definitions
5058 */
5059
5060 if (drive_get_index(interface, bus_id, unit_id) != -1)
5061 return 0;
5062
5063 /* init */
5064
balrogc8522bd2007-12-06 22:11:20 +00005065 if (interface == IF_IDE || interface == IF_SCSI)
5066 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
5067 snprintf(buf, sizeof(buf), max_devs ? "%1$s%4$i%2$s%3$i" : "%s%s%i",
5068 devname, mediastr, unit_id, bus_id);
thse4bcb142007-12-02 04:51:10 +00005069 bdrv = bdrv_new(buf);
5070 drives_table[nb_drives].bdrv = bdrv;
5071 drives_table[nb_drives].interface = interface;
5072 drives_table[nb_drives].bus = bus_id;
5073 drives_table[nb_drives].unit = unit_id;
5074 nb_drives++;
5075
5076 switch(interface) {
5077 case IF_IDE:
5078 case IF_SCSI:
5079 switch(media) {
5080 case MEDIA_DISK:
5081 if (cyls != 0) {
5082 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
5083 bdrv_set_translation_hint(bdrv, translation);
5084 }
5085 break;
5086 case MEDIA_CDROM:
5087 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
5088 break;
5089 }
5090 break;
5091 case IF_SD:
5092 /* FIXME: This isn't really a floppy, but it's a reasonable
5093 approximation. */
5094 case IF_FLOPPY:
5095 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
5096 break;
5097 case IF_PFLASH:
5098 case IF_MTD:
5099 break;
5100 }
5101 if (!file[0])
5102 return 0;
5103 if (bdrv_open(bdrv, file, snapshot ? BDRV_O_SNAPSHOT : 0) < 0 ||
5104 qemu_key_check(bdrv, file)) {
5105 fprintf(stderr, "qemu: could not open disk image %s\n",
5106 file);
5107 return -1;
5108 }
5109 return 0;
5110}
5111
bellard330d0412003-07-26 18:11:40 +00005112/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00005113/* USB devices */
5114
pbrook0d92ed32006-05-21 16:30:15 +00005115static USBPort *used_usb_ports;
5116static USBPort *free_usb_ports;
5117
5118/* ??? Maybe change this to register a hub to keep track of the topology. */
5119void qemu_register_usb_port(USBPort *port, void *opaque, int index,
5120 usb_attachfn attach)
5121{
5122 port->opaque = opaque;
5123 port->index = index;
5124 port->attach = attach;
5125 port->next = free_usb_ports;
5126 free_usb_ports = port;
5127}
5128
bellarda594cfb2005-11-06 16:13:29 +00005129static int usb_device_add(const char *devname)
5130{
5131 const char *p;
5132 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00005133 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00005134
pbrook0d92ed32006-05-21 16:30:15 +00005135 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00005136 return -1;
5137
5138 if (strstart(devname, "host:", &p)) {
5139 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00005140 } else if (!strcmp(devname, "mouse")) {
5141 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00005142 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00005143 dev = usb_tablet_init();
5144 } else if (!strcmp(devname, "keyboard")) {
5145 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00005146 } else if (strstart(devname, "disk:", &p)) {
5147 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00005148 } else if (!strcmp(devname, "wacom-tablet")) {
5149 dev = usb_wacom_init();
bellarda594cfb2005-11-06 16:13:29 +00005150 } else {
5151 return -1;
5152 }
pbrook0d92ed32006-05-21 16:30:15 +00005153 if (!dev)
5154 return -1;
5155
5156 /* Find a USB port to add the device to. */
5157 port = free_usb_ports;
5158 if (!port->next) {
5159 USBDevice *hub;
5160
5161 /* Create a new hub and chain it on. */
5162 free_usb_ports = NULL;
5163 port->next = used_usb_ports;
5164 used_usb_ports = port;
5165
5166 hub = usb_hub_init(VM_USB_HUB_SIZE);
5167 usb_attach(port, hub);
5168 port = free_usb_ports;
5169 }
5170
5171 free_usb_ports = port->next;
5172 port->next = used_usb_ports;
5173 used_usb_ports = port;
5174 usb_attach(port, dev);
bellarda594cfb2005-11-06 16:13:29 +00005175 return 0;
5176}
5177
5178static int usb_device_del(const char *devname)
5179{
pbrook0d92ed32006-05-21 16:30:15 +00005180 USBPort *port;
5181 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00005182 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00005183 int bus_num, addr;
bellarda594cfb2005-11-06 16:13:29 +00005184 const char *p;
5185
pbrook0d92ed32006-05-21 16:30:15 +00005186 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00005187 return -1;
5188
5189 p = strchr(devname, '.');
ths5fafdf22007-09-16 21:08:06 +00005190 if (!p)
bellarda594cfb2005-11-06 16:13:29 +00005191 return -1;
5192 bus_num = strtoul(devname, NULL, 0);
5193 addr = strtoul(p + 1, NULL, 0);
5194 if (bus_num != 0)
5195 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00005196
5197 lastp = &used_usb_ports;
5198 port = used_usb_ports;
5199 while (port && port->dev->addr != addr) {
5200 lastp = &port->next;
5201 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00005202 }
pbrook0d92ed32006-05-21 16:30:15 +00005203
5204 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00005205 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00005206
bellard059809e2006-07-19 18:06:15 +00005207 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00005208 *lastp = port->next;
5209 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00005210 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00005211 port->next = free_usb_ports;
5212 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00005213 return 0;
5214}
5215
5216void do_usb_add(const char *devname)
5217{
5218 int ret;
5219 ret = usb_device_add(devname);
ths5fafdf22007-09-16 21:08:06 +00005220 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00005221 term_printf("Could not add USB device '%s'\n", devname);
5222}
5223
5224void do_usb_del(const char *devname)
5225{
5226 int ret;
5227 ret = usb_device_del(devname);
ths5fafdf22007-09-16 21:08:06 +00005228 if (ret < 0)
bellarda594cfb2005-11-06 16:13:29 +00005229 term_printf("Could not remove USB device '%s'\n", devname);
5230}
5231
5232void usb_info(void)
5233{
5234 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00005235 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00005236 const char *speed_str;
5237
pbrook0d92ed32006-05-21 16:30:15 +00005238 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00005239 term_printf("USB support not enabled\n");
5240 return;
5241 }
5242
pbrook0d92ed32006-05-21 16:30:15 +00005243 for (port = used_usb_ports; port; port = port->next) {
5244 dev = port->dev;
5245 if (!dev)
5246 continue;
5247 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00005248 case USB_SPEED_LOW:
5249 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00005250 break;
ths5fafdf22007-09-16 21:08:06 +00005251 case USB_SPEED_FULL:
5252 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00005253 break;
ths5fafdf22007-09-16 21:08:06 +00005254 case USB_SPEED_HIGH:
5255 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00005256 break;
5257 default:
ths5fafdf22007-09-16 21:08:06 +00005258 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00005259 break;
bellarda594cfb2005-11-06 16:13:29 +00005260 }
ths5fafdf22007-09-16 21:08:06 +00005261 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00005262 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00005263 }
5264}
5265
bellardf7cce892004-12-08 22:21:25 +00005266/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00005267/* PCMCIA/Cardbus */
5268
5269static struct pcmcia_socket_entry_s {
5270 struct pcmcia_socket_s *socket;
5271 struct pcmcia_socket_entry_s *next;
5272} *pcmcia_sockets = 0;
5273
5274void pcmcia_socket_register(struct pcmcia_socket_s *socket)
5275{
5276 struct pcmcia_socket_entry_s *entry;
5277
5278 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
5279 entry->socket = socket;
5280 entry->next = pcmcia_sockets;
5281 pcmcia_sockets = entry;
5282}
5283
5284void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
5285{
5286 struct pcmcia_socket_entry_s *entry, **ptr;
5287
5288 ptr = &pcmcia_sockets;
5289 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
5290 if (entry->socket == socket) {
5291 *ptr = entry->next;
5292 qemu_free(entry);
5293 }
5294}
5295
5296void pcmcia_info(void)
5297{
5298 struct pcmcia_socket_entry_s *iter;
5299 if (!pcmcia_sockets)
5300 term_printf("No PCMCIA sockets\n");
5301
5302 for (iter = pcmcia_sockets; iter; iter = iter->next)
5303 term_printf("%s: %s\n", iter->socket->slot_string,
5304 iter->socket->attached ? iter->socket->card_string :
5305 "Empty");
5306}
5307
5308/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00005309/* dumb display */
5310
5311static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
5312{
5313}
5314
5315static void dumb_resize(DisplayState *ds, int w, int h)
5316{
5317}
5318
5319static void dumb_refresh(DisplayState *ds)
5320{
5321#if defined(CONFIG_SDL)
5322 vga_hw_update();
5323#endif
5324}
5325
5326static void dumb_display_init(DisplayState *ds)
5327{
5328 ds->data = NULL;
5329 ds->linesize = 0;
5330 ds->depth = 0;
5331 ds->dpy_update = dumb_update;
5332 ds->dpy_resize = dumb_resize;
5333 ds->dpy_refresh = dumb_refresh;
5334}
5335
5336/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00005337/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00005338
bellardc4b1fcc2004-03-14 21:44:30 +00005339#define MAX_IO_HANDLERS 64
5340
5341typedef struct IOHandlerRecord {
5342 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00005343 IOCanRWHandler *fd_read_poll;
5344 IOHandler *fd_read;
5345 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00005346 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00005347 void *opaque;
5348 /* temporary data */
5349 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00005350 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00005351} IOHandlerRecord;
5352
bellard8a7ddc32004-03-31 19:00:16 +00005353static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00005354
bellard7c9d8e02005-11-15 22:16:05 +00005355/* XXX: fd_read_poll should be suppressed, but an API change is
5356 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00005357int qemu_set_fd_handler2(int fd,
5358 IOCanRWHandler *fd_read_poll,
5359 IOHandler *fd_read,
5360 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00005361 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00005362{
bellard8a7ddc32004-03-31 19:00:16 +00005363 IOHandlerRecord **pioh, *ioh;
5364
bellard7c9d8e02005-11-15 22:16:05 +00005365 if (!fd_read && !fd_write) {
5366 pioh = &first_io_handler;
5367 for(;;) {
5368 ioh = *pioh;
5369 if (ioh == NULL)
5370 break;
5371 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00005372 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00005373 break;
5374 }
5375 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00005376 }
bellard7c9d8e02005-11-15 22:16:05 +00005377 } else {
5378 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
5379 if (ioh->fd == fd)
5380 goto found;
5381 }
5382 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
5383 if (!ioh)
5384 return -1;
5385 ioh->next = first_io_handler;
5386 first_io_handler = ioh;
5387 found:
5388 ioh->fd = fd;
5389 ioh->fd_read_poll = fd_read_poll;
5390 ioh->fd_read = fd_read;
5391 ioh->fd_write = fd_write;
5392 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00005393 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00005394 }
bellard7c9d8e02005-11-15 22:16:05 +00005395 return 0;
5396}
5397
ths5fafdf22007-09-16 21:08:06 +00005398int qemu_set_fd_handler(int fd,
5399 IOHandler *fd_read,
5400 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00005401 void *opaque)
5402{
5403 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00005404}
5405
bellard8a7ddc32004-03-31 19:00:16 +00005406/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00005407/* Polling handling */
5408
5409typedef struct PollingEntry {
5410 PollingFunc *func;
5411 void *opaque;
5412 struct PollingEntry *next;
5413} PollingEntry;
5414
5415static PollingEntry *first_polling_entry;
5416
5417int qemu_add_polling_cb(PollingFunc *func, void *opaque)
5418{
5419 PollingEntry **ppe, *pe;
5420 pe = qemu_mallocz(sizeof(PollingEntry));
5421 if (!pe)
5422 return -1;
5423 pe->func = func;
5424 pe->opaque = opaque;
5425 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
5426 *ppe = pe;
5427 return 0;
5428}
5429
5430void qemu_del_polling_cb(PollingFunc *func, void *opaque)
5431{
5432 PollingEntry **ppe, *pe;
5433 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
5434 pe = *ppe;
5435 if (pe->func == func && pe->opaque == opaque) {
5436 *ppe = pe->next;
5437 qemu_free(pe);
5438 break;
5439 }
5440 }
5441}
5442
bellarda18e5242006-06-25 17:18:27 +00005443#ifdef _WIN32
5444/***********************************************************/
5445/* Wait objects support */
5446typedef struct WaitObjects {
5447 int num;
5448 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
5449 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
5450 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
5451} WaitObjects;
5452
5453static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00005454
bellarda18e5242006-06-25 17:18:27 +00005455int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5456{
5457 WaitObjects *w = &wait_objects;
5458
5459 if (w->num >= MAXIMUM_WAIT_OBJECTS)
5460 return -1;
5461 w->events[w->num] = handle;
5462 w->func[w->num] = func;
5463 w->opaque[w->num] = opaque;
5464 w->num++;
5465 return 0;
5466}
5467
5468void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5469{
5470 int i, found;
5471 WaitObjects *w = &wait_objects;
5472
5473 found = 0;
5474 for (i = 0; i < w->num; i++) {
5475 if (w->events[i] == handle)
5476 found = 1;
5477 if (found) {
5478 w->events[i] = w->events[i + 1];
5479 w->func[i] = w->func[i + 1];
5480 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00005481 }
bellarda18e5242006-06-25 17:18:27 +00005482 }
5483 if (found)
5484 w->num--;
5485}
5486#endif
5487
bellardf3311102006-04-12 20:21:17 +00005488/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00005489/* savevm/loadvm support */
bellardb4608c02003-06-27 17:34:32 +00005490
bellardfaea38e2006-08-05 21:31:00 +00005491#define IO_BUF_SIZE 32768
5492
5493struct QEMUFile {
5494 FILE *outfile;
5495 BlockDriverState *bs;
5496 int is_file;
5497 int is_writable;
5498 int64_t base_offset;
5499 int64_t buf_offset; /* start of buffer when writing, end of buffer
5500 when reading */
5501 int buf_index;
5502 int buf_size; /* 0 when writing */
5503 uint8_t buf[IO_BUF_SIZE];
5504};
5505
5506QEMUFile *qemu_fopen(const char *filename, const char *mode)
5507{
5508 QEMUFile *f;
5509
5510 f = qemu_mallocz(sizeof(QEMUFile));
5511 if (!f)
5512 return NULL;
5513 if (!strcmp(mode, "wb")) {
5514 f->is_writable = 1;
5515 } else if (!strcmp(mode, "rb")) {
5516 f->is_writable = 0;
5517 } else {
5518 goto fail;
5519 }
5520 f->outfile = fopen(filename, mode);
5521 if (!f->outfile)
5522 goto fail;
5523 f->is_file = 1;
5524 return f;
5525 fail:
5526 if (f->outfile)
5527 fclose(f->outfile);
5528 qemu_free(f);
5529 return NULL;
5530}
5531
pbrook9596ebb2007-11-18 01:44:38 +00005532static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
bellardfaea38e2006-08-05 21:31:00 +00005533{
5534 QEMUFile *f;
5535
5536 f = qemu_mallocz(sizeof(QEMUFile));
5537 if (!f)
5538 return NULL;
5539 f->is_file = 0;
5540 f->bs = bs;
5541 f->is_writable = is_writable;
5542 f->base_offset = offset;
5543 return f;
5544}
5545
5546void qemu_fflush(QEMUFile *f)
5547{
5548 if (!f->is_writable)
5549 return;
5550 if (f->buf_index > 0) {
5551 if (f->is_file) {
5552 fseek(f->outfile, f->buf_offset, SEEK_SET);
5553 fwrite(f->buf, 1, f->buf_index, f->outfile);
5554 } else {
ths5fafdf22007-09-16 21:08:06 +00005555 bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005556 f->buf, f->buf_index);
5557 }
5558 f->buf_offset += f->buf_index;
5559 f->buf_index = 0;
5560 }
5561}
5562
5563static void qemu_fill_buffer(QEMUFile *f)
5564{
5565 int len;
5566
5567 if (f->is_writable)
5568 return;
5569 if (f->is_file) {
5570 fseek(f->outfile, f->buf_offset, SEEK_SET);
5571 len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
5572 if (len < 0)
5573 len = 0;
5574 } else {
ths5fafdf22007-09-16 21:08:06 +00005575 len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
bellardfaea38e2006-08-05 21:31:00 +00005576 f->buf, IO_BUF_SIZE);
5577 if (len < 0)
5578 len = 0;
5579 }
5580 f->buf_index = 0;
5581 f->buf_size = len;
5582 f->buf_offset += len;
5583}
5584
5585void qemu_fclose(QEMUFile *f)
5586{
5587 if (f->is_writable)
5588 qemu_fflush(f);
5589 if (f->is_file) {
5590 fclose(f->outfile);
5591 }
5592 qemu_free(f);
5593}
5594
bellard8a7ddc32004-03-31 19:00:16 +00005595void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
5596{
bellardfaea38e2006-08-05 21:31:00 +00005597 int l;
5598 while (size > 0) {
5599 l = IO_BUF_SIZE - f->buf_index;
5600 if (l > size)
5601 l = size;
5602 memcpy(f->buf + f->buf_index, buf, l);
5603 f->buf_index += l;
5604 buf += l;
5605 size -= l;
5606 if (f->buf_index >= IO_BUF_SIZE)
5607 qemu_fflush(f);
5608 }
bellard8a7ddc32004-03-31 19:00:16 +00005609}
5610
5611void qemu_put_byte(QEMUFile *f, int v)
5612{
bellardfaea38e2006-08-05 21:31:00 +00005613 f->buf[f->buf_index++] = v;
5614 if (f->buf_index >= IO_BUF_SIZE)
5615 qemu_fflush(f);
5616}
5617
5618int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
5619{
5620 int size, l;
5621
5622 size = size1;
5623 while (size > 0) {
5624 l = f->buf_size - f->buf_index;
5625 if (l == 0) {
5626 qemu_fill_buffer(f);
5627 l = f->buf_size - f->buf_index;
5628 if (l == 0)
5629 break;
5630 }
5631 if (l > size)
5632 l = size;
5633 memcpy(buf, f->buf + f->buf_index, l);
5634 f->buf_index += l;
5635 buf += l;
5636 size -= l;
5637 }
5638 return size1 - size;
5639}
5640
5641int qemu_get_byte(QEMUFile *f)
5642{
5643 if (f->buf_index >= f->buf_size) {
5644 qemu_fill_buffer(f);
5645 if (f->buf_index >= f->buf_size)
5646 return 0;
5647 }
5648 return f->buf[f->buf_index++];
5649}
5650
5651int64_t qemu_ftell(QEMUFile *f)
5652{
5653 return f->buf_offset - f->buf_size + f->buf_index;
5654}
5655
5656int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
5657{
5658 if (whence == SEEK_SET) {
5659 /* nothing to do */
5660 } else if (whence == SEEK_CUR) {
5661 pos += qemu_ftell(f);
5662 } else {
5663 /* SEEK_END not supported */
5664 return -1;
5665 }
5666 if (f->is_writable) {
5667 qemu_fflush(f);
5668 f->buf_offset = pos;
5669 } else {
5670 f->buf_offset = pos;
5671 f->buf_index = 0;
5672 f->buf_size = 0;
5673 }
5674 return pos;
bellard8a7ddc32004-03-31 19:00:16 +00005675}
5676
5677void qemu_put_be16(QEMUFile *f, unsigned int v)
5678{
5679 qemu_put_byte(f, v >> 8);
5680 qemu_put_byte(f, v);
5681}
5682
5683void qemu_put_be32(QEMUFile *f, unsigned int v)
5684{
5685 qemu_put_byte(f, v >> 24);
5686 qemu_put_byte(f, v >> 16);
5687 qemu_put_byte(f, v >> 8);
5688 qemu_put_byte(f, v);
5689}
5690
5691void qemu_put_be64(QEMUFile *f, uint64_t v)
5692{
5693 qemu_put_be32(f, v >> 32);
5694 qemu_put_be32(f, v);
5695}
5696
bellard8a7ddc32004-03-31 19:00:16 +00005697unsigned int qemu_get_be16(QEMUFile *f)
5698{
5699 unsigned int v;
5700 v = qemu_get_byte(f) << 8;
5701 v |= qemu_get_byte(f);
5702 return v;
5703}
5704
5705unsigned int qemu_get_be32(QEMUFile *f)
5706{
5707 unsigned int v;
5708 v = qemu_get_byte(f) << 24;
5709 v |= qemu_get_byte(f) << 16;
5710 v |= qemu_get_byte(f) << 8;
5711 v |= qemu_get_byte(f);
5712 return v;
5713}
5714
5715uint64_t qemu_get_be64(QEMUFile *f)
5716{
5717 uint64_t v;
5718 v = (uint64_t)qemu_get_be32(f) << 32;
5719 v |= qemu_get_be32(f);
5720 return v;
5721}
5722
bellard8a7ddc32004-03-31 19:00:16 +00005723typedef struct SaveStateEntry {
5724 char idstr[256];
5725 int instance_id;
5726 int version_id;
5727 SaveStateHandler *save_state;
5728 LoadStateHandler *load_state;
5729 void *opaque;
5730 struct SaveStateEntry *next;
5731} SaveStateEntry;
5732
5733static SaveStateEntry *first_se;
5734
ths5fafdf22007-09-16 21:08:06 +00005735int register_savevm(const char *idstr,
5736 int instance_id,
bellard8a7ddc32004-03-31 19:00:16 +00005737 int version_id,
5738 SaveStateHandler *save_state,
5739 LoadStateHandler *load_state,
5740 void *opaque)
5741{
5742 SaveStateEntry *se, **pse;
5743
5744 se = qemu_malloc(sizeof(SaveStateEntry));
5745 if (!se)
5746 return -1;
5747 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
5748 se->instance_id = instance_id;
5749 se->version_id = version_id;
5750 se->save_state = save_state;
5751 se->load_state = load_state;
5752 se->opaque = opaque;
5753 se->next = NULL;
5754
5755 /* add at the end of list */
5756 pse = &first_se;
5757 while (*pse != NULL)
5758 pse = &(*pse)->next;
5759 *pse = se;
5760 return 0;
5761}
5762
5763#define QEMU_VM_FILE_MAGIC 0x5145564d
bellardfaea38e2006-08-05 21:31:00 +00005764#define QEMU_VM_FILE_VERSION 0x00000002
bellard8a7ddc32004-03-31 19:00:16 +00005765
pbrook9596ebb2007-11-18 01:44:38 +00005766static int qemu_savevm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005767{
5768 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005769 int len, ret;
5770 int64_t cur_pos, len_pos, total_len_pos;
bellard313aa562003-08-10 21:52:11 +00005771
bellard8a7ddc32004-03-31 19:00:16 +00005772 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
5773 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
bellardfaea38e2006-08-05 21:31:00 +00005774 total_len_pos = qemu_ftell(f);
5775 qemu_put_be64(f, 0); /* total size */
bellard8a7ddc32004-03-31 19:00:16 +00005776
5777 for(se = first_se; se != NULL; se = se->next) {
5778 /* ID string */
5779 len = strlen(se->idstr);
5780 qemu_put_byte(f, len);
5781 qemu_put_buffer(f, se->idstr, len);
5782
5783 qemu_put_be32(f, se->instance_id);
5784 qemu_put_be32(f, se->version_id);
5785
5786 /* record size: filled later */
bellardfaea38e2006-08-05 21:31:00 +00005787 len_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005788 qemu_put_be32(f, 0);
bellard8a7ddc32004-03-31 19:00:16 +00005789 se->save_state(f, se->opaque);
5790
5791 /* fill record size */
bellardfaea38e2006-08-05 21:31:00 +00005792 cur_pos = qemu_ftell(f);
5793 len = cur_pos - len_pos - 4;
5794 qemu_fseek(f, len_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005795 qemu_put_be32(f, len);
bellardfaea38e2006-08-05 21:31:00 +00005796 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005797 }
bellardfaea38e2006-08-05 21:31:00 +00005798 cur_pos = qemu_ftell(f);
5799 qemu_fseek(f, total_len_pos, SEEK_SET);
5800 qemu_put_be64(f, cur_pos - total_len_pos - 8);
5801 qemu_fseek(f, cur_pos, SEEK_SET);
bellard8a7ddc32004-03-31 19:00:16 +00005802
bellard8a7ddc32004-03-31 19:00:16 +00005803 ret = 0;
bellard8a7ddc32004-03-31 19:00:16 +00005804 return ret;
5805}
5806
5807static SaveStateEntry *find_se(const char *idstr, int instance_id)
5808{
5809 SaveStateEntry *se;
5810
5811 for(se = first_se; se != NULL; se = se->next) {
ths5fafdf22007-09-16 21:08:06 +00005812 if (!strcmp(se->idstr, idstr) &&
bellard8a7ddc32004-03-31 19:00:16 +00005813 instance_id == se->instance_id)
5814 return se;
5815 }
5816 return NULL;
5817}
5818
pbrook9596ebb2007-11-18 01:44:38 +00005819static int qemu_loadvm_state(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00005820{
5821 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00005822 int len, ret, instance_id, record_len, version_id;
5823 int64_t total_len, end_pos, cur_pos;
bellard8a7ddc32004-03-31 19:00:16 +00005824 unsigned int v;
5825 char idstr[256];
ths3b46e622007-09-17 08:09:54 +00005826
bellard8a7ddc32004-03-31 19:00:16 +00005827 v = qemu_get_be32(f);
5828 if (v != QEMU_VM_FILE_MAGIC)
5829 goto fail;
5830 v = qemu_get_be32(f);
5831 if (v != QEMU_VM_FILE_VERSION) {
5832 fail:
bellard8a7ddc32004-03-31 19:00:16 +00005833 ret = -1;
5834 goto the_end;
5835 }
bellardfaea38e2006-08-05 21:31:00 +00005836 total_len = qemu_get_be64(f);
5837 end_pos = total_len + qemu_ftell(f);
bellardb4608c02003-06-27 17:34:32 +00005838 for(;;) {
bellardfaea38e2006-08-05 21:31:00 +00005839 if (qemu_ftell(f) >= end_pos)
bellard8a7ddc32004-03-31 19:00:16 +00005840 break;
bellardfaea38e2006-08-05 21:31:00 +00005841 len = qemu_get_byte(f);
bellard8a7ddc32004-03-31 19:00:16 +00005842 qemu_get_buffer(f, idstr, len);
5843 idstr[len] = '\0';
5844 instance_id = qemu_get_be32(f);
5845 version_id = qemu_get_be32(f);
5846 record_len = qemu_get_be32(f);
5847#if 0
ths5fafdf22007-09-16 21:08:06 +00005848 printf("idstr=%s instance=0x%x version=%d len=%d\n",
bellard8a7ddc32004-03-31 19:00:16 +00005849 idstr, instance_id, version_id, record_len);
bellardc45886d2004-01-05 00:02:06 +00005850#endif
bellardfaea38e2006-08-05 21:31:00 +00005851 cur_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00005852 se = find_se(idstr, instance_id);
5853 if (!se) {
ths5fafdf22007-09-16 21:08:06 +00005854 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
bellard8a7ddc32004-03-31 19:00:16 +00005855 instance_id, idstr);
5856 } else {
5857 ret = se->load_state(f, se->opaque, version_id);
5858 if (ret < 0) {
ths5fafdf22007-09-16 21:08:06 +00005859 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
bellard8a7ddc32004-03-31 19:00:16 +00005860 instance_id, idstr);
5861 }
bellard34865132003-10-05 14:28:56 +00005862 }
bellard8a7ddc32004-03-31 19:00:16 +00005863 /* always seek to exact end of record */
5864 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
5865 }
bellard8a7ddc32004-03-31 19:00:16 +00005866 ret = 0;
5867 the_end:
bellardfaea38e2006-08-05 21:31:00 +00005868 return ret;
5869}
5870
5871/* device can contain snapshots */
5872static int bdrv_can_snapshot(BlockDriverState *bs)
5873{
5874 return (bs &&
5875 !bdrv_is_removable(bs) &&
5876 !bdrv_is_read_only(bs));
5877}
5878
5879/* device must be snapshots in order to have a reliable snapshot */
5880static int bdrv_has_snapshot(BlockDriverState *bs)
5881{
5882 return (bs &&
5883 !bdrv_is_removable(bs) &&
5884 !bdrv_is_read_only(bs));
5885}
5886
5887static BlockDriverState *get_bs_snapshots(void)
5888{
5889 BlockDriverState *bs;
5890 int i;
5891
5892 if (bs_snapshots)
5893 return bs_snapshots;
thse4bcb142007-12-02 04:51:10 +00005894 for(i = 0; i <= nb_drives; i++) {
5895 bs = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00005896 if (bdrv_can_snapshot(bs))
5897 goto ok;
5898 }
5899 return NULL;
5900 ok:
5901 bs_snapshots = bs;
5902 return bs;
5903}
5904
5905static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
5906 const char *name)
5907{
5908 QEMUSnapshotInfo *sn_tab, *sn;
5909 int nb_sns, i, ret;
ths3b46e622007-09-17 08:09:54 +00005910
bellardfaea38e2006-08-05 21:31:00 +00005911 ret = -ENOENT;
5912 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
5913 if (nb_sns < 0)
5914 return ret;
5915 for(i = 0; i < nb_sns; i++) {
5916 sn = &sn_tab[i];
5917 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
5918 *sn_info = *sn;
5919 ret = 0;
5920 break;
5921 }
5922 }
5923 qemu_free(sn_tab);
5924 return ret;
5925}
5926
5927void do_savevm(const char *name)
5928{
5929 BlockDriverState *bs, *bs1;
5930 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
5931 int must_delete, ret, i;
5932 BlockDriverInfo bdi1, *bdi = &bdi1;
5933 QEMUFile *f;
5934 int saved_vm_running;
bellard4c279bd2006-08-17 09:43:50 +00005935#ifdef _WIN32
5936 struct _timeb tb;
5937#else
bellardfaea38e2006-08-05 21:31:00 +00005938 struct timeval tv;
bellard4c279bd2006-08-17 09:43:50 +00005939#endif
bellardfaea38e2006-08-05 21:31:00 +00005940
5941 bs = get_bs_snapshots();
5942 if (!bs) {
5943 term_printf("No block device can accept snapshots\n");
5944 return;
5945 }
5946
pbrook6192bc32006-09-03 12:08:37 +00005947 /* ??? Should this occur after vm_stop? */
5948 qemu_aio_flush();
5949
bellardfaea38e2006-08-05 21:31:00 +00005950 saved_vm_running = vm_running;
5951 vm_stop(0);
ths3b46e622007-09-17 08:09:54 +00005952
bellardfaea38e2006-08-05 21:31:00 +00005953 must_delete = 0;
5954 if (name) {
5955 ret = bdrv_snapshot_find(bs, old_sn, name);
5956 if (ret >= 0) {
5957 must_delete = 1;
5958 }
5959 }
5960 memset(sn, 0, sizeof(*sn));
5961 if (must_delete) {
5962 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
5963 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
5964 } else {
5965 if (name)
5966 pstrcpy(sn->name, sizeof(sn->name), name);
5967 }
5968
5969 /* fill auxiliary fields */
bellard4c279bd2006-08-17 09:43:50 +00005970#ifdef _WIN32
5971 _ftime(&tb);
5972 sn->date_sec = tb.time;
5973 sn->date_nsec = tb.millitm * 1000000;
5974#else
bellardfaea38e2006-08-05 21:31:00 +00005975 gettimeofday(&tv, NULL);
5976 sn->date_sec = tv.tv_sec;
5977 sn->date_nsec = tv.tv_usec * 1000;
bellard4c279bd2006-08-17 09:43:50 +00005978#endif
bellardfaea38e2006-08-05 21:31:00 +00005979 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
ths3b46e622007-09-17 08:09:54 +00005980
bellardfaea38e2006-08-05 21:31:00 +00005981 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
5982 term_printf("Device %s does not support VM state snapshots\n",
5983 bdrv_get_device_name(bs));
5984 goto the_end;
5985 }
ths3b46e622007-09-17 08:09:54 +00005986
bellardfaea38e2006-08-05 21:31:00 +00005987 /* save the VM state */
5988 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
5989 if (!f) {
5990 term_printf("Could not open VM state file\n");
5991 goto the_end;
5992 }
5993 ret = qemu_savevm_state(f);
5994 sn->vm_state_size = qemu_ftell(f);
5995 qemu_fclose(f);
5996 if (ret < 0) {
5997 term_printf("Error %d while writing VM\n", ret);
5998 goto the_end;
5999 }
ths3b46e622007-09-17 08:09:54 +00006000
bellardfaea38e2006-08-05 21:31:00 +00006001 /* create the snapshots */
6002
thse4bcb142007-12-02 04:51:10 +00006003 for(i = 0; i < nb_drives; i++) {
6004 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00006005 if (bdrv_has_snapshot(bs1)) {
6006 if (must_delete) {
6007 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
6008 if (ret < 0) {
6009 term_printf("Error while deleting snapshot on '%s'\n",
6010 bdrv_get_device_name(bs1));
6011 }
6012 }
6013 ret = bdrv_snapshot_create(bs1, sn);
6014 if (ret < 0) {
6015 term_printf("Error while creating snapshot on '%s'\n",
6016 bdrv_get_device_name(bs1));
6017 }
6018 }
6019 }
6020
6021 the_end:
bellard8a7ddc32004-03-31 19:00:16 +00006022 if (saved_vm_running)
6023 vm_start();
bellardfaea38e2006-08-05 21:31:00 +00006024}
6025
6026void do_loadvm(const char *name)
6027{
6028 BlockDriverState *bs, *bs1;
6029 BlockDriverInfo bdi1, *bdi = &bdi1;
6030 QEMUFile *f;
6031 int i, ret;
6032 int saved_vm_running;
6033
6034 bs = get_bs_snapshots();
6035 if (!bs) {
6036 term_printf("No block device supports snapshots\n");
6037 return;
6038 }
ths3b46e622007-09-17 08:09:54 +00006039
pbrook6192bc32006-09-03 12:08:37 +00006040 /* Flush all IO requests so they don't interfere with the new state. */
6041 qemu_aio_flush();
6042
bellardfaea38e2006-08-05 21:31:00 +00006043 saved_vm_running = vm_running;
6044 vm_stop(0);
6045
thse4bcb142007-12-02 04:51:10 +00006046 for(i = 0; i <= nb_drives; i++) {
6047 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00006048 if (bdrv_has_snapshot(bs1)) {
6049 ret = bdrv_snapshot_goto(bs1, name);
6050 if (ret < 0) {
6051 if (bs != bs1)
6052 term_printf("Warning: ");
6053 switch(ret) {
6054 case -ENOTSUP:
6055 term_printf("Snapshots not supported on device '%s'\n",
6056 bdrv_get_device_name(bs1));
6057 break;
6058 case -ENOENT:
6059 term_printf("Could not find snapshot '%s' on device '%s'\n",
6060 name, bdrv_get_device_name(bs1));
6061 break;
6062 default:
6063 term_printf("Error %d while activating snapshot on '%s'\n",
6064 ret, bdrv_get_device_name(bs1));
6065 break;
6066 }
6067 /* fatal on snapshot block device */
6068 if (bs == bs1)
6069 goto the_end;
6070 }
6071 }
6072 }
6073
6074 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6075 term_printf("Device %s does not support VM state snapshots\n",
6076 bdrv_get_device_name(bs));
6077 return;
6078 }
ths3b46e622007-09-17 08:09:54 +00006079
bellardfaea38e2006-08-05 21:31:00 +00006080 /* restore the VM state */
6081 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
6082 if (!f) {
6083 term_printf("Could not open VM state file\n");
6084 goto the_end;
6085 }
6086 ret = qemu_loadvm_state(f);
6087 qemu_fclose(f);
6088 if (ret < 0) {
6089 term_printf("Error %d while loading VM state\n", ret);
6090 }
6091 the_end:
6092 if (saved_vm_running)
6093 vm_start();
6094}
6095
6096void do_delvm(const char *name)
6097{
6098 BlockDriverState *bs, *bs1;
6099 int i, ret;
6100
6101 bs = get_bs_snapshots();
6102 if (!bs) {
6103 term_printf("No block device supports snapshots\n");
6104 return;
6105 }
ths3b46e622007-09-17 08:09:54 +00006106
thse4bcb142007-12-02 04:51:10 +00006107 for(i = 0; i <= nb_drives; i++) {
6108 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00006109 if (bdrv_has_snapshot(bs1)) {
6110 ret = bdrv_snapshot_delete(bs1, name);
6111 if (ret < 0) {
6112 if (ret == -ENOTSUP)
6113 term_printf("Snapshots not supported on device '%s'\n",
6114 bdrv_get_device_name(bs1));
6115 else
6116 term_printf("Error %d while deleting snapshot on '%s'\n",
6117 ret, bdrv_get_device_name(bs1));
6118 }
6119 }
6120 }
6121}
6122
6123void do_info_snapshots(void)
6124{
6125 BlockDriverState *bs, *bs1;
6126 QEMUSnapshotInfo *sn_tab, *sn;
6127 int nb_sns, i;
6128 char buf[256];
6129
6130 bs = get_bs_snapshots();
6131 if (!bs) {
6132 term_printf("No available block device supports snapshots\n");
6133 return;
6134 }
6135 term_printf("Snapshot devices:");
thse4bcb142007-12-02 04:51:10 +00006136 for(i = 0; i <= nb_drives; i++) {
6137 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00006138 if (bdrv_has_snapshot(bs1)) {
6139 if (bs == bs1)
6140 term_printf(" %s", bdrv_get_device_name(bs1));
6141 }
6142 }
6143 term_printf("\n");
6144
6145 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
6146 if (nb_sns < 0) {
6147 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
6148 return;
6149 }
6150 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
6151 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
6152 for(i = 0; i < nb_sns; i++) {
6153 sn = &sn_tab[i];
6154 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
6155 }
6156 qemu_free(sn_tab);
bellard8a7ddc32004-03-31 19:00:16 +00006157}
6158
6159/***********************************************************/
6160/* cpu save/restore */
6161
6162#if defined(TARGET_I386)
6163
6164static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
6165{
bellard02ba45c2004-06-25 14:46:23 +00006166 qemu_put_be32(f, dt->selector);
bellard20f32282005-01-03 23:36:21 +00006167 qemu_put_betl(f, dt->base);
bellard8a7ddc32004-03-31 19:00:16 +00006168 qemu_put_be32(f, dt->limit);
6169 qemu_put_be32(f, dt->flags);
6170}
6171
6172static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
6173{
bellard02ba45c2004-06-25 14:46:23 +00006174 dt->selector = qemu_get_be32(f);
bellard20f32282005-01-03 23:36:21 +00006175 dt->base = qemu_get_betl(f);
bellard8a7ddc32004-03-31 19:00:16 +00006176 dt->limit = qemu_get_be32(f);
6177 dt->flags = qemu_get_be32(f);
6178}
6179
6180void cpu_save(QEMUFile *f, void *opaque)
6181{
6182 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00006183 uint16_t fptag, fpus, fpuc, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00006184 uint32_t hflags;
6185 int i;
ths3b46e622007-09-17 08:09:54 +00006186
bellard20f32282005-01-03 23:36:21 +00006187 for(i = 0; i < CPU_NB_REGS; i++)
6188 qemu_put_betls(f, &env->regs[i]);
6189 qemu_put_betls(f, &env->eip);
6190 qemu_put_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00006191 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
6192 qemu_put_be32s(f, &hflags);
ths3b46e622007-09-17 08:09:54 +00006193
bellard8a7ddc32004-03-31 19:00:16 +00006194 /* FPU */
6195 fpuc = env->fpuc;
6196 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
6197 fptag = 0;
bellard664e0f12005-01-08 18:58:29 +00006198 for(i = 0; i < 8; i++) {
6199 fptag |= ((!env->fptags[i]) << i);
bellard8a7ddc32004-03-31 19:00:16 +00006200 }
ths3b46e622007-09-17 08:09:54 +00006201
bellard8a7ddc32004-03-31 19:00:16 +00006202 qemu_put_be16s(f, &fpuc);
6203 qemu_put_be16s(f, &fpus);
6204 qemu_put_be16s(f, &fptag);
6205
bellard664e0f12005-01-08 18:58:29 +00006206#ifdef USE_X86LDOUBLE
6207 fpregs_format = 0;
6208#else
6209 fpregs_format = 1;
6210#endif
6211 qemu_put_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00006212
bellard8a7ddc32004-03-31 19:00:16 +00006213 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00006214#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00006215 {
6216 uint64_t mant;
6217 uint16_t exp;
6218 /* we save the real CPU data (in case of MMX usage only 'mant'
6219 contains the MMX register */
6220 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
6221 qemu_put_be64(f, mant);
6222 qemu_put_be16(f, exp);
6223 }
bellard664e0f12005-01-08 18:58:29 +00006224#else
6225 /* if we use doubles for float emulation, we save the doubles to
6226 avoid losing information in case of MMX usage. It can give
6227 problems if the image is restored on a CPU where long
6228 doubles are used instead. */
bellard8636b5d2005-01-09 00:03:14 +00006229 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
bellard664e0f12005-01-08 18:58:29 +00006230#endif
bellard8a7ddc32004-03-31 19:00:16 +00006231 }
6232
6233 for(i = 0; i < 6; i++)
6234 cpu_put_seg(f, &env->segs[i]);
6235 cpu_put_seg(f, &env->ldt);
6236 cpu_put_seg(f, &env->tr);
6237 cpu_put_seg(f, &env->gdt);
6238 cpu_put_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00006239
bellard8a7ddc32004-03-31 19:00:16 +00006240 qemu_put_be32s(f, &env->sysenter_cs);
6241 qemu_put_be32s(f, &env->sysenter_esp);
6242 qemu_put_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00006243
bellard20f32282005-01-03 23:36:21 +00006244 qemu_put_betls(f, &env->cr[0]);
6245 qemu_put_betls(f, &env->cr[2]);
6246 qemu_put_betls(f, &env->cr[3]);
6247 qemu_put_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00006248
bellard8a7ddc32004-03-31 19:00:16 +00006249 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00006250 qemu_put_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00006251
6252 /* MMU */
6253 qemu_put_be32s(f, &env->a20_mask);
bellard02536f82005-01-06 20:43:38 +00006254
bellard664e0f12005-01-08 18:58:29 +00006255 /* XMM */
6256 qemu_put_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00006257 for(i = 0; i < CPU_NB_REGS; i++) {
6258 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
6259 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
6260 }
6261
bellard664e0f12005-01-08 18:58:29 +00006262#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00006263 qemu_put_be64s(f, &env->efer);
6264 qemu_put_be64s(f, &env->star);
6265 qemu_put_be64s(f, &env->lstar);
6266 qemu_put_be64s(f, &env->cstar);
6267 qemu_put_be64s(f, &env->fmask);
6268 qemu_put_be64s(f, &env->kernelgsbase);
6269#endif
bellard3b21e032006-09-24 18:41:56 +00006270 qemu_put_be32s(f, &env->smbase);
bellard8a7ddc32004-03-31 19:00:16 +00006271}
6272
bellard8636b5d2005-01-09 00:03:14 +00006273#ifdef USE_X86LDOUBLE
bellard664e0f12005-01-08 18:58:29 +00006274/* XXX: add that in a FPU generic layer */
6275union x86_longdouble {
6276 uint64_t mant;
6277 uint16_t exp;
6278};
6279
6280#define MANTD1(fp) (fp & ((1LL << 52) - 1))
6281#define EXPBIAS1 1023
6282#define EXPD1(fp) ((fp >> 52) & 0x7FF)
6283#define SIGND1(fp) ((fp >> 32) & 0x80000000)
6284
6285static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
6286{
6287 int e;
6288 /* mantissa */
6289 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
6290 /* exponent + sign */
6291 e = EXPD1(temp) - EXPBIAS1 + 16383;
6292 e |= SIGND1(temp) >> 16;
6293 p->exp = e;
6294}
bellard8636b5d2005-01-09 00:03:14 +00006295#endif
bellard664e0f12005-01-08 18:58:29 +00006296
bellard8a7ddc32004-03-31 19:00:16 +00006297int cpu_load(QEMUFile *f, void *opaque, int version_id)
6298{
6299 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00006300 int i, guess_mmx;
bellard8a7ddc32004-03-31 19:00:16 +00006301 uint32_t hflags;
bellard664e0f12005-01-08 18:58:29 +00006302 uint16_t fpus, fpuc, fptag, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00006303
bellard3b21e032006-09-24 18:41:56 +00006304 if (version_id != 3 && version_id != 4)
bellard8a7ddc32004-03-31 19:00:16 +00006305 return -EINVAL;
bellard20f32282005-01-03 23:36:21 +00006306 for(i = 0; i < CPU_NB_REGS; i++)
6307 qemu_get_betls(f, &env->regs[i]);
6308 qemu_get_betls(f, &env->eip);
6309 qemu_get_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00006310 qemu_get_be32s(f, &hflags);
6311
6312 qemu_get_be16s(f, &fpuc);
6313 qemu_get_be16s(f, &fpus);
6314 qemu_get_be16s(f, &fptag);
bellard664e0f12005-01-08 18:58:29 +00006315 qemu_get_be16s(f, &fpregs_format);
ths3b46e622007-09-17 08:09:54 +00006316
bellard664e0f12005-01-08 18:58:29 +00006317 /* NOTE: we cannot always restore the FPU state if the image come
6318 from a host with a different 'USE_X86LDOUBLE' define. We guess
6319 if we are in an MMX state to restore correctly in that case. */
6320 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
bellard8a7ddc32004-03-31 19:00:16 +00006321 for(i = 0; i < 8; i++) {
6322 uint64_t mant;
6323 uint16_t exp;
ths3b46e622007-09-17 08:09:54 +00006324
bellard664e0f12005-01-08 18:58:29 +00006325 switch(fpregs_format) {
6326 case 0:
6327 mant = qemu_get_be64(f);
6328 exp = qemu_get_be16(f);
6329#ifdef USE_X86LDOUBLE
6330 env->fpregs[i].d = cpu_set_fp80(mant, exp);
6331#else
6332 /* difficult case */
6333 if (guess_mmx)
bellard8636b5d2005-01-09 00:03:14 +00006334 env->fpregs[i].mmx.MMX_Q(0) = mant;
bellard664e0f12005-01-08 18:58:29 +00006335 else
6336 env->fpregs[i].d = cpu_set_fp80(mant, exp);
6337#endif
6338 break;
6339 case 1:
6340 mant = qemu_get_be64(f);
6341#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00006342 {
6343 union x86_longdouble *p;
6344 /* difficult case */
6345 p = (void *)&env->fpregs[i];
6346 if (guess_mmx) {
6347 p->mant = mant;
6348 p->exp = 0xffff;
6349 } else {
6350 fp64_to_fp80(p, mant);
6351 }
bellard664e0f12005-01-08 18:58:29 +00006352 }
6353#else
bellard8636b5d2005-01-09 00:03:14 +00006354 env->fpregs[i].mmx.MMX_Q(0) = mant;
ths3b46e622007-09-17 08:09:54 +00006355#endif
bellard664e0f12005-01-08 18:58:29 +00006356 break;
6357 default:
6358 return -EINVAL;
6359 }
bellard8a7ddc32004-03-31 19:00:16 +00006360 }
6361
6362 env->fpuc = fpuc;
bellard7a0e1f42005-03-13 17:01:47 +00006363 /* XXX: restore FPU round state */
bellard8a7ddc32004-03-31 19:00:16 +00006364 env->fpstt = (fpus >> 11) & 7;
6365 env->fpus = fpus & ~0x3800;
bellard664e0f12005-01-08 18:58:29 +00006366 fptag ^= 0xff;
bellard8a7ddc32004-03-31 19:00:16 +00006367 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00006368 env->fptags[i] = (fptag >> i) & 1;
bellard8a7ddc32004-03-31 19:00:16 +00006369 }
ths3b46e622007-09-17 08:09:54 +00006370
bellard8a7ddc32004-03-31 19:00:16 +00006371 for(i = 0; i < 6; i++)
6372 cpu_get_seg(f, &env->segs[i]);
6373 cpu_get_seg(f, &env->ldt);
6374 cpu_get_seg(f, &env->tr);
6375 cpu_get_seg(f, &env->gdt);
6376 cpu_get_seg(f, &env->idt);
ths3b46e622007-09-17 08:09:54 +00006377
bellard8a7ddc32004-03-31 19:00:16 +00006378 qemu_get_be32s(f, &env->sysenter_cs);
6379 qemu_get_be32s(f, &env->sysenter_esp);
6380 qemu_get_be32s(f, &env->sysenter_eip);
ths3b46e622007-09-17 08:09:54 +00006381
bellard20f32282005-01-03 23:36:21 +00006382 qemu_get_betls(f, &env->cr[0]);
6383 qemu_get_betls(f, &env->cr[2]);
6384 qemu_get_betls(f, &env->cr[3]);
6385 qemu_get_betls(f, &env->cr[4]);
ths3b46e622007-09-17 08:09:54 +00006386
bellard8a7ddc32004-03-31 19:00:16 +00006387 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00006388 qemu_get_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00006389
6390 /* MMU */
6391 qemu_get_be32s(f, &env->a20_mask);
6392
bellard664e0f12005-01-08 18:58:29 +00006393 qemu_get_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00006394 for(i = 0; i < CPU_NB_REGS; i++) {
6395 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
6396 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
6397 }
6398
bellard664e0f12005-01-08 18:58:29 +00006399#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00006400 qemu_get_be64s(f, &env->efer);
6401 qemu_get_be64s(f, &env->star);
6402 qemu_get_be64s(f, &env->lstar);
6403 qemu_get_be64s(f, &env->cstar);
6404 qemu_get_be64s(f, &env->fmask);
6405 qemu_get_be64s(f, &env->kernelgsbase);
6406#endif
ths5fafdf22007-09-16 21:08:06 +00006407 if (version_id >= 4)
bellard3b21e032006-09-24 18:41:56 +00006408 qemu_get_be32s(f, &env->smbase);
bellard02536f82005-01-06 20:43:38 +00006409
bellard8a7ddc32004-03-31 19:00:16 +00006410 /* XXX: compute hflags from scratch, except for CPL and IIF */
6411 env->hflags = hflags;
6412 tlb_flush(env, 1);
6413 return 0;
6414}
6415
bellarda541f292004-04-12 20:39:29 +00006416#elif defined(TARGET_PPC)
6417void cpu_save(QEMUFile *f, void *opaque)
6418{
6419}
6420
6421int cpu_load(QEMUFile *f, void *opaque, int version_id)
6422{
6423 return 0;
6424}
bellard6af0bf92005-07-02 14:58:51 +00006425
6426#elif defined(TARGET_MIPS)
6427void cpu_save(QEMUFile *f, void *opaque)
6428{
6429}
6430
6431int cpu_load(QEMUFile *f, void *opaque, int version_id)
6432{
6433 return 0;
6434}
6435
bellarde95c8d52004-09-30 22:22:08 +00006436#elif defined(TARGET_SPARC)
6437void cpu_save(QEMUFile *f, void *opaque)
6438{
bellarde80cfcf2004-12-19 23:18:01 +00006439 CPUState *env = opaque;
6440 int i;
6441 uint32_t tmp;
6442
bellard4fa5d772005-01-30 22:57:54 +00006443 for(i = 0; i < 8; i++)
6444 qemu_put_betls(f, &env->gregs[i]);
6445 for(i = 0; i < NWINDOWS * 16; i++)
6446 qemu_put_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006447
6448 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006449 for(i = 0; i < TARGET_FPREGS; i++) {
6450 union {
bellard1bdb68e2006-06-21 18:48:01 +00006451 float32 f;
6452 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006453 } u;
6454 u.f = env->fpr[i];
bellard1bdb68e2006-06-21 18:48:01 +00006455 qemu_put_be32(f, u.i);
bellarde80cfcf2004-12-19 23:18:01 +00006456 }
bellard4fa5d772005-01-30 22:57:54 +00006457
6458 qemu_put_betls(f, &env->pc);
6459 qemu_put_betls(f, &env->npc);
6460 qemu_put_betls(f, &env->y);
bellarde80cfcf2004-12-19 23:18:01 +00006461 tmp = GET_PSR(env);
bellard4fa5d772005-01-30 22:57:54 +00006462 qemu_put_be32(f, tmp);
bellard34751872005-07-02 14:31:34 +00006463 qemu_put_betls(f, &env->fsr);
6464 qemu_put_betls(f, &env->tbr);
6465#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006466 qemu_put_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006467 /* MMU */
6468 for(i = 0; i < 16; i++)
6469 qemu_put_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006470#endif
bellarde95c8d52004-09-30 22:22:08 +00006471}
6472
6473int cpu_load(QEMUFile *f, void *opaque, int version_id)
6474{
bellarde80cfcf2004-12-19 23:18:01 +00006475 CPUState *env = opaque;
6476 int i;
6477 uint32_t tmp;
6478
bellard4fa5d772005-01-30 22:57:54 +00006479 for(i = 0; i < 8; i++)
6480 qemu_get_betls(f, &env->gregs[i]);
6481 for(i = 0; i < NWINDOWS * 16; i++)
6482 qemu_get_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00006483
6484 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00006485 for(i = 0; i < TARGET_FPREGS; i++) {
6486 union {
bellard1bdb68e2006-06-21 18:48:01 +00006487 float32 f;
6488 uint32_t i;
bellard4fa5d772005-01-30 22:57:54 +00006489 } u;
bellard1bdb68e2006-06-21 18:48:01 +00006490 u.i = qemu_get_be32(f);
bellard4fa5d772005-01-30 22:57:54 +00006491 env->fpr[i] = u.f;
bellarde80cfcf2004-12-19 23:18:01 +00006492 }
bellard4fa5d772005-01-30 22:57:54 +00006493
6494 qemu_get_betls(f, &env->pc);
6495 qemu_get_betls(f, &env->npc);
6496 qemu_get_betls(f, &env->y);
6497 tmp = qemu_get_be32(f);
6498 env->cwp = 0; /* needed to ensure that the wrapping registers are
6499 correctly updated */
bellarde80cfcf2004-12-19 23:18:01 +00006500 PUT_PSR(env, tmp);
bellard34751872005-07-02 14:31:34 +00006501 qemu_get_betls(f, &env->fsr);
6502 qemu_get_betls(f, &env->tbr);
6503#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00006504 qemu_get_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00006505 /* MMU */
6506 for(i = 0; i < 16; i++)
6507 qemu_get_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00006508#endif
bellarde80cfcf2004-12-19 23:18:01 +00006509 tlb_flush(env, 1);
bellarde95c8d52004-09-30 22:22:08 +00006510 return 0;
6511}
bellardb5ff1b32005-11-26 10:38:39 +00006512
6513#elif defined(TARGET_ARM)
6514
bellardb5ff1b32005-11-26 10:38:39 +00006515void cpu_save(QEMUFile *f, void *opaque)
6516{
balrogaa941b92007-05-24 18:50:09 +00006517 int i;
6518 CPUARMState *env = (CPUARMState *)opaque;
6519
6520 for (i = 0; i < 16; i++) {
6521 qemu_put_be32(f, env->regs[i]);
6522 }
6523 qemu_put_be32(f, cpsr_read(env));
6524 qemu_put_be32(f, env->spsr);
6525 for (i = 0; i < 6; i++) {
6526 qemu_put_be32(f, env->banked_spsr[i]);
6527 qemu_put_be32(f, env->banked_r13[i]);
6528 qemu_put_be32(f, env->banked_r14[i]);
6529 }
6530 for (i = 0; i < 5; i++) {
6531 qemu_put_be32(f, env->usr_regs[i]);
6532 qemu_put_be32(f, env->fiq_regs[i]);
6533 }
6534 qemu_put_be32(f, env->cp15.c0_cpuid);
6535 qemu_put_be32(f, env->cp15.c0_cachetype);
6536 qemu_put_be32(f, env->cp15.c1_sys);
6537 qemu_put_be32(f, env->cp15.c1_coproc);
balrog610c3c82007-06-24 12:09:48 +00006538 qemu_put_be32(f, env->cp15.c1_xscaleauxcr);
pbrook9ee6e8b2007-11-11 00:04:49 +00006539 qemu_put_be32(f, env->cp15.c2_base0);
6540 qemu_put_be32(f, env->cp15.c2_base1);
6541 qemu_put_be32(f, env->cp15.c2_mask);
balrogaa941b92007-05-24 18:50:09 +00006542 qemu_put_be32(f, env->cp15.c2_data);
6543 qemu_put_be32(f, env->cp15.c2_insn);
6544 qemu_put_be32(f, env->cp15.c3);
6545 qemu_put_be32(f, env->cp15.c5_insn);
6546 qemu_put_be32(f, env->cp15.c5_data);
6547 for (i = 0; i < 8; i++) {
6548 qemu_put_be32(f, env->cp15.c6_region[i]);
6549 }
6550 qemu_put_be32(f, env->cp15.c6_insn);
6551 qemu_put_be32(f, env->cp15.c6_data);
6552 qemu_put_be32(f, env->cp15.c9_insn);
6553 qemu_put_be32(f, env->cp15.c9_data);
6554 qemu_put_be32(f, env->cp15.c13_fcse);
6555 qemu_put_be32(f, env->cp15.c13_context);
pbrook9ee6e8b2007-11-11 00:04:49 +00006556 qemu_put_be32(f, env->cp15.c13_tls1);
6557 qemu_put_be32(f, env->cp15.c13_tls2);
6558 qemu_put_be32(f, env->cp15.c13_tls3);
balrogaa941b92007-05-24 18:50:09 +00006559 qemu_put_be32(f, env->cp15.c15_cpar);
6560
6561 qemu_put_be32(f, env->features);
6562
6563 if (arm_feature(env, ARM_FEATURE_VFP)) {
6564 for (i = 0; i < 16; i++) {
6565 CPU_DoubleU u;
6566 u.d = env->vfp.regs[i];
6567 qemu_put_be32(f, u.l.upper);
6568 qemu_put_be32(f, u.l.lower);
6569 }
6570 for (i = 0; i < 16; i++) {
6571 qemu_put_be32(f, env->vfp.xregs[i]);
6572 }
6573
6574 /* TODO: Should use proper FPSCR access functions. */
6575 qemu_put_be32(f, env->vfp.vec_len);
6576 qemu_put_be32(f, env->vfp.vec_stride);
pbrook9ee6e8b2007-11-11 00:04:49 +00006577
6578 if (arm_feature(env, ARM_FEATURE_VFP3)) {
6579 for (i = 16; i < 32; i++) {
6580 CPU_DoubleU u;
6581 u.d = env->vfp.regs[i];
6582 qemu_put_be32(f, u.l.upper);
6583 qemu_put_be32(f, u.l.lower);
6584 }
6585 }
balrogaa941b92007-05-24 18:50:09 +00006586 }
6587
6588 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6589 for (i = 0; i < 16; i++) {
6590 qemu_put_be64(f, env->iwmmxt.regs[i]);
6591 }
6592 for (i = 0; i < 16; i++) {
6593 qemu_put_be32(f, env->iwmmxt.cregs[i]);
6594 }
6595 }
pbrook9ee6e8b2007-11-11 00:04:49 +00006596
6597 if (arm_feature(env, ARM_FEATURE_M)) {
6598 qemu_put_be32(f, env->v7m.other_sp);
6599 qemu_put_be32(f, env->v7m.vecbase);
6600 qemu_put_be32(f, env->v7m.basepri);
6601 qemu_put_be32(f, env->v7m.control);
6602 qemu_put_be32(f, env->v7m.current_sp);
6603 qemu_put_be32(f, env->v7m.exception);
6604 }
bellardb5ff1b32005-11-26 10:38:39 +00006605}
6606
6607int cpu_load(QEMUFile *f, void *opaque, int version_id)
6608{
balrogaa941b92007-05-24 18:50:09 +00006609 CPUARMState *env = (CPUARMState *)opaque;
6610 int i;
6611
pbrook9ee6e8b2007-11-11 00:04:49 +00006612 if (version_id != ARM_CPU_SAVE_VERSION)
balrogaa941b92007-05-24 18:50:09 +00006613 return -EINVAL;
6614
6615 for (i = 0; i < 16; i++) {
6616 env->regs[i] = qemu_get_be32(f);
6617 }
6618 cpsr_write(env, qemu_get_be32(f), 0xffffffff);
6619 env->spsr = qemu_get_be32(f);
6620 for (i = 0; i < 6; i++) {
6621 env->banked_spsr[i] = qemu_get_be32(f);
6622 env->banked_r13[i] = qemu_get_be32(f);
6623 env->banked_r14[i] = qemu_get_be32(f);
6624 }
6625 for (i = 0; i < 5; i++) {
6626 env->usr_regs[i] = qemu_get_be32(f);
6627 env->fiq_regs[i] = qemu_get_be32(f);
6628 }
6629 env->cp15.c0_cpuid = qemu_get_be32(f);
6630 env->cp15.c0_cachetype = qemu_get_be32(f);
6631 env->cp15.c1_sys = qemu_get_be32(f);
6632 env->cp15.c1_coproc = qemu_get_be32(f);
balrog610c3c82007-06-24 12:09:48 +00006633 env->cp15.c1_xscaleauxcr = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006634 env->cp15.c2_base0 = qemu_get_be32(f);
6635 env->cp15.c2_base1 = qemu_get_be32(f);
6636 env->cp15.c2_mask = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006637 env->cp15.c2_data = qemu_get_be32(f);
6638 env->cp15.c2_insn = qemu_get_be32(f);
6639 env->cp15.c3 = qemu_get_be32(f);
6640 env->cp15.c5_insn = qemu_get_be32(f);
6641 env->cp15.c5_data = qemu_get_be32(f);
6642 for (i = 0; i < 8; i++) {
6643 env->cp15.c6_region[i] = qemu_get_be32(f);
6644 }
6645 env->cp15.c6_insn = qemu_get_be32(f);
6646 env->cp15.c6_data = qemu_get_be32(f);
6647 env->cp15.c9_insn = qemu_get_be32(f);
6648 env->cp15.c9_data = qemu_get_be32(f);
6649 env->cp15.c13_fcse = qemu_get_be32(f);
6650 env->cp15.c13_context = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006651 env->cp15.c13_tls1 = qemu_get_be32(f);
6652 env->cp15.c13_tls2 = qemu_get_be32(f);
6653 env->cp15.c13_tls3 = qemu_get_be32(f);
balrogaa941b92007-05-24 18:50:09 +00006654 env->cp15.c15_cpar = qemu_get_be32(f);
6655
6656 env->features = qemu_get_be32(f);
6657
6658 if (arm_feature(env, ARM_FEATURE_VFP)) {
6659 for (i = 0; i < 16; i++) {
6660 CPU_DoubleU u;
6661 u.l.upper = qemu_get_be32(f);
6662 u.l.lower = qemu_get_be32(f);
6663 env->vfp.regs[i] = u.d;
6664 }
6665 for (i = 0; i < 16; i++) {
6666 env->vfp.xregs[i] = qemu_get_be32(f);
6667 }
6668
6669 /* TODO: Should use proper FPSCR access functions. */
6670 env->vfp.vec_len = qemu_get_be32(f);
6671 env->vfp.vec_stride = qemu_get_be32(f);
pbrook9ee6e8b2007-11-11 00:04:49 +00006672
6673 if (arm_feature(env, ARM_FEATURE_VFP3)) {
6674 for (i = 0; i < 16; i++) {
6675 CPU_DoubleU u;
6676 u.l.upper = qemu_get_be32(f);
6677 u.l.lower = qemu_get_be32(f);
6678 env->vfp.regs[i] = u.d;
6679 }
6680 }
balrogaa941b92007-05-24 18:50:09 +00006681 }
6682
6683 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6684 for (i = 0; i < 16; i++) {
6685 env->iwmmxt.regs[i] = qemu_get_be64(f);
6686 }
6687 for (i = 0; i < 16; i++) {
6688 env->iwmmxt.cregs[i] = qemu_get_be32(f);
6689 }
6690 }
6691
pbrook9ee6e8b2007-11-11 00:04:49 +00006692 if (arm_feature(env, ARM_FEATURE_M)) {
6693 env->v7m.other_sp = qemu_get_be32(f);
6694 env->v7m.vecbase = qemu_get_be32(f);
6695 env->v7m.basepri = qemu_get_be32(f);
6696 env->v7m.control = qemu_get_be32(f);
6697 env->v7m.current_sp = qemu_get_be32(f);
6698 env->v7m.exception = qemu_get_be32(f);
6699 }
6700
bellardb5ff1b32005-11-26 10:38:39 +00006701 return 0;
6702}
6703
bellard8a7ddc32004-03-31 19:00:16 +00006704#else
6705
bellard6dfd59d2007-11-11 20:14:28 +00006706//#warning No CPU save/restore functions
bellard8a7ddc32004-03-31 19:00:16 +00006707
6708#endif
6709
6710/***********************************************************/
6711/* ram save/restore */
6712
bellard8a7ddc32004-03-31 19:00:16 +00006713static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
6714{
6715 int v;
6716
6717 v = qemu_get_byte(f);
6718 switch(v) {
6719 case 0:
6720 if (qemu_get_buffer(f, buf, len) != len)
6721 return -EIO;
6722 break;
6723 case 1:
6724 v = qemu_get_byte(f);
6725 memset(buf, v, len);
6726 break;
6727 default:
6728 return -EINVAL;
6729 }
6730 return 0;
6731}
6732
bellardc88676f2006-08-06 13:36:11 +00006733static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00006734{
6735 int i, ret;
6736
bellard8a7ddc32004-03-31 19:00:16 +00006737 if (qemu_get_be32(f) != phys_ram_size)
6738 return -EINVAL;
6739 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
6740 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
6741 if (ret)
6742 return ret;
6743 }
6744 return 0;
6745}
6746
bellardc88676f2006-08-06 13:36:11 +00006747#define BDRV_HASH_BLOCK_SIZE 1024
6748#define IOBUF_SIZE 4096
6749#define RAM_CBLOCK_MAGIC 0xfabe
6750
6751typedef struct RamCompressState {
6752 z_stream zstream;
6753 QEMUFile *f;
6754 uint8_t buf[IOBUF_SIZE];
6755} RamCompressState;
6756
6757static int ram_compress_open(RamCompressState *s, QEMUFile *f)
6758{
6759 int ret;
6760 memset(s, 0, sizeof(*s));
6761 s->f = f;
6762 ret = deflateInit2(&s->zstream, 1,
ths5fafdf22007-09-16 21:08:06 +00006763 Z_DEFLATED, 15,
bellardc88676f2006-08-06 13:36:11 +00006764 9, Z_DEFAULT_STRATEGY);
6765 if (ret != Z_OK)
6766 return -1;
6767 s->zstream.avail_out = IOBUF_SIZE;
6768 s->zstream.next_out = s->buf;
6769 return 0;
6770}
6771
6772static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len)
6773{
6774 qemu_put_be16(s->f, RAM_CBLOCK_MAGIC);
6775 qemu_put_be16(s->f, len);
6776 qemu_put_buffer(s->f, buf, len);
6777}
6778
6779static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len)
6780{
6781 int ret;
6782
6783 s->zstream.avail_in = len;
6784 s->zstream.next_in = (uint8_t *)buf;
6785 while (s->zstream.avail_in > 0) {
6786 ret = deflate(&s->zstream, Z_NO_FLUSH);
6787 if (ret != Z_OK)
6788 return -1;
6789 if (s->zstream.avail_out == 0) {
6790 ram_put_cblock(s, s->buf, IOBUF_SIZE);
6791 s->zstream.avail_out = IOBUF_SIZE;
6792 s->zstream.next_out = s->buf;
6793 }
6794 }
6795 return 0;
6796}
6797
6798static void ram_compress_close(RamCompressState *s)
6799{
6800 int len, ret;
6801
6802 /* compress last bytes */
6803 for(;;) {
6804 ret = deflate(&s->zstream, Z_FINISH);
6805 if (ret == Z_OK || ret == Z_STREAM_END) {
6806 len = IOBUF_SIZE - s->zstream.avail_out;
6807 if (len > 0) {
6808 ram_put_cblock(s, s->buf, len);
6809 }
6810 s->zstream.avail_out = IOBUF_SIZE;
6811 s->zstream.next_out = s->buf;
6812 if (ret == Z_STREAM_END)
6813 break;
6814 } else {
6815 goto fail;
6816 }
6817 }
6818fail:
6819 deflateEnd(&s->zstream);
6820}
6821
6822typedef struct RamDecompressState {
6823 z_stream zstream;
6824 QEMUFile *f;
6825 uint8_t buf[IOBUF_SIZE];
6826} RamDecompressState;
6827
6828static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
6829{
6830 int ret;
6831 memset(s, 0, sizeof(*s));
6832 s->f = f;
6833 ret = inflateInit(&s->zstream);
6834 if (ret != Z_OK)
6835 return -1;
6836 return 0;
6837}
6838
6839static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
6840{
6841 int ret, clen;
6842
6843 s->zstream.avail_out = len;
6844 s->zstream.next_out = buf;
6845 while (s->zstream.avail_out > 0) {
6846 if (s->zstream.avail_in == 0) {
6847 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
6848 return -1;
6849 clen = qemu_get_be16(s->f);
6850 if (clen > IOBUF_SIZE)
6851 return -1;
6852 qemu_get_buffer(s->f, s->buf, clen);
6853 s->zstream.avail_in = clen;
6854 s->zstream.next_in = s->buf;
6855 }
6856 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
6857 if (ret != Z_OK && ret != Z_STREAM_END) {
6858 return -1;
6859 }
6860 }
6861 return 0;
6862}
6863
6864static void ram_decompress_close(RamDecompressState *s)
6865{
6866 inflateEnd(&s->zstream);
6867}
6868
6869static void ram_save(QEMUFile *f, void *opaque)
6870{
6871 int i;
6872 RamCompressState s1, *s = &s1;
6873 uint8_t buf[10];
ths3b46e622007-09-17 08:09:54 +00006874
bellardc88676f2006-08-06 13:36:11 +00006875 qemu_put_be32(f, phys_ram_size);
6876 if (ram_compress_open(s, f) < 0)
6877 return;
6878 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6879#if 0
6880 if (tight_savevm_enabled) {
6881 int64_t sector_num;
6882 int j;
6883
6884 /* find if the memory block is available on a virtual
6885 block device */
6886 sector_num = -1;
thse4bcb142007-12-02 04:51:10 +00006887 for(j = 0; j < nb_drives; j++) {
6888 sector_num = bdrv_hash_find(drives_table[j].bdrv,
6889 phys_ram_base + i,
6890 BDRV_HASH_BLOCK_SIZE);
6891 if (sector_num >= 0)
6892 break;
bellardc88676f2006-08-06 13:36:11 +00006893 }
thse4bcb142007-12-02 04:51:10 +00006894 if (j == nb_drives)
bellardc88676f2006-08-06 13:36:11 +00006895 goto normal_compress;
6896 buf[0] = 1;
6897 buf[1] = j;
6898 cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
6899 ram_compress_buf(s, buf, 10);
ths5fafdf22007-09-16 21:08:06 +00006900 } else
bellardc88676f2006-08-06 13:36:11 +00006901#endif
6902 {
6903 // normal_compress:
6904 buf[0] = 0;
6905 ram_compress_buf(s, buf, 1);
6906 ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6907 }
6908 }
6909 ram_compress_close(s);
6910}
6911
6912static int ram_load(QEMUFile *f, void *opaque, int version_id)
6913{
6914 RamDecompressState s1, *s = &s1;
6915 uint8_t buf[10];
6916 int i;
6917
6918 if (version_id == 1)
6919 return ram_load_v1(f, opaque);
6920 if (version_id != 2)
6921 return -EINVAL;
6922 if (qemu_get_be32(f) != phys_ram_size)
6923 return -EINVAL;
6924 if (ram_decompress_open(s, f) < 0)
6925 return -EINVAL;
6926 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6927 if (ram_decompress_buf(s, buf, 1) < 0) {
6928 fprintf(stderr, "Error while reading ram block header\n");
6929 goto error;
6930 }
6931 if (buf[0] == 0) {
6932 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
6933 fprintf(stderr, "Error while reading ram block address=0x%08x", i);
6934 goto error;
6935 }
ths5fafdf22007-09-16 21:08:06 +00006936 } else
bellardc88676f2006-08-06 13:36:11 +00006937#if 0
6938 if (buf[0] == 1) {
6939 int bs_index;
6940 int64_t sector_num;
6941
6942 ram_decompress_buf(s, buf + 1, 9);
6943 bs_index = buf[1];
6944 sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
thse4bcb142007-12-02 04:51:10 +00006945 if (bs_index >= nb_drives) {
bellardc88676f2006-08-06 13:36:11 +00006946 fprintf(stderr, "Invalid block device index %d\n", bs_index);
6947 goto error;
6948 }
thse4bcb142007-12-02 04:51:10 +00006949 if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
6950 phys_ram_base + i,
bellardc88676f2006-08-06 13:36:11 +00006951 BDRV_HASH_BLOCK_SIZE / 512) < 0) {
ths5fafdf22007-09-16 21:08:06 +00006952 fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
bellardc88676f2006-08-06 13:36:11 +00006953 bs_index, sector_num);
6954 goto error;
6955 }
ths5fafdf22007-09-16 21:08:06 +00006956 } else
bellardc88676f2006-08-06 13:36:11 +00006957#endif
6958 {
6959 error:
6960 printf("Error block header\n");
6961 return -EINVAL;
6962 }
6963 }
6964 ram_decompress_close(s);
6965 return 0;
6966}
6967
bellard8a7ddc32004-03-31 19:00:16 +00006968/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00006969/* bottom halves (can be seen as timers which expire ASAP) */
6970
6971struct QEMUBH {
6972 QEMUBHFunc *cb;
6973 void *opaque;
6974 int scheduled;
6975 QEMUBH *next;
6976};
6977
6978static QEMUBH *first_bh = NULL;
6979
6980QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
6981{
6982 QEMUBH *bh;
6983 bh = qemu_mallocz(sizeof(QEMUBH));
6984 if (!bh)
6985 return NULL;
6986 bh->cb = cb;
6987 bh->opaque = opaque;
6988 return bh;
6989}
6990
bellard6eb57332006-08-06 09:51:25 +00006991int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00006992{
6993 QEMUBH *bh, **pbh;
bellard6eb57332006-08-06 09:51:25 +00006994 int ret;
bellard83f64092006-08-01 16:21:11 +00006995
bellard6eb57332006-08-06 09:51:25 +00006996 ret = 0;
bellard83f64092006-08-01 16:21:11 +00006997 for(;;) {
6998 pbh = &first_bh;
6999 bh = *pbh;
7000 if (!bh)
7001 break;
bellard6eb57332006-08-06 09:51:25 +00007002 ret = 1;
bellard83f64092006-08-01 16:21:11 +00007003 *pbh = bh->next;
7004 bh->scheduled = 0;
7005 bh->cb(bh->opaque);
7006 }
bellard6eb57332006-08-06 09:51:25 +00007007 return ret;
bellard83f64092006-08-01 16:21:11 +00007008}
7009
7010void qemu_bh_schedule(QEMUBH *bh)
7011{
7012 CPUState *env = cpu_single_env;
7013 if (bh->scheduled)
7014 return;
7015 bh->scheduled = 1;
7016 bh->next = first_bh;
7017 first_bh = bh;
7018
7019 /* stop the currently executing CPU to execute the BH ASAP */
7020 if (env) {
7021 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
7022 }
7023}
7024
7025void qemu_bh_cancel(QEMUBH *bh)
7026{
7027 QEMUBH **pbh;
7028 if (bh->scheduled) {
7029 pbh = &first_bh;
7030 while (*pbh != bh)
7031 pbh = &(*pbh)->next;
7032 *pbh = bh->next;
7033 bh->scheduled = 0;
7034 }
7035}
7036
7037void qemu_bh_delete(QEMUBH *bh)
7038{
7039 qemu_bh_cancel(bh);
7040 qemu_free(bh);
7041}
7042
7043/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00007044/* machine registration */
7045
7046QEMUMachine *first_machine = NULL;
7047
7048int qemu_register_machine(QEMUMachine *m)
7049{
7050 QEMUMachine **pm;
7051 pm = &first_machine;
7052 while (*pm != NULL)
7053 pm = &(*pm)->next;
7054 m->next = NULL;
7055 *pm = m;
7056 return 0;
7057}
7058
pbrook9596ebb2007-11-18 01:44:38 +00007059static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00007060{
7061 QEMUMachine *m;
7062
7063 for(m = first_machine; m != NULL; m = m->next) {
7064 if (!strcmp(m->name, name))
7065 return m;
7066 }
7067 return NULL;
7068}
7069
7070/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00007071/* main execution loop */
7072
pbrook9596ebb2007-11-18 01:44:38 +00007073static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00007074{
ths740733b2007-06-08 01:57:56 +00007075 DisplayState *ds = opaque;
7076 ds->dpy_refresh(ds);
7077 qemu_mod_timer(ds->gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00007078}
7079
bellard0bd48852005-11-11 00:00:47 +00007080struct vm_change_state_entry {
7081 VMChangeStateHandler *cb;
7082 void *opaque;
7083 LIST_ENTRY (vm_change_state_entry) entries;
7084};
7085
7086static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
7087
7088VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
7089 void *opaque)
7090{
7091 VMChangeStateEntry *e;
7092
7093 e = qemu_mallocz(sizeof (*e));
7094 if (!e)
7095 return NULL;
7096
7097 e->cb = cb;
7098 e->opaque = opaque;
7099 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
7100 return e;
7101}
7102
7103void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
7104{
7105 LIST_REMOVE (e, entries);
7106 qemu_free (e);
7107}
7108
7109static void vm_state_notify(int running)
7110{
7111 VMChangeStateEntry *e;
7112
7113 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
7114 e->cb(e->opaque, running);
7115 }
7116}
7117
bellard8a7ddc32004-03-31 19:00:16 +00007118/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00007119static VMStopHandler *vm_stop_cb;
7120static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00007121
7122int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
7123{
7124 vm_stop_cb = cb;
7125 vm_stop_opaque = opaque;
7126 return 0;
7127}
7128
7129void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
7130{
7131 vm_stop_cb = NULL;
7132}
7133
7134void vm_start(void)
7135{
7136 if (!vm_running) {
7137 cpu_enable_ticks();
7138 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00007139 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00007140 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00007141 }
7142}
7143
ths5fafdf22007-09-16 21:08:06 +00007144void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00007145{
7146 if (vm_running) {
7147 cpu_disable_ticks();
7148 vm_running = 0;
7149 if (reason != 0) {
7150 if (vm_stop_cb) {
7151 vm_stop_cb(vm_stop_opaque, reason);
7152 }
7153 }
bellard0bd48852005-11-11 00:00:47 +00007154 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00007155 }
7156}
7157
bellardbb0c6722004-06-20 12:37:32 +00007158/* reset/shutdown handler */
7159
7160typedef struct QEMUResetEntry {
7161 QEMUResetHandler *func;
7162 void *opaque;
7163 struct QEMUResetEntry *next;
7164} QEMUResetEntry;
7165
7166static QEMUResetEntry *first_reset_entry;
7167static int reset_requested;
7168static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00007169static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00007170
7171void qemu_register_reset(QEMUResetHandler *func, void *opaque)
7172{
7173 QEMUResetEntry **pre, *re;
7174
7175 pre = &first_reset_entry;
7176 while (*pre != NULL)
7177 pre = &(*pre)->next;
7178 re = qemu_mallocz(sizeof(QEMUResetEntry));
7179 re->func = func;
7180 re->opaque = opaque;
7181 re->next = NULL;
7182 *pre = re;
7183}
7184
ths52f61fd2006-12-22 21:20:52 +00007185static void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00007186{
7187 QEMUResetEntry *re;
7188
7189 /* reset all devices */
7190 for(re = first_reset_entry; re != NULL; re = re->next) {
7191 re->func(re->opaque);
7192 }
7193}
7194
7195void qemu_system_reset_request(void)
7196{
bellardd1beab82006-10-02 19:44:22 +00007197 if (no_reboot) {
7198 shutdown_requested = 1;
7199 } else {
7200 reset_requested = 1;
7201 }
bellard6a00d602005-11-21 23:25:50 +00007202 if (cpu_single_env)
7203 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00007204}
7205
7206void qemu_system_shutdown_request(void)
7207{
7208 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00007209 if (cpu_single_env)
7210 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00007211}
7212
bellard34751872005-07-02 14:31:34 +00007213void qemu_system_powerdown_request(void)
7214{
7215 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00007216 if (cpu_single_env)
7217 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00007218}
7219
bellard5905b2e2004-08-01 21:53:26 +00007220void main_loop_wait(int timeout)
bellard8a7ddc32004-03-31 19:00:16 +00007221{
thscafffd42007-02-28 21:59:44 +00007222 IOHandlerRecord *ioh;
bellarde0356492006-05-01 13:33:02 +00007223 fd_set rfds, wfds, xfds;
ths877cf882007-04-18 18:11:47 +00007224 int ret, nfds;
7225#ifdef _WIN32
7226 int ret2, i;
7227#endif
bellardfd1dff42006-02-01 21:29:26 +00007228 struct timeval tv;
bellardf3311102006-04-12 20:21:17 +00007229 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00007230
bellardf3311102006-04-12 20:21:17 +00007231
7232 /* XXX: need to suppress polling by better using win32 events */
7233 ret = 0;
7234 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
7235 ret |= pe->func(pe->opaque);
7236 }
bellard38e205a2004-04-06 19:29:17 +00007237#ifdef _WIN32
thse6b1e552007-04-18 17:56:02 +00007238 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00007239 int err;
7240 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00007241
bellarda18e5242006-06-25 17:18:27 +00007242 ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
7243 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
7244 if (w->func[ret - WAIT_OBJECT_0])
7245 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00007246
ths5fafdf22007-09-16 21:08:06 +00007247 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00007248 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00007249
thse6b1e552007-04-18 17:56:02 +00007250 /* Check if event is signaled */
7251 ret2 = WaitForSingleObject(w->events[i], 0);
7252 if(ret2 == WAIT_OBJECT_0) {
7253 if (w->func[i])
7254 w->func[i](w->opaque[i]);
7255 } else if (ret2 == WAIT_TIMEOUT) {
7256 } else {
7257 err = GetLastError();
7258 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00007259 }
7260 }
bellarda18e5242006-06-25 17:18:27 +00007261 } else if (ret == WAIT_TIMEOUT) {
7262 } else {
7263 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00007264 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00007265 }
bellardf3311102006-04-12 20:21:17 +00007266 }
bellardfd1dff42006-02-01 21:29:26 +00007267#endif
7268 /* poll any events */
7269 /* XXX: separate device handlers from system ones */
7270 nfds = -1;
7271 FD_ZERO(&rfds);
7272 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00007273 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00007274 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00007275 if (ioh->deleted)
7276 continue;
bellardfd1dff42006-02-01 21:29:26 +00007277 if (ioh->fd_read &&
7278 (!ioh->fd_read_poll ||
7279 ioh->fd_read_poll(ioh->opaque) != 0)) {
7280 FD_SET(ioh->fd, &rfds);
7281 if (ioh->fd > nfds)
7282 nfds = ioh->fd;
7283 }
7284 if (ioh->fd_write) {
7285 FD_SET(ioh->fd, &wfds);
7286 if (ioh->fd > nfds)
7287 nfds = ioh->fd;
7288 }
7289 }
ths3b46e622007-09-17 08:09:54 +00007290
bellardfd1dff42006-02-01 21:29:26 +00007291 tv.tv_sec = 0;
7292#ifdef _WIN32
7293 tv.tv_usec = 0;
bellard38e205a2004-04-06 19:29:17 +00007294#else
bellardfd1dff42006-02-01 21:29:26 +00007295 tv.tv_usec = timeout * 1000;
7296#endif
bellarde0356492006-05-01 13:33:02 +00007297#if defined(CONFIG_SLIRP)
7298 if (slirp_inited) {
7299 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
7300 }
7301#endif
7302 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00007303 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00007304 IOHandlerRecord **pioh;
7305
7306 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00007307 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00007308 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00007309 }
ths6ab43fd2007-08-25 01:34:19 +00007310 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00007311 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00007312 }
7313 }
thscafffd42007-02-28 21:59:44 +00007314
7315 /* remove deleted IO handlers */
7316 pioh = &first_io_handler;
7317 while (*pioh) {
7318 ioh = *pioh;
7319 if (ioh->deleted) {
7320 *pioh = ioh->next;
7321 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00007322 } else
thscafffd42007-02-28 21:59:44 +00007323 pioh = &ioh->next;
7324 }
bellardfd1dff42006-02-01 21:29:26 +00007325 }
bellarde0356492006-05-01 13:33:02 +00007326#if defined(CONFIG_SLIRP)
7327 if (slirp_inited) {
7328 if (ret < 0) {
7329 FD_ZERO(&rfds);
7330 FD_ZERO(&wfds);
7331 FD_ZERO(&xfds);
7332 }
7333 slirp_select_poll(&rfds, &wfds, &xfds);
7334 }
7335#endif
bellard83f64092006-08-01 16:21:11 +00007336 qemu_aio_poll();
bellardc20709a2004-04-21 23:27:19 +00007337
bellardfd1dff42006-02-01 21:29:26 +00007338 if (vm_running) {
ths5fafdf22007-09-16 21:08:06 +00007339 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
bellardfd1dff42006-02-01 21:29:26 +00007340 qemu_get_clock(vm_clock));
7341 /* run dma transfers, if any */
7342 DMA_run();
7343 }
pbrook423f0742007-05-23 00:06:54 +00007344
bellardfd1dff42006-02-01 21:29:26 +00007345 /* real time timers */
ths5fafdf22007-09-16 21:08:06 +00007346 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
bellardfd1dff42006-02-01 21:29:26 +00007347 qemu_get_clock(rt_clock));
pbrook423f0742007-05-23 00:06:54 +00007348
7349 /* Check bottom-halves last in case any of the earlier events triggered
7350 them. */
7351 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00007352
bellard5905b2e2004-08-01 21:53:26 +00007353}
7354
pbrook9596ebb2007-11-18 01:44:38 +00007355static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00007356{
7357 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00007358#ifdef CONFIG_PROFILER
7359 int64_t ti;
7360#endif
bellard6a00d602005-11-21 23:25:50 +00007361 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00007362
bellard6a00d602005-11-21 23:25:50 +00007363 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00007364 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00007365 for(;;) {
7366 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00007367
bellard15a76442005-11-23 21:01:03 +00007368 for(;;) {
7369 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00007370 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00007371#ifdef CONFIG_PROFILER
7372 ti = profile_getclock();
7373#endif
bellard6a00d602005-11-21 23:25:50 +00007374 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00007375#ifdef CONFIG_PROFILER
7376 qemu_time += profile_getclock() - ti;
7377#endif
balrogee5605e2007-12-03 03:01:40 +00007378 next_cpu = env->next_cpu ?: first_cpu;
7379 if (event_pending) {
7380 ret = EXCP_INTERRUPT;
7381 event_pending = 0;
7382 break;
7383 }
pbrookbd967e02007-03-11 18:54:57 +00007384 if (ret == EXCP_HLT) {
7385 /* Give the next CPU a chance to run. */
7386 cur_cpu = env;
7387 continue;
7388 }
bellard15a76442005-11-23 21:01:03 +00007389 if (ret != EXCP_HALTED)
7390 break;
7391 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00007392 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00007393 break;
bellard15a76442005-11-23 21:01:03 +00007394 }
7395 cur_cpu = env;
7396
bellard5905b2e2004-08-01 21:53:26 +00007397 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00007398 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00007399 break;
7400 }
7401 if (reset_requested) {
7402 reset_requested = 0;
7403 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00007404 ret = EXCP_INTERRUPT;
7405 }
7406 if (powerdown_requested) {
7407 powerdown_requested = 0;
7408 qemu_system_powerdown();
7409 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00007410 }
7411 if (ret == EXCP_DEBUG) {
7412 vm_stop(EXCP_DEBUG);
7413 }
pbrookbd967e02007-03-11 18:54:57 +00007414 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00007415 /* XXX: use timeout computed from timers */
pbrookbd967e02007-03-11 18:54:57 +00007416 if (ret == EXCP_HALTED)
bellard5905b2e2004-08-01 21:53:26 +00007417 timeout = 10;
7418 else
7419 timeout = 0;
7420 } else {
7421 timeout = 10;
7422 }
bellard89bfc102006-02-08 22:46:31 +00007423#ifdef CONFIG_PROFILER
7424 ti = profile_getclock();
7425#endif
bellard5905b2e2004-08-01 21:53:26 +00007426 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00007427#ifdef CONFIG_PROFILER
7428 dev_time += profile_getclock() - ti;
7429#endif
bellardb4608c02003-06-27 17:34:32 +00007430 }
bellard34865132003-10-05 14:28:56 +00007431 cpu_disable_ticks();
7432 return ret;
bellardb4608c02003-06-27 17:34:32 +00007433}
7434
ths15f82202007-06-29 23:26:08 +00007435static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00007436{
bellard84f2e8e2007-02-05 20:21:32 +00007437 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00007438 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00007439 "\n"
bellarda20dd502003-09-30 21:07:02 +00007440 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00007441 "\n"
bellarda20dd502003-09-30 21:07:02 +00007442 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00007443 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00007444 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00007445 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00007446 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
7447 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007448 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
thse4bcb142007-12-02 04:51:10 +00007449 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][index=i]\n"
7450 " [,cyls=c,heads=h,secs=s[,trans=t]][snapshot=on|off]\n"
7451 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00007452 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00007453 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00007454 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00007455 "-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 +00007456 "-snapshot write to temporary files instead of disk image files\n"
7457#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007458 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00007459 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00007460 "-no-quit disable SDL window close capability\n"
7461#endif
bellard52ca8d62006-06-14 16:03:05 +00007462#ifdef TARGET_I386
7463 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
7464#endif
bellarda00bad72004-05-22 21:39:06 +00007465 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00007466 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007467 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00007468 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00007469#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00007470 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00007471#endif
bellard1d14ffa2005-10-30 18:58:22 +00007472#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007473 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00007474 "-soundhw c1,... enable audio support\n"
7475 " and only specified sound cards (comma separated list)\n"
7476 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00007477 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00007478#endif
bellard89980282004-06-03 14:04:03 +00007479 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00007480 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00007481#ifdef TARGET_I386
7482 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
7483#endif
bellardb389dbf2005-11-06 16:49:55 +00007484 "-usb enable the USB driver (will be the default soon)\n"
7485 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00007486#if defined(TARGET_PPC) || defined(TARGET_SPARC)
7487 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00007488#endif
thsc35734b2007-03-19 15:17:08 +00007489 "-name string set the name of the guest\n"
bellarda20dd502003-09-30 21:07:02 +00007490 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00007491 "Network options:\n"
pbrooka41b2ff2006-02-05 04:14:41 +00007492 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007493 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00007494#ifdef CONFIG_SLIRP
pbrook115defd2006-04-16 11:06:58 +00007495 "-net user[,vlan=n][,hostname=host]\n"
7496 " connect the user mode network stack to VLAN 'n' and send\n"
7497 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00007498#endif
bellard7fb843f2006-02-01 23:06:55 +00007499#ifdef _WIN32
7500 "-net tap[,vlan=n],ifname=name\n"
7501 " connect the host TAP network interface to VLAN 'n'\n"
7502#else
thsb46a8902007-10-21 23:20:45 +00007503 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7504 " connect the host TAP network interface to VLAN 'n' and use the\n"
7505 " network scripts 'file' (default=%s)\n"
7506 " and 'dfile' (default=%s);\n"
7507 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00007508 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00007509#endif
bellard6a00d602005-11-21 23:25:50 +00007510 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00007511 " connect the vlan 'n' to another VLAN using a socket connection\n"
bellard3d830452005-12-18 16:36:49 +00007512 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7513 " connect the vlan 'n' to multicast maddr and port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007514 "-net none use it alone to have zero network devices; if no -net option\n"
7515 " is provided, the default is '-net nic -net user'\n"
7516 "\n"
7517#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00007518 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00007519 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00007520#ifndef _WIN32
7521 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00007522#endif
bellard9bf05442004-08-25 22:12:49 +00007523 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00007524 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00007525#endif
bellardc4b1fcc2004-03-14 21:44:30 +00007526 "\n"
7527 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00007528 "-kernel bzImage use 'bzImage' as kernel image\n"
7529 "-append cmdline use 'cmdline' as kernel command line\n"
7530 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00007531 "\n"
bellard330d0412003-07-26 18:11:40 +00007532 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00007533 "-monitor dev redirect the monitor to char device 'dev'\n"
7534 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00007535 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00007536 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00007537 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00007538 "-s wait gdb connection to port\n"
7539 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00007540 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00007541 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
7542 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00007543 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00007544#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00007545 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00007546 "-no-kqemu disable KQEMU kernel module usage\n"
7547#endif
bellardbb0c6722004-06-20 12:37:32 +00007548#ifdef TARGET_I386
bellard1bfe8562004-07-08 21:17:50 +00007549 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
7550 " (default is CL-GD5446 PCI VGA)\n"
bellard6515b202006-05-03 22:02:44 +00007551 "-no-acpi disable ACPI\n"
bellardbb0c6722004-06-20 12:37:32 +00007552#endif
bellardd1beab82006-10-02 19:44:22 +00007553 "-no-reboot exit instead of rebooting\n"
bellardd63d3072004-10-03 13:29:03 +00007554 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00007555 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00007556#ifndef _WIN32
7557 "-daemonize daemonize QEMU after initializing\n"
7558#endif
ths9ae02552007-01-05 17:39:04 +00007559 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00007560#ifdef TARGET_SPARC
7561 "-prom-env variable=value set OpenBIOS nvram variables\n"
7562#endif
thsf3dcfad2007-08-24 01:26:02 +00007563 "-clock force the use of the given methods for timer alarm.\n"
7564 " To see what timers are available use -clock help\n"
bellard0824d6f2003-06-24 13:42:40 +00007565 "\n"
bellard82c643f2004-07-14 17:28:13 +00007566 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00007567 "ctrl-alt-f toggle full screen\n"
7568 "ctrl-alt-n switch to virtual console 'n'\n"
7569 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00007570 "\n"
7571 "When using -nographic, press 'ctrl-a h' to get some help.\n"
7572 ,
bellard0db63472003-10-27 21:37:46 +00007573 "qemu",
bellarda00bad72004-05-22 21:39:06 +00007574 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00007575#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00007576 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00007577 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00007578#endif
bellard6e44ba72004-01-18 21:56:49 +00007579 DEFAULT_GDBSTUB_PORT,
7580 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00007581 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00007582}
7583
bellardcd6f1162004-05-13 22:02:20 +00007584#define HAS_ARG 0x0001
7585
7586enum {
7587 QEMU_OPTION_h,
7588
bellardcc1daa42005-06-05 14:49:17 +00007589 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00007590 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00007591 QEMU_OPTION_fda,
7592 QEMU_OPTION_fdb,
7593 QEMU_OPTION_hda,
7594 QEMU_OPTION_hdb,
7595 QEMU_OPTION_hdc,
7596 QEMU_OPTION_hdd,
thse4bcb142007-12-02 04:51:10 +00007597 QEMU_OPTION_drive,
bellardcd6f1162004-05-13 22:02:20 +00007598 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00007599 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00007600 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00007601 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00007602 QEMU_OPTION_boot,
7603 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00007604#ifdef TARGET_I386
7605 QEMU_OPTION_no_fd_bootchk,
7606#endif
bellardcd6f1162004-05-13 22:02:20 +00007607 QEMU_OPTION_m,
7608 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00007609 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00007610#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007611 QEMU_OPTION_audio_help,
7612 QEMU_OPTION_soundhw,
7613#endif
bellardcd6f1162004-05-13 22:02:20 +00007614
bellard7c9d8e02005-11-15 22:16:05 +00007615 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00007616 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00007617 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00007618 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00007619 QEMU_OPTION_redir,
bellardcd6f1162004-05-13 22:02:20 +00007620
7621 QEMU_OPTION_kernel,
7622 QEMU_OPTION_append,
7623 QEMU_OPTION_initrd,
7624
7625 QEMU_OPTION_S,
7626 QEMU_OPTION_s,
7627 QEMU_OPTION_p,
7628 QEMU_OPTION_d,
7629 QEMU_OPTION_hdachs,
7630 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00007631 QEMU_OPTION_bios,
bellardcd6f1162004-05-13 22:02:20 +00007632 QEMU_OPTION_no_code_copy,
bellard3d11d0e2004-12-12 16:56:30 +00007633 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00007634 QEMU_OPTION_localtime,
bellard1f042752004-06-05 13:46:47 +00007635 QEMU_OPTION_cirrusvga,
thsd34cab92007-04-02 01:10:46 +00007636 QEMU_OPTION_vmsvga,
bellarde9b137c2004-06-21 16:46:10 +00007637 QEMU_OPTION_g,
bellard1bfe8562004-07-08 21:17:50 +00007638 QEMU_OPTION_std_vga,
ths20d8a3e2007-02-18 17:04:49 +00007639 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00007640 QEMU_OPTION_monitor,
7641 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00007642 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00007643 QEMU_OPTION_loadvm,
7644 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00007645 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00007646 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00007647 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00007648 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00007649 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00007650 QEMU_OPTION_kernel_kqemu,
bellarda09db212005-04-30 16:10:35 +00007651 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00007652 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00007653 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00007654 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00007655 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00007656 QEMU_OPTION_no_acpi,
bellardd1beab82006-10-02 19:44:22 +00007657 QEMU_OPTION_no_reboot,
balrog9467cd42007-05-01 01:34:14 +00007658 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00007659 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00007660 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00007661 QEMU_OPTION_semihosting,
7662 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00007663 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00007664 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00007665 QEMU_OPTION_clock,
bellard7e0af5d02007-11-07 16:24:33 +00007666 QEMU_OPTION_startdate,
bellardcd6f1162004-05-13 22:02:20 +00007667};
7668
7669typedef struct QEMUOption {
7670 const char *name;
7671 int flags;
7672 int index;
7673} QEMUOption;
7674
7675const QEMUOption qemu_options[] = {
7676 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00007677 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00007678
bellardcc1daa42005-06-05 14:49:17 +00007679 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00007680 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00007681 { "fda", HAS_ARG, QEMU_OPTION_fda },
7682 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
7683 { "hda", HAS_ARG, QEMU_OPTION_hda },
7684 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
7685 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
7686 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
thse4bcb142007-12-02 04:51:10 +00007687 { "drive", HAS_ARG, QEMU_OPTION_drive },
bellardcd6f1162004-05-13 22:02:20 +00007688 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00007689 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00007690 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00007691 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00007692 { "boot", HAS_ARG, QEMU_OPTION_boot },
7693 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00007694#ifdef TARGET_I386
7695 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
7696#endif
bellardcd6f1162004-05-13 22:02:20 +00007697 { "m", HAS_ARG, QEMU_OPTION_m },
7698 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00007699 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00007700 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00007701#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00007702 { "audio-help", 0, QEMU_OPTION_audio_help },
7703 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
7704#endif
bellardcd6f1162004-05-13 22:02:20 +00007705
bellard7c9d8e02005-11-15 22:16:05 +00007706 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00007707#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00007708 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00007709 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00007710#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00007711 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00007712#endif
bellard9bf05442004-08-25 22:12:49 +00007713 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00007714#endif
bellardcd6f1162004-05-13 22:02:20 +00007715
7716 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
7717 { "append", HAS_ARG, QEMU_OPTION_append },
7718 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
7719
7720 { "S", 0, QEMU_OPTION_S },
7721 { "s", 0, QEMU_OPTION_s },
7722 { "p", HAS_ARG, QEMU_OPTION_p },
7723 { "d", HAS_ARG, QEMU_OPTION_d },
7724 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
7725 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00007726 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardcd6f1162004-05-13 22:02:20 +00007727 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
bellardd993e022005-02-10 22:00:06 +00007728#ifdef USE_KQEMU
7729 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00007730 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00007731#endif
bellard6f7e9ae2005-03-13 09:43:36 +00007732#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00007733 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00007734#endif
bellardee22c2f2004-06-03 12:49:50 +00007735 { "localtime", 0, QEMU_OPTION_localtime },
bellard1bfe8562004-07-08 21:17:50 +00007736 { "std-vga", 0, QEMU_OPTION_std_vga },
balrog8b6e0722007-06-22 08:23:44 +00007737 { "echr", HAS_ARG, QEMU_OPTION_echr },
7738 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
7739 { "serial", HAS_ARG, QEMU_OPTION_serial },
7740 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00007741 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
7742 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00007743#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00007744 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00007745 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00007746 { "no-quit", 0, QEMU_OPTION_no_quit },
7747#endif
bellardf7cce892004-12-08 22:21:25 +00007748 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00007749 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00007750 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00007751 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00007752 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
ths96d30e42007-01-07 20:42:14 +00007753
bellard1f042752004-06-05 13:46:47 +00007754 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00007755 { "usb", 0, QEMU_OPTION_usb },
bellard1f042752004-06-05 13:46:47 +00007756 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
thsd34cab92007-04-02 01:10:46 +00007757 { "vmwarevga", 0, QEMU_OPTION_vmsvga },
bellard6515b202006-05-03 22:02:44 +00007758 { "no-acpi", 0, QEMU_OPTION_no_acpi },
bellardd1beab82006-10-02 19:44:22 +00007759 { "no-reboot", 0, QEMU_OPTION_no_reboot },
balrog9467cd42007-05-01 01:34:14 +00007760 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00007761 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00007762 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00007763#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00007764 { "semihosting", 0, QEMU_OPTION_semihosting },
7765#endif
thsc35734b2007-03-19 15:17:08 +00007766 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir166508602007-05-01 14:16:52 +00007767#if defined(TARGET_SPARC)
7768 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
7769#endif
balrog2b8f2d42007-07-27 22:08:46 +00007770#if defined(TARGET_ARM)
7771 { "old-param", 0, QEMU_OPTION_old_param },
7772#endif
thsf3dcfad2007-08-24 01:26:02 +00007773 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellard7e0af5d02007-11-07 16:24:33 +00007774 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
bellardcd6f1162004-05-13 22:02:20 +00007775 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00007776};
7777
bellard5905b2e2004-08-01 21:53:26 +00007778/* password input */
7779
balrog2bac6012007-04-30 01:34:31 +00007780int qemu_key_check(BlockDriverState *bs, const char *name)
7781{
7782 char password[256];
7783 int i;
7784
7785 if (!bdrv_is_encrypted(bs))
7786 return 0;
7787
7788 term_printf("%s is encrypted.\n", name);
7789 for(i = 0; i < 3; i++) {
7790 monitor_readline("Password: ", 1, password, sizeof(password));
7791 if (bdrv_set_key(bs, password) == 0)
7792 return 0;
7793 term_printf("invalid password\n");
7794 }
7795 return -EPERM;
7796}
7797
bellard5905b2e2004-08-01 21:53:26 +00007798static BlockDriverState *get_bdrv(int index)
7799{
thse4bcb142007-12-02 04:51:10 +00007800 if (index > nb_drives)
7801 return NULL;
7802 return drives_table[index].bdrv;
bellard5905b2e2004-08-01 21:53:26 +00007803}
7804
7805static void read_passwords(void)
7806{
7807 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00007808 int i;
bellard5905b2e2004-08-01 21:53:26 +00007809
7810 for(i = 0; i < 6; i++) {
7811 bs = get_bdrv(i);
balrog2bac6012007-04-30 01:34:31 +00007812 if (bs)
7813 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00007814 }
7815}
7816
bellardcc1daa42005-06-05 14:49:17 +00007817/* XXX: currently we cannot use simultaneously different CPUs */
pbrook9596ebb2007-11-18 01:44:38 +00007818static void register_machines(void)
bellardcc1daa42005-06-05 14:49:17 +00007819{
7820#if defined(TARGET_I386)
7821 qemu_register_machine(&pc_machine);
bellard3dbbdc22005-11-06 18:20:37 +00007822 qemu_register_machine(&isapc_machine);
bellardcc1daa42005-06-05 14:49:17 +00007823#elif defined(TARGET_PPC)
7824 qemu_register_machine(&heathrow_machine);
7825 qemu_register_machine(&core99_machine);
7826 qemu_register_machine(&prep_machine);
j_mayer1a6c0882007-04-24 07:40:49 +00007827 qemu_register_machine(&ref405ep_machine);
7828 qemu_register_machine(&taihu_machine);
bellard6af0bf92005-07-02 14:58:51 +00007829#elif defined(TARGET_MIPS)
7830 qemu_register_machine(&mips_machine);
ths5856de82007-01-15 23:58:11 +00007831 qemu_register_machine(&mips_malta_machine);
thsad6fe1d2007-04-16 17:23:27 +00007832 qemu_register_machine(&mips_pica61_machine);
ths6bf5b4e2007-10-17 13:08:32 +00007833 qemu_register_machine(&mips_mipssim_machine);
bellardcc1daa42005-06-05 14:49:17 +00007834#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +00007835#ifdef TARGET_SPARC64
7836 qemu_register_machine(&sun4u_machine);
7837#else
blueswir136cd9212007-04-01 15:44:43 +00007838 qemu_register_machine(&ss5_machine);
blueswir1e0353fe2007-04-01 15:55:28 +00007839 qemu_register_machine(&ss10_machine);
blueswir16a3b9cc2007-11-11 17:56:38 +00007840 qemu_register_machine(&ss600mp_machine);
bellardcc1daa42005-06-05 14:49:17 +00007841#endif
bellardb5ff1b32005-11-26 10:38:39 +00007842#elif defined(TARGET_ARM)
pbrook3371d272007-03-08 03:04:12 +00007843 qemu_register_machine(&integratorcp_machine);
pbrookcdbdb642006-04-09 01:32:52 +00007844 qemu_register_machine(&versatilepb_machine);
pbrook16406952006-04-27 23:15:07 +00007845 qemu_register_machine(&versatileab_machine);
pbrooke69954b2006-09-23 17:40:58 +00007846 qemu_register_machine(&realview_machine);
balrogb00052e2007-04-30 02:22:06 +00007847 qemu_register_machine(&akitapda_machine);
7848 qemu_register_machine(&spitzpda_machine);
7849 qemu_register_machine(&borzoipda_machine);
7850 qemu_register_machine(&terrierpda_machine);
balrogc3d26892007-07-29 17:57:26 +00007851 qemu_register_machine(&palmte_machine);
pbrook9ee6e8b2007-11-11 00:04:49 +00007852 qemu_register_machine(&lm3s811evb_machine);
7853 qemu_register_machine(&lm3s6965evb_machine);
balrog05ee37e2007-11-17 11:50:55 +00007854 qemu_register_machine(&connex_machine);
balrog3e3f6752007-11-24 23:47:38 +00007855 qemu_register_machine(&verdex_machine);
balrogef056e42007-11-25 01:57:38 +00007856 qemu_register_machine(&mainstone2_machine);
bellard27c7ca72006-04-27 21:32:09 +00007857#elif defined(TARGET_SH4)
7858 qemu_register_machine(&shix_machine);
ths0d78f542007-09-29 19:24:41 +00007859 qemu_register_machine(&r2d_machine);
j_mayereddf68a2007-04-05 07:22:49 +00007860#elif defined(TARGET_ALPHA)
7861 /* XXX: TODO */
pbrook06338792007-05-23 19:58:11 +00007862#elif defined(TARGET_M68K)
pbrook20dcee92007-06-03 11:13:39 +00007863 qemu_register_machine(&mcf5208evb_machine);
pbrook06338792007-05-23 19:58:11 +00007864 qemu_register_machine(&an5206_machine);
pbrookca02f312007-11-10 18:03:23 +00007865 qemu_register_machine(&dummy_m68k_machine);
thsf1ccf902007-10-08 13:16:14 +00007866#elif defined(TARGET_CRIS)
7867 qemu_register_machine(&bareetraxfs_machine);
bellardb5ff1b32005-11-26 10:38:39 +00007868#else
7869#error unsupported CPU
bellard34751872005-07-02 14:31:34 +00007870#endif
bellardcc1daa42005-06-05 14:49:17 +00007871}
7872
bellard1d14ffa2005-10-30 18:58:22 +00007873#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00007874struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00007875#ifdef HAS_AUDIO_CHOICE
bellardfd06c372006-04-24 21:58:30 +00007876#ifdef TARGET_I386
7877 {
7878 "pcspk",
7879 "PC speaker",
7880 0,
7881 1,
7882 { .init_isa = pcspk_audio_init }
7883 },
7884#endif
bellard6a36d842005-12-18 20:34:32 +00007885 {
7886 "sb16",
7887 "Creative Sound Blaster 16",
7888 0,
7889 1,
7890 { .init_isa = SB16_init }
7891 },
7892
7893#ifdef CONFIG_ADLIB
7894 {
7895 "adlib",
7896#ifdef HAS_YMF262
7897 "Yamaha YMF262 (OPL3)",
7898#else
7899 "Yamaha YM3812 (OPL2)",
7900#endif
7901 0,
7902 1,
7903 { .init_isa = Adlib_init }
7904 },
7905#endif
7906
7907#ifdef CONFIG_GUS
7908 {
7909 "gus",
7910 "Gravis Ultrasound GF1",
7911 0,
7912 1,
7913 { .init_isa = GUS_init }
7914 },
7915#endif
7916
7917 {
7918 "es1370",
7919 "ENSONIQ AudioPCI ES1370",
7920 0,
7921 0,
7922 { .init_pci = es1370_init }
7923 },
balrogb00052e2007-04-30 02:22:06 +00007924#endif
bellard6a36d842005-12-18 20:34:32 +00007925
7926 { NULL, NULL, 0, 0, { NULL } }
7927};
7928
bellard1d14ffa2005-10-30 18:58:22 +00007929static void select_soundhw (const char *optarg)
7930{
bellard6a36d842005-12-18 20:34:32 +00007931 struct soundhw *c;
7932
bellard1d14ffa2005-10-30 18:58:22 +00007933 if (*optarg == '?') {
7934 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00007935
bellard1d14ffa2005-10-30 18:58:22 +00007936 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00007937 for (c = soundhw; c->name; ++c) {
7938 printf ("%-11s %s\n", c->name, c->descr);
7939 }
7940 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00007941 exit (*optarg != '?');
7942 }
7943 else {
bellard6a36d842005-12-18 20:34:32 +00007944 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00007945 const char *p;
7946 char *e;
7947 int bad_card = 0;
7948
bellard6a36d842005-12-18 20:34:32 +00007949 if (!strcmp (optarg, "all")) {
7950 for (c = soundhw; c->name; ++c) {
7951 c->enabled = 1;
7952 }
7953 return;
7954 }
bellard1d14ffa2005-10-30 18:58:22 +00007955
bellard6a36d842005-12-18 20:34:32 +00007956 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00007957 while (*p) {
7958 e = strchr (p, ',');
7959 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00007960
7961 for (c = soundhw; c->name; ++c) {
7962 if (!strncmp (c->name, p, l)) {
7963 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00007964 break;
7965 }
7966 }
bellard6a36d842005-12-18 20:34:32 +00007967
7968 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00007969 if (l > 80) {
7970 fprintf (stderr,
7971 "Unknown sound card name (too big to show)\n");
7972 }
7973 else {
7974 fprintf (stderr, "Unknown sound card name `%.*s'\n",
7975 (int) l, p);
7976 }
7977 bad_card = 1;
7978 }
7979 p += l + (e != NULL);
7980 }
7981
7982 if (bad_card)
7983 goto show_valid_cards;
7984 }
7985}
7986#endif
7987
bellard3587d7e2006-06-26 20:03:44 +00007988#ifdef _WIN32
7989static BOOL WINAPI qemu_ctrl_handler(DWORD type)
7990{
7991 exit(STATUS_CONTROL_C_EXIT);
7992 return TRUE;
7993}
7994#endif
7995
bellard7c9d8e02005-11-15 22:16:05 +00007996#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00007997
bellard0824d6f2003-06-24 13:42:40 +00007998int main(int argc, char **argv)
7999{
bellard67b915a2004-03-31 23:37:16 +00008000#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00008001 int use_gdbstub;
8002 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00008003#endif
j_mayer28c5af52007-11-11 01:50:45 +00008004 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00008005 int i;
j_mayer28c5af52007-11-11 01:50:45 +00008006 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00008007 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00008008 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00008009 const char *boot_devices = "";
bellard313aa562003-08-10 21:52:11 +00008010 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00008011 int cyls, heads, secs, translation;
bellard7c9d8e02005-11-15 22:16:05 +00008012 char net_clients[MAX_NET_CLIENTS][256];
8013 int nb_net_clients;
thse4bcb142007-12-02 04:51:10 +00008014 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00008015 int optind;
8016 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00008017 CharDriverState *monitor_hd;
8018 char monitor_device[128];
bellard8d11df92004-08-24 21:13:40 +00008019 char serial_devices[MAX_SERIAL_PORTS][128];
8020 int serial_device_index;
bellard6508fe52005-01-15 12:02:56 +00008021 char parallel_devices[MAX_PARALLEL_PORTS][128];
8022 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00008023 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00008024 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00008025 const char *cpu_model;
pbrook0d92ed32006-05-21 16:30:15 +00008026 char usb_devices[MAX_USB_CMDLINE][128];
bellarda594cfb2005-11-06 16:13:29 +00008027 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00008028 int fds[2];
ths93815bc2007-03-19 15:58:31 +00008029 const char *pid_file = NULL;
blueswir1833c7172007-05-27 19:36:43 +00008030 VLANState *vlan;
bellard0bd48852005-11-11 00:00:47 +00008031
8032 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00008033#ifndef _WIN32
8034 {
8035 struct sigaction act;
8036 sigfillset(&act.sa_mask);
8037 act.sa_flags = 0;
8038 act.sa_handler = SIG_IGN;
8039 sigaction(SIGPIPE, &act, NULL);
8040 }
bellard3587d7e2006-06-26 20:03:44 +00008041#else
8042 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00008043 /* Note: cpu_interrupt() is currently not SMP safe, so we force
8044 QEMU to run on a single CPU */
8045 {
8046 HANDLE h;
8047 DWORD mask, smask;
8048 int i;
8049 h = GetCurrentProcess();
8050 if (GetProcessAffinityMask(h, &mask, &smask)) {
8051 for(i = 0; i < 32; i++) {
8052 if (mask & (1 << i))
8053 break;
8054 }
8055 if (i != 32) {
8056 mask = 1 << i;
8057 SetProcessAffinityMask(h, mask);
8058 }
8059 }
8060 }
bellard67b915a2004-03-31 23:37:16 +00008061#endif
bellardbe995c22006-06-25 16:25:21 +00008062
bellardcc1daa42005-06-05 14:49:17 +00008063 register_machines();
8064 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00008065 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00008066 initrd_filename = NULL;
bellarda00bad72004-05-22 21:39:06 +00008067 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
bellard313aa562003-08-10 21:52:11 +00008068 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00008069#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00008070 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00008071 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00008072#endif
bellard33e39632003-07-06 17:15:21 +00008073 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00008074 nographic = 0;
8075 kernel_filename = NULL;
8076 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00008077 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00008078 translation = BIOS_ATA_TRANSLATION_AUTO;
bellard82c643f2004-07-14 17:28:13 +00008079 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
bellardc4b1fcc2004-03-14 21:44:30 +00008080
bellard8d11df92004-08-24 21:13:40 +00008081 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
8082 for(i = 1; i < MAX_SERIAL_PORTS; i++)
8083 serial_devices[i][0] = '\0';
8084 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00008085
bellard6508fe52005-01-15 12:02:56 +00008086 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
8087 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
8088 parallel_devices[i][0] = '\0';
8089 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00008090
bellarda594cfb2005-11-06 16:13:29 +00008091 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00008092
bellard7c9d8e02005-11-15 22:16:05 +00008093 nb_net_clients = 0;
thse4bcb142007-12-02 04:51:10 +00008094 nb_drives = 0;
8095 nb_drives_opt = 0;
8096 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00008097
8098 nb_nics = 0;
bellard702c6512004-04-02 21:21:32 +00008099 /* default mac address of the first network interface */
ths3b46e622007-09-17 08:09:54 +00008100
bellardcd6f1162004-05-13 22:02:20 +00008101 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00008102 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00008103 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00008104 break;
bellardcd6f1162004-05-13 22:02:20 +00008105 r = argv[optind];
8106 if (r[0] != '-') {
thse4bcb142007-12-02 04:51:10 +00008107 hda_index = drive_add(HD_ALIAS, argv[optind++], 0);
bellardcd6f1162004-05-13 22:02:20 +00008108 } else {
8109 const QEMUOption *popt;
8110
8111 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00008112 /* Treat --foo the same as -foo. */
8113 if (r[1] == '-')
8114 r++;
bellardcd6f1162004-05-13 22:02:20 +00008115 popt = qemu_options;
8116 for(;;) {
8117 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00008118 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00008119 argv[0], r);
8120 exit(1);
8121 }
8122 if (!strcmp(popt->name, r + 1))
8123 break;
8124 popt++;
8125 }
8126 if (popt->flags & HAS_ARG) {
8127 if (optind >= argc) {
8128 fprintf(stderr, "%s: option '%s' requires an argument\n",
8129 argv[0], r);
8130 exit(1);
8131 }
8132 optarg = argv[optind++];
8133 } else {
8134 optarg = NULL;
8135 }
8136
8137 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00008138 case QEMU_OPTION_M:
8139 machine = find_machine(optarg);
8140 if (!machine) {
8141 QEMUMachine *m;
8142 printf("Supported machines are:\n");
8143 for(m = first_machine; m != NULL; m = m->next) {
8144 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00008145 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00008146 m == first_machine ? " (default)" : "");
8147 }
ths15f82202007-06-29 23:26:08 +00008148 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00008149 }
8150 break;
j_mayer94fc95c2007-03-05 19:44:02 +00008151 case QEMU_OPTION_cpu:
8152 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00008153 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00008154/* XXX: implement xxx_cpu_list for targets that still miss it */
8155#if defined(cpu_list)
8156 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00008157#endif
ths15f82202007-06-29 23:26:08 +00008158 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00008159 } else {
8160 cpu_model = optarg;
8161 }
8162 break;
bellardcd6f1162004-05-13 22:02:20 +00008163 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00008164 initrd_filename = optarg;
8165 break;
bellardcd6f1162004-05-13 22:02:20 +00008166 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00008167 if (cyls == 0)
8168 hda_index = drive_add(HD_ALIAS, optarg, 0);
8169 else
8170 hda_index = drive_add(HD_ALIAS
8171 ",cyls=%d,heads=%d,secs=%d%s",
8172 optarg, 0, cyls, heads, secs,
8173 translation == BIOS_ATA_TRANSLATION_LBA ?
8174 ",trans=lba" :
8175 translation == BIOS_ATA_TRANSLATION_NONE ?
8176 ",trans=none" : "");
8177 break;
bellardcd6f1162004-05-13 22:02:20 +00008178 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00008179 case QEMU_OPTION_hdc:
8180 case QEMU_OPTION_hdd:
thse4bcb142007-12-02 04:51:10 +00008181 drive_add(HD_ALIAS, optarg, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00008182 break;
thse4bcb142007-12-02 04:51:10 +00008183 case QEMU_OPTION_drive:
8184 drive_add("%s", optarg);
8185 break;
balrog3e3d5812007-04-30 02:09:25 +00008186 case QEMU_OPTION_mtdblock:
thse4bcb142007-12-02 04:51:10 +00008187 drive_add(MTD_ALIAS, optarg);
balrog3e3d5812007-04-30 02:09:25 +00008188 break;
pbrooka1bb27b2007-04-06 16:49:48 +00008189 case QEMU_OPTION_sd:
balrog9d413d12007-12-04 00:10:34 +00008190 drive_add("file=\"%s\"," SD_ALIAS, optarg);
pbrooka1bb27b2007-04-06 16:49:48 +00008191 break;
j_mayer86f55662007-04-24 06:52:59 +00008192 case QEMU_OPTION_pflash:
thse4bcb142007-12-02 04:51:10 +00008193 drive_add(PFLASH_ALIAS, optarg);
j_mayer86f55662007-04-24 06:52:59 +00008194 break;
bellardcd6f1162004-05-13 22:02:20 +00008195 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00008196 snapshot = 1;
8197 break;
bellardcd6f1162004-05-13 22:02:20 +00008198 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00008199 {
bellard330d0412003-07-26 18:11:40 +00008200 const char *p;
8201 p = optarg;
8202 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00008203 if (cyls < 1 || cyls > 16383)
8204 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00008205 if (*p != ',')
8206 goto chs_fail;
8207 p++;
8208 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00008209 if (heads < 1 || heads > 16)
8210 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00008211 if (*p != ',')
8212 goto chs_fail;
8213 p++;
8214 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00008215 if (secs < 1 || secs > 63)
8216 goto chs_fail;
8217 if (*p == ',') {
8218 p++;
8219 if (!strcmp(p, "none"))
8220 translation = BIOS_ATA_TRANSLATION_NONE;
8221 else if (!strcmp(p, "lba"))
8222 translation = BIOS_ATA_TRANSLATION_LBA;
8223 else if (!strcmp(p, "auto"))
8224 translation = BIOS_ATA_TRANSLATION_AUTO;
8225 else
8226 goto chs_fail;
8227 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00008228 chs_fail:
bellard46d47672004-11-16 01:45:27 +00008229 fprintf(stderr, "qemu: invalid physical CHS format\n");
8230 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00008231 }
thse4bcb142007-12-02 04:51:10 +00008232 if (hda_index != -1)
8233 snprintf(drives_opt[hda_index] +
8234 strlen(drives_opt[hda_index]),
8235 sizeof(drives_opt[0]) -
8236 strlen(drives_opt[hda_index]),
8237 ",cyls=%d,heads=%d,secs=%d%s",
8238 cyls, heads, secs,
8239 translation == BIOS_ATA_TRANSLATION_LBA ?
8240 ",trans=lba" :
8241 translation == BIOS_ATA_TRANSLATION_NONE ?
8242 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00008243 }
8244 break;
bellardcd6f1162004-05-13 22:02:20 +00008245 case QEMU_OPTION_nographic:
bellard8d11df92004-08-24 21:13:40 +00008246 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
thsa39437a2007-03-25 20:27:04 +00008247 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null");
ths20d8a3e2007-02-18 17:04:49 +00008248 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
bellarda20dd502003-09-30 21:07:02 +00008249 nographic = 1;
8250 break;
balroga171fe32007-04-30 01:48:07 +00008251 case QEMU_OPTION_portrait:
8252 graphic_rotate = 1;
8253 break;
bellardcd6f1162004-05-13 22:02:20 +00008254 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00008255 kernel_filename = optarg;
8256 break;
bellardcd6f1162004-05-13 22:02:20 +00008257 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00008258 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00008259 break;
bellardcd6f1162004-05-13 22:02:20 +00008260 case QEMU_OPTION_cdrom:
thse4bcb142007-12-02 04:51:10 +00008261 drive_add("file=\"%s\"," CDROM_ALIAS, optarg);
bellard36b486b2003-11-11 13:36:08 +00008262 break;
bellardcd6f1162004-05-13 22:02:20 +00008263 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00008264 boot_devices = optarg;
8265 /* We just do some generic consistency checks */
8266 {
8267 /* Could easily be extended to 64 devices if needed */
8268 const unsigned char *p;
8269
8270 boot_devices_bitmap = 0;
8271 for (p = boot_devices; *p != '\0'; p++) {
8272 /* Allowed boot devices are:
8273 * a b : floppy disk drives
8274 * c ... f : IDE disk drives
8275 * g ... m : machine implementation dependant drives
8276 * n ... p : network devices
8277 * It's up to each machine implementation to check
8278 * if the given boot devices match the actual hardware
8279 * implementation and firmware features.
8280 */
8281 if (*p < 'a' || *p > 'q') {
8282 fprintf(stderr, "Invalid boot device '%c'\n", *p);
8283 exit(1);
8284 }
8285 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
8286 fprintf(stderr,
8287 "Boot device '%c' was given twice\n",*p);
8288 exit(1);
8289 }
8290 boot_devices_bitmap |= 1 << (*p - 'a');
8291 }
bellard36b486b2003-11-11 13:36:08 +00008292 }
8293 break;
bellardcd6f1162004-05-13 22:02:20 +00008294 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00008295 case QEMU_OPTION_fdb:
thse4bcb142007-12-02 04:51:10 +00008296 drive_add("file=\"%s\"," FD_ALIAS, optarg,
8297 popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00008298 break;
bellard52ca8d62006-06-14 16:03:05 +00008299#ifdef TARGET_I386
8300 case QEMU_OPTION_no_fd_bootchk:
8301 fd_bootchk = 0;
8302 break;
8303#endif
bellardcd6f1162004-05-13 22:02:20 +00008304 case QEMU_OPTION_no_code_copy:
bellard77fef8c2004-02-16 22:05:46 +00008305 code_copy_enabled = 0;
8306 break;
bellard7c9d8e02005-11-15 22:16:05 +00008307 case QEMU_OPTION_net:
8308 if (nb_net_clients >= MAX_NET_CLIENTS) {
8309 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00008310 exit(1);
8311 }
bellard7c9d8e02005-11-15 22:16:05 +00008312 pstrcpy(net_clients[nb_net_clients],
8313 sizeof(net_clients[0]),
8314 optarg);
8315 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00008316 break;
bellardc7f74642004-08-24 21:57:12 +00008317#ifdef CONFIG_SLIRP
8318 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00008319 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00008320 break;
ths47d5d012007-02-20 00:05:08 +00008321 case QEMU_OPTION_bootp:
8322 bootp_filename = optarg;
8323 break;
bellardc94c8d62004-09-13 21:37:34 +00008324#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00008325 case QEMU_OPTION_smb:
8326 net_slirp_smb(optarg);
8327 break;
bellardc94c8d62004-09-13 21:37:34 +00008328#endif
bellard9bf05442004-08-25 22:12:49 +00008329 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00008330 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00008331 break;
bellardc7f74642004-08-24 21:57:12 +00008332#endif
bellard1d14ffa2005-10-30 18:58:22 +00008333#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00008334 case QEMU_OPTION_audio_help:
8335 AUD_help ();
8336 exit (0);
8337 break;
8338 case QEMU_OPTION_soundhw:
8339 select_soundhw (optarg);
8340 break;
8341#endif
bellardcd6f1162004-05-13 22:02:20 +00008342 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00008343 help(0);
bellardcd6f1162004-05-13 22:02:20 +00008344 break;
8345 case QEMU_OPTION_m:
8346 ram_size = atoi(optarg) * 1024 * 1024;
8347 if (ram_size <= 0)
ths15f82202007-06-29 23:26:08 +00008348 help(1);
bellardcd6f1162004-05-13 22:02:20 +00008349 if (ram_size > PHYS_RAM_MAX_SIZE) {
8350 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
8351 PHYS_RAM_MAX_SIZE / (1024 * 1024));
8352 exit(1);
8353 }
8354 break;
8355 case QEMU_OPTION_d:
8356 {
8357 int mask;
8358 CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00008359
bellardcd6f1162004-05-13 22:02:20 +00008360 mask = cpu_str_to_log_mask(optarg);
8361 if (!mask) {
8362 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00008363 for(item = cpu_log_items; item->mask != 0; item++) {
8364 printf("%-10s %s\n", item->name, item->help);
8365 }
8366 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00008367 }
8368 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00008369 }
bellardcd6f1162004-05-13 22:02:20 +00008370 break;
bellard67b915a2004-03-31 23:37:16 +00008371#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00008372 case QEMU_OPTION_s:
8373 use_gdbstub = 1;
8374 break;
8375 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00008376 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00008377 break;
bellard67b915a2004-03-31 23:37:16 +00008378#endif
bellardcd6f1162004-05-13 22:02:20 +00008379 case QEMU_OPTION_L:
8380 bios_dir = optarg;
8381 break;
j_mayer1192dad2007-10-05 13:08:35 +00008382 case QEMU_OPTION_bios:
8383 bios_name = optarg;
8384 break;
bellardcd6f1162004-05-13 22:02:20 +00008385 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00008386 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00008387 break;
bellard3d11d0e2004-12-12 16:56:30 +00008388 case QEMU_OPTION_k:
8389 keyboard_layout = optarg;
8390 break;
bellardee22c2f2004-06-03 12:49:50 +00008391 case QEMU_OPTION_localtime:
8392 rtc_utc = 0;
8393 break;
bellard1f042752004-06-05 13:46:47 +00008394 case QEMU_OPTION_cirrusvga:
8395 cirrus_vga_enabled = 1;
thsd34cab92007-04-02 01:10:46 +00008396 vmsvga_enabled = 0;
8397 break;
8398 case QEMU_OPTION_vmsvga:
8399 cirrus_vga_enabled = 0;
8400 vmsvga_enabled = 1;
bellard1f042752004-06-05 13:46:47 +00008401 break;
bellard1bfe8562004-07-08 21:17:50 +00008402 case QEMU_OPTION_std_vga:
8403 cirrus_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +00008404 vmsvga_enabled = 0;
bellard1bfe8562004-07-08 21:17:50 +00008405 break;
bellarde9b137c2004-06-21 16:46:10 +00008406 case QEMU_OPTION_g:
8407 {
8408 const char *p;
8409 int w, h, depth;
8410 p = optarg;
8411 w = strtol(p, (char **)&p, 10);
8412 if (w <= 0) {
8413 graphic_error:
8414 fprintf(stderr, "qemu: invalid resolution or depth\n");
8415 exit(1);
8416 }
8417 if (*p != 'x')
8418 goto graphic_error;
8419 p++;
8420 h = strtol(p, (char **)&p, 10);
8421 if (h <= 0)
8422 goto graphic_error;
8423 if (*p == 'x') {
8424 p++;
8425 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00008426 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00008427 depth != 24 && depth != 32)
8428 goto graphic_error;
8429 } else if (*p == '\0') {
8430 depth = graphic_depth;
8431 } else {
8432 goto graphic_error;
8433 }
ths3b46e622007-09-17 08:09:54 +00008434
bellarde9b137c2004-06-21 16:46:10 +00008435 graphic_width = w;
8436 graphic_height = h;
8437 graphic_depth = depth;
8438 }
8439 break;
ths20d8a3e2007-02-18 17:04:49 +00008440 case QEMU_OPTION_echr:
8441 {
8442 char *r;
8443 term_escape_char = strtol(optarg, &r, 0);
8444 if (r == optarg)
8445 printf("Bad argument to echr\n");
8446 break;
8447 }
bellard82c643f2004-07-14 17:28:13 +00008448 case QEMU_OPTION_monitor:
8449 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
8450 break;
8451 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00008452 if (serial_device_index >= MAX_SERIAL_PORTS) {
8453 fprintf(stderr, "qemu: too many serial ports\n");
8454 exit(1);
8455 }
ths5fafdf22007-09-16 21:08:06 +00008456 pstrcpy(serial_devices[serial_device_index],
bellard8d11df92004-08-24 21:13:40 +00008457 sizeof(serial_devices[0]), optarg);
8458 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00008459 break;
bellard6508fe52005-01-15 12:02:56 +00008460 case QEMU_OPTION_parallel:
8461 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
8462 fprintf(stderr, "qemu: too many parallel ports\n");
8463 exit(1);
8464 }
ths5fafdf22007-09-16 21:08:06 +00008465 pstrcpy(parallel_devices[parallel_device_index],
bellard6508fe52005-01-15 12:02:56 +00008466 sizeof(parallel_devices[0]), optarg);
8467 parallel_device_index++;
8468 break;
bellardd63d3072004-10-03 13:29:03 +00008469 case QEMU_OPTION_loadvm:
8470 loadvm = optarg;
8471 break;
8472 case QEMU_OPTION_full_screen:
8473 full_screen = 1;
8474 break;
ths667acca2006-12-11 02:08:05 +00008475#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00008476 case QEMU_OPTION_no_frame:
8477 no_frame = 1;
8478 break;
ths3780e192007-06-21 21:08:02 +00008479 case QEMU_OPTION_alt_grab:
8480 alt_grab = 1;
8481 break;
ths667acca2006-12-11 02:08:05 +00008482 case QEMU_OPTION_no_quit:
8483 no_quit = 1;
8484 break;
8485#endif
bellardf7cce892004-12-08 22:21:25 +00008486 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00008487 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00008488 break;
bellarda09db212005-04-30 16:10:35 +00008489#ifdef TARGET_I386
8490 case QEMU_OPTION_win2k_hack:
8491 win2k_install_hack = 1;
8492 break;
8493#endif
bellardd993e022005-02-10 22:00:06 +00008494#ifdef USE_KQEMU
8495 case QEMU_OPTION_no_kqemu:
8496 kqemu_allowed = 0;
8497 break;
bellard89bfc102006-02-08 22:46:31 +00008498 case QEMU_OPTION_kernel_kqemu:
8499 kqemu_allowed = 2;
8500 break;
bellardd993e022005-02-10 22:00:06 +00008501#endif
bellardbb36d472005-11-05 14:22:28 +00008502 case QEMU_OPTION_usb:
8503 usb_enabled = 1;
8504 break;
bellarda594cfb2005-11-06 16:13:29 +00008505 case QEMU_OPTION_usbdevice:
8506 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00008507 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00008508 fprintf(stderr, "Too many USB devices\n");
8509 exit(1);
8510 }
8511 pstrcpy(usb_devices[usb_devices_index],
8512 sizeof(usb_devices[usb_devices_index]),
8513 optarg);
8514 usb_devices_index++;
8515 break;
bellard6a00d602005-11-21 23:25:50 +00008516 case QEMU_OPTION_smp:
8517 smp_cpus = atoi(optarg);
bellardba3c64f2005-12-05 20:31:52 +00008518 if (smp_cpus < 1 || smp_cpus > MAX_CPUS) {
bellard6a00d602005-11-21 23:25:50 +00008519 fprintf(stderr, "Invalid number of CPUs\n");
8520 exit(1);
8521 }
8522 break;
bellard24236862006-04-30 21:28:36 +00008523 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00008524 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00008525 break;
bellard6515b202006-05-03 22:02:44 +00008526 case QEMU_OPTION_no_acpi:
8527 acpi_enabled = 0;
8528 break;
bellardd1beab82006-10-02 19:44:22 +00008529 case QEMU_OPTION_no_reboot:
8530 no_reboot = 1;
8531 break;
balrog9467cd42007-05-01 01:34:14 +00008532 case QEMU_OPTION_show_cursor:
8533 cursor_hide = 0;
8534 break;
ths71e3ceb2006-12-22 02:11:31 +00008535 case QEMU_OPTION_daemonize:
8536 daemonize = 1;
8537 break;
ths9ae02552007-01-05 17:39:04 +00008538 case QEMU_OPTION_option_rom:
8539 if (nb_option_roms >= MAX_OPTION_ROMS) {
8540 fprintf(stderr, "Too many option ROMs\n");
8541 exit(1);
8542 }
8543 option_rom[nb_option_roms] = optarg;
8544 nb_option_roms++;
8545 break;
pbrook8e716212007-01-20 17:12:09 +00008546 case QEMU_OPTION_semihosting:
8547 semihosting_enabled = 1;
8548 break;
thsc35734b2007-03-19 15:17:08 +00008549 case QEMU_OPTION_name:
8550 qemu_name = optarg;
8551 break;
blueswir166508602007-05-01 14:16:52 +00008552#ifdef TARGET_SPARC
8553 case QEMU_OPTION_prom_env:
8554 if (nb_prom_envs >= MAX_PROM_ENVS) {
8555 fprintf(stderr, "Too many prom variables\n");
8556 exit(1);
8557 }
8558 prom_envs[nb_prom_envs] = optarg;
8559 nb_prom_envs++;
8560 break;
8561#endif
balrog2b8f2d42007-07-27 22:08:46 +00008562#ifdef TARGET_ARM
8563 case QEMU_OPTION_old_param:
8564 old_param = 1;
8565#endif
thsf3dcfad2007-08-24 01:26:02 +00008566 case QEMU_OPTION_clock:
8567 configure_alarms(optarg);
8568 break;
bellard7e0af5d02007-11-07 16:24:33 +00008569 case QEMU_OPTION_startdate:
8570 {
8571 struct tm tm;
8572 if (!strcmp(optarg, "now")) {
8573 rtc_start_date = -1;
8574 } else {
8575 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
8576 &tm.tm_year,
8577 &tm.tm_mon,
8578 &tm.tm_mday,
8579 &tm.tm_hour,
8580 &tm.tm_min,
8581 &tm.tm_sec) == 6) {
8582 /* OK */
8583 } else if (sscanf(optarg, "%d-%d-%d",
8584 &tm.tm_year,
8585 &tm.tm_mon,
8586 &tm.tm_mday) == 3) {
8587 tm.tm_hour = 0;
8588 tm.tm_min = 0;
8589 tm.tm_sec = 0;
8590 } else {
8591 goto date_fail;
8592 }
8593 tm.tm_year -= 1900;
8594 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00008595 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00008596 if (rtc_start_date == -1) {
8597 date_fail:
8598 fprintf(stderr, "Invalid date format. Valid format are:\n"
8599 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8600 exit(1);
8601 }
8602 }
8603 }
8604 break;
bellardcd6f1162004-05-13 22:02:20 +00008605 }
bellard0824d6f2003-06-24 13:42:40 +00008606 }
8607 }
bellard330d0412003-07-26 18:11:40 +00008608
ths71e3ceb2006-12-22 02:11:31 +00008609#ifndef _WIN32
8610 if (daemonize && !nographic && vnc_display == NULL) {
8611 fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n");
8612 daemonize = 0;
8613 }
8614
8615 if (daemonize) {
8616 pid_t pid;
8617
8618 if (pipe(fds) == -1)
8619 exit(1);
8620
8621 pid = fork();
8622 if (pid > 0) {
8623 uint8_t status;
8624 ssize_t len;
8625
8626 close(fds[1]);
8627
8628 again:
ths93815bc2007-03-19 15:58:31 +00008629 len = read(fds[0], &status, 1);
8630 if (len == -1 && (errno == EINTR))
8631 goto again;
8632
8633 if (len != 1)
8634 exit(1);
8635 else if (status == 1) {
8636 fprintf(stderr, "Could not acquire pidfile\n");
8637 exit(1);
8638 } else
8639 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00008640 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00008641 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00008642
8643 setsid();
8644
8645 pid = fork();
8646 if (pid > 0)
8647 exit(0);
8648 else if (pid < 0)
8649 exit(1);
8650
8651 umask(027);
8652 chdir("/");
8653
8654 signal(SIGTSTP, SIG_IGN);
8655 signal(SIGTTOU, SIG_IGN);
8656 signal(SIGTTIN, SIG_IGN);
8657 }
8658#endif
8659
thsaa26bb22007-03-25 21:33:06 +00008660 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00008661 if (daemonize) {
8662 uint8_t status = 1;
8663 write(fds[1], &status, 1);
8664 } else
8665 fprintf(stderr, "Could not acquire pid file\n");
8666 exit(1);
8667 }
8668
bellardff3fbb32006-01-08 10:53:14 +00008669#ifdef USE_KQEMU
8670 if (smp_cpus > 1)
8671 kqemu_allowed = 0;
8672#endif
bellarda20dd502003-09-30 21:07:02 +00008673 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00008674 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00008675
j_mayer28c5af52007-11-11 01:50:45 +00008676 /* XXX: this should not be: some embedded targets just have flash */
8677 if (!linux_boot && net_boot == 0 &&
thse4bcb142007-12-02 04:51:10 +00008678 nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00008679 help(1);
bellard0824d6f2003-06-24 13:42:40 +00008680
ths96d30e42007-01-07 20:42:14 +00008681 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00008682 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00008683 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00008684 }
bellardb118d612003-06-30 23:36:21 +00008685 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00008686
pbrook634fce92006-07-15 17:40:09 +00008687 init_timers();
8688 init_timer_alarm();
bellard83f64092006-08-01 16:21:11 +00008689 qemu_aio_init();
pbrook634fce92006-07-15 17:40:09 +00008690
bellardfd1dff42006-02-01 21:29:26 +00008691#ifdef _WIN32
8692 socket_init();
8693#endif
8694
bellard7c9d8e02005-11-15 22:16:05 +00008695 /* init network clients */
8696 if (nb_net_clients == 0) {
8697 /* if no clients, we use a default config */
8698 pstrcpy(net_clients[0], sizeof(net_clients[0]),
8699 "nic");
8700 pstrcpy(net_clients[1], sizeof(net_clients[0]),
8701 "user");
8702 nb_net_clients = 2;
bellardc20709a2004-04-21 23:27:19 +00008703 }
8704
bellard7c9d8e02005-11-15 22:16:05 +00008705 for(i = 0;i < nb_net_clients; i++) {
8706 if (net_client_init(net_clients[i]) < 0)
8707 exit(1);
bellard702c6512004-04-02 21:21:32 +00008708 }
blueswir1833c7172007-05-27 19:36:43 +00008709 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8710 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
8711 continue;
8712 if (vlan->nb_guest_devs == 0) {
8713 fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
8714 exit(1);
8715 }
8716 if (vlan->nb_host_devs == 0)
8717 fprintf(stderr,
8718 "Warning: vlan %d is not connected to host network\n",
8719 vlan->id);
8720 }
bellardf1510b22003-06-25 00:07:40 +00008721
thseec85c22007-01-05 17:41:07 +00008722#ifdef TARGET_I386
j_mayer28c5af52007-11-11 01:50:45 +00008723 /* XXX: this should be moved in the PC machine instanciation code */
8724 if (net_boot != 0) {
8725 int netroms = 0;
8726 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00008727 const char *model = nd_table[i].model;
8728 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00008729 if (net_boot & (1 << i)) {
8730 if (model == NULL)
8731 model = "ne2k_pci";
8732 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
8733 if (get_image_size(buf) > 0) {
8734 if (nb_option_roms >= MAX_OPTION_ROMS) {
8735 fprintf(stderr, "Too many option ROMs\n");
8736 exit(1);
8737 }
8738 option_rom[nb_option_roms] = strdup(buf);
8739 nb_option_roms++;
8740 netroms++;
8741 }
8742 }
thseec85c22007-01-05 17:41:07 +00008743 }
j_mayer28c5af52007-11-11 01:50:45 +00008744 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00008745 fprintf(stderr, "No valid PXE rom found for network device\n");
8746 exit(1);
8747 }
thseec85c22007-01-05 17:41:07 +00008748 }
8749#endif
8750
bellard0824d6f2003-06-24 13:42:40 +00008751 /* init the memory */
bellard970ac5a2007-02-08 23:09:59 +00008752 phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
ths9ae02552007-01-05 17:39:04 +00008753
bellardd993e022005-02-10 22:00:06 +00008754 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00008755 if (!phys_ram_base) {
8756 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00008757 exit(1);
8758 }
8759
bellard5905b2e2004-08-01 21:53:26 +00008760 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00008761
8762 /* we always create the cdrom drive, even if no disk is there */
8763
8764 if (nb_drives_opt < MAX_DRIVES)
8765 drive_add(CDROM_ALIAS);
8766
balrog9d413d12007-12-04 00:10:34 +00008767 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00008768
8769 if (nb_drives_opt < MAX_DRIVES)
8770 drive_add(FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00008771
balrog9d413d12007-12-04 00:10:34 +00008772 /* we always create one sd slot, even if no card is in it */
8773
8774 if (nb_drives_opt < MAX_DRIVES)
8775 drive_add(SD_ALIAS);
8776
ths96d30e42007-01-07 20:42:14 +00008777 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00008778
thse4bcb142007-12-02 04:51:10 +00008779 for(i = 0; i < nb_drives_opt; i++)
8780 if (drive_init(drives_opt[i], snapshot, machine) == -1)
8781 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00008782
bellardc88676f2006-08-06 13:36:11 +00008783 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
8784 register_savevm("ram", 0, 2, ram_save, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00008785
bellard330d0412003-07-26 18:11:40 +00008786 init_ioports();
bellard0824d6f2003-06-24 13:42:40 +00008787
bellard313aa562003-08-10 21:52:11 +00008788 /* terminal init */
ths740733b2007-06-08 01:57:56 +00008789 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00008790 if (nographic) {
ths2ff89792007-06-21 23:34:19 +00008791 /* nearly nothing to do */
8792 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00008793 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00008794 vnc_display_init(ds);
8795 if (vnc_display_open(ds, vnc_display) < 0)
8796 exit(1);
bellard313aa562003-08-10 21:52:11 +00008797 } else {
bellard5b0753e2005-03-01 21:37:28 +00008798#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00008799 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00008800#elif defined(CONFIG_COCOA)
8801 cocoa_display_init(ds, full_screen);
pbrook67276f52007-11-15 19:04:08 +00008802#else
8803 dumb_display_init(ds);
bellard313aa562003-08-10 21:52:11 +00008804#endif
8805 }
bellard0824d6f2003-06-24 13:42:40 +00008806
ths20d8a3e2007-02-18 17:04:49 +00008807 /* Maintain compatibility with multiple stdio monitors */
8808 if (!strcmp(monitor_device,"stdio")) {
8809 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
8810 if (!strcmp(serial_devices[i],"mon:stdio")) {
8811 monitor_device[0] = '\0';
8812 break;
8813 } else if (!strcmp(serial_devices[i],"stdio")) {
8814 monitor_device[0] = '\0';
8815 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio");
8816 break;
8817 }
8818 }
bellard82c643f2004-07-14 17:28:13 +00008819 }
ths20d8a3e2007-02-18 17:04:49 +00008820 if (monitor_device[0] != '\0') {
8821 monitor_hd = qemu_chr_open(monitor_device);
8822 if (!monitor_hd) {
8823 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
8824 exit(1);
8825 }
8826 monitor_init(monitor_hd, !nographic);
8827 }
bellard82c643f2004-07-14 17:28:13 +00008828
bellard8d11df92004-08-24 21:13:40 +00008829 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008830 const char *devname = serial_devices[i];
8831 if (devname[0] != '\0' && strcmp(devname, "none")) {
8832 serial_hds[i] = qemu_chr_open(devname);
bellard8d11df92004-08-24 21:13:40 +00008833 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008834 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008835 devname);
bellard8d11df92004-08-24 21:13:40 +00008836 exit(1);
8837 }
thsaf3a9032007-07-11 23:14:59 +00008838 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008839 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00008840 }
bellard82c643f2004-07-14 17:28:13 +00008841 }
bellard82c643f2004-07-14 17:28:13 +00008842
bellard6508fe52005-01-15 12:02:56 +00008843 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00008844 const char *devname = parallel_devices[i];
8845 if (devname[0] != '\0' && strcmp(devname, "none")) {
8846 parallel_hds[i] = qemu_chr_open(devname);
bellard6508fe52005-01-15 12:02:56 +00008847 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00008848 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00008849 devname);
bellard6508fe52005-01-15 12:02:56 +00008850 exit(1);
8851 }
thsaf3a9032007-07-11 23:14:59 +00008852 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00008853 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00008854 }
8855 }
8856
blueswir1b881c2c2007-11-18 08:46:58 +00008857 machine->init(ram_size, vga_ram_size, boot_devices, ds,
j_mayer94fc95c2007-03-05 19:44:02 +00008858 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00008859
pbrook0d92ed32006-05-21 16:30:15 +00008860 /* init USB devices */
8861 if (usb_enabled) {
8862 for(i = 0; i < usb_devices_index; i++) {
8863 if (usb_device_add(usb_devices[i]) < 0) {
8864 fprintf(stderr, "Warning: could not add USB device %s\n",
8865 usb_devices[i]);
8866 }
8867 }
8868 }
8869
ths740733b2007-06-08 01:57:56 +00008870 if (display_state.dpy_refresh) {
8871 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
8872 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
8873 }
bellard7f7f9872003-10-30 01:11:23 +00008874
bellard67b915a2004-03-31 23:37:16 +00008875#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00008876 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00008877 /* XXX: use standard host:port notation and modify options
8878 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00008879 if (gdbserver_start(gdbstub_port) < 0) {
8880 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00008881 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00008882 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00008883 }
balrog45669e02007-07-02 13:20:17 +00008884 }
bellard67b915a2004-03-31 23:37:16 +00008885#endif
balrog45669e02007-07-02 13:20:17 +00008886
bellardd63d3072004-10-03 13:29:03 +00008887 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00008888 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00008889
bellard67b915a2004-03-31 23:37:16 +00008890 {
bellard5905b2e2004-08-01 21:53:26 +00008891 /* XXX: simplify init */
8892 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00008893 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00008894 vm_start();
8895 }
bellard0824d6f2003-06-24 13:42:40 +00008896 }
thsffd843b2006-12-21 19:46:43 +00008897
ths71e3ceb2006-12-22 02:11:31 +00008898 if (daemonize) {
8899 uint8_t status = 0;
8900 ssize_t len;
8901 int fd;
8902
8903 again1:
8904 len = write(fds[1], &status, 1);
8905 if (len == -1 && (errno == EINTR))
8906 goto again1;
8907
8908 if (len != 1)
8909 exit(1);
8910
balrogaeb30be2007-07-02 15:03:13 +00008911 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00008912 if (fd == -1)
8913 exit(1);
8914
8915 dup2(fd, 0);
8916 dup2(fd, 1);
8917 dup2(fd, 2);
8918
8919 close(fd);
8920 }
8921
bellard8a7ddc32004-03-31 19:00:16 +00008922 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00008923 quit_timers();
thsb46a8902007-10-21 23:20:45 +00008924
ths7d294b62007-10-26 17:21:58 +00008925#if !defined(_WIN32)
thsb46a8902007-10-21 23:20:45 +00008926 /* close network clients */
8927 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8928 VLANClientState *vc;
8929
ths7d294b62007-10-26 17:21:58 +00008930 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
thsb46a8902007-10-21 23:20:45 +00008931 if (vc->fd_read == tap_receive) {
8932 char ifname[64];
8933 TAPState *s = vc->opaque;
8934
8935 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
8936 s->down_script[0])
8937 launch_script(s->down_script, ifname, s->fd);
8938 }
thsb46a8902007-10-21 23:20:45 +00008939 }
ths7d294b62007-10-26 17:21:58 +00008940 }
8941#endif
bellard0824d6f2003-06-24 13:42:40 +00008942 return 0;
8943}