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
)
263 HMODULE16 hModule
= pModule
? pModule
->self
: 0;
265 /* Allocate the task structure */
267 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
268 if (!hTask
) return NULL
;
269 pTask
= TASK_GetPtr( hTask
);
270 FarSetOwner16( hTask
, hModule
);
272 /* Fill the task structure */
274 pTask
->hSelf
= hTask
;
276 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
278 pTask
->flags
|= TDBF_WIN32
;
279 pTask
->hInstance
= hModule
;
280 pTask
->hPrevInstance
= 0;
281 /* NOTE: for 16-bit tasks, the instance handles are updated later on
285 pTask
->version
= pModule
? pModule
->expected_version
: 0x0400;
286 pTask
->hModule
= hModule
;
287 pTask
->hParent
= GetCurrentTask();
288 pTask
->magic
= TDB_MAGIC
;
289 pTask
->nCmdShow
= cmdShow
;
291 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
292 strcpy( pTask
->curdir
, "\\" );
293 WideCharToMultiByte(CP_ACP
, 0, DRIVE_GetDosCwd(DRIVE_GetCurrentDrive()), -1,
294 pTask
->curdir
+ 1, sizeof(pTask
->curdir
) - 1, NULL
, NULL
);
295 pTask
->curdir
[sizeof(pTask
->curdir
) - 1] = 0; /* ensure 0 termination */
297 /* Create the thunks block */
299 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
301 /* Copy the module name */
305 GetModuleName16( hModule
, name
, sizeof(name
) );
306 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
309 /* Allocate a selector for the PDB */
311 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
312 hModule
, WINE_LDT_FLAGS_DATA
);
316 pTask
->pdb
.int20
= 0x20cd;
317 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
318 proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
319 memcpy( &pTask
->pdb
.dispatcher
[1], &proc
, sizeof(proc
) );
320 pTask
->pdb
.savedint22
= 0;
321 pTask
->pdb
.savedint23
= 0;
322 pTask
->pdb
.savedint24
= 0;
323 pTask
->pdb
.fileHandlesPtr
=
324 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
325 pTask
->pdb
.hFileHandles
= 0;
326 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
327 /* FIXME: should we make a copy of the environment? */
328 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
329 pTask
->pdb
.nbFiles
= 20;
331 /* Fill the command line */
335 cmdline
= GetCommandLineA();
336 /* remove the first word (program name) */
338 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
339 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
340 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
341 len
= strlen(cmdline
);
343 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
344 pTask
->pdb
.cmdLine
[0] = len
;
345 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
346 /* pTask->pdb.cmdLine[len+1] = 0; */
348 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
350 /* Get the compatibility flags */
352 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
354 /* Allocate a code segment alias for the TDB */
356 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
357 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
359 /* Default DTA overwrites command line */
361 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
363 /* Create scheduler event for 16-bit tasks */
365 if ( !(pTask
->flags
& TDBF_WIN32
) )
366 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
368 /* Enter task handle into thread */
370 if (teb
) teb
->htask16
= hTask
;
371 if (!initial_task
) initial_task
= hTask
;
377 /***********************************************************************
380 static void TASK_DeleteTask( HTASK16 hTask
)
385 if (!(pTask
= TASK_GetPtr( hTask
))) return;
388 pTask
->magic
= 0xdead; /* invalidate signature */
390 /* Free the selector aliases */
392 GLOBAL_FreeBlock( pTask
->hCSAlias
);
393 GLOBAL_FreeBlock( pTask
->hPDB
);
395 /* Free the task module */
397 FreeModule16( pTask
->hModule
);
399 /* Free the task structure itself */
401 GlobalFree16( hTask
);
403 /* Free all memory used by this task (including the 32-bit stack, */
404 /* the environment block and the thunk segments). */
406 GlobalFreeAll16( hPDB
);
410 /***********************************************************************
411 * TASK_CreateMainTask
413 * Create a task for the main (32-bit) process.
415 void TASK_CreateMainTask(void)
418 STARTUPINFOA startup_info
;
419 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
421 GetStartupInfoA( &startup_info
);
422 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
423 pTask
= TASK_Create( NULL
, cmdShow
, NtCurrentTeb(), NULL
, 0 );
426 ERR("could not create task for main process\n");
430 /* Add the task to the linked list */
431 /* (no need to get the win16 lock, we are the only thread at this point) */
432 TASK_LinkTask( pTask
->hSelf
);
436 /* startup routine for a new 16-bit thread */
437 static DWORD CALLBACK
task_start( TDB
*pTask
)
441 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
442 NtCurrentTeb()->htask16
= pTask
->hSelf
;
445 TASK_LinkTask( pTask
->hSelf
);
446 pTask
->teb
= NtCurrentTeb();
447 ret
= NE_StartTask();
453 /***********************************************************************
456 * Spawn a new 16-bit task.
458 HTASK16
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
,
459 LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
463 if (!(pTask
= TASK_Create( pModule
, cmdShow
, NULL
, cmdline
, len
))) return 0;
464 if (!(*hThread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)task_start
, pTask
, 0, NULL
)))
466 TASK_DeleteTask( pTask
->hSelf
);
473 /***********************************************************************
476 void TASK_ExitTask(void)
481 /* Enter the Win16Lock to protect global data structures */
484 pTask
= TASK_GetCurrent();
491 TRACE("Killing task %04x\n", pTask
->hSelf
);
493 /* Perform USER cleanup */
495 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
497 /* Remove the task from the list to be sure we never switch back to it */
498 TASK_UnlinkTask( pTask
->hSelf
);
500 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
502 TRACE("this is the last task, exiting\n" );
508 TDB
* p
= TASK_GetPtr( hFirstTask
);
511 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
512 p
= TASK_GetPtr( p
->hNext
);
518 if ( hLockedTask
== pTask
->hSelf
)
521 TASK_DeleteTask( pTask
->hSelf
);
523 /* ... and completely release the Win16Lock, just in case. */
524 ReleaseThunkLock( &lockCount
);
528 /***********************************************************************
529 * ExitKernel (KERNEL.2)
531 * Clean-up everything and exit the Wine process.
533 void WINAPI
ExitKernel16(void)
535 WriteOutProfiles16();
536 TerminateProcess( GetCurrentProcess(), 0 );
540 /***********************************************************************
541 * InitTask (KERNEL.91)
543 * Called by the application startup code.
545 void WINAPI
InitTask16( CONTEXT86
*context
)
548 INSTANCEDATA
*pinstance
;
552 if (!(pTask
= TASK_GetCurrent())) return;
554 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
555 as some apps, notably Visual Basic apps, *modify* the heap/stack
556 size of the instance data segment before calling InitTask() */
558 /* Initialize the INSTANCEDATA structure */
559 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
560 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
561 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
562 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
563 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
565 /* Initialize the local heap */
566 if (LOWORD(context
->Ecx
))
567 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
569 /* Initialize implicitly loaded DLLs */
570 NE_InitializeDLLs( pTask
->hModule
);
571 NE_DllProcessAttach( pTask
->hModule
);
573 /* Registers on return are:
574 * ax 1 if OK, 0 on error
575 * cx stack limit in bytes
576 * dx cmdShow parameter
577 * si instance handle of the previous instance
578 * di instance handle of the new task
579 * es:bx pointer to command line inside PSP
581 * 0 (=%bp) is pushed on the stack
583 ptr
= stack16_push( sizeof(WORD
) );
584 *(WORD
*)MapSL(ptr
) = 0;
589 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
592 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
593 while ((*p
== ' ') || (*p
== '\t')) p
++;
594 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
596 context
->Ecx
= pinstance
->stacktop
;
597 context
->Edx
= pTask
->nCmdShow
;
598 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
599 context
->Edi
= (DWORD
)pTask
->hInstance
;
600 context
->SegEs
= (WORD
)pTask
->hPDB
;
604 /***********************************************************************
605 * WaitEvent (KERNEL.30)
607 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
611 if (!hTask
) hTask
= GetCurrentTask();
612 pTask
= TASK_GetPtr( hTask
);
614 if (pTask
->flags
& TDBF_WIN32
)
616 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
620 if (pTask
->nEvents
> 0)
626 if (pTask
->teb
== NtCurrentTeb())
630 NtResetEvent( pTask
->hEvent
, NULL
);
631 ReleaseThunkLock( &lockCount
);
632 SYSLEVEL_CheckNotLevel( 1 );
633 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
634 RestoreThunkLock( lockCount
);
635 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
637 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
643 /***********************************************************************
644 * PostEvent (KERNEL.31)
646 void WINAPI
PostEvent16( HTASK16 hTask
)
650 if (!hTask
) hTask
= GetCurrentTask();
651 if (!(pTask
= TASK_GetPtr( hTask
))) return;
653 if (pTask
->flags
& TDBF_WIN32
)
655 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
661 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
665 /***********************************************************************
666 * SetPriority (KERNEL.32)
668 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
673 if (!hTask
) hTask
= GetCurrentTask();
674 if (!(pTask
= TASK_GetPtr( hTask
))) return;
675 newpriority
= pTask
->priority
+ delta
;
676 if (newpriority
< -32) newpriority
= -32;
677 else if (newpriority
> 15) newpriority
= 15;
679 pTask
->priority
= newpriority
+ 1;
680 TASK_UnlinkTask( pTask
->hSelf
);
681 TASK_LinkTask( pTask
->hSelf
);
686 /***********************************************************************
687 * LockCurrentTask (KERNEL.33)
689 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
691 if (bLock
) hLockedTask
= GetCurrentTask();
692 else hLockedTask
= 0;
697 /***********************************************************************
698 * IsTaskLocked (KERNEL.122)
700 HTASK16 WINAPI
IsTaskLocked16(void)
706 /***********************************************************************
707 * OldYield (KERNEL.117)
709 void WINAPI
OldYield16(void)
713 ReleaseThunkLock(&count
);
714 RestoreThunkLock(count
);
717 /***********************************************************************
718 * WIN32_OldYield (KERNEL.447)
720 void WINAPI
WIN32_OldYield16(void)
724 ReleaseThunkLock(&count
);
725 RestoreThunkLock(count
);
728 /***********************************************************************
729 * DirectedYield (KERNEL.150)
731 void WINAPI
DirectedYield16( HTASK16 hTask
)
733 TDB
*pCurTask
= TASK_GetCurrent();
735 if (!pCurTask
|| (pCurTask
->flags
& TDBF_WIN32
)) OldYield16();
738 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
739 pCurTask
->hYieldTo
= hTask
;
741 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
745 /***********************************************************************
748 void WINAPI
Yield16(void)
750 TDB
*pCurTask
= TASK_GetCurrent();
752 if (pCurTask
) pCurTask
->hYieldTo
= 0;
753 if (pCurTask
&& pCurTask
->hQueue
)
755 HMODULE mod
= GetModuleHandleA( "user32.dll" );
758 FARPROC proc
= GetProcAddress( mod
, "UserYield16" );
769 /***********************************************************************
770 * KERNEL_490 (KERNEL.490)
772 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
774 if ( !someTask
) return 0;
776 FIXME("(%04x): stub\n", someTask
);
780 /***********************************************************************
781 * MakeProcInstance (KERNEL.51)
783 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
787 WORD hInstanceSelector
;
789 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
791 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
794 /* Win95 actually protects via SEH, but this is better for debugging */
795 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
799 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
801 && (hInstance
!= 0xffff) )
803 /* calling MPI with a foreign DSEG is invalid ! */
804 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
805 hInstance
,CURRENT_DS
);
808 /* Always use the DSEG that MPI was entered with.
809 * We used to set hInstance to GetTaskDS16(), but this should be wrong
810 * as CURRENT_DS provides the DSEG value we need.
811 * ("calling" DS, *not* "task" DS !) */
812 hInstanceSelector
= CURRENT_DS
;
813 hInstance
= GlobalHandle16(hInstanceSelector
);
815 /* no thunking for DLLs */
816 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
819 thunkaddr
= TASK_AllocThunk();
820 if (!thunkaddr
) return (FARPROC16
)0;
821 thunk
= MapSL( thunkaddr
);
822 lfunc
= MapSL( (SEGPTR
)func
);
824 TRACE("(%08lx,%04x): got thunk %08lx\n",
825 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
826 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
827 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
829 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
832 *thunk
++ = 0xb8; /* movw instance, %ax */
833 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
834 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
835 *thunk
++ = 0xea; /* ljmp func */
836 *(DWORD
*)thunk
= (DWORD
)func
;
837 return (FARPROC16
)thunkaddr
;
838 /* CX reg indicates if thunkaddr != NULL, implement if needed */
842 /***********************************************************************
843 * FreeProcInstance (KERNEL.52)
845 void WINAPI
FreeProcInstance16( FARPROC16 func
)
847 TRACE("(%08lx)\n", (DWORD
)func
);
848 TASK_FreeThunk( (SEGPTR
)func
);
851 /**********************************************************************
852 * TASK_GetCodeSegment
854 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
855 * and logical segment number of a given code segment.
857 * 'proc' either *is* already a pair of module handle and segment number,
858 * in which case there's nothing to do. Otherwise, it is a pointer to
859 * a function, and we need to retrieve the code segment. If the pointer
860 * happens to point to a thunk, we'll retrieve info about the code segment
861 * where the function pointed to by the thunk resides, not the thunk itself.
863 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
864 * the function the snoop code will return to ...
867 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
868 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
870 NE_MODULE
*pModule
= NULL
;
871 SEGTABLEENTRY
*pSeg
= NULL
;
874 /* Try pair of module handle / segment number */
875 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
876 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
878 segNr
= LOWORD( proc
);
879 if ( segNr
&& segNr
<= pModule
->seg_count
)
880 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
883 /* Try thunk or function */
886 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
889 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
890 selector
= thunk
[6] + (thunk
[7] << 8);
892 selector
= HIWORD( proc
);
894 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
895 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
898 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
899 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
902 if ( pModule
&& segNr
> pModule
->seg_count
)
906 /* Abort if segment not found */
908 if ( !pModule
|| !pSeg
)
911 /* Return segment data */
913 if ( ppModule
) *ppModule
= pModule
;
914 if ( ppSeg
) *ppSeg
= pSeg
;
915 if ( pSegNr
) *pSegNr
= segNr
;
920 /**********************************************************************
921 * GetCodeHandle (KERNEL.93)
923 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
927 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
933 /**********************************************************************
934 * GetCodeInfo (KERNEL.104)
936 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
942 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
945 /* Fill in segment information */
947 segInfo
->offSegment
= pSeg
->filepos
;
948 segInfo
->cbSegment
= pSeg
->size
;
949 segInfo
->flags
= pSeg
->flags
;
950 segInfo
->cbAlloc
= pSeg
->minsize
;
951 segInfo
->h
= pSeg
->hSeg
;
952 segInfo
->alignShift
= pModule
->alignment
;
954 if ( segNr
== pModule
->dgroup
)
955 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
957 /* Return module handle in %es */
959 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
965 /**********************************************************************
966 * DefineHandleTable (KERNEL.94)
968 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
970 FIXME("(%04x): stub ?\n", wOffset
);
975 /***********************************************************************
976 * SetTaskQueue (KERNEL.34)
978 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
983 if (!hTask
) hTask
= GetCurrentTask();
984 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
986 hPrev
= pTask
->hQueue
;
987 pTask
->hQueue
= hQueue
;
993 /***********************************************************************
994 * GetTaskQueue (KERNEL.35)
996 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1000 if (!hTask
) hTask
= GetCurrentTask();
1001 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
1002 return pTask
->hQueue
;
1005 /***********************************************************************
1006 * SetThreadQueue (KERNEL.463)
1008 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1010 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1011 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1015 teb
->queue
= hQueue
;
1017 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1018 SetTaskQueue16( teb
->htask16
, hQueue
);
1024 /***********************************************************************
1025 * GetThreadQueue (KERNEL.464)
1027 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1031 teb
= NtCurrentTeb();
1032 else if ( HIWORD(thread
) )
1033 teb
= THREAD_IdToTEB( thread
);
1034 else if ( IsTask16( (HTASK16
)thread
) )
1035 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1037 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1040 /***********************************************************************
1041 * SetFastQueue (KERNEL.624)
1043 VOID WINAPI
SetFastQueue16( DWORD thread
, HQUEUE16 hQueue
)
1047 teb
= NtCurrentTeb();
1048 else if ( HIWORD(thread
) )
1049 teb
= THREAD_IdToTEB( thread
);
1050 else if ( IsTask16( (HTASK16
)thread
) )
1051 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1053 if ( teb
) teb
->queue
= hQueue
;
1056 /***********************************************************************
1057 * GetFastQueue (KERNEL.625)
1059 HQUEUE16 WINAPI
GetFastQueue16( void )
1061 HQUEUE16 ret
= NtCurrentTeb()->queue
;
1063 if (!ret
) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1067 /***********************************************************************
1068 * SwitchStackTo (KERNEL.108)
1070 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1073 STACK16FRAME
*oldFrame
, *newFrame
;
1074 INSTANCEDATA
*pData
;
1077 if (!(pTask
= TASK_GetCurrent())) return;
1078 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1079 TRACE("old=%04x:%04x new=%04x:%04x\n",
1080 SELECTOROF( pTask
->teb
->cur_stack
),
1081 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1083 /* Save the old stack */
1085 oldFrame
= THREAD_STACK16( pTask
->teb
);
1086 /* pop frame + args and push bp */
1087 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1089 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1090 pData
->stacktop
= top
;
1091 pData
->stackmin
= ptr
;
1092 pData
->stackbottom
= ptr
;
1094 /* Switch to the new stack */
1096 /* Note: we need to take the 3 arguments into account; otherwise,
1097 * the stack will underflow upon return from this function.
1099 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1100 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1101 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1102 newFrame
= THREAD_STACK16( pTask
->teb
);
1104 /* Copy the stack frame and the local variables to the new stack */
1106 memmove( newFrame
, oldFrame
, copySize
);
1108 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1112 /***********************************************************************
1113 * SwitchStackBack (KERNEL.109)
1115 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1117 STACK16FRAME
*oldFrame
, *newFrame
;
1118 INSTANCEDATA
*pData
;
1120 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1122 if (!pData
->old_ss_sp
)
1124 WARN("No previous SwitchStackTo\n" );
1127 TRACE("restoring stack %04x:%04x\n",
1128 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1130 oldFrame
= CURRENT_STACK16
;
1132 /* Pop bp from the previous stack */
1134 context
->Ebp
= (context
->Ebp
& ~0xffff) | *(WORD
*)MapSL(pData
->old_ss_sp
);
1135 pData
->old_ss_sp
+= sizeof(WORD
);
1137 /* Switch back to the old stack */
1139 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1140 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1141 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1142 pData
->old_ss_sp
= 0;
1144 /* Build a stack frame for the return */
1146 newFrame
= CURRENT_STACK16
;
1147 newFrame
->frame32
= oldFrame
->frame32
;
1148 newFrame
->module_cs
= oldFrame
->module_cs
;
1149 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1150 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1154 /***********************************************************************
1155 * GetTaskQueueDS (KERNEL.118)
1157 void WINAPI
GetTaskQueueDS16(void)
1159 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1163 /***********************************************************************
1164 * GetTaskQueueES (KERNEL.119)
1166 void WINAPI
GetTaskQueueES16(void)
1168 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1172 /***********************************************************************
1173 * GetCurrentTask (KERNEL32.@)
1175 HTASK16 WINAPI
GetCurrentTask(void)
1177 return NtCurrentTeb()->htask16
;
1180 /***********************************************************************
1181 * GetCurrentTask (KERNEL.36)
1183 DWORD WINAPI
WIN16_GetCurrentTask(void)
1185 /* This is the version used by relay code; the first task is */
1186 /* returned in the high word of the result */
1187 return MAKELONG( GetCurrentTask(), hFirstTask
);
1191 /***********************************************************************
1192 * GetCurrentPDB (KERNEL.37)
1194 * UNDOC: returns PSP of KERNEL in high word
1196 DWORD WINAPI
GetCurrentPDB16(void)
1200 if (!(pTask
= TASK_GetCurrent())) return 0;
1201 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1205 /***********************************************************************
1206 * GetCurPID (KERNEL.157)
1208 DWORD WINAPI
GetCurPID16( DWORD unused
)
1214 /***********************************************************************
1215 * GetInstanceData (KERNEL.54)
1217 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1219 char *ptr
= (char *)GlobalLock16( instance
);
1220 if (!ptr
|| !len
) return 0;
1221 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1222 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1227 /***********************************************************************
1228 * GetExeVersion (KERNEL.105)
1230 WORD WINAPI
GetExeVersion16(void)
1234 if (!(pTask
= TASK_GetCurrent())) return 0;
1235 return pTask
->version
;
1239 /***********************************************************************
1240 * SetErrorMode (KERNEL.107)
1242 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1245 if (!(pTask
= TASK_GetCurrent())) return 0;
1246 pTask
->error_mode
= mode
;
1247 return SetErrorMode( mode
);
1251 /***********************************************************************
1252 * GetNumTasks (KERNEL.152)
1254 UINT16 WINAPI
GetNumTasks16(void)
1260 /***********************************************************************
1261 * GetTaskDS (KERNEL.155)
1263 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1264 * I don't think we need to bother with this.
1266 HINSTANCE16 WINAPI
GetTaskDS16(void)
1270 if (!(pTask
= TASK_GetCurrent())) return 0;
1271 return GlobalHandleToSel16(pTask
->hInstance
);
1274 /***********************************************************************
1275 * GetDummyModuleHandleDS (KERNEL.602)
1277 WORD WINAPI
GetDummyModuleHandleDS16(void)
1282 if (!(pTask
= TASK_GetCurrent())) return 0;
1283 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1284 selector
= GlobalHandleToSel16( pTask
->hModule
);
1285 CURRENT_DS
= selector
;
1289 /***********************************************************************
1290 * IsTask (KERNEL.320)
1292 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1296 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1297 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1298 return (pTask
->magic
== TDB_MAGIC
);
1302 /***********************************************************************
1303 * IsWinOldApTask (KERNEL.158)
1305 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1307 /* should return bit 0 of byte 0x48 in PSP */
1311 /***********************************************************************
1312 * SetTaskSignalProc (KERNEL.38)
1314 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1319 if (!hTask
) hTask
= GetCurrentTask();
1320 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1321 oldProc
= pTask
->userhandler
;
1322 pTask
->userhandler
= proc
;
1326 /***********************************************************************
1327 * TASK_CallTaskSignalProc
1329 /* ### start build ### */
1330 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1331 /* ### stop build ### */
1332 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1334 TDB
*pTask
= TASK_GetCurrent();
1335 if ( !pTask
|| !pTask
->userhandler
) return;
1337 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1338 hTaskOrModule
, uCode
, 0,
1339 pTask
->hInstance
, pTask
->hQueue
);
1342 /***********************************************************************
1343 * SetSigHandler (KERNEL.140)
1345 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1346 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1348 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1349 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1351 if (flag
!= 1) return 0;
1352 if (!newmode
) newhandler
= NULL
; /* Default handler */
1357 if (!(pTask
= TASK_GetCurrent())) return 0;
1358 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1359 pTask
->signal_flags
= newmode
;
1360 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1361 pTask
->sighandler
= newhandler
;
1367 /***********************************************************************
1368 * GlobalNotify (KERNEL.154)
1370 * Note that GlobalNotify does _not_ return the old NotifyProc
1371 * -- contrary to LocalNotify !!
1373 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1377 if (!(pTask
= TASK_GetCurrent())) return;
1378 pTask
->discardhandler
= proc
;
1382 /***********************************************************************
1385 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1390 /* Check for module handle */
1392 if (!(ptr
= GlobalLock16( handle
))) return 0;
1393 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1395 /* Search for this handle inside all tasks */
1397 *hTask
= hFirstTask
;
1400 TDB
*pTask
= TASK_GetPtr( *hTask
);
1401 if ((*hTask
== handle
) ||
1402 (pTask
->hInstance
== handle
) ||
1403 (pTask
->hQueue
== handle
) ||
1404 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1405 *hTask
= pTask
->hNext
;
1408 /* Check the owner for module handle */
1410 owner
= FarGetOwner16( handle
);
1411 if (!(ptr
= GlobalLock16( owner
))) return 0;
1412 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1414 /* Search for the owner inside all tasks */
1416 *hTask
= hFirstTask
;
1419 TDB
*pTask
= TASK_GetPtr( *hTask
);
1420 if ((*hTask
== owner
) ||
1421 (pTask
->hInstance
== owner
) ||
1422 (pTask
->hQueue
== owner
) ||
1423 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1424 *hTask
= pTask
->hNext
;
1430 /***********************************************************************
1431 * GetExePtr (KERNEL.133)
1433 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1436 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1437 STACK16FRAME
*frame
= CURRENT_STACK16
;
1438 frame
->ecx
= hModule
;
1439 if (hTask
) frame
->es
= hTask
;
1444 /***********************************************************************
1447 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1450 return GetExePtrHelper( handle
, &hTask
);
1454 /***********************************************************************
1455 * TaskFirst (TOOLHELP.63)
1457 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1459 lpte
->hNext
= hFirstTask
;
1460 return TaskNext16( lpte
);
1464 /***********************************************************************
1465 * TaskNext (TOOLHELP.64)
1467 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1470 INSTANCEDATA
*pInstData
;
1472 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1473 if (!lpte
->hNext
) return FALSE
;
1475 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1477 pTask
= TASK_GetPtr( lpte
->hNext
);
1478 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1479 if (pTask
->hInstance
)
1481 lpte
->hNext
= pTask
->hNext
;
1483 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1484 lpte
->hTask
= lpte
->hNext
;
1485 lpte
->hTaskParent
= pTask
->hParent
;
1486 lpte
->hInst
= pTask
->hInstance
;
1487 lpte
->hModule
= pTask
->hModule
;
1488 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1489 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1490 lpte
->wStackTop
= pInstData
->stacktop
;
1491 lpte
->wStackMinimum
= pInstData
->stackmin
;
1492 lpte
->wStackBottom
= pInstData
->stackbottom
;
1493 lpte
->wcEvents
= pTask
->nEvents
;
1494 lpte
->hQueue
= pTask
->hQueue
;
1495 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1496 lpte
->wPSPOffset
= 0x100; /*??*/
1497 lpte
->hNext
= pTask
->hNext
;
1502 /***********************************************************************
1503 * TaskFindHandle (TOOLHELP.65)
1505 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1507 lpte
->hNext
= hTask
;
1508 return TaskNext16( lpte
);
1512 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1514 /**************************************************************************
1515 * FatalAppExit (KERNEL.137)
1517 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1519 TDB
*pTask
= TASK_GetCurrent();
1521 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1523 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1526 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1529 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1533 ERR( "%s\n", debugstr_a(str
) );
1540 /***********************************************************************
1541 * TerminateApp (TOOLHELP.77)
1543 * See "Undocumented Windows".
1545 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1547 if (hTask
&& hTask
!= GetCurrentTask())
1549 FIXME("cannot terminate task %x\n", hTask
);
1553 if (wFlags
& NO_UAE_BOX
)
1556 old_mode
= SetErrorMode16(0);
1557 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1559 FatalAppExit16( 0, NULL
);
1561 /* hmm, we're still alive ?? */
1563 /* check undocumented flag */
1564 if (!(wFlags
& 0x8000))
1565 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1567 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1568 but let's just call ExitThread */
1573 /***********************************************************************
1574 * GetAppCompatFlags (KERNEL.354)
1576 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1580 if (!hTask
) hTask
= GetCurrentTask();
1581 if (!(pTask
=TASK_GetPtr( hTask
))) return 0;
1582 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1583 return pTask
->compat_flags
;