blob: b6f704fb61c79cf064b092b57380530b89a86bb0 [file] [log] [blame]
Alexandre Julliard58199531994-04-21 01:20:00 +00001/*
Alexandre Julliard594997c1995-04-30 10:05:20 +00002 * Task functions
Alexandre Julliard234bc241994-12-10 13:02:28 +00003 *
Alexandre Julliard594997c1995-04-30 10:05:20 +00004 * Copyright 1995 Alexandre Julliard
5 */
6
Alexandre Julliard58199531994-04-21 01:20:00 +00007#include <stdlib.h>
8#include <string.h>
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00009#include <assert.h>
Ulrich Weigandafac2e41998-11-14 18:45:25 +000010#include <unistd.h>
Alexandre Julliardef702d81996-05-28 18:54:58 +000011
Marcus Meissner317af321999-02-17 13:51:06 +000012#include "wine/winbase16.h"
Alexandre Julliard77b99181997-09-14 17:17:23 +000013#include "user.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000014#include "callback.h"
Alexandre Julliard491502b1997-11-01 19:08:16 +000015#include "drive.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000016#include "file.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000017#include "global.h"
18#include "instance.h"
Alexandre Julliardef702d81996-05-28 18:54:58 +000019#include "message.h"
Alexandre Julliarda2f2e011995-06-06 16:40:35 +000020#include "miscemu.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000021#include "module.h"
22#include "neexe.h"
Alexandre Julliard3051b641996-07-05 17:14:13 +000023#include "pe_image.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000024#include "process.h"
Alexandre Julliardb817f4f1996-03-14 18:08:34 +000025#include "queue.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000026#include "selectors.h"
Alexandre Julliardf1aa3031996-08-05 17:42:43 +000027#include "stackframe.h"
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000028#include "task.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000029#include "thread.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000030#include "toolhelp.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000031#include "winnt.h"
Alexandre Julliarda845b881998-06-01 10:44:35 +000032#include "winsock.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000033#include "syslevel.h"
Alexandre Julliarde8c6a081999-05-02 14:33:41 +000034#include "debugtools.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000035#include "dosexe.h"
Ulrich Weigand70b2e381999-05-04 16:43:38 +000036#include "services.h"
Alexandre Julliard642d3131998-07-12 19:29:36 +000037#include "server.h"
Alexandre Julliardaca05781994-10-17 18:12:41 +000038
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000039DEFAULT_DEBUG_CHANNEL(task)
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000040DECLARE_DEBUG_CHANNEL(relay)
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000041DECLARE_DEBUG_CHANNEL(toolhelp)
42
Alexandre Julliard2787be81995-05-22 18:23:01 +000043 /* Min. number of thunks allocated when creating a new segment */
44#define MIN_THUNKS 32
Alexandre Julliard58199531994-04-21 01:20:00 +000045
Alexandre Julliard670cdc41997-08-24 16:00:30 +000046 /* Pointer to function to switch to a larger stack */
47int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
Alexandre Julliard18f92e71996-07-17 20:02:21 +000048
Alexandre Julliard642d3131998-07-12 19:29:36 +000049static THHOOK DefaultThhook = { 0 };
50THHOOK *pThhook = &DefaultThhook;
51
52#define hCurrentTask (pThhook->CurTDB)
53#define hFirstTask (pThhook->HeadTDB)
54#define hLockedTask (pThhook->LockTDB)
55
Alexandre Julliardbf9130a1996-10-13 17:45:47 +000056static UINT16 nTaskCount = 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +000057
Alexandre Julliard67a24c82000-02-18 21:54:32 +000058static HTASK initial_task;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000059
Alexandre Julliard642d3131998-07-12 19:29:36 +000060/***********************************************************************
61 * TASK_InstallTHHook
62 */
63void TASK_InstallTHHook( THHOOK *pNewThhook )
64{
65 THHOOK *pOldThhook = pThhook;
66
67 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
68
69 *pThhook = *pOldThhook;
70}
Alexandre Julliard808cb041995-08-17 17:11:36 +000071
72/***********************************************************************
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000073 * TASK_GetNextTask
74 */
Alexandre Julliardbf9130a1996-10-13 17:45:47 +000075HTASK16 TASK_GetNextTask( HTASK16 hTask )
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000076{
Alexandre Julliard1285c2f1996-05-06 16:06:24 +000077 TDB* pTask = (TDB*)GlobalLock16(hTask);
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000078
79 if (pTask->hNext) return pTask->hNext;
80 return (hFirstTask != hTask) ? hFirstTask : 0;
81}
82
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000083/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +000084 * TASK_LinkTask
Alexandre Julliard58199531994-04-21 01:20:00 +000085 */
Alexandre Julliard18f92e71996-07-17 20:02:21 +000086static void TASK_LinkTask( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +000087{
Alexandre Julliard18f92e71996-07-17 20:02:21 +000088 HTASK16 *prevTask;
Alexandre Julliard594997c1995-04-30 10:05:20 +000089 TDB *pTask;
90
Alexandre Julliard1285c2f1996-05-06 16:06:24 +000091 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
Alexandre Julliard594997c1995-04-30 10:05:20 +000092 prevTask = &hFirstTask;
93 while (*prevTask)
94 {
Alexandre Julliard1285c2f1996-05-06 16:06:24 +000095 TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +000096 if (prevTaskPtr->priority >= pTask->priority) break;
97 prevTask = &prevTaskPtr->hNext;
98 }
99 pTask->hNext = *prevTask;
100 *prevTask = hTask;
101 nTaskCount++;
Alexandre Julliard58199531994-04-21 01:20:00 +0000102}
103
104
Alexandre Julliard594997c1995-04-30 10:05:20 +0000105/***********************************************************************
106 * TASK_UnlinkTask
Alexandre Julliard58199531994-04-21 01:20:00 +0000107 */
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000108static void TASK_UnlinkTask( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +0000109{
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000110 HTASK16 *prevTask;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000111 TDB *pTask;
112
113 prevTask = &hFirstTask;
114 while (*prevTask && (*prevTask != hTask))
115 {
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000116 pTask = (TDB *)GlobalLock16( *prevTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000117 prevTask = &pTask->hNext;
118 }
119 if (*prevTask)
120 {
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000121 pTask = (TDB *)GlobalLock16( *prevTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000122 *prevTask = pTask->hNext;
123 pTask->hNext = 0;
124 nTaskCount--;
125 }
Alexandre Julliard58199531994-04-21 01:20:00 +0000126}
127
128
Alexandre Julliard594997c1995-04-30 10:05:20 +0000129/***********************************************************************
130 * TASK_CreateThunks
131 *
132 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
133 * and containing 'count' entries.
Alexandre Julliard58199531994-04-21 01:20:00 +0000134 */
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000135static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
Alexandre Julliard58199531994-04-21 01:20:00 +0000136{
Alexandre Julliard594997c1995-04-30 10:05:20 +0000137 int i;
138 WORD free;
139 THUNKS *pThunk;
140
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000141 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
Alexandre Julliard594997c1995-04-30 10:05:20 +0000142 pThunk->next = 0;
143 pThunk->magic = THUNK_MAGIC;
144 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
145 free = pThunk->free;
146 for (i = 0; i < count-1; i++)
147 {
148 free += 8; /* Offset of next thunk */
149 pThunk->thunks[4*i] = free;
150 }
151 pThunk->thunks[4*i] = 0; /* Last thunk */
Alexandre Julliard58199531994-04-21 01:20:00 +0000152}
153
154
Alexandre Julliard594997c1995-04-30 10:05:20 +0000155/***********************************************************************
156 * TASK_AllocThunk
157 *
158 * Allocate a thunk for MakeProcInstance().
Alexandre Julliard58199531994-04-21 01:20:00 +0000159 */
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000160static SEGPTR TASK_AllocThunk( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +0000161{
Alexandre Julliard594997c1995-04-30 10:05:20 +0000162 TDB *pTask;
163 THUNKS *pThunk;
164 WORD sel, base;
165
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000166 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000167 sel = pTask->hCSAlias;
168 pThunk = &pTask->thunks;
169 base = (int)pThunk - (int)pTask;
170 while (!pThunk->free)
171 {
172 sel = pThunk->next;
173 if (!sel) /* Allocate a new segment */
174 {
175 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
176 pTask->hPDB, TRUE, FALSE, FALSE );
177 if (!sel) return (SEGPTR)0;
178 TASK_CreateThunks( sel, 0, MIN_THUNKS );
179 pThunk->next = sel;
180 }
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000181 pThunk = (THUNKS *)GlobalLock16( sel );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000182 base = 0;
183 }
184 base += pThunk->free;
185 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000186 return PTR_SEG_OFF_TO_SEGPTR( sel, base );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000187}
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +0000188
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +0000189
Alexandre Julliard594997c1995-04-30 10:05:20 +0000190/***********************************************************************
191 * TASK_FreeThunk
192 *
193 * Free a MakeProcInstance() thunk.
194 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000195static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000196{
197 TDB *pTask;
198 THUNKS *pThunk;
199 WORD sel, base;
200
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000201 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000202 sel = pTask->hCSAlias;
203 pThunk = &pTask->thunks;
204 base = (int)pThunk - (int)pTask;
205 while (sel && (sel != HIWORD(thunk)))
206 {
207 sel = pThunk->next;
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000208 pThunk = (THUNKS *)GlobalLock16( sel );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000209 base = 0;
210 }
211 if (!sel) return FALSE;
212 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
213 pThunk->free = LOWORD(thunk) - base;
214 return TRUE;
215}
216
217
218/***********************************************************************
219 * TASK_CallToStart
220 *
221 * 32-bit entry point for a new task. This function is responsible for
222 * setting up the registers and jumping to the 16-bit entry point.
223 */
Ulrich Weiganded490031999-06-06 14:47:50 +0000224void TASK_CallToStart(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000225{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000226 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000227 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000228 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
Alexandre Julliard617955d1999-06-26 18:40:24 +0000229 CONTEXT86 context;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000230
Ulrich Weigandfa5647a2000-01-17 22:21:29 +0000231 SYSLEVEL_EnterWin16Lock();
232
233 /* Add task to 16-bit scheduler pool if necessary */
234 if ( hCurrentTask != GetCurrentTask() )
235 TASK_Reschedule();
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000236
Ulrich Weiganded490031999-06-06 14:47:50 +0000237 /* Registers at initialization must be:
238 * ax zero
239 * bx stack size in bytes
240 * cx heap size in bytes
241 * si previous app instance
242 * di current app instance
243 * bp zero
244 * es selector to the PSP
245 * ds dgroup of the application
246 * ss stack selector
247 * sp top of the stack
248 */
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000249
Ulrich Weiganded490031999-06-06 14:47:50 +0000250 memset( &context, 0, sizeof(context) );
251 CS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg);
Ulrich Weigand3c717e41999-07-03 15:36:45 +0000252 DS_reg(&context) = GlobalHandleToSel16(pTask->hInstance);
Ulrich Weiganded490031999-06-06 14:47:50 +0000253 ES_reg(&context) = pTask->hPDB;
254 EIP_reg(&context) = pModule->ip;
255 EBX_reg(&context) = pModule->stack_size;
256 ECX_reg(&context) = pModule->heap_size;
Ulrich Weigand3c717e41999-07-03 15:36:45 +0000257 EDI_reg(&context) = pTask->hInstance;
258 ESI_reg(&context) = pTask->hPrevInstance;
Ulrich Weigand1c971e31999-05-08 09:58:47 +0000259
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000260 TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n",
261 CS_reg(&context), EIP_reg(&context), DS_reg(&context),
262 SELECTOROF(pTask->teb->cur_stack),
263 OFFSETOF(pTask->teb->cur_stack) );
Ulrich Weigand8139c301999-04-01 11:43:05 +0000264
Ulrich Weiganded490031999-06-06 14:47:50 +0000265 Callbacks->CallRegisterShortProc( &context, 0 );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000266}
267
268
269/***********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000270 * TASK_Create
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000271 *
Ulrich Weiganded490031999-06-06 14:47:50 +0000272 * NOTE: This routine might be called by a Win32 thread. Thus, we need
273 * to be careful to protect global data structures. We do this
274 * by entering the Win16Lock while linking the task into the
275 * global task list.
Alexandre Julliard594997c1995-04-30 10:05:20 +0000276 */
Ulrich Weigandfbea5301999-07-03 12:26:29 +0000277BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000278{
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000279 HTASK16 hTask;
Ulrich Weigand8139c301999-04-01 11:43:05 +0000280 TDB *pTask;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000281 LPSTR cmd_line;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000282 char name[10];
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000283 PDB *pdb32 = PROCESS_Current();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000284
285 /* Allocate the task structure */
286
287 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000288 pModule->self, FALSE, FALSE, FALSE );
Ulrich Weigand6e0d3861999-02-28 11:14:32 +0000289 if (!hTask) return FALSE;
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000290 pTask = (TDB *)GlobalLock16( hTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000291
Alexandre Julliard491502b1997-11-01 19:08:16 +0000292 /* Fill the task structure */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000293
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000294 pTask->nEvents = 0;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000295 pTask->hSelf = hTask;
296 pTask->flags = 0;
Alexandre Julliardb1bac321996-12-15 19:45:59 +0000297
298 if (pModule->flags & NE_FFLAGS_WIN32)
299 pTask->flags |= TDBF_WIN32;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000300 if (pModule->lpDosTask)
301 pTask->flags |= TDBF_WINOLDAP;
Alexandre Julliardb1bac321996-12-15 19:45:59 +0000302
Ulrich Weigandfbea5301999-07-03 12:26:29 +0000303 pTask->hInstance = pModule->self;
304 pTask->hPrevInstance = 0;
305 /* NOTE: for 16-bit tasks, the instance handles are updated later on
306 in NE_InitProcess */
307
Alexandre Julliard2787be81995-05-22 18:23:01 +0000308 pTask->version = pModule->expected_version;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000309 pTask->hModule = pModule->self;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000310 pTask->hParent = GetCurrentTask();
Alexandre Julliard2787be81995-05-22 18:23:01 +0000311 pTask->magic = TDB_MAGIC;
312 pTask->nCmdShow = cmdShow;
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000313 pTask->teb = NtCurrentTeb();
Alexandre Julliard491502b1997-11-01 19:08:16 +0000314 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
315 strcpy( pTask->curdir, "\\" );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000316 lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ),
Alexandre Julliard491502b1997-11-01 19:08:16 +0000317 sizeof(pTask->curdir) - 1 );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000318
319 /* Create the thunks block */
320
321 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
322
323 /* Copy the module name */
324
Alexandre Julliarda3960291999-02-26 11:11:13 +0000325 GetModuleName16( pModule->self, name, sizeof(name) );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000326 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
327
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000328 /* Allocate a selector for the PDB */
329
Alexandre Julliarda3960291999-02-26 11:11:13 +0000330 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000331 pModule->self, FALSE, FALSE, FALSE, NULL );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000332
Alexandre Julliard594997c1995-04-30 10:05:20 +0000333 /* Fill the PDB */
334
335 pTask->pdb.int20 = 0x20cd;
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000336 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000337 PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint(
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000338 GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000339 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
340 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
341 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000342 pTask->pdb.fileHandlesPtr =
Alexandre Julliarda3960291999-02-26 11:11:13 +0000343 PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask->hPDB),
344 (int)&((PDB16 *)0)->fileHandles );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000345 pTask->pdb.hFileHandles = 0;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000346 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000347 pTask->pdb.environment = pdb32->env_db->env_sel;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000348 pTask->pdb.nbFiles = 20;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000349
350 /* Fill the command line */
351
352 cmd_line = pdb32->env_db->cmd_line;
353 while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +0000354 while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000355 lstrcpynA( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000356 pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000357
358 /* Get the compatibility flags */
359
Alexandre Julliarda3960291999-02-26 11:11:13 +0000360 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000361
Alexandre Julliard594997c1995-04-30 10:05:20 +0000362 /* Allocate a code segment alias for the TDB */
363
364 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
365 sizeof(TDB), pTask->hPDB, TRUE,
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000366 FALSE, FALSE, NULL );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000367
368 /* Set the owner of the environment block */
369
Alexandre Julliarda3960291999-02-26 11:11:13 +0000370 FarSetOwner16( pTask->pdb.environment, pTask->hPDB );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000371
372 /* Default DTA overwrites command-line */
373
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000374 pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB,
375 (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000376
Ulrich Weiganded490031999-06-06 14:47:50 +0000377 /* Create scheduler event for 16-bit tasks */
378
379 if ( !(pTask->flags & TDBF_WIN32) )
380 {
381 pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL );
382 pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent );
383 }
384
Alexandre Julliard1c4c4091999-05-02 19:10:04 +0000385 /* Enter task handle into thread and process */
386
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000387 pTask->teb->htask16 = pTask->teb->process->task = hTask;
Alexandre Julliard67a24c82000-02-18 21:54:32 +0000388 if (!initial_task) initial_task = hTask;
389
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000390 TRACE("module='%s' cmdline='%s' task=%04x\n", name, cmd_line, hTask );
Alexandre Julliard1c4c4091999-05-02 19:10:04 +0000391
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000392 /* Add the task to the linked list */
393
394 SYSLEVEL_EnterWin16Lock();
395 TASK_LinkTask( hTask );
396 SYSLEVEL_LeaveWin16Lock();
397
Ulrich Weiganded490031999-06-06 14:47:50 +0000398 return TRUE;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000399}
400
Alexandre Julliard594997c1995-04-30 10:05:20 +0000401/***********************************************************************
402 * TASK_DeleteTask
Alexandre Julliard58199531994-04-21 01:20:00 +0000403 */
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000404static void TASK_DeleteTask( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +0000405{
Alexandre Julliard594997c1995-04-30 10:05:20 +0000406 TDB *pTask;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000407 HGLOBAL16 hPDB;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000408
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000409 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000410 hPDB = pTask->hPDB;
411
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000412 pTask->magic = 0xdead; /* invalidate signature */
413
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000414 /* Delete the Win32 part of the task */
415
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000416/* PROCESS_FreePDB( pTask->teb->process ); FIXME */
417/* K32OBJ_DecCount( &pTask->teb->header ); FIXME */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000418
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000419 /* Free the selector aliases */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000420
421 GLOBAL_FreeBlock( pTask->hCSAlias );
422 GLOBAL_FreeBlock( pTask->hPDB );
423
Ulrich Weigandafac2e41998-11-14 18:45:25 +0000424 /* Free the task module */
425
426 FreeModule16( pTask->hModule );
427
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000428 /* Free the task structure itself */
Alexandre Julliard2787be81995-05-22 18:23:01 +0000429
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000430 GlobalFree16( hTask );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000431
432 /* Free all memory used by this task (including the 32-bit stack, */
433 /* the environment block and the thunk segments). */
434
Alexandre Julliarda3960291999-02-26 11:11:13 +0000435 GlobalFreeAll16( hPDB );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000436}
437
Alexandre Julliard594997c1995-04-30 10:05:20 +0000438/***********************************************************************
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000439 * TASK_KillTask
Alexandre Julliard594997c1995-04-30 10:05:20 +0000440 */
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000441void TASK_KillTask( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000442{
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000443 TDB *pTask;
Alexandre Julliard2ace16a1996-04-28 15:09:19 +0000444
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000445 /* Enter the Win16Lock to protect global data structures */
446 SYSLEVEL_EnterWin16Lock();
447
448 if ( !hTask ) hTask = GetCurrentTask();
449 pTask = (TDB *)GlobalLock16( hTask );
450 if ( !pTask )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000451 {
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000452 SYSLEVEL_LeaveWin16Lock();
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000453 return;
454 }
455
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000456 TRACE("Killing task %04x\n", hTask );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000457
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000458#ifdef MZ_SUPPORTED
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000459{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000460 /* Kill DOS VM task */
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000461 NE_MODULE *pModule = NE_GetPtr( pTask->hModule );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000462 if ( pModule->lpDosTask )
463 MZ_KillModule( pModule->lpDosTask );
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000464}
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000465#endif
466
Alexandre Julliard2ace16a1996-04-28 15:09:19 +0000467 /* Perform USER cleanup */
468
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +0000469 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000470 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
471 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
472 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
Ulrich Weigand8139c301999-04-01 11:43:05 +0000473
Ulrich Weigandafac2e41998-11-14 18:45:25 +0000474 if (nTaskCount <= 1)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000475 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000476 TRACE("this is the last task, exiting\n" );
Ulrich Weigandc319c661999-05-22 16:18:36 +0000477 ExitKernel16();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000478 }
479
Ulrich Weigandafac2e41998-11-14 18:45:25 +0000480 /* FIXME: Hack! Send a message to the initial task so that
481 * the GetMessage wakes up and the initial task can check whether
482 * it is the only remaining one and terminate itself ...
483 * The initial task should probably install hooks or something
484 * to get informed about task termination :-/
485 */
Alexandre Julliard67a24c82000-02-18 21:54:32 +0000486 Callout.PostAppMessage16( initial_task, WM_NULL, 0, 0 );
Ulrich Weigandafac2e41998-11-14 18:45:25 +0000487
Alexandre Julliard594997c1995-04-30 10:05:20 +0000488 /* Remove the task from the list to be sure we never switch back to it */
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000489 TASK_UnlinkTask( hTask );
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000490 if( nTaskCount )
491 {
492 TDB* p = (TDB *)GlobalLock16( hFirstTask );
493 while( p )
494 {
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000495 if( p->hYieldTo == hTask ) p->hYieldTo = 0;
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000496 p = (TDB *)GlobalLock16( p->hNext );
497 }
498 }
499
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000500 pTask->nEvents = 0;
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000501
502 if ( hLockedTask == hTask )
503 hLockedTask = 0;
504
Ulrich Weiganded490031999-06-06 14:47:50 +0000505 TASK_DeleteTask( hTask );
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000506
Ulrich Weiganded490031999-06-06 14:47:50 +0000507 /* When deleting the current task ... */
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000508 if ( hTask == hCurrentTask )
509 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000510 DWORD lockCount;
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000511
Ulrich Weiganded490031999-06-06 14:47:50 +0000512 /* ... schedule another one ... */
513 TASK_Reschedule();
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000514
Ulrich Weiganded490031999-06-06 14:47:50 +0000515 /* ... and completely release the Win16Lock, just in case. */
516 ReleaseThunkLock( &lockCount );
517
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000518 return;
519 }
520
Ulrich Weiganded490031999-06-06 14:47:50 +0000521 SYSLEVEL_LeaveWin16Lock();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000522}
523
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000524/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +0000525 * TASK_Reschedule
526 *
527 * This is where all the magic of task-switching happens!
528 *
Ulrich Weiganded490031999-06-06 14:47:50 +0000529 * 16-bit Windows performs non-preemptive (cooperative) multitasking.
530 * This means that each 16-bit task runs until it voluntarily yields
531 * control, at which point the scheduler gets active and selects the
532 * next task to run.
533 *
534 * In Wine, all processes, even 16-bit ones, are scheduled preemptively
535 * by the standard scheduler of the underlying OS. As many 16-bit apps
536 * *rely* on the behaviour of the Windows scheduler, however, we have
537 * to simulate that behaviour.
538 *
539 * This is achieved as follows: every 16-bit task is at time (except
540 * during task creation and deletion) in one of two states: either it
541 * is the one currently running, then the global variable hCurrentTask
542 * contains its task handle, or it is not currently running, then it
543 * is blocked on a special scheduler event, a global handle to which
544 * is stored in the task struct.
545 *
546 * When the current task yields control, this routine gets called. Its
547 * purpose is to determine the next task to be active, signal the
548 * scheduler event of that task, and then put the current task to sleep
549 * waiting for *its* scheduler event to get signalled again.
550 *
551 * This routine can get called in a few other special situations as well:
552 *
553 * - On creation of a 16-bit task, the Unix process executing the task
554 * calls TASK_Reschedule once it has completed its initialization.
555 * At this point, the task needs to be blocked until its scheduler
556 * event is signalled the first time (this will be done by the parent
557 * process to get the task up and running).
558 *
559 * - When the task currently running terminates itself, this routine gets
560 * called and has to schedule another task, *without* blocking the
561 * terminating task.
562 *
563 * - When a 32-bit thread posts an event for a 16-bit task, it might be
564 * the case that *no* 16-bit task is currently running. In this case
565 * the task that has now an event pending is to be scheduled.
566 *
Alexandre Julliard594997c1995-04-30 10:05:20 +0000567 */
Ulrich Weiganded490031999-06-06 14:47:50 +0000568void TASK_Reschedule(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000569{
Ulrich Weiganded490031999-06-06 14:47:50 +0000570 TDB *pOldTask = NULL, *pNewTask = NULL;
571 HTASK16 hOldTask = 0, hNewTask = 0;
572 enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode;
573 DWORD lockCount;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000574
Ulrich Weiganded490031999-06-06 14:47:50 +0000575 SYSLEVEL_EnterWin16Lock();
576
577 /* Check what we need to do */
578 hOldTask = GetCurrentTask();
579 pOldTask = (TDB *)GlobalLock16( hOldTask );
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000580 TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n",
581 hCurrentTask, hOldTask, (long) getpid() );
Ulrich Weiganded490031999-06-06 14:47:50 +0000582
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000583 if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) )
Ulrich Weigand89fc6fd1999-05-24 08:13:02 +0000584 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000585 /* We are called by an active (non-deleted) 16-bit task */
586
587 /* If we don't even have a current task, or else the current
588 task has yielded, we'll need to schedule a new task and
589 (possibly) put the calling task to sleep. Otherwise, we
590 only block the caller. */
591
592 if ( !hCurrentTask || hCurrentTask == hOldTask )
593 mode = MODE_YIELD;
594 else
595 mode = MODE_SLEEP;
Ulrich Weigand89fc6fd1999-05-24 08:13:02 +0000596 }
Ulrich Weiganded490031999-06-06 14:47:50 +0000597 else
Ulrich Weigandc75d0e11998-11-01 17:59:35 +0000598 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000599 /* We are called by a deleted 16-bit task or a 32-bit thread */
600
601 /* The only situation where we need to do something is if we
602 now do not have a current task. Then, we'll need to wake up
603 some task that has events pending. */
604
605 if ( !hCurrentTask || hCurrentTask == hOldTask )
606 mode = MODE_WAKEUP;
607 else
Ulrich Weigandc75d0e11998-11-01 17:59:35 +0000608 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000609 /* nothing to do */
610 SYSLEVEL_LeaveWin16Lock();
611 return;
Ulrich Weigandc75d0e11998-11-01 17:59:35 +0000612 }
Ulrich Weigandc75d0e11998-11-01 17:59:35 +0000613 }
614
Ulrich Weiganded490031999-06-06 14:47:50 +0000615 /* Find a task to yield to: check for DirectedYield() */
616 if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo )
Alexandre Julliardb7258be1995-09-01 15:57:28 +0000617 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000618 hNewTask = pOldTask->hYieldTo;
619 pNewTask = (TDB *)GlobalLock16( hNewTask );
620 if( !pNewTask || !pNewTask->nEvents) hNewTask = 0;
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000621 pOldTask->hYieldTo = 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000622 }
623
Ulrich Weiganded490031999-06-06 14:47:50 +0000624 /* Find a task to yield to: check for pending events */
625 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000626 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000627 hNewTask = hFirstTask;
628 while (hNewTask)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000629 {
Ulrich Weiganded490031999-06-06 14:47:50 +0000630 pNewTask = (TDB *)GlobalLock16( hNewTask );
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000631
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000632 TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents );
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000633
Alexandre Julliardef702d81996-05-28 18:54:58 +0000634 if (pNewTask->nEvents) break;
Ulrich Weiganded490031999-06-06 14:47:50 +0000635 hNewTask = pNewTask->hNext;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000636 }
Ulrich Weiganded490031999-06-06 14:47:50 +0000637 if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000638 }
639
Ulrich Weiganded490031999-06-06 14:47:50 +0000640 /* If we are still the task with highest priority, just return ... */
641 if ( mode == MODE_YIELD && hNewTask == hCurrentTask )
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000642 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000643 TRACE("returning to the current task (%04x)\n", hCurrentTask );
Ulrich Weiganded490031999-06-06 14:47:50 +0000644 SYSLEVEL_LeaveWin16Lock();
645
Ulrich Weigandd1e22391999-04-15 15:30:33 +0000646 /* Allow Win32 threads to thunk down even while a Win16 task is
647 in a tight PeekMessage() or Yield() loop ... */
Ulrich Weiganded490031999-06-06 14:47:50 +0000648 ReleaseThunkLock( &lockCount );
649 RestoreThunkLock( lockCount );
Ulrich Weigandbe672fe1998-12-24 14:59:53 +0000650 return;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000651 }
Ulrich Weigandbe672fe1998-12-24 14:59:53 +0000652
Ulrich Weiganded490031999-06-06 14:47:50 +0000653 /* If no task to yield to found, suspend 16-bit scheduler ... */
654 if ( mode == MODE_YIELD && !hNewTask )
655 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000656 TRACE("No currently active task\n");
Ulrich Weiganded490031999-06-06 14:47:50 +0000657 hCurrentTask = 0;
658 }
Ulrich Weigand8c2dc8b1999-05-22 18:58:36 +0000659
Ulrich Weiganded490031999-06-06 14:47:50 +0000660 /* If we found a task to wake up, do it ... */
661 if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask )
662 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000663 TRACE("Switching to task %04x (%.8s)\n",
Ulrich Weiganded490031999-06-06 14:47:50 +0000664 hNewTask, pNewTask->module_name );
665
666 pNewTask->priority++;
667 TASK_UnlinkTask( hNewTask );
668 TASK_LinkTask( hNewTask );
669 pNewTask->priority--;
670
671 hCurrentTask = hNewTask;
672 SetEvent( pNewTask->hEvent );
673
674 /* This is set just in case some app reads it ... */
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000675 pNewTask->ss_sp = pNewTask->teb->cur_stack;
Ulrich Weiganded490031999-06-06 14:47:50 +0000676 }
677
678 /* If we need to put the current task to sleep, do it ... */
679 if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask )
680 {
681 ResetEvent( pOldTask->hEvent );
682
683 ReleaseThunkLock( &lockCount );
684 SYSLEVEL_CheckNotLevel( 1 );
685 WaitForSingleObject( pOldTask->hEvent, INFINITE );
686 RestoreThunkLock( lockCount );
687 }
688
689 SYSLEVEL_LeaveWin16Lock();
Alexandre Julliard2787be81995-05-22 18:23:01 +0000690}
691
Alexandre Julliard2787be81995-05-22 18:23:01 +0000692/***********************************************************************
693 * InitTask (KERNEL.91)
Alexandre Julliard77b99181997-09-14 17:17:23 +0000694 *
695 * Called by the application startup code.
Alexandre Julliard2787be81995-05-22 18:23:01 +0000696 */
Alexandre Julliard617955d1999-06-26 18:40:24 +0000697void WINAPI InitTask16( CONTEXT86 *context )
Alexandre Julliard2787be81995-05-22 18:23:01 +0000698{
699 TDB *pTask;
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000700 INSTANCEDATA *pinstance;
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000701 SEGPTR ptr;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000702
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000703 EAX_reg(context) = 0;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000704 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000705
706 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
707 as some apps, notably Visual Basic apps, *modify* the heap/stack
708 size of the instance data segment before calling InitTask() */
709
710 /* Initialize the INSTANCEDATA structure */
711 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
712 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack );
713 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
714 pinstance->stacktop = ( pinstance->stackmin > BX_reg(context)?
715 pinstance->stackmin - BX_reg(context) : 0 ) + 150;
716
717 /* Initialize the local heap */
718 if ( CX_reg(context) )
Bernd Herd4d057612000-05-11 00:10:36 +0000719 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, CX_reg(context) );
Alexandre Julliard2787be81995-05-22 18:23:01 +0000720
Alexandre Julliard77b99181997-09-14 17:17:23 +0000721 /* Initialize implicitly loaded DLLs */
Alexandre Julliard2787be81995-05-22 18:23:01 +0000722 NE_InitializeDLLs( pTask->hModule );
Ulrich Weigand979000e1999-12-05 02:48:53 +0000723 NE_DllProcessAttach( pTask->hModule );
Alexandre Julliard2787be81995-05-22 18:23:01 +0000724
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000725 /* Registers on return are:
726 * ax 1 if OK, 0 on error
727 * cx stack limit in bytes
728 * dx cmdShow parameter
729 * si instance handle of the previous instance
730 * di instance handle of the new task
731 * es:bx pointer to command-line inside PSP
732 *
733 * 0 (=%bp) is pushed on the stack
734 */
Alexandre Julliarda1089c51999-08-01 12:51:55 +0000735 ptr = stack16_push( sizeof(WORD) );
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000736 *(WORD *)PTR_SEG_TO_LIN(ptr) = 0;
Alexandre Julliarda1089c51999-08-01 12:51:55 +0000737 ESP_reg(context) -= 2;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000738
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000739 EAX_reg(context) = 1;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000740
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000741 if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80;
742 else
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000743 {
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000744 LPBYTE p = &pTask->pdb.cmdLine[1];
745 while ((*p == ' ') || (*p == '\t')) p++;
746 EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine);
747 }
748 ECX_reg(context) = pinstance->stacktop;
749 EDX_reg(context) = pTask->nCmdShow;
750 ESI_reg(context) = (DWORD)pTask->hPrevInstance;
751 EDI_reg(context) = (DWORD)pTask->hInstance;
752 ES_reg (context) = (WORD)pTask->hPDB;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000753}
754
755
756/***********************************************************************
757 * WaitEvent (KERNEL.30)
758 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000759BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000760{
761 TDB *pTask;
762
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000763 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000764 pTask = (TDB *)GlobalLock16( hTask );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000765
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000766 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000767 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000768 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000769 return TRUE;
770 }
771
Alexandre Julliard594997c1995-04-30 10:05:20 +0000772 if (pTask->nEvents > 0)
773 {
774 pTask->nEvents--;
775 return FALSE;
776 }
Ulrich Weiganded490031999-06-06 14:47:50 +0000777 TASK_Reschedule();
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000778
Alexandre Julliardef702d81996-05-28 18:54:58 +0000779 /* When we get back here, we have an event */
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000780
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000781 if (pTask->nEvents > 0) pTask->nEvents--;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000782 return TRUE;
783}
784
785
786/***********************************************************************
787 * PostEvent (KERNEL.31)
788 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000789void WINAPI PostEvent16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000790{
791 TDB *pTask;
792
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000793 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000794 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000795
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000796 if ( !THREAD_IsWin16( pTask->teb ) )
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000797 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000798 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000799 return;
800 }
801
Stephane Lussierb50272b1999-02-21 18:15:26 +0000802 pTask->nEvents++;
Ulrich Weiganded490031999-06-06 14:47:50 +0000803
804 /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000805 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
Ulrich Weiganded490031999-06-06 14:47:50 +0000806 TASK_Reschedule();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000807}
808
809
810/***********************************************************************
811 * SetPriority (KERNEL.32)
812 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000813void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000814{
815 TDB *pTask;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000816 INT16 newpriority;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000817
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000818 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000819 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000820 newpriority = pTask->priority + delta;
821 if (newpriority < -32) newpriority = -32;
822 else if (newpriority > 15) newpriority = 15;
823
824 pTask->priority = newpriority + 1;
825 TASK_UnlinkTask( hTask );
826 TASK_LinkTask( hTask );
827 pTask->priority--;
828}
829
830
831/***********************************************************************
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000832 * LockCurrentTask (KERNEL.33)
833 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000834HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000835{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000836 if (bLock) hLockedTask = GetCurrentTask();
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000837 else hLockedTask = 0;
838 return hLockedTask;
839}
840
841
842/***********************************************************************
843 * IsTaskLocked (KERNEL.122)
844 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000845HTASK16 WINAPI IsTaskLocked16(void)
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000846{
847 return hLockedTask;
848}
849
850
851/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +0000852 * OldYield (KERNEL.117)
853 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000854void WINAPI OldYield16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000855{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000856 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000857
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000858 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000859 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000860 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000861 return;
862 }
863
Alexandre Julliard594997c1995-04-30 10:05:20 +0000864 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
Ulrich Weiganded490031999-06-06 14:47:50 +0000865 TASK_Reschedule();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000866 if (pCurTask) pCurTask->nEvents--;
867}
868
Ulrich Weigand081daf71999-07-31 17:27:38 +0000869/***********************************************************************
870 * WIN32_OldYield16 (KERNEL.447)
871 */
872void WINAPI WIN32_OldYield16(void)
873{
874 DWORD count;
875
876 ReleaseThunkLock(&count);
877 RestoreThunkLock(count);
878}
Alexandre Julliard594997c1995-04-30 10:05:20 +0000879
880/***********************************************************************
881 * DirectedYield (KERNEL.150)
882 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000883void WINAPI DirectedYield16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000884{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000885 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
886
Alexandre Julliard0a860a01999-06-22 11:43:42 +0000887 if ( !THREAD_IsWin16( NtCurrentTeb() ) )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000888 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000889 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000890 return;
891 }
892
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000893 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
Alex Korobka92c81781998-10-18 10:47:03 +0000894
Alexandre Julliardef702d81996-05-28 18:54:58 +0000895 pCurTask->hYieldTo = hTask;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000896 OldYield16();
Alex Korobka92c81781998-10-18 10:47:03 +0000897
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000898 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
Alexandre Julliardef702d81996-05-28 18:54:58 +0000899}
Alexandre Julliard594997c1995-04-30 10:05:20 +0000900
Alexandre Julliard594997c1995-04-30 10:05:20 +0000901/***********************************************************************
Alexandre Julliarde658d821997-11-30 17:45:40 +0000902 * Yield16 (KERNEL.29)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000903 */
Alexandre Julliarde658d821997-11-30 17:45:40 +0000904void WINAPI Yield16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000905{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000906 TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() );
907
Alexandre Julliardef702d81996-05-28 18:54:58 +0000908 if (pCurTask) pCurTask->hYieldTo = 0;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000909 if (pCurTask && pCurTask->hQueue) Callout.UserYield16();
910 else OldYield16();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000911}
912
Ulrich Weigandb9eab151998-11-26 14:58:11 +0000913/***********************************************************************
914 * KERNEL_490 (KERNEL.490)
915 */
916HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
917{
918 if ( !someTask ) return 0;
919
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000920 FIXME("(%04x): stub\n", someTask );
Ulrich Weigandb9eab151998-11-26 14:58:11 +0000921 return 0;
922}
Alexandre Julliard594997c1995-04-30 10:05:20 +0000923
924/***********************************************************************
Alexandre Julliard3051b641996-07-05 17:14:13 +0000925 * MakeProcInstance16 (KERNEL.51)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000926 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000927FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000928{
Alexandre Julliarde658d821997-11-30 17:45:40 +0000929 BYTE *thunk,*lfunc;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000930 SEGPTR thunkaddr;
Bernd Herd4d057612000-05-11 00:10:36 +0000931 WORD hInstanceSelector;
932
933 hInstanceSelector = GlobalHandleToSel16(hInstance);
Alexandre Julliard54c27111998-03-29 19:44:57 +0000934
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000935 TRACE("(%08lx, %04x);", (DWORD)func, hInstance);
936
937 if (!HIWORD(func)) {
938 /* Win95 actually protects via SEH, but this is better for debugging */
939 ERR("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
940 return (FARPROC16)0;
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000941 }
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000942
943 if (hInstance)
Uwe Bonnesf959be91999-10-23 19:47:55 +0000944 {
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000945 if ( (!(hInstance & 4)) ||
946 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
947 {
948 ERR("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
949 hInstance);
950 return 0;
951 }
Uwe Bonnesf959be91999-10-23 19:47:55 +0000952 }
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000953
Bernd Herd4d057612000-05-11 00:10:36 +0000954 if ( (CURRENT_DS != hInstanceSelector)
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000955 && (hInstance != 0)
956 && (hInstance != 0xffff) )
957 {
958 /* calling MPI with a foreign DSEG is invalid ! */
959 ERR("Problem with hInstance? Got %04x, using %04x instead\n",
960 hInstance,CURRENT_DS);
961 }
962
963 /* Always use the DSEG that MPI was entered with.
964 * We used to set hInstance to GetTaskDS16(), but this should be wrong
965 * as CURRENT_DS provides the DSEG value we need.
966 * ("calling" DS, *not* "task" DS !) */
Bernd Herd4d057612000-05-11 00:10:36 +0000967 hInstanceSelector = CURRENT_DS;
968 hInstance = GlobalHandle16(hInstanceSelector);
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000969
970 /* no thunking for DLLs */
971 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
972 return func;
973
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000974 thunkaddr = TASK_AllocThunk( GetCurrentTask() );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000975 if (!thunkaddr) return (FARPROC16)0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000976 thunk = PTR_SEG_TO_LIN( thunkaddr );
Alexandre Julliarde658d821997-11-30 17:45:40 +0000977 lfunc = PTR_SEG_TO_LIN( func );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000978
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000979 TRACE("(%08lx,%04x): got thunk %08lx\n",
980 (DWORD)func, hInstance, (DWORD)thunkaddr );
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000981 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
982 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
Alexandre Julliarde658d821997-11-30 17:45:40 +0000983 ) {
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000984 FIXME("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n");
Alexandre Julliarde658d821997-11-30 17:45:40 +0000985 }
Bernd Herd4d057612000-05-11 00:10:36 +0000986
Alexandre Julliard594997c1995-04-30 10:05:20 +0000987 *thunk++ = 0xb8; /* movw instance, %ax */
Bernd Herd4d057612000-05-11 00:10:36 +0000988 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
989 *thunk++ = (BYTE)(hInstanceSelector >> 8);
Alexandre Julliard594997c1995-04-30 10:05:20 +0000990 *thunk++ = 0xea; /* ljmp func */
991 *(DWORD *)thunk = (DWORD)func;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000992 return (FARPROC16)thunkaddr;
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000993 /* CX reg indicates if thunkaddr != NULL, implement if needed */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000994}
995
996
997/***********************************************************************
Alexandre Julliard3051b641996-07-05 17:14:13 +0000998 * FreeProcInstance16 (KERNEL.52)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000999 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001000void WINAPI FreeProcInstance16( FARPROC16 func )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001001{
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001002 TRACE("(%08lx)\n", (DWORD)func );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001003 TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func );
Alexandre Julliard58199531994-04-21 01:20:00 +00001004}
1005
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001006/**********************************************************************
1007 * TASK_GetCodeSegment
1008 *
1009 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
1010 * and logical segment number of a given code segment.
1011 *
1012 * 'proc' either *is* already a pair of module handle and segment number,
1013 * in which case there's nothing to do. Otherwise, it is a pointer to
1014 * a function, and we need to retrieve the code segment. If the pointer
1015 * happens to point to a thunk, we'll retrieve info about the code segment
1016 * where the function pointed to by the thunk resides, not the thunk itself.
1017 *
1018 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
1019 * the function the snoop code will return to ...
1020 *
1021 */
1022static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
1023 SEGTABLEENTRY **ppSeg, int *pSegNr )
1024{
1025 NE_MODULE *pModule = NULL;
1026 SEGTABLEENTRY *pSeg = NULL;
1027 int segNr;
1028
1029 /* Try pair of module handle / segment number */
1030 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
1031 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
1032 {
1033 segNr = LOWORD( proc );
1034 if ( segNr && segNr <= pModule->seg_count )
1035 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
1036 }
1037
1038 /* Try thunk or function */
1039 else
1040 {
1041 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
1042 WORD selector;
1043
1044 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
1045 selector = thunk[6] + (thunk[7] << 8);
1046 else
1047 selector = HIWORD( proc );
1048
1049 pModule = NE_GetPtr( GlobalHandle16( selector ) );
1050 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
1051
1052 if ( pModule )
1053 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
1054 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
1055 break;
1056
1057 if ( pModule && segNr > pModule->seg_count )
1058 pSeg = NULL;
1059 }
1060
1061 /* Abort if segment not found */
1062
1063 if ( !pModule || !pSeg )
1064 return FALSE;
1065
1066 /* Return segment data */
1067
1068 if ( ppModule ) *ppModule = pModule;
1069 if ( ppSeg ) *ppSeg = pSeg;
1070 if ( pSegNr ) *pSegNr = segNr;
1071
1072 return TRUE;
1073}
Alexandre Julliard58199531994-04-21 01:20:00 +00001074
1075/**********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +00001076 * GetCodeHandle (KERNEL.93)
Alexandre Julliard58199531994-04-21 01:20:00 +00001077 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001078HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
Alexandre Julliard58199531994-04-21 01:20:00 +00001079{
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001080 SEGTABLEENTRY *pSeg;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001081
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001082 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
1083 return (HANDLE16)0;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001084
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001085 return pSeg->hSeg;
Alexandre Julliard58199531994-04-21 01:20:00 +00001086}
1087
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001088/**********************************************************************
1089 * GetCodeInfo (KERNEL.104)
1090 */
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001091BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001092{
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001093 NE_MODULE *pModule;
1094 SEGTABLEENTRY *pSeg;
1095 int segNr;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001096
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001097 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
1098 return FALSE;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001099
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001100 /* Fill in segment information */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001101
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001102 segInfo->offSegment = pSeg->filepos;
1103 segInfo->cbSegment = pSeg->size;
1104 segInfo->flags = pSeg->flags;
1105 segInfo->cbAlloc = pSeg->minsize;
1106 segInfo->h = pSeg->hSeg;
1107 segInfo->alignShift = pModule->alignment;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001108
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001109 if ( segNr == pModule->dgroup )
1110 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001111
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001112 /* Return module handle in %es */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001113
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001114 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001115
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +00001116 return TRUE;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001117}
1118
Alexandre Julliard594997c1995-04-30 10:05:20 +00001119
Alexandre Julliarde658d821997-11-30 17:45:40 +00001120/**********************************************************************
1121 * DefineHandleTable16 (KERNEL.94)
1122 */
1123BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
1124{
1125 return TRUE; /* FIXME */
1126}
1127
1128
Alexandre Julliard594997c1995-04-30 10:05:20 +00001129/***********************************************************************
1130 * SetTaskQueue (KERNEL.34)
1131 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001132HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001133{
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +00001134 HQUEUE16 hPrev;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001135 TDB *pTask;
1136
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001137 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001138 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
Alexandre Julliardc981d0b1996-03-31 16:40:13 +00001139
Alexandre Julliard594997c1995-04-30 10:05:20 +00001140 hPrev = pTask->hQueue;
1141 pTask->hQueue = hQueue;
Alexandre Julliardc981d0b1996-03-31 16:40:13 +00001142
Alexandre Julliard594997c1995-04-30 10:05:20 +00001143 return hPrev;
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001144}
1145
Alexandre Julliard594997c1995-04-30 10:05:20 +00001146
1147/***********************************************************************
1148 * GetTaskQueue (KERNEL.35)
1149 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001150HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001151{
Alexandre Julliard594997c1995-04-30 10:05:20 +00001152 TDB *pTask;
1153
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001154 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001155 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001156 return pTask->hQueue;
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001157}
1158
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001159/***********************************************************************
1160 * SetThreadQueue (KERNEL.463)
1161 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001162HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001163{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001164 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1165 HQUEUE16 oldQueue = teb? teb->queue : 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001166
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001167 if ( teb )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001168 {
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001169 teb->queue = hQueue;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001170
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001171 if ( GetTaskQueue16( teb->process->task ) == oldQueue )
1172 SetTaskQueue16( teb->process->task, hQueue );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001173 }
1174
1175 return oldQueue;
1176}
1177
1178/***********************************************************************
1179 * GetThreadQueue (KERNEL.464)
1180 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001181HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001182{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001183 TEB *teb = NULL;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001184 if ( !thread )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001185 teb = NtCurrentTeb();
Ulrich Weigand901ca361999-01-20 13:35:34 +00001186 else if ( HIWORD(thread) )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001187 teb = THREAD_IdToTEB( thread );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001188 else if ( IsTask16( (HTASK16)thread ) )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001189 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001190
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001191 return (HQUEUE16)(teb? teb->queue : 0);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001192}
1193
1194/***********************************************************************
1195 * SetFastQueue (KERNEL.624)
1196 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001197VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001198{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001199 TEB *teb = NULL;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001200 if ( !thread )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001201 teb = NtCurrentTeb();
Ulrich Weigand901ca361999-01-20 13:35:34 +00001202 else if ( HIWORD(thread) )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001203 teb = THREAD_IdToTEB( thread );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001204 else if ( IsTask16( (HTASK16)thread ) )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001205 teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001206
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001207 if ( teb ) teb->queue = (HQUEUE16) hQueue;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001208}
1209
1210/***********************************************************************
1211 * GetFastQueue (KERNEL.625)
1212 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001213HANDLE WINAPI GetFastQueue16( void )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001214{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001215 TEB *teb = NtCurrentTeb();
1216 if (!teb) return 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001217
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001218 if (!teb->queue)
1219 Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 );
Ulrich Weigandc75d0e11998-11-01 17:59:35 +00001220
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001221 if (!teb->queue)
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001222 FIXME("(): should initialize thread-local queue, expect failure!\n" );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001223
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001224 return (HANDLE)teb->queue;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001225}
Alexandre Julliard594997c1995-04-30 10:05:20 +00001226
1227/***********************************************************************
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001228 * SwitchStackTo (KERNEL.108)
1229 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001230void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001231{
1232 TDB *pTask;
1233 STACK16FRAME *oldFrame, *newFrame;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001234 INSTANCEDATA *pData;
1235 UINT16 copySize;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001236
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001237 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001238 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001239 TRACE("old=%04x:%04x new=%04x:%04x\n",
1240 SELECTOROF( pTask->teb->cur_stack ),
1241 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001242
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001243 /* Save the old stack */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001244
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001245 oldFrame = THREAD_STACK16( pTask->teb );
Alexandre Julliarda845b881998-06-01 10:44:35 +00001246 /* pop frame + args and push bp */
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001247 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
Alexandre Julliard75796ea1998-10-25 08:05:46 +00001248 + 2 * sizeof(WORD);
Alexandre Julliarda845b881998-06-01 10:44:35 +00001249 *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001250 pData->stacktop = top;
1251 pData->stackmin = ptr;
1252 pData->stackbottom = ptr;
1253
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001254 /* Switch to the new stack */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001255
Alexandre Julliard17216f51997-10-12 16:30:17 +00001256 /* Note: we need to take the 3 arguments into account; otherwise,
1257 * the stack will underflow upon return from this function.
1258 */
Alexandre Julliarda845b881998-06-01 10:44:35 +00001259 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1260 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001261 pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize );
1262 newFrame = THREAD_STACK16( pTask->teb );
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001263
1264 /* Copy the stack frame and the local variables to the new stack */
1265
Alexandre Julliarda845b881998-06-01 10:44:35 +00001266 memmove( newFrame, oldFrame, copySize );
1267 newFrame->bp = ptr;
1268 *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001269}
1270
1271
1272/***********************************************************************
1273 * SwitchStackBack (KERNEL.109)
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001274 */
Alexandre Julliard617955d1999-06-26 18:40:24 +00001275void WINAPI SwitchStackBack16( CONTEXT86 *context )
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001276{
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001277 STACK16FRAME *oldFrame, *newFrame;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001278 INSTANCEDATA *pData;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001279
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001280 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001281 return;
1282 if (!pData->old_ss_sp)
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001283 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001284 WARN("No previous SwitchStackTo\n" );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001285 return;
1286 }
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001287 TRACE("restoring stack %04x:%04x\n",
1288 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001289
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001290 oldFrame = CURRENT_STACK16;
Alexandre Julliarda845b881998-06-01 10:44:35 +00001291
1292 /* Pop bp from the previous stack */
1293
1294 BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp);
1295 pData->old_ss_sp += sizeof(WORD);
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001296
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001297 /* Switch back to the old stack */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001298
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001299 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001300 SS_reg(context) = SELECTOROF(pData->old_ss_sp);
Alexandre Julliarda845b881998-06-01 10:44:35 +00001301 ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001302 pData->old_ss_sp = 0;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001303
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001304 /* Build a stack frame for the return */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001305
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001306 newFrame = CURRENT_STACK16;
Ulrich Weigand3df3e531999-08-18 18:24:08 +00001307 newFrame->frame32 = oldFrame->frame32;
1308 newFrame->module_cs = oldFrame->module_cs;
1309 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1310 newFrame->entry_ip = oldFrame->entry_ip;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001311}
1312
1313
1314/***********************************************************************
Alexandre Julliard4220b291999-07-11 17:20:01 +00001315 * GetTaskQueueDS16 (KERNEL.118)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001316 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001317void WINAPI GetTaskQueueDS16(void)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001318{
Alexandre Julliard4220b291999-07-11 17:20:01 +00001319 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
Alexandre Julliardade697e1995-11-26 13:59:11 +00001320}
Alexandre Julliardade697e1995-11-26 13:59:11 +00001321
1322
1323/***********************************************************************
Alexandre Julliard4220b291999-07-11 17:20:01 +00001324 * GetTaskQueueES16 (KERNEL.119)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001325 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001326void WINAPI GetTaskQueueES16(void)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001327{
Alexandre Julliard4220b291999-07-11 17:20:01 +00001328 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
Alexandre Julliardade697e1995-11-26 13:59:11 +00001329}
Alexandre Julliardade697e1995-11-26 13:59:11 +00001330
1331
1332/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +00001333 * GetCurrentTask (KERNEL.36)
1334 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001335HTASK16 WINAPI GetCurrentTask(void)
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001336{
Alexandre Julliard54a39e21999-05-29 14:27:26 +00001337 return PROCESS_Current()->task;
Alexandre Julliard2d93d001996-05-21 15:01:41 +00001338}
1339
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001340DWORD WINAPI WIN16_GetCurrentTask(void)
Alexandre Julliard2d93d001996-05-21 15:01:41 +00001341{
1342 /* This is the version used by relay code; the first task is */
1343 /* returned in the high word of the result */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001344 return MAKELONG( GetCurrentTask(), hFirstTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001345}
1346
1347
1348/***********************************************************************
1349 * GetCurrentPDB (KERNEL.37)
Andreas Mohra00b49f1998-12-07 10:48:09 +00001350 *
1351 * UNDOC: returns PSP of KERNEL in high word
Alexandre Julliard594997c1995-04-30 10:05:20 +00001352 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001353DWORD WINAPI GetCurrentPDB16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001354{
1355 TDB *pTask;
1356
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001357 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
Andreas Mohra00b49f1998-12-07 10:48:09 +00001358 return MAKELONG(pTask->hPDB, 0); /* FIXME */
Alexandre Julliard594997c1995-04-30 10:05:20 +00001359}
1360
1361
1362/***********************************************************************
Alexandre Julliard4220b291999-07-11 17:20:01 +00001363 * GetCurPID16 (KERNEL.157)
1364 */
1365DWORD WINAPI GetCurPID16( DWORD unused )
1366{
1367 return 0;
1368}
1369
1370
1371/***********************************************************************
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001372 * GetInstanceData (KERNEL.54)
1373 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001374INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001375{
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001376 char *ptr = (char *)GlobalLock16( instance );
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001377 if (!ptr || !len) return 0;
1378 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
Alexandre Julliard18f92e71996-07-17 20:02:21 +00001379 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001380 return len;
1381}
1382
1383
1384/***********************************************************************
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001385 * GetExeVersion (KERNEL.105)
1386 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001387WORD WINAPI GetExeVersion16(void)
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001388{
1389 TDB *pTask;
1390
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001391 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001392 return pTask->version;
1393}
1394
1395
1396/***********************************************************************
Alexandre Julliard349a9531997-02-02 19:01:52 +00001397 * SetErrorMode16 (KERNEL.107)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001398 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001399UINT16 WINAPI SetErrorMode16( UINT16 mode )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001400{
1401 TDB *pTask;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001402 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001403 pTask->error_mode = mode;
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001404 return SetErrorMode( mode );
Alexandre Julliard349a9531997-02-02 19:01:52 +00001405}
1406
1407
1408/***********************************************************************
Alexandre Julliard808cb041995-08-17 17:11:36 +00001409 * GetDOSEnvironment (KERNEL.131)
1410 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001411SEGPTR WINAPI GetDOSEnvironment16(void)
Alexandre Julliard808cb041995-08-17 17:11:36 +00001412{
1413 TDB *pTask;
1414
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001415 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +00001416 return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 );
Alexandre Julliard808cb041995-08-17 17:11:36 +00001417}
1418
1419
1420/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +00001421 * GetNumTasks (KERNEL.152)
1422 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001423UINT16 WINAPI GetNumTasks16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001424{
1425 return nTaskCount;
1426}
1427
1428
1429/***********************************************************************
1430 * GetTaskDS (KERNEL.155)
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001431 *
1432 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1433 * I don't think we need to bother with this.
Alexandre Julliard594997c1995-04-30 10:05:20 +00001434 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001435HINSTANCE16 WINAPI GetTaskDS16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001436{
1437 TDB *pTask;
1438
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001439 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
Bernd Herd4d057612000-05-11 00:10:36 +00001440 return GlobalHandleToSel16(pTask->hInstance);
Alexandre Julliard594997c1995-04-30 10:05:20 +00001441}
1442
Ulrich Weigandb9eab151998-11-26 14:58:11 +00001443/***********************************************************************
1444 * GetDummyModuleHandleDS (KERNEL.602)
1445 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001446WORD WINAPI GetDummyModuleHandleDS16(void)
Ulrich Weigandb9eab151998-11-26 14:58:11 +00001447{
1448 TDB *pTask;
1449 WORD selector;
1450
Alexandre Julliard4220b291999-07-11 17:20:01 +00001451 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
1452 if (!(pTask->flags & TDBF_WIN32)) return 0;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001453 selector = GlobalHandleToSel16( pTask->hModule );
Alexandre Julliard4220b291999-07-11 17:20:01 +00001454 CURRENT_DS = selector;
1455 return selector;
Ulrich Weigandb9eab151998-11-26 14:58:11 +00001456}
Alexandre Julliard594997c1995-04-30 10:05:20 +00001457
1458/***********************************************************************
1459 * IsTask (KERNEL.320)
1460 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001461BOOL16 WINAPI IsTask16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001462{
1463 TDB *pTask;
1464
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001465 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE;
1466 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001467 return (pTask->magic == TDB_MAGIC);
1468}
1469
1470
1471/***********************************************************************
Alexandre Julliard4220b291999-07-11 17:20:01 +00001472 * IsWinOldApTask16 (KERNEL.158)
1473 */
1474BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1475{
1476 /* should return bit 0 of byte 0x48 in PSP */
1477 return FALSE;
1478}
1479
1480/***********************************************************************
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001481 * SetTaskSignalProc (KERNEL.38)
1482 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001483FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001484{
1485 TDB *pTask;
1486 FARPROC16 oldProc;
1487
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001488 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001489 if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL;
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +00001490 oldProc = pTask->userhandler;
1491 pTask->userhandler = proc;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001492 return oldProc;
1493}
1494
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +00001495/***********************************************************************
1496 * TASK_CallTaskSignalProc
1497 */
1498/* ### start build ### */
1499extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1500/* ### stop build ### */
1501void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1502{
1503 TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() );
1504 if ( !pTask || !pTask->userhandler ) return;
1505
1506 TASK_CallTo16_word_wwwww( pTask->userhandler,
1507 hTaskOrModule, uCode, 0,
1508 pTask->hInstance, pTask->hQueue );
1509}
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001510
1511/***********************************************************************
1512 * SetSigHandler (KERNEL.140)
1513 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001514WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001515 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001516{
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001517 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +00001518 newhandler,oldhandler,oldmode,newmode,flag );
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001519
1520 if (flag != 1) return 0;
1521 if (!newmode) newhandler = NULL; /* Default handler */
1522 if (newmode != 4)
1523 {
1524 TDB *pTask;
1525
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001526 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001527 if (oldmode) *oldmode = pTask->signal_flags;
1528 pTask->signal_flags = newmode;
1529 if (oldhandler) *oldhandler = pTask->sighandler;
1530 pTask->sighandler = newhandler;
1531 }
1532 return 0;
1533}
1534
1535
1536/***********************************************************************
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001537 * GlobalNotify (KERNEL.154)
Andreas Mohra00b49f1998-12-07 10:48:09 +00001538 *
1539 * Note that GlobalNotify does _not_ return the old NotifyProc
1540 * -- contrary to LocalNotify !!
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001541 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001542VOID WINAPI GlobalNotify16( FARPROC16 proc )
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001543{
1544 TDB *pTask;
1545
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001546 if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001547 pTask->discardhandler = proc;
1548}
1549
1550
1551/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +00001552 * GetExePtr (KERNEL.133)
1553 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001554static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001555{
1556 char *ptr;
Alexandre Julliard18f92e71996-07-17 20:02:21 +00001557 HANDLE16 owner;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001558
1559 /* Check for module handle */
1560
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001561 if (!(ptr = GlobalLock16( handle ))) return 0;
Alexandre Julliard23946ad1997-06-16 17:43:53 +00001562 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001563
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001564 /* Search for this handle inside all tasks */
1565
1566 *hTask = hFirstTask;
1567 while (*hTask)
1568 {
1569 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1570 if ((*hTask == handle) ||
1571 (pTask->hInstance == handle) ||
1572 (pTask->hQueue == handle) ||
1573 (pTask->hPDB == handle)) return pTask->hModule;
1574 *hTask = pTask->hNext;
1575 }
1576
Alexandre Julliard594997c1995-04-30 10:05:20 +00001577 /* Check the owner for module handle */
1578
Alexandre Julliarda3960291999-02-26 11:11:13 +00001579 owner = FarGetOwner16( handle );
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001580 if (!(ptr = GlobalLock16( owner ))) return 0;
Alexandre Julliard23946ad1997-06-16 17:43:53 +00001581 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001582
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001583 /* Search for the owner inside all tasks */
Alexandre Julliard594997c1995-04-30 10:05:20 +00001584
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001585 *hTask = hFirstTask;
1586 while (*hTask)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001587 {
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001588 TDB *pTask = (TDB *)GlobalLock16( *hTask );
1589 if ((*hTask == owner) ||
Alexandre Julliard594997c1995-04-30 10:05:20 +00001590 (pTask->hInstance == owner) ||
1591 (pTask->hQueue == owner) ||
1592 (pTask->hPDB == owner)) return pTask->hModule;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001593 *hTask = pTask->hNext;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001594 }
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001595
Alexandre Julliard594997c1995-04-30 10:05:20 +00001596 return 0;
1597}
1598
Ulrich Weigandcfa977d1999-07-12 13:39:10 +00001599HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001600{
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001601 HTASK16 hTask = 0;
Alexandre Julliard4220b291999-07-11 17:20:01 +00001602 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
Ulrich Weigandcfa977d1999-07-12 13:39:10 +00001603 STACK16FRAME *frame = CURRENT_STACK16;
1604 frame->ecx = hModule;
1605 if (hTask) frame->es = hTask;
Alexandre Julliard4220b291999-07-11 17:20:01 +00001606 return hModule;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001607}
1608
Ulrich Weigandcfa977d1999-07-12 13:39:10 +00001609HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1610{
1611 HTASK16 hTask = 0;
1612 return GetExePtrHelper( handle, &hTask );
1613}
1614
1615
Alexandre Julliard594997c1995-04-30 10:05:20 +00001616/***********************************************************************
1617 * TaskFirst (TOOLHELP.63)
1618 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001619BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001620{
1621 lpte->hNext = hFirstTask;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001622 return TaskNext16( lpte );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001623}
1624
1625
1626/***********************************************************************
1627 * TaskNext (TOOLHELP.64)
1628 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001629BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001630{
1631 TDB *pTask;
1632 INSTANCEDATA *pInstData;
1633
Alexandre Julliarde8c6a081999-05-02 14:33:41 +00001634 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001635 if (!lpte->hNext) return FALSE;
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001636 pTask = (TDB *)GlobalLock16( lpte->hNext );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001637 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
Bernd Herd4d057612000-05-11 00:10:36 +00001638 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( GlobalHandleToSel16(pTask->hInstance), 0 );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001639 lpte->hTask = lpte->hNext;
1640 lpte->hTaskParent = pTask->hParent;
1641 lpte->hInst = pTask->hInstance;
1642 lpte->hModule = pTask->hModule;
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001643 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1644 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001645 lpte->wStackTop = pInstData->stacktop;
1646 lpte->wStackMinimum = pInstData->stackmin;
1647 lpte->wStackBottom = pInstData->stackbottom;
1648 lpte->wcEvents = pTask->nEvents;
1649 lpte->hQueue = pTask->hQueue;
Francois Gougetbaa9bf91999-12-27 05:24:06 +00001650 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001651 lpte->wPSPOffset = 0x100; /*??*/
1652 lpte->hNext = pTask->hNext;
1653 return TRUE;
1654}
1655
1656
1657/***********************************************************************
1658 * TaskFindHandle (TOOLHELP.65)
1659 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001660BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001661{
1662 lpte->hNext = hTask;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001663 return TaskNext16( lpte );
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001664}
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001665
1666
1667/***********************************************************************
Alexandre Julliard21979011997-03-05 08:22:35 +00001668 * GetAppCompatFlags16 (KERNEL.354)
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001669 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001670DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
Alexandre Julliard21979011997-03-05 08:22:35 +00001671{
Alexandre Julliarda3960291999-02-26 11:11:13 +00001672 return GetAppCompatFlags( hTask );
Alexandre Julliard21979011997-03-05 08:22:35 +00001673}
1674
1675
1676/***********************************************************************
Patrik Stridvall2d6457c2000-03-28 20:22:59 +00001677 * GetAppCompatFlags (USER32.206)
Alexandre Julliard21979011997-03-05 08:22:35 +00001678 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001679DWORD WINAPI GetAppCompatFlags( HTASK hTask )
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001680{
1681 TDB *pTask;
1682
1683 if (!hTask) hTask = GetCurrentTask();
1684 if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0;
1685 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1686 return pTask->compat_flags;
1687}