Commit 8a7ddc38a60648257dc0645ab4a05b33d6040063

Authored by bellard
1 parent b0a21b53

new timer API - new API to save/restore the virtual machine state


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@690 c046a42c-6fe2-441c-8c8c-71466251a162
monitor.c
... ... @@ -281,6 +281,50 @@ static void do_log(int argc, const char **argv)
281 281 cpu_set_log(mask);
282 282 }
283 283  
  284 +static void do_savevm(int argc, const char **argv)
  285 +{
  286 + if (argc != 2) {
  287 + help_cmd(argv[0]);
  288 + return;
  289 + }
  290 + if (qemu_savevm(argv[1]) < 0)
  291 + term_printf("I/O error when saving VM to '%s'\n", argv[1]);
  292 +}
  293 +
  294 +static void do_loadvm(int argc, const char **argv)
  295 +{
  296 + if (argc != 2) {
  297 + help_cmd(argv[0]);
  298 + return;
  299 + }
  300 + if (qemu_loadvm(argv[1]) < 0)
  301 + term_printf("I/O error when loading VM from '%s'\n", argv[1]);
  302 +}
  303 +
  304 +static void do_stop(int argc, const char **argv)
  305 +{
  306 + vm_stop(EXCP_INTERRUPT);
  307 +}
  308 +
  309 +static void do_cont(int argc, const char **argv)
  310 +{
  311 + vm_start();
  312 +}
  313 +
  314 +static void do_gdbserver(int argc, const char **argv)
  315 +{
  316 + int port;
  317 +
  318 + port = DEFAULT_GDBSTUB_PORT;
  319 + if (argc >= 2)
  320 + port = atoi(argv[1]);
  321 + if (gdbserver_start(port) < 0) {
  322 + qemu_printf("Could not open gdbserver socket on port %d\n", port);
  323 + } else {
  324 + qemu_printf("Waiting gdb connection on port %d\n", port);
  325 + }
  326 +}
  327 +
284 328 static term_cmd_t term_cmds[] = {
285 329 { "help|?", do_help,
286 330 "[cmd]", "show the help" },
... ... @@ -298,6 +342,13 @@ static term_cmd_t term_cmds[] = {
298 342 "filename", "save screen into PPM image 'filename'" },
299 343 { "log", do_log,
300 344 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
  345 + { "savevm", do_savevm,
  346 + "filename", "save the whole virtual machine state to 'filename'" },
  347 + { "loadvm", do_loadvm,
  348 + "filename", "restore the whole virtual machine state from 'filename'" },
  349 + { "stop", do_stop, "", "stop emulation", },
  350 + { "c|cont", do_cont, "", "resume emulation", },
  351 + { "gdbserver", do_gdbserver, "[port]", "start gdbserver session (default port=1234)", },
301 352 { NULL, NULL, },
302 353 };
303 354  
... ... @@ -601,5 +652,5 @@ void monitor_init(void)
601 652 QEMU_VERSION);
602 653 term_show_prompt();
603 654 }
604   - add_fd_read_handler(0, term_can_read, term_read, NULL);
  655 + qemu_add_fd_read_handler(0, term_can_read, term_read, NULL);
605 656 }
... ...
... ... @@ -459,11 +459,11 @@ int AUD_get_free (void)
459 459 uint64_t ua_elapsed;
460 460 uint64_t al_elapsed;
461 461  
462   - ticks = cpu_get_ticks ();
  462 + ticks = qemu_get_clock(rt_clock);
463 463 delta = ticks - old_ticks;
464 464 old_ticks = ticks;
465 465  
466   - ua_elapsed = (delta * bytes_per_second) / ticks_per_sec;
  466 + ua_elapsed = (delta * bytes_per_second) / 1000;
467 467 al_elapsed = ua_elapsed & ~3ULL;
468 468  
469 469 ldebug ("tid elapsed %llu bytes\n", ua_elapsed);
... ...
... ... @@ -49,6 +49,7 @@
49 49  
50 50 static SDL_Surface *screen;
51 51 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
  52 +static int last_vm_running;
52 53  
53 54 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
54 55 {
... ... @@ -165,22 +166,35 @@ static void sdl_process_key(SDL_KeyboardEvent *ev)
165 166 }
166 167 }
167 168  
  169 +static void sdl_update_caption(void)
  170 +{
  171 + char buf[1024];
  172 + strcpy(buf, "QEMU");
  173 + if (!vm_running) {
  174 + strcat(buf, " [Stopped]");
  175 + }
  176 + if (gui_grab) {
  177 + strcat(buf, " - Press Ctrl-Shift to exit grab");
  178 + }
  179 + SDL_WM_SetCaption(buf, "QEMU");
  180 +}
  181 +
