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 /* Enter the Win16Lock to protect global data structures */
555 pTask
= TASK_GetCurrent();
562 TRACE("Killing task %04x\n", pTask
->hSelf
);
564 /* Perform USER cleanup */
566 TASK_CallTaskSignalProc( USIG16_TERMINATION
, pTask
->hSelf
);
568 /* Remove the task from the list to be sure we never switch back to it */
569 TASK_UnlinkTask( pTask
->hSelf
);
571 if (!nTaskCount
|| (nTaskCount
== 1 && hFirstTask
== initial_task
))
573 TRACE("this is the last task, exiting\n" );
579 if ( hLockedTask
== pTask
->hSelf
)
582 TASK_DeleteTask( pTask
->hSelf
);
584 if ((tib
= NtCurrentTeb()->Tib
.SubSystemTib
))
586 free_win16_tib( tib
);
587 NtCurrentTeb()->Tib
.SubSystemTib
= NULL
;
590 /* ... and completely release the Win16Lock, just in case. */
591 ReleaseThunkLock( &lockCount
);
595 /***********************************************************************
596 * ExitKernel (KERNEL.2)
598 * Clean-up everything and exit the Wine process.
600 void WINAPI
ExitKernel16(void)
602 WriteOutProfiles16();
603 TerminateProcess( GetCurrentProcess(), 0 );
607 /***********************************************************************
608 * InitTask (KERNEL.91)
610 * Called by the application startup code.
612 void WINAPI
InitTask16( CONTEXT
*context
)
615 INSTANCEDATA
*pinstance
;
619 if (!(pTask
= TASK_GetCurrent())) return;
621 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
622 as some apps, notably Visual Basic apps, *modify* the heap/stack
623 size of the instance data segment before calling InitTask() */
625 /* Initialize the INSTANCEDATA structure */
626 pinstance
= MapSL( MAKESEGPTR(CURRENT_DS
, 0) );
627 pinstance
->stackmin
= CURRENT_SP
+ sizeof( STACK16FRAME
);
628 pinstance
->stackbottom
= pinstance
->stackmin
; /* yup, that's right. Confused me too. */
629 pinstance
->stacktop
= ( pinstance
->stackmin
> LOWORD(context
->Ebx
) ?
630 pinstance
->stackmin
- LOWORD(context
->Ebx
) : 0 ) + 150;
632 /* Initialize the local heap */
633 if (LOWORD(context
->Ecx
))
634 LocalInit16( GlobalHandleToSel16(pTask
->hInstance
), 0, LOWORD(context
->Ecx
) );
636 /* Initialize implicitly loaded DLLs */
637 NE_InitializeDLLs( pTask
->hModule
);
638 NE_DllProcessAttach( pTask
->hModule
);
640 /* Registers on return are:
641 * ax 1 if OK, 0 on error
642 * cx stack limit in bytes
643 * dx cmdShow parameter
644 * si instance handle of the previous instance
645 * di instance handle of the new task
646 * es:bx pointer to command line inside PSP
648 * 0 (=%bp) is pushed on the stack
650 ptr
= stack16_push( sizeof(WORD
) );
651 *(WORD
*)MapSL(ptr
) = 0;
656 if (!pTask
->pdb
.cmdLine
[0]) context
->Ebx
= 0x80;
659 LPBYTE p
= &pTask
->pdb
.cmdLine
[1];
660 while ((*p
== ' ') || (*p
== '\t')) p
++;
661 context
->Ebx
= 0x80 + (p
- pTask
->pdb
.cmdLine
);
663 context
->Ecx
= pinstance
->stacktop
;
664 context
->Edx
= pTask
->nCmdShow
;
665 context
->Esi
= (DWORD
)pTask
->hPrevInstance
;
666 context
->Edi
= (DWORD
)pTask
->hInstance
;
667 context
->SegEs
= (WORD
)pTask
->hPDB
;
671 /***********************************************************************
672 * WaitEvent (KERNEL.30)
674 BOOL16 WINAPI
WaitEvent16( HTASK16 hTask
)
678 if (!hTask
) hTask
= GetCurrentTask();
679 pTask
= TASK_GetPtr( hTask
);
681 if (pTask
->flags
& TDBF_WIN32
)
683 FIXME("called for Win32 thread (%04x)!\n", GetCurrentThreadId());
687 if (pTask
->nEvents
> 0)
693 if (pTask
->teb
== NtCurrentTeb())
697 NtResetEvent( pTask
->hEvent
, NULL
);
698 ReleaseThunkLock( &lockCount
);
699 SYSLEVEL_CheckNotLevel( 1 );
700 WaitForSingleObject( pTask
->hEvent
, INFINITE
);
701 RestoreThunkLock( lockCount
);
702 if (pTask
->nEvents
> 0) pTask
->nEvents
--;
704 else FIXME("for other task %04x cur=%04x\n",pTask
->hSelf
,GetCurrentTask());
710 /***********************************************************************
711 * PostEvent (KERNEL.31)
713 void WINAPI
PostEvent16( HTASK16 hTask
)
717 if (!hTask
) hTask
= GetCurrentTask();
718 if (!(pTask
= TASK_GetPtr( hTask
))) return;
720 if (pTask
->flags
& TDBF_WIN32
)
722 FIXME("called for Win32 thread (%04x)!\n", (DWORD
)pTask
->teb
->ClientId
.UniqueThread
);
728 if (pTask
->nEvents
== 1) NtSetEvent( pTask
->hEvent
, NULL
);
732 /***********************************************************************
733 * SetPriority (KERNEL.32)
735 void WINAPI
SetPriority16( HTASK16 hTask
, INT16 delta
)
740 if (!hTask
) hTask
= GetCurrentTask();
741 if (!(pTask
= TASK_GetPtr( hTask
))) return;
742 newpriority
= pTask
->priority
+ delta
;
743 if (newpriority
< -32) newpriority
= -32;
744 else if (newpriority
> 15) newpriority
= 15;
746 pTask
->priority
= newpriority
+ 1;
747 TASK_UnlinkTask( pTask
->hSelf
);
748 TASK_LinkTask( pTask
->hSelf
);
753 /***********************************************************************
754 * LockCurrentTask (KERNEL.33)
756 HTASK16 WINAPI
LockCurrentTask16( BOOL16 bLock
)
758 if (bLock
) hLockedTask
= GetCurrentTask();
759 else hLockedTask
= 0;
764 /***********************************************************************
765 * IsTaskLocked (KERNEL.122)
767 HTASK16 WINAPI
IsTaskLocked16(void)
773 /***********************************************************************
774 * OldYield (KERNEL.117)
776 void WINAPI
OldYield16(void)
780 ReleaseThunkLock(&count
);
781 RestoreThunkLock(count
);
784 /***********************************************************************
785 * WIN32_OldYield (KERNEL.447)
787 void WINAPI
WIN32_OldYield16(void)
791 ReleaseThunkLock(&count
);
792 RestoreThunkLock(count
);
795 /***********************************************************************
796 * DirectedYield (KERNEL.150)
798 void WINAPI
DirectedYield16( HTASK16 hTask
)
803 /***********************************************************************
806 void WINAPI
Yield16(void)
808 TDB
*pCurTask
= TASK_GetCurrent();
810 if (pCurTask
&& pCurTask
->hQueue
)
812 HMODULE mod
= GetModuleHandleA( "user32.dll" );
815 BOOL (WINAPI
*pPeekMessageW
)( MSG
*msg
, HWND hwnd
, UINT first
, UINT last
, UINT flags
);
816 pPeekMessageW
= (void *)GetProcAddress( mod
, "PeekMessageW" );
820 pPeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
| PM_QS_SENDMESSAGE
);
828 /***********************************************************************
829 * KERNEL_490 (KERNEL.490)
831 HTASK16 WINAPI
KERNEL_490( HTASK16 someTask
)
833 if ( !someTask
) return 0;
835 FIXME("(%04x): stub\n", someTask
);
839 /***********************************************************************
840 * MakeProcInstance (KERNEL.51)
842 FARPROC16 WINAPI
MakeProcInstance16( FARPROC16 func
, HANDLE16 hInstance
)
847 WORD hInstanceSelector
;
849 hInstanceSelector
= GlobalHandleToSel16(hInstance
);
851 TRACE("(%p, %04x);\n", func
, hInstance
);
854 /* Win95 actually protects via SEH, but this is better for debugging */
855 WARN("Ouch ! Called with invalid func %p !\n", func
);
859 if ( (GlobalHandleToSel16(CURRENT_DS
) != hInstanceSelector
)
861 && (hInstance
!= 0xffff) )
863 /* calling MPI with a foreign DSEG is invalid ! */
864 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
865 hInstance
,CURRENT_DS
);
868 /* Always use the DSEG that MPI was entered with.
869 * We used to set hInstance to GetTaskDS16(), but this should be wrong
870 * as CURRENT_DS provides the DSEG value we need.
871 * ("calling" DS, *not* "task" DS !) */
872 hInstanceSelector
= CURRENT_DS
;
873 hInstance
= GlobalHandle16(hInstanceSelector
);
875 /* no thunking for DLLs */
876 if (NE_GetPtr(FarGetOwner16(hInstance
))->ne_flags
& NE_FFLAGS_LIBMODULE
)
879 thunkaddr
= TASK_AllocThunk();
880 if (!thunkaddr
) return NULL
;
881 thunk
= MapSL( thunkaddr
);
882 lfunc
= MapSL( (SEGPTR
)func
);
884 TRACE("(%p,%04x): got thunk %08x\n", func
, hInstance
, thunkaddr
);
885 if (((lfunc
[0]==0x8c) && (lfunc
[1]==0xd8)) || /* movw %ds, %ax */
886 ((lfunc
[0]==0x1e) && (lfunc
[1]==0x58)) /* pushw %ds, popw %ax */
888 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
891 thunk
->movw
= 0xb8; /* movw instance, %ax */
892 thunk
->instance
= hInstanceSelector
;
893 thunk
->ljmp
= 0xea; /* ljmp func */
895 return (FARPROC16
)thunkaddr
;
896 /* CX reg indicates if thunkaddr != NULL, implement if needed */
900 /***********************************************************************
901 * FreeProcInstance (KERNEL.52)
903 void WINAPI
FreeProcInstance16( FARPROC16 func
)
905 TRACE("(%p)\n", func
);
906 TASK_FreeThunk( (SEGPTR
)func
);
909 /**********************************************************************
910 * TASK_GetCodeSegment
912 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
913 * and logical segment number of a given code segment.
915 * 'proc' either *is* already a pair of module handle and segment number,
916 * in which case there's nothing to do. Otherwise, it is a pointer to
917 * a function, and we need to retrieve the code segment. If the pointer
918 * happens to point to a thunk, we'll retrieve info about the code segment
919 * where the function pointed to by the thunk resides, not the thunk itself.
921 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
922 * the function the snoop code will return to ...
925 static BOOL
TASK_GetCodeSegment( FARPROC16 proc
, NE_MODULE
**ppModule
,
926 SEGTABLEENTRY
**ppSeg
, int *pSegNr
)
928 NE_MODULE
*pModule
= NULL
;
929 SEGTABLEENTRY
*pSeg
= NULL
;
932 /* Try pair of module handle / segment number */
933 pModule
= GlobalLock16( HIWORD( proc
) );
934 if ( pModule
&& pModule
->ne_magic
== IMAGE_OS2_SIGNATURE
)
936 segNr
= LOWORD( proc
);
937 if ( segNr
&& segNr
<= pModule
->ne_cseg
)
938 pSeg
= NE_SEG_TABLE( pModule
) + segNr
-1;
941 /* Try thunk or function */
944 BYTE
*thunk
= MapSL( (SEGPTR
)proc
);
947 if ((thunk
[0] == 0xb8) && (thunk
[3] == 0xea))
948 selector
= thunk
[6] + (thunk
[7] << 8);
950 selector
= HIWORD( proc
);
952 pModule
= NE_GetPtr( GlobalHandle16( selector
) );
953 pSeg
= pModule
? NE_SEG_TABLE( pModule
) : NULL
;
956 for ( segNr
= 1; segNr
<= pModule
->ne_cseg
; segNr
++, pSeg
++ )
957 if ( GlobalHandleToSel16(pSeg
->hSeg
) == selector
)
960 if ( pModule
&& segNr
> pModule
->ne_cseg
)
964 /* Abort if segment not found */
966 if ( !pModule
|| !pSeg
)
969 /* Return segment data */
971 if ( ppModule
) *ppModule
= pModule
;
972 if ( ppSeg
) *ppSeg
= pSeg
;
973 if ( pSegNr
) *pSegNr
= segNr
;
978 /**********************************************************************
979 * GetCodeHandle (KERNEL.93)
981 DWORD WINAPI
GetCodeHandle16( FARPROC16 proc
)
985 if ( !TASK_GetCodeSegment( proc
, NULL
, &pSeg
, NULL
) )
988 return MAKELONG( pSeg
->hSeg
, GlobalHandleToSel16(pSeg
->hSeg
) );
991 /**********************************************************************
992 * GetCodeInfo (KERNEL.104)
994 BOOL16 WINAPI
GetCodeInfo16( FARPROC16 proc
, SEGINFO
*segInfo
)
1000 if ( !TASK_GetCodeSegment( proc
, &pModule
, &pSeg
, &segNr
) )
1003 /* Fill in segment information */
1005 segInfo
->offSegment
= pSeg
->filepos
;
1006 segInfo
->cbSegment
= pSeg
->size
;
1007 segInfo
->flags
= pSeg
->flags
;
1008 segInfo
->cbAlloc
= pSeg
->minsize
;
1009 segInfo
->h
= pSeg
->hSeg
;
1010 segInfo
->alignShift
= pModule
->ne_align
;
1012 if ( segNr
== pModule
->ne_autodata
)
1013 segInfo
->cbAlloc
+= pModule
->ne_heap
+ pModule
->ne_stack
;
1015 /* Return module handle in %es */
1017 CURRENT_STACK16
->es
= GlobalHandleToSel16( pModule
->self
);
1023 /**********************************************************************
1024 * DefineHandleTable (KERNEL.94)
1026 BOOL16 WINAPI
DefineHandleTable16( WORD wOffset
)
1028 FIXME("(%04x): stub ?\n", wOffset
);
1033 /***********************************************************************
1034 * SetTaskQueue (KERNEL.34)
1036 HQUEUE16 WINAPI
SetTaskQueue16( HTASK16 hTask
, HQUEUE16 hQueue
)
1038 FIXME( "stub, should not get called\n" );
1043 /***********************************************************************
1044 * GetTaskQueue (KERNEL.35)
1046 HQUEUE16 WINAPI
GetTaskQueue16( HTASK16 hTask
)
1048 FIXME( "stub, should not get called\n" );
1052 /***********************************************************************
1053 * SetThreadQueue (KERNEL.463)
1055 HQUEUE16 WINAPI
SetThreadQueue16( DWORD thread
, HQUEUE16 hQueue
)
1057 FIXME( "stub, should not get called\n" );
1061 /***********************************************************************
1062 * GetThreadQueue (KERNEL.464)
1064 HQUEUE16 WINAPI
GetThreadQueue16( DWORD thread
)
1066 FIXME( "stub, should not get called\n" );
1070 /***********************************************************************
1071 * SetFastQueue (KERNEL.624)
1073 VOID WINAPI
SetFastQueue16( DWORD thread
, HQUEUE16 hQueue
)
1075 FIXME( "stub, should not get called\n" );
1078 /***********************************************************************
1079 * GetFastQueue (KERNEL.625)
1081 HQUEUE16 WINAPI
GetFastQueue16( void )
1083 FIXME( "stub, should not get called\n" );
1087 /***********************************************************************
1088 * SwitchStackTo (KERNEL.108)
1090 void WINAPI
SwitchStackTo16( WORD seg
, WORD ptr
, WORD top
)
1092 STACK16FRAME
*oldFrame
, *newFrame
;
1093 INSTANCEDATA
*pData
;
1096 if (!(pData
= GlobalLock16( seg
))) return;
1097 TRACE( "old=%04x:%04x new=%04x:%04x\n", CURRENT_SS
, CURRENT_SP
, seg
, ptr
);
1099 /* Save the old stack */
1101 oldFrame
= CURRENT_STACK16
;
1102 /* pop frame + args and push bp */
1103 pData
->old_ss
= CURRENT_SS
;
1104 pData
->old_sp
= CURRENT_SP
+ sizeof(STACK16FRAME
) + 2 * sizeof(WORD
);
1105 *(WORD
*)MapSL(MAKESEGPTR(pData
->old_ss
, pData
->old_sp
)) = oldFrame
->bp
;
1106 pData
->stacktop
= top
;
1107 pData
->stackmin
= ptr
;
1108 pData
->stackbottom
= ptr
;
1110 /* Switch to the new stack */
1112 /* Note: we need to take the 3 arguments into account; otherwise,
1113 * the stack will underflow upon return from this function.
1115 copySize
= oldFrame
->bp
- pData
->old_sp
;
1116 copySize
+= 3 * sizeof(WORD
) + sizeof(STACK16FRAME
);
1118 CURRENT_SP
= ptr
- copySize
;
1119 newFrame
= CURRENT_STACK16
;
1121 /* Copy the stack frame and the local variables to the new stack */
1123 memmove( newFrame
, oldFrame
, copySize
);
1125 *(WORD
*)MapSL( MAKESEGPTR( seg
, ptr
) ) = 0; /* clear previous bp */
1129 /***********************************************************************
1130 * SwitchStackBack (KERNEL.109)
1132 void WINAPI
SwitchStackBack16( CONTEXT
*context
)
1134 STACK16FRAME
*oldFrame
, *newFrame
;
1135 INSTANCEDATA
*pData
;
1137 if (!(pData
= GlobalLock16(CURRENT_SS
)))
1141 WARN("No previous SwitchStackTo\n" );
1144 TRACE( "restoring stack %04x:%04x\n", pData
->old_ss
, pData
->old_sp
);
1146 oldFrame
= CURRENT_STACK16
;
1148 /* Pop bp from the previous stack */
1150 context
->Ebp
= (context
->Ebp
& ~0xffff) | *(WORD
*)MapSL(MAKESEGPTR(pData
->old_ss
, pData
->old_sp
));
1151 pData
->old_sp
+= sizeof(WORD
);
1153 /* Switch back to the old stack */
1155 CURRENT_SS
= pData
->old_ss
;
1156 CURRENT_SP
= pData
->old_sp
- sizeof(STACK16FRAME
);
1157 context
->SegSs
= pData
->old_ss
;
1158 context
->Esp
= pData
->old_sp
- sizeof(DWORD
); /*ret addr*/
1159 pData
->old_ss
= pData
->old_sp
= 0;
1161 /* Build a stack frame for the return */
1163 newFrame
= CURRENT_STACK16
;
1164 newFrame
->frame32
= oldFrame
->frame32
;
1165 newFrame
->module_cs
= oldFrame
->module_cs
;
1166 newFrame
->callfrom_ip
= oldFrame
->callfrom_ip
;
1167 newFrame
->entry_ip
= oldFrame
->entry_ip
;
1171 /***********************************************************************
1172 * GetTaskQueueDS (KERNEL.118)
1174 void WINAPI
GetTaskQueueDS16(void)
1176 CURRENT_STACK16
->ds
= GlobalHandleToSel16( GetTaskQueue16(0) );
1180 /***********************************************************************
1181 * GetTaskQueueES (KERNEL.119)
1183 void WINAPI
GetTaskQueueES16(void)
1185 CURRENT_STACK16
->es
= GlobalHandleToSel16( GetTaskQueue16(0) );
1189 /***********************************************************************
1190 * GetCurrentTask (KERNEL32.@)
1192 HTASK16 WINAPI
GetCurrentTask(void)
1194 HTASK16 ret
= kernel_get_thread_data()->htask16
;
1195 if (!ret
) ret
= main_task
;
1199 /***********************************************************************
1200 * GetCurrentTask (KERNEL.36)
1202 DWORD WINAPI
WIN16_GetCurrentTask(void)
1204 /* This is the version used by relay code; the first task is */
1205 /* returned in the high word of the result */
1206 return MAKELONG( GetCurrentTask(), hFirstTask
);
1210 /***********************************************************************
1211 * GetCurrentPDB (KERNEL.37)
1213 * UNDOC: returns PSP of KERNEL in high word
1215 DWORD WINAPI
GetCurrentPDB16(void)
1219 if (!(pTask
= TASK_GetCurrent())) return 0;
1220 return MAKELONG(pTask
->hPDB
, 0); /* FIXME */
1224 /***********************************************************************
1225 * GetCurPID (KERNEL.157)
1227 DWORD WINAPI
GetCurPID16( DWORD unused
)
1233 /***********************************************************************
1234 * GetInstanceData (KERNEL.54)
1236 INT16 WINAPI
GetInstanceData16( HINSTANCE16 instance
, WORD buffer
, INT16 len
)
1238 char *ptr
= GlobalLock16( instance
);
1239 if (!ptr
|| !len
) return 0;
1240 if ((int)buffer
+ len
>= 0x10000) len
= 0x10000 - buffer
;
1241 memcpy( (char *)GlobalLock16(CURRENT_DS
) + buffer
, ptr
+ buffer
, len
);
1246 /***********************************************************************
1247 * GetExeVersion (KERNEL.105)
1249 WORD WINAPI
GetExeVersion16(void)
1253 if (!(pTask
= TASK_GetCurrent())) return 0;
1254 return pTask
->version
;
1258 /***********************************************************************
1259 * SetErrorMode (KERNEL.107)
1261 UINT16 WINAPI
SetErrorMode16( UINT16 mode
)
1264 if (!(pTask
= TASK_GetCurrent())) return 0;
1265 pTask
->error_mode
= mode
;
1266 return SetErrorMode( mode
);
1270 /***********************************************************************
1271 * GetNumTasks (KERNEL.152)
1273 UINT16 WINAPI
GetNumTasks16(void)
1279 /***********************************************************************
1280 * GetTaskDS (KERNEL.155)
1282 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1283 * I don't think we need to bother with this.
1285 HINSTANCE16 WINAPI
GetTaskDS16(void)
1289 if (!(pTask
= TASK_GetCurrent())) return 0;
1290 return GlobalHandleToSel16(pTask
->hInstance
);
1293 /***********************************************************************
1294 * GetDummyModuleHandleDS (KERNEL.602)
1296 WORD WINAPI
GetDummyModuleHandleDS16(void)
1301 if (!(pTask
= TASK_GetCurrent())) return 0;
1302 if (!(pTask
->flags
& TDBF_WIN32
)) return 0;
1303 selector
= GlobalHandleToSel16( pTask
->hModule
);
1304 CURRENT_DS
= selector
;
1308 /***********************************************************************
1309 * IsTask (KERNEL.320)
1311 BOOL16 WINAPI
IsTask16( HTASK16 hTask
)
1315 if (!(pTask
= TASK_GetPtr( hTask
))) return FALSE
;
1316 if (GlobalSize16( hTask
) < sizeof(TDB
)) return FALSE
;
1317 return (pTask
->magic
== TDB_MAGIC
);
1321 /***********************************************************************
1322 * IsWinOldApTask (KERNEL.158)
1324 BOOL16 WINAPI
IsWinOldApTask16( HTASK16 hTask
)
1326 /* should return bit 0 of byte 0x48 in PSP */
1330 /***********************************************************************
1331 * SetTaskSignalProc (KERNEL.38)
1333 FARPROC16 WINAPI
SetTaskSignalProc( HTASK16 hTask
, FARPROC16 proc
)
1338 if (!hTask
) hTask
= GetCurrentTask();
1339 if (!(pTask
= TASK_GetPtr( hTask
))) return NULL
;
1340 oldProc
= pTask
->userhandler
;
1341 pTask
->userhandler
= proc
;
1345 /***********************************************************************
1346 * SetSigHandler (KERNEL.140)
1348 WORD WINAPI
SetSigHandler16( FARPROC16 newhandler
, FARPROC16
* oldhandler
,
1349 UINT16
*oldmode
, UINT16 newmode
, UINT16 flag
)
1351 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1352 newhandler
,oldhandler
,oldmode
,newmode
,flag
);
1354 if (flag
!= 1) return 0;
1355 if (!newmode
) newhandler
= NULL
; /* Default handler */
1360 if (!(pTask
= TASK_GetCurrent())) return 0;
1361 if (oldmode
) *oldmode
= pTask
->signal_flags
;
1362 pTask
->signal_flags
= newmode
;
1363 if (oldhandler
) *oldhandler
= pTask
->sighandler
;
1364 pTask
->sighandler
= newhandler
;
1370 /***********************************************************************
1371 * GlobalNotify (KERNEL.154)
1373 * Note that GlobalNotify does _not_ return the old NotifyProc
1374 * -- contrary to LocalNotify !!
1376 VOID WINAPI
GlobalNotify16( FARPROC16 proc
)
1380 if (!(pTask
= TASK_GetCurrent())) return;
1381 pTask
->discardhandler
= proc
;
1385 /***********************************************************************
1388 static inline HMODULE16
GetExePtrHelper( HANDLE16 handle
, HTASK16
*hTask
)
1393 /* Check for module handle */
1395 if (!(ptr
= GlobalLock16( handle
))) return 0;
1396 if (((NE_MODULE
*)ptr
)->ne_magic
== IMAGE_OS2_SIGNATURE
) return handle
;
1398 /* Search for this handle inside all tasks */
1400 *hTask
= hFirstTask
;
1403 TDB
*pTask
= TASK_GetPtr( *hTask
);
1404 if ((*hTask
== handle
) ||
1405 (pTask
->hInstance
== handle
) ||
1406 (pTask
->hQueue
== handle
) ||
1407 (pTask
->hPDB
== handle
)) return pTask
->hModule
;
1408 *hTask
= pTask
->hNext
;
1411 /* Check the owner for module handle */
1413 owner
= FarGetOwner16( handle
);
1414 if (!(ptr
= GlobalLock16( owner
))) return 0;
1415 if (((NE_MODULE
*)ptr
)->ne_magic
== IMAGE_OS2_SIGNATURE
) return owner
;
1417 /* Search for the owner inside all tasks */
1419 *hTask
= hFirstTask
;
1422 TDB
*pTask
= TASK_GetPtr( *hTask
);
1423 if ((*hTask
== owner
) ||
1424 (pTask
->hInstance
== owner
) ||
1425 (pTask
->hQueue
== owner
) ||
1426 (pTask
->hPDB
== owner
)) return pTask
->hModule
;
1427 *hTask
= pTask
->hNext
;
1433 /***********************************************************************
1434 * GetExePtr (KERNEL.133)
1436 HMODULE16 WINAPI
WIN16_GetExePtr( HANDLE16 handle
)
1439 HMODULE16 hModule
= GetExePtrHelper( handle
, &hTask
);
1440 STACK16FRAME
*frame
= CURRENT_STACK16
;
1441 frame
->ecx
= hModule
;
1442 if (hTask
) frame
->es
= hTask
;
1447 /***********************************************************************
1450 HMODULE16 WINAPI
GetExePtr( HANDLE16 handle
)
1453 return GetExePtrHelper( handle
, &hTask
);
1457 typedef INT (WINAPI
*MessageBoxA_funcptr
)(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
);
1459 /**************************************************************************
1460 * FatalAppExit (KERNEL.137)
1462 void WINAPI
FatalAppExit16( UINT16 action
, LPCSTR str
)
1464 TDB
*pTask
= TASK_GetCurrent();
1466 if (!pTask
|| !(pTask
->error_mode
& SEM_NOGPFAULTERRORBOX
))
1468 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1471 MessageBoxA_funcptr pMessageBoxA
= (MessageBoxA_funcptr
)GetProcAddress( mod
, "MessageBoxA" );
1474 pMessageBoxA( 0, str
, NULL
, MB_SYSTEMMODAL
| MB_OK
);
1478 ERR( "%s\n", debugstr_a(str
) );
1485 /***********************************************************************
1486 * GetAppCompatFlags (KERNEL.354)
1488 DWORD WINAPI
GetAppCompatFlags16( HTASK16 hTask
)
1492 if (!hTask
) hTask
= GetCurrentTask();
1493 if (!(pTask
=TASK_GetPtr( hTask
))) return 0;
1494 if (GlobalSize16(hTask
) < sizeof(TDB
)) return 0;
1495 return pTask
->compat_flags
;
1499 /***********************************************************************
1500 * GetDOSEnvironment (KERNEL.131)
1502 * Note: the environment is allocated once, it doesn't track changes
1503 * made using the Win32 API. This shouldn't matter.
1505 * Format of a 16-bit environment block:
1506 * ASCIIZ string 1 (xx=yy format)
1511 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1513 SEGPTR WINAPI
GetDOSEnvironment16(void)
1515 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1516 static HGLOBAL16 handle
; /* handle to the 16 bit environment */
1523 p
= env
= GetEnvironmentStringsA();
1524 while (*p
) p
+= strlen(p
) + 1;
1525 p
++; /* skip last null */
1526 size
= (p
- env
) + sizeof(WORD
) + sizeof(ENV_program_name
);
1527 handle
= GlobalAlloc16( GMEM_FIXED
, size
);
1531 LPSTR env16
= GlobalLock16( handle
);
1532 memcpy( env16
, env
, p
- env
);
1533 memcpy( env16
+ (p
- env
), &one
, sizeof(one
));
1534 memcpy( env16
+ (p
- env
) + sizeof(WORD
), ENV_program_name
, sizeof(ENV_program_name
));
1535 GlobalUnlock16( handle
);
1537 FreeEnvironmentStringsA( env
);
1539 return WOWGlobalLock16( handle
);