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"
34 #include "wine/winbase16.h"
42 #include "selectors.h"
43 #include "wine/server.h"
45 #include "stackframe.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(task
);
53 WINE_DECLARE_DEBUG_CHANNEL(relay
);
54 WINE_DECLARE_DEBUG_CHANNEL(toolhelp
);
56 /* Min. number of thunks allocated when creating a new segment */
60 static THHOOK DefaultThhook
;
61 THHOOK
*pThhook
= &DefaultThhook
;
63 #define hCurrentTask (pThhook->CurTDB)
64 #define hFirstTask (pThhook->HeadTDB)
65 #define hLockedTask (pThhook->LockTDB)
67 static UINT16 nTaskCount
= 0;
69 static HTASK16 initial_task
;
71 /***********************************************************************
74 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
76 THHOOK
*pOldThhook
= pThhook
;
78 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
80 *pThhook
= *pOldThhook
;
83 /***********************************************************************
86 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
88 TDB
* pTask
= TASK_GetPtr( hTask
);
90 if (pTask
->hNext
) return pTask
->hNext
;
91 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
95 /***********************************************************************
98 TDB
*TASK_GetPtr( HTASK16 hTask
)
100 return GlobalLock16( hTask
);
104 /***********************************************************************
107 TDB
*TASK_GetCurrent(void)
109 return TASK_GetPtr( GetCurrentTask() );
113 /***********************************************************************
116 static void TASK_LinkTask( HTASK16 hTask
)
121 if (!(pTask
= TASK_GetPtr( hTask
))) return;
122 prevTask
= &hFirstTask
;
125 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
126 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
127 prevTask
= &prevTaskPtr
->hNext
;
129 pTask
->hNext
= *prevTask
;
135 /***********************************************************************
138 static void TASK_UnlinkTask( HTASK16 hTask
)
143 prevTask
= &hFirstTask
;
144 while (*prevTask
&& (*prevTask
!= hTask
))
146 pTask
= TASK_GetPtr( *prevTask
);
147 prevTask
= &pTask
->hNext
;
151 pTask
= TASK_GetPtr( *prevTask
);
152 *prevTask
= pTask
->hNext
;
159 /***********************************************************************
162 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
163 * and containing 'count' entries.
165 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
171 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
173 pThunk
->magic
= THUNK_MAGIC
;
174 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
176 for (i
= 0; i
< count
-1; i
++)
178 free
+= 8; /* Offset of next thunk */
179 pThunk
->thunks
[4*i
] = free
;
181 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
185 /***********************************************************************
188 * Allocate a thunk for MakeProcInstance().
190 static SEGPTR
TASK_AllocThunk(void)
196 if (!(pTask
= TASK_GetCurrent())) return 0;
197 sel
= pTask
->hCSAlias
;
198 pThunk
= &pTask
->thunks
;
199 base
= (int)pThunk
- (int)pTask
;
200 while (!pThunk
->free
)
203 if (!sel
) /* Allocate a new segment */
205 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
206 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
207 if (!sel
) return (SEGPTR
)0;
208 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
211 pThunk
= (THUNKS
*)GlobalLock16( sel
);
214 base
+= pThunk
->free
;
215 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
216 return MAKESEGPTR( sel
, base
);
220 /***********************************************************************
223 * Free a MakeProcInstance() thunk.
225 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
231 if (!(pTask
= TASK_GetCurrent())) return 0;
232 sel
= pTask
->hCSAlias
;
233 pThunk
= &pTask
->thunks
;
234 base
= (int)pThunk
- (int)pTask
;
235 while (sel
&& (sel
!= HIWORD(thunk
)))
238 pThunk
= (THUNKS
*)GlobalLock16( sel
);
241 if (!sel
) return FALSE
;
242 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
243 pThunk
->free
= LOWORD(thunk
) - base
;
248 /***********************************************************************
251 * NOTE: This routine might be called by a Win32 thread. Thus, we need
252 * to be careful to protect global data structures. We do this
253 * by entering the Win16Lock while linking the task into the
256 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
263 /* Allocate the task structure */
265 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
266 if (!hTask
) return NULL
;
267 pTask
= TASK_GetPtr( hTask
);
268 FarSetOwner16( hTask
, pModule
->self
);
270 /* Fill the task structure */
272 pTask
->hSelf
= hTask
;
274 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
276 pTask
->flags
|= TDBF_WIN32
;
277 pTask
->hInstance
= pModule
->self
;
278 pTask
->hPrevInstance
= 0;
279 /* NOTE: for 16-bit tasks, the instance handles are updated later on
283 pTask
->version
= pModule
->expected_version
;
284 pTask
->hModule
= pModule
->self
;
285 pTask
->hParent
= GetCurrentTask();
286 pTask
->magic
= TDB_MAGIC
;
287 pTask
->nCmdShow
= cmdShow
;
289 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
290 strcpy( pTask
->curdir
, "\\" );
291 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
292 sizeof(pTask
->curdir
) - 1 );
294 /* Create the thunks block */
296 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
298 /* Copy the module name */
300 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
301 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
303 /* Allocate a selector for the PDB */
305 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
306 pModule
->self
, WINE_LDT_FLAGS_DATA
);
310 pTask
->pdb
.int20
= 0x20cd;
311 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
312 proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
313 memcpy( &pTask
->pdb
.dispatcher
[1], &proc
, sizeof(proc
) );
314 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
315 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
316 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
317 pTask
->pdb
.fileHandlesPtr
=
318 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
319 pTask
->pdb
.hFileHandles
= 0;
320 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
321 /* FIXME: should we make a copy of the environment? */
322 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
323 pTask
->pdb
.nbFiles
= 20;
325 /* Fill the command line */
329 cmdline
= GetCommandLineA();
330 /* remove the first word (program name) */
332 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
333 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
334 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
335 len
= strlen(cmdline
);
337 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
338 pTask
->pdb
.cmdLine
[0] = len
;
339 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
340 /* pTask->pdb.cmdLine[len+1] = 0; */
342 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
344 /* Get the compatibility flags */
346 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
348 /* Allocate a code segment alias for the TDB */
350 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
351 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
353 /* Default DTA overwrites command line */
355 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
357 /* Create scheduler event for 16-bit tasks */
359 if ( !(pTask
->flags
& TDBF_WIN32
) )
360 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
362 /* Enter task handle into thread */
364 if (teb
) teb
->htask16
= hTask
;
365 if (!initial_task
) initial_task
= hTask
;
371 /***********************************************************************
374 static void TASK_DeleteTask( HTASK16 hTask
)
379 if (!(pTask
= TASK_GetPtr( hTask
))) return;
382 pTask
->magic
= 0xdead; /* invalidate signature */
384 /* Free the selector aliases */
386 GLOBAL_FreeBlock( pTask
->hCSAlias
);
387 GLOBAL_FreeBlock( pTask
->hPDB
);
389 /* Free the task module */
391 FreeModule16( pTask
->hModule
);
393 /* Free the task structure itself */
395 GlobalFree16( hTask
);
397 /* Free all memory used by this task (including the 32-bit stack, */
398 /* the environment block and the thunk segments). */
400 GlobalFreeAll16( hPDB
);
404 /***********************************************************************
405 * TASK_CreateMainTask
407 * Create a task for the main (32-bit) process.
409 void TASK_CreateMainTask(void)
412 STARTUPINFOA startup_info
;
413 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
415 GetStartupInfoA( &startup_info
);
416 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
417 pTask
= TASK_Create( (NE_MODULE
*)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
418 cmdShow
, NtCurrentTeb(), NULL
, 0 );
421 ERR("could not create task for main process\n");
425 /* Add the task to the linked list */
426 /* (no need to get the win16 lock, we are the only thread at this point) */
427 TASK_LinkTask( pTask
->hSelf
);
431 /* startup routine for a new 16-bit thread */
432 static DWORD CALLBACK
task_start( TDB
*pTask
)
436 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
437 NtCurrentTeb()->htask16
= pTask
->hSelf
;
440 TASK_LinkTask( pTask
->hSelf
);
441 pTask
->teb
= NtCurrentTeb();
442 ret
= NE_StartTask();
448 /***********************************************************************
451 * Spawn a new 16-bit task.
453 HTASK16
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
,
454 LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
458 if (!(pTask
= TASK_Create( pModule
, cmdShow
, NULL
, cmdline
, len
))) return 0;
459 if (!(*hThread
= CreateThread( NULL
, 0, (LPTHREAD_START_ROUTINE
)task_start
, pTask
, 0, NULL
)))
461 TASK_DeleteTask( pTask
->hSelf
);
468 /***********************************************************************
471 void TASK_ExitTask(void)
476 /* Enter the Win16Lock to protect global data structures */
479 pTask
= TASK_GetCurrent();
486 TRACE("Killing task %04x\n", pTask
->hSelf
);
488 /* Perform USER cleanup */
490 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
491 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
492 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
493 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
495 /* Remove the task from the list to be sure we never switch back to it */
496 TASK_UnlinkTask( pTask
->hSelf
);
498 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
500 TRACE("this is the last task, exiting\n" );
506 TDB
* p
= TASK_GetPtr( hFirstTask
);
509 if( p
->hYieldTo
== pTask
->hSelf
) p
->hYieldTo
= 0;
510 p
= TASK_GetPtr( p
->hNext
);
516 if ( hLockedTask
== pTask
->hSelf
)
519 TASK_DeleteTask( pTask
->hSelf
);
521 /* ... and completely release the Win16Lock, just in case. */
522 ReleaseThunkLock( &lockCount
);
526 /***********************************************************************
527 * InitTask (KERNEL.91)
529 * Called by the application startup code.
531 void WINAPI
InitTask16( CONTEXT86
*context
)
534 INSTANCEDATA
*pinstance
;
538 if (!(pTask
= TASK_GetCurrent())) return;
540 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
541 as some apps, notably Visual Basic apps, *modify* the heap/stack
542 size of the instance data segment before calling InitTask() */
544 /* Initialize the INSTANCEDATA structure */
545 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
546 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
547 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
548 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
549 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
551 /* Initialize the local heap */
552 if (LOWORD(context
->Ecx
))
553 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
555 /* Initialize implicitly loaded DLLs */
556 NE_InitializeDLLs( pTask
->hModule
);
557 NE_DllProcessAttach( pTask
->hModule
);
559 /* Registers on return are:
560 * ax 1 if OK, 0 on error
561 * cx stack limit in bytes
562 * dx cmdShow parameter
563 * si instance handle of the previous instance
564 * di instance handle of the new task
565 * es:bx pointer to command line inside PSP
567 * 0 (=%bp) is pushed on the stack
569 ptr
= stack16_push( sizeof(WORD
) );
570 *(WORD
*)MapSL(ptr
) = 0;
575 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
578 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
579 while ((*p
== ' ') || (*p
== '\t')) p
++;
580 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
582 context
->Ecx
= pinstance
->stacktop
;
583 context
->Edx
= pTask
->nCmdShow
;
584 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
585 context
->Edi
= (DWORD
)pTask
->hInstance
;
586 context
->SegEs
= (WORD
)pTask
->hPDB
;
590 /***********************************************************************
591 * WaitEvent (KERNEL.30)
593 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
597 if (!hTask
) hTask
= GetCurrentTask();
598 pTask
= TASK_GetPtr( hTask
);
600 if (pTask
->flags
& TDBF_WIN32
)
602 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
606 if (pTask
->nEvents
> 0)
612 if (pTask
->teb
== NtCurrentTeb())
616 NtResetEvent( pTask
->hEvent
, NULL
);
617 ReleaseThunkLock( &lockCount
);
618 SYSLEVEL_CheckNotLevel( 1 );
619 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
620 RestoreThunkLock( lockCount
);
621 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
623 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
629 /***********************************************************************
630 * PostEvent (KERNEL.31)
632 void WINAPI
PostEvent16( HTASK16 hTask
)
636 if (!hTask
) hTask
= GetCurrentTask();
637 if (!(pTask
= TASK_GetPtr( hTask
))) return;
639 if (pTask
->flags
& TDBF_WIN32
)
641 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
647 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
651 /***********************************************************************
652 * SetPriority (KERNEL.32)
654 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
659 if (!hTask
) hTask
= GetCurrentTask();
660 if (!(pTask
= TASK_GetPtr( hTask
))) return;
661 newpriority
= pTask
->priority
+ delta
;
662 if (newpriority
< -32) newpriority
= -32;
663 else if (newpriority
> 15) newpriority
= 15;
665 pTask
->priority
= newpriority
+ 1;
666 TASK_UnlinkTask( pTask
->hSelf
);
667 TASK_LinkTask( pTask
->hSelf
);
672 /***********************************************************************
673 * LockCurrentTask (KERNEL.33)
675 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
677 if (bLock
) hLockedTask
= GetCurrentTask();
678 else hLockedTask
= 0;
683 /***********************************************************************
684 * IsTaskLocked (KERNEL.122)
686 HTASK16 WINAPI
IsTaskLocked16(void)
692 /***********************************************************************
693 * OldYield (KERNEL.117)
695 void WINAPI
OldYield16(void)
699 ReleaseThunkLock(&count
);
700 RestoreThunkLock(count
);
703 /***********************************************************************
704 * WIN32_OldYield (KERNEL.447)
706 void WINAPI
WIN32_OldYield16(void)
710 ReleaseThunkLock(&count
);
711 RestoreThunkLock(count
);
714 /***********************************************************************
715 * DirectedYield (KERNEL.150)
717 void WINAPI
DirectedYield16( HTASK16 hTask
)
719 TDB
*pCurTask
= TASK_GetCurrent();
721 if (pCurTask
->flags
& TDBF_WIN32
)
723 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
727 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
729 pCurTask
->hYieldTo
= hTask
;
732 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
735 /***********************************************************************
738 void WINAPI
Yield16(void)
740 TDB
*pCurTask
= TASK_GetCurrent();
742 if (pCurTask
) pCurTask
->hYieldTo
= 0;
743 if (pCurTask
&& pCurTask
->hQueue
)
745 HMODULE mod
= GetModuleHandleA( "user32.dll" );
748 FARPROC proc
= GetProcAddress( mod
, "UserYield16" );
759 /***********************************************************************
760 * KERNEL_490 (KERNEL.490)
762 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
764 if ( !someTask
) return 0;
766 FIXME("(%04x): stub\n", someTask
);
770 /***********************************************************************
771 * MakeProcInstance (KERNEL.51)
773 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
777 WORD hInstanceSelector
;
779 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
781 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
784 /* Win95 actually protects via SEH, but this is better for debugging */
785 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
791 if ( (!(hInstance
& 4)) ||
792 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
794 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
800 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
802 && (hInstance
!= 0xffff) )
804 /* calling MPI with a foreign DSEG is invalid ! */
805 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
806 hInstance
,CURRENT_DS
);
809 /* Always use the DSEG that MPI was entered with.
810 * We used to set hInstance to GetTaskDS16(), but this should be wrong
811 * as CURRENT_DS provides the DSEG value we need.
812 * ("calling" DS, *not* "task" DS !) */
813 hInstanceSelector
= CURRENT_DS
;
814 hInstance
= GlobalHandle16(hInstanceSelector
);
816 /* no thunking for DLLs */
817 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
820 thunkaddr
= TASK_AllocThunk();
821 if (!thunkaddr
) return (FARPROC16
)0;
822 thunk
= MapSL( thunkaddr
);
823 lfunc
= MapSL( (SEGPTR
)func
);
825 TRACE("(%08lx,%04x): got thunk %08lx\n",
826 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
827 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
828 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
830 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
833 *thunk
++ = 0xb8; /* movw instance, %ax */
834 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
835 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
836 *thunk
++ = 0xea; /* ljmp func */
837 *(DWORD
*)thunk
= (DWORD
)func
;
838 return (FARPROC16
)thunkaddr
;
839 /* CX reg indicates if thunkaddr != NULL, implement if needed */
843 /***********************************************************************
844 * FreeProcInstance (KERNEL.52)
846 void WINAPI
FreeProcInstance16( FARPROC16 func
)
848 TRACE("(%08lx)\n", (DWORD
)func
);
849 TASK_FreeThunk( (SEGPTR
)func
);
852 /**********************************************************************
853 * TASK_GetCodeSegment
855 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
856 * and logical segment number of a given code segment.
858 * 'proc' either *is* already a pair of module handle and segment number,
859 * in which case there's nothing to do. Otherwise, it is a pointer to
860 * a function, and we need to retrieve the code segment. If the pointer
861 * happens to point to a thunk, we'll retrieve info about the code segment
862 * where the function pointed to by the thunk resides, not the thunk itself.
864 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
865 * the function the snoop code will return to ...
868 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
869 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
871 NE_MODULE
*pModule
= NULL
;
872 SEGTABLEENTRY
*pSeg
= NULL
;
875 /* Try pair of module handle / segment number */
876 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
877 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
879 segNr
= LOWORD( proc
);
880 if ( segNr
&& segNr
<= pModule
->seg_count
)
881 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
884 /* Try thunk or function */
887 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
890 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
891 selector
= thunk
[6] + (thunk
[7] << 8);
893 selector
= HIWORD( proc
);
895 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
896 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
899 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
900 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
903 if ( pModule
&& segNr
> pModule
->seg_count
)
907 /* Abort if segment not found */
909 if ( !pModule
|| !pSeg
)
912 /* Return segment data */
914 if ( ppModule
) *ppModule
= pModule
;
915 if ( ppSeg
) *ppSeg
= pSeg
;
916 if ( pSegNr
) *pSegNr
= segNr
;
921 /**********************************************************************
922 * GetCodeHandle (KERNEL.93)
924 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
928 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
934 /**********************************************************************
935 * GetCodeInfo (KERNEL.104)
937 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
943 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
946 /* Fill in segment information */
948 segInfo
->offSegment
= pSeg
->filepos
;
949 segInfo
->cbSegment
= pSeg
->size
;
950 segInfo
->flags
= pSeg
->flags
;
951 segInfo
->cbAlloc
= pSeg
->minsize
;
952 segInfo
->h
= pSeg
->hSeg
;
953 segInfo
->alignShift
= pModule
->alignment
;
955 if ( segNr
== pModule
->dgroup
)
956 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
958 /* Return module handle in %es */
960 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
966 /**********************************************************************
967 * DefineHandleTable (KERNEL.94)
969 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
971 FIXME("(%04x): stub ?\n", wOffset
);
976 /***********************************************************************
977 * SetTaskQueue (KERNEL.34)
979 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
984 if (!hTask
) hTask
= GetCurrentTask();
985 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
987 hPrev
= pTask
->hQueue
;
988 pTask
->hQueue
= hQueue
;
994 /***********************************************************************
995 * GetTaskQueue (KERNEL.35)
997 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1001 if (!hTask
) hTask
= GetCurrentTask();
1002 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
1003 return pTask
->hQueue
;
1006 /***********************************************************************
1007 * SetThreadQueue (KERNEL.463)
1009 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1011 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1012 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1016 teb
->queue
= hQueue
;
1018 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1019 SetTaskQueue16( teb
->htask16
, hQueue
);
1025 /***********************************************************************
1026 * GetThreadQueue (KERNEL.464)
1028 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1032 teb
= NtCurrentTeb();
1033 else if ( HIWORD(thread
) )
1034 teb
= THREAD_IdToTEB( thread
);
1035 else if ( IsTask16( (HTASK16
)thread
) )
1036 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1038 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1041 /***********************************************************************
1042 * SetFastQueue (KERNEL.624)
1044 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1048 teb
= NtCurrentTeb();
1049 else if ( HIWORD(thread
) )
1050 teb
= THREAD_IdToTEB( thread
);
1051 else if ( IsTask16( (HTASK16
)thread
) )
1052 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1054 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1057 /***********************************************************************
1058 * GetFastQueue (KERNEL.625)
1060 HANDLE WINAPI
GetFastQueue16( void )
1062 HANDLE ret
= (HANDLE
)NtCurrentTeb()->queue
;
1064 if (!ret
) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1068 /***********************************************************************
1069 * SwitchStackTo (KERNEL.108)
1071 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1074 STACK16FRAME
*oldFrame
, *newFrame
;
1075 INSTANCEDATA
*pData
;
1078 if (!(pTask
= TASK_GetCurrent())) return;
1079 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1080 TRACE("old=%04x:%04x new=%04x:%04x\n",
1081 SELECTOROF( pTask
->teb
->cur_stack
),
1082 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1084 /* Save the old stack */
1086 oldFrame
= THREAD_STACK16( pTask
->teb
);
1087 /* pop frame + args and push bp */
1088 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1090 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1091 pData
->stacktop
= top
;
1092 pData
->stackmin
= ptr
;
1093 pData
->stackbottom
= ptr
;
1095 /* Switch to the new stack */
1097 /* Note: we need to take the 3 arguments into account; otherwise,
1098 * the stack will underflow upon return from this function.
1100 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1101 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1102 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1103 newFrame
= THREAD_STACK16( pTask
->teb
);
1105 /* Copy the stack frame and the local variables to the new stack */
1107 memmove( newFrame
, oldFrame
, copySize
);
1109 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1113 /***********************************************************************
1114 * SwitchStackBack (KERNEL.109)
1116 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1118 STACK16FRAME
*oldFrame
, *newFrame
;
1119 INSTANCEDATA
*pData
;
1121 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1123 if (!pData
->old_ss_sp
)
1125 WARN("No previous SwitchStackTo\n" );
1128 TRACE("restoring stack %04x:%04x\n",
1129 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1131 oldFrame
= CURRENT_STACK16
;
1133 /* Pop bp from the previous stack */
1135 BP_reg(context
) = *(WORD
*)MapSL(pData
->old_ss_sp
);
1136 pData
->old_ss_sp
+= sizeof(WORD
);
1138 /* Switch back to the old stack */
1140 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1141 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1142 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1143 pData
->old_ss_sp
= 0;
1145 /* Build a stack frame for the return */
1147 newFrame
= CURRENT_STACK16
;
1148 newFrame
->frame32
= oldFrame
->frame32
;
1149 newFrame
->module_cs
= oldFrame
->module_cs
;
1150 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1151 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1155 /***********************************************************************
1156 * GetTaskQueueDS (KERNEL.118)
1158 void WINAPI
GetTaskQueueDS16(void)
1160 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1164 /***********************************************************************
1165 * GetTaskQueueES (KERNEL.119)
1167 void WINAPI
GetTaskQueueES16(void)
1169 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1173 /***********************************************************************
1174 * GetCurrentTask (KERNEL32.@)
1176 HTASK16 WINAPI
GetCurrentTask(void)
1178 return NtCurrentTeb()->htask16
;
1181 /***********************************************************************
1182 * GetCurrentTask (KERNEL.36)
1184 DWORD WINAPI
WIN16_GetCurrentTask(void)
1186 /* This is the version used by relay code; the first task is */
1187 /* returned in the high word of the result */
1188 return MAKELONG( GetCurrentTask(), hFirstTask
);
1192 /***********************************************************************
1193 * GetCurrentPDB (KERNEL.37)
1195 * UNDOC: returns PSP of KERNEL in high word
1197 DWORD WINAPI
GetCurrentPDB16(void)
1201 if (!(pTask
= TASK_GetCurrent())) return 0;
1202 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1206 /***********************************************************************
1207 * GetCurPID (KERNEL.157)
1209 DWORD WINAPI
GetCurPID16( DWORD unused
)
1215 /***********************************************************************
1216 * GetInstanceData (KERNEL.54)
1218 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1220 char *ptr
= (char *)GlobalLock16( instance
);
1221 if (!ptr
|| !len
) return 0;
1222 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1223 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1228 /***********************************************************************
1229 * GetExeVersion (KERNEL.105)
1231 WORD WINAPI
GetExeVersion16(void)
1235 if (!(pTask
= TASK_GetCurrent())) return 0;
1236 return pTask
->version
;
1240 /***********************************************************************
1241 * SetErrorMode (KERNEL.107)
1243 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1246 if (!(pTask
= TASK_GetCurrent())) return 0;
1247 pTask
->error_mode
= mode
;
1248 return SetErrorMode( mode
);
1252 /***********************************************************************
1253 * GetNumTasks (KERNEL.152)
1255 UINT16 WINAPI
GetNumTasks16(void)
1261 /***********************************************************************
1262 * GetTaskDS (KERNEL.155)
1264 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1265 * I don't think we need to bother with this.
1267 HINSTANCE16 WINAPI
GetTaskDS16(void)
1271 if (!(pTask
= TASK_GetCurrent())) return 0;
1272 return GlobalHandleToSel16(pTask
->hInstance
);
1275 /***********************************************************************
1276 * GetDummyModuleHandleDS (KERNEL.602)
1278 WORD WINAPI
GetDummyModuleHandleDS16(void)
1283 if (!(pTask
= TASK_GetCurrent())) return 0;
1284 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1285 selector
= GlobalHandleToSel16( pTask
->hModule
);
1286 CURRENT_DS
= selector
;
1290 /***********************************************************************
1291 * IsTask (KERNEL.320)
1292 * IsTask16 (KERNEL32.@)
1294 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1298 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1299 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1300 return (pTask
->magic
== TDB_MAGIC
);
1304 /***********************************************************************
1305 * IsWinOldApTask (KERNEL.158)
1307 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1309 /* should return bit 0 of byte 0x48 in PSP */
1313 /***********************************************************************
1314 * SetTaskSignalProc (KERNEL.38)
1316 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1321 if (!hTask
) hTask
= GetCurrentTask();
1322 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1323 oldProc
= pTask
->userhandler
;
1324 pTask
->userhandler
= proc
;
1328 /***********************************************************************
1329 * TASK_CallTaskSignalProc
1331 /* ### start build ### */
1332 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1333 /* ### stop build ### */
1334 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1336 TDB
*pTask
= TASK_GetCurrent();
1337 if ( !pTask
|| !pTask
->userhandler
) return;
1339 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1340 hTaskOrModule
, uCode
, 0,
1341 pTask
->hInstance
, pTask
->hQueue
);
1344 /***********************************************************************
1345 * SetSigHandler (KERNEL.140)
1347 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1348 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1350 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1351 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1353 if (flag
!= 1) return 0;
1354 if (!newmode
) newhandler
= NULL
; /* Default handler */
1359 if (!(pTask
= TASK_GetCurrent())) return 0;
1360 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1361 pTask
->signal_flags
= newmode
;
1362 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1363 pTask
->sighandler
= newhandler
;
1369 /***********************************************************************
1370 * GlobalNotify (KERNEL.154)
1372 * Note that GlobalNotify does _not_ return the old NotifyProc
1373 * -- contrary to LocalNotify !!
1375 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1379 if (!(pTask
= TASK_GetCurrent())) return;
1380 pTask
->discardhandler
= proc
;
1384 /***********************************************************************
1387 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1392 /* Check for module handle */
1394 if (!(ptr
= GlobalLock16( handle
))) return 0;
1395 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1397 /* Search for this handle inside all tasks */
1399 *hTask
= hFirstTask
;
1402 TDB
*pTask
= TASK_GetPtr( *hTask
);
1403 if ((*hTask
== handle
) ||
1404 (pTask
->hInstance
== handle
) ||
1405 (pTask
->hQueue
== handle
) ||
1406 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1407 *hTask
= pTask
->hNext
;
1410 /* Check the owner for module handle */
1412 owner
= FarGetOwner16( handle
);
1413 if (!(ptr
= GlobalLock16( owner
))) return 0;
1414 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1416 /* Search for the owner inside all tasks */
1418 *hTask
= hFirstTask
;
1421 TDB
*pTask
= TASK_GetPtr( *hTask
);
1422 if ((*hTask
== owner
) ||
1423 (pTask
->hInstance
== owner
) ||
1424 (pTask
->hQueue
== owner
) ||
1425 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1426 *hTask
= pTask
->hNext
;
1432 /***********************************************************************
1433 * GetExePtr (KERNEL.133)
1435 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1438 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1439 STACK16FRAME
*frame
= CURRENT_STACK16
;
1440 frame
->ecx
= hModule
;
1441 if (hTask
) frame
->es
= hTask
;
1446 /***********************************************************************
1449 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1452 return GetExePtrHelper( handle
, &hTask
);
1456 /***********************************************************************
1457 * TaskFirst (TOOLHELP.63)
1459 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1461 lpte
->hNext
= hFirstTask
;
1462 return TaskNext16( lpte
);
1466 /***********************************************************************
1467 * TaskNext (TOOLHELP.64)
1469 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1472 INSTANCEDATA
*pInstData
;
1474 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1475 if (!lpte
->hNext
) return FALSE
;
1477 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1479 pTask
= TASK_GetPtr( lpte
->hNext
);
1480 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1481 if (pTask
->hInstance
)
1483 lpte
->hNext
= pTask
->hNext
;
1485 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1486 lpte
->hTask
= lpte
->hNext
;
1487 lpte
->hTaskParent
= pTask
->hParent
;
1488 lpte
->hInst
= pTask
->hInstance
;
1489 lpte
->hModule
= pTask
->hModule
;
1490 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1491 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1492 lpte
->wStackTop
= pInstData
->stacktop
;
1493 lpte
->wStackMinimum
= pInstData
->stackmin
;
1494 lpte
->wStackBottom
= pInstData
->stackbottom
;
1495 lpte
->wcEvents
= pTask
->nEvents
;
1496 lpte
->hQueue
= pTask
->hQueue
;
1497 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1498 lpte
->wPSPOffset
= 0x100; /*??*/
1499 lpte
->hNext
= pTask
->hNext
;
1504 /***********************************************************************
1505 * TaskFindHandle (TOOLHELP.65)
1507 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1509 lpte
->hNext
= hTask
;
1510 return TaskNext16( lpte
);
1514 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1516 /**************************************************************************
1517 * FatalAppExit (KERNEL.137)
1519 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1521 TDB
*pTask
= TASK_GetCurrent();
1523 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1525 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1528 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1531 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1535 ERR( "%s\n", debugstr_a(str
) );
1542 /***********************************************************************
1543 * TerminateApp (TOOLHELP.77)
1545 * See "Undocumented Windows".
1547 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1549 if (hTask
&& hTask
!= GetCurrentTask())
1551 FIXME("cannot terminate task %x\n", hTask
);
1555 if (wFlags
& NO_UAE_BOX
)
1558 old_mode
= SetErrorMode16(0);
1559 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1561 FatalAppExit16( 0, NULL
);
1563 /* hmm, we're still alive ?? */
1565 /* check undocumented flag */
1566 if (!(wFlags
& 0x8000))
1567 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1569 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1570 but let's just call ExitThread */
1575 /***********************************************************************
1576 * GetAppCompatFlags (KERNEL.354)
1578 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1582 if (!hTask
) hTask
= GetCurrentTask();
1583 if (!(pTask
=TASK_GetPtr( hTask
))) return 0;
1584 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1585 return pTask
->compat_flags
;