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