168 182 static void sdl_grab_start(void)
169 183 {
170   - SDL_WM_SetCaption("QEMU - Press Ctrl-Shift to exit grab", "QEMU");
171 184 SDL_ShowCursor(0);
172 185 SDL_WM_GrabInput(SDL_GRAB_ON);
173 186 /* dummy read to avoid moving the mouse */
174 187 SDL_GetRelativeMouseState(NULL, NULL);
175 188 gui_grab = 1;
  189 + sdl_update_caption();
176 190 }
177 191  
178 192 static void sdl_grab_end(void)
179 193 {
180   - SDL_WM_SetCaption("QEMU", "QEMU");
181 194 SDL_WM_GrabInput(SDL_GRAB_OFF);
182 195 SDL_ShowCursor(1);
183 196 gui_grab = 0;
  197 + sdl_update_caption();
184 198 }
185 199  
186 200 static void sdl_send_mouse_event(void)
... ... @@ -209,6 +223,11 @@ static void sdl_refresh(DisplayState *ds)
209 223 {
210 224 SDL_Event ev1, *ev = &ev1;
211 225  
  226 + if (last_vm_running != vm_running) {
  227 + last_vm_running = vm_running;
  228 + sdl_update_caption();
  229 + }
  230 +
212 231 vga_update_display();
213 232 while (SDL_PollEvent(ev)) {
214 233 switch (ev->type) {
... ... @@ -281,7 +300,7 @@ void sdl_display_init(DisplayState *ds)
281 300 ds->dpy_refresh = sdl_refresh;
282 301  
283 302 sdl_resize(ds, 640, 400);
284   - SDL_WM_SetCaption("QEMU", "QEMU");
  303 + sdl_update_caption();
285 304 SDL_EnableKeyRepeat(250, 50);
286 305 gui_grab = 0;
287 306  
... ...
... ... @@ -40,6 +40,7 @@
40 40 #include <errno.h>
41 41 #include <sys/wait.h>
42 42 #include <pty.h>
  43 +#include <sys/times.h>
43 44  
44 45 #include <sys/ioctl.h>
45 46 #include <sys/socket.h>
... ... @@ -49,6 +50,7 @@
49 50 #include "disas.h"
50 51  
51 52 #include "vl.h"
  53 +#include "exec-all.h"
52 54  
53 55 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
54 56  
... ... @@ -60,19 +62,8 @@
60 62 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
61 63 #endif
62 64  
63   -#if defined (TARGET_I386)
64   -#elif defined (TARGET_PPC)
65   -//#define USE_OPEN_FIRMWARE
66   -#if !defined (USE_OPEN_FIRMWARE)
67   -#define KERNEL_LOAD_ADDR 0x01000000
68   -#define KERNEL_STACK_ADDR 0x01200000
69   -#else
70   -#define KERNEL_LOAD_ADDR 0x00000000
71   -#define KERNEL_STACK_ADDR 0x00400000
72   -#endif
73   -#endif
74   -
75   -#define GUI_REFRESH_INTERVAL 30
  65 +/* in ms */
  66 +#define GUI_REFRESH_INTERVAL 30
76 67  
77 68 /* XXX: use a two level table to limit memory usage */
78 69 #define MAX_IOPORTS 65536
... ... @@ -88,7 +79,6 @@ BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
88 79 int vga_ram_size;
89 80 static DisplayState display_state;
90 81 int nographic;
91   -int term_inited;
92 82 int64_t ticks_per_sec;
93 83 int boot_device = 'c';
94 84 static int ram_size;
... ... @@ -97,6 +87,8 @@ int pit_min_timer_count = 0;
97 87 int nb_nics;
98 88 NetDriverState nd_table[MAX_NICS];
99 89 SerialState *serial_console;
  90 +QEMUTimer *gui_timer;
  91 +int vm_running;
100 92  
101 93 /***********************************************************/
102 94 /* x86 io ports */
... ... @@ -308,6 +300,9 @@ void hw_error(const char *fmt, ...)
308 300 abort();
309 301 }
310 302  
  303 +/***********************************************************/
  304 +/* timers */
  305 +
311 306 #if defined(__powerpc__)
312 307  
313 308 static inline uint32_t get_tbl(void)
... ... @@ -350,24 +345,34 @@ int64_t cpu_get_real_ticks(void)
350 345 #endif
351 346  
352 347 static int64_t cpu_ticks_offset;
353   -static int64_t cpu_ticks_last;
  348 +static int cpu_ticks_enabled;
354 349  
355   -int64_t cpu_get_ticks(void)
  350 +static inline int64_t cpu_get_ticks(void)
356 351 {
357   - return cpu_get_real_ticks() + cpu_ticks_offset;
  352 + if (!cpu_ticks_enabled) {
  353 + return cpu_ticks_offset;
  354 + } else {
  355 + return cpu_get_real_ticks() + cpu_ticks_offset;
  356 + }
358 357 }
359 358  
360 359 /* enable cpu_get_ticks() */
361 360 void cpu_enable_ticks(void)
362 361 {
363   - cpu_ticks_offset = cpu_ticks_last - cpu_get_real_ticks();
  362 + if (!cpu_ticks_enabled) {
  363 + cpu_ticks_offset -= cpu_get_real_ticks();
  364 + cpu_ticks_enabled = 1;
  365 + }
364 366 }
365 367  
366 368 /* disable cpu_get_ticks() : the clock is stopped. You must not call
367 369 cpu_get_ticks() after that. */
368 370 void cpu_disable_ticks(void)
369 371 {
370   - cpu_ticks_last = cpu_get_ticks();
  372 + if (cpu_ticks_enabled) {
  373 + cpu_ticks_offset = cpu_get_ticks();
  374 + cpu_ticks_enabled = 0;
  375 + }
371 376 }
372 377  
373 378 int64_t get_clock(void)
... ... @@ -382,10 +387,10 @@ void cpu_calibrate_ticks(void)
382 387 int64_t usec, ticks;
383 388  
384 389 usec = get_clock();
385   - ticks = cpu_get_ticks();
  390 + ticks = cpu_get_real_ticks();
386 391 usleep(50 * 1000);
387 392 usec = get_clock() - usec;
388   - ticks = cpu_get_ticks() - ticks;
  393 + ticks = cpu_get_real_ticks() - ticks;
389 394 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
390 395 }
391 396  
... ... @@ -413,6 +418,239 @@ uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
413 418 return res.ll;
414 419 }
415 420  
  421 +#define QEMU_TIMER_REALTIME 0
  422 +#define QEMU_TIMER_VIRTUAL 1
  423 +
  424 +struct QEMUClock {
  425 + int type;
  426 + /* XXX: add frequency */
  427 +};
  428 +
  429 +struct QEMUTimer {
  430 + QEMUClock *clock;
  431 + int64_t expire_time;
  432 + QEMUTimerCB *cb;
  433 + void *opaque;
  434 + struct QEMUTimer *next;
  435 +};
  436 +
  437 +QEMUClock *rt_clock;
  438 +QEMUClock *vm_clock;
  439 +
  440 +static QEMUTimer *active_timers[2];
  441 +/* frequency of the times() clock tick */
  442 +static int timer_freq;
  443 +
  444 +QEMUClock *qemu_new_clock(int type)
  445 +{
  446 + QEMUClock *clock;
  447 + clock = qemu_mallocz(sizeof(QEMUClock));
  448 + if (!clock)
  449 + return NULL;
  450 + clock->type = type;
  451 + return clock;
  452 +}
  453 +
  454 +QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
  455 +{
  456 + QEMUTimer *ts;
  457 +
  458 + ts = qemu_mallocz(sizeof(QEMUTimer));
  459 + ts->clock = clock;
  460 + ts->cb = cb;
  461 + ts->opaque = opaque;
  462 + return ts;
  463 +}
  464 +
  465 +void qemu_free_timer(QEMUTimer *ts)
  466 +{
  467 + qemu_free(ts);
  468 +}
  469 +
  470 +/* stop a timer, but do not dealloc it */
  471 +void qemu_del_timer(QEMUTimer *ts)
  472 +{
  473 + QEMUTimer **pt, *t;
  474 +
  475 + /* NOTE: this code must be signal safe because
  476 + qemu_timer_expired() can be called from a signal. */
  477 + pt = &active_timers[ts->clock->type];
  478 + for(;;) {
  479 + t = *pt;
  480 + if (!t)
  481 + break;
  482 + if (t == ts) {
  483 + *pt = t->next;
  484 + break;
  485 + }
  486 + pt = &t->next;
  487 + }
  488 +}
  489 +
  490 +/* modify the current timer so that it will be fired when current_time
  491 + >= expire_time. The corresponding callback will be called. */
  492 +void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
  493 +{
  494 + QEMUTimer **pt, *t;
  495 +
  496 + qemu_del_timer(ts);
  497 +
  498 + /* add the timer in the sorted list */
  499 + /* NOTE: this code must be signal safe because
  500 + qemu_timer_expired() can be called from a signal. */
  501 + pt = &active_timers[ts->clock->type];
  502 + for(;;) {
  503 + t = *pt;
  504 + if (!t)
  505 + break;
  506 + if (t->expire_time > expire_time)
  507 + break;
  508 + pt = &t->next;
  509 + }
  510 + ts->expire_time = expire_time;
  511 + ts->next = *pt;
  512 + *pt = ts;
  513 +}
  514 +
  515 +int qemu_timer_pending(QEMUTimer *ts)
  516 +{
  517 + QEMUTimer *t;
  518 + for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
  519 + if (t == ts)
  520 + return 1;
  521 + }
  522 + return 0;
  523 +}
  524 +
  525 +static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
  526 +{
  527 + if (!timer_head)
  528 + return 0;
  529 + return (timer_head->expire_time <= current_time);
  530 +}
  531 +
  532 +static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
  533 +{
  534 + QEMUTimer *ts;
  535 +
  536 + for(;;) {
  537 + ts = *ptimer_head;
  538 + if (ts->expire_time > current_time)
  539 + break;
  540 + /* remove timer from the list before calling the callback */
  541 + *ptimer_head = ts->next;
  542 + ts->next = NULL;
  543 +
  544 + /* run the callback (the timer list can be modified) */
  545 + ts->cb(ts->opaque);
  546 + }
  547 +}
  548 +
  549 +int64_t qemu_get_clock(QEMUClock *clock)
  550 +{
  551 + switch(clock->type) {
  552 + case QEMU_TIMER_REALTIME:
  553 + /* XXX: portability among Linux hosts */
  554 + if (timer_freq == 100) {
  555 + return times(NULL) * 10;
  556 + } else {
  557 + return ((int64_t)times(NULL) * 1000) / timer_freq;
  558 + }
  559 + default:
  560 + case QEMU_TIMER_VIRTUAL:
  561 + return cpu_get_ticks();
  562 + }
  563 +}
  564 +
  565 +/* save a timer */
  566 +void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
  567 +{
  568 + uint64_t expire_time;
  569 +
  570 + if (qemu_timer_pending(ts)) {
  571 + expire_time = ts->expire_time;
  572 + } else {
  573 + expire_time = -1;
  574 + }
  575 + qemu_put_be64(f, expire_time);
  576 +}
  577 +
  578 +void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
  579 +{
  580 + uint64_t expire_time;
  581 +
  582 + expire_time = qemu_get_be64(f);
  583 + if (expire_time != -1) {
  584 + qemu_mod_timer(ts, expire_time);
  585 + } else {
  586 + qemu_del_timer(ts);
  587 + }
  588 +}
  589 +
  590 +static void timer_save(QEMUFile *f, void *opaque)
  591 +{
  592 + if (cpu_ticks_enabled) {
  593 + hw_error("cannot save state if virtual timers are running");
  594 + }
  595 + qemu_put_be64s(f, &cpu_ticks_offset);
  596 + qemu_put_be64s(f, &ticks_per_sec);
  597 +}
  598 +
  599 +static int timer_load(QEMUFile *f, void *opaque, int version_id)
  600 +{
  601 + if (version_id != 1)
  602 + return -EINVAL;
  603 + if (cpu_ticks_enabled) {
  604 + return -EINVAL;
  605 + }
  606 + qemu_get_be64s(f, &cpu_ticks_offset);
  607 + qemu_get_be64s(f, &ticks_per_sec);
  608 + return 0;
  609 +}
  610 +
  611 +static void host_alarm_handler(int host_signum)
  612 +{
  613 + if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
  614 + qemu_get_clock(vm_clock)) ||
  615 + qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
  616 + qemu_get_clock(rt_clock))) {
  617 + /* stop the cpu because a timer occured */
  618 + cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
  619 + }
  620 +}
  621 +
  622 +static void init_timers(void)
  623 +{
  624 + struct sigaction act;
  625 + struct itimerval itv;
  626 +
  627 + /* get times() syscall frequency */
  628 + timer_freq = sysconf(_SC_CLK_TCK);
  629 +
  630 + rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
  631 + vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
  632 +
  633 + /* timer signal */
  634 + sigfillset(&act.sa_mask);
  635 + act.sa_flags = 0;
  636 +#if defined (TARGET_I386) && defined(USE_CODE_COPY)
  637 + act.sa_flags |= SA_ONSTACK;
  638 +#endif
  639 + act.sa_handler = host_alarm_handler;
  640 + sigaction(SIGALRM, &act, NULL);
  641 +
  642 + itv.it_interval.tv_sec = 0;
  643 + itv.it_interval.tv_usec = 1000;
  644 + itv.it_value.tv_sec = 0;
  645 + itv.it_value.tv_usec = 10 * 1000;
  646 + setitimer(ITIMER_REAL, &itv, NULL);
  647 + /* we probe the tick duration of the kernel to inform the user if
  648 + the emulated kernel requested a too high timer frequency */
  649 + getitimer(ITIMER_REAL, &itv);
  650 + pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
  651 + 1000000;
  652 +}
  653 +
