4 * Copyright 1995 Alexandre Julliard
21 #include "wine/winbase16.h"
29 #include "selectors.h"
31 #include "wine/server.h"
33 #include "stackframe.h"
38 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(task
);
41 DECLARE_DEBUG_CHANNEL(relay
);
42 DECLARE_DEBUG_CHANNEL(toolhelp
);
44 /* Min. number of thunks allocated when creating a new segment */
48 static THHOOK DefaultThhook
;
49 THHOOK
*pThhook
= &DefaultThhook
;
51 #define hCurrentTask (pThhook->CurTDB)
52 #define hFirstTask (pThhook->HeadTDB)
53 #define hLockedTask (pThhook->LockTDB)
55 static UINT16 nTaskCount
= 0;
57 static HTASK16 initial_task
;
59 /***********************************************************************
62 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
64 THHOOK
*pOldThhook
= pThhook
;
66 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
68 *pThhook
= *pOldThhook
;
71 /***********************************************************************
74 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
76 TDB
* pTask
= TASK_GetPtr( hTask
);
78 if (pTask
->hNext
) return pTask
->hNext
;
79 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
83 /***********************************************************************
86 TDB
*TASK_GetPtr( HTASK16 hTask
)
88 return GlobalLock16( hTask
);
92 /***********************************************************************
95 TDB
*TASK_GetCurrent(void)
97 return TASK_GetPtr( GetCurrentTask() );
101 /***********************************************************************
104 static void TASK_LinkTask( HTASK16 hTask
)
109 if (!(pTask
= TASK_GetPtr( hTask
))) return;
110 prevTask
= &hFirstTask
;
113 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
114 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
115 prevTask
= &prevTaskPtr
->hNext
;
117 pTask
->hNext
= *prevTask
;
123 /***********************************************************************
126 static void TASK_UnlinkTask( HTASK16 hTask
)
131 prevTask
= &hFirstTask
;
132 while (*prevTask
&& (*prevTask
!= hTask
))
134 pTask
= TASK_GetPtr( *prevTask
);
135 prevTask
= &pTask
->hNext
;
139 pTask
= TASK_GetPtr( *prevTask
);
140 *prevTask
= pTask
->hNext
;
147 /***********************************************************************
150 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
151 * and containing 'count' entries.
153 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
159 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
161 pThunk
->magic
= THUNK_MAGIC
;
162 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
164 for (i
= 0; i
< count
-1; i
++)
166 free
+= 8; /* Offset of next thunk */
167 pThunk
->thunks
[4*i
] = free
;
169 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
173 /***********************************************************************
176 * Allocate a thunk for MakeProcInstance().
178 static SEGPTR
TASK_AllocThunk(void)
184 if (!(pTask
= TASK_GetCurrent())) return 0;
185 sel
= pTask
->hCSAlias
;
186 pThunk
= &pTask
->thunks
;
187 base
= (int)pThunk
- (int)pTask
;
188 while (!pThunk
->free
)
191 if (!sel
) /* Allocate a new segment */
193 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
194 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
195 if (!sel
) return (SEGPTR
)0;
196 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
199 pThunk
= (THUNKS
*)GlobalLock16( sel
);
202 base
+= pThunk
->free
;
203 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
204 return MAKESEGPTR( sel
, base
);
208 /***********************************************************************
211 * Free a MakeProcInstance() thunk.
213 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
219 if (!(pTask
= TASK_GetCurrent())) return 0;
220 sel
= pTask
->hCSAlias
;
221 pThunk
= &pTask
->thunks
;
222 base
= (int)pThunk
- (int)pTask
;
223 while (sel
&& (sel
!= HIWORD(thunk
)))
226 pThunk
= (THUNKS
*)GlobalLock16( sel
);
229 if (!sel
) return FALSE
;
230 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
231 pThunk
->free
= LOWORD(thunk
) - base
;
236 /***********************************************************************
239 * NOTE: This routine might be called by a Win32 thread. Thus, we need
240 * to be careful to protect global data structures. We do this
241 * by entering the Win16Lock while linking the task into the
244 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
250 /* Allocate the task structure */
252 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
253 if (!hTask
) return NULL
;
254 pTask
= TASK_GetPtr( hTask
);
255 FarSetOwner16( hTask
, pModule
->self
);
257 /* Fill the task structure */
259 pTask
->hSelf
= hTask
;
261 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
263 pTask
->flags
|= TDBF_WIN32
;
264 pTask
->hInstance
= pModule
->self
;
265 pTask
->hPrevInstance
= 0;
266 /* NOTE: for 16-bit tasks, the instance handles are updated later on
270 pTask
->version
= pModule
->expected_version
;
271 pTask
->hModule
= pModule
->self
;
272 pTask
->hParent
= GetCurrentTask();
273 pTask
->magic
= TDB_MAGIC
;
274 pTask
->nCmdShow
= cmdShow
;
276 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
277 strcpy( pTask
->curdir
, "\\" );
278 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
279 sizeof(pTask
->curdir
) - 1 );
281 /* Create the thunks block */
283 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
285 /* Copy the module name */
287 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
288 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
290 /* Allocate a selector for the PDB */
292 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
293 pModule
->self
, WINE_LDT_FLAGS_DATA
);
297 pTask
->pdb
.int20
= 0x20cd;
298 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
299 PUT_UA_DWORD(&pTask
->pdb
.dispatcher
[1],
300 (DWORD
)GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" ));
301 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
302 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
303 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
304 pTask
->pdb
.fileHandlesPtr
=
305 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
306 pTask
->pdb
.hFileHandles
= 0;
307 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
308 /* FIXME: should we make a copy of the environment? */
309 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
310 pTask
->pdb
.nbFiles
= 20;
312 /* Fill the command line */
316 cmdline
= GetCommandLineA();
317 /* remove the first word (program name) */
319 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
320 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
321 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
322 len
= strlen(cmdline
);
324 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
325 pTask
->pdb
.cmdLine
[0] = len
;
326 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
327 /* pTask->pdb.cmdLine[len+1] = 0; */
329 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
331 /* Get the compatibility flags */
333 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
335 /* Allocate a code segment alias for the TDB */
337 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
338 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
340 /* Default DTA overwrites command line */
342 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
344 /* Create scheduler event for 16-bit tasks */
346 if ( !(pTask
->flags
& TDBF_WIN32
) )
347 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
349 /* Enter task handle into thread */
351 if (teb
) teb
->htask16
= hTask
;
352 if (!initial_task
) initial_task
= hTask
;
358 /***********************************************************************
361 static void TASK_DeleteTask( HTASK16 hTask
)
366 if (!(pTask
= TASK_GetPtr( hTask
))) return;
369 pTask
->magic
= 0xdead; /* invalidate signature */
371 /* Free the selector aliases */
373 GLOBAL_FreeBlock( pTask
->hCSAlias
);
374 GLOBAL_FreeBlock( pTask
->hPDB
);
376 /* Free the task module */
378 FreeModule16( pTask
->hModule
);
380 /* Free the task structure itself */
382 GlobalFree16( hTask
);
384 /* Free all memory used by this task (including the 32-bit stack, */
385 /* the environment block and the thunk segments). */
387 GlobalFreeAll16( hPDB
);
391 /***********************************************************************
392 * TASK_CreateMainTask
394 * Create a task for the main (32-bit) process.
396 void TASK_CreateMainTask(void)
399 STARTUPINFOA startup_info
;
400 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
402 GetStartupInfoA( &startup_info
);
403 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
404 pTask
= TASK_Create( (NE_MODULE
*)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
405 cmdShow
, NtCurrentTeb(), NULL
, 0 );
408 ERR("could not create task for main process\n");
412 /* Add the task to the linked list */
413 /* (no need to get the win16 lock, we are the only thread at this point) */
414 TASK_LinkTask( pTask
->hSelf
);
418 /* startup routine for a new 16-bit thread */
419 static DWORD CALLBACK
task_start( TDB
*pTask
)
423 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
424 NtCurrentTeb()->htask16
= pTask
->hSelf
;
427 TASK_LinkTask( pTask
->hSelf
);
428 pTask
->teb
= NtCurrentTeb();
429 ret
= NE_StartTask();
435 /***********************************************************************
438 * Spawn a new 16-bit task.
440 HTASK16
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
,
441 LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
445 if (!(pTask
= TASK_Create( pModule
, cmdShow
, NULL
, cmdline
, len
))) return 0;
446 if (!(*hThread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)task_start
, pTask
, 0, NULL
)))
448 TASK_DeleteTask( pTask
->hSelf
);
455 /***********************************************************************
458 void TASK_ExitTask(void)
463 /* Enter the Win16Lock to protect global data structures */
466 pTask
= TASK_GetCurrent();
473 TRACE("Killing task %04x\n", pTask
->hSelf
);
475 /* Perform USER cleanup */
477 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
478 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
479 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
480 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
482 /* Remove the task from the list to be sure we never switch back to it */
483 TASK_UnlinkTask( pTask
->hSelf
);
485 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
487 TRACE("this is the last task, exiting\n" );
493 TDB
* p
= TASK_GetPtr( hFirstTask
);
496 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
497 p
= TASK_GetPtr( p
->hNext
);
503 if ( hLockedTask
== pTask
->hSelf
)
506 TASK_DeleteTask( pTask
->hSelf
);
508 /* ... and completely release the Win16Lock, just in case. */
509 ReleaseThunkLock( &lockCount
);
513 /***********************************************************************
514 * InitTask (KERNEL.91)
516 * Called by the application startup code.
518 void WINAPI
InitTask16( CONTEXT86
*context
)
521 INSTANCEDATA
*pinstance
;
525 if (!(pTask
= TASK_GetCurrent())) return;
527 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
528 as some apps, notably Visual Basic apps, *modify* the heap/stack
529 size of the instance data segment before calling InitTask() */
531 /* Initialize the INSTANCEDATA structure */
532 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
533 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
534 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
535 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
536 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
538 /* Initialize the local heap */
539 if (LOWORD(context
->Ecx
))
540 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
542 /* Initialize implicitly loaded DLLs */
543 NE_InitializeDLLs( pTask
->hModule
);
544 NE_DllProcessAttach( pTask
->hModule
);
546 /* Registers on return are:
547 * ax 1 if OK, 0 on error
548 * cx stack limit in bytes
549 * dx cmdShow parameter
550 * si instance handle of the previous instance
551 * di instance handle of the new task
552 * es:bx pointer to command line inside PSP
554 * 0 (=%bp) is pushed on the stack
556 ptr
= stack16_push( sizeof(WORD
) );
557 *(WORD
*)MapSL(ptr
) = 0;
562 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
565 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
566 while ((*p
== ' ') || (*p
== '\t')) p
++;
567 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
569 context
->Ecx
= pinstance
->stacktop
;
570 context
->Edx
= pTask
->nCmdShow
;
571 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
572 context
->Edi
= (DWORD
)pTask
->hInstance
;
573 context
->SegEs
= (WORD
)pTask
->hPDB
;
577 /***********************************************************************
578 * WaitEvent (KERNEL.30)
580 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
584 if (!hTask
) hTask
= GetCurrentTask();
585 pTask
= TASK_GetPtr( hTask
);
587 if (pTask
->flags
& TDBF_WIN32
)
589 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
593 if (pTask
->nEvents
> 0)
599 if (pTask
->teb
== NtCurrentTeb())
603 NtResetEvent( pTask
->hEvent
, NULL
);
604 ReleaseThunkLock( &lockCount
);
605 SYSLEVEL_CheckNotLevel( 1 );
606 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
607 RestoreThunkLock( lockCount
);
608 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
610 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
616 /***********************************************************************
617 * PostEvent (KERNEL.31)
619 void WINAPI
PostEvent16( HTASK16 hTask
)
623 if (!hTask
) hTask
= GetCurrentTask();
624 if (!(pTask
= TASK_GetPtr( hTask
))) return;
626 if (pTask
->flags
& TDBF_WIN32
)
628 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
634 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
638 /***********************************************************************
639 * SetPriority (KERNEL.32)
641 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
646 if (!hTask
) hTask
= GetCurrentTask();
647 if (!(pTask
= TASK_GetPtr( hTask
))) return;
648 newpriority
= pTask
->priority
+ delta
;
649 if (newpriority
< -32) newpriority
= -32;
650 else if (newpriority
> 15) newpriority
= 15;
652 pTask
->priority
= newpriority
+ 1;
653 TASK_UnlinkTask( pTask
->hSelf
);
654 TASK_LinkTask( pTask
->hSelf
);
659 /***********************************************************************
660 * LockCurrentTask (KERNEL.33)
662 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
664 if (bLock
) hLockedTask
= GetCurrentTask();
665 else hLockedTask
= 0;
670 /***********************************************************************
671 * IsTaskLocked (KERNEL.122)
673 HTASK16 WINAPI
IsTaskLocked16(void)
679 /***********************************************************************
680 * OldYield (KERNEL.117)
682 void WINAPI
OldYield16(void)
686 ReleaseThunkLock(&count
);
687 RestoreThunkLock(count
);
690 /***********************************************************************
691 * WIN32_OldYield (KERNEL.447)
693 void WINAPI
WIN32_OldYield16(void)
697 ReleaseThunkLock(&count
);
698 RestoreThunkLock(count
);
701 /***********************************************************************
702 * DirectedYield (KERNEL.150)
704 void WINAPI
DirectedYield16( HTASK16 hTask
)
706 TDB
*pCurTask
= TASK_GetCurrent();
708 if (pCurTask
->flags
& TDBF_WIN32
)
710 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
714 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
716 pCurTask
->hYieldTo
= hTask
;
719 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
722 /***********************************************************************
725 void WINAPI
Yield16(void)
727 TDB
*pCurTask
= TASK_GetCurrent();
729 if (pCurTask
) pCurTask
->hYieldTo
= 0;
730 if (pCurTask
&& pCurTask
->hQueue
)
732 HMODULE mod
= GetModuleHandleA( "user32.dll" );
735 FARPROC proc
= GetProcAddress( mod
, "UserYield16" );
746 /***********************************************************************
747 * KERNEL_490 (KERNEL.490)
749 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
751 if ( !someTask
) return 0;
753 FIXME("(%04x): stub\n", someTask
);
757 /***********************************************************************
758 * MakeProcInstance (KERNEL.51)
760 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
764 WORD hInstanceSelector
;
766 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
768 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
771 /* Win95 actually protects via SEH, but this is better for debugging */
772 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
778 if ( (!(hInstance
& 4)) ||
779 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
781 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
787 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
789 && (hInstance
!= 0xffff) )
791 /* calling MPI with a foreign DSEG is invalid ! */
792 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
793 hInstance
,CURRENT_DS
);
796 /* Always use the DSEG that MPI was entered with.
797 * We used to set hInstance to GetTaskDS16(), but this should be wrong
798 * as CURRENT_DS provides the DSEG value we need.
799 * ("calling" DS, *not* "task" DS !) */
800 hInstanceSelector
= CURRENT_DS
;
801 hInstance
= GlobalHandle16(hInstanceSelector
);
803 /* no thunking for DLLs */
804 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
807 thunkaddr
= TASK_AllocThunk();
808 if (!thunkaddr
) return (FARPROC16
)0;
809 thunk
= MapSL( thunkaddr
);
810 lfunc
= MapSL( (SEGPTR
)func
);
812 TRACE("(%08lx,%04x): got thunk %08lx\n",
813 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
814 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
815 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
817 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
820 *thunk
++ = 0xb8; /* movw instance, %ax */
821 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
822 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
823 *thunk
++ = 0xea; /* ljmp func */
824 *(DWORD
*)thunk
= (DWORD
)func
;
825 return (FARPROC16
)thunkaddr
;
826 /* CX reg indicates if thunkaddr != NULL, implement if needed */
830 /***********************************************************************
831 * FreeProcInstance (KERNEL.52)
833 void WINAPI
FreeProcInstance16( FARPROC16 func
)
835 TRACE("(%08lx)\n", (DWORD
)func
);
836 TASK_FreeThunk( (SEGPTR
)func
);
839 /**********************************************************************
840 * TASK_GetCodeSegment
842 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
843 * and logical segment number of a given code segment.
845 * 'proc' either *is* already a pair of module handle and segment number,
846 * in which case there's nothing to do. Otherwise, it is a pointer to
847 * a function, and we need to retrieve the code segment. If the pointer
848 * happens to point to a thunk, we'll retrieve info about the code segment
849 * where the function pointed to by the thunk resides, not the thunk itself.
851 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
852 * the function the snoop code will return to ...
855 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
856 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
858 NE_MODULE
*pModule
= NULL
;
859 SEGTABLEENTRY
*pSeg
= NULL
;
862 /* Try pair of module handle / segment number */
863 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
864 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
866 segNr
= LOWORD( proc
);
867 if ( segNr
&& segNr
<= pModule
->seg_count
)
868 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
871 /* Try thunk or function */
874 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
877 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
878 selector
= thunk
[6] + (thunk
[7] << 8);
880 selector
= HIWORD( proc
);
882 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
883 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
886 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
887 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
890 if ( pModule
&& segNr
> pModule
->seg_count
)
894 /* Abort if segment not found */
896 if ( !pModule
|| !pSeg
)
899 /* Return segment data */
901 if ( ppModule
) *ppModule
= pModule
;
902 if ( ppSeg
) *ppSeg
= pSeg
;
903 if ( pSegNr
) *pSegNr
= segNr
;
908 /**********************************************************************
909 * GetCodeHandle (KERNEL.93)
911 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
915 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
921 /**********************************************************************
922 * GetCodeInfo (KERNEL.104)
924 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
930 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
933 /* Fill in segment information */
935 segInfo
->offSegment
= pSeg
->filepos
;
936 segInfo
->cbSegment
= pSeg
->size
;
937 segInfo
->flags
= pSeg
->flags
;
938 segInfo
->cbAlloc
= pSeg
->minsize
;
939 segInfo
->h
= pSeg
->hSeg
;
940 segInfo
->alignShift
= pModule
->alignment
;
942 if ( segNr
== pModule
->dgroup
)
943 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
945 /* Return module handle in %es */
947 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
953 /**********************************************************************
954 * DefineHandleTable (KERNEL.94)
956 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
958 FIXME("(%04x): stub ?\n", wOffset
);
963 /***********************************************************************
964 * SetTaskQueue (KERNEL.34)
966 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
971 if (!hTask
) hTask
= GetCurrentTask();
972 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
974 hPrev
= pTask
->hQueue
;
975 pTask
->hQueue
= hQueue
;
981 /***********************************************************************
982 * GetTaskQueue (KERNEL.35)
984 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
988 if (!hTask
) hTask
= GetCurrentTask();
989 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
990 return pTask
->hQueue
;
993 /***********************************************************************
994 * SetThreadQueue (KERNEL.463)
996 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
998 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
999 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1003 teb
->queue
= hQueue
;
1005 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1006 SetTaskQueue16( teb
->htask16
, hQueue
);
1012 /***********************************************************************
1013 * GetThreadQueue (KERNEL.464)
1015 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1019 teb
= NtCurrentTeb();
1020 else if ( HIWORD(thread
) )
1021 teb
= THREAD_IdToTEB( thread
);
1022 else if ( IsTask16( (HTASK16
)thread
) )
1023 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1025 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1028 /***********************************************************************
1029 * SetFastQueue (KERNEL.624)
1031 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1035 teb
= NtCurrentTeb();
1036 else if ( HIWORD(thread
) )
1037 teb
= THREAD_IdToTEB( thread
);
1038 else if ( IsTask16( (HTASK16
)thread
) )
1039 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1041 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1044 /***********************************************************************
1045 * GetFastQueue (KERNEL.625)
1047 HANDLE WINAPI
GetFastQueue16( void )
1049 HANDLE ret
= (HANDLE
)NtCurrentTeb()->queue
;
1051 if (!ret
) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1055 /***********************************************************************
1056 * SwitchStackTo (KERNEL.108)
1058 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1061 STACK16FRAME
*oldFrame
, *newFrame
;
1062 INSTANCEDATA
*pData
;
1065 if (!(pTask
= TASK_GetCurrent())) return;
1066 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1067 TRACE("old=%04x:%04x new=%04x:%04x\n",
1068 SELECTOROF( pTask
->teb
->cur_stack
),
1069 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1071 /* Save the old stack */
1073 oldFrame
= THREAD_STACK16( pTask
->teb
);
1074 /* pop frame + args and push bp */
1075 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1077 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1078 pData
->stacktop
= top
;
1079 pData
->stackmin
= ptr
;
1080 pData
->stackbottom
= ptr
;
1082 /* Switch to the new stack */
1084 /* Note: we need to take the 3 arguments into account; otherwise,
1085 * the stack will underflow upon return from this function.
1087 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1088 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1089 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1090 newFrame
= THREAD_STACK16( pTask
->teb
);
1092 /* Copy the stack frame and the local variables to the new stack */
1094 memmove( newFrame
, oldFrame
, copySize
);
1096 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1100 /***********************************************************************
1101 * SwitchStackBack (KERNEL.109)
1103 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1105 STACK16FRAME
*oldFrame
, *newFrame
;
1106 INSTANCEDATA
*pData
;
1108 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1110 if (!pData
->old_ss_sp
)
1112 WARN("No previous SwitchStackTo\n" );
1115 TRACE("restoring stack %04x:%04x\n",
1116 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1118 oldFrame
= CURRENT_STACK16
;
1120 /* Pop bp from the previous stack */
1122 BP_reg(context
) = *(WORD
*)MapSL(pData
->old_ss_sp
);
1123 pData
->old_ss_sp
+= sizeof(WORD
);
1125 /* Switch back to the old stack */
1127 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1128 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1129 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1130 pData
->old_ss_sp
= 0;
1132 /* Build a stack frame for the return */
1134 newFrame
= CURRENT_STACK16
;
1135 newFrame
->frame32
= oldFrame
->frame32
;
1136 newFrame
->module_cs
= oldFrame
->module_cs
;
1137 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1138 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1142 /***********************************************************************
1143 * GetTaskQueueDS (KERNEL.118)
1145 void WINAPI
GetTaskQueueDS16(void)
1147 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1151 /***********************************************************************
1152 * GetTaskQueueES (KERNEL.119)
1154 void WINAPI
GetTaskQueueES16(void)
1156 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1160 /***********************************************************************
1161 * GetCurrentTask (KERNEL32.@)
1163 HTASK16 WINAPI
GetCurrentTask(void)
1165 return NtCurrentTeb()->htask16
;
1168 /***********************************************************************
1169 * GetCurrentTask (KERNEL.36)
1171 DWORD WINAPI
WIN16_GetCurrentTask(void)
1173 /* This is the version used by relay code; the first task is */
1174 /* returned in the high word of the result */
1175 return MAKELONG( GetCurrentTask(), hFirstTask
);
1179 /***********************************************************************
1180 * GetCurrentPDB (KERNEL.37)
1182 * UNDOC: returns PSP of KERNEL in high word
1184 DWORD WINAPI
GetCurrentPDB16(void)
1188 if (!(pTask
= TASK_GetCurrent())) return 0;
1189 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1193 /***********************************************************************
1194 * GetCurPID (KERNEL.157)
1196 DWORD WINAPI
GetCurPID16( DWORD unused
)
1202 /***********************************************************************
1203 * GetInstanceData (KERNEL.54)
1205 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1207 char *ptr
= (char *)GlobalLock16( instance
);
1208 if (!ptr
|| !len
) return 0;
1209 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1210 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1215 /***********************************************************************
1216 * GetExeVersion (KERNEL.105)
1218 WORD WINAPI
GetExeVersion16(void)
1222 if (!(pTask
= TASK_GetCurrent())) return 0;
1223 return pTask
->version
;
1227 /***********************************************************************
1228 * SetErrorMode (KERNEL.107)
1230 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1233 if (!(pTask
= TASK_GetCurrent())) return 0;
1234 pTask
->error_mode
= mode
;
1235 return SetErrorMode( mode
);
1239 /***********************************************************************
1240 * GetNumTasks (KERNEL.152)
1242 UINT16 WINAPI
GetNumTasks16(void)
1248 /***********************************************************************
1249 * GetTaskDS (KERNEL.155)
1251 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1252 * I don't think we need to bother with this.
1254 HINSTANCE16 WINAPI
GetTaskDS16(void)
1258 if (!(pTask
= TASK_GetCurrent())) return 0;
1259 return GlobalHandleToSel16(pTask
->hInstance
);
1262 /***********************************************************************
1263 * GetDummyModuleHandleDS (KERNEL.602)
1265 WORD WINAPI
GetDummyModuleHandleDS16(void)
1270 if (!(pTask
= TASK_GetCurrent())) return 0;
1271 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1272 selector
= GlobalHandleToSel16( pTask
->hModule
);
1273 CURRENT_DS
= selector
;
1277 /***********************************************************************
1278 * IsTask (KERNEL.320)
1279 * IsTask16 (KERNEL32.@)
1281 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1285 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1286 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1287 return (pTask
->magic
== TDB_MAGIC
);
1291 /***********************************************************************
1292 * IsWinOldApTask (KERNEL.158)
1294 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1296 /* should return bit 0 of byte 0x48 in PSP */
1300 /***********************************************************************
1301 * SetTaskSignalProc (KERNEL.38)
1303 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1308 if (!hTask
) hTask
= GetCurrentTask();
1309 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1310 oldProc
= pTask
->userhandler
;
1311 pTask
->userhandler
= proc
;
1315 /***********************************************************************
1316 * TASK_CallTaskSignalProc
1318 /* ### start build ### */
1319 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1320 /* ### stop build ### */
1321 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1323 TDB
*pTask
= TASK_GetCurrent();
1324 if ( !pTask
|| !pTask
->userhandler
) return;
1326 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1327 hTaskOrModule
, uCode
, 0,
1328 pTask
->hInstance
, pTask
->hQueue
);
1331 /***********************************************************************
1332 * SetSigHandler (KERNEL.140)
1334 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1335 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1337 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1338 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1340 if (flag
!= 1) return 0;
1341 if (!newmode
) newhandler
= NULL
; /* Default handler */
1346 if (!(pTask
= TASK_GetCurrent())) return 0;
1347 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1348 pTask
->signal_flags
= newmode
;
1349 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1350 pTask
->sighandler
= newhandler
;
1356 /***********************************************************************
1357 * GlobalNotify (KERNEL.154)
1359 * Note that GlobalNotify does _not_ return the old NotifyProc
1360 * -- contrary to LocalNotify !!
1362 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1366 if (!(pTask
= TASK_GetCurrent())) return;
1367 pTask
->discardhandler
= proc
;
1371 /***********************************************************************
1374 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1379 /* Check for module handle */
1381 if (!(ptr
= GlobalLock16( handle
))) return 0;
1382 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1384 /* Search for this handle inside all tasks */
1386 *hTask
= hFirstTask
;
1389 TDB
*pTask
= TASK_GetPtr( *hTask
);
1390 if ((*hTask
== handle
) ||
1391 (pTask
->hInstance
== handle
) ||
1392 (pTask
->hQueue
== handle
) ||
1393 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1394 *hTask
= pTask
->hNext
;
1397 /* Check the owner for module handle */
1399 owner
= FarGetOwner16( handle
);
1400 if (!(ptr
= GlobalLock16( owner
))) return 0;
1401 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1403 /* Search for the owner inside all tasks */
1405 *hTask
= hFirstTask
;
1408 TDB
*pTask
= TASK_GetPtr( *hTask
);
1409 if ((*hTask
== owner
) ||
1410 (pTask
->hInstance
== owner
) ||
1411 (pTask
->hQueue
== owner
) ||
1412 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1413 *hTask
= pTask
->hNext
;
1419 /***********************************************************************
1420 * GetExePtr (KERNEL.133)
1422 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1425 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1426 STACK16FRAME
*frame
= CURRENT_STACK16
;
1427 frame
->ecx
= hModule
;
1428 if (hTask
) frame
->es
= hTask
;
1433 /***********************************************************************
1436 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1439 return GetExePtrHelper( handle
, &hTask
);
1443 /***********************************************************************
1444 * TaskFirst (TOOLHELP.63)
1446 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1448 lpte
->hNext
= hFirstTask
;
1449 return TaskNext16( lpte
);
1453 /***********************************************************************
1454 * TaskNext (TOOLHELP.64)
1456 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1459 INSTANCEDATA
*pInstData
;
1461 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1462 if (!lpte
->hNext
) return FALSE
;
1464 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1466 pTask
= TASK_GetPtr( lpte
->hNext
);
1467 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1468 if (pTask
->hInstance
)
1470 lpte
->hNext
= pTask
->hNext
;
1472 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1473 lpte
->hTask
= lpte
->hNext
;
1474 lpte
->hTaskParent
= pTask
->hParent
;
1475 lpte
->hInst
= pTask
->hInstance
;
1476 lpte
->hModule
= pTask
->hModule
;
1477 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1478 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1479 lpte
->wStackTop
= pInstData
->stacktop
;
1480 lpte
->wStackMinimum
= pInstData
->stackmin
;
1481 lpte
->wStackBottom
= pInstData
->stackbottom
;
1482 lpte
->wcEvents
= pTask
->nEvents
;
1483 lpte
->hQueue
= pTask
->hQueue
;
1484 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1485 lpte
->wPSPOffset
= 0x100; /*??*/
1486 lpte
->hNext
= pTask
->hNext
;
1491 /***********************************************************************
1492 * TaskFindHandle (TOOLHELP.65)
1494 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1496 lpte
->hNext
= hTask
;
1497 return TaskNext16( lpte
);
1501 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1503 /**************************************************************************
1504 * FatalAppExit (KERNEL.137)
1506 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1508 TDB
*pTask
= TASK_GetCurrent();
1510 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1512 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1515 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1518 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1522 ERR( "%s\n", debugstr_a(str
) );
1529 /***********************************************************************
1530 * TerminateApp (TOOLHELP.77)
1532 * See "Undocumented Windows".
1534 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1536 if (hTask
&& hTask
!= GetCurrentTask())
1538 FIXME("cannot terminate task %x\n", hTask
);
1542 if (wFlags
& NO_UAE_BOX
)
1545 old_mode
= SetErrorMode16(0);
1546 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1548 FatalAppExit16( 0, NULL
);
1550 /* hmm, we're still alive ?? */
1552 /* check undocumented flag */
1553 if (!(wFlags
& 0x8000))
1554 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1556 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1557 but let's just call ExitThread */
1562 /***********************************************************************
1563 * GetAppCompatFlags (KERNEL.354)
1565 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1567 return GetAppCompatFlags( hTask
);
1571 /***********************************************************************
1572 * GetAppCompatFlags (USER32.@)
1574 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1578 if (!hTask
) hTask
= GetCurrentTask();
1579 if (!(pTask
=TASK_GetPtr( (HTASK16
)hTask
))) return 0;
1580 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1581 return pTask
->compat_flags
;