Release 951226
[wine/gsoc-2012-control.git] / loader / task.c
blobe7f2835d3b9559e45bae6043b90a94796b2df621
1 /*
2 * Task functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "task.h"
12 #include "callback.h"
13 #include "dos_fs.h"
14 #include "debugger.h"
15 #include "global.h"
16 #include "instance.h"
17 #include "message.h"
18 #include "miscemu.h"
19 #include "module.h"
20 #include "neexe.h"
21 #include "options.h"
22 #include "selectors.h"
23 #include "toolhelp.h"
24 #include "stddebug.h"
25 #include "debug.h"
26 #include "dde_proc.h"
28 /* Min. number of thunks allocated when creating a new segment */
29 #define MIN_THUNKS 32
31 /* 32-bit stack size for each task */
32 /* Must not be greater than 64k, or MAKE_SEGPTR won't work */
33 #define STACK32_SIZE 0x10000
35 /* ------ Internal variables ------ */
37 static HTASK hFirstTask = 0;
38 static HTASK hCurrentTask = 0;
39 static HTASK hTaskToKill = 0;
40 static HTASK hLockedTask = 0;
41 static WORD nTaskCount = 0;
42 static HANDLE hDOSEnvironment = 0;
44 /* ------ Internal declarations ------ */
46 /* TASK_Reschedule() 16-bit entry point */
47 static FARPROC TASK_RescheduleProc;
49 #ifdef WINELIB
50 #define TASK_SCHEDULE() TASK_Reschedule();
51 #else
52 #define TASK_SCHEDULE() CallTo16_word_(TASK_RescheduleProc,0)
53 #endif
55 static HANDLE TASK_CreateDOSEnvironment(void);
57 /***********************************************************************
58 * TASK_Init
60 BOOL TASK_Init(void)
62 TASK_RescheduleProc = (FARPROC)GetWndProcEntry16( "TASK_Reschedule" );
63 if (!(hDOSEnvironment = TASK_CreateDOSEnvironment()))
64 fprintf( stderr, "Not enough memory for DOS Environment\n" );
65 return (hDOSEnvironment != 0);
69 /***********************************************************************
70 * TASK_CreateDOSEnvironment
72 * Create the original DOS environment.
74 static HANDLE TASK_CreateDOSEnvironment(void)
76 static const char program_name[] = "KRNL386.EXE";
77 char **e, *p;
78 int initial_size, size;
79 HANDLE handle;
81 extern char **environ;
82 extern char WindowsDirectory[], SystemDirectory[];
84 /* DOS environment format:
85 * ASCIIZ string 1
86 * ASCIIZ string 2
87 * ...
88 * ASCIIZ string n
89 * ASCIIZ PATH=xxx
90 * ASCIIZ windir=xxx
91 * BYTE 0
92 * WORD 1
93 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
96 /* First compute the size of the fixed part of the environment */
98 initial_size = 5 + /* PATH= */
99 strlen(WindowsPath) + 1 + /* path value */
100 7 + /* windir= */
101 strlen(WindowsDirectory) + 1 + /* windir value */
102 1 + /* BYTE 0 at end */
103 sizeof(WORD) + /* WORD 1 */
104 strlen(SystemDirectory) + 1 + /* program directory */
105 strlen(program_name) + 1; /* program name */
107 /* Compute the total size of the Unix environment (except path) */
109 for (e = environ, size = initial_size; *e; e++)
111 if (strncasecmp(*e, "path=", 5))
113 int len = strlen(*e) + 1;
114 if (size + len >= 32767)
116 fprintf( stderr, "Warning: environment larger than 32k.\n" );
117 break;
119 size += len;
124 /* Now allocate the environment */
126 if (!(handle = GlobalAlloc( GMEM_FIXED, size ))) return 0;
127 p = (char *)GlobalLock( handle );
129 /* And fill it with the Unix environment */
131 for (e = environ, size = initial_size; *e; e++)
133 if (strncasecmp(*e, "path=", 5))
135 int len = strlen(*e) + 1;
136 if (size + len >= 32767) break;
137 strcpy( p, *e );
138 size += len;
139 p += len;
143 /* Now add the path and Windows directory */
145 strcpy( p, "PATH=" );
146 strcat( p, WindowsPath );
147 p += strlen(p) + 1;
149 strcpy( p, "windir=" );
150 strcat( p, WindowsDirectory );
151 p += strlen(p) + 1;
153 /* Now add the program name */
155 *p++ = '\0';
156 *(WORD *)p = 1;
157 p += sizeof(WORD);
158 strcpy( p, SystemDirectory );
159 strcat( p, "\\" );
160 strcat( p, program_name );
162 /* Display it */
164 p = (char *) GlobalLock( handle );
165 dprintf_task(stddeb, "Master DOS environment at %p\n", p);
166 for (; *p; p += strlen(p) + 1) dprintf_task(stddeb, " %s\n", p);
167 dprintf_task( stddeb, "Progname: %s\n", p+3 );
169 return handle;
173 /***********************************************************************
174 * TASK_LinkTask
176 static void TASK_LinkTask( HTASK hTask )
178 HTASK *prevTask;
179 TDB *pTask;
181 if (!(pTask = (TDB *)GlobalLock( hTask ))) return;
182 prevTask = &hFirstTask;
183 while (*prevTask)
185 TDB *prevTaskPtr = (TDB *)GlobalLock( *prevTask );
186 if (prevTaskPtr->priority >= pTask->priority) break;
187 prevTask = &prevTaskPtr->hNext;
189 pTask->hNext = *prevTask;
190 *prevTask = hTask;
191 nTaskCount++;
195 /***********************************************************************
196 * TASK_UnlinkTask
198 static void TASK_UnlinkTask( HTASK hTask )
200 HTASK *prevTask;
201 TDB *pTask;
203 prevTask = &hFirstTask;
204 while (*prevTask && (*prevTask != hTask))
206 pTask = (TDB *)GlobalLock( *prevTask );
207 prevTask = &pTask->hNext;
209 if (*prevTask)
211 pTask = (TDB *)GlobalLock( *prevTask );
212 *prevTask = pTask->hNext;
213 pTask->hNext = 0;
214 nTaskCount--;
219 /***********************************************************************
220 * TASK_CreateThunks
222 * Create a thunk free-list in segment 'handle', starting from offset 'offset'
223 * and containing 'count' entries.
225 static void TASK_CreateThunks( HGLOBAL handle, WORD offset, WORD count )
227 int i;
228 WORD free;
229 THUNKS *pThunk;
231 pThunk = (THUNKS *)((BYTE *)GlobalLock( handle ) + offset);
232 pThunk->next = 0;
233 pThunk->magic = THUNK_MAGIC;
234 pThunk->free = (int)&pThunk->thunks - (int)pThunk;
235 free = pThunk->free;
236 for (i = 0; i < count-1; i++)
238 free += 8; /* Offset of next thunk */
239 pThunk->thunks[4*i] = free;
241 pThunk->thunks[4*i] = 0; /* Last thunk */
245 /***********************************************************************
246 * TASK_AllocThunk
248 * Allocate a thunk for MakeProcInstance().
250 static SEGPTR TASK_AllocThunk( HTASK hTask )
252 TDB *pTask;
253 THUNKS *pThunk;
254 WORD sel, base;
256 if (!(pTask = (TDB *)GlobalLock( hTask ))) return 0;
257 sel = pTask->hCSAlias;
258 pThunk = &pTask->thunks;
259 base = (int)pThunk - (int)pTask;
260 while (!pThunk->free)
262 sel = pThunk->next;
263 if (!sel) /* Allocate a new segment */
265 sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8,
266 pTask->hPDB, TRUE, FALSE, FALSE );
267 if (!sel) return (SEGPTR)0;
268 TASK_CreateThunks( sel, 0, MIN_THUNKS );
269 pThunk->next = sel;
271 pThunk = (THUNKS *)GlobalLock( sel );
272 base = 0;
274 base += pThunk->free;
275 pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free);
276 return MAKELONG( base, sel );
280 /***********************************************************************
281 * TASK_FreeThunk
283 * Free a MakeProcInstance() thunk.
285 static BOOL TASK_FreeThunk( HTASK hTask, SEGPTR thunk )
287 TDB *pTask;
288 THUNKS *pThunk;
289 WORD sel, base;
291 if (!(pTask = (TDB *)GlobalLock( hTask ))) return 0;
292 sel = pTask->hCSAlias;
293 pThunk = &pTask->thunks;
294 base = (int)pThunk - (int)pTask;
295 while (sel && (sel != HIWORD(thunk)))
297 sel = pThunk->next;
298 pThunk = (THUNKS *)GlobalLock( sel );
299 base = 0;
301 if (!sel) return FALSE;
302 *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free;
303 pThunk->free = LOWORD(thunk) - base;
304 return TRUE;
308 /***********************************************************************
309 * TASK_CallToStart
311 * 32-bit entry point for a new task. This function is responsible for
312 * setting up the registers and jumping to the 16-bit entry point.
314 #ifndef WINELIB
315 static void TASK_CallToStart(void)
317 int cs_reg, ds_reg, ip_reg;
318 TDB *pTask = (TDB *)GlobalLock( hCurrentTask );
319 NE_MODULE *pModule = (NE_MODULE *)GlobalLock( pTask->hModule );
320 SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule );
322 /* Registers at initialization must be:
323 * ax zero
324 * bx stack size in bytes
325 * cx heap size in bytes
326 * si previous app instance
327 * di current app instance
328 * bp zero
329 * es selector to the PSP
330 * ds dgroup of the application
331 * ss stack selector
332 * sp top of the stack
335 cs_reg = pSegTable[pModule->cs - 1].selector;
336 ip_reg = pModule->ip;
337 ds_reg = pSegTable[pModule->dgroup - 1].selector;
338 #ifndef WINELIB
339 /* JBP: I doubt a CallTo16_regs_ is possible in libwine.a, and IF1632 is not
340 * allowed.
342 IF1632_Saved16_ss = pTask->ss;
343 IF1632_Saved16_sp = pTask->sp;
344 dprintf_task( stddeb, "Starting main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
345 cs_reg, ip_reg, ds_reg,
346 IF1632_Saved16_ss, IF1632_Saved16_sp);
348 CallTo16_regs_( (FARPROC)(cs_reg << 16 | ip_reg), ds_reg,
349 pTask->hPDB /*es*/, 0 /*bp*/, 0 /*ax*/,
350 pModule->stack_size /*bx*/, pModule->heap_size /*cx*/,
351 0 /*dx*/, 0 /*si*/, ds_reg /*di*/ );
352 #else
353 fprintf(stderr, "JBP: Ignoring main program: cs:ip=%04x:%04x ds=%04x ss:sp=%04x:%04x\n",
354 cs_reg, ip_reg, ds_reg,
355 pTask->ss, pTask->sp);
356 #endif
357 /* This should never return */
358 fprintf( stderr, "TASK_CallToStart: Main program returned!\n" );
359 TASK_KillCurrentTask( 1 );
361 #endif
364 /***********************************************************************
365 * TASK_CreateTask
367 HTASK TASK_CreateTask( HMODULE hModule, HANDLE hInstance, HANDLE hPrevInstance,
368 HANDLE hEnvironment, char *cmdLine, WORD cmdShow )
370 HTASK hTask;
371 TDB *pTask;
372 HANDLE hParentEnv;
373 NE_MODULE *pModule;
374 SEGTABLEENTRY *pSegTable;
375 LPSTR name;
376 char filename[256];
377 #ifndef WINELIB32
378 char *stack16Top, *stack32Top;
379 STACK16FRAME *frame16;
380 STACK32FRAME *frame32;
381 extern DWORD CALL16_RetAddr_word;
382 #endif
384 if (!(pModule = (NE_MODULE *)GlobalLock( hModule ))) return 0;
385 pSegTable = NE_SEG_TABLE( pModule );
387 /* Allocate the task structure */
389 hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB),
390 hModule, FALSE, FALSE, FALSE );
391 if (!hTask) return 0;
392 pTask = (TDB *)GlobalLock( hTask );
394 /* Allocate the new environment block */
396 if (!(hParentEnv = hEnvironment))
398 TDB *pParent = (TDB *)GlobalLock( hCurrentTask );
399 hParentEnv = pParent ? pParent->pdb.environment : hDOSEnvironment;
401 /* FIXME: do we really need to make a copy also when */
402 /* we don't use the parent environment? */
403 if (!(hEnvironment = GlobalAlloc( GMEM_FIXED, GlobalSize( hParentEnv ) )))
405 GlobalFree( hTask );
406 return 0;
408 memcpy( GlobalLock( hEnvironment ), GlobalLock( hParentEnv ),
409 GlobalSize( hParentEnv ) );
411 /* Get current directory */
413 GetModuleFileName( hModule, filename, sizeof(filename) );
414 name = strrchr(filename, '\\');
415 if (name) *(name+1) = 0;
417 /* Fill the task structure */
419 pTask->nEvents = 1; /* So the task can be started */
420 pTask->hSelf = hTask;
421 pTask->flags = 0;
422 pTask->version = pModule->expected_version;
423 pTask->hInstance = hInstance;
424 pTask->hPrevInstance = hPrevInstance;
425 pTask->hModule = hModule;
426 pTask->hParent = hCurrentTask;
427 #ifdef WINELIB
428 pTask->curdrive = 'C' - 'A' + 0x80;
429 #else
430 pTask->curdrive = filename[0] - 'A' + 0x80;
431 #endif
432 pTask->magic = TDB_MAGIC;
433 pTask->nCmdShow = cmdShow;
434 strcpy( pTask->curdir, filename+2 );
436 /* Create the thunks block */
438 TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 );
440 /* Copy the module name */
442 name = MODULE_GetModuleName( hModule );
443 strncpy( pTask->module_name, name, sizeof(pTask->module_name) );
445 /* Fill the PDB */
447 pTask->pdb.int20 = 0x20cd;
448 pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */
449 *(DWORD *)&pTask->pdb.dispatcher[1] = MODULE_GetEntryPoint( GetModuleHandle("KERNEL"), 102 ); /* KERNEL.102 is DOS3Call() */
450 #ifndef WINELIB
451 pTask->pdb.savedint22 = INT_GetHandler( 0x22 );
452 pTask->pdb.savedint23 = INT_GetHandler( 0x23 );
453 pTask->pdb.savedint24 = INT_GetHandler( 0x24 );
454 #endif
455 pTask->pdb.environment = hEnvironment;
456 strncpy( pTask->pdb.cmdLine + 1, cmdLine, 126 );
457 pTask->pdb.cmdLine[127] = '\0';
458 pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 );
460 /* Get the compatibility flags */
462 pTask->compat_flags = GetProfileInt( name, "Compatibility", 0 );
464 /* Allocate a selector for the PDB */
466 pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB),
467 hModule, FALSE, FALSE, FALSE, NULL );
469 /* Allocate a code segment alias for the TDB */
471 pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask,
472 sizeof(TDB), pTask->hPDB, TRUE,
473 FALSE, FALSE, NULL );
475 /* Set the owner of the environment block */
477 FarSetOwner( pTask->pdb.environment, pTask->hPDB );
479 /* Default DTA overwrites command-line */
481 pTask->dta = MAKELONG( (int)&pTask->pdb.cmdLine - (int)&pTask->pdb,
482 pTask->hPDB );
484 /* Allocate the 32-bit stack */
486 #ifndef WINELIB
487 pTask->hStack32 = GLOBAL_Alloc( GMEM_FIXED, STACK32_SIZE, pTask->hPDB,
488 FALSE, FALSE, FALSE );
490 /* Create the 32-bit stack frame */
492 *(DWORD *)GlobalLock(pTask->hStack32) = 0xDEADBEEF;
493 stack32Top = (char*)GlobalLock(pTask->hStack32) + STACK32_SIZE;
494 frame32 = (STACK32FRAME *)stack32Top - 1;
495 frame32->saved_esp = (DWORD)stack32Top;
496 frame32->edi = 0;
497 frame32->esi = 0;
498 frame32->edx = 0;
499 frame32->ecx = 0;
500 frame32->ebx = 0;
501 frame32->ebp = 0;
502 frame32->retaddr = (DWORD)TASK_CallToStart;
503 frame32->codeselector = WINE_CODE_SELECTOR;
504 pTask->esp = (DWORD)frame32;
506 /* Create the 16-bit stack frame */
508 pTask->ss = hInstance;
509 pTask->sp = ((pModule->sp != 0) ? pModule->sp :
510 pSegTable[pModule->ss-1].minsize + pModule->stack_size) & ~1;
511 stack16Top = (char *)PTR_SEG_OFF_TO_LIN( pTask->ss, pTask->sp );
512 frame16 = (STACK16FRAME *)stack16Top - 1;
513 frame16->saved_ss = 0; /*pTask->ss;*/
514 frame16->saved_sp = 0; /*pTask->sp;*/
515 frame16->ds = frame16->es = pTask->hInstance;
516 frame16->entry_point = 0;
517 frame16->ordinal_number = 24; /* WINPROCS.24 is TASK_Reschedule */
518 frame16->dll_id = 24; /* WINPROCS */
519 frame16->bp = 0;
520 frame16->ip = LOWORD( CALL16_RetAddr_word );
521 frame16->cs = HIWORD( CALL16_RetAddr_word );
522 pTask->sp -= sizeof(STACK16FRAME);
524 /* If there's no 16-bit stack yet, use a part of the new task stack */
525 /* This is only needed to have a stack to switch from on the first */
526 /* call to DirectedYield(). */
528 if (!IF1632_Saved16_ss)
530 IF1632_Saved16_ss = pTask->ss;
531 IF1632_Saved16_sp = pTask->sp;
534 /* Add a breakpoint at the start of the task */
536 if (Options.debug)
538 DBG_ADDR addr = { pSegTable[pModule->cs-1].selector, pModule->ip };
539 fprintf( stderr, "Task '%s': ", name );
540 DEBUG_AddBreakpoint( &addr );
542 #endif
544 /* Add the task to the linked list */
546 TASK_LinkTask( hTask );
548 dprintf_task( stddeb, "CreateTask: module='%s' cmdline='%s' task="NPFMT"\n",
549 name, cmdLine, hTask );
551 return hTask;
555 /***********************************************************************
556 * TASK_DeleteTask
558 static void TASK_DeleteTask( HTASK hTask )
560 TDB *pTask;
562 if (!(pTask = (TDB *)GlobalLock( hTask ))) return;
564 /* Free the task module */
566 FreeModule( pTask->hModule );
568 /* Free all memory used by this task (including the 32-bit stack, */
569 /* the environment block and the thunk segments). */
571 GlobalFreeAll( pTask->hPDB );
573 /* Free message queue */
575 MSG_DeleteMsgQueue( pTask->hQueue );
577 /* Free the selector aliases */
579 GLOBAL_FreeBlock( pTask->hCSAlias );
580 GLOBAL_FreeBlock( pTask->hPDB );
582 /* Free the task structure itself */
584 GlobalFree( hTask );
588 /***********************************************************************
589 * TASK_KillCurrentTask
591 * Kill the currently running task. As it's not possible to kill the
592 * current task like this, it is simply marked for destruction, and will
593 * be killed when either TASK_Reschedule or this function is called again
594 * in the context of another task.
596 void TASK_KillCurrentTask( int exitCode )
598 if (hTaskToKill && (hTaskToKill != hCurrentTask))
600 /* If another task is already marked for destruction, */
601 /* we call kill it now, as we are in another context. */
602 TASK_DeleteTask( hTaskToKill );
605 if (nTaskCount <= 1)
607 dprintf_task( stddeb, "Killing the last task, exiting\n" );
608 exit(0);
611 /* Remove the task from the list to be sure we never switch back to it */
612 TASK_UnlinkTask( hCurrentTask );
614 hTaskToKill = hCurrentTask;
615 hLockedTask = 0;
617 Yield();
618 /* We should never return from this Yield() */
620 fprintf(stderr,"It's alive! Alive!!!\n");
621 exit(1);
625 /***********************************************************************
626 * TASK_Reschedule
628 * This is where all the magic of task-switching happens!
630 * This function should only be called via the TASK_SCHEDULE() macro, to make
631 * sure that all the context is saved correctly.
633 void TASK_Reschedule(void)
635 TDB *pOldTask = NULL, *pNewTask;
636 HTASK hTask = 0;
638 #ifdef CONFIG_IPC
639 dde_reschedule();
640 #endif
641 /* First check if there's a task to kill */
643 if (hTaskToKill && (hTaskToKill != hCurrentTask))
645 TASK_DeleteTask( hTaskToKill );
646 hTaskToKill = 0;
649 /* If current task is locked, simply return */
651 if (hLockedTask) return;
653 /* Find a task to yield to */
655 pOldTask = (TDB *)GlobalLock( hCurrentTask );
656 if (pOldTask && pOldTask->hYieldTo)
658 /* If a task is stored in hYieldTo of the current task (put there */
659 /* by DirectedYield), yield to it only if it has events pending. */
660 hTask = pOldTask->hYieldTo;
661 if (!(pNewTask = (TDB *)GlobalLock( hTask )) || !pNewTask->nEvents)
662 hTask = 0;
665 if (!hTask)
667 hTask = hFirstTask;
668 while (hTask)
670 pNewTask = (TDB *)GlobalLock( hTask );
671 if (pNewTask->nEvents && (hTask != hCurrentTask)) break;
672 hTask = pNewTask->hNext;
676 /* If there's a task to kill, switch to any other task, */
677 /* even if it doesn't have events pending. */
679 if (!hTask && hTaskToKill) hTask = hFirstTask;
681 if (!hTask) return; /* Do nothing */
683 pNewTask = (TDB *)GlobalLock( hTask );
684 dprintf_task( stddeb, "Switching to task "NPFMT" (%.8s)\n",
685 hTask, pNewTask->module_name );
687 /* Save the stacks of the previous task (if any) */
689 #ifndef WINELIB /* FIXME: JBP: IF1632 not allowed in libwine.a */
690 if (pOldTask)
692 pOldTask->ss = IF1632_Saved16_ss;
693 pOldTask->sp = IF1632_Saved16_sp;
694 pOldTask->esp = IF1632_Saved32_esp;
696 else IF1632_Original32_esp = IF1632_Saved32_esp;
697 #endif
699 /* Make the task the last in the linked list (round-robin scheduling) */
701 pNewTask->priority++;
702 TASK_UnlinkTask( hTask );
703 TASK_LinkTask( hTask );
704 pNewTask->priority--;
706 /* Switch to the new stack */
708 hCurrentTask = hTask;
709 #ifndef WINELIB /* FIXME: JBP: IF1632 not allowed in libwine.a */
710 IF1632_Saved16_ss = pNewTask->ss;
711 IF1632_Saved16_sp = pNewTask->sp;
712 IF1632_Saved32_esp = pNewTask->esp;
713 IF1632_Stack32_base = WIN16_GlobalLock( pNewTask->hStack32 );
714 #endif
718 /***********************************************************************
719 * InitTask (KERNEL.91)
721 void InitTask( struct sigcontext_struct context )
723 static int firstTask = 1;
724 TDB *pTask;
725 NE_MODULE *pModule;
726 SEGTABLEENTRY *pSegTable;
727 INSTANCEDATA *pinstance;
728 LONG stacklow, stackhi;
730 EAX_reg(&context) = 0;
731 if (!(pTask = (TDB *)GlobalLock( hCurrentTask ))) return;
732 if (!(pModule = (NE_MODULE *)GlobalLock( pTask->hModule ))) return;
734 if (firstTask)
736 extern BOOL WIDGETS_Init(void);
737 extern BOOL WIN_CreateDesktopWindow(void);
739 /* Perform global initialisations that need a task context */
741 /* Initialize built-in window classes */
742 if (!WIDGETS_Init()) return;
744 /* Create desktop window */
745 if (!WIN_CreateDesktopWindow()) return;
747 firstTask = 0;
750 #ifndef WINELIB
751 NE_InitializeDLLs( pTask->hModule );
753 /* Registers on return are:
754 * ax 1 if OK, 0 on error
755 * cx stack limit in bytes
756 * dx cmdShow parameter
757 * si instance handle of the previous instance
758 * di instance handle of the new task
759 * es:bx pointer to command-line inside PSP
761 EAX_reg(&context) = 1;
762 EBX_reg(&context) = 0x81;
763 ECX_reg(&context) = pModule->stack_size;
764 EDX_reg(&context) = pTask->nCmdShow;
765 ESI_reg(&context) = (DWORD)pTask->hPrevInstance;
766 EDI_reg(&context) = (DWORD)pTask->hInstance;
767 ES_reg (&context) = (WORD)pTask->hPDB;
768 #endif
770 /* Initialize the local heap */
771 if ( pModule->heap_size )
773 LocalInit( pTask->hInstance, 0, pModule->heap_size );
777 /* Initialize the INSTANCEDATA structure */
778 pSegTable = NE_SEG_TABLE( pModule );
779 stacklow = pSegTable[pModule->ss - 1].minsize;
780 stackhi = stacklow + pModule->stack_size;
781 if (stackhi > 0xffff) stackhi = 0xffff;
782 pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0);
783 pinstance->stackbottom = stackhi; /* yup, that's right. Confused me too. */
784 pinstance->stacktop = stacklow;
785 #ifndef WINELIB /* FIXME: JBP: IF1632 not allowed in libwine.a */
786 pinstance->stackmin = IF1632_Saved16_sp;
787 #endif
791 /***********************************************************************
792 * WaitEvent (KERNEL.30)
794 BOOL WaitEvent( HTASK hTask )
796 TDB *pTask;
798 if (!hTask) hTask = hCurrentTask;
799 pTask = (TDB *)GlobalLock( hTask );
800 if (pTask->nEvents > 0)
802 pTask->nEvents--;
803 return FALSE;
805 TASK_SCHEDULE();
806 /* When we get back here, we have an event (or the task is the only one) */
807 if (pTask->nEvents > 0) pTask->nEvents--;
808 return TRUE;
812 /***********************************************************************
813 * PostEvent (KERNEL.31)
815 void PostEvent( HTASK hTask )
817 TDB *pTask;
819 if (!hTask) hTask = hCurrentTask;
820 if (!(pTask = (TDB *)GlobalLock( hTask ))) return;
821 pTask->nEvents++;
825 /***********************************************************************
826 * SetPriority (KERNEL.32)
828 void SetPriority( HTASK hTask, int delta )
830 TDB *pTask;
831 int newpriority;
833 if (!hTask) hTask = hCurrentTask;
834 if (!(pTask = (TDB *)GlobalLock( hTask ))) return;
835 newpriority = pTask->priority + delta;
836 if (newpriority < -32) newpriority = -32;
837 else if (newpriority > 15) newpriority = 15;
839 pTask->priority = newpriority + 1;
840 TASK_UnlinkTask( hTask );
841 TASK_LinkTask( hTask );
842 pTask->priority--;
846 /***********************************************************************
847 * LockCurrentTask (KERNEL.33)
849 HTASK LockCurrentTask( BOOL bLock )
851 if (bLock) hLockedTask = hCurrentTask;
852 else hLockedTask = 0;
853 return hLockedTask;
857 /***********************************************************************
858 * IsTaskLocked (KERNEL.122)
860 WORD IsTaskLocked(void)
862 return hLockedTask;
866 /***********************************************************************
867 * OldYield (KERNEL.117)
869 void OldYield(void)
871 TDB *pCurTask;
873 pCurTask = (TDB *)GlobalLock( hCurrentTask );
874 if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */
875 TASK_SCHEDULE();
876 if (pCurTask) pCurTask->nEvents--;
880 /***********************************************************************
881 * DirectedYield (KERNEL.150)
883 void DirectedYield( HTASK hTask )
885 TDB *pCurTask;
887 if ((pCurTask = (TDB *)GlobalLock( hCurrentTask )) != NULL)
888 pCurTask->hYieldTo = hTask;
890 OldYield();
894 /***********************************************************************
895 * Yield (KERNEL.29)
897 void Yield(void)
899 DirectedYield( 0 );
903 /***********************************************************************
904 * MakeProcInstance (KERNEL.51)
906 FARPROC MakeProcInstance( FARPROC func, HANDLE hInstance )
908 BYTE *thunk;
909 SEGPTR thunkaddr;
911 thunkaddr = TASK_AllocThunk( hCurrentTask );
912 if (!thunkaddr) return (FARPROC)0;
913 thunk = PTR_SEG_TO_LIN( thunkaddr );
915 dprintf_task( stddeb, "MakeProcInstance("SPFMT","NPFMT"): got thunk "SPFMT"\n",
916 (SEGPTR)func, hInstance, (SEGPTR)thunkaddr );
918 *thunk++ = 0xb8; /* movw instance, %ax */
919 #ifndef WINELIB
920 *thunk++ = (BYTE)(hInstance & 0xff);
921 *thunk++ = (BYTE)(hInstance >> 8);
922 #endif
923 *thunk++ = 0xea; /* ljmp func */
924 *(DWORD *)thunk = (DWORD)func;
925 return (FARPROC)thunkaddr;
929 /***********************************************************************
930 * FreeProcInstance (KERNEL.52)
932 void FreeProcInstance( FARPROC func )
934 dprintf_task( stddeb, "FreeProcInstance("SPFMT")\n", (SEGPTR)func );
935 TASK_FreeThunk( hCurrentTask, (SEGPTR)func );
939 /**********************************************************************
940 * GetCodeHandle (KERNEL.93)
942 HANDLE GetCodeHandle( FARPROC proc )
944 HANDLE handle;
945 BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc );
947 /* Return the code segment containing 'proc'. */
948 /* Not sure if this is really correct (shouldn't matter that much). */
950 /* Check if it is really a thunk */
951 if ((thunk[0] == 0xb8) && (thunk[3] == 0xea))
952 handle = GlobalHandle( thunk[6] + (thunk[7] << 8) );
953 else
954 handle = GlobalHandle( HIWORD(proc) );
956 printf( "STUB: GetCodeHandle(%08lx) returning "NPFMT"\n",
957 (DWORD)proc, handle );
958 return handle;
962 /***********************************************************************
963 * SetTaskQueue (KERNEL.34)
965 HGLOBAL SetTaskQueue( HANDLE hTask, HGLOBAL hQueue )
967 HGLOBAL hPrev;
968 TDB *pTask;
970 if (!hTask) hTask = hCurrentTask;
971 if (!(pTask = (TDB *)GlobalLock( hTask ))) return 0;
972 hPrev = pTask->hQueue;
973 pTask->hQueue = hQueue;
974 return hPrev;
978 /***********************************************************************
979 * GetTaskQueue (KERNEL.35)
981 HGLOBAL GetTaskQueue( HANDLE hTask )
983 TDB *pTask;
985 if (!hTask) hTask = hCurrentTask;
986 if (!(pTask = (TDB *)GlobalLock( hTask ))) return 0;
987 return pTask->hQueue;
991 /***********************************************************************
992 * GetTaskQueueDS (KERNEL.118)
994 #ifndef WINELIB
995 void GetTaskQueueDS( struct sigcontext_struct context )
997 DS_reg(&context) = GlobalHandleToSel( GetTaskQueue(0) );
999 #endif /* WINELIB */
1002 /***********************************************************************
1003 * GetTaskQueueES (KERNEL.119)
1005 #ifndef WINELIB
1006 void GetTaskQueueES( struct sigcontext_struct context )
1008 ES_reg(&context) = GlobalHandleToSel( GetTaskQueue(0) );
1010 #endif /* WINELIB */
1013 /***********************************************************************
1014 * GetCurrentTask (KERNEL.36)
1016 HTASK GetCurrentTask(void)
1018 /* Undocumented: first task is returned in high word */
1019 #ifdef WINELIB32
1020 return hCurrentTask;
1021 #else
1022 return MAKELONG( hCurrentTask, hFirstTask );
1023 #endif
1027 /***********************************************************************
1028 * GetCurrentPDB (KERNEL.37)
1030 HANDLE GetCurrentPDB(void)
1032 TDB *pTask;
1034 if (!(pTask = (TDB *)GlobalLock( hCurrentTask ))) return 0;
1035 return pTask->hPDB;
1039 /***********************************************************************
1040 * GetInstanceData (KERNEL.54)
1042 int GetInstanceData( HANDLE instance, WORD buffer, int len )
1044 char *ptr = (char *)GlobalLock( instance );
1045 if (!ptr || !len) return 0;
1046 if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer;
1047 memcpy( ptr + buffer, (char *)GlobalLock( CURRENT_DS ) + buffer, len );
1048 return len;
1052 /***********************************************************************
1053 * GetDOSEnvironment (KERNEL.131)
1055 SEGPTR GetDOSEnvironment(void)
1057 TDB *pTask;
1059 if (!(pTask = (TDB *)GlobalLock( hCurrentTask ))) return 0;
1060 return (SEGPTR)WIN16_GlobalLock( pTask->pdb.environment );
1064 /***********************************************************************
1065 * GetNumTasks (KERNEL.152)
1067 WORD GetNumTasks(void)
1069 return nTaskCount;
1073 /***********************************************************************
1074 * GetTaskDS (KERNEL.155)
1076 HINSTANCE GetTaskDS(void)
1078 TDB *pTask;
1080 if (!(pTask = (TDB *)GlobalLock( hCurrentTask ))) return 0;
1081 return pTask->hInstance;
1085 /***********************************************************************
1086 * IsTask (KERNEL.320)
1088 BOOL IsTask( HTASK hTask )
1090 TDB *pTask;
1092 if (!(pTask = (TDB *)GlobalLock( hTask ))) return FALSE;
1093 if (GlobalSize( hTask ) < sizeof(TDB)) return FALSE;
1094 return (pTask->magic == TDB_MAGIC);
1098 /***********************************************************************
1099 * GetExePtr (KERNEL.133)
1101 HMODULE GetExePtr( HANDLE handle )
1103 char *ptr;
1104 HTASK hTask;
1105 HANDLE owner;
1107 /* Check for module handle */
1109 if (!(ptr = GlobalLock( handle ))) return 0;
1110 if (((NE_MODULE *)ptr)->magic == NE_SIGNATURE) return handle;
1112 /* Check the owner for module handle */
1114 #ifndef WINELIB
1115 owner = FarGetOwner( handle );
1116 #else
1117 owner = NULL;
1118 fprintf(stderr,"JBP: FarGetOwner() ignored.\n");
1119 #endif
1120 if (!(ptr = GlobalLock( owner ))) return 0;
1121 if (((NE_MODULE *)ptr)->magic == NE_SIGNATURE) return owner;
1123 /* Search for this handle and its owner inside all tasks */
1125 hTask = hFirstTask;
1126 while (hTask)
1128 TDB *pTask = (TDB *)GlobalLock( hTask );
1129 if ((hTask == handle) ||
1130 (pTask->hInstance == handle) ||
1131 (pTask->hQueue == handle) ||
1132 (pTask->hPDB == handle)) return pTask->hModule;
1133 if ((hTask == owner) ||
1134 (pTask->hInstance == owner) ||
1135 (pTask->hQueue == owner) ||
1136 (pTask->hPDB == owner)) return pTask->hModule;
1137 hTask = pTask->hNext;
1139 return 0;
1143 /***********************************************************************
1144 * TaskFirst (TOOLHELP.63)
1146 BOOL TaskFirst( TASKENTRY *lpte )
1148 lpte->hNext = hFirstTask;
1149 return TaskNext( lpte );
1153 /***********************************************************************
1154 * TaskNext (TOOLHELP.64)
1156 BOOL TaskNext( TASKENTRY *lpte )
1158 TDB *pTask;
1159 INSTANCEDATA *pInstData;
1161 dprintf_toolhelp( stddeb, "TaskNext(%p): task="NPFMT"\n", lpte, lpte->hNext );
1162 if (!lpte->hNext) return FALSE;
1163 pTask = (TDB *)GlobalLock( lpte->hNext );
1164 if (!pTask || pTask->magic != TDB_MAGIC) return FALSE;
1165 pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( pTask->hInstance, 0 );
1166 lpte->hTask = lpte->hNext;
1167 lpte->hTaskParent = pTask->hParent;
1168 lpte->hInst = pTask->hInstance;
1169 lpte->hModule = pTask->hModule;
1170 lpte->wSS = pTask->ss;
1171 lpte->wSP = pTask->sp;
1172 lpte->wStackTop = pInstData->stacktop;
1173 lpte->wStackMinimum = pInstData->stackmin;
1174 lpte->wStackBottom = pInstData->stackbottom;
1175 lpte->wcEvents = pTask->nEvents;
1176 lpte->hQueue = pTask->hQueue;
1177 strncpy( lpte->szModule, pTask->module_name, 8 );
1178 lpte->szModule[8] = '\0';
1179 lpte->wPSPOffset = 0x100; /*??*/
1180 lpte->hNext = pTask->hNext;
1181 return TRUE;
1185 /***********************************************************************
1186 * TaskFindHandle (TOOLHELP.65)
1188 BOOL TaskFindHandle( TASKENTRY *lpte, HTASK hTask )
1190 lpte->hNext = hTask;
1191 return TaskNext( lpte );