416 654 /***********************************************************/
417 655 /* serial device */
418 656  
... ... @@ -588,36 +826,8 @@ static void host_segv_handler(int host_signum, siginfo_t *info,
588 826 }
589 827 #endif
590 828  
591   -static int timer_irq_pending;
592   -static int timer_irq_count;
593   -
594   -static int timer_ms;
595   -static int gui_refresh_pending, gui_refresh_count;
596   -
597   -static void host_alarm_handler(int host_signum, siginfo_t *info,
598   - void *puc)
599   -{
600   - /* NOTE: since usually the OS asks a 100 Hz clock, there can be
601   - some drift between cpu_get_ticks() and the interrupt time. So
602   - we queue some interrupts to avoid missing some */
603   - timer_irq_count += pit_get_out_edges(&pit_channels[0]);
604   - if (timer_irq_count) {
605   - if (timer_irq_count > 2)
606   - timer_irq_count = 2;
607   - timer_irq_count--;
608   - timer_irq_pending = 1;
609   - }
610   - gui_refresh_count += timer_ms;
611   - if (gui_refresh_count >= GUI_REFRESH_INTERVAL) {
612   - gui_refresh_count = 0;
613   - gui_refresh_pending = 1;
614   - }
615   -
616   - if (gui_refresh_pending || timer_irq_pending) {
617   - /* just exit from the cpu to have a chance to handle timers */
618   - cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
619   - }
620   -}
  829 +/***********************************************************/
  830 +/* I/O handling */
621 831  
622 832 #define MAX_IO_HANDLERS 64
623 833  
... ... @@ -629,142 +839,653 @@ typedef struct IOHandlerRecord {
629 839 /* temporary data */
630 840 struct pollfd *ufd;
631 841 int max_size;
  842 + struct IOHandlerRecord *next;
632 843 } IOHandlerRecord;
633 844  
634   -static IOHandlerRecord io_handlers[MAX_IO_HANDLERS];
635   -static int nb_io_handlers = 0;
  845 +static IOHandlerRecord *first_io_handler;
636 846  
637   -int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
638   - IOReadHandler *fd_read, void *opaque)
  847 +int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
  848 + IOReadHandler *fd_read, void *opaque)
639 849 {
640 850 IOHandlerRecord *ioh;
641 851  
642   - if (nb_io_handlers >= MAX_IO_HANDLERS)
  852 + ioh = qemu_mallocz(sizeof(IOHandlerRecord));
  853 + if (!ioh)
643 854 return -1;
644   - ioh = &io_handlers[nb_io_handlers];
645 855 ioh->fd = fd;
646 856 ioh->fd_can_read = fd_can_read;
647 857 ioh->fd_read = fd_read;
648 858 ioh->opaque = opaque;
649   - nb_io_handlers++;
  859 + ioh->next = first_io_handler;
  860 + first_io_handler = ioh;
650 861 return 0;
651 862 }
652 863  
653   -/* main execution loop */
  864 +void qemu_del_fd_read_handler(int fd)
  865 +{
  866 + IOHandlerRecord **pioh, *ioh;
654 867  
655   -CPUState *cpu_gdbstub_get_env(void *opaque)
  868 + pioh = &first_io_handler;
  869 + for(;;) {
  870 + ioh = *pioh;
  871 + if (ioh == NULL)
  872 + break;
  873 + if (ioh->fd == fd) {
  874 + *pioh = ioh->next;
  875 + break;
  876 + }
  877 + pioh = &ioh->next;
  878 + }
  879 +}
  880 +
  881 +/***********************************************************/
  882 +/* savevm/loadvm support */
  883 +
  884 +void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
