1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
|
/*
* plex86: run multiple x86 operating systems concurrently
* Copyright (C) 1999-2001 Kevin P. Lawton
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <signal.h>
#include <errno.h>
#include "plex86.h"
#include "user.h"
#include "plugin.h"
#include "decode.h"
/* TODO: pluginReadPhyMem doesn't deal with UCMem (VGA) */
/* #define RunMethod RunGuestNMethodBreakpoint */
/* #define RunMethod RunGuestNMethodEmulate */
#define RunMethod RunGuestNMethodExecute
/************************************************************************/
/* Structures / Variables */
/************************************************************************/
char *ptr = NULL; /* Pointer to the guest virtual phys mem */
char *printBuffer = NULL; /* mmap to monitor print buffer */
int filehdl; /* File handle for /dev/plex86 */
static unsigned char int_usage[256]; /* Interrupt usage count table */
static void vm_timer_handler(int i);
volatile Bit64u user_time_usec = 0;
static unsigned a20_val = 1;
unsigned guestPhyMemSize = 0;
unsigned vm_mem_updated = 0;
/************************************************************************/
/* Initialization and abort code */
/************************************************************************/
void
vm_kickstart (void)
{
/* open a new VM */
vm_init();
memset (int_usage, 0, 256);
/* initialize all plugins */
fprintf (stderr, "Initializing plugins\n");
/*plugin_init_all (); */
#if !COSIMULATE
{
struct sigaction sg_act;
memset(&sg_act, 0, sizeof(sg_act));
sg_act.sa_handler = vm_timer_handler;
sg_act.sa_flags = SA_RESTART;
sigaction(SIGALRM,&sg_act,NULL);
}
#endif
#if !COSIMULATE
{
struct itimerval timer;
memset(&timer, 0, sizeof(timer));
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 500000;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 500000;
setitimer(ITIMER_REAL, &timer, NULL);
}
#endif
}
void
vm_timer_handler(int i)
{
#warning "fix: need exclusive access to variable in async handler"
user_time_usec += 500000;
}
void
vm_abort(void)
{
static int was_waiting = 0;
if (! was_waiting && vm_conf.exit_wait > 0) {
struct timeval tv;
was_waiting = 1;
tv.tv_sec = vm_conf.exit_wait;
tv.tv_usec = 0;
printf("Delaying exit by %d seconds\n", (int)tv.tv_sec);
while (1) {
select(0, NULL, NULL, NULL, &tv);
if (errno == EINTR && tv.tv_sec > 0)
continue;
break;
}
}
/* deinitialize all plugins */
fprintf (stderr, "Shutting down plugins\n");
plugin_fini_all ();
/* shut down the virtual machine */
vm_fini ();
/* we're done!! */
#if COSIMULATE
{
void bx_dbg_exit(int);
bx_dbg_exit(1);
}
#endif
exit (0);
}
/************************************************************************/
/* VM initialization and deinitialization code */
/************************************************************************/
void
vm_open(void)
{
/* open a new VM */
fprintf (stderr, "Opening VM (/dev/plex86)\n");
filehdl = open ("/dev/misc/plex86", O_RDWR);
if (filehdl < 0)
{
/* Try the old name */
filehdl = open("/dev/plex86", O_RDWR);
if (filehdl < 0) {
fprintf(stderr, "Did you load the kernel module?"
" Read the toplevel README file!\n");
perror ("open");
exit (1);
}
}
}
void
vm_init_prescan_depth(unsigned depth)
{
/* Request any tweaks to the VM environment: */
if (vm_conf.prescanDepth) {
fprintf (stderr, "Setting prescan depth to %u\n", vm_conf.prescanDepth);
if (ioctl (filehdl, PLEX86_PRESCANDEPTH, vm_conf.prescanDepth) == -1) {
perror ("ioctl PRESCANDEPTH: ");
close (filehdl);
exit (1);
}
}
}
void
vm_init_prescan_ring3(unsigned request)
{
if (ioctl (filehdl, PLEX86_PRESCANRING3, vm_conf.prescanRing3) == -1) {
perror ("ioctl PRESCANRING3: ");
close (filehdl);
exit (1);
}
}
void
vm_init_memory(unsigned nmegs)
{
/* allocate memory from the host OS for the virtual physical memory */
fprintf (stderr, "Allocating %dMB of physical memory in VM\n", nmegs);
if (ioctl (filehdl, PLEX86_ALLOCVPHYS, nmegs) == -1)
{
perror ("ioctl ALLOCVPHYS: ");
close (filehdl);
exit (1);
}
/* map guest virtual physical memory into user address space and zero it */
fprintf (stderr, "Mapping virtualized physical memory into monitor\n");
ptr = mmap (NULL, nmegs * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_SHARED, filehdl, 0);
if (ptr == (void *) -1)
{
perror ("mmap of physical pages");
close (filehdl);
exit (1);
}
fprintf (stderr, "Zeroing virtualized physical memory\n");
guestPhyMemSize = nmegs * 1024 * 1024;
memset(ptr, 0, guestPhyMemSize);
/* Create a memory mapping of the monitor's print buffer into
* user memory. This is used for efficient printing of info that
* the monitor prints out.
*/
fprintf (stderr, "Mapping monitor print buffer into user mem.\n");
printBuffer = mmap(NULL, 4096, PROT_READ,
MAP_SHARED, filehdl, nmegs*1024*1024);
if (printBuffer == (void *) -1)
{
perror ("mmap of monitor print buffer");
close (filehdl);
exit (1);
}
}
void
vm_init(void)
{
}
void
vm_fini (void)
{
/* unmap the guest's virtual physical memory */
fprintf (stderr, "Unmapping guest physical memory.\n");
if (munmap (ptr, vm_conf.max_memory * 1024 * 1024) != 0)
{
perror ("munmap of guest physical memory");
exit (1);
}
fprintf (stderr, "Unmapping monitor print buffer.\n");
if (munmap (printBuffer, 4096) != 0)
{
perror ("munmap of monitor print buffer.");
exit (1);
}
/* tell kernel module to clean up the VM */
fprintf (stderr, "Tearing down VM\n");
if (ioctl (filehdl, PLEX86_TEARDOWN, 0) == -1)
{
perror ("ioctl TEARDOWN: ");
exit (1);
}
/* close the connection to the kernel module */
fprintf (stderr, "Closing VM\n");
if (close (filehdl) == -1)
{
perror ("close\n");
exit (1);
}
}
#if 0
#if USE_LOADER
/* Special loader stuff only I use - KPL */
#include "../../loader/loader_misc.h"
special_loader_misc_t loader_misc;
char *special_loader_fname = NULL;
#endif
#endif
/************************************************************************/
/* The main event loop */
/************************************************************************/
void
vm_event_loop (void)
{
vm_messages_t user_msgs;
run_guest_n_t *run_guest_n_p;
#if 0
#if USE_LOADER
/* Special loader stuff only I use - KPL */
special_loader(special_loader_fname, &loader_misc, ptr);
#endif
#endif
/* Build message to run guest */
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p = (run_guest_n_t *) user_msgs.msg;
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
for (;;)
{
#if 0
// xxx Only needed for DT mode
/* First check if physical memory has been updated. If it has,
* we need to tell the monitor about it.
*/
if (vm_mem_updated) {
if ( ioctl(filehdl, PLEX86_PHYMEM_MOD, 0) ) {
perror("ioctl PHYMEM_MOD:");
vm_abort();
}
vm_mem_updated = 0;
}
#endif
if (ioctl (filehdl, PLEX86_MESSAGEQ, &user_msgs) == -1)
{
perror ("ioctl MESSSAGEQ: ");
vm_abort();
}
switch (user_msgs.header.msg_type)
{
case VMMessageNone:
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
break;
case VMMessagePanic:
fprintf(stderr, "Fatal monitor error caused Panic\n");
vm_debug_exception();
vm_abort();
break;
case VMMessagePrintBuf:
{
fprintf(stderr, "::%s\n", printBuffer);
/* No response required */
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
break;
}
case VMMessageIOInRequest:
{
IO_msg_t *io_msg = (IO_msg_t *) user_msgs.msg;
ioReadHandler_t handler = (ioReadHandler_t) io_msg->callback;
/* Zero out data, since it's potentially bigger in size than
* the IO operation we will request
*/
io_msg->data = 0; // xxx Not needed because of assignment
io_msg->data =
handler(io_msg->thisPtr, io_msg->port, io_msg->len);
/* Change message type; most data stays the same */
user_msgs.header.msg_type = VMMessageIOInResponse;
if (pluginHRQ && pluginHRQHackCallback)
pluginHRQHackCallback();
break;
}
case VMMessageIOBatchRequest:
{
IOBatch_msg_t *msg = (IOBatch_msg_t *) user_msgs.msg;
unsigned i;
#warning "put physical mem limit checks here"
if (msg->op == IO_IN) {
ioReadHandler_t handler = (ioReadHandler_t) msg->callback;
if (msg->len == 1) {
Bit8u *phyMemPtr = &ptr[msg->paddr];
for (i=0; i<msg->n; i++)
*phyMemPtr++ = handler(msg->thisPtr, msg->port, msg->len);
}
else if (msg->len == 2) {
Bit16u *phyMemPtr = (Bit16u*) &ptr[msg->paddr];
for (i=0; i<msg->n; i++)
*phyMemPtr++ = handler(msg->thisPtr, msg->port, msg->len);
}
else if (msg->len == 4) {
Bit32u *phyMemPtr = (Bit32u*) &ptr[msg->paddr];
for (i=0; i<msg->n; i++)
*phyMemPtr++ = handler(msg->thisPtr, msg->port, msg->len);
}
else {
fprintf(stderr, "IOBatchRequest(In): len=%u.\n", msg->len);
vm_abort();
}
}
else { /* IO_OUT */
ioWriteHandler_t handler = (ioWriteHandler_t) msg->callback;
if (msg->len == 1) {
Bit8u *phyMemPtr = &ptr[msg->paddr];
for (i=0; i<msg->n; i++)
handler(msg->thisPtr, msg->port, *phyMemPtr++, msg->len);
}
else if (msg->len == 2) {
Bit16u *phyMemPtr = (Bit16u*) &ptr[msg->paddr];
for (i=0; i<msg->n; i++)
handler(msg->thisPtr, msg->port, *phyMemPtr++, msg->len);
}
else if (msg->len == 4) {
Bit32u *phyMemPtr = (Bit32u*) &ptr[msg->paddr];
for (i=0; i<msg->n; i++)
handler(msg->thisPtr, msg->port, *phyMemPtr++, msg->len);
}
else {
fprintf(stderr, "IOBatchRequest(Out): len=%u.\n", msg->len);
vm_abort();
}
}
msg->n = msg->n; /* xxx Should be result of call above */
/* Change message type; most data stays the same */
user_msgs.header.msg_type = VMMessageIOBatchResponse;
if (pluginHRQ && pluginHRQHackCallback)
pluginHRQHackCallback();
break;
}
case VMMessageIOOutRequest:
{
IO_msg_t *io_msg = (IO_msg_t *) user_msgs.msg;
ioWriteHandler_t handler;
handler = (ioWriteHandler_t) io_msg->callback;
handler(io_msg->thisPtr, io_msg->port, io_msg->data, io_msg->len);
/* No response required */
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
if (pluginHRQ && pluginHRQHackCallback)
pluginHRQHackCallback();
break;
}
case VMMessageMemMapIOReadRequest:
{
memMapIO_msg_t *msg = (memMapIO_msg_t *) user_msgs.msg;
/* Zero out data, since it's potentially bigger in size than
* the IO operation we will request
*/
msg->data = 0;
if (memMapFunct) {
memMapFunct(msg->addr, msg->len, IO_IN, &msg->data);
}
else {
fprintf(stderr, "MemMapIOWriteRequest: not handled.\n");
vm_abort();
}
/* Change message type; most data stays the same */
user_msgs.header.msg_type = VMMessageMemMapIOReadResponse;
break;
}
case VMMessageMemMapIOWriteRequest:
{
memMapIO_msg_t *msg = (memMapIO_msg_t *) user_msgs.msg;
if (memMapFunct) {
memMapFunct(msg->addr, msg->len, IO_OUT, &msg->data);
}
else {
fprintf(stderr, "MemMapIOWriteRequest: not handled.\n");
vm_abort();
}
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
break;
}
case VMMessageIntRequest:
{
INT_msg_t *int_msg = (INT_msg_t *) user_msgs.msg;
int_msg->reflect = plugin_emulate_int (int_msg->vector);
user_msgs.header.msg_type = VMMessageIntResponse;
break;
}
case VMMessageTimeElapsed:
{
void do_timer(Bit64u elapsed);
timerCallback_t *timerCallback = (timerCallback_t *) user_msgs.msg;
void (*callback)(void *);
callback = timerCallback->callback;
callback(timerCallback->thisPtr);
//fputc('0' + timerCallback->callbackID, stderr); // xxx Delete this
/* No response required */
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
break;
}
case VMMessageDisasm:
vm_debug_exception();
user_msgs.header.msg_type = VMMessageRunGuestN;
user_msgs.header.msg_len = sizeof(run_guest_n_t);
run_guest_n_p->icount = ICOUNT_INDEFINITE;
run_guest_n_p->method = RunMethod;
fprintf(stderr, "> ");
getchar();
break;
default:
fprintf(stderr, "VMMessage type %u not handled yet\n",
user_msgs.header.msg_type);
vm_abort();
break;
}
plugin_handle_periodic();
}
}
// xxx Move to plugin.c
int
pluginRegisterTimer(void *this_ptr, void (*funct)(void *),
Bit32u useconds, Boolean continuous, Boolean active)
{
int id;
timerRegister_t timer;
timer.thisPtr = this_ptr;
timer.callback = funct;
timer.useconds = useconds;
timer.continuous = continuous;
timer.active = active;
id = ioctl(filehdl, PLEX86_REGTIMER, &timer);
if (id == -1) {
perror ("ioctl PLEX86_REGTIMER: ");
vm_abort();
}
return( id );
}
void
pluginActivateTimer(unsigned id, Bit32u usec, Boolean continuous)
{
int ret;
timerRegister_t timer;
timer.thisPtr = 0;
timer.callback = (void *) id;
timer.useconds = usec;
timer.continuous = continuous;
timer.active = 1;
ret = ioctl(filehdl, PLEX86_ACTTIMER, &timer);
if (ret == -1) {
perror ("ioctl PLEX86_ACTTIMER: ");
vm_abort();
}
}
void
pluginDeactivateTimer(unsigned id)
{
int ret;
ret = ioctl(filehdl, PLEX86_DEACTTIMER, id);
if (ret == -1) {
perror ("ioctl PLEX86_DEACTTIMER: ");
vm_abort();
}
}
/************************************************************************/
/* Interrupt stuff */
/************************************************************************/
int
vm_alloc_intr (int intr)
{
if (intr < 0 || intr > 255)
return 1;
if (ioctl (filehdl, PLEX86_ALLOCINT, intr) == -1)
return 1;
int_usage[intr]++;
return 0;
}
int
vm_release_intr (int intr)
{
if (intr < 0 || intr > 255)
return 1;
if (--int_usage[intr])
return 0;
if (ioctl (filehdl, PLEX86_RELEASEINT, intr) == -1)
return 1;
return 0;
}
void
vm_set_cpu(guest_cpu_t *guest_cpu)
{
if (ioctl (filehdl, PLEX86_SET_CPU, guest_cpu)) {
perror ("ioctl SET_CPU: \n");
close (filehdl);
exit (1);
}
}
void
vm_get_cpu(guest_cpu_t *cpu)
{
if (ioctl(filehdl, PLEX86_GET_CPU, cpu)) {
perror("ioctl GET_CPU:");
close(filehdl);
exit(1);
}
}
/************************************************************************/
/* Guest memory access */
/************************************************************************/
int
pluginReadPhyMem(Bit32u address, unsigned length, void *data)
{
if ( address+length >= vm_conf.max_memory*1024*1024 )
return 1;
memcpy (data, ptr+address, length);
return 0;
}
int
pluginWritePhyMem(Bit32u address, unsigned length, void *data)
{
if ( address+length >= vm_conf.max_memory*1024*1024 )
return 1;
memcpy(((char *)ptr)+address, data, length);
vm_mem_updated = 1;
#if 0
// xxx Delete this
if ( ioctl(filehdl, PLEX86_PHYMEM_MOD, 0) ) {
perror("ioctl PHYMEM_MOD:");
close(filehdl);
exit(1);
}
#endif
return 0;
}
/************************************************************************/
/* Debug event handler */
/************************************************************************/
int
vm_debug_exception(void)
{
guest_cpu_t cpu;
struct i386_context ctx;
struct i386_decode instr;
int i, len = 8;
char prefix[256], text[256];
Bit8u *seg_base_ptr;
#if COSIMULATE
bx_print_last_sync_icount();
#endif
/* get guest context */
vm_get_cpu(&cpu);
#define LowDWord(d) ( ((Bit32u*) (&(d)))[0] )
#define HighDWord(d) ( ((Bit32u*) (&(d)))[1] )
/* Dump state of CPU */
fprintf(stderr, "eax:0x%x\n", (unsigned) cpu.eax);
fprintf(stderr, "ebx:0x%x\n", (unsigned) cpu.ebx);
fprintf(stderr, "ecx:0x%x\n", (unsigned) cpu.ecx);
fprintf(stderr, "edx:0x%x\n", (unsigned) cpu.edx);
fprintf(stderr, "ebp:0x%x\n", (unsigned) cpu.ebp);
fprintf(stderr, "esi:0x%x\n", (unsigned) cpu.esi);
fprintf(stderr, "edi:0x%x\n", (unsigned) cpu.edi);
fprintf(stderr, "esp:0x%x\n", (unsigned) cpu.esp);
fprintf(stderr, "eflags:0x%x\n", (unsigned) cpu.eflags);
fprintf(stderr, "eip:0x%x\n", (unsigned) cpu.eip);
fprintf(stderr, "cs:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.cs.sel.raw, LowDWord(cpu.cs.des),
(unsigned) HighDWord(cpu.cs.des), (unsigned) cpu.cs.valid);
fprintf(stderr, "ss:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.ss.sel.raw, (unsigned) LowDWord(cpu.ss.des),
(unsigned) HighDWord(cpu.ss.des), (unsigned) cpu.ss.valid);
fprintf(stderr, "ds:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.ds.sel.raw, (unsigned) LowDWord(cpu.ds.des),
(unsigned) HighDWord(cpu.ds.des), (unsigned) cpu.ds.valid);
fprintf(stderr, "es:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.es.sel.raw, (unsigned) LowDWord(cpu.es.des),
(unsigned) HighDWord(cpu.es.des), (unsigned) cpu.es.valid);
fprintf(stderr, "fs:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.fs.sel.raw, (unsigned) LowDWord(cpu.fs.des),
(unsigned) HighDWord(cpu.fs.des), (unsigned) cpu.fs.valid);
fprintf(stderr, "gs:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.gs.sel.raw, (unsigned) LowDWord(cpu.gs.des),
(unsigned) HighDWord(cpu.gs.des), (unsigned) cpu.gs.valid);
fprintf(stderr, "ldtr:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.ldtr.sel.raw, (unsigned) LowDWord(cpu.ldtr.des),
(unsigned) HighDWord(cpu.ldtr.des), (unsigned) cpu.ldtr.valid);
fprintf(stderr, "tr:s=0x%x, dl=0x%x, dh=0x%x, valid=%u\n",
(unsigned) cpu.tr.sel.raw, (unsigned) LowDWord(cpu.tr.des),
(unsigned) HighDWord(cpu.tr.des), (unsigned) cpu.tr.valid);
fprintf(stderr, "gdtr:base=0x%x, limit=0x%x\n",
(unsigned) cpu.gdtr.base, (unsigned) cpu.gdtr.limit);
fprintf(stderr, "idtr:base=0x%x, limit=0x%x\n",
(unsigned) cpu.idtr.base, (unsigned) cpu.idtr.limit);
fprintf(stderr, "dr0:0x%x\n", (unsigned) cpu.dr0);
fprintf(stderr, "dr1:0x%x\n", (unsigned) cpu.dr1);
fprintf(stderr, "dr2:0x%x\n", (unsigned) cpu.dr2);
fprintf(stderr, "dr3:0x%x\n", (unsigned) cpu.dr3);
fprintf(stderr, "dr6:0x%x\n", (unsigned) cpu.dr6);
fprintf(stderr, "dr7:0x%x\n", (unsigned) cpu.dr7);
fprintf(stderr, "tr3:0x%x\n", (unsigned) cpu.tr3);
fprintf(stderr, "tr4:0x%x\n", (unsigned) cpu.tr4);
fprintf(stderr, "tr5:0x%x\n", (unsigned) cpu.tr5);
fprintf(stderr, "tr6:0x%x\n", (unsigned) cpu.tr6);
fprintf(stderr, "tr7:0x%x\n", (unsigned) cpu.tr7);
fprintf(stderr, "cr0:0x%x\n", (unsigned) cpu.cr0);
fprintf(stderr, "cr1:0x%x\n", (unsigned) cpu.cr1);
fprintf(stderr, "cr2:0x%x\n", (unsigned) cpu.cr2);
fprintf(stderr, "cr3:0x%x\n", (unsigned) cpu.cr3);
fprintf(stderr, "cr4:0x%x\n", (unsigned) cpu.cr4);
fprintf(stderr, "inhibit_mask:%u\n", cpu.inhibit_mask);
/* dump the guest context */
memset (&ctx, 0, sizeof (ctx));
memset (&instr, 0, sizeof (instr));
if (!cpu.cs.valid) {
fprintf(stderr, "vm_debug_exception: CS.valid=0\n");
return 0;
}
if (cpu.cr0 & 1) {
if (cpu.eflags & 0x20000)
ctx.mode = VM86;
else if (cpu.cs.des.d_b)
ctx.mode = CODE32;
else
ctx.mode = CODE16;
}
else { /* Real Mode */
ctx.mode = VM86;
}
seg_base_ptr = ptr + BaseOfDescriptor(cpu.cs.des);
ctx.base = seg_base_ptr;
ctx.segment = cpu.cs.sel.raw;
ctx.offset = cpu.eip;
//if ( (BaseOfDescriptor(cpu.cs.des) + cpu.eip) > guestPhyMemSize ) {
// return 0;
// }
if (!i386_decode (&ctx, &instr) || !i386_decode_text (&ctx, &instr, text, vm_conf.syntax))
strcpy (text, "???");
else
len = instr.length;
sprintf (prefix, "%04X.%08X ", cpu.cs.sel.raw, cpu.eip);
for (i = 0; i < len && i < 12; i++)
sprintf (prefix + strlen (prefix), "%02X",
((unsigned char *) seg_base_ptr)[cpu.eip + i]);
for (; i < 12; i++)
strcat (prefix, " ");
fprintf (stderr, "Stack dump:\n");
#if 0
for (i = 0; i < 4; i++)
fprintf (stderr, " %08x: %08lx %08lx %08lx %08lx\n",
context.esp + 16 * i,
*(unsigned long *) (ptr + context.esp + 16 * i),
*(unsigned long *) (ptr + context.esp + 16 * i + 4),
*(unsigned long *) (ptr + context.esp + 16 * i + 8),
*(unsigned long *) (ptr + context.esp + 16 * i + 12));
fprintf (stderr, "\n");
#endif
fprintf (stderr, "Current instruction:\n");
fprintf (stderr, " %s %s\n", prefix, text);
fprintf (stderr, "\n");
return 1;
}
void
vm_register_callback(const char *command, void (*f)(unsigned char *))
{
callback_command_t *entry;
entry = malloc( sizeof(callback_command_t) );
if (!entry) {
fprintf(stderr, "vm_register_callback: malloc failed\n");
exit(1);
}
entry->next = callback_command_list;
callback_command_list = entry;
entry->command = command;
entry->f = f;
}
unsigned
vm_load_rom(const char *path, Bit32u address)
{
struct stat stat_buf;
int fileno;
unsigned char *image;
fileno = open(path, O_RDONLY);
if (fileno < 0) {
fprintf(stderr, "rom: open %s: %s\n", path, strerror(errno));
return 1;
}
if (fstat(fileno, &stat_buf) != 0) {
fprintf(stderr, "rom: fstat %s: %s\n", path, strerror(errno));
return 1;
}
fprintf(stderr, "ROM: loading image '%s' @ 0x%x (%u bytes)\n",
path, address, (unsigned) stat_buf.st_size);
if ((image = (void *) malloc (stat_buf.st_size)) == NULL) {
perror("rom: malloc");
return 1;
}
if (read (fileno, image, stat_buf.st_size) != stat_buf.st_size) {
perror ("rom: read");
return 1;
}
close(fileno);
if (pluginWritePhyMem(address, stat_buf.st_size, image)) {
fprintf (stderr, "rom: trying to load beyond available VM memory.\n");
return 1;
}
free(image);
return 0;
}
unsigned
linux_hack(void)
{
Bit32u data;
/* +++ Enable A20 line */
/*BX_SET_ENABLE_A20( 1 ); */
/* Setup PICs the way Linux likes it */
data = 0x11; plugin_emulate_outport( 0x20, 1, 1, &data);
data = 0x11; plugin_emulate_outport( 0xA0, 1, 1, &data);
data = 0x20; plugin_emulate_outport( 0x21, 1, 1, &data);
data = 0x28; plugin_emulate_outport( 0xA1, 1, 1, &data);
data = 0x04; plugin_emulate_outport( 0x21, 1, 1, &data);
data = 0x02; plugin_emulate_outport( 0xA1, 1, 1, &data);
data = 0x01; plugin_emulate_outport( 0x21, 1, 1, &data);
data = 0x01; plugin_emulate_outport( 0xA1, 1, 1, &data);
data = 0xFF; plugin_emulate_outport( 0x21, 1, 1, &data);
data = 0xFB; plugin_emulate_outport( 0xA1, 1, 1, &data);
/* Disable interrupts and NMIs */
/*BX_CPU_THIS_PTR eflags.if_ = 0; */
data = 0x80; plugin_emulate_outport( 0x70, 1, 1, &data);
/* Enter protected mode */
/*BX_CPU_THIS_PTR cr0.pe = 1; */
/* Set up initial GDT */
/*BX_CPU_THIS_PTR gdtr.limit = 0x400; */
/*BX_CPU_THIS_PTR gdtr.base = 0x00090400; */
/* Jump to protected mode entry point */
/*BX_CPU_THIS_PTR jump_protected( NULL, 0x10, 0x00100000 ); */
return 0;
}
unsigned
pluginGetA20E(void)
{
return a20_val > 0;
}
void
pluginSetA20E(unsigned val)
{
a20_val = val;
if (ioctl(filehdl, PLEX86_SET_A20, val)) {
perror("ioctl SET_A20: ");
vm_abort();
}
}
|