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
|
/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2001-2018 The Bochs Project
//
// 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., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
/////////////////////////////////////////////////////////////////////////
#define NEED_CPU_REG_SHORTCUTS 1
#include "bochs.h"
#include "cpu.h"
#define LOG_THIS BX_CPU_THIS_PTR
#include "memory/memory-bochs.h"
#include "pc_system.h"
#include "cpustats.h"
#include "bx_debug/debug.h"
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
#define BX_SYNC_TIME_IF_SINGLE_PROCESSOR(allowed_delta) { \
if (BX_SMP_PROCESSORS == 1) { \
Bit32u delta = (Bit32u)(BX_CPU_THIS_PTR icount - BX_CPU_THIS_PTR icount_last_sync); \
if (delta >= allowed_delta) { \
BX_CPU_THIS_PTR sync_icount(); \
BX_TICKN(delta); \
} \
} \
}
#else
#define BX_SYNC_TIME_IF_SINGLE_PROCESSOR(allowed_delta) \
if (BX_SMP_PROCESSORS == 1) BX_TICK1()
#endif
jmp_buf BX_CPU_C::jmp_buf_env;
#if BX_DEBUGGER
void BX_CPU_C::cpu_loop_debugger(void)
{
BX_CPU_THIS_PTR break_point = 0;
BX_CPU_THIS_PTR magic_break = 0;
BX_CPU_THIS_PTR stop_reason = STOP_NO_REASON;
if (setjmp(BX_CPU_THIS_PTR jmp_buf_env)) {
// can get here only from exception function or VMEXIT
BX_CPU_THIS_PTR icount++;
if (BX_SMP_PROCESSORS == 1) BX_TICK1();
if (dbg_instruction_epilog()) return;
}
// If the exception() routine has encountered a nasty fault scenario,
// the debugger may request that control is returned to it so that
// the situation may be examined.
if (bx_guard.interrupt_requested) return;
// We get here either by a normal function call, or by a longjmp
// back from an exception() call. In either case, commit the
// new EIP/ESP, and set up other environmental fields. This code
// mirrors similar code below, after the interrupt() call.
BX_CPU_THIS_PTR prev_rip = RIP; // commit new EIP
BX_CPU_THIS_PTR speculative_rsp = false;
while (1) {
// check on events which occurred for previous instructions (traps)
// and ones which are asynchronous to the CPU (hardware interrupts)
Bit32u handle_event = BX_CPU_THIS_PTR async_event & ~BX_ASYNC_EVENT_STOP_TRACE;
if (handle_event) {
if (handleAsyncEvent()) {
// If request to return to caller ASAP.
return;
}
}
// stop tracing after every instruction to handle in internal debugger
BX_CPU_THIS_PTR async_event |= BX_ASYNC_EVENT_STOP_TRACE;
bxICacheEntry_c *entry = getICacheEntry();
bxInstruction_c *i = entry->i;
bxInstruction_c *last = i + (entry->tlen);
for(;;) {
if (BX_CPU_THIS_PTR trace)
debug_disasm_instruction(BX_CPU_THIS_PTR prev_rip);
// want to allow changing of the instruction inside instrumentation callback
BX_INSTR_BEFORE_EXECUTION(BX_CPU_ID, i);
RIP += i->ilen();
BX_CPU_CALL_METHOD(i->execute1, (i)); // might iterate repeat instruction
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS == 0
BX_CPU_THIS_PTR prev_rip = RIP; // commit new RIP
BX_INSTR_AFTER_EXECUTION(BX_CPU_ID, i);
BX_CPU_THIS_PTR icount++;
#endif
if (BX_SMP_PROCESSORS == 1) BX_TICK1();
// note instructions generating exceptions never reach this point
if (dbg_instruction_epilog()) return;
if (BX_CPU_THIS_PTR async_event & ~BX_ASYNC_EVENT_STOP_TRACE) break;
if (++i == last) {
entry = getICacheEntry();
i = entry->i;
last = i + (entry->tlen);
}
}
} // while (1)
}
#endif // BX_DEBUGGER
void BX_CPU_C::cpu_loop(void)
{
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
volatile Bit8u stack_anchor = 0;
BX_CPU_THIS_PTR cpuloop_stack_anchor = &stack_anchor;
#endif
#if BX_DEBUGGER
BX_ASSERT(! bx_dbg.debugger_active);
#endif
if (setjmp(BX_CPU_THIS_PTR jmp_buf_env)) {
// can get here only from exception function or VMEXIT
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(0);
#if BX_GDBSTUB
if (gdbstub_instruction_epilog() || bx_dbg.gdbstub_enabled) return;
#endif
}
// We get here either by a normal function call, or by a longjmp
// back from an exception() call. In either case, commit the
// new EIP/ESP, and set up other environmental fields. This code
// mirrors similar code below, after the interrupt() call.
BX_CPU_THIS_PTR prev_rip = RIP; // commit new EIP
BX_CPU_THIS_PTR speculative_rsp = false;
#if BX_SUPPORT_CET && BX_SUPPORT_VMX
if (BX_CPU_THIS_PTR in_vmx_guest) {
VMCS_CACHE *vm = &BX_CPU_THIS_PTR vmcs;
if (vm->shadow_stack_prematurely_busy)
BX_PANIC(("Shadow stack prematurely busy is left set !"));
vm->shadow_stack_prematurely_busy = false; // for safety
}
#endif
while (1) {
// check on events which occurred for previous instructions (traps)
// and ones which are asynchronous to the CPU (hardware interrupts)
if (BX_CPU_THIS_PTR async_event) {
if (handleAsyncEvent()) {
// If request to return to caller ASAP.
return;
}
}
bxICacheEntry_c *entry = getICacheEntry();
bxInstruction_c *i = entry->i;
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
for(;;) {
// want to allow changing of the instruction inside instrumentation callback
BX_INSTR_BEFORE_EXECUTION(BX_CPU_ID, i);
RIP += i->ilen();
// when handlers chaining is enabled this single call will execute entire trace
BX_CPU_CALL_METHOD(i->execute1, (i)); // might iterate repeat instruction
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(0);
if (BX_CPU_THIS_PTR async_event) break;
i = getICacheEntry()->i;
}
#else // BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS == 0
bxInstruction_c *last = i + (entry->tlen);
for(;;) {
// want to allow changing of the instruction inside instrumentation callback
BX_INSTR_BEFORE_EXECUTION(BX_CPU_ID, i);
RIP += i->ilen();
BX_CPU_CALL_METHOD(i->execute1, (i)); // might iterate repeat instruction
BX_CPU_THIS_PTR prev_rip = RIP; // commit new RIP
BX_INSTR_AFTER_EXECUTION(BX_CPU_ID, i);
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(0);
// note instructions generating exceptions never reach this point
#if BX_GDBSTUB
if (gdbstub_instruction_epilog()) return;
#endif
if (BX_CPU_THIS_PTR async_event) break;
if (++i == last) {
entry = getICacheEntry();
i = entry->i;
last = i + (entry->tlen);
}
}
#endif
// clear stop trace magic indication that probably was set by repeat or branch32/64
BX_CPU_THIS_PTR async_event &= ~BX_ASYNC_EVENT_STOP_TRACE;
} // while (1)
}
#if BX_SUPPORT_SMP
void BX_CPU_C::cpu_run_trace(void)
{
// check on events which occurred for previous instructions (traps)
// and ones which are asynchronous to the CPU (hardware interrupts)
if (BX_CPU_THIS_PTR async_event) {
if (handleAsyncEvent()) {
// If request to return to caller ASAP.
return;
}
}
bxICacheEntry_c *entry = getICacheEntry();
bxInstruction_c *i = entry->i;
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
// want to allow changing of the instruction inside instrumentation callback
BX_INSTR_BEFORE_EXECUTION(BX_CPU_ID, i);
RIP += i->ilen();
// when handlers chaining is enabled this single call will execute entire trace
BX_CPU_CALL_METHOD(i->execute1, (i)); // might iterate repeat instruction
if (BX_CPU_THIS_PTR async_event) {
// clear stop trace magic indication that probably was set by repeat or branch32/64
BX_CPU_THIS_PTR async_event &= ~BX_ASYNC_EVENT_STOP_TRACE;
}
#else
bxInstruction_c *last = i + (entry->tlen);
for(;;) {
// want to allow changing of the instruction inside instrumentation callback
BX_INSTR_BEFORE_EXECUTION(BX_CPU_ID, i);
RIP += i->ilen();
BX_CPU_CALL_METHOD(i->execute1, (i)); // might iterate repeat instruction
BX_CPU_THIS_PTR prev_rip = RIP; // commit new RIP
BX_INSTR_AFTER_EXECUTION(BX_CPU_ID, i);
BX_CPU_THIS_PTR icount++;
if (BX_CPU_THIS_PTR async_event) {
// clear stop trace magic indication that probably was set by repeat or branch32/64
BX_CPU_THIS_PTR async_event &= ~BX_ASYNC_EVENT_STOP_TRACE;
break;
}
if (++i == last) break;
}
#endif // BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS
}
#endif
#include "decoder/ia_opcodes.h"
bxICacheEntry_c* BX_CPU_C::getICacheEntry(void)
{
bx_address eipBiased = RIP + BX_CPU_THIS_PTR eipPageBias;
if (eipBiased >= BX_CPU_THIS_PTR eipPageWindowSize) {
prefetch();
eipBiased = RIP + BX_CPU_THIS_PTR eipPageBias;
}
INC_ICACHE_STAT(iCacheLookups);
bx_phy_address pAddr = BX_CPU_THIS_PTR pAddrFetchPage + eipBiased;
bxICacheEntry_c *entry = BX_CPU_THIS_PTR iCache.find_entry(pAddr, BX_CPU_THIS_PTR fetchModeMask);
if (entry == NULL)
{
// iCache miss. No validated instruction with matching fetch parameters
// is in the iCache.
INC_ICACHE_STAT(iCacheMisses);
entry = serveICacheMiss((Bit32u) eipBiased, pAddr);
}
#if BX_SUPPORT_CET
if (WaitingForEndbranch(CPL)) {
bxInstruction_c *i = entry->i;
if (i->getIaOpcode() != (long64_mode() ? BX_IA_ENDBRANCH64 : BX_IA_ENDBRANCH32) && i->getIaOpcode() != BX_IA_INT3) {
if (LegacyEndbranchTreatment(CPL)) {
BX_ERROR(("Endbranch is expected for CPL=%d", CPL));
exception(BX_CP_EXCEPTION, BX_CP_ENDBRANCH);
}
}
}
#endif
return entry;
}
#if BX_SUPPORT_HANDLERS_CHAINING_SPEEDUPS && BX_ENABLE_TRACE_LINKING
// The function is called after taken branch instructions and tries to link the branch to the next trace
void BX_CPP_AttrRegparmN(1) BX_CPU_C::linkTrace(bxInstruction_c *i)
{
volatile Bit8u stack_anchor = 0;
if (bx_dbg.debugger_active)
return;
#if BX_SUPPORT_SMP
if (BX_SMP_PROCESSORS > 1)
return;
#endif
#define BX_HANDLERS_CHAINING_MAX_LINK_DEPTH 1000
// do not allow extreme trace link depth / avoid host stack overflow
// (could happen with badly compiled instruction handlers)
static Bit32u linkDepth = 0;
if (BX_CPU_THIS_PTR async_event || ++linkDepth > BX_HANDLERS_CHAINING_MAX_LINK_DEPTH) {
linkDepth = 0;
return;
}
#define BX_HANDLERS_CHAINING_MAX_STACK_DEPTH 0x10000
size_t stack_depth = BX_CPU_THIS_PTR cpuloop_stack_anchor - &stack_anchor;
if (stack_depth > BX_HANDLERS_CHAINING_MAX_STACK_DEPTH) {
linkDepth = 0;
return;
}
Bit32u delta = (Bit32u) (BX_CPU_THIS_PTR icount - BX_CPU_THIS_PTR icount_last_sync);
if(delta >= bx_pc_system.getNumCpuTicksLeftNextEvent()) {
linkDepth = 0;
return;
}
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(0);
bxInstruction_c *next = i->getNextTrace(BX_CPU_THIS_PTR iCache.traceLinkTimeStamp);
if (next) {
BX_EXECUTE_INSTRUCTION(next);
return;
}
bx_address eipBiased = RIP + BX_CPU_THIS_PTR eipPageBias;
if (eipBiased >= BX_CPU_THIS_PTR eipPageWindowSize) {
prefetch();
eipBiased = RIP + BX_CPU_THIS_PTR eipPageBias;
}
INC_ICACHE_STAT(iCacheLookups);
bx_phy_address pAddr = BX_CPU_THIS_PTR pAddrFetchPage + eipBiased;
bxICacheEntry_c *entry = BX_CPU_THIS_PTR iCache.find_entry(pAddr, BX_CPU_THIS_PTR fetchModeMask);
if (entry != NULL) // link traces - handle only hit cases
{
i->setNextTrace(entry->i, BX_CPU_THIS_PTR iCache.traceLinkTimeStamp);
i = entry->i;
BX_EXECUTE_INSTRUCTION(i);
}
}
#endif
#define BX_REPEAT_TIME_UPDATE_INTERVAL (BX_MAX_TRACE_LENGTH-1)
void BX_CPP_AttrRegparmN(2) BX_CPU_C::repeat(bxInstruction_c *i, BxRepIterationPtr_tR execute)
{
// non repeated instruction
if (! i->repUsedL()) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
return;
}
BX_ASSERT(! bx_dbg.debugger_active || BX_CPU_THIS_PTR async_event);
BX_CPU_THIS_PTR clear_RF();
#if BX_SUPPORT_X86_64
if (i->as64L()) {
while(1) {
if (RCX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
RCX --;
}
if (RCX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
else
#endif
if (i->as32L()) {
while(1) {
if (ECX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
RCX = ECX - 1;
}
if (ECX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
else // 16bit addrsize
{
while(1) {
if (CX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
CX --;
}
if (CX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
BX_CPU_THIS_PTR assert_RF();
RIP = BX_CPU_THIS_PTR prev_rip; // repeat loop not done, restore RIP
// assert magic async_event to stop trace execution
BX_CPU_THIS_PTR async_event |= BX_ASYNC_EVENT_STOP_TRACE;
}
void BX_CPP_AttrRegparmN(2) BX_CPU_C::repeat_ZF(bxInstruction_c *i, BxRepIterationPtr_tR execute)
{
unsigned rep = i->lockRepUsedValue();
// non repeated instruction
if (rep < 2) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
return;
}
BX_CPU_THIS_PTR clear_RF();
if (rep == 3) { /* repeat prefix 0xF3 */
#if BX_SUPPORT_X86_64
if (i->as64L()) {
while(1) {
if (RCX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
RCX --;
}
if (! get_ZF() || RCX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
else
#endif
if (i->as32L()) {
while(1) {
if (ECX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
RCX = ECX - 1;
}
if (! get_ZF() || ECX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
else // 16bit addrsize
{
while(1) {
if (CX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
CX --;
}
if (! get_ZF() || CX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
}
else { /* repeat prefix 0xF2 */
#if BX_SUPPORT_X86_64
if (i->as64L()) {
while(1) {
if (RCX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
RCX --;
}
if (get_ZF() || RCX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
else
#endif
if (i->as32L()) {
while(1) {
if (ECX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
RCX = ECX - 1;
}
if (get_ZF() || ECX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
else // 16bit addrsize
{
while(1) {
if (CX != 0) {
BX_CPU_CALL_REP_ITERATION(execute, (i));
BX_INSTR_REPEAT_ITERATION(BX_CPU_ID, i);
CX --;
}
if (get_ZF() || CX == 0) return;
if (BX_CPU_THIS_PTR async_event)
break; // exit always if debugger enabled
BX_CPU_THIS_PTR icount++;
BX_SYNC_TIME_IF_SINGLE_PROCESSOR(BX_REPEAT_TIME_UPDATE_INTERVAL);
}
}
}
BX_CPU_THIS_PTR assert_RF();
RIP = BX_CPU_THIS_PTR prev_rip; // repeat loop not done, restore RIP
// assert magic async_event to stop trace execution
BX_CPU_THIS_PTR async_event |= BX_ASYNC_EVENT_STOP_TRACE;
}
// boundaries of consideration:
//
// * physical memory boundary: 1024k (1Megabyte) (increments of...)
// * A20 boundary: 1024k (1Megabyte)
// * page boundary: 4k
// * ROM boundary: 2k (dont care since we are only reading)
// * segment boundary: any
void BX_CPU_C::prefetch(void)
{
bx_address laddr;
unsigned pageOffset;
INC_ICACHE_STAT(iCachePrefetch);
#if BX_SUPPORT_X86_64
if (long64_mode()) {
if (! IsCanonicalAccess(RIP, BX_EXECUTE, USER_PL)) {
BX_ERROR(("prefetch: #GP(0): RIP crossed canonical boundary"));
exception(BX_GP_EXCEPTION, 0);
}
// linear address is equal to RIP in 64-bit long mode
pageOffset = PAGE_OFFSET(EIP);
laddr = RIP;
// Calculate RIP at the beginning of the page.
BX_CPU_THIS_PTR eipPageBias = pageOffset - RIP;
BX_CPU_THIS_PTR eipPageWindowSize = 4096;
}
else
#endif
{
#if BX_CPU_LEVEL >= 5
if (USER_PL && BX_CPU_THIS_PTR get_VIP() && BX_CPU_THIS_PTR get_VIF()) {
if (BX_CPU_THIS_PTR cr4.get_PVI() | (v8086_mode() && BX_CPU_THIS_PTR cr4.get_VME())) {
BX_ERROR(("prefetch: inconsistent VME state"));
exception(BX_GP_EXCEPTION, 0);
}
}
#endif
BX_CLEAR_64BIT_HIGH(BX_64BIT_REG_RIP); /* avoid 32-bit EIP wrap */
laddr = get_laddr32(BX_SEG_REG_CS, EIP);
pageOffset = PAGE_OFFSET(laddr);
// Calculate RIP at the beginning of the page.
BX_CPU_THIS_PTR eipPageBias = (bx_address) pageOffset - EIP;
Bit32u limit = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled;
if (EIP > limit) {
BX_ERROR(("prefetch: EIP [%08x] > CS.limit [%08x]", EIP, limit));
exception(BX_GP_EXCEPTION, 0);
}
BX_CPU_THIS_PTR eipPageWindowSize = 4096;
if (limit + BX_CPU_THIS_PTR eipPageBias < 4096) {
BX_CPU_THIS_PTR eipPageWindowSize = (Bit32u)(limit + BX_CPU_THIS_PTR eipPageBias + 1);
}
}
#if BX_X86_DEBUGGER
if (hwbreakpoint_check(laddr, BX_HWDebugInstruction, BX_HWDebugInstruction)) {
signal_event(BX_EVENT_CODE_BREAKPOINT_ASSIST);
if (! interrupts_inhibited(BX_INHIBIT_DEBUG)) {
// The next instruction could already hit a code breakpoint but
// async_event won't take effect immediatelly.
// Check if the next executing instruction hits code breakpoint
// check only if not fetching page cross instruction
// this check is 32-bit wrap safe as well
if (EIP == (Bit32u) BX_CPU_THIS_PTR prev_rip) {
Bit32u dr6_bits = code_breakpoint_match(laddr);
if (dr6_bits & BX_DEBUG_TRAP_HIT) {
BX_ERROR(("#DB: x86 code breakpoint caught"));
BX_CPU_THIS_PTR debug_trap |= dr6_bits;
exception(BX_DB_EXCEPTION, 0);
}
}
}
}
else {
clear_event(BX_EVENT_CODE_BREAKPOINT_ASSIST);
}
#endif
BX_CPU_THIS_PTR clear_RF();
bx_address lpf = LPFOf(laddr);
bx_TLB_entry *tlbEntry = BX_ITLB_ENTRY_OF(laddr);
Bit8u *fetchPtr = 0;
if ((tlbEntry->lpf == lpf) && (tlbEntry->accessBits & (1 << unsigned(USER_PL))) != 0) {
BX_CPU_THIS_PTR pAddrFetchPage = tlbEntry->ppf;
fetchPtr = (Bit8u*) tlbEntry->hostPageAddr;
}
else {
bx_phy_address pAddr = translate_linear(tlbEntry, laddr, USER_PL, BX_EXECUTE);
BX_CPU_THIS_PTR pAddrFetchPage = PPFOf(pAddr);
}
if (fetchPtr) {
BX_CPU_THIS_PTR eipFetchPtr = fetchPtr;
}
else {
BX_CPU_THIS_PTR eipFetchPtr = (const Bit8u*) getHostMemAddr(BX_CPU_THIS_PTR pAddrFetchPage, BX_EXECUTE);
// Sanity checks
if (! BX_CPU_THIS_PTR eipFetchPtr) {
bx_phy_address pAddr = BX_CPU_THIS_PTR pAddrFetchPage + pageOffset;
if (pAddr >= BX_MEM(0)->get_memory_len()) {
BX_PANIC(("prefetch: running in bogus memory, pAddr=0x" FMT_PHY_ADDRX, pAddr));
}
else {
BX_PANIC(("prefetch: getHostMemAddr vetoed direct read, pAddr=0x" FMT_PHY_ADDRX, pAddr));
}
}
}
}
#if BX_DEBUGGER
bool BX_CPU_C::dbg_instruction_epilog(void)
{
bx_address debug_eip = RIP;
Bit16u cs = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
dbg_get_guard_state(&BX_CPU_THIS_PTR guard_found.guard_state);
//
// Take care of break point conditions generated during instruction execution
//
// Check if we hit read/write or time breakpoint
if (BX_CPU_THIS_PTR break_point) {
Bit64u tt = bx_pc_system.time_ticks();
switch (BX_CPU_THIS_PTR break_point) {
case BREAK_POINT_TIME:
BX_INFO(("[" FMT_LL "d] Caught time breakpoint", tt));
BX_CPU_THIS_PTR stop_reason = STOP_TIME_BREAK_POINT;
return true; // on a breakpoint
case BREAK_POINT_READ:
BX_INFO(("[" FMT_LL "d] Caught read watch point", tt));
BX_CPU_THIS_PTR stop_reason = STOP_READ_WATCH_POINT;
return true; // on a breakpoint
case BREAK_POINT_WRITE:
BX_INFO(("[" FMT_LL "d] Caught write watch point", tt));
BX_CPU_THIS_PTR stop_reason = STOP_WRITE_WATCH_POINT;
return true; // on a breakpoint
default:
BX_PANIC(("Weird break point condition"));
}
}
if (BX_CPU_THIS_PTR magic_break) {
BX_INFO(("[" FMT_LL "d] Stopped on MAGIC BREAKPOINT", bx_pc_system.time_ticks()));
BX_CPU_THIS_PTR stop_reason = STOP_MAGIC_BREAK_POINT;
return true; // on a breakpoint
}
// convenient point to see if user requested debug break or typed Ctrl-C
if (bx_guard.interrupt_requested) {
return true;
}
// support for 'show' command in debugger
extern unsigned dbg_show_mask;
if(dbg_show_mask) {
bx_dbg_show_symbolic();
}
// Just committed an instruction, before fetching a new one
// see if debugger is looking for iaddr breakpoint of any type
if (bx_guard.guard_for) {
if (bx_guard.guard_for & BX_DBG_GUARD_IADDR_ALL) {
#if (BX_DBG_MAX_VIR_BPOINTS > 0)
if (bx_guard.guard_for & BX_DBG_GUARD_IADDR_VIR) {
for (unsigned n=0; n<bx_guard.iaddr.num_virtual; n++) {
if (bx_guard.iaddr.vir[n].enabled &&
(bx_guard.iaddr.vir[n].cs == cs) &&
(bx_guard.iaddr.vir[n].eip == debug_eip))
{
if (! bx_guard.iaddr.vir[n].condition || bx_dbg_eval_condition(bx_guard.iaddr.vir[n].condition)) {
BX_CPU_THIS_PTR guard_found.guard_found = BX_DBG_GUARD_IADDR_VIR;
BX_CPU_THIS_PTR guard_found.iaddr_index = n;
return true; // on a breakpoint
}
}
}
}
#endif
#if (BX_DBG_MAX_LIN_BPOINTS > 0)
if (bx_guard.guard_for & BX_DBG_GUARD_IADDR_LIN) {
for (unsigned n=0; n<bx_guard.iaddr.num_linear; n++) {
if (bx_guard.iaddr.lin[n].enabled &&
(bx_guard.iaddr.lin[n].addr == BX_CPU_THIS_PTR guard_found.guard_state.laddr))
{
if (! bx_guard.iaddr.lin[n].condition || bx_dbg_eval_condition(bx_guard.iaddr.lin[n].condition)) {
BX_CPU_THIS_PTR guard_found.guard_found = BX_DBG_GUARD_IADDR_LIN;
BX_CPU_THIS_PTR guard_found.iaddr_index = n;
return true; // on a breakpoint
}
}
}
}
#endif
#if (BX_DBG_MAX_PHY_BPOINTS > 0)
if (bx_guard.guard_for & BX_DBG_GUARD_IADDR_PHY) {
bx_phy_address phy;
bool valid = dbg_xlate_linear2phy(BX_CPU_THIS_PTR guard_found.guard_state.laddr, &phy);
if (valid) {
for (unsigned n=0; n<bx_guard.iaddr.num_physical; n++) {
if (bx_guard.iaddr.phy[n].enabled && (bx_guard.iaddr.phy[n].addr == phy))
{
if (! bx_guard.iaddr.phy[n].condition || bx_dbg_eval_condition(bx_guard.iaddr.phy[n].condition)) {
BX_CPU_THIS_PTR guard_found.guard_found = BX_DBG_GUARD_IADDR_PHY;
BX_CPU_THIS_PTR guard_found.iaddr_index = n;
return true; // on a breakpoint
}
}
}
}
}
#endif
}
// see if debugger requesting icount guard
if (bx_guard.guard_for & BX_DBG_GUARD_ICOUNT) {
if (get_icount() >= BX_CPU_THIS_PTR guard_found.icount_max) {
return true;
}
}
}
return false;
}
#endif
#if BX_GDBSTUB
bool BX_CPU_C::gdbstub_instruction_epilog(void)
{
if (bx_dbg.gdbstub_enabled) {
unsigned reason =
#if BX_SUPPORT_X86_64 == 0
bx_gdbstub_check(EIP);
#else
bx_gdbstub_check(RIP);
#endif
if (reason != GDBSTUB_STOP_NO_REASON)
return(1);
}
return(0);
}
#endif
|