656 885 {
657   - return global_env;
  886 + fwrite(buf, 1, size, f);
658 887 }
659 888  
660   -int main_loop(void *opaque)
  889 +void qemu_put_byte(QEMUFile *f, int v)
661 890 {
662   - struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf, *gdb_ufd;
663   - int ret, n, timeout, serial_ok, max_size, i;
664   - uint8_t buf[4096];
665   - IOHandlerRecord *ioh;
666   - CPUState *env = global_env;
  891 + fputc(v, f);
  892 +}
  893 +
  894 +void qemu_put_be16(QEMUFile *f, unsigned int v)
  895 +{
  896 + qemu_put_byte(f, v >> 8);
  897 + qemu_put_byte(f, v);
  898 +}
  899 +
  900 +void qemu_put_be32(QEMUFile *f, unsigned int v)
  901 +{
  902 + qemu_put_byte(f, v >> 24);
  903 + qemu_put_byte(f, v >> 16);
  904 + qemu_put_byte(f, v >> 8);
  905 + qemu_put_byte(f, v);
  906 +}
  907 +
  908 +void qemu_put_be64(QEMUFile *f, uint64_t v)
  909 +{
  910 + qemu_put_be32(f, v >> 32);
  911 + qemu_put_be32(f, v);
  912 +}
  913 +
  914 +int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
  915 +{
  916 + return fread(buf, 1, size, f);
  917 +}
  918 +
  919 +int qemu_get_byte(QEMUFile *f)
  920 +{
  921 + int v;
  922 + v = fgetc(f);
  923 + if (v == EOF)
  924 + return 0;
  925 + else
  926 + return v;
  927 +}
  928 +
  929 +unsigned int qemu_get_be16(QEMUFile *f)
  930 +{
  931 + unsigned int v;
  932 + v = qemu_get_byte(f) << 8;
  933 + v |= qemu_get_byte(f);
  934 + return v;
  935 +}
  936 +
  937 +unsigned int qemu_get_be32(QEMUFile *f)
  938 +{
  939 + unsigned int v;
  940 + v = qemu_get_byte(f) << 24;
  941 + v |= qemu_get_byte(f) << 16;
  942 + v |= qemu_get_byte(f) << 8;
  943 + v |= qemu_get_byte(f);
  944 + return v;
  945 +}
  946 +
  947 +uint64_t qemu_get_be64(QEMUFile *f)
  948 +{
  949 + uint64_t v;
  950 + v = (uint64_t)qemu_get_be32(f) << 32;
  951 + v |= qemu_get_be32(f);
  952 + return v;
  953 +}
  954 +
  955 +int64_t qemu_ftell(QEMUFile *f)
  956 +{
  957 + return ftell(f);
  958 +}
  959 +
  960 +int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
  961 +{
  962 + if (fseek(f, pos, whence) < 0)
  963 + return -1;
  964 + return ftell(f);
  965 +}
  966 +
  967 +typedef struct SaveStateEntry {
  968 + char idstr[256];
  969 + int instance_id;
  970 + int version_id;
  971 + SaveStateHandler *save_state;
  972 + LoadStateHandler *load_state;
  973 + void *opaque;
  974 + struct SaveStateEntry *next;
  975 +} SaveStateEntry;
