Added HWND_MESSAGE.
[wine/multimedia.git] / loader / task.c
blob3d13b47e962e2376faf575388ddae91254c668ca
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 "pe_image.h"
24 #include "process.h"
25 #include "queue.h"
26 #include "selectors.h"
27 #include "stackframe.h"
28 #include "task.h"
29 #include "thread.h"
30 #include "toolhelp.h"
31 #include "winnt.h"
32 #include "winsock.h"
33 #include "syslevel.h"
34 #include "debugtools.h"
35 #include "dosexe.h"
36 #include "services.h"
37 #include "server.h"
39 DEFAULT_DEBUG_CHANNEL(task)
40 DECLARE_DEBUG_CHANNEL(relay)
41 DECLARE_DEBUG_CHANNEL(toolhelp)
43 /* Min. number of thunks allocated when creating a new segment */
44 #define MIN_THUNKS 32
46 /* Pointer to function to switch to a larger stack */
47 int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
49 static THHOOK DefaultThhook = { 0 };
50 THHOOK *pThhook = &DefaultThhook;
52 #define hCurrentTask (pThhook->CurTDB)
53 #define hFirstTask (pThhook->HeadTDB)
54 #define hLockedTask (pThhook->LockTDB)
56 static UINT16 nTaskCount = 0;
58 static HTASK initial_task;
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 SYSLEVEL_EnterWin16Lock();
233 /* Add task to 16-bit scheduler pool if necessary */
234 if ( hCurrentTask != GetCurrentTask() )
235 TASK_Reschedule();
237 /* Registers at initialization must be:
238 * ax zero
239 * bx stack size in bytes
240 * cx heap size in bytes
241 * si previous app instance
242 * di current app instance
243 * bp zero
244 * es selector to the PSP
245 * ds dgroup of the application
246 * ss stack selector
247 * sp top of the stack
250 memset( &context, 0, sizeof(context) );
251 CS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
252 DS_reg(&context) = GlobalHandleToSel16(pTask->hInstance);
253 ES_reg(&context) = pTask->hPDB;
254 EIP_reg(&context) = pModule->ip;
255 EBX_reg(&context) = pModule->stack_size;
256 ECX_reg(&context) = pModule->heap_size;
257 EDI_reg(&context) = pTask->hInstance;
258 ESI_reg(&context) = pTask->hPrevInstance;
260 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
261 CS_reg(&context), EIP_reg(&context), DS_reg(&context),
262 SELECTOROF(pTask->teb->cur_stack),
263 OFFSETOF(pTask->teb->cur_stack) );
265 Callbacks->CallRegisterShortProc( &context, 0 );
269 /***********************************************************************
270 * TASK_Create
272 * NOTE: This routine might be called by a Win32 thread. Thus, we need
273 * to be careful to protect global data structures. We do this
274 * by entering the Win16Lock while linking the task into the
275 * global task list.
277 BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow)
279 HTASK16 hTask;
280 TDB *pTask;
281 LPSTR cmd_line;
282 char name[10];
283 PDB *pdb32 = PROCESS_Current();
285 /* Allocate the task structure */
287 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
288 pModule->self, FALSE, FALSE, FALSE );
289 if (!hTask) return FALSE;
290 pTask = (TDB *)GlobalLock16( hTask );
292 /* Fill the task structure */
294 pTask->nEvents = 0;
295 pTask->hSelf = hTask;
296 pTask->flags = 0;
298 if (pModule->flags & NE_FFLAGS_WIN32)
299 pTask->flags |= TDBF_WIN32;
300 if (pModule->lpDosTask)
301 pTask->flags |= TDBF_WINOLDAP;
303 pTask->hInstance = pModule->self;
304 pTask->hPrevInstance = 0;
305 /* NOTE: for 16-bit tasks, the instance handles are updated later on
306 in NE_InitProcess */
308 pTask->version = pModule->expected_version;
309 pTask->hModule = pModule->self;
310 pTask->hParent = GetCurrentTask();
311 pTask->magic = TDB_MAGIC;
312 pTask->nCmdShow = cmdShow;
313 pTask->teb = NtCurrentTeb();
314 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
315 strcpy( pTask->curdir, "\\" );
316 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
317 sizeof(pTask->curdir) - 1 );
319 /* Create the thunks block */
321 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
323 /* Copy the module name */
325 GetModuleName16( pModule->self, name, sizeof(name) );
326 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
328 /* Allocate a selector for the PDB */
330 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
331 pModule->self, FALSE, FALSE, FALSE, NULL );
333 /* Fill the PDB */
335 pTask->pdb.int20 = 0x20cd;
336 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
337 PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint(
338 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
339 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
340 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
341 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
342 pTask->pdb.fileHandlesPtr =
343 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask->hPDB),
344 (int)&((PDB16 *)0)->fileHandles );
345 pTask->pdb.hFileHandles = 0;
346 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
347 pTask->pdb.environment = pdb32->env_db->env_sel;
348 pTask->pdb.nbFiles = 20;
350 /* Fill the command line */
352 cmd_line = pdb32->env_db->cmd_line;
353 while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++;
354 while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++;
355 lstrcpynA( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1);
356 pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
358 /* Get the compatibility flags */
360 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
362 /* Allocate a code segment alias for the TDB */
364 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
365 sizeof(TDB), pTask->hPDB, TRUE,
366 FALSE, FALSE, NULL );
368 /* Set the owner of the environment block */
370 FarSetOwner16( pTask->pdb.environment, pTask->hPDB );
372 /* Default DTA overwrites command-line */
374 pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
375 (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
377 /* Create scheduler event for 16-bit tasks */
379 if ( !(pTask->flags & TDBF_WIN32) )
381 pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
382 pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent );
385 /* Enter task handle into thread and process */
387 pTask->teb->htask16 = pTask->teb->process->task = hTask;
388 if (!initial_task) initial_task = hTask;
390 TRACE("module='%s' cmdline='%s' task=%04x\n", name, cmd_line, hTask );
392 /* Add the task to the linked list */
394 SYSLEVEL_EnterWin16Lock();
395 TASK_LinkTask( hTask );
396 SYSLEVEL_LeaveWin16Lock();
398 return TRUE;
401 /***********************************************************************
402 * TASK_DeleteTask
404 static void TASK_DeleteTask( HTASK16 hTask )
406 TDB *pTask;
407 HGLOBAL16 hPDB;
409 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
410 hPDB = pTask->hPDB;
412 pTask->magic = 0xdead; /* invalidate signature */
414 /* Delete the Win32 part of the task */
416 /* PROCESS_FreePDB( pTask->teb->process ); FIXME */
417 /* K32OBJ_DecCount( &pTask->teb->header ); FIXME */
419 /* Free the selector aliases */
421 GLOBAL_FreeBlock( pTask->hCSAlias );
422 GLOBAL_FreeBlock( pTask->hPDB );
424 /* Free the task module */
426 FreeModule16( pTask->hModule );
428 /* Free the task structure itself */
430 GlobalFree16( hTask );
432 /* Free all memory used by this task (including the 32-bit stack, */
433 /* the environment block and the thunk segments). */
435 GlobalFreeAll16( hPDB );
438 /***********************************************************************
439 * TASK_KillTask
441 void TASK_KillTask( HTASK16 hTask )
443 TDB *pTask;
445 /* Enter the Win16Lock to protect global data structures */
446 SYSLEVEL_EnterWin16Lock();
448 if ( !hTask ) hTask = GetCurrentTask();
449 pTask = (TDB *)GlobalLock16( hTask );
450 if ( !pTask )
452 SYSLEVEL_LeaveWin16Lock();
453 return;
456 TRACE("Killing task %04x\n", hTask );
458 #ifdef MZ_SUPPORTED
460 /* Kill DOS VM task */
461 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
462 if ( pModule->lpDosTask )
463 MZ_KillModule( pModule->lpDosTask );
465 #endif
467 /* Perform USER cleanup */
469 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
470 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
471 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
472 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
474 if (nTaskCount <= 1)
476 TRACE("this is the last task, exiting\n" );
477 ExitKernel16();
480 /* FIXME: Hack! Send a message to the initial task so that
481 * the GetMessage wakes up and the initial task can check whether
482 * it is the only remaining one and terminate itself ...
483 * The initial task should probably install hooks or something
484 * to get informed about task termination :-/
486 Callout.PostAppMessage16( initial_task, WM_NULL, 0, 0 );
488 /* Remove the task from the list to be sure we never switch back to it */
489 TASK_UnlinkTask( hTask );
490 if( nTaskCount )
492 TDB* p = (TDB *)GlobalLock16( hFirstTask );
493 while( p )
495 if( p->hYieldTo == hTask ) p->hYieldTo = 0;
496 p = (TDB *)GlobalLock16( p->hNext );
500 pTask->nEvents = 0;
502 if ( hLockedTask == hTask )
503 hLockedTask = 0;
505 TASK_DeleteTask( hTask );
507 /* When deleting the current task ... */
508 if ( hTask == hCurrentTask )
510 DWORD lockCount;
512 /* ... schedule another one ... */
513 TASK_Reschedule();
515 /* ... and completely release the Win16Lock, just in case. */
516 ReleaseThunkLock( &lockCount );
518 return;
521 SYSLEVEL_LeaveWin16Lock();
524 /***********************************************************************
525 * TASK_Reschedule
527 * This is where all the magic of task-switching happens!
529 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
530 * This means that each 16-bit task runs until it voluntarily yields
531 * control, at which point the scheduler gets active and selects the
532 * next task to run.
534 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
535 * by the standard scheduler of the underlying OS. As many 16-bit apps
536 * *rely* on the behaviour of the Windows scheduler, however, we have
537 * to simulate that behaviour.
539 * This is achieved as follows: every 16-bit task is at time (except
540 * during task creation and deletion) in one of two states: either it
541 * is the one currently running, then the global variable hCurrentTask
542 * contains its task handle, or it is not currently running, then it
543 * is blocked on a special scheduler event, a global handle to which
544 * is stored in the task struct.
546 * When the current task yields control, this routine gets called. Its
547 * purpose is to determine the next task to be active, signal the
548 * scheduler event of that task, and then put the current task to sleep
549 * waiting for *its* scheduler event to get signalled again.
551 * This routine can get called in a few other special situations as well:
553 * - On creation of a 16-bit task, the Unix process executing the task
554 * calls TASK_Reschedule once it has completed its initialization.
555 * At this point, the task needs to be blocked until its scheduler
556 * event is signalled the first time (this will be done by the parent
557 * process to get the task up and running).
559 * - When the task currently running terminates itself, this routine gets
560 * called and has to schedule another task, *without* blocking the
561 * terminating task.
563 * - When a 32-bit thread posts an event for a 16-bit task, it might be
564 * the case that *no* 16-bit task is currently running. In this case
565 * the task that has now an event pending is to be scheduled.
568 void TASK_Reschedule(void)
570 TDB *pOldTask = NULL, *pNewTask = NULL;
571 HTASK16 hOldTask = 0, hNewTask = 0;
572 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
573 DWORD lockCount;
575 SYSLEVEL_EnterWin16Lock();
577 /* Check what we need to do */
578 hOldTask = GetCurrentTask();
579 pOldTask = (TDB *)GlobalLock16( hOldTask );
580 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
581 hCurrentTask, hOldTask, (long) getpid() );
583 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
585 /* We are called by an active (non-deleted) 16-bit task */
587 /* If we don't even have a current task, or else the current
588 task has yielded, we'll need to schedule a new task and
589 (possibly) put the calling task to sleep. Otherwise, we
590 only block the caller. */
592 if ( !hCurrentTask || hCurrentTask == hOldTask )
593 mode = MODE_YIELD;
594 else
595 mode = MODE_SLEEP;
597 else
599 /* We are called by a deleted 16-bit task or a 32-bit thread */
601 /* The only situation where we need to do something is if we
602 now do not have a current task. Then, we'll need to wake up
603 some task that has events pending. */
605 if ( !hCurrentTask || hCurrentTask == hOldTask )
606 mode = MODE_WAKEUP;
607 else
609 /* nothing to do */
610 SYSLEVEL_LeaveWin16Lock();
611 return;
615 /* Find a task to yield to: check for DirectedYield() */
616 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
618 hNewTask = pOldTask->hYieldTo;
619 pNewTask = (TDB *)GlobalLock16( hNewTask );
620 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
621 pOldTask->hYieldTo = 0;
624 /* Find a task to yield to: check for pending events */
625 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
627 hNewTask = hFirstTask;
628 while (hNewTask)
630 pNewTask = (TDB *)GlobalLock16( hNewTask );
632 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
634 if (pNewTask->nEvents) break;
635 hNewTask = pNewTask->hNext;
637 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
640 /* If we are still the task with highest priority, just return ... */
641 if ( mode == MODE_YIELD && hNewTask == hCurrentTask )
643 TRACE("returning to the current task (%04x)\n", hCurrentTask );
644 SYSLEVEL_LeaveWin16Lock();
646 /* Allow Win32 threads to thunk down even while a Win16 task is
647 in a tight PeekMessage() or Yield() loop ... */
648 ReleaseThunkLock( &lockCount );
649 RestoreThunkLock( lockCount );
650 return;
653 /* If no task to yield to found, suspend 16-bit scheduler ... */
654 if ( mode == MODE_YIELD && !hNewTask )
656 TRACE("No currently active task\n");
657 hCurrentTask = 0;
660 /* If we found a task to wake up, do it ... */
661 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
663 TRACE("Switching to task %04x (%.8s)\n",
664 hNewTask, pNewTask->module_name );
666 pNewTask->priority++;
667 TASK_UnlinkTask( hNewTask );
668 TASK_LinkTask( hNewTask );
669 pNewTask->priority--;
671 hCurrentTask = hNewTask;
672 SetEvent( pNewTask->hEvent );
674 /* This is set just in case some app reads it ... */
675 pNewTask->ss_sp = pNewTask->teb->cur_stack;
678 /* If we need to put the current task to sleep, do it ... */
679 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
681 ResetEvent( pOldTask->hEvent );
683 ReleaseThunkLock( &lockCount );
684 SYSLEVEL_CheckNotLevel( 1 );
685 WaitForSingleObject( pOldTask->hEvent, INFINITE );
686 RestoreThunkLock( lockCount );
689 SYSLEVEL_LeaveWin16Lock();
692 /***********************************************************************
693 * InitTask (KERNEL.91)
695 * Called by the application startup code.
697 void WINAPI InitTask16( CONTEXT86 *context )
699 TDB *pTask;
700 INSTANCEDATA *pinstance;
701 SEGPTR ptr;
703 EAX_reg(context) = 0;
704 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
706 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
707 as some apps, notably Visual Basic apps, *modify* the heap/stack
708 size of the instance data segment before calling InitTask() */
710 /* Initialize the INSTANCEDATA structure */
711 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
712 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack );
713 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
714 pinstance->stacktop = ( pinstance->stackmin > BX_reg(context)?
715 pinstance->stackmin - BX_reg(context) : 0 ) + 150;
717 /* Initialize the local heap */
718 if ( CX_reg(context) )
719 LocalInit16( pTask->hInstance, 0, CX_reg(context) );
721 /* Initialize implicitly loaded DLLs */
722 NE_InitializeDLLs( pTask->hModule );
723 NE_DllProcessAttach( pTask->hModule );
725 /* Registers on return are:
726 * ax 1 if OK, 0 on error
727 * cx stack limit in bytes
728 * dx cmdShow parameter
729 * si instance handle of the previous instance
730 * di instance handle of the new task
731 * es:bx pointer to command-line inside PSP
733 * 0 (=%bp) is pushed on the stack
735 ptr = stack16_push( sizeof(WORD) );
736 *(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
737 ESP_reg(context) -= 2;
739 EAX_reg(context) = 1;
741 if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
742 else
744 LPBYTE p = &pTask->pdb.cmdLine[1];
745 while ((*p == ' ') || (*p == '\t')) p++;
746 EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
748 ECX_reg(context) = pinstance->stacktop;
749 EDX_reg(context) = pTask->nCmdShow;
750 ESI_reg(context) = (DWORD)pTask->hPrevInstance;
751 EDI_reg(context) = (DWORD)pTask->hInstance;
752 ES_reg (context) = (WORD)pTask->hPDB;
756 /***********************************************************************
757 * WaitEvent (KERNEL.30)
759 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
761 TDB *pTask;
763 if (!hTask) hTask = GetCurrentTask();
764 pTask = (TDB *)GlobalLock16( hTask );
766 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
768 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
769 return TRUE;
772 if (pTask->nEvents > 0)
774 pTask->nEvents--;
775 return FALSE;
777 TASK_Reschedule();
779 /* When we get back here, we have an event */
781 if (pTask->nEvents > 0) pTask->nEvents--;
782 return TRUE;
786 /***********************************************************************
787 * PostEvent (KERNEL.31)
789 void WINAPI PostEvent16( HTASK16 hTask )
791 TDB *pTask;
793 if (!hTask) hTask = GetCurrentTask();
794 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
796 if ( !THREAD_IsWin16( pTask->teb ) )
798 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
799 return;
802 pTask->nEvents++;
804 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
805 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
806 TASK_Reschedule();
810 /***********************************************************************
811 * SetPriority (KERNEL.32)
813 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
815 TDB *pTask;
816 INT16 newpriority;
818 if (!hTask) hTask = GetCurrentTask();
819 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
820 newpriority = pTask->priority + delta;
821 if (newpriority < -32) newpriority = -32;
822 else if (newpriority > 15) newpriority = 15;
824 pTask->priority = newpriority + 1;
825 TASK_UnlinkTask( hTask );
826 TASK_LinkTask( hTask );
827 pTask->priority--;
831 /***********************************************************************
832 * LockCurrentTask (KERNEL.33)
834 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
836 if (bLock) hLockedTask = GetCurrentTask();
837 else hLockedTask = 0;
838 return hLockedTask;
842 /***********************************************************************
843 * IsTaskLocked (KERNEL.122)
845 HTASK16 WINAPI IsTaskLocked16(void)
847 return hLockedTask;
851 /***********************************************************************
852 * OldYield (KERNEL.117)
854 void WINAPI OldYield16(void)
856 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
858 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
860 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
861 return;
864 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
865 TASK_Reschedule();
866 if (pCurTask) pCurTask->nEvents--;
869 /***********************************************************************
870 * WIN32_OldYield16 (KERNEL.447)
872 void WINAPI WIN32_OldYield16(void)
874 DWORD count;
876 ReleaseThunkLock(&count);
877 RestoreThunkLock(count);
880 /***********************************************************************
881 * DirectedYield (KERNEL.150)
883 void WINAPI DirectedYield16( HTASK16 hTask )
885 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
887 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
889 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
890 return;
893 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
895 pCurTask->hYieldTo = hTask;
896 OldYield16();
898 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
901 /***********************************************************************
902 * Yield16 (KERNEL.29)
904 void WINAPI Yield16(void)
906 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
908 if (pCurTask) pCurTask->hYieldTo = 0;
909 if (pCurTask && pCurTask->hQueue) Callout.UserYield16();
910 else OldYield16();
913 /***********************************************************************
914 * KERNEL_490 (KERNEL.490)
916 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
918 if ( !someTask ) return 0;
920 FIXME("(%04x): stub\n", someTask );
921 return 0;
924 /***********************************************************************
925 * MakeProcInstance16 (KERNEL.51)
927 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
929 BYTE *thunk,*lfunc;
930 SEGPTR thunkaddr;
932 TRACE("(%08lx, %04x);", (DWORD)func, hInstance);
934 if (!HIWORD(func)) {
935 /* Win95 actually protects via SEH, but this is better for debugging */
936 ERR("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
937 return (FARPROC16)0;
940 if (hInstance)
942 if ( (!(hInstance & 4)) ||
943 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
945 ERR("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
946 hInstance);
947 return 0;
951 if ( (CURRENT_DS != hInstance)
952 && (hInstance != 0)
953 && (hInstance != 0xffff) )
955 /* calling MPI with a foreign DSEG is invalid ! */
956 ERR("Problem with hInstance? Got %04x, using %04x instead\n",
957 hInstance,CURRENT_DS);
960 /* Always use the DSEG that MPI was entered with.
961 * We used to set hInstance to GetTaskDS16(), but this should be wrong
962 * as CURRENT_DS provides the DSEG value we need.
963 * ("calling" DS, *not* "task" DS !) */
964 hInstance = CURRENT_DS;
966 /* no thunking for DLLs */
967 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
968 return func;
970 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
971 if (!thunkaddr) return (FARPROC16)0;
972 thunk = PTR_SEG_TO_LIN( thunkaddr );
973 lfunc = PTR_SEG_TO_LIN( func );
975 TRACE("(%08lx,%04x): got thunk %08lx\n",
976 (DWORD)func, hInstance, (DWORD)thunkaddr );
977 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
978 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
980 FIXME("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
983 *thunk++ = 0xb8; /* movw instance, %ax */
984 *thunk++ = (BYTE)(hInstance & 0xff);
985 *thunk++ = (BYTE)(hInstance >> 8);
986 *thunk++ = 0xea; /* ljmp func */
987 *(DWORD *)thunk = (DWORD)func;
988 return (FARPROC16)thunkaddr;
989 /* CX reg indicates if thunkaddr != NULL, implement if needed */
993 /***********************************************************************
994 * FreeProcInstance16 (KERNEL.52)
996 void WINAPI FreeProcInstance16( FARPROC16 func )
998 TRACE("(%08lx)\n", (DWORD)func );
999 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
1002 /**********************************************************************
1003 * TASK_GetCodeSegment
1005 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
1006 * and logical segment number of a given code segment.
1008 * 'proc' either *is* already a pair of module handle and segment number,
1009 * in which case there's nothing to do. Otherwise, it is a pointer to
1010 * a function, and we need to retrieve the code segment. If the pointer
1011 * happens to point to a thunk, we'll retrieve info about the code segment
1012 * where the function pointed to by the thunk resides, not the thunk itself.
1014 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
1015 * the function the snoop code will return to ...
1018 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
1019 SEGTABLEENTRY **ppSeg, int *pSegNr )
1021 NE_MODULE *pModule = NULL;
1022 SEGTABLEENTRY *pSeg = NULL;
1023 int segNr;
1025 /* Try pair of module handle / segment number */
1026 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
1027 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
1029 segNr = LOWORD( proc );
1030 if ( segNr && segNr <= pModule->seg_count )
1031 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
1034 /* Try thunk or function */
1035 else
1037 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
1038 WORD selector;
1040 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1041 selector = thunk[6] + (thunk[7] << 8);
1042 else
1043 selector = HIWORD( proc );
1045 pModule = NE_GetPtr( GlobalHandle16( selector ) );
1046 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
1048 if ( pModule )
1049 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
1050 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
1051 break;
1053 if ( pModule && segNr > pModule->seg_count )
1054 pSeg = NULL;
1057 /* Abort if segment not found */
1059 if ( !pModule || !pSeg )
1060 return FALSE;
1062 /* Return segment data */
1064 if ( ppModule ) *ppModule = pModule;
1065 if ( ppSeg ) *ppSeg = pSeg;
1066 if ( pSegNr ) *pSegNr = segNr;
1068 return TRUE;
1071 /**********************************************************************
1072 * GetCodeHandle (KERNEL.93)
1074 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
1076 SEGTABLEENTRY *pSeg;
1078 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
1079 return (HANDLE16)0;
1081 return pSeg->hSeg;
1084 /**********************************************************************
1085 * GetCodeInfo (KERNEL.104)
1087 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
1089 NE_MODULE *pModule;
1090 SEGTABLEENTRY *pSeg;
1091 int segNr;
1093 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1094 return FALSE;
1096 /* Fill in segment information */
1098 segInfo->offSegment = pSeg->filepos;
1099 segInfo->cbSegment = pSeg->size;
1100 segInfo->flags = pSeg->flags;
1101 segInfo->cbAlloc = pSeg->minsize;
1102 segInfo->h = pSeg->hSeg;
1103 segInfo->alignShift = pModule->alignment;
1105 if ( segNr == pModule->dgroup )
1106 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1108 /* Return module handle in %es */
1110 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1112 return TRUE;
1116 /**********************************************************************
1117 * DefineHandleTable16 (KERNEL.94)
1119 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1121 return TRUE; /* FIXME */
1125 /***********************************************************************
1126 * SetTaskQueue (KERNEL.34)
1128 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1130 HQUEUE16 hPrev;
1131 TDB *pTask;
1133 if (!hTask) hTask = GetCurrentTask();
1134 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1136 hPrev = pTask->hQueue;
1137 pTask->hQueue = hQueue;
1139 return hPrev;
1143 /***********************************************************************
1144 * GetTaskQueue (KERNEL.35)
1146 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1148 TDB *pTask;
1150 if (!hTask) hTask = GetCurrentTask();
1151 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
1152 return pTask->hQueue;
1155 /***********************************************************************
1156 * SetThreadQueue (KERNEL.463)
1158 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1160 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1161 HQUEUE16 oldQueue = teb? teb->queue : 0;
1163 if ( teb )
1165 teb->queue = hQueue;
1167 if ( GetTaskQueue16( teb->process->task ) == oldQueue )
1168 SetTaskQueue16( teb->process->task, hQueue );
1171 return oldQueue;
1174 /***********************************************************************
1175 * GetThreadQueue (KERNEL.464)
1177 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1179 TEB *teb = NULL;
1180 if ( !thread )
1181 teb = NtCurrentTeb();
1182 else if ( HIWORD(thread) )
1183 teb = THREAD_IdToTEB( thread );
1184 else if ( IsTask16( (HTASK16)thread ) )
1185 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1187 return (HQUEUE16)(teb? teb->queue : 0);
1190 /***********************************************************************
1191 * SetFastQueue (KERNEL.624)
1193 VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
1195 TEB *teb = NULL;
1196 if ( !thread )
1197 teb = NtCurrentTeb();
1198 else if ( HIWORD(thread) )
1199 teb = THREAD_IdToTEB( thread );
1200 else if ( IsTask16( (HTASK16)thread ) )
1201 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
1203 if ( teb ) teb->queue = (HQUEUE16) hQueue;
1206 /***********************************************************************
1207 * GetFastQueue (KERNEL.625)
1209 HANDLE WINAPI GetFastQueue16( void )
1211 TEB *teb = NtCurrentTeb();
1212 if (!teb) return 0;
1214 if (!teb->queue)
1215 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
1217 if (!teb->queue)
1218 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1220 return (HANDLE)teb->queue;
1223 /***********************************************************************
1224 * SwitchStackTo (KERNEL.108)
1226 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1228 TDB *pTask;
1229 STACK16FRAME *oldFrame, *newFrame;
1230 INSTANCEDATA *pData;
1231 UINT16 copySize;
1233 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1234 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1235 TRACE("old=%04x:%04x new=%04x:%04x\n",
1236 SELECTOROF( pTask->teb->cur_stack ),
1237 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
1239 /* Save the old stack */
1241 oldFrame = THREAD_STACK16( pTask->teb );
1242 /* pop frame + args and push bp */
1243 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
1244 + 2 * sizeof(WORD);
1245 *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
1246 pData->stacktop = top;
1247 pData->stackmin = ptr;
1248 pData->stackbottom = ptr;
1250 /* Switch to the new stack */
1252 /* Note: we need to take the 3 arguments into account; otherwise,
1253 * the stack will underflow upon return from this function.
1255 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1256 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1257 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
1258 newFrame = THREAD_STACK16( pTask->teb );
1260 /* Copy the stack frame and the local variables to the new stack */
1262 memmove( newFrame, oldFrame, copySize );
1263 newFrame->bp = ptr;
1264 *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
1268 /***********************************************************************
1269 * SwitchStackBack (KERNEL.109)
1271 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1273 STACK16FRAME *oldFrame, *newFrame;
1274 INSTANCEDATA *pData;
1276 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1277 return;
1278 if (!pData->old_ss_sp)
1280 WARN("No previous SwitchStackTo\n" );
1281 return;
1283 TRACE("restoring stack %04x:%04x\n",
1284 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1286 oldFrame = CURRENT_STACK16;
1288 /* Pop bp from the previous stack */
1290 BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
1291 pData->old_ss_sp += sizeof(WORD);
1293 /* Switch back to the old stack */
1295 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1296 SS_reg(context) = SELECTOROF(pData->old_ss_sp);
1297 ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1298 pData->old_ss_sp = 0;
1300 /* Build a stack frame for the return */
1302 newFrame = CURRENT_STACK16;
1303 newFrame->frame32 = oldFrame->frame32;
1304 newFrame->module_cs = oldFrame->module_cs;
1305 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1306 newFrame->entry_ip = oldFrame->entry_ip;
1310 /***********************************************************************
1311 * GetTaskQueueDS16 (KERNEL.118)
1313 void WINAPI GetTaskQueueDS16(void)
1315 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1319 /***********************************************************************
1320 * GetTaskQueueES16 (KERNEL.119)
1322 void WINAPI GetTaskQueueES16(void)
1324 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1328 /***********************************************************************
1329 * GetCurrentTask (KERNEL.36)
1331 HTASK16 WINAPI GetCurrentTask(void)
1333 return PROCESS_Current()->task;
1336 DWORD WINAPI WIN16_GetCurrentTask(void)
1338 /* This is the version used by relay code; the first task is */
1339 /* returned in the high word of the result */
1340 return MAKELONG( GetCurrentTask(), hFirstTask );
1344 /***********************************************************************
1345 * GetCurrentPDB (KERNEL.37)
1347 * UNDOC: returns PSP of KERNEL in high word
1349 DWORD WINAPI GetCurrentPDB16(void)
1351 TDB *pTask;
1353 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1354 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1358 /***********************************************************************
1359 * GetCurPID16 (KERNEL.157)
1361 DWORD WINAPI GetCurPID16( DWORD unused )
1363 return 0;
1367 /***********************************************************************
1368 * GetInstanceData (KERNEL.54)
1370 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1372 char *ptr = (char *)GlobalLock16( instance );
1373 if (!ptr || !len) return 0;
1374 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1375 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1376 return len;
1380 /***********************************************************************
1381 * GetExeVersion (KERNEL.105)
1383 WORD WINAPI GetExeVersion16(void)
1385 TDB *pTask;
1387 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1388 return pTask->version;
1392 /***********************************************************************
1393 * SetErrorMode16 (KERNEL.107)
1395 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1397 TDB *pTask;
1398 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1399 pTask->error_mode = mode;
1400 return SetErrorMode( mode );
1404 /***********************************************************************
1405 * GetDOSEnvironment (KERNEL.131)
1407 SEGPTR WINAPI GetDOSEnvironment16(void)
1409 TDB *pTask;
1411 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1412 return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
1416 /***********************************************************************
1417 * GetNumTasks (KERNEL.152)
1419 UINT16 WINAPI GetNumTasks16(void)
1421 return nTaskCount;
1425 /***********************************************************************
1426 * GetTaskDS (KERNEL.155)
1428 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1429 * I don't think we need to bother with this.
1431 HINSTANCE16 WINAPI GetTaskDS16(void)
1433 TDB *pTask;
1435 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1436 return pTask->hInstance;
1439 /***********************************************************************
1440 * GetDummyModuleHandleDS (KERNEL.602)
1442 WORD WINAPI GetDummyModuleHandleDS16(void)
1444 TDB *pTask;
1445 WORD selector;
1447 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1448 if (!(pTask->flags & TDBF_WIN32)) return 0;
1449 selector = GlobalHandleToSel16( pTask->hModule );
1450 CURRENT_DS = selector;
1451 return selector;
1454 /***********************************************************************
1455 * IsTask (KERNEL.320)
1457 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1459 TDB *pTask;
1461 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1462 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1463 return (pTask->magic == TDB_MAGIC);
1467 /***********************************************************************
1468 * IsWinOldApTask16 (KERNEL.158)
1470 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1472 /* should return bit 0 of byte 0x48 in PSP */
1473 return FALSE;
1476 /***********************************************************************
1477 * SetTaskSignalProc (KERNEL.38)
1479 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1481 TDB *pTask;
1482 FARPROC16 oldProc;
1484 if (!hTask) hTask = GetCurrentTask();
1485 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
1486 oldProc = pTask->userhandler;
1487 pTask->userhandler = proc;
1488 return oldProc;
1491 /***********************************************************************
1492 * TASK_CallTaskSignalProc
1494 /* ### start build ### */
1495 extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1496 /* ### stop build ### */
1497 void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1499 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1500 if ( !pTask || !pTask->userhandler ) return;
1502 TASK_CallTo16_word_wwwww( pTask->userhandler,
1503 hTaskOrModule, uCode, 0,
1504 pTask->hInstance, pTask->hQueue );
1507 /***********************************************************************
1508 * SetSigHandler (KERNEL.140)
1510 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1511 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1513 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1514 newhandler,oldhandler,oldmode,newmode,flag );
1516 if (flag != 1) return 0;
1517 if (!newmode) newhandler = NULL; /* Default handler */
1518 if (newmode != 4)
1520 TDB *pTask;
1522 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1523 if (oldmode) *oldmode = pTask->signal_flags;
1524 pTask->signal_flags = newmode;
1525 if (oldhandler) *oldhandler = pTask->sighandler;
1526 pTask->sighandler = newhandler;
1528 return 0;
1532 /***********************************************************************
1533 * GlobalNotify (KERNEL.154)
1535 * Note that GlobalNotify does _not_ return the old NotifyProc
1536 * -- contrary to LocalNotify !!
1538 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1540 TDB *pTask;
1542 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
1543 pTask->discardhandler = proc;
1547 /***********************************************************************
1548 * GetExePtr (KERNEL.133)
1550 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1552 char *ptr;
1553 HANDLE16 owner;
1555 /* Check for module handle */
1557 if (!(ptr = GlobalLock16( handle ))) return 0;
1558 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1560 /* Search for this handle inside all tasks */
1562 *hTask = hFirstTask;
1563 while (*hTask)
1565 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1566 if ((*hTask == handle) ||
1567 (pTask->hInstance == handle) ||
1568 (pTask->hQueue == handle) ||
1569 (pTask->hPDB == handle)) return pTask->hModule;
1570 *hTask = pTask->hNext;
1573 /* Check the owner for module handle */
1575 owner = FarGetOwner16( handle );
1576 if (!(ptr = GlobalLock16( owner ))) return 0;
1577 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1579 /* Search for the owner inside all tasks */
1581 *hTask = hFirstTask;
1582 while (*hTask)
1584 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1585 if ((*hTask == owner) ||
1586 (pTask->hInstance == owner) ||
1587 (pTask->hQueue == owner) ||
1588 (pTask->hPDB == owner)) return pTask->hModule;
1589 *hTask = pTask->hNext;
1592 return 0;
1595 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1597 HTASK16 hTask = 0;
1598 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1599 STACK16FRAME *frame = CURRENT_STACK16;
1600 frame->ecx = hModule;
1601 if (hTask) frame->es = hTask;
1602 return hModule;
1605 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1607 HTASK16 hTask = 0;
1608 return GetExePtrHelper( handle, &hTask );
1612 /***********************************************************************
1613 * TaskFirst (TOOLHELP.63)
1615 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1617 lpte->hNext = hFirstTask;
1618 return TaskNext16( lpte );
1622 /***********************************************************************
1623 * TaskNext (TOOLHELP.64)
1625 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1627 TDB *pTask;
1628 INSTANCEDATA *pInstData;
1630 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1631 if (!lpte->hNext) return FALSE;
1632 pTask = (TDB *)GlobalLock16( lpte->hNext );
1633 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1634 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( pTask->hInstance, 0 );
1635 lpte->hTask = lpte->hNext;
1636 lpte->hTaskParent = pTask->hParent;
1637 lpte->hInst = pTask->hInstance;
1638 lpte->hModule = pTask->hModule;
1639 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1640 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1641 lpte->wStackTop = pInstData->stacktop;
1642 lpte->wStackMinimum = pInstData->stackmin;
1643 lpte->wStackBottom = pInstData->stackbottom;
1644 lpte->wcEvents = pTask->nEvents;
1645 lpte->hQueue = pTask->hQueue;
1646 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1647 lpte->wPSPOffset = 0x100; /*??*/
1648 lpte->hNext = pTask->hNext;
1649 return TRUE;
1653 /***********************************************************************
1654 * TaskFindHandle (TOOLHELP.65)
1656 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1658 lpte->hNext = hTask;
1659 return TaskNext16( lpte );
1663 /***********************************************************************
1664 * GetAppCompatFlags16 (KERNEL.354)
1666 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1668 return GetAppCompatFlags( hTask );
1672 /***********************************************************************
1673 * GetAppCompatFlags (USER32.206)
1675 DWORD WINAPI GetAppCompatFlags( HTASK hTask )
1677 TDB *pTask;
1679 if (!hTask) hTask = GetCurrentTask();
1680 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1681 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1682 return pTask->compat_flags;