Store the 16-bit stack pointer in the WOW32Reserved TEB field.
[wine/wine64.git] / dlls / kernel / task.c
blob3cfdba9738d7fd4e0503f047bf93387c6031d9c0
1 /*
2 * Task functions
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winnt.h"
36 #include "wownt32.h"
37 #include "winuser.h"
39 #include "wine/winbase16.h"
40 #include "module.h"
41 #include "winternl.h"
42 #include "wine/server.h"
43 #include "stackframe.h"
44 #include "thread.h"
45 #include "toolhelp.h"
46 #include "kernel_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(task);
51 WINE_DECLARE_DEBUG_CHANNEL(relay);
52 WINE_DECLARE_DEBUG_CHANNEL(toolhelp);
54 #include "pshpack1.h"
56 /* Segment containing MakeProcInstance() thunks */
57 typedef struct
59 WORD next; /* Selector of next segment */
60 WORD magic; /* Thunks signature */
61 WORD unused;
62 WORD free; /* Head of the free list */
63 WORD thunks[4]; /* Each thunk is 4 words long */
64 } THUNKS;
66 #include "poppack.h"
68 #define THUNK_MAGIC ('P' | ('T' << 8))
70 /* Min. number of thunks allocated when creating a new segment */
71 #define MIN_THUNKS 32
73 #define TDB_MAGIC ('T' | ('D' << 8))
75 static THHOOK DefaultThhook;
76 THHOOK *pThhook = &DefaultThhook;
78 #define hFirstTask (pThhook->HeadTDB)
79 #define hLockedTask (pThhook->LockTDB)
81 static UINT16 nTaskCount = 0;
83 static HTASK16 initial_task;
85 /***********************************************************************
86 * TASK_InstallTHHook
88 void TASK_InstallTHHook( THHOOK *pNewThhook )
90 THHOOK *pOldThhook = pThhook;
92 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
94 *pThhook = *pOldThhook;
97 /***********************************************************************
98 * TASK_GetPtr
100 static TDB *TASK_GetPtr( HTASK16 hTask )
102 return GlobalLock16( hTask );
106 /***********************************************************************
107 * TASK_GetCurrent
109 TDB *TASK_GetCurrent(void)
111 return TASK_GetPtr( GetCurrentTask() );
115 /***********************************************************************
116 * TASK_LinkTask
118 static void TASK_LinkTask( HTASK16 hTask )
120 HTASK16 *prevTask;
121 TDB *pTask;
123 if (!(pTask = TASK_GetPtr( hTask ))) return;
124 prevTask = &hFirstTask;
125 while (*prevTask)
127 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
128 if (prevTaskPtr->priority >= pTask->priority) break;
129 prevTask = &prevTaskPtr->hNext;
131 pTask->hNext = *prevTask;
132 *prevTask = hTask;
133 nTaskCount++;
137 /***********************************************************************
138 * TASK_UnlinkTask
140 static void TASK_UnlinkTask( HTASK16 hTask )
142 HTASK16 *prevTask;
143 TDB *pTask;
145 prevTask = &hFirstTask;
146 while (*prevTask && (*prevTask != hTask))
148 pTask = TASK_GetPtr( *prevTask );
149 prevTask = &pTask->hNext;
151 if (*prevTask)
153 pTask = TASK_GetPtr( *prevTask );
154 *prevTask = pTask->hNext;
155 pTask->hNext = 0;
156 nTaskCount--;
161 /***********************************************************************
162 * TASK_CreateThunks
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 )
169 int i;
170 WORD free;
171 THUNKS *pThunk;
173 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
174 pThunk->next = 0;
175 pThunk->magic = THUNK_MAGIC;
176 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
177 free = pThunk->free;
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 /***********************************************************************
188 * TASK_AllocThunk
190 * Allocate a thunk for MakeProcInstance().
192 static SEGPTR TASK_AllocThunk(void)
194 TDB *pTask;
195 THUNKS *pThunk;
196 WORD sel, base;
198 if (!(pTask = TASK_GetCurrent())) return 0;
199 sel = pTask->hCSAlias;
200 pThunk = (THUNKS *)&pTask->thunks;
201 base = (char *)pThunk - (char *)pTask;
202 while (!pThunk->free)
204 sel = pThunk->next;
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 );
211 pThunk->next = sel;
213 pThunk = (THUNKS *)GlobalLock16( sel );
214 base = 0;
216 base += pThunk->free;
217 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
218 return MAKESEGPTR( sel, base );
222 /***********************************************************************
223 * TASK_FreeThunk
225 * Free a MakeProcInstance() thunk.
227 static BOOL TASK_FreeThunk( SEGPTR thunk )
229 TDB *pTask;
230 THUNKS *pThunk;
231 WORD sel, base;
233 if (!(pTask = TASK_GetCurrent())) return 0;
234 sel = pTask->hCSAlias;
235 pThunk = (THUNKS *)&pTask->thunks;
236 base = (char *)pThunk - (char *)pTask;
237 while (sel && (sel != HIWORD(thunk)))
239 sel = pThunk->next;
240 pThunk = (THUNKS *)GlobalLock16( sel );
241 base = 0;
243 if (!sel) return FALSE;
244 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
245 pThunk->free = LOWORD(thunk) - base;
246 return TRUE;
250 /***********************************************************************
251 * TASK_Create
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
256 * global task list.
258 static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, LPCSTR cmdline, BYTE len )
260 HTASK16 hTask;
261 TDB *pTask;
262 FARPROC16 proc;
263 char curdir[MAX_PATH];
264 HMODULE16 hModule = pModule ? pModule->self : 0;
266 /* Allocate the task structure */
268 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
269 if (!hTask) return NULL;
270 pTask = TASK_GetPtr( hTask );
271 FarSetOwner16( hTask, hModule );
273 /* Fill the task structure */
275 pTask->hSelf = hTask;
277 pTask->version = pModule ? pModule->expected_version : 0x0400;
278 pTask->hModule = hModule;
279 pTask->hParent = GetCurrentTask();
280 pTask->magic = TDB_MAGIC;
281 pTask->nCmdShow = cmdShow;
283 GetCurrentDirectoryA( sizeof(curdir), curdir );
284 GetShortPathNameA( curdir, curdir, sizeof(curdir) );
285 pTask->curdrive = (curdir[0] - 'A') | 0x80;
286 lstrcpynA( pTask->curdir, curdir + 2, sizeof(pTask->curdir) );
288 /* Create the thunks block */
290 TASK_CreateThunks( hTask, (char *)&pTask->thunks - (char *)pTask, 7 );
292 /* Copy the module name */
294 if (hModule)
296 char name[sizeof(pTask->module_name)+1];
297 size_t len;
298 GetModuleName16( hModule, name, sizeof(name) );
299 len = strlen(name) + 1;
300 memcpy(pTask->module_name, name, min(len,sizeof(pTask->module_name)));
301 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
304 /* Allocate a selector for the PDB */
306 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
307 hModule, WINE_LDT_FLAGS_DATA );
309 /* Fill the PDB */
311 pTask->pdb.int20 = 0x20cd;
312 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
313 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
314 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
315 pTask->pdb.savedint22 = 0;
316 pTask->pdb.savedint23 = 0;
317 pTask->pdb.savedint24 = 0;
318 pTask->pdb.fileHandlesPtr =
319 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
320 pTask->pdb.hFileHandles = 0;
321 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
322 /* FIXME: should we make a copy of the environment? */
323 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
324 pTask->pdb.nbFiles = 20;
326 /* Fill the command line */
328 if (!cmdline)
330 cmdline = GetCommandLineA();
331 /* remove the first word (program name) */
332 if (*cmdline == '"')
333 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
334 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
335 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
336 len = strlen(cmdline);
338 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
339 pTask->pdb.cmdLine[0] = len;
340 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
341 /* pTask->pdb.cmdLine[len+1] = 0; */
343 TRACE("cmdline='%.*s' task=%04x\n", len, cmdline, hTask );
345 /* Allocate a code segment alias for the TDB */
347 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
348 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
350 /* Default DTA overwrites command line */
352 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
354 /* Create scheduler event for 16-bit tasks */
356 if ( !(pTask->flags & TDBF_WIN32) )
357 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
359 if (!initial_task) initial_task = hTask;
361 return pTask;
365 /***********************************************************************
366 * TASK_DeleteTask
368 static void TASK_DeleteTask( HTASK16 hTask )
370 TDB *pTask;
371 HGLOBAL16 hPDB;
373 if (!(pTask = TASK_GetPtr( hTask ))) return;
374 hPDB = pTask->hPDB;
376 pTask->magic = 0xdead; /* invalidate signature */
378 /* Free the selector aliases */
380 GLOBAL_FreeBlock( pTask->hCSAlias );
381 GLOBAL_FreeBlock( pTask->hPDB );
383 /* Free the task module */
385 FreeModule16( pTask->hModule );
387 /* Free the task structure itself */
389 GlobalFree16( hTask );
391 /* Free all memory used by this task (including the 32-bit stack, */
392 /* the environment block and the thunk segments). */
394 GlobalFreeAll16( hPDB );
398 /***********************************************************************
399 * TASK_CreateMainTask
401 * Create a task for the main (32-bit) process.
403 void TASK_CreateMainTask(void)
405 TDB *pTask;
406 STARTUPINFOA startup_info;
407 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
409 GetStartupInfoA( &startup_info );
410 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
411 pTask = TASK_Create( NULL, cmdShow, NULL, 0 );
412 if (!pTask)
414 ERR("could not create task for main process\n");
415 ExitProcess(1);
418 pTask->flags |= TDBF_WIN32;
419 pTask->hInstance = 0;
420 pTask->hPrevInstance = 0;
421 pTask->teb = NtCurrentTeb();
423 NtCurrentTeb()->htask16 = pTask->hSelf;
425 /* Add the task to the linked list */
426 /* (no need to get the win16 lock, we are the only thread at this point) */
427 TASK_LinkTask( pTask->hSelf );
431 /* allocate the win16 TIB for a new 16-bit task */
432 static WIN16_SUBSYSTEM_TIB *allocate_win16_tib( TDB *pTask )
434 WCHAR path[MAX_PATH];
435 WIN16_SUBSYSTEM_TIB *tib;
436 UNICODE_STRING *curdir;
437 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
439 if (!(tib = HeapAlloc( GetProcessHeap(), 0, sizeof(*tib) ))) return NULL;
440 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
441 GetLongPathNameW( path, path, MAX_PATH );
442 if (RtlCreateUnicodeString( &tib->exe_str, path )) tib->exe_name = &tib->exe_str;
443 else tib->exe_name = NULL;
445 RtlAcquirePebLock();
446 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
447 tib->curdir.DosPath.MaximumLength = sizeof(tib->curdir_buffer);
448 tib->curdir.DosPath.Length = min( curdir->Length, tib->curdir.DosPath.MaximumLength-sizeof(WCHAR) );
449 tib->curdir.DosPath.Buffer = tib->curdir_buffer;
450 tib->curdir.Handle = 0;
451 memcpy( tib->curdir_buffer, curdir->Buffer, tib->curdir.DosPath.Length );
452 tib->curdir_buffer[tib->curdir.DosPath.Length/sizeof(WCHAR)] = 0;
453 RtlReleasePebLock();
454 return tib;
457 /* startup routine for a new 16-bit thread */
458 static DWORD CALLBACK task_start( LPVOID p )
460 TDB *pTask = (TDB *)p;
461 DWORD ret;
463 NtCurrentTeb()->htask16 = pTask->hSelf;
464 NtCurrentTeb()->Tib.SubSystemTib = allocate_win16_tib( pTask );
466 _EnterWin16Lock();
467 TASK_LinkTask( pTask->hSelf );
468 pTask->teb = NtCurrentTeb();
469 ret = NE_StartTask();
470 _LeaveWin16Lock();
471 return ret;
475 /***********************************************************************
476 * TASK_SpawnTask
478 * Spawn a new 16-bit task.
480 HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
481 LPCSTR cmdline, BYTE len, HANDLE *hThread )
483 TDB *pTask;
485 if (!(pTask = TASK_Create( pModule, cmdShow, cmdline, len ))) return 0;
486 if (!(*hThread = CreateThread( NULL, 0, task_start, pTask, 0, NULL )))
488 TASK_DeleteTask( pTask->hSelf );
489 return 0;
491 return pTask->hSelf;
495 /***********************************************************************
496 * TASK_GetTaskFromThread
498 HTASK16 TASK_GetTaskFromThread( DWORD thread )
500 TDB *p = TASK_GetPtr( hFirstTask );
501 while (p)
503 if (p->teb->ClientId.UniqueThread == (HANDLE)thread) return p->hSelf;
504 p = TASK_GetPtr( p->hNext );
506 return 0;
510 /***********************************************************************
511 * TASK_CallTaskSignalProc
513 static void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
515 WORD args[5];
516 TDB *pTask = TASK_GetCurrent();
518 if ( !pTask || !pTask->userhandler ) return;
520 args[4] = hTaskOrModule;
521 args[3] = uCode;
522 args[2] = 0;
523 args[1] = pTask->hInstance;
524 args[0] = pTask->hQueue;
525 WOWCallback16Ex( (DWORD)pTask->userhandler, WCB16_PASCAL, sizeof(args), args, NULL );
529 /***********************************************************************
530 * TASK_ExitTask
532 void TASK_ExitTask(void)
534 WIN16_SUBSYSTEM_TIB *tib;
535 TDB *pTask;
536 DWORD lockCount;
538 /* Enter the Win16Lock to protect global data structures */
539 _EnterWin16Lock();
541 pTask = TASK_GetCurrent();
542 if ( !pTask )
544 _LeaveWin16Lock();
545 return;
548 TRACE("Killing task %04x\n", pTask->hSelf );
550 /* Perform USER cleanup */
552 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
554 /* Remove the task from the list to be sure we never switch back to it */
555 TASK_UnlinkTask( pTask->hSelf );
557 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
559 TRACE("this is the last task, exiting\n" );
560 ExitKernel16();
563 pTask->nEvents = 0;
565 if ( hLockedTask == pTask->hSelf )
566 hLockedTask = 0;
568 TASK_DeleteTask( pTask->hSelf );
570 if ((tib = NtCurrentTeb()->Tib.SubSystemTib))
572 if (tib->exe_name) RtlFreeUnicodeString( tib->exe_name );
573 HeapFree( GetProcessHeap(), 0, tib );
574 NtCurrentTeb()->Tib.SubSystemTib = NULL;
577 /* ... and completely release the Win16Lock, just in case. */
578 ReleaseThunkLock( &lockCount );
582 /***********************************************************************
583 * ExitKernel (KERNEL.2)
585 * Clean-up everything and exit the Wine process.
587 void WINAPI ExitKernel16(void)
589 WriteOutProfiles16();
590 TerminateProcess( GetCurrentProcess(), 0 );
594 /***********************************************************************
595 * InitTask (KERNEL.91)
597 * Called by the application startup code.
599 void WINAPI InitTask16( CONTEXT86 *context )
601 TDB *pTask;
602 INSTANCEDATA *pinstance;
603 SEGPTR ptr;
605 context->Eax = 0;
606 if (!(pTask = TASK_GetCurrent())) return;
608 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
609 as some apps, notably Visual Basic apps, *modify* the heap/stack
610 size of the instance data segment before calling InitTask() */
612 /* Initialize the INSTANCEDATA structure */
613 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
614 pinstance->stackmin = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + sizeof( STACK16FRAME );
615 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
616 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
617 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
619 /* Initialize the local heap */
620 if (LOWORD(context->Ecx))
621 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
623 /* Initialize implicitly loaded DLLs */
624 NE_InitializeDLLs( pTask->hModule );
625 NE_DllProcessAttach( pTask->hModule );
627 /* Registers on return are:
628 * ax 1 if OK, 0 on error
629 * cx stack limit in bytes
630 * dx cmdShow parameter
631 * si instance handle of the previous instance
632 * di instance handle of the new task
633 * es:bx pointer to command line inside PSP
635 * 0 (=%bp) is pushed on the stack
637 ptr = stack16_push( sizeof(WORD) );
638 *(WORD *)MapSL(ptr) = 0;
639 context->Esp -= 2;
641 context->Eax = 1;
643 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
644 else
646 LPBYTE p = &pTask->pdb.cmdLine[1];
647 while ((*p == ' ') || (*p == '\t')) p++;
648 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
650 context->Ecx = pinstance->stacktop;
651 context->Edx = pTask->nCmdShow;
652 context->Esi = (DWORD)pTask->hPrevInstance;
653 context->Edi = (DWORD)pTask->hInstance;
654 context->SegEs = (WORD)pTask->hPDB;
658 /***********************************************************************
659 * WaitEvent (KERNEL.30)
661 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
663 TDB *pTask;
665 if (!hTask) hTask = GetCurrentTask();
666 pTask = TASK_GetPtr( hTask );
668 if (pTask->flags & TDBF_WIN32)
670 FIXME("called for Win32 thread (%04lx)!\n", GetCurrentThreadId());
671 return TRUE;
674 if (pTask->nEvents > 0)
676 pTask->nEvents--;
677 return FALSE;
680 if (pTask->teb == NtCurrentTeb())
682 DWORD lockCount;
684 NtResetEvent( pTask->hEvent, NULL );
685 ReleaseThunkLock( &lockCount );
686 SYSLEVEL_CheckNotLevel( 1 );
687 WaitForSingleObject( pTask->hEvent, INFINITE );
688 RestoreThunkLock( lockCount );
689 if (pTask->nEvents > 0) pTask->nEvents--;
691 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
693 return TRUE;
697 /***********************************************************************
698 * PostEvent (KERNEL.31)
700 void WINAPI PostEvent16( HTASK16 hTask )
702 TDB *pTask;
704 if (!hTask) hTask = GetCurrentTask();
705 if (!(pTask = TASK_GetPtr( hTask ))) return;
707 if (pTask->flags & TDBF_WIN32)
709 FIXME("called for Win32 thread (%04lx)!\n", (DWORD)pTask->teb->ClientId.UniqueThread );
710 return;
713 pTask->nEvents++;
715 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
719 /***********************************************************************
720 * SetPriority (KERNEL.32)
722 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
724 TDB *pTask;
725 INT16 newpriority;
727 if (!hTask) hTask = GetCurrentTask();
728 if (!(pTask = TASK_GetPtr( hTask ))) return;
729 newpriority = pTask->priority + delta;
730 if (newpriority < -32) newpriority = -32;
731 else if (newpriority > 15) newpriority = 15;
733 pTask->priority = newpriority + 1;
734 TASK_UnlinkTask( pTask->hSelf );
735 TASK_LinkTask( pTask->hSelf );
736 pTask->priority--;
740 /***********************************************************************
741 * LockCurrentTask (KERNEL.33)
743 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
745 if (bLock) hLockedTask = GetCurrentTask();
746 else hLockedTask = 0;
747 return hLockedTask;
751 /***********************************************************************
752 * IsTaskLocked (KERNEL.122)
754 HTASK16 WINAPI IsTaskLocked16(void)
756 return hLockedTask;
760 /***********************************************************************
761 * OldYield (KERNEL.117)
763 void WINAPI OldYield16(void)
765 DWORD count;
767 ReleaseThunkLock(&count);
768 RestoreThunkLock(count);
771 /***********************************************************************
772 * WIN32_OldYield (KERNEL.447)
774 void WINAPI WIN32_OldYield16(void)
776 DWORD count;
778 ReleaseThunkLock(&count);
779 RestoreThunkLock(count);
782 /***********************************************************************
783 * DirectedYield (KERNEL.150)
785 void WINAPI DirectedYield16( HTASK16 hTask )
787 OldYield16();
790 /***********************************************************************
791 * Yield (KERNEL.29)
793 void WINAPI Yield16(void)
795 TDB *pCurTask = TASK_GetCurrent();
797 if (pCurTask && pCurTask->hQueue)
799 HMODULE mod = GetModuleHandleA( "user32.dll" );
800 if (mod)
802 FARPROC proc = GetProcAddress( mod, "UserYield16" );
803 if (proc)
805 proc();
806 return;
810 OldYield16();
813 /***********************************************************************
814 * KERNEL_490 (KERNEL.490)
816 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
818 if ( !someTask ) return 0;
820 FIXME("(%04x): stub\n", someTask );
821 return 0;
824 /***********************************************************************
825 * MakeProcInstance (KERNEL.51)
827 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
829 BYTE *thunk,*lfunc;
830 SEGPTR thunkaddr;
831 WORD hInstanceSelector;
833 hInstanceSelector = GlobalHandleToSel16(hInstance);
835 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
837 if (!HIWORD(func)) {
838 /* Win95 actually protects via SEH, but this is better for debugging */
839 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
840 return (FARPROC16)0;
843 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
844 && (hInstance != 0)
845 && (hInstance != 0xffff) )
847 /* calling MPI with a foreign DSEG is invalid ! */
848 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
849 hInstance,CURRENT_DS);
852 /* Always use the DSEG that MPI was entered with.
853 * We used to set hInstance to GetTaskDS16(), but this should be wrong
854 * as CURRENT_DS provides the DSEG value we need.
855 * ("calling" DS, *not* "task" DS !) */
856 hInstanceSelector = CURRENT_DS;
857 hInstance = GlobalHandle16(hInstanceSelector);
859 /* no thunking for DLLs */
860 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
861 return func;
863 thunkaddr = TASK_AllocThunk();
864 if (!thunkaddr) return (FARPROC16)0;
865 thunk = MapSL( thunkaddr );
866 lfunc = MapSL( (SEGPTR)func );
868 TRACE("(%08lx,%04x): got thunk %08lx\n",
869 (DWORD)func, hInstance, (DWORD)thunkaddr );
870 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
871 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
873 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
876 *thunk++ = 0xb8; /* movw instance, %ax */
877 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
878 *thunk++ = (BYTE)(hInstanceSelector >> 8);
879 *thunk++ = 0xea; /* ljmp func */
880 *(DWORD *)thunk = (DWORD)func;
881 return (FARPROC16)thunkaddr;
882 /* CX reg indicates if thunkaddr != NULL, implement if needed */
886 /***********************************************************************
887 * FreeProcInstance (KERNEL.52)
889 void WINAPI FreeProcInstance16( FARPROC16 func )
891 TRACE("(%08lx)\n", (DWORD)func );
892 TASK_FreeThunk( (SEGPTR)func );
895 /**********************************************************************
896 * TASK_GetCodeSegment
898 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
899 * and logical segment number of a given code segment.
901 * 'proc' either *is* already a pair of module handle and segment number,
902 * in which case there's nothing to do. Otherwise, it is a pointer to
903 * a function, and we need to retrieve the code segment. If the pointer
904 * happens to point to a thunk, we'll retrieve info about the code segment
905 * where the function pointed to by the thunk resides, not the thunk itself.
907 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
908 * the function the snoop code will return to ...
911 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
912 SEGTABLEENTRY **ppSeg, int *pSegNr )
914 NE_MODULE *pModule = NULL;
915 SEGTABLEENTRY *pSeg = NULL;
916 int segNr=0;
918 /* Try pair of module handle / segment number */
919 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
920 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
922 segNr = LOWORD( proc );
923 if ( segNr && segNr <= pModule->seg_count )
924 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
927 /* Try thunk or function */
928 else
930 BYTE *thunk = MapSL( (SEGPTR)proc );
931 WORD selector;
933 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
934 selector = thunk[6] + (thunk[7] << 8);
935 else
936 selector = HIWORD( proc );
938 pModule = NE_GetPtr( GlobalHandle16( selector ) );
939 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
941 if ( pModule )
942 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
943 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
944 break;
946 if ( pModule && segNr > pModule->seg_count )
947 pSeg = NULL;
950 /* Abort if segment not found */
952 if ( !pModule || !pSeg )
953 return FALSE;
955 /* Return segment data */
957 if ( ppModule ) *ppModule = pModule;
958 if ( ppSeg ) *ppSeg = pSeg;
959 if ( pSegNr ) *pSegNr = segNr;
961 return TRUE;
964 /**********************************************************************
965 * GetCodeHandle (KERNEL.93)
967 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
969 SEGTABLEENTRY *pSeg;
971 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
972 return (HANDLE16)0;
974 return pSeg->hSeg;
977 /**********************************************************************
978 * GetCodeInfo (KERNEL.104)
980 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
982 NE_MODULE *pModule;
983 SEGTABLEENTRY *pSeg;
984 int segNr;
986 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
987 return FALSE;
989 /* Fill in segment information */
991 segInfo->offSegment = pSeg->filepos;
992 segInfo->cbSegment = pSeg->size;
993 segInfo->flags = pSeg->flags;
994 segInfo->cbAlloc = pSeg->minsize;
995 segInfo->h = pSeg->hSeg;
996 segInfo->alignShift = pModule->alignment;
998 if ( segNr == pModule->dgroup )
999 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
1001 /* Return module handle in %es */
1003 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
1005 return TRUE;
1009 /**********************************************************************
1010 * DefineHandleTable (KERNEL.94)
1012 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1014 FIXME("(%04x): stub ?\n", wOffset);
1015 return TRUE;
1019 /***********************************************************************
1020 * SetTaskQueue (KERNEL.34)
1022 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1024 FIXME( "stub, should not get called\n" );
1025 return 0xbeef;
1029 /***********************************************************************
1030 * GetTaskQueue (KERNEL.35)
1032 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1034 FIXME( "stub, should not get called\n" );
1035 return 0xbeef;
1038 /***********************************************************************
1039 * SetThreadQueue (KERNEL.463)
1041 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1043 FIXME( "stub, should not get called\n" );
1044 return 0xbeef;
1047 /***********************************************************************
1048 * GetThreadQueue (KERNEL.464)
1050 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1052 FIXME( "stub, should not get called\n" );
1053 return 0xbeef;
1056 /***********************************************************************
1057 * SetFastQueue (KERNEL.624)
1059 VOID WINAPI SetFastQueue16( DWORD thread, HQUEUE16 hQueue )
1061 FIXME( "stub, should not get called\n" );
1064 /***********************************************************************
1065 * GetFastQueue (KERNEL.625)
1067 HQUEUE16 WINAPI GetFastQueue16( void )
1069 FIXME( "stub, should not get called\n" );
1070 return 0xbeef;
1073 /***********************************************************************
1074 * SwitchStackTo (KERNEL.108)
1076 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1078 STACK16FRAME *oldFrame, *newFrame;
1079 INSTANCEDATA *pData;
1080 UINT16 copySize;
1082 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1083 TRACE("old=%04x:%04x new=%04x:%04x\n",
1084 SELECTOROF( NtCurrentTeb()->WOW32Reserved ),
1085 OFFSETOF( NtCurrentTeb()->WOW32Reserved ), seg, ptr );
1087 /* Save the old stack */
1089 oldFrame = CURRENT_STACK16;
1090 /* pop frame + args and push bp */
1091 pData->old_ss_sp = (SEGPTR)NtCurrentTeb()->WOW32Reserved + sizeof(STACK16FRAME)
1092 + 2 * sizeof(WORD);
1093 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1094 pData->stacktop = top;
1095 pData->stackmin = ptr;
1096 pData->stackbottom = ptr;
1098 /* Switch to the new stack */
1100 /* Note: we need to take the 3 arguments into account; otherwise,
1101 * the stack will underflow upon return from this function.
1103 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1104 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1105 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR( seg, ptr - copySize );
1106 newFrame = CURRENT_STACK16;
1108 /* Copy the stack frame and the local variables to the new stack */
1110 memmove( newFrame, oldFrame, copySize );
1111 newFrame->bp = ptr;
1112 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1116 /***********************************************************************
1117 * SwitchStackBack (KERNEL.109)
1119 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1121 STACK16FRAME *oldFrame, *newFrame;
1122 INSTANCEDATA *pData;
1124 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->WOW32Reserved))))
1125 return;
1126 if (!pData->old_ss_sp)
1128 WARN("No previous SwitchStackTo\n" );
1129 return;
1131 TRACE("restoring stack %04x:%04x\n",
1132 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1134 oldFrame = CURRENT_STACK16;
1136 /* Pop bp from the previous stack */
1138 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(pData->old_ss_sp);
1139 pData->old_ss_sp += sizeof(WORD);
1141 /* Switch back to the old stack */
1143 NtCurrentTeb()->WOW32Reserved = (void *)(pData->old_ss_sp - sizeof(STACK16FRAME));
1144 context->SegSs = SELECTOROF(pData->old_ss_sp);
1145 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1146 pData->old_ss_sp = 0;
1148 /* Build a stack frame for the return */
1150 newFrame = CURRENT_STACK16;
1151 newFrame->frame32 = oldFrame->frame32;
1152 newFrame->module_cs = oldFrame->module_cs;
1153 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1154 newFrame->entry_ip = oldFrame->entry_ip;
1158 /***********************************************************************
1159 * GetTaskQueueDS (KERNEL.118)
1161 void WINAPI GetTaskQueueDS16(void)
1163 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1167 /***********************************************************************
1168 * GetTaskQueueES (KERNEL.119)
1170 void WINAPI GetTaskQueueES16(void)
1172 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1176 /***********************************************************************
1177 * GetCurrentTask (KERNEL32.@)
1179 HTASK16 WINAPI GetCurrentTask(void)
1181 return NtCurrentTeb()->htask16;
1184 /***********************************************************************
1185 * GetCurrentTask (KERNEL.36)
1187 DWORD WINAPI WIN16_GetCurrentTask(void)
1189 /* This is the version used by relay code; the first task is */
1190 /* returned in the high word of the result */
1191 return MAKELONG( GetCurrentTask(), hFirstTask );
1195 /***********************************************************************
1196 * GetCurrentPDB (KERNEL.37)
1198 * UNDOC: returns PSP of KERNEL in high word
1200 DWORD WINAPI GetCurrentPDB16(void)
1202 TDB *pTask;
1204 if (!(pTask = TASK_GetCurrent())) return 0;
1205 return MAKELONG(pTask->hPDB, 0); /* FIXME */
1209 /***********************************************************************
1210 * GetCurPID (KERNEL.157)
1212 DWORD WINAPI GetCurPID16( DWORD unused )
1214 return 0;
1218 /***********************************************************************
1219 * GetInstanceData (KERNEL.54)
1221 INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
1223 char *ptr = (char *)GlobalLock16( instance );
1224 if (!ptr || !len) return 0;
1225 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1226 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
1227 return len;
1231 /***********************************************************************
1232 * GetExeVersion (KERNEL.105)
1234 WORD WINAPI GetExeVersion16(void)
1236 TDB *pTask;
1238 if (!(pTask = TASK_GetCurrent())) return 0;
1239 return pTask->version;
1243 /***********************************************************************
1244 * SetErrorMode (KERNEL.107)
1246 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1248 TDB *pTask;
1249 if (!(pTask = TASK_GetCurrent())) return 0;
1250 pTask->error_mode = mode;
1251 return SetErrorMode( mode );
1255 /***********************************************************************
1256 * GetNumTasks (KERNEL.152)
1258 UINT16 WINAPI GetNumTasks16(void)
1260 return nTaskCount;
1264 /***********************************************************************
1265 * GetTaskDS (KERNEL.155)
1267 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1268 * I don't think we need to bother with this.
1270 HINSTANCE16 WINAPI GetTaskDS16(void)
1272 TDB *pTask;
1274 if (!(pTask = TASK_GetCurrent())) return 0;
1275 return GlobalHandleToSel16(pTask->hInstance);
1278 /***********************************************************************
1279 * GetDummyModuleHandleDS (KERNEL.602)
1281 WORD WINAPI GetDummyModuleHandleDS16(void)
1283 TDB *pTask;
1284 WORD selector;
1286 if (!(pTask = TASK_GetCurrent())) return 0;
1287 if (!(pTask->flags & TDBF_WIN32)) return 0;
1288 selector = GlobalHandleToSel16( pTask->hModule );
1289 CURRENT_DS = selector;
1290 return selector;
1293 /***********************************************************************
1294 * IsTask (KERNEL.320)
1296 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1298 TDB *pTask;
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 */
1312 return FALSE;
1315 /***********************************************************************
1316 * SetTaskSignalProc (KERNEL.38)
1318 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1320 TDB *pTask;
1321 FARPROC16 oldProc;
1323 if (!hTask) hTask = GetCurrentTask();
1324 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1325 oldProc = pTask->userhandler;
1326 pTask->userhandler = proc;
1327 return oldProc;
1330 /***********************************************************************
1331 * SetSigHandler (KERNEL.140)
1333 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1334 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1336 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1337 newhandler,oldhandler,oldmode,newmode,flag );
1339 if (flag != 1) return 0;
1340 if (!newmode) newhandler = NULL; /* Default handler */
1341 if (newmode != 4)
1343 TDB *pTask;
1345 if (!(pTask = TASK_GetCurrent())) return 0;
1346 if (oldmode) *oldmode = pTask->signal_flags;
1347 pTask->signal_flags = newmode;
1348 if (oldhandler) *oldhandler = pTask->sighandler;
1349 pTask->sighandler = newhandler;
1351 return 0;
1355 /***********************************************************************
1356 * GlobalNotify (KERNEL.154)
1358 * Note that GlobalNotify does _not_ return the old NotifyProc
1359 * -- contrary to LocalNotify !!
1361 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1363 TDB *pTask;
1365 if (!(pTask = TASK_GetCurrent())) return;
1366 pTask->discardhandler = proc;
1370 /***********************************************************************
1371 * GetExePtrHelper
1373 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1375 char *ptr;
1376 HANDLE16 owner;
1378 /* Check for module handle */
1380 if (!(ptr = GlobalLock16( handle ))) return 0;
1381 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1383 /* Search for this handle inside all tasks */
1385 *hTask = hFirstTask;
1386 while (*hTask)
1388 TDB *pTask = TASK_GetPtr( *hTask );
1389 if ((*hTask == handle) ||
1390 (pTask->hInstance == handle) ||
1391 (pTask->hQueue == handle) ||
1392 (pTask->hPDB == handle)) return pTask->hModule;
1393 *hTask = pTask->hNext;
1396 /* Check the owner for module handle */
1398 owner = FarGetOwner16( handle );
1399 if (!(ptr = GlobalLock16( owner ))) return 0;
1400 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1402 /* Search for the owner inside all tasks */
1404 *hTask = hFirstTask;
1405 while (*hTask)
1407 TDB *pTask = TASK_GetPtr( *hTask );
1408 if ((*hTask == owner) ||
1409 (pTask->hInstance == owner) ||
1410 (pTask->hQueue == owner) ||
1411 (pTask->hPDB == owner)) return pTask->hModule;
1412 *hTask = pTask->hNext;
1415 return 0;
1418 /***********************************************************************
1419 * GetExePtr (KERNEL.133)
1421 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1423 HTASK16 hTask = 0;
1424 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1425 STACK16FRAME *frame = CURRENT_STACK16;
1426 frame->ecx = hModule;
1427 if (hTask) frame->es = hTask;
1428 return hModule;
1432 /***********************************************************************
1433 * K228 (KERNEL.228)
1435 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1437 HTASK16 hTask = 0;
1438 return GetExePtrHelper( handle, &hTask );
1442 /***********************************************************************
1443 * TaskFirst (TOOLHELP.63)
1445 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1447 lpte->hNext = hFirstTask;
1448 return TaskNext16( lpte );
1452 /***********************************************************************
1453 * TaskNext (TOOLHELP.64)
1455 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1457 TDB *pTask;
1458 INSTANCEDATA *pInstData;
1460 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1461 if (!lpte->hNext) return FALSE;
1463 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1464 while (1) {
1465 pTask = TASK_GetPtr( lpte->hNext );
1466 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1467 if (pTask->hInstance)
1468 break;
1469 lpte->hNext = pTask->hNext;
1471 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1472 lpte->hTask = lpte->hNext;
1473 lpte->hTaskParent = pTask->hParent;
1474 lpte->hInst = pTask->hInstance;
1475 lpte->hModule = pTask->hModule;
1476 lpte->wSS = SELECTOROF( pTask->teb->WOW32Reserved );
1477 lpte->wSP = OFFSETOF( pTask->teb->WOW32Reserved );
1478 lpte->wStackTop = pInstData->stacktop;
1479 lpte->wStackMinimum = pInstData->stackmin;
1480 lpte->wStackBottom = pInstData->stackbottom;
1481 lpte->wcEvents = pTask->nEvents;
1482 lpte->hQueue = pTask->hQueue;
1483 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1484 lpte->wPSPOffset = 0x100; /*??*/
1485 lpte->hNext = pTask->hNext;
1486 return TRUE;
1490 /***********************************************************************
1491 * TaskFindHandle (TOOLHELP.65)
1493 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1495 lpte->hNext = hTask;
1496 return TaskNext16( lpte );
1500 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1502 /**************************************************************************
1503 * FatalAppExit (KERNEL.137)
1505 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1507 TDB *pTask = TASK_GetCurrent();
1509 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1511 HMODULE mod = GetModuleHandleA( "user32.dll" );
1512 if (mod)
1514 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1515 if (pMessageBoxA)
1517 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1518 goto done;
1521 ERR( "%s\n", debugstr_a(str) );
1523 done:
1524 ExitThread(0xff);
1528 /***********************************************************************
1529 * TerminateApp (TOOLHELP.77)
1531 * See "Undocumented Windows".
1533 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1535 if (hTask && hTask != GetCurrentTask())
1537 FIXME("cannot terminate task %x\n", hTask);
1538 return;
1541 if (wFlags & NO_UAE_BOX)
1543 UINT16 old_mode;
1544 old_mode = SetErrorMode16(0);
1545 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1547 FatalAppExit16( 0, NULL );
1549 /* hmm, we're still alive ?? */
1551 /* check undocumented flag */
1552 if (!(wFlags & 0x8000))
1553 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1555 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1556 but let's just call ExitThread */
1557 ExitThread(0xff);
1561 /***********************************************************************
1562 * GetAppCompatFlags (KERNEL.354)
1564 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1566 TDB *pTask;
1568 if (!hTask) hTask = GetCurrentTask();
1569 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1570 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1571 return pTask->compat_flags;
1575 /***********************************************************************
1576 * GetDOSEnvironment (KERNEL.131)
1578 * Note: the environment is allocated once, it doesn't track changes
1579 * made using the Win32 API. This shouldn't matter.
1581 * Format of a 16-bit environment block:
1582 * ASCIIZ string 1 (xx=yy format)
1583 * ...
1584 * ASCIIZ string n
1585 * BYTE 0
1586 * WORD 1
1587 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1589 SEGPTR WINAPI GetDOSEnvironment16(void)
1591 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1592 static HGLOBAL16 handle; /* handle to the 16 bit environment */
1594 if (!handle)
1596 DWORD size;
1597 LPSTR p, env;
1599 p = env = GetEnvironmentStringsA();
1600 while (*p) p += strlen(p) + 1;
1601 p++; /* skip last null */
1602 size = (p - env) + sizeof(WORD) + sizeof(ENV_program_name);
1603 handle = GlobalAlloc16( GMEM_FIXED, size );
1604 if (handle)
1606 WORD one = 1;
1607 LPSTR env16 = GlobalLock16( handle );
1608 memcpy( env16, env, p - env );
1609 memcpy( env16 + (p - env), &one, sizeof(one));
1610 memcpy( env16 + (p - env) + sizeof(WORD), ENV_program_name, sizeof(ENV_program_name));
1611 GlobalUnlock16( handle );
1613 FreeEnvironmentStringsA( env );
1615 return K32WOWGlobalLock16( handle );