blob: b56692f2b83fb044d06e3552a74e84ccf311095c [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
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Alexandre Julliard594997c1995-04-30 10:05:20 +000019 */
20
Patrik Stridvall33929be2001-07-18 21:04:23 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliard58199531994-04-21 01:20:00 +000024#include <stdlib.h>
25#include <string.h>
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000026#include <assert.h>
Patrik Stridvalld016f812002-08-17 00:43:16 +000027#ifdef HAVE_UNISTD_H
28# include <unistd.h>
29#endif
Alexandre Julliardef702d81996-05-28 18:54:58 +000030
Patrik Stridvall33929be2001-07-18 21:04:23 +000031#include "winbase.h"
32#include "wingdi.h"
33#include "winnt.h"
34#include "winuser.h"
Patrik Stridvall33929be2001-07-18 21:04:23 +000035
Marcus Meissner317af321999-02-17 13:51:06 +000036#include "wine/winbase16.h"
Alexandre Julliard491502b1997-11-01 19:08:16 +000037#include "drive.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000038#include "file.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000039#include "global.h"
40#include "instance.h"
Alexandre Julliarda2f2e011995-06-06 16:40:35 +000041#include "miscemu.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000042#include "module.h"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000043#include "winternl.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000044#include "selectors.h"
Alexandre Julliard37e95032001-07-19 00:39:09 +000045#include "wine/server.h"
Patrik Stridvall33929be2001-07-18 21:04:23 +000046#include "syslevel.h"
Alexandre Julliardf1aa3031996-08-05 17:42:43 +000047#include "stackframe.h"
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000048#include "task.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000049#include "thread.h"
Alexandre Julliard594997c1995-04-30 10:05:20 +000050#include "toolhelp.h"
Alexandre Julliardaca05781994-10-17 18:12:41 +000051
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000052#include "wine/debug.h"
Alexandre Julliard770eb512000-09-24 20:47:50 +000053
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000054WINE_DEFAULT_DEBUG_CHANNEL(task);
55WINE_DECLARE_DEBUG_CHANNEL(relay);
56WINE_DECLARE_DEBUG_CHANNEL(toolhelp);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000057
Alexandre Julliard2787be81995-05-22 18:23:01 +000058 /* Min. number of thunks allocated when creating a new segment */
59#define MIN_THUNKS 32
Alexandre Julliard58199531994-04-21 01:20:00 +000060
Alexandre Julliard18f92e71996-07-17 20:02:21 +000061
Patrik Stridvalla9f6a9d2000-10-24 02:22:16 +000062static THHOOK DefaultThhook;
Alexandre Julliard642d3131998-07-12 19:29:36 +000063THHOOK *pThhook = &DefaultThhook;
64
65#define hCurrentTask (pThhook->CurTDB)
66#define hFirstTask (pThhook->HeadTDB)
67#define hLockedTask (pThhook->LockTDB)
68
Alexandre Julliardbf9130a1996-10-13 17:45:47 +000069static UINT16 nTaskCount = 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +000070
Alexandre Julliard7c4b1402001-07-30 19:01:01 +000071static HTASK16 initial_task;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +000072
Alexandre Julliard642d3131998-07-12 19:29:36 +000073/***********************************************************************
74 * TASK_InstallTHHook
75 */
76void TASK_InstallTHHook( THHOOK *pNewThhook )
77{
78 THHOOK *pOldThhook = pThhook;
79
80 pThhook = pNewThhook? pNewThhook : &DefaultThhook;
81
82 *pThhook = *pOldThhook;
83}
Alexandre Julliard808cb041995-08-17 17:11:36 +000084
85/***********************************************************************
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000086 * TASK_GetNextTask
87 */
Alexandre Julliardbf9130a1996-10-13 17:45:47 +000088HTASK16 TASK_GetNextTask( HTASK16 hTask )
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000089{
Alexandre Julliard3e7497f2001-04-04 00:19:55 +000090 TDB* pTask = TASK_GetPtr( hTask );
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000091
92 if (pTask->hNext) return pTask->hNext;
Vincent BĂ©ron9a624912002-05-31 23:06:46 +000093 return (hFirstTask != hTask) ? hFirstTask : 0;
Alexandre Julliard2ace16a1996-04-28 15:09:19 +000094}
95
Alexandre Julliard3e7497f2001-04-04 00:19:55 +000096
97/***********************************************************************
98 * TASK_GetPtr
99 */
100TDB *TASK_GetPtr( HTASK16 hTask )
101{
102 return GlobalLock16( hTask );
103}
104
105
106/***********************************************************************
107 * TASK_GetCurrent
108 */
109TDB *TASK_GetCurrent(void)
110{
111 return TASK_GetPtr( GetCurrentTask() );
112}
113
114
Alexandre Julliard2ace16a1996-04-28 15:09:19 +0000115/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +0000116 * TASK_LinkTask
Alexandre Julliard58199531994-04-21 01:20:00 +0000117 */
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000118static void TASK_LinkTask( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +0000119{
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000120 HTASK16 *prevTask;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000121 TDB *pTask;
122
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000123 if (!(pTask = TASK_GetPtr( hTask ))) return;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000124 prevTask = &hFirstTask;
125 while (*prevTask)
126 {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000127 TDB *prevTaskPtr = TASK_GetPtr( *prevTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000128 if (prevTaskPtr->priority >= pTask->priority) break;
129 prevTask = &prevTaskPtr->hNext;
130 }
131 pTask->hNext = *prevTask;
132 *prevTask = hTask;
133 nTaskCount++;
Alexandre Julliard58199531994-04-21 01:20:00 +0000134}
135
136
Alexandre Julliard594997c1995-04-30 10:05:20 +0000137/***********************************************************************
138 * TASK_UnlinkTask
Alexandre Julliard58199531994-04-21 01:20:00 +0000139 */
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000140static void TASK_UnlinkTask( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +0000141{
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000142 HTASK16 *prevTask;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000143 TDB *pTask;
144
145 prevTask = &hFirstTask;
146 while (*prevTask && (*prevTask != hTask))
147 {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000148 pTask = TASK_GetPtr( *prevTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000149 prevTask = &pTask->hNext;
150 }
151 if (*prevTask)
152 {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000153 pTask = TASK_GetPtr( *prevTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000154 *prevTask = pTask->hNext;
155 pTask->hNext = 0;
156 nTaskCount--;
157 }
Alexandre Julliard58199531994-04-21 01:20:00 +0000158}
159
160
Alexandre Julliard594997c1995-04-30 10:05:20 +0000161/***********************************************************************
162 * TASK_CreateThunks
163 *
164 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
165 * and containing 'count' entries.
Alexandre Julliard58199531994-04-21 01:20:00 +0000166 */
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000167static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count )
Alexandre Julliard58199531994-04-21 01:20:00 +0000168{
Alexandre Julliard594997c1995-04-30 10:05:20 +0000169 int i;
170 WORD free;
171 THUNKS *pThunk;
172
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000173 pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset);
Alexandre Julliard594997c1995-04-30 10:05:20 +0000174 pThunk->next = 0;
175 pThunk->magic = THUNK_MAGIC;
176 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
177 free = pThunk->free;
178 for (i = 0; i < count-1; i++)
179 {
180 free += 8; /* Offset of next thunk */
181 pThunk->thunks[4*i] = free;
182 }
183 pThunk->thunks[4*i] = 0; /* Last thunk */
Alexandre Julliard58199531994-04-21 01:20:00 +0000184}
185
186
Alexandre Julliard594997c1995-04-30 10:05:20 +0000187/***********************************************************************
188 * TASK_AllocThunk
189 *
190 * Allocate a thunk for MakeProcInstance().
Alexandre Julliard58199531994-04-21 01:20:00 +0000191 */
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000192static SEGPTR TASK_AllocThunk(void)
Alexandre Julliard58199531994-04-21 01:20:00 +0000193{
Alexandre Julliard594997c1995-04-30 10:05:20 +0000194 TDB *pTask;
195 THUNKS *pThunk;
196 WORD sel, base;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000197
198 if (!(pTask = TASK_GetCurrent())) return 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000199 sel = pTask->hCSAlias;
200 pThunk = &pTask->thunks;
201 base = (int)pThunk - (int)pTask;
202 while (!pThunk->free)
203 {
204 sel = pThunk->next;
205 if (!sel) /* Allocate a new segment */
206 {
207 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
Alexandre Julliard914406f2000-11-14 01:54:49 +0000208 pTask->hPDB, WINE_LDT_FLAGS_CODE );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000209 if (!sel) return (SEGPTR)0;
210 TASK_CreateThunks( sel, 0, MIN_THUNKS );
211 pThunk->next = sel;
212 }
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000213 pThunk = (THUNKS *)GlobalLock16( sel );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000214 base = 0;
215 }
216 base += pThunk->free;
217 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
Alexandre Julliard982a2232000-12-13 20:20:09 +0000218 return MAKESEGPTR( sel, base );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000219}
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +0000220
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +0000221
Alexandre Julliard594997c1995-04-30 10:05:20 +0000222/***********************************************************************
223 * TASK_FreeThunk
224 *
225 * Free a MakeProcInstance() thunk.
226 */
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000227static BOOL TASK_FreeThunk( SEGPTR thunk )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000228{
229 TDB *pTask;
230 THUNKS *pThunk;
231 WORD sel, base;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000232
233 if (!(pTask = TASK_GetCurrent())) return 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000234 sel = pTask->hCSAlias;
235 pThunk = &pTask->thunks;
236 base = (int)pThunk - (int)pTask;
237 while (sel && (sel != HIWORD(thunk)))
238 {
239 sel = pThunk->next;
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000240 pThunk = (THUNKS *)GlobalLock16( sel );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000241 base = 0;
242 }
243 if (!sel) return FALSE;
244 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
245 pThunk->free = LOWORD(thunk) - base;
246 return TRUE;
247}
248
249
250/***********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000251 * TASK_Create
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000252 *
Ulrich Weiganded490031999-06-06 14:47:50 +0000253 * NOTE: This routine might be called by a Win32 thread. Thus, we need
254 * to be careful to protect global data structures. We do this
255 * by entering the Win16Lock while linking the task into the
256 * global task list.
Alexandre Julliard594997c1995-04-30 10:05:20 +0000257 */
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000258static TDB *TASK_Create( NE_MODULE *pModule, UINT16 cmdShow, TEB *teb, LPCSTR cmdline, BYTE len )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000259{
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000260 HTASK16 hTask;
Ulrich Weigand8139c301999-04-01 11:43:05 +0000261 TDB *pTask;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000262 char name[10];
Alexandre Julliard7fbd74e2002-05-23 02:53:10 +0000263 FARPROC16 proc;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000264
265 /* Allocate the task structure */
266
Alexandre Julliard4f2df512001-01-15 20:13:43 +0000267 hTask = GlobalAlloc16( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB) );
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000268 if (!hTask) return NULL;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000269 pTask = TASK_GetPtr( hTask );
Alexandre Julliard4f2df512001-01-15 20:13:43 +0000270 FarSetOwner16( hTask, pModule->self );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000271
Alexandre Julliard491502b1997-11-01 19:08:16 +0000272 /* Fill the task structure */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000273
Alexandre Julliardb18dbd22000-07-25 12:11:53 +0000274 pTask->hSelf = hTask;
Alexandre Julliardb1bac321996-12-15 19:45:59 +0000275
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000276 if (teb && teb->tibflags & TEBF_WIN32)
Alexandre Julliardb18dbd22000-07-25 12:11:53 +0000277 {
278 pTask->flags |= TDBF_WIN32;
279 pTask->hInstance = pModule->self;
280 pTask->hPrevInstance = 0;
281 /* NOTE: for 16-bit tasks, the instance handles are updated later on
282 in NE_InitProcess */
283 }
Alexandre Julliardb1bac321996-12-15 19:45:59 +0000284
Alexandre Julliard2787be81995-05-22 18:23:01 +0000285 pTask->version = pModule->expected_version;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000286 pTask->hModule = pModule->self;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000287 pTask->hParent = GetCurrentTask();
Alexandre Julliard2787be81995-05-22 18:23:01 +0000288 pTask->magic = TDB_MAGIC;
289 pTask->nCmdShow = cmdShow;
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000290 pTask->teb = teb;
Alexandre Julliard491502b1997-11-01 19:08:16 +0000291 pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80;
292 strcpy( pTask->curdir, "\\" );
Dmitry Timoshkovd75aed22002-08-27 01:13:58 +0000293 WideCharToMultiByte(CP_ACP, 0, DRIVE_GetDosCwd(DRIVE_GetCurrentDrive()), -1,
294 pTask->curdir + 1, sizeof(pTask->curdir) - 1, NULL, NULL);
295 pTask->curdir[sizeof(pTask->curdir) - 1] = 0; /* ensure 0 termination */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000296
297 /* Create the thunks block */
298
299 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
300
301 /* Copy the module name */
302
Alexandre Julliarda3960291999-02-26 11:11:13 +0000303 GetModuleName16( pModule->self, name, sizeof(name) );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000304 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
305
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000306 /* Allocate a selector for the PDB */
307
Alexandre Julliarda3960291999-02-26 11:11:13 +0000308 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16),
Alexandre Julliard914406f2000-11-14 01:54:49 +0000309 pModule->self, WINE_LDT_FLAGS_DATA );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000310
Alexandre Julliard594997c1995-04-30 10:05:20 +0000311 /* Fill the PDB */
312
313 pTask->pdb.int20 = 0x20cd;
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000314 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
Alexandre Julliard7fbd74e2002-05-23 02:53:10 +0000315 proc = GetProcAddress16( GetModuleHandle16("KERNEL"), "DOS3Call" );
316 memcpy( &pTask->pdb.dispatcher[1], &proc, sizeof(proc) );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000317 pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 );
318 pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 );
319 pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 );
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000320 pTask->pdb.fileHandlesPtr =
Alexandre Julliard982a2232000-12-13 20:20:09 +0000321 MAKESEGPTR( GlobalHandleToSel16(pTask->hPDB), (int)&((PDB16 *)0)->fileHandles );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000322 pTask->pdb.hFileHandles = 0;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000323 memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) );
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000324 /* FIXME: should we make a copy of the environment? */
325 pTask->pdb.environment = SELECTOROF(GetDOSEnvironment16());
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000326 pTask->pdb.nbFiles = 20;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000327
328 /* Fill the command line */
329
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000330 if (!cmdline)
331 {
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000332 cmdline = GetCommandLineA();
Alexandre Julliard15aef002000-07-31 21:02:53 +0000333 /* remove the first word (program name) */
334 if (*cmdline == '"')
Alexandre Julliardbecb9a32000-12-11 03:48:15 +0000335 if (!(cmdline = strchr( cmdline+1, '"' ))) cmdline = GetCommandLineA();
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000336 while (*cmdline && (*cmdline != ' ') && (*cmdline != '\t')) cmdline++;
337 while ((*cmdline == ' ') || (*cmdline == '\t')) cmdline++;
338 len = strlen(cmdline);
339 }
340 if (len >= sizeof(pTask->pdb.cmdLine)) len = sizeof(pTask->pdb.cmdLine)-1;
341 pTask->pdb.cmdLine[0] = len;
342 memcpy( pTask->pdb.cmdLine + 1, cmdline, len );
343 /* pTask->pdb.cmdLine[len+1] = 0; */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000344
Alexandre Julliard15aef002000-07-31 21:02:53 +0000345 TRACE("module='%s' cmdline='%.*s' task=%04x\n", name, len, cmdline, hTask );
346
Alexandre Julliard594997c1995-04-30 10:05:20 +0000347 /* Get the compatibility flags */
348
Alexandre Julliarda3960291999-02-26 11:11:13 +0000349 pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000350
Alexandre Julliard594997c1995-04-30 10:05:20 +0000351 /* Allocate a code segment alias for the TDB */
352
353 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
Alexandre Julliard914406f2000-11-14 01:54:49 +0000354 sizeof(TDB), pTask->hPDB, WINE_LDT_FLAGS_CODE );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000355
Andreas Mohr2caee712000-07-16 15:44:22 +0000356 /* Default DTA overwrites command line */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000357
Alexandre Julliard982a2232000-12-13 20:20:09 +0000358 pTask->dta = MAKESEGPTR( pTask->hPDB, (int)&pTask->pdb.cmdLine - (int)&pTask->pdb );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000359
Ulrich Weiganded490031999-06-06 14:47:50 +0000360 /* Create scheduler event for 16-bit tasks */
361
362 if ( !(pTask->flags & TDBF_WIN32) )
Alexandre Julliardafb49ea2000-09-29 20:48:04 +0000363 NtCreateEvent( &pTask->hEvent, EVENT_ALL_ACCESS, NULL, TRUE, FALSE );
Ulrich Weiganded490031999-06-06 14:47:50 +0000364
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000365 /* Enter task handle into thread */
Alexandre Julliard7bbba4f2000-08-26 21:18:36 +0000366
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000367 if (teb) teb->htask16 = hTask;
Alexandre Julliard67a24c82000-02-18 21:54:32 +0000368 if (!initial_task) initial_task = hTask;
369
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000370 return pTask;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000371}
372
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000373
Alexandre Julliard594997c1995-04-30 10:05:20 +0000374/***********************************************************************
375 * TASK_DeleteTask
Alexandre Julliard58199531994-04-21 01:20:00 +0000376 */
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000377static void TASK_DeleteTask( HTASK16 hTask )
Alexandre Julliard58199531994-04-21 01:20:00 +0000378{
Alexandre Julliard594997c1995-04-30 10:05:20 +0000379 TDB *pTask;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000380 HGLOBAL16 hPDB;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000381
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000382 if (!(pTask = TASK_GetPtr( hTask ))) return;
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000383 hPDB = pTask->hPDB;
384
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000385 pTask->magic = 0xdead; /* invalidate signature */
386
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000387 /* Free the selector aliases */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000388
389 GLOBAL_FreeBlock( pTask->hCSAlias );
390 GLOBAL_FreeBlock( pTask->hPDB );
391
Ulrich Weigandafac2e41998-11-14 18:45:25 +0000392 /* Free the task module */
393
394 FreeModule16( pTask->hModule );
395
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000396 /* Free the task structure itself */
Alexandre Julliard2787be81995-05-22 18:23:01 +0000397
Alexandre Julliard1285c2f1996-05-06 16:06:24 +0000398 GlobalFree16( hTask );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000399
400 /* Free all memory used by this task (including the 32-bit stack, */
401 /* the environment block and the thunk segments). */
402
Alexandre Julliarda3960291999-02-26 11:11:13 +0000403 GlobalFreeAll16( hPDB );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000404}
405
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000406
407/***********************************************************************
408 * TASK_CreateMainTask
409 *
410 * Create a task for the main (32-bit) process.
411 */
412void TASK_CreateMainTask(void)
413{
414 TDB *pTask;
415 STARTUPINFOA startup_info;
416 UINT cmdShow = 1; /* SW_SHOWNORMAL but we don't want to include winuser.h here */
417
418 GetStartupInfoA( &startup_info );
419 if (startup_info.dwFlags & STARTF_USESHOWWINDOW) cmdShow = startup_info.wShowWindow;
420 pTask = TASK_Create( (NE_MODULE *)GlobalLock16( MapHModuleLS(GetModuleHandleA(0)) ),
421 cmdShow, NtCurrentTeb(), NULL, 0 );
422 if (!pTask)
423 {
424 ERR("could not create task for main process\n");
425 ExitProcess(1);
426 }
427
428 /* Add the task to the linked list */
429 /* (no need to get the win16 lock, we are the only thread at this point) */
430 TASK_LinkTask( pTask->hSelf );
431}
432
433
434/* startup routine for a new 16-bit thread */
435static DWORD CALLBACK task_start( TDB *pTask )
436{
437 DWORD ret;
438
439 NtCurrentTeb()->tibflags &= ~TEBF_WIN32;
440 NtCurrentTeb()->htask16 = pTask->hSelf;
441
442 _EnterWin16Lock();
443 TASK_LinkTask( pTask->hSelf );
444 pTask->teb = NtCurrentTeb();
445 ret = NE_StartTask();
446 _LeaveWin16Lock();
447 return ret;
448}
449
450
451/***********************************************************************
452 * TASK_SpawnTask
453 *
454 * Spawn a new 16-bit task.
455 */
Alexandre Julliard7c4b1402001-07-30 19:01:01 +0000456HTASK16 TASK_SpawnTask( NE_MODULE *pModule, WORD cmdShow,
457 LPCSTR cmdline, BYTE len, HANDLE *hThread )
Alexandre Julliard9ee9eff2001-02-28 05:29:50 +0000458{
459 TDB *pTask;
460
461 if (!(pTask = TASK_Create( pModule, cmdShow, NULL, cmdline, len ))) return 0;
462 if (!(*hThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)task_start, pTask, 0, NULL )))
463 {
464 TASK_DeleteTask( pTask->hSelf );
465 return 0;
466 }
467 return pTask->hSelf;
468}
469
470
Alexandre Julliard594997c1995-04-30 10:05:20 +0000471/***********************************************************************
Andreas Mohrc2bc22b2001-05-24 18:44:25 +0000472 * TASK_ExitTask
Alexandre Julliard594997c1995-04-30 10:05:20 +0000473 */
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000474void TASK_ExitTask(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000475{
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000476 TDB *pTask;
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000477 DWORD lockCount;
Alexandre Julliard2ace16a1996-04-28 15:09:19 +0000478
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000479 /* Enter the Win16Lock to protect global data structures */
Alexandre Julliardab687972000-11-15 23:41:46 +0000480 _EnterWin16Lock();
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000481
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000482 pTask = TASK_GetCurrent();
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000483 if ( !pTask )
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000484 {
Alexandre Julliardab687972000-11-15 23:41:46 +0000485 _LeaveWin16Lock();
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000486 return;
487 }
488
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000489 TRACE("Killing task %04x\n", pTask->hSelf );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000490
Alexandre Julliard2ace16a1996-04-28 15:09:19 +0000491 /* Perform USER cleanup */
492
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000493 TASK_CallTaskSignalProc( USIG16_TERMINATION, pTask->hSelf );
Alexandre Julliard9a0e28f2000-03-25 19:14:37 +0000494 PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 );
495 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
496 PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 );
Ulrich Weigand8139c301999-04-01 11:43:05 +0000497
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000498 /* Remove the task from the list to be sure we never switch back to it */
499 TASK_UnlinkTask( pTask->hSelf );
500
501 if (!nTaskCount || (nTaskCount == 1 && hFirstTask == initial_task))
Alexandre Julliard594997c1995-04-30 10:05:20 +0000502 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000503 TRACE("this is the last task, exiting\n" );
Ulrich Weigandc319c661999-05-22 16:18:36 +0000504 ExitKernel16();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000505 }
506
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000507 if( nTaskCount )
508 {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000509 TDB* p = TASK_GetPtr( hFirstTask );
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000510 while( p )
511 {
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000512 if( p->hYieldTo == pTask->hSelf ) p->hYieldTo = 0;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000513 p = TASK_GetPtr( p->hNext );
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000514 }
515 }
516
Alexandre Julliardda0cfb31996-12-01 17:17:47 +0000517 pTask->nEvents = 0;
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000518
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000519 if ( hLockedTask == pTask->hSelf )
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000520 hLockedTask = 0;
521
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000522 TASK_DeleteTask( pTask->hSelf );
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000523
Alexandre Julliard4e951ea2000-11-08 22:47:53 +0000524 /* ... and completely release the Win16Lock, just in case. */
525 ReleaseThunkLock( &lockCount );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000526}
527
Alexandre Julliard2787be81995-05-22 18:23:01 +0000528
Alexandre Julliard2787be81995-05-22 18:23:01 +0000529/***********************************************************************
530 * InitTask (KERNEL.91)
Alexandre Julliard77b99181997-09-14 17:17:23 +0000531 *
532 * Called by the application startup code.
Alexandre Julliard2787be81995-05-22 18:23:01 +0000533 */
Alexandre Julliard617955d1999-06-26 18:40:24 +0000534void WINAPI InitTask16( CONTEXT86 *context )
Alexandre Julliard2787be81995-05-22 18:23:01 +0000535{
536 TDB *pTask;
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000537 INSTANCEDATA *pinstance;
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000538 SEGPTR ptr;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000539
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000540 context->Eax = 0;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000541 if (!(pTask = TASK_GetCurrent())) return;
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000542
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000543 /* Note: we need to trust that BX/CX contain the stack/heap sizes,
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000544 as some apps, notably Visual Basic apps, *modify* the heap/stack
545 size of the instance data segment before calling InitTask() */
546
547 /* Initialize the INSTANCEDATA structure */
Alexandre Julliard982a2232000-12-13 20:20:09 +0000548 pinstance = MapSL( MAKESEGPTR(CURRENT_DS, 0) );
Ulrich Weigandf2f09272000-05-19 03:40:22 +0000549 pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ) + sizeof( STACK16FRAME );
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000550 pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000551 pinstance->stacktop = ( pinstance->stackmin > LOWORD(context->Ebx) ?
552 pinstance->stackmin - LOWORD(context->Ebx) : 0 ) + 150;
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000553
554 /* Initialize the local heap */
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000555 if (LOWORD(context->Ecx))
556 LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, LOWORD(context->Ecx) );
Alexandre Julliard2787be81995-05-22 18:23:01 +0000557
Alexandre Julliard77b99181997-09-14 17:17:23 +0000558 /* Initialize implicitly loaded DLLs */
Alexandre Julliard2787be81995-05-22 18:23:01 +0000559 NE_InitializeDLLs( pTask->hModule );
Ulrich Weigand979000e1999-12-05 02:48:53 +0000560 NE_DllProcessAttach( pTask->hModule );
Alexandre Julliard2787be81995-05-22 18:23:01 +0000561
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000562 /* Registers on return are:
563 * ax 1 if OK, 0 on error
564 * cx stack limit in bytes
565 * dx cmdShow parameter
566 * si instance handle of the previous instance
567 * di instance handle of the new task
Andreas Mohr2caee712000-07-16 15:44:22 +0000568 * es:bx pointer to command line inside PSP
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000569 *
570 * 0 (=%bp) is pushed on the stack
571 */
Alexandre Julliarda1089c51999-08-01 12:51:55 +0000572 ptr = stack16_push( sizeof(WORD) );
Alexandre Julliard982a2232000-12-13 20:20:09 +0000573 *(WORD *)MapSL(ptr) = 0;
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000574 context->Esp -= 2;
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000575
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000576 context->Eax = 1;
577
578 if (!pTask->pdb.cmdLine[0]) context->Ebx = 0x80;
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000579 else
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000580 {
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000581 LPBYTE p = &pTask->pdb.cmdLine[1];
582 while ((*p == ' ') || (*p == '\t')) p++;
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000583 context->Ebx = 0x80 + (p - pTask->pdb.cmdLine);
Ulrich Weiganda3afeef1999-07-10 10:18:26 +0000584 }
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000585 context->Ecx = pinstance->stacktop;
586 context->Edx = pTask->nCmdShow;
587 context->Esi = (DWORD)pTask->hPrevInstance;
588 context->Edi = (DWORD)pTask->hInstance;
589 context->SegEs = (WORD)pTask->hPDB;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000590}
591
592
593/***********************************************************************
594 * WaitEvent (KERNEL.30)
595 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000596BOOL16 WINAPI WaitEvent16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000597{
598 TDB *pTask;
599
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000600 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000601 pTask = TASK_GetPtr( hTask );
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000602
Alexandre Julliarda67e6482001-05-18 23:21:22 +0000603 if (pTask->flags & TDBF_WIN32)
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000604 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000605 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000606 return TRUE;
607 }
608
Alexandre Julliard594997c1995-04-30 10:05:20 +0000609 if (pTask->nEvents > 0)
610 {
611 pTask->nEvents--;
612 return FALSE;
613 }
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000614
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000615 if (pTask->teb == NtCurrentTeb())
616 {
617 DWORD lockCount;
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000618
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000619 NtResetEvent( pTask->hEvent, NULL );
620 ReleaseThunkLock( &lockCount );
621 SYSLEVEL_CheckNotLevel( 1 );
622 WaitForSingleObject( pTask->hEvent, INFINITE );
623 RestoreThunkLock( lockCount );
624 if (pTask->nEvents > 0) pTask->nEvents--;
625 }
626 else FIXME("for other task %04x cur=%04x\n",pTask->hSelf,GetCurrentTask());
627
Alexandre Julliard594997c1995-04-30 10:05:20 +0000628 return TRUE;
629}
630
631
632/***********************************************************************
633 * PostEvent (KERNEL.31)
634 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000635void WINAPI PostEvent16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000636{
637 TDB *pTask;
638
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000639 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000640 if (!(pTask = TASK_GetPtr( hTask ))) return;
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000641
Alexandre Julliarda67e6482001-05-18 23:21:22 +0000642 if (pTask->flags & TDBF_WIN32)
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000643 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000644 FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel );
Ulrich Weigand93b23d71999-03-22 10:35:44 +0000645 return;
646 }
647
Stephane Lussierb50272b1999-02-21 18:15:26 +0000648 pTask->nEvents++;
Ulrich Weiganded490031999-06-06 14:47:50 +0000649
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000650 if (pTask->nEvents == 1) NtSetEvent( pTask->hEvent, NULL );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000651}
652
653
654/***********************************************************************
655 * SetPriority (KERNEL.32)
656 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000657void WINAPI SetPriority16( HTASK16 hTask, INT16 delta )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000658{
659 TDB *pTask;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +0000660 INT16 newpriority;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000661
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000662 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000663 if (!(pTask = TASK_GetPtr( hTask ))) return;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000664 newpriority = pTask->priority + delta;
665 if (newpriority < -32) newpriority = -32;
666 else if (newpriority > 15) newpriority = 15;
667
668 pTask->priority = newpriority + 1;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000669 TASK_UnlinkTask( pTask->hSelf );
670 TASK_LinkTask( pTask->hSelf );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000671 pTask->priority--;
672}
673
674
675/***********************************************************************
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000676 * LockCurrentTask (KERNEL.33)
677 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000678HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock )
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000679{
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000680 if (bLock) hLockedTask = GetCurrentTask();
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000681 else hLockedTask = 0;
682 return hLockedTask;
683}
684
685
686/***********************************************************************
687 * IsTaskLocked (KERNEL.122)
688 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000689HTASK16 WINAPI IsTaskLocked16(void)
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000690{
691 return hLockedTask;
692}
693
694
695/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +0000696 * OldYield (KERNEL.117)
697 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000698void WINAPI OldYield16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000699{
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000700 DWORD count;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000701
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000702 ReleaseThunkLock(&count);
703 RestoreThunkLock(count);
Alexandre Julliard594997c1995-04-30 10:05:20 +0000704}
705
Ulrich Weigand081daf71999-07-31 17:27:38 +0000706/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000707 * WIN32_OldYield (KERNEL.447)
Ulrich Weigand081daf71999-07-31 17:27:38 +0000708 */
709void WINAPI WIN32_OldYield16(void)
710{
711 DWORD count;
712
713 ReleaseThunkLock(&count);
714 RestoreThunkLock(count);
715}
Alexandre Julliard594997c1995-04-30 10:05:20 +0000716
717/***********************************************************************
718 * DirectedYield (KERNEL.150)
719 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000720void WINAPI DirectedYield16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000721{
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000722 TDB *pCurTask = TASK_GetCurrent();
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000723
Alexandre Julliarda67e6482001-05-18 23:21:22 +0000724 if (pCurTask->flags & TDBF_WIN32)
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000725 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000726 FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000727 return;
728 }
729
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000730 TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
Alex Korobka92c81781998-10-18 10:47:03 +0000731
Alexandre Julliardef702d81996-05-28 18:54:58 +0000732 pCurTask->hYieldTo = hTask;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000733 OldYield16();
Alex Korobka92c81781998-10-18 10:47:03 +0000734
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000735 TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask );
Alexandre Julliardef702d81996-05-28 18:54:58 +0000736}
Alexandre Julliard594997c1995-04-30 10:05:20 +0000737
Alexandre Julliard594997c1995-04-30 10:05:20 +0000738/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000739 * Yield (KERNEL.29)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000740 */
Alexandre Julliarde658d821997-11-30 17:45:40 +0000741void WINAPI Yield16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000742{
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000743 TDB *pCurTask = TASK_GetCurrent();
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000744
Alexandre Julliardef702d81996-05-28 18:54:58 +0000745 if (pCurTask) pCurTask->hYieldTo = 0;
Alexandre Julliard5edf4e12001-07-26 20:12:54 +0000746 if (pCurTask && pCurTask->hQueue)
747 {
748 HMODULE mod = GetModuleHandleA( "user32.dll" );
749 if (mod)
750 {
751 FARPROC proc = GetProcAddress( mod, "UserYield16" );
752 if (proc)
753 {
754 proc();
755 return;
756 }
757 }
758 }
759 OldYield16();
Alexandre Julliard594997c1995-04-30 10:05:20 +0000760}
761
Ulrich Weigandb9eab151998-11-26 14:58:11 +0000762/***********************************************************************
763 * KERNEL_490 (KERNEL.490)
764 */
765HTASK16 WINAPI KERNEL_490( HTASK16 someTask )
766{
767 if ( !someTask ) return 0;
768
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000769 FIXME("(%04x): stub\n", someTask );
Ulrich Weigandb9eab151998-11-26 14:58:11 +0000770 return 0;
771}
Alexandre Julliard594997c1995-04-30 10:05:20 +0000772
773/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000774 * MakeProcInstance (KERNEL.51)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000775 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000776FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000777{
Alexandre Julliarde658d821997-11-30 17:45:40 +0000778 BYTE *thunk,*lfunc;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000779 SEGPTR thunkaddr;
Bernd Herd4d057612000-05-11 00:10:36 +0000780 WORD hInstanceSelector;
781
782 hInstanceSelector = GlobalHandleToSel16(hInstance);
Alexandre Julliard54c27111998-03-29 19:44:57 +0000783
Alexandre Julliardc192ba22000-05-29 21:25:10 +0000784 TRACE("(%08lx, %04x);\n", (DWORD)func, hInstance);
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000785
786 if (!HIWORD(func)) {
787 /* Win95 actually protects via SEH, but this is better for debugging */
Andreas Mohr62f3eaa2000-08-21 20:16:26 +0000788 WARN("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func);
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000789 return (FARPROC16)0;
Alexandre Julliarda0d77311998-09-13 16:32:00 +0000790 }
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000791
792 if (hInstance)
Uwe Bonnesf959be91999-10-23 19:47:55 +0000793 {
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000794 if ( (!(hInstance & 4)) ||
795 ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) )
796 {
Andreas Mohr62f3eaa2000-08-21 20:16:26 +0000797 WARN("Invalid hInstance (%04x) passed to MakeProcInstance !\n",
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000798 hInstance);
799 return 0;
800 }
Uwe Bonnesf959be91999-10-23 19:47:55 +0000801 }
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000802
Andreas Mohr62f3eaa2000-08-21 20:16:26 +0000803 if ( (GlobalHandleToSel16(CURRENT_DS) != hInstanceSelector)
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000804 && (hInstance != 0)
805 && (hInstance != 0xffff) )
806 {
807 /* calling MPI with a foreign DSEG is invalid ! */
Andreas Mohr62f3eaa2000-08-21 20:16:26 +0000808 WARN("Problem with hInstance? Got %04x, using %04x instead\n",
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000809 hInstance,CURRENT_DS);
810 }
811
812 /* Always use the DSEG that MPI was entered with.
813 * We used to set hInstance to GetTaskDS16(), but this should be wrong
814 * as CURRENT_DS provides the DSEG value we need.
815 * ("calling" DS, *not* "task" DS !) */
Bernd Herd4d057612000-05-11 00:10:36 +0000816 hInstanceSelector = CURRENT_DS;
817 hInstance = GlobalHandle16(hInstanceSelector);
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000818
819 /* no thunking for DLLs */
820 if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE)
821 return func;
822
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000823 thunkaddr = TASK_AllocThunk();
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000824 if (!thunkaddr) return (FARPROC16)0;
Alexandre Julliard982a2232000-12-13 20:20:09 +0000825 thunk = MapSL( thunkaddr );
826 lfunc = MapSL( (SEGPTR)func );
Alexandre Julliard594997c1995-04-30 10:05:20 +0000827
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000828 TRACE("(%08lx,%04x): got thunk %08lx\n",
829 (DWORD)func, hInstance, (DWORD)thunkaddr );
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000830 if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */
831 ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */
Alexandre Julliarde658d821997-11-30 17:45:40 +0000832 ) {
Andreas Mohr62f3eaa2000-08-21 20:16:26 +0000833 WARN("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 +0000834 }
Bernd Herd4d057612000-05-11 00:10:36 +0000835
Alexandre Julliard594997c1995-04-30 10:05:20 +0000836 *thunk++ = 0xb8; /* movw instance, %ax */
Bernd Herd4d057612000-05-11 00:10:36 +0000837 *thunk++ = (BYTE)(hInstanceSelector & 0xff);
838 *thunk++ = (BYTE)(hInstanceSelector >> 8);
Alexandre Julliard594997c1995-04-30 10:05:20 +0000839 *thunk++ = 0xea; /* ljmp func */
840 *(DWORD *)thunk = (DWORD)func;
Alexandre Julliard18f92e71996-07-17 20:02:21 +0000841 return (FARPROC16)thunkaddr;
Andreas Mohr45b5bcf2000-03-07 12:22:35 +0000842 /* CX reg indicates if thunkaddr != NULL, implement if needed */
Alexandre Julliard594997c1995-04-30 10:05:20 +0000843}
844
845
846/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000847 * FreeProcInstance (KERNEL.52)
Alexandre Julliard594997c1995-04-30 10:05:20 +0000848 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000849void WINAPI FreeProcInstance16( FARPROC16 func )
Alexandre Julliard594997c1995-04-30 10:05:20 +0000850{
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000851 TRACE("(%08lx)\n", (DWORD)func );
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000852 TASK_FreeThunk( (SEGPTR)func );
Alexandre Julliard58199531994-04-21 01:20:00 +0000853}
854
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000855/**********************************************************************
856 * TASK_GetCodeSegment
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000857 *
858 * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000859 * and logical segment number of a given code segment.
860 *
861 * 'proc' either *is* already a pair of module handle and segment number,
862 * in which case there's nothing to do. Otherwise, it is a pointer to
863 * a function, and we need to retrieve the code segment. If the pointer
864 * happens to point to a thunk, we'll retrieve info about the code segment
865 * where the function pointed to by the thunk resides, not the thunk itself.
866 *
867 * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about
868 * the function the snoop code will return to ...
869 *
870 */
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000871static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule,
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000872 SEGTABLEENTRY **ppSeg, int *pSegNr )
873{
874 NE_MODULE *pModule = NULL;
875 SEGTABLEENTRY *pSeg = NULL;
Dimitrie O. Paun334a42f2000-10-29 18:07:04 +0000876 int segNr=0;
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000877
878 /* Try pair of module handle / segment number */
879 pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) );
880 if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE )
881 {
882 segNr = LOWORD( proc );
883 if ( segNr && segNr <= pModule->seg_count )
884 pSeg = NE_SEG_TABLE( pModule ) + segNr-1;
885 }
886
887 /* Try thunk or function */
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000888 else
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000889 {
Alexandre Julliard982a2232000-12-13 20:20:09 +0000890 BYTE *thunk = MapSL( (SEGPTR)proc );
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000891 WORD selector;
892
893 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
894 selector = thunk[6] + (thunk[7] << 8);
895 else
896 selector = HIWORD( proc );
897
898 pModule = NE_GetPtr( GlobalHandle16( selector ) );
899 pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL;
900
901 if ( pModule )
902 for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ )
903 if ( GlobalHandleToSel16(pSeg->hSeg) == selector )
904 break;
905
906 if ( pModule && segNr > pModule->seg_count )
907 pSeg = NULL;
908 }
909
910 /* Abort if segment not found */
911
912 if ( !pModule || !pSeg )
913 return FALSE;
914
915 /* Return segment data */
916
917 if ( ppModule ) *ppModule = pModule;
918 if ( ppSeg ) *ppSeg = pSeg;
919 if ( pSegNr ) *pSegNr = segNr;
920
921 return TRUE;
922}
Alexandre Julliard58199531994-04-21 01:20:00 +0000923
924/**********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +0000925 * GetCodeHandle (KERNEL.93)
Alexandre Julliard58199531994-04-21 01:20:00 +0000926 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000927HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc )
Alexandre Julliard58199531994-04-21 01:20:00 +0000928{
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000929 SEGTABLEENTRY *pSeg;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000930
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000931 if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) )
932 return (HANDLE16)0;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000933
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000934 return pSeg->hSeg;
Alexandre Julliard58199531994-04-21 01:20:00 +0000935}
936
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000937/**********************************************************************
938 * GetCodeInfo (KERNEL.104)
939 */
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000940BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000941{
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000942 NE_MODULE *pModule;
943 SEGTABLEENTRY *pSeg;
944 int segNr;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000945
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000946 if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) )
947 return FALSE;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000948
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000949 /* Fill in segment information */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000950
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000951 segInfo->offSegment = pSeg->filepos;
952 segInfo->cbSegment = pSeg->size;
953 segInfo->flags = pSeg->flags;
954 segInfo->cbAlloc = pSeg->minsize;
955 segInfo->h = pSeg->hSeg;
956 segInfo->alignShift = pModule->alignment;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000957
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000958 if ( segNr == pModule->dgroup )
959 segInfo->cbAlloc += pModule->heap_size + pModule->stack_size;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000960
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000961 /* Return module handle in %es */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000962
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000963 CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000964
Ulrich Weiganda2d5bb31999-09-20 15:30:27 +0000965 return TRUE;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000966}
967
Alexandre Julliard594997c1995-04-30 10:05:20 +0000968
Alexandre Julliarde658d821997-11-30 17:45:40 +0000969/**********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000970 * DefineHandleTable (KERNEL.94)
Alexandre Julliarde658d821997-11-30 17:45:40 +0000971 */
972BOOL16 WINAPI DefineHandleTable16( WORD wOffset )
973{
Andreas Mohr2caee712000-07-16 15:44:22 +0000974 FIXME("(%04x): stub ?\n", wOffset);
975 return TRUE;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000976}
977
978
Alexandre Julliard594997c1995-04-30 10:05:20 +0000979/***********************************************************************
980 * SetTaskQueue (KERNEL.34)
981 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000982HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue )
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +0000983{
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000984 HQUEUE16 hPrev;
Alexandre Julliard594997c1995-04-30 10:05:20 +0000985 TDB *pTask;
986
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000987 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard3e7497f2001-04-04 00:19:55 +0000988 if (!(pTask = TASK_GetPtr( hTask ))) return 0;
Alexandre Julliardc981d0b1996-03-31 16:40:13 +0000989
Alexandre Julliard594997c1995-04-30 10:05:20 +0000990 hPrev = pTask->hQueue;
991 pTask->hQueue = hQueue;
Alexandre Julliardc981d0b1996-03-31 16:40:13 +0000992
Alexandre Julliard594997c1995-04-30 10:05:20 +0000993 return hPrev;
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +0000994}
995
Alexandre Julliard594997c1995-04-30 10:05:20 +0000996
997/***********************************************************************
998 * GetTaskQueue (KERNEL.35)
999 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001000HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask )
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001001{
Alexandre Julliard594997c1995-04-30 10:05:20 +00001002 TDB *pTask;
1003
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001004 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001005 if (!(pTask = TASK_GetPtr( hTask ))) return 0;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001006 return pTask->hQueue;
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001007}
1008
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001009/***********************************************************************
1010 * SetThreadQueue (KERNEL.463)
1011 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001012HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001013{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001014 TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb();
1015 HQUEUE16 oldQueue = teb? teb->queue : 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001016
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001017 if ( teb )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001018 {
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001019 teb->queue = hQueue;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001020
Alexandre Julliardc192ba22000-05-29 21:25:10 +00001021 if ( GetTaskQueue16( teb->htask16 ) == oldQueue )
1022 SetTaskQueue16( teb->htask16, hQueue );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001023 }
1024
1025 return oldQueue;
1026}
1027
1028/***********************************************************************
1029 * GetThreadQueue (KERNEL.464)
1030 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001031HQUEUE16 WINAPI GetThreadQueue16( DWORD thread )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001032{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001033 TEB *teb = NULL;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001034 if ( !thread )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001035 teb = NtCurrentTeb();
Ulrich Weigand901ca361999-01-20 13:35:34 +00001036 else if ( HIWORD(thread) )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001037 teb = THREAD_IdToTEB( thread );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001038 else if ( IsTask16( (HTASK16)thread ) )
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001039 teb = (TASK_GetPtr( (HANDLE16)thread ))->teb;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001040
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001041 return (HQUEUE16)(teb? teb->queue : 0);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001042}
1043
1044/***********************************************************************
1045 * SetFastQueue (KERNEL.624)
1046 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001047VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001048{
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001049 TEB *teb = NULL;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001050 if ( !thread )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001051 teb = NtCurrentTeb();
Ulrich Weigand901ca361999-01-20 13:35:34 +00001052 else if ( HIWORD(thread) )
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001053 teb = THREAD_IdToTEB( thread );
Alexandre Julliarda3960291999-02-26 11:11:13 +00001054 else if ( IsTask16( (HTASK16)thread ) )
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001055 teb = (TASK_GetPtr( (HANDLE16)thread ))->teb;
Ulrich Weigand901ca361999-01-20 13:35:34 +00001056
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001057 if ( teb ) teb->queue = (HQUEUE16) hQueue;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001058}
1059
1060/***********************************************************************
1061 * GetFastQueue (KERNEL.625)
1062 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001063HANDLE WINAPI GetFastQueue16( void )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001064{
Alexandre Julliard5edf4e12001-07-26 20:12:54 +00001065 HANDLE ret = (HANDLE)NtCurrentTeb()->queue;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001066
Alexandre Julliard5edf4e12001-07-26 20:12:54 +00001067 if (!ret) FIXME("(): should initialize thread-local queue, expect failure!\n" );
1068 return ret;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001069}
Alexandre Julliard594997c1995-04-30 10:05:20 +00001070
1071/***********************************************************************
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001072 * SwitchStackTo (KERNEL.108)
1073 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001074void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top )
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001075{
1076 TDB *pTask;
1077 STACK16FRAME *oldFrame, *newFrame;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001078 INSTANCEDATA *pData;
1079 UINT16 copySize;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001080
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001081 if (!(pTask = TASK_GetCurrent())) return;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001082 if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return;
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001083 TRACE("old=%04x:%04x new=%04x:%04x\n",
1084 SELECTOROF( pTask->teb->cur_stack ),
1085 OFFSETOF( pTask->teb->cur_stack ), seg, ptr );
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001086
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001087 /* Save the old stack */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001088
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001089 oldFrame = THREAD_STACK16( pTask->teb );
Alexandre Julliarda845b881998-06-01 10:44:35 +00001090 /* pop frame + args and push bp */
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001091 pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME)
Alexandre Julliard75796ea1998-10-25 08:05:46 +00001092 + 2 * sizeof(WORD);
Alexandre Julliard982a2232000-12-13 20:20:09 +00001093 *(WORD *)MapSL(pData->old_ss_sp) = oldFrame->bp;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001094 pData->stacktop = top;
1095 pData->stackmin = ptr;
1096 pData->stackbottom = ptr;
1097
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001098 /* Switch to the new stack */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001099
Alexandre Julliard17216f51997-10-12 16:30:17 +00001100 /* Note: we need to take the 3 arguments into account; otherwise,
1101 * the stack will underflow upon return from this function.
1102 */
Alexandre Julliarda845b881998-06-01 10:44:35 +00001103 copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp);
1104 copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME);
Alexandre Julliard982a2232000-12-13 20:20:09 +00001105 pTask->teb->cur_stack = MAKESEGPTR( seg, ptr - copySize );
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001106 newFrame = THREAD_STACK16( pTask->teb );
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001107
1108 /* Copy the stack frame and the local variables to the new stack */
1109
Alexandre Julliarda845b881998-06-01 10:44:35 +00001110 memmove( newFrame, oldFrame, copySize );
1111 newFrame->bp = ptr;
Alexandre Julliard982a2232000-12-13 20:20:09 +00001112 *(WORD *)MapSL( MAKESEGPTR( seg, ptr ) ) = 0; /* clear previous bp */
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001113}
1114
1115
1116/***********************************************************************
1117 * SwitchStackBack (KERNEL.109)
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001118 */
Alexandre Julliard617955d1999-06-26 18:40:24 +00001119void WINAPI SwitchStackBack16( CONTEXT86 *context )
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001120{
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001121 STACK16FRAME *oldFrame, *newFrame;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001122 INSTANCEDATA *pData;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001123
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001124 if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack))))
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001125 return;
1126 if (!pData->old_ss_sp)
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001127 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001128 WARN("No previous SwitchStackTo\n" );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001129 return;
1130 }
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001131 TRACE("restoring stack %04x:%04x\n",
1132 SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) );
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001133
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001134 oldFrame = CURRENT_STACK16;
Alexandre Julliarda845b881998-06-01 10:44:35 +00001135
1136 /* Pop bp from the previous stack */
1137
Alexandre Julliard3fa613c2002-08-31 18:47:00 +00001138 context->Ebp = (context->Ebp & ~0xffff) | *(WORD *)MapSL(pData->old_ss_sp);
Alexandre Julliarda845b881998-06-01 10:44:35 +00001139 pData->old_ss_sp += sizeof(WORD);
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001140
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001141 /* Switch back to the old stack */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001142
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001143 NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME);
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +00001144 context->SegSs = SELECTOROF(pData->old_ss_sp);
1145 context->Esp = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/
Alexandre Julliardd37eb361997-07-20 16:23:21 +00001146 pData->old_ss_sp = 0;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001147
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001148 /* Build a stack frame for the return */
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001149
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001150 newFrame = CURRENT_STACK16;
Ulrich Weigand3df3e531999-08-18 18:24:08 +00001151 newFrame->frame32 = oldFrame->frame32;
1152 newFrame->module_cs = oldFrame->module_cs;
1153 newFrame->callfrom_ip = oldFrame->callfrom_ip;
1154 newFrame->entry_ip = oldFrame->entry_ip;
Alexandre Julliardac9c9b01996-07-28 18:50:11 +00001155}
1156
1157
1158/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001159 * GetTaskQueueDS (KERNEL.118)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001160 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001161void WINAPI GetTaskQueueDS16(void)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001162{
Alexandre Julliard4220b291999-07-11 17:20:01 +00001163 CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) );
Alexandre Julliardade697e1995-11-26 13:59:11 +00001164}
Alexandre Julliardade697e1995-11-26 13:59:11 +00001165
1166
1167/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001168 * GetTaskQueueES (KERNEL.119)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001169 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001170void WINAPI GetTaskQueueES16(void)
Alexandre Julliardade697e1995-11-26 13:59:11 +00001171{
Alexandre Julliard4220b291999-07-11 17:20:01 +00001172 CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) );
Alexandre Julliardade697e1995-11-26 13:59:11 +00001173}
Alexandre Julliardade697e1995-11-26 13:59:11 +00001174
1175
1176/***********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +00001177 * GetCurrentTask (KERNEL32.@)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001178 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001179HTASK16 WINAPI GetCurrentTask(void)
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001180{
Alexandre Julliardc192ba22000-05-29 21:25:10 +00001181 return NtCurrentTeb()->htask16;
Alexandre Julliard2d93d001996-05-21 15:01:41 +00001182}
1183
Patrik Stridvall044855c2001-07-11 18:56:41 +00001184/***********************************************************************
1185 * GetCurrentTask (KERNEL.36)
1186 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001187DWORD WINAPI WIN16_GetCurrentTask(void)
Alexandre Julliard2d93d001996-05-21 15:01:41 +00001188{
1189 /* This is the version used by relay code; the first task is */
1190 /* returned in the high word of the result */
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001191 return MAKELONG( GetCurrentTask(), hFirstTask );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001192}
1193
1194
1195/***********************************************************************
1196 * GetCurrentPDB (KERNEL.37)
Andreas Mohra00b49f1998-12-07 10:48:09 +00001197 *
1198 * UNDOC: returns PSP of KERNEL in high word
Alexandre Julliard594997c1995-04-30 10:05:20 +00001199 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001200DWORD WINAPI GetCurrentPDB16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001201{
1202 TDB *pTask;
1203
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001204 if (!(pTask = TASK_GetCurrent())) return 0;
Andreas Mohra00b49f1998-12-07 10:48:09 +00001205 return MAKELONG(pTask->hPDB, 0); /* FIXME */
Alexandre Julliard594997c1995-04-30 10:05:20 +00001206}
1207
1208
1209/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001210 * GetCurPID (KERNEL.157)
Alexandre Julliard4220b291999-07-11 17:20:01 +00001211 */
1212DWORD WINAPI GetCurPID16( DWORD unused )
1213{
1214 return 0;
1215}
1216
1217
1218/***********************************************************************
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001219 * GetInstanceData (KERNEL.54)
1220 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001221INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len )
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001222{
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001223 char *ptr = (char *)GlobalLock16( instance );
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001224 if (!ptr || !len) return 0;
1225 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
Alexandre Julliard18f92e71996-07-17 20:02:21 +00001226 memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len );
Alexandre Julliarda2f2e011995-06-06 16:40:35 +00001227 return len;
1228}
1229
1230
1231/***********************************************************************
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001232 * GetExeVersion (KERNEL.105)
1233 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001234WORD WINAPI GetExeVersion16(void)
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001235{
1236 TDB *pTask;
1237
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001238 if (!(pTask = TASK_GetCurrent())) return 0;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001239 return pTask->version;
1240}
1241
1242
1243/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001244 * SetErrorMode (KERNEL.107)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001245 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001246UINT16 WINAPI SetErrorMode16( UINT16 mode )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001247{
1248 TDB *pTask;
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001249 if (!(pTask = TASK_GetCurrent())) return 0;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001250 pTask->error_mode = mode;
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001251 return SetErrorMode( mode );
Alexandre Julliard349a9531997-02-02 19:01:52 +00001252}
1253
1254
1255/***********************************************************************
Alexandre Julliard594997c1995-04-30 10:05:20 +00001256 * GetNumTasks (KERNEL.152)
1257 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001258UINT16 WINAPI GetNumTasks16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001259{
1260 return nTaskCount;
1261}
1262
1263
1264/***********************************************************************
1265 * GetTaskDS (KERNEL.155)
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001266 *
1267 * Note: this function apparently returns a DWORD with LOWORD == HIWORD.
1268 * I don't think we need to bother with this.
Alexandre Julliard594997c1995-04-30 10:05:20 +00001269 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001270HINSTANCE16 WINAPI GetTaskDS16(void)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001271{
1272 TDB *pTask;
1273
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001274 if (!(pTask = TASK_GetCurrent())) return 0;
Bernd Herd4d057612000-05-11 00:10:36 +00001275 return GlobalHandleToSel16(pTask->hInstance);
Alexandre Julliard594997c1995-04-30 10:05:20 +00001276}
1277
Ulrich Weigandb9eab151998-11-26 14:58:11 +00001278/***********************************************************************
1279 * GetDummyModuleHandleDS (KERNEL.602)
1280 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001281WORD WINAPI GetDummyModuleHandleDS16(void)
Ulrich Weigandb9eab151998-11-26 14:58:11 +00001282{
1283 TDB *pTask;
1284 WORD selector;
1285
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001286 if (!(pTask = TASK_GetCurrent())) return 0;
Alexandre Julliard4220b291999-07-11 17:20:01 +00001287 if (!(pTask->flags & TDBF_WIN32)) return 0;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001288 selector = GlobalHandleToSel16( pTask->hModule );
Alexandre Julliard4220b291999-07-11 17:20:01 +00001289 CURRENT_DS = selector;
1290 return selector;
Ulrich Weigandb9eab151998-11-26 14:58:11 +00001291}
Alexandre Julliard594997c1995-04-30 10:05:20 +00001292
1293/***********************************************************************
1294 * IsTask (KERNEL.320)
1295 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001296BOOL16 WINAPI IsTask16( HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001297{
1298 TDB *pTask;
1299
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001300 if (!(pTask = TASK_GetPtr( hTask ))) return FALSE;
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001301 if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001302 return (pTask->magic == TDB_MAGIC);
1303}
1304
1305
1306/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001307 * IsWinOldApTask (KERNEL.158)
Alexandre Julliard4220b291999-07-11 17:20:01 +00001308 */
1309BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask )
1310{
1311 /* should return bit 0 of byte 0x48 in PSP */
1312 return FALSE;
1313}
1314
1315/***********************************************************************
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001316 * SetTaskSignalProc (KERNEL.38)
1317 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001318FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001319{
1320 TDB *pTask;
1321 FARPROC16 oldProc;
1322
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001323 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001324 if (!(pTask = TASK_GetPtr( hTask ))) return NULL;
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +00001325 oldProc = pTask->userhandler;
1326 pTask->userhandler = proc;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001327 return oldProc;
1328}
1329
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +00001330/***********************************************************************
1331 * TASK_CallTaskSignalProc
1332 */
1333/* ### start build ### */
1334extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
1335/* ### stop build ### */
1336void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule )
1337{
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001338 TDB *pTask = TASK_GetCurrent();
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +00001339 if ( !pTask || !pTask->userhandler ) return;
1340
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00001341 TASK_CallTo16_word_wwwww( pTask->userhandler,
1342 hTaskOrModule, uCode, 0,
Ulrich Weigandc44ab1f1999-09-20 18:48:29 +00001343 pTask->hInstance, pTask->hQueue );
1344}
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001345
1346/***********************************************************************
1347 * SetSigHandler (KERNEL.140)
1348 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001349WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler,
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001350 UINT16 *oldmode, UINT16 newmode, UINT16 flag )
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001351{
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +00001352 FIXME("(%p,%p,%p,%d,%d), unimplemented.\n",
Alexandre Julliard54c27111998-03-29 19:44:57 +00001353 newhandler,oldhandler,oldmode,newmode,flag );
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001354
1355 if (flag != 1) return 0;
1356 if (!newmode) newhandler = NULL; /* Default handler */
1357 if (newmode != 4)
1358 {
1359 TDB *pTask;
1360
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001361 if (!(pTask = TASK_GetCurrent())) return 0;
Alexandre Julliardf1aa3031996-08-05 17:42:43 +00001362 if (oldmode) *oldmode = pTask->signal_flags;
1363 pTask->signal_flags = newmode;
1364 if (oldhandler) *oldhandler = pTask->sighandler;
1365 pTask->sighandler = newhandler;
1366 }
1367 return 0;
1368}
1369
1370
1371/***********************************************************************
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001372 * GlobalNotify (KERNEL.154)
Andreas Mohra00b49f1998-12-07 10:48:09 +00001373 *
1374 * Note that GlobalNotify does _not_ return the old NotifyProc
1375 * -- contrary to LocalNotify !!
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001376 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001377VOID WINAPI GlobalNotify16( FARPROC16 proc )
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001378{
1379 TDB *pTask;
1380
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001381 if (!(pTask = TASK_GetCurrent())) return;
Alexandre Julliardbf9130a1996-10-13 17:45:47 +00001382 pTask->discardhandler = proc;
1383}
1384
1385
1386/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001387 * GetExePtrHelper
Alexandre Julliard594997c1995-04-30 10:05:20 +00001388 */
Alexandre Julliard4220b291999-07-11 17:20:01 +00001389static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001390{
1391 char *ptr;
Alexandre Julliard18f92e71996-07-17 20:02:21 +00001392 HANDLE16 owner;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001393
1394 /* Check for module handle */
1395
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001396 if (!(ptr = GlobalLock16( handle ))) return 0;
Alexandre Julliard23946ad1997-06-16 17:43:53 +00001397 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001398
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001399 /* Search for this handle inside all tasks */
1400
1401 *hTask = hFirstTask;
1402 while (*hTask)
1403 {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001404 TDB *pTask = TASK_GetPtr( *hTask );
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001405 if ((*hTask == handle) ||
1406 (pTask->hInstance == handle) ||
1407 (pTask->hQueue == handle) ||
1408 (pTask->hPDB == handle)) return pTask->hModule;
1409 *hTask = pTask->hNext;
1410 }
1411
Alexandre Julliard594997c1995-04-30 10:05:20 +00001412 /* Check the owner for module handle */
1413
Alexandre Julliarda3960291999-02-26 11:11:13 +00001414 owner = FarGetOwner16( handle );
Alexandre Julliard1285c2f1996-05-06 16:06:24 +00001415 if (!(ptr = GlobalLock16( owner ))) return 0;
Alexandre Julliard23946ad1997-06-16 17:43:53 +00001416 if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001417
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001418 /* Search for the owner inside all tasks */
Alexandre Julliard594997c1995-04-30 10:05:20 +00001419
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001420 *hTask = hFirstTask;
1421 while (*hTask)
Alexandre Julliard594997c1995-04-30 10:05:20 +00001422 {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001423 TDB *pTask = TASK_GetPtr( *hTask );
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001424 if ((*hTask == owner) ||
Alexandre Julliard594997c1995-04-30 10:05:20 +00001425 (pTask->hInstance == owner) ||
1426 (pTask->hQueue == owner) ||
1427 (pTask->hPDB == owner)) return pTask->hModule;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001428 *hTask = pTask->hNext;
Alexandre Julliard594997c1995-04-30 10:05:20 +00001429 }
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001430
Alexandre Julliard594997c1995-04-30 10:05:20 +00001431 return 0;
1432}
1433
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001434/***********************************************************************
1435 * GetExePtr (KERNEL.133)
1436 */
Ulrich Weigandcfa977d1999-07-12 13:39:10 +00001437HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle )
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001438{
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001439 HTASK16 hTask = 0;
Alexandre Julliard4220b291999-07-11 17:20:01 +00001440 HMODULE16 hModule = GetExePtrHelper( handle, &hTask );
Ulrich Weigandcfa977d1999-07-12 13:39:10 +00001441 STACK16FRAME *frame = CURRENT_STACK16;
1442 frame->ecx = hModule;
1443 if (hTask) frame->es = hTask;
Alexandre Julliard4220b291999-07-11 17:20:01 +00001444 return hModule;
Alexandre Julliardebfc0fe1998-06-28 18:40:26 +00001445}
1446
Patrik Stridvall2b3aa612000-12-01 23:58:28 +00001447
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001448/***********************************************************************
1449 * K228 (KERNEL.228)
1450 */
Ulrich Weigandcfa977d1999-07-12 13:39:10 +00001451HMODULE16 WINAPI GetExePtr( HANDLE16 handle )
1452{
1453 HTASK16 hTask = 0;
1454 return GetExePtrHelper( handle, &hTask );
1455}
1456
1457
Alexandre Julliard594997c1995-04-30 10:05:20 +00001458/***********************************************************************
1459 * TaskFirst (TOOLHELP.63)
1460 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001461BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001462{
1463 lpte->hNext = hFirstTask;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001464 return TaskNext16( lpte );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001465}
1466
1467
1468/***********************************************************************
1469 * TaskNext (TOOLHELP.64)
1470 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001471BOOL16 WINAPI TaskNext16( TASKENTRY *lpte )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001472{
1473 TDB *pTask;
1474 INSTANCEDATA *pInstData;
1475
Alexandre Julliarde8c6a081999-05-02 14:33:41 +00001476 TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001477 if (!lpte->hNext) return FALSE;
Andreas Mohr6a2a2eb2000-10-02 22:16:34 +00001478
1479 /* make sure that task and hInstance are valid (skip initial Wine task !) */
1480 while (1) {
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001481 pTask = TASK_GetPtr( lpte->hNext );
Andreas Mohr6a2a2eb2000-10-02 22:16:34 +00001482 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1483 if (pTask->hInstance)
1484 break;
1485 lpte->hNext = pTask->hNext;
1486 }
Alexandre Julliard982a2232000-12-13 20:20:09 +00001487 pInstData = MapSL( MAKESEGPTR( GlobalHandleToSel16(pTask->hInstance), 0 ) );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001488 lpte->hTask = lpte->hNext;
1489 lpte->hTaskParent = pTask->hParent;
1490 lpte->hInst = pTask->hInstance;
1491 lpte->hModule = pTask->hModule;
Alexandre Julliard0a860a01999-06-22 11:43:42 +00001492 lpte->wSS = SELECTOROF( pTask->teb->cur_stack );
1493 lpte->wSP = OFFSETOF( pTask->teb->cur_stack );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001494 lpte->wStackTop = pInstData->stacktop;
1495 lpte->wStackMinimum = pInstData->stackmin;
1496 lpte->wStackBottom = pInstData->stackbottom;
1497 lpte->wcEvents = pTask->nEvents;
1498 lpte->hQueue = pTask->hQueue;
Francois Gougetbaa9bf91999-12-27 05:24:06 +00001499 lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) );
Alexandre Julliard594997c1995-04-30 10:05:20 +00001500 lpte->wPSPOffset = 0x100; /*??*/
1501 lpte->hNext = pTask->hNext;
1502 return TRUE;
1503}
1504
1505
1506/***********************************************************************
1507 * TaskFindHandle (TOOLHELP.65)
1508 */
Alexandre Julliarda3960291999-02-26 11:11:13 +00001509BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask )
Alexandre Julliard594997c1995-04-30 10:05:20 +00001510{
1511 lpte->hNext = hTask;
Alexandre Julliarda3960291999-02-26 11:11:13 +00001512 return TaskNext16( lpte );
Alexandre Julliard3f2abfa1994-08-16 15:43:11 +00001513}
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001514
1515
Alexandre Julliardea2a9a82001-02-20 00:55:17 +00001516typedef INT (WINAPI *MessageBoxA_funcptr)(HWND hWnd, LPCSTR text, LPCSTR title, UINT type);
1517
Andreas Mohrfc2c5df2000-11-27 01:36:24 +00001518/**************************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001519 * FatalAppExit (KERNEL.137)
Andreas Mohrfc2c5df2000-11-27 01:36:24 +00001520 */
1521void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
1522{
Alexandre Julliard3e7497f2001-04-04 00:19:55 +00001523 TDB *pTask = TASK_GetCurrent();
Andreas Mohrfc2c5df2000-11-27 01:36:24 +00001524
1525 if (!pTask || !(pTask->error_mode & SEM_NOGPFAULTERRORBOX))
1526 {
Alexandre Julliardea2a9a82001-02-20 00:55:17 +00001527 HMODULE mod = GetModuleHandleA( "user32.dll" );
1528 if (mod)
1529 {
1530 MessageBoxA_funcptr pMessageBoxA = (MessageBoxA_funcptr)GetProcAddress( mod, "MessageBoxA" );
1531 if (pMessageBoxA)
1532 {
1533 pMessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
1534 goto done;
1535 }
1536 }
1537 ERR( "%s\n", debugstr_a(str) );
Andreas Mohrfc2c5df2000-11-27 01:36:24 +00001538 }
Alexandre Julliardea2a9a82001-02-20 00:55:17 +00001539 done:
Andreas Mohrfc2c5df2000-11-27 01:36:24 +00001540 ExitThread(0xff);
1541}
1542
1543
1544/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001545 * TerminateApp (TOOLHELP.77)
Andreas Mohrfc2c5df2000-11-27 01:36:24 +00001546 *
1547 * See "Undocumented Windows".
1548 */
1549void WINAPI TerminateApp16(HTASK16 hTask, WORD wFlags)
1550{
1551 if (hTask && hTask != GetCurrentTask())
1552 {
1553 FIXME("cannot terminate task %x\n", hTask);
1554 return;
1555 }
1556
1557 if (wFlags & NO_UAE_BOX)
1558 {
1559 UINT16 old_mode;
1560 old_mode = SetErrorMode16(0);
1561 SetErrorMode16(old_mode|SEM_NOGPFAULTERRORBOX);
1562 }
1563 FatalAppExit16( 0, NULL );
1564
1565 /* hmm, we're still alive ?? */
1566
1567 /* check undocumented flag */
1568 if (!(wFlags & 0x8000))
1569 TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask );
1570
1571 /* UndocWin says to call int 0x21/0x4c exit=0xff here,
1572 but let's just call ExitThread */
1573 ExitThread(0xff);
1574}
1575
1576
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001577/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +00001578 * GetAppCompatFlags (KERNEL.354)
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001579 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +00001580DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask )
Alexandre Julliard21979011997-03-05 08:22:35 +00001581{
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001582 TDB *pTask;
1583
1584 if (!hTask) hTask = GetCurrentTask();
Alexandre Julliard13f2a172002-07-28 23:48:27 +00001585 if (!(pTask=TASK_GetPtr( hTask ))) return 0;
Alexandre Julliard339eefc1996-06-23 14:56:20 +00001586 if (GlobalSize16(hTask) < sizeof(TDB)) return 0;
1587 return pTask->compat_flags;
1588}