Share the HMODULE of built-in modules over all processes.
[wine/hacks.git] / loader / task.c
blobeba0b72ff477db902301024da1a4110e5afdd390
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 "thread.h"
35 #include "syslevel.h"
36 #include "debugtools.h"
37 #include "dosexe.h"
38 #include "dde_proc.h"
39 #include "services.h"
40 #include "server.h"
42 DECLARE_DEBUG_CHANNEL(relay)
43 DECLARE_DEBUG_CHANNEL(task)
44 DECLARE_DEBUG_CHANNEL(toolhelp)
46 /* Min. number of thunks allocated when creating a new segment */
47 #define MIN_THUNKS 32
49 /* Pointer to function to switch to a larger stack */
50 int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
52 /* Pointer to debugger callback routine */
53 void (*TASK_AddTaskEntryBreakpoint)( HTASK16 hTask ) = NULL;
55 static THHOOK DefaultThhook = { 0 };
56 THHOOK *pThhook = &DefaultThhook;
58 #define hCurrentTask (pThhook->CurTDB)
59 #define hFirstTask (pThhook->HeadTDB)
60 #define hLockedTask (pThhook->LockTDB)
62 static UINT16 nTaskCount = 0;
65 /***********************************************************************
66 * TASK_InstallTHHook
68 void TASK_InstallTHHook( THHOOK *pNewThhook )
70 THHOOK *pOldThhook = pThhook;
72 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
74 *pThhook = *pOldThhook;
77 /***********************************************************************
78 * TASK_GetNextTask
80 HTASK16 TASK_GetNextTask( HTASK16 hTask )
82 TDB* pTask = (TDB*)GlobalLock16(hTask);
84 if (pTask->hNext) return pTask->hNext;
85 return (hFirstTask != hTask) ? hFirstTask : 0;
88 /***********************************************************************
89 * TASK_LinkTask
91 static void TASK_LinkTask( HTASK16 hTask )
93 HTASK16 *prevTask;
94 TDB *pTask;
96 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
97 prevTask = &hFirstTask;
98 while (*prevTask)
100 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
101 if (prevTaskPtr->priority >= pTask->priority) break;
102 prevTask = &prevTaskPtr->hNext;
104 pTask->hNext = *prevTask;
105 *prevTask = hTask;
106 nTaskCount++;
110 /***********************************************************************
111 * TASK_UnlinkTask
113 static void TASK_UnlinkTask( HTASK16 hTask )
115 HTASK16 *prevTask;
116 TDB *pTask;
118 prevTask = &hFirstTask;
119 while (*prevTask && (*prevTask != hTask))
121 pTask = (TDB *)GlobalLock16( *prevTask );
122 prevTask = &pTask->hNext;
124 if (*prevTask)
126 pTask = (TDB *)GlobalLock16( *prevTask );
127 *prevTask = pTask->hNext;
128 pTask->hNext = 0;
129 nTaskCount--;
134 /***********************************************************************
135 * TASK_CreateThunks
137 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
138 * and containing 'count' entries.
140 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
142 int i;
143 WORD free;
144 THUNKS *pThunk;
146 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
147 pThunk->next = 0;
148 pThunk->magic = THUNK_MAGIC;
149 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
150 free = pThunk->free;
151 for (i = 0; i < count-1; i++)
153 free += 8; /* Offset of next thunk */
154 pThunk->thunks[4*i] = free;
156 pThunk->thunks[4*i] = 0; /* Last thunk */
160 /***********************************************************************
161 * TASK_AllocThunk
163 * Allocate a thunk for MakeProcInstance().
165 static SEGPTR TASK_AllocThunk( HTASK16 hTask )
167 TDB *pTask;
168 THUNKS *pThunk;
169 WORD sel, base;
171 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
172 sel = pTask->hCSAlias;
173 pThunk = &pTask->thunks;
174 base = (int)pThunk - (int)pTask;
175 while (!pThunk->free)
177 sel = pThunk->next;
178 if (!sel) /* Allocate a new segment */
180 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
181 pTask->hPDB, TRUE, FALSE, FALSE );
182 if (!sel) return (SEGPTR)0;
183 TASK_CreateThunks( sel, 0, MIN_THUNKS );
184 pThunk->next = sel;
186 pThunk = (THUNKS *)GlobalLock16( sel );
187 base = 0;
189 base += pThunk->free;
190 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
191 return PTR_SEG_OFF_TO_SEGPTR( sel, base );
195 /***********************************************************************
196 * TASK_FreeThunk
198 * Free a MakeProcInstance() thunk.
200 static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
202 TDB *pTask;
203 THUNKS *pThunk;
204 WORD sel, base;
206 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
207 sel = pTask->hCSAlias;
208 pThunk = &pTask->thunks;
209 base = (int)pThunk - (int)pTask;
210 while (sel && (sel != HIWORD(thunk)))
212 sel = pThunk->next;
213 pThunk = (THUNKS *)GlobalLock16( sel );
214 base = 0;
216 if (!sel) return FALSE;
217 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
218 pThunk->free = LOWORD(thunk) - base;
219 return TRUE;
223 /***********************************************************************
224 * TASK_CallToStart
226 * 32-bit entry point for a new task. This function is responsible for
227 * setting up the registers and jumping to the 16-bit entry point.
229 void TASK_CallToStart(void)
231 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
232 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
233 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
234 CONTEXT context;
236 /* Add task to 16-bit scheduler pool */
237 TASK_Reschedule();
239 /* Registers at initialization must be:
240 * ax zero
241 * bx stack size in bytes
242 * cx heap size in bytes
243 * si previous app instance
244 * di current app instance
245 * bp zero
246 * es selector to the PSP
247 * ds dgroup of the application
248 * ss stack selector
249 * sp top of the stack
252 memset( &context, 0, sizeof(context) );
253 CS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
254 DS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->dgroup - 1].hSeg);
255 ES_reg(&context) = pTask->hPDB;
256 EIP_reg(&context) = pModule->ip;
257 EBX_reg(&context) = pModule->stack_size;
258 ECX_reg(&context) = pModule->heap_size;
259 EDI_reg(&context) = context.SegDs;
261 TRACE_(task)("Starting main program: cs:ip=%04lx:%04x ds=%04lx ss:sp=%04x:%04x\n",
262 CS_reg(&context), IP_reg(&context), DS_reg(&context),
263 SELECTOROF(pTask->teb->cur_stack),
264 OFFSETOF(pTask->teb->cur_stack) );
266 Callbacks->CallRegisterShortProc( &context, 0 );
270 /***********************************************************************
271 * TASK_Create
273 * NOTE: This routine might be called by a Win32 thread. Thus, we need
274 * to be careful to protect global data structures. We do this
275 * by entering the Win16Lock while linking the task into the
276 * global task list.
278 BOOL TASK_Create( NE_MODULE *pModule, HINSTANCE16 hInstance,
279 HINSTANCE16 hPrevInstance, UINT16 cmdShow)
281 HTASK16 hTask;
282 TDB *pTask;
283 LPSTR cmd_line;
284 WORD sp;
285 char name[10];
286 PDB *pdb32 = PROCESS_Current();
287 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
289 /* Allocate the task structure */
291 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
292 pModule->self, FALSE, FALSE, FALSE );
293 if (!hTask) return FALSE;
294 pTask = (TDB *)GlobalLock16( hTask );
296 /* Fill the task structure */
298 pTask->nEvents = 0;
299 pTask->hSelf = hTask;
300 pTask->flags = 0;
302 if (pModule->flags & NE_FFLAGS_WIN32)
303 pTask->flags |= TDBF_WIN32;
304 if (pModule->lpDosTask)
305 pTask->flags |= TDBF_WINOLDAP;
307 pTask->version = pModule->expected_version;
308 pTask->hInstance = hInstance? hInstance : pModule->self;
309 pTask->hPrevInstance = hPrevInstance;
310 pTask->hModule = pModule->self;
311 pTask->hParent = GetCurrentTask();
312 pTask->magic = TDB_MAGIC;
313 pTask->nCmdShow = cmdShow;
314 pTask->teb = NtCurrentTeb();
315 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
316 strcpy( pTask->curdir, "\\" );
317 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
318 sizeof(pTask->curdir) - 1 );
320 /* Create the thunks block */
322 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
324 /* Copy the module name */
326 GetModuleName16( pModule->self, name, sizeof(name) );
327 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
329 /* Allocate a selector for the PDB */
331 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
332 pModule->self, FALSE, FALSE, FALSE, NULL );
334 /* Fill the PDB */
336 pTask->pdb.int20 = 0x20cd;
337 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
338 PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint(
339 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
340 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
341 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
342 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
343 pTask->pdb.fileHandlesPtr =
344 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask->hPDB),
345 (int)&((PDB16 *)0)->fileHandles );
346 pTask->pdb.hFileHandles = 0;
347 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
348 pTask->pdb.environment = pdb32->env_db->env_sel;
349 pTask->pdb.nbFiles = 20;
351 /* Fill the command line */
353 cmd_line = pdb32->env_db->cmd_line;
354 while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++;
355 while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++;
356 lstrcpynA( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1);
357 pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
359 /* Get the compatibility flags */
361 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
363 /* Allocate a code segment alias for the TDB */
365 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
366 sizeof(TDB), pTask->hPDB, TRUE,
367 FALSE, FALSE, NULL );
369 /* Set the owner of the environment block */
371 FarSetOwner16( pTask->pdb.environment, pTask->hPDB );
373 /* Default DTA overwrites command-line */
375 pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
376 (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
378 /* Create scheduler event for 16-bit tasks */
380 if ( !(pTask->flags & TDBF_WIN32) )
382 pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
383 pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent );
386 /* Enter task handle into thread and process */
388 pTask->teb->htask16 = pTask->teb->process->task = hTask;
389 TRACE_(task)("module='%s' cmdline='%s' task=%04x\n", name, cmd_line, hTask );
391 /* If we have a DGROUP/hInstance, use it for 16-bit stack */
393 if ( hInstance )
395 if (!(sp = pModule->sp))
396 sp = pSegTable[pModule->ss-1].minsize + pModule->stack_size;
397 sp &= ~1; sp -= sizeof(STACK16FRAME);
398 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( hInstance, sp );
401 /* If requested, add entry point breakpoint */
403 if ( TASK_AddTaskEntryBreakpoint )
404 TASK_AddTaskEntryBreakpoint( hTask );
406 /* Add the task to the linked list */
408 SYSLEVEL_EnterWin16Lock();
409 TASK_LinkTask( hTask );
410 SYSLEVEL_LeaveWin16Lock();
412 return TRUE;
415 /***********************************************************************
416 * TASK_DeleteTask
418 static void TASK_DeleteTask( HTASK16 hTask )
420 TDB *pTask;
421 HGLOBAL16 hPDB;
423 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
424 hPDB = pTask->hPDB;
426 pTask->magic = 0xdead; /* invalidate signature */
428 /* Delete the Win32 part of the task */
430 /* PROCESS_FreePDB( pTask->teb->process ); FIXME */
431 /* K32OBJ_DecCount( &pTask->teb->header ); FIXME */
433 /* Free the selector aliases */
435 GLOBAL_FreeBlock( pTask->hCSAlias );
436 GLOBAL_FreeBlock( pTask->hPDB );
438 /* Free the task module */
440 FreeModule16( pTask->hModule );
442 /* Free the task structure itself */
444 GlobalFree16( hTask );
446 /* Free all memory used by this task (including the 32-bit stack, */
447 /* the environment block and the thunk segments). */
449 GlobalFreeAll16( hPDB );
452 /***********************************************************************
453 * TASK_KillTask
455 void TASK_KillTask( HTASK16 hTask )
457 TDB *pTask;
459 /* Enter the Win16Lock to protect global data structures */
460 SYSLEVEL_EnterWin16Lock();
462 if ( !hTask ) hTask = GetCurrentTask();
463 pTask = (TDB *)GlobalLock16( hTask );
464 if ( !pTask )
466 SYSLEVEL_LeaveWin16Lock();
467 return;
470 TRACE_(task)("Killing task %04x\n", hTask );
472 /* Delete active sockets */
474 if( pTask->pwsi )
475 WINSOCK_DeleteTaskWSI( pTask, pTask->pwsi );
477 #ifdef MZ_SUPPORTED
479 /* Kill DOS VM task */
480 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
481 if ( pModule->lpDosTask )
482 MZ_KillModule( pModule->lpDosTask );
484 #endif
486 /* Perform USER cleanup */
488 if (pTask->userhandler)
489 pTask->userhandler( hTask, USIG16_TERMINATION, 0,
490 pTask->hInstance, pTask->hQueue );
492 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
493 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 ); /* FIXME */
494 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
496 if (nTaskCount <= 1)
498 TRACE_(task)("this is the last task, exiting\n" );
499 ExitKernel16();
502 /* FIXME: Hack! Send a message to the initial task so that
503 * the GetMessage wakes up and the initial task can check whether
504 * it is the only remaining one and terminate itself ...
505 * The initial task should probably install hooks or something
506 * to get informed about task termination :-/
508 Callout.PostAppMessage16( PROCESS_Initial()->task, WM_NULL, 0, 0 );
510 /* Remove the task from the list to be sure we never switch back to it */
511 TASK_UnlinkTask( hTask );
512 if( nTaskCount )
514 TDB* p = (TDB *)GlobalLock16( hFirstTask );
515 while( p )
517 if( p->hYieldTo == hTask ) p->hYieldTo = 0;
518 p = (TDB *)GlobalLock16( p->hNext );
522 pTask->nEvents = 0;
524 if ( hLockedTask == hTask )
525 hLockedTask = 0;
527 TASK_DeleteTask( hTask );
529 /* When deleting the current task ... */
530 if ( hTask == hCurrentTask )
532 DWORD lockCount;
534 /* ... schedule another one ... */
535 TASK_Reschedule();
537 /* ... and completely release the Win16Lock, just in case. */
538 ReleaseThunkLock( &lockCount );
540 return;
543 SYSLEVEL_LeaveWin16Lock();
546 /***********************************************************************
547 * TASK_Reschedule
549 * This is where all the magic of task-switching happens!
551 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
552 * This means that each 16-bit task runs until it voluntarily yields
553 * control, at which point the scheduler gets active and selects the
554 * next task to run.
556 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
557 * by the standard scheduler of the underlying OS. As many 16-bit apps
558 * *rely* on the behaviour of the Windows scheduler, however, we have
559 * to simulate that behaviour.
561 * This is achieved as follows: every 16-bit task is at time (except
562 * during task creation and deletion) in one of two states: either it
563 * is the one currently running, then the global variable hCurrentTask
564 * contains its task handle, or it is not currently running, then it
565 * is blocked on a special scheduler event, a global handle to which
566 * is stored in the task struct.
568 * When the current task yields control, this routine gets called. Its
569 * purpose is to determine the next task to be active, signal the
570 * scheduler event of that task, and then put the current task to sleep
571 * waiting for *its* scheduler event to get signalled again.
573 * This routine can get called in a few other special situations as well:
575 * - On creation of a 16-bit task, the Unix process executing the task
576 * calls TASK_Reschedule once it has completed its initialization.
577 * At this point, the task needs to be blocked until its scheduler
578 * event is signalled the first time (this will be done by the parent
579 * process to get the task up and running).
581 * - When the task currently running terminates itself, this routine gets
582 * called and has to schedule another task, *without* blocking the
583 * terminating task.
585 * - When a 32-bit thread posts an event for a 16-bit task, it might be
586 * the case that *no* 16-bit task is currently running. In this case
587 * the task that has now an event pending is to be scheduled.
590 void TASK_Reschedule(void)
592 TDB *pOldTask = NULL, *pNewTask = NULL;
593 HTASK16 hOldTask = 0, hNewTask = 0;
594 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
595 DWORD lockCount;
597 SYSLEVEL_EnterWin16Lock();
599 /* Check what we need to do */
600 hOldTask = GetCurrentTask();
601 pOldTask = (TDB *)GlobalLock16( hOldTask );
602 TRACE_(task)( "entered with hCurrentTask %04x by hTask %04x (pid %d)\n",
603 hCurrentTask, hOldTask, getpid() );
605 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
607 /* We are called by an active (non-deleted) 16-bit task */
609 /* If we don't even have a current task, or else the current
610 task has yielded, we'll need to schedule a new task and
611 (possibly) put the calling task to sleep. Otherwise, we
612 only block the caller. */
614 if ( !hCurrentTask || hCurrentTask == hOldTask )
615 mode = MODE_YIELD;
616 else
617 mode = MODE_SLEEP;
619 else
621 /* We are called by a deleted 16-bit task or a 32-bit thread */
623 /* The only situation where we need to do something is if we
624 now do not have a current task. Then, we'll need to wake up
625 some task that has events pending. */
627 if ( !hCurrentTask || hCurrentTask == hOldTask )
628 mode = MODE_WAKEUP;
629 else
631 /* nothing to do */
632 SYSLEVEL_LeaveWin16Lock();
633 return;
637 /* Find a task to yield to: check for DirectedYield() */
638 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
640 hNewTask = pOldTask->hYieldTo;
641 pNewTask = (TDB *)GlobalLock16( hNewTask );
642 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
643 pOldTask->hYieldTo = 0;
646 /* Find a task to yield to: check for pending events */
647 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
649 hNewTask = hFirstTask;
650 while (hNewTask)
652 pNewTask = (TDB *)GlobalLock16( hNewTask );
654 TRACE_(task)( "\ttask = %04x, events = %i\n",
655 hNewTask, pNewTask->nEvents );
657 if (pNewTask->nEvents) break;
658 hNewTask = pNewTask->hNext;
660 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
663 /* If we are still the task with highest priority, just return ... */
664 if ( mode == MODE_YIELD && hNewTask == hCurrentTask )
666 TRACE_(task)("returning to the current task (%04x)\n", hCurrentTask );
667 SYSLEVEL_LeaveWin16Lock();
669 /* Allow Win32 threads to thunk down even while a Win16 task is
670 in a tight PeekMessage() or Yield() loop ... */
671 ReleaseThunkLock( &lockCount );
672 RestoreThunkLock( lockCount );
673 return;
676 /* If no task to yield to found, suspend 16-bit scheduler ... */
677 if ( mode == MODE_YIELD && !hNewTask )
679 TRACE_(task)("No currently active task\n");
680 hCurrentTask = 0;
683 /* If we found a task to wake up, do it ... */
684 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
686 TRACE_(task)("Switching to task %04x (%.8s)\n",
687 hNewTask, pNewTask->module_name );
689 pNewTask->priority++;
690 TASK_UnlinkTask( hNewTask );
691 TASK_LinkTask( hNewTask );
692 pNewTask->priority--;
694 hCurrentTask = hNewTask;
695 SetEvent( pNewTask->hEvent );
697 /* This is set just in case some app reads it ... */
698 pNewTask->ss_sp = pNewTask->teb->cur_stack;
701 /* If we need to put the current task to sleep, do it ... */
702 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
704 ResetEvent( pOldTask->hEvent );
706 ReleaseThunkLock( &lockCount );
707 SYSLEVEL_CheckNotLevel( 1 );
708 WaitForSingleObject( pOldTask->hEvent, INFINITE );
709 RestoreThunkLock( lockCount );
712 SYSLEVEL_LeaveWin16Lock();
715 /***********************************************************************
716 * InitTask (KERNEL.91)
718 * Called by the application startup code.
720 void WINAPI InitTask16( CONTEXT *context )
722 TDB *pTask;
723 NE_MODULE *pModule;
724 SEGTABLEENTRY *pSegTable;
725 INSTANCEDATA *pinstance;
726 LONG stacklow, stackhi;
728 if (context) EAX_reg(context) = 0;
729 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
730 if (!(pModule = NE_GetPtr( pTask->hModule ))) return;
732 /* Initialize implicitly loaded DLLs */
733 NE_InitializeDLLs( pTask->hModule );
735 if (context)
737 /* Registers on return are:
738 * ax 1 if OK, 0 on error
739 * cx stack limit in bytes
740 * dx cmdShow parameter
741 * si instance handle of the previous instance
742 * di instance handle of the new task
743 * es:bx pointer to command-line inside PSP
745 * 0 (=%bp) is pushed on the stack
747 SEGPTR ptr = STACK16_PUSH( pTask->teb, sizeof(WORD) );
748 *(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
749 SP_reg(context) -= 2;
751 EAX_reg(context) = 1;
753 if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
754 else
756 LPBYTE p = &pTask->pdb.cmdLine[1];
757 while ((*p == ' ') || (*p == '\t')) p++;
758 EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
760 ECX_reg(context) = pModule->stack_size;
761 EDX_reg(context) = pTask->nCmdShow;
762 ESI_reg(context) = (DWORD)pTask->hPrevInstance;
763 EDI_reg(context) = (DWORD)pTask->hInstance;
764 ES_reg (context) = (WORD)pTask->hPDB;
767 /* Initialize the local heap */
768 if ( pModule->heap_size )
770 LocalInit16( pTask->hInstance, 0, pModule->heap_size );
773 /* Initialize the INSTANCEDATA structure */
774 pSegTable = NE_SEG_TABLE( pModule );
775 stacklow = pSegTable[pModule->ss - 1].minsize;
776 stackhi = stacklow + pModule->stack_size;
777 if (stackhi > 0xffff) stackhi = 0xffff;
778 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
779 pinstance->stackbottom = stackhi; /* yup, that's right. Confused me too. */
780 pinstance->stacktop = stacklow;
781 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack );
785 /***********************************************************************
786 * WaitEvent (KERNEL.30)
788 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
790 TDB *pTask;
792 if (!hTask) hTask = GetCurrentTask();
793 pTask = (TDB *)GlobalLock16( hTask );
795 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
797 FIXME_(task)("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
798 return TRUE;
801 if (pTask->nEvents > 0)
803 pTask->nEvents--;
804 return FALSE;
806 TASK_Reschedule();
808 /* When we get back here, we have an event */
810 if (pTask->nEvents > 0) pTask->nEvents--;
811 return TRUE;
815 /***********************************************************************
816 * PostEvent (KERNEL.31)
818 void WINAPI PostEvent16( HTASK16 hTask )
820 TDB *pTask;
822 if (!hTask) hTask = GetCurrentTask();
823 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
825 if ( !THREAD_IsWin16( pTask->teb ) )
827 FIXME_(task)("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
828 return;
831 pTask->nEvents++;
833 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
834 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
835 TASK_Reschedule();
839 /***********************************************************************
840 * SetPriority (KERNEL.32)
842 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
844 TDB *pTask;
845 INT16 newpriority;
847 if (!hTask) hTask = GetCurrentTask();
848 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
849 newpriority = pTask->priority + delta;
850 if (newpriority < -32) newpriority = -32;
851 else if (newpriority > 15) newpriority = 15;
853 pTask->priority = newpriority + 1;
854 TASK_UnlinkTask( hTask );
855 TASK_LinkTask( hTask );
856 pTask->priority--;
860 /***********************************************************************
861 * LockCurrentTask (KERNEL.33)
863 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
865 if (bLock) hLockedTask = GetCurrentTask();
866 else hLockedTask = 0;
867 return hLockedTask;
871 /***********************************************************************
872 * IsTaskLocked (KERNEL.122)
874 HTASK16 WINAPI IsTaskLocked16(void)
876 return hLockedTask;
880 /***********************************************************************
881 * OldYield (KERNEL.117)
883 void WINAPI OldYield16(void)
885 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
887 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
889 FIXME_(task)("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
890 return;
893 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
894 TASK_Reschedule();
895 if (pCurTask) pCurTask->nEvents--;
899 /***********************************************************************
900 * DirectedYield (KERNEL.150)
902 void WINAPI DirectedYield16( HTASK16 hTask )
904 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
906 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
908 FIXME_(task)("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
909 return;
912 TRACE_(task)("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
914 pCurTask->hYieldTo = hTask;
915 OldYield16();
917 TRACE_(task)("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
920 /***********************************************************************
921 * Yield16 (KERNEL.29)
923 void WINAPI Yield16(void)
925 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
927 if (pCurTask) pCurTask->hYieldTo = 0;
928 if (pCurTask && pCurTask->hQueue) Callout.UserYield16();
929 else OldYield16();
932 /***********************************************************************
933 * KERNEL_490 (KERNEL.490)
935 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
937 if ( !someTask ) return 0;
939 FIXME_(task)("(%04x): stub\n", someTask );
940 return 0;
943 /***********************************************************************
944 * MakeProcInstance16 (KERNEL.51)
946 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
948 BYTE *thunk,*lfunc;
949 SEGPTR thunkaddr;
951 if (!func) {
952 ERR_(task)("Ouch ! MakeProcInstance called with func == NULL !\n");
953 return (FARPROC16)0; /* Windows seems to do the same */
955 if (!hInstance) hInstance = CURRENT_DS;
956 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
957 if (!thunkaddr) return (FARPROC16)0;
958 thunk = PTR_SEG_TO_LIN( thunkaddr );
959 lfunc = PTR_SEG_TO_LIN( func );
961 TRACE_(task)("(%08lx,%04x): got thunk %08lx\n",
962 (DWORD)func, hInstance, (DWORD)thunkaddr );
963 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) ||
964 ((lfunc[0]==0x1e) && (lfunc[1]==0x58))
966 FIXME_(task)("thunk would be useless for %p, overwriting with nop;nop;\n", func );
967 lfunc[0]=0x90; /* nop */
968 lfunc[1]=0x90; /* nop */
971 *thunk++ = 0xb8; /* movw instance, %ax */
972 *thunk++ = (BYTE)(hInstance & 0xff);
973 *thunk++ = (BYTE)(hInstance >> 8);
974 *thunk++ = 0xea; /* ljmp func */
975 *(DWORD *)thunk = (DWORD)func;
976 return (FARPROC16)thunkaddr;
980 /***********************************************************************
981 * FreeProcInstance16 (KERNEL.52)
983 void WINAPI FreeProcInstance16( FARPROC16 func )
985 TRACE_(task)("(%08lx)\n", (DWORD)func );
986 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
990 /**********************************************************************
991 * GetCodeHandle (KERNEL.93)
993 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
995 HANDLE16 handle;
996 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
998 /* Return the code segment containing 'proc'. */
999 /* Not sure if this is really correct (shouldn't matter that much). */
1001 /* Check if it is really a thunk */
1002 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1003 handle = GlobalHandle16( thunk[6] + (thunk[7] << 8) );
1004 else
1005 handle = GlobalHandle16( HIWORD(proc) );
1007 return handle;
1010 /**********************************************************************
1011 * GetCodeInfo (KERNEL.104)
1013 VOID WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1015 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
1016 NE_MODULE *pModule = NULL;
1017 SEGTABLEENTRY *pSeg = NULL;
1018 WORD segNr;
1020 /* proc is either a thunk, or else a pair of module handle
1021 and segment number. In the first case, we also need to
1022 extract module and segment number. */
1024 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1026 WORD selector = thunk[6] + (thunk[7] << 8);
1027 pModule = NE_GetPtr( GlobalHandle16( selector ) );
1028 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
1030 if ( pModule )
1031 for ( segNr = 0; segNr < pModule->seg_count; segNr++, pSeg++ )
1032 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
1033 break;
1035 if ( pModule && segNr >= pModule->seg_count )
1036 pSeg = NULL;
1038 else
1040 pModule = NE_GetPtr( HIWORD( proc ) );
1041 segNr = LOWORD( proc );
1043 if ( pModule && segNr < pModule->seg_count )
1044 pSeg = NE_SEG_TABLE( pModule ) + segNr;
1047 /* fill in segment information */
1049 segInfo->offSegment = pSeg? pSeg->filepos : 0;
1050 segInfo->cbSegment = pSeg? pSeg->size : 0;
1051 segInfo->flags = pSeg? pSeg->flags : 0;
1052 segInfo->cbAlloc = pSeg? pSeg->minsize : 0;
1053 segInfo->h = pSeg? pSeg->hSeg : 0;
1054 segInfo->alignShift = pModule? pModule->alignment : 0;
1058 /**********************************************************************
1059 * DefineHandleTable16 (KERNEL.94)
1061 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1063 return TRUE; /* FIXME */
1067 /***********************************************************************
1068 * SetTaskQueue (KERNEL.34)
1070 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1072 HQUEUE16 hPrev;
1073 TDB *pTask;
1075 if (!hTask) hTask = GetCurrentTask();
1076 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1078 hPrev = pTask->hQueue;
1079 pTask->hQueue = hQueue;
1081 return hPrev;
1085 /***********************************************************************
1086 * GetTaskQueue (KERNEL.35)
1088 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1090 TDB *pTask;
1092 if (!hTask) hTask = GetCurrentTask();
1093 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1094 return pTask->hQueue;
1097 /***********************************************************************
1098 * SetThreadQueue (KERNEL.463)
1100 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1102 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1103 HQUEUE16 oldQueue = teb? teb->queue : 0;
1105 if ( teb )
1107 teb->queue = hQueue;
1109 if ( GetTaskQueue16( teb->process->task ) == oldQueue )
1110 SetTaskQueue16( teb->process->task, hQueue );
1113 return oldQueue;
1116 /***********************************************************************
1117 * GetThreadQueue (KERNEL.464)
1119 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1121 TEB *teb = NULL;
1122 if ( !thread )
1123 teb = NtCurrentTeb();
1124 else if ( HIWORD(thread) )
1125 teb = THREAD_IdToTEB( thread );
1126 else if ( IsTask16( (HTASK16)thread ) )
1127 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1129 return (HQUEUE16)(teb? teb->queue : 0);
1132 /***********************************************************************
1133 * SetFastQueue (KERNEL.624)
1135 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1137 TEB *teb = NULL;
1138 if ( !thread )
1139 teb = NtCurrentTeb();
1140 else if ( HIWORD(thread) )
1141 teb = THREAD_IdToTEB( thread );
1142 else if ( IsTask16( (HTASK16)thread ) )
1143 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1145 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1148 /***********************************************************************
1149 * GetFastQueue (KERNEL.625)
1151 HANDLE WINAPI GetFastQueue16( void )
1153 TEB *teb = NtCurrentTeb();
1154 if (!teb) return 0;
1156 if (!teb->queue)
1157 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1159 if (!teb->queue)
1160 FIXME_(task)("(): should initialize thread-local queue, expect failure!\n" );
1162 return (HANDLE)teb->queue;
1165 /***********************************************************************
1166 * SwitchStackTo (KERNEL.108)
1168 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1170 TDB *pTask;
1171 STACK16FRAME *oldFrame, *newFrame;
1172 INSTANCEDATA *pData;
1173 UINT16 copySize;
1175 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1176 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1177 TRACE_(task)("old=%04x:%04x new=%04x:%04x\n",
1178 SELECTOROF( pTask->teb->cur_stack ),
1179 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1181 /* Save the old stack */
1183 oldFrame = THREAD_STACK16( pTask->teb );
1184 /* pop frame + args and push bp */
1185 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1186 + 2 * sizeof(WORD);
1187 *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
1188 pData->stacktop = top;
1189 pData->stackmin = ptr;
1190 pData->stackbottom = ptr;
1192 /* Switch to the new stack */
1194 /* Note: we need to take the 3 arguments into account; otherwise,
1195 * the stack will underflow upon return from this function.
1197 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1198 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1199 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
1200 newFrame = THREAD_STACK16( pTask->teb );
1202 /* Copy the stack frame and the local variables to the new stack */
1204 memmove( newFrame, oldFrame, copySize );
1205 newFrame->bp = ptr;
1206 *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
1210 /***********************************************************************
1211 * SwitchStackBack (KERNEL.109)
1213 void WINAPI SwitchStackBack16( CONTEXT *context )
1215 STACK16FRAME *oldFrame, *newFrame;
1216 INSTANCEDATA *pData;
1218 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1219 return;
1220 if (!pData->old_ss_sp)
1222 WARN_(task)("No previous SwitchStackTo\n" );
1223 return;
1225 TRACE_(task)("restoring stack %04x:%04x\n",
1226 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1228 oldFrame = CURRENT_STACK16;
1230 /* Pop bp from the previous stack */
1232 BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
1233 pData->old_ss_sp += sizeof(WORD);
1235 /* Switch back to the old stack */
1237 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1238 SS_reg(context) = SELECTOROF(pData->old_ss_sp);
1239 ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1240 pData->old_ss_sp = 0;
1242 /* Build a stack frame for the return */
1244 newFrame = CURRENT_STACK16;
1245 newFrame->frame32 = oldFrame->frame32;
1246 if (TRACE_ON(relay))
1248 newFrame->entry_ip = oldFrame->entry_ip;
1249 newFrame->entry_cs = oldFrame->entry_cs;
1254 /***********************************************************************
1255 * GetTaskQueueDS (KERNEL.118)
1257 void WINAPI GetTaskQueueDS16( CONTEXT *context )
1259 DS_reg(context) = GlobalHandleToSel16( GetTaskQueue16(0) );
1263 /***********************************************************************
1264 * GetTaskQueueES (KERNEL.119)
1266 void WINAPI GetTaskQueueES16( CONTEXT *context )
1268 ES_reg(context) = GlobalHandleToSel16( GetTaskQueue16(0) );
1272 /***********************************************************************
1273 * GetCurrentTask (KERNEL.36)
1275 HTASK16 WINAPI GetCurrentTask(void)
1277 return PROCESS_Current()->task;
1280 DWORD WINAPI WIN16_GetCurrentTask(void)
1282 /* This is the version used by relay code; the first task is */
1283 /* returned in the high word of the result */
1284 return MAKELONG( GetCurrentTask(), hFirstTask );
1288 /***********************************************************************
1289 * GetCurrentPDB (KERNEL.37)
1291 * UNDOC: returns PSP of KERNEL in high word
1293 DWORD WINAPI GetCurrentPDB16(void)
1295 TDB *pTask;
1297 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1298 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1302 /***********************************************************************
1303 * GetInstanceData (KERNEL.54)
1305 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1307 char *ptr = (char *)GlobalLock16( instance );
1308 if (!ptr || !len) return 0;
1309 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1310 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1311 return len;
1315 /***********************************************************************
1316 * GetExeVersion (KERNEL.105)
1318 WORD WINAPI GetExeVersion16(void)
1320 TDB *pTask;
1322 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1323 return pTask->version;
1327 /***********************************************************************
1328 * SetErrorMode16 (KERNEL.107)
1330 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1332 TDB *pTask;
1333 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1334 pTask->error_mode = mode;
1335 return SetErrorMode( mode );
1339 /***********************************************************************
1340 * GetDOSEnvironment (KERNEL.131)
1342 SEGPTR WINAPI GetDOSEnvironment16(void)
1344 TDB *pTask;
1346 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1347 return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
1351 /***********************************************************************
1352 * GetNumTasks (KERNEL.152)
1354 UINT16 WINAPI GetNumTasks16(void)
1356 return nTaskCount;
1360 /***********************************************************************
1361 * GetTaskDS (KERNEL.155)
1363 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1364 * I don't think we need to bother with this.
1366 HINSTANCE16 WINAPI GetTaskDS16(void)
1368 TDB *pTask;
1370 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1371 return pTask->hInstance;
1374 /***********************************************************************
1375 * GetDummyModuleHandleDS (KERNEL.602)
1377 VOID WINAPI GetDummyModuleHandleDS16( CONTEXT *context )
1379 TDB *pTask;
1380 WORD selector;
1382 AX_reg( context ) = 0;
1383 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1384 if (!(pTask->flags & TDBF_WIN32)) return;
1386 selector = GlobalHandleToSel16( pTask->hModule );
1387 DS_reg( context ) = selector;
1388 AX_reg( context ) = selector;
1391 /***********************************************************************
1392 * IsTask (KERNEL.320)
1394 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1396 TDB *pTask;
1398 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1399 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1400 return (pTask->magic == TDB_MAGIC);
1404 /***********************************************************************
1405 * SetTaskSignalProc (KERNEL.38)
1407 * Real 16-bit interface is provided by the THUNK_SetTaskSignalProc.
1409 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1411 TDB *pTask;
1412 FARPROC16 oldProc;
1414 if (!hTask) hTask = GetCurrentTask();
1415 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1416 oldProc = (FARPROC16)pTask->userhandler;
1417 pTask->userhandler = (USERSIGNALPROC)proc;
1418 return oldProc;
1422 /***********************************************************************
1423 * SetSigHandler (KERNEL.140)
1425 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1426 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1428 FIXME_(task)("(%p,%p,%p,%d,%d), unimplemented.\n",
1429 newhandler,oldhandler,oldmode,newmode,flag );
1431 if (flag != 1) return 0;
1432 if (!newmode) newhandler = NULL; /* Default handler */
1433 if (newmode != 4)
1435 TDB *pTask;
1437 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1438 if (oldmode) *oldmode = pTask->signal_flags;
1439 pTask->signal_flags = newmode;
1440 if (oldhandler) *oldhandler = pTask->sighandler;
1441 pTask->sighandler = newhandler;
1443 return 0;
1447 /***********************************************************************
1448 * GlobalNotify (KERNEL.154)
1450 * Note that GlobalNotify does _not_ return the old NotifyProc
1451 * -- contrary to LocalNotify !!
1453 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1455 TDB *pTask;
1457 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1458 pTask->discardhandler = proc;
1462 /***********************************************************************
1463 * GetExePtr (KERNEL.133)
1465 static HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1467 char *ptr;
1468 HANDLE16 owner;
1470 /* Check for module handle */
1472 if (!(ptr = GlobalLock16( handle ))) return 0;
1473 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1475 /* Search for this handle inside all tasks */
1477 *hTask = hFirstTask;
1478 while (*hTask)
1480 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1481 if ((*hTask == handle) ||
1482 (pTask->hInstance == handle) ||
1483 (pTask->hQueue == handle) ||
1484 (pTask->hPDB == handle)) return pTask->hModule;
1485 *hTask = pTask->hNext;
1488 /* Check the owner for module handle */
1490 owner = FarGetOwner16( handle );
1491 if (!(ptr = GlobalLock16( owner ))) return 0;
1492 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1494 /* Search for the owner inside all tasks */
1496 *hTask = hFirstTask;
1497 while (*hTask)
1499 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1500 if ((*hTask == owner) ||
1501 (pTask->hInstance == owner) ||
1502 (pTask->hQueue == owner) ||
1503 (pTask->hPDB == owner)) return pTask->hModule;
1504 *hTask = pTask->hNext;
1507 return 0;
1510 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1512 HTASK16 dummy;
1513 return GetExePtrHelper( handle, &dummy );
1516 void WINAPI WIN16_GetExePtr( CONTEXT *context )
1518 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1519 HANDLE16 handle = (HANDLE16)stack[2];
1520 HTASK16 hTask = 0;
1521 HMODULE16 hModule;
1523 hModule = GetExePtrHelper( handle, &hTask );
1525 AX_reg(context) = CX_reg(context) = hModule;
1526 if (hTask) ES_reg(context) = hTask;
1529 /***********************************************************************
1530 * TaskFirst (TOOLHELP.63)
1532 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1534 lpte->hNext = hFirstTask;
1535 return TaskNext16( lpte );
1539 /***********************************************************************
1540 * TaskNext (TOOLHELP.64)
1542 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1544 TDB *pTask;
1545 INSTANCEDATA *pInstData;
1547 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1548 if (!lpte->hNext) return FALSE;
1549 pTask = (TDB *)GlobalLock16( lpte->hNext );
1550 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1551 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( pTask->hInstance, 0 );
1552 lpte->hTask = lpte->hNext;
1553 lpte->hTaskParent = pTask->hParent;
1554 lpte->hInst = pTask->hInstance;
1555 lpte->hModule = pTask->hModule;
1556 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1557 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1558 lpte->wStackTop = pInstData->stacktop;
1559 lpte->wStackMinimum = pInstData->stackmin;
1560 lpte->wStackBottom = pInstData->stackbottom;
1561 lpte->wcEvents = pTask->nEvents;
1562 lpte->hQueue = pTask->hQueue;
1563 strncpy( lpte->szModule, pTask->module_name, 8 );
1564 lpte->szModule[8] = '\0';
1565 lpte->wPSPOffset = 0x100; /*??*/
1566 lpte->hNext = pTask->hNext;
1567 return TRUE;
1571 /***********************************************************************
1572 * TaskFindHandle (TOOLHELP.65)
1574 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1576 lpte->hNext = hTask;
1577 return TaskNext16( lpte );
1581 /***********************************************************************
1582 * GetAppCompatFlags16 (KERNEL.354)
1584 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1586 return GetAppCompatFlags( hTask );
1590 /***********************************************************************
1591 * GetAppCompatFlags32 (USER32.206)
1593 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1595 TDB *pTask;
1597 if (!hTask) hTask = GetCurrentTask();
1598 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1599 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1600 return pTask->compat_flags;