667 976  
668   - if (!term_inited) {
669   - /* initialize terminal only there so that the user has a
670   - chance to stop QEMU with Ctrl-C before the gdb connection
671   - is launched */
672   - term_inited = 1;
673   - term_init();
  977 +static SaveStateEntry *first_se;
  978 +
  979 +int register_savevm(const char *idstr,
  980 + int instance_id,
  981 + int version_id,
  982 + SaveStateHandler *save_state,
  983 + LoadStateHandler *load_state,
  984 + void *opaque)
  985 +{
  986 + SaveStateEntry *se, **pse;
  987 +
  988 + se = qemu_malloc(sizeof(SaveStateEntry));
  989 + if (!se)
  990 + return -1;
  991 + pstrcpy(se->idstr, sizeof(se->idstr), idstr);
  992 + se->instance_id = instance_id;
  993 + se->version_id = version_id;
  994 + se->save_state = save_state;
  995 + se->load_state = load_state;
  996 + se->opaque = opaque;
  997 + se->next = NULL;
  998 +
  999 + /* add at the end of list */
  1000 + pse = &first_se;
  1001 + while (*pse != NULL)
  1002 + pse = &(*pse)->next;
  1003 + *pse = se;
  1004 + return 0;
  1005 +}
  1006 +
  1007 +#define QEMU_VM_FILE_MAGIC 0x5145564d
  1008 +#define QEMU_VM_FILE_VERSION 0x00000001
  1009 +
  1010 +int qemu_savevm(const char *filename)
  1011 +{
  1012 + SaveStateEntry *se;
  1013 + QEMUFile *f;
  1014 + int len, len_pos, cur_pos, saved_vm_running, ret;
  1015 +
  1016 + saved_vm_running = vm_running;
  1017 + vm_stop(0);
  1018 +
  1019 + f = fopen(filename, "wb");
  1020 + if (!f) {
  1021 + ret = -1;
  1022 + goto the_end;
674 1023 }
675 1024  
676   - serial_ok = 1;
677   - cpu_enable_ticks();
  1025 + qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  1026 + qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  1027 +
  1028 + for(se = first_se; se != NULL; se = se->next) {
  1029 + /* ID string */
  1030 + len = strlen(se->idstr);
  1031 + qemu_put_byte(f, len);
  1032 + qemu_put_buffer(f, se->idstr, len);
  1033 +
  1034 + qemu_put_be32(f, se->instance_id);
  1035 + qemu_put_be32(f, se->version_id);
  1036 +
  1037 + /* record size: filled later */
  1038 + len_pos = ftell(f);
  1039 + qemu_put_be32(f, 0);
  1040 +
  1041 + se->save_state(f, se->opaque);
  1042 +
  1043 + /* fill record size */
  1044 + cur_pos = ftell(f);
  1045 + len = ftell(f) - len_pos - 4;
  1046 + fseek(f, len_pos, SEEK_SET);
  1047 + qemu_put_be32(f, len);
  1048 + fseek(f, cur_pos, SEEK_SET);
  1049 + }
  1050 +
  1051 + fclose(f);
  1052 + ret = 0;
  1053 + the_end:
  1054 + if (saved_vm_running)
  1055 + vm_start();
  1056 + return ret;
  1057 +}
  1058 +
  1059 +static SaveStateEntry *find_se(const char *idstr, int instance_id)
  1060 +{
  1061 + SaveStateEntry *se;
  1062 +
  1063 + for(se = first_se; se != NULL; se = se->next) {
  1064 + if (!strcmp(se->idstr, idstr) &&
  1065 + instance_id == se->instance_id)
  1066 + return se;
  1067 + }
  1068 + return NULL;
  1069 +}
  1070 +
  1071 +int qemu_loadvm(const char *filename)
  1072 +{
  1073 + SaveStateEntry *se;
  1074 + QEMUFile *f;
  1075 + int len, cur_pos, ret, instance_id, record_len, version_id;
  1076 + int saved_vm_running;
  1077 + unsigned int v;
  1078 + char idstr[256];
  1079 +
  1080 + saved_vm_running = vm_running;
  1081 + vm_stop(0);
  1082 +
  1083 + f = fopen(filename, "rb");
  1084 + if (!f) {
  1085 + ret = -1;
  1086 + goto the_end;
  1087 + }
  1088 +
  1089 + v = qemu_get_be32(f);
  1090 + if (v != QEMU_VM_FILE_MAGIC)
  1091 + goto fail;
  1092 + v = qemu_get_be32(f);
  1093 + if (v != QEMU_VM_FILE_VERSION) {
  1094 + fail:
  1095 + fclose(f);
  1096 + ret = -1;
  1097 + goto the_end;
  1098 + }
678 1099 for(;;) {
679   -#if defined (DO_TB_FLUSH)
680   - tb_flush();
681   -#endif
682   - ret = cpu_exec(env);
683   - if (reset_requested) {
684   - ret = EXCP_INTERRUPT;
  1100 + len = qemu_get_byte(f);
  1101 + if (feof(f))
685 1102 break;
  1103 + qemu_get_buffer(f, idstr, len);
  1104 + idstr[len] = '\0';
  1105 + instance_id = qemu_get_be32(f);
  1106 + version_id = qemu_get_be32(f);
  1107 + record_len = qemu_get_be32(f);
  1108 +#if 0
  1109 + printf("idstr=%s instance=0x%x version=%d len=%d\n",
  1110 + idstr, instance_id, version_id, record_len);
  1111 +#endif
  1112 + cur_pos = ftell(f);
  1113 + se = find_se(idstr, instance_id);
  1114 + if (!se) {
  1115 + fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
  1116 + instance_id, idstr);
  1117 + } else {
  1118 + ret = se->load_state(f, se->opaque, version_id);
  1119 + if (ret < 0) {
  1120 + fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
  1121 + instance_id, idstr);
  1122 + }
686 1123 }
687   - if (ret == EXCP_DEBUG) {
688   - ret = EXCP_DEBUG;
689   - break;
  1124 + /* always seek to exact end of record */
  1125 + qemu_fseek(f, cur_pos + record_len, SEEK_SET);
  1126 + }
  1127 + fclose(f);
  1128 + ret = 0;
  1129 + the_end:
  1130 + if (saved_vm_running)
  1131 + vm_start();
  1132 + return ret;
  1133 +}
  1134 +
  1135 +/***********************************************************/
  1136 +/* cpu save/restore */
  1137 +
  1138 +#if defined(TARGET_I386)
  1139 +
  1140 +static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
  1141 +{
  1142 + qemu_put_be32(f, (uint32_t)dt->base);
  1143 + qemu_put_be32(f, dt->limit);
  1144 + qemu_put_be32(f, dt->flags);
  1145 +}
  1146 +
  1147 +static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
  1148 +{
  1149 + dt->base = (uint8_t *)qemu_get_be32(f);
  1150 + dt->limit = qemu_get_be32(f);
  1151 + dt->flags = qemu_get_be32(f);
  1152 +}
  1153 +
  1154 +void cpu_save(QEMUFile *f, void *opaque)
  1155 +{
  1156 + CPUState *env = opaque;
  1157 + uint16_t fptag, fpus, fpuc;
  1158 + uint32_t hflags;
  1159 + int i;
  1160 +
  1161 + for(i = 0; i < 8; i++)
  1162 + qemu_put_be32s(f, &env->regs[i]);
  1163 + qemu_put_be32s(f, &env->eip);
  1164 + qemu_put_be32s(f, &env->eflags);
  1165 + qemu_put_be32s(f, &env->eflags);
  1166 + hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
  1167 + qemu_put_be32s(f, &hflags);
  1168 +
  1169 + /* FPU */
  1170 + fpuc = env->fpuc;
  1171 + fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
  1172 + fptag = 0;
  1173 + for (i=7; i>=0; i--) {
  1174 + fptag <<= 2;
  1175 + if (env->fptags[i]) {
  1176 + fptag |= 3;
  1177 + }
  1178 + }
  1179 +
  1180 + qemu_put_be16s(f, &fpuc);
  1181 + qemu_put_be16s(f, &fpus);
  1182 + qemu_put_be16s(f, &fptag);
  1183 +
  1184 + for(i = 0; i < 8; i++) {
  1185 + uint64_t mant;
  1186 + uint16_t exp;
  1187 + cpu_get_fp80(&mant, &exp, env->fpregs[i]);
  1188 + qemu_put_be64(f, mant);
  1189 + qemu_put_be16(f, exp);
  1190 + }
  1191 +
  1192 + for(i = 0; i < 6; i++)
  1193 + cpu_put_seg(f, &env->segs[i]);
  1194 + cpu_put_seg(f, &env->ldt);
  1195 + cpu_put_seg(f, &env->tr);
  1196 + cpu_put_seg(f, &env->gdt);
  1197 + cpu_put_seg(f, &env->idt);
  1198 +
  1199 + qemu_put_be32s(f, &env->sysenter_cs);
  1200 + qemu_put_be32s(f, &env->sysenter_esp);
  1201 + qemu_put_be32s(f, &env->sysenter_eip);
  1202 +
  1203 + qemu_put_be32s(f, &env->cr[0]);
  1204 + qemu_put_be32s(f, &env->cr[2]);
  1205 + qemu_put_be32s(f, &env->cr[3]);
  1206 + qemu_put_be32s(f, &env->cr[4]);
  1207 +
  1208 + for(i = 0; i < 8; i++)
  1209 + qemu_put_be32s(f, &env->dr[i]);
  1210 +
  1211 + /* MMU */
  1212 + qemu_put_be32s(f, &env->a20_mask);
  1213 +}
  1214 +
  1215 +int cpu_load(QEMUFile *f, void *opaque, int version_id)
  1216 +{
  1217 + CPUState *env = opaque;
  1218 + int i;
  1219 + uint32_t hflags;
  1220 + uint16_t fpus, fpuc, fptag;
  1221 +
  1222 + if (version_id != 1)
  1223 + return -EINVAL;
  1224 + for(i = 0; i < 8; i++)
  1225 + qemu_get_be32s(f, &env->regs[i]);
  1226 + qemu_get_be32s(f, &env->eip);
  1227 + qemu_get_be32s(f, &env->eflags);
  1228 + qemu_get_be32s(f, &env->eflags);
  1229 + qemu_get_be32s(f, &hflags);
  1230 +
  1231 + qemu_get_be16s(f, &fpuc);
  1232 + qemu_get_be16s(f, &fpus);
  1233 + qemu_get_be16s(f, &fptag);
  1234 +
  1235 + for(i = 0; i < 8; i++) {
  1236 + uint64_t mant;
  1237 + uint16_t exp;
  1238 + mant = qemu_get_be64(f);
  1239 + exp = qemu_get_be16(f);
  1240 + env->fpregs[i] = cpu_set_fp80(mant, exp);
  1241 + }
  1242 +
  1243 + env->fpuc = fpuc;
  1244 + env->fpstt = (fpus >> 11) & 7;
  1245 + env->fpus = fpus & ~0x3800;
  1246 + for(i = 0; i < 8; i++) {
  1247 + env->fptags[i] = ((fptag & 3) == 3);
  1248 + fptag >>= 2;
  1249 + }
  1250 +
  1251 + for(i = 0; i < 6; i++)
  1252 + cpu_get_seg(f, &env->segs[i]);
  1253 + cpu_get_seg(f, &env->ldt);
  1254 + cpu_get_seg(f, &env->tr);
  1255 + cpu_get_seg(f, &env->gdt);
  1256 + cpu_get_seg(f, &env->idt);
  1257 +
  1258 + qemu_get_be32s(f, &env->sysenter_cs);
  1259 + qemu_get_be32s(f, &env->sysenter_esp);
  1260 + qemu_get_be32s(f, &env->sysenter_eip);
  1261 +
  1262 + qemu_get_be32s(f, &env->cr[0]);
  1263 + qemu_get_be32s(f, &env->cr[2]);
  1264 + qemu_get_be32s(f, &env->cr[3]);
  1265 + qemu_get_be32s(f, &env->cr[4]);
  1266 +
  1267 + for(i = 0; i < 8; i++)
  1268 + qemu_get_be32s(f, &env->dr[i]);
  1269 +
  1270 + /* MMU */
  1271 + qemu_get_be32s(f, &env->a20_mask);
  1272 +
  1273 + /* XXX: compute hflags from scratch, except for CPL and IIF */
  1274 + env->hflags = hflags;
  1275 + tlb_flush(env, 1);
  1276 + return 0;
  1277 +}
  1278 +
  1279 +#else
  1280 +
  1281 +#warning No CPU save/restore functions
  1282 +
  1283 +#endif
  1284 +
  1285 +/***********************************************************/
  1286 +/* ram save/restore */
  1287 +
  1288 +/* we just avoid storing empty pages */
  1289 +static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
  1290 +{
  1291 + int i, v;
  1292 +
  1293 + v = buf[0];
  1294 + for(i = 1; i < len; i++) {
  1295 + if (buf[i] != v)
  1296 + goto normal_save;
  1297 + }
  1298 + qemu_put_byte(f, 1);
  1299 + qemu_put_byte(f, v);
  1300 + return;
  1301 + normal_save:
  1302 + qemu_put_byte(f, 0);
  1303 + qemu_put_buffer(f, buf, len);
  1304 +}
  1305 +
  1306 +static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
  1307 +{
  1308 + int v;
  1309 +
  1310 + v = qemu_get_byte(f);
  1311 + switch(v) {
  1312 + case 0:
  1313 + if (qemu_get_buffer(f, buf, len) != len)
  1314 + return -EIO;
  1315 + break;
  1316 + case 1:
  1317 + v = qemu_get_byte(f);
  1318 + memset(buf, v, len);
  1319 + break;
  1320 + default:
  1321 + return -EINVAL;
  1322 + }
  1323 + return 0;
  1324 +}
  1325 +
  1326 +static void ram_save(QEMUFile *f, void *opaque)
  1327 +{
  1328 + int i;
  1329 + qemu_put_be32(f, phys_ram_size);
  1330 + for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
  1331 + ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
  1332 + }
  1333 +}
  1334 +
  1335 +static int ram_load(QEMUFile *f, void *opaque, int version_id)
  1336 +{
  1337 + int i, ret;
  1338 +
  1339 + if (version_id != 1)
  1340 + return -EINVAL;
  1341 + if (qemu_get_be32(f) != phys_ram_size)
  1342 + return -EINVAL;
  1343 + for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
  1344 + ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
  1345 + if (ret)
  1346 + return ret;
  1347 + }
  1348 + return 0;
  1349 +}
  1350 +
  1351 +/***********************************************************/
  1352 +/* main execution loop */
  1353 +
  1354 +void gui_update(void *opaque)
  1355 +{
  1356 + display_state.dpy_refresh(&display_state);
  1357 + qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
  1358 +}
  1359 +
  1360 +/* XXX: support several handlers */
  1361 +VMStopHandler *vm_stop_cb;
  1362 +VMStopHandler *vm_stop_opaque;
  1363 +
  1364 +int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
  1365 +{
  1366 + vm_stop_cb = cb;
  1367 + vm_stop_opaque = opaque;
  1368 + return 0;
  1369 +}
  1370 +
  1371 +void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
  1372 +{
  1373 + vm_stop_cb = NULL;
  1374 +}
  1375 +
  1376 +void vm_start(void)
  1377 +{
  1378 + if (!vm_running) {
  1379 + cpu_enable_ticks();
  1380 + vm_running = 1;
  1381 + }
  1382 +}
  1383 +
  1384 +void vm_stop(int reason)
  1385 +{
  1386 + if (vm_running) {
  1387 + cpu_disable_ticks();
  1388 + vm_running = 0;
  1389 + if (reason != 0) {
  1390 + if (vm_stop_cb) {
  1391 + vm_stop_cb(vm_stop_opaque, reason);
  1392 + }
690 1393 }
691   - /* if hlt instruction, we wait until the next IRQ */
692   - if (ret == EXCP_HLT)
  1394 + }
  1395 +}
  1396 +
  1397 +int main_loop(void)
  1398 +{
  1399 + struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
  1400 + int ret, n, timeout, max_size;
  1401 + uint8_t buf[4096];
  1402 + IOHandlerRecord *ioh, *ioh_next;
  1403 + CPUState *env = global_env;
  1404 +
  1405 + for(;;) {
  1406 + if (vm_running) {
  1407 + ret = cpu_exec(env);
  1408 + if (reset_requested) {
  1409 + ret = EXCP_INTERRUPT;
  1410 + break;
  1411 + }
  1412 + if (ret == EXCP_DEBUG) {
  1413 + vm_stop(EXCP_DEBUG);
  1414 + }
  1415 + /* if hlt instruction, we wait until the next IRQ */
  1416 + /* XXX: use timeout computed from timers */
  1417 + if (ret == EXCP_HLT)
  1418 + timeout = 10;
  1419 + else
  1420 + timeout = 0;
  1421 + } else {
693 1422 timeout = 10;
694   - else
695   - timeout = 0;
  1423 + }
696 1424  
697 1425 /* poll any events */
  1426 + /* XXX: separate device handlers from system ones */
698 1427 pf = ufds;
699   - ioh = io_handlers;
700   - for(i = 0; i < nb_io_handlers; i++) {
701   - max_size = ioh->fd_can_read(ioh->opaque);
702   - if (max_size > 0) {
703   - if (max_size > sizeof(buf))
704   - max_size = sizeof(buf);
  1428 + for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
  1429 + if (!ioh->fd_can_read) {
  1430 + max_size = 0;
705 1431 pf->fd = ioh->fd;
706 1432 pf->events = POLLIN;
707 1433 ioh->ufd = pf;
708 1434 pf++;
709 1435 } else {
710   - ioh->ufd = NULL;
  1436 + max_size = ioh->fd_can_read(ioh->opaque);
  1437 + if (max_size > 0) {
  1438 + if (max_size > sizeof(buf))
  1439 + max_size = sizeof(buf);
  1440 + pf->fd = ioh->fd;
  1441 + pf->events = POLLIN;
  1442 + ioh->ufd = pf;
  1443 + pf++;
  1444 + } else {
  1445 + ioh->ufd = NULL;
  1446 + }
711 1447 }
712 1448 ioh->max_size = max_size;
713   - ioh++;
714   - }
715   -
716   - gdb_ufd = NULL;
717   - if (gdbstub_fd > 0) {
718   - gdb_ufd = pf;
719   - pf->fd = gdbstub_fd;
720   - pf->events = POLLIN;
721   - pf++;
722 1449 }
723 1450  
724 1451 ret = poll(ufds, pf - ufds, timeout);
725 1452 if (ret > 0) {
726   - ioh = io_handlers;
727   - for(i = 0; i < nb_io_handlers; i++) {
  1453 + /* XXX: better handling of removal */
  1454 + for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
  1455 + ioh_next = ioh->next;
728 1456 pf = ioh->ufd;
729 1457 if (pf) {
730   - n = read(ioh->fd, buf, ioh->max_size);
731   - if (n > 0) {
732   - ioh->fd_read(ioh->opaque, buf, n);
  1458 + if (pf->revents & POLLIN) {
  1459 + if (ioh->max_size == 0) {
  1460 + /* just a read event */
  1461 + ioh->fd_read(ioh->opaque, NULL, 0);
  1462 + } else {
  1463 + n = read(ioh->fd, buf, ioh->max_size);
  1464 + if (n >= 0) {
  1465 + ioh->fd_read(ioh->opaque, buf, n);
  1466 + } else if (errno != -EAGAIN) {
  1467 + ioh->fd_read(ioh->opaque, NULL, -errno);
  1468 + }
  1469 + }
733 1470 }
734 1471 }
735   - ioh++;
736   - }
737   - if (gdb_ufd && (gdb_ufd->revents & POLLIN)) {
738   - uint8_t buf[1];
739   - /* stop emulation if requested by gdb */
740   - n = read(gdbstub_fd, buf, 1);
741   - if (n == 1) {
742   - ret = EXCP_INTERRUPT;
743   - break;
744   - }
745 1472 }
746 1473 }
747 1474  
748   - /* timer IRQ */
749   - if (timer_irq_pending) {
750   -#if defined (TARGET_I386)
751   - pic_set_irq(0, 1);
752   - pic_set_irq(0, 0);
753   - timer_irq_pending = 0;
754   - rtc_timer();
755   -#endif
  1475 + if (vm_running) {
  1476 + qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
  1477 + qemu_get_clock(vm_clock));
  1478 +
  1479 + /* XXX: add explicit timer */
  1480 + SB16_run();
  1481 +
  1482 + /* run dma transfers, if any */
  1483 + DMA_run();
756 1484 }
757   - /* XXX: add explicit timer */
758   - SB16_run();
759   -
760   - /* run dma transfers, if any */
761   - DMA_run();
762 1485  
763   - /* VGA */
764   - if (gui_refresh_pending) {
765   - display_state.dpy_refresh(&display_state);
766   - gui_refresh_pending = 0;
767   - }
  1486 + /* real time timers */
  1487 + qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
  1488 + qemu_get_clock(rt_clock));
768 1489 }
769 1490 cpu_disable_ticks();
770 1491 return ret;
... ... @@ -873,8 +1594,6 @@ int main(int argc, char **argv)
873 1594 {
874 1595 int c, i, use_gdbstub, gdbstub_port, long_index, has_cdrom;
875 1596 int snapshot, linux_boot;
876   - struct sigaction act;
877   - struct itimerval itv;
878 1597 CPUState *env;
879 1598 const char *initrd_filename;
880 1599 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
... ... @@ -1186,11 +1905,17 @@ int main(int argc, char **argv)
1186 1905 }
1187 1906 }
1188 1907  
  1908 + init_timers();
  1909 +
1189 1910 /* init CPU state */
1190 1911 env = cpu_init();
1191 1912 global_env = env;
1192 1913 cpu_single_env = env;
1193 1914  
  1915 + register_savevm("timer", 0, 1, timer_save, timer_load, env);
  1916 + register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
  1917 + register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
  1918 +
1194 1919 init_ioports();
1195 1920 cpu_calibrate_ticks();
1196 1921  
... ... @@ -1219,7 +1944,7 @@ int main(int argc, char **argv)
1219 1944  
1220 1945 /* setup cpu signal handlers for MMU / self modifying code handling */
1221 1946 #if !defined(CONFIG_SOFTMMU)
1222   -
  1947 +
1223 1948 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1224 1949 {
1225 1950 stack_t stk;
... ... @@ -1234,45 +1959,46 @@ int main(int argc, char **argv)
1234 1959 }
1235 1960 }
1236 1961 #endif
  1962 + {
  1963 + struct sigaction act;
1237 1964  
1238   - sigfillset(&act.sa_mask);
1239   - act.sa_flags = SA_SIGINFO;
  1965 + sigfillset(&act.sa_mask);
  1966 + act.sa_flags = SA_SIGINFO;
1240 1967 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1241   - act.sa_flags |= SA_ONSTACK;
  1968 + act.sa_flags |= SA_ONSTACK;
1242 1969 #endif
1243   - act.sa_sigaction = host_segv_handler;
1244   - sigaction(SIGSEGV, &act, NULL);
1245   - sigaction(SIGBUS, &act, NULL);
  1970 + act.sa_sigaction = host_segv_handler;
  1971 + sigaction(SIGSEGV, &act, NULL);
  1972 + sigaction(SIGBUS, &act, NULL);
1246 1973 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1247   - sigaction(SIGFPE, &act, NULL);
  1974 + sigaction(SIGFPE, &act, NULL);
1248 1975 #endif
  1976 + }
