Added support for WODM_BREAKLOOP message.
[wine.git] / loader / task.c
blob118696bf38f386737e664ee5e4f7b5f1f2e5b300
1 /*
2 * Task functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <unistd.h>
12 #include "wine/winbase16.h"
13 #include "user.h"
14 #include "callback.h"
15 #include "drive.h"
16 #include "file.h"
17 #include "global.h"
18 #include "instance.h"
19 #include "message.h"
20 #include "miscemu.h"
21 #include "module.h"
22 #include "neexe.h"
23 #include "peexe.h"
24 #include "pe_image.h"
25 #include "process.h"
26 #include "queue.h"
27 #include "selectors.h"
28 #include "stackframe.h"
29 #include "task.h"
30 #include "thread.h"
31 #include "toolhelp.h"
32 #include "winnt.h"
33 #include "winsock.h"
34 #include "syslevel.h"
35 #include "debugtools.h"
36 #include "dosexe.h"
37 #include "services.h"
38 #include "server.h"
40 DEFAULT_DEBUG_CHANNEL(task)
41 DECLARE_DEBUG_CHANNEL(relay)
42 DECLARE_DEBUG_CHANNEL(toolhelp)
44 /* Min. number of thunks allocated when creating a new segment */
45 #define MIN_THUNKS 32
47 /* Pointer to function to switch to a larger stack */
48 int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
50 static THHOOK DefaultThhook = { 0 };
51 THHOOK *pThhook = &DefaultThhook;
53 #define hCurrentTask (pThhook->CurTDB)
54 #define hFirstTask (pThhook->HeadTDB)
55 #define hLockedTask (pThhook->LockTDB)
57 static UINT16 nTaskCount = 0;
60 /***********************************************************************
61 * TASK_InstallTHHook
63 void TASK_InstallTHHook( THHOOK *pNewThhook )
65 THHOOK *pOldThhook = pThhook;
67 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
69 *pThhook = *pOldThhook;
72 /***********************************************************************
73 * TASK_GetNextTask
75 HTASK16 TASK_GetNextTask( HTASK16 hTask )
77 TDB* pTask = (TDB*)GlobalLock16(hTask);
79 if (pTask->hNext) return pTask->hNext;
80 return (hFirstTask != hTask) ? hFirstTask : 0;
83 /***********************************************************************
84 * TASK_LinkTask
86 static void TASK_LinkTask( HTASK16 hTask )
88 HTASK16 *prevTask;
89 TDB *pTask;
91 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
92 prevTask = &hFirstTask;
93 while (*prevTask)
95 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
96 if (prevTaskPtr->priority >= pTask->priority) break;
97 prevTask = &prevTaskPtr->hNext;
99 pTask->hNext = *prevTask;
100 *prevTask = hTask;
101 nTaskCount++;
105 /***********************************************************************
106 * TASK_UnlinkTask
108 static void TASK_UnlinkTask( HTASK16 hTask )
110 HTASK16 *prevTask;
111 TDB *pTask;
113 prevTask = &hFirstTask;
114 while (*prevTask && (*prevTask != hTask))
116 pTask = (TDB *)GlobalLock16( *prevTask );
117 prevTask = &pTask->hNext;
119 if (*prevTask)
121 pTask = (TDB *)GlobalLock16( *prevTask );
122 *prevTask = pTask->hNext;
123 pTask->hNext = 0;
124 nTaskCount--;
129 /***********************************************************************
130 * TASK_CreateThunks
132 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
133 * and containing 'count' entries.
135 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
137 int i;
138 WORD free;
139 THUNKS *pThunk;
141 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
142 pThunk->next = 0;
143 pThunk->magic = THUNK_MAGIC;
144 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
145 free = pThunk->free;
146 for (i = 0; i < count-1; i++)
148 free += 8; /* Offset of next thunk */
149 pThunk->thunks[4*i] = free;
151 pThunk->thunks[4*i] = 0; /* Last thunk */
155 /***********************************************************************
156 * TASK_AllocThunk
158 * Allocate a thunk for MakeProcInstance().
160 static SEGPTR TASK_AllocThunk( HTASK16 hTask )
162 TDB *pTask;
163 THUNKS *pThunk;
164 WORD sel, base;
166 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
167 sel = pTask->hCSAlias;
168 pThunk = &pTask->thunks;
169 base = (int)pThunk - (int)pTask;
170 while (!pThunk->free)
172 sel = pThunk->next;
173 if (!sel) /* Allocate a new segment */
175 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
176 pTask->hPDB, TRUE, FALSE, FALSE );
177 if (!sel) return (SEGPTR)0;
178 TASK_CreateThunks( sel, 0, MIN_THUNKS );
179 pThunk->next = sel;
181 pThunk = (THUNKS *)GlobalLock16( sel );
182 base = 0;
184 base += pThunk->free;
185 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
186 return PTR_SEG_OFF_TO_SEGPTR( sel, base );
190 /***********************************************************************
191 * TASK_FreeThunk
193 * Free a MakeProcInstance() thunk.
195 static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
197 TDB *pTask;
198 THUNKS *pThunk;
199 WORD sel, base;
201 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
202 sel = pTask->hCSAlias;
203 pThunk = &pTask->thunks;
204 base = (int)pThunk - (int)pTask;
205 while (sel && (sel != HIWORD(thunk)))
207 sel = pThunk->next;
208 pThunk = (THUNKS *)GlobalLock16( sel );
209 base = 0;
211 if (!sel) return FALSE;
212 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
213 pThunk->free = LOWORD(thunk) - base;
214 return TRUE;
218 /***********************************************************************
219 * TASK_CallToStart
221 * 32-bit entry point for a new task. This function is responsible for
222 * setting up the registers and jumping to the 16-bit entry point.
224 void TASK_CallToStart(void)
226 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
227 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
228 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
229 CONTEXT86 context;
231 /* Add task to 16-bit scheduler pool */
232 TASK_Reschedule();
234 /* Registers at initialization must be:
235 * ax zero
236 * bx stack size in bytes
237 * cx heap size in bytes
238 * si previous app instance
239 * di current app instance
240 * bp zero
241 * es selector to the PSP
242 * ds dgroup of the application
243 * ss stack selector
244 * sp top of the stack
247 memset( &context, 0, sizeof(context) );
248 CS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
249 DS_reg(&context) = GlobalHandleToSel16(pTask->hInstance);
250 ES_reg(&context) = pTask->hPDB;
251 EIP_reg(&context) = pModule->ip;
252 EBX_reg(&context) = pModule->stack_size;
253 ECX_reg(&context) = pModule->heap_size;
254 EDI_reg(&context) = pTask->hInstance;
255 ESI_reg(&context) = pTask->hPrevInstance;
257 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
258 CS_reg(&context), EIP_reg(&context), DS_reg(&context),
259 SELECTOROF(pTask->teb->cur_stack),
260 OFFSETOF(pTask->teb->cur_stack) );
262 Callbacks->CallRegisterShortProc( &context, 0 );
266 /***********************************************************************
267 * TASK_Create
269 * NOTE: This routine might be called by a Win32 thread. Thus, we need
270 * to be careful to protect global data structures. We do this
271 * by entering the Win16Lock while linking the task into the
272 * global task list.
274 BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow)
276 HTASK16 hTask;
277 TDB *pTask;
278 LPSTR cmd_line;
279 char name[10];
280 PDB *pdb32 = PROCESS_Current();
282 /* Allocate the task structure */
284 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
285 pModule->self, FALSE, FALSE, FALSE );
286 if (!hTask) return FALSE;
287 pTask = (TDB *)GlobalLock16( hTask );
289 /* Fill the task structure */
291 pTask->nEvents = 0;
292 pTask->hSelf = hTask;
293 pTask->flags = 0;
295 if (pModule->flags & NE_FFLAGS_WIN32)
296 pTask->flags |= TDBF_WIN32;
297 if (pModule->lpDosTask)
298 pTask->flags |= TDBF_WINOLDAP;
300 pTask->hInstance = pModule->self;
301 pTask->hPrevInstance = 0;
302 /* NOTE: for 16-bit tasks, the instance handles are updated later on
303 in NE_InitProcess */
305 pTask->version = pModule->expected_version;
306 pTask->hModule = pModule->self;
307 pTask->hParent = GetCurrentTask();
308 pTask->magic = TDB_MAGIC;
309 pTask->nCmdShow = cmdShow;
310 pTask->teb = NtCurrentTeb();
311 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
312 strcpy( pTask->curdir, "\\" );
313 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
314 sizeof(pTask->curdir) - 1 );
316 /* Create the thunks block */
318 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
320 /* Copy the module name */
322 GetModuleName16( pModule->self, name, sizeof(name) );
323 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
325 /* Allocate a selector for the PDB */
327 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
328 pModule->self, FALSE, FALSE, FALSE, NULL );
330 /* Fill the PDB */
332 pTask->pdb.int20 = 0x20cd;
333 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
334 PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint(
335 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
336 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
337 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
338 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
339 pTask->pdb.fileHandlesPtr =
340 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask->hPDB),
341 (int)&((PDB16 *)0)->fileHandles );
342 pTask->pdb.hFileHandles = 0;
343 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
344 pTask->pdb.environment = pdb32->env_db->env_sel;
345 pTask->pdb.nbFiles = 20;
347 /* Fill the command line */
349 cmd_line = pdb32->env_db->cmd_line;
350 while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++;
351 while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++;
352 lstrcpynA( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1);
353 pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
355 /* Get the compatibility flags */
357 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
359 /* Allocate a code segment alias for the TDB */
361 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
362 sizeof(TDB), pTask->hPDB, TRUE,
363 FALSE, FALSE, NULL );
365 /* Set the owner of the environment block */
367 FarSetOwner16( pTask->pdb.environment, pTask->hPDB );
369 /* Default DTA overwrites command-line */
371 pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
372 (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
374 /* Create scheduler event for 16-bit tasks */
376 if ( !(pTask->flags & TDBF_WIN32) )
378 pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
379 pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent );
382 /* Enter task handle into thread and process */
384 pTask->teb->htask16 = pTask->teb->process->task = hTask;
385 TRACE("module='%s' cmdline='%s' task=%04x\n", name, cmd_line, hTask );
387 /* Add the task to the linked list */
389 SYSLEVEL_EnterWin16Lock();
390 TASK_LinkTask( hTask );
391 SYSLEVEL_LeaveWin16Lock();
393 return TRUE;
396 /***********************************************************************
397 * TASK_DeleteTask
399 static void TASK_DeleteTask( HTASK16 hTask )
401 TDB *pTask;
402 HGLOBAL16 hPDB;
404 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
405 hPDB = pTask->hPDB;
407 pTask->magic = 0xdead; /* invalidate signature */
409 /* Delete the Win32 part of the task */
411 /* PROCESS_FreePDB( pTask->teb->process ); FIXME */
412 /* K32OBJ_DecCount( &pTask->teb->header ); FIXME */
414 /* Free the selector aliases */
416 GLOBAL_FreeBlock( pTask->hCSAlias );
417 GLOBAL_FreeBlock( pTask->hPDB );
419 /* Free the task module */
421 FreeModule16( pTask->hModule );
423 /* Free the task structure itself */
425 GlobalFree16( hTask );
427 /* Free all memory used by this task (including the 32-bit stack, */
428 /* the environment block and the thunk segments). */
430 GlobalFreeAll16( hPDB );
433 /***********************************************************************
434 * TASK_KillTask
436 void TASK_KillTask( HTASK16 hTask )
438 TDB *pTask;
440 /* Enter the Win16Lock to protect global data structures */
441 SYSLEVEL_EnterWin16Lock();
443 if ( !hTask ) hTask = GetCurrentTask();
444 pTask = (TDB *)GlobalLock16( hTask );
445 if ( !pTask )
447 SYSLEVEL_LeaveWin16Lock();
448 return;
451 TRACE("Killing task %04x\n", hTask );
453 #ifdef MZ_SUPPORTED
455 /* Kill DOS VM task */
456 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
457 if ( pModule->lpDosTask )
458 MZ_KillModule( pModule->lpDosTask );
460 #endif
462 /* Perform USER cleanup */
464 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
465 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0, 0 );
466 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, GetCurrentThreadId(), 0 );
467 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0, 0 );
469 if (nTaskCount <= 1)
471 TRACE("this is the last task, exiting\n" );
472 ExitKernel16();
475 /* FIXME: Hack! Send a message to the initial task so that
476 * the GetMessage wakes up and the initial task can check whether
477 * it is the only remaining one and terminate itself ...
478 * The initial task should probably install hooks or something
479 * to get informed about task termination :-/
481 Callout.PostAppMessage16( PROCESS_Initial()->task, WM_NULL, 0, 0 );
483 /* Remove the task from the list to be sure we never switch back to it */
484 TASK_UnlinkTask( hTask );
485 if( nTaskCount )
487 TDB* p = (TDB *)GlobalLock16( hFirstTask );
488 while( p )
490 if( p->hYieldTo == hTask ) p->hYieldTo = 0;
491 p = (TDB *)GlobalLock16( p->hNext );
495 pTask->nEvents = 0;
497 if ( hLockedTask == hTask )
498 hLockedTask = 0;
500 TASK_DeleteTask( hTask );
502 /* When deleting the current task ... */
503 if ( hTask == hCurrentTask )
505 DWORD lockCount;
507 /* ... schedule another one ... */
508 TASK_Reschedule();
510 /* ... and completely release the Win16Lock, just in case. */
511 ReleaseThunkLock( &lockCount );
513 return;
516 SYSLEVEL_LeaveWin16Lock();
519 /***********************************************************************
520 * TASK_Reschedule
522 * This is where all the magic of task-switching happens!
524 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
525 * This means that each 16-bit task runs until it voluntarily yields
526 * control, at which point the scheduler gets active and selects the
527 * next task to run.
529 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
530 * by the standard scheduler of the underlying OS. As many 16-bit apps
531 * *rely* on the behaviour of the Windows scheduler, however, we have
532 * to simulate that behaviour.
534 * This is achieved as follows: every 16-bit task is at time (except
535 * during task creation and deletion) in one of two states: either it
536 * is the one currently running, then the global variable hCurrentTask
537 * contains its task handle, or it is not currently running, then it
538 * is blocked on a special scheduler event, a global handle to which
539 * is stored in the task struct.
541 * When the current task yields control, this routine gets called. Its
542 * purpose is to determine the next task to be active, signal the
543 * scheduler event of that task, and then put the current task to sleep
544 * waiting for *its* scheduler event to get signalled again.
546 * This routine can get called in a few other special situations as well:
548 * - On creation of a 16-bit task, the Unix process executing the task
549 * calls TASK_Reschedule once it has completed its initialization.
550 * At this point, the task needs to be blocked until its scheduler
551 * event is signalled the first time (this will be done by the parent
552 * process to get the task up and running).
554 * - When the task currently running terminates itself, this routine gets
555 * called and has to schedule another task, *without* blocking the
556 * terminating task.
558 * - When a 32-bit thread posts an event for a 16-bit task, it might be
559 * the case that *no* 16-bit task is currently running. In this case
560 * the task that has now an event pending is to be scheduled.
563 void TASK_Reschedule(void)
565 TDB *pOldTask = NULL, *pNewTask = NULL;
566 HTASK16 hOldTask = 0, hNewTask = 0;
567 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
568 DWORD lockCount;
570 SYSLEVEL_EnterWin16Lock();
572 /* Check what we need to do */
573 hOldTask = GetCurrentTask();
574 pOldTask = (TDB *)GlobalLock16( hOldTask );
575 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
576 hCurrentTask, hOldTask, (long) getpid() );
578 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
580 /* We are called by an active (non-deleted) 16-bit task */
582 /* If we don't even have a current task, or else the current
583 task has yielded, we'll need to schedule a new task and
584 (possibly) put the calling task to sleep. Otherwise, we
585 only block the caller. */
587 if ( !hCurrentTask || hCurrentTask == hOldTask )
588 mode = MODE_YIELD;
589 else
590 mode = MODE_SLEEP;
592 else
594 /* We are called by a deleted 16-bit task or a 32-bit thread */
596 /* The only situation where we need to do something is if we
597 now do not have a current task. Then, we'll need to wake up
598 some task that has events pending. */
600 if ( !hCurrentTask || hCurrentTask == hOldTask )
601 mode = MODE_WAKEUP;
602 else
604 /* nothing to do */
605 SYSLEVEL_LeaveWin16Lock();
606 return;
610 /* Find a task to yield to: check for DirectedYield() */
611 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
613 hNewTask = pOldTask->hYieldTo;
614 pNewTask = (TDB *)GlobalLock16( hNewTask );
615 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
616 pOldTask->hYieldTo = 0;
619 /* Find a task to yield to: check for pending events */
620 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
622 hNewTask = hFirstTask;
623 while (hNewTask)
625 pNewTask = (TDB *)GlobalLock16( hNewTask );
627 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
629 if (pNewTask->nEvents) break;
630 hNewTask = pNewTask->hNext;
632 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
635 /* If we are still the task with highest priority, just return ... */
636 if ( mode == MODE_YIELD && hNewTask == hCurrentTask )
638 TRACE("returning to the current task (%04x)\n", hCurrentTask );
639 SYSLEVEL_LeaveWin16Lock();
641 /* Allow Win32 threads to thunk down even while a Win16 task is
642 in a tight PeekMessage() or Yield() loop ... */
643 ReleaseThunkLock( &lockCount );
644 RestoreThunkLock( lockCount );
645 return;
648 /* If no task to yield to found, suspend 16-bit scheduler ... */
649 if ( mode == MODE_YIELD && !hNewTask )
651 TRACE("No currently active task\n");
652 hCurrentTask = 0;
655 /* If we found a task to wake up, do it ... */
656 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
658 TRACE("Switching to task %04x (%.8s)\n",
659 hNewTask, pNewTask->module_name );
661 pNewTask->priority++;
662 TASK_UnlinkTask( hNewTask );
663 TASK_LinkTask( hNewTask );
664 pNewTask->priority--;
666 hCurrentTask = hNewTask;
667 SetEvent( pNewTask->hEvent );
669 /* This is set just in case some app reads it ... */
670 pNewTask->ss_sp = pNewTask->teb->cur_stack;
673 /* If we need to put the current task to sleep, do it ... */
674 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
676 ResetEvent( pOldTask->hEvent );
678 ReleaseThunkLock( &lockCount );
679 SYSLEVEL_CheckNotLevel( 1 );
680 WaitForSingleObject( pOldTask->hEvent, INFINITE );
681 RestoreThunkLock( lockCount );
684 SYSLEVEL_LeaveWin16Lock();
687 /***********************************************************************
688 * InitTask (KERNEL.91)
690 * Called by the application startup code.
692 void WINAPI InitTask16( CONTEXT86 *context )
694 TDB *pTask;
695 INSTANCEDATA *pinstance;
696 SEGPTR ptr;
698 EAX_reg(context) = 0;
699 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
701 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
702 as some apps, notably Visual Basic apps, *modify* the heap/stack
703 size of the instance data segment before calling InitTask() */
705 /* Initialize the INSTANCEDATA structure */
706 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
707 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack );
708 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
709 pinstance->stacktop = ( pinstance->stackmin > BX_reg(context)?
710 pinstance->stackmin - BX_reg(context) : 0 ) + 150;
712 /* Initialize the local heap */
713 if ( CX_reg(context) )
714 LocalInit16( pTask->hInstance, 0, CX_reg(context) );
716 /* Initialize implicitly loaded DLLs */
717 NE_InitializeDLLs( pTask->hModule );
718 NE_DllProcessAttach( pTask->hModule );
720 /* Registers on return are:
721 * ax 1 if OK, 0 on error
722 * cx stack limit in bytes
723 * dx cmdShow parameter
724 * si instance handle of the previous instance
725 * di instance handle of the new task
726 * es:bx pointer to command-line inside PSP
728 * 0 (=%bp) is pushed on the stack
730 ptr = stack16_push( sizeof(WORD) );
731 *(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
732 ESP_reg(context) -= 2;
734 EAX_reg(context) = 1;
736 if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
737 else
739 LPBYTE p = &pTask->pdb.cmdLine[1];
740 while ((*p == ' ') || (*p == '\t')) p++;
741 EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
743 ECX_reg(context) = pinstance->stacktop;
744 EDX_reg(context) = pTask->nCmdShow;
745 ESI_reg(context) = (DWORD)pTask->hPrevInstance;
746 EDI_reg(context) = (DWORD)pTask->hInstance;
747 ES_reg (context) = (WORD)pTask->hPDB;
751 /***********************************************************************
752 * WaitEvent (KERNEL.30)
754 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
756 TDB *pTask;
758 if (!hTask) hTask = GetCurrentTask();
759 pTask = (TDB *)GlobalLock16( hTask );
761 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
763 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
764 return TRUE;
767 if (pTask->nEvents > 0)
769 pTask->nEvents--;
770 return FALSE;
772 TASK_Reschedule();
774 /* When we get back here, we have an event */
776 if (pTask->nEvents > 0) pTask->nEvents--;
777 return TRUE;
781 /***********************************************************************
782 * PostEvent (KERNEL.31)
784 void WINAPI PostEvent16( HTASK16 hTask )
786 TDB *pTask;
788 if (!hTask) hTask = GetCurrentTask();
789 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
791 if ( !THREAD_IsWin16( pTask->teb ) )
793 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
794 return;
797 pTask->nEvents++;
799 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
800 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
801 TASK_Reschedule();
805 /***********************************************************************
806 * SetPriority (KERNEL.32)
808 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
810 TDB *pTask;
811 INT16 newpriority;
813 if (!hTask) hTask = GetCurrentTask();
814 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
815 newpriority = pTask->priority + delta;
816 if (newpriority < -32) newpriority = -32;
817 else if (newpriority > 15) newpriority = 15;
819 pTask->priority = newpriority + 1;
820 TASK_UnlinkTask( hTask );
821 TASK_LinkTask( hTask );
822 pTask->priority--;
826 /***********************************************************************
827 * LockCurrentTask (KERNEL.33)
829 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
831 if (bLock) hLockedTask = GetCurrentTask();
832 else hLockedTask = 0;
833 return hLockedTask;
837 /***********************************************************************
838 * IsTaskLocked (KERNEL.122)
840 HTASK16 WINAPI IsTaskLocked16(void)
842 return hLockedTask;
846 /***********************************************************************
847 * OldYield (KERNEL.117)
849 void WINAPI OldYield16(void)
851 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
853 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
855 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
856 return;
859 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
860 TASK_Reschedule();
861 if (pCurTask) pCurTask->nEvents--;
864 /***********************************************************************
865 * WIN32_OldYield16 (KERNEL.447)
867 void WINAPI WIN32_OldYield16(void)
869 DWORD count;
871 ReleaseThunkLock(&count);
872 RestoreThunkLock(count);
875 /***********************************************************************
876 * DirectedYield (KERNEL.150)
878 void WINAPI DirectedYield16( HTASK16 hTask )
880 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
882 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
884 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
885 return;
888 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
890 pCurTask->hYieldTo = hTask;
891 OldYield16();
893 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
896 /***********************************************************************
897 * Yield16 (KERNEL.29)
899 void WINAPI Yield16(void)
901 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
903 if (pCurTask) pCurTask->hYieldTo = 0;
904 if (pCurTask && pCurTask->hQueue) Callout.UserYield16();
905 else OldYield16();
908 /***********************************************************************
909 * KERNEL_490 (KERNEL.490)
911 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
913 if ( !someTask ) return 0;
915 FIXME("(%04x): stub\n", someTask );
916 return 0;
919 /***********************************************************************
920 * MakeProcInstance16 (KERNEL.51)
922 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
924 BYTE *thunk,*lfunc;
925 SEGPTR thunkaddr;
927 if (!func) {
928 ERR("Ouch ! MakeProcInstance called with func == NULL !\n");
929 return (FARPROC16)0; /* Windows seems to do the same */
931 if ( GetTaskDS16() !=hInstance )
933 ERR("Problem with hInstance? Got %04x, using %04x instead\n",
934 hInstance,GetTaskDS16());
935 hInstance = GetTaskDS16();
937 if (!hInstance) hInstance = CURRENT_DS;
938 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
939 if (!thunkaddr) return (FARPROC16)0;
940 thunk = PTR_SEG_TO_LIN( thunkaddr );
941 lfunc = PTR_SEG_TO_LIN( func );
943 TRACE("(%08lx,%04x): got thunk %08lx\n",
944 (DWORD)func, hInstance, (DWORD)thunkaddr );
945 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) ||
946 ((lfunc[0]==0x1e) && (lfunc[1]==0x58))
948 FIXME("thunk would be useless for %p, overwriting with nop;nop;\n", func );
949 lfunc[0]=0x90; /* nop */
950 lfunc[1]=0x90; /* nop */
953 *thunk++ = 0xb8; /* movw instance, %ax */
954 *thunk++ = (BYTE)(hInstance & 0xff);
955 *thunk++ = (BYTE)(hInstance >> 8);
956 *thunk++ = 0xea; /* ljmp func */
957 *(DWORD *)thunk = (DWORD)func;
958 return (FARPROC16)thunkaddr;
962 /***********************************************************************
963 * FreeProcInstance16 (KERNEL.52)
965 void WINAPI FreeProcInstance16( FARPROC16 func )
967 TRACE("(%08lx)\n", (DWORD)func );
968 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
971 /**********************************************************************
972 * TASK_GetCodeSegment
974 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
975 * and logical segment number of a given code segment.
977 * 'proc' either *is* already a pair of module handle and segment number,
978 * in which case there's nothing to do. Otherwise, it is a pointer to
979 * a function, and we need to retrieve the code segment. If the pointer
980 * happens to point to a thunk, we'll retrieve info about the code segment
981 * where the function pointed to by the thunk resides, not the thunk itself.
983 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
984 * the function the snoop code will return to ...
987 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
988 SEGTABLEENTRY **ppSeg, int *pSegNr )
990 NE_MODULE *pModule = NULL;
991 SEGTABLEENTRY *pSeg = NULL;
992 int segNr;
994 /* Try pair of module handle / segment number */
995 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
996 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
998 segNr = LOWORD( proc );
999 if ( segNr && segNr <= pModule->seg_count )
1000 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
1003 /* Try thunk or function */
1004 else
1006 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
1007 WORD selector;
1009 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1010 selector = thunk[6] + (thunk[7] << 8);
1011 else
1012 selector = HIWORD( proc );
1014 pModule = NE_GetPtr( GlobalHandle16( selector ) );
1015 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
1017 if ( pModule )
1018 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
1019 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
1020 break;
1022 if ( pModule && segNr > pModule->seg_count )
1023 pSeg = NULL;
1026 /* Abort if segment not found */
1028 if ( !pModule || !pSeg )
1029 return FALSE;
1031 /* Return segment data */
1033 if ( ppModule ) *ppModule = pModule;
1034 if ( ppSeg ) *ppSeg = pSeg;
1035 if ( pSegNr ) *pSegNr = segNr;
1037 return TRUE;
1040 /**********************************************************************
1041 * GetCodeHandle (KERNEL.93)
1043 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
1045 SEGTABLEENTRY *pSeg;
1047 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
1048 return (HANDLE16)0;
1050 return pSeg->hSeg;
1053 /**********************************************************************
1054 * GetCodeInfo (KERNEL.104)
1056 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1058 NE_MODULE *pModule;
1059 SEGTABLEENTRY *pSeg;
1060 int segNr;
1062 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1063 return FALSE;
1065 /* Fill in segment information */
1067 segInfo->offSegment = pSeg->filepos;
1068 segInfo->cbSegment = pSeg->size;
1069 segInfo->flags = pSeg->flags;
1070 segInfo->cbAlloc = pSeg->minsize;
1071 segInfo->h = pSeg->hSeg;
1072 segInfo->alignShift = pModule->alignment;
1074 if ( segNr == pModule->dgroup )
1075 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1077 /* Return module handle in %es */
1079 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1081 return TRUE;
1085 /**********************************************************************
1086 * DefineHandleTable16 (KERNEL.94)
1088 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1090 return TRUE; /* FIXME */
1094 /***********************************************************************
1095 * SetTaskQueue (KERNEL.34)
1097 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1099 HQUEUE16 hPrev;
1100 TDB *pTask;
1102 if (!hTask) hTask = GetCurrentTask();
1103 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1105 hPrev = pTask->hQueue;
1106 pTask->hQueue = hQueue;
1108 return hPrev;
1112 /***********************************************************************
1113 * GetTaskQueue (KERNEL.35)
1115 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1117 TDB *pTask;
1119 if (!hTask) hTask = GetCurrentTask();
1120 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1121 return pTask->hQueue;
1124 /***********************************************************************
1125 * SetThreadQueue (KERNEL.463)
1127 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1129 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1130 HQUEUE16 oldQueue = teb? teb->queue : 0;
1132 if ( teb )
1134 teb->queue = hQueue;
1136 if ( GetTaskQueue16( teb->process->task ) == oldQueue )
1137 SetTaskQueue16( teb->process->task, hQueue );
1140 return oldQueue;
1143 /***********************************************************************
1144 * GetThreadQueue (KERNEL.464)
1146 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1148 TEB *teb = NULL;
1149 if ( !thread )
1150 teb = NtCurrentTeb();
1151 else if ( HIWORD(thread) )
1152 teb = THREAD_IdToTEB( thread );
1153 else if ( IsTask16( (HTASK16)thread ) )
1154 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1156 return (HQUEUE16)(teb? teb->queue : 0);
1159 /***********************************************************************
1160 * SetFastQueue (KERNEL.624)
1162 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1164 TEB *teb = NULL;
1165 if ( !thread )
1166 teb = NtCurrentTeb();
1167 else if ( HIWORD(thread) )
1168 teb = THREAD_IdToTEB( thread );
1169 else if ( IsTask16( (HTASK16)thread ) )
1170 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1172 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1175 /***********************************************************************
1176 * GetFastQueue (KERNEL.625)
1178 HANDLE WINAPI GetFastQueue16( void )
1180 TEB *teb = NtCurrentTeb();
1181 if (!teb) return 0;
1183 if (!teb->queue)
1184 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1186 if (!teb->queue)
1187 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1189 return (HANDLE)teb->queue;
1192 /***********************************************************************
1193 * SwitchStackTo (KERNEL.108)
1195 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1197 TDB *pTask;
1198 STACK16FRAME *oldFrame, *newFrame;
1199 INSTANCEDATA *pData;
1200 UINT16 copySize;
1202 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1203 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1204 TRACE("old=%04x:%04x new=%04x:%04x\n",
1205 SELECTOROF( pTask->teb->cur_stack ),
1206 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1208 /* Save the old stack */
1210 oldFrame = THREAD_STACK16( pTask->teb );
1211 /* pop frame + args and push bp */
1212 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1213 + 2 * sizeof(WORD);
1214 *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
1215 pData->stacktop = top;
1216 pData->stackmin = ptr;
1217 pData->stackbottom = ptr;
1219 /* Switch to the new stack */
1221 /* Note: we need to take the 3 arguments into account; otherwise,
1222 * the stack will underflow upon return from this function.
1224 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1225 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1226 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
1227 newFrame = THREAD_STACK16( pTask->teb );
1229 /* Copy the stack frame and the local variables to the new stack */
1231 memmove( newFrame, oldFrame, copySize );
1232 newFrame->bp = ptr;
1233 *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
1237 /***********************************************************************
1238 * SwitchStackBack (KERNEL.109)
1240 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1242 STACK16FRAME *oldFrame, *newFrame;
1243 INSTANCEDATA *pData;
1245 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1246 return;
1247 if (!pData->old_ss_sp)
1249 WARN("No previous SwitchStackTo\n" );
1250 return;
1252 TRACE("restoring stack %04x:%04x\n",
1253 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1255 oldFrame = CURRENT_STACK16;
1257 /* Pop bp from the previous stack */
1259 BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
1260 pData->old_ss_sp += sizeof(WORD);
1262 /* Switch back to the old stack */
1264 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1265 SS_reg(context) = SELECTOROF(pData->old_ss_sp);
1266 ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1267 pData->old_ss_sp = 0;
1269 /* Build a stack frame for the return */
1271 newFrame = CURRENT_STACK16;
1272 newFrame->frame32 = oldFrame->frame32;
1273 newFrame->module_cs = oldFrame->module_cs;
1274 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1275 newFrame->entry_ip = oldFrame->entry_ip;
1279 /***********************************************************************
1280 * GetTaskQueueDS16 (KERNEL.118)
1282 void WINAPI GetTaskQueueDS16(void)
1284 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1288 /***********************************************************************
1289 * GetTaskQueueES16 (KERNEL.119)
1291 void WINAPI GetTaskQueueES16(void)
1293 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1297 /***********************************************************************
1298 * GetCurrentTask (KERNEL.36)
1300 HTASK16 WINAPI GetCurrentTask(void)
1302 return PROCESS_Current()->task;
1305 DWORD WINAPI WIN16_GetCurrentTask(void)
1307 /* This is the version used by relay code; the first task is */
1308 /* returned in the high word of the result */
1309 return MAKELONG( GetCurrentTask(), hFirstTask );
1313 /***********************************************************************
1314 * GetCurrentPDB (KERNEL.37)
1316 * UNDOC: returns PSP of KERNEL in high word
1318 DWORD WINAPI GetCurrentPDB16(void)
1320 TDB *pTask;
1322 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1323 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1327 /***********************************************************************
1328 * GetCurPID16 (KERNEL.157)
1330 DWORD WINAPI GetCurPID16( DWORD unused )
1332 return 0;
1336 /***********************************************************************
1337 * GetInstanceData (KERNEL.54)
1339 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1341 char *ptr = (char *)GlobalLock16( instance );
1342 if (!ptr || !len) return 0;
1343 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1344 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1345 return len;
1349 /***********************************************************************
1350 * GetExeVersion (KERNEL.105)
1352 WORD WINAPI GetExeVersion16(void)
1354 TDB *pTask;
1356 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1357 return pTask->version;
1361 /***********************************************************************
1362 * SetErrorMode16 (KERNEL.107)
1364 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1366 TDB *pTask;
1367 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1368 pTask->error_mode = mode;
1369 return SetErrorMode( mode );
1373 /***********************************************************************
1374 * GetDOSEnvironment (KERNEL.131)
1376 SEGPTR WINAPI GetDOSEnvironment16(void)
1378 TDB *pTask;
1380 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1381 return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
1385 /***********************************************************************
1386 * GetNumTasks (KERNEL.152)
1388 UINT16 WINAPI GetNumTasks16(void)
1390 return nTaskCount;
1394 /***********************************************************************
1395 * GetTaskDS (KERNEL.155)
1397 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1398 * I don't think we need to bother with this.
1400 HINSTANCE16 WINAPI GetTaskDS16(void)
1402 TDB *pTask;
1404 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1405 return pTask->hInstance;
1408 /***********************************************************************
1409 * GetDummyModuleHandleDS (KERNEL.602)
1411 WORD WINAPI GetDummyModuleHandleDS16(void)
1413 TDB *pTask;
1414 WORD selector;
1416 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1417 if (!(pTask->flags & TDBF_WIN32)) return 0;
1418 selector = GlobalHandleToSel16( pTask->hModule );
1419 CURRENT_DS = selector;
1420 return selector;
1423 /***********************************************************************
1424 * IsTask (KERNEL.320)
1426 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1428 TDB *pTask;
1430 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1431 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1432 return (pTask->magic == TDB_MAGIC);
1436 /***********************************************************************
1437 * IsWinOldApTask16 (KERNEL.158)
1439 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1441 /* should return bit 0 of byte 0x48 in PSP */
1442 return FALSE;
1445 /***********************************************************************
1446 * SetTaskSignalProc (KERNEL.38)
1448 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1450 TDB *pTask;
1451 FARPROC16 oldProc;
1453 if (!hTask) hTask = GetCurrentTask();
1454 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1455 oldProc = pTask->userhandler;
1456 pTask->userhandler = proc;
1457 return oldProc;
1460 /***********************************************************************
1461 * TASK_CallTaskSignalProc
1463 /* ### start build ### */
1464 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1465 /* ### stop build ### */
1466 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1468 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1469 if ( !pTask || !pTask->userhandler ) return;
1471 TASK_CallTo16_word_wwwww( pTask->userhandler,
1472 hTaskOrModule, uCode, 0,
1473 pTask->hInstance, pTask->hQueue );
1476 /***********************************************************************
1477 * SetSigHandler (KERNEL.140)
1479 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1480 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1482 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1483 newhandler,oldhandler,oldmode,newmode,flag );
1485 if (flag != 1) return 0;
1486 if (!newmode) newhandler = NULL; /* Default handler */
1487 if (newmode != 4)
1489 TDB *pTask;
1491 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1492 if (oldmode) *oldmode = pTask->signal_flags;
1493 pTask->signal_flags = newmode;
1494 if (oldhandler) *oldhandler = pTask->sighandler;
1495 pTask->sighandler = newhandler;
1497 return 0;
1501 /***********************************************************************
1502 * GlobalNotify (KERNEL.154)
1504 * Note that GlobalNotify does _not_ return the old NotifyProc
1505 * -- contrary to LocalNotify !!
1507 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1509 TDB *pTask;
1511 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1512 pTask->discardhandler = proc;
1516 /***********************************************************************
1517 * GetExePtr (KERNEL.133)
1519 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1521 char *ptr;
1522 HANDLE16 owner;
1524 /* Check for module handle */
1526 if (!(ptr = GlobalLock16( handle ))) return 0;
1527 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1529 /* Search for this handle inside all tasks */
1531 *hTask = hFirstTask;
1532 while (*hTask)
1534 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1535 if ((*hTask == handle) ||
1536 (pTask->hInstance == handle) ||
1537 (pTask->hQueue == handle) ||
1538 (pTask->hPDB == handle)) return pTask->hModule;
1539 *hTask = pTask->hNext;
1542 /* Check the owner for module handle */
1544 owner = FarGetOwner16( handle );
1545 if (!(ptr = GlobalLock16( owner ))) return 0;
1546 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1548 /* Search for the owner inside all tasks */
1550 *hTask = hFirstTask;
1551 while (*hTask)
1553 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1554 if ((*hTask == owner) ||
1555 (pTask->hInstance == owner) ||
1556 (pTask->hQueue == owner) ||
1557 (pTask->hPDB == owner)) return pTask->hModule;
1558 *hTask = pTask->hNext;
1561 return 0;
1564 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1566 HTASK16 hTask = 0;
1567 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1568 STACK16FRAME *frame = CURRENT_STACK16;
1569 frame->ecx = hModule;
1570 if (hTask) frame->es = hTask;
1571 return hModule;
1574 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1576 HTASK16 hTask = 0;
1577 return GetExePtrHelper( handle, &hTask );
1581 /***********************************************************************
1582 * TaskFirst (TOOLHELP.63)
1584 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1586 lpte->hNext = hFirstTask;
1587 return TaskNext16( lpte );
1591 /***********************************************************************
1592 * TaskNext (TOOLHELP.64)
1594 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1596 TDB *pTask;
1597 INSTANCEDATA *pInstData;
1599 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1600 if (!lpte->hNext) return FALSE;
1601 pTask = (TDB *)GlobalLock16( lpte->hNext );
1602 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1603 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( pTask->hInstance, 0 );
1604 lpte->hTask = lpte->hNext;
1605 lpte->hTaskParent = pTask->hParent;
1606 lpte->hInst = pTask->hInstance;
1607 lpte->hModule = pTask->hModule;
1608 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1609 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1610 lpte->wStackTop = pInstData->stacktop;
1611 lpte->wStackMinimum = pInstData->stackmin;
1612 lpte->wStackBottom = pInstData->stackbottom;
1613 lpte->wcEvents = pTask->nEvents;
1614 lpte->hQueue = pTask->hQueue;
1615 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1616 lpte->wPSPOffset = 0x100; /*??*/
1617 lpte->hNext = pTask->hNext;
1618 return TRUE;
1622 /***********************************************************************
1623 * TaskFindHandle (TOOLHELP.65)
1625 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1627 lpte->hNext = hTask;
1628 return TaskNext16( lpte );
1632 /***********************************************************************
1633 * GetAppCompatFlags16 (KERNEL.354)
1635 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1637 return GetAppCompatFlags( hTask );
1641 /***********************************************************************
1642 * GetAppCompatFlags32 (USER32.206)
1644 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1646 TDB *pTask;
1648 if (!hTask) hTask = GetCurrentTask();
1649 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1650 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1651 return pTask->compat_flags;