4 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
22 #include "selectors.h"
23 #include "stackframe.h"
30 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(task
);
36 DECLARE_DEBUG_CHANNEL(relay
);
37 DECLARE_DEBUG_CHANNEL(toolhelp
);
39 /* Min. number of thunks allocated when creating a new segment */
43 static THHOOK DefaultThhook
;
44 THHOOK
*pThhook
= &DefaultThhook
;
46 #define hCurrentTask (pThhook->CurTDB)
47 #define hFirstTask (pThhook->HeadTDB)
48 #define hLockedTask (pThhook->LockTDB)
50 static UINT16 nTaskCount
= 0;
52 static HTASK initial_task
;
54 /***********************************************************************
57 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
59 THHOOK
*pOldThhook
= pThhook
;
61 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
63 *pThhook
= *pOldThhook
;
66 /***********************************************************************
69 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
71 TDB
* pTask
= (TDB
*)GlobalLock16(hTask
);
73 if (pTask
->hNext
) return pTask
->hNext
;
74 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
77 /***********************************************************************
80 static void TASK_LinkTask( HTASK16 hTask
)
85 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
86 prevTask
= &hFirstTask
;
89 TDB
*prevTaskPtr
= (TDB
*)GlobalLock16( *prevTask
);
90 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
91 prevTask
= &prevTaskPtr
->hNext
;
93 pTask
->hNext
= *prevTask
;
99 /***********************************************************************
102 static void TASK_UnlinkTask( HTASK16 hTask
)
107 prevTask
= &hFirstTask
;
108 while (*prevTask
&& (*prevTask
!= hTask
))
110 pTask
= (TDB
*)GlobalLock16( *prevTask
);
111 prevTask
= &pTask
->hNext
;
115 pTask
= (TDB
*)GlobalLock16( *prevTask
);
116 *prevTask
= pTask
->hNext
;
123 /***********************************************************************
126 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
127 * and containing 'count' entries.
129 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
135 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
137 pThunk
->magic
= THUNK_MAGIC
;
138 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
140 for (i
= 0; i
< count
-1; i
++)
142 free
+= 8; /* Offset of next thunk */
143 pThunk
->thunks
[4*i
] = free
;
145 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
149 /***********************************************************************
152 * Allocate a thunk for MakeProcInstance().
154 static SEGPTR
TASK_AllocThunk( HTASK16 hTask
)
160 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
161 sel
= pTask
->hCSAlias
;
162 pThunk
= &pTask
->thunks
;
163 base
= (int)pThunk
- (int)pTask
;
164 while (!pThunk
->free
)
167 if (!sel
) /* Allocate a new segment */
169 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
170 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
171 if (!sel
) return (SEGPTR
)0;
172 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
175 pThunk
= (THUNKS
*)GlobalLock16( sel
);
178 base
+= pThunk
->free
;
179 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
180 return MAKESEGPTR( sel
, base
);
184 /***********************************************************************
187 * Free a MakeProcInstance() thunk.
189 static BOOL
TASK_FreeThunk( HTASK16 hTask
, SEGPTR thunk
)
195 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
196 sel
= pTask
->hCSAlias
;
197 pThunk
= &pTask
->thunks
;
198 base
= (int)pThunk
- (int)pTask
;
199 while (sel
&& (sel
!= HIWORD(thunk
)))
202 pThunk
= (THUNKS
*)GlobalLock16( sel
);
205 if (!sel
) return FALSE
;
206 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
207 pThunk
->free
= LOWORD(thunk
) - base
;
212 /***********************************************************************
215 * NOTE: This routine might be called by a Win32 thread. Thus, we need
216 * to be careful to protect global data structures. We do this
217 * by entering the Win16Lock while linking the task into the
220 BOOL
TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
226 /* Allocate the task structure */
228 hTask
= GLOBAL_Alloc( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
),
229 pModule
->self
, WINE_LDT_FLAGS_DATA
);
230 if (!hTask
) return FALSE
;
231 pTask
= (TDB
*)GlobalLock16( hTask
);
233 /* Fill the task structure */
235 pTask
->hSelf
= hTask
;
237 if (teb
->tibflags
& TEBF_WIN32
)
239 pTask
->flags
|= TDBF_WIN32
;
240 pTask
->hInstance
= pModule
->self
;
241 pTask
->hPrevInstance
= 0;
242 /* NOTE: for 16-bit tasks, the instance handles are updated later on
246 pTask
->version
= pModule
->expected_version
;
247 pTask
->hModule
= pModule
->self
;
248 pTask
->hParent
= GetCurrentTask();
249 pTask
->magic
= TDB_MAGIC
;
250 pTask
->nCmdShow
= cmdShow
;
252 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
253 strcpy( pTask
->curdir
, "\\" );
254 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
255 sizeof(pTask
->curdir
) - 1 );
257 /* Create the thunks block */
259 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
261 /* Copy the module name */
263 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
264 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
266 /* Allocate a selector for the PDB */
268 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
269 pModule
->self
, WINE_LDT_FLAGS_DATA
);
273 pTask
->pdb
.int20
= 0x20cd;
274 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
275 PUT_DWORD(&pTask
->pdb
.dispatcher
[1], (DWORD
)GetProcAddress16( GetModuleHandle16("KERNEL"),
277 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
278 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
279 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
280 pTask
->pdb
.fileHandlesPtr
=
281 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
282 pTask
->pdb
.hFileHandles
= 0;
283 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
284 /* FIXME: should we make a copy of the environment? */
285 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
286 pTask
->pdb
.nbFiles
= 20;
288 /* Fill the command line */
292 cmdline
= GetCommandLineA();
293 /* remove the first word (program name) */
295 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
296 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
297 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
298 len
= strlen(cmdline
);
300 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
301 pTask
->pdb
.cmdLine
[0] = len
;
302 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
303 /* pTask->pdb.cmdLine[len+1] = 0; */
305 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
307 /* Get the compatibility flags */
309 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
311 /* Allocate a code segment alias for the TDB */
313 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
314 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
316 /* Default DTA overwrites command line */
318 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
320 /* Create scheduler event for 16-bit tasks */
322 if ( !(pTask
->flags
& TDBF_WIN32
) )
323 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
325 /* Enter task handle into thread and process */
327 teb
->htask16
= hTask
;
328 if (!initial_task
) initial_task
= hTask
;
330 /* Add the task to the linked list */
333 TASK_LinkTask( hTask
);
339 /***********************************************************************
342 static void TASK_DeleteTask( HTASK16 hTask
)
347 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
350 pTask
->magic
= 0xdead; /* invalidate signature */
352 /* Free the selector aliases */
354 GLOBAL_FreeBlock( pTask
->hCSAlias
);
355 GLOBAL_FreeBlock( pTask
->hPDB
);
357 /* Free the task module */
359 FreeModule16( pTask
->hModule
);
361 /* Free the task structure itself */
363 GlobalFree16( hTask
);
365 /* Free all memory used by this task (including the 32-bit stack, */
366 /* the environment block and the thunk segments). */
368 GlobalFreeAll16( hPDB
);
371 /***********************************************************************
374 void TASK_ExitTask(void)
379 /* Enter the Win16Lock to protect global data structures */
382 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
389 TRACE("Killing task %04x\n", pTask
->hSelf
);
391 /* Perform USER cleanup */
393 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
394 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
395 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
396 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
398 /* Remove the task from the list to be sure we never switch back to it */
399 TASK_UnlinkTask( pTask
->hSelf
);
401 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
403 TRACE("this is the last task, exiting\n" );
409 TDB
* p
= (TDB
*)GlobalLock16( hFirstTask
);
412 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
413 p
= (TDB
*)GlobalLock16( p
->hNext
);
419 if ( hLockedTask
== pTask
->hSelf
)
422 TASK_DeleteTask( pTask
->hSelf
);
424 /* ... schedule another one ... */
427 /* ... and completely release the Win16Lock, just in case. */
428 ReleaseThunkLock( &lockCount
);
431 /***********************************************************************
434 * This is where all the magic of task-switching happens!
436 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
437 * This means that each 16-bit task runs until it voluntarily yields
438 * control, at which point the scheduler gets active and selects the
441 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
442 * by the standard scheduler of the underlying OS. As many 16-bit apps
443 * *rely* on the behaviour of the Windows scheduler, however, we have
444 * to simulate that behaviour.
446 * This is achieved as follows: every 16-bit task is at time (except
447 * during task creation and deletion) in one of two states: either it
448 * is the one currently running, then the global variable hCurrentTask
449 * contains its task handle, or it is not currently running, then it
450 * is blocked on a special scheduler event, a global handle which
451 * is stored in the task struct.
453 * When the current task yields control, this routine gets called. Its
454 * purpose is to determine the next task to be active, signal the
455 * scheduler event of that task, and then put the current task to sleep
456 * waiting for *its* scheduler event to get signalled again.
458 * This routine can get called in a few other special situations as well:
460 * - On creation of a 16-bit task, the Unix process executing the task
461 * calls TASK_Reschedule once it has completed its initialization.
462 * At this point, the task needs to be blocked until its scheduler
463 * event is signalled the first time (this will be done by the parent
464 * process to get the task up and running).
466 * - When the task currently running terminates itself, this routine gets
467 * called and has to schedule another task, *without* blocking the
470 * - When a 32-bit thread posts an event for a 16-bit task, it might be
471 * the case that *no* 16-bit task is currently running. In this case
472 * the task that has now an event pending is to be scheduled.
475 void TASK_Reschedule(void)
477 TDB
*pOldTask
= NULL
, *pNewTask
= NULL
;
478 HTASK16 hOldTask
= 0, hNewTask
= 0;
479 enum { MODE_YIELD
, MODE_SLEEP
, MODE_WAKEUP
} mode
;
484 /* Check what we need to do */
485 hOldTask
= GetCurrentTask();
486 pOldTask
= (TDB
*)GlobalLock16( hOldTask
);
487 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
488 hCurrentTask
, hOldTask
, (long) getpid() );
490 if ( pOldTask
&& THREAD_IsWin16( NtCurrentTeb() ) )
492 /* We are called by an active (non-deleted) 16-bit task */
494 /* If we don't even have a current task, or else the current
495 task has yielded, we'll need to schedule a new task and
496 (possibly) put the calling task to sleep. Otherwise, we
497 only block the caller. */
499 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
506 /* We are called by a deleted 16-bit task or a 32-bit thread */
508 /* The only situation where we need to do something is if we
509 now do not have a current task. Then, we'll need to wake up
510 some task that has events pending. */
512 if ( !hCurrentTask
|| hCurrentTask
== hOldTask
)
522 /* Find a task to yield to: check for DirectedYield() */
523 if ( mode
== MODE_YIELD
&& pOldTask
&& pOldTask
->hYieldTo
)
525 hNewTask
= pOldTask
->hYieldTo
;
526 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
527 if( !pNewTask
|| !pNewTask
->nEvents
) hNewTask
= 0;
528 pOldTask
->hYieldTo
= 0;
531 /* Find a task to yield to: check for pending events */
532 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && !hNewTask
)
534 hNewTask
= hFirstTask
;
537 pNewTask
= (TDB
*)GlobalLock16( hNewTask
);
539 TRACE( "\ttask = %04x, events = %i\n", hNewTask
, pNewTask
->nEvents
);
541 if (pNewTask
->nEvents
) break;
542 hNewTask
= pNewTask
->hNext
;
544 if (hLockedTask
&& (hNewTask
!= hLockedTask
)) hNewTask
= 0;
547 /* If we are still the task with highest priority, just return ... */
548 if ( mode
== MODE_YIELD
&& hNewTask
&& hNewTask
== hCurrentTask
)
550 TRACE("returning to the current task (%04x)\n", hCurrentTask
);
553 /* Allow Win32 threads to thunk down even while a Win16 task is
554 in a tight PeekMessage() or Yield() loop ... */
555 ReleaseThunkLock( &lockCount
);
556 RestoreThunkLock( lockCount
);
560 /* If no task to yield to found, suspend 16-bit scheduler ... */
561 if ( mode
== MODE_YIELD
&& !hNewTask
)
563 TRACE("No currently active task\n");
567 /* If we found a task to wake up, do it ... */
568 if ( (mode
== MODE_YIELD
|| mode
== MODE_WAKEUP
) && hNewTask
)
570 TRACE("Switching to task %04x (%.8s)\n",
571 hNewTask
, pNewTask
->module_name
);
573 pNewTask
->priority
++;
574 TASK_UnlinkTask( hNewTask
);
575 TASK_LinkTask( hNewTask
);
576 pNewTask
->priority
--;
578 hCurrentTask
= hNewTask
;
579 NtSetEvent( pNewTask
->hEvent
, NULL
);
581 /* This is set just in case some app reads it ... */
582 pNewTask
->ss_sp
= pNewTask
->teb
->cur_stack
;
585 /* If we need to put the current task to sleep, do it ... */
586 if ( (mode
== MODE_YIELD
|| mode
== MODE_SLEEP
) && hOldTask
!= hCurrentTask
)
588 NtResetEvent( pOldTask
->hEvent
, NULL
);
590 ReleaseThunkLock( &lockCount
);
591 SYSLEVEL_CheckNotLevel( 1 );
592 WaitForSingleObject( pOldTask
->hEvent
, INFINITE
);
593 RestoreThunkLock( lockCount
);
599 /***********************************************************************
600 * InitTask (KERNEL.91)
602 * Called by the application startup code.
604 void WINAPI
InitTask16( CONTEXT86
*context
)
607 INSTANCEDATA
*pinstance
;
611 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
613 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
614 as some apps, notably Visual Basic apps, *modify* the heap/stack
615 size of the instance data segment before calling InitTask() */
617 /* Initialize the INSTANCEDATA structure */
618 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
619 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
620 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
621 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
622 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
624 /* Initialize the local heap */
625 if (LOWORD(context
->Ecx
))
626 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
628 /* Initialize implicitly loaded DLLs */
629 NE_InitializeDLLs( pTask
->hModule
);
630 NE_DllProcessAttach( pTask
->hModule
);
632 /* Registers on return are:
633 * ax 1 if OK, 0 on error
634 * cx stack limit in bytes
635 * dx cmdShow parameter
636 * si instance handle of the previous instance
637 * di instance handle of the new task
638 * es:bx pointer to command line inside PSP
640 * 0 (=%bp) is pushed on the stack
642 ptr
= stack16_push( sizeof(WORD
) );
643 *(WORD
*)MapSL(ptr
) = 0;
648 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
651 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
652 while ((*p
== ' ') || (*p
== '\t')) p
++;
653 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
655 context
->Ecx
= pinstance
->stacktop
;
656 context
->Edx
= pTask
->nCmdShow
;
657 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
658 context
->Edi
= (DWORD
)pTask
->hInstance
;
659 context
->SegEs
= (WORD
)pTask
->hPDB
;
663 /***********************************************************************
664 * WaitEvent (KERNEL.30)
666 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
670 if (!hTask
) hTask
= GetCurrentTask();
671 pTask
= (TDB
*)GlobalLock16( hTask
);
673 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
675 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
679 if (pTask
->nEvents
> 0)
686 /* When we get back here, we have an event */
688 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
693 /***********************************************************************
694 * PostEvent (KERNEL.31)
696 void WINAPI
PostEvent16( HTASK16 hTask
)
700 if (!hTask
) hTask
= GetCurrentTask();
701 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
703 if ( !THREAD_IsWin16( pTask
->teb
) )
705 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
711 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
712 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
717 /***********************************************************************
718 * SetPriority (KERNEL.32)
720 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
725 if (!hTask
) hTask
= GetCurrentTask();
726 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return;
727 newpriority
= pTask
->priority
+ delta
;
728 if (newpriority
< -32) newpriority
= -32;
729 else if (newpriority
> 15) newpriority
= 15;
731 pTask
->priority
= newpriority
+ 1;
732 TASK_UnlinkTask( hTask
);
733 TASK_LinkTask( hTask
);
738 /***********************************************************************
739 * LockCurrentTask (KERNEL.33)
741 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
743 if (bLock
) hLockedTask
= GetCurrentTask();
744 else hLockedTask
= 0;
749 /***********************************************************************
750 * IsTaskLocked (KERNEL.122)
752 HTASK16 WINAPI
IsTaskLocked16(void)
758 /***********************************************************************
759 * OldYield (KERNEL.117)
761 void WINAPI
OldYield16(void)
763 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
765 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
767 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
771 if (pCurTask
) pCurTask
->nEvents
++; /* Make sure we get back here */
773 if (pCurTask
) pCurTask
->nEvents
--;
776 /***********************************************************************
777 * WIN32_OldYield16 (KERNEL.447)
779 void WINAPI
WIN32_OldYield16(void)
783 ReleaseThunkLock(&count
);
784 RestoreThunkLock(count
);
787 /***********************************************************************
788 * DirectedYield (KERNEL.150)
790 void WINAPI
DirectedYield16( HTASK16 hTask
)
792 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
794 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
796 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
800 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
802 pCurTask
->hYieldTo
= hTask
;
805 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
808 /***********************************************************************
809 * Yield16 (KERNEL.29)
811 void WINAPI
Yield16(void)
813 TDB
*pCurTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
815 if (pCurTask
) pCurTask
->hYieldTo
= 0;
816 if (pCurTask
&& pCurTask
->hQueue
&& Callout
.UserYield16
) Callout
.UserYield16();
820 /***********************************************************************
821 * KERNEL_490 (KERNEL.490)
823 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
825 if ( !someTask
) return 0;
827 FIXME("(%04x): stub\n", someTask
);
831 /***********************************************************************
832 * MakeProcInstance16 (KERNEL.51)
834 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
838 WORD hInstanceSelector
;
840 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
842 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
845 /* Win95 actually protects via SEH, but this is better for debugging */
846 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
852 if ( (!(hInstance
& 4)) ||
853 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
855 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
861 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
863 && (hInstance
!= 0xffff) )
865 /* calling MPI with a foreign DSEG is invalid ! */
866 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
867 hInstance
,CURRENT_DS
);
870 /* Always use the DSEG that MPI was entered with.
871 * We used to set hInstance to GetTaskDS16(), but this should be wrong
872 * as CURRENT_DS provides the DSEG value we need.
873 * ("calling" DS, *not* "task" DS !) */
874 hInstanceSelector
= CURRENT_DS
;
875 hInstance
= GlobalHandle16(hInstanceSelector
);
877 /* no thunking for DLLs */
878 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
881 thunkaddr
= TASK_AllocThunk( GetCurrentTask() );
882 if (!thunkaddr
) return (FARPROC16
)0;
883 thunk
= MapSL( thunkaddr
);
884 lfunc
= MapSL( (SEGPTR
)func
);
886 TRACE("(%08lx,%04x): got thunk %08lx\n",
887 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
888 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
889 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
891 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
894 *thunk
++ = 0xb8; /* movw instance, %ax */
895 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
896 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
897 *thunk
++ = 0xea; /* ljmp func */
898 *(DWORD
*)thunk
= (DWORD
)func
;
899 return (FARPROC16
)thunkaddr
;
900 /* CX reg indicates if thunkaddr != NULL, implement if needed */
904 /***********************************************************************
905 * FreeProcInstance16 (KERNEL.52)
907 void WINAPI
FreeProcInstance16( FARPROC16 func
)
909 TRACE("(%08lx)\n", (DWORD
)func
);
910 TASK_FreeThunk( GetCurrentTask(), (SEGPTR
)func
);
913 /**********************************************************************
914 * TASK_GetCodeSegment
916 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
917 * and logical segment number of a given code segment.
919 * 'proc' either *is* already a pair of module handle and segment number,
920 * in which case there's nothing to do. Otherwise, it is a pointer to
921 * a function, and we need to retrieve the code segment. If the pointer
922 * happens to point to a thunk, we'll retrieve info about the code segment
923 * where the function pointed to by the thunk resides, not the thunk itself.
925 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
926 * the function the snoop code will return to ...
929 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
930 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
932 NE_MODULE
*pModule
= NULL
;
933 SEGTABLEENTRY
*pSeg
= NULL
;
936 /* Try pair of module handle / segment number */
937 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
938 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
940 segNr
= LOWORD( proc
);
941 if ( segNr
&& segNr
<= pModule
->seg_count
)
942 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
945 /* Try thunk or function */
948 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
951 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
952 selector
= thunk
[6] + (thunk
[7] << 8);
954 selector
= HIWORD( proc
);
956 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
957 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
960 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
961 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
964 if ( pModule
&& segNr
> pModule
->seg_count
)
968 /* Abort if segment not found */
970 if ( !pModule
|| !pSeg
)
973 /* Return segment data */
975 if ( ppModule
) *ppModule
= pModule
;
976 if ( ppSeg
) *ppSeg
= pSeg
;
977 if ( pSegNr
) *pSegNr
= segNr
;
982 /**********************************************************************
983 * GetCodeHandle (KERNEL.93)
985 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
989 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
995 /**********************************************************************
996 * GetCodeInfo (KERNEL.104)
998 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
1001 SEGTABLEENTRY
*pSeg
;
1004 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
1007 /* Fill in segment information */
1009 segInfo
->offSegment
= pSeg
->filepos
;
1010 segInfo
->cbSegment
= pSeg
->size
;
1011 segInfo
->flags
= pSeg
->flags
;
1012 segInfo
->cbAlloc
= pSeg
->minsize
;
1013 segInfo
->h
= pSeg
->hSeg
;
1014 segInfo
->alignShift
= pModule
->alignment
;
1016 if ( segNr
== pModule
->dgroup
)
1017 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
1019 /* Return module handle in %es */
1021 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
1027 /**********************************************************************
1028 * DefineHandleTable16 (KERNEL.94)
1030 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1032 FIXME("(%04x): stub ?\n", wOffset
);
1037 /***********************************************************************
1038 * SetTaskQueue (KERNEL.34)
1040 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1045 if (!hTask
) hTask
= GetCurrentTask();
1046 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1048 hPrev
= pTask
->hQueue
;
1049 pTask
->hQueue
= hQueue
;
1055 /***********************************************************************
1056 * GetTaskQueue (KERNEL.35)
1058 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1062 if (!hTask
) hTask
= GetCurrentTask();
1063 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return 0;
1064 return pTask
->hQueue
;
1067 /***********************************************************************
1068 * SetThreadQueue (KERNEL.463)
1070 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1072 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1073 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1077 teb
->queue
= hQueue
;
1079 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1080 SetTaskQueue16( teb
->htask16
, hQueue
);
1086 /***********************************************************************
1087 * GetThreadQueue (KERNEL.464)
1089 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1093 teb
= NtCurrentTeb();
1094 else if ( HIWORD(thread
) )
1095 teb
= THREAD_IdToTEB( thread
);
1096 else if ( IsTask16( (HTASK16
)thread
) )
1097 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1099 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1102 /***********************************************************************
1103 * SetFastQueue (KERNEL.624)
1105 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1109 teb
= NtCurrentTeb();
1110 else if ( HIWORD(thread
) )
1111 teb
= THREAD_IdToTEB( thread
);
1112 else if ( IsTask16( (HTASK16
)thread
) )
1113 teb
= ((TDB
*)GlobalLock16( (HANDLE16
)thread
))->teb
;
1115 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1118 /***********************************************************************
1119 * GetFastQueue (KERNEL.625)
1121 HANDLE WINAPI
GetFastQueue16( void )
1123 TEB
*teb
= NtCurrentTeb();
1126 if (!teb
->queue
&& Callout
.InitThreadInput16
)
1127 Callout
.InitThreadInput16( 0, THREAD_IsWin16(teb
)? 4 : 5 );
1130 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1132 return (HANDLE
)teb
->queue
;
1135 /***********************************************************************
1136 * SwitchStackTo (KERNEL.108)
1138 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1141 STACK16FRAME
*oldFrame
, *newFrame
;
1142 INSTANCEDATA
*pData
;
1145 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1146 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1147 TRACE("old=%04x:%04x new=%04x:%04x\n",
1148 SELECTOROF( pTask
->teb
->cur_stack
),
1149 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1151 /* Save the old stack */
1153 oldFrame
= THREAD_STACK16( pTask
->teb
);
1154 /* pop frame + args and push bp */
1155 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1157 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1158 pData
->stacktop
= top
;
1159 pData
->stackmin
= ptr
;
1160 pData
->stackbottom
= ptr
;
1162 /* Switch to the new stack */
1164 /* Note: we need to take the 3 arguments into account; otherwise,
1165 * the stack will underflow upon return from this function.
1167 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1168 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1169 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1170 newFrame
= THREAD_STACK16( pTask
->teb
);
1172 /* Copy the stack frame and the local variables to the new stack */
1174 memmove( newFrame
, oldFrame
, copySize
);
1176 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1180 /***********************************************************************
1181 * SwitchStackBack (KERNEL.109)
1183 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1185 STACK16FRAME
*oldFrame
, *newFrame
;
1186 INSTANCEDATA
*pData
;
1188 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1190 if (!pData
->old_ss_sp
)
1192 WARN("No previous SwitchStackTo\n" );
1195 TRACE("restoring stack %04x:%04x\n",
1196 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1198 oldFrame
= CURRENT_STACK16
;
1200 /* Pop bp from the previous stack */
1202 BP_reg(context
) = *(WORD
*)MapSL(pData
->old_ss_sp
);
1203 pData
->old_ss_sp
+= sizeof(WORD
);
1205 /* Switch back to the old stack */
1207 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1208 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1209 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1210 pData
->old_ss_sp
= 0;
1212 /* Build a stack frame for the return */
1214 newFrame
= CURRENT_STACK16
;
1215 newFrame
->frame32
= oldFrame
->frame32
;
1216 newFrame
->module_cs
= oldFrame
->module_cs
;
1217 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1218 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1222 /***********************************************************************
1223 * GetTaskQueueDS16 (KERNEL.118)
1225 void WINAPI
GetTaskQueueDS16(void)
1227 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1231 /***********************************************************************
1232 * GetTaskQueueES16 (KERNEL.119)
1234 void WINAPI
GetTaskQueueES16(void)
1236 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1240 /***********************************************************************
1241 * GetCurrentTask (KERNEL.36)
1243 HTASK16 WINAPI
GetCurrentTask(void)
1245 return NtCurrentTeb()->htask16
;
1248 DWORD WINAPI
WIN16_GetCurrentTask(void)
1250 /* This is the version used by relay code; the first task is */
1251 /* returned in the high word of the result */
1252 return MAKELONG( GetCurrentTask(), hFirstTask
);
1256 /***********************************************************************
1257 * GetCurrentPDB (KERNEL.37)
1259 * UNDOC: returns PSP of KERNEL in high word
1261 DWORD WINAPI
GetCurrentPDB16(void)
1265 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1266 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1270 /***********************************************************************
1271 * GetCurPID16 (KERNEL.157)
1273 DWORD WINAPI
GetCurPID16( DWORD unused
)
1279 /***********************************************************************
1280 * GetInstanceData (KERNEL.54)
1282 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1284 char *ptr
= (char *)GlobalLock16( instance
);
1285 if (!ptr
|| !len
) return 0;
1286 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1287 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1292 /***********************************************************************
1293 * GetExeVersion (KERNEL.105)
1295 WORD WINAPI
GetExeVersion16(void)
1299 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1300 return pTask
->version
;
1304 /***********************************************************************
1305 * SetErrorMode16 (KERNEL.107)
1307 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1310 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1311 pTask
->error_mode
= mode
;
1312 return SetErrorMode( mode
);
1316 /***********************************************************************
1317 * GetNumTasks (KERNEL.152)
1319 UINT16 WINAPI
GetNumTasks16(void)
1325 /***********************************************************************
1326 * GetTaskDS (KERNEL.155)
1328 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1329 * I don't think we need to bother with this.
1331 HINSTANCE16 WINAPI
GetTaskDS16(void)
1335 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1336 return GlobalHandleToSel16(pTask
->hInstance
);
1339 /***********************************************************************
1340 * GetDummyModuleHandleDS (KERNEL.602)
1342 WORD WINAPI
GetDummyModuleHandleDS16(void)
1347 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1348 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1349 selector
= GlobalHandleToSel16( pTask
->hModule
);
1350 CURRENT_DS
= selector
;
1354 /***********************************************************************
1355 * IsTask (KERNEL.320)
1357 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1361 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return FALSE
;
1362 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1363 return (pTask
->magic
== TDB_MAGIC
);
1367 /***********************************************************************
1368 * IsWinOldApTask16 (KERNEL.158)
1370 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1372 /* should return bit 0 of byte 0x48 in PSP */
1376 /***********************************************************************
1377 * SetTaskSignalProc (KERNEL.38)
1379 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1384 if (!hTask
) hTask
= GetCurrentTask();
1385 if (!(pTask
= (TDB
*)GlobalLock16( hTask
))) return NULL
;
1386 oldProc
= pTask
->userhandler
;
1387 pTask
->userhandler
= proc
;
1391 /***********************************************************************
1392 * TASK_CallTaskSignalProc
1394 /* ### start build ### */
1395 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1396 /* ### stop build ### */
1397 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1399 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1400 if ( !pTask
|| !pTask
->userhandler
) return;
1402 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1403 hTaskOrModule
, uCode
, 0,
1404 pTask
->hInstance
, pTask
->hQueue
);
1407 /***********************************************************************
1408 * SetSigHandler (KERNEL.140)
1410 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1411 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1413 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1414 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1416 if (flag
!= 1) return 0;
1417 if (!newmode
) newhandler
= NULL
; /* Default handler */
1422 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return 0;
1423 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1424 pTask
->signal_flags
= newmode
;
1425 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1426 pTask
->sighandler
= newhandler
;
1432 /***********************************************************************
1433 * GlobalNotify (KERNEL.154)
1435 * Note that GlobalNotify does _not_ return the old NotifyProc
1436 * -- contrary to LocalNotify !!
1438 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1442 if (!(pTask
= (TDB
*)GlobalLock16( GetCurrentTask() ))) return;
1443 pTask
->discardhandler
= proc
;
1447 /***********************************************************************
1448 * GetExePtr (KERNEL.133)
1450 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1455 /* Check for module handle */
1457 if (!(ptr
= GlobalLock16( handle
))) return 0;
1458 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1460 /* Search for this handle inside all tasks */
1462 *hTask
= hFirstTask
;
1465 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1466 if ((*hTask
== handle
) ||
1467 (pTask
->hInstance
== handle
) ||
1468 (pTask
->hQueue
== handle
) ||
1469 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1470 *hTask
= pTask
->hNext
;
1473 /* Check the owner for module handle */
1475 owner
= FarGetOwner16( handle
);
1476 if (!(ptr
= GlobalLock16( owner
))) return 0;
1477 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1479 /* Search for the owner inside all tasks */
1481 *hTask
= hFirstTask
;
1484 TDB
*pTask
= (TDB
*)GlobalLock16( *hTask
);
1485 if ((*hTask
== owner
) ||
1486 (pTask
->hInstance
== owner
) ||
1487 (pTask
->hQueue
== owner
) ||
1488 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1489 *hTask
= pTask
->hNext
;
1495 /**********************************************************************/
1497 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1500 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1501 STACK16FRAME
*frame
= CURRENT_STACK16
;
1502 frame
->ecx
= hModule
;
1503 if (hTask
) frame
->es
= hTask
;
1507 /**********************************************************************/
1509 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1512 return GetExePtrHelper( handle
, &hTask
);
1516 /***********************************************************************
1517 * TaskFirst (TOOLHELP.63)
1519 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1521 lpte
->hNext
= hFirstTask
;
1522 return TaskNext16( lpte
);
1526 /***********************************************************************
1527 * TaskNext (TOOLHELP.64)
1529 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1532 INSTANCEDATA
*pInstData
;
1534 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1535 if (!lpte
->hNext
) return FALSE
;
1537 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1539 pTask
= (TDB
*)GlobalLock16( lpte
->hNext
);
1540 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1541 if (pTask
->hInstance
)
1543 lpte
->hNext
= pTask
->hNext
;
1545 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1546 lpte
->hTask
= lpte
->hNext
;
1547 lpte
->hTaskParent
= pTask
->hParent
;
1548 lpte
->hInst
= pTask
->hInstance
;
1549 lpte
->hModule
= pTask
->hModule
;
1550 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1551 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1552 lpte
->wStackTop
= pInstData
->stacktop
;
1553 lpte
->wStackMinimum
= pInstData
->stackmin
;
1554 lpte
->wStackBottom
= pInstData
->stackbottom
;
1555 lpte
->wcEvents
= pTask
->nEvents
;
1556 lpte
->hQueue
= pTask
->hQueue
;
1557 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1558 lpte
->wPSPOffset
= 0x100; /*??*/
1559 lpte
->hNext
= pTask
->hNext
;
1564 /***********************************************************************
1565 * TaskFindHandle (TOOLHELP.65)
1567 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1569 lpte
->hNext
= hTask
;
1570 return TaskNext16( lpte
);
1574 /**************************************************************************
1575 * FatalAppExit16 (KERNEL.137)
1577 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1579 TDB
*pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
1581 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1583 if (Callout
.MessageBoxA
)
1584 Callout
.MessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1586 ERR( "%s\n", debugstr_a(str
) );
1592 /***********************************************************************
1593 * TerminateApp16 (TOOLHELP.77)
1595 * See "Undocumented Windows".
1597 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1599 if (hTask
&& hTask
!= GetCurrentTask())
1601 FIXME("cannot terminate task %x\n", hTask
);
1605 if (wFlags
& NO_UAE_BOX
)
1608 old_mode
= SetErrorMode16(0);
1609 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1611 FatalAppExit16( 0, NULL
);
1613 /* hmm, we're still alive ?? */
1615 /* check undocumented flag */
1616 if (!(wFlags
& 0x8000))
1617 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1619 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1620 but let's just call ExitThread */
1625 /***********************************************************************
1626 * GetAppCompatFlags16 (KERNEL.354)
1628 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1630 return GetAppCompatFlags( hTask
);
1634 /***********************************************************************
1635 * GetAppCompatFlags (USER32.206)
1637 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1641 if (!hTask
) hTask
= GetCurrentTask();
1642 if (!(pTask
=(TDB
*)GlobalLock16( (HTASK16
)hTask
))) return 0;
1643 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1644 return pTask
->compat_flags
;