1249 1977 #endif
1250 1978  
1251   - /* timer signal */
1252   - sigfillset(&act.sa_mask);
1253   - act.sa_flags = SA_SIGINFO;
1254   -#if defined (TARGET_I386) && defined(USE_CODE_COPY)
1255   - act.sa_flags |= SA_ONSTACK;
1256   -#endif
1257   - act.sa_sigaction = host_alarm_handler;
1258   - sigaction(SIGALRM, &act, NULL);
  1979 + {
  1980 + struct sigaction act;
  1981 + sigfillset(&act.sa_mask);
  1982 + act.sa_flags = 0;
  1983 + act.sa_handler = SIG_IGN;
  1984 + sigaction(SIGPIPE, &act, NULL);
  1985 + }
1259 1986  
1260   - itv.it_interval.tv_sec = 0;
1261   - itv.it_interval.tv_usec = 1000;
1262   - itv.it_value.tv_sec = 0;
1263   - itv.it_value.tv_usec = 10 * 1000;
1264   - setitimer(ITIMER_REAL, &itv, NULL);
1265   - /* we probe the tick duration of the kernel to inform the user if
1266   - the emulated kernel requested a too high timer frequency */
1267   - getitimer(ITIMER_REAL, &itv);
1268   - timer_ms = itv.it_interval.tv_usec / 1000;
1269   - pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
1270   - 1000000;
  1987 + gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
  1988 + qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
