4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
39 #include "wine/winbase16.h"
43 #include "wine/server.h"
44 #include "stackframe.h"
47 #include "kernel_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(task);
52 WINE_DECLARE_DEBUG_CHANNEL(relay);
53 WINE_DECLARE_DEBUG_CHANNEL(toolhelp);
57 /* Segment containing MakeProcInstance() thunks */
60 WORD next; /* Selector of next segment */
61 WORD magic; /* Thunks signature */
63 WORD free; /* Head of the free list */
64 WORD thunks[4]; /* Each thunk is 4 words long */
69 #define THUNK_MAGIC ('P' | ('T' << 8))
71 /* Min. number of thunks allocated when creating a new segment */
74 #define TDB_MAGIC ('T' | ('D' << 8))
76 static THHOOK DefaultThhook;
77 THHOOK *pThhook = &DefaultThhook;
79 #define hFirstTask (pThhook->HeadTDB)
80 #define hLockedTask (pThhook->LockTDB)
82 static UINT16 nTaskCount = 0;
84 static HTASK16 initial_task;
86 /***********************************************************************
89 void TASK_InstallTHHook( THHOOK *pNewThhook )
91 THHOOK *pOldThhook = pThhook;
93 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
95 *pThhook = *pOldThhook;
98 /***********************************************************************
101 static TDB *TASK_GetPtr( HTASK16 hTask )
103 return GlobalLock16( hTask );
107 /***********************************************************************
110 TDB *TASK_GetCurrent(void)
112 return TASK_GetPtr( GetCurrentTask() );
116 /***********************************************************************
119 static void TASK_LinkTask( HTASK16 hTask )
124 if (!(pTask = TASK_GetPtr( hTask ))) return;
125 prevTask = &hFirstTask;
128 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
129 if (prevTaskPtr->priority >= pTask->priority) break;
130 prevTask = &prevTaskPtr->hNext;
132 pTask->hNext = *prevTask;
138 /***********************************************************************
141 static void TASK_UnlinkTask( HTASK16 hTask )
146 prevTask = &hFirstTask;
147 while (*prevTask && (*prevTask != hTask))
149 pTask = TASK_GetPtr( *prevTask );
150 prevTask = &pTask->hNext;
154 pTask = TASK_GetPtr( *prevTask );
155 *prevTask = pTask->hNext;
162 /***********************************************************************
165 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
166 * and containing 'count' entries.
168 static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
174 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
176 pThunk->magic = THUNK_MAGIC;
177 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
179 for (i = 0; i < count-1; i++)
181 free += 8; /* Offset of next thunk */
182 pThunk->thunks[4*i] = free;
184 pThunk->thunks[4*i] = 0; /* Last thunk */
188 /***********************************************************************
191 * Allocate a thunk for MakeProcInstance().
193 static SEGPTR TASK_AllocThunk(void)
199 if (!(pTask = TASK_GetCurrent())) return 0;
200 sel = pTask->hCSAlias;
201 pThunk = (THUNKS *)&pTask->thunks;
202 base = (char *)pThunk - (char *)pTask;
203 while (!pThunk->free)
206 if (!sel) /* Allocate a new segment */
208 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
209 pTask->hPDB, WINE_LDT_FLAGS_CODE );
210 if (!sel) return (SEGPTR)0;
211 TASK_CreateThunks( sel, 0, MIN_THUNKS );
214 pThunk = (THUNKS *)GlobalLock16( sel );
217 base += pThunk->free;
218 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
219 return MAKESEGPTR( sel, base );
223 /***********************************************************************
226 * Free a MakeProcInstance() thunk.
228 static BOOL TASK_FreeThunk( SEGPTR thunk )
234 if (!(pTask = TASK_GetCurrent())) return 0;
235 sel = pTask->hCSAlias;
236 pThunk = (THUNKS *)&pTask->thunks;
237 base = (char *)pThunk - (char *)pTask;
238 while (sel && (sel != HIWORD(thunk)))
241 pThunk = (THUNKS *)GlobalLock16( sel );
244 if (!sel) return FALSE;
245 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
246 pThunk->free = LOWORD(thunk) - base;
251 /***********************************************************************
254 * NOTE: This routine might be called by a Win32 thread. Thus, we need
255 * to be careful to protect global data structures. We do this
256 * by entering the Win16Lock while linking the task into the
259 static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, LPCSTR cmdline, BYTE len )
264 char curdir[MAX_PATH];
265 HMODULE16 hModule = pModule ? pModule->self : 0;
267 /* Allocate the task structure */
269 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
270 if (!hTask) return NULL;
271 pTask = TASK_GetPtr( hTask );
272 FarSetOwner16( hTask, hModule );
274 /* Fill the task structure */
276 pTask->hSelf = hTask;
278 pTask->version = pModule ? pModule->expected_version : 0x0400;
279 pTask->hModule = hModule;
280 pTask->hParent = GetCurrentTask();
281 pTask->magic = TDB_MAGIC;
282 pTask->nCmdShow = cmdShow;
284 GetCurrentDirectoryA( sizeof(curdir), curdir );
285 GetShortPathNameA( curdir, curdir, sizeof(curdir) );
286 pTask->curdrive = (curdir[0] - 'A') | 0x80;
287 lstrcpynA( pTask->curdir, curdir + 2, sizeof(pTask->curdir) );
289 /* Create the thunks block */
291 TASK_CreateThunks( hTask, (char *)&pTask->thunks - (char *)pTask, 7 );
293 /* Copy the module name */
298 GetModuleName16( hModule, name, sizeof(name) );
299 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
300 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
303 /* Allocate a selector for the PDB */
305 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
306 hModule, WINE_LDT_FLAGS_DATA );
310 pTask->pdb.int20 = 0x20cd;
311 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
312 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
313 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
314 pTask->pdb.savedint22 = 0;
315 pTask->pdb.savedint23 = 0;
316 pTask->pdb.savedint24 = 0;
317 pTask->pdb.fileHandlesPtr =
318 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
319 pTask->pdb.hFileHandles = 0;
320 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
321 /* FIXME: should we make a copy of the environment? */
322 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
323 pTask->pdb.nbFiles = 20;
325 /* Fill the command line */
329 cmdline = GetCommandLineA();
330 /* remove the first word (program name) */
332 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
333 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
334 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
335 len = strlen(cmdline);
337 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
338 pTask->pdb.cmdLine[0] = len;
339 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
340 /* pTask->pdb.cmdLine[len+1] = 0; */
342 TRACE("cmdline='%.*s' task=%04x\n", len, cmdline, hTask );
344 /* Allocate a code segment alias for the TDB */
346 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
347 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
349 /* Default DTA overwrites command line */
351 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
353 /* Create scheduler event for 16-bit tasks */
355 if ( !(pTask->flags & TDBF_WIN32) )
356 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
358 if (!initial_task) initial_task = hTask;
364 /***********************************************************************
367 static void TASK_DeleteTask( HTASK16 hTask )
372 if (!(pTask = TASK_GetPtr( hTask ))) return;
375 pTask->magic = 0xdead; /* invalidate signature */
377 /* Free the selector aliases */
379 GLOBAL_FreeBlock( pTask->hCSAlias );
380 GLOBAL_FreeBlock( pTask->hPDB );
382 /* Free the task module */
384 FreeModule16( pTask->hModule );
386 /* Free the task structure itself */
388 GlobalFree16( hTask );
390 /* Free all memory used by this task (including the 32-bit stack, */
391 /* the environment block and the thunk segments). */
393 GlobalFreeAll16( hPDB );
397 /***********************************************************************
398 * TASK_CreateMainTask
400 * Create a task for the main (32-bit) process.
402 void TASK_CreateMainTask(void)
405 STARTUPINFOA startup_info;
406 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
408 GetStartupInfoA( &startup_info );
409 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
410 pTask = TASK_Create( NULL, cmdShow, NULL, 0 );
413 ERR("could not create task for main process\n");
417 pTask->flags |= TDBF_WIN32;
418 pTask->hInstance = 0;
419 pTask->hPrevInstance = 0;
420 pTask->teb = NtCurrentTeb();
422 NtCurrentTeb()->htask16 = pTask->hSelf;
424 /* Add the task to the linked list */
425 /* (no need to get the win16 lock, we are the only thread at this point) */
426 TASK_LinkTask( pTask->hSelf );
430 /* allocate the win16 TIB for a new 16-bit task */
431 static WIN16_SUBSYSTEM_TIB *allocate_win16_tib( TDB *pTask )
433 WCHAR path[MAX_PATH];
434 WIN16_SUBSYSTEM_TIB *tib;
435 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
437 if (!(tib = HeapAlloc( GetProcessHeap(), 0, sizeof(*tib) ))) return NULL;
438 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
439 GetLongPathNameW( path, path, MAX_PATH );
440 if (RtlCreateUnicodeString( &tib->exe_str, path )) tib->exe_name = &tib->exe_str;
441 else tib->exe_name = NULL;
445 /* startup routine for a new 16-bit thread */
446 static DWORD CALLBACK task_start( TDB *pTask )
450 NtCurrentTeb()->htask16 = pTask->hSelf;
451 NtCurrentTeb()->Tib.SubSystemTib = allocate_win16_tib( pTask );
454 TASK_LinkTask( pTask->hSelf );
455 pTask->teb = NtCurrentTeb();
456 ret = NE_StartTask();
462 /***********************************************************************
465 * Spawn a new 16-bit task.
467 HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
468 LPCSTR cmdline, BYTE len, HANDLE *hThread )
472 if (!(pTask = TASK_Create( pModule, cmdShow, cmdline, len ))) return 0;
473 if (!(*hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)task_start, pTask, 0, NULL )))
475 TASK_DeleteTask( pTask->hSelf );
482 /***********************************************************************
483 * TASK_GetTaskFromThread
485 HTASK16 TASK_GetTaskFromThread( DWORD thread )
487 TDB *p = TASK_GetPtr( hFirstTask );
490 if (p->teb->ClientId.UniqueThread == (HANDLE)thread) return p->hSelf;
491 p = TASK_GetPtr( p->hNext );
497 /***********************************************************************
498 * TASK_CallTaskSignalProc
500 static void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
503 TDB *pTask = TASK_GetCurrent();
505 if ( !pTask || !pTask->userhandler ) return;
507 args[4] = hTaskOrModule;
510 args[1] = pTask->hInstance;
511 args[0] = pTask->hQueue;
512 WOWCallback16Ex( (DWORD)pTask->userhandler, WCB16_PASCAL, sizeof(args), args, NULL );
516 /***********************************************************************
519 void TASK_ExitTask(void)
524 /* Enter the Win16Lock to protect global data structures */
527 pTask = TASK_GetCurrent();
534 TRACE("Killing task %04x\n", pTask->hSelf );
536 /* Perform USER cleanup */
538 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
540 /* Remove the task from the list to be sure we never switch back to it */
541 TASK_UnlinkTask( pTask->hSelf );
543 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
545 TRACE("this is the last task, exiting\n" );
551 if ( hLockedTask == pTask->hSelf )
554 TASK_DeleteTask( pTask->hSelf );
556 /* ... and completely release the Win16Lock, just in case. */
557 ReleaseThunkLock( &lockCount );
561 /***********************************************************************
562 * ExitKernel (KERNEL.2)
564 * Clean-up everything and exit the Wine process.
566 void WINAPI ExitKernel16(void)
568 WriteOutProfiles16();
569 TerminateProcess( GetCurrentProcess(), 0 );
573 /***********************************************************************
574 * InitTask (KERNEL.91)
576 * Called by the application startup code.
578 void WINAPI InitTask16( CONTEXT86 *context )
581 INSTANCEDATA *pinstance;
585 if (!(pTask = TASK_GetCurrent())) return;
587 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
588 as some apps, notably Visual Basic apps, *modify* the heap/stack
589 size of the instance data segment before calling InitTask() */
591 /* Initialize the INSTANCEDATA structure */
592 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
593 pinstance->stackmin = OFFSETOF( NtCurrentTeb()->cur_stack ) + sizeof( STACK16FRAME );
594 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
595 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
596 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
598 /* Initialize the local heap */
599 if (LOWORD(context->Ecx))
600 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
602 /* Initialize implicitly loaded DLLs */
603 NE_InitializeDLLs( pTask->hModule );
604 NE_DllProcessAttach( pTask->hModule );
606 /* Registers on return are:
607 * ax 1 if OK, 0 on error
608 * cx stack limit in bytes
609 * dx cmdShow parameter
610 * si instance handle of the previous instance
611 * di instance handle of the new task
612 * es:bx pointer to command line inside PSP
614 * 0 (=%bp) is pushed on the stack
616 ptr = stack16_push( sizeof(WORD) );
617 *(WORD *)MapSL(ptr) = 0;
622 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
625 LPBYTE p = &pTask->pdb.cmdLine[1];
626 while ((*p == ' ') || (*p == '\t')) p++;
627 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
629 context->Ecx = pinstance->stacktop;
630 context->Edx = pTask->nCmdShow;
631 context->Esi = (DWORD)pTask->hPrevInstance;
632 context->Edi = (DWORD)pTask->hInstance;
633 context->SegEs = (WORD)pTask->hPDB;
637 /***********************************************************************
638 * WaitEvent (KERNEL.30)
640 BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
644 if (!hTask) hTask = GetCurrentTask();
645 pTask = TASK_GetPtr( hTask );
647 if (pTask->flags & TDBF_WIN32)
649 FIXME("called for Win32 thread (%04lx)!\n", GetCurrentThreadId());
653 if (pTask->nEvents > 0)
659 if (pTask->teb == NtCurrentTeb())
663 NtResetEvent( pTask->hEvent, NULL );
664 ReleaseThunkLock( &lockCount );
665 SYSLEVEL_CheckNotLevel( 1 );
666 WaitForSingleObject( pTask->hEvent, INFINITE );
667 RestoreThunkLock( lockCount );
668 if (pTask->nEvents > 0) pTask->nEvents--;
670 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
676 /***********************************************************************
677 * PostEvent (KERNEL.31)
679 void WINAPI PostEvent16( HTASK16 hTask )
683 if (!hTask) hTask = GetCurrentTask();
684 if (!(pTask = TASK_GetPtr( hTask ))) return;
686 if (pTask->flags & TDBF_WIN32)
688 FIXME("called for Win32 thread (%04lx)!\n", (DWORD)pTask->teb->ClientId.UniqueThread );
694 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
698 /***********************************************************************
699 * SetPriority (KERNEL.32)
701 void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
706 if (!hTask) hTask = GetCurrentTask();
707 if (!(pTask = TASK_GetPtr( hTask ))) return;
708 newpriority = pTask->priority + delta;
709 if (newpriority < -32) newpriority = -32;
710 else if (newpriority > 15) newpriority = 15;
712 pTask->priority = newpriority + 1;
713 TASK_UnlinkTask( pTask->hSelf );
714 TASK_LinkTask( pTask->hSelf );
719 /***********************************************************************
720 * LockCurrentTask (KERNEL.33)
722 HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
724 if (bLock) hLockedTask = GetCurrentTask();
725 else hLockedTask = 0;
730 /***********************************************************************
731 * IsTaskLocked (KERNEL.122)
733 HTASK16 WINAPI IsTaskLocked16(void)
739 /***********************************************************************
740 * OldYield (KERNEL.117)
742 void WINAPI OldYield16(void)
746 ReleaseThunkLock(&count);
747 RestoreThunkLock(count);
750 /***********************************************************************
751 * WIN32_OldYield (KERNEL.447)
753 void WINAPI WIN32_OldYield16(void)
757 ReleaseThunkLock(&count);
758 RestoreThunkLock(count);
761 /***********************************************************************
762 * DirectedYield (KERNEL.150)
764 void WINAPI DirectedYield16( HTASK16 hTask )
769 /***********************************************************************
772 void WINAPI Yield16(void)
774 TDB *pCurTask = TASK_GetCurrent();
776 if (pCurTask && pCurTask->hQueue)
778 HMODULE mod = GetModuleHandleA( "user32.dll" );
781 FARPROC proc = GetProcAddress( mod, "UserYield16" );
792 /***********************************************************************
793 * KERNEL_490 (KERNEL.490)
795 HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
797 if ( !someTask ) return 0;
799 FIXME("(%04x): stub\n", someTask );
803 /***********************************************************************
804 * MakeProcInstance (KERNEL.51)
806 FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
810 WORD hInstanceSelector;
812 hInstanceSelector = GlobalHandleToSel16(hInstance);
814 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
817 /* Win95 actually protects via SEH, but this is better for debugging */
818 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
822 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
824 && (hInstance != 0xffff) )
826 /* calling MPI with a foreign DSEG is invalid ! */
827 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
828 hInstance,CURRENT_DS);
831 /* Always use the DSEG that MPI was entered with.
832 * We used to set hInstance to GetTaskDS16(), but this should be wrong
833 * as CURRENT_DS provides the DSEG value we need.
834 * ("calling" DS, *not* "task" DS !) */
835 hInstanceSelector = CURRENT_DS;
836 hInstance = GlobalHandle16(hInstanceSelector);
838 /* no thunking for DLLs */
839 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
842 thunkaddr = TASK_AllocThunk();
843 if (!thunkaddr) return (FARPROC16)0;
844 thunk = MapSL( thunkaddr );
845 lfunc = MapSL( (SEGPTR)func );
847 TRACE("(%08lx,%04x): got thunk %08lx\n",
848 (DWORD)func, hInstance, (DWORD)thunkaddr );
849 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
850 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
852 WARN("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
855 *thunk++ = 0xb8; /* movw instance, %ax */
856 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
857 *thunk++ = (BYTE)(hInstanceSelector >> 8);
858 *thunk++ = 0xea; /* ljmp func */
859 *(DWORD *)thunk = (DWORD)func;
860 return (FARPROC16)thunkaddr;
861 /* CX reg indicates if thunkaddr != NULL, implement if needed */
865 /***********************************************************************
866 * FreeProcInstance (KERNEL.52)
868 void WINAPI FreeProcInstance16( FARPROC16 func )
870 TRACE("(%08lx)\n", (DWORD)func );
871 TASK_FreeThunk( (SEGPTR)func );
874 /**********************************************************************
875 * TASK_GetCodeSegment
877 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
878 * and logical segment number of a given code segment.
880 * 'proc' either *is* already a pair of module handle and segment number,
881 * in which case there's nothing to do. Otherwise, it is a pointer to
882 * a function, and we need to retrieve the code segment. If the pointer
883 * happens to point to a thunk, we'll retrieve info about the code segment
884 * where the function pointed to by the thunk resides, not the thunk itself.
886 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
887 * the function the snoop code will return to ...
890 static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
891 SEGTABLEENTRY **ppSeg, int *pSegNr )
893 NE_MODULE *pModule = NULL;
894 SEGTABLEENTRY *pSeg = NULL;
897 /* Try pair of module handle / segment number */
898 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
899 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
901 segNr = LOWORD( proc );
902 if ( segNr && segNr <= pModule->seg_count )
903 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
906 /* Try thunk or function */
909 BYTE *thunk = MapSL( (SEGPTR)proc );
912 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
913 selector = thunk[6] + (thunk[7] << 8);
915 selector = HIWORD( proc );
917 pModule = NE_GetPtr( GlobalHandle16( selector ) );
918 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
921 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
922 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
925 if ( pModule && segNr > pModule->seg_count )
929 /* Abort if segment not found */
931 if ( !pModule || !pSeg )
934 /* Return segment data */
936 if ( ppModule ) *ppModule = pModule;
937 if ( ppSeg ) *ppSeg = pSeg;
938 if ( pSegNr ) *pSegNr = segNr;
943 /**********************************************************************
944 * GetCodeHandle (KERNEL.93)
946 HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
950 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
956 /**********************************************************************
957 * GetCodeInfo (KERNEL.104)
959 BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
965 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
968 /* Fill in segment information */
970 segInfo->offSegment = pSeg->filepos;
971 segInfo->cbSegment = pSeg->size;
972 segInfo->flags = pSeg->flags;
973 segInfo->cbAlloc = pSeg->minsize;
974 segInfo->h = pSeg->hSeg;
975 segInfo->alignShift = pModule->alignment;
977 if ( segNr == pModule->dgroup )
978 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
980 /* Return module handle in %es */
982 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
988 /**********************************************************************
989 * DefineHandleTable (KERNEL.94)
991 BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
993 FIXME("(%04x): stub ?\n", wOffset);
998 /***********************************************************************
999 * SetTaskQueue (KERNEL.34)
1001 HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
1006 if (!hTask) hTask = GetCurrentTask();
1007 if (!(pTask = TASK_GetPtr( hTask ))) return 0;
1009 hPrev = pTask->hQueue;
1010 pTask->hQueue = hQueue;
1016 /***********************************************************************
1017 * GetTaskQueue (KERNEL.35)
1019 HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
1023 if (!hTask) hTask = GetCurrentTask();
1024 if (!(pTask = TASK_GetPtr( hTask ))) return 0;
1025 return pTask->hQueue;
1028 /***********************************************************************
1029 * SetThreadQueue (KERNEL.463)
1031 HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
1033 HQUEUE16 oldQueue = NtCurrentTeb()->queue;
1035 if (thread && thread != GetCurrentThreadId())
1037 FIXME( "not supported on other thread %04lx\n", thread );
1040 NtCurrentTeb()->queue = hQueue;
1041 if ( GetTaskQueue16( NtCurrentTeb()->htask16 ) == oldQueue )
1042 SetTaskQueue16( NtCurrentTeb()->htask16, hQueue );
1046 /***********************************************************************
1047 * GetThreadQueue (KERNEL.464)
1049 HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
1051 if (thread && thread != GetCurrentThreadId())
1053 FIXME( "not supported on other thread %04lx\n", thread );
1056 return NtCurrentTeb()->queue;
1059 /***********************************************************************
1060 * SetFastQueue (KERNEL.624)
1062 VOID WINAPI SetFastQueue16( DWORD thread, HQUEUE16 hQueue )
1064 if (!thread || thread == GetCurrentThreadId())
1065 NtCurrentTeb()->queue = hQueue;
1067 FIXME( "not supported on other thread %04lx\n", thread );
1070 /***********************************************************************
1071 * GetFastQueue (KERNEL.625)
1073 HQUEUE16 WINAPI GetFastQueue16( void )
1075 HQUEUE16 ret = NtCurrentTeb()->queue;
1077 if (!ret) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1081 /***********************************************************************
1082 * SwitchStackTo (KERNEL.108)
1084 void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
1086 STACK16FRAME *oldFrame, *newFrame;
1087 INSTANCEDATA *pData;
1090 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
1091 TRACE("old=%04x:%04x new=%04x:%04x\n",
1092 SELECTOROF( NtCurrentTeb()->cur_stack ),
1093 OFFSETOF( NtCurrentTeb()->cur_stack ), seg, ptr );
1095 /* Save the old stack */
1097 oldFrame = CURRENT_STACK16;
1098 /* pop frame + args and push bp */
1099 pData->old_ss_sp = NtCurrentTeb()->cur_stack + sizeof(STACK16FRAME)
1101 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
1102 pData->stacktop = top;
1103 pData->stackmin = ptr;
1104 pData->stackbottom = ptr;
1106 /* Switch to the new stack */
1108 /* Note: we need to take the 3 arguments into account; otherwise,
1109 * the stack will underflow upon return from this function.
1111 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1112 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
1113 NtCurrentTeb()->cur_stack = MAKESEGPTR( seg, ptr - copySize );
1114 newFrame = CURRENT_STACK16;
1116 /* Copy the stack frame and the local variables to the new stack */
1118 memmove( newFrame, oldFrame, copySize );
1120 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
1124 /***********************************************************************
1125 * SwitchStackBack (KERNEL.109)
1127 void WINAPI SwitchStackBack16( CONTEXT86 *context )
1129 STACK16FRAME *oldFrame, *newFrame;
1130 INSTANCEDATA *pData;
1132 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
1134 if (!pData->old_ss_sp)
1136 WARN("No previous SwitchStackTo\n" );
1139 TRACE("restoring stack %04x:%04x\n",
1140 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
1142 oldFrame = CURRENT_STACK16;
1144 /* Pop bp from the previous stack */
1146 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(pData->old_ss_sp);
1147 pData->old_ss_sp += sizeof(WORD);
1149 /* Switch back to the old stack */
1151 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
1152 context->SegSs = SELECTOROF(pData->old_ss_sp);
1153 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
1154 pData->old_ss_sp = 0;
1156 /* Build a stack frame for the return */
1158 newFrame = CURRENT_STACK16;
1159 newFrame->frame32 = oldFrame->frame32;
1160 newFrame->module_cs = oldFrame->module_cs;
1161 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1162 newFrame->entry_ip = oldFrame->entry_ip;
1166 /***********************************************************************
1167 * GetTaskQueueDS (KERNEL.118)
1169 void WINAPI GetTaskQueueDS16(void)
1171 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
1175 /***********************************************************************
1176 * GetTaskQueueES (KERNEL.119)
1178 void WINAPI GetTaskQueueES16(void)
1180 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
1184 /***********************************************************************
1185 * GetCurrentTask (KERNEL32.@)
1187 HTASK16 WINAPI GetCurrentTask(void)
1189 return NtCurrentTeb()->htask16;
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 = (char *)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 * SetErrorMode (KERNEL.107)
1254 UINT16 WINAPI SetErrorMode16( UINT16 mode )
1257 if (!(pTask = TASK_GetCurrent())) return 0;
1258 pTask->error_mode = mode;
1259 return SetErrorMode( mode );
1263 /***********************************************************************
1264 * GetNumTasks (KERNEL.152)
1266 UINT16 WINAPI GetNumTasks16(void)
1272 /***********************************************************************
1273 * GetTaskDS (KERNEL.155)
1275 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1276 * I don't think we need to bother with this.
1278 HINSTANCE16 WINAPI GetTaskDS16(void)
1282 if (!(pTask = TASK_GetCurrent())) return 0;
1283 return GlobalHandleToSel16(pTask->hInstance);
1286 /***********************************************************************
1287 * GetDummyModuleHandleDS (KERNEL.602)
1289 WORD WINAPI GetDummyModuleHandleDS16(void)
1294 if (!(pTask = TASK_GetCurrent())) return 0;
1295 if (!(pTask->flags & TDBF_WIN32)) return 0;
1296 selector = GlobalHandleToSel16( pTask->hModule );
1297 CURRENT_DS = selector;
1301 /***********************************************************************
1302 * IsTask (KERNEL.320)
1304 BOOL16 WINAPI IsTask16( HTASK16 hTask )
1308 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
1309 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
1310 return (pTask->magic == TDB_MAGIC);
1314 /***********************************************************************
1315 * IsWinOldApTask (KERNEL.158)
1317 BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1319 /* should return bit 0 of byte 0x48 in PSP */
1323 /***********************************************************************
1324 * SetTaskSignalProc (KERNEL.38)
1326 FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
1331 if (!hTask) hTask = GetCurrentTask();
1332 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
1333 oldProc = pTask->userhandler;
1334 pTask->userhandler = proc;
1338 /***********************************************************************
1339 * SetSigHandler (KERNEL.140)
1341 WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
1342 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
1344 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
1345 newhandler,oldhandler,oldmode,newmode,flag );
1347 if (flag != 1) return 0;
1348 if (!newmode) newhandler = NULL; /* Default handler */
1353 if (!(pTask = TASK_GetCurrent())) return 0;
1354 if (oldmode) *oldmode = pTask->signal_flags;
1355 pTask->signal_flags = newmode;
1356 if (oldhandler) *oldhandler = pTask->sighandler;
1357 pTask->sighandler = newhandler;
1363 /***********************************************************************
1364 * GlobalNotify (KERNEL.154)
1366 * Note that GlobalNotify does _not_ return the old NotifyProc
1367 * -- contrary to LocalNotify !!
1369 VOID WINAPI GlobalNotify16( FARPROC16 proc )
1373 if (!(pTask = TASK_GetCurrent())) return;
1374 pTask->discardhandler = proc;
1378 /***********************************************************************
1381 static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
1386 /* Check for module handle */
1388 if (!(ptr = GlobalLock16( handle ))) return 0;
1389 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
1391 /* Search for this handle inside all tasks */
1393 *hTask = hFirstTask;
1396 TDB *pTask = TASK_GetPtr( *hTask );
1397 if ((*hTask == handle) ||
1398 (pTask->hInstance == handle) ||
1399 (pTask->hQueue == handle) ||
1400 (pTask->hPDB == handle)) return pTask->hModule;
1401 *hTask = pTask->hNext;
1404 /* Check the owner for module handle */
1406 owner = FarGetOwner16( handle );
1407 if (!(ptr = GlobalLock16( owner ))) return 0;
1408 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
1410 /* Search for the owner inside all tasks */
1412 *hTask = hFirstTask;
1415 TDB *pTask = TASK_GetPtr( *hTask );
1416 if ((*hTask == owner) ||
1417 (pTask->hInstance == owner) ||
1418 (pTask->hQueue == owner) ||
1419 (pTask->hPDB == owner)) return pTask->hModule;
1420 *hTask = pTask->hNext;
1426 /***********************************************************************
1427 * GetExePtr (KERNEL.133)
1429 HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
1432 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
1433 STACK16FRAME *frame = CURRENT_STACK16;
1434 frame->ecx = hModule;
1435 if (hTask) frame->es = hTask;
1440 /***********************************************************************
1443 HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1446 return GetExePtrHelper( handle, &hTask );
1450 /***********************************************************************
1451 * TaskFirst (TOOLHELP.63)
1453 BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
1455 lpte->hNext = hFirstTask;
1456 return TaskNext16( lpte );
1460 /***********************************************************************
1461 * TaskNext (TOOLHELP.64)
1463 BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
1466 INSTANCEDATA *pInstData;
1468 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
1469 if (!lpte->hNext) return FALSE;
1471 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1473 pTask = TASK_GetPtr( lpte->hNext );
1474 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1475 if (pTask->hInstance)
1477 lpte->hNext = pTask->hNext;
1479 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
1480 lpte->hTask = lpte->hNext;
1481 lpte->hTaskParent = pTask->hParent;
1482 lpte->hInst = pTask->hInstance;
1483 lpte->hModule = pTask->hModule;
1484 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1485 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
1486 lpte->wStackTop = pInstData->stacktop;
1487 lpte->wStackMinimum = pInstData->stackmin;
1488 lpte->wStackBottom = pInstData->stackbottom;
1489 lpte->wcEvents = pTask->nEvents;
1490 lpte->hQueue = pTask->hQueue;
1491 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
1492 lpte->wPSPOffset = 0x100; /*??*/
1493 lpte->hNext = pTask->hNext;
1498 /***********************************************************************
1499 * TaskFindHandle (TOOLHELP.65)
1501 BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
1503 lpte->hNext = hTask;
1504 return TaskNext16( lpte );
1508 typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1510 /**************************************************************************
1511 * FatalAppExit (KERNEL.137)
1513 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1515 TDB *pTask = TASK_GetCurrent();
1517 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1519 HMODULE mod = GetModuleHandleA( "user32.dll" );
1522 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1525 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1529 ERR( "%s\n", debugstr_a(str) );
1536 /***********************************************************************
1537 * TerminateApp (TOOLHELP.77)
1539 * See "Undocumented Windows".
1541 void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1543 if (hTask && hTask != GetCurrentTask())
1545 FIXME("cannot terminate task %x\n", hTask);
1549 if (wFlags & NO_UAE_BOX)
1552 old_mode = SetErrorMode16(0);
1553 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1555 FatalAppExit16( 0, NULL );
1557 /* hmm, we're still alive ?? */
1559 /* check undocumented flag */
1560 if (!(wFlags & 0x8000))
1561 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1563 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1564 but let's just call ExitThread */
1569 /***********************************************************************
1570 * GetAppCompatFlags (KERNEL.354)
1572 DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
1576 if (!hTask) hTask = GetCurrentTask();
1577 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
1578 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1579 return pTask->compat_flags;
1583 /***********************************************************************
1584 * GetDOSEnvironment (KERNEL.131)
1586 * Note: the environment is allocated once, it doesn't track changes
1587 * made using the Win32 API. This shouldn't matter.
1589 * Format of a 16-bit environment block:
1590 * ASCIIZ string 1 (xx=yy format)
1595 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
1597 SEGPTR WINAPI GetDOSEnvironment16(void)
1599 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
1600 static HGLOBAL16 handle; /* handle to the 16 bit environment */
1607 p = env = GetEnvironmentStringsA();
1608 while (*p) p += strlen(p) + 1;
1609 p++; /* skip last null */
1610 size = (p - env) + sizeof(WORD) + sizeof(ENV_program_name);
1611 handle = GlobalAlloc16( GMEM_FIXED, size );
1615 LPSTR env16 = GlobalLock16( handle );
1616 memcpy( env16, env, p - env );
1617 memcpy( env16 + (p - env), &one, sizeof(one));
1618 memcpy( env16 + (p - env) + sizeof(WORD), ENV_program_name, sizeof(ENV_program_name));
1619 GlobalUnlock16( handle );
1621 FreeEnvironmentStringsA( env );
1623 return K32WOWGlobalLock16( handle );