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"
44 #include "selectors.h"
45 #include "wine/server.h"
47 #include "stackframe.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(task
);
55 WINE_DECLARE_DEBUG_CHANNEL(relay
);
56 WINE_DECLARE_DEBUG_CHANNEL(toolhelp
);
58 /* Min. number of thunks allocated when creating a new segment */
62 static THHOOK DefaultThhook
;
63 THHOOK
*pThhook
= &DefaultThhook
;
65 #define hCurrentTask (pThhook->CurTDB)
66 #define hFirstTask (pThhook->HeadTDB)
67 #define hLockedTask (pThhook->LockTDB)
69 static UINT16 nTaskCount
= 0;
71 static HTASK16 initial_task
;
73 /***********************************************************************
76 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
78 THHOOK
*pOldThhook
= pThhook
;
80 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
82 *pThhook
= *pOldThhook
;
85 /***********************************************************************
88 HTASK16
TASK_GetNextTask( HTASK16 hTask
)
90 TDB
* pTask
= TASK_GetPtr( hTask
);
92 if (pTask
->hNext
) return pTask
->hNext
;
93 return (hFirstTask
!= hTask
) ? hFirstTask
: 0;
97 /***********************************************************************
100 TDB
*TASK_GetPtr( HTASK16 hTask
)
102 return GlobalLock16( hTask
);
106 /***********************************************************************
109 TDB
*TASK_GetCurrent(void)
111 return TASK_GetPtr( GetCurrentTask() );
115 /***********************************************************************
118 static void TASK_LinkTask( HTASK16 hTask
)
123 if (!(pTask
= TASK_GetPtr( hTask
))) return;
124 prevTask
= &hFirstTask
;
127 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
128 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
129 prevTask
= &prevTaskPtr
->hNext
;
131 pTask
->hNext
= *prevTask
;
137 /***********************************************************************
140 static void TASK_UnlinkTask( HTASK16 hTask
)
145 prevTask
= &hFirstTask
;
146 while (*prevTask
&& (*prevTask
!= hTask
))
148 pTask
= TASK_GetPtr( *prevTask
);
149 prevTask
= &pTask
->hNext
;
153 pTask
= TASK_GetPtr( *prevTask
);
154 *prevTask
= pTask
->hNext
;
161 /***********************************************************************
164 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
165 * and containing 'count' entries.
167 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
173 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
175 pThunk
->magic
= THUNK_MAGIC
;
176 pThunk
->free
= (int)&pThunk
->thunks
- (int)pThunk
;
178 for (i
= 0; i
< count
-1; i
++)
180 free
+= 8; /* Offset of next thunk */
181 pThunk
->thunks
[4*i
] = free
;
183 pThunk
->thunks
[4*i
] = 0; /* Last thunk */
187 /***********************************************************************
190 * Allocate a thunk for MakeProcInstance().
192 static SEGPTR
TASK_AllocThunk(void)
198 if (!(pTask
= TASK_GetCurrent())) return 0;
199 sel
= pTask
->hCSAlias
;
200 pThunk
= &pTask
->thunks
;
201 base
= (int)pThunk
- (int)pTask
;
202 while (!pThunk
->free
)
205 if (!sel
) /* Allocate a new segment */
207 sel
= GLOBAL_Alloc( GMEM_FIXED
, sizeof(THUNKS
) + (MIN_THUNKS
-1)*8,
208 pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
209 if (!sel
) return (SEGPTR
)0;
210 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
213 pThunk
= (THUNKS
*)GlobalLock16( sel
);
216 base
+= pThunk
->free
;
217 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
218 return MAKESEGPTR( sel
, base
);
222 /***********************************************************************
225 * Free a MakeProcInstance() thunk.
227 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
233 if (!(pTask
= TASK_GetCurrent())) return 0;
234 sel
= pTask
->hCSAlias
;
235 pThunk
= &pTask
->thunks
;
236 base
= (int)pThunk
- (int)pTask
;
237 while (sel
&& (sel
!= HIWORD(thunk
)))
240 pThunk
= (THUNKS
*)GlobalLock16( sel
);
243 if (!sel
) return FALSE
;
244 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
245 pThunk
->free
= LOWORD(thunk
) - base
;
250 /***********************************************************************
253 * NOTE: This routine might be called by a Win32 thread. Thus, we need
254 * to be careful to protect global data structures. We do this
255 * by entering the Win16Lock while linking the task into the
258 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, TEB
*teb
, LPCSTR cmdline
, BYTE len
)
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
, pModule
->self
);
272 /* Fill the task structure */
274 pTask
->hSelf
= hTask
;
276 if (teb
&& teb
->tibflags
& TEBF_WIN32
)
278 pTask
->flags
|= TDBF_WIN32
;
279 pTask
->hInstance
= pModule
->self
;
280 pTask
->hPrevInstance
= 0;
281 /* NOTE: for 16-bit tasks, the instance handles are updated later on
285 pTask
->version
= pModule
->expected_version
;
286 pTask
->hModule
= pModule
->self
;
287 pTask
->hParent
= GetCurrentTask();
288 pTask
->magic
= TDB_MAGIC
;
289 pTask
->nCmdShow
= cmdShow
;
291 pTask
->curdrive
= DRIVE_GetCurrentDrive() | 0x80;
292 strcpy( pTask
->curdir
, "\\" );
293 lstrcpynA( pTask
->curdir
+ 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
294 sizeof(pTask
->curdir
) - 1 );
296 /* Create the thunks block */
298 TASK_CreateThunks( hTask
, (int)&pTask
->thunks
- (int)pTask
, 7 );
300 /* Copy the module name */
302 GetModuleName16( pModule
->self
, name
, sizeof(name
) );
303 strncpy( pTask
->module_name
, name
, sizeof(pTask
->module_name
) );
305 /* Allocate a selector for the PDB */
307 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
308 pModule
->self
, WINE_LDT_FLAGS_DATA
);
312 pTask
->pdb
.int20
= 0x20cd;
313 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
314 proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
315 memcpy( &pTask
->pdb
.dispatcher
[1], &proc
, sizeof(proc
) );
316 pTask
->pdb
.savedint22
= INT_GetPMHandler( 0x22 );
317 pTask
->pdb
.savedint23
= INT_GetPMHandler( 0x23 );
318 pTask
->pdb
.savedint24
= INT_GetPMHandler( 0x24 );
319 pTask
->pdb
.fileHandlesPtr
=
320 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), (int)&((PDB16
*)0)->fileHandles
);
321 pTask
->pdb
.hFileHandles
= 0;
322 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
323 /* FIXME: should we make a copy of the environment? */
324 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
325 pTask
->pdb
.nbFiles
= 20;
327 /* Fill the command line */
331 cmdline
= GetCommandLineA();
332 /* remove the first word (program name) */
334 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
335 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
336 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
337 len
= strlen(cmdline
);
339 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
340 pTask
->pdb
.cmdLine
[0] = len
;
341 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
342 /* pTask->pdb.cmdLine[len+1] = 0; */
344 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name
, len
, cmdline
, hTask
);
346 /* Get the compatibility flags */
348 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
350 /* Allocate a code segment alias for the TDB */
352 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)pTask
,
353 sizeof(TDB
), pTask
->hPDB
, WINE_LDT_FLAGS_CODE
);
355 /* Default DTA overwrites command line */
357 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, (int)&pTask
->pdb
.cmdLine
- (int)&pTask
->pdb
);
359 /* Create scheduler event for 16-bit tasks */
361 if ( !(pTask
->flags
& TDBF_WIN32
) )
362 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, TRUE
, FALSE
);
364 /* Enter task handle into thread */
366 if (teb
) teb
->htask16
= hTask
;
367 if (!initial_task
) initial_task
= hTask
;
373 /***********************************************************************
376 static void TASK_DeleteTask( HTASK16 hTask
)
381 if (!(pTask
= TASK_GetPtr( hTask
))) return;
384 pTask
->magic
= 0xdead; /* invalidate signature */
386 /* Free the selector aliases */
388 GLOBAL_FreeBlock( pTask
->hCSAlias
);
389 GLOBAL_FreeBlock( pTask
->hPDB
);
391 /* Free the task module */
393 FreeModule16( pTask
->hModule
);
395 /* Free the task structure itself */
397 GlobalFree16( hTask
);
399 /* Free all memory used by this task (including the 32-bit stack, */
400 /* the environment block and the thunk segments). */
402 GlobalFreeAll16( hPDB
);
406 /***********************************************************************
407 * TASK_CreateMainTask
409 * Create a task for the main (32-bit) process.
411 void TASK_CreateMainTask(void)
414 STARTUPINFOA startup_info
;
415 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
417 GetStartupInfoA( &startup_info
);
418 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
419 pTask
= TASK_Create( (NE_MODULE
*)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
420 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
);
493 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT
, 0 );
494 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT
, 0 );
495 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY
, 0 );
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 * InitTask (KERNEL.91)
531 * Called by the application startup code.
533 void WINAPI
InitTask16( CONTEXT86
*context
)
536 INSTANCEDATA
*pinstance
;
540 if (!(pTask
= TASK_GetCurrent())) return;
542 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
543 as some apps, notably Visual Basic apps, *modify* the heap/stack
544 size of the instance data segment before calling InitTask() */
546 /* Initialize the INSTANCEDATA structure */
547 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
548 pinstance
->stackmin
= OFFSETOF( pTask
->teb
->cur_stack
) + sizeof( STACK16FRAME
);
549 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
550 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
551 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
553 /* Initialize the local heap */
554 if (LOWORD(context
->Ecx
))
555 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
557 /* Initialize implicitly loaded DLLs */
558 NE_InitializeDLLs( pTask
->hModule
);
559 NE_DllProcessAttach( pTask
->hModule
);
561 /* Registers on return are:
562 * ax 1 if OK, 0 on error
563 * cx stack limit in bytes
564 * dx cmdShow parameter
565 * si instance handle of the previous instance
566 * di instance handle of the new task
567 * es:bx pointer to command line inside PSP
569 * 0 (=%bp) is pushed on the stack
571 ptr
= stack16_push( sizeof(WORD
) );
572 *(WORD
*)MapSL(ptr
) = 0;
577 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
580 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
581 while ((*p
== ' ') || (*p
== '\t')) p
++;
582 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
584 context
->Ecx
= pinstance
->stacktop
;
585 context
->Edx
= pTask
->nCmdShow
;
586 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
587 context
->Edi
= (DWORD
)pTask
->hInstance
;
588 context
->SegEs
= (WORD
)pTask
->hPDB
;
592 /***********************************************************************
593 * WaitEvent (KERNEL.30)
595 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
599 if (!hTask
) hTask
= GetCurrentTask();
600 pTask
= TASK_GetPtr( hTask
);
602 if (pTask
->flags
& TDBF_WIN32
)
604 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
608 if (pTask
->nEvents
> 0)
614 if (pTask
->teb
== NtCurrentTeb())
618 NtResetEvent( pTask
->hEvent
, NULL
);
619 ReleaseThunkLock( &lockCount
);
620 SYSLEVEL_CheckNotLevel( 1 );
621 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
622 RestoreThunkLock( lockCount
);
623 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
625 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
631 /***********************************************************************
632 * PostEvent (KERNEL.31)
634 void WINAPI
PostEvent16( HTASK16 hTask
)
638 if (!hTask
) hTask
= GetCurrentTask();
639 if (!(pTask
= TASK_GetPtr( hTask
))) return;
641 if (pTask
->flags
& TDBF_WIN32
)
643 FIXME("called for Win32 thread (%04x)!\n", pTask
->teb
->teb_sel
);
649 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
653 /***********************************************************************
654 * SetPriority (KERNEL.32)
656 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
661 if (!hTask
) hTask
= GetCurrentTask();
662 if (!(pTask
= TASK_GetPtr( hTask
))) return;
663 newpriority
= pTask
->priority
+ delta
;
664 if (newpriority
< -32) newpriority
= -32;
665 else if (newpriority
> 15) newpriority
= 15;
667 pTask
->priority
= newpriority
+ 1;
668 TASK_UnlinkTask( pTask
->hSelf
);
669 TASK_LinkTask( pTask
->hSelf
);
674 /***********************************************************************
675 * LockCurrentTask (KERNEL.33)
677 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
679 if (bLock
) hLockedTask
= GetCurrentTask();
680 else hLockedTask
= 0;
685 /***********************************************************************
686 * IsTaskLocked (KERNEL.122)
688 HTASK16 WINAPI
IsTaskLocked16(void)
694 /***********************************************************************
695 * OldYield (KERNEL.117)
697 void WINAPI
OldYield16(void)
701 ReleaseThunkLock(&count
);
702 RestoreThunkLock(count
);
705 /***********************************************************************
706 * WIN32_OldYield (KERNEL.447)
708 void WINAPI
WIN32_OldYield16(void)
712 ReleaseThunkLock(&count
);
713 RestoreThunkLock(count
);
716 /***********************************************************************
717 * DirectedYield (KERNEL.150)
719 void WINAPI
DirectedYield16( HTASK16 hTask
)
721 TDB
*pCurTask
= TASK_GetCurrent();
723 if (pCurTask
->flags
& TDBF_WIN32
)
725 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel
);
729 TRACE("%04x: DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
731 pCurTask
->hYieldTo
= hTask
;
734 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask
->hSelf
, hTask
);
737 /***********************************************************************
740 void WINAPI
Yield16(void)
742 TDB
*pCurTask
= TASK_GetCurrent();
744 if (pCurTask
) pCurTask
->hYieldTo
= 0;
745 if (pCurTask
&& pCurTask
->hQueue
)
747 HMODULE mod
= GetModuleHandleA( "user32.dll" );
750 FARPROC proc
= GetProcAddress( mod
, "UserYield16" );
761 /***********************************************************************
762 * KERNEL_490 (KERNEL.490)
764 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
766 if ( !someTask
) return 0;
768 FIXME("(%04x): stub\n", someTask
);
772 /***********************************************************************
773 * MakeProcInstance (KERNEL.51)
775 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
779 WORD hInstanceSelector
;
781 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
783 TRACE("(%08lx, %04x);\n", (DWORD
)func
, hInstance
);
786 /* Win95 actually protects via SEH, but this is better for debugging */
787 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD
)func
);
793 if ( (!(hInstance
& 4)) ||
794 ((hInstance
!= 0xffff) && IS_SELECTOR_FREE(hInstance
|7)) )
796 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
802 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
804 && (hInstance
!= 0xffff) )
806 /* calling MPI with a foreign DSEG is invalid ! */
807 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
808 hInstance
,CURRENT_DS
);
811 /* Always use the DSEG that MPI was entered with.
812 * We used to set hInstance to GetTaskDS16(), but this should be wrong
813 * as CURRENT_DS provides the DSEG value we need.
814 * ("calling" DS, *not* "task" DS !) */
815 hInstanceSelector
= CURRENT_DS
;
816 hInstance
= GlobalHandle16(hInstanceSelector
);
818 /* no thunking for DLLs */
819 if (NE_GetPtr(FarGetOwner16(hInstance
))->flags
& NE_FFLAGS_LIBMODULE
)
822 thunkaddr
= TASK_AllocThunk();
823 if (!thunkaddr
) return (FARPROC16
)0;
824 thunk
= MapSL( thunkaddr
);
825 lfunc
= MapSL( (SEGPTR
)func
);
827 TRACE("(%08lx,%04x): got thunk %08lx\n",
828 (DWORD
)func
, hInstance
, (DWORD
)thunkaddr
);
829 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
830 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
832 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
835 *thunk
++ = 0xb8; /* movw instance, %ax */
836 *thunk
++ = (BYTE
)(hInstanceSelector
& 0xff);
837 *thunk
++ = (BYTE
)(hInstanceSelector
>> 8);
838 *thunk
++ = 0xea; /* ljmp func */
839 *(DWORD
*)thunk
= (DWORD
)func
;
840 return (FARPROC16
)thunkaddr
;
841 /* CX reg indicates if thunkaddr != NULL, implement if needed */
845 /***********************************************************************
846 * FreeProcInstance (KERNEL.52)
848 void WINAPI
FreeProcInstance16( FARPROC16 func
)
850 TRACE("(%08lx)\n", (DWORD
)func
);
851 TASK_FreeThunk( (SEGPTR
)func
);
854 /**********************************************************************
855 * TASK_GetCodeSegment
857 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
858 * and logical segment number of a given code segment.
860 * 'proc' either *is* already a pair of module handle and segment number,
861 * in which case there's nothing to do. Otherwise, it is a pointer to
862 * a function, and we need to retrieve the code segment. If the pointer
863 * happens to point to a thunk, we'll retrieve info about the code segment
864 * where the function pointed to by the thunk resides, not the thunk itself.
866 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
867 * the function the snoop code will return to ...
870 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
871 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
873 NE_MODULE
*pModule
= NULL
;
874 SEGTABLEENTRY
*pSeg
= NULL
;
877 /* Try pair of module handle / segment number */
878 pModule
= (NE_MODULE
*) GlobalLock16( HIWORD( proc
) );
879 if ( pModule
&& pModule
->magic
== IMAGE_OS2_SIGNATURE
)
881 segNr
= LOWORD( proc
);
882 if ( segNr
&& segNr
<= pModule
->seg_count
)
883 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
886 /* Try thunk or function */
889 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
892 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
893 selector
= thunk
[6] + (thunk
[7] << 8);
895 selector
= HIWORD( proc
);
897 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
898 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
901 for ( segNr
= 1; segNr
<= pModule
->seg_count
; segNr
++, pSeg
++ )
902 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
905 if ( pModule
&& segNr
> pModule
->seg_count
)
909 /* Abort if segment not found */
911 if ( !pModule
|| !pSeg
)
914 /* Return segment data */
916 if ( ppModule
) *ppModule
= pModule
;
917 if ( ppSeg
) *ppSeg
= pSeg
;
918 if ( pSegNr
) *pSegNr
= segNr
;
923 /**********************************************************************
924 * GetCodeHandle (KERNEL.93)
926 HANDLE16 WINAPI
GetCodeHandle16( FARPROC16 proc
)
930 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
936 /**********************************************************************
937 * GetCodeInfo (KERNEL.104)
939 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
945 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
948 /* Fill in segment information */
950 segInfo
->offSegment
= pSeg
->filepos
;
951 segInfo
->cbSegment
= pSeg
->size
;
952 segInfo
->flags
= pSeg
->flags
;
953 segInfo
->cbAlloc
= pSeg
->minsize
;
954 segInfo
->h
= pSeg
->hSeg
;
955 segInfo
->alignShift
= pModule
->alignment
;
957 if ( segNr
== pModule
->dgroup
)
958 segInfo
->cbAlloc
+= pModule
->heap_size
+ pModule
->stack_size
;
960 /* Return module handle in %es */
962 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
968 /**********************************************************************
969 * DefineHandleTable (KERNEL.94)
971 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
973 FIXME("(%04x): stub ?\n", wOffset
);
978 /***********************************************************************
979 * SetTaskQueue (KERNEL.34)
981 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
986 if (!hTask
) hTask
= GetCurrentTask();
987 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
989 hPrev
= pTask
->hQueue
;
990 pTask
->hQueue
= hQueue
;
996 /***********************************************************************
997 * GetTaskQueue (KERNEL.35)
999 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1003 if (!hTask
) hTask
= GetCurrentTask();
1004 if (!(pTask
= TASK_GetPtr( hTask
))) return 0;
1005 return pTask
->hQueue
;
1008 /***********************************************************************
1009 * SetThreadQueue (KERNEL.463)
1011 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1013 TEB
*teb
= thread
? THREAD_IdToTEB( thread
) : NtCurrentTeb();
1014 HQUEUE16 oldQueue
= teb
? teb
->queue
: 0;
1018 teb
->queue
= hQueue
;
1020 if ( GetTaskQueue16( teb
->htask16
) == oldQueue
)
1021 SetTaskQueue16( teb
->htask16
, hQueue
);
1027 /***********************************************************************
1028 * GetThreadQueue (KERNEL.464)
1030 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1034 teb
= NtCurrentTeb();
1035 else if ( HIWORD(thread
) )
1036 teb
= THREAD_IdToTEB( thread
);
1037 else if ( IsTask16( (HTASK16
)thread
) )
1038 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1040 return (HQUEUE16
)(teb
? teb
->queue
: 0);
1043 /***********************************************************************
1044 * SetFastQueue (KERNEL.624)
1046 VOID WINAPI
SetFastQueue16( DWORD thread
, HANDLE hQueue
)
1050 teb
= NtCurrentTeb();
1051 else if ( HIWORD(thread
) )
1052 teb
= THREAD_IdToTEB( thread
);
1053 else if ( IsTask16( (HTASK16
)thread
) )
1054 teb
= (TASK_GetPtr( (HANDLE16
)thread
))->teb
;
1056 if ( teb
) teb
->queue
= (HQUEUE16
) hQueue
;
1059 /***********************************************************************
1060 * GetFastQueue (KERNEL.625)
1062 HANDLE WINAPI
GetFastQueue16( void )
1064 HANDLE ret
= (HANDLE
)NtCurrentTeb()->queue
;
1066 if (!ret
) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1070 /***********************************************************************
1071 * SwitchStackTo (KERNEL.108)
1073 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1076 STACK16FRAME
*oldFrame
, *newFrame
;
1077 INSTANCEDATA
*pData
;
1080 if (!(pTask
= TASK_GetCurrent())) return;
1081 if (!(pData
= (INSTANCEDATA
*)GlobalLock16( seg
))) return;
1082 TRACE("old=%04x:%04x new=%04x:%04x\n",
1083 SELECTOROF( pTask
->teb
->cur_stack
),
1084 OFFSETOF( pTask
->teb
->cur_stack
), seg
, ptr
);
1086 /* Save the old stack */
1088 oldFrame
= THREAD_STACK16( pTask
->teb
);
1089 /* pop frame + args and push bp */
1090 pData
->old_ss_sp
= pTask
->teb
->cur_stack
+ sizeof(STACK16FRAME
)
1092 *(WORD
*)MapSL(pData
->old_ss_sp
) = oldFrame
->bp
;
1093 pData
->stacktop
= top
;
1094 pData
->stackmin
= ptr
;
1095 pData
->stackbottom
= ptr
;
1097 /* Switch to the new stack */
1099 /* Note: we need to take the 3 arguments into account; otherwise,
1100 * the stack will underflow upon return from this function.
1102 copySize
= oldFrame
->bp
- OFFSETOF(pData
->old_ss_sp
);
1103 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1104 pTask
->teb
->cur_stack
= MAKESEGPTR( seg
, ptr
- copySize
);
1105 newFrame
= THREAD_STACK16( pTask
->teb
);
1107 /* Copy the stack frame and the local variables to the new stack */
1109 memmove( newFrame
, oldFrame
, copySize
);
1111 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1115 /***********************************************************************
1116 * SwitchStackBack (KERNEL.109)
1118 void WINAPI
SwitchStackBack16( CONTEXT86
*context
)
1120 STACK16FRAME
*oldFrame
, *newFrame
;
1121 INSTANCEDATA
*pData
;
1123 if (!(pData
= (INSTANCEDATA
*)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack
))))
1125 if (!pData
->old_ss_sp
)
1127 WARN("No previous SwitchStackTo\n" );
1130 TRACE("restoring stack %04x:%04x\n",
1131 SELECTOROF(pData
->old_ss_sp
), OFFSETOF(pData
->old_ss_sp
) );
1133 oldFrame
= CURRENT_STACK16
;
1135 /* Pop bp from the previous stack */
1137 BP_reg(context
) = *(WORD
*)MapSL(pData
->old_ss_sp
);
1138 pData
->old_ss_sp
+= sizeof(WORD
);
1140 /* Switch back to the old stack */
1142 NtCurrentTeb()->cur_stack
= pData
->old_ss_sp
- sizeof(STACK16FRAME
);
1143 context
->SegSs
= SELECTOROF(pData
->old_ss_sp
);
1144 context
->Esp
= OFFSETOF(pData
->old_ss_sp
) - sizeof(DWORD
); /*ret addr*/
1145 pData
->old_ss_sp
= 0;
1147 /* Build a stack frame for the return */
1149 newFrame
= CURRENT_STACK16
;
1150 newFrame
->frame32
= oldFrame
->frame32
;
1151 newFrame
->module_cs
= oldFrame
->module_cs
;
1152 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1153 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1157 /***********************************************************************
1158 * GetTaskQueueDS (KERNEL.118)
1160 void WINAPI
GetTaskQueueDS16(void)
1162 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1166 /***********************************************************************
1167 * GetTaskQueueES (KERNEL.119)
1169 void WINAPI
GetTaskQueueES16(void)
1171 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1175 /***********************************************************************
1176 * GetCurrentTask (KERNEL32.@)
1178 HTASK16 WINAPI
GetCurrentTask(void)
1180 return NtCurrentTeb()->htask16
;
1183 /***********************************************************************
1184 * GetCurrentTask (KERNEL.36)
1186 DWORD WINAPI
WIN16_GetCurrentTask(void)
1188 /* This is the version used by relay code; the first task is */
1189 /* returned in the high word of the result */
1190 return MAKELONG( GetCurrentTask(), hFirstTask
);
1194 /***********************************************************************
1195 * GetCurrentPDB (KERNEL.37)
1197 * UNDOC: returns PSP of KERNEL in high word
1199 DWORD WINAPI
GetCurrentPDB16(void)
1203 if (!(pTask
= TASK_GetCurrent())) return 0;
1204 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1208 /***********************************************************************
1209 * GetCurPID (KERNEL.157)
1211 DWORD WINAPI
GetCurPID16( DWORD unused
)
1217 /***********************************************************************
1218 * GetInstanceData (KERNEL.54)
1220 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1222 char *ptr
= (char *)GlobalLock16( instance
);
1223 if (!ptr
|| !len
) return 0;
1224 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1225 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1230 /***********************************************************************
1231 * GetExeVersion (KERNEL.105)
1233 WORD WINAPI
GetExeVersion16(void)
1237 if (!(pTask
= TASK_GetCurrent())) return 0;
1238 return pTask
->version
;
1242 /***********************************************************************
1243 * SetErrorMode (KERNEL.107)
1245 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1248 if (!(pTask
= TASK_GetCurrent())) return 0;
1249 pTask
->error_mode
= mode
;
1250 return SetErrorMode( mode
);
1254 /***********************************************************************
1255 * GetNumTasks (KERNEL.152)
1257 UINT16 WINAPI
GetNumTasks16(void)
1263 /***********************************************************************
1264 * GetTaskDS (KERNEL.155)
1266 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1267 * I don't think we need to bother with this.
1269 HINSTANCE16 WINAPI
GetTaskDS16(void)
1273 if (!(pTask
= TASK_GetCurrent())) return 0;
1274 return GlobalHandleToSel16(pTask
->hInstance
);
1277 /***********************************************************************
1278 * GetDummyModuleHandleDS (KERNEL.602)
1280 WORD WINAPI
GetDummyModuleHandleDS16(void)
1285 if (!(pTask
= TASK_GetCurrent())) return 0;
1286 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1287 selector
= GlobalHandleToSel16( pTask
->hModule
);
1288 CURRENT_DS
= selector
;
1292 /***********************************************************************
1293 * IsTask (KERNEL.320)
1294 * IsTask16 (KERNEL32.@)
1296 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1300 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1301 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1302 return (pTask
->magic
== TDB_MAGIC
);
1306 /***********************************************************************
1307 * IsWinOldApTask (KERNEL.158)
1309 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1311 /* should return bit 0 of byte 0x48 in PSP */
1315 /***********************************************************************
1316 * SetTaskSignalProc (KERNEL.38)
1318 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1323 if (!hTask
) hTask
= GetCurrentTask();
1324 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1325 oldProc
= pTask
->userhandler
;
1326 pTask
->userhandler
= proc
;
1330 /***********************************************************************
1331 * TASK_CallTaskSignalProc
1333 /* ### start build ### */
1334 extern WORD CALLBACK
TASK_CallTo16_word_wwwww(FARPROC16
,WORD
,WORD
,WORD
,WORD
,WORD
);
1335 /* ### stop build ### */
1336 void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
1338 TDB
*pTask
= TASK_GetCurrent();
1339 if ( !pTask
|| !pTask
->userhandler
) return;
1341 TASK_CallTo16_word_wwwww( pTask
->userhandler
,
1342 hTaskOrModule
, uCode
, 0,
1343 pTask
->hInstance
, pTask
->hQueue
);
1346 /***********************************************************************
1347 * SetSigHandler (KERNEL.140)
1349 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1350 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1352 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1353 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1355 if (flag
!= 1) return 0;
1356 if (!newmode
) newhandler
= NULL
; /* Default handler */
1361 if (!(pTask
= TASK_GetCurrent())) return 0;
1362 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1363 pTask
->signal_flags
= newmode
;
1364 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1365 pTask
->sighandler
= newhandler
;
1371 /***********************************************************************
1372 * GlobalNotify (KERNEL.154)
1374 * Note that GlobalNotify does _not_ return the old NotifyProc
1375 * -- contrary to LocalNotify !!
1377 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1381 if (!(pTask
= TASK_GetCurrent())) return;
1382 pTask
->discardhandler
= proc
;
1386 /***********************************************************************
1389 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1394 /* Check for module handle */
1396 if (!(ptr
= GlobalLock16( handle
))) return 0;
1397 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1399 /* Search for this handle inside all tasks */
1401 *hTask
= hFirstTask
;
1404 TDB
*pTask
= TASK_GetPtr( *hTask
);
1405 if ((*hTask
== handle
) ||
1406 (pTask
->hInstance
== handle
) ||
1407 (pTask
->hQueue
== handle
) ||
1408 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1409 *hTask
= pTask
->hNext
;
1412 /* Check the owner for module handle */
1414 owner
= FarGetOwner16( handle
);
1415 if (!(ptr
= GlobalLock16( owner
))) return 0;
1416 if (((NE_MODULE
*)ptr
)->magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1418 /* Search for the owner inside all tasks */
1420 *hTask
= hFirstTask
;
1423 TDB
*pTask
= TASK_GetPtr( *hTask
);
1424 if ((*hTask
== owner
) ||
1425 (pTask
->hInstance
== owner
) ||
1426 (pTask
->hQueue
== owner
) ||
1427 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1428 *hTask
= pTask
->hNext
;
1434 /***********************************************************************
1435 * GetExePtr (KERNEL.133)
1437 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1440 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1441 STACK16FRAME
*frame
= CURRENT_STACK16
;
1442 frame
->ecx
= hModule
;
1443 if (hTask
) frame
->es
= hTask
;
1448 /***********************************************************************
1451 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1454 return GetExePtrHelper( handle
, &hTask
);
1458 /***********************************************************************
1459 * TaskFirst (TOOLHELP.63)
1461 BOOL16 WINAPI
TaskFirst16( TASKENTRY
*lpte
)
1463 lpte
->hNext
= hFirstTask
;
1464 return TaskNext16( lpte
);
1468 /***********************************************************************
1469 * TaskNext (TOOLHELP.64)
1471 BOOL16 WINAPI
TaskNext16( TASKENTRY
*lpte
)
1474 INSTANCEDATA
*pInstData
;
1476 TRACE_(toolhelp
)("(%p): task=%04x\n", lpte
, lpte
->hNext
);
1477 if (!lpte
->hNext
) return FALSE
;
1479 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1481 pTask
= TASK_GetPtr( lpte
->hNext
);
1482 if (!pTask
|| pTask
->magic
!= TDB_MAGIC
) return FALSE
;
1483 if (pTask
->hInstance
)
1485 lpte
->hNext
= pTask
->hNext
;
1487 pInstData
= MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask
->hInstance
), 0 ) );
1488 lpte
->hTask
= lpte
->hNext
;
1489 lpte
->hTaskParent
= pTask
->hParent
;
1490 lpte
->hInst
= pTask
->hInstance
;
1491 lpte
->hModule
= pTask
->hModule
;
1492 lpte
->wSS
= SELECTOROF( pTask
->teb
->cur_stack
);
1493 lpte
->wSP
= OFFSETOF( pTask
->teb
->cur_stack
);
1494 lpte
->wStackTop
= pInstData
->stacktop
;
1495 lpte
->wStackMinimum
= pInstData
->stackmin
;
1496 lpte
->wStackBottom
= pInstData
->stackbottom
;
1497 lpte
->wcEvents
= pTask
->nEvents
;
1498 lpte
->hQueue
= pTask
->hQueue
;
1499 lstrcpynA( lpte
->szModule
, pTask
->module_name
, sizeof(lpte
->szModule
) );
1500 lpte
->wPSPOffset
= 0x100; /*??*/
1501 lpte
->hNext
= pTask
->hNext
;
1506 /***********************************************************************
1507 * TaskFindHandle (TOOLHELP.65)
1509 BOOL16 WINAPI
TaskFindHandle16( TASKENTRY
*lpte
, HTASK16 hTask
)
1511 lpte
->hNext
= hTask
;
1512 return TaskNext16( lpte
);
1516 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1518 /**************************************************************************
1519 * FatalAppExit (KERNEL.137)
1521 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1523 TDB
*pTask
= TASK_GetCurrent();
1525 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1527 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1530 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1533 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1537 ERR( "%s\n", debugstr_a(str
) );
1544 /***********************************************************************
1545 * TerminateApp (TOOLHELP.77)
1547 * See "Undocumented Windows".
1549 void WINAPI
TerminateApp16(HTASK16 hTask
, WORD wFlags
)
1551 if (hTask
&& hTask
!= GetCurrentTask())
1553 FIXME("cannot terminate task %x\n", hTask
);
1557 if (wFlags
& NO_UAE_BOX
)
1560 old_mode
= SetErrorMode16(0);
1561 SetErrorMode16(old_mode
|SEM_NOGPFAULTERRORBOX
);
1563 FatalAppExit16( 0, NULL
);
1565 /* hmm, we're still alive ?? */
1567 /* check undocumented flag */
1568 if (!(wFlags
& 0x8000))
1569 TASK_CallTaskSignalProc( USIG16_TERMINATION
, hTask
);
1571 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1572 but let's just call ExitThread */
1577 /***********************************************************************
1578 * GetAppCompatFlags (KERNEL.354)
1580 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1584 if (!hTask
) hTask
= GetCurrentTask();
1585 if (!(pTask
=TASK_GetPtr( hTask
))) return 0;
1586 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1587 return pTask
->compat_flags
;