1271 1989  
1272 1990 if (use_gdbstub) {
1273   - cpu_gdbstub(NULL, main_loop, gdbstub_port);
  1991 + if (gdbserver_start(gdbstub_port) < 0) {
  1992 + fprintf(stderr, "Could not open gdbserver socket on port %d\n",
  1993 + gdbstub_port);
  1994 + exit(1);
  1995 + } else {
  1996 + printf("Waiting gdb connection on port %d\n", gdbstub_port);
  1997 + }
1274 1998 } else {
1275   - main_loop(NULL);
  1999 + vm_start();
1276 2000 }
  2001 + term_init();
  2002 + main_loop();
1277 2003 return 0;
1278 2004 }
... ...
... ... @@ -24,12 +24,12 @@
24 24 #ifndef VL_H
25 25 #define VL_H
26 26  
  27 +#include <time.h>
  28 +
27 29 #include "cpu.h"
28 30  
29 31 /* vl.c */
30 32 extern int reset_requested;
31   -extern int64_t ticks_per_sec;
32   -extern int pit_min_timer_count;
33 33  
34 34 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
35 35 typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
... ... @@ -38,7 +38,6 @@ int register_ioport_read(int start, int length, int size,
38 38 IOPortReadFunc *func, void *opaque);
39 39 int register_ioport_write(int start, int length, int size,
40 40 IOPortWriteFunc *func, void *opaque);
41   -int64_t cpu_get_ticks(void);
42 41 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
43 42  
44 43 void hw_error(const char *fmt, ...);
... ... @@ -51,6 +50,16 @@ char *pstrcat(char *buf, int buf_size, const char *s);
51 50  
52 51 int serial_open_device(void);
53 52  
  53 +extern int vm_running;
  54 +
  55 +typedef void VMStopHandler(void *opaque, int reason);
  56 +
  57 +int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque);
  58 +void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque);
  59 +
  60 +void vm_start(void);
  61 +void vm_stop(int reason);
  62 +
