4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
27 #include "selectors.h"
28 #include "stackframe.h"
36 #include "debugtools.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 */
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 HTASK16 hTaskToKill
= 0;
63 static UINT16 nTaskCount
= 0;
65 static HANDLE TASK_ScheduleEvent
= INVALID_HANDLE_VALUE
;
67 static void TASK_YieldToSystem( void );
70 /***********************************************************************
73 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
75 THHOOK
*pOldThhook
= pThhook
;
77 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
79 *pThhook
= *pOldThhook
;
82 /***********************************************************************
85 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
87 TDB
* pTask
= (TDB
*)GlobalLock16(hTask
);
89 if (pTask
->hNext
) return pTask
->hNext
;
90 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
93 /***********************************************************************
96 static void TASK_LinkTask( HTASK16 hTask
)
101 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
102 prevTask
= &hFirstTask
;
105 TDB
*prevTaskPtr
= (TDB
*)GlobalLock16( *prevTask
);
106 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
107 prevTask
= &prevTaskPtr
->hNext
;
109 pTask
->hNext
= *prevTask
;
115 /***********************************************************************
118 static void TASK_UnlinkTask( HTASK16 hTask
)
123 prevTask
= &hFirstTask
;
124 while (*prevTask
&& (*prevTask
!= hTask
))
126 pTask
= (TDB
*)GlobalLock16( *prevTask
);
127 prevTask
= &pTask
->hNext
;
131 pTask
= (TDB
*)GlobalLock16( *prevTask
);
132 *prevTask
= pTask
->hNext
;
139 /***********************************************************************
142 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
143 * and containing 'count' entries.
145 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
151 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
153 pThunk
->magic
= THUNK_MAGIC
;
154 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
156 for (i
= 0; i
< count
-1; i
++)
158 free
+= 8; /* Offset of next thunk */
159 pThunk
->thunks
[4*i
] = free
;
161 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
165 /***********************************************************************
168 * Allocate a thunk for MakeProcInstance().
170 static SEGPTR
TASK_AllocThunk( HTASK16 hTask
)
176 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
177 sel
= pTask
->hCSAlias
;
178 pThunk
= &pTask
->thunks
;
179 base
= (int)pThunk
- (int)pTask
;
180 while (!pThunk
->free
)
183 if (!sel
) /* Allocate a new segment */
185 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
186 pTask
->hPDB
, TRUE
, FALSE
, FALSE
);
187 if (!sel
) return (SEGPTR
)0;
188 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
191 pThunk
= (THUNKS
*)GlobalLock16( sel
);
194 base
+= pThunk
->free
;
195 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
196 return PTR_SEG_OFF_TO_SEGPTR( sel
, base
);
200 /***********************************************************************
203 * Free a MakeProcInstance() thunk.
205 static BOOL
TASK_FreeThunk( HTASK16 hTask
, SEGPTR thunk
)
211 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
212 sel
= pTask
->hCSAlias
;
213 pThunk
= &pTask
->thunks
;
214 base
= (int)pThunk
- (int)pTask
;
215 while (sel
&& (sel
!= HIWORD(thunk
)))
218 pThunk
= (THUNKS
*)GlobalLock16( sel
);
221 if (!sel
) return FALSE
;
222 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
223 pThunk
->free
= LOWORD(thunk
) - base
;
228 /***********************************************************************
231 * 32-bit entry point for a new task. This function is responsible for
232 * setting up the registers and jumping to the 16-bit entry point.
234 static void TASK_CallToStart(void)
236 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
237 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
238 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
240 SYSDEPS_SetCurThread( pTask
->thdb
);
243 /* Terminate the stack frame chain */
244 memset(THREAD_STACK16( pTask
->thdb
), '\0', sizeof(STACK16FRAME
));
246 /* Initialize process critical section */
247 InitializeCriticalSection( &PROCESS_Current()->crit_section
);
249 /* Call USER signal proc */
250 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 ); /* for initial thread */
251 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
252 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
253 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
255 if (pModule
->flags
& NE_FFLAGS_WIN32
)
257 ERR_(task
)("Called for Win32 task!\n" );
260 else if (pModule
->dos_image
)
267 /* Registers at initialization must be:
269 * bx stack size in bytes
270 * cx heap size in bytes
271 * si previous app instance
272 * di current app instance
274 * es selector to the PSP
275 * ds dgroup of the application
277 * sp top of the stack
281 memset( &context
, 0, sizeof(context
) );
282 CS_reg(&context
) = GlobalHandleToSel16(pSegTable
[pModule
->cs
- 1].hSeg
);
283 DS_reg(&context
) = GlobalHandleToSel16(pSegTable
[pModule
->dgroup
- 1].hSeg
);
284 ES_reg(&context
) = pTask
->hPDB
;
285 EIP_reg(&context
) = pModule
->ip
;
286 EBX_reg(&context
) = pModule
->stack_size
;
287 ECX_reg(&context
) = pModule
->heap_size
;
288 EDI_reg(&context
) = context
.SegDs
;
290 TRACE_(task
)("Starting main program: cs:ip=%04lx:%04x ds=%04lx ss:sp=%04x:%04x\n",
291 CS_reg(&context
), IP_reg(&context
), DS_reg(&context
),
292 SELECTOROF(pTask
->thdb
->cur_stack
),
293 OFFSETOF(pTask
->thdb
->cur_stack
) );
295 Callbacks
->CallRegisterShortProc( &context
, 0 );
296 /* This should never return */
297 ERR_(task
)("Main program returned! (should never happen)\n" );
303 /***********************************************************************
306 * NOTE: This routine might be called by a Win32 thread. We don't have
307 * any real problems with that, since we operated merely on a private
308 * TDB structure that is not yet linked into the task list.
310 BOOL
TASK_Create( THDB
*thdb
, NE_MODULE
*pModule
, HINSTANCE16 hInstance
,
311 HINSTANCE16 hPrevInstance
, UINT16 cmdShow
)
319 STACK16FRAME
*frame16
;
320 STACK32FRAME
*frame32
;
321 PDB
*pdb32
= thdb
->process
;
322 SEGTABLEENTRY
*pSegTable
= NE_SEG_TABLE( pModule
);
324 /* Allocate the task structure */
326 hTask
= GLOBAL_Alloc( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
),
327 pModule
->self
, FALSE
, FALSE
, FALSE
);
328 if (!hTask
) return FALSE
;
329 pTask
= (TDB
*)GlobalLock16( hTask
);
331 /* Fill the task structure */
334 pTask
->hSelf
= hTask
;
337 if (pModule
->flags
& NE_FFLAGS_WIN32
)
338 pTask
->flags
|= TDBF_WIN32
;
339 if (pModule
->lpDosTask
)
340 pTask
->flags
|= TDBF_WINOLDAP
;
342 pTask
->version
= pModule
->expected_version
;
343 pTask
->hInstance
= hInstance
? hInstance
: pModule
->self
;
344 pTask
->hPrevInstance
= hPrevInstance
;
345 pTask
->hModule
= pModule
->self
;
346 pTask
->hParent
= GetCurrentTask();
347 pTask
->magic
= TDB_MAGIC
;
348 pTask
->nCmdShow
= cmdShow
;
350 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
351 strcpy( pTask
->curdir
, "\\" );
352 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
353 sizeof(pTask
->curdir
) - 1 );
355 /* Create the thunks block */
357 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
359 /* Copy the module name */
361 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
362 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
364 /* Allocate a selector for the PDB */
366 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
367 pModule
->self
, FALSE
, FALSE
, FALSE
, NULL
);
371 pTask
->pdb
.int20
= 0x20cd;
372 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
373 PUT_DWORD(&pTask
->pdb
.dispatcher
[1], (DWORD
)NE_GetEntryPoint(
374 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
375 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
376 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
377 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
378 pTask
->pdb
.fileHandlesPtr
=
379 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask
->hPDB
),
380 (int)&((PDB16
*)0)->fileHandles
);
381 pTask
->pdb
.hFileHandles
= 0;
382 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
383 pTask
->pdb
.environment
= pdb32
->env_db
->env_sel
;
384 pTask
->pdb
.nbFiles
= 20;
386 /* Fill the command line */
388 cmd_line
= pdb32
->env_db
->cmd_line
;
389 while (*cmd_line
&& (*cmd_line
!= ' ') && (*cmd_line
!= '\t')) cmd_line
++;
390 while ((*cmd_line
== ' ') || (*cmd_line
== '\t')) cmd_line
++;
391 lstrcpynA( pTask
->pdb
.cmdLine
+1, cmd_line
, sizeof(pTask
->pdb
.cmdLine
)-1);
392 pTask
->pdb
.cmdLine
[0] = strlen( pTask
->pdb
.cmdLine
+ 1 );
394 /* Get the compatibility flags */
396 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
398 /* Allocate a code segment alias for the TDB */
400 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
401 sizeof(TDB
), pTask
->hPDB
, TRUE
,
402 FALSE
, FALSE
, NULL
);
404 /* Set the owner of the environment block */
406 FarSetOwner16( pTask
->pdb
.environment
, pTask
->hPDB
);
408 /* Default DTA overwrites command-line */
410 pTask
->dta
= PTR_SEG_OFF_TO_SEGPTR( pTask
->hPDB
,
411 (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
413 /* Enter task handle into thread and process */
415 pTask
->thdb
->teb
.htask16
= pTask
->thdb
->process
->task
= hTask
;
416 TRACE_(task
)("module='%s' cmdline='%s' task=%04x\n", name
, cmd_line
, hTask
);
418 if (pTask
->flags
& TDBF_WIN32
) return TRUE
;
420 /* If we have a DGROUP/hInstance, use it for 16-bit stack */
424 if (!(sp
= pModule
->sp
))
425 sp
= pSegTable
[pModule
->ss
-1].minsize
+ pModule
->stack_size
;
426 sp
&= ~1; sp
-= sizeof(STACK16FRAME
);
427 pTask
->thdb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( hInstance
, sp
);
430 /* Create the 16-bit stack frame */
432 pTask
->thdb
->cur_stack
-= sizeof(STACK16FRAME
);
433 frame16
= (STACK16FRAME
*)PTR_SEG_TO_LIN( pTask
->thdb
->cur_stack
);
434 frame16
->ebp
= OFFSETOF( pTask
->thdb
->cur_stack
) + (int)&((STACK16FRAME
*)0)->bp
;
435 frame16
->bp
= LOWORD(frame16
->ebp
);
436 frame16
->ds
= frame16
->es
= hInstance
;
438 frame16
->entry_point
= 0;
439 frame16
->entry_cs
= 0;
440 frame16
->mutex_count
= 1; /* TASK_Reschedule is called from 16-bit code */
441 /* The remaining fields will be initialized in TASK_Reschedule */
443 /* Create the 32-bit stack frame */
445 stack32Top
= (char*)pTask
->thdb
->teb
.stack_top
;
446 frame16
->frame32
= frame32
= (STACK32FRAME
*)stack32Top
- 1;
447 frame32
->frame16
= pTask
->thdb
->cur_stack
+ sizeof(STACK16FRAME
);
453 frame32
->retaddr
= (DWORD
)TASK_CallToStart
;
454 /* The remaining fields will be initialized in TASK_Reschedule */
459 /***********************************************************************
462 * NOTE: This routine might be called by a Win32 thread. Thus, we need
463 * to be careful to protect global data structures. We do this
464 * by entering the Win16Lock while linking the task into the
467 void TASK_StartTask( HTASK16 hTask
)
469 TDB
*pTask
= (TDB
*)GlobalLock16( hTask
);
470 if ( !pTask
) return;
472 /* Add the task to the linked list */
474 SYSLEVEL_EnterWin16Lock();
475 TASK_LinkTask( hTask
);
476 SYSLEVEL_LeaveWin16Lock();
478 TRACE_(task
)("linked task %04x\n", hTask
);
480 /* If requested, add entry point breakpoint */
482 if ( TASK_AddTaskEntryBreakpoint
)
483 TASK_AddTaskEntryBreakpoint( hTask
);
485 /* Get the task up and running. */
487 if ( THREAD_IsWin16( pTask
->thdb
) )
489 /* Post event to start the task */
490 PostEvent16( hTask
);
492 /* If we ourselves are a 16-bit task, we Yield() directly. */
493 if ( THREAD_IsWin16( THREAD_Current() ) )
499 /***********************************************************************
502 static void TASK_DeleteTask( HTASK16 hTask
)
507 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
510 pTask
->magic
= 0xdead; /* invalidate signature */
512 /* Delete the Win32 part of the task */
514 /* PROCESS_FreePDB( pTask->thdb->process ); FIXME */
515 /* K32OBJ_DecCount( &pTask->thdb->header ); FIXME */
517 /* Free the selector aliases */
519 GLOBAL_FreeBlock( pTask
->hCSAlias
);
520 GLOBAL_FreeBlock( pTask
->hPDB
);
522 /* Free the task module */
524 FreeModule16( pTask
->hModule
);
526 /* Free the task structure itself */
528 GlobalFree16( hTask
);
530 /* Free all memory used by this task (including the 32-bit stack, */
531 /* the environment block and the thunk segments). */
533 GlobalFreeAll16( hPDB
);
536 /***********************************************************************
539 void TASK_KillTask( HTASK16 hTask
)
543 /* Enter the Win16Lock to protect global data structures */
544 SYSLEVEL_EnterWin16Lock();
546 if ( !hTask
) hTask
= GetCurrentTask();
547 pTask
= (TDB
*)GlobalLock16( hTask
);
550 SYSLEVEL_LeaveWin16Lock();
554 TRACE_(task
)("Killing task %04x\n", hTask
);
556 /* Delete active sockets */
559 WINSOCK_DeleteTaskWSI( pTask
, pTask
->pwsi
);
563 /* Kill DOS VM task */
564 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
565 if ( pModule
->lpDosTask
)
566 MZ_KillModule( pModule
->lpDosTask
);
570 /* Perform USER cleanup */
572 if (pTask
->userhandler
)
573 pTask
->userhandler( hTask
, USIG16_TERMINATION
, 0,
574 pTask
->hInstance
, pTask
->hQueue
);
576 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
577 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 ); /* FIXME */
578 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
582 TRACE_(task
)("this is the last task, exiting\n" );
586 /* FIXME: Hack! Send a message to the initial task so that
587 * the GetMessage wakes up and the initial task can check whether
588 * it is the only remaining one and terminate itself ...
589 * The initial task should probably install hooks or something
590 * to get informed about task termination :-/
592 Callout
.PostAppMessage16( PROCESS_Initial()->task
, WM_NULL
, 0, 0 );
594 /* Remove the task from the list to be sure we never switch back to it */
595 TASK_UnlinkTask( hTask
);
598 TDB
* p
= (TDB
*)GlobalLock16( hFirstTask
);
601 if( p
->hYieldTo
== hTask
) p
->hYieldTo
= 0;
602 p
= (TDB
*)GlobalLock16( p
->hNext
);
608 if ( hLockedTask
== hTask
)
611 if ( hTaskToKill
&& ( hTaskToKill
!= hCurrentTask
) )
613 /* If another task is already marked for destruction, */
614 /* we can kill it now, as we are in another context. */
615 TASK_DeleteTask( hTaskToKill
);
620 * If hTask is not the task currently scheduled by the Win16
621 * scheduler, we simply delete it; otherwise we mark it for
622 * destruction. Note that if the current task is a 32-bit
623 * one, hCurrentTask is *different* from GetCurrentTask()!
625 if ( hTask
== hCurrentTask
)
627 assert( hTaskToKill
== 0 || hTaskToKill
== hCurrentTask
);
628 hTaskToKill
= hCurrentTask
;
631 TASK_DeleteTask( hTask
);
633 SYSLEVEL_LeaveWin16Lock();
637 /***********************************************************************
638 * TASK_KillCurrentTask
640 * Kill the currently running task. As it's not possible to kill the
641 * current task like this, it is simply marked for destruction, and will
642 * be killed when either TASK_Reschedule or this function is called again
643 * in the context of another task.
645 void TASK_KillCurrentTask( INT16 exitCode
)
647 if ( !THREAD_IsWin16( THREAD_Current() ) )
649 FIXME_(task
)("called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel
);
653 assert(hCurrentTask
== GetCurrentTask());
655 TRACE_(task
)("Killing current task %04x\n", hCurrentTask
);
659 TASK_YieldToSystem();
661 /* We should never return from this Yield() */
663 ERR_(task
)("Return of the living dead %04x!!!\n", hCurrentTask
);
667 /***********************************************************************
670 * This is where all the magic of task-switching happens!
672 * Note: This function should only be called via the TASK_YieldToSystem()
673 * wrapper, to make sure that all the context is saved correctly.
675 * It must not call functions that may yield control.
677 BOOL
TASK_Reschedule(void)
679 TDB
*pOldTask
= NULL
, *pNewTask
;
681 STACK16FRAME
*newframe16
;
683 /* Create scheduler event */
684 if ( TASK_ScheduleEvent
== INVALID_HANDLE_VALUE
)
686 TASK_ScheduleEvent
= CreateEventA( NULL
, TRUE
, FALSE
, NULL
);
687 TASK_ScheduleEvent
= ConvertToGlobalHandle( TASK_ScheduleEvent
);
690 /* Get the initial task up and running */
691 if (!hCurrentTask
&& GetCurrentTask())
693 /* We need to remove one pair of stackframes (exept for Winelib) */
694 STACK16FRAME
*oldframe16
= CURRENT_STACK16
;
695 STACK32FRAME
*oldframe32
= oldframe16
? oldframe16
->frame32
: NULL
;
696 STACK16FRAME
*newframe16
= oldframe32
? PTR_SEG_TO_LIN( oldframe32
->frame16
) : NULL
;
697 STACK32FRAME
*newframe32
= newframe16
? newframe16
->frame32
: NULL
;
700 newframe16
->entry_ip
= oldframe16
->entry_ip
;
701 newframe16
->entry_cs
= oldframe16
->entry_cs
;
702 newframe16
->ip
= oldframe16
->ip
;
703 newframe16
->cs
= oldframe16
->cs
;
704 newframe32
->ebp
= oldframe32
->ebp
;
705 newframe32
->restore_addr
= oldframe32
->restore_addr
;
706 newframe32
->codeselector
= oldframe32
->codeselector
;
708 THREAD_Current()->cur_stack
= oldframe32
->frame16
;
711 hCurrentTask
= GetCurrentTask();
712 pNewTask
= (TDB
*)GlobalLock16( hCurrentTask
);
713 pNewTask
->ss_sp
= pNewTask
->thdb
->cur_stack
;
717 /* NOTE: As we are entered from 16-bit code, we hold the Win16Lock.
718 We hang onto it thoughout most of this routine, so that accesses
719 to global variables (most notably the task list) are protected. */
720 assert(hCurrentTask
== GetCurrentTask());
722 TRACE_(task
)("entered with hTask %04x (pid %d)\n", hCurrentTask
, getpid());
725 /* FIXME: What about the Win16Lock ??? */
728 /* First check if there's a task to kill */
730 if (hTaskToKill
&& (hTaskToKill
!= hCurrentTask
))
732 TASK_DeleteTask( hTaskToKill
);
736 /* Find a task to yield to */
738 pOldTask
= (TDB
*)GlobalLock16( hCurrentTask
);
739 if (pOldTask
&& pOldTask
->hYieldTo
)
741 /* check for DirectedYield() */
743 hTask
= pOldTask
->hYieldTo
;
744 pNewTask
= (TDB
*)GlobalLock16( hTask
);
745 if( !pNewTask
|| !pNewTask
->nEvents
) hTask
= 0;
746 pOldTask
->hYieldTo
= 0;
751 /* Find a task that has an event pending */
756 pNewTask
= (TDB
*)GlobalLock16( hTask
);
758 TRACE_(task
)("\ttask = %04x, events = %i\n", hTask
, pNewTask
->nEvents
);
760 if (pNewTask
->nEvents
) break;
761 hTask
= pNewTask
->hNext
;
763 if (hLockedTask
&& (hTask
!= hLockedTask
)) hTask
= 0;
766 /* No task found, wait for some events to come in */
768 /* NOTE: We release the Win16Lock while waiting for events. This is to enable
769 Win32 threads to thunk down to 16-bit temporarily. Since Win16
770 tasks won't execute and Win32 threads are not allowed to enter
771 TASK_Reschedule anyway, there should be no re-entrancy problem ... */
773 ResetEvent( TASK_ScheduleEvent
);
774 SYSLEVEL_ReleaseWin16Lock();
775 WaitForSingleObject( TASK_ScheduleEvent
, INFINITE
);
776 SYSLEVEL_RestoreWin16Lock();
779 if (hTask
== hCurrentTask
)
781 /* Allow Win32 threads to thunk down even while a Win16 task is
782 in a tight PeekMessage() or Yield() loop ... */
783 SYSLEVEL_ReleaseWin16Lock();
784 SYSLEVEL_RestoreWin16Lock();
786 TRACE_(task
)("returning to the current task(%04x)\n", hTask
);
787 return FALSE
; /* Nothing to do */
789 pNewTask
= (TDB
*)GlobalLock16( hTask
);
790 TRACE_(task
)("Switching to task %04x (%.8s)\n",
791 hTask
, pNewTask
->module_name
);
793 /* Make the task the last in the linked list (round-robin scheduling) */
795 pNewTask
->priority
++;
796 TASK_UnlinkTask( hTask
);
797 TASK_LinkTask( hTask
);
798 pNewTask
->priority
--;
800 /* Finish initializing the new task stack if necessary */
802 newframe16
= THREAD_STACK16( pNewTask
->thdb
);
803 if (!newframe16
->entry_cs
)
805 STACK16FRAME
*oldframe16
= CURRENT_STACK16
;
806 STACK32FRAME
*oldframe32
= oldframe16
->frame32
;
807 STACK32FRAME
*newframe32
= newframe16
->frame32
;
808 newframe16
->entry_ip
= oldframe16
->entry_ip
;
809 newframe16
->entry_cs
= oldframe16
->entry_cs
;
810 newframe16
->ip
= oldframe16
->ip
;
811 newframe16
->cs
= oldframe16
->cs
;
812 newframe32
->ebp
= oldframe32
->ebp
;
813 newframe32
->restore_addr
= oldframe32
->restore_addr
;
814 newframe32
->codeselector
= oldframe32
->codeselector
;
817 /* Switch to the new stack */
819 /* NOTE: We need to release/restore the Win16Lock, as the task
820 switched to might be at another recursion level than
823 SYSLEVEL_ReleaseWin16Lock();
825 hCurrentTask
= hTask
;
826 SYSDEPS_SetCurThread( pNewTask
->thdb
);
827 pNewTask
->ss_sp
= pNewTask
->thdb
->cur_stack
;
829 SYSLEVEL_RestoreWin16Lock();
835 /***********************************************************************
838 * Scheduler interface, this way we ensure that all "unsafe" events are
839 * processed outside the scheduler.
841 static void TASK_YieldToSystem( void )
843 if ( !THREAD_IsWin16( THREAD_Current() ) )
845 FIXME_(task
)("called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel
);
849 EVENT_Synchronize( FALSE
);
851 Callbacks
->CallTaskRescheduleProc();
855 /***********************************************************************
856 * InitTask (KERNEL.91)
858 * Called by the application startup code.
860 void WINAPI
InitTask16( CONTEXT
*context
)
864 SEGTABLEENTRY
*pSegTable
;
865 INSTANCEDATA
*pinstance
;
866 LONG stacklow
, stackhi
;
868 if (context
) EAX_reg(context
) = 0;
869 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
870 if (!(pModule
= NE_GetPtr( pTask
->hModule
))) return;
872 /* Initialize implicitly loaded DLLs */
873 NE_InitializeDLLs( pTask
->hModule
);
877 /* Registers on return are:
878 * ax 1 if OK, 0 on error
879 * cx stack limit in bytes
880 * dx cmdShow parameter
881 * si instance handle of the previous instance
882 * di instance handle of the new task
883 * es:bx pointer to command-line inside PSP
885 * 0 (=%bp) is pushed on the stack
887 SEGPTR ptr
= STACK16_PUSH( pTask
->thdb
, sizeof(WORD
) );
888 *(WORD
*)PTR_SEG_TO_LIN(ptr
) = 0;
889 SP_reg(context
) -= 2;
891 EAX_reg(context
) = 1;
893 if (!pTask
->pdb
.cmdLine
[0]) EBX_reg(context
) = 0x80;
896 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
897 while ((*p
== ' ') || (*p
== '\t')) p
++;
898 EBX_reg(context
) = 0x80 + (p
- pTask
->pdb
.cmdLine
);
900 ECX_reg(context
) = pModule
->stack_size
;
901 EDX_reg(context
) = pTask
->nCmdShow
;
902 ESI_reg(context
) = (DWORD
)pTask
->hPrevInstance
;
903 EDI_reg(context
) = (DWORD
)pTask
->hInstance
;
904 ES_reg (context
) = (WORD
)pTask
->hPDB
;
907 /* Initialize the local heap */
908 if ( pModule
->heap_size
)
910 LocalInit16( pTask
->hInstance
, 0, pModule
->heap_size
);
913 /* Initialize the INSTANCEDATA structure */
914 pSegTable
= NE_SEG_TABLE( pModule
);
915 stacklow
= pSegTable
[pModule
->ss
- 1].minsize
;
916 stackhi
= stacklow
+ pModule
->stack_size
;
917 if (stackhi
> 0xffff) stackhi
= 0xffff;
918 pinstance
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN(CURRENT_DS
, 0);
919 pinstance
->stackbottom
= stackhi
; /* yup, that's right. Confused me too. */
920 pinstance
->stacktop
= stacklow
;
921 pinstance
->stackmin
= OFFSETOF( pTask
->thdb
->cur_stack
);
925 /***********************************************************************
926 * WaitEvent (KERNEL.30)
928 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
932 if (!hTask
) hTask
= GetCurrentTask();
933 pTask
= (TDB
*)GlobalLock16( hTask
);
935 if ( !THREAD_IsWin16( THREAD_Current() ) )
937 FIXME_(task
)("called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel
);
941 if (pTask
->nEvents
> 0)
946 TASK_YieldToSystem();
948 /* When we get back here, we have an event */
950 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
955 /***********************************************************************
956 * PostEvent (KERNEL.31)
958 void WINAPI
PostEvent16( HTASK16 hTask
)
962 if (!hTask
) hTask
= GetCurrentTask();
963 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
965 if ( !THREAD_IsWin16( pTask
->thdb
) )
967 FIXME_(task
)("called for Win32 thread (%04x)!\n", pTask
->thdb
->teb_sel
);
972 SetEvent( TASK_ScheduleEvent
);
976 /***********************************************************************
977 * SetPriority (KERNEL.32)
979 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
984 if (!hTask
) hTask
= GetCurrentTask();
985 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
986 newpriority
= pTask
->priority
+ delta
;
987 if (newpriority
< -32) newpriority
= -32;
988 else if (newpriority
> 15) newpriority
= 15;
990 pTask
->priority
= newpriority
+ 1;
991 TASK_UnlinkTask( hTask
);
992 TASK_LinkTask( hTask
);
997 /***********************************************************************
998 * LockCurrentTask (KERNEL.33)
1000 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
1002 if (bLock
) hLockedTask
= GetCurrentTask();
1003 else hLockedTask
= 0;
1008 /***********************************************************************
1009 * IsTaskLocked (KERNEL.122)
1011 HTASK16 WINAPI
IsTaskLocked16(void)
1017 /***********************************************************************
1018 * OldYield (KERNEL.117)
1020 void WINAPI
OldYield16(void)
1022 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1024 if ( !THREAD_IsWin16( THREAD_Current() ) )
1026 FIXME_(task
)("called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel
);
1030 if (pCurTask
) pCurTask
->nEvents
++; /* Make sure we get back here */
1031 TASK_YieldToSystem();
1032 if (pCurTask
) pCurTask
->nEvents
--;
1036 /***********************************************************************
1037 * DirectedYield (KERNEL.150)
1039 void WINAPI
DirectedYield16( HTASK16 hTask
)
1041 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1043 if ( !THREAD_IsWin16( THREAD_Current() ) )
1045 FIXME_(task
)("called for Win32 thread (%04x)!\n", THREAD_Current()->teb_sel
);
1049 TRACE_(task
)("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
1051 pCurTask
->hYieldTo
= hTask
;
1054 TRACE_(task
)("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
1057 /***********************************************************************
1058 * Yield16 (KERNEL.29)
1060 void WINAPI
Yield16(void)
1062 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1064 if (pCurTask
) pCurTask
->hYieldTo
= 0;
1065 if (pCurTask
&& pCurTask
->hQueue
) Callout
.UserYield16();
1069 /***********************************************************************
1070 * KERNEL_490 (KERNEL.490)
1072 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
1074 if ( !someTask
) return 0;
1076 FIXME_(task
)("(%04x): stub\n", someTask
);
1080 /***********************************************************************
1081 * MakeProcInstance16 (KERNEL.51)
1083 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
1089 ERR_(task
)("Ouch ! MakeProcInstance called with func == NULL !\n");
1090 return (FARPROC16
)0; /* Windows seems to do the same */
1092 if (!hInstance
) hInstance
= CURRENT_DS
;
1093 thunkaddr
= TASK_AllocThunk( GetCurrentTask() );
1094 if (!thunkaddr
) return (FARPROC16
)0;
1095 thunk
= PTR_SEG_TO_LIN( thunkaddr
);
1096 lfunc
= PTR_SEG_TO_LIN( func
);
1098 TRACE_(task
)("(%08lx,%04x): got thunk %08lx\n",
1099 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
1100 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) ||
1101 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58))
1103 FIXME_(task
)("thunk would be useless for %p, overwriting with nop;nop;\n", func
);
1104 lfunc
[0]=0x90; /* nop */
1105 lfunc
[1]=0x90; /* nop */
1108 *thunk
++ = 0xb8; /* movw instance, %ax */
1109 *thunk
++ = (BYTE
)(hInstance
& 0xff);
1110 *thunk
++ = (BYTE
)(hInstance
>> 8);
1111 *thunk
++ = 0xea; /* ljmp func */
1112 *(DWORD
*)thunk
= (DWORD
)func
;
1113 return (FARPROC16
)thunkaddr
;
1117 /***********************************************************************
1118 * FreeProcInstance16 (KERNEL.52)
1120 void WINAPI
FreeProcInstance16( FARPROC16 func
)
1122 TRACE_(task
)("(%08lx)\n", (DWORD
)func
);
1123 TASK_FreeThunk( GetCurrentTask(), (SEGPTR
)func
);
1127 /**********************************************************************
1128 * GetCodeHandle (KERNEL.93)
1130 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
1133 BYTE
*thunk
= (BYTE
*)PTR_SEG_TO_LIN( proc
);
1135 /* Return the code segment containing 'proc'. */
1136 /* Not sure if this is really correct (shouldn't matter that much). */
1138 /* Check if it is really a thunk */
1139 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
1140 handle
= GlobalHandle16( thunk
[6] + (thunk
[7] << 8) );
1142 handle
= GlobalHandle16( HIWORD(proc
) );
1147 /**********************************************************************
1148 * GetCodeInfo (KERNEL.104)
1150 VOID WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
1152 BYTE
*thunk
= (BYTE
*)PTR_SEG_TO_LIN( proc
);
1153 NE_MODULE
*pModule
= NULL
;
1154 SEGTABLEENTRY
*pSeg
= NULL
;
1157 /* proc is either a thunk, or else a pair of module handle
1158 and segment number. In the first case, we also need to
1159 extract module and segment number. */
1161 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
1163 WORD selector
= thunk
[6] + (thunk
[7] << 8);
1164 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
1165 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
1168 for ( segNr
= 0; segNr
< pModule
->seg_count
; segNr
++, pSeg
++ )
1169 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
1172 if ( pModule
&& segNr
>= pModule
->seg_count
)
1177 pModule
= NE_GetPtr( HIWORD( proc
) );
1178 segNr
= LOWORD( proc
);
1180 if ( pModule
&& segNr
< pModule
->seg_count
)
1181 pSeg
= NE_SEG_TABLE( pModule
) + segNr
;
1184 /* fill in segment information */
1186 segInfo
->offSegment
= pSeg
? pSeg
->filepos
: 0;
1187 segInfo
->cbSegment
= pSeg
? pSeg
->size
: 0;
1188 segInfo
->flags
= pSeg
? pSeg
->flags
: 0;
1189 segInfo
->cbAlloc
= pSeg
? pSeg
->minsize
: 0;
1190 segInfo
->h
= pSeg
? pSeg
->hSeg
: 0;
1191 segInfo
->alignShift
= pModule
? pModule
->alignment
: 0;
1195 /**********************************************************************
1196 * DefineHandleTable16 (KERNEL.94)
1198 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1200 return TRUE
; /* FIXME */
1204 /***********************************************************************
1205 * SetTaskQueue (KERNEL.34)
1207 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1212 if (!hTask
) hTask
= GetCurrentTask();
1213 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1215 hPrev
= pTask
->hQueue
;
1216 pTask
->hQueue
= hQueue
;
1222 /***********************************************************************
1223 * GetTaskQueue (KERNEL.35)
1225 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1229 if (!hTask
) hTask
= GetCurrentTask();
1230 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1231 return pTask
->hQueue
;
1234 /***********************************************************************
1235 * SetThreadQueue (KERNEL.463)
1237 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1239 THDB
*thdb
= thread
? THREAD_IdToTHDB( thread
) : THREAD_Current();
1240 HQUEUE16 oldQueue
= thdb
? thdb
->teb
.queue
: 0;
1244 thdb
->teb
.queue
= hQueue
;
1246 if ( GetTaskQueue16( thdb
->process
->task
) == oldQueue
)
1247 SetTaskQueue16( thdb
->process
->task
, hQueue
);
1253 /***********************************************************************
1254 * GetThreadQueue (KERNEL.464)
1256 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1260 thdb
= THREAD_Current();
1261 else if ( HIWORD(thread
) )
1262 thdb
= THREAD_IdToTHDB( thread
);
1263 else if ( IsTask16( (HTASK16
)thread
) )
1264 thdb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->thdb
;
1266 return (HQUEUE16
)(thdb
? thdb
->teb
.queue
: 0);
1269 /***********************************************************************
1270 * SetFastQueue (KERNEL.624)
1272 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1276 thdb
= THREAD_Current();
1277 else if ( HIWORD(thread
) )
1278 thdb
= THREAD_IdToTHDB( thread
);
1279 else if ( IsTask16( (HTASK16
)thread
) )
1280 thdb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->thdb
;
1282 if ( thdb
) thdb
->teb
.queue
= (HQUEUE16
) hQueue
;
1285 /***********************************************************************
1286 * GetFastQueue (KERNEL.625)
1288 HANDLE WINAPI
GetFastQueue16( void )
1290 THDB
*thdb
= THREAD_Current();
1291 if (!thdb
) return 0;
1293 if (!thdb
->teb
.queue
)
1294 Callout
.InitThreadInput16( 0, THREAD_IsWin16(thdb
)? 4 : 5 );
1296 if (!thdb
->teb
.queue
)
1297 FIXME_(task
)("(): should initialize thread-local queue, expect failure!\n" );
1299 return (HANDLE
)thdb
->teb
.queue
;
1302 /***********************************************************************
1303 * SwitchStackTo (KERNEL.108)
1305 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1308 STACK16FRAME
*oldFrame
, *newFrame
;
1309 INSTANCEDATA
*pData
;
1312 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1313 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1314 TRACE_(task
)("old=%04x:%04x new=%04x:%04x\n",
1315 SELECTOROF( pTask
->thdb
->cur_stack
),
1316 OFFSETOF( pTask
->thdb
->cur_stack
), seg
, ptr
);
1318 /* Save the old stack */
1320 oldFrame
= THREAD_STACK16( pTask
->thdb
);
1321 /* pop frame + args and push bp */
1322 pData
->old_ss_sp
= pTask
->thdb
->cur_stack
+ sizeof(STACK16FRAME
)
1324 *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
) = oldFrame
->bp
;
1325 pData
->stacktop
= top
;
1326 pData
->stackmin
= ptr
;
1327 pData
->stackbottom
= ptr
;
1329 /* Switch to the new stack */
1331 /* Note: we need to take the 3 arguments into account; otherwise,
1332 * the stack will underflow upon return from this function.
1334 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1335 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1336 pTask
->thdb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( seg
, ptr
- copySize
);
1337 newFrame
= THREAD_STACK16( pTask
->thdb
);
1339 /* Copy the stack frame and the local variables to the new stack */
1341 memmove( newFrame
, oldFrame
, copySize
);
1343 *(WORD
*)PTR_SEG_OFF_TO_LIN( seg
, ptr
) = 0; /* clear previous bp */
1347 /***********************************************************************
1348 * SwitchStackBack (KERNEL.109)
1350 void WINAPI
SwitchStackBack16( CONTEXT
*context
)
1353 STACK16FRAME
*oldFrame
, *newFrame
;
1354 INSTANCEDATA
*pData
;
1356 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1357 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(pTask
->thdb
->cur_stack
))))
1359 if (!pData
->old_ss_sp
)
1361 WARN_(task
)("No previous SwitchStackTo\n" );
1364 TRACE_(task
)("restoring stack %04x:%04x\n",
1365 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1367 oldFrame
= THREAD_STACK16( pTask
->thdb
);
1369 /* Pop bp from the previous stack */
1371 BP_reg(context
) = *(WORD
*)PTR_SEG_TO_LIN(pData
->old_ss_sp
);
1372 pData
->old_ss_sp
+= sizeof(WORD
);
1374 /* Switch back to the old stack */
1376 pTask
->thdb
->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1377 SS_reg(context
) = SELECTOROF(pData
->old_ss_sp
);
1378 ESP_reg(context
) = OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1379 pData
->old_ss_sp
= 0;
1381 /* Build a stack frame for the return */
1383 newFrame
= THREAD_STACK16( pTask
->thdb
);
1384 newFrame
->frame32
= oldFrame
->frame32
;
1385 if (TRACE_ON(relay
))
1387 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1388 newFrame
->entry_cs
= oldFrame
->entry_cs
;
1393 /***********************************************************************
1394 * GetTaskQueueDS (KERNEL.118)
1396 void WINAPI
GetTaskQueueDS16( CONTEXT
*context
)
1398 DS_reg(context
) = GlobalHandleToSel16( GetTaskQueue16(0) );
1402 /***********************************************************************
1403 * GetTaskQueueES (KERNEL.119)
1405 void WINAPI
GetTaskQueueES16( CONTEXT
*context
)
1407 ES_reg(context
) = GlobalHandleToSel16( GetTaskQueue16(0) );
1411 /***********************************************************************
1412 * GetCurrentTask (KERNEL.36)
1414 HTASK16 WINAPI
GetCurrentTask(void)
1416 return PROCESS_Current()->task
;
1419 DWORD WINAPI
WIN16_GetCurrentTask(void)
1421 /* This is the version used by relay code; the first task is */
1422 /* returned in the high word of the result */
1423 return MAKELONG( GetCurrentTask(), hFirstTask
);
1427 /***********************************************************************
1428 * GetCurrentPDB (KERNEL.37)
1430 * UNDOC: returns PSP of KERNEL in high word
1432 DWORD WINAPI
GetCurrentPDB16(void)
1436 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1437 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1441 /***********************************************************************
1442 * GetInstanceData (KERNEL.54)
1444 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1446 char *ptr
= (char *)GlobalLock16( instance
);
1447 if (!ptr
|| !len
) return 0;
1448 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1449 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1454 /***********************************************************************
1455 * GetExeVersion (KERNEL.105)
1457 WORD WINAPI
GetExeVersion16(void)
1461 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1462 return pTask
->version
;
1466 /***********************************************************************
1467 * SetErrorMode16 (KERNEL.107)
1469 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1474 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1475 oldMode
= pTask
->error_mode
;
1476 pTask
->error_mode
= mode
;
1477 pTask
->thdb
->process
->error_mode
= mode
;
1482 /***********************************************************************
1483 * SetErrorMode32 (KERNEL32.486)
1485 UINT WINAPI
SetErrorMode( UINT mode
)
1487 return SetErrorMode16( (UINT16
)mode
);
1491 /***********************************************************************
1492 * GetDOSEnvironment (KERNEL.131)
1494 SEGPTR WINAPI
GetDOSEnvironment16(void)
1498 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1499 return PTR_SEG_OFF_TO_SEGPTR( pTask
->pdb
.environment
, 0 );
1503 /***********************************************************************
1504 * GetNumTasks (KERNEL.152)
1506 UINT16 WINAPI
GetNumTasks16(void)
1512 /***********************************************************************
1513 * GetTaskDS (KERNEL.155)
1515 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1516 * I don't think we need to bother with this.
1518 HINSTANCE16 WINAPI
GetTaskDS16(void)
1522 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1523 return pTask
->hInstance
;
1526 /***********************************************************************
1527 * GetDummyModuleHandleDS (KERNEL.602)
1529 VOID WINAPI
GetDummyModuleHandleDS16( CONTEXT
*context
)
1534 AX_reg( context
) = 0;
1535 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1536 if (!(pTask
->flags
& TDBF_WIN32
)) return;
1538 selector
= GlobalHandleToSel16( pTask
->hModule
);
1539 DS_reg( context
) = selector
;
1540 AX_reg( context
) = selector
;
1543 /***********************************************************************
1544 * IsTask (KERNEL.320)
1546 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1550 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return FALSE
;
1551 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1552 return (pTask
->magic
== TDB_MAGIC
);
1556 /***********************************************************************
1557 * SetTaskSignalProc (KERNEL.38)
1559 * Real 16-bit interface is provided by the THUNK_SetTaskSignalProc.
1561 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1566 if (!hTask
) hTask
= GetCurrentTask();
1567 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return NULL
;
1568 oldProc
= (FARPROC16
)pTask
->userhandler
;
1569 pTask
->userhandler
= (USERSIGNALPROC
)proc
;
1574 /***********************************************************************
1575 * SetSigHandler (KERNEL.140)
1577 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1578 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1580 FIXME_(task
)("(%p,%p,%p,%d,%d), unimplemented.\n",
1581 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1583 if (flag
!= 1) return 0;
1584 if (!newmode
) newhandler
= NULL
; /* Default handler */
1589 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1590 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1591 pTask
->signal_flags
= newmode
;
1592 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1593 pTask
->sighandler
= newhandler
;
1599 /***********************************************************************
1600 * GlobalNotify (KERNEL.154)
1602 * Note that GlobalNotify does _not_ return the old NotifyProc
1603 * -- contrary to LocalNotify !!
1605 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1609 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1610 pTask
->discardhandler
= proc
;
1614 /***********************************************************************
1615 * GetExePtr (KERNEL.133)
1617 static HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1622 /* Check for module handle */
1624 if (!(ptr
= GlobalLock16( handle
))) return 0;
1625 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1627 /* Search for this handle inside all tasks */
1629 *hTask
= hFirstTask
;
1632 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1633 if ((*hTask
== handle
) ||
1634 (pTask
->hInstance
== handle
) ||
1635 (pTask
->hQueue
== handle
) ||
1636 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1637 *hTask
= pTask
->hNext
;
1640 /* Check the owner for module handle */
1642 owner
= FarGetOwner16( handle
);
1643 if (!(ptr
= GlobalLock16( owner
))) return 0;
1644 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1646 /* Search for the owner inside all tasks */
1648 *hTask
= hFirstTask
;
1651 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1652 if ((*hTask
== owner
) ||
1653 (pTask
->hInstance
== owner
) ||
1654 (pTask
->hQueue
== owner
) ||
1655 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1656 *hTask
= pTask
->hNext
;
1662 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1665 return GetExePtrHelper( handle
, &dummy
);
1668 void WINAPI
WIN16_GetExePtr( CONTEXT
*context
)
1670 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), SP_reg(context
));
1671 HANDLE16 handle
= (HANDLE16
)stack
[2];
1675 hModule
= GetExePtrHelper( handle
, &hTask
);
1677 AX_reg(context
) = CX_reg(context
) = hModule
;
1678 if (hTask
) ES_reg(context
) = hTask
;
1681 /***********************************************************************
1682 * TaskFirst (TOOLHELP.63)
1684 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1686 lpte
->hNext
= hFirstTask
;
1687 return TaskNext16( lpte
);
1691 /***********************************************************************
1692 * TaskNext (TOOLHELP.64)
1694 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1697 INSTANCEDATA
*pInstData
;
1699 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1700 if (!lpte
->hNext
) return FALSE
;
1701 pTask
= (TDB
*)GlobalLock16( lpte
->hNext
);
1702 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1703 pInstData
= (INSTANCEDATA
*)PTR_SEG_OFF_TO_LIN( pTask
->hInstance
, 0 );
1704 lpte
->hTask
= lpte
->hNext
;
1705 lpte
->hTaskParent
= pTask
->hParent
;
1706 lpte
->hInst
= pTask
->hInstance
;
1707 lpte
->hModule
= pTask
->hModule
;
1708 lpte
->wSS
= SELECTOROF( pTask
->thdb
->cur_stack
);
1709 lpte
->wSP
= OFFSETOF( pTask
->thdb
->cur_stack
);
1710 lpte
->wStackTop
= pInstData
->stacktop
;
1711 lpte
->wStackMinimum
= pInstData
->stackmin
;
1712 lpte
->wStackBottom
= pInstData
->stackbottom
;
1713 lpte
->wcEvents
= pTask
->nEvents
;
1714 lpte
->hQueue
= pTask
->hQueue
;
1715 strncpy( lpte
->szModule
, pTask
->module_name
, 8 );
1716 lpte
->szModule
[8] = '\0';
1717 lpte
->wPSPOffset
= 0x100; /*??*/
1718 lpte
->hNext
= pTask
->hNext
;
1723 /***********************************************************************
1724 * TaskFindHandle (TOOLHELP.65)
1726 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1728 lpte
->hNext
= hTask
;
1729 return TaskNext16( lpte
);
1733 /***********************************************************************
1734 * GetAppCompatFlags16 (KERNEL.354)
1736 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1738 return GetAppCompatFlags( hTask
);
1742 /***********************************************************************
1743 * GetAppCompatFlags32 (USER32.206)
1745 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1749 if (!hTask
) hTask
= GetCurrentTask();
1750 if (!(pTask
=(TDB
*)GlobalLock16( (HTASK16
)hTask
))) return 0;
1751 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1752 return pTask
->compat_flags
;