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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include "wine/winbase16.h"
35 #include "kernel16_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(task
);
51 /* Segment containing MakeProcInstance() thunks */
54 WORD next
; /* Selector of next segment */
55 WORD magic
; /* Thunks signature */
57 WORD free
; /* Head of the free list */
58 struct thunk thunks
[1];
63 #define THUNK_MAGIC ('P' | ('T' << 8))
65 /* Min. number of thunks allocated when creating a new segment */
68 #define TDB_MAGIC ('T' | ('D' << 8))
70 static THHOOK DefaultThhook
;
71 THHOOK
*pThhook
= &DefaultThhook
;
73 #define hFirstTask (pThhook->HeadTDB)
74 #define hLockedTask (pThhook->LockTDB)
76 static UINT16 nTaskCount
= 0;
78 static HTASK16 initial_task
, main_task
;
80 /***********************************************************************
83 void TASK_InstallTHHook( THHOOK
*pNewThhook
)
85 THHOOK
*pOldThhook
= pThhook
;
87 pThhook
= pNewThhook
? pNewThhook
: &DefaultThhook
;
89 *pThhook
= *pOldThhook
;
92 /***********************************************************************
95 static TDB
*TASK_GetPtr( HTASK16 hTask
)
97 return GlobalLock16( hTask
);
101 /***********************************************************************
104 TDB
*TASK_GetCurrent(void)
106 return TASK_GetPtr( GetCurrentTask() );
110 /***********************************************************************
113 static void TASK_LinkTask( HTASK16 hTask
)
118 if (!(pTask
= TASK_GetPtr( hTask
))) return;
119 prevTask
= &hFirstTask
;
122 TDB
*prevTaskPtr
= TASK_GetPtr( *prevTask
);
123 if (prevTaskPtr
->priority
>= pTask
->priority
) break;
124 prevTask
= &prevTaskPtr
->hNext
;
126 pTask
->hNext
= *prevTask
;
132 /***********************************************************************
135 static void TASK_UnlinkTask( HTASK16 hTask
)
140 prevTask
= &hFirstTask
;
141 while (*prevTask
&& (*prevTask
!= hTask
))
143 pTask
= TASK_GetPtr( *prevTask
);
144 prevTask
= &pTask
->hNext
;
148 pTask
= TASK_GetPtr( *prevTask
);
149 *prevTask
= pTask
->hNext
;
156 /***********************************************************************
159 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
160 * and containing 'count' entries.
162 static void TASK_CreateThunks( HGLOBAL16 handle
, WORD offset
, WORD count
)
167 pThunk
= (THUNKS
*)((BYTE
*)GlobalLock16( handle
) + offset
);
169 pThunk
->magic
= THUNK_MAGIC
;
170 pThunk
->free
= FIELD_OFFSET( THUNKS
, thunks
);
171 for (i
= 0; i
< count
-1; i
++)
172 *(WORD
*)&pThunk
->thunks
[i
] = FIELD_OFFSET( THUNKS
, thunks
[i
+1] );
173 *(WORD
*)&pThunk
->thunks
[i
] = 0; /* Last thunk */
177 /***********************************************************************
180 * Allocate a thunk for MakeProcInstance().
182 static SEGPTR
TASK_AllocThunk(void)
188 if (!(pTask
= TASK_GetCurrent())) return 0;
189 sel
= pTask
->hCSAlias
;
190 pThunk
= (THUNKS
*)pTask
->thunks
;
191 base
= (char *)pThunk
- (char *)pTask
;
192 while (!pThunk
->free
)
195 if (!sel
) /* Allocate a new segment */
197 sel
= GLOBAL_Alloc( GMEM_FIXED
, FIELD_OFFSET( THUNKS
, thunks
[MIN_THUNKS
] ),
198 pTask
->hPDB
, LDT_FLAGS_CODE
);
200 TASK_CreateThunks( sel
, 0, MIN_THUNKS
);
203 pThunk
= GlobalLock16( sel
);
206 base
+= pThunk
->free
;
207 pThunk
->free
= *(WORD
*)((BYTE
*)pThunk
+ pThunk
->free
);
208 return MAKESEGPTR( sel
, base
);
212 /***********************************************************************
215 * Free a MakeProcInstance() thunk.
217 static BOOL
TASK_FreeThunk( SEGPTR thunk
)
223 if (!(pTask
= TASK_GetCurrent())) return FALSE
;
224 sel
= pTask
->hCSAlias
;
225 pThunk
= (THUNKS
*)pTask
->thunks
;
226 base
= (char *)pThunk
- (char *)pTask
;
227 while (sel
&& (sel
!= HIWORD(thunk
)))
230 pThunk
= GlobalLock16( sel
);
233 if (!sel
) return FALSE
;
234 *(WORD
*)((BYTE
*)pThunk
+ LOWORD(thunk
) - base
) = pThunk
->free
;
235 pThunk
->free
= LOWORD(thunk
) - base
;
240 /***********************************************************************
243 * NOTE: This routine might be called by a Win32 thread. Thus, we need
244 * to be careful to protect global data structures. We do this
245 * by entering the Win16Lock while linking the task into the
248 static TDB
*TASK_Create( NE_MODULE
*pModule
, UINT16 cmdShow
, LPCSTR cmdline
, BYTE len
)
253 char curdir
[MAX_PATH
];
254 HMODULE16 hModule
= pModule
? pModule
->self
: 0;
256 /* Allocate the task structure */
258 hTask
= GlobalAlloc16( GMEM_FIXED
| GMEM_ZEROINIT
, sizeof(TDB
) );
259 if (!hTask
) return NULL
;
260 pTask
= TASK_GetPtr( hTask
);
261 FarSetOwner16( hTask
, hModule
);
263 /* Fill the task structure */
265 pTask
->hSelf
= hTask
;
267 pTask
->version
= pModule
? pModule
->ne_expver
: 0x0400;
268 pTask
->hModule
= hModule
;
269 pTask
->hParent
= GetCurrentTask();
270 pTask
->magic
= TDB_MAGIC
;
271 pTask
->nCmdShow
= cmdShow
;
273 GetCurrentDirectoryA( sizeof(curdir
), curdir
);
274 GetShortPathNameA( curdir
, curdir
, sizeof(curdir
) );
275 pTask
->curdrive
= (curdir
[0] - 'A') | 0x80;
276 lstrcpynA( pTask
->curdir
, curdir
+ 2, sizeof(pTask
->curdir
) );
278 /* Create the thunks block */
280 TASK_CreateThunks( hTask
, (char *)pTask
->thunks
- (char *)pTask
, 7 );
282 /* Copy the module name */
286 char name
[sizeof(pTask
->module_name
)+1];
288 GetModuleName16( hModule
, name
, sizeof(name
) );
289 len
= strlen(name
) + 1;
290 memcpy(pTask
->module_name
, name
, min(len
,sizeof(pTask
->module_name
)));
291 pTask
->compat_flags
= GetProfileIntA( "Compatibility", name
, 0 );
294 /* Allocate a selector for the PDB */
296 pTask
->hPDB
= GLOBAL_CreateBlock( GMEM_FIXED
, &pTask
->pdb
, sizeof(PDB16
),
297 hModule
, LDT_FLAGS_DATA
);
301 pTask
->pdb
.int20
= 0x20cd;
302 pTask
->pdb
.dispatcher
[0] = 0x9a; /* ljmp */
303 proc
= GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
304 memcpy( &pTask
->pdb
.dispatcher
[1], &proc
, sizeof(proc
) );
305 pTask
->pdb
.savedint22
= 0;
306 pTask
->pdb
.savedint23
= 0;
307 pTask
->pdb
.savedint24
= 0;
308 pTask
->pdb
.fileHandlesPtr
=
309 MAKESEGPTR( GlobalHandleToSel16(pTask
->hPDB
), FIELD_OFFSET( PDB16
, fileHandles
));
310 pTask
->pdb
.hFileHandles
= 0;
311 memset( pTask
->pdb
.fileHandles
, 0xff, sizeof(pTask
->pdb
.fileHandles
) );
312 /* FIXME: should we make a copy of the environment? */
313 pTask
->pdb
.environment
= SELECTOROF(GetDOSEnvironment16());
314 pTask
->pdb
.nbFiles
= 20;
316 /* Fill the command line */
320 cmdline
= GetCommandLineA();
321 /* remove the first word (program name) */
323 if (!(cmdline
= strchr( cmdline
+1, '"' ))) cmdline
= GetCommandLineA();
324 while (*cmdline
&& (*cmdline
!= ' ') && (*cmdline
!= '\t')) cmdline
++;
325 while ((*cmdline
== ' ') || (*cmdline
== '\t')) cmdline
++;
326 len
= strlen(cmdline
);
328 if (len
>= sizeof(pTask
->pdb
.cmdLine
)) len
= sizeof(pTask
->pdb
.cmdLine
)-1;
329 pTask
->pdb
.cmdLine
[0] = len
;
330 memcpy( pTask
->pdb
.cmdLine
+ 1, cmdline
, len
);
331 /* pTask->pdb.cmdLine[len+1] = 0; */
333 TRACE("cmdline='%.*s' task=%04x\n", len
, cmdline
, hTask
);
335 /* Allocate a code segment alias for the TDB */
337 pTask
->hCSAlias
= GLOBAL_CreateBlock( GMEM_FIXED
, pTask
, sizeof(TDB
),
338 pTask
->hPDB
, LDT_FLAGS_CODE
);
340 /* Default DTA overwrites command line */
342 pTask
->dta
= MAKESEGPTR( pTask
->hPDB
, FIELD_OFFSET( PDB16
, cmdLine
));
344 /* Create scheduler event for 16-bit tasks */
346 if ( !(pTask
->flags
& TDBF_WIN32
) )
347 NtCreateEvent( &pTask
->hEvent
, EVENT_ALL_ACCESS
, NULL
, NotificationEvent
, FALSE
);
349 if (!initial_task
) initial_task
= hTask
;
355 /***********************************************************************
358 static void TASK_DeleteTask( HTASK16 hTask
)
363 if (!(pTask
= TASK_GetPtr( hTask
))) return;
366 pTask
->magic
= 0xdead; /* invalidate signature */
368 /* Free the selector aliases */
370 GLOBAL_FreeBlock( pTask
->hCSAlias
);
371 GLOBAL_FreeBlock( pTask
->hPDB
);
373 /* Free the task module */
375 FreeModule16( pTask
->hModule
);
377 /* Free the task structure itself */
379 GlobalFree16( hTask
);
381 /* Free all memory used by this task (including the 32-bit stack, */
382 /* the environment block and the thunk segments). */
384 GlobalFreeAll16( hPDB
);
388 /***********************************************************************
389 * TASK_CreateMainTask
391 * Create a task for the main (32-bit) process.
393 void TASK_CreateMainTask(void)
396 STARTUPINFOA startup_info
;
397 UINT cmdShow
= 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
399 GetStartupInfoA( &startup_info
);
400 if (startup_info
.dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup_info
.wShowWindow
;
401 pTask
= TASK_Create( NULL
, cmdShow
, NULL
, 0 );
404 ERR("could not create task for main process\n");
408 pTask
->flags
|= TDBF_WIN32
;
409 pTask
->hInstance
= 0;
410 pTask
->hPrevInstance
= 0;
411 pTask
->teb
= NtCurrentTeb();
413 /* Add the task to the linked list */
414 /* (no need to get the win16 lock, we are the only thread at this point) */
415 TASK_LinkTask( pTask
->hSelf
);
416 main_task
= pTask
->hSelf
;
423 WIN16_SUBSYSTEM_TIB
*tib
;
426 /* allocate the win16 TIB for a new 16-bit task */
427 static WIN16_SUBSYSTEM_TIB
*allocate_win16_tib( TDB
*pTask
)
429 WCHAR path
[MAX_PATH
];
430 WIN16_SUBSYSTEM_TIB
*tib
;
431 UNICODE_STRING
*curdir
;
432 NE_MODULE
*pModule
= NE_GetPtr( pTask
->hModule
);
434 if (!(tib
= HeapAlloc( GetProcessHeap(), 0, sizeof(*tib
) ))) return NULL
;
435 MultiByteToWideChar( CP_ACP
, 0, NE_MODULE_NAME(pModule
), -1, path
, MAX_PATH
);
436 GetLongPathNameW( path
, path
, MAX_PATH
);
437 if (RtlCreateUnicodeString( &tib
->exe_str
, path
)) tib
->exe_name
= &tib
->exe_str
;
438 else tib
->exe_name
= NULL
;
441 if (NtCurrentTeb()->Tib
.SubSystemTib
)
442 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
444 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
445 tib
->curdir
.DosPath
.MaximumLength
= sizeof(tib
->curdir_buffer
);
446 tib
->curdir
.DosPath
.Length
= min( curdir
->Length
, tib
->curdir
.DosPath
.MaximumLength
-sizeof(WCHAR
) );
447 tib
->curdir
.DosPath
.Buffer
= tib
->curdir_buffer
;
448 tib
->curdir
.Handle
= 0;
449 memcpy( tib
->curdir_buffer
, curdir
->Buffer
, tib
->curdir
.DosPath
.Length
);
450 tib
->curdir_buffer
[tib
->curdir
.DosPath
.Length
/sizeof(WCHAR
)] = 0;
455 static inline void free_win16_tib( WIN16_SUBSYSTEM_TIB
*tib
)
457 if (tib
->exe_name
) RtlFreeUnicodeString( tib
->exe_name
);
458 HeapFree( GetProcessHeap(), 0, tib
);
461 /* startup routine for a new 16-bit thread */
462 static DWORD CALLBACK
task_start( LPVOID p
)
464 struct create_data
*data
= p
;
465 TDB
*pTask
= data
->task
;
468 kernel_get_thread_data()->htask16
= pTask
->hSelf
;
469 NtCurrentTeb()->Tib
.SubSystemTib
= data
->tib
;
470 HeapFree( GetProcessHeap(), 0, data
);
473 TASK_LinkTask( pTask
->hSelf
);
474 pTask
->teb
= NtCurrentTeb();
475 ret
= NE_StartTask();
481 /***********************************************************************
484 * Spawn a new 16-bit task.
486 HTASK16
TASK_SpawnTask( NE_MODULE
*pModule
, WORD cmdShow
,
487 LPCSTR cmdline
, BYTE len
, HANDLE
*hThread
)
489 struct create_data
*data
= NULL
;
490 WIN16_SUBSYSTEM_TIB
*tib
;
493 if (!(pTask
= TASK_Create( pModule
, cmdShow
, cmdline
, len
))) return 0;
494 if (!(tib
= allocate_win16_tib( pTask
))) goto failed
;
495 if (!(data
= HeapAlloc( GetProcessHeap(), 0, sizeof(*data
)))) goto failed
;
498 if (!(*hThread
= CreateThread( NULL
, 0, task_start
, data
, 0, NULL
))) goto failed
;
502 if (tib
) free_win16_tib( tib
);
503 HeapFree( GetProcessHeap(), 0, data
);
504 TASK_DeleteTask( pTask
->hSelf
);
509 /***********************************************************************
510 * TASK_GetTaskFromThread
512 HTASK16
TASK_GetTaskFromThread( DWORD thread
)
514 TDB
*p
= TASK_GetPtr( hFirstTask
);
517 if (p
->teb
->ClientId
.UniqueThread
== (HANDLE
)thread
) return p
->hSelf
;
518 p
= TASK_GetPtr( p
->hNext
);
524 /***********************************************************************
525 * TASK_CallTaskSignalProc
527 static void TASK_CallTaskSignalProc( UINT16 uCode
, HANDLE16 hTaskOrModule
)
530 TDB
*pTask
= TASK_GetCurrent();
532 if ( !pTask
|| !pTask
->userhandler
) return;
534 args
[4] = hTaskOrModule
;
537 args
[1] = pTask
->hInstance
;
538 args
[0] = pTask
->hQueue
;
539 WOWCallback16Ex( (DWORD
)pTask
->userhandler
, WCB16_PASCAL
, sizeof(args
), args
, NULL
);
543 /***********************************************************************
546 void TASK_ExitTask(void)
548 WIN16_SUBSYSTEM_TIB
*tib
;
552 pTask
= TASK_GetCurrent();
555 TRACE("Killing task %04x\n", pTask
->hSelf
);
557 /* Perform USER cleanup */
559 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
561 /* Remove the task from the list to be sure we never switch back to it */
562 TASK_UnlinkTask( pTask
->hSelf
);
564 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
566 TRACE("this is the last task, exiting\n" );
572 if ( hLockedTask
== pTask
->hSelf
)
575 TASK_DeleteTask( pTask
->hSelf
);
577 if ((tib
= NtCurrentTeb()->Tib
.SubSystemTib
))
579 free_win16_tib( tib
);
580 NtCurrentTeb()->Tib
.SubSystemTib
= NULL
;
583 /* ... and completely release the Win16Lock, just in case. */
584 ReleaseThunkLock( &lockCount
);
588 /***********************************************************************
589 * ExitKernel (KERNEL.2)
591 * Clean-up everything and exit the Wine process.
593 void WINAPI
ExitKernel16(void)
595 WriteOutProfiles16();
596 TerminateProcess( GetCurrentProcess(), 0 );
600 /***********************************************************************
601 * InitTask (KERNEL.91)
603 * Called by the application startup code.
605 void WINAPI
InitTask16( CONTEXT
*context
)
608 INSTANCEDATA
*pinstance
;
612 if (!(pTask
= TASK_GetCurrent())) return;
614 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
615 as some apps, notably Visual Basic apps, *modify* the heap/stack
616 size of the instance data segment before calling InitTask() */
618 /* Initialize the INSTANCEDATA structure */
619 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
620 pinstance
->stackmin
= CURRENT_SP
+ sizeof( STACK16FRAME
);
621 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
622 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
623 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
625 /* Initialize the local heap */
626 if (LOWORD(context
->Ecx
))
627 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
629 /* Initialize implicitly loaded DLLs */
630 NE_InitializeDLLs( pTask
->hModule
);
631 NE_DllProcessAttach( pTask
->hModule
);
633 /* Registers on return are:
634 * ax 1 if OK, 0 on error
635 * cx stack limit in bytes
636 * dx cmdShow parameter
637 * si instance handle of the previous instance
638 * di instance handle of the new task
639 * es:bx pointer to command line inside PSP
641 * 0 (=%bp) is pushed on the stack
643 ptr
= stack16_push( sizeof(WORD
) );
644 *(WORD
*)MapSL(ptr
) = 0;
649 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
652 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
653 while ((*p
== ' ') || (*p
== '\t')) p
++;
654 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
656 context
->Ecx
= pinstance
->stacktop
;
657 context
->Edx
= pTask
->nCmdShow
;
658 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
659 context
->Edi
= (DWORD
)pTask
->hInstance
;
660 context
->SegEs
= (WORD
)pTask
->hPDB
;
664 /***********************************************************************
665 * WaitEvent (KERNEL.30)
667 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
671 if (!hTask
) hTask
= GetCurrentTask();
672 pTask
= TASK_GetPtr( hTask
);
674 if (pTask
->flags
& TDBF_WIN32
)
676 FIXME("called for Win32 thread (%04lx)!\n", GetCurrentThreadId());
680 if (pTask
->nEvents
> 0)
686 if (pTask
->teb
== NtCurrentTeb())
690 NtResetEvent( pTask
->hEvent
, NULL
);
691 ReleaseThunkLock( &lockCount
);
692 SYSLEVEL_CheckNotLevel( 1 );
693 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
694 RestoreThunkLock( lockCount
);
695 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
697 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
703 /***********************************************************************
704 * PostEvent (KERNEL.31)
706 void WINAPI
PostEvent16( HTASK16 hTask
)
710 if (!hTask
) hTask
= GetCurrentTask();
711 if (!(pTask
= TASK_GetPtr( hTask
))) return;
713 if (pTask
->flags
& TDBF_WIN32
)
715 FIXME("called for Win32 thread (%04lx)!\n", (DWORD
)pTask
->teb
->ClientId
.UniqueThread
);
721 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
725 /***********************************************************************
726 * SetPriority (KERNEL.32)
728 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
733 if (!hTask
) hTask
= GetCurrentTask();
734 if (!(pTask
= TASK_GetPtr( hTask
))) return;
735 newpriority
= pTask
->priority
+ delta
;
736 if (newpriority
< -32) newpriority
= -32;
737 else if (newpriority
> 15) newpriority
= 15;
739 pTask
->priority
= newpriority
+ 1;
740 TASK_UnlinkTask( pTask
->hSelf
);
741 TASK_LinkTask( pTask
->hSelf
);
746 /***********************************************************************
747 * LockCurrentTask (KERNEL.33)
749 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
751 if (bLock
) hLockedTask
= GetCurrentTask();
752 else hLockedTask
= 0;
757 /***********************************************************************
758 * IsTaskLocked (KERNEL.122)
760 HTASK16 WINAPI
IsTaskLocked16(void)
766 /***********************************************************************
767 * OldYield (KERNEL.117)
769 void WINAPI
OldYield16(void)
773 ReleaseThunkLock(&count
);
774 RestoreThunkLock(count
);
777 /***********************************************************************
778 * WIN32_OldYield (KERNEL.447)
780 void WINAPI
WIN32_OldYield16(void)
784 ReleaseThunkLock(&count
);
785 RestoreThunkLock(count
);
788 /***********************************************************************
789 * DirectedYield (KERNEL.150)
791 void WINAPI
DirectedYield16( HTASK16 hTask
)
796 /***********************************************************************
799 void WINAPI
Yield16(void)
801 TDB
*pCurTask
= TASK_GetCurrent();
803 if (pCurTask
&& pCurTask
->hQueue
)
805 HMODULE mod
= GetModuleHandleA( "user32.dll" );
808 BOOL (WINAPI
*pPeekMessageW
)( MSG
*msg
, HWND hwnd
, UINT first
, UINT last
, UINT flags
);
809 pPeekMessageW
= (void *)GetProcAddress( mod
, "PeekMessageW" );
813 pPeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
| PM_QS_SENDMESSAGE
);
821 /***********************************************************************
822 * KERNEL_490 (KERNEL.490)
824 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
826 if ( !someTask
) return 0;
828 FIXME("(%04x): stub\n", someTask
);
832 /***********************************************************************
833 * MakeProcInstance (KERNEL.51)
835 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
840 WORD hInstanceSelector
;
842 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
844 TRACE("(%p, %04x);\n", func
, hInstance
);
847 /* Win95 actually protects via SEH, but this is better for debugging */
848 WARN("Ouch ! Called with invalid func %p !\n", func
);
852 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
854 && (hInstance
!= 0xffff) )
856 /* calling MPI with a foreign DSEG is invalid ! */
857 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
858 hInstance
,CURRENT_DS
);
861 /* Always use the DSEG that MPI was entered with.
862 * We used to set hInstance to GetTaskDS16(), but this should be wrong
863 * as CURRENT_DS provides the DSEG value we need.
864 * ("calling" DS, *not* "task" DS !) */
865 hInstanceSelector
= CURRENT_DS
;
866 hInstance
= GlobalHandle16(hInstanceSelector
);
868 /* no thunking for DLLs */
869 if (NE_GetPtr(FarGetOwner16(hInstance
))->ne_flags
& NE_FFLAGS_LIBMODULE
)
872 thunkaddr
= TASK_AllocThunk();
873 if (!thunkaddr
) return NULL
;
874 thunk
= MapSL( thunkaddr
);
875 lfunc
= MapSL( (SEGPTR
)func
);
877 TRACE("(%p,%04x): got thunk %08lx\n", func
, hInstance
, thunkaddr
);
878 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
879 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
881 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
884 thunk
->movw
= 0xb8; /* movw instance, %ax */
885 thunk
->instance
= hInstanceSelector
;
886 thunk
->ljmp
= 0xea; /* ljmp func */
888 return (FARPROC16
)thunkaddr
;
889 /* CX reg indicates if thunkaddr != NULL, implement if needed */
893 /***********************************************************************
894 * FreeProcInstance (KERNEL.52)
896 void WINAPI
FreeProcInstance16( FARPROC16 func
)
898 TRACE("(%p)\n", func
);
899 TASK_FreeThunk( (SEGPTR
)func
);
902 /**********************************************************************
903 * TASK_GetCodeSegment
905 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
906 * and logical segment number of a given code segment.
908 * 'proc' either *is* already a pair of module handle and segment number,
909 * in which case there's nothing to do. Otherwise, it is a pointer to
910 * a function, and we need to retrieve the code segment. If the pointer
911 * happens to point to a thunk, we'll retrieve info about the code segment
912 * where the function pointed to by the thunk resides, not the thunk itself.
914 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
915 * the function the snoop code will return to ...
918 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
919 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
921 NE_MODULE
*pModule
= NULL
;
922 SEGTABLEENTRY
*pSeg
= NULL
;
925 /* Try pair of module handle / segment number */
926 pModule
= GlobalLock16( HIWORD( proc
) );
927 if ( pModule
&& pModule
->ne_magic
== IMAGE_OS2_SIGNATURE
)
929 segNr
= LOWORD( proc
);
930 if ( segNr
&& segNr
<= pModule
->ne_cseg
)
931 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
934 /* Try thunk or function */
937 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
940 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
941 selector
= thunk
[6] + (thunk
[7] << 8);
943 selector
= HIWORD( proc
);
945 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
946 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
949 for ( segNr
= 1; segNr
<= pModule
->ne_cseg
; segNr
++, pSeg
++ )
950 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
953 if ( pModule
&& segNr
> pModule
->ne_cseg
)
957 /* Abort if segment not found */
959 if ( !pModule
|| !pSeg
)
962 /* Return segment data */
964 if ( ppModule
) *ppModule
= pModule
;
965 if ( ppSeg
) *ppSeg
= pSeg
;
966 if ( pSegNr
) *pSegNr
= segNr
;
971 /**********************************************************************
972 * GetCodeHandle (KERNEL.93)
974 DWORD WINAPI
GetCodeHandle16( FARPROC16 proc
)
978 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
981 return MAKELONG( pSeg
->hSeg
, GlobalHandleToSel16(pSeg
->hSeg
) );
984 /**********************************************************************
985 * GetCodeInfo (KERNEL.104)
987 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
993 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
996 /* Fill in segment information */
998 segInfo
->offSegment
= pSeg
->filepos
;
999 segInfo
->cbSegment
= pSeg
->size
;
1000 segInfo
->flags
= pSeg
->flags
;
1001 segInfo
->cbAlloc
= pSeg
->minsize
;
1002 segInfo
->h
= pSeg
->hSeg
;
1003 segInfo
->alignShift
= pModule
->ne_align
;
1005 if ( segNr
== pModule
->ne_autodata
)
1006 segInfo
->cbAlloc
+= pModule
->ne_heap
+ pModule
->ne_stack
;
1008 /* Return module handle in %es */
1010 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
1016 /**********************************************************************
1017 * DefineHandleTable (KERNEL.94)
1019 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1021 FIXME("(%04x): stub ?\n", wOffset
);
1026 /***********************************************************************
1027 * SetTaskQueue (KERNEL.34)
1029 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1031 FIXME( "stub, should not get called\n" );
1036 /***********************************************************************
1037 * GetTaskQueue (KERNEL.35)
1039 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1041 FIXME( "stub, should not get called\n" );
1045 /***********************************************************************
1046 * SetThreadQueue (KERNEL.463)
1048 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1050 FIXME( "stub, should not get called\n" );
1054 /***********************************************************************
1055 * GetThreadQueue (KERNEL.464)
1057 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1059 FIXME( "stub, should not get called\n" );
1063 /***********************************************************************
1064 * SetFastQueue (KERNEL.624)
1066 VOID WINAPI
SetFastQueue16( DWORD thread
, HQUEUE16 hQueue
)
1068 FIXME( "stub, should not get called\n" );
1071 /***********************************************************************
1072 * GetFastQueue (KERNEL.625)
1074 HQUEUE16 WINAPI
GetFastQueue16( void )
1076 FIXME( "stub, should not get called\n" );
1080 /***********************************************************************
1081 * SwitchStackTo (KERNEL.108)
1083 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1085 STACK16FRAME
*oldFrame
, *newFrame
;
1086 INSTANCEDATA
*pData
;
1089 if (!(pData
= GlobalLock16( seg
))) return;
1090 TRACE( "old=%04x:%04x new=%04x:%04x\n", CURRENT_SS
, CURRENT_SP
, seg
, ptr
);
1092 /* Save the old stack */
1094 oldFrame
= CURRENT_STACK16
;
1095 /* pop frame + args and push bp */
1096 pData
->old_ss
= CURRENT_SS
;
1097 pData
->old_sp
= CURRENT_SP
+ sizeof(STACK16FRAME
) + 2 * sizeof(WORD
);
1098 *(WORD
*)MapSL(MAKESEGPTR(pData
->old_ss
, pData
->old_sp
)) = oldFrame
->bp
;
1099 pData
->stacktop
= top
;
1100 pData
->stackmin
= ptr
;
1101 pData
->stackbottom
= ptr
;
1103 /* Switch to the new stack */
1105 /* Note: we need to take the 3 arguments into account; otherwise,
1106 * the stack will underflow upon return from this function.
1108 copySize
= oldFrame
->bp
- pData
->old_sp
;
1109 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1111 CURRENT_SP
= ptr
- copySize
;
1112 newFrame
= CURRENT_STACK16
;
1114 /* Copy the stack frame and the local variables to the new stack */
1116 memmove( newFrame
, oldFrame
, copySize
);
1118 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1122 /***********************************************************************
1123 * SwitchStackBack (KERNEL.109)
1125 void WINAPI
SwitchStackBack16( CONTEXT
*context
)
1127 STACK16FRAME
*oldFrame
, *newFrame
;
1128 INSTANCEDATA
*pData
;
1130 if (!(pData
= GlobalLock16(CURRENT_SS
)))
1134 WARN("No previous SwitchStackTo\n" );
1137 TRACE( "restoring stack %04x:%04x\n", pData
->old_ss
, pData
->old_sp
);
1139 oldFrame
= CURRENT_STACK16
;
1141 /* Pop bp from the previous stack */
1143 context
->Ebp
= (context
->Ebp
& ~0xffff) | *(WORD
*)MapSL(MAKESEGPTR(pData
->old_ss
, pData
->old_sp
));
1144 pData
->old_sp
+= sizeof(WORD
);
1146 /* Switch back to the old stack */
1148 CURRENT_SS
= pData
->old_ss
;
1149 CURRENT_SP
= pData
->old_sp
- sizeof(STACK16FRAME
);
1150 context
->SegSs
= pData
->old_ss
;
1151 context
->Esp
= pData
->old_sp
- sizeof(DWORD
); /*ret addr*/
1152 pData
->old_ss
= pData
->old_sp
= 0;
1154 /* Build a stack frame for the return */
1156 newFrame
= CURRENT_STACK16
;
1157 newFrame
->frame32
= oldFrame
->frame32
;
1158 newFrame
->module_cs
= oldFrame
->module_cs
;
1159 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1160 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1164 /***********************************************************************
1165 * GetTaskQueueDS (KERNEL.118)
1167 void WINAPI
GetTaskQueueDS16(void)
1169 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1173 /***********************************************************************
1174 * GetTaskQueueES (KERNEL.119)
1176 void WINAPI
GetTaskQueueES16(void)
1178 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1182 /***********************************************************************
1183 * GetCurrentTask (KERNEL32.@)
1185 HTASK16 WINAPI
GetCurrentTask(void)
1187 HTASK16 ret
= kernel_get_thread_data()->htask16
;
1188 if (!ret
) ret
= main_task
;
1192 /***********************************************************************
1193 * GetCurrentTask (KERNEL.36)
1195 DWORD WINAPI
WIN16_GetCurrentTask(void)
1197 /* This is the version used by relay code; the first task is */
1198 /* returned in the high word of the result */
1199 return MAKELONG( GetCurrentTask(), hFirstTask
);
1203 /***********************************************************************
1204 * GetCurrentPDB (KERNEL.37)
1206 * UNDOC: returns PSP of KERNEL in high word
1208 DWORD WINAPI
GetCurrentPDB16(void)
1212 if (!(pTask
= TASK_GetCurrent())) return 0;
1213 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1217 /***********************************************************************
1218 * GetCurPID (KERNEL.157)
1220 DWORD WINAPI
GetCurPID16( DWORD unused
)
1226 /***********************************************************************
1227 * GetInstanceData (KERNEL.54)
1229 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1231 char *ptr
= GlobalLock16( instance
);
1232 if (!ptr
|| !len
) return 0;
1233 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1234 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1239 /***********************************************************************
1240 * GetExeVersion (KERNEL.105)
1242 WORD WINAPI
GetExeVersion16(void)
1246 if (!(pTask
= TASK_GetCurrent())) return 0;
1247 return pTask
->version
;
1251 /***********************************************************************
1252 * GetLPErrMode (KERNEL.99)
1254 SEGPTR WINAPI
GetLPErrMode(void)
1256 FIXME("(): stub\n");
1261 /***********************************************************************
1262 * SetErrorMode (KERNEL.107)
1264 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1267 if (!(pTask
= TASK_GetCurrent())) return 0;
1268 pTask
->error_mode
= mode
;
1269 return SetErrorMode( mode
);
1273 /***********************************************************************
1274 * GetNumTasks (KERNEL.152)
1276 UINT16 WINAPI
GetNumTasks16(void)
1282 /***********************************************************************
1283 * GetTaskDS (KERNEL.155)
1285 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1286 * I don't think we need to bother with this.
1288 HINSTANCE16 WINAPI
GetTaskDS16(void)
1292 if (!(pTask
= TASK_GetCurrent())) return 0;
1293 return GlobalHandleToSel16(pTask
->hInstance
);
1296 /***********************************************************************
1297 * GetDummyModuleHandleDS (KERNEL.602)
1299 WORD WINAPI
GetDummyModuleHandleDS16(void)
1304 if (!(pTask
= TASK_GetCurrent())) return 0;
1305 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1306 selector
= GlobalHandleToSel16( pTask
->hModule
);
1307 CURRENT_DS
= selector
;
1311 /***********************************************************************
1312 * IsTask (KERNEL.320)
1314 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1318 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1319 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1320 return (pTask
->magic
== TDB_MAGIC
);
1324 /***********************************************************************
1325 * IsWinOldApTask (KERNEL.158)
1327 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1329 /* should return bit 0 of byte 0x48 in PSP */
1333 /***********************************************************************
1334 * SetTaskSignalProc (KERNEL.38)
1336 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1341 if (!hTask
) hTask
= GetCurrentTask();
1342 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1343 oldProc
= pTask
->userhandler
;
1344 pTask
->userhandler
= proc
;
1348 /***********************************************************************
1349 * SetSigHandler (KERNEL.140)
1351 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1352 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1354 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1355 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1357 if (flag
!= 1) return 0;
1358 if (!newmode
) newhandler
= NULL
; /* Default handler */
1363 if (!(pTask
= TASK_GetCurrent())) return 0;
1364 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1365 pTask
->signal_flags
= newmode
;
1366 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1367 pTask
->sighandler
= newhandler
;
1373 /***********************************************************************
1374 * GlobalNotify (KERNEL.154)
1376 * Note that GlobalNotify does _not_ return the old NotifyProc
1377 * -- contrary to LocalNotify !!
1379 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1383 if (!(pTask
= TASK_GetCurrent())) return;
1384 pTask
->discardhandler
= proc
;
1388 /***********************************************************************
1391 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1396 /* Check for module handle */
1398 if (!(ptr
= GlobalLock16( handle
))) return 0;
1399 if (((NE_MODULE
*)ptr
)->ne_magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1401 /* Search for this handle inside all tasks */
1403 *hTask
= hFirstTask
;
1406 TDB
*pTask
= TASK_GetPtr( *hTask
);
1407 if ((*hTask
== handle
) ||
1408 (pTask
->hInstance
== handle
) ||
1409 (pTask
->hQueue
== handle
) ||
1410 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1411 *hTask
= pTask
->hNext
;
1414 /* Check the owner for module handle */
1416 owner
= FarGetOwner16( handle
);
1417 if (!(ptr
= GlobalLock16( owner
))) return 0;
1418 if (((NE_MODULE
*)ptr
)->ne_magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1420 /* Search for the owner inside all tasks */
1422 *hTask
= hFirstTask
;
1425 TDB
*pTask
= TASK_GetPtr( *hTask
);
1426 if ((*hTask
== owner
) ||
1427 (pTask
->hInstance
== owner
) ||
1428 (pTask
->hQueue
== owner
) ||
1429 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1430 *hTask
= pTask
->hNext
;
1436 /***********************************************************************
1437 * GetExePtr (KERNEL.133)
1439 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1442 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1443 STACK16FRAME
*frame
= CURRENT_STACK16
;
1444 frame
->ecx
= hModule
;
1445 if (hTask
) frame
->es
= hTask
;
1450 /***********************************************************************
1453 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1456 return GetExePtrHelper( handle
, &hTask
);
1460 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1462 /**************************************************************************
1463 * FatalAppExit (KERNEL.137)
1465 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1467 TDB
*pTask
= TASK_GetCurrent();
1469 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1471 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1474 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1477 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1481 ERR( "%s\n", debugstr_a(str
) );
1488 /***********************************************************************
1489 * GetAppCompatFlags (KERNEL.354)
1491 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1495 if (!hTask
) hTask
= GetCurrentTask();
1496 if (!(pTask
=TASK_GetPtr( hTask
))) return 0;
1497 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1498 return pTask
->compat_flags
;
1502 /***********************************************************************
1503 * GetDOSEnvironment (KERNEL.131)
1505 * Note: the environment is allocated once, it doesn't track changes
1506 * made using the Win32 API. This shouldn't matter.
1508 * Format of a 16-bit environment block:
1509 * ASCIIZ string 1 (xx=yy format)
1514 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1516 SEGPTR WINAPI
GetDOSEnvironment16(void)
1518 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1519 static HGLOBAL16 handle
; /* handle to the 16 bit environment */
1526 p
= env
= GetEnvironmentStringsA();
1527 while (*p
) p
+= strlen(p
) + 1;
1528 p
++; /* skip last null */
1529 size
= (p
- env
) + sizeof(WORD
) + sizeof(ENV_program_name
);
1530 handle
= GlobalAlloc16( GMEM_FIXED
, size
);
1534 LPSTR env16
= GlobalLock16( handle
);
1535 memcpy( env16
, env
, p
- env
);
1536 memcpy( env16
+ (p
- env
), &one
, sizeof(one
));
1537 memcpy( env16
+ (p
- env
) + sizeof(WORD
), ENV_program_name
, sizeof(ENV_program_name
));
1538 GlobalUnlock16( handle
);
1540 FreeEnvironmentStringsA( env
);
1542 return WOWGlobalLock16( handle
);