54 63 /* network redirectors support */
55 64  
56 65 #define MAX_NICS 8
... ... @@ -71,8 +80,112 @@ void net_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
71 80 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
72 81 typedef int IOCanRWHandler(void *opaque);
73 82  
74   -int add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
75   - IOReadHandler *fd_read, void *opaque);
  83 +int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
  84 + IOReadHandler *fd_read, void *opaque);
  85 +void qemu_del_fd_read_handler(int fd);
  86 +
  87 +/* timers */
  88 +
  89 +typedef struct QEMUClock QEMUClock;
  90 +typedef struct QEMUTimer QEMUTimer;
  91 +typedef void QEMUTimerCB(void *opaque);
  92 +
  93 +/* The real time clock should be used only for stuff which does not
  94 + change the virtual machine state, as it is run even if the virtual
  95 + machine is stopped. The real time clock has a frequency or 1000
  96 + Hz. */
  97 +extern QEMUClock *rt_clock;
  98 +
  99 +/* Rge virtual clock is only run during the emulation. It is stopped
  100 + when the virtual machine is stopped. Virtual timers use a high
  101 + precision clock, usually cpu cycles (use ticks_per_sec). */
  102 +extern QEMUClock *vm_clock;
  103 +
  104 +int64_t qemu_get_clock(QEMUClock *clock);
  105 +
  106 +QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
  107 +void qemu_free_timer(QEMUTimer *ts);
  108 +void qemu_del_timer(QEMUTimer *ts);
  109 +void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
  110 +int qemu_timer_pending(QEMUTimer *ts);
  111 +
  112 +extern int64_t ticks_per_sec;
  113 +extern int pit_min_timer_count;
  114 +
  115 +void cpu_enable_ticks(void);
  116 +void cpu_disable_ticks(void);
  117 +
  118 +/* VM Load/Save */
  119 +
  120 +typedef FILE QEMUFile;
  121 +
  122 +void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
  123 +void qemu_put_byte(QEMUFile *f, int v);
  124 +void qemu_put_be16(QEMUFile *f, unsigned int v);
  125 +void qemu_put_be32(QEMUFile *f, unsigned int v);
  126 +void qemu_put_be64(QEMUFile *f, uint64_t v);
  127 +int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
  128 +int qemu_get_byte(QEMUFile *f);
  129 +unsigned int qemu_get_be16(QEMUFile *f);
  130 +unsigned int qemu_get_be32(QEMUFile *f);
  131 +uint64_t qemu_get_be64(QEMUFile *f);
  132 +
  133 +static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
  134 +{
  135 + qemu_put_be64(f, *pv);
  136 +}
  137 +
  138 +static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
  139 +{
  140 + qemu_put_be32(f, *pv);
  141 +}
  142 +
  143 +static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
  144 +{
  145 + qemu_put_be16(f, *pv);
  146 +}
  147 +
  148 +static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
  149 +{
  150 + qemu_put_byte(f, *pv);
  151 +}
  152 +
  153 +static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
  154 +{
  155 + *pv = qemu_get_be64(f);
  156 +}
  157 +
  158 +static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
  159 +{
  160 + *pv = qemu_get_be32(f);
  161 +}
  162 +
  163 +static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
  164 +{
  165 + *pv = qemu_get_be16(f);
  166 +}
  167 +
  168 +static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
  169 +{
  170 + *pv = qemu_get_byte(f);
  171 +}
  172 +
  173 +int64_t qemu_ftell(QEMUFile *f);
  174 +int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
  175 +
  176 +typedef void SaveStateHandler(QEMUFile *f, void *opaque);
  177 +typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
  178 +
  179 +int qemu_loadvm(const char *filename);
  180 +int qemu_savevm(const char *filename);
  181 +int register_savevm(const char *idstr,
  182 + int instance_id,
  183 + int version_id,
  184 + SaveStateHandler *save_state,
  185 + LoadStateHandler *load_state,
  186 + void *opaque);
  187 +void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
  188 +void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
76 189  
77 190 /* block.c */
78 191 typedef struct BlockDriverState BlockDriverState;
... ... @@ -210,16 +323,11 @@ void kbd_init(void);
210 323  
211 324 /* mc146818rtc.c */
212 325  
213   -typedef struct RTCState {
214   - uint8_t cmos_data[128];
215   - uint8_t cmos_index;
216   - int irq;
217   -} RTCState;
218   -
219   -extern RTCState rtc_state;
  326 +typedef struct RTCState RTCState;
220 327  
221   -void rtc_init(int base, int irq);
222   -void rtc_timer(void);
  328 +RTCState *rtc_init(int base, int irq);
  329 +void rtc_set_memory(RTCState *s, int addr, int val);
  330 +void rtc_set_date(RTCState *s, const struct tm *tm);
223 331  
224 332 /* serial.c */
225 333  
... ... @@ -249,14 +357,17 @@ typedef struct PITChannelState {
249 357 uint8_t bcd; /* not supported */
250 358 uint8_t gate; /* timer start */
251 359 int64_t count_load_time;
252   - int64_t count_last_edge_check_time;
  360 + /* irq handling */
  361 + int64_t next_transition_time;
  362 + QEMUTimer *irq_timer;
  363 + int irq;
253 364 } PITChannelState;
254 365  
255 366 extern PITChannelState pit_channels[3];
256 367  
257   -void pit_init(int base);
  368 +void pit_init(int base, int irq);
258 369 void pit_set_gate(PITChannelState *s, int val);
259   -int pit_get_out(PITChannelState *s);
  370 +int pit_get_out(PITChannelState *s, int64_t current_time);
260 371 int pit_get_out_edges(PITChannelState *s);
261 372  
262 373 /* pc.c */
... ... @@ -271,4 +382,10 @@ void term_printf(const char *fmt, ...);
271 382 void term_flush(void);
272 383 void term_print_help(void);
273 384  
  385 +/* gdbstub.c */
  386 +
  387 +#define DEFAULT_GDBSTUB_PORT 1234
  388 +
  389 +int gdbserver_start(int port);
  390 +
274 391 #endif /* VL_H */
... ...