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
|
/*
* Task functions
*
* Copyright 1995 Alexandre Julliard
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "wine/winbase16.h"
#include "user.h"
#include "callback.h"
#include "drive.h"
#include "file.h"
#include "global.h"
#include "instance.h"
#include "message.h"
#include "miscemu.h"
#include "module.h"
#include "neexe.h"
#include "peexe.h"
#include "pe_image.h"
#include "process.h"
#include "queue.h"
#include "selectors.h"
#include "stackframe.h"
#include "task.h"
#include "thread.h"
#include "toolhelp.h"
#include "winnt.h"
#include "winsock.h"
#include "syslevel.h"
#include "debugtools.h"
#include "dosexe.h"
#include "services.h"
#include "server.h"
DEFAULT_DEBUG_CHANNEL(task)
DECLARE_DEBUG_CHANNEL(relay)
DECLARE_DEBUG_CHANNEL(toolhelp)
/* Min. number of thunks allocated when creating a new segment */
#define MIN_THUNKS 32
/* Pointer to function to switch to a larger stack */
int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
static THHOOK DefaultThhook = { 0 };
THHOOK *pThhook = &DefaultThhook;
#define hCurrentTask (pThhook->CurTDB)
#define hFirstTask (pThhook->HeadTDB)
#define hLockedTask (pThhook->LockTDB)
static UINT16 nTaskCount = 0;
/***********************************************************************
* TASK_InstallTHHook
*/
void TASK_InstallTHHook( THHOOK *pNewThhook )
{
THHOOK *pOldThhook = pThhook;
pThhook = pNewThhook? pNewThhook : &DefaultThhook;
*pThhook = *pOldThhook;
}
/***********************************************************************
* TASK_GetNextTask
*/
HTASK16 TASK_GetNextTask( HTASK16 hTask )
{
TDB* pTask = (TDB*)GlobalLock16(hTask);
if (pTask->hNext) return pTask->hNext;
return (hFirstTask != hTask) ? hFirstTask : 0;
}
/***********************************************************************
* TASK_LinkTask
*/
static void TASK_LinkTask( HTASK16 hTask )
{
HTASK16 *prevTask;
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
prevTask = &hFirstTask;
while (*prevTask)
{
TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
if (prevTaskPtr->priority >= pTask->priority) break;
prevTask = &prevTaskPtr->hNext;
}
pTask->hNext = *prevTask;
*prevTask = hTask;
nTaskCount++;
}
/***********************************************************************
* TASK_UnlinkTask
*/
static void TASK_UnlinkTask( HTASK16 hTask )
{
HTASK16 *prevTask;
TDB *pTask;
prevTask = &hFirstTask;
while (*prevTask && (*prevTask != hTask))
{
pTask = (TDB *)GlobalLock16( *prevTask );
prevTask = &pTask->hNext;
}
if (*prevTask)
{
pTask = (TDB *)GlobalLock16( *prevTask );
*prevTask = pTask->hNext;
pTask->hNext = 0;
nTaskCount--;
}
}
/***********************************************************************
* TASK_CreateThunks
*
* Create a thunk free-list in segment 'handle', starting from offset 'offset'
* and containing 'count' entries.
*/
static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
{
int i;
WORD free;
THUNKS *pThunk;
pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
pThunk->next = 0;
pThunk->magic = THUNK_MAGIC;
pThunk->free = (int)&pThunk->thunks - (int)pThunk;
free = pThunk->free;
for (i = 0; i < count-1; i++)
{
free += 8; /* Offset of next thunk */
pThunk->thunks[4*i] = free;
}
pThunk->thunks[4*i] = 0; /* Last thunk */
}
/***********************************************************************
* TASK_AllocThunk
*
* Allocate a thunk for MakeProcInstance().
*/
static SEGPTR TASK_AllocThunk( HTASK16 hTask )
{
TDB *pTask;
THUNKS *pThunk;
WORD sel, base;
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
sel = pTask->hCSAlias;
pThunk = &pTask->thunks;
base = (int)pThunk - (int)pTask;
while (!pThunk->free)
{
sel = pThunk->next;
if (!sel) /* Allocate a new segment */
{
sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
pTask->hPDB, TRUE, FALSE, FALSE );
if (!sel) return (SEGPTR)0;
TASK_CreateThunks( sel, 0, MIN_THUNKS );
pThunk->next = sel;
}
pThunk = (THUNKS *)GlobalLock16( sel );
base = 0;
}
base += pThunk->free;
pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
return PTR_SEG_OFF_TO_SEGPTR( sel, base );
}
/***********************************************************************
* TASK_FreeThunk
*
* Free a MakeProcInstance() thunk.
*/
static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
{
TDB *pTask;
THUNKS *pThunk;
WORD sel, base;
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
sel = pTask->hCSAlias;
pThunk = &pTask->thunks;
base = (int)pThunk - (int)pTask;
while (sel && (sel != HIWORD(thunk)))
{
sel = pThunk->next;
pThunk = (THUNKS *)GlobalLock16( sel );
base = 0;
}
if (!sel) return FALSE;
*(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
pThunk->free = LOWORD(thunk) - base;
return TRUE;
}
/***********************************************************************
* TASK_CallToStart
*
* 32-bit entry point for a new task. This function is responsible for
* setting up the registers and jumping to the 16-bit entry point.
*/
void TASK_CallToStart(void)
{
TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
CONTEXT86 context;
/* Add task to 16-bit scheduler pool */
TASK_Reschedule();
/* Registers at initialization must be:
* ax zero
* bx stack size in bytes
* cx heap size in bytes
* si previous app instance
* di current app instance
* bp zero
* es selector to the PSP
* ds dgroup of the application
* ss stack selector
* sp top of the stack
*/
memset( &context, 0, sizeof(context) );
CS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
DS_reg(&context) = GlobalHandleToSel16(pTask->hInstance);
ES_reg(&context) = pTask->hPDB;
EIP_reg(&context) = pModule->ip;
EBX_reg(&context) = pModule->stack_size;
ECX_reg(&context) = pModule->heap_size;
EDI_reg(&context) = pTask->hInstance;
ESI_reg(&context) = pTask->hPrevInstance;
TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
CS_reg(&context), EIP_reg(&context), DS_reg(&context),
SELECTOROF(pTask->teb->cur_stack),
OFFSETOF(pTask->teb->cur_stack) );
Callbacks->CallRegisterShortProc( &context, 0 );
}
/***********************************************************************
* TASK_Create
*
* NOTE: This routine might be called by a Win32 thread. Thus, we need
* to be careful to protect global data structures. We do this
* by entering the Win16Lock while linking the task into the
* global task list.
*/
BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow)
{
HTASK16 hTask;
TDB *pTask;
LPSTR cmd_line;
char name[10];
PDB *pdb32 = PROCESS_Current();
/* Allocate the task structure */
hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
pModule->self, FALSE, FALSE, FALSE );
if (!hTask) return FALSE;
pTask = (TDB *)GlobalLock16( hTask );
/* Fill the task structure */
pTask->nEvents = 0;
pTask->hSelf = hTask;
pTask->flags = 0;
if (pModule->flags & NE_FFLAGS_WIN32)
pTask->flags |= TDBF_WIN32;
if (pModule->lpDosTask)
pTask->flags |= TDBF_WINOLDAP;
pTask->hInstance = pModule->self;
pTask->hPrevInstance = 0;
/* NOTE: for 16-bit tasks, the instance handles are updated later on
in NE_InitProcess */
pTask->version = pModule->expected_version;
pTask->hModule = pModule->self;
pTask->hParent = GetCurrentTask();
pTask->magic = TDB_MAGIC;
pTask->nCmdShow = cmdShow;
pTask->teb = NtCurrentTeb();
pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
strcpy( pTask->curdir, "\\" );
lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
sizeof(pTask->curdir) - 1 );
/* Create the thunks block */
TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
/* Copy the module name */
GetModuleName16( pModule->self, name, sizeof(name) );
strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
/* Allocate a selector for the PDB */
pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
pModule->self, FALSE, FALSE, FALSE, NULL );
/* Fill the PDB */
pTask->pdb.int20 = 0x20cd;
pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint(
GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
pTask->pdb.fileHandlesPtr =
PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask->hPDB),
(int)&((PDB16 *)0)->fileHandles );
pTask->pdb.hFileHandles = 0;
memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
pTask->pdb.environment = pdb32->env_db->env_sel;
pTask->pdb.nbFiles = 20;
/* Fill the command line */
cmd_line = pdb32->env_db->cmd_line;
while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++;
while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++;
lstrcpynA( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1);
pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
/* Get the compatibility flags */
pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
/* Allocate a code segment alias for the TDB */
pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
sizeof(TDB), pTask->hPDB, TRUE,
FALSE, FALSE, NULL );
/* Set the owner of the environment block */
FarSetOwner16( pTask->pdb.environment, pTask->hPDB );
/* Default DTA overwrites command-line */
pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
(int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
/* Create scheduler event for 16-bit tasks */
if ( !(pTask->flags & TDBF_WIN32) )
{
pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent );
}
/* Enter task handle into thread and process */
pTask->teb->htask16 = pTask->teb->process->task = hTask;
TRACE("module='%s' cmdline='%s' task=%04x\n", name, cmd_line, hTask );
/* Add the task to the linked list */
SYSLEVEL_EnterWin16Lock();
TASK_LinkTask( hTask );
SYSLEVEL_LeaveWin16Lock();
return TRUE;
}
/***********************************************************************
* TASK_DeleteTask
*/
static void TASK_DeleteTask( HTASK16 hTask )
{
TDB *pTask;
HGLOBAL16 hPDB;
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
hPDB = pTask->hPDB;
pTask->magic = 0xdead; /* invalidate signature */
/* Delete the Win32 part of the task */
/* PROCESS_FreePDB( pTask->teb->process ); FIXME */
/* K32OBJ_DecCount( &pTask->teb->header ); FIXME */
/* Free the selector aliases */
GLOBAL_FreeBlock( pTask->hCSAlias );
GLOBAL_FreeBlock( pTask->hPDB );
/* Free the task module */
FreeModule16( pTask->hModule );
/* Free the task structure itself */
GlobalFree16( hTask );
/* Free all memory used by this task (including the 32-bit stack, */
/* the environment block and the thunk segments). */
GlobalFreeAll16( hPDB );
}
/***********************************************************************
* TASK_KillTask
*/
void TASK_KillTask( HTASK16 hTask )
{
TDB *pTask;
/* Enter the Win16Lock to protect global data structures */
SYSLEVEL_EnterWin16Lock();
if ( !hTask ) hTask = GetCurrentTask();
pTask = (TDB *)GlobalLock16( hTask );
if ( !pTask )
{
SYSLEVEL_LeaveWin16Lock();
return;
}
TRACE("Killing task %04x\n", hTask );
#ifdef MZ_SUPPORTED
{
/* Kill DOS VM task */
NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
if ( pModule->lpDosTask )
MZ_KillModule( pModule->lpDosTask );
}
#endif
/* Perform USER cleanup */
TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0, 0 );
PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, GetCurrentThreadId(), 0 );
PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0, 0 );
if (nTaskCount <= 1)
{
TRACE("this is the last task, exiting\n" );
ExitKernel16();
}
/* FIXME: Hack! Send a message to the initial task so that
* the GetMessage wakes up and the initial task can check whether
* it is the only remaining one and terminate itself ...
* The initial task should probably install hooks or something
* to get informed about task termination :-/
*/
Callout.PostAppMessage16( PROCESS_Initial()->task, WM_NULL, 0, 0 );
/* Remove the task from the list to be sure we never switch back to it */
TASK_UnlinkTask( hTask );
if( nTaskCount )
{
TDB* p = (TDB *)GlobalLock16( hFirstTask );
while( p )
{
if( p->hYieldTo == hTask ) p->hYieldTo = 0;
p = (TDB *)GlobalLock16( p->hNext );
}
}
pTask->nEvents = 0;
if ( hLockedTask == hTask )
hLockedTask = 0;
TASK_DeleteTask( hTask );
/* When deleting the current task ... */
if ( hTask == hCurrentTask )
{
DWORD lockCount;
/* ... schedule another one ... */
TASK_Reschedule();
/* ... and completely release the Win16Lock, just in case. */
ReleaseThunkLock( &lockCount );
return;
}
SYSLEVEL_LeaveWin16Lock();
}
/***********************************************************************
* TASK_Reschedule
*
* This is where all the magic of task-switching happens!
*
* 16-bit Windows performs non-preemptive (cooperative) multitasking.
* This means that each 16-bit task runs until it voluntarily yields
* control, at which point the scheduler gets active and selects the
* next task to run.
*
* In Wine, all processes, even 16-bit ones, are scheduled preemptively
* by the standard scheduler of the underlying OS. As many 16-bit apps
* *rely* on the behaviour of the Windows scheduler, however, we have
* to simulate that behaviour.
*
* This is achieved as follows: every 16-bit task is at time (except
* during task creation and deletion) in one of two states: either it
* is the one currently running, then the global variable hCurrentTask
* contains its task handle, or it is not currently running, then it
* is blocked on a special scheduler event, a global handle to which
* is stored in the task struct.
*
* When the current task yields control, this routine gets called. Its
* purpose is to determine the next task to be active, signal the
* scheduler event of that task, and then put the current task to sleep
* waiting for *its* scheduler event to get signalled again.
*
* This routine can get called in a few other special situations as well:
*
* - On creation of a 16-bit task, the Unix process executing the task
* calls TASK_Reschedule once it has completed its initialization.
* At this point, the task needs to be blocked until its scheduler
* event is signalled the first time (this will be done by the parent
* process to get the task up and running).
*
* - When the task currently running terminates itself, this routine gets
* called and has to schedule another task, *without* blocking the
* terminating task.
*
* - When a 32-bit thread posts an event for a 16-bit task, it might be
* the case that *no* 16-bit task is currently running. In this case
* the task that has now an event pending is to be scheduled.
*
*/
void TASK_Reschedule(void)
{
TDB *pOldTask = NULL, *pNewTask = NULL;
HTASK16 hOldTask = 0, hNewTask = 0;
enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
DWORD lockCount;
SYSLEVEL_EnterWin16Lock();
/* Check what we need to do */
hOldTask = GetCurrentTask();
pOldTask = (TDB *)GlobalLock16( hOldTask );
TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
hCurrentTask, hOldTask, (long) getpid() );
if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
{
/* We are called by an active (non-deleted) 16-bit task */
/* If we don't even have a current task, or else the current
task has yielded, we'll need to schedule a new task and
(possibly) put the calling task to sleep. Otherwise, we
only block the caller. */
if ( !hCurrentTask || hCurrentTask == hOldTask )
mode = MODE_YIELD;
else
mode = MODE_SLEEP;
}
else
{
/* We are called by a deleted 16-bit task or a 32-bit thread */
/* The only situation where we need to do something is if we
now do not have a current task. Then, we'll need to wake up
some task that has events pending. */
if ( !hCurrentTask || hCurrentTask == hOldTask )
mode = MODE_WAKEUP;
else
{
/* nothing to do */
SYSLEVEL_LeaveWin16Lock();
return;
}
}
/* Find a task to yield to: check for DirectedYield() */
if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
{
hNewTask = pOldTask->hYieldTo;
pNewTask = (TDB *)GlobalLock16( hNewTask );
if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
pOldTask->hYieldTo = 0;
}
/* Find a task to yield to: check for pending events */
if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
{
hNewTask = hFirstTask;
while (hNewTask)
{
pNewTask = (TDB *)GlobalLock16( hNewTask );
TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
if (pNewTask->nEvents) break;
hNewTask = pNewTask->hNext;
}
if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
}
/* If we are still the task with highest priority, just return ... */
if ( mode == MODE_YIELD && hNewTask == hCurrentTask )
{
TRACE("returning to the current task (%04x)\n", hCurrentTask );
SYSLEVEL_LeaveWin16Lock();
/* Allow Win32 threads to thunk down even while a Win16 task is
in a tight PeekMessage() or Yield() loop ... */
ReleaseThunkLock( &lockCount );
RestoreThunkLock( lockCount );
return;
}
/* If no task to yield to found, suspend 16-bit scheduler ... */
if ( mode == MODE_YIELD && !hNewTask )
{
TRACE("No currently active task\n");
hCurrentTask = 0;
}
/* If we found a task to wake up, do it ... */
if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
{
TRACE("Switching to task %04x (%.8s)\n",
hNewTask, pNewTask->module_name );
pNewTask->priority++;
TASK_UnlinkTask( hNewTask );
TASK_LinkTask( hNewTask );
pNewTask->priority--;
hCurrentTask = hNewTask;
SetEvent( pNewTask->hEvent );
/* This is set just in case some app reads it ... */
pNewTask->ss_sp = pNewTask->teb->cur_stack;
}
/* If we need to put the current task to sleep, do it ... */
if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
{
ResetEvent( pOldTask->hEvent );
ReleaseThunkLock( &lockCount );
SYSLEVEL_CheckNotLevel( 1 );
WaitForSingleObject( pOldTask->hEvent, INFINITE );
RestoreThunkLock( lockCount );
}
SYSLEVEL_LeaveWin16Lock();
}
/***********************************************************************
* InitTask (KERNEL.91)
*
* Called by the application startup code.
*/
void WINAPI InitTask16( CONTEXT86 *context )
{
TDB *pTask;
INSTANCEDATA *pinstance;
SEGPTR ptr;
EAX_reg(context) = 0;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
/* Note: we need to trust that BX/CX contain the stack/heap sizes,
as some apps, notably Visual Basic apps, *modify* the heap/stack
size of the instance data segment before calling InitTask() */
/* Initialize the INSTANCEDATA structure */
pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack );
pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
pinstance->stacktop = ( pinstance->stackmin > BX_reg(context)?
pinstance->stackmin - BX_reg(context) : 0 ) + 150;
/* Initialize the local heap */
if ( CX_reg(context) )
LocalInit16( pTask->hInstance, 0, CX_reg(context) );
/* Initialize implicitly loaded DLLs */
NE_InitializeDLLs( pTask->hModule );
NE_DllProcessAttach( pTask->hModule );
/* Registers on return are:
* ax 1 if OK, 0 on error
* cx stack limit in bytes
* dx cmdShow parameter
* si instance handle of the previous instance
* di instance handle of the new task
* es:bx pointer to command-line inside PSP
*
* 0 (=%bp) is pushed on the stack
*/
ptr = stack16_push( sizeof(WORD) );
*(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
ESP_reg(context) -= 2;
EAX_reg(context) = 1;
if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
else
{
LPBYTE p = &pTask->pdb.cmdLine[1];
while ((*p == ' ') || (*p == '\t')) p++;
EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
}
ECX_reg(context) = pinstance->stacktop;
EDX_reg(context) = pTask->nCmdShow;
ESI_reg(context) = (DWORD)pTask->hPrevInstance;
EDI_reg(context) = (DWORD)pTask->hInstance;
ES_reg (context) = (WORD)pTask->hPDB;
}
/***********************************************************************
* WaitEvent (KERNEL.30)
*/
BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
{
TDB *pTask;
if (!hTask) hTask = GetCurrentTask();
pTask = (TDB *)GlobalLock16( hTask );
if ( !THREAD_IsWin16( NtCurrentTeb() ) )
{
FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
return TRUE;
}
if (pTask->nEvents > 0)
{
pTask->nEvents--;
return FALSE;
}
TASK_Reschedule();
/* When we get back here, we have an event */
if (pTask->nEvents > 0) pTask->nEvents--;
return TRUE;
}
/***********************************************************************
* PostEvent (KERNEL.31)
*/
void WINAPI PostEvent16( HTASK16 hTask )
{
TDB *pTask;
if (!hTask) hTask = GetCurrentTask();
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
if ( !THREAD_IsWin16( pTask->teb ) )
{
FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
return;
}
pTask->nEvents++;
/* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
if ( !THREAD_IsWin16( NtCurrentTeb() ) )
TASK_Reschedule();
}
/***********************************************************************
* SetPriority (KERNEL.32)
*/
void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
{
TDB *pTask;
INT16 newpriority;
if (!hTask) hTask = GetCurrentTask();
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
newpriority = pTask->priority + delta;
if (newpriority < -32) newpriority = -32;
else if (newpriority > 15) newpriority = 15;
pTask->priority = newpriority + 1;
TASK_UnlinkTask( hTask );
TASK_LinkTask( hTask );
pTask->priority--;
}
/***********************************************************************
* LockCurrentTask (KERNEL.33)
*/
HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
{
if (bLock) hLockedTask = GetCurrentTask();
else hLockedTask = 0;
return hLockedTask;
}
/***********************************************************************
* IsTaskLocked (KERNEL.122)
*/
HTASK16 WINAPI IsTaskLocked16(void)
{
return hLockedTask;
}
/***********************************************************************
* OldYield (KERNEL.117)
*/
void WINAPI OldYield16(void)
{
TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
if ( !THREAD_IsWin16( NtCurrentTeb() ) )
{
FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
return;
}
if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
TASK_Reschedule();
if (pCurTask) pCurTask->nEvents--;
}
/***********************************************************************
* WIN32_OldYield16 (KERNEL.447)
*/
void WINAPI WIN32_OldYield16(void)
{
DWORD count;
ReleaseThunkLock(&count);
RestoreThunkLock(count);
}
/***********************************************************************
* DirectedYield (KERNEL.150)
*/
void WINAPI DirectedYield16( HTASK16 hTask )
{
TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
if ( !THREAD_IsWin16( NtCurrentTeb() ) )
{
FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
return;
}
TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
pCurTask->hYieldTo = hTask;
OldYield16();
TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
}
/***********************************************************************
* Yield16 (KERNEL.29)
*/
void WINAPI Yield16(void)
{
TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
if (pCurTask) pCurTask->hYieldTo = 0;
if (pCurTask && pCurTask->hQueue) Callout.UserYield16();
else OldYield16();
}
/***********************************************************************
* KERNEL_490 (KERNEL.490)
*/
HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
{
if ( !someTask ) return 0;
FIXME("(%04x): stub\n", someTask );
return 0;
}
/***********************************************************************
* MakeProcInstance16 (KERNEL.51)
*/
FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
{
BYTE *thunk,*lfunc;
SEGPTR thunkaddr;
if (!func) {
ERR("Ouch ! MakeProcInstance called with func == NULL !\n");
return (FARPROC16)0; /* Windows seems to do the same */
}
if ( GetTaskDS16() !=hInstance )
{
ERR("Problem with hInstance? Got %04x, using %04x instead\n",
hInstance,GetTaskDS16());
hInstance = GetTaskDS16();
}
if (!hInstance) hInstance = CURRENT_DS;
thunkaddr = TASK_AllocThunk( GetCurrentTask() );
if (!thunkaddr) return (FARPROC16)0;
thunk = PTR_SEG_TO_LIN( thunkaddr );
lfunc = PTR_SEG_TO_LIN( func );
TRACE("(%08lx,%04x): got thunk %08lx\n",
(DWORD)func, hInstance, (DWORD)thunkaddr );
if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) ||
((lfunc[0]==0x1e) && (lfunc[1]==0x58))
) {
FIXME("thunk would be useless for %p, overwriting with nop;nop;\n", func );
lfunc[0]=0x90; /* nop */
lfunc[1]=0x90; /* nop */
}
*thunk++ = 0xb8; /* movw instance, %ax */
*thunk++ = (BYTE)(hInstance & 0xff);
*thunk++ = (BYTE)(hInstance >> 8);
*thunk++ = 0xea; /* ljmp func */
*(DWORD *)thunk = (DWORD)func;
return (FARPROC16)thunkaddr;
}
/***********************************************************************
* FreeProcInstance16 (KERNEL.52)
*/
void WINAPI FreeProcInstance16( FARPROC16 func )
{
TRACE("(%08lx)\n", (DWORD)func );
TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
}
/**********************************************************************
* TASK_GetCodeSegment
*
* Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
* and logical segment number of a given code segment.
*
* 'proc' either *is* already a pair of module handle and segment number,
* in which case there's nothing to do. Otherwise, it is a pointer to
* a function, and we need to retrieve the code segment. If the pointer
* happens to point to a thunk, we'll retrieve info about the code segment
* where the function pointed to by the thunk resides, not the thunk itself.
*
* FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
* the function the snoop code will return to ...
*
*/
static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
SEGTABLEENTRY **ppSeg, int *pSegNr )
{
NE_MODULE *pModule = NULL;
SEGTABLEENTRY *pSeg = NULL;
int segNr;
/* Try pair of module handle / segment number */
pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
{
segNr = LOWORD( proc );
if ( segNr && segNr <= pModule->seg_count )
pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
}
/* Try thunk or function */
else
{
BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
WORD selector;
if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
selector = thunk[6] + (thunk[7] << 8);
else
selector = HIWORD( proc );
pModule = NE_GetPtr( GlobalHandle16( selector ) );
pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
if ( pModule )
for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
break;
if ( pModule && segNr > pModule->seg_count )
pSeg = NULL;
}
/* Abort if segment not found */
if ( !pModule || !pSeg )
return FALSE;
/* Return segment data */
if ( ppModule ) *ppModule = pModule;
if ( ppSeg ) *ppSeg = pSeg;
if ( pSegNr ) *pSegNr = segNr;
return TRUE;
}
/**********************************************************************
* GetCodeHandle (KERNEL.93)
*/
HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
{
SEGTABLEENTRY *pSeg;
if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
return (HANDLE16)0;
return pSeg->hSeg;
}
/**********************************************************************
* GetCodeInfo (KERNEL.104)
*/
BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
{
NE_MODULE *pModule;
SEGTABLEENTRY *pSeg;
int segNr;
if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
return FALSE;
/* Fill in segment information */
segInfo->offSegment = pSeg->filepos;
segInfo->cbSegment = pSeg->size;
segInfo->flags = pSeg->flags;
segInfo->cbAlloc = pSeg->minsize;
segInfo->h = pSeg->hSeg;
segInfo->alignShift = pModule->alignment;
if ( segNr == pModule->dgroup )
segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
/* Return module handle in %es */
CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
return TRUE;
}
/**********************************************************************
* DefineHandleTable16 (KERNEL.94)
*/
BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
{
return TRUE; /* FIXME */
}
/***********************************************************************
* SetTaskQueue (KERNEL.34)
*/
HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
{
HQUEUE16 hPrev;
TDB *pTask;
if (!hTask) hTask = GetCurrentTask();
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
hPrev = pTask->hQueue;
pTask->hQueue = hQueue;
return hPrev;
}
/***********************************************************************
* GetTaskQueue (KERNEL.35)
*/
HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
{
TDB *pTask;
if (!hTask) hTask = GetCurrentTask();
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
return pTask->hQueue;
}
/***********************************************************************
* SetThreadQueue (KERNEL.463)
*/
HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
{
TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
HQUEUE16 oldQueue = teb? teb->queue : 0;
if ( teb )
{
teb->queue = hQueue;
if ( GetTaskQueue16( teb->process->task ) == oldQueue )
SetTaskQueue16( teb->process->task, hQueue );
}
return oldQueue;
}
/***********************************************************************
* GetThreadQueue (KERNEL.464)
*/
HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
{
TEB *teb = NULL;
if ( !thread )
teb = NtCurrentTeb();
else if ( HIWORD(thread) )
teb = THREAD_IdToTEB( thread );
else if ( IsTask16( (HTASK16)thread ) )
teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
return (HQUEUE16)(teb? teb->queue : 0);
}
/***********************************************************************
* SetFastQueue (KERNEL.624)
*/
VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
{
TEB *teb = NULL;
if ( !thread )
teb = NtCurrentTeb();
else if ( HIWORD(thread) )
teb = THREAD_IdToTEB( thread );
else if ( IsTask16( (HTASK16)thread ) )
teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
if ( teb ) teb->queue = (HQUEUE16) hQueue;
}
/***********************************************************************
* GetFastQueue (KERNEL.625)
*/
HANDLE WINAPI GetFastQueue16( void )
{
TEB *teb = NtCurrentTeb();
if (!teb) return 0;
if (!teb->queue)
Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
if (!teb->queue)
FIXME("(): should initialize thread-local queue, expect failure!\n" );
return (HANDLE)teb->queue;
}
/***********************************************************************
* SwitchStackTo (KERNEL.108)
*/
void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
{
TDB *pTask;
STACK16FRAME *oldFrame, *newFrame;
INSTANCEDATA *pData;
UINT16 copySize;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
TRACE("old=%04x:%04x new=%04x:%04x\n",
SELECTOROF( pTask->teb->cur_stack ),
OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
/* Save the old stack */
oldFrame = THREAD_STACK16( pTask->teb );
/* pop frame + args and push bp */
pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
+ 2 * sizeof(WORD);
*(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
pData->stacktop = top;
pData->stackmin = ptr;
pData->stackbottom = ptr;
/* Switch to the new stack */
/* Note: we need to take the 3 arguments into account; otherwise,
* the stack will underflow upon return from this function.
*/
copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
newFrame = THREAD_STACK16( pTask->teb );
/* Copy the stack frame and the local variables to the new stack */
memmove( newFrame, oldFrame, copySize );
newFrame->bp = ptr;
*(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
}
/***********************************************************************
* SwitchStackBack (KERNEL.109)
*/
void WINAPI SwitchStackBack16( CONTEXT86 *context )
{
STACK16FRAME *oldFrame, *newFrame;
INSTANCEDATA *pData;
if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
return;
if (!pData->old_ss_sp)
{
WARN("No previous SwitchStackTo\n" );
return;
}
TRACE("restoring stack %04x:%04x\n",
SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
oldFrame = CURRENT_STACK16;
/* Pop bp from the previous stack */
BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
pData->old_ss_sp += sizeof(WORD);
/* Switch back to the old stack */
NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
SS_reg(context) = SELECTOROF(pData->old_ss_sp);
ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
pData->old_ss_sp = 0;
/* Build a stack frame for the return */
newFrame = CURRENT_STACK16;
newFrame->frame32 = oldFrame->frame32;
newFrame->module_cs = oldFrame->module_cs;
newFrame->callfrom_ip = oldFrame->callfrom_ip;
newFrame->entry_ip = oldFrame->entry_ip;
}
/***********************************************************************
* GetTaskQueueDS16 (KERNEL.118)
*/
void WINAPI GetTaskQueueDS16(void)
{
CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
}
/***********************************************************************
* GetTaskQueueES16 (KERNEL.119)
*/
void WINAPI GetTaskQueueES16(void)
{
CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
}
/***********************************************************************
* GetCurrentTask (KERNEL.36)
*/
HTASK16 WINAPI GetCurrentTask(void)
{
return PROCESS_Current()->task;
}
DWORD WINAPI WIN16_GetCurrentTask(void)
{
/* This is the version used by relay code; the first task is */
/* returned in the high word of the result */
return MAKELONG( GetCurrentTask(), hFirstTask );
}
/***********************************************************************
* GetCurrentPDB (KERNEL.37)
*
* UNDOC: returns PSP of KERNEL in high word
*/
DWORD WINAPI GetCurrentPDB16(void)
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
return MAKELONG(pTask->hPDB, 0); /* FIXME */
}
/***********************************************************************
* GetCurPID16 (KERNEL.157)
*/
DWORD WINAPI GetCurPID16( DWORD unused )
{
return 0;
}
/***********************************************************************
* GetInstanceData (KERNEL.54)
*/
INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
{
char *ptr = (char *)GlobalLock16( instance );
if (!ptr || !len) return 0;
if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
return len;
}
/***********************************************************************
* GetExeVersion (KERNEL.105)
*/
WORD WINAPI GetExeVersion16(void)
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
return pTask->version;
}
/***********************************************************************
* SetErrorMode16 (KERNEL.107)
*/
UINT16 WINAPI SetErrorMode16( UINT16 mode )
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
pTask->error_mode = mode;
return SetErrorMode( mode );
}
/***********************************************************************
* GetDOSEnvironment (KERNEL.131)
*/
SEGPTR WINAPI GetDOSEnvironment16(void)
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
}
/***********************************************************************
* GetNumTasks (KERNEL.152)
*/
UINT16 WINAPI GetNumTasks16(void)
{
return nTaskCount;
}
/***********************************************************************
* GetTaskDS (KERNEL.155)
*
* Note: this function apparently returns a DWORD with LOWORD == HIWORD.
* I don't think we need to bother with this.
*/
HINSTANCE16 WINAPI GetTaskDS16(void)
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
return pTask->hInstance;
}
/***********************************************************************
* GetDummyModuleHandleDS (KERNEL.602)
*/
WORD WINAPI GetDummyModuleHandleDS16(void)
{
TDB *pTask;
WORD selector;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
if (!(pTask->flags & TDBF_WIN32)) return 0;
selector = GlobalHandleToSel16( pTask->hModule );
CURRENT_DS = selector;
return selector;
}
/***********************************************************************
* IsTask (KERNEL.320)
*/
BOOL16 WINAPI IsTask16( HTASK16 hTask )
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
return (pTask->magic == TDB_MAGIC);
}
/***********************************************************************
* IsWinOldApTask16 (KERNEL.158)
*/
BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
{
/* should return bit 0 of byte 0x48 in PSP */
return FALSE;
}
/***********************************************************************
* SetTaskSignalProc (KERNEL.38)
*/
FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
{
TDB *pTask;
FARPROC16 oldProc;
if (!hTask) hTask = GetCurrentTask();
if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
oldProc = pTask->userhandler;
pTask->userhandler = proc;
return oldProc;
}
/***********************************************************************
* TASK_CallTaskSignalProc
*/
/* ### start build ### */
extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
/* ### stop build ### */
void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
{
TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
if ( !pTask || !pTask->userhandler ) return;
TASK_CallTo16_word_wwwww( pTask->userhandler,
hTaskOrModule, uCode, 0,
pTask->hInstance, pTask->hQueue );
}
/***********************************************************************
* SetSigHandler (KERNEL.140)
*/
WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
UINT16 *oldmode, UINT16 newmode, UINT16 flag )
{
FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
newhandler,oldhandler,oldmode,newmode,flag );
if (flag != 1) return 0;
if (!newmode) newhandler = NULL; /* Default handler */
if (newmode != 4)
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
if (oldmode) *oldmode = pTask->signal_flags;
pTask->signal_flags = newmode;
if (oldhandler) *oldhandler = pTask->sighandler;
pTask->sighandler = newhandler;
}
return 0;
}
/***********************************************************************
* GlobalNotify (KERNEL.154)
*
* Note that GlobalNotify does _not_ return the old NotifyProc
* -- contrary to LocalNotify !!
*/
VOID WINAPI GlobalNotify16( FARPROC16 proc )
{
TDB *pTask;
if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
pTask->discardhandler = proc;
}
/***********************************************************************
* GetExePtr (KERNEL.133)
*/
static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
{
char *ptr;
HANDLE16 owner;
/* Check for module handle */
if (!(ptr = GlobalLock16( handle ))) return 0;
if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
/* Search for this handle inside all tasks */
*hTask = hFirstTask;
while (*hTask)
{
TDB *pTask = (TDB *)GlobalLock16( *hTask );
if ((*hTask == handle) ||
(pTask->hInstance == handle) ||
(pTask->hQueue == handle) ||
(pTask->hPDB == handle)) return pTask->hModule;
*hTask = pTask->hNext;
}
/* Check the owner for module handle */
owner = FarGetOwner16( handle );
if (!(ptr = GlobalLock16( owner ))) return 0;
if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
/* Search for the owner inside all tasks */
*hTask = hFirstTask;
while (*hTask)
{
TDB *pTask = (TDB *)GlobalLock16( *hTask );
if ((*hTask == owner) ||
(pTask->hInstance == owner) ||
(pTask->hQueue == owner) ||
(pTask->hPDB == owner)) return pTask->hModule;
*hTask = pTask->hNext;
}
return 0;
}
HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
{
HTASK16 hTask = 0;
HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
STACK16FRAME *frame = CURRENT_STACK16;
frame->ecx = hModule;
if (hTask) frame->es = hTask;
return hModule;
}
HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
{
HTASK16 hTask = 0;
return GetExePtrHelper( handle, &hTask );
}
/***********************************************************************
* TaskFirst (TOOLHELP.63)
*/
BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
{
lpte->hNext = hFirstTask;
return TaskNext16( lpte );
}
/***********************************************************************
* TaskNext (TOOLHELP.64)
*/
BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
{
TDB *pTask;
INSTANCEDATA *pInstData;
TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
if (!lpte->hNext) return FALSE;
pTask = (TDB *)GlobalLock16( lpte->hNext );
if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( pTask->hInstance, 0 );
lpte->hTask = lpte->hNext;
lpte->hTaskParent = pTask->hParent;
lpte->hInst = pTask->hInstance;
lpte->hModule = pTask->hModule;
lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
lpte->wStackTop = pInstData->stacktop;
lpte->wStackMinimum = pInstData->stackmin;
lpte->wStackBottom = pInstData->stackbottom;
lpte->wcEvents = pTask->nEvents;
lpte->hQueue = pTask->hQueue;
lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
lpte->wPSPOffset = 0x100; /*??*/
lpte->hNext = pTask->hNext;
return TRUE;
}
/***********************************************************************
* TaskFindHandle (TOOLHELP.65)
*/
BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
{
lpte->hNext = hTask;
return TaskNext16( lpte );
}
/***********************************************************************
* GetAppCompatFlags16 (KERNEL.354)
*/
DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
{
return GetAppCompatFlags( hTask );
}
/***********************************************************************
* GetAppCompatFlags32 (USER32.206)
*/
DWORD WINAPI GetAppCompatFlags( HTASK hTask )
{
TDB *pTask;
if (!hTask) hTask = GetCurrentTask();
if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
return pTask->compat_flags;
}
|