blob: 8472f0a230adaa8f80bd85de647fddcc68ebc385 [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 *
bellard80cabfa2004-03-14 12:20:30 +00004 * Copyright (c) 2003-2004 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 */
24#include <stdlib.h>
25#include <stdio.h>
bellard1df912c2003-06-25 16:20:35 +000026#include <stdarg.h>
bellard0824d6f2003-06-24 13:42:40 +000027#include <string.h>
bellardc45886d2004-01-05 00:02:06 +000028#include <ctype.h>
bellard0824d6f2003-06-24 13:42:40 +000029#include <getopt.h>
30#include <inttypes.h>
31#include <unistd.h>
32#include <sys/mman.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <time.h>
36#include <sys/time.h>
37#include <malloc.h>
38#include <termios.h>
39#include <sys/poll.h>
40#include <errno.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/wait.h>
bellardc4b1fcc2004-03-14 21:44:30 +000042#include <pty.h>
bellardf1510b22003-06-25 00:07:40 +000043
44#include <sys/ioctl.h>
45#include <sys/socket.h>
46#include <linux/if.h>
47#include <linux/if_tun.h>
bellard0824d6f2003-06-24 13:42:40 +000048
bellard0824d6f2003-06-24 13:42:40 +000049#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +000050#include "thunk.h"
51
52#include "vl.h"
bellard0824d6f2003-06-24 13:42:40 +000053
bellard5a671352003-10-01 00:13:48 +000054#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
bellardf1510b22003-06-25 00:07:40 +000055
bellard0824d6f2003-06-24 13:42:40 +000056//#define DEBUG_UNUSED_IOPORT
bellard330d0412003-07-26 18:11:40 +000057
bellardbb551fa2004-01-24 13:42:26 +000058#if !defined(CONFIG_SOFTMMU)
bellard7916e222003-07-01 16:27:45 +000059#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
bellardbb551fa2004-01-24 13:42:26 +000060#else
61#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
62#endif
bellard7916e222003-07-01 16:27:45 +000063
bellardc45886d2004-01-05 00:02:06 +000064#if defined (TARGET_I386)
bellardc45886d2004-01-05 00:02:06 +000065#elif defined (TARGET_PPC)
66//#define USE_OPEN_FIRMWARE
bellard3f5dcc32004-01-18 22:44:01 +000067#if !defined (USE_OPEN_FIRMWARE)
bellardc45886d2004-01-05 00:02:06 +000068#define KERNEL_LOAD_ADDR 0x01000000
69#define KERNEL_STACK_ADDR 0x01200000
70#else
71#define KERNEL_LOAD_ADDR 0x00000000
72#define KERNEL_STACK_ADDR 0x00400000
73#endif
74#endif
bellard0824d6f2003-06-24 13:42:40 +000075
bellard313aa562003-08-10 21:52:11 +000076#define GUI_REFRESH_INTERVAL 30
77
bellard7dea1da2003-11-16 15:59:30 +000078/* XXX: use a two level table to limit memory usage */
79#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +000080
bellard80cabfa2004-03-14 12:20:30 +000081const char *bios_dir = CONFIG_QEMU_SHAREDIR;
bellard0824d6f2003-06-24 13:42:40 +000082char phys_ram_file[1024];
bellardc45886d2004-01-05 00:02:06 +000083CPUState *global_env;
84CPUState *cpu_single_env;
bellardc4b1fcc2004-03-14 21:44:30 +000085void *ioport_opaque[MAX_IOPORTS];
bellardfc01f7e2003-06-30 10:03:06 +000086IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
87IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
bellardc45886d2004-01-05 00:02:06 +000088BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
bellard313aa562003-08-10 21:52:11 +000089int vga_ram_size;
90static DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +000091int nographic;
bellard313aa562003-08-10 21:52:11 +000092int term_inited;
93int64_t ticks_per_sec;
bellard36b486b2003-11-11 13:36:08 +000094int boot_device = 'c';
bellard1ccde1c2004-02-06 19:46:14 +000095static int ram_size;
bellard80cabfa2004-03-14 12:20:30 +000096static char network_script[1024];
97int pit_min_timer_count = 0;
bellardc4b1fcc2004-03-14 21:44:30 +000098int nb_nics;
99NetDriverState nd_table[MAX_NICS];
100SerialState *serial_console;
bellard0824d6f2003-06-24 13:42:40 +0000101
102/***********************************************************/
103/* x86 io ports */
104
bellardc4b1fcc2004-03-14 21:44:30 +0000105uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000106{
107#ifdef DEBUG_UNUSED_IOPORT
108 fprintf(stderr, "inb: port=0x%04x\n", address);
109#endif
bellardfc01f7e2003-06-30 10:03:06 +0000110 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000111}
112
bellardc4b1fcc2004-03-14 21:44:30 +0000113void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000114{
115#ifdef DEBUG_UNUSED_IOPORT
116 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
117#endif
118}
119
120/* default is to make two byte accesses */
bellardc4b1fcc2004-03-14 21:44:30 +0000121uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000122{
123 uint32_t data;
bellardc4b1fcc2004-03-14 21:44:30 +0000124 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
125 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000126 return data;
127}
128
bellardc4b1fcc2004-03-14 21:44:30 +0000129void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000130{
bellardc4b1fcc2004-03-14 21:44:30 +0000131 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
132 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000133}
134
bellardc4b1fcc2004-03-14 21:44:30 +0000135uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000136{
137#ifdef DEBUG_UNUSED_IOPORT
138 fprintf(stderr, "inl: port=0x%04x\n", address);
139#endif
140 return 0xffffffff;
141}
142
bellardc4b1fcc2004-03-14 21:44:30 +0000143void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000144{
145#ifdef DEBUG_UNUSED_IOPORT
146 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
147#endif
bellard0824d6f2003-06-24 13:42:40 +0000148}
149
150void init_ioports(void)
151{
152 int i;
153
154 for(i = 0; i < MAX_IOPORTS; i++) {
bellardfc01f7e2003-06-30 10:03:06 +0000155 ioport_read_table[0][i] = default_ioport_readb;
156 ioport_write_table[0][i] = default_ioport_writeb;
157 ioport_read_table[1][i] = default_ioport_readw;
158 ioport_write_table[1][i] = default_ioport_writew;
159 ioport_read_table[2][i] = default_ioport_readl;
160 ioport_write_table[2][i] = default_ioport_writel;
bellard0824d6f2003-06-24 13:42:40 +0000161 }
162}
163
bellardfc01f7e2003-06-30 10:03:06 +0000164/* size is the word size in byte */
bellardc4b1fcc2004-03-14 21:44:30 +0000165int register_ioport_read(int start, int length, int size,
166 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000167{
bellardfc01f7e2003-06-30 10:03:06 +0000168 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000169
bellardc4b1fcc2004-03-14 21:44:30 +0000170 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000171 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000172 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000173 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000174 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000175 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000176 } else {
177 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000178 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000179 }
180 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000181 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000182 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
183 hw_error("register_ioport_read: invalid opaque");
184 ioport_opaque[i] = opaque;
185 }
bellard0824d6f2003-06-24 13:42:40 +0000186 return 0;
187}
188
bellardfc01f7e2003-06-30 10:03:06 +0000189/* size is the word size in byte */
bellardc4b1fcc2004-03-14 21:44:30 +0000190int register_ioport_write(int start, int length, int size,
191 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000192{
bellardfc01f7e2003-06-30 10:03:06 +0000193 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000194
bellardc4b1fcc2004-03-14 21:44:30 +0000195 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000196 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000197 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000198 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000199 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000200 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000201 } else {
202 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000203 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000204 }
205 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000206 ioport_write_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000207 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
208 hw_error("register_ioport_read: invalid opaque");
209 ioport_opaque[i] = opaque;
210 }
bellardf1510b22003-06-25 00:07:40 +0000211 return 0;
212}
213
bellard0824d6f2003-06-24 13:42:40 +0000214void pstrcpy(char *buf, int buf_size, const char *str)
215{
216 int c;
217 char *q = buf;
218
219 if (buf_size <= 0)
220 return;
221
222 for(;;) {
223 c = *str++;
224 if (c == 0 || q >= buf + buf_size - 1)
225 break;
226 *q++ = c;
227 }
228 *q = '\0';
229}
230
231/* strcat and truncate. */
232char *pstrcat(char *buf, int buf_size, const char *s)
233{
234 int len;
235 len = strlen(buf);
236 if (len < buf_size)
237 pstrcpy(buf + len, buf_size - len, s);
238 return buf;
239}
240
bellard0824d6f2003-06-24 13:42:40 +0000241/* return the size or -1 if error */
242int load_image(const char *filename, uint8_t *addr)
243{
244 int fd, size;
245 fd = open(filename, O_RDONLY);
246 if (fd < 0)
247 return -1;
248 size = lseek(fd, 0, SEEK_END);
249 lseek(fd, 0, SEEK_SET);
250 if (read(fd, addr, size) != size) {
251 close(fd);
252 return -1;
253 }
254 close(fd);
255 return size;
256}
257
bellardc45886d2004-01-05 00:02:06 +0000258void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000259{
bellardc4b1fcc2004-03-14 21:44:30 +0000260 addr &= (MAX_IOPORTS - 1);
261 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
bellard0824d6f2003-06-24 13:42:40 +0000262}
263
bellardc45886d2004-01-05 00:02:06 +0000264void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000265{
bellardc4b1fcc2004-03-14 21:44:30 +0000266 addr &= (MAX_IOPORTS - 1);
267 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
bellard0824d6f2003-06-24 13:42:40 +0000268}
269
bellardc45886d2004-01-05 00:02:06 +0000270void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000271{
bellardc4b1fcc2004-03-14 21:44:30 +0000272 addr &= (MAX_IOPORTS - 1);
273 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
bellard0824d6f2003-06-24 13:42:40 +0000274}
275
bellardc45886d2004-01-05 00:02:06 +0000276int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000277{
bellardc4b1fcc2004-03-14 21:44:30 +0000278 addr &= (MAX_IOPORTS - 1);
279 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
bellard0824d6f2003-06-24 13:42:40 +0000280}
281
bellardc45886d2004-01-05 00:02:06 +0000282int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000283{
bellardc4b1fcc2004-03-14 21:44:30 +0000284 addr &= (MAX_IOPORTS - 1);
285 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
bellard0824d6f2003-06-24 13:42:40 +0000286}
287
bellardc45886d2004-01-05 00:02:06 +0000288int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000289{
bellardc4b1fcc2004-03-14 21:44:30 +0000290 addr &= (MAX_IOPORTS - 1);
291 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
bellard0824d6f2003-06-24 13:42:40 +0000292}
293
294/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000295void hw_error(const char *fmt, ...)
296{
297 va_list ap;
298
299 va_start(ap, fmt);
300 fprintf(stderr, "qemu: hardware error: ");
301 vfprintf(stderr, fmt, ap);
302 fprintf(stderr, "\n");
303#ifdef TARGET_I386
304 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
bellardc45886d2004-01-05 00:02:06 +0000305#else
306 cpu_dump_state(global_env, stderr, 0);
bellard0824d6f2003-06-24 13:42:40 +0000307#endif
308 va_end(ap);
309 abort();
310}
311
bellard34865132003-10-05 14:28:56 +0000312#if defined(__powerpc__)
313
314static inline uint32_t get_tbl(void)
315{
316 uint32_t tbl;
317 asm volatile("mftb %0" : "=r" (tbl));
318 return tbl;
319}
320
321static inline uint32_t get_tbu(void)
322{
323 uint32_t tbl;
324 asm volatile("mftbu %0" : "=r" (tbl));
325 return tbl;
326}
327
328int64_t cpu_get_real_ticks(void)
329{
330 uint32_t l, h, h1;
331 /* NOTE: we test if wrapping has occurred */
332 do {
333 h = get_tbu();
334 l = get_tbl();
335 h1 = get_tbu();
336 } while (h != h1);
337 return ((int64_t)h << 32) | l;
338}
339
340#elif defined(__i386__)
341
342int64_t cpu_get_real_ticks(void)
343{
344 int64_t val;
345 asm("rdtsc" : "=A" (val));
346 return val;
347}
348
349#else
350#error unsupported CPU
351#endif
352
353static int64_t cpu_ticks_offset;
354static int64_t cpu_ticks_last;
355
356int64_t cpu_get_ticks(void)
357{
358 return cpu_get_real_ticks() + cpu_ticks_offset;
359}
360
361/* enable cpu_get_ticks() */
362void cpu_enable_ticks(void)
363{
364 cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
365}
366
367/* disable cpu_get_ticks() : the clock is stopped. You must not call
368 cpu_get_ticks() after that. */
369void cpu_disable_ticks(void)
370{
371 cpu_ticks_last = cpu_get_ticks();
372}
373
bellard0824d6f2003-06-24 13:42:40 +0000374int64_t get_clock(void)
375{
376 struct timeval tv;
377 gettimeofday(&tv, NULL);
378 return tv.tv_sec * 1000000LL + tv.tv_usec;
379}
380
bellard0824d6f2003-06-24 13:42:40 +0000381void cpu_calibrate_ticks(void)
382{
383 int64_t usec, ticks;
384
385 usec = get_clock();
386 ticks = cpu_get_ticks();
387 usleep(50 * 1000);
388 usec = get_clock() - usec;
389 ticks = cpu_get_ticks() - ticks;
390 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
391}
392
bellard87858c82003-06-27 12:01:39 +0000393/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000394uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000395{
396 union {
397 uint64_t ll;
398 struct {
399#ifdef WORDS_BIGENDIAN
400 uint32_t high, low;
401#else
402 uint32_t low, high;
403#endif
404 } l;
405 } u, res;
406 uint64_t rl, rh;
407
408 u.ll = a;
409 rl = (uint64_t)u.l.low * (uint64_t)b;
410 rh = (uint64_t)u.l.high * (uint64_t)b;
411 rh += (rl >> 32);
412 res.l.high = rh / c;
413 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
414 return res.ll;
415}
416
bellardc4b1fcc2004-03-14 21:44:30 +0000417/***********************************************************/
418/* serial device */
bellardc45886d2004-01-05 00:02:06 +0000419
bellardc4b1fcc2004-03-14 21:44:30 +0000420int serial_open_device(void)
bellard0824d6f2003-06-24 13:42:40 +0000421{
bellardc4b1fcc2004-03-14 21:44:30 +0000422 char slave_name[1024];
423 int master_fd, slave_fd;
bellard0824d6f2003-06-24 13:42:40 +0000424
bellardc4b1fcc2004-03-14 21:44:30 +0000425 if (serial_console == NULL && nographic) {
426 /* use console for serial port */
427 return 0;
bellard0824d6f2003-06-24 13:42:40 +0000428 } else {
bellardc4b1fcc2004-03-14 21:44:30 +0000429 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
430 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
431 return -1;
432 }
433 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
434 return master_fd;
bellard0824d6f2003-06-24 13:42:40 +0000435 }
436}
437
bellardf1510b22003-06-25 00:07:40 +0000438/***********************************************************/
bellard80cabfa2004-03-14 12:20:30 +0000439/* Linux network device redirector */
bellardf1510b22003-06-25 00:07:40 +0000440
bellardc4b1fcc2004-03-14 21:44:30 +0000441static int tun_open(char *ifname, int ifname_size)
bellardf1510b22003-06-25 00:07:40 +0000442{
443 struct ifreq ifr;
bellardc4b1fcc2004-03-14 21:44:30 +0000444 int fd, ret;
bellardf1510b22003-06-25 00:07:40 +0000445
446 fd = open("/dev/net/tun", O_RDWR);
447 if (fd < 0) {
448 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
449 return -1;
450 }
451 memset(&ifr, 0, sizeof(ifr));
452 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
453 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
454 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
455 if (ret != 0) {
456 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
457 close(fd);
458 return -1;
459 }
bellardfc01f7e2003-06-30 10:03:06 +0000460 printf("Connected to host network interface: %s\n", ifr.ifr_name);
bellardc4b1fcc2004-03-14 21:44:30 +0000461 pstrcpy(ifname, ifname_size, ifr.ifr_name);
bellardf1510b22003-06-25 00:07:40 +0000462 fcntl(fd, F_SETFL, O_NONBLOCK);
bellardc4b1fcc2004-03-14 21:44:30 +0000463 return fd;
464}
bellardf1510b22003-06-25 00:07:40 +0000465
bellardc4b1fcc2004-03-14 21:44:30 +0000466static int net_init(void)
467{
468 int pid, status, launch_script, i;
469 NetDriverState *nd;
470 char *args[MAX_NICS + 2];
471 char **parg;
472
473 launch_script = 0;
474 for(i = 0; i < nb_nics; i++) {
475 nd = &nd_table[i];
476 if (nd->fd < 0) {
477 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
478 if (nd->fd >= 0)
479 launch_script = 1;
bellardf1510b22003-06-25 00:07:40 +0000480 }
bellardc4b1fcc2004-03-14 21:44:30 +0000481 }
482
483 if (launch_script) {
484 /* try to launch network init script */
485 pid = fork();
486 if (pid >= 0) {
487 if (pid == 0) {
488 parg = args;
489 *parg++ = network_script;
490 for(i = 0; i < nb_nics; i++) {
491 nd = &nd_table[i];
492 if (nd->fd >= 0) {
493 *parg++ = nd->ifname;
494 }
495 }
496 *parg++ = NULL;
497 execv(network_script, args);
498 exit(1);
499 }
500 while (waitpid(pid, &status, 0) != pid);
501 if (!WIFEXITED(status) ||
502 WEXITSTATUS(status) != 0) {
503 fprintf(stderr, "%s: could not launch network script\n",
504 network_script);
505 }
bellardf1510b22003-06-25 00:07:40 +0000506 }
507 }
508 return 0;
509}
510
bellardc4b1fcc2004-03-14 21:44:30 +0000511void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
bellardf1510b22003-06-25 00:07:40 +0000512{
513#ifdef DEBUG_NE2000
514 printf("NE2000: sending packet size=%d\n", size);
515#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000516 write(nd->fd, buf, size);
bellardf1510b22003-06-25 00:07:40 +0000517}
518
bellard330d0412003-07-26 18:11:40 +0000519/***********************************************************/
bellard313aa562003-08-10 21:52:11 +0000520/* dumb display */
521
522/* init terminal so that we can grab keys */
523static struct termios oldtty;
524
525static void term_exit(void)
526{
527 tcsetattr (0, TCSANOW, &oldtty);
528}
529
530static void term_init(void)
531{
532 struct termios tty;
533
534 tcgetattr (0, &tty);
535 oldtty = tty;
536
537 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
538 |INLCR|IGNCR|ICRNL|IXON);
539 tty.c_oflag |= OPOST;
bellarda20dd502003-09-30 21:07:02 +0000540 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
541 /* if graphical mode, we allow Ctrl-C handling */
542 if (nographic)
543 tty.c_lflag &= ~ISIG;
bellard313aa562003-08-10 21:52:11 +0000544 tty.c_cflag &= ~(CSIZE|PARENB);
545 tty.c_cflag |= CS8;
546 tty.c_cc[VMIN] = 1;
547 tty.c_cc[VTIME] = 0;
548
549 tcsetattr (0, TCSANOW, &tty);
550
551 atexit(term_exit);
552
553 fcntl(0, F_SETFL, O_NONBLOCK);
554}
555
556static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
557{
558}
559
560static void dumb_resize(DisplayState *ds, int w, int h)
561{
562}
563
564static void dumb_refresh(DisplayState *ds)
565{
566 vga_update_display();
567}
568
569void dumb_display_init(DisplayState *ds)
570{
571 ds->data = NULL;
572 ds->linesize = 0;
573 ds->depth = 0;
574 ds->dpy_update = dumb_update;
575 ds->dpy_resize = dumb_resize;
576 ds->dpy_refresh = dumb_refresh;
577}
578
bellard3a51dee2003-10-27 21:18:35 +0000579#if !defined(CONFIG_SOFTMMU)
bellard313aa562003-08-10 21:52:11 +0000580/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000581/* cpu signal handler */
582static void host_segv_handler(int host_signum, siginfo_t *info,
583 void *puc)
584{
585 if (cpu_signal_handler(host_signum, info, puc))
586 return;
587 term_exit();
588 abort();
589}
bellard3a51dee2003-10-27 21:18:35 +0000590#endif
bellard0824d6f2003-06-24 13:42:40 +0000591
592static int timer_irq_pending;
bellard87858c82003-06-27 12:01:39 +0000593static int timer_irq_count;
bellard0824d6f2003-06-24 13:42:40 +0000594
bellard313aa562003-08-10 21:52:11 +0000595static int timer_ms;
596static int gui_refresh_pending, gui_refresh_count;
597
bellard0824d6f2003-06-24 13:42:40 +0000598static void host_alarm_handler(int host_signum, siginfo_t *info,
599 void *puc)
600{
bellard87858c82003-06-27 12:01:39 +0000601 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
602 some drift between cpu_get_ticks() and the interrupt time. So
603 we queue some interrupts to avoid missing some */
604 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
605 if (timer_irq_count) {
606 if (timer_irq_count > 2)
607 timer_irq_count = 2;
608 timer_irq_count--;
bellard313aa562003-08-10 21:52:11 +0000609 timer_irq_pending = 1;
610 }
611 gui_refresh_count += timer_ms;
612 if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
613 gui_refresh_count = 0;
614 gui_refresh_pending = 1;
615 }
616
617 if (gui_refresh_pending || timer_irq_pending) {
bellard87858c82003-06-27 12:01:39 +0000618 /* just exit from the cpu to have a chance to handle timers */
bellardc45886d2004-01-05 00:02:06 +0000619 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard87858c82003-06-27 12:01:39 +0000620 }
bellard0824d6f2003-06-24 13:42:40 +0000621}
622
bellardc4b1fcc2004-03-14 21:44:30 +0000623#define MAX_IO_HANDLERS 64
624
625typedef struct IOHandlerRecord {
626 int fd;
627 IOCanRWHandler *fd_can_read;
628 IOReadHandler *fd_read;
629 void *opaque;
630 /* temporary data */
631 struct pollfd *ufd;
632 int max_size;
633} IOHandlerRecord;
634
635static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
636static int nb_io_handlers = 0;
637
638int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
639 IOReadHandler *fd_read, void *opaque)
640{
641 IOHandlerRecord *ioh;
642
643 if (nb_io_handlers >= MAX_IO_HANDLERS)
644 return -1;
645 ioh = &io_handlers[nb_io_handlers];
646 ioh->fd = fd;
647 ioh->fd_can_read = fd_can_read;
648 ioh->fd_read = fd_read;
649 ioh->opaque = opaque;
650 nb_io_handlers++;
651 return 0;
652}
653
bellardb4608c02003-06-27 17:34:32 +0000654/* main execution loop */
655
656CPUState *cpu_gdbstub_get_env(void *opaque)
657{
658 return global_env;
659}
660
bellard4c3a88a2003-07-26 12:06:08 +0000661int main_loop(void *opaque)
bellardb4608c02003-06-27 17:34:32 +0000662{
bellardc4b1fcc2004-03-14 21:44:30 +0000663 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
664 int ret, n, timeout, serial_ok, max_size, i;
665 uint8_t buf[4096];
666 IOHandlerRecord *ioh;
bellardb4608c02003-06-27 17:34:32 +0000667 CPUState *env = global_env;
668
bellarda20dd502003-09-30 21:07:02 +0000669 if (!term_inited) {
bellard313aa562003-08-10 21:52:11 +0000670 /* initialize terminal only there so that the user has a
671 chance to stop QEMU with Ctrl-C before the gdb connection
672 is launched */
673 term_inited = 1;
674 term_init();
675 }
676
bellard27c3f2c2003-09-30 21:40:47 +0000677 serial_ok = 1;
bellard34865132003-10-05 14:28:56 +0000678 cpu_enable_ticks();
bellardb4608c02003-06-27 17:34:32 +0000679 for(;;) {
bellardc45886d2004-01-05 00:02:06 +0000680#if defined (DO_TB_FLUSH)
681 tb_flush();
682#endif
683 ret = cpu_exec(env);
bellard34865132003-10-05 14:28:56 +0000684 if (reset_requested) {
685 ret = EXCP_INTERRUPT;
bellardcd4c3e82003-07-04 14:38:25 +0000686 break;
bellard34865132003-10-05 14:28:56 +0000687 }
688 if (ret == EXCP_DEBUG) {
689 ret = EXCP_DEBUG;
690 break;
691 }
bellardb4608c02003-06-27 17:34:32 +0000692 /* if hlt instruction, we wait until the next IRQ */
693 if (ret == EXCP_HLT)
694 timeout = 10;
695 else
696 timeout = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000697
bellardb4608c02003-06-27 17:34:32 +0000698 /* poll any events */
bellardb4608c02003-06-27 17:34:32 +0000699 pf = ufds;
bellardc4b1fcc2004-03-14 21:44:30 +0000700 ioh = io_handlers;
701 for(i = 0; i < nb_io_handlers; i++) {
702 max_size = ioh->fd_can_read(ioh->opaque);
703 if (max_size > 0) {
704 if (max_size > sizeof(buf))
705 max_size = sizeof(buf);
706 pf->fd = ioh->fd;
707 pf->events = POLLIN;
708 ioh->ufd = pf;
709 pf++;
710 } else {
711 ioh->ufd = NULL;
712 }
713 ioh->max_size = max_size;
714 ioh++;
bellardb4608c02003-06-27 17:34:32 +0000715 }
bellardc4b1fcc2004-03-14 21:44:30 +0000716
bellardb4608c02003-06-27 17:34:32 +0000717 gdb_ufd = NULL;
718 if (gdbstub_fd > 0) {
719 gdb_ufd = pf;
720 pf->fd = gdbstub_fd;
721 pf->events = POLLIN;
722 pf++;
723 }
724
725 ret = poll(ufds, pf - ufds, timeout);
726 if (ret > 0) {
bellardc4b1fcc2004-03-14 21:44:30 +0000727 ioh = io_handlers;
728 for(i = 0; i < nb_io_handlers; i++) {
729 pf = ioh->ufd;
730 if (pf) {
731 n = read(ioh->fd, buf, ioh->max_size);
732 if (n > 0) {
733 ioh->fd_read(ioh->opaque, buf, n);
bellardb4608c02003-06-27 17:34:32 +0000734 }
bellardb4608c02003-06-27 17:34:32 +0000735 }
bellardc4b1fcc2004-03-14 21:44:30 +0000736 ioh++;
bellardb4608c02003-06-27 17:34:32 +0000737 }
738 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
739 uint8_t buf[1];
740 /* stop emulation if requested by gdb */
741 n = read(gdbstub_fd, buf, 1);
bellard34865132003-10-05 14:28:56 +0000742 if (n == 1) {
743 ret = EXCP_INTERRUPT;
bellardb4608c02003-06-27 17:34:32 +0000744 break;
bellard34865132003-10-05 14:28:56 +0000745 }
bellardb4608c02003-06-27 17:34:32 +0000746 }
747 }
748
749 /* timer IRQ */
750 if (timer_irq_pending) {
bellardc45886d2004-01-05 00:02:06 +0000751#if defined (TARGET_I386)
bellardb4608c02003-06-27 17:34:32 +0000752 pic_set_irq(0, 1);
753 pic_set_irq(0, 0);
754 timer_irq_pending = 0;
bellard80cabfa2004-03-14 12:20:30 +0000755 rtc_timer();
bellardc45886d2004-01-05 00:02:06 +0000756#endif
bellardb4608c02003-06-27 17:34:32 +0000757 }
bellard8dc75d72004-02-25 23:30:56 +0000758 /* XXX: add explicit timer */
759 SB16_run();
760
761 /* run dma transfers, if any */
762 DMA_run();
bellard313aa562003-08-10 21:52:11 +0000763
764 /* VGA */
765 if (gui_refresh_pending) {
766 display_state.dpy_refresh(&display_state);
767 gui_refresh_pending = 0;
768 }
bellardb4608c02003-06-27 17:34:32 +0000769 }
bellard34865132003-10-05 14:28:56 +0000770 cpu_disable_ticks();
771 return ret;
bellardb4608c02003-06-27 17:34:32 +0000772}
773
bellard0824d6f2003-06-24 13:42:40 +0000774void help(void)
775{
bellarda20dd502003-09-30 21:07:02 +0000776 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +0000777 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +0000778 "\n"
bellarda20dd502003-09-30 21:07:02 +0000779 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +0000780 "\n"
bellarda20dd502003-09-30 21:07:02 +0000781 "Standard options:\n"
bellardc45886d2004-01-05 00:02:06 +0000782 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +0000783 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
784 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +0000785 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
bellard6e44ba72004-01-18 21:56:49 +0000786 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
bellarda20dd502003-09-30 21:07:02 +0000787 "-snapshot write to temporary files instead of disk image files\n"
788 "-m megs set virtual RAM size to megs MB\n"
bellardc4b1fcc2004-03-14 21:44:30 +0000789 "-nographic disable graphical output and redirect serial I/Os to console\n"
bellarda20dd502003-09-30 21:07:02 +0000790 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +0000791 "Network options:\n"
792 "-n script set network init script [default=%s]\n"
793 "-nics n simulate 'n' network interfaces [default=1]\n"
794 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
795 "\n"
796 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +0000797 "-kernel bzImage use 'bzImage' as kernel image\n"
798 "-append cmdline use 'cmdline' as kernel command line\n"
799 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +0000800 "\n"
bellard330d0412003-07-26 18:11:40 +0000801 "Debug/Expert options:\n"
bellarda20dd502003-09-30 21:07:02 +0000802 "-s wait gdb connection to port %d\n"
803 "-p port change gdb connection port\n"
bellard6e44ba72004-01-18 21:56:49 +0000804 "-d output log to %s\n"
bellarda20dd502003-09-30 21:07:02 +0000805 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
806 "-L path set the directory for the BIOS and VGA BIOS\n"
bellard77fef8c2004-02-16 22:05:46 +0000807#ifdef USE_CODE_COPY
808 "-no-code-copy disable code copy acceleration\n"
809#endif
810
bellard0824d6f2003-06-24 13:42:40 +0000811 "\n"
bellardf1510b22003-06-25 00:07:40 +0000812 "During emulation, use C-a h to get terminal commands:\n",
bellard0db63472003-10-27 21:37:46 +0000813#ifdef CONFIG_SOFTMMU
814 "qemu",
815#else
816 "qemu-fast",
817#endif
818 DEFAULT_NETWORK_SCRIPT,
bellard6e44ba72004-01-18 21:56:49 +0000819 DEFAULT_GDBSTUB_PORT,
820 "/tmp/qemu.log");
bellard0824d6f2003-06-24 13:42:40 +0000821 term_print_help();
bellard0db63472003-10-27 21:37:46 +0000822#ifndef CONFIG_SOFTMMU
823 printf("\n"
824 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
825 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
826 "PC emulation.\n");
827#endif
bellard0824d6f2003-06-24 13:42:40 +0000828 exit(1);
829}
830
bellardfc01f7e2003-06-30 10:03:06 +0000831struct option long_options[] = {
832 { "initrd", 1, NULL, 0, },
833 { "hda", 1, NULL, 0, },
834 { "hdb", 1, NULL, 0, },
bellard33e39632003-07-06 17:15:21 +0000835 { "snapshot", 0, NULL, 0, },
bellard330d0412003-07-26 18:11:40 +0000836 { "hdachs", 1, NULL, 0, },
bellarda20dd502003-09-30 21:07:02 +0000837 { "nographic", 0, NULL, 0, },
838 { "kernel", 1, NULL, 0, },
839 { "append", 1, NULL, 0, },
bellard42f1e0e2003-09-30 22:11:17 +0000840 { "tun-fd", 1, NULL, 0, },
bellard36b486b2003-11-11 13:36:08 +0000841 { "hdc", 1, NULL, 0, },
842 { "hdd", 1, NULL, 0, },
843 { "cdrom", 1, NULL, 0, },
844 { "boot", 1, NULL, 0, },
bellardc45886d2004-01-05 00:02:06 +0000845 { "fda", 1, NULL, 0, },
846 { "fdb", 1, NULL, 0, },
bellardc4b1fcc2004-03-14 21:44:30 +0000847 { "no-code-copy", 0, NULL, 0 },
848 { "nics", 1, NULL, 0 },
bellardfc01f7e2003-06-30 10:03:06 +0000849 { NULL, 0, NULL, 0 },
850};
851
bellarda20dd502003-09-30 21:07:02 +0000852#ifdef CONFIG_SDL
853/* SDL use the pthreads and they modify sigaction. We don't
854 want that. */
bellarddc887a42004-01-04 18:18:57 +0000855#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
bellarda20dd502003-09-30 21:07:02 +0000856extern void __libc_sigaction();
857#define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
858#else
859extern void __sigaction();
860#define sigaction(sig, act, oact) __sigaction(sig, act, oact)
861#endif
862#endif /* CONFIG_SDL */
863
bellard77fef8c2004-02-16 22:05:46 +0000864#if defined (TARGET_I386) && defined(USE_CODE_COPY)
865
866/* this stack is only used during signal handling */
867#define SIGNAL_STACK_SIZE 32768
868
869static uint8_t *signal_stack;
870
871#endif
872
bellard0824d6f2003-06-24 13:42:40 +0000873int main(int argc, char **argv)
874{
bellardc4b1fcc2004-03-14 21:44:30 +0000875 int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
bellard1ccde1c2004-02-06 19:46:14 +0000876 int snapshot, linux_boot;
bellard0824d6f2003-06-24 13:42:40 +0000877 struct sigaction act;
878 struct itimerval itv;
bellardc45886d2004-01-05 00:02:06 +0000879 CPUState *env;
bellard7f7f9872003-10-30 01:11:23 +0000880 const char *initrd_filename;
bellardc45886d2004-01-05 00:02:06 +0000881 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
bellarda20dd502003-09-30 21:07:02 +0000882 const char *kernel_filename, *kernel_cmdline;
bellard313aa562003-08-10 21:52:11 +0000883 DisplayState *ds = &display_state;
bellardc4b1fcc2004-03-14 21:44:30 +0000884 int cyls, heads, secs;
bellard313aa562003-08-10 21:52:11 +0000885
bellard0824d6f2003-06-24 13:42:40 +0000886 /* we never want that malloc() uses mmap() */
887 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
bellardfc01f7e2003-06-30 10:03:06 +0000888 initrd_filename = NULL;
bellardc45886d2004-01-05 00:02:06 +0000889 for(i = 0; i < MAX_FD; i++)
890 fd_filename[i] = NULL;
bellardfc01f7e2003-06-30 10:03:06 +0000891 for(i = 0; i < MAX_DISKS; i++)
892 hd_filename[i] = NULL;
bellard1ccde1c2004-02-06 19:46:14 +0000893 ram_size = 32 * 1024 * 1024;
bellard313aa562003-08-10 21:52:11 +0000894 vga_ram_size = VGA_RAM_SIZE;
bellardf1510b22003-06-25 00:07:40 +0000895 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
bellardb4608c02003-06-27 17:34:32 +0000896 use_gdbstub = 0;
897 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard33e39632003-07-06 17:15:21 +0000898 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +0000899 nographic = 0;
900 kernel_filename = NULL;
901 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +0000902 has_cdrom = 1;
903 cyls = heads = secs = 0;
904
905 nb_nics = 1;
906 for(i = 0; i < MAX_NICS; i++) {
907 NetDriverState *nd = &nd_table[i];
908 nd->fd = -1;
909 /* init virtual mac address */
910 nd->macaddr[0] = 0x52;
911 nd->macaddr[1] = 0x54;
912 nd->macaddr[2] = 0x00;
913 nd->macaddr[3] = 0x12;
914 nd->macaddr[4] = 0x34;
915 nd->macaddr[5] = 0x56 + i;
916 }
917
bellard0824d6f2003-06-24 13:42:40 +0000918 for(;;) {
bellard330d0412003-07-26 18:11:40 +0000919 c = getopt_long_only(argc, argv, "hm:dn:sp:L:", long_options, &long_index);
bellard0824d6f2003-06-24 13:42:40 +0000920 if (c == -1)
921 break;
922 switch(c) {
bellardfc01f7e2003-06-30 10:03:06 +0000923 case 0:
924 switch(long_index) {
925 case 0:
926 initrd_filename = optarg;
927 break;
928 case 1:
929 hd_filename[0] = optarg;
930 break;
931 case 2:
932 hd_filename[1] = optarg;
933 break;
bellard33e39632003-07-06 17:15:21 +0000934 case 3:
935 snapshot = 1;
936 break;
bellard330d0412003-07-26 18:11:40 +0000937 case 4:
938 {
bellard330d0412003-07-26 18:11:40 +0000939 const char *p;
940 p = optarg;
941 cyls = strtol(p, (char **)&p, 0);
942 if (*p != ',')
943 goto chs_fail;
944 p++;
945 heads = strtol(p, (char **)&p, 0);
946 if (*p != ',')
947 goto chs_fail;
948 p++;
949 secs = strtol(p, (char **)&p, 0);
bellardc4b1fcc2004-03-14 21:44:30 +0000950 if (*p != '\0') {
951 chs_fail:
952 cyls = 0;
953 }
bellard330d0412003-07-26 18:11:40 +0000954 }
955 break;
bellard313aa562003-08-10 21:52:11 +0000956 case 5:
bellarda20dd502003-09-30 21:07:02 +0000957 nographic = 1;
958 break;
959 case 6:
960 kernel_filename = optarg;
961 break;
962 case 7:
963 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +0000964 break;
bellard42f1e0e2003-09-30 22:11:17 +0000965 case 8:
bellardc4b1fcc2004-03-14 21:44:30 +0000966 {
967 const char *p;
968 int fd;
969 p = optarg;
970 nb_nics = 0;
971 for(;;) {
972 fd = strtol(p, (char **)&p, 0);
973 nd_table[nb_nics].fd = fd;
974 snprintf(nd_table[nb_nics].ifname,
975 sizeof(nd_table[nb_nics].ifname),
976 "fd%d", nb_nics);
977 nb_nics++;
978 if (*p == ',') {
979 p++;
980 } else if (*p != '\0') {
981 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
982 exit(1);
983 }
984 }
985 }
bellard42f1e0e2003-09-30 22:11:17 +0000986 break;
bellard36b486b2003-11-11 13:36:08 +0000987 case 9:
988 hd_filename[2] = optarg;
bellardc4b1fcc2004-03-14 21:44:30 +0000989 has_cdrom = 0;
bellard36b486b2003-11-11 13:36:08 +0000990 break;
991 case 10:
992 hd_filename[3] = optarg;
993 break;
994 case 11:
995 hd_filename[2] = optarg;
bellardc4b1fcc2004-03-14 21:44:30 +0000996 has_cdrom = 1;
bellard36b486b2003-11-11 13:36:08 +0000997 break;
998 case 12:
999 boot_device = optarg[0];
bellardc45886d2004-01-05 00:02:06 +00001000 if (boot_device != 'a' && boot_device != 'b' &&
1001 boot_device != 'c' && boot_device != 'd') {
bellard36b486b2003-11-11 13:36:08 +00001002 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1003 exit(1);
1004 }
1005 break;
bellardc45886d2004-01-05 00:02:06 +00001006 case 13:
1007 fd_filename[0] = optarg;
1008 break;
1009 case 14:
1010 fd_filename[1] = optarg;
1011 break;
bellard77fef8c2004-02-16 22:05:46 +00001012 case 15:
1013 code_copy_enabled = 0;
1014 break;
bellardc4b1fcc2004-03-14 21:44:30 +00001015 case 16:
1016 nb_nics = atoi(optarg);
1017 if (nb_nics < 1 || nb_nics > MAX_NICS) {
1018 fprintf(stderr, "qemu: invalid number of network interfaces\n");
1019 exit(1);
1020 }
1021 break;
bellardfc01f7e2003-06-30 10:03:06 +00001022 }
1023 break;
bellard0824d6f2003-06-24 13:42:40 +00001024 case 'h':
1025 help();
1026 break;
1027 case 'm':
bellard1ccde1c2004-02-06 19:46:14 +00001028 ram_size = atoi(optarg) * 1024 * 1024;
1029 if (ram_size <= 0)
bellard0824d6f2003-06-24 13:42:40 +00001030 help();
bellard1ccde1c2004-02-06 19:46:14 +00001031 if (ram_size > PHYS_RAM_MAX_SIZE) {
bellard36b486b2003-11-11 13:36:08 +00001032 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
bellard7916e222003-07-01 16:27:45 +00001033 PHYS_RAM_MAX_SIZE / (1024 * 1024));
1034 exit(1);
1035 }
bellard0824d6f2003-06-24 13:42:40 +00001036 break;
1037 case 'd':
bellard34865132003-10-05 14:28:56 +00001038 cpu_set_log(CPU_LOG_ALL);
bellard0824d6f2003-06-24 13:42:40 +00001039 break;
bellardf1510b22003-06-25 00:07:40 +00001040 case 'n':
1041 pstrcpy(network_script, sizeof(network_script), optarg);
1042 break;
bellardb4608c02003-06-27 17:34:32 +00001043 case 's':
1044 use_gdbstub = 1;
1045 break;
1046 case 'p':
1047 gdbstub_port = atoi(optarg);
1048 break;
bellard330d0412003-07-26 18:11:40 +00001049 case 'L':
bellard5a671352003-10-01 00:13:48 +00001050 bios_dir = optarg;
bellard330d0412003-07-26 18:11:40 +00001051 break;
bellard0824d6f2003-06-24 13:42:40 +00001052 }
1053 }
bellard330d0412003-07-26 18:11:40 +00001054
bellarda20dd502003-09-30 21:07:02 +00001055 if (optind < argc) {
1056 hd_filename[0] = argv[optind++];
1057 }
1058
1059 linux_boot = (kernel_filename != NULL);
bellard330d0412003-07-26 18:11:40 +00001060
bellardc45886d2004-01-05 00:02:06 +00001061 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
1062 fd_filename[0] == '\0')
bellard0824d6f2003-06-24 13:42:40 +00001063 help();
bellard8f2b1fb2003-11-16 19:46:01 +00001064
1065 /* boot to cd by default if no hard disk */
bellardd0309312004-01-18 22:35:25 +00001066 if (hd_filename[0] == '\0' && boot_device == 'c') {
1067 if (fd_filename[0] != '\0')
1068 boot_device = 'a';
1069 else
1070 boot_device = 'd';
1071 }
bellard0824d6f2003-06-24 13:42:40 +00001072
bellarddc887a42004-01-04 18:18:57 +00001073#if !defined(CONFIG_SOFTMMU)
1074 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1075 {
1076 static uint8_t stdout_buf[4096];
1077 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
1078 }
1079#else
bellardb118d612003-06-30 23:36:21 +00001080 setvbuf(stdout, NULL, _IOLBF, 0);
bellarddc887a42004-01-04 18:18:57 +00001081#endif
bellard0824d6f2003-06-24 13:42:40 +00001082
bellardc4b1fcc2004-03-14 21:44:30 +00001083 /* init host network redirectors */
1084 net_init();
bellardf1510b22003-06-25 00:07:40 +00001085
bellard0824d6f2003-06-24 13:42:40 +00001086 /* init the memory */
bellard1ccde1c2004-02-06 19:46:14 +00001087 phys_ram_size = ram_size + vga_ram_size;
bellard7f7f9872003-10-30 01:11:23 +00001088
1089#ifdef CONFIG_SOFTMMU
bellard1ccde1c2004-02-06 19:46:14 +00001090 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00001091 if (!phys_ram_base) {
1092 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00001093 exit(1);
1094 }
bellard7f7f9872003-10-30 01:11:23 +00001095#else
1096 /* as we must map the same page at several addresses, we must use
1097 a fd */
1098 {
1099 const char *tmpdir;
1100
1101 tmpdir = getenv("QEMU_TMPDIR");
1102 if (!tmpdir)
1103 tmpdir = "/tmp";
1104 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
1105 if (mkstemp(phys_ram_file) < 0) {
1106 fprintf(stderr, "Could not create temporary memory file '%s'\n",
1107 phys_ram_file);
1108 exit(1);
1109 }
1110 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
1111 if (phys_ram_fd < 0) {
1112 fprintf(stderr, "Could not open temporary memory file '%s'\n",
1113 phys_ram_file);
1114 exit(1);
1115 }
bellard1ccde1c2004-02-06 19:46:14 +00001116 ftruncate(phys_ram_fd, phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00001117 unlink(phys_ram_file);
bellard1ccde1c2004-02-06 19:46:14 +00001118 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
1119 phys_ram_size,
bellard7f7f9872003-10-30 01:11:23 +00001120 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
1121 phys_ram_fd, 0);
1122 if (phys_ram_base == MAP_FAILED) {
1123 fprintf(stderr, "Could not map physical memory\n");
1124 exit(1);
1125 }
1126 }
1127#endif
bellard0824d6f2003-06-24 13:42:40 +00001128
bellardc4b1fcc2004-03-14 21:44:30 +00001129 /* we always create the cdrom drive, even if no disk is there */
1130 if (has_cdrom) {
1131 bs_table[2] = bdrv_new("cdrom");
1132 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
1133 }
1134
bellard33e39632003-07-06 17:15:21 +00001135 /* open the virtual block devices */
1136 for(i = 0; i < MAX_DISKS; i++) {
1137 if (hd_filename[i]) {
bellard33e39632003-07-06 17:15:21 +00001138 if (!bs_table[i]) {
bellardc4b1fcc2004-03-14 21:44:30 +00001139 char buf[64];
1140 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
1141 bs_table[i] = bdrv_new(buf);
1142 }
1143 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
bellard36b486b2003-11-11 13:36:08 +00001144 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
bellard33e39632003-07-06 17:15:21 +00001145 hd_filename[i]);
1146 exit(1);
1147 }
bellardc4b1fcc2004-03-14 21:44:30 +00001148 if (i == 0 && cyls != 0)
1149 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
1150 }
1151 }
1152
1153 /* we always create at least one floppy disk */
1154 fd_table[0] = bdrv_new("fda");
1155 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
1156
1157 for(i = 0; i < MAX_FD; i++) {
1158 if (fd_filename[i]) {
1159 if (!fd_table[i]) {
1160 char buf[64];
1161 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
1162 fd_table[i] = bdrv_new(buf);
1163 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
1164 }
1165 if (fd_filename[i] != '\0') {
1166 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
1167 fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
1168 fd_filename[i]);
1169 exit(1);
1170 }
1171 }
bellard33e39632003-07-06 17:15:21 +00001172 }
1173 }
1174
bellard330d0412003-07-26 18:11:40 +00001175 /* init CPU state */
1176 env = cpu_init();
1177 global_env = env;
1178 cpu_single_env = env;
bellard0824d6f2003-06-24 13:42:40 +00001179
bellard330d0412003-07-26 18:11:40 +00001180 init_ioports();
bellard80cabfa2004-03-14 12:20:30 +00001181 cpu_calibrate_ticks();
bellard0824d6f2003-06-24 13:42:40 +00001182
bellard313aa562003-08-10 21:52:11 +00001183 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00001184 if (nographic) {
bellard313aa562003-08-10 21:52:11 +00001185 dumb_display_init(ds);
1186 } else {
1187#ifdef CONFIG_SDL
1188 sdl_display_init(ds);
bellard313aa562003-08-10 21:52:11 +00001189#else
1190 dumb_display_init(ds);
1191#endif
1192 }
bellard0824d6f2003-06-24 13:42:40 +00001193
bellard80cabfa2004-03-14 12:20:30 +00001194#if defined(TARGET_I386)
1195 pc_init(ram_size, vga_ram_size, boot_device,
1196 ds, fd_filename, snapshot,
1197 kernel_filename, kernel_cmdline, initrd_filename);
1198#elif defined(TARGET_PPC)
1199 ppc_init();
bellardc45886d2004-01-05 00:02:06 +00001200#endif
bellard77fef8c2004-02-16 22:05:46 +00001201
bellardc4b1fcc2004-03-14 21:44:30 +00001202 /* launched after the device init so that it can display or not a
1203 banner */
1204 monitor_init();
1205
bellard0824d6f2003-06-24 13:42:40 +00001206 /* setup cpu signal handlers for MMU / self modifying code handling */
bellard77fef8c2004-02-16 22:05:46 +00001207#if !defined(CONFIG_SOFTMMU)
1208
1209#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1210 {
1211 stack_t stk;
1212 signal_stack = malloc(SIGNAL_STACK_SIZE);
1213 stk.ss_sp = signal_stack;
1214 stk.ss_size = SIGNAL_STACK_SIZE;
1215 stk.ss_flags = 0;
1216
1217 if (sigaltstack(&stk, NULL) < 0) {
1218 perror("sigaltstack");
1219 exit(1);
1220 }
1221 }
1222#endif
1223
bellard0824d6f2003-06-24 13:42:40 +00001224 sigfillset(&act.sa_mask);
1225 act.sa_flags = SA_SIGINFO;
bellard77fef8c2004-02-16 22:05:46 +00001226#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1227 act.sa_flags |= SA_ONSTACK;
1228#endif
bellard0824d6f2003-06-24 13:42:40 +00001229 act.sa_sigaction = host_segv_handler;
1230 sigaction(SIGSEGV, &act, NULL);
1231 sigaction(SIGBUS, &act, NULL);
bellard77fef8c2004-02-16 22:05:46 +00001232#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1233 sigaction(SIGFPE, &act, NULL);
1234#endif
bellard3a51dee2003-10-27 21:18:35 +00001235#endif
bellard0824d6f2003-06-24 13:42:40 +00001236
bellard77fef8c2004-02-16 22:05:46 +00001237 /* timer signal */
1238 sigfillset(&act.sa_mask);
1239 act.sa_flags = SA_SIGINFO;
1240#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1241 act.sa_flags |= SA_ONSTACK;
1242#endif
bellard0824d6f2003-06-24 13:42:40 +00001243 act.sa_sigaction = host_alarm_handler;
1244 sigaction(SIGALRM, &act, NULL);
1245
bellard0824d6f2003-06-24 13:42:40 +00001246 itv.it_interval.tv_sec = 0;
bellard87858c82003-06-27 12:01:39 +00001247 itv.it_interval.tv_usec = 1000;
bellard0824d6f2003-06-24 13:42:40 +00001248 itv.it_value.tv_sec = 0;
1249 itv.it_value.tv_usec = 10 * 1000;
1250 setitimer(ITIMER_REAL, &itv, NULL);
bellard87858c82003-06-27 12:01:39 +00001251 /* we probe the tick duration of the kernel to inform the user if
1252 the emulated kernel requested a too high timer frequency */
1253 getitimer(ITIMER_REAL, &itv);
bellard313aa562003-08-10 21:52:11 +00001254 timer_ms = itv.it_interval.tv_usec / 1000;
bellard87858c82003-06-27 12:01:39 +00001255 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1256 1000000;
bellard7f7f9872003-10-30 01:11:23 +00001257
bellardb4608c02003-06-27 17:34:32 +00001258 if (use_gdbstub) {
1259 cpu_gdbstub(NULL, main_loop, gdbstub_port);
1260 } else {
1261 main_loop(NULL);
bellard0824d6f2003-06-24 13:42:40 +00001262 }
bellard0824d6f2003-06-24 13:42:40 +00001263 return 0;
1264}