]> git.proxmox.com Git - mirror_qemu.git/blob - vl.c
do not depend on thunk.h - more log items
[mirror_qemu.git] / vl.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * 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.
23 */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
29 #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>
41 #include <sys/wait.h>
42 #include <pty.h>
43
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <linux/if.h>
47 #include <linux/if_tun.h>
48
49 #include "disas.h"
50
51 #include "vl.h"
52
53 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
54
55 //#define DEBUG_UNUSED_IOPORT
56
57 #if !defined(CONFIG_SOFTMMU)
58 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
59 #else
60 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
61 #endif
62
63 #if defined (TARGET_I386)
64 #elif defined (TARGET_PPC)
65 //#define USE_OPEN_FIRMWARE
66 #if !defined (USE_OPEN_FIRMWARE)
67 #define KERNEL_LOAD_ADDR 0x01000000
68 #define KERNEL_STACK_ADDR 0x01200000
69 #else
70 #define KERNEL_LOAD_ADDR 0x00000000
71 #define KERNEL_STACK_ADDR 0x00400000
72 #endif
73 #endif
74
75 #define GUI_REFRESH_INTERVAL 30
76
77 /* XXX: use a two level table to limit memory usage */
78 #define MAX_IOPORTS 65536
79
80 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
81 char phys_ram_file[1024];
82 CPUState *global_env;
83 CPUState *cpu_single_env;
84 void *ioport_opaque[MAX_IOPORTS];
85 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
86 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
87 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
88 int vga_ram_size;
89 static DisplayState display_state;
90 int nographic;
91 int term_inited;
92 int64_t ticks_per_sec;
93 int boot_device = 'c';
94 static int ram_size;
95 static char network_script[1024];
96 int pit_min_timer_count = 0;
97 int nb_nics;
98 NetDriverState nd_table[MAX_NICS];
99 SerialState *serial_console;
100
101 /***********************************************************/
102 /* x86 io ports */
103
104 uint32_t default_ioport_readb(void *opaque, uint32_t address)
105 {
106 #ifdef DEBUG_UNUSED_IOPORT
107 fprintf(stderr, "inb: port=0x%04x\n", address);
108 #endif
109 return 0xff;
110 }
111
112 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
113 {
114 #ifdef DEBUG_UNUSED_IOPORT
115 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
116 #endif
117 }
118
119 /* default is to make two byte accesses */
120 uint32_t default_ioport_readw(void *opaque, uint32_t address)
121 {
122 uint32_t data;
123 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
124 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
125 return data;
126 }
127
128 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
129 {
130 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
131 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
132 }
133
134 uint32_t default_ioport_readl(void *opaque, uint32_t address)
135 {
136 #ifdef DEBUG_UNUSED_IOPORT
137 fprintf(stderr, "inl: port=0x%04x\n", address);
138 #endif
139 return 0xffffffff;
140 }
141
142 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
143 {
144 #ifdef DEBUG_UNUSED_IOPORT
145 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
146 #endif
147 }
148
149 void init_ioports(void)
150 {
151 int i;
152
153 for(i = 0; i < MAX_IOPORTS; i++) {
154 ioport_read_table[0][i] = default_ioport_readb;
155 ioport_write_table[0][i] = default_ioport_writeb;
156 ioport_read_table[1][i] = default_ioport_readw;
157 ioport_write_table[1][i] = default_ioport_writew;
158 ioport_read_table[2][i] = default_ioport_readl;
159 ioport_write_table[2][i] = default_ioport_writel;
160 }
161 }
162
163 /* size is the word size in byte */
164 int register_ioport_read(int start, int length, int size,
165 IOPortReadFunc *func, void *opaque)
166 {
167 int i, bsize;
168
169 if (size == 1) {
170 bsize = 0;
171 } else if (size == 2) {
172 bsize = 1;
173 } else if (size == 4) {
174 bsize = 2;
175 } else {
176 hw_error("register_ioport_read: invalid size");
177 return -1;
178 }
179 for(i = start; i < start + length; i += size) {
180 ioport_read_table[bsize][i] = func;
181 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
182 hw_error("register_ioport_read: invalid opaque");
183 ioport_opaque[i] = opaque;
184 }
185 return 0;
186 }
187
188 /* size is the word size in byte */
189 int register_ioport_write(int start, int length, int size,
190 IOPortWriteFunc *func, void *opaque)
191 {
192 int i, bsize;
193
194 if (size == 1) {
195 bsize = 0;
196 } else if (size == 2) {
197 bsize = 1;
198 } else if (size == 4) {
199 bsize = 2;
200 } else {
201 hw_error("register_ioport_write: invalid size");
202 return -1;
203 }
204 for(i = start; i < start + length; i += size) {
205 ioport_write_table[bsize][i] = func;
206 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
207 hw_error("register_ioport_read: invalid opaque");
208 ioport_opaque[i] = opaque;
209 }
210 return 0;
211 }
212
213 void pstrcpy(char *buf, int buf_size, const char *str)
214 {
215 int c;
216 char *q = buf;
217
218 if (buf_size <= 0)
219 return;
220
221 for(;;) {
222 c = *str++;
223 if (c == 0 || q >= buf + buf_size - 1)
224 break;
225 *q++ = c;
226 }
227 *q = '\0';
228 }
229
230 /* strcat and truncate. */
231 char *pstrcat(char *buf, int buf_size, const char *s)
232 {
233 int len;
234 len = strlen(buf);
235 if (len < buf_size)
236 pstrcpy(buf + len, buf_size - len, s);
237 return buf;
238 }
239
240 /* return the size or -1 if error */
241 int load_image(const char *filename, uint8_t *addr)
242 {
243 int fd, size;
244 fd = open(filename, O_RDONLY);
245 if (fd < 0)
246 return -1;
247 size = lseek(fd, 0, SEEK_END);
248 lseek(fd, 0, SEEK_SET);
249 if (read(fd, addr, size) != size) {
250 close(fd);
251 return -1;
252 }
253 close(fd);
254 return size;
255 }
256
257 void cpu_outb(CPUState *env, int addr, int val)
258 {
259 addr &= (MAX_IOPORTS - 1);
260 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
261 }
262
263 void cpu_outw(CPUState *env, int addr, int val)
264 {
265 addr &= (MAX_IOPORTS - 1);
266 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
267 }
268
269 void cpu_outl(CPUState *env, int addr, int val)
270 {
271 addr &= (MAX_IOPORTS - 1);
272 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
273 }
274
275 int cpu_inb(CPUState *env, int addr)
276 {
277 addr &= (MAX_IOPORTS - 1);
278 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
279 }
280
281 int cpu_inw(CPUState *env, int addr)
282 {
283 addr &= (MAX_IOPORTS - 1);
284 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
285 }
286
287 int cpu_inl(CPUState *env, int addr)
288 {
289 addr &= (MAX_IOPORTS - 1);
290 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
291 }
292
293 /***********************************************************/
294 void hw_error(const char *fmt, ...)
295 {
296 va_list ap;
297
298 va_start(ap, fmt);
299 fprintf(stderr, "qemu: hardware error: ");
300 vfprintf(stderr, fmt, ap);
301 fprintf(stderr, "\n");
302 #ifdef TARGET_I386
303 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
304 #else
305 cpu_dump_state(global_env, stderr, 0);
306 #endif
307 va_end(ap);
308 abort();
309 }
310
311 #if defined(__powerpc__)
312
313 static inline uint32_t get_tbl(void)
314 {
315 uint32_t tbl;
316 asm volatile("mftb %0" : "=r" (tbl));
317 return tbl;
318 }
319
320 static inline uint32_t get_tbu(void)
321 {
322 uint32_t tbl;
323 asm volatile("mftbu %0" : "=r" (tbl));
324 return tbl;
325 }
326
327 int64_t cpu_get_real_ticks(void)
328 {
329 uint32_t l, h, h1;
330 /* NOTE: we test if wrapping has occurred */
331 do {
332 h = get_tbu();
333 l = get_tbl();
334 h1 = get_tbu();
335 } while (h != h1);
336 return ((int64_t)h << 32) | l;
337 }
338
339 #elif defined(__i386__)
340
341 int64_t cpu_get_real_ticks(void)
342 {
343 int64_t val;
344 asm("rdtsc" : "=A" (val));
345 return val;
346 }
347
348 #else
349 #error unsupported CPU
350 #endif
351
352 static int64_t cpu_ticks_offset;
353 static int64_t cpu_ticks_last;
354
355 int64_t cpu_get_ticks(void)
356 {
357 return cpu_get_real_ticks() + cpu_ticks_offset;
358 }
359
360 /* enable cpu_get_ticks() */
361 void cpu_enable_ticks(void)
362 {
363 cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
364 }
365
366 /* disable cpu_get_ticks() : the clock is stopped. You must not call
367 cpu_get_ticks() after that. */
368 void cpu_disable_ticks(void)
369 {
370 cpu_ticks_last = cpu_get_ticks();
371 }
372
373 int64_t get_clock(void)
374 {
375 struct timeval tv;
376 gettimeofday(&tv, NULL);
377 return tv.tv_sec * 1000000LL + tv.tv_usec;
378 }
379
380 void cpu_calibrate_ticks(void)
381 {
382 int64_t usec, ticks;
383
384 usec = get_clock();
385 ticks = cpu_get_ticks();
386 usleep(50 * 1000);
387 usec = get_clock() - usec;
388 ticks = cpu_get_ticks() - ticks;
389 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
390 }
391
392 /* compute with 96 bit intermediate result: (a*b)/c */
393 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
394 {
395 union {
396 uint64_t ll;
397 struct {
398 #ifdef WORDS_BIGENDIAN
399 uint32_t high, low;
400 #else
401 uint32_t low, high;
402 #endif
403 } l;
404 } u, res;
405 uint64_t rl, rh;
406
407 u.ll = a;
408 rl = (uint64_t)u.l.low * (uint64_t)b;
409 rh = (uint64_t)u.l.high * (uint64_t)b;
410 rh += (rl >> 32);
411 res.l.high = rh / c;
412 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
413 return res.ll;
414 }
415
416 /***********************************************************/
417 /* serial device */
418
419 int serial_open_device(void)
420 {
421 char slave_name[1024];
422 int master_fd, slave_fd;
423
424 if (serial_console == NULL && nographic) {
425 /* use console for serial port */
426 return 0;
427 } else {
428 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
429 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
430 return -1;
431 }
432 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
433 return master_fd;
434 }
435 }
436
437 /***********************************************************/
438 /* Linux network device redirector */
439
440 static int tun_open(char *ifname, int ifname_size)
441 {
442 struct ifreq ifr;
443 int fd, ret;
444
445 fd = open("/dev/net/tun", O_RDWR);
446 if (fd < 0) {
447 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
448 return -1;
449 }
450 memset(&ifr, 0, sizeof(ifr));
451 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
452 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
453 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
454 if (ret != 0) {
455 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
456 close(fd);
457 return -1;
458 }
459 printf("Connected to host network interface: %s\n", ifr.ifr_name);
460 pstrcpy(ifname, ifname_size, ifr.ifr_name);
461 fcntl(fd, F_SETFL, O_NONBLOCK);
462 return fd;
463 }
464
465 static int net_init(void)
466 {
467 int pid, status, launch_script, i;
468 NetDriverState *nd;
469 char *args[MAX_NICS + 2];
470 char **parg;
471
472 launch_script = 0;
473 for(i = 0; i < nb_nics; i++) {
474 nd = &nd_table[i];
475 if (nd->fd < 0) {
476 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
477 if (nd->fd >= 0)
478 launch_script = 1;
479 }
480 }
481
482 if (launch_script) {
483 /* try to launch network init script */
484 pid = fork();
485 if (pid >= 0) {
486 if (pid == 0) {
487 parg = args;
488 *parg++ = network_script;
489 for(i = 0; i < nb_nics; i++) {
490 nd = &nd_table[i];
491 if (nd->fd >= 0) {
492 *parg++ = nd->ifname;
493 }
494 }
495 *parg++ = NULL;
496 execv(network_script, args);
497 exit(1);
498 }
499 while (waitpid(pid, &status, 0) != pid);
500 if (!WIFEXITED(status) ||
501 WEXITSTATUS(status) != 0) {
502 fprintf(stderr, "%s: could not launch network script\n",
503 network_script);
504 }
505 }
506 }
507 return 0;
508 }
509
510 void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
511 {
512 #ifdef DEBUG_NE2000
513 printf("NE2000: sending packet size=%d\n", size);
514 #endif
515 write(nd->fd, buf, size);
516 }
517
518 /***********************************************************/
519 /* dumb display */
520
521 /* init terminal so that we can grab keys */
522 static struct termios oldtty;
523
524 static void term_exit(void)
525 {
526 tcsetattr (0, TCSANOW, &oldtty);
527 }
528
529 static void term_init(void)
530 {
531 struct termios tty;
532
533 tcgetattr (0, &tty);
534 oldtty = tty;
535
536 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
537 |INLCR|IGNCR|ICRNL|IXON);
538 tty.c_oflag |= OPOST;
539 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
540 /* if graphical mode, we allow Ctrl-C handling */
541 if (nographic)
542 tty.c_lflag &= ~ISIG;
543 tty.c_cflag &= ~(CSIZE|PARENB);
544 tty.c_cflag |= CS8;
545 tty.c_cc[VMIN] = 1;
546 tty.c_cc[VTIME] = 0;
547
548 tcsetattr (0, TCSANOW, &tty);
549
550 atexit(term_exit);
551
552 fcntl(0, F_SETFL, O_NONBLOCK);
553 }
554
555 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
556 {
557 }
558
559 static void dumb_resize(DisplayState *ds, int w, int h)
560 {
561 }
562
563 static void dumb_refresh(DisplayState *ds)
564 {
565 vga_update_display();
566 }
567
568 void dumb_display_init(DisplayState *ds)
569 {
570 ds->data = NULL;
571 ds->linesize = 0;
572 ds->depth = 0;
573 ds->dpy_update = dumb_update;
574 ds->dpy_resize = dumb_resize;
575 ds->dpy_refresh = dumb_refresh;
576 }
577
578 #if !defined(CONFIG_SOFTMMU)
579 /***********************************************************/
580 /* cpu signal handler */
581 static void host_segv_handler(int host_signum, siginfo_t *info,
582 void *puc)
583 {
584 if (cpu_signal_handler(host_signum, info, puc))
585 return;
586 term_exit();
587 abort();
588 }
589 #endif
590
591 static int timer_irq_pending;
592 static int timer_irq_count;
593
594 static int timer_ms;
595 static int gui_refresh_pending, gui_refresh_count;
596
597 static void host_alarm_handler(int host_signum, siginfo_t *info,
598 void *puc)
599 {
600 /* NOTE: since usually the OS asks a 100 Hz clock, there can be
601 some drift between cpu_get_ticks() and the interrupt time. So
602 we queue some interrupts to avoid missing some */
603 timer_irq_count += pit_get_out_edges(&pit_channels[0]);
604 if (timer_irq_count) {
605 if (timer_irq_count > 2)
606 timer_irq_count = 2;
607 timer_irq_count--;
608 timer_irq_pending = 1;
609 }
610 gui_refresh_count += timer_ms;
611 if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
612 gui_refresh_count = 0;
613 gui_refresh_pending = 1;
614 }
615
616 if (gui_refresh_pending || timer_irq_pending) {
617 /* just exit from the cpu to have a chance to handle timers */
618 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
619 }
620 }
621
622 #define MAX_IO_HANDLERS 64
623
624 typedef struct IOHandlerRecord {
625 int fd;
626 IOCanRWHandler *fd_can_read;
627 IOReadHandler *fd_read;
628 void *opaque;
629 /* temporary data */
630 struct pollfd *ufd;
631 int max_size;
632 } IOHandlerRecord;
633
634 static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
635 static int nb_io_handlers = 0;
636
637 int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
638 IOReadHandler *fd_read, void *opaque)
639 {
640 IOHandlerRecord *ioh;
641
642 if (nb_io_handlers >= MAX_IO_HANDLERS)
643 return -1;
644 ioh = &io_handlers[nb_io_handlers];
645 ioh->fd = fd;
646 ioh->fd_can_read = fd_can_read;
647 ioh->fd_read = fd_read;
648 ioh->opaque = opaque;
649 nb_io_handlers++;
650 return 0;
651 }
652
653 /* main execution loop */
654
655 CPUState *cpu_gdbstub_get_env(void *opaque)
656 {
657 return global_env;
658 }
659
660 int main_loop(void *opaque)
661 {
662 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
663 int ret, n, timeout, serial_ok, max_size, i;
664 uint8_t buf[4096];
665 IOHandlerRecord *ioh;
666 CPUState *env = global_env;
667
668 if (!term_inited) {
669 /* initialize terminal only there so that the user has a
670 chance to stop QEMU with Ctrl-C before the gdb connection
671 is launched */
672 term_inited = 1;
673 term_init();
674 }
675
676 serial_ok = 1;
677 cpu_enable_ticks();
678 for(;;) {
679 #if defined (DO_TB_FLUSH)
680 tb_flush();
681 #endif
682 ret = cpu_exec(env);
683 if (reset_requested) {
684 ret = EXCP_INTERRUPT;
685 break;
686 }
687 if (ret == EXCP_DEBUG) {
688 ret = EXCP_DEBUG;
689 break;
690 }
691 /* if hlt instruction, we wait until the next IRQ */
692 if (ret == EXCP_HLT)
693 timeout = 10;
694 else
695 timeout = 0;
696
697 /* poll any events */
698 pf = ufds;
699 ioh = io_handlers;
700 for(i = 0; i < nb_io_handlers; i++) {
701 max_size = ioh->fd_can_read(ioh->opaque);
702 if (max_size > 0) {
703 if (max_size > sizeof(buf))
704 max_size = sizeof(buf);
705 pf->fd = ioh->fd;
706 pf->events = POLLIN;
707 ioh->ufd = pf;
708 pf++;
709 } else {
710 ioh->ufd = NULL;
711 }
712 ioh->max_size = max_size;
713 ioh++;
714 }
715
716 gdb_ufd = NULL;
717 if (gdbstub_fd > 0) {
718 gdb_ufd = pf;
719 pf->fd = gdbstub_fd;
720 pf->events = POLLIN;
721 pf++;
722 }
723
724 ret = poll(ufds, pf - ufds, timeout);
725 if (ret > 0) {
726 ioh = io_handlers;
727 for(i = 0; i < nb_io_handlers; i++) {
728 pf = ioh->ufd;
729 if (pf) {
730 n = read(ioh->fd, buf, ioh->max_size);
731 if (n > 0) {
732 ioh->fd_read(ioh->opaque, buf, n);
733 }
734 }
735 ioh++;
736 }
737 if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
738 uint8_t buf[1];
739 /* stop emulation if requested by gdb */
740 n = read(gdbstub_fd, buf, 1);
741 if (n == 1) {
742 ret = EXCP_INTERRUPT;
743 break;
744 }
745 }
746 }
747
748 /* timer IRQ */
749 if (timer_irq_pending) {
750 #if defined (TARGET_I386)
751 pic_set_irq(0, 1);
752 pic_set_irq(0, 0);
753 timer_irq_pending = 0;
754 rtc_timer();
755 #endif
756 }
757 /* XXX: add explicit timer */
758 SB16_run();
759
760 /* run dma transfers, if any */
761 DMA_run();
762
763 /* VGA */
764 if (gui_refresh_pending) {
765 display_state.dpy_refresh(&display_state);
766 gui_refresh_pending = 0;
767 }
768 }
769 cpu_disable_ticks();
770 return ret;
771 }
772
773 void help(void)
774 {
775 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
776 "usage: %s [options] [disk_image]\n"
777 "\n"
778 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
779 "\n"
780 "Standard options:\n"
781 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
782 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
783 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
784 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
785 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
786 "-snapshot write to temporary files instead of disk image files\n"
787 "-m megs set virtual RAM size to megs MB\n"
788 "-nographic disable graphical output and redirect serial I/Os to console\n"
789 "\n"
790 "Network options:\n"
791 "-n script set network init script [default=%s]\n"
792 "-nics n simulate 'n' network interfaces [default=1]\n"
793 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
794 "\n"
795 "Linux boot specific:\n"
796 "-kernel bzImage use 'bzImage' as kernel image\n"
797 "-append cmdline use 'cmdline' as kernel command line\n"
798 "-initrd file use 'file' as initial ram disk\n"
799 "\n"
800 "Debug/Expert options:\n"
801 "-s wait gdb connection to port %d\n"
802 "-p port change gdb connection port\n"
803 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
804 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
805 "-L path set the directory for the BIOS and VGA BIOS\n"
806 #ifdef USE_CODE_COPY
807 "-no-code-copy disable code copy acceleration\n"
808 #endif
809
810 "\n"
811 "During emulation, use C-a h to get terminal commands:\n",
812 #ifdef CONFIG_SOFTMMU
813 "qemu",
814 #else
815 "qemu-fast",
816 #endif
817 DEFAULT_NETWORK_SCRIPT,
818 DEFAULT_GDBSTUB_PORT,
819 "/tmp/qemu.log");
820 term_print_help();
821 #ifndef CONFIG_SOFTMMU
822 printf("\n"
823 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
824 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
825 "PC emulation.\n");
826 #endif
827 exit(1);
828 }
829
830 struct option long_options[] = {
831 { "initrd", 1, NULL, 0, },
832 { "hda", 1, NULL, 0, },
833 { "hdb", 1, NULL, 0, },
834 { "snapshot", 0, NULL, 0, },
835 { "hdachs", 1, NULL, 0, },
836 { "nographic", 0, NULL, 0, },
837 { "kernel", 1, NULL, 0, },
838 { "append", 1, NULL, 0, },
839 { "tun-fd", 1, NULL, 0, },
840 { "hdc", 1, NULL, 0, },
841 { "hdd", 1, NULL, 0, },
842 { "cdrom", 1, NULL, 0, },
843 { "boot", 1, NULL, 0, },
844 { "fda", 1, NULL, 0, },
845 { "fdb", 1, NULL, 0, },
846 { "no-code-copy", 0, NULL, 0 },
847 { "nics", 1, NULL, 0 },
848 { NULL, 0, NULL, 0 },
849 };
850
851 #ifdef CONFIG_SDL
852 /* SDL use the pthreads and they modify sigaction. We don't
853 want that. */
854 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
855 extern void __libc_sigaction();
856 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
857 #else
858 extern void __sigaction();
859 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
860 #endif
861 #endif /* CONFIG_SDL */
862
863 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
864
865 /* this stack is only used during signal handling */
866 #define SIGNAL_STACK_SIZE 32768
867
868 static uint8_t *signal_stack;
869
870 #endif
871
872 int main(int argc, char **argv)
873 {
874 int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
875 int snapshot, linux_boot;
876 struct sigaction act;
877 struct itimerval itv;
878 CPUState *env;
879 const char *initrd_filename;
880 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
881 const char *kernel_filename, *kernel_cmdline;
882 DisplayState *ds = &display_state;
883 int cyls, heads, secs;
884
885 /* we never want that malloc() uses mmap() */
886 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
887 initrd_filename = NULL;
888 for(i = 0; i < MAX_FD; i++)
889 fd_filename[i] = NULL;
890 for(i = 0; i < MAX_DISKS; i++)
891 hd_filename[i] = NULL;
892 ram_size = 32 * 1024 * 1024;
893 vga_ram_size = VGA_RAM_SIZE;
894 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
895 use_gdbstub = 0;
896 gdbstub_port = DEFAULT_GDBSTUB_PORT;
897 snapshot = 0;
898 nographic = 0;
899 kernel_filename = NULL;
900 kernel_cmdline = "";
901 has_cdrom = 1;
902 cyls = heads = secs = 0;
903
904 nb_nics = 1;
905 for(i = 0; i < MAX_NICS; i++) {
906 NetDriverState *nd = &nd_table[i];
907 nd->fd = -1;
908 /* init virtual mac address */
909 nd->macaddr[0] = 0x52;
910 nd->macaddr[1] = 0x54;
911 nd->macaddr[2] = 0x00;
912 nd->macaddr[3] = 0x12;
913 nd->macaddr[4] = 0x34;
914 nd->macaddr[5] = 0x56 + i;
915 }
916
917 for(;;) {
918 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:", long_options, &long_index);
919 if (c == -1)
920 break;
921 switch(c) {
922 case 0:
923 switch(long_index) {
924 case 0:
925 initrd_filename = optarg;
926 break;
927 case 1:
928 hd_filename[0] = optarg;
929 break;
930 case 2:
931 hd_filename[1] = optarg;
932 break;
933 case 3:
934 snapshot = 1;
935 break;
936 case 4:
937 {
938 const char *p;
939 p = optarg;
940 cyls = strtol(p, (char **)&p, 0);
941 if (*p != ',')
942 goto chs_fail;
943 p++;
944 heads = strtol(p, (char **)&p, 0);
945 if (*p != ',')
946 goto chs_fail;
947 p++;
948 secs = strtol(p, (char **)&p, 0);
949 if (*p != '\0') {
950 chs_fail:
951 cyls = 0;
952 }
953 }
954 break;
955 case 5:
956 nographic = 1;
957 break;
958 case 6:
959 kernel_filename = optarg;
960 break;
961 case 7:
962 kernel_cmdline = optarg;
963 break;
964 case 8:
965 {
966 const char *p;
967 int fd;
968 p = optarg;
969 nb_nics = 0;
970 for(;;) {
971 fd = strtol(p, (char **)&p, 0);
972 nd_table[nb_nics].fd = fd;
973 snprintf(nd_table[nb_nics].ifname,
974 sizeof(nd_table[nb_nics].ifname),
975 "fd%d", nb_nics);
976 nb_nics++;
977 if (*p == ',') {
978 p++;
979 } else if (*p != '\0') {
980 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_nics);
981 exit(1);
982 } else {
983 break;
984 }
985 }
986 }
987 break;
988 case 9:
989 hd_filename[2] = optarg;
990 has_cdrom = 0;
991 break;
992 case 10:
993 hd_filename[3] = optarg;
994 break;
995 case 11:
996 hd_filename[2] = optarg;
997 has_cdrom = 1;
998 break;
999 case 12:
1000 boot_device = optarg[0];
1001 if (boot_device != 'a' && boot_device != 'b' &&
1002 boot_device != 'c' && boot_device != 'd') {
1003 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
1004 exit(1);
1005 }
1006 break;
1007 case 13:
1008 fd_filename[0] = optarg;
1009 break;
1010 case 14:
1011 fd_filename[1] = optarg;
1012 break;
1013 case 15:
1014 code_copy_enabled = 0;
1015 break;
1016 case 16:
1017 nb_nics = atoi(optarg);
1018 if (nb_nics < 1 || nb_nics > MAX_NICS) {
1019 fprintf(stderr, "qemu: invalid number of network interfaces\n");
1020 exit(1);
1021 }
1022 break;
1023 }
1024 break;
1025 case 'h':
1026 help();
1027 break;
1028 case 'm':
1029 ram_size = atoi(optarg) * 1024 * 1024;
1030 if (ram_size <= 0)
1031 help();
1032 if (ram_size > PHYS_RAM_MAX_SIZE) {
1033 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
1034 PHYS_RAM_MAX_SIZE / (1024 * 1024));
1035 exit(1);
1036 }
1037 break;
1038 case 'd':
1039 {
1040 int mask;
1041 CPULogItem *item;
1042
1043 mask = cpu_str_to_log_mask(optarg);
1044 if (!mask) {
1045 printf("Log items (comma separated):\n");
1046 for(item = cpu_log_items; item->mask != 0; item++) {
1047 printf("%-10s %s\n", item->name, item->help);
1048 }
1049 exit(1);
1050 }
1051 cpu_set_log(mask);
1052 }
1053 break;
1054 case 'n':
1055 pstrcpy(network_script, sizeof(network_script), optarg);
1056 break;
1057 case 's':
1058 use_gdbstub = 1;
1059 break;
1060 case 'p':
1061 gdbstub_port = atoi(optarg);
1062 break;
1063 case 'L':
1064 bios_dir = optarg;
1065 break;
1066 }
1067 }
1068
1069 if (optind < argc) {
1070 hd_filename[0] = argv[optind++];
1071 }
1072
1073 linux_boot = (kernel_filename != NULL);
1074
1075 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
1076 fd_filename[0] == '\0')
1077 help();
1078
1079 /* boot to cd by default if no hard disk */
1080 if (hd_filename[0] == '\0' && boot_device == 'c') {
1081 if (fd_filename[0] != '\0')
1082 boot_device = 'a';
1083 else
1084 boot_device = 'd';
1085 }
1086
1087 #if !defined(CONFIG_SOFTMMU)
1088 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1089 {
1090 static uint8_t stdout_buf[4096];
1091 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
1092 }
1093 #else
1094 setvbuf(stdout, NULL, _IOLBF, 0);
1095 #endif
1096
1097 /* init host network redirectors */
1098 net_init();
1099
1100 /* init the memory */
1101 phys_ram_size = ram_size + vga_ram_size;
1102
1103 #ifdef CONFIG_SOFTMMU
1104 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
1105 if (!phys_ram_base) {
1106 fprintf(stderr, "Could not allocate physical memory\n");
1107 exit(1);
1108 }
1109 #else
1110 /* as we must map the same page at several addresses, we must use
1111 a fd */
1112 {
1113 const char *tmpdir;
1114
1115 tmpdir = getenv("QEMU_TMPDIR");
1116 if (!tmpdir)
1117 tmpdir = "/tmp";
1118 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
1119 if (mkstemp(phys_ram_file) < 0) {
1120 fprintf(stderr, "Could not create temporary memory file '%s'\n",
1121 phys_ram_file);
1122 exit(1);
1123 }
1124 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
1125 if (phys_ram_fd < 0) {
1126 fprintf(stderr, "Could not open temporary memory file '%s'\n",
1127 phys_ram_file);
1128 exit(1);
1129 }
1130 ftruncate(phys_ram_fd, phys_ram_size);
1131 unlink(phys_ram_file);
1132 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
1133 phys_ram_size,
1134 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
1135 phys_ram_fd, 0);
1136 if (phys_ram_base == MAP_FAILED) {
1137 fprintf(stderr, "Could not map physical memory\n");
1138 exit(1);
1139 }
1140 }
1141 #endif
1142
1143 /* we always create the cdrom drive, even if no disk is there */
1144 if (has_cdrom) {
1145 bs_table[2] = bdrv_new("cdrom");
1146 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
1147 }
1148
1149 /* open the virtual block devices */
1150 for(i = 0; i < MAX_DISKS; i++) {
1151 if (hd_filename[i]) {
1152 if (!bs_table[i]) {
1153 char buf[64];
1154 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
1155 bs_table[i] = bdrv_new(buf);
1156 }
1157 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
1158 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
1159 hd_filename[i]);
1160 exit(1);
1161 }
1162 if (i == 0 && cyls != 0)
1163 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
1164 }
1165 }
1166
1167 /* we always create at least one floppy disk */
1168 fd_table[0] = bdrv_new("fda");
1169 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
1170
1171 for(i = 0; i < MAX_FD; i++) {
1172 if (fd_filename[i]) {
1173 if (!fd_table[i]) {
1174 char buf[64];
1175 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
1176 fd_table[i] = bdrv_new(buf);
1177 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
1178 }
1179 if (fd_filename[i] != '\0') {
1180 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
1181 fprintf(stderr, "qemu: could not open floppy disk image '%s\n",
1182 fd_filename[i]);
1183 exit(1);
1184 }
1185 }
1186 }
1187 }
1188
1189 /* init CPU state */
1190 env = cpu_init();
1191 global_env = env;
1192 cpu_single_env = env;
1193
1194 init_ioports();
1195 cpu_calibrate_ticks();
1196
1197 /* terminal init */
1198 if (nographic) {
1199 dumb_display_init(ds);
1200 } else {
1201 #ifdef CONFIG_SDL
1202 sdl_display_init(ds);
1203 #else
1204 dumb_display_init(ds);
1205 #endif
1206 }
1207
1208 #if defined(TARGET_I386)
1209 pc_init(ram_size, vga_ram_size, boot_device,
1210 ds, fd_filename, snapshot,
1211 kernel_filename, kernel_cmdline, initrd_filename);
1212 #elif defined(TARGET_PPC)
1213 ppc_init();
1214 #endif
1215
1216 /* launched after the device init so that it can display or not a
1217 banner */
1218 monitor_init();
1219
1220 /* setup cpu signal handlers for MMU / self modifying code handling */
1221 #if !defined(CONFIG_SOFTMMU)
1222
1223 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1224 {
1225 stack_t stk;
1226 signal_stack = malloc(SIGNAL_STACK_SIZE);
1227 stk.ss_sp = signal_stack;
1228 stk.ss_size = SIGNAL_STACK_SIZE;
1229 stk.ss_flags = 0;
1230
1231 if (sigaltstack(&stk, NULL) < 0) {
1232 perror("sigaltstack");
1233 exit(1);
1234 }
1235 }
1236 #endif
1237
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
1243 act.sa_sigaction = host_segv_handler;
1244 sigaction(SIGSEGV, &act, NULL);
1245 sigaction(SIGBUS, &act, NULL);
1246 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1247 sigaction(SIGFPE, &act, NULL);
1248 #endif
1249 #endif
1250
1251 /* timer signal */
1252 sigfillset(&act.sa_mask);
1253 act.sa_flags = SA_SIGINFO;
1254 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1255 act.sa_flags |= SA_ONSTACK;
1256 #endif
1257 act.sa_sigaction = host_alarm_handler;
1258 sigaction(SIGALRM, &act, NULL);
1259
1260 itv.it_interval.tv_sec = 0;
1261 itv.it_interval.tv_usec = 1000;
1262 itv.it_value.tv_sec = 0;
1263 itv.it_value.tv_usec = 10 * 1000;
1264 setitimer(ITIMER_REAL, &itv, NULL);
1265 /* we probe the tick duration of the kernel to inform the user if
1266 the emulated kernel requested a too high timer frequency */
1267 getitimer(ITIMER_REAL, &itv);
1268 timer_ms = itv.it_interval.tv_usec / 1000;
1269 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1270 1000000;
1271
1272 if (use_gdbstub) {
1273 cpu_gdbstub(NULL, main_loop, gdbstub_port);
1274 } else {
1275 main_loop(NULL);
1276 }
1277 return 0;
1278 }