4 * Copyright 1995 Alexandre Julliard
20 #include "wine/winbase16.h"
28 #include "selectors.h"
30 #include "wine/server.h"
32 #include "stackframe.h"
37 #include "debugtools.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 */
47 static THHOOK DefaultThhook
;
48 THHOOK
*pThhook
= &DefaultThhook
;
50 #define hCurrentTask (pThhook->CurTDB)
51 #define hFirstTask (pThhook->HeadTDB)
52 #define hLockedTask (pThhook->LockTDB)
54 static UINT16 nTaskCount
= 0;
56 static HTASK16 initial_task
;
58 /***********************************************************************
61 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
63 THHOOK
*pOldThhook
= pThhook
;
65 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
67 *pThhook
= *pOldThhook
;
70 /***********************************************************************
73 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
75 TDB
* pTask
= TASK_GetPtr( hTask
);
77 if (pTask
->hNext
) return pTask
->hNext
;
78 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
82 /***********************************************************************
85 TDB
*TASK_GetPtr( HTASK16 hTask
)
87 return GlobalLock16( hTask
);
91 /***********************************************************************
94 TDB
*TASK_GetCurrent(void)
96 return TASK_GetPtr( GetCurrentTask() );
100 /***********************************************************************
103 static void TASK_LinkTask( HTASK16 hTask
)
108 if (!(pTask
= TASK_GetPtr( hTask
))) return;
109 prevTask
= &hFirstTask
;
112 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
113 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
114 prevTask
= &prevTaskPtr
->hNext
;
116 pTask
->hNext
= *prevTask
;
122 /***********************************************************************
125 static void TASK_UnlinkTask( HTASK16 hTask
)
130 prevTask
= &hFirstTask
;
131 while (*prevTask
&& (*prevTask
!= hTask
))
133 pTask
= TASK_GetPtr( *prevTask
);
134 prevTask
= &pTask
->hNext
;
138 pTask
= TASK_GetPtr( *prevTask
);
139 *prevTask
= pTask
->hNext
;
146 /***********************************************************************
149 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
150 * and containing 'count' entries.
152 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
158 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
160 pThunk
->magic
= THUNK_MAGIC
;
161 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
163 for (i
= 0; i
< count
-1; i
++)
165 free
+= 8; /* Offset of next thunk */
166 pThunk
->thunks
[4*i
] = free
;
168 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
172 /***********************************************************************
175 * Allocate a thunk for MakeProcInstance().
177 static SEGPTR
TASK_AllocThunk(void)
183 if (!(pTask
= TASK_GetCurrent())) return 0;
184 sel
= pTask
->hCSAlias
;
185 pThunk
= &pTask
->thunks
;
186 base
= (int)pThunk
- (int)pTask
;
187 while (!pThunk
->free
)
190 if (!sel
) /* Allocate a new segment */
192 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
193 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
194 if (!sel
) return (SEGPTR
)0;
195 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
198 pThunk
= (THUNKS
*)GlobalLock16( sel
);
201 base
+= pThunk
->free
;
202 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
203 return MAKESEGPTR( sel
, base
);
207 /***********************************************************************
210 * Free a MakeProcInstance() thunk.
212 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
218 if (!(pTask
= TASK_GetCurrent())) return 0;
219 sel
= pTask
->hCSAlias
;
220 pThunk
= &pTask
->thunks
;
221 base
= (int)pThunk
- (int)pTask
;
222 while (sel
&& (sel
!= HIWORD(thunk
)))
225 pThunk
= (THUNKS
*)GlobalLock16( sel
);
228 if (!sel
) return FALSE
;
229 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
230 pThunk
->free
= LOWORD(thunk
) - base
;
235 /***********************************************************************
238 * NOTE: This routine might be called by a Win32 thread. Thus, we need
239 * to be careful to protect global data structures. We do this
240 * by entering the Win16Lock while linking the task into the
243 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
249 /* Allocate the task structure */
251 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
252 if (!hTask
) return NULL
;
253 pTask
= TASK_GetPtr( hTask
);
254 FarSetOwner16( hTask
, pModule
->self
);
256 /* Fill the task structure */
258 pTask
->hSelf
= hTask
;
260 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
262 pTask
->flags
|= TDBF_WIN32
;
263 pTask
->hInstance
= pModule
->self
;
264 pTask
->hPrevInstance
= 0;
265 /* NOTE: for 16-bit tasks, the instance handles are updated later on
269 pTask
->version
= pModule
->expected_version
;
270 pTask
->hModule
= pModule
->self
;
271 pTask
->hParent
= GetCurrentTask();
272 pTask
->magic
= TDB_MAGIC
;
273 pTask
->nCmdShow
= cmdShow
;
275 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
276 strcpy( pTask
->curdir
, "\\" );
277 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
278 sizeof(pTask
->curdir
) - 1 );
280 /* Create the thunks block */
282 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
284 /* Copy the module name */
286 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
287 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
289 /* Allocate a selector for the PDB */
291 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
292 pModule
->self
, WINE_LDT_FLAGS_DATA
);
296 pTask
->pdb
.int20
= 0x20cd;
297 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
298 PUT_UA_DWORD(&pTask
->pdb
.dispatcher
[1],
299 (DWORD
)GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" ));
300 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
301 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
302 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
303 pTask
->pdb
.fileHandlesPtr
=
304 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
305 pTask
->pdb
.hFileHandles
= 0;
306 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
307 /* FIXME: should we make a copy of the environment? */
308 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
309 pTask
->pdb
.nbFiles
= 20;
311 /* Fill the command line */
315 cmdline
= GetCommandLineA();
316 /* remove the first word (program name) */
318 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
319 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
320 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
321 len
= strlen(cmdline
);
323 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
324 pTask
->pdb
.cmdLine
[0] = len
;
325 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
326 /* pTask->pdb.cmdLine[len+1] = 0; */
328 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
330 /* Get the compatibility flags */
332 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
334 /* Allocate a code segment alias for the TDB */
336 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
337 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
339 /* Default DTA overwrites command line */
341 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
343 /* Create scheduler event for 16-bit tasks */
345 if ( !(pTask
->flags
& TDBF_WIN32
) )
346 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
348 /* Enter task handle into thread */
350 if (teb
) teb
->htask16
= hTask
;
351 if (!initial_task
) initial_task
= hTask
;
357 /***********************************************************************
360 static void TASK_DeleteTask( HTASK16 hTask
)
365 if (!(pTask
= TASK_GetPtr( hTask
))) return;
368 pTask
->magic
= 0xdead; /* invalidate signature */
370 /* Free the selector aliases */
372 GLOBAL_FreeBlock( pTask
->hCSAlias
);
373 GLOBAL_FreeBlock( pTask
->hPDB
);
375 /* Free the task module */
377 FreeModule16( pTask
->hModule
);
379 /* Free the task structure itself */
381 GlobalFree16( hTask
);
383 /* Free all memory used by this task (including the 32-bit stack, */
384 /* the environment block and the thunk segments). */
386 GlobalFreeAll16( hPDB
);
390 /***********************************************************************
391 * TASK_CreateMainTask
393 * Create a task for the main (32-bit) process.
395 void TASK_CreateMainTask(void)
398 STARTUPINFOA startup_info
;
399 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
401 GetStartupInfoA( &startup_info
);
402 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
403 pTask
= TASK_Create( (NE_MODULE
*)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
404 cmdShow
, NtCurrentTeb(), NULL
, 0 );
407 ERR("could not create task for main process\n");
411 /* Add the task to the linked list */
412 /* (no need to get the win16 lock, we are the only thread at this point) */
413 TASK_LinkTask( pTask
->hSelf
);
417 /* startup routine for a new 16-bit thread */
418 static DWORD CALLBACK
task_start( TDB
*pTask
)
422 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
423 NtCurrentTeb()->htask16
= pTask
->hSelf
;
426 TASK_LinkTask( pTask
->hSelf
);
427 pTask
->teb
= NtCurrentTeb();
428 ret
= NE_StartTask();
434 /***********************************************************************
437 * Spawn a new 16-bit task.
439 HTASK16
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
,
440 LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
444 if (!(pTask
= TASK_Create( pModule
, cmdShow
, NULL
, cmdline
, len
))) return 0;
445 if (!(*hThread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)task_start
, pTask
, 0, NULL
)))
447 TASK_DeleteTask( pTask
->hSelf
);
454 /***********************************************************************
457 void TASK_ExitTask(void)
462 /* Enter the Win16Lock to protect global data structures */
465 pTask
= TASK_GetCurrent();
472 TRACE("Killing task %04x\n", pTask
->hSelf
);
474 /* Perform USER cleanup */
476 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
477 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
478 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
479 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
481 /* Remove the task from the list to be sure we never switch back to it */
482 TASK_UnlinkTask( pTask
->hSelf
);
484 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
486 TRACE("this is the last task, exiting\n" );
492 TDB
* p
= TASK_GetPtr( hFirstTask
);
495 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
496 p
= TASK_GetPtr( p
->hNext
);
502 if ( hLockedTask
== pTask
->hSelf
)
505 TASK_DeleteTask( pTask
->hSelf
);
507 /* ... and completely release the Win16Lock, just in case. */
508 ReleaseThunkLock( &lockCount
);
512 /***********************************************************************
513 * InitTask (KERNEL.91)
515 * Called by the application startup code.
517 void WINAPI
InitTask16( CONTEXT86
*context
)
520 INSTANCEDATA
*pinstance
;
524 if (!(pTask
= TASK_GetCurrent())) return;
526 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
527 as some apps, notably Visual Basic apps, *modify* the heap/stack
528 size of the instance data segment before calling InitTask() */
530 /* Initialize the INSTANCEDATA structure */
531 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
532 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
533 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
534 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
535 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
537 /* Initialize the local heap */
538 if (LOWORD(context
->Ecx
))
539 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
541 /* Initialize implicitly loaded DLLs */
542 NE_InitializeDLLs( pTask
->hModule
);
543 NE_DllProcessAttach( pTask
->hModule
);
545 /* Registers on return are:
546 * ax 1 if OK, 0 on error
547 * cx stack limit in bytes
548 * dx cmdShow parameter
549 * si instance handle of the previous instance
550 * di instance handle of the new task
551 * es:bx pointer to command line inside PSP
553 * 0 (=%bp) is pushed on the stack
555 ptr
= stack16_push( sizeof(WORD
) );
556 *(WORD
*)MapSL(ptr
) = 0;
561 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
564 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
565 while ((*p
== ' ') || (*p
== '\t')) p
++;
566 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
568 context
->Ecx
= pinstance
->stacktop
;
569 context
->Edx
= pTask
->nCmdShow
;
570 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
571 context
->Edi
= (DWORD
)pTask
->hInstance
;
572 context
->SegEs
= (WORD
)pTask
->hPDB
;
576 /***********************************************************************
577 * WaitEvent (KERNEL.30)
579 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
583 if (!hTask
) hTask
= GetCurrentTask();
584 pTask
= TASK_GetPtr( hTask
);
586 if (pTask
->flags
& TDBF_WIN32
)
588 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
592 if (pTask
->nEvents
> 0)
598 if (pTask
->teb
== NtCurrentTeb())
602 NtResetEvent( pTask
->hEvent
, NULL
);
603 ReleaseThunkLock( &lockCount
);
604 SYSLEVEL_CheckNotLevel( 1 );
605 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
606 RestoreThunkLock( lockCount
);
607 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
609 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
615 /***********************************************************************
616 * PostEvent (KERNEL.31)
618 void WINAPI
PostEvent16( HTASK16 hTask
)
622 if (!hTask
) hTask
= GetCurrentTask();
623 if (!(pTask
= TASK_GetPtr( hTask
))) return;
625 if (pTask
->flags
& TDBF_WIN32
)
627 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
633 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
637 /***********************************************************************
638 * SetPriority (KERNEL.32)
640 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
645 if (!hTask
) hTask
= GetCurrentTask();
646 if (!(pTask
= TASK_GetPtr( hTask
))) return;
647 newpriority
= pTask
->priority
+ delta
;
648 if (newpriority
< -32) newpriority
= -32;
649 else if (newpriority
> 15) newpriority
= 15;
651 pTask
->priority
= newpriority
+ 1;
652 TASK_UnlinkTask( pTask
->hSelf
);
653 TASK_LinkTask( pTask
->hSelf
);
658 /***********************************************************************
659 * LockCurrentTask (KERNEL.33)
661 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
663 if (bLock
) hLockedTask
= GetCurrentTask();
664 else hLockedTask
= 0;
669 /***********************************************************************
670 * IsTaskLocked (KERNEL.122)
672 HTASK16 WINAPI
IsTaskLocked16(void)
678 /***********************************************************************
679 * OldYield (KERNEL.117)
681 void WINAPI
OldYield16(void)
685 ReleaseThunkLock(&count
);
686 RestoreThunkLock(count
);
689 /***********************************************************************
690 * WIN32_OldYield (KERNEL.447)
692 void WINAPI
WIN32_OldYield16(void)
696 ReleaseThunkLock(&count
);
697 RestoreThunkLock(count
);
700 /***********************************************************************
701 * DirectedYield (KERNEL.150)
703 void WINAPI
DirectedYield16( HTASK16 hTask
)
705 TDB
*pCurTask
= TASK_GetCurrent();
707 if (pCurTask
->flags
& TDBF_WIN32
)
709 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
713 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
715 pCurTask
->hYieldTo
= hTask
;
718 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
721 /***********************************************************************
724 void WINAPI
Yield16(void)
726 TDB
*pCurTask
= TASK_GetCurrent();
728 if (pCurTask
) pCurTask
->hYieldTo
= 0;
729 if (pCurTask
&& pCurTask
->hQueue
)
731 HMODULE mod
= GetModuleHandleA( "user32.dll" );
734 FARPROC proc
= GetProcAddress( mod
, "UserYield16" );
745 /***********************************************************************
746 * KERNEL_490 (KERNEL.490)
748 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
750 if ( !someTask
) return 0;
752 FIXME("(%04x): stub\n", someTask
);
756 /***********************************************************************
757 * MakeProcInstance (KERNEL.51)
759 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
763 WORD hInstanceSelector
;
765 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
767 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
770 /* Win95 actually protects via SEH, but this is better for debugging */
771 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
777 if ( (!(hInstance
& 4)) ||
778 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
780 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
786 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
788 && (hInstance
!= 0xffff) )
790 /* calling MPI with a foreign DSEG is invalid ! */
791 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
792 hInstance
,CURRENT_DS
);
795 /* Always use the DSEG that MPI was entered with.
796 * We used to set hInstance to GetTaskDS16(), but this should be wrong
797 * as CURRENT_DS provides the DSEG value we need.
798 * ("calling" DS, *not* "task" DS !) */
799 hInstanceSelector
= CURRENT_DS
;
800 hInstance
= GlobalHandle16(hInstanceSelector
);
802 /* no thunking for DLLs */
803 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
806 thunkaddr
= TASK_AllocThunk();
807 if (!thunkaddr
) return (FARPROC16
)0;
808 thunk
= MapSL( thunkaddr
);
809 lfunc
= MapSL( (SEGPTR
)func
);
811 TRACE("(%08lx,%04x): got thunk %08lx\n",
812 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
813 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
814 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
816 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
819 *thunk
++ = 0xb8; /* movw instance, %ax */
820 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
821 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
822 *thunk
++ = 0xea; /* ljmp func */
823 *(DWORD
*)thunk
= (DWORD
)func
;
824 return (FARPROC16
)thunkaddr
;
825 /* CX reg indicates if thunkaddr != NULL, implement if needed */
829 /***********************************************************************
830 * FreeProcInstance (KERNEL.52)
832 void WINAPI
FreeProcInstance16( FARPROC16 func
)
834 TRACE("(%08lx)\n", (DWORD
)func
);
835 TASK_FreeThunk( (SEGPTR
)func
);
838 /**********************************************************************
839 * TASK_GetCodeSegment
841 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
842 * and logical segment number of a given code segment.
844 * 'proc' either *is* already a pair of module handle and segment number,
845 * in which case there's nothing to do. Otherwise, it is a pointer to
846 * a function, and we need to retrieve the code segment. If the pointer
847 * happens to point to a thunk, we'll retrieve info about the code segment
848 * where the function pointed to by the thunk resides, not the thunk itself.
850 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
851 * the function the snoop code will return to ...
854 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
855 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
857 NE_MODULE
*pModule
= NULL
;
858 SEGTABLEENTRY
*pSeg
= NULL
;
861 /* Try pair of module handle / segment number */
862 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
863 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
865 segNr
= LOWORD( proc
);
866 if ( segNr
&& segNr
<= pModule
->seg_count
)
867 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
870 /* Try thunk or function */
873 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
876 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
877 selector
= thunk
[6] + (thunk
[7] << 8);
879 selector
= HIWORD( proc
);
881 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
882 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
885 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
886 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
889 if ( pModule
&& segNr
> pModule
->seg_count
)
893 /* Abort if segment not found */
895 if ( !pModule
|| !pSeg
)
898 /* Return segment data */
900 if ( ppModule
) *ppModule
= pModule
;
901 if ( ppSeg
) *ppSeg
= pSeg
;
902 if ( pSegNr
) *pSegNr
= segNr
;
907 /**********************************************************************
908 * GetCodeHandle (KERNEL.93)
910 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
914 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
920 /**********************************************************************
921 * GetCodeInfo (KERNEL.104)
923 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
929 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
932 /* Fill in segment information */
934 segInfo
->offSegment
= pSeg
->filepos
;
935 segInfo
->cbSegment
= pSeg
->size
;
936 segInfo
->flags
= pSeg
->flags
;
937 segInfo
->cbAlloc
= pSeg
->minsize
;
938 segInfo
->h
= pSeg
->hSeg
;
939 segInfo
->alignShift
= pModule
->alignment
;
941 if ( segNr
== pModule
->dgroup
)
942 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
944 /* Return module handle in %es */
946 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
952 /**********************************************************************
953 * DefineHandleTable (KERNEL.94)
955 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
957 FIXME("(%04x): stub ?\n", wOffset
);
962 /***********************************************************************
963 * SetTaskQueue (KERNEL.34)
965 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
970 if (!hTask
) hTask
= GetCurrentTask();
971 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
973 hPrev
= pTask
->hQueue
;
974 pTask
->hQueue
= hQueue
;
980 /***********************************************************************
981 * GetTaskQueue (KERNEL.35)
983 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
987 if (!hTask
) hTask
= GetCurrentTask();
988 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
989 return pTask
->hQueue
;
992 /***********************************************************************
993 * SetThreadQueue (KERNEL.463)
995 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
997 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
998 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1002 teb
->queue
= hQueue
;
1004 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1005 SetTaskQueue16( teb
->htask16
, hQueue
);
1011 /***********************************************************************
1012 * GetThreadQueue (KERNEL.464)
1014 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1018 teb
= NtCurrentTeb();
1019 else if ( HIWORD(thread
) )
1020 teb
= THREAD_IdToTEB( thread
);
1021 else if ( IsTask16( (HTASK16
)thread
) )
1022 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1024 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1027 /***********************************************************************
1028 * SetFastQueue (KERNEL.624)
1030 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1034 teb
= NtCurrentTeb();
1035 else if ( HIWORD(thread
) )
1036 teb
= THREAD_IdToTEB( thread
);
1037 else if ( IsTask16( (HTASK16
)thread
) )
1038 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1040 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1043 /***********************************************************************
1044 * GetFastQueue (KERNEL.625)
1046 HANDLE WINAPI
GetFastQueue16( void )
1048 HANDLE ret
= (HANDLE
)NtCurrentTeb()->queue
;
1050 if (!ret
) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1054 /***********************************************************************
1055 * SwitchStackTo (KERNEL.108)
1057 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1060 STACK16FRAME
*oldFrame
, *newFrame
;
1061 INSTANCEDATA
*pData
;
1064 if (!(pTask
= TASK_GetCurrent())) return;
1065 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1066 TRACE("old=%04x:%04x new=%04x:%04x\n",
1067 SELECTOROF( pTask
->teb
->cur_stack
),
1068 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1070 /* Save the old stack */
1072 oldFrame
= THREAD_STACK16( pTask
->teb
);
1073 /* pop frame + args and push bp */
1074 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1076 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1077 pData
->stacktop
= top
;
1078 pData
->stackmin
= ptr
;
1079 pData
->stackbottom
= ptr
;
1081 /* Switch to the new stack */
1083 /* Note: we need to take the 3 arguments into account; otherwise,
1084 * the stack will underflow upon return from this function.
1086 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1087 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1088 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1089 newFrame
= THREAD_STACK16( pTask
->teb
);
1091 /* Copy the stack frame and the local variables to the new stack */
1093 memmove( newFrame
, oldFrame
, copySize
);
1095 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1099 /***********************************************************************
1100 * SwitchStackBack (KERNEL.109)
1102 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1104 STACK16FRAME
*oldFrame
, *newFrame
;
1105 INSTANCEDATA
*pData
;
1107 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1109 if (!pData
->old_ss_sp
)
1111 WARN("No previous SwitchStackTo\n" );
1114 TRACE("restoring stack %04x:%04x\n",
1115 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1117 oldFrame
= CURRENT_STACK16
;
1119 /* Pop bp from the previous stack */
1121 BP_reg(context
) = *(WORD
*)MapSL(pData
->old_ss_sp
);
1122 pData
->old_ss_sp
+= sizeof(WORD
);
1124 /* Switch back to the old stack */
1126 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1127 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1128 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1129 pData
->old_ss_sp
= 0;
1131 /* Build a stack frame for the return */
1133 newFrame
= CURRENT_STACK16
;
1134 newFrame
->frame32
= oldFrame
->frame32
;
1135 newFrame
->module_cs
= oldFrame
->module_cs
;
1136 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1137 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1141 /***********************************************************************
1142 * GetTaskQueueDS (KERNEL.118)
1144 void WINAPI
GetTaskQueueDS16(void)
1146 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1150 /***********************************************************************
1151 * GetTaskQueueES (KERNEL.119)
1153 void WINAPI
GetTaskQueueES16(void)
1155 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1159 /***********************************************************************
1160 * GetCurrentTask (KERNEL32.@)
1162 HTASK16 WINAPI
GetCurrentTask(void)
1164 return NtCurrentTeb()->htask16
;
1167 /***********************************************************************
1168 * GetCurrentTask (KERNEL.36)
1170 DWORD WINAPI
WIN16_GetCurrentTask(void)
1172 /* This is the version used by relay code; the first task is */
1173 /* returned in the high word of the result */
1174 return MAKELONG( GetCurrentTask(), hFirstTask
);
1178 /***********************************************************************
1179 * GetCurrentPDB (KERNEL.37)
1181 * UNDOC: returns PSP of KERNEL in high word
1183 DWORD WINAPI
GetCurrentPDB16(void)
1187 if (!(pTask
= TASK_GetCurrent())) return 0;
1188 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1192 /***********************************************************************
1193 * GetCurPID (KERNEL.157)
1195 DWORD WINAPI
GetCurPID16( DWORD unused
)
1201 /***********************************************************************
1202 * GetInstanceData (KERNEL.54)
1204 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1206 char *ptr
= (char *)GlobalLock16( instance
);
1207 if (!ptr
|| !len
) return 0;
1208 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1209 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1214 /***********************************************************************
1215 * GetExeVersion (KERNEL.105)
1217 WORD WINAPI
GetExeVersion16(void)
1221 if (!(pTask
= TASK_GetCurrent())) return 0;
1222 return pTask
->version
;
1226 /***********************************************************************
1227 * SetErrorMode (KERNEL.107)
1229 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1232 if (!(pTask
= TASK_GetCurrent())) return 0;
1233 pTask
->error_mode
= mode
;
1234 return SetErrorMode( mode
);
1238 /***********************************************************************
1239 * GetNumTasks (KERNEL.152)
1241 UINT16 WINAPI
GetNumTasks16(void)
1247 /***********************************************************************
1248 * GetTaskDS (KERNEL.155)
1250 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1251 * I don't think we need to bother with this.
1253 HINSTANCE16 WINAPI
GetTaskDS16(void)
1257 if (!(pTask
= TASK_GetCurrent())) return 0;
1258 return GlobalHandleToSel16(pTask
->hInstance
);
1261 /***********************************************************************
1262 * GetDummyModuleHandleDS (KERNEL.602)
1264 WORD WINAPI
GetDummyModuleHandleDS16(void)
1269 if (!(pTask
= TASK_GetCurrent())) return 0;
1270 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1271 selector
= GlobalHandleToSel16( pTask
->hModule
);
1272 CURRENT_DS
= selector
;
1276 /***********************************************************************
1277 * IsTask (KERNEL.320)
1278 * IsTask16 (KERNEL32.@)
1280 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1284 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1285 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1286 return (pTask
->magic
== TDB_MAGIC
);
1290 /***********************************************************************
1291 * IsWinOldApTask (KERNEL.158)
1293 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1295 /* should return bit 0 of byte 0x48 in PSP */
1299 /***********************************************************************
1300 * SetTaskSignalProc (KERNEL.38)
1302 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1307 if (!hTask
) hTask
= GetCurrentTask();
1308 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1309 oldProc
= pTask
->userhandler
;
1310 pTask
->userhandler
= proc
;
1314 /***********************************************************************
1315 * TASK_CallTaskSignalProc
1317 /* ### start build ### */
1318 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1319 /* ### stop build ### */
1320 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1322 TDB
*pTask
= TASK_GetCurrent();
1323 if ( !pTask
|| !pTask
->userhandler
) return;
1325 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1326 hTaskOrModule
, uCode
, 0,
1327 pTask
->hInstance
, pTask
->hQueue
);
1330 /***********************************************************************
1331 * SetSigHandler (KERNEL.140)
1333 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1334 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1336 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1337 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1339 if (flag
!= 1) return 0;
1340 if (!newmode
) newhandler
= NULL
; /* Default handler */
1345 if (!(pTask
= TASK_GetCurrent())) return 0;
1346 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1347 pTask
->signal_flags
= newmode
;
1348 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1349 pTask
->sighandler
= newhandler
;
1355 /***********************************************************************
1356 * GlobalNotify (KERNEL.154)
1358 * Note that GlobalNotify does _not_ return the old NotifyProc
1359 * -- contrary to LocalNotify !!
1361 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1365 if (!(pTask
= TASK_GetCurrent())) return;
1366 pTask
->discardhandler
= proc
;
1370 /***********************************************************************
1373 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1378 /* Check for module handle */
1380 if (!(ptr
= GlobalLock16( handle
))) return 0;
1381 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1383 /* Search for this handle inside all tasks */
1385 *hTask
= hFirstTask
;
1388 TDB
*pTask
= TASK_GetPtr( *hTask
);
1389 if ((*hTask
== handle
) ||
1390 (pTask
->hInstance
== handle
) ||
1391 (pTask
->hQueue
== handle
) ||
1392 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1393 *hTask
= pTask
->hNext
;
1396 /* Check the owner for module handle */
1398 owner
= FarGetOwner16( handle
);
1399 if (!(ptr
= GlobalLock16( owner
))) return 0;
1400 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1402 /* Search for the owner inside all tasks */
1404 *hTask
= hFirstTask
;
1407 TDB
*pTask
= TASK_GetPtr( *hTask
);
1408 if ((*hTask
== owner
) ||
1409 (pTask
->hInstance
== owner
) ||
1410 (pTask
->hQueue
== owner
) ||
1411 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1412 *hTask
= pTask
->hNext
;
1418 /***********************************************************************
1419 * GetExePtr (KERNEL.133)
1421 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1424 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1425 STACK16FRAME
*frame
= CURRENT_STACK16
;
1426 frame
->ecx
= hModule
;
1427 if (hTask
) frame
->es
= hTask
;
1432 /***********************************************************************
1435 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1438 return GetExePtrHelper( handle
, &hTask
);
1442 /***********************************************************************
1443 * TaskFirst (TOOLHELP.63)
1445 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1447 lpte
->hNext
= hFirstTask
;
1448 return TaskNext16( lpte
);
1452 /***********************************************************************
1453 * TaskNext (TOOLHELP.64)
1455 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1458 INSTANCEDATA
*pInstData
;
1460 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1461 if (!lpte
->hNext
) return FALSE
;
1463 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1465 pTask
= TASK_GetPtr( lpte
->hNext
);
1466 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1467 if (pTask
->hInstance
)
1469 lpte
->hNext
= pTask
->hNext
;
1471 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1472 lpte
->hTask
= lpte
->hNext
;
1473 lpte
->hTaskParent
= pTask
->hParent
;
1474 lpte
->hInst
= pTask
->hInstance
;
1475 lpte
->hModule
= pTask
->hModule
;
1476 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1477 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1478 lpte
->wStackTop
= pInstData
->stacktop
;
1479 lpte
->wStackMinimum
= pInstData
->stackmin
;
1480 lpte
->wStackBottom
= pInstData
->stackbottom
;
1481 lpte
->wcEvents
= pTask
->nEvents
;
1482 lpte
->hQueue
= pTask
->hQueue
;
1483 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1484 lpte
->wPSPOffset
= 0x100; /*??*/
1485 lpte
->hNext
= pTask
->hNext
;
1490 /***********************************************************************
1491 * TaskFindHandle (TOOLHELP.65)
1493 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1495 lpte
->hNext
= hTask
;
1496 return TaskNext16( lpte
);
1500 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1502 /**************************************************************************
1503 * FatalAppExit (KERNEL.137)
1505 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1507 TDB
*pTask
= TASK_GetCurrent();
1509 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1511 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1514 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1517 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1521 ERR( "%s\n", debugstr_a(str
) );
1528 /***********************************************************************
1529 * TerminateApp (TOOLHELP.77)
1531 * See "Undocumented Windows".
1533 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1535 if (hTask
&& hTask
!= GetCurrentTask())
1537 FIXME("cannot terminate task %x\n", hTask
);
1541 if (wFlags
& NO_UAE_BOX
)
1544 old_mode
= SetErrorMode16(0);
1545 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1547 FatalAppExit16( 0, NULL
);
1549 /* hmm, we're still alive ?? */
1551 /* check undocumented flag */
1552 if (!(wFlags
& 0x8000))
1553 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1555 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1556 but let's just call ExitThread */
1561 /***********************************************************************
1562 * GetAppCompatFlags (KERNEL.354)
1564 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1566 return GetAppCompatFlags( hTask
);
1570 /***********************************************************************
1571 * GetAppCompatFlags (USER32.@)
1573 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1577 if (!hTask
) hTask
= GetCurrentTask();
1578 if (!(pTask
=TASK_GetPtr( (HTASK16
)hTask
))) return 0;
1579 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1580 return pTask
->compat_flags
;