]>
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 | 23 | */ |
67b915a5 FB |
24 | #include "vl.h" |
25 | ||
0824d6fc | 26 | #include <getopt.h> |
0824d6fc | 27 | #include <unistd.h> |
0824d6fc FB |
28 | #include <fcntl.h> |
29 | #include <signal.h> | |
30 | #include <time.h> | |
0824d6fc | 31 | #include <errno.h> |
67b915a5 FB |
32 | #include <sys/time.h> |
33 | ||
34 | #ifndef _WIN32 | |
35 | #include <sys/times.h> | |
f1510b2c | 36 | #include <sys/wait.h> |
67b915a5 FB |
37 | #include <termios.h> |
38 | #include <sys/poll.h> | |
39 | #include <sys/mman.h> | |
f1510b2c FB |
40 | #include <sys/ioctl.h> |
41 | #include <sys/socket.h> | |
7d3505c5 FB |
42 | #ifdef _BSD |
43 | #include <sys/stat.h> | |
44 | #include <libutil.h> | |
45 | #else | |
f1510b2c FB |
46 | #include <linux/if.h> |
47 | #include <linux/if_tun.h> | |
7d3505c5 FB |
48 | #include <pty.h> |
49 | #include <malloc.h> | |
fd872598 | 50 | #include <linux/rtc.h> |
67b915a5 | 51 | #endif |
7d3505c5 | 52 | #endif |
67b915a5 | 53 | |
c20709aa FB |
54 | #if defined(CONFIG_SLIRP) |
55 | #include "libslirp.h" | |
56 | #endif | |
57 | ||
67b915a5 | 58 | #ifdef _WIN32 |
7d3505c5 | 59 | #include <malloc.h> |
67b915a5 FB |
60 | #include <sys/timeb.h> |
61 | #include <windows.h> | |
62 | #define getopt_long_only getopt_long | |
63 | #define memalign(align, size) malloc(size) | |
64 | #endif | |
65 | ||
73332e5c | 66 | #ifdef CONFIG_SDL |
7d3505c5 | 67 | #if defined(__linux__) |
73332e5c FB |
68 | /* SDL use the pthreads and they modify sigaction. We don't |
69 | want that. */ | |
7d3505c5 | 70 | #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) |
73332e5c FB |
71 | extern void __libc_sigaction(); |
72 | #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact) | |
73 | #else | |
74 | extern void __sigaction(); | |
75 | #define sigaction(sig, act, oact) __sigaction(sig, act, oact) | |
76 | #endif | |
7d3505c5 | 77 | #endif /* __linux__ */ |
73332e5c | 78 | #endif /* CONFIG_SDL */ |
0824d6fc | 79 | |
0824d6fc | 80 | #include "disas.h" |
fc01f7e7 | 81 | |
8a7ddc38 | 82 | #include "exec-all.h" |
0824d6fc | 83 | |
a541f297 FB |
84 | //#define DO_TB_FLUSH |
85 | ||
5a67135a | 86 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
f1510b2c | 87 | |
0824d6fc | 88 | //#define DEBUG_UNUSED_IOPORT |
fd872598 | 89 | //#define DEBUG_IOPORT |
330d0414 | 90 | |
bb551faa | 91 | #if !defined(CONFIG_SOFTMMU) |
7916e224 | 92 | #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) |
bb551faa FB |
93 | #else |
94 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) | |
95 | #endif | |
7916e224 | 96 | |
8a7ddc38 FB |
97 | /* in ms */ |
98 | #define GUI_REFRESH_INTERVAL 30 | |
313aa567 | 99 | |
7dea1da4 FB |
100 | /* XXX: use a two level table to limit memory usage */ |
101 | #define MAX_IOPORTS 65536 | |
0824d6fc | 102 | |
80cabfad | 103 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
0824d6fc | 104 | char phys_ram_file[1024]; |
c45886db FB |
105 | CPUState *global_env; |
106 | CPUState *cpu_single_env; | |
c4b1fcc0 | 107 | void *ioport_opaque[MAX_IOPORTS]; |
fc01f7e7 FB |
108 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
109 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; | |
c45886db | 110 | BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD]; |
313aa567 FB |
111 | int vga_ram_size; |
112 | static DisplayState display_state; | |
a20dd508 | 113 | int nographic; |
313aa567 | 114 | int64_t ticks_per_sec; |
36b486bb | 115 | int boot_device = 'c'; |
1ccde1cb | 116 | static int ram_size; |
80cabfad FB |
117 | static char network_script[1024]; |
118 | int pit_min_timer_count = 0; | |
c4b1fcc0 FB |
119 | int nb_nics; |
120 | NetDriverState nd_table[MAX_NICS]; | |
121 | SerialState *serial_console; | |
8a7ddc38 FB |
122 | QEMUTimer *gui_timer; |
123 | int vm_running; | |
aaaa7df6 | 124 | int audio_enabled = 0; |
0824d6fc FB |
125 | |
126 | /***********************************************************/ | |
26aa7d72 FB |
127 | /* x86 ISA bus support */ |
128 | ||
129 | target_phys_addr_t isa_mem_base = 0; | |
0824d6fc | 130 | |
c4b1fcc0 | 131 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
0824d6fc FB |
132 | { |
133 | #ifdef DEBUG_UNUSED_IOPORT | |
134 | fprintf(stderr, "inb: port=0x%04x\n", address); | |
135 | #endif | |
fc01f7e7 | 136 | return 0xff; |
0824d6fc FB |
137 | } |
138 | ||
c4b1fcc0 | 139 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
0824d6fc FB |
140 | { |
141 | #ifdef DEBUG_UNUSED_IOPORT | |
142 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); | |
143 | #endif | |
144 | } | |
145 | ||
146 | /* default is to make two byte accesses */ | |
c4b1fcc0 | 147 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
0824d6fc FB |
148 | { |
149 | uint32_t data; | |
db45c29a FB |
150 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
151 | address = (address + 1) & (MAX_IOPORTS - 1); | |
152 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; | |
0824d6fc FB |
153 | return data; |
154 | } | |
155 | ||
c4b1fcc0 | 156 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
0824d6fc | 157 | { |
db45c29a FB |
158 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
159 | address = (address + 1) & (MAX_IOPORTS - 1); | |
160 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); | |
0824d6fc FB |
161 | } |
162 | ||
c4b1fcc0 | 163 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
0824d6fc | 164 | { |
fc01f7e7 FB |
165 | #ifdef DEBUG_UNUSED_IOPORT |
166 | fprintf(stderr, "inl: port=0x%04x\n", address); | |
167 | #endif | |
168 | return 0xffffffff; | |
0824d6fc FB |
169 | } |
170 | ||
c4b1fcc0 | 171 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
0824d6fc | 172 | { |
fc01f7e7 FB |
173 | #ifdef DEBUG_UNUSED_IOPORT |
174 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); | |
175 | #endif | |
0824d6fc FB |
176 | } |
177 | ||
fc01f7e7 | 178 | void init_ioports(void) |
0824d6fc FB |
179 | { |
180 | int i; | |
181 | ||
fc01f7e7 FB |
182 | for(i = 0; i < MAX_IOPORTS; i++) { |
183 | ioport_read_table[0][i] = default_ioport_readb; | |
184 | ioport_write_table[0][i] = default_ioport_writeb; | |
185 | ioport_read_table[1][i] = default_ioport_readw; | |
186 | ioport_write_table[1][i] = default_ioport_writew; | |
187 | ioport_read_table[2][i] = default_ioport_readl; | |
188 | ioport_write_table[2][i] = default_ioport_writel; | |
189 | } | |
0824d6fc FB |
190 | } |
191 | ||
fc01f7e7 | 192 | /* size is the word size in byte */ |
c4b1fcc0 FB |
193 | int register_ioport_read(int start, int length, int size, |
194 | IOPortReadFunc *func, void *opaque) | |
f1510b2c | 195 | { |
fc01f7e7 | 196 | int i, bsize; |
f1510b2c | 197 | |
c4b1fcc0 | 198 | if (size == 1) { |
fc01f7e7 | 199 | bsize = 0; |
c4b1fcc0 | 200 | } else if (size == 2) { |
fc01f7e7 | 201 | bsize = 1; |
c4b1fcc0 | 202 | } else if (size == 4) { |
fc01f7e7 | 203 | bsize = 2; |
c4b1fcc0 FB |
204 | } else { |
205 | hw_error("register_ioport_read: invalid size"); | |
fc01f7e7 | 206 | return -1; |
c4b1fcc0 FB |
207 | } |
208 | for(i = start; i < start + length; i += size) { | |
fc01f7e7 | 209 | ioport_read_table[bsize][i] = func; |
c4b1fcc0 FB |
210 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
211 | hw_error("register_ioport_read: invalid opaque"); | |
212 | ioport_opaque[i] = opaque; | |
213 | } | |
f1510b2c FB |
214 | return 0; |
215 | } | |
216 | ||
fc01f7e7 | 217 | /* size is the word size in byte */ |
c4b1fcc0 FB |
218 | int register_ioport_write(int start, int length, int size, |
219 | IOPortWriteFunc *func, void *opaque) | |
f1510b2c | 220 | { |
fc01f7e7 | 221 | int i, bsize; |
f1510b2c | 222 | |
c4b1fcc0 | 223 | if (size == 1) { |
fc01f7e7 | 224 | bsize = 0; |
c4b1fcc0 | 225 | } else if (size == 2) { |
fc01f7e7 | 226 | bsize = 1; |
c4b1fcc0 | 227 | } else if (size == 4) { |
fc01f7e7 | 228 | bsize = 2; |
c4b1fcc0 FB |
229 | } else { |
230 | hw_error("register_ioport_write: invalid size"); | |
fc01f7e7 | 231 | return -1; |
c4b1fcc0 FB |
232 | } |
233 | for(i = start; i < start + length; i += size) { | |
fc01f7e7 | 234 | ioport_write_table[bsize][i] = func; |
c4b1fcc0 FB |
235 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
236 | hw_error("register_ioport_read: invalid opaque"); | |
237 | ioport_opaque[i] = opaque; | |
238 | } | |
f1510b2c FB |
239 | return 0; |
240 | } | |
241 | ||
0824d6fc FB |
242 | void pstrcpy(char *buf, int buf_size, const char *str) |
243 | { | |
244 | int c; | |
245 | char *q = buf; | |
246 | ||
247 | if (buf_size <= 0) | |
248 | return; | |
249 | ||
250 | for(;;) { | |
251 | c = *str++; | |
252 | if (c == 0 || q >= buf + buf_size - 1) | |
253 | break; | |
254 | *q++ = c; | |
255 | } | |
256 | *q = '\0'; | |
257 | } | |
258 | ||
259 | /* strcat and truncate. */ | |
260 | char *pstrcat(char *buf, int buf_size, const char *s) | |
261 | { | |
262 | int len; | |
263 | len = strlen(buf); | |
264 | if (len < buf_size) | |
265 | pstrcpy(buf + len, buf_size - len, s); | |
266 | return buf; | |
267 | } | |
268 | ||
0824d6fc FB |
269 | /* return the size or -1 if error */ |
270 | int load_image(const char *filename, uint8_t *addr) | |
271 | { | |
272 | int fd, size; | |
40c3bac3 | 273 | fd = open(filename, O_RDONLY | O_BINARY); |
0824d6fc FB |
274 | if (fd < 0) |
275 | return -1; | |
276 | size = lseek(fd, 0, SEEK_END); | |
277 | lseek(fd, 0, SEEK_SET); | |
278 | if (read(fd, addr, size) != size) { | |
279 | close(fd); | |
280 | return -1; | |
281 | } | |
282 | close(fd); | |
283 | return size; | |
284 | } | |
285 | ||
c45886db | 286 | void cpu_outb(CPUState *env, int addr, int val) |
0824d6fc | 287 | { |
fd872598 FB |
288 | #ifdef DEBUG_IOPORT |
289 | if (loglevel & CPU_LOG_IOPORT) | |
290 | fprintf(logfile, "outb: %04x %02x\n", addr, val); | |
291 | #endif | |
c4b1fcc0 | 292 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
0824d6fc FB |
293 | } |
294 | ||
c45886db | 295 | void cpu_outw(CPUState *env, int addr, int val) |
0824d6fc | 296 | { |
fd872598 FB |
297 | #ifdef DEBUG_IOPORT |
298 | if (loglevel & CPU_LOG_IOPORT) | |
299 | fprintf(logfile, "outw: %04x %04x\n", addr, val); | |
300 | #endif | |
c4b1fcc0 | 301 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
0824d6fc FB |
302 | } |
303 | ||
c45886db | 304 | void cpu_outl(CPUState *env, int addr, int val) |
0824d6fc | 305 | { |
fd872598 FB |
306 | #ifdef DEBUG_IOPORT |
307 | if (loglevel & CPU_LOG_IOPORT) | |
308 | fprintf(logfile, "outl: %04x %08x\n", addr, val); | |
309 | #endif | |
c4b1fcc0 | 310 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
0824d6fc FB |
311 | } |
312 | ||
c45886db | 313 | int cpu_inb(CPUState *env, int addr) |
0824d6fc | 314 | { |
fd872598 | 315 | int val; |
fd872598 FB |
316 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
317 | #ifdef DEBUG_IOPORT | |
318 | if (loglevel & CPU_LOG_IOPORT) | |
319 | fprintf(logfile, "inb : %04x %02x\n", addr, val); | |
320 | #endif | |
321 | return val; | |
0824d6fc FB |
322 | } |
323 | ||
c45886db | 324 | int cpu_inw(CPUState *env, int addr) |
0824d6fc | 325 | { |
fd872598 | 326 | int val; |
fd872598 FB |
327 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
328 | #ifdef DEBUG_IOPORT | |
329 | if (loglevel & CPU_LOG_IOPORT) | |
330 | fprintf(logfile, "inw : %04x %04x\n", addr, val); | |
331 | #endif | |
332 | return val; | |
0824d6fc FB |
333 | } |
334 | ||
c45886db | 335 | int cpu_inl(CPUState *env, int addr) |
0824d6fc | 336 | { |
fd872598 | 337 | int val; |
fd872598 FB |
338 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
339 | #ifdef DEBUG_IOPORT | |
340 | if (loglevel & CPU_LOG_IOPORT) | |
341 | fprintf(logfile, "inl : %04x %08x\n", addr, val); | |
342 | #endif | |
343 | return val; | |
0824d6fc FB |
344 | } |
345 | ||
346 | /***********************************************************/ | |
0824d6fc FB |
347 | void hw_error(const char *fmt, ...) |
348 | { | |
349 | va_list ap; | |
350 | ||
351 | va_start(ap, fmt); | |
352 | fprintf(stderr, "qemu: hardware error: "); | |
353 | vfprintf(stderr, fmt, ap); | |
354 | fprintf(stderr, "\n"); | |
355 | #ifdef TARGET_I386 | |
356 | cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP); | |
c45886db FB |
357 | #else |
358 | cpu_dump_state(global_env, stderr, 0); | |
0824d6fc FB |
359 | #endif |
360 | va_end(ap); | |
361 | abort(); | |
362 | } | |
363 | ||
8a7ddc38 FB |
364 | /***********************************************************/ |
365 | /* timers */ | |
366 | ||
34865134 FB |
367 | #if defined(__powerpc__) |
368 | ||
369 | static inline uint32_t get_tbl(void) | |
0824d6fc | 370 | { |
34865134 FB |
371 | uint32_t tbl; |
372 | asm volatile("mftb %0" : "=r" (tbl)); | |
373 | return tbl; | |
0824d6fc FB |
374 | } |
375 | ||
34865134 FB |
376 | static inline uint32_t get_tbu(void) |
377 | { | |
378 | uint32_t tbl; | |
379 | asm volatile("mftbu %0" : "=r" (tbl)); | |
380 | return tbl; | |
381 | } | |
382 | ||
383 | int64_t cpu_get_real_ticks(void) | |
384 | { | |
385 | uint32_t l, h, h1; | |
386 | /* NOTE: we test if wrapping has occurred */ | |
387 | do { | |
388 | h = get_tbu(); | |
389 | l = get_tbl(); | |
390 | h1 = get_tbu(); | |
391 | } while (h != h1); | |
392 | return ((int64_t)h << 32) | l; | |
393 | } | |
394 | ||
395 | #elif defined(__i386__) | |
396 | ||
397 | int64_t cpu_get_real_ticks(void) | |
0824d6fc FB |
398 | { |
399 | int64_t val; | |
e463b581 | 400 | asm volatile ("rdtsc" : "=A" (val)); |
0824d6fc FB |
401 | return val; |
402 | } | |
403 | ||
1115dde7 FB |
404 | #elif defined(__x86_64__) |
405 | ||
406 | int64_t cpu_get_real_ticks(void) | |
407 | { | |
408 | uint32_t low,high; | |
409 | int64_t val; | |
410 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); | |
411 | val = high; | |
412 | val <<= 32; | |
413 | val |= low; | |
414 | return val; | |
415 | } | |
416 | ||
34865134 FB |
417 | #else |
418 | #error unsupported CPU | |
419 | #endif | |
420 | ||
421 | static int64_t cpu_ticks_offset; | |
8a7ddc38 | 422 | static int cpu_ticks_enabled; |
34865134 | 423 | |
8a7ddc38 | 424 | static inline int64_t cpu_get_ticks(void) |
34865134 | 425 | { |
8a7ddc38 FB |
426 | if (!cpu_ticks_enabled) { |
427 | return cpu_ticks_offset; | |
428 | } else { | |
429 | return cpu_get_real_ticks() + cpu_ticks_offset; | |
430 | } | |
34865134 FB |
431 | } |
432 | ||
433 | /* enable cpu_get_ticks() */ | |
434 | void cpu_enable_ticks(void) | |
435 | { | |
8a7ddc38 FB |
436 | if (!cpu_ticks_enabled) { |
437 | cpu_ticks_offset -= cpu_get_real_ticks(); | |
438 | cpu_ticks_enabled = 1; | |
439 | } | |
34865134 FB |
440 | } |
441 | ||
442 | /* disable cpu_get_ticks() : the clock is stopped. You must not call | |
443 | cpu_get_ticks() after that. */ | |
444 | void cpu_disable_ticks(void) | |
445 | { | |
8a7ddc38 FB |
446 | if (cpu_ticks_enabled) { |
447 | cpu_ticks_offset = cpu_get_ticks(); | |
448 | cpu_ticks_enabled = 0; | |
449 | } | |
34865134 FB |
450 | } |
451 | ||
67b915a5 | 452 | static int64_t get_clock(void) |
34865134 | 453 | { |
67b915a5 FB |
454 | #ifdef _WIN32 |
455 | struct _timeb tb; | |
456 | _ftime(&tb); | |
457 | return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000; | |
458 | #else | |
34865134 FB |
459 | struct timeval tv; |
460 | gettimeofday(&tv, NULL); | |
461 | return tv.tv_sec * 1000000LL + tv.tv_usec; | |
67b915a5 | 462 | #endif |
34865134 FB |
463 | } |
464 | ||
0824d6fc FB |
465 | void cpu_calibrate_ticks(void) |
466 | { | |
467 | int64_t usec, ticks; | |
468 | ||
469 | usec = get_clock(); | |
8a7ddc38 | 470 | ticks = cpu_get_real_ticks(); |
67b915a5 FB |
471 | #ifdef _WIN32 |
472 | Sleep(50); | |
473 | #else | |
0824d6fc | 474 | usleep(50 * 1000); |
67b915a5 | 475 | #endif |
0824d6fc | 476 | usec = get_clock() - usec; |
8a7ddc38 | 477 | ticks = cpu_get_real_ticks() - ticks; |
0824d6fc FB |
478 | ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; |
479 | } | |
480 | ||
87858c89 | 481 | /* compute with 96 bit intermediate result: (a*b)/c */ |
80cabfad | 482 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
87858c89 FB |
483 | { |
484 | union { | |
485 | uint64_t ll; | |
486 | struct { | |
487 | #ifdef WORDS_BIGENDIAN | |
488 | uint32_t high, low; | |
489 | #else | |
490 | uint32_t low, high; | |
491 | #endif | |
492 | } l; | |
493 | } u, res; | |
494 | uint64_t rl, rh; | |
495 | ||
496 | u.ll = a; | |
497 | rl = (uint64_t)u.l.low * (uint64_t)b; | |
498 | rh = (uint64_t)u.l.high * (uint64_t)b; | |
499 | rh += (rl >> 32); | |
500 | res.l.high = rh / c; | |
501 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; | |
502 | return res.ll; | |
503 | } | |
504 | ||
8a7ddc38 FB |
505 | #define QEMU_TIMER_REALTIME 0 |
506 | #define QEMU_TIMER_VIRTUAL 1 | |
507 | ||
508 | struct QEMUClock { | |
509 | int type; | |
510 | /* XXX: add frequency */ | |
511 | }; | |
512 | ||
513 | struct QEMUTimer { | |
514 | QEMUClock *clock; | |
515 | int64_t expire_time; | |
516 | QEMUTimerCB *cb; | |
517 | void *opaque; | |
518 | struct QEMUTimer *next; | |
519 | }; | |
520 | ||
521 | QEMUClock *rt_clock; | |
522 | QEMUClock *vm_clock; | |
523 | ||
524 | static QEMUTimer *active_timers[2]; | |
40c3bac3 FB |
525 | #ifdef _WIN32 |
526 | static MMRESULT timerID; | |
527 | #else | |
8a7ddc38 FB |
528 | /* frequency of the times() clock tick */ |
529 | static int timer_freq; | |
67b915a5 | 530 | #endif |
8a7ddc38 FB |
531 | |
532 | QEMUClock *qemu_new_clock(int type) | |
533 | { | |
534 | QEMUClock *clock; | |
535 | clock = qemu_mallocz(sizeof(QEMUClock)); | |
536 | if (!clock) | |
537 | return NULL; | |
538 | clock->type = type; | |
539 | return clock; | |
540 | } | |
541 | ||
542 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) | |
543 | { | |
544 | QEMUTimer *ts; | |
545 | ||
546 | ts = qemu_mallocz(sizeof(QEMUTimer)); | |
547 | ts->clock = clock; | |
548 | ts->cb = cb; | |
549 | ts->opaque = opaque; | |
550 | return ts; | |
551 | } | |
552 | ||
553 | void qemu_free_timer(QEMUTimer *ts) | |
554 | { | |
555 | qemu_free(ts); | |
556 | } | |
557 | ||
558 | /* stop a timer, but do not dealloc it */ | |
559 | void qemu_del_timer(QEMUTimer *ts) | |
560 | { | |
561 | QEMUTimer **pt, *t; | |
562 | ||
563 | /* NOTE: this code must be signal safe because | |
564 | qemu_timer_expired() can be called from a signal. */ | |
565 | pt = &active_timers[ts->clock->type]; | |
566 | for(;;) { | |
567 | t = *pt; | |
568 | if (!t) | |
569 | break; | |
570 | if (t == ts) { | |
571 | *pt = t->next; | |
572 | break; | |
573 | } | |
574 | pt = &t->next; | |
575 | } | |
576 | } | |
577 | ||
578 | /* modify the current timer so that it will be fired when current_time | |
579 | >= expire_time. The corresponding callback will be called. */ | |
580 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) | |
581 | { | |
582 | QEMUTimer **pt, *t; | |
583 | ||
584 | qemu_del_timer(ts); | |
585 | ||
586 | /* add the timer in the sorted list */ | |
587 | /* NOTE: this code must be signal safe because | |
588 | qemu_timer_expired() can be called from a signal. */ | |
589 | pt = &active_timers[ts->clock->type]; | |
590 | for(;;) { | |
591 | t = *pt; | |
592 | if (!t) | |
593 | break; | |
594 | if (t->expire_time > expire_time) | |
595 | break; | |
596 | pt = &t->next; | |
597 | } | |
598 | ts->expire_time = expire_time; | |
599 | ts->next = *pt; | |
600 | *pt = ts; | |
601 | } | |
602 | ||
603 | int qemu_timer_pending(QEMUTimer *ts) | |
604 | { | |
605 | QEMUTimer *t; | |
606 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { | |
607 | if (t == ts) | |
608 | return 1; | |
609 | } | |
610 | return 0; | |
611 | } | |
612 | ||
613 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) | |
614 | { | |
615 | if (!timer_head) | |
616 | return 0; | |
617 | return (timer_head->expire_time <= current_time); | |
618 | } | |
619 | ||
620 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) | |
621 | { | |
622 | QEMUTimer *ts; | |
623 | ||
624 | for(;;) { | |
625 | ts = *ptimer_head; | |
626 | if (ts->expire_time > current_time) | |
627 | break; | |
628 | /* remove timer from the list before calling the callback */ | |
629 | *ptimer_head = ts->next; | |
630 | ts->next = NULL; | |
631 | ||
632 | /* run the callback (the timer list can be modified) */ | |
633 | ts->cb(ts->opaque); | |
634 | } | |
635 | } | |
636 | ||
637 | int64_t qemu_get_clock(QEMUClock *clock) | |
638 | { | |
639 | switch(clock->type) { | |
640 | case QEMU_TIMER_REALTIME: | |
67b915a5 FB |
641 | #ifdef _WIN32 |
642 | return GetTickCount(); | |
643 | #else | |
7d3505c5 FB |
644 | { |
645 | struct tms tp; | |
646 | ||
647 | /* Note that using gettimeofday() is not a good solution | |
648 | for timers because its value change when the date is | |
649 | modified. */ | |
650 | if (timer_freq == 100) { | |
651 | return times(&tp) * 10; | |
652 | } else { | |
653 | return ((int64_t)times(&tp) * 1000) / timer_freq; | |
654 | } | |
8a7ddc38 | 655 | } |
67b915a5 | 656 | #endif |
8a7ddc38 FB |
657 | default: |
658 | case QEMU_TIMER_VIRTUAL: | |
659 | return cpu_get_ticks(); | |
660 | } | |
661 | } | |
662 | ||
663 | /* save a timer */ | |
664 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) | |
665 | { | |
666 | uint64_t expire_time; | |
667 | ||
668 | if (qemu_timer_pending(ts)) { | |
669 | expire_time = ts->expire_time; | |
670 | } else { | |
671 | expire_time = -1; | |
672 | } | |
673 | qemu_put_be64(f, expire_time); | |
674 | } | |
675 | ||
676 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) | |
677 | { | |
678 | uint64_t expire_time; | |
679 | ||
680 | expire_time = qemu_get_be64(f); | |
681 | if (expire_time != -1) { | |
682 | qemu_mod_timer(ts, expire_time); | |
683 | } else { | |
684 | qemu_del_timer(ts); | |
685 | } | |
686 | } | |
687 | ||
688 | static void timer_save(QEMUFile *f, void *opaque) | |
689 | { | |
690 | if (cpu_ticks_enabled) { | |
691 | hw_error("cannot save state if virtual timers are running"); | |
692 | } | |
693 | qemu_put_be64s(f, &cpu_ticks_offset); | |
694 | qemu_put_be64s(f, &ticks_per_sec); | |
695 | } | |
696 | ||
697 | static int timer_load(QEMUFile *f, void *opaque, int version_id) | |
698 | { | |
699 | if (version_id != 1) | |
700 | return -EINVAL; | |
701 | if (cpu_ticks_enabled) { | |
702 | return -EINVAL; | |
703 | } | |
704 | qemu_get_be64s(f, &cpu_ticks_offset); | |
705 | qemu_get_be64s(f, &ticks_per_sec); | |
706 | return 0; | |
707 | } | |
708 | ||
67b915a5 FB |
709 | #ifdef _WIN32 |
710 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, | |
711 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) | |
712 | #else | |
8a7ddc38 | 713 | static void host_alarm_handler(int host_signum) |
67b915a5 | 714 | #endif |
8a7ddc38 FB |
715 | { |
716 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], | |
717 | qemu_get_clock(vm_clock)) || | |
718 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], | |
719 | qemu_get_clock(rt_clock))) { | |
720 | /* stop the cpu because a timer occured */ | |
721 | cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); | |
722 | } | |
723 | } | |
724 | ||
fd872598 FB |
725 | #ifndef _WIN32 |
726 | ||
727 | #define RTC_FREQ 1024 | |
728 | ||
729 | static int rtc_fd; | |
730 | ||
731 | static int start_rtc_timer(void) | |
732 | { | |
733 | rtc_fd = open("/dev/rtc", O_RDONLY); | |
734 | if (rtc_fd < 0) | |
735 | return -1; | |
736 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { | |
737 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" | |
738 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" | |
739 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); | |
740 | goto fail; | |
741 | } | |
742 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { | |
743 | fail: | |
744 | close(rtc_fd); | |
745 | return -1; | |
746 | } | |
747 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; | |
748 | return 0; | |
749 | } | |
750 | ||
751 | #endif | |
752 | ||
8a7ddc38 FB |
753 | static void init_timers(void) |
754 | { | |
8a7ddc38 FB |
755 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
756 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); | |
757 | ||
67b915a5 FB |
758 | #ifdef _WIN32 |
759 | { | |
760 | int count=0; | |
40c3bac3 FB |
761 | timerID = timeSetEvent(10, // interval (ms) |
762 | 0, // resolution | |
763 | host_alarm_handler, // function | |
764 | (DWORD)&count, // user parameter | |
765 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); | |
67b915a5 FB |
766 | if( !timerID ) { |
767 | perror("failed timer alarm"); | |
768 | exit(1); | |
769 | } | |
770 | } | |
771 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; | |
772 | #else | |
773 | { | |
774 | struct sigaction act; | |
775 | struct itimerval itv; | |
776 | ||
777 | /* get times() syscall frequency */ | |
778 | timer_freq = sysconf(_SC_CLK_TCK); | |
779 | ||
780 | /* timer signal */ | |
781 | sigfillset(&act.sa_mask); | |
782 | act.sa_flags = 0; | |
8a7ddc38 | 783 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
67b915a5 FB |
784 | act.sa_flags |= SA_ONSTACK; |
785 | #endif | |
786 | act.sa_handler = host_alarm_handler; | |
787 | sigaction(SIGALRM, &act, NULL); | |
fd872598 | 788 | |
67b915a5 FB |
789 | itv.it_interval.tv_sec = 0; |
790 | itv.it_interval.tv_usec = 1000; | |
791 | itv.it_value.tv_sec = 0; | |
792 | itv.it_value.tv_usec = 10 * 1000; | |
793 | setitimer(ITIMER_REAL, &itv, NULL); | |
794 | /* we probe the tick duration of the kernel to inform the user if | |
795 | the emulated kernel requested a too high timer frequency */ | |
796 | getitimer(ITIMER_REAL, &itv); | |
fd872598 FB |
797 | |
798 | if (itv.it_interval.tv_usec > 1000) { | |
799 | /* try to use /dev/rtc to have a faster timer */ | |
800 | if (start_rtc_timer() < 0) | |
801 | goto use_itimer; | |
802 | /* disable itimer */ | |
803 | itv.it_interval.tv_sec = 0; | |
804 | itv.it_interval.tv_usec = 0; | |
805 | itv.it_value.tv_sec = 0; | |
806 | itv.it_value.tv_usec = 0; | |
807 | setitimer(ITIMER_REAL, &itv, NULL); | |
808 | ||
809 | /* use the RTC */ | |
810 | sigaction(SIGIO, &act, NULL); | |
811 | fcntl(rtc_fd, F_SETFL, O_ASYNC); | |
812 | fcntl(rtc_fd, F_SETOWN, getpid()); | |
813 | } else { | |
814 | use_itimer: | |
815 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * | |
816 | PIT_FREQ) / 1000000; | |
817 | } | |
67b915a5 | 818 | } |
8a7ddc38 | 819 | #endif |
8a7ddc38 FB |
820 | } |
821 | ||
40c3bac3 FB |
822 | void quit_timers(void) |
823 | { | |
824 | #ifdef _WIN32 | |
825 | timeKillEvent(timerID); | |
826 | #endif | |
827 | } | |
828 | ||
c4b1fcc0 FB |
829 | /***********************************************************/ |
830 | /* serial device */ | |
313aa567 | 831 | |
67b915a5 FB |
832 | #ifdef _WIN32 |
833 | ||
834 | int serial_open_device(void) | |
835 | { | |
836 | return -1; | |
837 | } | |
838 | ||
839 | #else | |
840 | ||
c4b1fcc0 | 841 | int serial_open_device(void) |
313aa567 | 842 | { |
c4b1fcc0 FB |
843 | char slave_name[1024]; |
844 | int master_fd, slave_fd; | |
313aa567 | 845 | |
c4b1fcc0 FB |
846 | if (serial_console == NULL && nographic) { |
847 | /* use console for serial port */ | |
848 | return 0; | |
80cabfad | 849 | } else { |
c4b1fcc0 FB |
850 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
851 | fprintf(stderr, "warning: could not create pseudo terminal for serial port\n"); | |
852 | return -1; | |
853 | } | |
854 | fprintf(stderr, "Serial port redirected to %s\n", slave_name); | |
855 | return master_fd; | |
330d0414 | 856 | } |
330d0414 FB |
857 | } |
858 | ||
67b915a5 FB |
859 | #endif |
860 | ||
80cabfad | 861 | /***********************************************************/ |
c20709aa | 862 | /* Linux network device redirectors */ |
330d0414 | 863 | |
c20709aa FB |
864 | void hex_dump(FILE *f, const uint8_t *buf, int size) |
865 | { | |
866 | int len, i, j, c; | |
867 | ||
868 | for(i=0;i<size;i+=16) { | |
869 | len = size - i; | |
870 | if (len > 16) | |
871 | len = 16; | |
872 | fprintf(f, "%08x ", i); | |
873 | for(j=0;j<16;j++) { | |
874 | if (j < len) | |
875 | fprintf(f, " %02x", buf[i+j]); | |
876 | else | |
877 | fprintf(f, " "); | |
878 | } | |
879 | fprintf(f, " "); | |
880 | for(j=0;j<len;j++) { | |
881 | c = buf[i+j]; | |
882 | if (c < ' ' || c > '~') | |
883 | c = '.'; | |
884 | fprintf(f, "%c", c); | |
885 | } | |
886 | fprintf(f, "\n"); | |
887 | } | |
888 | } | |
889 | ||
890 | void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size) | |
891 | { | |
892 | nd->send_packet(nd, buf, size); | |
893 | } | |
67b915a5 | 894 | |
c20709aa FB |
895 | void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, |
896 | IOReadHandler *fd_read, void *opaque) | |
67b915a5 | 897 | { |
c20709aa FB |
898 | nd->add_read_packet(nd, fd_can_read, fd_read, opaque); |
899 | } | |
900 | ||
901 | /* dummy network adapter */ | |
902 | ||
903 | static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size) | |
904 | { | |
905 | } | |
906 | ||
907 | static void dummy_add_read_packet(NetDriverState *nd, | |
908 | IOCanRWHandler *fd_can_read, | |
909 | IOReadHandler *fd_read, void *opaque) | |
910 | { | |
911 | } | |
912 | ||
913 | static int net_dummy_init(NetDriverState *nd) | |
914 | { | |
915 | nd->send_packet = dummy_send_packet; | |
916 | nd->add_read_packet = dummy_add_read_packet; | |
917 | pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy"); | |
67b915a5 FB |
918 | return 0; |
919 | } | |
920 | ||
c20709aa FB |
921 | #if defined(CONFIG_SLIRP) |
922 | ||
923 | /* slirp network adapter */ | |
924 | ||
925 | static void *slirp_fd_opaque; | |
926 | static IOCanRWHandler *slirp_fd_can_read; | |
927 | static IOReadHandler *slirp_fd_read; | |
928 | static int slirp_inited; | |
929 | ||
930 | int slirp_can_output(void) | |
931 | { | |
932 | return slirp_fd_can_read(slirp_fd_opaque); | |
933 | } | |
934 | ||
935 | void slirp_output(const uint8_t *pkt, int pkt_len) | |
67b915a5 | 936 | { |
c20709aa FB |
937 | #if 0 |
938 | printf("output:\n"); | |
939 | hex_dump(stdout, pkt, pkt_len); | |
940 | #endif | |
941 | slirp_fd_read(slirp_fd_opaque, pkt, pkt_len); | |
67b915a5 FB |
942 | } |
943 | ||
c20709aa FB |
944 | static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
945 | { | |
946 | #if 0 | |
947 | printf("input:\n"); | |
948 | hex_dump(stdout, buf, size); | |
949 | #endif | |
950 | slirp_input(buf, size); | |
951 | } | |
952 | ||
953 | static void slirp_add_read_packet(NetDriverState *nd, | |
954 | IOCanRWHandler *fd_can_read, | |
955 | IOReadHandler *fd_read, void *opaque) | |
956 | { | |
957 | slirp_fd_opaque = opaque; | |
958 | slirp_fd_can_read = fd_can_read; | |
959 | slirp_fd_read = fd_read; | |
960 | } | |
961 | ||
962 | static int net_slirp_init(NetDriverState *nd) | |
963 | { | |
964 | if (!slirp_inited) { | |
965 | slirp_inited = 1; | |
966 | slirp_init(); | |
967 | } | |
968 | nd->send_packet = slirp_send_packet; | |
969 | nd->add_read_packet = slirp_add_read_packet; | |
970 | pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp"); | |
971 | return 0; | |
972 | } | |
973 | ||
974 | #endif /* CONFIG_SLIRP */ | |
975 | ||
976 | #if !defined(_WIN32) | |
7d3505c5 FB |
977 | #ifdef _BSD |
978 | static int tun_open(char *ifname, int ifname_size) | |
979 | { | |
980 | int fd; | |
981 | char *dev; | |
982 | struct stat s; | |
67b915a5 | 983 | |
7d3505c5 FB |
984 | fd = open("/dev/tap", O_RDWR); |
985 | if (fd < 0) { | |
986 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); | |
987 | return -1; | |
988 | } | |
989 | ||
990 | fstat(fd, &s); | |
991 | dev = devname(s.st_rdev, S_IFCHR); | |
992 | pstrcpy(ifname, ifname_size, dev); | |
993 | ||
994 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
995 | return fd; | |
996 | } | |
997 | #else | |
c4b1fcc0 | 998 | static int tun_open(char *ifname, int ifname_size) |
330d0414 | 999 | { |
80cabfad | 1000 | struct ifreq ifr; |
c4b1fcc0 | 1001 | int fd, ret; |
80cabfad FB |
1002 | |
1003 | fd = open("/dev/net/tun", O_RDWR); | |
1004 | if (fd < 0) { | |
1005 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); | |
1006 | return -1; | |
330d0414 | 1007 | } |
80cabfad FB |
1008 | memset(&ifr, 0, sizeof(ifr)); |
1009 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; | |
1010 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d"); | |
1011 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); | |
1012 | if (ret != 0) { | |
1013 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); | |
1014 | close(fd); | |
1015 | return -1; | |
1016 | } | |
1017 | printf("Connected to host network interface: %s\n", ifr.ifr_name); | |
c4b1fcc0 | 1018 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
80cabfad | 1019 | fcntl(fd, F_SETFL, O_NONBLOCK); |
c4b1fcc0 FB |
1020 | return fd; |
1021 | } | |
7d3505c5 | 1022 | #endif |
330d0414 | 1023 | |
c20709aa FB |
1024 | static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
1025 | { | |
1026 | write(nd->fd, buf, size); | |
1027 | } | |
1028 | ||
1029 | static void tun_add_read_packet(NetDriverState *nd, | |
1030 | IOCanRWHandler *fd_can_read, | |
1031 | IOReadHandler *fd_read, void *opaque) | |
c4b1fcc0 | 1032 | { |
c20709aa FB |
1033 | qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque); |
1034 | } | |
1035 | ||
1036 | static int net_tun_init(NetDriverState *nd) | |
1037 | { | |
1038 | int pid, status; | |
1039 | char *args[3]; | |
c4b1fcc0 FB |
1040 | char **parg; |
1041 | ||
c20709aa FB |
1042 | nd->fd = tun_open(nd->ifname, sizeof(nd->ifname)); |
1043 | if (nd->fd < 0) | |
1044 | return -1; | |
c4b1fcc0 | 1045 | |
c20709aa FB |
1046 | /* try to launch network init script */ |
1047 | pid = fork(); | |
1048 | if (pid >= 0) { | |
1049 | if (pid == 0) { | |
1050 | parg = args; | |
1051 | *parg++ = network_script; | |
1052 | *parg++ = nd->ifname; | |
1053 | *parg++ = NULL; | |
1054 | execv(network_script, args); | |
1055 | exit(1); | |
1056 | } | |
1057 | while (waitpid(pid, &status, 0) != pid); | |
1058 | if (!WIFEXITED(status) || | |
1059 | WEXITSTATUS(status) != 0) { | |
1060 | fprintf(stderr, "%s: could not launch network script\n", | |
1061 | network_script); | |
80cabfad | 1062 | } |
330d0414 | 1063 | } |
c20709aa FB |
1064 | nd->send_packet = tun_send_packet; |
1065 | nd->add_read_packet = tun_add_read_packet; | |
80cabfad | 1066 | return 0; |
330d0414 FB |
1067 | } |
1068 | ||
c20709aa | 1069 | static int net_fd_init(NetDriverState *nd, int fd) |
330d0414 | 1070 | { |
c20709aa FB |
1071 | nd->fd = fd; |
1072 | nd->send_packet = tun_send_packet; | |
1073 | nd->add_read_packet = tun_add_read_packet; | |
1074 | pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd"); | |
1075 | return 0; | |
80cabfad | 1076 | } |
330d0414 | 1077 | |
c20709aa | 1078 | #endif /* !_WIN32 */ |
67b915a5 | 1079 | |
313aa567 FB |
1080 | /***********************************************************/ |
1081 | /* dumb display */ | |
1082 | ||
67b915a5 FB |
1083 | #ifdef _WIN32 |
1084 | ||
1085 | static void term_exit(void) | |
1086 | { | |
1087 | } | |
1088 | ||
1089 | static void term_init(void) | |
1090 | { | |
1091 | } | |
1092 | ||
1093 | #else | |
1094 | ||
313aa567 FB |
1095 | /* init terminal so that we can grab keys */ |
1096 | static struct termios oldtty; | |
1097 | ||
1098 | static void term_exit(void) | |
1099 | { | |
1100 | tcsetattr (0, TCSANOW, &oldtty); | |
1101 | } | |
1102 | ||
1103 | static void term_init(void) | |
1104 | { | |
1105 | struct termios tty; | |
1106 | ||
1107 | tcgetattr (0, &tty); | |
1108 | oldtty = tty; | |
1109 | ||
1110 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
1111 | |INLCR|IGNCR|ICRNL|IXON); | |
1112 | tty.c_oflag |= OPOST; | |
a20dd508 FB |
1113 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
1114 | /* if graphical mode, we allow Ctrl-C handling */ | |
1115 | if (nographic) | |
1116 | tty.c_lflag &= ~ISIG; | |
313aa567 FB |
1117 | tty.c_cflag &= ~(CSIZE|PARENB); |
1118 | tty.c_cflag |= CS8; | |
1119 | tty.c_cc[VMIN] = 1; | |
1120 | tty.c_cc[VTIME] = 0; | |
1121 | ||
1122 | tcsetattr (0, TCSANOW, &tty); | |
1123 | ||
1124 | atexit(term_exit); | |
1125 | ||
1126 | fcntl(0, F_SETFL, O_NONBLOCK); | |
1127 | } | |
1128 | ||
67b915a5 FB |
1129 | #endif |
1130 | ||
313aa567 FB |
1131 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
1132 | { | |
1133 | } | |
1134 | ||
1135 | static void dumb_resize(DisplayState *ds, int w, int h) | |
1136 | { | |
1137 | } | |
1138 | ||
1139 | static void dumb_refresh(DisplayState *ds) | |
1140 | { | |
1141 | vga_update_display(); | |
1142 | } | |
1143 | ||
1144 | void dumb_display_init(DisplayState *ds) | |
1145 | { | |
1146 | ds->data = NULL; | |
1147 | ds->linesize = 0; | |
1148 | ds->depth = 0; | |
1149 | ds->dpy_update = dumb_update; | |
1150 | ds->dpy_resize = dumb_resize; | |
1151 | ds->dpy_refresh = dumb_refresh; | |
1152 | } | |
1153 | ||
3a51dee6 | 1154 | #if !defined(CONFIG_SOFTMMU) |
f1510b2c | 1155 | /***********************************************************/ |
0824d6fc FB |
1156 | /* cpu signal handler */ |
1157 | static void host_segv_handler(int host_signum, siginfo_t *info, | |
1158 | void *puc) | |
1159 | { | |
1160 | if (cpu_signal_handler(host_signum, info, puc)) | |
1161 | return; | |
1162 | term_exit(); | |
1163 | abort(); | |
1164 | } | |
3a51dee6 | 1165 | #endif |
0824d6fc | 1166 | |
8a7ddc38 FB |
1167 | /***********************************************************/ |
1168 | /* I/O handling */ | |
0824d6fc | 1169 | |
c4b1fcc0 FB |
1170 | #define MAX_IO_HANDLERS 64 |
1171 | ||
1172 | typedef struct IOHandlerRecord { | |
1173 | int fd; | |
1174 | IOCanRWHandler *fd_can_read; | |
1175 | IOReadHandler *fd_read; | |
1176 | void *opaque; | |
1177 | /* temporary data */ | |
1178 | struct pollfd *ufd; | |
1179 | int max_size; | |
8a7ddc38 | 1180 | struct IOHandlerRecord *next; |
c4b1fcc0 FB |
1181 | } IOHandlerRecord; |
1182 | ||
8a7ddc38 | 1183 | static IOHandlerRecord *first_io_handler; |
c4b1fcc0 | 1184 | |
8a7ddc38 FB |
1185 | int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, |
1186 | IOReadHandler *fd_read, void *opaque) | |
c4b1fcc0 FB |
1187 | { |
1188 | IOHandlerRecord *ioh; | |
1189 | ||
8a7ddc38 FB |
1190 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
1191 | if (!ioh) | |
c4b1fcc0 | 1192 | return -1; |
c4b1fcc0 FB |
1193 | ioh->fd = fd; |
1194 | ioh->fd_can_read = fd_can_read; | |
1195 | ioh->fd_read = fd_read; | |
1196 | ioh->opaque = opaque; | |
8a7ddc38 FB |
1197 | ioh->next = first_io_handler; |
1198 | first_io_handler = ioh; | |
c4b1fcc0 FB |
1199 | return 0; |
1200 | } | |
1201 | ||
8a7ddc38 FB |
1202 | void qemu_del_fd_read_handler(int fd) |
1203 | { | |
1204 | IOHandlerRecord **pioh, *ioh; | |
b4608c04 | 1205 | |
8a7ddc38 FB |
1206 | pioh = &first_io_handler; |
1207 | for(;;) { | |
1208 | ioh = *pioh; | |
1209 | if (ioh == NULL) | |
1210 | break; | |
1211 | if (ioh->fd == fd) { | |
1212 | *pioh = ioh->next; | |
1213 | break; | |
1214 | } | |
1215 | pioh = &ioh->next; | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | /***********************************************************/ | |
1220 | /* savevm/loadvm support */ | |
1221 | ||
1222 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) | |
b4608c04 | 1223 | { |
8a7ddc38 | 1224 | fwrite(buf, 1, size, f); |
b4608c04 FB |
1225 | } |
1226 | ||
8a7ddc38 | 1227 | void qemu_put_byte(QEMUFile *f, int v) |
b4608c04 | 1228 | { |
8a7ddc38 FB |
1229 | fputc(v, f); |
1230 | } | |
1231 | ||
1232 | void qemu_put_be16(QEMUFile *f, unsigned int v) | |
1233 | { | |
1234 | qemu_put_byte(f, v >> 8); | |
1235 | qemu_put_byte(f, v); | |
1236 | } | |
1237 | ||
1238 | void qemu_put_be32(QEMUFile *f, unsigned int v) | |
1239 | { | |
1240 | qemu_put_byte(f, v >> 24); | |
1241 | qemu_put_byte(f, v >> 16); | |
1242 | qemu_put_byte(f, v >> 8); | |
1243 | qemu_put_byte(f, v); | |
1244 | } | |
1245 | ||
1246 | void qemu_put_be64(QEMUFile *f, uint64_t v) | |
1247 | { | |
1248 | qemu_put_be32(f, v >> 32); | |
1249 | qemu_put_be32(f, v); | |
1250 | } | |
1251 | ||
1252 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) | |
1253 | { | |
1254 | return fread(buf, 1, size, f); | |
1255 | } | |
1256 | ||
1257 | int qemu_get_byte(QEMUFile *f) | |
1258 | { | |
1259 | int v; | |
1260 | v = fgetc(f); | |
1261 | if (v == EOF) | |
1262 | return 0; | |
1263 | else | |
1264 | return v; | |
1265 | } | |
1266 | ||
1267 | unsigned int qemu_get_be16(QEMUFile *f) | |
1268 | { | |
1269 | unsigned int v; | |
1270 | v = qemu_get_byte(f) << 8; | |
1271 | v |= qemu_get_byte(f); | |
1272 | return v; | |
1273 | } | |
1274 | ||
1275 | unsigned int qemu_get_be32(QEMUFile *f) | |
1276 | { | |
1277 | unsigned int v; | |
1278 | v = qemu_get_byte(f) << 24; | |
1279 | v |= qemu_get_byte(f) << 16; | |
1280 | v |= qemu_get_byte(f) << 8; | |
1281 | v |= qemu_get_byte(f); | |
1282 | return v; | |
1283 | } | |
1284 | ||
1285 | uint64_t qemu_get_be64(QEMUFile *f) | |
1286 | { | |
1287 | uint64_t v; | |
1288 | v = (uint64_t)qemu_get_be32(f) << 32; | |
1289 | v |= qemu_get_be32(f); | |
1290 | return v; | |
1291 | } | |
1292 | ||
1293 | int64_t qemu_ftell(QEMUFile *f) | |
1294 | { | |
1295 | return ftell(f); | |
1296 | } | |
1297 | ||
1298 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) | |
1299 | { | |
1300 | if (fseek(f, pos, whence) < 0) | |
1301 | return -1; | |
1302 | return ftell(f); | |
1303 | } | |
1304 | ||
1305 | typedef struct SaveStateEntry { | |
1306 | char idstr[256]; | |
1307 | int instance_id; | |
1308 | int version_id; | |
1309 | SaveStateHandler *save_state; | |
1310 | LoadStateHandler *load_state; | |
1311 | void *opaque; | |
1312 | struct SaveStateEntry *next; | |
1313 | } SaveStateEntry; | |
b4608c04 | 1314 | |
8a7ddc38 FB |
1315 | static SaveStateEntry *first_se; |
1316 | ||
1317 | int register_savevm(const char *idstr, | |
1318 | int instance_id, | |
1319 | int version_id, | |
1320 | SaveStateHandler *save_state, | |
1321 | LoadStateHandler *load_state, | |
1322 | void *opaque) | |
1323 | { | |
1324 | SaveStateEntry *se, **pse; | |
1325 | ||
1326 | se = qemu_malloc(sizeof(SaveStateEntry)); | |
1327 | if (!se) | |
1328 | return -1; | |
1329 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); | |
1330 | se->instance_id = instance_id; | |
1331 | se->version_id = version_id; | |
1332 | se->save_state = save_state; | |
1333 | se->load_state = load_state; | |
1334 | se->opaque = opaque; | |
1335 | se->next = NULL; | |
1336 | ||
1337 | /* add at the end of list */ | |
1338 | pse = &first_se; | |
1339 | while (*pse != NULL) | |
1340 | pse = &(*pse)->next; | |
1341 | *pse = se; | |
1342 | return 0; | |
1343 | } | |
1344 | ||
1345 | #define QEMU_VM_FILE_MAGIC 0x5145564d | |
1346 | #define QEMU_VM_FILE_VERSION 0x00000001 | |
1347 | ||
1348 | int qemu_savevm(const char *filename) | |
1349 | { | |
1350 | SaveStateEntry *se; | |
1351 | QEMUFile *f; | |
1352 | int len, len_pos, cur_pos, saved_vm_running, ret; | |
1353 | ||
1354 | saved_vm_running = vm_running; | |
1355 | vm_stop(0); | |
1356 | ||
1357 | f = fopen(filename, "wb"); | |
1358 | if (!f) { | |
1359 | ret = -1; | |
1360 | goto the_end; | |
313aa567 FB |
1361 | } |
1362 | ||
8a7ddc38 FB |
1363 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
1364 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); | |
1365 | ||
1366 | for(se = first_se; se != NULL; se = se->next) { | |
1367 | /* ID string */ | |
1368 | len = strlen(se->idstr); | |
1369 | qemu_put_byte(f, len); | |
1370 | qemu_put_buffer(f, se->idstr, len); | |
1371 | ||
1372 | qemu_put_be32(f, se->instance_id); | |
1373 | qemu_put_be32(f, se->version_id); | |
1374 | ||
1375 | /* record size: filled later */ | |
1376 | len_pos = ftell(f); | |
1377 | qemu_put_be32(f, 0); | |
1378 | ||
1379 | se->save_state(f, se->opaque); | |
1380 | ||
1381 | /* fill record size */ | |
1382 | cur_pos = ftell(f); | |
1383 | len = ftell(f) - len_pos - 4; | |
1384 | fseek(f, len_pos, SEEK_SET); | |
1385 | qemu_put_be32(f, len); | |
1386 | fseek(f, cur_pos, SEEK_SET); | |
1387 | } | |
1388 | ||
1389 | fclose(f); | |
1390 | ret = 0; | |
1391 | the_end: | |
1392 | if (saved_vm_running) | |
1393 | vm_start(); | |
1394 | return ret; | |
1395 | } | |
1396 | ||
1397 | static SaveStateEntry *find_se(const char *idstr, int instance_id) | |
1398 | { | |
1399 | SaveStateEntry *se; | |
1400 | ||
1401 | for(se = first_se; se != NULL; se = se->next) { | |
1402 | if (!strcmp(se->idstr, idstr) && | |
1403 | instance_id == se->instance_id) | |
1404 | return se; | |
1405 | } | |
1406 | return NULL; | |
1407 | } | |
1408 | ||
1409 | int qemu_loadvm(const char *filename) | |
1410 | { | |
1411 | SaveStateEntry *se; | |
1412 | QEMUFile *f; | |
1413 | int len, cur_pos, ret, instance_id, record_len, version_id; | |
1414 | int saved_vm_running; | |
1415 | unsigned int v; | |
1416 | char idstr[256]; | |
1417 | ||
1418 | saved_vm_running = vm_running; | |
1419 | vm_stop(0); | |
1420 | ||
1421 | f = fopen(filename, "rb"); | |
1422 | if (!f) { | |
1423 | ret = -1; | |
1424 | goto the_end; | |
1425 | } | |
1426 | ||
1427 | v = qemu_get_be32(f); | |
1428 | if (v != QEMU_VM_FILE_MAGIC) | |
1429 | goto fail; | |
1430 | v = qemu_get_be32(f); | |
1431 | if (v != QEMU_VM_FILE_VERSION) { | |
1432 | fail: | |
1433 | fclose(f); | |
1434 | ret = -1; | |
1435 | goto the_end; | |
1436 | } | |
b4608c04 | 1437 | for(;;) { |
a541f297 | 1438 | #if defined (DO_TB_FLUSH) |
d927637d | 1439 | tb_flush(global_env); |
a541f297 | 1440 | #endif |
8a7ddc38 FB |
1441 | len = qemu_get_byte(f); |
1442 | if (feof(f)) | |
cd4c3e88 | 1443 | break; |
8a7ddc38 FB |
1444 | qemu_get_buffer(f, idstr, len); |
1445 | idstr[len] = '\0'; | |
1446 | instance_id = qemu_get_be32(f); | |
1447 | version_id = qemu_get_be32(f); | |
1448 | record_len = qemu_get_be32(f); | |
1449 | #if 0 | |
1450 | printf("idstr=%s instance=0x%x version=%d len=%d\n", | |
1451 | idstr, instance_id, version_id, record_len); | |
1452 | #endif | |
1453 | cur_pos = ftell(f); | |
1454 | se = find_se(idstr, instance_id); | |
1455 | if (!se) { | |
1456 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", | |
1457 | instance_id, idstr); | |
1458 | } else { | |
1459 | ret = se->load_state(f, se->opaque, version_id); | |
1460 | if (ret < 0) { | |
1461 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", | |
1462 | instance_id, idstr); | |
1463 | } | |
34865134 | 1464 | } |
8a7ddc38 FB |
1465 | /* always seek to exact end of record */ |
1466 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); | |
1467 | } | |
1468 | fclose(f); | |
1469 | ret = 0; | |
1470 | the_end: | |
1471 | if (saved_vm_running) | |
1472 | vm_start(); | |
1473 | return ret; | |
1474 | } | |
1475 | ||
1476 | /***********************************************************/ | |
1477 | /* cpu save/restore */ | |
1478 | ||
1479 | #if defined(TARGET_I386) | |
1480 | ||
1481 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) | |
1482 | { | |
1483 | qemu_put_be32(f, (uint32_t)dt->base); | |
1484 | qemu_put_be32(f, dt->limit); | |
1485 | qemu_put_be32(f, dt->flags); | |
1486 | } | |
1487 | ||
1488 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) | |
1489 | { | |
1490 | dt->base = (uint8_t *)qemu_get_be32(f); | |
1491 | dt->limit = qemu_get_be32(f); | |
1492 | dt->flags = qemu_get_be32(f); | |
1493 | } | |
1494 | ||
1495 | void cpu_save(QEMUFile *f, void *opaque) | |
1496 | { | |
1497 | CPUState *env = opaque; | |
1498 | uint16_t fptag, fpus, fpuc; | |
1499 | uint32_t hflags; | |
1500 | int i; | |
1501 | ||
1502 | for(i = 0; i < 8; i++) | |
1503 | qemu_put_be32s(f, &env->regs[i]); | |
1504 | qemu_put_be32s(f, &env->eip); | |
1505 | qemu_put_be32s(f, &env->eflags); | |
1506 | qemu_put_be32s(f, &env->eflags); | |
1507 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ | |
1508 | qemu_put_be32s(f, &hflags); | |
1509 | ||
1510 | /* FPU */ | |
1511 | fpuc = env->fpuc; | |
1512 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; | |
1513 | fptag = 0; | |
1514 | for (i=7; i>=0; i--) { | |
1515 | fptag <<= 2; | |
1516 | if (env->fptags[i]) { | |
1517 | fptag |= 3; | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | qemu_put_be16s(f, &fpuc); | |
1522 | qemu_put_be16s(f, &fpus); | |
1523 | qemu_put_be16s(f, &fptag); | |
1524 | ||
1525 | for(i = 0; i < 8; i++) { | |
1526 | uint64_t mant; | |
1527 | uint16_t exp; | |
1528 | cpu_get_fp80(&mant, &exp, env->fpregs[i]); | |
1529 | qemu_put_be64(f, mant); | |
1530 | qemu_put_be16(f, exp); | |
1531 | } | |
1532 | ||
1533 | for(i = 0; i < 6; i++) | |
1534 | cpu_put_seg(f, &env->segs[i]); | |
1535 | cpu_put_seg(f, &env->ldt); | |
1536 | cpu_put_seg(f, &env->tr); | |
1537 | cpu_put_seg(f, &env->gdt); | |
1538 | cpu_put_seg(f, &env->idt); | |
1539 | ||
1540 | qemu_put_be32s(f, &env->sysenter_cs); | |
1541 | qemu_put_be32s(f, &env->sysenter_esp); | |
1542 | qemu_put_be32s(f, &env->sysenter_eip); | |
1543 | ||
1544 | qemu_put_be32s(f, &env->cr[0]); | |
1545 | qemu_put_be32s(f, &env->cr[2]); | |
1546 | qemu_put_be32s(f, &env->cr[3]); | |
1547 | qemu_put_be32s(f, &env->cr[4]); | |
1548 | ||
1549 | for(i = 0; i < 8; i++) | |
1550 | qemu_put_be32s(f, &env->dr[i]); | |
1551 | ||
1552 | /* MMU */ | |
1553 | qemu_put_be32s(f, &env->a20_mask); | |
1554 | } | |
1555 | ||
1556 | int cpu_load(QEMUFile *f, void *opaque, int version_id) | |
1557 | { | |
1558 | CPUState *env = opaque; | |
1559 | int i; | |
1560 | uint32_t hflags; | |
1561 | uint16_t fpus, fpuc, fptag; | |
1562 | ||
1563 | if (version_id != 1) | |
1564 | return -EINVAL; | |
1565 | for(i = 0; i < 8; i++) | |
1566 | qemu_get_be32s(f, &env->regs[i]); | |
1567 | qemu_get_be32s(f, &env->eip); | |
1568 | qemu_get_be32s(f, &env->eflags); | |
1569 | qemu_get_be32s(f, &env->eflags); | |
1570 | qemu_get_be32s(f, &hflags); | |
1571 | ||
1572 | qemu_get_be16s(f, &fpuc); | |
1573 | qemu_get_be16s(f, &fpus); | |
1574 | qemu_get_be16s(f, &fptag); | |
1575 | ||
1576 | for(i = 0; i < 8; i++) { | |
1577 | uint64_t mant; | |
1578 | uint16_t exp; | |
1579 | mant = qemu_get_be64(f); | |
1580 | exp = qemu_get_be16(f); | |
1581 | env->fpregs[i] = cpu_set_fp80(mant, exp); | |
1582 | } | |
1583 | ||
1584 | env->fpuc = fpuc; | |
1585 | env->fpstt = (fpus >> 11) & 7; | |
1586 | env->fpus = fpus & ~0x3800; | |
1587 | for(i = 0; i < 8; i++) { | |
1588 | env->fptags[i] = ((fptag & 3) == 3); | |
1589 | fptag >>= 2; | |
1590 | } | |
1591 | ||
1592 | for(i = 0; i < 6; i++) | |
1593 | cpu_get_seg(f, &env->segs[i]); | |
1594 | cpu_get_seg(f, &env->ldt); | |
1595 | cpu_get_seg(f, &env->tr); | |
1596 | cpu_get_seg(f, &env->gdt); | |
1597 | cpu_get_seg(f, &env->idt); | |
1598 | ||
1599 | qemu_get_be32s(f, &env->sysenter_cs); | |
1600 | qemu_get_be32s(f, &env->sysenter_esp); | |
1601 | qemu_get_be32s(f, &env->sysenter_eip); | |
1602 | ||
1603 | qemu_get_be32s(f, &env->cr[0]); | |
1604 | qemu_get_be32s(f, &env->cr[2]); | |
1605 | qemu_get_be32s(f, &env->cr[3]); | |
1606 | qemu_get_be32s(f, &env->cr[4]); | |
1607 | ||
1608 | for(i = 0; i < 8; i++) | |
1609 | qemu_get_be32s(f, &env->dr[i]); | |
1610 | ||
1611 | /* MMU */ | |
1612 | qemu_get_be32s(f, &env->a20_mask); | |
1613 | ||
1614 | /* XXX: compute hflags from scratch, except for CPL and IIF */ | |
1615 | env->hflags = hflags; | |
1616 | tlb_flush(env, 1); | |
1617 | return 0; | |
1618 | } | |
1619 | ||
a541f297 FB |
1620 | #elif defined(TARGET_PPC) |
1621 | void cpu_save(QEMUFile *f, void *opaque) | |
1622 | { | |
1623 | } | |
1624 | ||
1625 | int cpu_load(QEMUFile *f, void *opaque, int version_id) | |
1626 | { | |
1627 | return 0; | |
1628 | } | |
8a7ddc38 FB |
1629 | #else |
1630 | ||
1631 | #warning No CPU save/restore functions | |
1632 | ||
1633 | #endif | |
1634 | ||
1635 | /***********************************************************/ | |
1636 | /* ram save/restore */ | |
1637 | ||
1638 | /* we just avoid storing empty pages */ | |
1639 | static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len) | |
1640 | { | |
1641 | int i, v; | |
1642 | ||
1643 | v = buf[0]; | |
1644 | for(i = 1; i < len; i++) { | |
1645 | if (buf[i] != v) | |
1646 | goto normal_save; | |
1647 | } | |
1648 | qemu_put_byte(f, 1); | |
1649 | qemu_put_byte(f, v); | |
1650 | return; | |
1651 | normal_save: | |
1652 | qemu_put_byte(f, 0); | |
1653 | qemu_put_buffer(f, buf, len); | |
1654 | } | |
1655 | ||
1656 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) | |
1657 | { | |
1658 | int v; | |
1659 | ||
1660 | v = qemu_get_byte(f); | |
1661 | switch(v) { | |
1662 | case 0: | |
1663 | if (qemu_get_buffer(f, buf, len) != len) | |
1664 | return -EIO; | |
1665 | break; | |
1666 | case 1: | |
1667 | v = qemu_get_byte(f); | |
1668 | memset(buf, v, len); | |
1669 | break; | |
1670 | default: | |
1671 | return -EINVAL; | |
1672 | } | |
1673 | return 0; | |
1674 | } | |
1675 | ||
1676 | static void ram_save(QEMUFile *f, void *opaque) | |
1677 | { | |
1678 | int i; | |
1679 | qemu_put_be32(f, phys_ram_size); | |
1680 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { | |
1681 | ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); | |
1682 | } | |
1683 | } | |
1684 | ||
1685 | static int ram_load(QEMUFile *f, void *opaque, int version_id) | |
1686 | { | |
1687 | int i, ret; | |
1688 | ||
1689 | if (version_id != 1) | |
1690 | return -EINVAL; | |
1691 | if (qemu_get_be32(f) != phys_ram_size) | |
1692 | return -EINVAL; | |
1693 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { | |
1694 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); | |
1695 | if (ret) | |
1696 | return ret; | |
1697 | } | |
1698 | return 0; | |
1699 | } | |
1700 | ||
1701 | /***********************************************************/ | |
1702 | /* main execution loop */ | |
1703 | ||
1704 | void gui_update(void *opaque) | |
1705 | { | |
1706 | display_state.dpy_refresh(&display_state); | |
1707 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); | |
1708 | } | |
1709 | ||
1710 | /* XXX: support several handlers */ | |
1711 | VMStopHandler *vm_stop_cb; | |
1712 | VMStopHandler *vm_stop_opaque; | |
1713 | ||
1714 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) | |
1715 | { | |
1716 | vm_stop_cb = cb; | |
1717 | vm_stop_opaque = opaque; | |
1718 | return 0; | |
1719 | } | |
1720 | ||
1721 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) | |
1722 | { | |
1723 | vm_stop_cb = NULL; | |
1724 | } | |
1725 | ||
1726 | void vm_start(void) | |
1727 | { | |
1728 | if (!vm_running) { | |
1729 | cpu_enable_ticks(); | |
1730 | vm_running = 1; | |
1731 | } | |
1732 | } | |
1733 | ||
1734 | void vm_stop(int reason) | |
1735 | { | |
1736 | if (vm_running) { | |
1737 | cpu_disable_ticks(); | |
1738 | vm_running = 0; | |
1739 | if (reason != 0) { | |
1740 | if (vm_stop_cb) { | |
1741 | vm_stop_cb(vm_stop_opaque, reason); | |
1742 | } | |
34865134 | 1743 | } |
8a7ddc38 FB |
1744 | } |
1745 | } | |
1746 | ||
1747 | int main_loop(void) | |
1748 | { | |
67b915a5 | 1749 | #ifndef _WIN32 |
8a7ddc38 | 1750 | struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf; |
8a7ddc38 | 1751 | IOHandlerRecord *ioh, *ioh_next; |
67b915a5 FB |
1752 | uint8_t buf[4096]; |
1753 | int n, max_size; | |
1754 | #endif | |
1755 | int ret, timeout; | |
8a7ddc38 FB |
1756 | CPUState *env = global_env; |
1757 | ||
1758 | for(;;) { | |
1759 | if (vm_running) { | |
1760 | ret = cpu_exec(env); | |
1761 | if (reset_requested) { | |
1762 | ret = EXCP_INTERRUPT; | |
1763 | break; | |
1764 | } | |
1765 | if (ret == EXCP_DEBUG) { | |
1766 | vm_stop(EXCP_DEBUG); | |
1767 | } | |
1768 | /* if hlt instruction, we wait until the next IRQ */ | |
1769 | /* XXX: use timeout computed from timers */ | |
1770 | if (ret == EXCP_HLT) | |
1771 | timeout = 10; | |
1772 | else | |
1773 | timeout = 0; | |
1774 | } else { | |
b4608c04 | 1775 | timeout = 10; |
8a7ddc38 | 1776 | } |
c4b1fcc0 | 1777 | |
38e205a2 FB |
1778 | #ifdef _WIN32 |
1779 | if (timeout > 0) | |
1780 | Sleep(timeout); | |
1781 | #else | |
1782 | ||
b4608c04 | 1783 | /* poll any events */ |
8a7ddc38 | 1784 | /* XXX: separate device handlers from system ones */ |
b4608c04 | 1785 | pf = ufds; |
8a7ddc38 FB |
1786 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
1787 | if (!ioh->fd_can_read) { | |
1788 | max_size = 0; | |
c4b1fcc0 FB |
1789 | pf->fd = ioh->fd; |
1790 | pf->events = POLLIN; | |
1791 | ioh->ufd = pf; | |
1792 | pf++; | |
1793 | } else { | |
8a7ddc38 FB |
1794 | max_size = ioh->fd_can_read(ioh->opaque); |
1795 | if (max_size > 0) { | |
1796 | if (max_size > sizeof(buf)) | |
1797 | max_size = sizeof(buf); | |
1798 | pf->fd = ioh->fd; | |
1799 | pf->events = POLLIN; | |
1800 | ioh->ufd = pf; | |
1801 | pf++; | |
1802 | } else { | |
1803 | ioh->ufd = NULL; | |
1804 | } | |
c4b1fcc0 FB |
1805 | } |
1806 | ioh->max_size = max_size; | |
b4608c04 | 1807 | } |
73332e5c | 1808 | |
b4608c04 FB |
1809 | ret = poll(ufds, pf - ufds, timeout); |
1810 | if (ret > 0) { | |
8a7ddc38 FB |
1811 | /* XXX: better handling of removal */ |
1812 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { | |
1813 | ioh_next = ioh->next; | |
c4b1fcc0 FB |
1814 | pf = ioh->ufd; |
1815 | if (pf) { | |
8a7ddc38 FB |
1816 | if (pf->revents & POLLIN) { |
1817 | if (ioh->max_size == 0) { | |
1818 | /* just a read event */ | |
1819 | ioh->fd_read(ioh->opaque, NULL, 0); | |
1820 | } else { | |
1821 | n = read(ioh->fd, buf, ioh->max_size); | |
1822 | if (n >= 0) { | |
1823 | ioh->fd_read(ioh->opaque, buf, n); | |
1824 | } else if (errno != -EAGAIN) { | |
1825 | ioh->fd_read(ioh->opaque, NULL, -errno); | |
1826 | } | |
1827 | } | |
b4608c04 | 1828 | } |
b4608c04 | 1829 | } |
b4608c04 FB |
1830 | } |
1831 | } | |
c20709aa FB |
1832 | |
1833 | #if defined(CONFIG_SLIRP) | |
1834 | /* XXX: merge with poll() */ | |
1835 | if (slirp_inited) { | |
1836 | fd_set rfds, wfds, xfds; | |
1837 | int nfds; | |
1838 | struct timeval tv; | |
1839 | ||
1840 | nfds = -1; | |
1841 | FD_ZERO(&rfds); | |
1842 | FD_ZERO(&wfds); | |
1843 | FD_ZERO(&xfds); | |
1844 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); | |
1845 | tv.tv_sec = 0; | |
1846 | tv.tv_usec = 0; | |
1847 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); | |
1848 | if (ret >= 0) { | |
1849 | slirp_select_poll(&rfds, &wfds, &xfds); | |
1850 | } | |
1851 | } | |
1852 | #endif | |
1853 | ||
67b915a5 | 1854 | #endif |
b4608c04 | 1855 | |
8a7ddc38 FB |
1856 | if (vm_running) { |
1857 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], | |
1858 | qemu_get_clock(vm_clock)); | |
1859 | ||
aaaa7df6 FB |
1860 | if (audio_enabled) { |
1861 | /* XXX: add explicit timer */ | |
1862 | SB16_run(); | |
1863 | } | |
8a7ddc38 FB |
1864 | |
1865 | /* run dma transfers, if any */ | |
1866 | DMA_run(); | |
b4608c04 | 1867 | } |
313aa567 | 1868 | |
8a7ddc38 FB |
1869 | /* real time timers */ |
1870 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], | |
1871 | qemu_get_clock(rt_clock)); | |
b4608c04 | 1872 | } |
34865134 FB |
1873 | cpu_disable_ticks(); |
1874 | return ret; | |
b4608c04 FB |
1875 | } |
1876 | ||
0824d6fc FB |
1877 | void help(void) |
1878 | { | |
aaaa7df6 | 1879 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n" |
0db63474 | 1880 | "usage: %s [options] [disk_image]\n" |
0824d6fc | 1881 | "\n" |
a20dd508 | 1882 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" |
fc01f7e7 | 1883 | "\n" |
a20dd508 | 1884 | "Standard options:\n" |
c45886db | 1885 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
36b486bb FB |
1886 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
1887 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" | |
c4b1fcc0 | 1888 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
6e44ba7f | 1889 | "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n" |
a20dd508 FB |
1890 | "-snapshot write to temporary files instead of disk image files\n" |
1891 | "-m megs set virtual RAM size to megs MB\n" | |
c4b1fcc0 | 1892 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
aaaa7df6 | 1893 | "-enable-audio enable audio support\n" |
c4b1fcc0 FB |
1894 | "\n" |
1895 | "Network options:\n" | |
c20709aa | 1896 | "-nics n simulate 'n' network cards [default=1]\n" |
702c651c | 1897 | "-macaddr addr set the mac address of the first interface\n" |
c20709aa FB |
1898 | "-n script set tap/tun network init script [default=%s]\n" |
1899 | "-tun-fd fd use this fd as already opened tap/tun interface\n" | |
1900 | #ifdef CONFIG_SLIRP | |
1901 | "-user-net use user mode network stack [default if no tap/tun script]\n" | |
1902 | #endif | |
1903 | "-dummy-net use dummy network stack\n" | |
a20dd508 | 1904 | "\n" |
c4b1fcc0 | 1905 | "Linux boot specific:\n" |
a20dd508 FB |
1906 | "-kernel bzImage use 'bzImage' as kernel image\n" |
1907 | "-append cmdline use 'cmdline' as kernel command line\n" | |
1908 | "-initrd file use 'file' as initial ram disk\n" | |
fc01f7e7 | 1909 | "\n" |
330d0414 | 1910 | "Debug/Expert options:\n" |
a20dd508 FB |
1911 | "-s wait gdb connection to port %d\n" |
1912 | "-p port change gdb connection port\n" | |
f193c797 | 1913 | "-d item1,... output log to %s (use -d ? for a list of log items)\n" |
a20dd508 FB |
1914 | "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n" |
1915 | "-L path set the directory for the BIOS and VGA BIOS\n" | |
77fef8c1 FB |
1916 | #ifdef USE_CODE_COPY |
1917 | "-no-code-copy disable code copy acceleration\n" | |
1918 | #endif | |
1919 | ||
0824d6fc | 1920 | "\n" |
f1510b2c | 1921 | "During emulation, use C-a h to get terminal commands:\n", |
0db63474 FB |
1922 | #ifdef CONFIG_SOFTMMU |
1923 | "qemu", | |
1924 | #else | |
1925 | "qemu-fast", | |
1926 | #endif | |
1927 | DEFAULT_NETWORK_SCRIPT, | |
6e44ba7f FB |
1928 | DEFAULT_GDBSTUB_PORT, |
1929 | "/tmp/qemu.log"); | |
0824d6fc | 1930 | term_print_help(); |
0db63474 FB |
1931 | #ifndef CONFIG_SOFTMMU |
1932 | printf("\n" | |
1933 | "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n" | |
1934 | "work. Please use the 'qemu' executable to have a more accurate (but slower)\n" | |
1935 | "PC emulation.\n"); | |
1936 | #endif | |
0824d6fc FB |
1937 | exit(1); |
1938 | } | |
1939 | ||
fc01f7e7 FB |
1940 | struct option long_options[] = { |
1941 | { "initrd", 1, NULL, 0, }, | |
1942 | { "hda", 1, NULL, 0, }, | |
1943 | { "hdb", 1, NULL, 0, }, | |
33e3963e | 1944 | { "snapshot", 0, NULL, 0, }, |
330d0414 | 1945 | { "hdachs", 1, NULL, 0, }, |
a20dd508 FB |
1946 | { "nographic", 0, NULL, 0, }, |
1947 | { "kernel", 1, NULL, 0, }, | |
1948 | { "append", 1, NULL, 0, }, | |
42f1e0e4 | 1949 | { "tun-fd", 1, NULL, 0, }, |
36b486bb FB |
1950 | { "hdc", 1, NULL, 0, }, |
1951 | { "hdd", 1, NULL, 0, }, | |
1952 | { "cdrom", 1, NULL, 0, }, | |
1953 | { "boot", 1, NULL, 0, }, | |
c45886db FB |
1954 | { "fda", 1, NULL, 0, }, |
1955 | { "fdb", 1, NULL, 0, }, | |
c4b1fcc0 FB |
1956 | { "no-code-copy", 0, NULL, 0 }, |
1957 | { "nics", 1, NULL, 0 }, | |
702c651c | 1958 | { "macaddr", 1, NULL, 0 }, |
f6bac380 FB |
1959 | { "user-net", 0, NULL, 0 }, |
1960 | { "dummy-net", 0, NULL, 0 }, | |
aaaa7df6 | 1961 | { "enable-audio", 0, NULL, 0 }, |
fc01f7e7 FB |
1962 | { NULL, 0, NULL, 0 }, |
1963 | }; | |
1964 | ||
77fef8c1 FB |
1965 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
1966 | ||
1967 | /* this stack is only used during signal handling */ | |
1968 | #define SIGNAL_STACK_SIZE 32768 | |
1969 | ||
1970 | static uint8_t *signal_stack; | |
1971 | ||
1972 | #endif | |
1973 | ||
c20709aa FB |
1974 | #define NET_IF_TUN 0 |
1975 | #define NET_IF_USER 1 | |
1976 | #define NET_IF_DUMMY 2 | |
1977 | ||
0824d6fc FB |
1978 | int main(int argc, char **argv) |
1979 | { | |
67b915a5 FB |
1980 | #ifdef CONFIG_GDBSTUB |
1981 | int use_gdbstub, gdbstub_port; | |
1982 | #endif | |
1983 | int c, i, long_index, has_cdrom; | |
1ccde1cb | 1984 | int snapshot, linux_boot; |
c45886db | 1985 | CPUState *env; |
7f7f9873 | 1986 | const char *initrd_filename; |
c45886db | 1987 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
a20dd508 | 1988 | const char *kernel_filename, *kernel_cmdline; |
313aa567 | 1989 | DisplayState *ds = &display_state; |
c4b1fcc0 | 1990 | int cyls, heads, secs; |
a541f297 | 1991 | int start_emulation = 1; |
702c651c | 1992 | uint8_t macaddr[6]; |
c20709aa FB |
1993 | int net_if_type, nb_tun_fds, tun_fds[MAX_NICS]; |
1994 | ||
67b915a5 | 1995 | #if !defined(CONFIG_SOFTMMU) |
0824d6fc FB |
1996 | /* we never want that malloc() uses mmap() */ |
1997 | mallopt(M_MMAP_THRESHOLD, 4096 * 1024); | |
67b915a5 | 1998 | #endif |
fc01f7e7 | 1999 | initrd_filename = NULL; |
c45886db FB |
2000 | for(i = 0; i < MAX_FD; i++) |
2001 | fd_filename[i] = NULL; | |
fc01f7e7 FB |
2002 | for(i = 0; i < MAX_DISKS; i++) |
2003 | hd_filename[i] = NULL; | |
1ccde1cb | 2004 | ram_size = 32 * 1024 * 1024; |
313aa567 | 2005 | vga_ram_size = VGA_RAM_SIZE; |
f1510b2c | 2006 | pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT); |
67b915a5 | 2007 | #ifdef CONFIG_GDBSTUB |
b4608c04 FB |
2008 | use_gdbstub = 0; |
2009 | gdbstub_port = DEFAULT_GDBSTUB_PORT; | |
67b915a5 | 2010 | #endif |
33e3963e | 2011 | snapshot = 0; |
a20dd508 FB |
2012 | nographic = 0; |
2013 | kernel_filename = NULL; | |
2014 | kernel_cmdline = ""; | |
c4b1fcc0 FB |
2015 | has_cdrom = 1; |
2016 | cyls = heads = secs = 0; | |
2017 | ||
c20709aa FB |
2018 | nb_tun_fds = 0; |
2019 | net_if_type = -1; | |
c4b1fcc0 | 2020 | nb_nics = 1; |
702c651c FB |
2021 | /* default mac address of the first network interface */ |
2022 | macaddr[0] = 0x52; | |
2023 | macaddr[1] = 0x54; | |
2024 | macaddr[2] = 0x00; | |
2025 | macaddr[3] = 0x12; | |
2026 | macaddr[4] = 0x34; | |
2027 | macaddr[5] = 0x56; | |
c20709aa FB |
2028 | |
2029 | ||
0824d6fc | 2030 | for(;;) { |
a541f297 | 2031 | c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index); |
0824d6fc FB |
2032 | if (c == -1) |
2033 | break; | |
2034 | switch(c) { | |
fc01f7e7 FB |
2035 | case 0: |
2036 | switch(long_index) { | |
2037 | case 0: | |
2038 | initrd_filename = optarg; | |
2039 | break; | |
2040 | case 1: | |
2041 | hd_filename[0] = optarg; | |
2042 | break; | |
2043 | case 2: | |
2044 | hd_filename[1] = optarg; | |
2045 | break; | |
33e3963e FB |
2046 | case 3: |
2047 | snapshot = 1; | |
2048 | break; | |
330d0414 FB |
2049 | case 4: |
2050 | { | |
330d0414 FB |
2051 | const char *p; |
2052 | p = optarg; | |
2053 | cyls = strtol(p, (char **)&p, 0); | |
2054 | if (*p != ',') | |
2055 | goto chs_fail; | |
2056 | p++; | |
2057 | heads = strtol(p, (char **)&p, 0); | |
2058 | if (*p != ',') | |
2059 | goto chs_fail; | |
2060 | p++; | |
2061 | secs = strtol(p, (char **)&p, 0); | |
c4b1fcc0 FB |
2062 | if (*p != '\0') { |
2063 | chs_fail: | |
2064 | cyls = 0; | |
2065 | } | |
330d0414 FB |
2066 | } |
2067 | break; | |
313aa567 | 2068 | case 5: |
a20dd508 FB |
2069 | nographic = 1; |
2070 | break; | |
2071 | case 6: | |
2072 | kernel_filename = optarg; | |
2073 | break; | |
2074 | case 7: | |
2075 | kernel_cmdline = optarg; | |
313aa567 | 2076 | break; |
42f1e0e4 | 2077 | case 8: |
c4b1fcc0 FB |
2078 | { |
2079 | const char *p; | |
2080 | int fd; | |
d6b86f4d | 2081 | net_if_type = NET_IF_TUN; |
c20709aa FB |
2082 | if (nb_tun_fds < MAX_NICS) { |
2083 | fd = strtol(optarg, (char **)&p, 0); | |
2084 | if (*p != '\0') { | |
2085 | fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds); | |
c4b1fcc0 FB |
2086 | exit(1); |
2087 | } | |
c20709aa | 2088 | tun_fds[nb_tun_fds++] = fd; |
c4b1fcc0 FB |
2089 | } |
2090 | } | |
42f1e0e4 | 2091 | break; |
36b486bb FB |
2092 | case 9: |
2093 | hd_filename[2] = optarg; | |
c4b1fcc0 | 2094 | has_cdrom = 0; |
36b486bb FB |
2095 | break; |
2096 | case 10: | |
2097 | hd_filename[3] = optarg; | |
2098 | break; | |
2099 | case 11: | |
2100 | hd_filename[2] = optarg; | |
c4b1fcc0 | 2101 | has_cdrom = 1; |
36b486bb FB |
2102 | break; |
2103 | case 12: | |
2104 | boot_device = optarg[0]; | |
c45886db FB |
2105 | if (boot_device != 'a' && boot_device != 'b' && |
2106 | boot_device != 'c' && boot_device != 'd') { | |
36b486bb FB |
2107 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
2108 | exit(1); | |
2109 | } | |
2110 | break; | |
c45886db FB |
2111 | case 13: |
2112 | fd_filename[0] = optarg; | |
2113 | break; | |
2114 | case 14: | |
2115 | fd_filename[1] = optarg; | |
2116 | break; | |
77fef8c1 FB |
2117 | case 15: |
2118 | code_copy_enabled = 0; | |
2119 | break; | |
c4b1fcc0 FB |
2120 | case 16: |
2121 | nb_nics = atoi(optarg); | |
3a1bc175 | 2122 | if (nb_nics < 0 || nb_nics > MAX_NICS) { |
c4b1fcc0 FB |
2123 | fprintf(stderr, "qemu: invalid number of network interfaces\n"); |
2124 | exit(1); | |
2125 | } | |
2126 | break; | |
702c651c FB |
2127 | case 17: |
2128 | { | |
2129 | const char *p; | |
2130 | int i; | |
2131 | p = optarg; | |
2132 | for(i = 0; i < 6; i++) { | |
2133 | macaddr[i] = strtol(p, (char **)&p, 16); | |
2134 | if (i == 5) { | |
2135 | if (*p != '\0') | |
2136 | goto macaddr_error; | |
2137 | } else { | |
2138 | if (*p != ':') { | |
2139 | macaddr_error: | |
2140 | fprintf(stderr, "qemu: invalid syntax for ethernet address\n"); | |
2141 | exit(1); | |
2142 | } | |
2143 | p++; | |
2144 | } | |
2145 | } | |
2146 | } | |
2147 | break; | |
c20709aa FB |
2148 | case 18: |
2149 | net_if_type = NET_IF_USER; | |
2150 | break; | |
2151 | case 19: | |
2152 | net_if_type = NET_IF_DUMMY; | |
2153 | break; | |
aaaa7df6 FB |
2154 | case 20: |
2155 | audio_enabled = 1; | |
2156 | break; | |
fc01f7e7 FB |
2157 | } |
2158 | break; | |
0824d6fc FB |
2159 | case 'h': |
2160 | help(); | |
2161 | break; | |
2162 | case 'm': | |
1ccde1cb FB |
2163 | ram_size = atoi(optarg) * 1024 * 1024; |
2164 | if (ram_size <= 0) | |
0824d6fc | 2165 | help(); |
1ccde1cb | 2166 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
36b486bb | 2167 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
7916e224 FB |
2168 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
2169 | exit(1); | |
2170 | } | |
0824d6fc FB |
2171 | break; |
2172 | case 'd': | |
f193c797 FB |
2173 | { |
2174 | int mask; | |
2175 | CPULogItem *item; | |
2176 | ||
2177 | mask = cpu_str_to_log_mask(optarg); | |
2178 | if (!mask) { | |
2179 | printf("Log items (comma separated):\n"); | |
2180 | for(item = cpu_log_items; item->mask != 0; item++) { | |
2181 | printf("%-10s %s\n", item->name, item->help); | |
2182 | } | |
2183 | exit(1); | |
2184 | } | |
2185 | cpu_set_log(mask); | |
2186 | } | |
0824d6fc | 2187 | break; |
f1510b2c FB |
2188 | case 'n': |
2189 | pstrcpy(network_script, sizeof(network_script), optarg); | |
2190 | break; | |
67b915a5 | 2191 | #ifdef CONFIG_GDBSTUB |
b4608c04 FB |
2192 | case 's': |
2193 | use_gdbstub = 1; | |
2194 | break; | |
2195 | case 'p': | |
2196 | gdbstub_port = atoi(optarg); | |
2197 | break; | |
67b915a5 | 2198 | #endif |
330d0414 | 2199 | case 'L': |
5a67135a | 2200 | bios_dir = optarg; |
330d0414 | 2201 | break; |
a541f297 FB |
2202 | case 'S': |
2203 | start_emulation = 0; | |
2204 | break; | |
0824d6fc FB |
2205 | } |
2206 | } | |
330d0414 | 2207 | |
a20dd508 FB |
2208 | if (optind < argc) { |
2209 | hd_filename[0] = argv[optind++]; | |
2210 | } | |
2211 | ||
2212 | linux_boot = (kernel_filename != NULL); | |
330d0414 | 2213 | |
c45886db FB |
2214 | if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' && |
2215 | fd_filename[0] == '\0') | |
0824d6fc | 2216 | help(); |
8f2b1fb0 FB |
2217 | |
2218 | /* boot to cd by default if no hard disk */ | |
d0309311 FB |
2219 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
2220 | if (fd_filename[0] != '\0') | |
2221 | boot_device = 'a'; | |
2222 | else | |
2223 | boot_device = 'd'; | |
2224 | } | |
0824d6fc | 2225 | |
dc887a4d FB |
2226 | #if !defined(CONFIG_SOFTMMU) |
2227 | /* must avoid mmap() usage of glibc by setting a buffer "by hand" */ | |
2228 | { | |
2229 | static uint8_t stdout_buf[4096]; | |
2230 | setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf)); | |
2231 | } | |
2232 | #else | |
b118d61e | 2233 | setvbuf(stdout, NULL, _IOLBF, 0); |
dc887a4d | 2234 | #endif |
0824d6fc | 2235 | |
c4b1fcc0 | 2236 | /* init host network redirectors */ |
c20709aa FB |
2237 | if (net_if_type == -1) { |
2238 | net_if_type = NET_IF_TUN; | |
2239 | #if defined(CONFIG_SLIRP) | |
2240 | if (access(network_script, R_OK) < 0) { | |
2241 | net_if_type = NET_IF_USER; | |
2242 | } | |
2243 | #endif | |
2244 | } | |
2245 | ||
2246 | for(i = 0; i < nb_nics; i++) { | |
702c651c | 2247 | NetDriverState *nd = &nd_table[i]; |
c20709aa | 2248 | nd->index = i; |
702c651c FB |
2249 | /* init virtual mac address */ |
2250 | nd->macaddr[0] = macaddr[0]; | |
2251 | nd->macaddr[1] = macaddr[1]; | |
2252 | nd->macaddr[2] = macaddr[2]; | |
2253 | nd->macaddr[3] = macaddr[3]; | |
2254 | nd->macaddr[4] = macaddr[4]; | |
2255 | nd->macaddr[5] = macaddr[5] + i; | |
c20709aa FB |
2256 | switch(net_if_type) { |
2257 | #if defined(CONFIG_SLIRP) | |
2258 | case NET_IF_USER: | |
2259 | net_slirp_init(nd); | |
2260 | break; | |
2261 | #endif | |
2262 | #if !defined(_WIN32) | |
2263 | case NET_IF_TUN: | |
2264 | if (i < nb_tun_fds) { | |
2265 | net_fd_init(nd, tun_fds[i]); | |
2266 | } else { | |
d927637d FB |
2267 | if (net_tun_init(nd) < 0) |
2268 | net_dummy_init(nd); | |
c20709aa FB |
2269 | } |
2270 | break; | |
2271 | #endif | |
2272 | case NET_IF_DUMMY: | |
2273 | default: | |
2274 | net_dummy_init(nd); | |
2275 | break; | |
2276 | } | |
702c651c | 2277 | } |
f1510b2c | 2278 | |
0824d6fc | 2279 | /* init the memory */ |
1ccde1cb | 2280 | phys_ram_size = ram_size + vga_ram_size; |
7f7f9873 FB |
2281 | |
2282 | #ifdef CONFIG_SOFTMMU | |
7d3505c5 FB |
2283 | #ifdef _BSD |
2284 | /* mallocs are always aligned on BSD. */ | |
2285 | phys_ram_base = malloc(phys_ram_size); | |
2286 | #else | |
1ccde1cb | 2287 | phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size); |
7d3505c5 | 2288 | #endif |
7f7f9873 FB |
2289 | if (!phys_ram_base) { |
2290 | fprintf(stderr, "Could not allocate physical memory\n"); | |
0824d6fc FB |
2291 | exit(1); |
2292 | } | |
7f7f9873 FB |
2293 | #else |
2294 | /* as we must map the same page at several addresses, we must use | |
2295 | a fd */ | |
2296 | { | |
2297 | const char *tmpdir; | |
2298 | ||
2299 | tmpdir = getenv("QEMU_TMPDIR"); | |
2300 | if (!tmpdir) | |
2301 | tmpdir = "/tmp"; | |
2302 | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); | |
2303 | if (mkstemp(phys_ram_file) < 0) { | |
2304 | fprintf(stderr, "Could not create temporary memory file '%s'\n", | |
2305 | phys_ram_file); | |
2306 | exit(1); | |
2307 | } | |
2308 | phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600); | |
2309 | if (phys_ram_fd < 0) { | |
2310 | fprintf(stderr, "Could not open temporary memory file '%s'\n", | |
2311 | phys_ram_file); | |
2312 | exit(1); | |
2313 | } | |
1ccde1cb | 2314 | ftruncate(phys_ram_fd, phys_ram_size); |
7f7f9873 | 2315 | unlink(phys_ram_file); |
1ccde1cb FB |
2316 | phys_ram_base = mmap(get_mmap_addr(phys_ram_size), |
2317 | phys_ram_size, | |
7f7f9873 FB |
2318 | PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, |
2319 | phys_ram_fd, 0); | |
2320 | if (phys_ram_base == MAP_FAILED) { | |
2321 | fprintf(stderr, "Could not map physical memory\n"); | |
2322 | exit(1); | |
2323 | } | |
2324 | } | |
2325 | #endif | |
0824d6fc | 2326 | |
c4b1fcc0 FB |
2327 | /* we always create the cdrom drive, even if no disk is there */ |
2328 | if (has_cdrom) { | |
2329 | bs_table[2] = bdrv_new("cdrom"); | |
2330 | bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM); | |
2331 | } | |
2332 | ||
33e3963e FB |
2333 | /* open the virtual block devices */ |
2334 | for(i = 0; i < MAX_DISKS; i++) { | |
2335 | if (hd_filename[i]) { | |
33e3963e | 2336 | if (!bs_table[i]) { |
c4b1fcc0 FB |
2337 | char buf[64]; |
2338 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); | |
2339 | bs_table[i] = bdrv_new(buf); | |
2340 | } | |
2341 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) { | |
36b486bb | 2342 | fprintf(stderr, "qemu: could not open hard disk image '%s\n", |
33e3963e FB |
2343 | hd_filename[i]); |
2344 | exit(1); | |
2345 | } | |
c4b1fcc0 FB |
2346 | if (i == 0 && cyls != 0) |
2347 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); | |
2348 | } | |
2349 | } | |
2350 | ||
2351 | /* we always create at least one floppy disk */ | |
2352 | fd_table[0] = bdrv_new("fda"); | |
2353 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); | |
2354 | ||
2355 | for(i = 0; i < MAX_FD; i++) { | |
2356 | if (fd_filename[i]) { | |
2357 | if (!fd_table[i]) { | |
2358 | char buf[64]; | |
2359 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); | |
2360 | fd_table[i] = bdrv_new(buf); | |
2361 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); | |
2362 | } | |
2363 | if (fd_filename[i] != '\0') { | |
2364 | if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) { | |
c20709aa | 2365 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", |
c4b1fcc0 FB |
2366 | fd_filename[i]); |
2367 | exit(1); | |
2368 | } | |
2369 | } | |
33e3963e FB |
2370 | } |
2371 | } | |
2372 | ||
330d0414 FB |
2373 | /* init CPU state */ |
2374 | env = cpu_init(); | |
2375 | global_env = env; | |
2376 | cpu_single_env = env; | |
2377 | ||
8a7ddc38 FB |
2378 | register_savevm("timer", 0, 1, timer_save, timer_load, env); |
2379 | register_savevm("cpu", 0, 1, cpu_save, cpu_load, env); | |
2380 | register_savevm("ram", 0, 1, ram_save, ram_load, NULL); | |
2381 | ||
330d0414 | 2382 | init_ioports(); |
80cabfad | 2383 | cpu_calibrate_ticks(); |
0824d6fc | 2384 | |
313aa567 | 2385 | /* terminal init */ |
a20dd508 | 2386 | if (nographic) { |
313aa567 FB |
2387 | dumb_display_init(ds); |
2388 | } else { | |
2389 | #ifdef CONFIG_SDL | |
2390 | sdl_display_init(ds); | |
313aa567 FB |
2391 | #else |
2392 | dumb_display_init(ds); | |
2393 | #endif | |
2394 | } | |
0824d6fc | 2395 | |
0824d6fc | 2396 | /* setup cpu signal handlers for MMU / self modifying code handling */ |
77fef8c1 | 2397 | #if !defined(CONFIG_SOFTMMU) |
8a7ddc38 | 2398 | |
77fef8c1 FB |
2399 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
2400 | { | |
2401 | stack_t stk; | |
73332e5c | 2402 | signal_stack = memalign(16, SIGNAL_STACK_SIZE); |
77fef8c1 FB |
2403 | stk.ss_sp = signal_stack; |
2404 | stk.ss_size = SIGNAL_STACK_SIZE; | |
2405 | stk.ss_flags = 0; | |
2406 | ||
2407 | if (sigaltstack(&stk, NULL) < 0) { | |
2408 | perror("sigaltstack"); | |
2409 | exit(1); | |
2410 | } | |
2411 | } | |
2412 | #endif | |
8a7ddc38 FB |
2413 | { |
2414 | struct sigaction act; | |
77fef8c1 | 2415 | |
8a7ddc38 FB |
2416 | sigfillset(&act.sa_mask); |
2417 | act.sa_flags = SA_SIGINFO; | |
77fef8c1 | 2418 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
8a7ddc38 | 2419 | act.sa_flags |= SA_ONSTACK; |
77fef8c1 | 2420 | #endif |
8a7ddc38 FB |
2421 | act.sa_sigaction = host_segv_handler; |
2422 | sigaction(SIGSEGV, &act, NULL); | |
2423 | sigaction(SIGBUS, &act, NULL); | |
77fef8c1 | 2424 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
8a7ddc38 | 2425 | sigaction(SIGFPE, &act, NULL); |
77fef8c1 | 2426 | #endif |
8a7ddc38 | 2427 | } |
3a51dee6 | 2428 | #endif |
0824d6fc | 2429 | |
67b915a5 | 2430 | #ifndef _WIN32 |
8a7ddc38 FB |
2431 | { |
2432 | struct sigaction act; | |
2433 | sigfillset(&act.sa_mask); | |
2434 | act.sa_flags = 0; | |
2435 | act.sa_handler = SIG_IGN; | |
2436 | sigaction(SIGPIPE, &act, NULL); | |
2437 | } | |
67b915a5 | 2438 | #endif |
73332e5c FB |
2439 | init_timers(); |
2440 | ||
2441 | #if defined(TARGET_I386) | |
2442 | pc_init(ram_size, vga_ram_size, boot_device, | |
2443 | ds, fd_filename, snapshot, | |
2444 | kernel_filename, kernel_cmdline, initrd_filename); | |
2445 | #elif defined(TARGET_PPC) | |
a541f297 FB |
2446 | ppc_init(ram_size, vga_ram_size, boot_device, |
2447 | ds, fd_filename, snapshot, | |
2448 | kernel_filename, kernel_cmdline, initrd_filename); | |
73332e5c FB |
2449 | #endif |
2450 | ||
2451 | /* launched after the device init so that it can display or not a | |
2452 | banner */ | |
2453 | monitor_init(); | |
0824d6fc | 2454 | |
8a7ddc38 FB |
2455 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); |
2456 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); | |
7f7f9873 | 2457 | |
67b915a5 | 2458 | #ifdef CONFIG_GDBSTUB |
b4608c04 | 2459 | if (use_gdbstub) { |
8a7ddc38 FB |
2460 | if (gdbserver_start(gdbstub_port) < 0) { |
2461 | fprintf(stderr, "Could not open gdbserver socket on port %d\n", | |
2462 | gdbstub_port); | |
2463 | exit(1); | |
2464 | } else { | |
2465 | printf("Waiting gdb connection on port %d\n", gdbstub_port); | |
2466 | } | |
67b915a5 FB |
2467 | } else |
2468 | #endif | |
a541f297 | 2469 | if (start_emulation) |
67b915a5 | 2470 | { |
8a7ddc38 | 2471 | vm_start(); |
0824d6fc | 2472 | } |
8a7ddc38 FB |
2473 | term_init(); |
2474 | main_loop(); | |
40c3bac3 | 2475 | quit_timers(); |
0824d6fc FB |
2476 | return 0; |
2477 | } |