4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
36 #include "wine/winbase16.h"
43 #include "selectors.h"
44 #include "wine/server.h"
46 #include "stackframe.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(task
);
54 WINE_DECLARE_DEBUG_CHANNEL(relay
);
55 WINE_DECLARE_DEBUG_CHANNEL(toolhelp
);
57 /* Min. number of thunks allocated when creating a new segment */
61 static THHOOK DefaultThhook
;
62 THHOOK
*pThhook
= &DefaultThhook
;
64 #define hCurrentTask (pThhook->CurTDB)
65 #define hFirstTask (pThhook->HeadTDB)
66 #define hLockedTask (pThhook->LockTDB)
68 static UINT16 nTaskCount
= 0;
70 static HTASK16 initial_task
;
72 /***********************************************************************
75 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
77 THHOOK
*pOldThhook
= pThhook
;
79 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
81 *pThhook
= *pOldThhook
;
84 /***********************************************************************
87 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
89 TDB
* pTask
= TASK_GetPtr( hTask
);
91 if (pTask
->hNext
) return pTask
->hNext
;
92 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
96 /***********************************************************************
99 TDB
*TASK_GetPtr( HTASK16 hTask
)
101 return GlobalLock16( hTask
);
105 /***********************************************************************
108 TDB
*TASK_GetCurrent(void)
110 return TASK_GetPtr( GetCurrentTask() );
114 /***********************************************************************
117 static void TASK_LinkTask( HTASK16 hTask
)
122 if (!(pTask
= TASK_GetPtr( hTask
))) return;
123 prevTask
= &hFirstTask
;
126 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
127 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
128 prevTask
= &prevTaskPtr
->hNext
;
130 pTask
->hNext
= *prevTask
;
136 /***********************************************************************
139 static void TASK_UnlinkTask( HTASK16 hTask
)
144 prevTask
= &hFirstTask
;
145 while (*prevTask
&& (*prevTask
!= hTask
))
147 pTask
= TASK_GetPtr( *prevTask
);
148 prevTask
= &pTask
->hNext
;
152 pTask
= TASK_GetPtr( *prevTask
);
153 *prevTask
= pTask
->hNext
;
160 /***********************************************************************
163 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
164 * and containing 'count' entries.
166 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
172 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
174 pThunk
->magic
= THUNK_MAGIC
;
175 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
177 for (i
= 0; i
< count
-1; i
++)
179 free
+= 8; /* Offset of next thunk */
180 pThunk
->thunks
[4*i
] = free
;
182 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
186 /***********************************************************************
189 * Allocate a thunk for MakeProcInstance().
191 static SEGPTR
TASK_AllocThunk(void)
197 if (!(pTask
= TASK_GetCurrent())) return 0;
198 sel
= pTask
->hCSAlias
;
199 pThunk
= &pTask
->thunks
;
200 base
= (int)pThunk
- (int)pTask
;
201 while (!pThunk
->free
)
204 if (!sel
) /* Allocate a new segment */
206 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
207 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
208 if (!sel
) return (SEGPTR
)0;
209 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
212 pThunk
= (THUNKS
*)GlobalLock16( sel
);
215 base
+= pThunk
->free
;
216 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
217 return MAKESEGPTR( sel
, base
);
221 /***********************************************************************
224 * Free a MakeProcInstance() thunk.
226 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
232 if (!(pTask
= TASK_GetCurrent())) return 0;
233 sel
= pTask
->hCSAlias
;
234 pThunk
= &pTask
->thunks
;
235 base
= (int)pThunk
- (int)pTask
;
236 while (sel
&& (sel
!= HIWORD(thunk
)))
239 pThunk
= (THUNKS
*)GlobalLock16( sel
);
242 if (!sel
) return FALSE
;
243 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
244 pThunk
->free
= LOWORD(thunk
) - base
;
249 /***********************************************************************
252 * NOTE: This routine might be called by a Win32 thread. Thus, we need
253 * to be careful to protect global data structures. We do this
254 * by entering the Win16Lock while linking the task into the
257 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
262 HMODULE16 hModule
= pModule
? pModule
->self
: 0;
264 /* Allocate the task structure */
266 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
267 if (!hTask
) return NULL
;
268 pTask
= TASK_GetPtr( hTask
);
269 FarSetOwner16( hTask
, hModule
);
271 /* Fill the task structure */
273 pTask
->hSelf
= hTask
;
275 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
277 pTask
->flags
|= TDBF_WIN32
;
278 pTask
->hInstance
= hModule
;
279 pTask
->hPrevInstance
= 0;
280 /* NOTE: for 16-bit tasks, the instance handles are updated later on
284 pTask
->version
= pModule
? pModule
->expected_version
: 0x0400;
285 pTask
->hModule
= hModule
;
286 pTask
->hParent
= GetCurrentTask();
287 pTask
->magic
= TDB_MAGIC
;
288 pTask
->nCmdShow
= cmdShow
;
290 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
291 strcpy( pTask
->curdir
, "\\" );
292 WideCharToMultiByte(CP_ACP
, 0, DRIVE_GetDosCwd(DRIVE_GetCurrentDrive()), -1,
293 pTask
->curdir
+ 1, sizeof(pTask
->curdir
) - 1, NULL
, NULL
);
294 pTask
->curdir
[sizeof(pTask
->curdir
) - 1] = 0; /* ensure 0 termination */
296 /* Create the thunks block */
298 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
300 /* Copy the module name */
305 GetModuleName16( hModule
, name
, sizeof(name
) );
306 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
307 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
310 /* Allocate a selector for the PDB */
312 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
313 hModule
, WINE_LDT_FLAGS_DATA
);
317 pTask
->pdb
.int20
= 0x20cd;
318 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
319 proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
320 memcpy( &pTask
->pdb
.dispatcher
[1], &proc
, sizeof(proc
) );
321 pTask
->pdb
.savedint22
= 0;
322 pTask
->pdb
.savedint23
= 0;
323 pTask
->pdb
.savedint24
= 0;
324 pTask
->pdb
.fileHandlesPtr
=
325 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
326 pTask
->pdb
.hFileHandles
= 0;
327 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
328 /* FIXME: should we make a copy of the environment? */
329 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
330 pTask
->pdb
.nbFiles
= 20;
332 /* Fill the command line */
336 cmdline
= GetCommandLineA();
337 /* remove the first word (program name) */
339 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
340 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
341 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
342 len
= strlen(cmdline
);
344 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
345 pTask
->pdb
.cmdLine
[0] = len
;
346 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
347 /* pTask->pdb.cmdLine[len+1] = 0; */
349 TRACE("cmdline='%.*s' task=%04x\n", len
, cmdline
, hTask
);
351 /* Allocate a code segment alias for the TDB */
353 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
354 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
356 /* Default DTA overwrites command line */
358 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
360 /* Create scheduler event for 16-bit tasks */
362 if ( !(pTask
->flags
& TDBF_WIN32
) )
363 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
365 /* Enter task handle into thread */
367 if (teb
) teb
->htask16
= hTask
;
368 if (!initial_task
) initial_task
= hTask
;
374 /***********************************************************************
377 static void TASK_DeleteTask( HTASK16 hTask
)
382 if (!(pTask
= TASK_GetPtr( hTask
))) return;
385 pTask
->magic
= 0xdead; /* invalidate signature */
387 /* Free the selector aliases */
389 GLOBAL_FreeBlock( pTask
->hCSAlias
);
390 GLOBAL_FreeBlock( pTask
->hPDB
);
392 /* Free the task module */
394 FreeModule16( pTask
->hModule
);
396 /* Free the task structure itself */
398 GlobalFree16( hTask
);
400 /* Free all memory used by this task (including the 32-bit stack, */
401 /* the environment block and the thunk segments). */
403 GlobalFreeAll16( hPDB
);
407 /***********************************************************************
408 * TASK_CreateMainTask
410 * Create a task for the main (32-bit) process.
412 void TASK_CreateMainTask(void)
415 STARTUPINFOA startup_info
;
416 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
418 GetStartupInfoA( &startup_info
);
419 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
420 pTask
= TASK_Create( NULL
, cmdShow
, NtCurrentTeb(), NULL
, 0 );
423 ERR("could not create task for main process\n");
427 /* Add the task to the linked list */
428 /* (no need to get the win16 lock, we are the only thread at this point) */
429 TASK_LinkTask( pTask
->hSelf
);
433 /* startup routine for a new 16-bit thread */
434 static DWORD CALLBACK
task_start( TDB
*pTask
)
438 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
439 NtCurrentTeb()->htask16
= pTask
->hSelf
;
442 TASK_LinkTask( pTask
->hSelf
);
443 pTask
->teb
= NtCurrentTeb();
444 ret
= NE_StartTask();
450 /***********************************************************************
453 * Spawn a new 16-bit task.
455 HTASK16
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
,
456 LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
460 if (!(pTask
= TASK_Create( pModule
, cmdShow
, NULL
, cmdline
, len
))) return 0;
461 if (!(*hThread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)task_start
, pTask
, 0, NULL
)))
463 TASK_DeleteTask( pTask
->hSelf
);
470 /***********************************************************************
473 void TASK_ExitTask(void)
478 /* Enter the Win16Lock to protect global data structures */
481 pTask
= TASK_GetCurrent();
488 TRACE("Killing task %04x\n", pTask
->hSelf
);
490 /* Perform USER cleanup */
492 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
494 /* Remove the task from the list to be sure we never switch back to it */
495 TASK_UnlinkTask( pTask
->hSelf
);
497 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
499 TRACE("this is the last task, exiting\n" );
505 TDB
* p
= TASK_GetPtr( hFirstTask
);
508 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
509 p
= TASK_GetPtr( p
->hNext
);
515 if ( hLockedTask
== pTask
->hSelf
)
518 TASK_DeleteTask( pTask
->hSelf
);
520 /* ... and completely release the Win16Lock, just in case. */
521 ReleaseThunkLock( &lockCount
);
525 /***********************************************************************
526 * ExitKernel (KERNEL.2)
528 * Clean-up everything and exit the Wine process.
530 void WINAPI
ExitKernel16(void)
532 WriteOutProfiles16();
533 TerminateProcess( GetCurrentProcess(), 0 );
537 /***********************************************************************
538 * InitTask (KERNEL.91)
540 * Called by the application startup code.
542 void WINAPI
InitTask16( CONTEXT86
*context
)
545 INSTANCEDATA
*pinstance
;
549 if (!(pTask
= TASK_GetCurrent())) return;
551 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
552 as some apps, notably Visual Basic apps, *modify* the heap/stack
553 size of the instance data segment before calling InitTask() */
555 /* Initialize the INSTANCEDATA structure */
556 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
557 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
558 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
559 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
560 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
562 /* Initialize the local heap */
563 if (LOWORD(context
->Ecx
))
564 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
566 /* Initialize implicitly loaded DLLs */
567 NE_InitializeDLLs( pTask
->hModule
);
568 NE_DllProcessAttach( pTask
->hModule
);
570 /* Registers on return are:
571 * ax 1 if OK, 0 on error
572 * cx stack limit in bytes
573 * dx cmdShow parameter
574 * si instance handle of the previous instance
575 * di instance handle of the new task
576 * es:bx pointer to command line inside PSP
578 * 0 (=%bp) is pushed on the stack
580 ptr
= stack16_push( sizeof(WORD
) );
581 *(WORD
*)MapSL(ptr
) = 0;
586 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
589 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
590 while ((*p
== ' ') || (*p
== '\t')) p
++;
591 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
593 context
->Ecx
= pinstance
->stacktop
;
594 context
->Edx
= pTask
->nCmdShow
;
595 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
596 context
->Edi
= (DWORD
)pTask
->hInstance
;
597 context
->SegEs
= (WORD
)pTask
->hPDB
;
601 /***********************************************************************
602 * WaitEvent (KERNEL.30)
604 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
608 if (!hTask
) hTask
= GetCurrentTask();
609 pTask
= TASK_GetPtr( hTask
);
611 if (pTask
->flags
& TDBF_WIN32
)
613 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
617 if (pTask
->nEvents
> 0)
623 if (pTask
->teb
== NtCurrentTeb())
627 NtResetEvent( pTask
->hEvent
, NULL
);
628 ReleaseThunkLock( &lockCount
);
629 SYSLEVEL_CheckNotLevel( 1 );
630 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
631 RestoreThunkLock( lockCount
);
632 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
634 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
640 /***********************************************************************
641 * PostEvent (KERNEL.31)
643 void WINAPI
PostEvent16( HTASK16 hTask
)
647 if (!hTask
) hTask
= GetCurrentTask();
648 if (!(pTask
= TASK_GetPtr( hTask
))) return;
650 if (pTask
->flags
& TDBF_WIN32
)
652 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
658 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
662 /***********************************************************************
663 * SetPriority (KERNEL.32)
665 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
670 if (!hTask
) hTask
= GetCurrentTask();
671 if (!(pTask
= TASK_GetPtr( hTask
))) return;
672 newpriority
= pTask
->priority
+ delta
;
673 if (newpriority
< -32) newpriority
= -32;
674 else if (newpriority
> 15) newpriority
= 15;
676 pTask
->priority
= newpriority
+ 1;
677 TASK_UnlinkTask( pTask
->hSelf
);
678 TASK_LinkTask( pTask
->hSelf
);
683 /***********************************************************************
684 * LockCurrentTask (KERNEL.33)
686 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
688 if (bLock
) hLockedTask
= GetCurrentTask();
689 else hLockedTask
= 0;
694 /***********************************************************************
695 * IsTaskLocked (KERNEL.122)
697 HTASK16 WINAPI
IsTaskLocked16(void)
703 /***********************************************************************
704 * OldYield (KERNEL.117)
706 void WINAPI
OldYield16(void)
710 ReleaseThunkLock(&count
);
711 RestoreThunkLock(count
);
714 /***********************************************************************
715 * WIN32_OldYield (KERNEL.447)
717 void WINAPI
WIN32_OldYield16(void)
721 ReleaseThunkLock(&count
);
722 RestoreThunkLock(count
);
725 /***********************************************************************
726 * DirectedYield (KERNEL.150)
728 void WINAPI
DirectedYield16( HTASK16 hTask
)
730 TDB
*pCurTask
= TASK_GetCurrent();
732 if (!pCurTask
|| (pCurTask
->flags
& TDBF_WIN32
)) OldYield16();
735 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
736 pCurTask
->hYieldTo
= hTask
;
738 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
742 /***********************************************************************
745 void WINAPI
Yield16(void)
747 TDB
*pCurTask
= TASK_GetCurrent();
749 if (pCurTask
) pCurTask
->hYieldTo
= 0;
750 if (pCurTask
&& pCurTask
->hQueue
)
752 HMODULE mod
= GetModuleHandleA( "user32.dll" );
755 FARPROC proc
= GetProcAddress( mod
, "UserYield16" );
766 /***********************************************************************
767 * KERNEL_490 (KERNEL.490)
769 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
771 if ( !someTask
) return 0;
773 FIXME("(%04x): stub\n", someTask
);
777 /***********************************************************************
778 * MakeProcInstance (KERNEL.51)
780 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
784 WORD hInstanceSelector
;
786 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
788 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
791 /* Win95 actually protects via SEH, but this is better for debugging */
792 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
796 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
798 && (hInstance
!= 0xffff) )
800 /* calling MPI with a foreign DSEG is invalid ! */
801 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
802 hInstance
,CURRENT_DS
);
805 /* Always use the DSEG that MPI was entered with.
806 * We used to set hInstance to GetTaskDS16(), but this should be wrong
807 * as CURRENT_DS provides the DSEG value we need.
808 * ("calling" DS, *not* "task" DS !) */
809 hInstanceSelector
= CURRENT_DS
;
810 hInstance
= GlobalHandle16(hInstanceSelector
);
812 /* no thunking for DLLs */
813 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
816 thunkaddr
= TASK_AllocThunk();
817 if (!thunkaddr
) return (FARPROC16
)0;
818 thunk
= MapSL( thunkaddr
);
819 lfunc
= MapSL( (SEGPTR
)func
);
821 TRACE("(%08lx,%04x): got thunk %08lx\n",
822 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
823 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
824 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
826 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
829 *thunk
++ = 0xb8; /* movw instance, %ax */
830 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
831 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
832 *thunk
++ = 0xea; /* ljmp func */
833 *(DWORD
*)thunk
= (DWORD
)func
;
834 return (FARPROC16
)thunkaddr
;
835 /* CX reg indicates if thunkaddr != NULL, implement if needed */
839 /***********************************************************************
840 * FreeProcInstance (KERNEL.52)
842 void WINAPI
FreeProcInstance16( FARPROC16 func
)
844 TRACE("(%08lx)\n", (DWORD
)func
);
845 TASK_FreeThunk( (SEGPTR
)func
);
848 /**********************************************************************
849 * TASK_GetCodeSegment
851 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
852 * and logical segment number of a given code segment.
854 * 'proc' either *is* already a pair of module handle and segment number,
855 * in which case there's nothing to do. Otherwise, it is a pointer to
856 * a function, and we need to retrieve the code segment. If the pointer
857 * happens to point to a thunk, we'll retrieve info about the code segment
858 * where the function pointed to by the thunk resides, not the thunk itself.
860 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
861 * the function the snoop code will return to ...
864 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
865 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
867 NE_MODULE
*pModule
= NULL
;
868 SEGTABLEENTRY
*pSeg
= NULL
;
871 /* Try pair of module handle / segment number */
872 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
873 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
875 segNr
= LOWORD( proc
);
876 if ( segNr
&& segNr
<= pModule
->seg_count
)
877 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
880 /* Try thunk or function */
883 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
886 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
887 selector
= thunk
[6] + (thunk
[7] << 8);
889 selector
= HIWORD( proc
);
891 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
892 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
895 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
896 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
899 if ( pModule
&& segNr
> pModule
->seg_count
)
903 /* Abort if segment not found */
905 if ( !pModule
|| !pSeg
)
908 /* Return segment data */
910 if ( ppModule
) *ppModule
= pModule
;
911 if ( ppSeg
) *ppSeg
= pSeg
;
912 if ( pSegNr
) *pSegNr
= segNr
;
917 /**********************************************************************
918 * GetCodeHandle (KERNEL.93)
920 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
924 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
930 /**********************************************************************
931 * GetCodeInfo (KERNEL.104)
933 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
939 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
942 /* Fill in segment information */
944 segInfo
->offSegment
= pSeg
->filepos
;
945 segInfo
->cbSegment
= pSeg
->size
;
946 segInfo
->flags
= pSeg
->flags
;
947 segInfo
->cbAlloc
= pSeg
->minsize
;
948 segInfo
->h
= pSeg
->hSeg
;
949 segInfo
->alignShift
= pModule
->alignment
;
951 if ( segNr
== pModule
->dgroup
)
952 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
954 /* Return module handle in %es */
956 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
962 /**********************************************************************
963 * DefineHandleTable (KERNEL.94)
965 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
967 FIXME("(%04x): stub ?\n", wOffset
);
972 /***********************************************************************
973 * SetTaskQueue (KERNEL.34)
975 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
980 if (!hTask
) hTask
= GetCurrentTask();
981 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
983 hPrev
= pTask
->hQueue
;
984 pTask
->hQueue
= hQueue
;
990 /***********************************************************************
991 * GetTaskQueue (KERNEL.35)
993 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
997 if (!hTask
) hTask
= GetCurrentTask();
998 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
999 return pTask
->hQueue
;
1002 /***********************************************************************
1003 * SetThreadQueue (KERNEL.463)
1005 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1007 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1008 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1012 teb
->queue
= hQueue
;
1014 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1015 SetTaskQueue16( teb
->htask16
, hQueue
);
1021 /***********************************************************************
1022 * GetThreadQueue (KERNEL.464)
1024 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1028 teb
= NtCurrentTeb();
1029 else if ( HIWORD(thread
) )
1030 teb
= THREAD_IdToTEB( thread
);
1031 else if ( IsTask16( (HTASK16
)thread
) )
1032 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1034 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1037 /***********************************************************************
1038 * SetFastQueue (KERNEL.624)
1040 VOID WINAPI
SetFastQueue16( DWORD thread
, HQUEUE16 hQueue
)
1044 teb
= NtCurrentTeb();
1045 else if ( HIWORD(thread
) )
1046 teb
= THREAD_IdToTEB( thread
);
1047 else if ( IsTask16( (HTASK16
)thread
) )
1048 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1050 if ( teb
) teb
->queue
= hQueue
;
1053 /***********************************************************************
1054 * GetFastQueue (KERNEL.625)
1056 HQUEUE16 WINAPI
GetFastQueue16( void )
1058 HQUEUE16 ret
= NtCurrentTeb()->queue
;
1060 if (!ret
) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1064 /***********************************************************************
1065 * SwitchStackTo (KERNEL.108)
1067 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1070 STACK16FRAME
*oldFrame
, *newFrame
;
1071 INSTANCEDATA
*pData
;
1074 if (!(pTask
= TASK_GetCurrent())) return;
1075 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1076 TRACE("old=%04x:%04x new=%04x:%04x\n",
1077 SELECTOROF( pTask
->teb
->cur_stack
),
1078 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1080 /* Save the old stack */
1082 oldFrame
= THREAD_STACK16( pTask
->teb
);
1083 /* pop frame + args and push bp */
1084 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1086 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1087 pData
->stacktop
= top
;
1088 pData
->stackmin
= ptr
;
1089 pData
->stackbottom
= ptr
;
1091 /* Switch to the new stack */
1093 /* Note: we need to take the 3 arguments into account; otherwise,
1094 * the stack will underflow upon return from this function.
1096 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1097 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1098 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1099 newFrame
= THREAD_STACK16( pTask
->teb
);
1101 /* Copy the stack frame and the local variables to the new stack */
1103 memmove( newFrame
, oldFrame
, copySize
);
1105 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1109 /***********************************************************************
1110 * SwitchStackBack (KERNEL.109)
1112 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1114 STACK16FRAME
*oldFrame
, *newFrame
;
1115 INSTANCEDATA
*pData
;
1117 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1119 if (!pData
->old_ss_sp
)
1121 WARN("No previous SwitchStackTo\n" );
1124 TRACE("restoring stack %04x:%04x\n",
1125 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1127 oldFrame
= CURRENT_STACK16
;
1129 /* Pop bp from the previous stack */
1131 context
->Ebp
= (context
->Ebp
& ~0xffff) | *(WORD
*)MapSL(pData
->old_ss_sp
);
1132 pData
->old_ss_sp
+= sizeof(WORD
);
1134 /* Switch back to the old stack */
1136 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1137 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1138 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1139 pData
->old_ss_sp
= 0;
1141 /* Build a stack frame for the return */
1143 newFrame
= CURRENT_STACK16
;
1144 newFrame
->frame32
= oldFrame
->frame32
;
1145 newFrame
->module_cs
= oldFrame
->module_cs
;
1146 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1147 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1151 /***********************************************************************
1152 * GetTaskQueueDS (KERNEL.118)
1154 void WINAPI
GetTaskQueueDS16(void)
1156 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1160 /***********************************************************************
1161 * GetTaskQueueES (KERNEL.119)
1163 void WINAPI
GetTaskQueueES16(void)
1165 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1169 /***********************************************************************
1170 * GetCurrentTask (KERNEL32.@)
1172 HTASK16 WINAPI
GetCurrentTask(void)
1174 return NtCurrentTeb()->htask16
;
1177 /***********************************************************************
1178 * GetCurrentTask (KERNEL.36)
1180 DWORD WINAPI
WIN16_GetCurrentTask(void)
1182 /* This is the version used by relay code; the first task is */
1183 /* returned in the high word of the result */
1184 return MAKELONG( GetCurrentTask(), hFirstTask
);
1188 /***********************************************************************
1189 * GetCurrentPDB (KERNEL.37)
1191 * UNDOC: returns PSP of KERNEL in high word
1193 DWORD WINAPI
GetCurrentPDB16(void)
1197 if (!(pTask
= TASK_GetCurrent())) return 0;
1198 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1202 /***********************************************************************
1203 * GetCurPID (KERNEL.157)
1205 DWORD WINAPI
GetCurPID16( DWORD unused
)
1211 /***********************************************************************
1212 * GetInstanceData (KERNEL.54)
1214 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1216 char *ptr
= (char *)GlobalLock16( instance
);
1217 if (!ptr
|| !len
) return 0;
1218 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1219 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1224 /***********************************************************************
1225 * GetExeVersion (KERNEL.105)
1227 WORD WINAPI
GetExeVersion16(void)
1231 if (!(pTask
= TASK_GetCurrent())) return 0;
1232 return pTask
->version
;
1236 /***********************************************************************
1237 * SetErrorMode (KERNEL.107)
1239 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1242 if (!(pTask
= TASK_GetCurrent())) return 0;
1243 pTask
->error_mode
= mode
;
1244 return SetErrorMode( mode
);
1248 /***********************************************************************
1249 * GetNumTasks (KERNEL.152)
1251 UINT16 WINAPI
GetNumTasks16(void)
1257 /***********************************************************************
1258 * GetTaskDS (KERNEL.155)
1260 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1261 * I don't think we need to bother with this.
1263 HINSTANCE16 WINAPI
GetTaskDS16(void)
1267 if (!(pTask
= TASK_GetCurrent())) return 0;
1268 return GlobalHandleToSel16(pTask
->hInstance
);
1271 /***********************************************************************
1272 * GetDummyModuleHandleDS (KERNEL.602)
1274 WORD WINAPI
GetDummyModuleHandleDS16(void)
1279 if (!(pTask
= TASK_GetCurrent())) return 0;
1280 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1281 selector
= GlobalHandleToSel16( pTask
->hModule
);
1282 CURRENT_DS
= selector
;
1286 /***********************************************************************
1287 * IsTask (KERNEL.320)
1289 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1293 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1294 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1295 return (pTask
->magic
== TDB_MAGIC
);
1299 /***********************************************************************
1300 * IsWinOldApTask (KERNEL.158)
1302 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1304 /* should return bit 0 of byte 0x48 in PSP */
1308 /***********************************************************************
1309 * SetTaskSignalProc (KERNEL.38)
1311 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1316 if (!hTask
) hTask
= GetCurrentTask();
1317 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1318 oldProc
= pTask
->userhandler
;
1319 pTask
->userhandler
= proc
;
1323 /***********************************************************************
1324 * TASK_CallTaskSignalProc
1326 /* ### start build ### */
1327 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1328 /* ### stop build ### */
1329 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1331 TDB
*pTask
= TASK_GetCurrent();
1332 if ( !pTask
|| !pTask
->userhandler
) return;
1334 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1335 hTaskOrModule
, uCode
, 0,
1336 pTask
->hInstance
, pTask
->hQueue
);
1339 /***********************************************************************
1340 * SetSigHandler (KERNEL.140)
1342 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1343 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1345 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1346 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1348 if (flag
!= 1) return 0;
1349 if (!newmode
) newhandler
= NULL
; /* Default handler */
1354 if (!(pTask
= TASK_GetCurrent())) return 0;
1355 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1356 pTask
->signal_flags
= newmode
;
1357 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1358 pTask
->sighandler
= newhandler
;
1364 /***********************************************************************
1365 * GlobalNotify (KERNEL.154)
1367 * Note that GlobalNotify does _not_ return the old NotifyProc
1368 * -- contrary to LocalNotify !!
1370 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1374 if (!(pTask
= TASK_GetCurrent())) return;
1375 pTask
->discardhandler
= proc
;
1379 /***********************************************************************
1382 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1387 /* Check for module handle */
1389 if (!(ptr
= GlobalLock16( handle
))) return 0;
1390 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1392 /* Search for this handle inside all tasks */
1394 *hTask
= hFirstTask
;
1397 TDB
*pTask
= TASK_GetPtr( *hTask
);
1398 if ((*hTask
== handle
) ||
1399 (pTask
->hInstance
== handle
) ||
1400 (pTask
->hQueue
== handle
) ||
1401 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1402 *hTask
= pTask
->hNext
;
1405 /* Check the owner for module handle */
1407 owner
= FarGetOwner16( handle
);
1408 if (!(ptr
= GlobalLock16( owner
))) return 0;
1409 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1411 /* Search for the owner inside all tasks */
1413 *hTask
= hFirstTask
;
1416 TDB
*pTask
= TASK_GetPtr( *hTask
);
1417 if ((*hTask
== owner
) ||
1418 (pTask
->hInstance
== owner
) ||
1419 (pTask
->hQueue
== owner
) ||
1420 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1421 *hTask
= pTask
->hNext
;
1427 /***********************************************************************
1428 * GetExePtr (KERNEL.133)
1430 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1433 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1434 STACK16FRAME
*frame
= CURRENT_STACK16
;
1435 frame
->ecx
= hModule
;
1436 if (hTask
) frame
->es
= hTask
;
1441 /***********************************************************************
1444 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1447 return GetExePtrHelper( handle
, &hTask
);
1451 /***********************************************************************
1452 * TaskFirst (TOOLHELP.63)
1454 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1456 lpte
->hNext
= hFirstTask
;
1457 return TaskNext16( lpte
);
1461 /***********************************************************************
1462 * TaskNext (TOOLHELP.64)
1464 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1467 INSTANCEDATA
*pInstData
;
1469 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1470 if (!lpte
->hNext
) return FALSE
;
1472 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1474 pTask
= TASK_GetPtr( lpte
->hNext
);
1475 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1476 if (pTask
->hInstance
)
1478 lpte
->hNext
= pTask
->hNext
;
1480 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1481 lpte
->hTask
= lpte
->hNext
;
1482 lpte
->hTaskParent
= pTask
->hParent
;
1483 lpte
->hInst
= pTask
->hInstance
;
1484 lpte
->hModule
= pTask
->hModule
;
1485 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1486 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1487 lpte
->wStackTop
= pInstData
->stacktop
;
1488 lpte
->wStackMinimum
= pInstData
->stackmin
;
1489 lpte
->wStackBottom
= pInstData
->stackbottom
;
1490 lpte
->wcEvents
= pTask
->nEvents
;
1491 lpte
->hQueue
= pTask
->hQueue
;
1492 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1493 lpte
->wPSPOffset
= 0x100; /*??*/
1494 lpte
->hNext
= pTask
->hNext
;
1499 /***********************************************************************
1500 * TaskFindHandle (TOOLHELP.65)
1502 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1504 lpte
->hNext
= hTask
;
1505 return TaskNext16( lpte
);
1509 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1511 /**************************************************************************
1512 * FatalAppExit (KERNEL.137)
1514 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1516 TDB
*pTask
= TASK_GetCurrent();
1518 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1520 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1523 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1526 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1530 ERR( "%s\n", debugstr_a(str
) );
1537 /***********************************************************************
1538 * TerminateApp (TOOLHELP.77)
1540 * See "Undocumented Windows".
1542 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1544 if (hTask
&& hTask
!= GetCurrentTask())
1546 FIXME("cannot terminate task %x\n", hTask
);
1550 if (wFlags
& NO_UAE_BOX
)
1553 old_mode
= SetErrorMode16(0);
1554 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1556 FatalAppExit16( 0, NULL
);
1558 /* hmm, we're still alive ?? */
1560 /* check undocumented flag */
1561 if (!(wFlags
& 0x8000))
1562 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1564 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1565 but let's just call ExitThread */
1570 /***********************************************************************
1571 * GetAppCompatFlags (KERNEL.354)
1573 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1577 if (!hTask
) hTask
= GetCurrentTask();
1578 if (!(pTask
=TASK_GetPtr( hTask
))) return 0;
1579 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1580 return pTask
->compat_flags
;