4 * Copyright 1995 Alexandre Julliard
12 #include "wine/port.h"
13 #include "wine/winbase16.h"
23 #include "selectors.h"
24 #include "stackframe.h"
31 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(task
);
37 DECLARE_DEBUG_CHANNEL(relay
);
38 DECLARE_DEBUG_CHANNEL(toolhelp
);
40 /* Min. number of thunks allocated when creating a new segment */
44 static THHOOK DefaultThhook
;
45 THHOOK
*pThhook
= &DefaultThhook
;
47 #define hCurrentTask (pThhook->CurTDB)
48 #define hFirstTask (pThhook->HeadTDB)
49 #define hLockedTask (pThhook->LockTDB)
51 static UINT16 nTaskCount
= 0;
53 static HTASK initial_task
;
55 /***********************************************************************
58 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
60 THHOOK
*pOldThhook
= pThhook
;
62 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
64 *pThhook
= *pOldThhook
;
67 /***********************************************************************
70 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
72 TDB
* pTask
= TASK_GetPtr( hTask
);
74 if (pTask
->hNext
) return pTask
->hNext
;
75 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
79 /***********************************************************************
82 TDB
*TASK_GetPtr( HTASK16 hTask
)
84 return GlobalLock16( hTask
);
88 /***********************************************************************
91 TDB
*TASK_GetCurrent(void)
93 return TASK_GetPtr( GetCurrentTask() );
97 /***********************************************************************
100 static void TASK_LinkTask( HTASK16 hTask
)
105 if (!(pTask
= TASK_GetPtr( hTask
))) return;
106 prevTask
= &hFirstTask
;
109 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
110 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
111 prevTask
= &prevTaskPtr
->hNext
;
113 pTask
->hNext
= *prevTask
;
119 /***********************************************************************
122 static void TASK_UnlinkTask( HTASK16 hTask
)
127 prevTask
= &hFirstTask
;
128 while (*prevTask
&& (*prevTask
!= hTask
))
130 pTask
= TASK_GetPtr( *prevTask
);
131 prevTask
= &pTask
->hNext
;
135 pTask
= TASK_GetPtr( *prevTask
);
136 *prevTask
= pTask
->hNext
;
143 /***********************************************************************
146 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
147 * and containing 'count' entries.
149 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
155 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
157 pThunk
->magic
= THUNK_MAGIC
;
158 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
160 for (i
= 0; i
< count
-1; i
++)
162 free
+= 8; /* Offset of next thunk */
163 pThunk
->thunks
[4*i
] = free
;
165 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
169 /***********************************************************************
172 * Allocate a thunk for MakeProcInstance().
174 static SEGPTR
TASK_AllocThunk(void)
180 if (!(pTask
= TASK_GetCurrent())) return 0;
181 sel
= pTask
->hCSAlias
;
182 pThunk
= &pTask
->thunks
;
183 base
= (int)pThunk
- (int)pTask
;
184 while (!pThunk
->free
)
187 if (!sel
) /* Allocate a new segment */
189 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
190 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
191 if (!sel
) return (SEGPTR
)0;
192 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
195 pThunk
= (THUNKS
*)GlobalLock16( sel
);
198 base
+= pThunk
->free
;
199 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
200 return MAKESEGPTR( sel
, base
);
204 /***********************************************************************
207 * Free a MakeProcInstance() thunk.
209 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
215 if (!(pTask
= TASK_GetCurrent())) return 0;
216 sel
= pTask
->hCSAlias
;
217 pThunk
= &pTask
->thunks
;
218 base
= (int)pThunk
- (int)pTask
;
219 while (sel
&& (sel
!= HIWORD(thunk
)))
222 pThunk
= (THUNKS
*)GlobalLock16( sel
);
225 if (!sel
) return FALSE
;
226 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
227 pThunk
->free
= LOWORD(thunk
) - base
;
232 /***********************************************************************
235 * NOTE: This routine might be called by a Win32 thread. Thus, we need
236 * to be careful to protect global data structures. We do this
237 * by entering the Win16Lock while linking the task into the
240 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
246 /* Allocate the task structure */
248 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
249 if (!hTask
) return NULL
;
250 pTask
= TASK_GetPtr( hTask
);
251 FarSetOwner16( hTask
, pModule
->self
);
253 /* Fill the task structure */
255 pTask
->hSelf
= hTask
;
257 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
259 pTask
->flags
|= TDBF_WIN32
;
260 pTask
->hInstance
= pModule
->self
;
261 pTask
->hPrevInstance
= 0;
262 /* NOTE: for 16-bit tasks, the instance handles are updated later on
266 pTask
->version
= pModule
->expected_version
;
267 pTask
->hModule
= pModule
->self
;
268 pTask
->hParent
= GetCurrentTask();
269 pTask
->magic
= TDB_MAGIC
;
270 pTask
->nCmdShow
= cmdShow
;
272 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
273 strcpy( pTask
->curdir
, "\\" );
274 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
275 sizeof(pTask
->curdir
) - 1 );
277 /* Create the thunks block */
279 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
281 /* Copy the module name */
283 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
284 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
286 /* Allocate a selector for the PDB */
288 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
289 pModule
->self
, WINE_LDT_FLAGS_DATA
);
293 pTask
->pdb
.int20
= 0x20cd;
294 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
295 PUT_UA_DWORD(&pTask
->pdb
.dispatcher
[1],
296 (DWORD
)GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" ));
297 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
298 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
299 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
300 pTask
->pdb
.fileHandlesPtr
=
301 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
302 pTask
->pdb
.hFileHandles
= 0;
303 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
304 /* FIXME: should we make a copy of the environment? */
305 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
306 pTask
->pdb
.nbFiles
= 20;
308 /* Fill the command line */
312 cmdline
= GetCommandLineA();
313 /* remove the first word (program name) */
315 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
316 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
317 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
318 len
= strlen(cmdline
);
320 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
321 pTask
->pdb
.cmdLine
[0] = len
;
322 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
323 /* pTask->pdb.cmdLine[len+1] = 0; */
325 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
327 /* Get the compatibility flags */
329 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
331 /* Allocate a code segment alias for the TDB */
333 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
334 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
336 /* Default DTA overwrites command line */
338 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
340 /* Create scheduler event for 16-bit tasks */
342 if ( !(pTask
->flags
& TDBF_WIN32
) )
343 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
345 /* Enter task handle into thread */
347 if (teb
) teb
->htask16
= hTask
;
348 if (!initial_task
) initial_task
= hTask
;
354 /***********************************************************************
357 static void TASK_DeleteTask( HTASK16 hTask
)
362 if (!(pTask
= TASK_GetPtr( hTask
))) return;
365 pTask
->magic
= 0xdead; /* invalidate signature */
367 /* Free the selector aliases */
369 GLOBAL_FreeBlock( pTask
->hCSAlias
);
370 GLOBAL_FreeBlock( pTask
->hPDB
);
372 /* Free the task module */
374 FreeModule16( pTask
->hModule
);
376 /* Free the task structure itself */
378 GlobalFree16( hTask
);
380 /* Free all memory used by this task (including the 32-bit stack, */
381 /* the environment block and the thunk segments). */
383 GlobalFreeAll16( hPDB
);
387 /***********************************************************************
388 * TASK_CreateMainTask
390 * Create a task for the main (32-bit) process.
392 void TASK_CreateMainTask(void)
395 STARTUPINFOA startup_info
;
396 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
398 GetStartupInfoA( &startup_info
);
399 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
400 pTask
= TASK_Create( (NE_MODULE
*)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
401 cmdShow
, NtCurrentTeb(), NULL
, 0 );
404 ERR("could not create task for main process\n");
408 /* Add the task to the linked list */
409 /* (no need to get the win16 lock, we are the only thread at this point) */
410 TASK_LinkTask( pTask
->hSelf
);
414 /* startup routine for a new 16-bit thread */
415 static DWORD CALLBACK
task_start( TDB
*pTask
)
419 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
420 NtCurrentTeb()->htask16
= pTask
->hSelf
;
423 TASK_LinkTask( pTask
->hSelf
);
424 pTask
->teb
= NtCurrentTeb();
425 ret
= NE_StartTask();
431 /***********************************************************************
434 * Spawn a new 16-bit task.
436 HTASK
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
, LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
440 if (!(pTask
= TASK_Create( pModule
, cmdShow
, NULL
, cmdline
, len
))) return 0;
441 if (!(*hThread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)task_start
, pTask
, 0, NULL
)))
443 TASK_DeleteTask( pTask
->hSelf
);
450 /***********************************************************************
453 void TASK_ExitTask(void)
458 /* Enter the Win16Lock to protect global data structures */
461 pTask
= TASK_GetCurrent();
468 TRACE("Killing task %04x\n", pTask
->hSelf
);
470 /* Perform USER cleanup */
472 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
473 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
474 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
475 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
477 /* Remove the task from the list to be sure we never switch back to it */
478 TASK_UnlinkTask( pTask
->hSelf
);
480 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
482 TRACE("this is the last task, exiting\n" );
488 TDB
* p
= TASK_GetPtr( hFirstTask
);
491 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
492 p
= TASK_GetPtr( p
->hNext
);
498 if ( hLockedTask
== pTask
->hSelf
)
501 TASK_DeleteTask( pTask
->hSelf
);
503 /* ... and completely release the Win16Lock, just in case. */
504 ReleaseThunkLock( &lockCount
);
508 /***********************************************************************
509 * InitTask (KERNEL.91)
511 * Called by the application startup code.
513 void WINAPI
InitTask16( CONTEXT86
*context
)
516 INSTANCEDATA
*pinstance
;
520 if (!(pTask
= TASK_GetCurrent())) return;
522 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
523 as some apps, notably Visual Basic apps, *modify* the heap/stack
524 size of the instance data segment before calling InitTask() */
526 /* Initialize the INSTANCEDATA structure */
527 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
528 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
529 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
530 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
531 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
533 /* Initialize the local heap */
534 if (LOWORD(context
->Ecx
))
535 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
537 /* Initialize implicitly loaded DLLs */
538 NE_InitializeDLLs( pTask
->hModule
);
539 NE_DllProcessAttach( pTask
->hModule
);
541 /* Registers on return are:
542 * ax 1 if OK, 0 on error
543 * cx stack limit in bytes
544 * dx cmdShow parameter
545 * si instance handle of the previous instance
546 * di instance handle of the new task
547 * es:bx pointer to command line inside PSP
549 * 0 (=%bp) is pushed on the stack
551 ptr
= stack16_push( sizeof(WORD
) );
552 *(WORD
*)MapSL(ptr
) = 0;
557 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
560 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
561 while ((*p
== ' ') || (*p
== '\t')) p
++;
562 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
564 context
->Ecx
= pinstance
->stacktop
;
565 context
->Edx
= pTask
->nCmdShow
;
566 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
567 context
->Edi
= (DWORD
)pTask
->hInstance
;
568 context
->SegEs
= (WORD
)pTask
->hPDB
;
572 /***********************************************************************
573 * WaitEvent (KERNEL.30)
575 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
579 if (!hTask
) hTask
= GetCurrentTask();
580 pTask
= TASK_GetPtr( hTask
);
582 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
584 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
588 if (pTask
->nEvents
> 0)
594 if (pTask
->teb
== NtCurrentTeb())
598 NtResetEvent( pTask
->hEvent
, NULL
);
599 ReleaseThunkLock( &lockCount
);
600 SYSLEVEL_CheckNotLevel( 1 );
601 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
602 RestoreThunkLock( lockCount
);
603 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
605 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
611 /***********************************************************************
612 * PostEvent (KERNEL.31)
614 void WINAPI
PostEvent16( HTASK16 hTask
)
618 if (!hTask
) hTask
= GetCurrentTask();
619 if (!(pTask
= TASK_GetPtr( hTask
))) return;
621 if ( !THREAD_IsWin16( pTask
->teb
) )
623 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
629 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
633 /***********************************************************************
634 * SetPriority (KERNEL.32)
636 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
641 if (!hTask
) hTask
= GetCurrentTask();
642 if (!(pTask
= TASK_GetPtr( hTask
))) return;
643 newpriority
= pTask
->priority
+ delta
;
644 if (newpriority
< -32) newpriority
= -32;
645 else if (newpriority
> 15) newpriority
= 15;
647 pTask
->priority
= newpriority
+ 1;
648 TASK_UnlinkTask( pTask
->hSelf
);
649 TASK_LinkTask( pTask
->hSelf
);
654 /***********************************************************************
655 * LockCurrentTask (KERNEL.33)
657 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
659 if (bLock
) hLockedTask
= GetCurrentTask();
660 else hLockedTask
= 0;
665 /***********************************************************************
666 * IsTaskLocked (KERNEL.122)
668 HTASK16 WINAPI
IsTaskLocked16(void)
674 /***********************************************************************
675 * OldYield (KERNEL.117)
677 void WINAPI
OldYield16(void)
681 ReleaseThunkLock(&count
);
682 RestoreThunkLock(count
);
685 /***********************************************************************
686 * WIN32_OldYield16 (KERNEL.447)
688 void WINAPI
WIN32_OldYield16(void)
692 ReleaseThunkLock(&count
);
693 RestoreThunkLock(count
);
696 /***********************************************************************
697 * DirectedYield (KERNEL.150)
699 void WINAPI
DirectedYield16( HTASK16 hTask
)
701 TDB
*pCurTask
= TASK_GetCurrent();
703 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
705 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
709 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
711 pCurTask
->hYieldTo
= hTask
;
714 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
717 /***********************************************************************
718 * Yield16 (KERNEL.29)
720 void WINAPI
Yield16(void)
722 TDB
*pCurTask
= TASK_GetCurrent();
724 if (pCurTask
) pCurTask
->hYieldTo
= 0;
725 if (pCurTask
&& pCurTask
->hQueue
&& Callout
.UserYield16
) Callout
.UserYield16();
729 /***********************************************************************
730 * KERNEL_490 (KERNEL.490)
732 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
734 if ( !someTask
) return 0;
736 FIXME("(%04x): stub\n", someTask
);
740 /***********************************************************************
741 * MakeProcInstance16 (KERNEL.51)
743 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
747 WORD hInstanceSelector
;
749 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
751 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
754 /* Win95 actually protects via SEH, but this is better for debugging */
755 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
761 if ( (!(hInstance
& 4)) ||
762 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
764 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
770 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
772 && (hInstance
!= 0xffff) )
774 /* calling MPI with a foreign DSEG is invalid ! */
775 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
776 hInstance
,CURRENT_DS
);
779 /* Always use the DSEG that MPI was entered with.
780 * We used to set hInstance to GetTaskDS16(), but this should be wrong
781 * as CURRENT_DS provides the DSEG value we need.
782 * ("calling" DS, *not* "task" DS !) */
783 hInstanceSelector
= CURRENT_DS
;
784 hInstance
= GlobalHandle16(hInstanceSelector
);
786 /* no thunking for DLLs */
787 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
790 thunkaddr
= TASK_AllocThunk();
791 if (!thunkaddr
) return (FARPROC16
)0;
792 thunk
= MapSL( thunkaddr
);
793 lfunc
= MapSL( (SEGPTR
)func
);
795 TRACE("(%08lx,%04x): got thunk %08lx\n",
796 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
797 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
798 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
800 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
803 *thunk
++ = 0xb8; /* movw instance, %ax */
804 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
805 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
806 *thunk
++ = 0xea; /* ljmp func */
807 *(DWORD
*)thunk
= (DWORD
)func
;
808 return (FARPROC16
)thunkaddr
;
809 /* CX reg indicates if thunkaddr != NULL, implement if needed */
813 /***********************************************************************
814 * FreeProcInstance16 (KERNEL.52)
816 void WINAPI
FreeProcInstance16( FARPROC16 func
)
818 TRACE("(%08lx)\n", (DWORD
)func
);
819 TASK_FreeThunk( (SEGPTR
)func
);
822 /**********************************************************************
823 * TASK_GetCodeSegment
825 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
826 * and logical segment number of a given code segment.
828 * 'proc' either *is* already a pair of module handle and segment number,
829 * in which case there's nothing to do. Otherwise, it is a pointer to
830 * a function, and we need to retrieve the code segment. If the pointer
831 * happens to point to a thunk, we'll retrieve info about the code segment
832 * where the function pointed to by the thunk resides, not the thunk itself.
834 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
835 * the function the snoop code will return to ...
838 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
839 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
841 NE_MODULE
*pModule
= NULL
;
842 SEGTABLEENTRY
*pSeg
= NULL
;
845 /* Try pair of module handle / segment number */
846 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
847 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
849 segNr
= LOWORD( proc
);
850 if ( segNr
&& segNr
<= pModule
->seg_count
)
851 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
854 /* Try thunk or function */
857 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
860 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
861 selector
= thunk
[6] + (thunk
[7] << 8);
863 selector
= HIWORD( proc
);
865 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
866 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
869 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
870 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
873 if ( pModule
&& segNr
> pModule
->seg_count
)
877 /* Abort if segment not found */
879 if ( !pModule
|| !pSeg
)
882 /* Return segment data */
884 if ( ppModule
) *ppModule
= pModule
;
885 if ( ppSeg
) *ppSeg
= pSeg
;
886 if ( pSegNr
) *pSegNr
= segNr
;
891 /**********************************************************************
892 * GetCodeHandle (KERNEL.93)
894 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
898 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
904 /**********************************************************************
905 * GetCodeInfo (KERNEL.104)
907 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
913 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
916 /* Fill in segment information */
918 segInfo
->offSegment
= pSeg
->filepos
;
919 segInfo
->cbSegment
= pSeg
->size
;
920 segInfo
->flags
= pSeg
->flags
;
921 segInfo
->cbAlloc
= pSeg
->minsize
;
922 segInfo
->h
= pSeg
->hSeg
;
923 segInfo
->alignShift
= pModule
->alignment
;
925 if ( segNr
== pModule
->dgroup
)
926 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
928 /* Return module handle in %es */
930 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
936 /**********************************************************************
937 * DefineHandleTable16 (KERNEL.94)
939 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
941 FIXME("(%04x): stub ?\n", wOffset
);
946 /***********************************************************************
947 * SetTaskQueue (KERNEL.34)
949 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
954 if (!hTask
) hTask
= GetCurrentTask();
955 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
957 hPrev
= pTask
->hQueue
;
958 pTask
->hQueue
= hQueue
;
964 /***********************************************************************
965 * GetTaskQueue (KERNEL.35)
967 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
971 if (!hTask
) hTask
= GetCurrentTask();
972 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
973 return pTask
->hQueue
;
976 /***********************************************************************
977 * SetThreadQueue (KERNEL.463)
979 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
981 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
982 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
988 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
989 SetTaskQueue16( teb
->htask16
, hQueue
);
995 /***********************************************************************
996 * GetThreadQueue (KERNEL.464)
998 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1002 teb
= NtCurrentTeb();
1003 else if ( HIWORD(thread
) )
1004 teb
= THREAD_IdToTEB( thread
);
1005 else if ( IsTask16( (HTASK16
)thread
) )
1006 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1008 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1011 /***********************************************************************
1012 * SetFastQueue (KERNEL.624)
1014 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
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 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1027 /***********************************************************************
1028 * GetFastQueue (KERNEL.625)
1030 HANDLE WINAPI
GetFastQueue16( void )
1032 TEB
*teb
= NtCurrentTeb();
1037 if (!Callout
.InitThreadInput16
)
1039 THUNK_InitCallout();
1040 if (!Callout
.InitThreadInput16
)
1042 FIXME("InitThreadInput16 callout not found, trouble ahead\n");
1046 Callout
.InitThreadInput16( 0, THREAD_IsWin16(teb
)? 4 : 5 );
1049 FIXME("(): should initialize thread-local queue, expect failure!\n" );
1052 return (HANDLE
)teb
->queue
;
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 * GetTaskQueueDS16 (KERNEL.118)
1145 void WINAPI
GetTaskQueueDS16(void)
1147 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1151 /***********************************************************************
1152 * GetTaskQueueES16 (KERNEL.119)
1154 void WINAPI
GetTaskQueueES16(void)
1156 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1160 /***********************************************************************
1161 * GetCurrentTask (KERNEL.36)
1163 HTASK16 WINAPI
GetCurrentTask(void)
1165 return NtCurrentTeb()->htask16
;
1168 DWORD WINAPI
WIN16_GetCurrentTask(void)
1170 /* This is the version used by relay code; the first task is */
1171 /* returned in the high word of the result */
1172 return MAKELONG( GetCurrentTask(), hFirstTask
);
1176 /***********************************************************************
1177 * GetCurrentPDB (KERNEL.37)
1179 * UNDOC: returns PSP of KERNEL in high word
1181 DWORD WINAPI
GetCurrentPDB16(void)
1185 if (!(pTask
= TASK_GetCurrent())) return 0;
1186 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1190 /***********************************************************************
1191 * GetCurPID16 (KERNEL.157)
1193 DWORD WINAPI
GetCurPID16( DWORD unused
)
1199 /***********************************************************************
1200 * GetInstanceData (KERNEL.54)
1202 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1204 char *ptr
= (char *)GlobalLock16( instance
);
1205 if (!ptr
|| !len
) return 0;
1206 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1207 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1212 /***********************************************************************
1213 * GetExeVersion (KERNEL.105)
1215 WORD WINAPI
GetExeVersion16(void)
1219 if (!(pTask
= TASK_GetCurrent())) return 0;
1220 return pTask
->version
;
1224 /***********************************************************************
1225 * SetErrorMode16 (KERNEL.107)
1227 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1230 if (!(pTask
= TASK_GetCurrent())) return 0;
1231 pTask
->error_mode
= mode
;
1232 return SetErrorMode( mode
);
1236 /***********************************************************************
1237 * GetNumTasks (KERNEL.152)
1239 UINT16 WINAPI
GetNumTasks16(void)
1245 /***********************************************************************
1246 * GetTaskDS (KERNEL.155)
1248 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1249 * I don't think we need to bother with this.
1251 HINSTANCE16 WINAPI
GetTaskDS16(void)
1255 if (!(pTask
= TASK_GetCurrent())) return 0;
1256 return GlobalHandleToSel16(pTask
->hInstance
);
1259 /***********************************************************************
1260 * GetDummyModuleHandleDS (KERNEL.602)
1262 WORD WINAPI
GetDummyModuleHandleDS16(void)
1267 if (!(pTask
= TASK_GetCurrent())) return 0;
1268 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1269 selector
= GlobalHandleToSel16( pTask
->hModule
);
1270 CURRENT_DS
= selector
;
1274 /***********************************************************************
1275 * IsTask (KERNEL.320)
1276 * IsTask16 (KERNEL32.@)
1278 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1282 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1283 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1284 return (pTask
->magic
== TDB_MAGIC
);
1288 /***********************************************************************
1289 * IsWinOldApTask16 (KERNEL.158)
1291 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1293 /* should return bit 0 of byte 0x48 in PSP */
1297 /***********************************************************************
1298 * SetTaskSignalProc (KERNEL.38)
1300 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1305 if (!hTask
) hTask
= GetCurrentTask();
1306 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1307 oldProc
= pTask
->userhandler
;
1308 pTask
->userhandler
= proc
;
1312 /***********************************************************************
1313 * TASK_CallTaskSignalProc
1315 /* ### start build ### */
1316 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1317 /* ### stop build ### */
1318 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1320 TDB
*pTask
= TASK_GetCurrent();
1321 if ( !pTask
|| !pTask
->userhandler
) return;
1323 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1324 hTaskOrModule
, uCode
, 0,
1325 pTask
->hInstance
, pTask
->hQueue
);
1328 /***********************************************************************
1329 * SetSigHandler (KERNEL.140)
1331 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1332 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1334 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1335 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1337 if (flag
!= 1) return 0;
1338 if (!newmode
) newhandler
= NULL
; /* Default handler */
1343 if (!(pTask
= TASK_GetCurrent())) return 0;
1344 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1345 pTask
->signal_flags
= newmode
;
1346 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1347 pTask
->sighandler
= newhandler
;
1353 /***********************************************************************
1354 * GlobalNotify (KERNEL.154)
1356 * Note that GlobalNotify does _not_ return the old NotifyProc
1357 * -- contrary to LocalNotify !!
1359 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1363 if (!(pTask
= TASK_GetCurrent())) return;
1364 pTask
->discardhandler
= proc
;
1368 /***********************************************************************
1369 * GetExePtr (KERNEL.133)
1371 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1376 /* Check for module handle */
1378 if (!(ptr
= GlobalLock16( handle
))) return 0;
1379 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1381 /* Search for this handle inside all tasks */
1383 *hTask
= hFirstTask
;
1386 TDB
*pTask
= TASK_GetPtr( *hTask
);
1387 if ((*hTask
== handle
) ||
1388 (pTask
->hInstance
== handle
) ||
1389 (pTask
->hQueue
== handle
) ||
1390 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1391 *hTask
= pTask
->hNext
;
1394 /* Check the owner for module handle */
1396 owner
= FarGetOwner16( handle
);
1397 if (!(ptr
= GlobalLock16( owner
))) return 0;
1398 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1400 /* Search for the owner inside all tasks */
1402 *hTask
= hFirstTask
;
1405 TDB
*pTask
= TASK_GetPtr( *hTask
);
1406 if ((*hTask
== owner
) ||
1407 (pTask
->hInstance
== owner
) ||
1408 (pTask
->hQueue
== owner
) ||
1409 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1410 *hTask
= pTask
->hNext
;
1416 /**********************************************************************/
1418 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1421 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1422 STACK16FRAME
*frame
= CURRENT_STACK16
;
1423 frame
->ecx
= hModule
;
1424 if (hTask
) frame
->es
= hTask
;
1428 /**********************************************************************/
1430 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1433 return GetExePtrHelper( handle
, &hTask
);
1437 /***********************************************************************
1438 * TaskFirst (TOOLHELP.63)
1440 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1442 lpte
->hNext
= hFirstTask
;
1443 return TaskNext16( lpte
);
1447 /***********************************************************************
1448 * TaskNext (TOOLHELP.64)
1450 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1453 INSTANCEDATA
*pInstData
;
1455 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1456 if (!lpte
->hNext
) return FALSE
;
1458 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1460 pTask
= TASK_GetPtr( lpte
->hNext
);
1461 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1462 if (pTask
->hInstance
)
1464 lpte
->hNext
= pTask
->hNext
;
1466 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1467 lpte
->hTask
= lpte
->hNext
;
1468 lpte
->hTaskParent
= pTask
->hParent
;
1469 lpte
->hInst
= pTask
->hInstance
;
1470 lpte
->hModule
= pTask
->hModule
;
1471 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1472 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1473 lpte
->wStackTop
= pInstData
->stacktop
;
1474 lpte
->wStackMinimum
= pInstData
->stackmin
;
1475 lpte
->wStackBottom
= pInstData
->stackbottom
;
1476 lpte
->wcEvents
= pTask
->nEvents
;
1477 lpte
->hQueue
= pTask
->hQueue
;
1478 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1479 lpte
->wPSPOffset
= 0x100; /*??*/
1480 lpte
->hNext
= pTask
->hNext
;
1485 /***********************************************************************
1486 * TaskFindHandle (TOOLHELP.65)
1488 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1490 lpte
->hNext
= hTask
;
1491 return TaskNext16( lpte
);
1495 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1497 /**************************************************************************
1498 * FatalAppExit16 (KERNEL.137)
1500 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1502 TDB
*pTask
= TASK_GetCurrent();
1504 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1506 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1509 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1512 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1516 ERR( "%s\n", debugstr_a(str
) );
1523 /***********************************************************************
1524 * TerminateApp16 (TOOLHELP.77)
1526 * See "Undocumented Windows".
1528 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1530 if (hTask
&& hTask
!= GetCurrentTask())
1532 FIXME("cannot terminate task %x\n", hTask
);
1536 if (wFlags
& NO_UAE_BOX
)
1539 old_mode
= SetErrorMode16(0);
1540 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1542 FatalAppExit16( 0, NULL
);
1544 /* hmm, we're still alive ?? */
1546 /* check undocumented flag */
1547 if (!(wFlags
& 0x8000))
1548 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1550 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1551 but let's just call ExitThread */
1556 /***********************************************************************
1557 * GetAppCompatFlags16 (KERNEL.354)
1559 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1561 return GetAppCompatFlags( hTask
);
1565 /***********************************************************************
1566 * GetAppCompatFlags (USER32.206)
1568 DWORD WINAPI
GetAppCompatFlags( HTASK hTask
)
1572 if (!hTask
) hTask
= GetCurrentTask();
1573 if (!(pTask
=TASK_GetPtr( (HTASK16
)hTask
))) return 0;
1574 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1575 return pTask
->compat_flags
;