blob: b1cd6553cbc211f275325ead4bad2d0f4800ebd5 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
bellard0824d6f2003-06-24 13:42:40 +00003 *
bellardd8272202005-04-06 20:32:23 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellard0824d6f2003-06-24 13:42:40 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard67b915a2004-03-31 23:37:16 +000024#include "vl.h"
25
bellard0824d6f2003-06-24 13:42:40 +000026#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000027#include <fcntl.h>
28#include <signal.h>
29#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000030#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000031#include <sys/time.h>
32
33#ifndef _WIN32
34#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000035#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000036#include <termios.h>
37#include <sys/poll.h>
38#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/ioctl.h>
40#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000041#include <netinet/in.h>
bellard9d728e82004-09-05 23:09:03 +000042#include <dirent.h>
bellard7d3505c2004-05-12 19:32:15 +000043#ifdef _BSD
44#include <sys/stat.h>
bellard83fb7ad2004-07-05 21:25:26 +000045#ifndef __APPLE__
bellard7d3505c2004-05-12 19:32:15 +000046#include <libutil.h>
bellard83fb7ad2004-07-05 21:25:26 +000047#endif
bellard7d3505c2004-05-12 19:32:15 +000048#else
bellardf1510b22003-06-25 00:07:40 +000049#include <linux/if.h>
50#include <linux/if_tun.h>
bellard7d3505c2004-05-12 19:32:15 +000051#include <pty.h>
52#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000053#include <linux/rtc.h>
bellard67b915a2004-03-31 23:37:16 +000054#endif
bellard7d3505c2004-05-12 19:32:15 +000055#endif
bellard67b915a2004-03-31 23:37:16 +000056
bellardc20709a2004-04-21 23:27:19 +000057#if defined(CONFIG_SLIRP)
58#include "libslirp.h"
59#endif
60
bellard67b915a2004-03-31 23:37:16 +000061#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +000062#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +000063#include <sys/timeb.h>
64#include <windows.h>
65#define getopt_long_only getopt_long
66#define memalign(align, size) malloc(size)
67#endif
68
bellard73332e52004-04-04 20:22:28 +000069#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +000070#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +000071#include <SDL/SDL.h>
bellard96bcd4f2004-07-10 16:26:15 +000072#endif
bellard73332e52004-04-04 20:22:28 +000073#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +000074
bellard5b0753e2005-03-01 21:37:28 +000075#ifdef CONFIG_COCOA
76#undef main
77#define main qemu_main
78#endif /* CONFIG_COCOA */
79
bellard0824d6f2003-06-24 13:42:40 +000080#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +000081
bellard8a7ddc32004-03-31 19:00:16 +000082#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +000083
bellarda541f292004-04-12 20:39:29 +000084//#define DO_TB_FLUSH
85
bellard5a671352003-10-01 00:13:48 +000086#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
bellardf1510b22003-06-25 00:07:40 +000087
bellard0824d6f2003-06-24 13:42:40 +000088//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +000089//#define DEBUG_IOPORT
bellard330d0412003-07-26 18:11:40 +000090
bellardbb551fa2004-01-24 13:42:26 +000091#if !defined(CONFIG_SOFTMMU)
bellard7916e222003-07-01 16:27:45 +000092#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
bellardbb551fa2004-01-24 13:42:26 +000093#else
94#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
95#endif
bellard7916e222003-07-01 16:27:45 +000096
bellard77d4bc32004-05-26 22:13:53 +000097#ifdef TARGET_PPC
98#define DEFAULT_RAM_SIZE 144
99#else
bellard1bfe8562004-07-08 21:17:50 +0000100#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000101#endif
bellard8a7ddc32004-03-31 19:00:16 +0000102/* in ms */
103#define GUI_REFRESH_INTERVAL 30
bellard313aa562003-08-10 21:52:11 +0000104
bellard7dea1da2003-11-16 15:59:30 +0000105/* XXX: use a two level table to limit memory usage */
106#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000107
bellard80cabfa2004-03-14 12:20:30 +0000108const char *bios_dir = CONFIG_QEMU_SHAREDIR;
bellard0824d6f2003-06-24 13:42:40 +0000109char phys_ram_file[1024];
bellardc45886d2004-01-05 00:02:06 +0000110CPUState *global_env;
111CPUState *cpu_single_env;
bellardc4b1fcc2004-03-14 21:44:30 +0000112void *ioport_opaque[MAX_IOPORTS];
bellardfc01f7e2003-06-30 10:03:06 +0000113IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
114IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
bellardc45886d2004-01-05 00:02:06 +0000115BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
bellard313aa562003-08-10 21:52:11 +0000116int vga_ram_size;
bellard0ced6582004-05-23 21:06:12 +0000117int bios_size;
bellard313aa562003-08-10 21:52:11 +0000118static DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000119int nographic;
bellard3d11d0e2004-12-12 16:56:30 +0000120const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000121int64_t ticks_per_sec;
bellard36b486b2003-11-11 13:36:08 +0000122int boot_device = 'c';
bellard0ced6582004-05-23 21:06:12 +0000123int ram_size;
bellard80cabfa2004-03-14 12:20:30 +0000124static char network_script[1024];
125int pit_min_timer_count = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000126int nb_nics;
127NetDriverState nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000128QEMUTimer *gui_timer;
129int vm_running;
bellard1d14ffa2005-10-30 18:58:22 +0000130#ifdef HAS_AUDIO
bellardaaaa7df2004-04-26 20:56:53 +0000131int audio_enabled = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000132int sb16_enabled = 0;
133int adlib_enabled = 0;
134int gus_enabled = 0;
135int es1370_enabled = 0;
136#endif
bellardee22c2f2004-06-03 12:49:50 +0000137int rtc_utc = 1;
bellard1bfe8562004-07-08 21:17:50 +0000138int cirrus_vga_enabled = 1;
bellardd8272202005-04-06 20:32:23 +0000139#ifdef TARGET_SPARC
140int graphic_width = 1024;
141int graphic_height = 768;
142#else
bellard1bfe8562004-07-08 21:17:50 +0000143int graphic_width = 800;
144int graphic_height = 600;
bellardd8272202005-04-06 20:32:23 +0000145#endif
bellarde9b137c2004-06-21 16:46:10 +0000146int graphic_depth = 15;
bellardd63d3072004-10-03 13:29:03 +0000147int full_screen = 0;
bellard82c643f2004-07-14 17:28:13 +0000148TextConsole *vga_console;
bellard8d11df92004-08-24 21:13:40 +0000149CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000150CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000151#ifdef TARGET_I386
152int win2k_install_hack = 0;
153#endif
bellardbb36d472005-11-05 14:22:28 +0000154int usb_enabled = 0;
bellarda594cfb2005-11-06 16:13:29 +0000155USBPort *vm_usb_ports[MAX_VM_USB_PORTS];
156USBDevice *vm_usb_hub;
bellard0824d6f2003-06-24 13:42:40 +0000157
158/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000159/* x86 ISA bus support */
160
161target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000162PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000163
bellardc4b1fcc2004-03-14 21:44:30 +0000164uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000165{
166#ifdef DEBUG_UNUSED_IOPORT
167 fprintf(stderr, "inb: port=0x%04x\n", address);
168#endif
bellardfc01f7e2003-06-30 10:03:06 +0000169 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000170}
171
bellardc4b1fcc2004-03-14 21:44:30 +0000172void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000173{
174#ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
176#endif
177}
178
179/* default is to make two byte accesses */
bellardc4b1fcc2004-03-14 21:44:30 +0000180uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000181{
182 uint32_t data;
bellarddb45c292004-05-12 19:50:26 +0000183 data = ioport_read_table[0][address](ioport_opaque[address], address);
184 address = (address + 1) & (MAX_IOPORTS - 1);
185 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000186 return data;
187}
188
bellardc4b1fcc2004-03-14 21:44:30 +0000189void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000190{
bellarddb45c292004-05-12 19:50:26 +0000191 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
192 address = (address + 1) & (MAX_IOPORTS - 1);
193 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000194}
195
bellardc4b1fcc2004-03-14 21:44:30 +0000196uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000197{
198#ifdef DEBUG_UNUSED_IOPORT
199 fprintf(stderr, "inl: port=0x%04x\n", address);
200#endif
201 return 0xffffffff;
202}
203
bellardc4b1fcc2004-03-14 21:44:30 +0000204void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000205{
206#ifdef DEBUG_UNUSED_IOPORT
207 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
208#endif
bellard0824d6f2003-06-24 13:42:40 +0000209}
210
211void init_ioports(void)
212{
213 int i;
214
215 for(i = 0; i < MAX_IOPORTS; i++) {
bellardfc01f7e2003-06-30 10:03:06 +0000216 ioport_read_table[0][i] = default_ioport_readb;
217 ioport_write_table[0][i] = default_ioport_writeb;
218 ioport_read_table[1][i] = default_ioport_readw;
219 ioport_write_table[1][i] = default_ioport_writew;
220 ioport_read_table[2][i] = default_ioport_readl;
221 ioport_write_table[2][i] = default_ioport_writel;
bellard0824d6f2003-06-24 13:42:40 +0000222 }
223}
224
bellardfc01f7e2003-06-30 10:03:06 +0000225/* size is the word size in byte */
bellardc4b1fcc2004-03-14 21:44:30 +0000226int register_ioport_read(int start, int length, int size,
227 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000228{
bellardfc01f7e2003-06-30 10:03:06 +0000229 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000230
bellardc4b1fcc2004-03-14 21:44:30 +0000231 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000232 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000233 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000234 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000235 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000236 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000237 } else {
238 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000239 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000240 }
241 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000242 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000243 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
244 hw_error("register_ioport_read: invalid opaque");
245 ioport_opaque[i] = opaque;
246 }
bellard0824d6f2003-06-24 13:42:40 +0000247 return 0;
248}
249
bellardfc01f7e2003-06-30 10:03:06 +0000250/* size is the word size in byte */
bellardc4b1fcc2004-03-14 21:44:30 +0000251int register_ioport_write(int start, int length, int size,
252 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000253{
bellardfc01f7e2003-06-30 10:03:06 +0000254 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000255
bellardc4b1fcc2004-03-14 21:44:30 +0000256 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000257 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000258 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000259 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000260 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000261 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000262 } else {
263 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000264 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000265 }
266 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000267 ioport_write_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000268 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
269 hw_error("register_ioport_read: invalid opaque");
270 ioport_opaque[i] = opaque;
271 }
bellardf1510b22003-06-25 00:07:40 +0000272 return 0;
273}
274
bellard69b91032004-05-18 23:05:28 +0000275void isa_unassign_ioport(int start, int length)
276{
277 int i;
278
279 for(i = start; i < start + length; i++) {
280 ioport_read_table[0][i] = default_ioport_readb;
281 ioport_read_table[1][i] = default_ioport_readw;
282 ioport_read_table[2][i] = default_ioport_readl;
283
284 ioport_write_table[0][i] = default_ioport_writeb;
285 ioport_write_table[1][i] = default_ioport_writew;
286 ioport_write_table[2][i] = default_ioport_writel;
287 }
288}
289
bellard20f32282005-01-03 23:36:21 +0000290/***********************************************************/
291
bellard0824d6f2003-06-24 13:42:40 +0000292void pstrcpy(char *buf, int buf_size, const char *str)
293{
294 int c;
295 char *q = buf;
296
297 if (buf_size <= 0)
298 return;
299
300 for(;;) {
301 c = *str++;
302 if (c == 0 || q >= buf + buf_size - 1)
303 break;
304 *q++ = c;
305 }
306 *q = '\0';
307}
308
309/* strcat and truncate. */
310char *pstrcat(char *buf, int buf_size, const char *s)
311{
312 int len;
313 len = strlen(buf);
314 if (len < buf_size)
315 pstrcpy(buf + len, buf_size - len, s);
316 return buf;
317}
318
bellard82c643f2004-07-14 17:28:13 +0000319int strstart(const char *str, const char *val, const char **ptr)
320{
321 const char *p, *q;
322 p = str;
323 q = val;
324 while (*q != '\0') {
325 if (*p != *q)
326 return 0;
327 p++;
328 q++;
329 }
330 if (ptr)
331 *ptr = p;
332 return 1;
333}
334
bellard0824d6f2003-06-24 13:42:40 +0000335/* return the size or -1 if error */
bellard7587cf42004-06-20 13:43:27 +0000336int get_image_size(const char *filename)
337{
338 int fd, size;
339 fd = open(filename, O_RDONLY | O_BINARY);
340 if (fd < 0)
341 return -1;
342 size = lseek(fd, 0, SEEK_END);
343 close(fd);
344 return size;
345}
346
347/* return the size or -1 if error */
bellard0824d6f2003-06-24 13:42:40 +0000348int load_image(const char *filename, uint8_t *addr)
349{
350 int fd, size;
bellard40c3bac2004-04-04 12:56:28 +0000351 fd = open(filename, O_RDONLY | O_BINARY);
bellard0824d6f2003-06-24 13:42:40 +0000352 if (fd < 0)
353 return -1;
354 size = lseek(fd, 0, SEEK_END);
355 lseek(fd, 0, SEEK_SET);
356 if (read(fd, addr, size) != size) {
357 close(fd);
358 return -1;
359 }
360 close(fd);
361 return size;
362}
363
bellardc45886d2004-01-05 00:02:06 +0000364void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000365{
bellardfd872592004-05-12 19:11:15 +0000366#ifdef DEBUG_IOPORT
367 if (loglevel & CPU_LOG_IOPORT)
368 fprintf(logfile, "outb: %04x %02x\n", addr, val);
369#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000370 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
bellard0824d6f2003-06-24 13:42:40 +0000371}
372
bellardc45886d2004-01-05 00:02:06 +0000373void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000374{
bellardfd872592004-05-12 19:11:15 +0000375#ifdef DEBUG_IOPORT
376 if (loglevel & CPU_LOG_IOPORT)
377 fprintf(logfile, "outw: %04x %04x\n", addr, val);
378#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000379 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
bellard0824d6f2003-06-24 13:42:40 +0000380}
381
bellardc45886d2004-01-05 00:02:06 +0000382void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000383{
bellardfd872592004-05-12 19:11:15 +0000384#ifdef DEBUG_IOPORT
385 if (loglevel & CPU_LOG_IOPORT)
386 fprintf(logfile, "outl: %04x %08x\n", addr, val);
387#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000388 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
bellard0824d6f2003-06-24 13:42:40 +0000389}
390
bellardc45886d2004-01-05 00:02:06 +0000391int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000392{
bellardfd872592004-05-12 19:11:15 +0000393 int val;
bellardfd872592004-05-12 19:11:15 +0000394 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
395#ifdef DEBUG_IOPORT
396 if (loglevel & CPU_LOG_IOPORT)
397 fprintf(logfile, "inb : %04x %02x\n", addr, val);
398#endif
399 return val;
bellard0824d6f2003-06-24 13:42:40 +0000400}
401
bellardc45886d2004-01-05 00:02:06 +0000402int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000403{
bellardfd872592004-05-12 19:11:15 +0000404 int val;
bellardfd872592004-05-12 19:11:15 +0000405 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
406#ifdef DEBUG_IOPORT
407 if (loglevel & CPU_LOG_IOPORT)
408 fprintf(logfile, "inw : %04x %04x\n", addr, val);
409#endif
410 return val;
bellard0824d6f2003-06-24 13:42:40 +0000411}
412
bellardc45886d2004-01-05 00:02:06 +0000413int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000414{
bellardfd872592004-05-12 19:11:15 +0000415 int val;
bellardfd872592004-05-12 19:11:15 +0000416 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
417#ifdef DEBUG_IOPORT
418 if (loglevel & CPU_LOG_IOPORT)
419 fprintf(logfile, "inl : %04x %08x\n", addr, val);
420#endif
421 return val;
bellard0824d6f2003-06-24 13:42:40 +0000422}
423
424/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000425void hw_error(const char *fmt, ...)
426{
427 va_list ap;
428
429 va_start(ap, fmt);
430 fprintf(stderr, "qemu: hardware error: ");
431 vfprintf(stderr, fmt, ap);
432 fprintf(stderr, "\n");
433#ifdef TARGET_I386
bellard7fe48482004-10-09 18:08:01 +0000434 cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
bellardc45886d2004-01-05 00:02:06 +0000435#else
bellard7fe48482004-10-09 18:08:01 +0000436 cpu_dump_state(global_env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000437#endif
438 va_end(ap);
439 abort();
440}
441
bellard8a7ddc32004-03-31 19:00:16 +0000442/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000443/* keyboard/mouse */
444
445static QEMUPutKBDEvent *qemu_put_kbd_event;
446static void *qemu_put_kbd_event_opaque;
447static QEMUPutMouseEvent *qemu_put_mouse_event;
448static void *qemu_put_mouse_event_opaque;
449
450void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
451{
452 qemu_put_kbd_event_opaque = opaque;
453 qemu_put_kbd_event = func;
454}
455
456void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
457{
458 qemu_put_mouse_event_opaque = opaque;
459 qemu_put_mouse_event = func;
460}
461
462void kbd_put_keycode(int keycode)
463{
464 if (qemu_put_kbd_event) {
465 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
466 }
467}
468
469void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
470{
471 if (qemu_put_mouse_event) {
472 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
473 dx, dy, dz, buttons_state);
474 }
475}
476
477/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +0000478/* timers */
479
bellard34865132003-10-05 14:28:56 +0000480#if defined(__powerpc__)
481
482static inline uint32_t get_tbl(void)
483{
484 uint32_t tbl;
485 asm volatile("mftb %0" : "=r" (tbl));
486 return tbl;
487}
488
489static inline uint32_t get_tbu(void)
490{
491 uint32_t tbl;
492 asm volatile("mftbu %0" : "=r" (tbl));
493 return tbl;
494}
495
496int64_t cpu_get_real_ticks(void)
497{
498 uint32_t l, h, h1;
499 /* NOTE: we test if wrapping has occurred */
500 do {
501 h = get_tbu();
502 l = get_tbl();
503 h1 = get_tbu();
504 } while (h != h1);
505 return ((int64_t)h << 32) | l;
506}
507
508#elif defined(__i386__)
509
510int64_t cpu_get_real_ticks(void)
511{
512 int64_t val;
bellarde463b582004-04-02 20:57:58 +0000513 asm volatile ("rdtsc" : "=A" (val));
bellard34865132003-10-05 14:28:56 +0000514 return val;
515}
516
bellard1115dde2004-04-25 18:57:49 +0000517#elif defined(__x86_64__)
518
519int64_t cpu_get_real_ticks(void)
520{
521 uint32_t low,high;
522 int64_t val;
523 asm volatile("rdtsc" : "=a" (low), "=d" (high));
524 val = high;
525 val <<= 32;
526 val |= low;
527 return val;
528}
529
bellardb8076a72005-04-07 22:20:31 +0000530#elif defined(__ia64)
531
532int64_t cpu_get_real_ticks(void)
533{
534 int64_t val;
535 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
536 return val;
537}
538
bellard90cb9492005-07-24 15:11:38 +0000539#elif defined(__s390__)
540
541int64_t cpu_get_real_ticks(void)
542{
543 int64_t val;
544 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
545 return val;
546}
547
bellard34865132003-10-05 14:28:56 +0000548#else
549#error unsupported CPU
550#endif
551
552static int64_t cpu_ticks_offset;
bellard8a7ddc32004-03-31 19:00:16 +0000553static int cpu_ticks_enabled;
bellard34865132003-10-05 14:28:56 +0000554
bellard8a7ddc32004-03-31 19:00:16 +0000555static inline int64_t cpu_get_ticks(void)
bellard34865132003-10-05 14:28:56 +0000556{
bellard8a7ddc32004-03-31 19:00:16 +0000557 if (!cpu_ticks_enabled) {
558 return cpu_ticks_offset;
559 } else {
560 return cpu_get_real_ticks() + cpu_ticks_offset;
561 }
bellard34865132003-10-05 14:28:56 +0000562}
563
564/* enable cpu_get_ticks() */
565void cpu_enable_ticks(void)
566{
bellard8a7ddc32004-03-31 19:00:16 +0000567 if (!cpu_ticks_enabled) {
568 cpu_ticks_offset -= cpu_get_real_ticks();
569 cpu_ticks_enabled = 1;
570 }
bellard34865132003-10-05 14:28:56 +0000571}
572
573/* disable cpu_get_ticks() : the clock is stopped. You must not call
574 cpu_get_ticks() after that. */
575void cpu_disable_ticks(void)
576{
bellard8a7ddc32004-03-31 19:00:16 +0000577 if (cpu_ticks_enabled) {
578 cpu_ticks_offset = cpu_get_ticks();
579 cpu_ticks_enabled = 0;
580 }
bellard34865132003-10-05 14:28:56 +0000581}
582
bellard67b915a2004-03-31 23:37:16 +0000583static int64_t get_clock(void)
bellard0824d6f2003-06-24 13:42:40 +0000584{
bellard67b915a2004-03-31 23:37:16 +0000585#ifdef _WIN32
586 struct _timeb tb;
587 _ftime(&tb);
588 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
589#else
bellard0824d6f2003-06-24 13:42:40 +0000590 struct timeval tv;
591 gettimeofday(&tv, NULL);
592 return tv.tv_sec * 1000000LL + tv.tv_usec;
bellard67b915a2004-03-31 23:37:16 +0000593#endif
bellard0824d6f2003-06-24 13:42:40 +0000594}
595
bellard0824d6f2003-06-24 13:42:40 +0000596void cpu_calibrate_ticks(void)
597{
598 int64_t usec, ticks;
599
600 usec = get_clock();
bellard8a7ddc32004-03-31 19:00:16 +0000601 ticks = cpu_get_real_ticks();
bellard67b915a2004-03-31 23:37:16 +0000602#ifdef _WIN32
603 Sleep(50);
604#else
bellard0824d6f2003-06-24 13:42:40 +0000605 usleep(50 * 1000);
bellard67b915a2004-03-31 23:37:16 +0000606#endif
bellard0824d6f2003-06-24 13:42:40 +0000607 usec = get_clock() - usec;
bellard8a7ddc32004-03-31 19:00:16 +0000608 ticks = cpu_get_real_ticks() - ticks;
bellard0824d6f2003-06-24 13:42:40 +0000609 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
610}
611
bellard87858c82003-06-27 12:01:39 +0000612/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000613uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000614{
615 union {
616 uint64_t ll;
617 struct {
618#ifdef WORDS_BIGENDIAN
619 uint32_t high, low;
620#else
621 uint32_t low, high;
622#endif
623 } l;
624 } u, res;
625 uint64_t rl, rh;
626
627 u.ll = a;
628 rl = (uint64_t)u.l.low * (uint64_t)b;
629 rh = (uint64_t)u.l.high * (uint64_t)b;
630 rh += (rl >> 32);
631 res.l.high = rh / c;
632 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
633 return res.ll;
634}
635
bellard8a7ddc32004-03-31 19:00:16 +0000636#define QEMU_TIMER_REALTIME 0
637#define QEMU_TIMER_VIRTUAL 1
638
639struct QEMUClock {
640 int type;
641 /* XXX: add frequency */
642};
643
644struct QEMUTimer {
645 QEMUClock *clock;
646 int64_t expire_time;
647 QEMUTimerCB *cb;
648 void *opaque;
649 struct QEMUTimer *next;
650};
651
652QEMUClock *rt_clock;
653QEMUClock *vm_clock;
654
655static QEMUTimer *active_timers[2];
bellard40c3bac2004-04-04 12:56:28 +0000656#ifdef _WIN32
657static MMRESULT timerID;
658#else
bellard8a7ddc32004-03-31 19:00:16 +0000659/* frequency of the times() clock tick */
660static int timer_freq;
bellard67b915a2004-03-31 23:37:16 +0000661#endif
bellard8a7ddc32004-03-31 19:00:16 +0000662
663QEMUClock *qemu_new_clock(int type)
664{
665 QEMUClock *clock;
666 clock = qemu_mallocz(sizeof(QEMUClock));
667 if (!clock)
668 return NULL;
669 clock->type = type;
670 return clock;
671}
672
673QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
674{
675 QEMUTimer *ts;
676
677 ts = qemu_mallocz(sizeof(QEMUTimer));
678 ts->clock = clock;
679 ts->cb = cb;
680 ts->opaque = opaque;
681 return ts;
682}
683
684void qemu_free_timer(QEMUTimer *ts)
685{
686 qemu_free(ts);
687}
688
689/* stop a timer, but do not dealloc it */
690void qemu_del_timer(QEMUTimer *ts)
691{
692 QEMUTimer **pt, *t;
693
694 /* NOTE: this code must be signal safe because
695 qemu_timer_expired() can be called from a signal. */
696 pt = &active_timers[ts->clock->type];
697 for(;;) {
698 t = *pt;
699 if (!t)
700 break;
701 if (t == ts) {
702 *pt = t->next;
703 break;
704 }
705 pt = &t->next;
706 }
707}
708
709/* modify the current timer so that it will be fired when current_time
710 >= expire_time. The corresponding callback will be called. */
711void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
712{
713 QEMUTimer **pt, *t;
714
715 qemu_del_timer(ts);
716
717 /* add the timer in the sorted list */
718 /* NOTE: this code must be signal safe because
719 qemu_timer_expired() can be called from a signal. */
720 pt = &active_timers[ts->clock->type];
721 for(;;) {
722 t = *pt;
723 if (!t)
724 break;
725 if (t->expire_time > expire_time)
726 break;
727 pt = &t->next;
728 }
729 ts->expire_time = expire_time;
730 ts->next = *pt;
731 *pt = ts;
732}
733
734int qemu_timer_pending(QEMUTimer *ts)
735{
736 QEMUTimer *t;
737 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
738 if (t == ts)
739 return 1;
740 }
741 return 0;
742}
743
744static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
745{
746 if (!timer_head)
747 return 0;
748 return (timer_head->expire_time <= current_time);
749}
750
751static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
752{
753 QEMUTimer *ts;
754
755 for(;;) {
756 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +0000757 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +0000758 break;
759 /* remove timer from the list before calling the callback */
760 *ptimer_head = ts->next;
761 ts->next = NULL;
762
763 /* run the callback (the timer list can be modified) */
764 ts->cb(ts->opaque);
765 }
766}
767
768int64_t qemu_get_clock(QEMUClock *clock)
769{
770 switch(clock->type) {
771 case QEMU_TIMER_REALTIME:
bellard67b915a2004-03-31 23:37:16 +0000772#ifdef _WIN32
773 return GetTickCount();
774#else
bellard7d3505c2004-05-12 19:32:15 +0000775 {
776 struct tms tp;
777
778 /* Note that using gettimeofday() is not a good solution
779 for timers because its value change when the date is
780 modified. */
781 if (timer_freq == 100) {
782 return times(&tp) * 10;
783 } else {
784 return ((int64_t)times(&tp) * 1000) / timer_freq;
785 }
bellard8a7ddc32004-03-31 19:00:16 +0000786 }
bellard67b915a2004-03-31 23:37:16 +0000787#endif
bellard8a7ddc32004-03-31 19:00:16 +0000788 default:
789 case QEMU_TIMER_VIRTUAL:
790 return cpu_get_ticks();
791 }
792}
793
794/* save a timer */
795void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
796{
797 uint64_t expire_time;
798
799 if (qemu_timer_pending(ts)) {
800 expire_time = ts->expire_time;
801 } else {
802 expire_time = -1;
803 }
804 qemu_put_be64(f, expire_time);
805}
806
807void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
808{
809 uint64_t expire_time;
810
811 expire_time = qemu_get_be64(f);
812 if (expire_time != -1) {
813 qemu_mod_timer(ts, expire_time);
814 } else {
815 qemu_del_timer(ts);
816 }
817}
818
819static void timer_save(QEMUFile *f, void *opaque)
820{
821 if (cpu_ticks_enabled) {
822 hw_error("cannot save state if virtual timers are running");
823 }
824 qemu_put_be64s(f, &cpu_ticks_offset);
825 qemu_put_be64s(f, &ticks_per_sec);
826}
827
828static int timer_load(QEMUFile *f, void *opaque, int version_id)
829{
830 if (version_id != 1)
831 return -EINVAL;
832 if (cpu_ticks_enabled) {
833 return -EINVAL;
834 }
835 qemu_get_be64s(f, &cpu_ticks_offset);
836 qemu_get_be64s(f, &ticks_per_sec);
837 return 0;
838}
839
bellard67b915a2004-03-31 23:37:16 +0000840#ifdef _WIN32
841void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
842 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
843#else
bellard8a7ddc32004-03-31 19:00:16 +0000844static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +0000845#endif
bellard8a7ddc32004-03-31 19:00:16 +0000846{
bellard02ba45c2004-06-25 14:46:23 +0000847#if 0
848#define DISP_FREQ 1000
849 {
850 static int64_t delta_min = INT64_MAX;
851 static int64_t delta_max, delta_cum, last_clock, delta, ti;
852 static int count;
853 ti = qemu_get_clock(vm_clock);
854 if (last_clock != 0) {
855 delta = ti - last_clock;
856 if (delta < delta_min)
857 delta_min = delta;
858 if (delta > delta_max)
859 delta_max = delta;
860 delta_cum += delta;
861 if (++count == DISP_FREQ) {
862 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
863 muldiv64(delta_min, 1000000, ticks_per_sec),
864 muldiv64(delta_max, 1000000, ticks_per_sec),
865 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
866 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
867 count = 0;
868 delta_min = INT64_MAX;
869 delta_max = 0;
870 delta_cum = 0;
871 }
872 }
873 last_clock = ti;
874 }
875#endif
bellard8a7ddc32004-03-31 19:00:16 +0000876 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
877 qemu_get_clock(vm_clock)) ||
878 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
879 qemu_get_clock(rt_clock))) {
880 /* stop the cpu because a timer occured */
881 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +0000882#ifdef USE_KQEMU
bellard82893362005-09-03 21:33:43 +0000883 if (global_env->kqemu_enabled) {
884 kqemu_cpu_interrupt(global_env);
885 }
bellarda332e112005-09-03 17:55:47 +0000886#endif
bellard8a7ddc32004-03-31 19:00:16 +0000887 }
888}
889
bellardfd872592004-05-12 19:11:15 +0000890#ifndef _WIN32
891
bellard829309c2004-05-20 13:20:12 +0000892#if defined(__linux__)
893
bellardfd872592004-05-12 19:11:15 +0000894#define RTC_FREQ 1024
895
896static int rtc_fd;
bellard829309c2004-05-20 13:20:12 +0000897
bellardfd872592004-05-12 19:11:15 +0000898static int start_rtc_timer(void)
899{
900 rtc_fd = open("/dev/rtc", O_RDONLY);
901 if (rtc_fd < 0)
902 return -1;
903 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
904 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
905 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
906 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
907 goto fail;
908 }
909 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
910 fail:
911 close(rtc_fd);
912 return -1;
913 }
914 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
915 return 0;
916}
917
bellard829309c2004-05-20 13:20:12 +0000918#else
919
920static int start_rtc_timer(void)
921{
922 return -1;
923}
924
925#endif /* !defined(__linux__) */
926
927#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +0000928
bellard8a7ddc32004-03-31 19:00:16 +0000929static void init_timers(void)
930{
bellard8a7ddc32004-03-31 19:00:16 +0000931 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
932 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
933
bellard67b915a2004-03-31 23:37:16 +0000934#ifdef _WIN32
935 {
936 int count=0;
bellard1d14ffa2005-10-30 18:58:22 +0000937 timerID = timeSetEvent(1, // interval (ms)
bellard40c3bac2004-04-04 12:56:28 +0000938 0, // resolution
939 host_alarm_handler, // function
940 (DWORD)&count, // user parameter
941 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
bellard67b915a2004-03-31 23:37:16 +0000942 if( !timerID ) {
943 perror("failed timer alarm");
944 exit(1);
945 }
946 }
947 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
948#else
949 {
950 struct sigaction act;
951 struct itimerval itv;
952
953 /* get times() syscall frequency */
954 timer_freq = sysconf(_SC_CLK_TCK);
955
956 /* timer signal */
957 sigfillset(&act.sa_mask);
bellarda09db212005-04-30 16:10:35 +0000958 act.sa_flags = 0;
bellard8a7ddc32004-03-31 19:00:16 +0000959#if defined (TARGET_I386) && defined(USE_CODE_COPY)
bellard67b915a2004-03-31 23:37:16 +0000960 act.sa_flags |= SA_ONSTACK;
bellard8a7ddc32004-03-31 19:00:16 +0000961#endif
bellard67b915a2004-03-31 23:37:16 +0000962 act.sa_handler = host_alarm_handler;
963 sigaction(SIGALRM, &act, NULL);
bellardfd872592004-05-12 19:11:15 +0000964
bellard67b915a2004-03-31 23:37:16 +0000965 itv.it_interval.tv_sec = 0;
bellardd79284e2005-01-26 21:56:26 +0000966 itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */
bellard67b915a2004-03-31 23:37:16 +0000967 itv.it_value.tv_sec = 0;
968 itv.it_value.tv_usec = 10 * 1000;
969 setitimer(ITIMER_REAL, &itv, NULL);
970 /* we probe the tick duration of the kernel to inform the user if
971 the emulated kernel requested a too high timer frequency */
972 getitimer(ITIMER_REAL, &itv);
bellardfd872592004-05-12 19:11:15 +0000973
bellard83fb7ad2004-07-05 21:25:26 +0000974#if defined(__linux__)
bellardfd872592004-05-12 19:11:15 +0000975 if (itv.it_interval.tv_usec > 1000) {
976 /* try to use /dev/rtc to have a faster timer */
977 if (start_rtc_timer() < 0)
978 goto use_itimer;
979 /* disable itimer */
980 itv.it_interval.tv_sec = 0;
981 itv.it_interval.tv_usec = 0;
982 itv.it_value.tv_sec = 0;
983 itv.it_value.tv_usec = 0;
984 setitimer(ITIMER_REAL, &itv, NULL);
985
986 /* use the RTC */
bellarda1968d72004-06-25 17:06:27 +0000987 sigaction(SIGIO, &act, NULL);
bellardfd872592004-05-12 19:11:15 +0000988 fcntl(rtc_fd, F_SETFL, O_ASYNC);
989 fcntl(rtc_fd, F_SETOWN, getpid());
bellard83fb7ad2004-07-05 21:25:26 +0000990 } else
991#endif /* defined(__linux__) */
992 {
bellardfd872592004-05-12 19:11:15 +0000993 use_itimer:
994 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
995 PIT_FREQ) / 1000000;
996 }
bellard67b915a2004-03-31 23:37:16 +0000997 }
998#endif
bellard8a7ddc32004-03-31 19:00:16 +0000999}
1000
bellard40c3bac2004-04-04 12:56:28 +00001001void quit_timers(void)
1002{
1003#ifdef _WIN32
1004 timeKillEvent(timerID);
1005#endif
1006}
1007
bellardc4b1fcc2004-03-14 21:44:30 +00001008/***********************************************************/
bellard82c643f2004-07-14 17:28:13 +00001009/* character device */
bellardc45886d2004-01-05 00:02:06 +00001010
bellard82c643f2004-07-14 17:28:13 +00001011int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
bellard67b915a2004-03-31 23:37:16 +00001012{
bellard82c643f2004-07-14 17:28:13 +00001013 return s->chr_write(s, buf, len);
bellard67b915a2004-03-31 23:37:16 +00001014}
1015
bellard82c643f2004-07-14 17:28:13 +00001016void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
bellard0824d6f2003-06-24 13:42:40 +00001017{
bellard82c643f2004-07-14 17:28:13 +00001018 char buf[4096];
1019 va_list ap;
1020 va_start(ap, fmt);
1021 vsnprintf(buf, sizeof(buf), fmt, ap);
1022 qemu_chr_write(s, buf, strlen(buf));
1023 va_end(ap);
1024}
1025
bellard5905b2e2004-08-01 21:53:26 +00001026void qemu_chr_send_event(CharDriverState *s, int event)
1027{
1028 if (s->chr_send_event)
1029 s->chr_send_event(s, event);
1030}
1031
bellard82c643f2004-07-14 17:28:13 +00001032void qemu_chr_add_read_handler(CharDriverState *s,
1033 IOCanRWHandler *fd_can_read,
1034 IOReadHandler *fd_read, void *opaque)
1035{
1036 s->chr_add_read_handler(s, fd_can_read, fd_read, opaque);
1037}
1038
1039void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
1040{
1041 s->chr_event = chr_event;
1042}
1043
1044static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1045{
1046 return len;
1047}
1048
1049static void null_chr_add_read_handler(CharDriverState *chr,
1050 IOCanRWHandler *fd_can_read,
1051 IOReadHandler *fd_read, void *opaque)
1052{
1053}
1054
1055CharDriverState *qemu_chr_open_null(void)
1056{
1057 CharDriverState *chr;
1058
1059 chr = qemu_mallocz(sizeof(CharDriverState));
1060 if (!chr)
1061 return NULL;
1062 chr->chr_write = null_chr_write;
1063 chr->chr_add_read_handler = null_chr_add_read_handler;
1064 return chr;
1065}
1066
1067#ifndef _WIN32
1068
1069typedef struct {
1070 int fd_in, fd_out;
1071 /* for nographic stdio only */
1072 IOCanRWHandler *fd_can_read;
1073 IOReadHandler *fd_read;
1074 void *fd_opaque;
1075} FDCharDriver;
1076
1077#define STDIO_MAX_CLIENTS 2
1078
1079static int stdio_nb_clients;
1080static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
1081
bellard1d969052004-09-18 19:34:39 +00001082static int unix_write(int fd, const uint8_t *buf, int len1)
1083{
1084 int ret, len;
1085
1086 len = len1;
1087 while (len > 0) {
1088 ret = write(fd, buf, len);
1089 if (ret < 0) {
1090 if (errno != EINTR && errno != EAGAIN)
1091 return -1;
1092 } else if (ret == 0) {
1093 break;
1094 } else {
1095 buf += ret;
1096 len -= ret;
1097 }
1098 }
1099 return len1 - len;
1100}
1101
bellard82c643f2004-07-14 17:28:13 +00001102static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1103{
1104 FDCharDriver *s = chr->opaque;
bellard1d969052004-09-18 19:34:39 +00001105 return unix_write(s->fd_out, buf, len);
bellard82c643f2004-07-14 17:28:13 +00001106}
1107
1108static void fd_chr_add_read_handler(CharDriverState *chr,
1109 IOCanRWHandler *fd_can_read,
1110 IOReadHandler *fd_read, void *opaque)
1111{
1112 FDCharDriver *s = chr->opaque;
1113
1114 if (nographic && s->fd_in == 0) {
1115 s->fd_can_read = fd_can_read;
1116 s->fd_read = fd_read;
1117 s->fd_opaque = opaque;
bellard0824d6f2003-06-24 13:42:40 +00001118 } else {
bellard82c643f2004-07-14 17:28:13 +00001119 qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque);
bellard0824d6f2003-06-24 13:42:40 +00001120 }
1121}
1122
bellard82c643f2004-07-14 17:28:13 +00001123/* open a character device to a unix fd */
1124CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
1125{
1126 CharDriverState *chr;
1127 FDCharDriver *s;
1128
1129 chr = qemu_mallocz(sizeof(CharDriverState));
1130 if (!chr)
1131 return NULL;
1132 s = qemu_mallocz(sizeof(FDCharDriver));
1133 if (!s) {
1134 free(chr);
1135 return NULL;
1136 }
1137 s->fd_in = fd_in;
1138 s->fd_out = fd_out;
1139 chr->opaque = s;
1140 chr->chr_write = fd_chr_write;
1141 chr->chr_add_read_handler = fd_chr_add_read_handler;
1142 return chr;
1143}
1144
1145/* for STDIO, we handle the case where several clients use it
1146 (nographic mode) */
1147
1148#define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1149
bellardaa0bc6b2005-09-03 15:28:58 +00001150#define TERM_FIFO_MAX_SIZE 1
1151
bellard82c643f2004-07-14 17:28:13 +00001152static int term_got_escape, client_index;
bellardaa0bc6b2005-09-03 15:28:58 +00001153static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
1154int term_fifo_size;
bellard82c643f2004-07-14 17:28:13 +00001155
1156void term_print_help(void)
1157{
1158 printf("\n"
1159 "C-a h print this help\n"
1160 "C-a x exit emulator\n"
1161 "C-a s save disk data back to file (if -snapshot)\n"
1162 "C-a b send break (magic sysrq)\n"
1163 "C-a c switch between console and monitor\n"
1164 "C-a C-a send C-a\n"
1165 );
1166}
1167
1168/* called when a char is received */
1169static void stdio_received_byte(int ch)
1170{
1171 if (term_got_escape) {
1172 term_got_escape = 0;
1173 switch(ch) {
1174 case 'h':
1175 term_print_help();
1176 break;
1177 case 'x':
1178 exit(0);
1179 break;
1180 case 's':
1181 {
1182 int i;
1183 for (i = 0; i < MAX_DISKS; i++) {
1184 if (bs_table[i])
1185 bdrv_commit(bs_table[i]);
1186 }
1187 }
1188 break;
1189 case 'b':
1190 if (client_index < stdio_nb_clients) {
1191 CharDriverState *chr;
1192 FDCharDriver *s;
1193
1194 chr = stdio_clients[client_index];
1195 s = chr->opaque;
1196 chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK);
1197 }
1198 break;
1199 case 'c':
1200 client_index++;
1201 if (client_index >= stdio_nb_clients)
1202 client_index = 0;
1203 if (client_index == 0) {
1204 /* send a new line in the monitor to get the prompt */
1205 ch = '\r';
1206 goto send_char;
1207 }
1208 break;
1209 case TERM_ESCAPE:
1210 goto send_char;
1211 }
1212 } else if (ch == TERM_ESCAPE) {
1213 term_got_escape = 1;
1214 } else {
1215 send_char:
1216 if (client_index < stdio_nb_clients) {
1217 uint8_t buf[1];
1218 CharDriverState *chr;
1219 FDCharDriver *s;
1220
1221 chr = stdio_clients[client_index];
1222 s = chr->opaque;
bellardaa0bc6b2005-09-03 15:28:58 +00001223 if (s->fd_can_read(s->fd_opaque) > 0) {
1224 buf[0] = ch;
bellard82c643f2004-07-14 17:28:13 +00001225 s->fd_read(s->fd_opaque, buf, 1);
bellardaa0bc6b2005-09-03 15:28:58 +00001226 } else if (term_fifo_size == 0) {
1227 term_fifo[term_fifo_size++] = ch;
1228 }
bellard82c643f2004-07-14 17:28:13 +00001229 }
1230 }
1231}
1232
1233static int stdio_can_read(void *opaque)
1234{
bellardaa0bc6b2005-09-03 15:28:58 +00001235 CharDriverState *chr;
1236 FDCharDriver *s;
1237
1238 if (client_index < stdio_nb_clients) {
1239 chr = stdio_clients[client_index];
1240 s = chr->opaque;
1241 /* try to flush the queue if needed */
1242 if (term_fifo_size != 0 && s->fd_can_read(s->fd_opaque) > 0) {
1243 s->fd_read(s->fd_opaque, term_fifo, 1);
1244 term_fifo_size = 0;
1245 }
1246 /* see if we can absorb more chars */
1247 if (term_fifo_size == 0)
1248 return 1;
1249 else
1250 return 0;
1251 } else {
1252 return 1;
1253 }
bellard82c643f2004-07-14 17:28:13 +00001254}
1255
1256static void stdio_read(void *opaque, const uint8_t *buf, int size)
1257{
1258 int i;
1259 for(i = 0; i < size; i++)
1260 stdio_received_byte(buf[i]);
1261}
1262
bellard8d11df92004-08-24 21:13:40 +00001263/* init terminal so that we can grab keys */
1264static struct termios oldtty;
1265static int old_fd0_flags;
1266
1267static void term_exit(void)
1268{
1269 tcsetattr (0, TCSANOW, &oldtty);
1270 fcntl(0, F_SETFL, old_fd0_flags);
1271}
1272
1273static void term_init(void)
1274{
1275 struct termios tty;
1276
1277 tcgetattr (0, &tty);
1278 oldtty = tty;
1279 old_fd0_flags = fcntl(0, F_GETFL);
1280
1281 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1282 |INLCR|IGNCR|ICRNL|IXON);
1283 tty.c_oflag |= OPOST;
1284 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1285 /* if graphical mode, we allow Ctrl-C handling */
1286 if (nographic)
1287 tty.c_lflag &= ~ISIG;
1288 tty.c_cflag &= ~(CSIZE|PARENB);
1289 tty.c_cflag |= CS8;
1290 tty.c_cc[VMIN] = 1;
1291 tty.c_cc[VTIME] = 0;
1292
1293 tcsetattr (0, TCSANOW, &tty);
1294
1295 atexit(term_exit);
1296
1297 fcntl(0, F_SETFL, O_NONBLOCK);
1298}
1299
bellard82c643f2004-07-14 17:28:13 +00001300CharDriverState *qemu_chr_open_stdio(void)
1301{
1302 CharDriverState *chr;
1303
1304 if (nographic) {
1305 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
1306 return NULL;
1307 chr = qemu_chr_open_fd(0, 1);
1308 if (stdio_nb_clients == 0)
1309 qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL);
1310 client_index = stdio_nb_clients;
1311 } else {
1312 if (stdio_nb_clients != 0)
1313 return NULL;
1314 chr = qemu_chr_open_fd(0, 1);
1315 }
1316 stdio_clients[stdio_nb_clients++] = chr;
bellard8d11df92004-08-24 21:13:40 +00001317 if (stdio_nb_clients == 1) {
1318 /* set the terminal in raw mode */
1319 term_init();
1320 }
bellard82c643f2004-07-14 17:28:13 +00001321 return chr;
1322}
1323
1324#if defined(__linux__)
1325CharDriverState *qemu_chr_open_pty(void)
1326{
1327 char slave_name[1024];
1328 int master_fd, slave_fd;
1329
1330 /* Not satisfying */
1331 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
1332 return NULL;
1333 }
1334 fprintf(stderr, "char device redirected to %s\n", slave_name);
1335 return qemu_chr_open_fd(master_fd, master_fd);
1336}
1337#else
1338CharDriverState *qemu_chr_open_pty(void)
1339{
1340 return NULL;
1341}
bellard67b915a2004-03-31 23:37:16 +00001342#endif
1343
bellard82c643f2004-07-14 17:28:13 +00001344#endif /* !defined(_WIN32) */
1345
1346CharDriverState *qemu_chr_open(const char *filename)
1347{
1348 if (!strcmp(filename, "vc")) {
1349 return text_console_init(&display_state);
1350 } else if (!strcmp(filename, "null")) {
1351 return qemu_chr_open_null();
1352 } else
1353#ifndef _WIN32
1354 if (!strcmp(filename, "pty")) {
1355 return qemu_chr_open_pty();
1356 } else if (!strcmp(filename, "stdio")) {
1357 return qemu_chr_open_stdio();
1358 } else
1359#endif
1360 {
1361 return NULL;
1362 }
1363}
1364
bellardf1510b22003-06-25 00:07:40 +00001365/***********************************************************/
bellardc20709a2004-04-21 23:27:19 +00001366/* Linux network device redirectors */
bellardf1510b22003-06-25 00:07:40 +00001367
bellardc20709a2004-04-21 23:27:19 +00001368void hex_dump(FILE *f, const uint8_t *buf, int size)
bellard67b915a2004-03-31 23:37:16 +00001369{
bellardc20709a2004-04-21 23:27:19 +00001370 int len, i, j, c;
1371
1372 for(i=0;i<size;i+=16) {
1373 len = size - i;
1374 if (len > 16)
1375 len = 16;
1376 fprintf(f, "%08x ", i);
1377 for(j=0;j<16;j++) {
1378 if (j < len)
1379 fprintf(f, " %02x", buf[i+j]);
1380 else
1381 fprintf(f, " ");
1382 }
1383 fprintf(f, " ");
1384 for(j=0;j<len;j++) {
1385 c = buf[i+j];
1386 if (c < ' ' || c > '~')
1387 c = '.';
1388 fprintf(f, "%c", c);
1389 }
1390 fprintf(f, "\n");
1391 }
1392}
1393
1394void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1395{
1396 nd->send_packet(nd, buf, size);
1397}
1398
1399void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1400 IOReadHandler *fd_read, void *opaque)
1401{
1402 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1403}
1404
1405/* dummy network adapter */
1406
1407static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1408{
1409}
1410
1411static void dummy_add_read_packet(NetDriverState *nd,
1412 IOCanRWHandler *fd_can_read,
1413 IOReadHandler *fd_read, void *opaque)
1414{
1415}
1416
1417static int net_dummy_init(NetDriverState *nd)
1418{
1419 nd->send_packet = dummy_send_packet;
1420 nd->add_read_packet = dummy_add_read_packet;
1421 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
bellard67b915a2004-03-31 23:37:16 +00001422 return 0;
1423}
1424
bellardc20709a2004-04-21 23:27:19 +00001425#if defined(CONFIG_SLIRP)
1426
1427/* slirp network adapter */
1428
1429static void *slirp_fd_opaque;
1430static IOCanRWHandler *slirp_fd_can_read;
1431static IOReadHandler *slirp_fd_read;
1432static int slirp_inited;
1433
1434int slirp_can_output(void)
bellard67b915a2004-03-31 23:37:16 +00001435{
bellardc20709a2004-04-21 23:27:19 +00001436 return slirp_fd_can_read(slirp_fd_opaque);
bellard67b915a2004-03-31 23:37:16 +00001437}
1438
bellardc20709a2004-04-21 23:27:19 +00001439void slirp_output(const uint8_t *pkt, int pkt_len)
1440{
1441#if 0
1442 printf("output:\n");
1443 hex_dump(stdout, pkt, pkt_len);
1444#endif
1445 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1446}
1447
1448static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1449{
1450#if 0
1451 printf("input:\n");
1452 hex_dump(stdout, buf, size);
1453#endif
1454 slirp_input(buf, size);
1455}
1456
1457static void slirp_add_read_packet(NetDriverState *nd,
1458 IOCanRWHandler *fd_can_read,
1459 IOReadHandler *fd_read, void *opaque)
1460{
1461 slirp_fd_opaque = opaque;
1462 slirp_fd_can_read = fd_can_read;
1463 slirp_fd_read = fd_read;
1464}
1465
1466static int net_slirp_init(NetDriverState *nd)
1467{
1468 if (!slirp_inited) {
1469 slirp_inited = 1;
1470 slirp_init();
1471 }
1472 nd->send_packet = slirp_send_packet;
1473 nd->add_read_packet = slirp_add_read_packet;
1474 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1475 return 0;
1476}
1477
bellard9bf05442004-08-25 22:12:49 +00001478static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
1479{
1480 const char *p, *p1;
1481 int len;
1482 p = *pp;
1483 p1 = strchr(p, sep);
1484 if (!p1)
1485 return -1;
1486 len = p1 - p;
1487 p1++;
1488 if (buf_size > 0) {
1489 if (len > buf_size - 1)
1490 len = buf_size - 1;
1491 memcpy(buf, p, len);
1492 buf[len] = '\0';
1493 }
1494 *pp = p1;
1495 return 0;
1496}
1497
1498static void net_slirp_redir(const char *redir_str)
1499{
1500 int is_udp;
1501 char buf[256], *r;
1502 const char *p;
1503 struct in_addr guest_addr;
1504 int host_port, guest_port;
1505
1506 if (!slirp_inited) {
1507 slirp_inited = 1;
1508 slirp_init();
1509 }
1510
1511 p = redir_str;
1512 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1513 goto fail;
1514 if (!strcmp(buf, "tcp")) {
1515 is_udp = 0;
1516 } else if (!strcmp(buf, "udp")) {
1517 is_udp = 1;
1518 } else {
1519 goto fail;
1520 }
1521
1522 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1523 goto fail;
1524 host_port = strtol(buf, &r, 0);
1525 if (r == buf)
1526 goto fail;
1527
1528 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
1529 goto fail;
1530 if (buf[0] == '\0') {
1531 pstrcpy(buf, sizeof(buf), "10.0.2.15");
1532 }
1533 if (!inet_aton(buf, &guest_addr))
1534 goto fail;
1535
1536 guest_port = strtol(p, &r, 0);
1537 if (r == p)
1538 goto fail;
1539
1540 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
1541 fprintf(stderr, "qemu: could not set up redirection\n");
1542 exit(1);
1543 }
1544 return;
1545 fail:
1546 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1547 exit(1);
1548}
bellard9d728e82004-09-05 23:09:03 +00001549
bellardc94c8d62004-09-13 21:37:34 +00001550#ifndef _WIN32
1551
bellard9d728e82004-09-05 23:09:03 +00001552char smb_dir[1024];
1553
1554static void smb_exit(void)
1555{
1556 DIR *d;
1557 struct dirent *de;
1558 char filename[1024];
1559
1560 /* erase all the files in the directory */
1561 d = opendir(smb_dir);
1562 for(;;) {
1563 de = readdir(d);
1564 if (!de)
1565 break;
1566 if (strcmp(de->d_name, ".") != 0 &&
1567 strcmp(de->d_name, "..") != 0) {
1568 snprintf(filename, sizeof(filename), "%s/%s",
1569 smb_dir, de->d_name);
1570 unlink(filename);
1571 }
1572 }
bellard03ffbb62004-09-06 00:14:04 +00001573 closedir(d);
bellard9d728e82004-09-05 23:09:03 +00001574 rmdir(smb_dir);
1575}
1576
1577/* automatic user mode samba server configuration */
1578void net_slirp_smb(const char *exported_dir)
1579{
1580 char smb_conf[1024];
1581 char smb_cmdline[1024];
1582 FILE *f;
1583
1584 if (!slirp_inited) {
1585 slirp_inited = 1;
1586 slirp_init();
1587 }
1588
1589 /* XXX: better tmp dir construction */
1590 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
1591 if (mkdir(smb_dir, 0700) < 0) {
1592 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
1593 exit(1);
1594 }
1595 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
1596
1597 f = fopen(smb_conf, "w");
1598 if (!f) {
1599 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
1600 exit(1);
1601 }
1602 fprintf(f,
1603 "[global]\n"
bellard157777e2005-03-01 21:28:45 +00001604 "private dir=%s\n"
1605 "smb ports=0\n"
1606 "socket address=127.0.0.1\n"
bellard9d728e82004-09-05 23:09:03 +00001607 "pid directory=%s\n"
1608 "lock directory=%s\n"
1609 "log file=%s/log.smbd\n"
1610 "smb passwd file=%s/smbpasswd\n"
bellard03ffbb62004-09-06 00:14:04 +00001611 "security = share\n"
bellard9d728e82004-09-05 23:09:03 +00001612 "[qemu]\n"
1613 "path=%s\n"
1614 "read only=no\n"
1615 "guest ok=yes\n",
1616 smb_dir,
1617 smb_dir,
1618 smb_dir,
1619 smb_dir,
bellard157777e2005-03-01 21:28:45 +00001620 smb_dir,
bellard9d728e82004-09-05 23:09:03 +00001621 exported_dir
1622 );
1623 fclose(f);
1624 atexit(smb_exit);
1625
1626 snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s",
1627 smb_conf);
1628
1629 slirp_add_exec(0, smb_cmdline, 4, 139);
1630}
bellard9bf05442004-08-25 22:12:49 +00001631
bellardc94c8d62004-09-13 21:37:34 +00001632#endif /* !defined(_WIN32) */
1633
bellardc20709a2004-04-21 23:27:19 +00001634#endif /* CONFIG_SLIRP */
1635
1636#if !defined(_WIN32)
bellard7d3505c2004-05-12 19:32:15 +00001637#ifdef _BSD
1638static int tun_open(char *ifname, int ifname_size)
1639{
1640 int fd;
1641 char *dev;
1642 struct stat s;
bellard67b915a2004-03-31 23:37:16 +00001643
bellard7d3505c2004-05-12 19:32:15 +00001644 fd = open("/dev/tap", O_RDWR);
1645 if (fd < 0) {
1646 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1647 return -1;
1648 }
1649
1650 fstat(fd, &s);
1651 dev = devname(s.st_rdev, S_IFCHR);
1652 pstrcpy(ifname, ifname_size, dev);
1653
1654 fcntl(fd, F_SETFL, O_NONBLOCK);
1655 return fd;
1656}
1657#else
bellardc4b1fcc2004-03-14 21:44:30 +00001658static int tun_open(char *ifname, int ifname_size)
bellardf1510b22003-06-25 00:07:40 +00001659{
1660 struct ifreq ifr;
bellardc4b1fcc2004-03-14 21:44:30 +00001661 int fd, ret;
bellardf1510b22003-06-25 00:07:40 +00001662
1663 fd = open("/dev/net/tun", O_RDWR);
1664 if (fd < 0) {
1665 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1666 return -1;
1667 }
1668 memset(&ifr, 0, sizeof(ifr));
1669 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1670 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1671 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1672 if (ret != 0) {
1673 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1674 close(fd);
1675 return -1;
1676 }
bellardfc01f7e2003-06-30 10:03:06 +00001677 printf("Connected to host network interface: %s\n", ifr.ifr_name);
bellardc4b1fcc2004-03-14 21:44:30 +00001678 pstrcpy(ifname, ifname_size, ifr.ifr_name);
bellardf1510b22003-06-25 00:07:40 +00001679 fcntl(fd, F_SETFL, O_NONBLOCK);
bellardc4b1fcc2004-03-14 21:44:30 +00001680 return fd;
1681}
bellard7d3505c2004-05-12 19:32:15 +00001682#endif
bellardf1510b22003-06-25 00:07:40 +00001683
bellardc20709a2004-04-21 23:27:19 +00001684static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
bellardc4b1fcc2004-03-14 21:44:30 +00001685{
bellardc4b1fcc2004-03-14 21:44:30 +00001686 write(nd->fd, buf, size);
bellardf1510b22003-06-25 00:07:40 +00001687}
1688
bellardc20709a2004-04-21 23:27:19 +00001689static void tun_add_read_packet(NetDriverState *nd,
1690 IOCanRWHandler *fd_can_read,
1691 IOReadHandler *fd_read, void *opaque)
1692{
1693 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1694}
1695
1696static int net_tun_init(NetDriverState *nd)
1697{
1698 int pid, status;
1699 char *args[3];
1700 char **parg;
1701
1702 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1703 if (nd->fd < 0)
1704 return -1;
1705
1706 /* try to launch network init script */
1707 pid = fork();
1708 if (pid >= 0) {
1709 if (pid == 0) {
1710 parg = args;
1711 *parg++ = network_script;
1712 *parg++ = nd->ifname;
1713 *parg++ = NULL;
1714 execv(network_script, args);
1715 exit(1);
1716 }
1717 while (waitpid(pid, &status, 0) != pid);
1718 if (!WIFEXITED(status) ||
1719 WEXITSTATUS(status) != 0) {
1720 fprintf(stderr, "%s: could not launch network script\n",
1721 network_script);
1722 }
1723 }
1724 nd->send_packet = tun_send_packet;
1725 nd->add_read_packet = tun_add_read_packet;
1726 return 0;
1727}
1728
1729static int net_fd_init(NetDriverState *nd, int fd)
1730{
1731 nd->fd = fd;
1732 nd->send_packet = tun_send_packet;
1733 nd->add_read_packet = tun_add_read_packet;
1734 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1735 return 0;
1736}
1737
1738#endif /* !_WIN32 */
bellard67b915a2004-03-31 23:37:16 +00001739
bellard330d0412003-07-26 18:11:40 +00001740/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001741/* USB devices */
1742
1743static int usb_device_add(const char *devname)
1744{
1745 const char *p;
1746 USBDevice *dev;
1747 int i;
1748
1749 if (!vm_usb_hub)
1750 return -1;
1751 for(i = 0;i < MAX_VM_USB_PORTS; i++) {
1752 if (!vm_usb_ports[i]->dev)
1753 break;
1754 }
1755 if (i == MAX_VM_USB_PORTS)
1756 return -1;
1757
1758 if (strstart(devname, "host:", &p)) {
1759 dev = usb_host_device_open(p);
1760 if (!dev)
1761 return -1;
1762 } else if (!strcmp(devname, "mouse")) {
1763 dev = usb_mouse_init();
1764 if (!dev)
1765 return -1;
1766 } else {
1767 return -1;
1768 }
1769 usb_attach(vm_usb_ports[i], dev);
1770 return 0;
1771}
1772
1773static int usb_device_del(const char *devname)
1774{
1775 USBDevice *dev;
1776 int bus_num, addr, i;
1777 const char *p;
1778
1779 if (!vm_usb_hub)
1780 return -1;
1781
1782 p = strchr(devname, '.');
1783 if (!p)
1784 return -1;
1785 bus_num = strtoul(devname, NULL, 0);
1786 addr = strtoul(p + 1, NULL, 0);
1787 if (bus_num != 0)
1788 return -1;
1789 for(i = 0;i < MAX_VM_USB_PORTS; i++) {
1790 dev = vm_usb_ports[i]->dev;
1791 if (dev && dev->addr == addr)
1792 break;
1793 }
1794 if (i == MAX_VM_USB_PORTS)
1795 return -1;
1796 usb_attach(vm_usb_ports[i], NULL);
1797 return 0;
1798}
1799
1800void do_usb_add(const char *devname)
1801{
1802 int ret;
1803 ret = usb_device_add(devname);
1804 if (ret < 0)
1805 term_printf("Could not add USB device '%s'\n", devname);
1806}
1807
1808void do_usb_del(const char *devname)
1809{
1810 int ret;
1811 ret = usb_device_del(devname);
1812 if (ret < 0)
1813 term_printf("Could not remove USB device '%s'\n", devname);
1814}
1815
1816void usb_info(void)
1817{
1818 USBDevice *dev;
1819 int i;
1820 const char *speed_str;
1821
1822 if (!vm_usb_hub) {
1823 term_printf("USB support not enabled\n");
1824 return;
1825 }
1826
1827 for(i = 0; i < MAX_VM_USB_PORTS; i++) {
1828 dev = vm_usb_ports[i]->dev;
1829 if (dev) {
1830 term_printf("Hub port %d:\n", i);
1831 switch(dev->speed) {
1832 case USB_SPEED_LOW:
1833 speed_str = "1.5";
1834 break;
1835 case USB_SPEED_FULL:
1836 speed_str = "12";
1837 break;
1838 case USB_SPEED_HIGH:
1839 speed_str = "480";
1840 break;
1841 default:
1842 speed_str = "?";
1843 break;
1844 }
1845 term_printf(" Device %d.%d, speed %s Mb/s\n",
1846 0, dev->addr, speed_str);
1847 }
1848 }
1849}
1850
1851/***********************************************************/
bellardf7cce892004-12-08 22:21:25 +00001852/* pid file */
1853
1854static char *pid_filename;
1855
1856/* Remove PID file. Called on normal exit */
1857
1858static void remove_pidfile(void)
1859{
1860 unlink (pid_filename);
1861}
1862
1863static void create_pidfile(const char *filename)
1864{
1865 struct stat pidstat;
1866 FILE *f;
1867
1868 /* Try to write our PID to the named file */
1869 if (stat(filename, &pidstat) < 0) {
1870 if (errno == ENOENT) {
1871 if ((f = fopen (filename, "w")) == NULL) {
1872 perror("Opening pidfile");
1873 exit(1);
1874 }
1875 fprintf(f, "%d\n", getpid());
1876 fclose(f);
1877 pid_filename = qemu_strdup(filename);
1878 if (!pid_filename) {
1879 fprintf(stderr, "Could not save PID filename");
1880 exit(1);
1881 }
1882 atexit(remove_pidfile);
1883 }
1884 } else {
1885 fprintf(stderr, "%s already exists. Remove it and try again.\n",
1886 filename);
1887 exit(1);
1888 }
1889}
1890
1891/***********************************************************/
bellard313aa562003-08-10 21:52:11 +00001892/* dumb display */
1893
bellard313aa562003-08-10 21:52:11 +00001894static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1895{
1896}
1897
1898static void dumb_resize(DisplayState *ds, int w, int h)
1899{
1900}
1901
1902static void dumb_refresh(DisplayState *ds)
1903{
1904 vga_update_display();
1905}
1906
1907void dumb_display_init(DisplayState *ds)
1908{
1909 ds->data = NULL;
1910 ds->linesize = 0;
1911 ds->depth = 0;
1912 ds->dpy_update = dumb_update;
1913 ds->dpy_resize = dumb_resize;
1914 ds->dpy_refresh = dumb_refresh;
1915}
1916
bellard3a51dee2003-10-27 21:18:35 +00001917#if !defined(CONFIG_SOFTMMU)
bellard313aa562003-08-10 21:52:11 +00001918/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +00001919/* cpu signal handler */
1920static void host_segv_handler(int host_signum, siginfo_t *info,
1921 void *puc)
1922{
1923 if (cpu_signal_handler(host_signum, info, puc))
1924 return;
bellard8d11df92004-08-24 21:13:40 +00001925 if (stdio_nb_clients > 0)
1926 term_exit();
bellard0824d6f2003-06-24 13:42:40 +00001927 abort();
1928}
bellard3a51dee2003-10-27 21:18:35 +00001929#endif
bellard0824d6f2003-06-24 13:42:40 +00001930
bellard8a7ddc32004-03-31 19:00:16 +00001931/***********************************************************/
1932/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001933
bellardc4b1fcc2004-03-14 21:44:30 +00001934#define MAX_IO_HANDLERS 64
1935
1936typedef struct IOHandlerRecord {
1937 int fd;
1938 IOCanRWHandler *fd_can_read;
1939 IOReadHandler *fd_read;
1940 void *opaque;
1941 /* temporary data */
1942 struct pollfd *ufd;
1943 int max_size;
bellard8a7ddc32004-03-31 19:00:16 +00001944 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00001945} IOHandlerRecord;
1946
bellard8a7ddc32004-03-31 19:00:16 +00001947static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00001948
bellard8a7ddc32004-03-31 19:00:16 +00001949int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1950 IOReadHandler *fd_read, void *opaque)
bellardc4b1fcc2004-03-14 21:44:30 +00001951{
1952 IOHandlerRecord *ioh;
1953
bellard8a7ddc32004-03-31 19:00:16 +00001954 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1955 if (!ioh)
bellardc4b1fcc2004-03-14 21:44:30 +00001956 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +00001957 ioh->fd = fd;
1958 ioh->fd_can_read = fd_can_read;
1959 ioh->fd_read = fd_read;
1960 ioh->opaque = opaque;
bellard8a7ddc32004-03-31 19:00:16 +00001961 ioh->next = first_io_handler;
1962 first_io_handler = ioh;
bellardc4b1fcc2004-03-14 21:44:30 +00001963 return 0;
1964}
1965
bellard8a7ddc32004-03-31 19:00:16 +00001966void qemu_del_fd_read_handler(int fd)
bellardb4608c02003-06-27 17:34:32 +00001967{
bellard8a7ddc32004-03-31 19:00:16 +00001968 IOHandlerRecord **pioh, *ioh;
1969
1970 pioh = &first_io_handler;
1971 for(;;) {
1972 ioh = *pioh;
1973 if (ioh == NULL)
1974 break;
1975 if (ioh->fd == fd) {
1976 *pioh = ioh->next;
1977 break;
1978 }
1979 pioh = &ioh->next;
1980 }
bellardb4608c02003-06-27 17:34:32 +00001981}
1982
bellard8a7ddc32004-03-31 19:00:16 +00001983/***********************************************************/
1984/* savevm/loadvm support */
bellardb4608c02003-06-27 17:34:32 +00001985
bellard8a7ddc32004-03-31 19:00:16 +00001986void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1987{
1988 fwrite(buf, 1, size, f);
1989}
1990
1991void qemu_put_byte(QEMUFile *f, int v)
1992{
1993 fputc(v, f);
1994}
1995
1996void qemu_put_be16(QEMUFile *f, unsigned int v)
1997{
1998 qemu_put_byte(f, v >> 8);
1999 qemu_put_byte(f, v);
2000}
2001
2002void qemu_put_be32(QEMUFile *f, unsigned int v)
2003{
2004 qemu_put_byte(f, v >> 24);
2005 qemu_put_byte(f, v >> 16);
2006 qemu_put_byte(f, v >> 8);
2007 qemu_put_byte(f, v);
2008}
2009
2010void qemu_put_be64(QEMUFile *f, uint64_t v)
2011{
2012 qemu_put_be32(f, v >> 32);
2013 qemu_put_be32(f, v);
2014}
2015
2016int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
2017{
2018 return fread(buf, 1, size, f);
2019}
2020
2021int qemu_get_byte(QEMUFile *f)
2022{
2023 int v;
2024 v = fgetc(f);
2025 if (v == EOF)
2026 return 0;
2027 else
2028 return v;
2029}
2030
2031unsigned int qemu_get_be16(QEMUFile *f)
2032{
2033 unsigned int v;
2034 v = qemu_get_byte(f) << 8;
2035 v |= qemu_get_byte(f);
2036 return v;
2037}
2038
2039unsigned int qemu_get_be32(QEMUFile *f)
2040{
2041 unsigned int v;
2042 v = qemu_get_byte(f) << 24;
2043 v |= qemu_get_byte(f) << 16;
2044 v |= qemu_get_byte(f) << 8;
2045 v |= qemu_get_byte(f);
2046 return v;
2047}
2048
2049uint64_t qemu_get_be64(QEMUFile *f)
2050{
2051 uint64_t v;
2052 v = (uint64_t)qemu_get_be32(f) << 32;
2053 v |= qemu_get_be32(f);
2054 return v;
2055}
2056
2057int64_t qemu_ftell(QEMUFile *f)
2058{
2059 return ftell(f);
2060}
2061
2062int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
2063{
2064 if (fseek(f, pos, whence) < 0)
2065 return -1;
2066 return ftell(f);
2067}
2068
2069typedef struct SaveStateEntry {
2070 char idstr[256];
2071 int instance_id;
2072 int version_id;
2073 SaveStateHandler *save_state;
2074 LoadStateHandler *load_state;
2075 void *opaque;
2076 struct SaveStateEntry *next;
2077} SaveStateEntry;
2078
2079static SaveStateEntry *first_se;
2080
2081int register_savevm(const char *idstr,
2082 int instance_id,
2083 int version_id,
2084 SaveStateHandler *save_state,
2085 LoadStateHandler *load_state,
2086 void *opaque)
2087{
2088 SaveStateEntry *se, **pse;
2089
2090 se = qemu_malloc(sizeof(SaveStateEntry));
2091 if (!se)
2092 return -1;
2093 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
2094 se->instance_id = instance_id;
2095 se->version_id = version_id;
2096 se->save_state = save_state;
2097 se->load_state = load_state;
2098 se->opaque = opaque;
2099 se->next = NULL;
2100
2101 /* add at the end of list */
2102 pse = &first_se;
2103 while (*pse != NULL)
2104 pse = &(*pse)->next;
2105 *pse = se;
2106 return 0;
2107}
2108
2109#define QEMU_VM_FILE_MAGIC 0x5145564d
2110#define QEMU_VM_FILE_VERSION 0x00000001
2111
2112int qemu_savevm(const char *filename)
2113{
2114 SaveStateEntry *se;
2115 QEMUFile *f;
2116 int len, len_pos, cur_pos, saved_vm_running, ret;
2117
2118 saved_vm_running = vm_running;
2119 vm_stop(0);
2120
2121 f = fopen(filename, "wb");
2122 if (!f) {
2123 ret = -1;
2124 goto the_end;
bellard313aa562003-08-10 21:52:11 +00002125 }
2126
bellard8a7ddc32004-03-31 19:00:16 +00002127 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2128 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2129
2130 for(se = first_se; se != NULL; se = se->next) {
2131 /* ID string */
2132 len = strlen(se->idstr);
2133 qemu_put_byte(f, len);
2134 qemu_put_buffer(f, se->idstr, len);
2135
2136 qemu_put_be32(f, se->instance_id);
2137 qemu_put_be32(f, se->version_id);
2138
2139 /* record size: filled later */
2140 len_pos = ftell(f);
2141 qemu_put_be32(f, 0);
2142
2143 se->save_state(f, se->opaque);
2144
2145 /* fill record size */
2146 cur_pos = ftell(f);
2147 len = ftell(f) - len_pos - 4;
2148 fseek(f, len_pos, SEEK_SET);
2149 qemu_put_be32(f, len);
2150 fseek(f, cur_pos, SEEK_SET);
2151 }
2152
2153 fclose(f);
2154 ret = 0;
2155 the_end:
2156 if (saved_vm_running)
2157 vm_start();
2158 return ret;
2159}
2160
2161static SaveStateEntry *find_se(const char *idstr, int instance_id)
2162{
2163 SaveStateEntry *se;
2164
2165 for(se = first_se; se != NULL; se = se->next) {
2166 if (!strcmp(se->idstr, idstr) &&
2167 instance_id == se->instance_id)
2168 return se;
2169 }
2170 return NULL;
2171}
2172
2173int qemu_loadvm(const char *filename)
2174{
2175 SaveStateEntry *se;
2176 QEMUFile *f;
2177 int len, cur_pos, ret, instance_id, record_len, version_id;
2178 int saved_vm_running;
2179 unsigned int v;
2180 char idstr[256];
2181
2182 saved_vm_running = vm_running;
2183 vm_stop(0);
2184
2185 f = fopen(filename, "rb");
2186 if (!f) {
2187 ret = -1;
2188 goto the_end;
2189 }
2190
2191 v = qemu_get_be32(f);
2192 if (v != QEMU_VM_FILE_MAGIC)
2193 goto fail;
2194 v = qemu_get_be32(f);
2195 if (v != QEMU_VM_FILE_VERSION) {
2196 fail:
2197 fclose(f);
2198 ret = -1;
2199 goto the_end;
2200 }
bellardb4608c02003-06-27 17:34:32 +00002201 for(;;) {
bellarda541f292004-04-12 20:39:29 +00002202#if defined (DO_TB_FLUSH)
bellardd9276372004-04-26 19:44:57 +00002203 tb_flush(global_env);
bellarda541f292004-04-12 20:39:29 +00002204#endif
bellard8a7ddc32004-03-31 19:00:16 +00002205 len = qemu_get_byte(f);
2206 if (feof(f))
2207 break;
2208 qemu_get_buffer(f, idstr, len);
2209 idstr[len] = '\0';
2210 instance_id = qemu_get_be32(f);
2211 version_id = qemu_get_be32(f);
2212 record_len = qemu_get_be32(f);
2213#if 0
2214 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2215 idstr, instance_id, version_id, record_len);
bellardc45886d2004-01-05 00:02:06 +00002216#endif
bellard8a7ddc32004-03-31 19:00:16 +00002217 cur_pos = ftell(f);
2218 se = find_se(idstr, instance_id);
2219 if (!se) {
2220 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2221 instance_id, idstr);
2222 } else {
2223 ret = se->load_state(f, se->opaque, version_id);
2224 if (ret < 0) {
2225 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2226 instance_id, idstr);
2227 }
bellard34865132003-10-05 14:28:56 +00002228 }
bellard8a7ddc32004-03-31 19:00:16 +00002229 /* always seek to exact end of record */
2230 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
2231 }
2232 fclose(f);
2233 ret = 0;
2234 the_end:
2235 if (saved_vm_running)
2236 vm_start();
2237 return ret;
2238}
2239
2240/***********************************************************/
2241/* cpu save/restore */
2242
2243#if defined(TARGET_I386)
2244
2245static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
2246{
bellard02ba45c2004-06-25 14:46:23 +00002247 qemu_put_be32(f, dt->selector);
bellard20f32282005-01-03 23:36:21 +00002248 qemu_put_betl(f, dt->base);
bellard8a7ddc32004-03-31 19:00:16 +00002249 qemu_put_be32(f, dt->limit);
2250 qemu_put_be32(f, dt->flags);
2251}
2252
2253static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
2254{
bellard02ba45c2004-06-25 14:46:23 +00002255 dt->selector = qemu_get_be32(f);
bellard20f32282005-01-03 23:36:21 +00002256 dt->base = qemu_get_betl(f);
bellard8a7ddc32004-03-31 19:00:16 +00002257 dt->limit = qemu_get_be32(f);
2258 dt->flags = qemu_get_be32(f);
2259}
2260
2261void cpu_save(QEMUFile *f, void *opaque)
2262{
2263 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00002264 uint16_t fptag, fpus, fpuc, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00002265 uint32_t hflags;
2266 int i;
bellard664e0f12005-01-08 18:58:29 +00002267
bellard20f32282005-01-03 23:36:21 +00002268 for(i = 0; i < CPU_NB_REGS; i++)
2269 qemu_put_betls(f, &env->regs[i]);
2270 qemu_put_betls(f, &env->eip);
2271 qemu_put_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00002272 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
2273 qemu_put_be32s(f, &hflags);
2274
2275 /* FPU */
2276 fpuc = env->fpuc;
2277 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2278 fptag = 0;
bellard664e0f12005-01-08 18:58:29 +00002279 for(i = 0; i < 8; i++) {
2280 fptag |= ((!env->fptags[i]) << i);
bellard8a7ddc32004-03-31 19:00:16 +00002281 }
2282
2283 qemu_put_be16s(f, &fpuc);
2284 qemu_put_be16s(f, &fpus);
2285 qemu_put_be16s(f, &fptag);
2286
bellard664e0f12005-01-08 18:58:29 +00002287#ifdef USE_X86LDOUBLE
2288 fpregs_format = 0;
2289#else
2290 fpregs_format = 1;
2291#endif
2292 qemu_put_be16s(f, &fpregs_format);
2293
bellard8a7ddc32004-03-31 19:00:16 +00002294 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00002295#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00002296 {
2297 uint64_t mant;
2298 uint16_t exp;
2299 /* we save the real CPU data (in case of MMX usage only 'mant'
2300 contains the MMX register */
2301 cpu_get_fp80(&mant, &exp, env->fpregs[i].d);
2302 qemu_put_be64(f, mant);
2303 qemu_put_be16(f, exp);
2304 }
bellard664e0f12005-01-08 18:58:29 +00002305#else
2306 /* if we use doubles for float emulation, we save the doubles to
2307 avoid losing information in case of MMX usage. It can give
2308 problems if the image is restored on a CPU where long
2309 doubles are used instead. */
bellard8636b5d2005-01-09 00:03:14 +00002310 qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
bellard664e0f12005-01-08 18:58:29 +00002311#endif
bellard8a7ddc32004-03-31 19:00:16 +00002312 }
2313
2314 for(i = 0; i < 6; i++)
2315 cpu_put_seg(f, &env->segs[i]);
2316 cpu_put_seg(f, &env->ldt);
2317 cpu_put_seg(f, &env->tr);
2318 cpu_put_seg(f, &env->gdt);
2319 cpu_put_seg(f, &env->idt);
2320
2321 qemu_put_be32s(f, &env->sysenter_cs);
2322 qemu_put_be32s(f, &env->sysenter_esp);
2323 qemu_put_be32s(f, &env->sysenter_eip);
2324
bellard20f32282005-01-03 23:36:21 +00002325 qemu_put_betls(f, &env->cr[0]);
2326 qemu_put_betls(f, &env->cr[2]);
2327 qemu_put_betls(f, &env->cr[3]);
2328 qemu_put_betls(f, &env->cr[4]);
bellard8a7ddc32004-03-31 19:00:16 +00002329
2330 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00002331 qemu_put_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00002332
2333 /* MMU */
2334 qemu_put_be32s(f, &env->a20_mask);
bellard02536f82005-01-06 20:43:38 +00002335
bellard664e0f12005-01-08 18:58:29 +00002336 /* XMM */
2337 qemu_put_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00002338 for(i = 0; i < CPU_NB_REGS; i++) {
2339 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
2340 qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
2341 }
2342
bellard664e0f12005-01-08 18:58:29 +00002343#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00002344 qemu_put_be64s(f, &env->efer);
2345 qemu_put_be64s(f, &env->star);
2346 qemu_put_be64s(f, &env->lstar);
2347 qemu_put_be64s(f, &env->cstar);
2348 qemu_put_be64s(f, &env->fmask);
2349 qemu_put_be64s(f, &env->kernelgsbase);
2350#endif
bellard8a7ddc32004-03-31 19:00:16 +00002351}
2352
bellard8636b5d2005-01-09 00:03:14 +00002353#ifdef USE_X86LDOUBLE
bellard664e0f12005-01-08 18:58:29 +00002354/* XXX: add that in a FPU generic layer */
2355union x86_longdouble {
2356 uint64_t mant;
2357 uint16_t exp;
2358};
2359
2360#define MANTD1(fp) (fp & ((1LL << 52) - 1))
2361#define EXPBIAS1 1023
2362#define EXPD1(fp) ((fp >> 52) & 0x7FF)
2363#define SIGND1(fp) ((fp >> 32) & 0x80000000)
2364
2365static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp)
2366{
2367 int e;
2368 /* mantissa */
2369 p->mant = (MANTD1(temp) << 11) | (1LL << 63);
2370 /* exponent + sign */
2371 e = EXPD1(temp) - EXPBIAS1 + 16383;
2372 e |= SIGND1(temp) >> 16;
2373 p->exp = e;
2374}
bellard8636b5d2005-01-09 00:03:14 +00002375#endif
bellard664e0f12005-01-08 18:58:29 +00002376
bellard8a7ddc32004-03-31 19:00:16 +00002377int cpu_load(QEMUFile *f, void *opaque, int version_id)
2378{
2379 CPUState *env = opaque;
bellard664e0f12005-01-08 18:58:29 +00002380 int i, guess_mmx;
bellard8a7ddc32004-03-31 19:00:16 +00002381 uint32_t hflags;
bellard664e0f12005-01-08 18:58:29 +00002382 uint16_t fpus, fpuc, fptag, fpregs_format;
bellard8a7ddc32004-03-31 19:00:16 +00002383
bellard664e0f12005-01-08 18:58:29 +00002384 if (version_id != 3)
bellard8a7ddc32004-03-31 19:00:16 +00002385 return -EINVAL;
bellard20f32282005-01-03 23:36:21 +00002386 for(i = 0; i < CPU_NB_REGS; i++)
2387 qemu_get_betls(f, &env->regs[i]);
2388 qemu_get_betls(f, &env->eip);
2389 qemu_get_betls(f, &env->eflags);
bellard8a7ddc32004-03-31 19:00:16 +00002390 qemu_get_be32s(f, &hflags);
2391
2392 qemu_get_be16s(f, &fpuc);
2393 qemu_get_be16s(f, &fpus);
2394 qemu_get_be16s(f, &fptag);
bellard664e0f12005-01-08 18:58:29 +00002395 qemu_get_be16s(f, &fpregs_format);
2396
2397 /* NOTE: we cannot always restore the FPU state if the image come
2398 from a host with a different 'USE_X86LDOUBLE' define. We guess
2399 if we are in an MMX state to restore correctly in that case. */
2400 guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0);
bellard8a7ddc32004-03-31 19:00:16 +00002401 for(i = 0; i < 8; i++) {
2402 uint64_t mant;
2403 uint16_t exp;
bellard664e0f12005-01-08 18:58:29 +00002404
2405 switch(fpregs_format) {
2406 case 0:
2407 mant = qemu_get_be64(f);
2408 exp = qemu_get_be16(f);
2409#ifdef USE_X86LDOUBLE
2410 env->fpregs[i].d = cpu_set_fp80(mant, exp);
2411#else
2412 /* difficult case */
2413 if (guess_mmx)
bellard8636b5d2005-01-09 00:03:14 +00002414 env->fpregs[i].mmx.MMX_Q(0) = mant;
bellard664e0f12005-01-08 18:58:29 +00002415 else
2416 env->fpregs[i].d = cpu_set_fp80(mant, exp);
2417#endif
2418 break;
2419 case 1:
2420 mant = qemu_get_be64(f);
2421#ifdef USE_X86LDOUBLE
bellard8636b5d2005-01-09 00:03:14 +00002422 {
2423 union x86_longdouble *p;
2424 /* difficult case */
2425 p = (void *)&env->fpregs[i];
2426 if (guess_mmx) {
2427 p->mant = mant;
2428 p->exp = 0xffff;
2429 } else {
2430 fp64_to_fp80(p, mant);
2431 }
bellard664e0f12005-01-08 18:58:29 +00002432 }
2433#else
bellard8636b5d2005-01-09 00:03:14 +00002434 env->fpregs[i].mmx.MMX_Q(0) = mant;
bellard664e0f12005-01-08 18:58:29 +00002435#endif
2436 break;
2437 default:
2438 return -EINVAL;
2439 }
bellard8a7ddc32004-03-31 19:00:16 +00002440 }
2441
2442 env->fpuc = fpuc;
bellard7a0e1f42005-03-13 17:01:47 +00002443 /* XXX: restore FPU round state */
bellard8a7ddc32004-03-31 19:00:16 +00002444 env->fpstt = (fpus >> 11) & 7;
2445 env->fpus = fpus & ~0x3800;
bellard664e0f12005-01-08 18:58:29 +00002446 fptag ^= 0xff;
bellard8a7ddc32004-03-31 19:00:16 +00002447 for(i = 0; i < 8; i++) {
bellard664e0f12005-01-08 18:58:29 +00002448 env->fptags[i] = (fptag >> i) & 1;
bellard8a7ddc32004-03-31 19:00:16 +00002449 }
2450
2451 for(i = 0; i < 6; i++)
2452 cpu_get_seg(f, &env->segs[i]);
2453 cpu_get_seg(f, &env->ldt);
2454 cpu_get_seg(f, &env->tr);
2455 cpu_get_seg(f, &env->gdt);
2456 cpu_get_seg(f, &env->idt);
2457
2458 qemu_get_be32s(f, &env->sysenter_cs);
2459 qemu_get_be32s(f, &env->sysenter_esp);
2460 qemu_get_be32s(f, &env->sysenter_eip);
2461
bellard20f32282005-01-03 23:36:21 +00002462 qemu_get_betls(f, &env->cr[0]);
2463 qemu_get_betls(f, &env->cr[2]);
2464 qemu_get_betls(f, &env->cr[3]);
2465 qemu_get_betls(f, &env->cr[4]);
bellard8a7ddc32004-03-31 19:00:16 +00002466
2467 for(i = 0; i < 8; i++)
bellard20f32282005-01-03 23:36:21 +00002468 qemu_get_betls(f, &env->dr[i]);
bellard8a7ddc32004-03-31 19:00:16 +00002469
2470 /* MMU */
2471 qemu_get_be32s(f, &env->a20_mask);
2472
bellard664e0f12005-01-08 18:58:29 +00002473 qemu_get_be32s(f, &env->mxcsr);
bellard02536f82005-01-06 20:43:38 +00002474 for(i = 0; i < CPU_NB_REGS; i++) {
2475 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
2476 qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
2477 }
2478
bellard664e0f12005-01-08 18:58:29 +00002479#ifdef TARGET_X86_64
bellard02536f82005-01-06 20:43:38 +00002480 qemu_get_be64s(f, &env->efer);
2481 qemu_get_be64s(f, &env->star);
2482 qemu_get_be64s(f, &env->lstar);
2483 qemu_get_be64s(f, &env->cstar);
2484 qemu_get_be64s(f, &env->fmask);
2485 qemu_get_be64s(f, &env->kernelgsbase);
2486#endif
2487
bellard8a7ddc32004-03-31 19:00:16 +00002488 /* XXX: compute hflags from scratch, except for CPL and IIF */
2489 env->hflags = hflags;
2490 tlb_flush(env, 1);
2491 return 0;
2492}
2493
bellarda541f292004-04-12 20:39:29 +00002494#elif defined(TARGET_PPC)
2495void cpu_save(QEMUFile *f, void *opaque)
2496{
2497}
2498
2499int cpu_load(QEMUFile *f, void *opaque, int version_id)
2500{
2501 return 0;
2502}
bellard6af0bf92005-07-02 14:58:51 +00002503
2504#elif defined(TARGET_MIPS)
2505void cpu_save(QEMUFile *f, void *opaque)
2506{
2507}
2508
2509int cpu_load(QEMUFile *f, void *opaque, int version_id)
2510{
2511 return 0;
2512}
2513
bellarde95c8d52004-09-30 22:22:08 +00002514#elif defined(TARGET_SPARC)
2515void cpu_save(QEMUFile *f, void *opaque)
2516{
bellarde80cfcf2004-12-19 23:18:01 +00002517 CPUState *env = opaque;
2518 int i;
2519 uint32_t tmp;
2520
bellard4fa5d772005-01-30 22:57:54 +00002521 for(i = 0; i < 8; i++)
2522 qemu_put_betls(f, &env->gregs[i]);
2523 for(i = 0; i < NWINDOWS * 16; i++)
2524 qemu_put_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00002525
2526 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00002527 for(i = 0; i < TARGET_FPREGS; i++) {
2528 union {
2529 TARGET_FPREG_T f;
2530 target_ulong i;
2531 } u;
2532 u.f = env->fpr[i];
2533 qemu_put_betl(f, u.i);
bellarde80cfcf2004-12-19 23:18:01 +00002534 }
bellard4fa5d772005-01-30 22:57:54 +00002535
2536 qemu_put_betls(f, &env->pc);
2537 qemu_put_betls(f, &env->npc);
2538 qemu_put_betls(f, &env->y);
bellarde80cfcf2004-12-19 23:18:01 +00002539 tmp = GET_PSR(env);
bellard4fa5d772005-01-30 22:57:54 +00002540 qemu_put_be32(f, tmp);
bellard34751872005-07-02 14:31:34 +00002541 qemu_put_betls(f, &env->fsr);
2542 qemu_put_betls(f, &env->tbr);
2543#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00002544 qemu_put_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00002545 /* MMU */
2546 for(i = 0; i < 16; i++)
2547 qemu_put_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00002548#endif
bellarde95c8d52004-09-30 22:22:08 +00002549}
2550
2551int cpu_load(QEMUFile *f, void *opaque, int version_id)
2552{
bellarde80cfcf2004-12-19 23:18:01 +00002553 CPUState *env = opaque;
2554 int i;
2555 uint32_t tmp;
2556
bellard4fa5d772005-01-30 22:57:54 +00002557 for(i = 0; i < 8; i++)
2558 qemu_get_betls(f, &env->gregs[i]);
2559 for(i = 0; i < NWINDOWS * 16; i++)
2560 qemu_get_betls(f, &env->regbase[i]);
bellarde80cfcf2004-12-19 23:18:01 +00002561
2562 /* FPU */
bellard4fa5d772005-01-30 22:57:54 +00002563 for(i = 0; i < TARGET_FPREGS; i++) {
2564 union {
2565 TARGET_FPREG_T f;
2566 target_ulong i;
2567 } u;
2568 u.i = qemu_get_betl(f);
2569 env->fpr[i] = u.f;
bellarde80cfcf2004-12-19 23:18:01 +00002570 }
bellard4fa5d772005-01-30 22:57:54 +00002571
2572 qemu_get_betls(f, &env->pc);
2573 qemu_get_betls(f, &env->npc);
2574 qemu_get_betls(f, &env->y);
2575 tmp = qemu_get_be32(f);
2576 env->cwp = 0; /* needed to ensure that the wrapping registers are
2577 correctly updated */
bellarde80cfcf2004-12-19 23:18:01 +00002578 PUT_PSR(env, tmp);
bellard34751872005-07-02 14:31:34 +00002579 qemu_get_betls(f, &env->fsr);
2580 qemu_get_betls(f, &env->tbr);
2581#ifndef TARGET_SPARC64
bellarde80cfcf2004-12-19 23:18:01 +00002582 qemu_get_be32s(f, &env->wim);
bellarde80cfcf2004-12-19 23:18:01 +00002583 /* MMU */
2584 for(i = 0; i < 16; i++)
2585 qemu_get_be32s(f, &env->mmuregs[i]);
bellard34751872005-07-02 14:31:34 +00002586#endif
bellarde80cfcf2004-12-19 23:18:01 +00002587 tlb_flush(env, 1);
bellarde95c8d52004-09-30 22:22:08 +00002588 return 0;
2589}
bellard8a7ddc32004-03-31 19:00:16 +00002590#else
2591
2592#warning No CPU save/restore functions
2593
2594#endif
2595
2596/***********************************************************/
2597/* ram save/restore */
2598
2599/* we just avoid storing empty pages */
2600static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
2601{
2602 int i, v;
2603
2604 v = buf[0];
2605 for(i = 1; i < len; i++) {
2606 if (buf[i] != v)
2607 goto normal_save;
2608 }
2609 qemu_put_byte(f, 1);
2610 qemu_put_byte(f, v);
2611 return;
2612 normal_save:
2613 qemu_put_byte(f, 0);
2614 qemu_put_buffer(f, buf, len);
2615}
2616
2617static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2618{
2619 int v;
2620
2621 v = qemu_get_byte(f);
2622 switch(v) {
2623 case 0:
2624 if (qemu_get_buffer(f, buf, len) != len)
2625 return -EIO;
2626 break;
2627 case 1:
2628 v = qemu_get_byte(f);
2629 memset(buf, v, len);
2630 break;
2631 default:
2632 return -EINVAL;
2633 }
2634 return 0;
2635}
2636
2637static void ram_save(QEMUFile *f, void *opaque)
2638{
2639 int i;
2640 qemu_put_be32(f, phys_ram_size);
2641 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2642 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2643 }
2644}
2645
2646static int ram_load(QEMUFile *f, void *opaque, int version_id)
2647{
2648 int i, ret;
2649
2650 if (version_id != 1)
2651 return -EINVAL;
2652 if (qemu_get_be32(f) != phys_ram_size)
2653 return -EINVAL;
2654 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2655 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2656 if (ret)
2657 return ret;
2658 }
2659 return 0;
2660}
2661
2662/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00002663/* machine registration */
2664
2665QEMUMachine *first_machine = NULL;
2666
2667int qemu_register_machine(QEMUMachine *m)
2668{
2669 QEMUMachine **pm;
2670 pm = &first_machine;
2671 while (*pm != NULL)
2672 pm = &(*pm)->next;
2673 m->next = NULL;
2674 *pm = m;
2675 return 0;
2676}
2677
2678QEMUMachine *find_machine(const char *name)
2679{
2680 QEMUMachine *m;
2681
2682 for(m = first_machine; m != NULL; m = m->next) {
2683 if (!strcmp(m->name, name))
2684 return m;
2685 }
2686 return NULL;
2687}
2688
2689/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002690/* main execution loop */
2691
2692void gui_update(void *opaque)
2693{
2694 display_state.dpy_refresh(&display_state);
2695 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
2696}
2697
2698/* XXX: support several handlers */
2699VMStopHandler *vm_stop_cb;
2700VMStopHandler *vm_stop_opaque;
2701
2702int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
2703{
2704 vm_stop_cb = cb;
2705 vm_stop_opaque = opaque;
2706 return 0;
2707}
2708
2709void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
2710{
2711 vm_stop_cb = NULL;
2712}
2713
2714void vm_start(void)
2715{
2716 if (!vm_running) {
2717 cpu_enable_ticks();
2718 vm_running = 1;
2719 }
2720}
2721
2722void vm_stop(int reason)
2723{
2724 if (vm_running) {
2725 cpu_disable_ticks();
2726 vm_running = 0;
2727 if (reason != 0) {
2728 if (vm_stop_cb) {
2729 vm_stop_cb(vm_stop_opaque, reason);
2730 }
2731 }
2732 }
2733}
2734
bellardbb0c6722004-06-20 12:37:32 +00002735/* reset/shutdown handler */
2736
2737typedef struct QEMUResetEntry {
2738 QEMUResetHandler *func;
2739 void *opaque;
2740 struct QEMUResetEntry *next;
2741} QEMUResetEntry;
2742
2743static QEMUResetEntry *first_reset_entry;
2744static int reset_requested;
2745static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00002746static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00002747
2748void qemu_register_reset(QEMUResetHandler *func, void *opaque)
2749{
2750 QEMUResetEntry **pre, *re;
2751
2752 pre = &first_reset_entry;
2753 while (*pre != NULL)
2754 pre = &(*pre)->next;
2755 re = qemu_mallocz(sizeof(QEMUResetEntry));
2756 re->func = func;
2757 re->opaque = opaque;
2758 re->next = NULL;
2759 *pre = re;
2760}
2761
2762void qemu_system_reset(void)
2763{
2764 QEMUResetEntry *re;
2765
2766 /* reset all devices */
2767 for(re = first_reset_entry; re != NULL; re = re->next) {
2768 re->func(re->opaque);
2769 }
2770}
2771
2772void qemu_system_reset_request(void)
2773{
2774 reset_requested = 1;
2775 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2776}
2777
2778void qemu_system_shutdown_request(void)
2779{
2780 shutdown_requested = 1;
2781 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2782}
2783
bellard34751872005-07-02 14:31:34 +00002784void qemu_system_powerdown_request(void)
2785{
2786 powerdown_requested = 1;
2787 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
2788}
2789
bellardbb0c6722004-06-20 12:37:32 +00002790static void main_cpu_reset(void *opaque)
2791{
bellarde80cfcf2004-12-19 23:18:01 +00002792#if defined(TARGET_I386) || defined(TARGET_SPARC)
bellardbb0c6722004-06-20 12:37:32 +00002793 CPUState *env = opaque;
2794 cpu_reset(env);
2795#endif
2796}
2797
bellard5905b2e2004-08-01 21:53:26 +00002798void main_loop_wait(int timeout)
bellard8a7ddc32004-03-31 19:00:16 +00002799{
bellard67b915a2004-03-31 23:37:16 +00002800#ifndef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002801 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
bellard8a7ddc32004-03-31 19:00:16 +00002802 IOHandlerRecord *ioh, *ioh_next;
bellard67b915a2004-03-31 23:37:16 +00002803 uint8_t buf[4096];
2804 int n, max_size;
2805#endif
bellard5905b2e2004-08-01 21:53:26 +00002806 int ret;
bellardc4b1fcc2004-03-14 21:44:30 +00002807
bellard38e205a2004-04-06 19:29:17 +00002808#ifdef _WIN32
2809 if (timeout > 0)
2810 Sleep(timeout);
2811#else
bellardb4608c02003-06-27 17:34:32 +00002812 /* poll any events */
bellard8a7ddc32004-03-31 19:00:16 +00002813 /* XXX: separate device handlers from system ones */
bellardb4608c02003-06-27 17:34:32 +00002814 pf = ufds;
bellard8a7ddc32004-03-31 19:00:16 +00002815 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2816 if (!ioh->fd_can_read) {
2817 max_size = 0;
bellardc4b1fcc2004-03-14 21:44:30 +00002818 pf->fd = ioh->fd;
2819 pf->events = POLLIN;
2820 ioh->ufd = pf;
2821 pf++;
2822 } else {
bellard8a7ddc32004-03-31 19:00:16 +00002823 max_size = ioh->fd_can_read(ioh->opaque);
2824 if (max_size > 0) {
2825 if (max_size > sizeof(buf))
2826 max_size = sizeof(buf);
2827 pf->fd = ioh->fd;
2828 pf->events = POLLIN;
2829 ioh->ufd = pf;
2830 pf++;
2831 } else {
2832 ioh->ufd = NULL;
2833 }
bellardc4b1fcc2004-03-14 21:44:30 +00002834 }
2835 ioh->max_size = max_size;
bellardb4608c02003-06-27 17:34:32 +00002836 }
bellard73332e52004-04-04 20:22:28 +00002837
bellardb4608c02003-06-27 17:34:32 +00002838 ret = poll(ufds, pf - ufds, timeout);
2839 if (ret > 0) {
bellard8a7ddc32004-03-31 19:00:16 +00002840 /* XXX: better handling of removal */
2841 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2842 ioh_next = ioh->next;
bellardc4b1fcc2004-03-14 21:44:30 +00002843 pf = ioh->ufd;
2844 if (pf) {
bellard8a7ddc32004-03-31 19:00:16 +00002845 if (pf->revents & POLLIN) {
2846 if (ioh->max_size == 0) {
2847 /* just a read event */
2848 ioh->fd_read(ioh->opaque, NULL, 0);
2849 } else {
2850 n = read(ioh->fd, buf, ioh->max_size);
2851 if (n >= 0) {
2852 ioh->fd_read(ioh->opaque, buf, n);
bellard158156d2004-05-17 21:13:42 +00002853 } else if (errno != EAGAIN) {
bellard8a7ddc32004-03-31 19:00:16 +00002854 ioh->fd_read(ioh->opaque, NULL, -errno);
2855 }
2856 }
bellardb4608c02003-06-27 17:34:32 +00002857 }
bellardb4608c02003-06-27 17:34:32 +00002858 }
bellardb4608c02003-06-27 17:34:32 +00002859 }
2860 }
bellard5905b2e2004-08-01 21:53:26 +00002861#endif /* !defined(_WIN32) */
bellardc20709a2004-04-21 23:27:19 +00002862#if defined(CONFIG_SLIRP)
2863 /* XXX: merge with poll() */
2864 if (slirp_inited) {
2865 fd_set rfds, wfds, xfds;
2866 int nfds;
2867 struct timeval tv;
2868
2869 nfds = -1;
2870 FD_ZERO(&rfds);
2871 FD_ZERO(&wfds);
2872 FD_ZERO(&xfds);
2873 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2874 tv.tv_sec = 0;
2875 tv.tv_usec = 0;
2876 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2877 if (ret >= 0) {
2878 slirp_select_poll(&rfds, &wfds, &xfds);
2879 }
2880 }
2881#endif
2882
bellard8a7ddc32004-03-31 19:00:16 +00002883 if (vm_running) {
2884 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2885 qemu_get_clock(vm_clock));
bellard8a7ddc32004-03-31 19:00:16 +00002886 /* run dma transfers, if any */
2887 DMA_run();
bellardb4608c02003-06-27 17:34:32 +00002888 }
bellard8dc75d72004-02-25 23:30:56 +00002889
bellard8a7ddc32004-03-31 19:00:16 +00002890 /* real time timers */
2891 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2892 qemu_get_clock(rt_clock));
bellard5905b2e2004-08-01 21:53:26 +00002893}
2894
2895int main_loop(void)
2896{
2897 int ret, timeout;
2898 CPUState *env = global_env;
2899
2900 for(;;) {
2901 if (vm_running) {
2902 ret = cpu_exec(env);
2903 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00002904 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00002905 break;
2906 }
2907 if (reset_requested) {
2908 reset_requested = 0;
2909 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00002910 ret = EXCP_INTERRUPT;
2911 }
2912 if (powerdown_requested) {
2913 powerdown_requested = 0;
2914 qemu_system_powerdown();
2915 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00002916 }
2917 if (ret == EXCP_DEBUG) {
2918 vm_stop(EXCP_DEBUG);
2919 }
2920 /* if hlt instruction, we wait until the next IRQ */
2921 /* XXX: use timeout computed from timers */
bellard34751872005-07-02 14:31:34 +00002922 if (ret == EXCP_HLT)
bellard5905b2e2004-08-01 21:53:26 +00002923 timeout = 10;
2924 else
2925 timeout = 0;
2926 } else {
2927 timeout = 10;
2928 }
2929 main_loop_wait(timeout);
bellardb4608c02003-06-27 17:34:32 +00002930 }
bellard34865132003-10-05 14:28:56 +00002931 cpu_disable_ticks();
2932 return ret;
bellardb4608c02003-06-27 17:34:32 +00002933}
2934
bellard0824d6f2003-06-24 13:42:40 +00002935void help(void)
2936{
bellardf5a85102005-07-24 18:44:56 +00002937 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2005 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00002938 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00002939 "\n"
bellarda20dd502003-09-30 21:07:02 +00002940 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00002941 "\n"
bellarda20dd502003-09-30 21:07:02 +00002942 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00002943 "-M machine select emulated machine (-M ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00002944 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00002945 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2946 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00002947 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
bellard9e89a4b2004-11-14 15:42:27 +00002948 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
bellarda20dd502003-09-30 21:07:02 +00002949 "-snapshot write to temporary files instead of disk image files\n"
bellarda00bad72004-05-22 21:39:06 +00002950 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00002951 "-nographic disable graphical output and redirect serial I/Os to console\n"
bellard4ca00742004-12-12 22:20:04 +00002952#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00002953 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00002954#endif
bellard1d14ffa2005-10-30 18:58:22 +00002955#ifdef HAS_AUDIO
bellardc0fe3822005-11-05 18:55:28 +00002956 "-enable-audio enable audio support, and all the sound cars\n"
bellard1d14ffa2005-10-30 18:58:22 +00002957 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00002958 "-soundhw c1,... enable audio support\n"
2959 " and only specified sound cards (comma separated list)\n"
2960 " use -soundhw ? to get the list of supported cards\n"
bellard1d14ffa2005-10-30 18:58:22 +00002961#endif
bellard89980282004-06-03 14:04:03 +00002962 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00002963 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00002964#ifdef TARGET_I386
2965 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
2966#endif
bellardb389dbf2005-11-06 16:49:55 +00002967 "-usb enable the USB driver (will be the default soon)\n"
2968 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00002969#if defined(TARGET_PPC) || defined(TARGET_SPARC)
2970 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00002971#endif
bellarda20dd502003-09-30 21:07:02 +00002972 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00002973 "Network options:\n"
bellardc20709a2004-04-21 23:27:19 +00002974 "-nics n simulate 'n' network cards [default=1]\n"
bellard702c6512004-04-02 21:21:32 +00002975 "-macaddr addr set the mac address of the first interface\n"
bellardc20709a2004-04-21 23:27:19 +00002976 "-n script set tap/tun network init script [default=%s]\n"
2977 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2978#ifdef CONFIG_SLIRP
2979 "-user-net use user mode network stack [default if no tap/tun script]\n"
bellard9bf05442004-08-25 22:12:49 +00002980 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
bellardc94c8d62004-09-13 21:37:34 +00002981#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00002982 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
bellardc94c8d62004-09-13 21:37:34 +00002983#endif
bellard9bf05442004-08-25 22:12:49 +00002984 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2985 " redirect TCP or UDP connections from host to guest [-user-net]\n"
bellardc20709a2004-04-21 23:27:19 +00002986#endif
2987 "-dummy-net use dummy network stack\n"
bellardc4b1fcc2004-03-14 21:44:30 +00002988 "\n"
2989 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00002990 "-kernel bzImage use 'bzImage' as kernel image\n"
2991 "-append cmdline use 'cmdline' as kernel command line\n"
2992 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00002993 "\n"
bellard330d0412003-07-26 18:11:40 +00002994 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00002995 "-monitor dev redirect the monitor to char device 'dev'\n"
2996 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00002997 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00002998 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00002999 "-S freeze CPU at startup (use 'c' to start execution)\n"
bellarda20dd502003-09-30 21:07:02 +00003000 "-s wait gdb connection to port %d\n"
3001 "-p port change gdb connection port\n"
bellardf193c792004-03-21 17:06:25 +00003002 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00003003 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3004 " translation (t=none or lba) (usually qemu can guess them)\n"
bellarda20dd502003-09-30 21:07:02 +00003005 "-L path set the directory for the BIOS and VGA BIOS\n"
bellardd993e022005-02-10 22:00:06 +00003006#ifdef USE_KQEMU
3007 "-no-kqemu disable KQEMU kernel module usage\n"
3008#endif
bellard77fef8c2004-02-16 22:05:46 +00003009#ifdef USE_CODE_COPY
3010 "-no-code-copy disable code copy acceleration\n"
3011#endif
bellardbb0c6722004-06-20 12:37:32 +00003012#ifdef TARGET_I386
3013 "-isa simulate an ISA-only system (default is PCI system)\n"
bellard1bfe8562004-07-08 21:17:50 +00003014 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
3015 " (default is CL-GD5446 PCI VGA)\n"
bellardbb0c6722004-06-20 12:37:32 +00003016#endif
bellardd63d3072004-10-03 13:29:03 +00003017 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
bellard0824d6f2003-06-24 13:42:40 +00003018 "\n"
bellard82c643f2004-07-14 17:28:13 +00003019 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00003020 "ctrl-alt-f toggle full screen\n"
3021 "ctrl-alt-n switch to virtual console 'n'\n"
3022 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00003023 "\n"
3024 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3025 ,
bellard0db63472003-10-27 21:37:46 +00003026#ifdef CONFIG_SOFTMMU
3027 "qemu",
3028#else
3029 "qemu-fast",
3030#endif
bellarda00bad72004-05-22 21:39:06 +00003031 DEFAULT_RAM_SIZE,
3032 DEFAULT_NETWORK_SCRIPT,
bellard6e44ba72004-01-18 21:56:49 +00003033 DEFAULT_GDBSTUB_PORT,
3034 "/tmp/qemu.log");
bellard0db63472003-10-27 21:37:46 +00003035#ifndef CONFIG_SOFTMMU
3036 printf("\n"
3037 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
3038 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
3039 "PC emulation.\n");
3040#endif
bellard0824d6f2003-06-24 13:42:40 +00003041 exit(1);
3042}
3043
bellardcd6f1162004-05-13 22:02:20 +00003044#define HAS_ARG 0x0001
3045
3046enum {
3047 QEMU_OPTION_h,
3048
bellardcc1daa42005-06-05 14:49:17 +00003049 QEMU_OPTION_M,
bellardcd6f1162004-05-13 22:02:20 +00003050 QEMU_OPTION_fda,
3051 QEMU_OPTION_fdb,
3052 QEMU_OPTION_hda,
3053 QEMU_OPTION_hdb,
3054 QEMU_OPTION_hdc,
3055 QEMU_OPTION_hdd,
3056 QEMU_OPTION_cdrom,
3057 QEMU_OPTION_boot,
3058 QEMU_OPTION_snapshot,
3059 QEMU_OPTION_m,
3060 QEMU_OPTION_nographic,
bellard1d14ffa2005-10-30 18:58:22 +00003061#ifdef HAS_AUDIO
bellardcd6f1162004-05-13 22:02:20 +00003062 QEMU_OPTION_enable_audio,
bellard1d14ffa2005-10-30 18:58:22 +00003063 QEMU_OPTION_audio_help,
3064 QEMU_OPTION_soundhw,
3065#endif
bellardcd6f1162004-05-13 22:02:20 +00003066
3067 QEMU_OPTION_nics,
3068 QEMU_OPTION_macaddr,
3069 QEMU_OPTION_n,
3070 QEMU_OPTION_tun_fd,
3071 QEMU_OPTION_user_net,
bellardc7f74642004-08-24 21:57:12 +00003072 QEMU_OPTION_tftp,
bellard9d728e82004-09-05 23:09:03 +00003073 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00003074 QEMU_OPTION_redir,
bellardcd6f1162004-05-13 22:02:20 +00003075 QEMU_OPTION_dummy_net,
3076
3077 QEMU_OPTION_kernel,
3078 QEMU_OPTION_append,
3079 QEMU_OPTION_initrd,
3080
3081 QEMU_OPTION_S,
3082 QEMU_OPTION_s,
3083 QEMU_OPTION_p,
3084 QEMU_OPTION_d,
3085 QEMU_OPTION_hdachs,
3086 QEMU_OPTION_L,
3087 QEMU_OPTION_no_code_copy,
bellard3d11d0e2004-12-12 16:56:30 +00003088 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00003089 QEMU_OPTION_localtime,
bellard1f042752004-06-05 13:46:47 +00003090 QEMU_OPTION_cirrusvga,
bellarde9b137c2004-06-21 16:46:10 +00003091 QEMU_OPTION_g,
bellard1bfe8562004-07-08 21:17:50 +00003092 QEMU_OPTION_std_vga,
bellard82c643f2004-07-14 17:28:13 +00003093 QEMU_OPTION_monitor,
3094 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00003095 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00003096 QEMU_OPTION_loadvm,
3097 QEMU_OPTION_full_screen,
bellardf7cce892004-12-08 22:21:25 +00003098 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00003099 QEMU_OPTION_no_kqemu,
bellarda09db212005-04-30 16:10:35 +00003100 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00003101 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00003102 QEMU_OPTION_usbdevice,
bellardcd6f1162004-05-13 22:02:20 +00003103};
3104
3105typedef struct QEMUOption {
3106 const char *name;
3107 int flags;
3108 int index;
3109} QEMUOption;
3110
3111const QEMUOption qemu_options[] = {
3112 { "h", 0, QEMU_OPTION_h },
3113
bellardcc1daa42005-06-05 14:49:17 +00003114 { "M", HAS_ARG, QEMU_OPTION_M },
bellardcd6f1162004-05-13 22:02:20 +00003115 { "fda", HAS_ARG, QEMU_OPTION_fda },
3116 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
3117 { "hda", HAS_ARG, QEMU_OPTION_hda },
3118 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
3119 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
3120 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
3121 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
3122 { "boot", HAS_ARG, QEMU_OPTION_boot },
3123 { "snapshot", 0, QEMU_OPTION_snapshot },
3124 { "m", HAS_ARG, QEMU_OPTION_m },
3125 { "nographic", 0, QEMU_OPTION_nographic },
bellard3d11d0e2004-12-12 16:56:30 +00003126 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00003127#ifdef HAS_AUDIO
bellardcd6f1162004-05-13 22:02:20 +00003128 { "enable-audio", 0, QEMU_OPTION_enable_audio },
bellard1d14ffa2005-10-30 18:58:22 +00003129 { "audio-help", 0, QEMU_OPTION_audio_help },
3130 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
3131#endif
bellardcd6f1162004-05-13 22:02:20 +00003132
3133 { "nics", HAS_ARG, QEMU_OPTION_nics},
3134 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
bellard69b91032004-05-18 23:05:28 +00003135 { "n", HAS_ARG, QEMU_OPTION_n },
bellardcd6f1162004-05-13 22:02:20 +00003136 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
bellard158156d2004-05-17 21:13:42 +00003137#ifdef CONFIG_SLIRP
bellardcd6f1162004-05-13 22:02:20 +00003138 { "user-net", 0, QEMU_OPTION_user_net },
bellardc7f74642004-08-24 21:57:12 +00003139 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
bellardc94c8d62004-09-13 21:37:34 +00003140#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00003141 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00003142#endif
bellard9bf05442004-08-25 22:12:49 +00003143 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00003144#endif
bellardcd6f1162004-05-13 22:02:20 +00003145 { "dummy-net", 0, QEMU_OPTION_dummy_net },
3146
3147 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
3148 { "append", HAS_ARG, QEMU_OPTION_append },
3149 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
3150
3151 { "S", 0, QEMU_OPTION_S },
3152 { "s", 0, QEMU_OPTION_s },
3153 { "p", HAS_ARG, QEMU_OPTION_p },
3154 { "d", HAS_ARG, QEMU_OPTION_d },
3155 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
3156 { "L", HAS_ARG, QEMU_OPTION_L },
3157 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
bellardd993e022005-02-10 22:00:06 +00003158#ifdef USE_KQEMU
3159 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
3160#endif
bellard6f7e9ae2005-03-13 09:43:36 +00003161#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00003162 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00003163#endif
bellardee22c2f2004-06-03 12:49:50 +00003164 { "localtime", 0, QEMU_OPTION_localtime },
bellard1bfe8562004-07-08 21:17:50 +00003165 { "std-vga", 0, QEMU_OPTION_std_vga },
bellard82c643f2004-07-14 17:28:13 +00003166 { "monitor", 1, QEMU_OPTION_monitor },
3167 { "serial", 1, QEMU_OPTION_serial },
bellard6508fe52005-01-15 12:02:56 +00003168 { "parallel", 1, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00003169 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
3170 { "full-screen", 0, QEMU_OPTION_full_screen },
bellardf7cce892004-12-08 22:21:25 +00003171 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00003172 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00003173 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellarda09db212005-04-30 16:10:35 +00003174
bellard1f042752004-06-05 13:46:47 +00003175 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00003176 { "usb", 0, QEMU_OPTION_usb },
bellard1f042752004-06-05 13:46:47 +00003177 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
bellardcd6f1162004-05-13 22:02:20 +00003178 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00003179};
3180
bellard77fef8c2004-02-16 22:05:46 +00003181#if defined (TARGET_I386) && defined(USE_CODE_COPY)
3182
3183/* this stack is only used during signal handling */
3184#define SIGNAL_STACK_SIZE 32768
3185
3186static uint8_t *signal_stack;
3187
3188#endif
3189
bellard5905b2e2004-08-01 21:53:26 +00003190/* password input */
3191
3192static BlockDriverState *get_bdrv(int index)
3193{
3194 BlockDriverState *bs;
3195
3196 if (index < 4) {
3197 bs = bs_table[index];
3198 } else if (index < 6) {
3199 bs = fd_table[index - 4];
3200 } else {
3201 bs = NULL;
3202 }
3203 return bs;
3204}
3205
3206static void read_passwords(void)
3207{
3208 BlockDriverState *bs;
3209 int i, j;
3210 char password[256];
3211
3212 for(i = 0; i < 6; i++) {
3213 bs = get_bdrv(i);
3214 if (bs && bdrv_is_encrypted(bs)) {
3215 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
3216 for(j = 0; j < 3; j++) {
3217 monitor_readline("Password: ",
3218 1, password, sizeof(password));
3219 if (bdrv_set_key(bs, password) == 0)
3220 break;
3221 term_printf("invalid password\n");
3222 }
3223 }
3224 }
3225}
3226
bellardcc1daa42005-06-05 14:49:17 +00003227/* XXX: currently we cannot use simultaneously different CPUs */
3228void register_machines(void)
3229{
3230#if defined(TARGET_I386)
3231 qemu_register_machine(&pc_machine);
bellard3dbbdc22005-11-06 18:20:37 +00003232 qemu_register_machine(&isapc_machine);
bellardcc1daa42005-06-05 14:49:17 +00003233#elif defined(TARGET_PPC)
3234 qemu_register_machine(&heathrow_machine);
3235 qemu_register_machine(&core99_machine);
3236 qemu_register_machine(&prep_machine);
bellard6af0bf92005-07-02 14:58:51 +00003237#elif defined(TARGET_MIPS)
3238 qemu_register_machine(&mips_machine);
bellardcc1daa42005-06-05 14:49:17 +00003239#elif defined(TARGET_SPARC)
bellard34751872005-07-02 14:31:34 +00003240#ifdef TARGET_SPARC64
3241 qemu_register_machine(&sun4u_machine);
3242#else
bellardcc1daa42005-06-05 14:49:17 +00003243 qemu_register_machine(&sun4m_machine);
3244#endif
bellard34751872005-07-02 14:31:34 +00003245#endif
bellardcc1daa42005-06-05 14:49:17 +00003246}
3247
bellard1d14ffa2005-10-30 18:58:22 +00003248#ifdef HAS_AUDIO
3249static void select_soundhw (const char *optarg)
3250{
3251 if (*optarg == '?') {
3252 show_valid_cards:
3253 printf ("Valid sound card names (comma separated):\n");
3254 printf ("sb16 Creative Sound Blaster 16\n");
3255#ifdef CONFIG_ADLIB
3256#ifdef HAS_YMF262
bellardc0fe3822005-11-05 18:55:28 +00003257 printf ("adlib Yamaha YMF262 (OPL3)\n");
bellard1d14ffa2005-10-30 18:58:22 +00003258#else
bellardc0fe3822005-11-05 18:55:28 +00003259 printf ("adlib Yamaha YM3812 (OPL2)\n");
bellard1d14ffa2005-10-30 18:58:22 +00003260#endif
3261#endif
3262#ifdef CONFIG_GUS
3263 printf ("gus Gravis Ultrasound GF1\n");
3264#endif
3265 printf ("es1370 ENSONIQ AudioPCI ES1370\n");
3266 exit (*optarg != '?');
3267 }
3268 else {
3269 struct {
3270 char *name;
3271 int *enabledp;
3272 } soundhw_tab[] = {
3273 { "sb16", &sb16_enabled },
3274#ifdef CONFIG_ADLIB
3275 { "adlib", &adlib_enabled },
3276#endif
3277#ifdef CONFIG_GUS
3278 { "gus", &gus_enabled },
3279#endif
3280 { "es1370", &es1370_enabled },
3281 };
3282 size_t tablen, l, i;
3283 const char *p;
3284 char *e;
3285 int bad_card = 0;
3286
3287 p = optarg;
3288 tablen = sizeof (soundhw_tab) / sizeof (soundhw_tab[0]);
3289
3290 while (*p) {
3291 e = strchr (p, ',');
3292 l = !e ? strlen (p) : (size_t) (e - p);
3293 for (i = 0; i < tablen; ++i) {
3294 if (!strncmp (soundhw_tab[i].name, p, l)) {
3295 audio_enabled = 1;
3296 *soundhw_tab[i].enabledp = 1;
3297 break;
3298 }
3299 }
3300 if (i == tablen) {
3301 if (l > 80) {
3302 fprintf (stderr,
3303 "Unknown sound card name (too big to show)\n");
3304 }
3305 else {
3306 fprintf (stderr, "Unknown sound card name `%.*s'\n",
3307 (int) l, p);
3308 }
3309 bad_card = 1;
3310 }
3311 p += l + (e != NULL);
3312 }
3313
3314 if (bad_card)
3315 goto show_valid_cards;
3316 }
3317}
3318#endif
3319
bellardc20709a2004-04-21 23:27:19 +00003320#define NET_IF_TUN 0
3321#define NET_IF_USER 1
3322#define NET_IF_DUMMY 2
3323
bellard0824d6f2003-06-24 13:42:40 +00003324int main(int argc, char **argv)
3325{
bellard67b915a2004-03-31 23:37:16 +00003326#ifdef CONFIG_GDBSTUB
3327 int use_gdbstub, gdbstub_port;
3328#endif
bellardcc1daa42005-06-05 14:49:17 +00003329 int i, cdrom_index;
bellard1ccde1c2004-02-06 19:46:14 +00003330 int snapshot, linux_boot;
bellardc45886d2004-01-05 00:02:06 +00003331 CPUState *env;
bellard7f7f9872003-10-30 01:11:23 +00003332 const char *initrd_filename;
bellardc45886d2004-01-05 00:02:06 +00003333 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
bellarda20dd502003-09-30 21:07:02 +00003334 const char *kernel_filename, *kernel_cmdline;
bellard313aa562003-08-10 21:52:11 +00003335 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00003336 int cyls, heads, secs, translation;
bellarda541f292004-04-12 20:39:29 +00003337 int start_emulation = 1;
bellard702c6512004-04-02 21:21:32 +00003338 uint8_t macaddr[6];
bellardc20709a2004-04-21 23:27:19 +00003339 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
bellardcd6f1162004-05-13 22:02:20 +00003340 int optind;
3341 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00003342 CharDriverState *monitor_hd;
3343 char monitor_device[128];
bellard8d11df92004-08-24 21:13:40 +00003344 char serial_devices[MAX_SERIAL_PORTS][128];
3345 int serial_device_index;
bellard6508fe52005-01-15 12:02:56 +00003346 char parallel_devices[MAX_PARALLEL_PORTS][128];
3347 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00003348 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003349 QEMUMachine *machine;
bellarda594cfb2005-11-06 16:13:29 +00003350 char usb_devices[MAX_VM_USB_PORTS][128];
3351 int usb_devices_index;
3352
bellard67b915a2004-03-31 23:37:16 +00003353#if !defined(CONFIG_SOFTMMU)
bellard0824d6f2003-06-24 13:42:40 +00003354 /* we never want that malloc() uses mmap() */
3355 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
bellard67b915a2004-03-31 23:37:16 +00003356#endif
bellardcc1daa42005-06-05 14:49:17 +00003357 register_machines();
3358 machine = first_machine;
bellardfc01f7e2003-06-30 10:03:06 +00003359 initrd_filename = NULL;
bellardc45886d2004-01-05 00:02:06 +00003360 for(i = 0; i < MAX_FD; i++)
3361 fd_filename[i] = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00003362 for(i = 0; i < MAX_DISKS; i++)
3363 hd_filename[i] = NULL;
bellarda00bad72004-05-22 21:39:06 +00003364 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
bellard313aa562003-08-10 21:52:11 +00003365 vga_ram_size = VGA_RAM_SIZE;
bellard0ced6582004-05-23 21:06:12 +00003366 bios_size = BIOS_SIZE;
bellardf1510b22003-06-25 00:07:40 +00003367 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
bellard67b915a2004-03-31 23:37:16 +00003368#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00003369 use_gdbstub = 0;
3370 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00003371#endif
bellard33e39632003-07-06 17:15:21 +00003372 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00003373 nographic = 0;
3374 kernel_filename = NULL;
3375 kernel_cmdline = "";
bellardcc1daa42005-06-05 14:49:17 +00003376#ifdef TARGET_PPC
3377 cdrom_index = 1;
3378#else
3379 cdrom_index = 2;
3380#endif
bellardc4b1fcc2004-03-14 21:44:30 +00003381 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00003382 translation = BIOS_ATA_TRANSLATION_AUTO;
bellard82c643f2004-07-14 17:28:13 +00003383 pstrcpy(monitor_device, sizeof(monitor_device), "vc");
bellardc4b1fcc2004-03-14 21:44:30 +00003384
bellard8d11df92004-08-24 21:13:40 +00003385 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc");
3386 for(i = 1; i < MAX_SERIAL_PORTS; i++)
3387 serial_devices[i][0] = '\0';
3388 serial_device_index = 0;
3389
bellard6508fe52005-01-15 12:02:56 +00003390 pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");
3391 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
3392 parallel_devices[i][0] = '\0';
3393 parallel_device_index = 0;
3394
bellarda594cfb2005-11-06 16:13:29 +00003395 usb_devices_index = 0;
3396
bellardc20709a2004-04-21 23:27:19 +00003397 nb_tun_fds = 0;
3398 net_if_type = -1;
bellardc4b1fcc2004-03-14 21:44:30 +00003399 nb_nics = 1;
bellard702c6512004-04-02 21:21:32 +00003400 /* default mac address of the first network interface */
3401 macaddr[0] = 0x52;
3402 macaddr[1] = 0x54;
3403 macaddr[2] = 0x00;
3404 macaddr[3] = 0x12;
3405 macaddr[4] = 0x34;
3406 macaddr[5] = 0x56;
bellard82c643f2004-07-14 17:28:13 +00003407
bellardcd6f1162004-05-13 22:02:20 +00003408 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00003409 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00003410 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00003411 break;
bellardcd6f1162004-05-13 22:02:20 +00003412 r = argv[optind];
3413 if (r[0] != '-') {
3414 hd_filename[0] = argv[optind++];
3415 } else {
3416 const QEMUOption *popt;
3417
3418 optind++;
3419 popt = qemu_options;
3420 for(;;) {
3421 if (!popt->name) {
3422 fprintf(stderr, "%s: invalid option -- '%s'\n",
3423 argv[0], r);
3424 exit(1);
3425 }
3426 if (!strcmp(popt->name, r + 1))
3427 break;
3428 popt++;
3429 }
3430 if (popt->flags & HAS_ARG) {
3431 if (optind >= argc) {
3432 fprintf(stderr, "%s: option '%s' requires an argument\n",
3433 argv[0], r);
3434 exit(1);
3435 }
3436 optarg = argv[optind++];
3437 } else {
3438 optarg = NULL;
3439 }
3440
3441 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00003442 case QEMU_OPTION_M:
3443 machine = find_machine(optarg);
3444 if (!machine) {
3445 QEMUMachine *m;
3446 printf("Supported machines are:\n");
3447 for(m = first_machine; m != NULL; m = m->next) {
3448 printf("%-10s %s%s\n",
3449 m->name, m->desc,
3450 m == first_machine ? " (default)" : "");
3451 }
3452 exit(1);
3453 }
3454 break;
bellardcd6f1162004-05-13 22:02:20 +00003455 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00003456 initrd_filename = optarg;
3457 break;
bellardcd6f1162004-05-13 22:02:20 +00003458 case QEMU_OPTION_hda:
bellardcd6f1162004-05-13 22:02:20 +00003459 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00003460 case QEMU_OPTION_hdc:
3461 case QEMU_OPTION_hdd:
3462 {
3463 int hd_index;
3464 hd_index = popt->index - QEMU_OPTION_hda;
3465 hd_filename[hd_index] = optarg;
3466 if (hd_index == cdrom_index)
3467 cdrom_index = -1;
3468 }
bellardfc01f7e2003-06-30 10:03:06 +00003469 break;
bellardcd6f1162004-05-13 22:02:20 +00003470 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00003471 snapshot = 1;
3472 break;
bellardcd6f1162004-05-13 22:02:20 +00003473 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00003474 {
bellard330d0412003-07-26 18:11:40 +00003475 const char *p;
3476 p = optarg;
3477 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003478 if (cyls < 1 || cyls > 16383)
3479 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003480 if (*p != ',')
3481 goto chs_fail;
3482 p++;
3483 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003484 if (heads < 1 || heads > 16)
3485 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00003486 if (*p != ',')
3487 goto chs_fail;
3488 p++;
3489 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00003490 if (secs < 1 || secs > 63)
3491 goto chs_fail;
3492 if (*p == ',') {
3493 p++;
3494 if (!strcmp(p, "none"))
3495 translation = BIOS_ATA_TRANSLATION_NONE;
3496 else if (!strcmp(p, "lba"))
3497 translation = BIOS_ATA_TRANSLATION_LBA;
3498 else if (!strcmp(p, "auto"))
3499 translation = BIOS_ATA_TRANSLATION_AUTO;
3500 else
3501 goto chs_fail;
3502 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00003503 chs_fail:
bellard46d47672004-11-16 01:45:27 +00003504 fprintf(stderr, "qemu: invalid physical CHS format\n");
3505 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00003506 }
bellard330d0412003-07-26 18:11:40 +00003507 }
3508 break;
bellardcd6f1162004-05-13 22:02:20 +00003509 case QEMU_OPTION_nographic:
bellard82c643f2004-07-14 17:28:13 +00003510 pstrcpy(monitor_device, sizeof(monitor_device), "stdio");
bellard8d11df92004-08-24 21:13:40 +00003511 pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio");
bellarda20dd502003-09-30 21:07:02 +00003512 nographic = 1;
3513 break;
bellardcd6f1162004-05-13 22:02:20 +00003514 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00003515 kernel_filename = optarg;
3516 break;
bellardcd6f1162004-05-13 22:02:20 +00003517 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00003518 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00003519 break;
bellardcd6f1162004-05-13 22:02:20 +00003520 case QEMU_OPTION_tun_fd:
bellardc4b1fcc2004-03-14 21:44:30 +00003521 {
3522 const char *p;
3523 int fd;
bellardd6b86f42004-05-12 19:59:20 +00003524 net_if_type = NET_IF_TUN;
bellardc20709a2004-04-21 23:27:19 +00003525 if (nb_tun_fds < MAX_NICS) {
3526 fd = strtol(optarg, (char **)&p, 0);
3527 if (*p != '\0') {
3528 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
bellardc4b1fcc2004-03-14 21:44:30 +00003529 exit(1);
3530 }
bellardc20709a2004-04-21 23:27:19 +00003531 tun_fds[nb_tun_fds++] = fd;
bellardc4b1fcc2004-03-14 21:44:30 +00003532 }
3533 }
bellard42f1e0e2003-09-30 22:11:17 +00003534 break;
bellardcd6f1162004-05-13 22:02:20 +00003535 case QEMU_OPTION_cdrom:
bellardcc1daa42005-06-05 14:49:17 +00003536 if (cdrom_index >= 0) {
3537 hd_filename[cdrom_index] = optarg;
3538 }
bellard36b486b2003-11-11 13:36:08 +00003539 break;
bellardcd6f1162004-05-13 22:02:20 +00003540 case QEMU_OPTION_boot:
bellard36b486b2003-11-11 13:36:08 +00003541 boot_device = optarg[0];
bellard9e89a4b2004-11-14 15:42:27 +00003542 if (boot_device != 'a' &&
bellard6f7e9ae2005-03-13 09:43:36 +00003543#ifdef TARGET_SPARC
3544 // Network boot
3545 boot_device != 'n' &&
3546#endif
bellardc45886d2004-01-05 00:02:06 +00003547 boot_device != 'c' && boot_device != 'd') {
bellard36b486b2003-11-11 13:36:08 +00003548 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
3549 exit(1);
3550 }
3551 break;
bellardcd6f1162004-05-13 22:02:20 +00003552 case QEMU_OPTION_fda:
bellardc45886d2004-01-05 00:02:06 +00003553 fd_filename[0] = optarg;
3554 break;
bellardcd6f1162004-05-13 22:02:20 +00003555 case QEMU_OPTION_fdb:
bellardc45886d2004-01-05 00:02:06 +00003556 fd_filename[1] = optarg;
3557 break;
bellardcd6f1162004-05-13 22:02:20 +00003558 case QEMU_OPTION_no_code_copy:
bellard77fef8c2004-02-16 22:05:46 +00003559 code_copy_enabled = 0;
3560 break;
bellardcd6f1162004-05-13 22:02:20 +00003561 case QEMU_OPTION_nics:
bellardc4b1fcc2004-03-14 21:44:30 +00003562 nb_nics = atoi(optarg);
bellard3a1bc172004-05-12 19:54:43 +00003563 if (nb_nics < 0 || nb_nics > MAX_NICS) {
bellardc4b1fcc2004-03-14 21:44:30 +00003564 fprintf(stderr, "qemu: invalid number of network interfaces\n");
3565 exit(1);
3566 }
3567 break;
bellardcd6f1162004-05-13 22:02:20 +00003568 case QEMU_OPTION_macaddr:
bellard702c6512004-04-02 21:21:32 +00003569 {
3570 const char *p;
3571 int i;
3572 p = optarg;
3573 for(i = 0; i < 6; i++) {
3574 macaddr[i] = strtol(p, (char **)&p, 16);
3575 if (i == 5) {
3576 if (*p != '\0')
3577 goto macaddr_error;
3578 } else {
3579 if (*p != ':') {
3580 macaddr_error:
3581 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
3582 exit(1);
3583 }
3584 p++;
3585 }
3586 }
3587 }
3588 break;
bellardc7f74642004-08-24 21:57:12 +00003589#ifdef CONFIG_SLIRP
3590 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00003591 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00003592 break;
bellardc94c8d62004-09-13 21:37:34 +00003593#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00003594 case QEMU_OPTION_smb:
3595 net_slirp_smb(optarg);
3596 break;
bellardc94c8d62004-09-13 21:37:34 +00003597#endif
bellardcd6f1162004-05-13 22:02:20 +00003598 case QEMU_OPTION_user_net:
bellardc20709a2004-04-21 23:27:19 +00003599 net_if_type = NET_IF_USER;
3600 break;
bellard9bf05442004-08-25 22:12:49 +00003601 case QEMU_OPTION_redir:
3602 net_slirp_redir(optarg);
3603 break;
bellardc7f74642004-08-24 21:57:12 +00003604#endif
bellardcd6f1162004-05-13 22:02:20 +00003605 case QEMU_OPTION_dummy_net:
bellardc20709a2004-04-21 23:27:19 +00003606 net_if_type = NET_IF_DUMMY;
3607 break;
bellard1d14ffa2005-10-30 18:58:22 +00003608#ifdef HAS_AUDIO
bellardcd6f1162004-05-13 22:02:20 +00003609 case QEMU_OPTION_enable_audio:
bellardaaaa7df2004-04-26 20:56:53 +00003610 audio_enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00003611 sb16_enabled = 1;
3612 adlib_enabled = 1;
3613 gus_enabled = 1;
3614 es1370_enabled = 1;
bellardaaaa7df2004-04-26 20:56:53 +00003615 break;
bellard1d14ffa2005-10-30 18:58:22 +00003616 case QEMU_OPTION_audio_help:
3617 AUD_help ();
3618 exit (0);
3619 break;
3620 case QEMU_OPTION_soundhw:
3621 select_soundhw (optarg);
3622 break;
3623#endif
bellardcd6f1162004-05-13 22:02:20 +00003624 case QEMU_OPTION_h:
bellard0824d6f2003-06-24 13:42:40 +00003625 help();
bellardcd6f1162004-05-13 22:02:20 +00003626 break;
3627 case QEMU_OPTION_m:
3628 ram_size = atoi(optarg) * 1024 * 1024;
3629 if (ram_size <= 0)
3630 help();
3631 if (ram_size > PHYS_RAM_MAX_SIZE) {
3632 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
3633 PHYS_RAM_MAX_SIZE / (1024 * 1024));
3634 exit(1);
3635 }
3636 break;
3637 case QEMU_OPTION_d:
3638 {
3639 int mask;
3640 CPULogItem *item;
3641
3642 mask = cpu_str_to_log_mask(optarg);
3643 if (!mask) {
3644 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00003645 for(item = cpu_log_items; item->mask != 0; item++) {
3646 printf("%-10s %s\n", item->name, item->help);
3647 }
3648 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00003649 }
3650 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00003651 }
bellardcd6f1162004-05-13 22:02:20 +00003652 break;
3653 case QEMU_OPTION_n:
3654 pstrcpy(network_script, sizeof(network_script), optarg);
3655 break;
bellard67b915a2004-03-31 23:37:16 +00003656#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00003657 case QEMU_OPTION_s:
3658 use_gdbstub = 1;
3659 break;
3660 case QEMU_OPTION_p:
3661 gdbstub_port = atoi(optarg);
3662 break;
bellard67b915a2004-03-31 23:37:16 +00003663#endif
bellardcd6f1162004-05-13 22:02:20 +00003664 case QEMU_OPTION_L:
3665 bios_dir = optarg;
3666 break;
3667 case QEMU_OPTION_S:
3668 start_emulation = 0;
3669 break;
bellard3d11d0e2004-12-12 16:56:30 +00003670 case QEMU_OPTION_k:
3671 keyboard_layout = optarg;
3672 break;
bellardee22c2f2004-06-03 12:49:50 +00003673 case QEMU_OPTION_localtime:
3674 rtc_utc = 0;
3675 break;
bellard1f042752004-06-05 13:46:47 +00003676 case QEMU_OPTION_cirrusvga:
3677 cirrus_vga_enabled = 1;
3678 break;
bellard1bfe8562004-07-08 21:17:50 +00003679 case QEMU_OPTION_std_vga:
3680 cirrus_vga_enabled = 0;
3681 break;
bellarde9b137c2004-06-21 16:46:10 +00003682 case QEMU_OPTION_g:
3683 {
3684 const char *p;
3685 int w, h, depth;
3686 p = optarg;
3687 w = strtol(p, (char **)&p, 10);
3688 if (w <= 0) {
3689 graphic_error:
3690 fprintf(stderr, "qemu: invalid resolution or depth\n");
3691 exit(1);
3692 }
3693 if (*p != 'x')
3694 goto graphic_error;
3695 p++;
3696 h = strtol(p, (char **)&p, 10);
3697 if (h <= 0)
3698 goto graphic_error;
3699 if (*p == 'x') {
3700 p++;
3701 depth = strtol(p, (char **)&p, 10);
3702 if (depth != 8 && depth != 15 && depth != 16 &&
3703 depth != 24 && depth != 32)
3704 goto graphic_error;
3705 } else if (*p == '\0') {
3706 depth = graphic_depth;
3707 } else {
3708 goto graphic_error;
3709 }
3710
3711 graphic_width = w;
3712 graphic_height = h;
3713 graphic_depth = depth;
3714 }
3715 break;
bellard82c643f2004-07-14 17:28:13 +00003716 case QEMU_OPTION_monitor:
3717 pstrcpy(monitor_device, sizeof(monitor_device), optarg);
3718 break;
3719 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00003720 if (serial_device_index >= MAX_SERIAL_PORTS) {
3721 fprintf(stderr, "qemu: too many serial ports\n");
3722 exit(1);
3723 }
3724 pstrcpy(serial_devices[serial_device_index],
3725 sizeof(serial_devices[0]), optarg);
3726 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00003727 break;
bellard6508fe52005-01-15 12:02:56 +00003728 case QEMU_OPTION_parallel:
3729 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
3730 fprintf(stderr, "qemu: too many parallel ports\n");
3731 exit(1);
3732 }
3733 pstrcpy(parallel_devices[parallel_device_index],
3734 sizeof(parallel_devices[0]), optarg);
3735 parallel_device_index++;
3736 break;
bellardd63d3072004-10-03 13:29:03 +00003737 case QEMU_OPTION_loadvm:
3738 loadvm = optarg;
3739 break;
3740 case QEMU_OPTION_full_screen:
3741 full_screen = 1;
3742 break;
bellardf7cce892004-12-08 22:21:25 +00003743 case QEMU_OPTION_pidfile:
3744 create_pidfile(optarg);
3745 break;
bellarda09db212005-04-30 16:10:35 +00003746#ifdef TARGET_I386
3747 case QEMU_OPTION_win2k_hack:
3748 win2k_install_hack = 1;
3749 break;
3750#endif
bellardd993e022005-02-10 22:00:06 +00003751#ifdef USE_KQEMU
3752 case QEMU_OPTION_no_kqemu:
3753 kqemu_allowed = 0;
3754 break;
3755#endif
bellardbb36d472005-11-05 14:22:28 +00003756 case QEMU_OPTION_usb:
3757 usb_enabled = 1;
3758 break;
bellarda594cfb2005-11-06 16:13:29 +00003759 case QEMU_OPTION_usbdevice:
3760 usb_enabled = 1;
3761 if (usb_devices_index >= MAX_VM_USB_PORTS) {
3762 fprintf(stderr, "Too many USB devices\n");
3763 exit(1);
3764 }
3765 pstrcpy(usb_devices[usb_devices_index],
3766 sizeof(usb_devices[usb_devices_index]),
3767 optarg);
3768 usb_devices_index++;
3769 break;
bellardcd6f1162004-05-13 22:02:20 +00003770 }
bellard0824d6f2003-06-24 13:42:40 +00003771 }
3772 }
bellard330d0412003-07-26 18:11:40 +00003773
bellarda20dd502003-09-30 21:07:02 +00003774 linux_boot = (kernel_filename != NULL);
bellard330d0412003-07-26 18:11:40 +00003775
bellardcc1daa42005-06-05 14:49:17 +00003776 if (!linux_boot &&
3777 hd_filename[0] == '\0' &&
3778 (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') &&
bellardc45886d2004-01-05 00:02:06 +00003779 fd_filename[0] == '\0')
bellard0824d6f2003-06-24 13:42:40 +00003780 help();
bellard8f2b1fb2003-11-16 19:46:01 +00003781
3782 /* boot to cd by default if no hard disk */
bellardd0309312004-01-18 22:35:25 +00003783 if (hd_filename[0] == '\0' && boot_device == 'c') {
3784 if (fd_filename[0] != '\0')
3785 boot_device = 'a';
3786 else
3787 boot_device = 'd';
3788 }
bellard0824d6f2003-06-24 13:42:40 +00003789
bellarddc887a42004-01-04 18:18:57 +00003790#if !defined(CONFIG_SOFTMMU)
3791 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3792 {
3793 static uint8_t stdout_buf[4096];
3794 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
3795 }
3796#else
bellardb118d612003-06-30 23:36:21 +00003797 setvbuf(stdout, NULL, _IOLBF, 0);
bellarddc887a42004-01-04 18:18:57 +00003798#endif
bellard0824d6f2003-06-24 13:42:40 +00003799
bellardc4b1fcc2004-03-14 21:44:30 +00003800 /* init host network redirectors */
bellardc20709a2004-04-21 23:27:19 +00003801 if (net_if_type == -1) {
3802 net_if_type = NET_IF_TUN;
3803#if defined(CONFIG_SLIRP)
3804 if (access(network_script, R_OK) < 0) {
3805 net_if_type = NET_IF_USER;
3806 }
3807#endif
3808 }
3809
3810 for(i = 0; i < nb_nics; i++) {
bellard702c6512004-04-02 21:21:32 +00003811 NetDriverState *nd = &nd_table[i];
bellardc20709a2004-04-21 23:27:19 +00003812 nd->index = i;
bellard702c6512004-04-02 21:21:32 +00003813 /* init virtual mac address */
3814 nd->macaddr[0] = macaddr[0];
3815 nd->macaddr[1] = macaddr[1];
3816 nd->macaddr[2] = macaddr[2];
3817 nd->macaddr[3] = macaddr[3];
3818 nd->macaddr[4] = macaddr[4];
3819 nd->macaddr[5] = macaddr[5] + i;
bellardc20709a2004-04-21 23:27:19 +00003820 switch(net_if_type) {
3821#if defined(CONFIG_SLIRP)
3822 case NET_IF_USER:
3823 net_slirp_init(nd);
3824 break;
3825#endif
3826#if !defined(_WIN32)
3827 case NET_IF_TUN:
3828 if (i < nb_tun_fds) {
3829 net_fd_init(nd, tun_fds[i]);
3830 } else {
bellardd9276372004-04-26 19:44:57 +00003831 if (net_tun_init(nd) < 0)
3832 net_dummy_init(nd);
bellardc20709a2004-04-21 23:27:19 +00003833 }
3834 break;
3835#endif
3836 case NET_IF_DUMMY:
3837 default:
3838 net_dummy_init(nd);
3839 break;
3840 }
bellard702c6512004-04-02 21:21:32 +00003841 }
bellardf1510b22003-06-25 00:07:40 +00003842
bellard0824d6f2003-06-24 13:42:40 +00003843 /* init the memory */
bellard0ced6582004-05-23 21:06:12 +00003844 phys_ram_size = ram_size + vga_ram_size + bios_size;
bellard7f7f9872003-10-30 01:11:23 +00003845
3846#ifdef CONFIG_SOFTMMU
bellardd993e022005-02-10 22:00:06 +00003847 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00003848 if (!phys_ram_base) {
3849 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00003850 exit(1);
3851 }
bellard7f7f9872003-10-30 01:11:23 +00003852#else
3853 /* as we must map the same page at several addresses, we must use
3854 a fd */
3855 {
3856 const char *tmpdir;
3857
3858 tmpdir = getenv("QEMU_TMPDIR");
3859 if (!tmpdir)
3860 tmpdir = "/tmp";
3861 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
3862 if (mkstemp(phys_ram_file) < 0) {
3863 fprintf(stderr, "Could not create temporary memory file '%s'\n",
3864 phys_ram_file);
3865 exit(1);
3866 }
3867 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
3868 if (phys_ram_fd < 0) {
3869 fprintf(stderr, "Could not open temporary memory file '%s'\n",
3870 phys_ram_file);
3871 exit(1);
3872 }
bellard1ccde1c2004-02-06 19:46:14 +00003873 ftruncate(phys_ram_fd, phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00003874 unlink(phys_ram_file);
bellard1ccde1c2004-02-06 19:46:14 +00003875 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
3876 phys_ram_size,
bellard7f7f9872003-10-30 01:11:23 +00003877 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
3878 phys_ram_fd, 0);
3879 if (phys_ram_base == MAP_FAILED) {
3880 fprintf(stderr, "Could not map physical memory\n");
3881 exit(1);
3882 }
3883 }
3884#endif
bellard0824d6f2003-06-24 13:42:40 +00003885
bellardc4b1fcc2004-03-14 21:44:30 +00003886 /* we always create the cdrom drive, even if no disk is there */
bellard5905b2e2004-08-01 21:53:26 +00003887 bdrv_init();
bellardcc1daa42005-06-05 14:49:17 +00003888 if (cdrom_index >= 0) {
3889 bs_table[cdrom_index] = bdrv_new("cdrom");
3890 bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM);
bellardc4b1fcc2004-03-14 21:44:30 +00003891 }
3892
bellard33e39632003-07-06 17:15:21 +00003893 /* open the virtual block devices */
3894 for(i = 0; i < MAX_DISKS; i++) {
3895 if (hd_filename[i]) {
bellard33e39632003-07-06 17:15:21 +00003896 if (!bs_table[i]) {
bellardc4b1fcc2004-03-14 21:44:30 +00003897 char buf[64];
3898 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
3899 bs_table[i] = bdrv_new(buf);
3900 }
3901 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
bellard5905b2e2004-08-01 21:53:26 +00003902 fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
bellard33e39632003-07-06 17:15:21 +00003903 hd_filename[i]);
3904 exit(1);
3905 }
bellard46d47672004-11-16 01:45:27 +00003906 if (i == 0 && cyls != 0) {
bellardc4b1fcc2004-03-14 21:44:30 +00003907 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
bellard46d47672004-11-16 01:45:27 +00003908 bdrv_set_translation_hint(bs_table[i], translation);
3909 }
bellardc4b1fcc2004-03-14 21:44:30 +00003910 }
3911 }
3912
3913 /* we always create at least one floppy disk */
3914 fd_table[0] = bdrv_new("fda");
3915 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
3916
3917 for(i = 0; i < MAX_FD; i++) {
3918 if (fd_filename[i]) {
3919 if (!fd_table[i]) {
3920 char buf[64];
3921 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
3922 fd_table[i] = bdrv_new(buf);
3923 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
3924 }
3925 if (fd_filename[i] != '\0') {
3926 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
bellardc20709a2004-04-21 23:27:19 +00003927 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
bellardc4b1fcc2004-03-14 21:44:30 +00003928 fd_filename[i]);
3929 exit(1);
3930 }
3931 }
bellard33e39632003-07-06 17:15:21 +00003932 }
3933 }
3934
bellarda594cfb2005-11-06 16:13:29 +00003935 /* init USB devices */
3936 if (usb_enabled) {
3937 vm_usb_hub = usb_hub_init(vm_usb_ports, MAX_VM_USB_PORTS);
3938 for(i = 0; i < usb_devices_index; i++) {
3939 if (usb_device_add(usb_devices[i]) < 0) {
3940 fprintf(stderr, "Warning: could not add USB device %s\n",
3941 usb_devices[i]);
3942 }
3943 }
3944 }
3945
bellard330d0412003-07-26 18:11:40 +00003946 /* init CPU state */
3947 env = cpu_init();
3948 global_env = env;
3949 cpu_single_env = env;
bellard0824d6f2003-06-24 13:42:40 +00003950
bellard8a7ddc32004-03-31 19:00:16 +00003951 register_savevm("timer", 0, 1, timer_save, timer_load, env);
bellard664e0f12005-01-08 18:58:29 +00003952 register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
bellard8a7ddc32004-03-31 19:00:16 +00003953 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
bellardbb0c6722004-06-20 12:37:32 +00003954 qemu_register_reset(main_cpu_reset, global_env);
bellard8a7ddc32004-03-31 19:00:16 +00003955
bellard330d0412003-07-26 18:11:40 +00003956 init_ioports();
bellard80cabfa2004-03-14 12:20:30 +00003957 cpu_calibrate_ticks();
bellard0824d6f2003-06-24 13:42:40 +00003958
bellard313aa562003-08-10 21:52:11 +00003959 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00003960 if (nographic) {
bellard313aa562003-08-10 21:52:11 +00003961 dumb_display_init(ds);
3962 } else {
bellard5b0753e2005-03-01 21:37:28 +00003963#if defined(CONFIG_SDL)
bellardd63d3072004-10-03 13:29:03 +00003964 sdl_display_init(ds, full_screen);
bellard5b0753e2005-03-01 21:37:28 +00003965#elif defined(CONFIG_COCOA)
3966 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00003967#else
3968 dumb_display_init(ds);
3969#endif
3970 }
bellard0824d6f2003-06-24 13:42:40 +00003971
bellard82c643f2004-07-14 17:28:13 +00003972 vga_console = graphic_console_init(ds);
3973
3974 monitor_hd = qemu_chr_open(monitor_device);
3975 if (!monitor_hd) {
3976 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
3977 exit(1);
3978 }
3979 monitor_init(monitor_hd, !nographic);
3980
bellard8d11df92004-08-24 21:13:40 +00003981 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
3982 if (serial_devices[i][0] != '\0') {
3983 serial_hds[i] = qemu_chr_open(serial_devices[i]);
3984 if (!serial_hds[i]) {
3985 fprintf(stderr, "qemu: could not open serial device '%s'\n",
3986 serial_devices[i]);
3987 exit(1);
3988 }
3989 if (!strcmp(serial_devices[i], "vc"))
3990 qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
3991 }
bellard82c643f2004-07-14 17:28:13 +00003992 }
bellard82c643f2004-07-14 17:28:13 +00003993
bellard6508fe52005-01-15 12:02:56 +00003994 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
3995 if (parallel_devices[i][0] != '\0') {
3996 parallel_hds[i] = qemu_chr_open(parallel_devices[i]);
3997 if (!parallel_hds[i]) {
3998 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
3999 parallel_devices[i]);
4000 exit(1);
4001 }
4002 if (!strcmp(parallel_devices[i], "vc"))
4003 qemu_chr_printf(parallel_hds[i], "parallel%d console\n", i);
4004 }
4005 }
4006
bellard0824d6f2003-06-24 13:42:40 +00004007 /* setup cpu signal handlers for MMU / self modifying code handling */
bellard77fef8c2004-02-16 22:05:46 +00004008#if !defined(CONFIG_SOFTMMU)
bellard8a7ddc32004-03-31 19:00:16 +00004009
bellard77fef8c2004-02-16 22:05:46 +00004010#if defined (TARGET_I386) && defined(USE_CODE_COPY)
4011 {
4012 stack_t stk;
bellard73332e52004-04-04 20:22:28 +00004013 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
bellard77fef8c2004-02-16 22:05:46 +00004014 stk.ss_sp = signal_stack;
4015 stk.ss_size = SIGNAL_STACK_SIZE;
4016 stk.ss_flags = 0;
4017
4018 if (sigaltstack(&stk, NULL) < 0) {
4019 perror("sigaltstack");
4020 exit(1);
4021 }
4022 }
4023#endif
bellard8a7ddc32004-03-31 19:00:16 +00004024 {
4025 struct sigaction act;
bellard77fef8c2004-02-16 22:05:46 +00004026
bellard8a7ddc32004-03-31 19:00:16 +00004027 sigfillset(&act.sa_mask);
4028 act.sa_flags = SA_SIGINFO;
bellard77fef8c2004-02-16 22:05:46 +00004029#if defined (TARGET_I386) && defined(USE_CODE_COPY)
bellard8a7ddc32004-03-31 19:00:16 +00004030 act.sa_flags |= SA_ONSTACK;
bellard77fef8c2004-02-16 22:05:46 +00004031#endif
bellard8a7ddc32004-03-31 19:00:16 +00004032 act.sa_sigaction = host_segv_handler;
4033 sigaction(SIGSEGV, &act, NULL);
4034 sigaction(SIGBUS, &act, NULL);
bellard77fef8c2004-02-16 22:05:46 +00004035#if defined (TARGET_I386) && defined(USE_CODE_COPY)
bellard8a7ddc32004-03-31 19:00:16 +00004036 sigaction(SIGFPE, &act, NULL);
bellard77fef8c2004-02-16 22:05:46 +00004037#endif
bellard8a7ddc32004-03-31 19:00:16 +00004038 }
bellard3a51dee2003-10-27 21:18:35 +00004039#endif
bellard0824d6f2003-06-24 13:42:40 +00004040
bellard67b915a2004-03-31 23:37:16 +00004041#ifndef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00004042 {
4043 struct sigaction act;
4044 sigfillset(&act.sa_mask);
4045 act.sa_flags = 0;
4046 act.sa_handler = SIG_IGN;
4047 sigaction(SIGPIPE, &act, NULL);
4048 }
bellard67b915a2004-03-31 23:37:16 +00004049#endif
bellard73332e52004-04-04 20:22:28 +00004050 init_timers();
4051
bellardcc1daa42005-06-05 14:49:17 +00004052 machine->init(ram_size, vga_ram_size, boot_device,
4053 ds, fd_filename, snapshot,
4054 kernel_filename, kernel_cmdline, initrd_filename);
bellard73332e52004-04-04 20:22:28 +00004055
bellard8a7ddc32004-03-31 19:00:16 +00004056 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
4057 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
bellard7f7f9872003-10-30 01:11:23 +00004058
bellard67b915a2004-03-31 23:37:16 +00004059#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004060 if (use_gdbstub) {
bellard8a7ddc32004-03-31 19:00:16 +00004061 if (gdbserver_start(gdbstub_port) < 0) {
4062 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
4063 gdbstub_port);
4064 exit(1);
4065 } else {
4066 printf("Waiting gdb connection on port %d\n", gdbstub_port);
4067 }
bellard67b915a2004-03-31 23:37:16 +00004068 } else
4069#endif
bellardd63d3072004-10-03 13:29:03 +00004070 if (loadvm)
4071 qemu_loadvm(loadvm);
4072
bellard67b915a2004-03-31 23:37:16 +00004073 {
bellard5905b2e2004-08-01 21:53:26 +00004074 /* XXX: simplify init */
4075 read_passwords();
4076 if (start_emulation) {
4077 vm_start();
4078 }
bellard0824d6f2003-06-24 13:42:40 +00004079 }
bellard8a7ddc32004-03-31 19:00:16 +00004080 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00004081 quit_timers();
bellard0824d6f2003-06-24 13:42:40 +00004082 return 0;
4083}