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