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 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
All rights reserved.
This file is part of the Palm OS Emulator.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
\* ===================================================================== */
#include "EmCommon.h"
#include "EmPalmOS.h"
#include "EmBankDRAM.h" // EmBankDRAM::SetLong
#include "EmBankMapped.h" // EmBankMapped::SetLong
#include "EmBankROM.h" // EmBankROM::SetLong
#include "EmBankSRAM.h" // EmBankSRAM::SetLong
#include "DebugMgr.h" // Debug::HandleSystemCall
#include "EmCPU68K.h" // gCPU68K, gStackHigh, etc.
#include "EmErrCodes.h" // kError_UnimplementedTrap, kError_InvalidLibraryRefNum
#include "EmMemory.h" // CEnableFullAccess
#include "EmPalmHeap.h" // EmPalmHeap, GetHeapByPtr
#include "EmPalmFunction.h" // ProscribedFunction
#include "EmPalmStructs.h" // EmAliasCardHeaderType
#include "EmPatchMgr.h" // EmPatchMgr
#include "EmPatchState.h" // EmPatchState
#include "EmSession.h" // gSession->Reset
#include "ErrorHandling.h" // Errors::ReportInvalidPC
#include "Logging.h" // LogSystemCalls
#include "MetaMemory.h" // MetaMemory::InRAMOSComponent
#include "Miscellaneous.h" // GetSystemCallContext
#include "Profiling.h" // gProfilingEnabled
#include "ROMStubs.h" // IntlSetStrictChecks
#include "UAE.h" // CHECK_STACK_POINTER_DECREMENT
#include "EmEventPlayback.h" // EmEventPlayback::Initialize ();
#include "EmLowMem.h" // EmLowMem::Initialize ();
#include "EmPalmFunction.h" // EmPalmFunctionInit ();
#include "EmPalmHeap.h" // EmPalmHeap::Initialize ();
#include "EmPatchMgr.h" // EmPatchMgr::Initialize ();
#include "Hordes.h" // Hordes::Initialize ();
#include "Platform_NetLib.h" // Platform_NetLib::Initialize();
#define LOG_FUNCTION_CALLS 0
static StackList gStackList;
static StackRange gBootStack;
static StackRange gKernelStack;
static StackRange gInterruptStack;
static emuptr gBigROMEntry;
static const uint32 CJ_TAGAMX = 0x414D5800;
static const uint32 CJ_TAGFENCE = 0x55555555;
// Subtract 34 bytes from the size of the stack. That's because
// if an interrupt occurs, 34 bytes are pushed onto the stack before
// the OS switches over to the interrupt stack.
//
// Exception 6 bytes // Old SR and old PC
// LINK 4 bytes // Old A6
// MOVEM.L 20 bytes // Old D0, D1, D2, A0, A1
// MOVE A0, -(SP) 4 bytes // Old A0 (again)
// ---------------------------------------------------------
// Total 34 bytes
static const int kInterruptOverhead = 34;
static emuptr gStackLowWaterMark = 0;
/***********************************************************************
*
* FUNCTION: EmPalmOS::Initialize
*
* DESCRIPTION: Standard initialization function. Responsible for
* initializing this sub-system when a new session is
* created. Will be followed by at least one call to
* Reset or Load.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::Initialize (void)
{
EmAssert (gCPU68K);
gCPU68K->InstallHookException (kException_SysCall, HandleTrap15);
gCPU68K->InstallHookJSR (HandleJSR);
gCPU68K->InstallHookJSR_Ind (HandleJSR_Ind);
gCPU68K->InstallHookLINK (HandleLINK);
gCPU68K->InstallHookRTE (HandleRTE);
gCPU68K->InstallHookRTS (HandleRTS);
gCPU68K->InstallHookNewPC (HandleNewPC);
gCPU68K->InstallHookNewSP (HandleNewSP);
gBigROMEntry = EmMemNULL;
// Add a notification for IntlStrictChecks
gPrefs->AddNotification (&EmPalmOS::PrefsChanged, kPrefKeyReportStrictIntlChecks);
gPrefs->AddNotification (&EmPalmOS::PrefsChanged, kPrefKeyReportOverlayErrors);
Hordes::Initialize ();
EmEventPlayback::Initialize ();
EmPatchMgr::Initialize ();
Platform_NetLib::Initialize ();
EmPalmHeap::Initialize ();
EmLowMem::Initialize ();
EmPalmFunctionInit ();
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::Reset
*
* DESCRIPTION: Standard reset function. Sets the sub-system to a
* default state. This occurs not only on a Reset (as
* from the menu item), but also when the sub-system
* is first initialized (Reset is called after Initialize)
* as well as when the system is re-loaded from an
* insufficient session file.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::Reset (void)
{
gStackList.clear ();
gBootStack = StackRange ();
gKernelStack = StackRange ();
gInterruptStack = StackRange ();
gKernelStackOverflowed = 0;
Hordes::Reset ();
EmEventPlayback::Reset ();
EmPatchMgr::Reset ();
Platform_NetLib::Reset ();
EmPalmHeap::Reset ();
EmLowMem::Reset ();
// If the appropriate modifier key is down, install a temporary breakpoint
// at the start of the Big ROM.
if (Platform::StopOnResetKeyDown ())
{
emuptr romStart = EmBankROM::GetMemoryStart ();
emuptr headerVersion = EmMemGet32 (romStart + offsetof (CardHeaderType, hdrVersion));
long bigROMOffset = 0x03000;
if (headerVersion > 1)
{
bigROMOffset = EmMemGet32 (romStart + offsetof (CardHeaderType, bigROMOffset));
bigROMOffset &= 0x000FFFFF; // Allows for 1 Meg offset.
}
emuptr resetVector = EmMemGet32 (romStart + bigROMOffset + offsetof (CardHeaderType, resetVector));
Debug::SetBreakpoint (dbgTempBPIndex, resetVector, NULL);
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::Save
*
* DESCRIPTION: Standard save function. Saves any sub-system state to
* the given session file.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::Save (SessionFile& f)
{
Hordes::Save (f);
EmEventPlayback::Save (f);
EmPatchMgr::Save (f);
Platform_NetLib::Save (f);
EmPalmHeap::Save (f);
EmLowMem::Save (f);
const long kCurrentVersion = 2;
Chunk chunk;
EmStreamChunk s (chunk);
s << kCurrentVersion;
s << gStackList;
s << gBootStack;
s << gKernelStack;
s << gInterruptStack;
s << gStackHigh;
s << gStackLowWaterMark;
s << gStackLowWarn;
s << gStackLow;
s << gKernelStackOverflowed;
f.WriteStackInfo (chunk);
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::Load
*
* DESCRIPTION: Standard load function. Loads any sub-system state
* from the given session file.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::Load (SessionFile& f)
{
Hordes::Load (f);
EmEventPlayback::Load (f);
EmPatchMgr::Load (f);
Platform_NetLib::Load (f);
EmPalmHeap::Load (f);
EmLowMem::Load (f);
Chunk chunk;
if (f.ReadStackInfo (chunk))
{
long version;
EmStreamChunk s (chunk);
s >> version;
if (version >= 1)
{
s >> gStackList;
s >> gBootStack;
s >> gKernelStack;
s >> gInterruptStack;
s >> gStackHigh;
s >> gStackLowWaterMark;
s >> gStackLowWarn;
s >> gStackLow;
}
if (version >= 2)
{
s >> gKernelStackOverflowed;
}
}
else
{
f.SetCanReload (false); // Need to reboot
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::Dispose
*
* DESCRIPTION: Standard dispose function. Completely release any
* resources acquired or allocated in Initialize and/or
* Load.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::Dispose (void)
{
EmLowMem::Dispose ();
EmPalmHeap::Dispose ();
Platform_NetLib::Dispose ();
EmPatchMgr::Dispose ();
EmEventPlayback::Dispose ();
Hordes::Dispose ();
}
#pragma mark -
/***********************************************************************
*
* FUNCTION: EmPalmOS::CheckStackPointerAssignment
*
* DESCRIPTION: !!! This operation slows down Gremlins by about 10%; it
* should be sped up or made an option.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::CheckStackPointerAssignment (void)
{
// If it was a "drastic" change, assume that we're *setting* the
// stack pointer to a new stack. Scarf up information about that
// block of memory and treat that block as a stack.
// See if we already know about this stack.
emuptr curA7 = gCPU->GetSP ();
StackList::iterator iter = gStackList.begin ();
while (iter != gStackList.end ())
{
if ((curA7 >= (*iter).fBottom) && (curA7 <= (*iter).fTop))
{
// If so, switch to it.
SetCurrentStack (*iter);
return;
}
++iter;
}
// If not, get some information about it and save it off.
const EmPalmHeap* heap = EmPalmHeap::GetHeapByPtr (curA7);
if (heap)
{
const EmPalmChunk* chunk = heap->GetChunkBodyContaining (curA7);
if (chunk)
{
// The second time we switch stacks, we're switching from the
// boot stack to the kernel stack. In that case, forget any
// notion of the boot stack.
if (gBootStack.fBottom != EmMemNULL)
{
ForgetStack (gBootStack.fBottom);
}
// See if this looks like the AMX kernel stack.
// We detect this by seeing (a) if the stack pointer
// is being set to a value not too close to the end
// of the chunk it's in (indicating that the stack is
// embedded in some other data, as is the AMX stack)
// and (b) if the values the stack pointer points to
// are the appropriate tags.
CEnableFullAccess munge;
if (curA7 <= (chunk->BodyEnd () - 8) &&
EmMemGet32 (curA7) == CJ_TAGAMX &&
EmMemGet32 (curA7 + 4) == CJ_TAGFENCE)
{
RememberKernelStack ();
// Remember this stack as the "current" stack.
SetCurrentStack (gKernelStack);
}
else
{
RememberStackChunk (*chunk);
// Remember this stack as the "current" stack.
StackRange range (chunk->BodyStart (),
chunk->BodyStart () + chunk->BodySize ());
SetCurrentStack (range);
}
}
else
{
// !!!
}
}
else
{
if (gStackList.size () == 0)
{
// We're switching from the reset stack to the boot stack.
RememberBootStack ();
// Remember this stack as the "current" stack.
SetCurrentStack (gBootStack);
}
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::CheckStackPointerDecrement
*
* DESCRIPTION: !!! This operation slows down Gremlins by about 10%; it
* should be sped up or made an option.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::CheckStackPointerDecrement (void)
{
// If it was an "incremental" change, make sure that A7 is not
// now outside the stack it used to be in (as indicated by oldA7).
// Test for "close to the end" (in which case we display a warning), and
// "past the end" (in which case we print a fatal error).
if (gKernelStackOverflowed)
return;
emuptr curA7 = gCPU->GetSP ();
Bool warning = curA7 < gStackLowWarn;
Bool fatal = curA7 < gStackLow;
if (gStackLowWaterMark > curA7)
gStackLowWaterMark = curA7;
if (warning)
{
// Special hack for the kernel stack overflowing into the
// interrupt stack. If there was a stack overflow and the
// current stack is the kernel stack, then ignore the error.
// Also, try limiting this hack to older Palm OS's by looking
// at the stack size. Newer ROMs will have larger stacks and
// should not be allowed to have this bug.
if (fatal && gKernelStack.fTop == gStackHigh &&
gKernelStack.fTop - gKernelStack.fBottom <= 0x2F4)
{
gKernelStackOverflowed = 1;
return;
}
if (fatal)
{
Errors::ReportErrStackOverflow ();
}
else
{
EmAssert (gSession);
gSession->ScheduleDeferredError (new EmDeferredErrStackFull);
}
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::CheckKernelStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::CheckKernelStack (void)
{
EmAssert (gKernelStackOverflowed);
emuptr curA7 = gCPU->GetSP ();
if (curA7 <= gStackHigh && curA7 >= gStackLow)
{
gKernelStackOverflowed = 0;
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::RememberStackChunk
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::RememberStackChunk (const EmPalmChunk& chunk)
{
// Create a range object for the chunk.
StackRange range (chunk.BodyStart (), chunk.BodyEnd ());
// Remember it for later.
RememberStackRange (range);
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::RememberBootStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::RememberBootStack (void)
{
/*
There's a constant in SystemPrv.h that indicates that the boot
stack should be 4K long. However, that 4K is relative to the
start of the dynamic heap, which is used while the boot stack
is in effect. As the system boots up, blocks are allocated and
de-allocated in the stack range. We have a check in ForgetStack
that checks to see if a MemFooFree operation is taking place
within a stack -- normally a dubious operation -- and it's
noticing this situation.
For now, it looks like we only use 0x438 bytes* of the boot
stack, so I'll allow 0x500. This should prevent the overlap of
the boot stack with any allocated chunks.
* This was against one particular ROM, but I don't remember
which. Checking a Palm IIIc 4.0 Debug ROM just now, I see
it using 0x308 bytes.
*/
// const int kBootStackSize = 0x1000; // Per SystemPrv.h
const int kBootStackSize = 0x0500; // Per experimentation
gBootStack = StackRange (
gCPU->GetSP () - kBootStackSize,
gCPU->GetSP ());
RememberStackRange (gBootStack);
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::RememberKernelStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::RememberKernelStack (void)
{
// Scan down for the bottom of stack marker.
CEnableFullAccess munge;
emuptr ksp = gCPU->GetSP ();
while (EmMemGet32 (ksp) != CJ_TAGFENCE)
ksp -= 2;
emuptr isp = ksp - 4;
while (EmMemGet32 (isp) != CJ_TAGFENCE)
isp -= 2;
// Add the two stacks. Add the interrupt stack first, and the
// kernel stack second. That way, the last added stack -- the
// kernel stack -- is remembered as the "current" stack.
gInterruptStack = StackRange (isp + 4, ksp);
RememberStackRange (gInterruptStack);
gKernelStack = StackRange (ksp + 4, gCPU->GetSP ());
RememberStackRange (gKernelStack);
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::RememberStackRange
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::RememberStackRange (const StackRange& range)
{
#if _DEBUG
// Make sure this range doesn't overlap any other stack in our list.
StackList::iterator iter = gStackList.begin ();
while (iter != gStackList.end ())
{
if (range.fBottom >= iter->fBottom && range.fBottom < iter->fTop)
EmAssert (false);
if (range.fTop > iter->fBottom && range.fTop <= iter->fTop)
EmAssert (false);
++iter;
}
#endif
// Add the range to the list.
gStackList.push_back (range);
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::SetCurrentStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::SetCurrentStack (const StackRange& range)
{
// Record the low-water mark of the previous stack.
StackList::iterator iter = gStackList.begin ();
while (iter != gStackList.end ())
{
if (gStackHigh == iter->fTop)
{
iter->fLowWaterMark = gStackLowWaterMark;
if (gStackHigh == gBootStack.fTop)
gBootStack.fLowWaterMark = gStackLowWaterMark;
if (gStackHigh == gKernelStack.fTop)
gKernelStack.fLowWaterMark = gStackLowWaterMark;
if (gStackHigh == gInterruptStack.fTop)
gInterruptStack.fLowWaterMark = gStackLowWaterMark;
break;
}
++iter;
}
// Determine the amount to test against when determining if we are
// close to overflowing the stack. For applications, let's set this
// to 50 bytes. For the kernel stack, set it to zero; some Palm OS's
// get damn close to the end of the kernel stack (the Palm VII ROM gets
// within 8 bytes!).
const int kAppStackSlush = 50;
const int kKernelStackSlush = 0;
int stackSlush = kAppStackSlush;
if (range == gKernelStack)
stackSlush = kKernelStackSlush;
gStackHigh = range.fTop;
gStackLowWaterMark = range.fLowWaterMark;
gStackLowWarn = range.fBottom + kInterruptOverhead + stackSlush;
gStackLow = range.fBottom + kInterruptOverhead;
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::ForgetStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::ForgetStack (emuptr stackBottom)
{
StackList::iterator iter = gStackList.begin ();
while (iter != gStackList.end ())
{
// If the pointer is in the *middle* of a stack, that's not good.
if (stackBottom > iter->fBottom && stackBottom < iter->fTop)
EmAssert (false);
// If the pointer is to the beginning of the stack, we've found
// the one we want to remove.
if (stackBottom == iter->fBottom)
{
gStackList.erase (iter);
if (stackBottom == gBootStack.fBottom)
{
#define TRACK_BOOT_ALLOCATION 0
#if TRACK_BOOT_ALLOCATION
LogAppendMsg ("===== Forgetting Boot Stack, top = 0x%08X, low water mark: 0x%08X =====",
gBootStack.fTop, gBootStack.fLowWaterMark);
#endif
gBootStack = StackRange ();
}
return;
}
++iter;
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::ForgetStacksIn
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::ForgetStacksIn (emuptr start, uint32 range)
{
StackList::iterator iter = gStackList.begin ();
while (iter != gStackList.end ())
{
if ((iter->fBottom >= start) && (iter->fTop <= (start + range)))
{
gStackList.erase (iter);
if (start == gBootStack.fBottom)
{
#define TRACK_BOOT_ALLOCATION 0
#if TRACK_BOOT_ALLOCATION
LogAppendMsg ("===== Forgetting Boot Stack, top = 0x%08X, low water mark: 0x%08X =====",
gBootStack.fTop, gBootStack.fLowWaterMark);
#endif
gBootStack = StackRange ();
}
// We munged the collection; start over.
iter = gStackList.begin ();
continue;
}
++iter;
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::GetBootStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
StackRange EmPalmOS::GetBootStack (void)
{
return gBootStack;
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::IsInStack
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::IsInStack (emuptr addr)
{
return addr >= gStackLow && addr < gStackHigh;
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::IsInStackBlock
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::IsInStackBlock (emuptr addr)
{
return addr >= gCPU->GetSP () && addr < gStackHigh;
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::GenerateStackCrawl
*
* DESCRIPTION: Starting with the current PC and A6, generate a list
* of active functions.
*
* PARAMETERS: frameList - reference to the collection to receive
* the results.
*
* RETURNED: Nothing
*
***********************************************************************/
void EmPalmOS::GenerateStackCrawl (EmStackFrameList& frameList)
{
// Make sure we have access to all of memory, in case A6 is bogus
// or used for some purpose other than frame links.
CEnableFullAccess munge;
// Clear out the stack crawl list.
frameList.clear ();
// Push the initial stack frame onto the stack. This consists
// of the current PC and A6 values.
EmStackFrame frame;
frame.fAddressInFunction = m68k_getpc ();
frame.fA6 = m68k_areg (regs, 6);
frameList.push_back (frame);
// If A6 is odd or not in the current stack, stop the stack crawl.
if (::IsOdd (frame.fA6) || !EmPalmOS::IsInStack (frame.fA6))
return;
while (1)
{
emuptr oldA6 = frame.fA6;
// Get the previous A6 and function from the stack.
frame.fAddressInFunction = EmMemGet32 (oldA6 + 4);
frame.fA6 = EmMemGet32 (oldA6);
// If A6 is odd or not in the current stack, stop the stack crawl.
if (::IsOdd (frame.fA6) || !EmPalmOS::IsInStack (frame.fA6))
return;
// If the new A6 is not greater than the old A6, stop
// the stack crawl.
if (frame.fA6 <= oldA6)
return;
// If the return address is not valid, let's take a chance
// that what's on the stack is <A6> <SR> <return address>.
// We'd see this sequence if an exception has occurred and
// and exception frame is on the stack.
if (!EmMemCheckAddress (frame.fAddressInFunction, 2))
{
frame.fAddressInFunction = EmMemGet32 (oldA6 + 6);
// If the return address still doesn't look valid,
// stop the stack crawl.
if (!EmMemCheckAddress (frame.fAddressInFunction, 2))
{
return;
}
}
// Everything looks OK, push this information onto our stack.
frameList.push_back (frame);
}
}
// ---------------------------------------------------------------------------
// EmPalmOS::PrefsChanged
// ---------------------------------------------------------------------------
// Respond to a preference change.
static void PrvRespondToPrefsChange (PrefKeyType prefKey)
{
if (EmPatchState::UIInitialized ())
{
if (::PrefKeysEqual (prefKey, kPrefKeyReportStrictIntlChecks) && EmPatchMgr::IntlMgrAvailable ())
{
Preference<Bool> pref (kPrefKeyReportStrictIntlChecks, false);
::IntlSetStrictChecks (*pref);
}
if (::PrefKeysEqual (prefKey, kPrefKeyReportOverlayErrors))
{
Preference<Bool> pref (kPrefKeyReportOverlayErrors, false);
(void) ::FtrSet (omFtrCreator, omFtrShowErrorsFlag, *pref);
}
}
}
void EmPalmOS::PrefsChanged (PrefKeyType prefKey, void*)
{
if (gSession)
{
#if HAS_OMNI_THREAD
if (gSession->InCPUThread ())
{
::PrvRespondToPrefsChange (prefKey);
}
else
#endif
{
EmSessionStopper stopper (gSession, kStopOnSysCall);
::PrvRespondToPrefsChange (prefKey);
}
}
}
#pragma mark -
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleTrap15
*
* DESCRIPTION: Handle a trap. Traps are of the format TRAP $F / $Axxx.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::HandleTrap15 (ExceptionNumber)
{
return EmPalmOS::HandleSystemCall (true);
}
/***********************************************************************
*
* FUNCTION: Software::HandleJSR
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::HandleJSR (emuptr oldpc, emuptr dest)
{
#if HAS_PROFILING
if (gProfilingEnabled)
{
StDisableMemoryCycleCounting stopper;
Boolean skipProfiling = false;
// In multi-segmented application generated by CodeWarrior,
// inter-segment function calls are implemented by JSR'ing
// to a jump table entry. This jump table entry contains
// a JMP to the final location. Detect this case and emit
// a function entry record for the final location, not the
// jump table entry.
const uint16 kOpcode_JMP_Abs32 = 0x4EF9;
uint8* realMem = EmMemGetRealAddress (dest);
if (EmMemDoGet16 (realMem) == kOpcode_JMP_Abs32)
{
dest = EmMemDoGet32 (realMem + 2);
}
else
{
//---------------------------------------------------------------------
// We don't want to process JSRs to a switch statement. Currently
// CodeWarrior generates a switch statement block like this:
// MOVE.L (A7)+,A0 | 205F
// MOVE.L A0,A1 | 2248
// ADD.W (A0)+,A1 | D2D8
// CMP.W (A0)+,D0 | B058
// BGE.S *+$0004 ; 10C20F5C | 6C02
// JMP (A1) | 4ED1
// CMP.W (A0)+,D0 | B058
// BLE.S *+$0004 ; 10C20F62 | 6F02
// JMP (A1) | 4ED1
// MOVE.W (A0)+,D1 | 3218
// CMP.W (A0)+,D0 | B058
// BNE.S *+$0006 ; 10C20F6C | 6604
// ADD.W (A0),A0 | D0D0
// JMP (A0) | 4ED0
// ADDQ.W #$02,A0 | 5448
// DBF D1,*-$000A ; 10C20F64 | 51C9 FFF4
// JMP (A1) | 4ED1
//---------------------------------------------------------------------
skipProfiling = (EmMemDoGet32 (realMem) == 0x205F2248)
&& (EmMemDoGet32 (realMem + 4) == 0xD2D8B058)
&& (EmMemDoGet32 (realMem + 8) == 0x6C024ED1);
}
if (!skipProfiling)
{
ProfileFnEnter (dest, oldpc);
}
}
#endif
#if LOG_FUNCTION_CALLS
// char fromName[80];
char toName[80];
// ::FindFunctionName (oldpc, fromName, NULL, NULL, 80);
::FindFunctionName (dest, toName, NULL, NULL, 80);
EmStackFrameList stackCrawl;
EmPalmOS::GenerateStackCrawl (stackCrawl);
string dots ("|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-");
dots.resize (stackCrawl.size ());
// LogAppendMsg (">>> %s --> %s", fromName, toName);
// LogAppendMsg ("--- System Call 0xA08D: %s", fromName, toName);
LogAppendMsg ("--- Function Call: %s%s", dots.c_str (), toName);
#endif
return false; // We didn't completely handle it; do default handling.
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleJSR_Ind
*
* DESCRIPTION: Check for SYS_TRAP_FAST calls.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::HandleJSR_Ind (emuptr oldpc, emuptr dest)
{
Bool handledIt = false; // Default to calling ROM.
// inline asm SysTrapFast(Int trapNum)
// {
// MOVE.L struct(LowMemType.fixed.globals.sysDispatchTableP), A1
// MOVE.L ((trapNum-sysTrapBase)*4)(A1), A1
// JSR (A1) // call it
// }
//
// #define SYS_TRAP_FAST(trapNum)
// FIVEWORD_INLINE(
// 0x2278, 0x0122,
// 0x2269, (trapNum-sysTrapBase)*4,
// 0x4e91)
uint8* realMem = EmMemGetRealAddress (oldpc);
if (EmMemDoGet16 (realMem) == 0x4e91 &&
EmMemDoGet16 (realMem - 4) == 0x2269 &&
EmMemDoGet16 (realMem - 8) == 0x2278)
{
handledIt = EmPalmOS::HandleSystemCall (false);
}
else
{
if (gBigROMEntry == EmMemNULL)
{
emuptr base = EmBankROM::GetMemoryStart ();
// Check every romDelta (4K) for up to maxBigROMOffset
//(256K) till we find the "Big" ROM
const UInt32 romDelta = 4 * 1024L; // BigROM starts on a 4K boundary
const UInt32 maxBigROMOffset = 256 * 1024L; // Give up looking past here
UInt16 loops = maxBigROMOffset / romDelta; // How many loops to do
emuptr bP = base + romDelta;
while (loops--)
{
// Ignore older card headers that might have been
// programmed in at a lower address (hdrVersion < 3)
EmAliasCardHeaderType<PAS> cardHdr (bP);
UInt32 signature = cardHdr.signature;
UInt16 hdrVersion = cardHdr.hdrVersion;
if (signature == sysCardSignature && hdrVersion >= 3)
{
gBigROMEntry = cardHdr.resetVector;
break;
}
bP += romDelta;
}
// See if we found it (may have hdrVersion < 3)
if (gBigROMEntry == EmMemNULL)
{
EmAliasCardHeaderType<PAS> smallCardHdr (base);
UInt32 bigROMOffset;
if (smallCardHdr.hdrVersion == 2)
{
bigROMOffset = smallCardHdr.bigROMOffset & 0x000FFFFF;
}
else
{
bigROMOffset = 0x3000;
}
EmAliasCardHeaderType<PAS> bigCardHdr (base + bigROMOffset);
gBigROMEntry = bigCardHdr.resetVector;
}
}
if (dest == gBigROMEntry)
{
EmAssert (gSession);
gSession->ScheduleReset (kResetSys);
}
}
return handledIt;
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleLINK
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::HandleLINK (int /*linkSize*/)
{
#if 0
Preference<bool> pref (kPrefKeyFillStack);
if (*pref && linkSize < 0)
{
uae_memset (gCPU->GetSP (), 0xD5, -linkSize);
}
#endif
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleRTS
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::HandleRTS (emuptr dest)
{
UNUSED_PARAM (dest);
#if HAS_PROFILING
if (gProfilingEnabled)
{
StDisableMemoryCycleCounting stopper;
ProfileFnExit (dest, gCPU->GetPC () + 2);
}
#endif
#if LOG_FUNCTION_CALLS
char fromName[80];
char toName[80];
::FindFunctionName (gCPU->GetPC(), fromName, NULL, NULL, 80);
::FindFunctionName (dest, toName, NULL, NULL, 80);
LogAppendMsg ("<<< %s --> %s", fromName, toName);
#endif
#if 0
char fromName[80];
::FindFunctionName (gCPU->GetPC(), fromName, NULL, NULL, 80);
if (strcmp (fromName, "HwrADC") == 0)
{
// A7 points to return address
// A7 + 4 points to UInt16 (command)
// A7 + 6 points to pointer to result
enum hwrADCCmdEnum
{
hwrADCCmdReadXYPos=0, // Read Digitizer X & Y Positions
hwrADCCmdReadBatt, // Read Battery Level
hwrADCCmdReadAux, // Read Battery Level
hwrADCCmdReadTemp, // Read Temperature
hwrADCCmdReadXPos, // Read Digitizer X Position
hwrADCCmdReadYPos, // Read Digitizer Y Position
hwrADCCmdReadZPos, // Read Digitizer Z Position (Pressure)
hwrADCCmdReadXYZPos, // Read Digitizer X, Y & Z Positions
hwrADCCmdPenIRQOn // Enable Pen IRQ
} ;
struct HwrADCReadResultType
{
UInt16 reserved1; //mVolts; // level in millivolts (2500 = 2.5 volts)
UInt16 abs; // absolute level (0 -> 255)
UInt16 aux; // absolute level (0 -> 255)
};
const char* text[] =
{
"hwrADCCmdReadXYPos",
"hwrADCCmdReadBatt",
"hwrADCCmdReadAux",
"hwrADCCmdReadTemp",
"hwrADCCmdReadXPos",
"hwrADCCmdReadYPos",
"hwrADCCmdReadZPos",
"hwrADCCmdReadXYZPos",
"hwrADCCmdPenIRQOn",
"unknown"
};
UInt16 cmd = EmMemGet16 (gCPU->GetSP () + 4);
emuptr ptr = EmMemGet32 (gCPU->GetSP () + 6);
UInt16 reserved1 = EmMemGet16 (ptr + 0);
UInt16 abs = EmMemGet16 (ptr + 2);
UInt16 aux = EmMemGet16 (ptr + 4);
LogAppendMsg ("HwrADC (%s), reserved1 = 0x%04X, abs = 0x%04X, aux = 0x%04X",
text[cmd], (int) reserved1, (int) abs, (int) aux);
}
else if (strcmp (fromName, "HwrDockStatus") == 0)
{
UInt16 result = m68k_dreg (regs, 0);
LogAppendMsg ("HwrDockStatus result = 0x%04X",(int) result);
}
#endif
return false; // We didn't completely handle it; do default handling.
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleRTE
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::HandleRTE (emuptr dest)
{
UNUSED_PARAM (dest);
#if HAS_PROFILING
if (gProfilingEnabled)
{
ProfileInterruptExit (dest);
}
#endif
#if LOG_FUNCTION_CALLS
char fromName[80];
char toName[80];
::FindFunctionName (gCPU->GetPC(), fromName, NULL, NULL, 80);
::FindFunctionName (dest, toName, NULL, NULL, 80);
LogAppendMsg ("<<< %s --> %s", fromName, toName);
#endif
return false; // We didn't completely handle it; do default handling.
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleNewPC
*
* DESCRIPTION: The PC is about to be changed via a JSR, BSR, RTS, or
* RTE. Check that the new address appears to be valid.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::HandleNewPC (emuptr dest)
{
// If the address is odd, then it's bad.
if ((dest & 1) != 0)
Errors::ReportErrInvalidPC (dest, kOddAddress);
// if (!EmPatchState::UIInitialized ())
// return;
// Ensure that the PC is in ROM or RAM.
EmMemPutFunc longSetter = EmMemGetBank (dest).lput;
if (longSetter == EmBankROM::SetLong || longSetter == EmBankFlash::SetLong)
{
if (!EmBankROM::ValidAddress (dest, 2))
{
Errors::ReportErrInvalidPC (dest, kNotInCodeSegment);
}
}
else if (longSetter == EmBankSRAM::SetLong)
{
}
else if (longSetter == EmBankDRAM::SetLong)
{
if (dest < (256 + 693 * 4) /*MetaMemory::GetSysGlobalsEnd ()*/)
{
Errors::ReportErrInvalidPC (dest, kNotInCodeSegment);
}
}
else if (longSetter == EmBankMapped::SetLong)
{
if (!EmBankMapped::ValidAddress (dest, 2))
{
Errors::ReportErrInvalidPC (dest, kUnmappedAddress);
}
}
else
{
Errors::ReportErrInvalidPC (dest, kUnmappedAddress);
}
}
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleNewSP
*
* DESCRIPTION: !!! This operation slows down Gremlins by about 10%; it
* should be sped up or made an option.
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
void EmPalmOS::HandleNewSP (EmStackChangeType type)
{
if (type == kStackPointerChanged)
{
CheckStackPointerAssignment ();
}
else if (type == kStackPointerDecremented)
{
CheckStackPointerDecrement ();
}
else if (type == kStackPointerKernelStackHack)
{
CheckKernelStack ();
}
}
#pragma mark -
/***********************************************************************
*
* FUNCTION: EmPalmOS::HandleSystemCall
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
Bool EmPalmOS::HandleSystemCall (Bool fromTrap)
{
// ======================================================================
// First things first: if we need to break execution on the next call
// to a system function, make sure that happens.
// ======================================================================
EmAssert (gSession);
EmAssert (gCPU68K);
#if HAS_PROFILING
int oldProfilingCounted = gProfilingCounted;
gProfilingCounted = false;
#endif
// If the system call is being made by a TRAP $F, the PC has already
// been bumped past the opcode. If being made with a JSR via the
// SYS_TRAP_FAST macro, the PC has not been adjusted. Determine a
// "pcAdjust" value that allows us to get back to the start of the
// instruction that got us here.
int pcAdjust = fromTrap ? 2 : 0;
if (!gSession->IsNested () && gSession->GetBreakOnSysCall ())
{
CEnableFullAccess munge;
// Check the memory manager semaphore. If we're stopping on a
// system call, it's most likely that we want to make some nested
// Palm OS calls. Some of those calls may try to acquire the
// memory manager semaphore. Therefore, make sure it's available.
UInt32 memSemaphoreIDP = EmLowMem_GetGlobal (memSemaphoreID);
EmAliascj_xsmb<PAS> memSemaphoreID (memSemaphoreIDP);
if (memSemaphoreID.xsmuse == 0)
{
// Check the current task. We don't want to break on a system
// call if we're in the background. If we're breaking on a
// system call, it's likely because some other part of Poser
// wants to make OS calls. Occassionally, those OS calls result
// in trying to switch to the UI task (see, for example,
// PrvEditCardOptionsOK). If we're already in the UI task, then
// that's OK, but if we're in a background task, an attempted
// switch to the UI task will fail, due to Poser turning off
// interrupts when it makes OS calls.
SysKernelInfoType taskInfo;
taskInfo.selector = sysKernelInfoSelCurTaskInfo;
Err err = ::SysKernelInfo (&taskInfo);
if (err == errNone)
{
if (taskInfo.param.task.tag == 'psys')
{
gCPU->SetPC (gCPU->GetPC () - pcAdjust);
gSession->ScheduleSuspendSysCall ();
#if HAS_PROFILING
gProfilingCounted = oldProfilingCounted;
#endif
// Return true to say that everything has been handled.
return true;
}
// If we took a stab at this and didn't find that we were
// in the UI task, then call EvtWakeup. If our context is
// one where an application has called EvtGetEvent with a
// "sleep forever" timeout, then the current task will be
// 'AMX ', and will stay that way. Calling EvtWakeup will
// force an eventual switch to the UI task.
//
// Note that we wake-up the UI thread only if we're in the
// root AMX thread. This prevents us from trying to call
// it when the Palm context is one where a background task
// is the current task. It's possible for that task to be
// sleeping or something (the example we ran into was with
// a background task calling SysDelay). EvtWakeup will try
// to switch to the UI task, but will not return until the
// UI task switches back to the background task. That will
// never happen because interrupts are turned off during
// all Poser calls into the OS (such as the call to
// EvtWakeup). Thus, the only safe places to call
// EvtWakeup are in the UI or AMX tasks.
else if (taskInfo.param.task.tag == 0x414d5800)
{
::EvtWakeup ();
}
}
}
}
// ======================================================================
// Determine what ROM function is about to be called, and determine
// the method by which it is being called.
// ======================================================================
SystemCallContext context;
Bool gotFunction = GetSystemCallContext (
gCPU->GetPC () - pcAdjust, context);
// ======================================================================
// Validate the address for the ROM function we're about to call.
// ======================================================================
if (!gotFunction)
{
#if HAS_PROFILING
gProfilingCounted = oldProfilingCounted;
#endif
if (context.fError == kError_UnimplementedTrap)
{
Errors::ReportErrUnimplementedTrap (context);
}
else if (context.fError == kError_InvalidLibraryRefNum)
{
Errors::ReportErrInvalidRefNum (context);
}
// We should never get here. context.fError should always equal
// one of those two error codes, and those two functions we call
// should never return (they should throw exceptions).
EmAssert (false);
}
// ======================================================================
// Record what function we're calling.
// ======================================================================
if (!gSession->IsNested () && LogSystemCalls ())
{
char name [sysPktMaxNameLen];
strcpy (name, ::GetTrapName (context, true));
EmStackFrameList stackCrawl;
EmPalmOS::GenerateStackCrawl (stackCrawl);
string dots ("|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-");
dots.resize (stackCrawl.size ());
LogAppendMsg ("--- System Call 0x%04X: %s%s.", (long) context.fTrapWord, dots.c_str (), name);
}
// ======================================================================
// Let the debugger have a crack at it. It may want to do a "break
// on ATrap" thingy. If so, it will return true. Otherwise,
// if no action is necessary, it will return false.
// ======================================================================
if (!gSession->IsNested () && Debug::HandleSystemCall (context))
{
// Return true to say that everything has been handled.
#if HAS_PROFILING
gProfilingCounted = oldProfilingCounted;
#endif
return true;
}
// ======================================================================
// Check for functions unsupported on ARM. ProscribedFunction
// returns true if function is not supported.
// ======================================================================
if ((Memory::IsPCInRAM () && !MetaMemory::InRAMOSComponent (context.fPC))
&& ::ProscribedFunction (context))
{
gSession->ScheduleDeferredError (
new EmDeferredErrProscribedFunction (context));
}
// ======================================================================
// Tell the profiler that we're entering a function.
// ======================================================================
#if HAS_PROFILING
// This ProfileFnEnter will show the function call within the trap handler
// exception, which gives us a nice breakdown of which traps are being
// called and frequency.
if (gProfilingEnabled && context.fViaTrap)
{
ProfileFnEnter (context.fTrapWord, context.fNextPC);
}
#endif
// ======================================================================
// If this trap is patched, let the patch handler handle the patch.
// ======================================================================
CallROMType result = EmPatchMgr::HandleSystemCall (context);
// ======================================================================
// If we completely handled the function in head and tail patches, tell
// the profiler that we exited the function and get out of here.
// ======================================================================
if (result == kSkipROM)
{
#if HAS_PROFILING
if (gProfilingEnabled)
{
ProfileFnExit (context.fNextPC, context.fTrapWord);
}
#endif
// Set the PC to point past the instructions that got us here.
gCPU->SetPC (context.fNextPC);
// Return true to say that everything has been handled.
#if HAS_PROFILING
gProfilingCounted = oldProfilingCounted;
#endif
return true;
}
// ======================================================================
// If we're profiling, don't dispatch to the ROM function outselves.
// We want the ROM to do it so that we get accurate dispatch times.
// ======================================================================
#if HAS_PROFILING
// Don't do native dispatching if the profiler is on!
if (gProfilingEnabled)
{
// Return false to do default exception handler processing.
gProfilingCounted = oldProfilingCounted;
return false;
}
#endif
#if HAS_PROFILING
gProfilingCounted = oldProfilingCounted;
#endif
// ======================================================================
// Otherwise, let's run the trap dispatcher native. This gives
// us about a 10-20% speed-up.
// ======================================================================
// Push the return address onto the stack. Subtract 4 from the stack,
// and then store the appropriate return address.
gCPU->SetSP (gCPU->GetSP () - 4);
CHECK_STACK_POINTER_DECREMENT ();
EmMemPut32 (gCPU->GetSP (), context.fNextPC);
// Change to the new PC. If possible, use the fully dereferenced destination
// address. This speeds things up for libraries and sub-dispatched managers
// (like Floating Point, International, Text, and Overlay managers).
emuptr destPC = context.fDestPC2;
if (Debug::BreakpointInstalled ())
{
destPC = context.fDestPC1;
}
EmPalmOS::HandleNewPC (destPC);
gCPU->SetPC (destPC);
// Return true to say that everything has been handled.
return true;
}
EmStream& operator << (EmStream& s, const StackRange& range)
{
s << range.fBottom;
s << range.fTop;
s << range.fLowWaterMark;
return s;
}
EmStream& operator >> (EmStream& s, StackRange& range)
{
s >> range.fBottom;
s >> range.fTop;
s >> range.fLowWaterMark;
return s;
}
|