Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 1 | /* |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 2 | * Task functions |
Alexandre Julliard | 234bc24 | 1994-12-10 13:02:28 +0000 | [diff] [blame] | 3 | * |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 4 | * Copyright 1995 Alexandre Julliard |
| 5 | */ |
| 6 | |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 9 | #include <assert.h> |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 10 | #include <unistd.h> |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 11 | |
Marcus Meissner | 317af32 | 1999-02-17 13:51:06 +0000 | [diff] [blame] | 12 | #include "wine/winbase16.h" |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 13 | #include "user.h" |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 14 | #include "callback.h" |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 15 | #include "drive.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 16 | #include "file.h" |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 17 | #include "global.h" |
| 18 | #include "instance.h" |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 19 | #include "message.h" |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 20 | #include "miscemu.h" |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 21 | #include "module.h" |
| 22 | #include "neexe.h" |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 23 | #include "pe_image.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 24 | #include "process.h" |
Alexandre Julliard | b817f4f | 1996-03-14 18:08:34 +0000 | [diff] [blame] | 25 | #include "queue.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 26 | #include "selectors.h" |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 27 | #include "stackframe.h" |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 28 | #include "task.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 29 | #include "thread.h" |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 30 | #include "toolhelp.h" |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 31 | #include "winnt.h" |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 32 | #include "winsock.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 33 | #include "syslevel.h" |
Alexandre Julliard | e8c6a08 | 1999-05-02 14:33:41 +0000 | [diff] [blame] | 34 | #include "debugtools.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 35 | #include "dosexe.h" |
Ulrich Weigand | 70b2e38 | 1999-05-04 16:43:38 +0000 | [diff] [blame] | 36 | #include "services.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 37 | #include "server.h" |
Alexandre Julliard | aca0578 | 1994-10-17 18:12:41 +0000 | [diff] [blame] | 38 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 39 | DEFAULT_DEBUG_CHANNEL(task) |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 40 | DECLARE_DEBUG_CHANNEL(relay) |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 41 | DECLARE_DEBUG_CHANNEL(toolhelp) |
| 42 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 43 | /* Min. number of thunks allocated when creating a new segment */ |
| 44 | #define MIN_THUNKS 32 |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 45 | |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 46 | /* Pointer to function to switch to a larger stack */ |
| 47 | int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 48 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 49 | static THHOOK DefaultThhook = { 0 }; |
| 50 | THHOOK *pThhook = &DefaultThhook; |
| 51 | |
| 52 | #define hCurrentTask (pThhook->CurTDB) |
| 53 | #define hFirstTask (pThhook->HeadTDB) |
| 54 | #define hLockedTask (pThhook->LockTDB) |
| 55 | |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 56 | static UINT16 nTaskCount = 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 57 | |
Alexandre Julliard | 67a24c8 | 2000-02-18 21:54:32 +0000 | [diff] [blame] | 58 | static HTASK initial_task; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 59 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 60 | /*********************************************************************** |
| 61 | * TASK_InstallTHHook |
| 62 | */ |
| 63 | void TASK_InstallTHHook( THHOOK *pNewThhook ) |
| 64 | { |
| 65 | THHOOK *pOldThhook = pThhook; |
| 66 | |
| 67 | pThhook = pNewThhook? pNewThhook : &DefaultThhook; |
| 68 | |
| 69 | *pThhook = *pOldThhook; |
| 70 | } |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame] | 71 | |
| 72 | /*********************************************************************** |
Alexandre Julliard | 2ace16a | 1996-04-28 15:09:19 +0000 | [diff] [blame] | 73 | * TASK_GetNextTask |
| 74 | */ |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 75 | HTASK16 TASK_GetNextTask( HTASK16 hTask ) |
Alexandre Julliard | 2ace16a | 1996-04-28 15:09:19 +0000 | [diff] [blame] | 76 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 77 | TDB* pTask = (TDB*)GlobalLock16(hTask); |
Alexandre Julliard | 2ace16a | 1996-04-28 15:09:19 +0000 | [diff] [blame] | 78 | |
| 79 | if (pTask->hNext) return pTask->hNext; |
| 80 | return (hFirstTask != hTask) ? hFirstTask : 0; |
| 81 | } |
| 82 | |
Alexandre Julliard | 2ace16a | 1996-04-28 15:09:19 +0000 | [diff] [blame] | 83 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 84 | * TASK_LinkTask |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 85 | */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 86 | static void TASK_LinkTask( HTASK16 hTask ) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 87 | { |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 88 | HTASK16 *prevTask; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 89 | TDB *pTask; |
| 90 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 91 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 92 | prevTask = &hFirstTask; |
| 93 | while (*prevTask) |
| 94 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 95 | TDB *prevTaskPtr = (TDB *)GlobalLock16( *prevTask ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 96 | if (prevTaskPtr->priority >= pTask->priority) break; |
| 97 | prevTask = &prevTaskPtr->hNext; |
| 98 | } |
| 99 | pTask->hNext = *prevTask; |
| 100 | *prevTask = hTask; |
| 101 | nTaskCount++; |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 105 | /*********************************************************************** |
| 106 | * TASK_UnlinkTask |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 107 | */ |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 108 | static void TASK_UnlinkTask( HTASK16 hTask ) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 109 | { |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 110 | HTASK16 *prevTask; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 111 | TDB *pTask; |
| 112 | |
| 113 | prevTask = &hFirstTask; |
| 114 | while (*prevTask && (*prevTask != hTask)) |
| 115 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 116 | pTask = (TDB *)GlobalLock16( *prevTask ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 117 | prevTask = &pTask->hNext; |
| 118 | } |
| 119 | if (*prevTask) |
| 120 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 121 | pTask = (TDB *)GlobalLock16( *prevTask ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 122 | *prevTask = pTask->hNext; |
| 123 | pTask->hNext = 0; |
| 124 | nTaskCount--; |
| 125 | } |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 129 | /*********************************************************************** |
| 130 | * TASK_CreateThunks |
| 131 | * |
| 132 | * Create a thunk free-list in segment 'handle', starting from offset 'offset' |
| 133 | * and containing 'count' entries. |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 134 | */ |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 135 | static void TASK_CreateThunks( HGLOBAL16 handle, WORD offset, WORD count ) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 136 | { |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 137 | int i; |
| 138 | WORD free; |
| 139 | THUNKS *pThunk; |
| 140 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 141 | pThunk = (THUNKS *)((BYTE *)GlobalLock16( handle ) + offset); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 142 | pThunk->next = 0; |
| 143 | pThunk->magic = THUNK_MAGIC; |
| 144 | pThunk->free = (int)&pThunk->thunks - (int)pThunk; |
| 145 | free = pThunk->free; |
| 146 | for (i = 0; i < count-1; i++) |
| 147 | { |
| 148 | free += 8; /* Offset of next thunk */ |
| 149 | pThunk->thunks[4*i] = free; |
| 150 | } |
| 151 | pThunk->thunks[4*i] = 0; /* Last thunk */ |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 155 | /*********************************************************************** |
| 156 | * TASK_AllocThunk |
| 157 | * |
| 158 | * Allocate a thunk for MakeProcInstance(). |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 159 | */ |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 160 | static SEGPTR TASK_AllocThunk( HTASK16 hTask ) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 161 | { |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 162 | TDB *pTask; |
| 163 | THUNKS *pThunk; |
| 164 | WORD sel, base; |
| 165 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 166 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 167 | sel = pTask->hCSAlias; |
| 168 | pThunk = &pTask->thunks; |
| 169 | base = (int)pThunk - (int)pTask; |
| 170 | while (!pThunk->free) |
| 171 | { |
| 172 | sel = pThunk->next; |
| 173 | if (!sel) /* Allocate a new segment */ |
| 174 | { |
| 175 | sel = GLOBAL_Alloc( GMEM_FIXED, sizeof(THUNKS) + (MIN_THUNKS-1)*8, |
| 176 | pTask->hPDB, TRUE, FALSE, FALSE ); |
| 177 | if (!sel) return (SEGPTR)0; |
| 178 | TASK_CreateThunks( sel, 0, MIN_THUNKS ); |
| 179 | pThunk->next = sel; |
| 180 | } |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 181 | pThunk = (THUNKS *)GlobalLock16( sel ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 182 | base = 0; |
| 183 | } |
| 184 | base += pThunk->free; |
| 185 | pThunk->free = *(WORD *)((BYTE *)pThunk + pThunk->free); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 186 | return PTR_SEG_OFF_TO_SEGPTR( sel, base ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 187 | } |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 188 | |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 189 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 190 | /*********************************************************************** |
| 191 | * TASK_FreeThunk |
| 192 | * |
| 193 | * Free a MakeProcInstance() thunk. |
| 194 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 195 | static BOOL TASK_FreeThunk( HTASK16 hTask, SEGPTR thunk ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 196 | { |
| 197 | TDB *pTask; |
| 198 | THUNKS *pThunk; |
| 199 | WORD sel, base; |
| 200 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 201 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 202 | sel = pTask->hCSAlias; |
| 203 | pThunk = &pTask->thunks; |
| 204 | base = (int)pThunk - (int)pTask; |
| 205 | while (sel && (sel != HIWORD(thunk))) |
| 206 | { |
| 207 | sel = pThunk->next; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 208 | pThunk = (THUNKS *)GlobalLock16( sel ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 209 | base = 0; |
| 210 | } |
| 211 | if (!sel) return FALSE; |
| 212 | *(WORD *)((BYTE *)pThunk + LOWORD(thunk) - base) = pThunk->free; |
| 213 | pThunk->free = LOWORD(thunk) - base; |
| 214 | return TRUE; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /*********************************************************************** |
| 219 | * TASK_CallToStart |
| 220 | * |
| 221 | * 32-bit entry point for a new task. This function is responsible for |
| 222 | * setting up the registers and jumping to the 16-bit entry point. |
| 223 | */ |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 224 | void TASK_CallToStart(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 225 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 226 | TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() ); |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 227 | NE_MODULE *pModule = NE_GetPtr( pTask->hModule ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 228 | SEGTABLEENTRY *pSegTable = NE_SEG_TABLE( pModule ); |
Alexandre Julliard | 617955d | 1999-06-26 18:40:24 +0000 | [diff] [blame] | 229 | CONTEXT86 context; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 230 | |
Ulrich Weigand | fa5647a | 2000-01-17 22:21:29 +0000 | [diff] [blame] | 231 | SYSLEVEL_EnterWin16Lock(); |
| 232 | |
| 233 | /* Add task to 16-bit scheduler pool if necessary */ |
| 234 | if ( hCurrentTask != GetCurrentTask() ) |
| 235 | TASK_Reschedule(); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 236 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 237 | /* Registers at initialization must be: |
| 238 | * ax zero |
| 239 | * bx stack size in bytes |
| 240 | * cx heap size in bytes |
| 241 | * si previous app instance |
| 242 | * di current app instance |
| 243 | * bp zero |
| 244 | * es selector to the PSP |
| 245 | * ds dgroup of the application |
| 246 | * ss stack selector |
| 247 | * sp top of the stack |
| 248 | */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 249 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 250 | memset( &context, 0, sizeof(context) ); |
| 251 | CS_reg(&context) = GlobalHandleToSel16(pSegTable[pModule->cs - 1].hSeg); |
Ulrich Weigand | 3c717e4 | 1999-07-03 15:36:45 +0000 | [diff] [blame] | 252 | DS_reg(&context) = GlobalHandleToSel16(pTask->hInstance); |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 253 | ES_reg(&context) = pTask->hPDB; |
| 254 | EIP_reg(&context) = pModule->ip; |
| 255 | EBX_reg(&context) = pModule->stack_size; |
| 256 | ECX_reg(&context) = pModule->heap_size; |
Ulrich Weigand | 3c717e4 | 1999-07-03 15:36:45 +0000 | [diff] [blame] | 257 | EDI_reg(&context) = pTask->hInstance; |
| 258 | ESI_reg(&context) = pTask->hPrevInstance; |
Ulrich Weigand | 1c971e3 | 1999-05-08 09:58:47 +0000 | [diff] [blame] | 259 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 260 | TRACE("Starting main program: cs:ip=%04lx:%04lx ds=%04lx ss:sp=%04x:%04x\n", |
| 261 | CS_reg(&context), EIP_reg(&context), DS_reg(&context), |
| 262 | SELECTOROF(pTask->teb->cur_stack), |
| 263 | OFFSETOF(pTask->teb->cur_stack) ); |
Ulrich Weigand | 8139c30 | 1999-04-01 11:43:05 +0000 | [diff] [blame] | 264 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 265 | Callbacks->CallRegisterShortProc( &context, 0 ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | /*********************************************************************** |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 270 | * TASK_Create |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 271 | * |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 272 | * NOTE: This routine might be called by a Win32 thread. Thus, we need |
| 273 | * to be careful to protect global data structures. We do this |
| 274 | * by entering the Win16Lock while linking the task into the |
| 275 | * global task list. |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 276 | */ |
Ulrich Weigand | fbea530 | 1999-07-03 12:26:29 +0000 | [diff] [blame] | 277 | BOOL TASK_Create( NE_MODULE *pModule, UINT16 cmdShow) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 278 | { |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 279 | HTASK16 hTask; |
Ulrich Weigand | 8139c30 | 1999-04-01 11:43:05 +0000 | [diff] [blame] | 280 | TDB *pTask; |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 281 | LPSTR cmd_line; |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 282 | char name[10]; |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 283 | PDB *pdb32 = PROCESS_Current(); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 284 | |
| 285 | /* Allocate the task structure */ |
| 286 | |
| 287 | hTask = GLOBAL_Alloc( GMEM_FIXED | GMEM_ZEROINIT, sizeof(TDB), |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 288 | pModule->self, FALSE, FALSE, FALSE ); |
Ulrich Weigand | 6e0d386 | 1999-02-28 11:14:32 +0000 | [diff] [blame] | 289 | if (!hTask) return FALSE; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 290 | pTask = (TDB *)GlobalLock16( hTask ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 291 | |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 292 | /* Fill the task structure */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 293 | |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 294 | pTask->nEvents = 0; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 295 | pTask->hSelf = hTask; |
| 296 | pTask->flags = 0; |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 297 | |
| 298 | if (pModule->flags & NE_FFLAGS_WIN32) |
| 299 | pTask->flags |= TDBF_WIN32; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 300 | if (pModule->lpDosTask) |
| 301 | pTask->flags |= TDBF_WINOLDAP; |
Alexandre Julliard | b1bac32 | 1996-12-15 19:45:59 +0000 | [diff] [blame] | 302 | |
Ulrich Weigand | fbea530 | 1999-07-03 12:26:29 +0000 | [diff] [blame] | 303 | pTask->hInstance = pModule->self; |
| 304 | pTask->hPrevInstance = 0; |
| 305 | /* NOTE: for 16-bit tasks, the instance handles are updated later on |
| 306 | in NE_InitProcess */ |
| 307 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 308 | pTask->version = pModule->expected_version; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 309 | pTask->hModule = pModule->self; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 310 | pTask->hParent = GetCurrentTask(); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 311 | pTask->magic = TDB_MAGIC; |
| 312 | pTask->nCmdShow = cmdShow; |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 313 | pTask->teb = NtCurrentTeb(); |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 314 | pTask->curdrive = DRIVE_GetCurrentDrive() | 0x80; |
| 315 | strcpy( pTask->curdir, "\\" ); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 316 | lstrcpynA( pTask->curdir + 1, DRIVE_GetDosCwd( DRIVE_GetCurrentDrive() ), |
Alexandre Julliard | 491502b | 1997-11-01 19:08:16 +0000 | [diff] [blame] | 317 | sizeof(pTask->curdir) - 1 ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 318 | |
| 319 | /* Create the thunks block */ |
| 320 | |
| 321 | TASK_CreateThunks( hTask, (int)&pTask->thunks - (int)pTask, 7 ); |
| 322 | |
| 323 | /* Copy the module name */ |
| 324 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 325 | GetModuleName16( pModule->self, name, sizeof(name) ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 326 | strncpy( pTask->module_name, name, sizeof(pTask->module_name) ); |
| 327 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 328 | /* Allocate a selector for the PDB */ |
| 329 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 330 | pTask->hPDB = GLOBAL_CreateBlock( GMEM_FIXED, &pTask->pdb, sizeof(PDB16), |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 331 | pModule->self, FALSE, FALSE, FALSE, NULL ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 332 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 333 | /* Fill the PDB */ |
| 334 | |
| 335 | pTask->pdb.int20 = 0x20cd; |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 336 | pTask->pdb.dispatcher[0] = 0x9a; /* ljmp */ |
Alexandre Julliard | 46ea8b3 | 1998-05-03 19:01:20 +0000 | [diff] [blame] | 337 | PUT_DWORD(&pTask->pdb.dispatcher[1], (DWORD)NE_GetEntryPoint( |
Alexandre Julliard | a0b2b1d | 1997-11-16 17:38:29 +0000 | [diff] [blame] | 338 | GetModuleHandle16("KERNEL"), 102 )); /* KERNEL.102 is DOS3Call() */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 339 | pTask->pdb.savedint22 = INT_GetPMHandler( 0x22 ); |
| 340 | pTask->pdb.savedint23 = INT_GetPMHandler( 0x23 ); |
| 341 | pTask->pdb.savedint24 = INT_GetPMHandler( 0x24 ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 342 | pTask->pdb.fileHandlesPtr = |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 343 | PTR_SEG_OFF_TO_SEGPTR( GlobalHandleToSel16(pTask->hPDB), |
| 344 | (int)&((PDB16 *)0)->fileHandles ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 345 | pTask->pdb.hFileHandles = 0; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 346 | memset( pTask->pdb.fileHandles, 0xff, sizeof(pTask->pdb.fileHandles) ); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 347 | pTask->pdb.environment = pdb32->env_db->env_sel; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 348 | pTask->pdb.nbFiles = 20; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 349 | |
| 350 | /* Fill the command line */ |
| 351 | |
| 352 | cmd_line = pdb32->env_db->cmd_line; |
| 353 | while (*cmd_line && (*cmd_line != ' ') && (*cmd_line != '\t')) cmd_line++; |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 354 | while ((*cmd_line == ' ') || (*cmd_line == '\t')) cmd_line++; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 355 | lstrcpynA( pTask->pdb.cmdLine+1, cmd_line, sizeof(pTask->pdb.cmdLine)-1); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 356 | pTask->pdb.cmdLine[0] = strlen( pTask->pdb.cmdLine + 1 ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 357 | |
| 358 | /* Get the compatibility flags */ |
| 359 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 360 | pTask->compat_flags = GetProfileIntA( "Compatibility", name, 0 ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 361 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 362 | /* Allocate a code segment alias for the TDB */ |
| 363 | |
| 364 | pTask->hCSAlias = GLOBAL_CreateBlock( GMEM_FIXED, (void *)pTask, |
| 365 | sizeof(TDB), pTask->hPDB, TRUE, |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 366 | FALSE, FALSE, NULL ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 367 | |
| 368 | /* Set the owner of the environment block */ |
| 369 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 370 | FarSetOwner16( pTask->pdb.environment, pTask->hPDB ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 371 | |
| 372 | /* Default DTA overwrites command-line */ |
| 373 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 374 | pTask->dta = PTR_SEG_OFF_TO_SEGPTR( pTask->hPDB, |
| 375 | (int)&pTask->pdb.cmdLine - (int)&pTask->pdb ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 376 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 377 | /* Create scheduler event for 16-bit tasks */ |
| 378 | |
| 379 | if ( !(pTask->flags & TDBF_WIN32) ) |
| 380 | { |
| 381 | pTask->hEvent = CreateEventA( NULL, TRUE, FALSE, NULL ); |
| 382 | pTask->hEvent = ConvertToGlobalHandle( pTask->hEvent ); |
| 383 | } |
| 384 | |
Alexandre Julliard | 1c4c409 | 1999-05-02 19:10:04 +0000 | [diff] [blame] | 385 | /* Enter task handle into thread and process */ |
| 386 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 387 | pTask->teb->htask16 = pTask->teb->process->task = hTask; |
Alexandre Julliard | 67a24c8 | 2000-02-18 21:54:32 +0000 | [diff] [blame] | 388 | if (!initial_task) initial_task = hTask; |
| 389 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 390 | TRACE("module='%s' cmdline='%s' task=%04x\n", name, cmd_line, hTask ); |
Alexandre Julliard | 1c4c409 | 1999-05-02 19:10:04 +0000 | [diff] [blame] | 391 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 392 | /* Add the task to the linked list */ |
| 393 | |
| 394 | SYSLEVEL_EnterWin16Lock(); |
| 395 | TASK_LinkTask( hTask ); |
| 396 | SYSLEVEL_LeaveWin16Lock(); |
| 397 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 398 | return TRUE; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 401 | /*********************************************************************** |
| 402 | * TASK_DeleteTask |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 403 | */ |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 404 | static void TASK_DeleteTask( HTASK16 hTask ) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 405 | { |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 406 | TDB *pTask; |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 407 | HGLOBAL16 hPDB; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 408 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 409 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return; |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 410 | hPDB = pTask->hPDB; |
| 411 | |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 412 | pTask->magic = 0xdead; /* invalidate signature */ |
| 413 | |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 414 | /* Delete the Win32 part of the task */ |
| 415 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 416 | /* PROCESS_FreePDB( pTask->teb->process ); FIXME */ |
| 417 | /* K32OBJ_DecCount( &pTask->teb->header ); FIXME */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 418 | |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 419 | /* Free the selector aliases */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 420 | |
| 421 | GLOBAL_FreeBlock( pTask->hCSAlias ); |
| 422 | GLOBAL_FreeBlock( pTask->hPDB ); |
| 423 | |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 424 | /* Free the task module */ |
| 425 | |
| 426 | FreeModule16( pTask->hModule ); |
| 427 | |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 428 | /* Free the task structure itself */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 429 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 430 | GlobalFree16( hTask ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 431 | |
| 432 | /* Free all memory used by this task (including the 32-bit stack, */ |
| 433 | /* the environment block and the thunk segments). */ |
| 434 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 435 | GlobalFreeAll16( hPDB ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 438 | /*********************************************************************** |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 439 | * TASK_KillTask |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 440 | */ |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 441 | void TASK_KillTask( HTASK16 hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 442 | { |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 443 | TDB *pTask; |
Alexandre Julliard | 2ace16a | 1996-04-28 15:09:19 +0000 | [diff] [blame] | 444 | |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 445 | /* Enter the Win16Lock to protect global data structures */ |
| 446 | SYSLEVEL_EnterWin16Lock(); |
| 447 | |
| 448 | if ( !hTask ) hTask = GetCurrentTask(); |
| 449 | pTask = (TDB *)GlobalLock16( hTask ); |
| 450 | if ( !pTask ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 451 | { |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 452 | SYSLEVEL_LeaveWin16Lock(); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 456 | TRACE("Killing task %04x\n", hTask ); |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 457 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 458 | #ifdef MZ_SUPPORTED |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 459 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 460 | /* Kill DOS VM task */ |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 461 | NE_MODULE *pModule = NE_GetPtr( pTask->hModule ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 462 | if ( pModule->lpDosTask ) |
| 463 | MZ_KillModule( pModule->lpDosTask ); |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 464 | } |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 465 | #endif |
| 466 | |
Alexandre Julliard | 2ace16a | 1996-04-28 15:09:19 +0000 | [diff] [blame] | 467 | /* Perform USER cleanup */ |
| 468 | |
Ulrich Weigand | c44ab1f | 1999-09-20 18:48:29 +0000 | [diff] [blame] | 469 | TASK_CallTaskSignalProc( USIG16_TERMINATION, hTask ); |
Alexandre Julliard | 9a0e28f | 2000-03-25 19:14:37 +0000 | [diff] [blame] | 470 | PROCESS_CallUserSignalProc( USIG_PROCESS_EXIT, 0 ); |
| 471 | PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 ); |
| 472 | PROCESS_CallUserSignalProc( USIG_PROCESS_DESTROY, 0 ); |
Ulrich Weigand | 8139c30 | 1999-04-01 11:43:05 +0000 | [diff] [blame] | 473 | |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 474 | if (nTaskCount <= 1) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 475 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 476 | TRACE("this is the last task, exiting\n" ); |
Ulrich Weigand | c319c66 | 1999-05-22 16:18:36 +0000 | [diff] [blame] | 477 | ExitKernel16(); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 480 | /* FIXME: Hack! Send a message to the initial task so that |
| 481 | * the GetMessage wakes up and the initial task can check whether |
| 482 | * it is the only remaining one and terminate itself ... |
| 483 | * The initial task should probably install hooks or something |
| 484 | * to get informed about task termination :-/ |
| 485 | */ |
Alexandre Julliard | 67a24c8 | 2000-02-18 21:54:32 +0000 | [diff] [blame] | 486 | Callout.PostAppMessage16( initial_task, WM_NULL, 0, 0 ); |
Ulrich Weigand | afac2e4 | 1998-11-14 18:45:25 +0000 | [diff] [blame] | 487 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 488 | /* Remove the task from the list to be sure we never switch back to it */ |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 489 | TASK_UnlinkTask( hTask ); |
Alexandre Julliard | da0cfb3 | 1996-12-01 17:17:47 +0000 | [diff] [blame] | 490 | if( nTaskCount ) |
| 491 | { |
| 492 | TDB* p = (TDB *)GlobalLock16( hFirstTask ); |
| 493 | while( p ) |
| 494 | { |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 495 | if( p->hYieldTo == hTask ) p->hYieldTo = 0; |
Alexandre Julliard | da0cfb3 | 1996-12-01 17:17:47 +0000 | [diff] [blame] | 496 | p = (TDB *)GlobalLock16( p->hNext ); |
| 497 | } |
| 498 | } |
| 499 | |
Alexandre Julliard | da0cfb3 | 1996-12-01 17:17:47 +0000 | [diff] [blame] | 500 | pTask->nEvents = 0; |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 501 | |
| 502 | if ( hLockedTask == hTask ) |
| 503 | hLockedTask = 0; |
| 504 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 505 | TASK_DeleteTask( hTask ); |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 506 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 507 | /* When deleting the current task ... */ |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 508 | if ( hTask == hCurrentTask ) |
| 509 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 510 | DWORD lockCount; |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 511 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 512 | /* ... schedule another one ... */ |
| 513 | TASK_Reschedule(); |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 514 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 515 | /* ... and completely release the Win16Lock, just in case. */ |
| 516 | ReleaseThunkLock( &lockCount ); |
| 517 | |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 521 | SYSLEVEL_LeaveWin16Lock(); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 524 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 525 | * TASK_Reschedule |
| 526 | * |
| 527 | * This is where all the magic of task-switching happens! |
| 528 | * |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 529 | * 16-bit Windows performs non-preemptive (cooperative) multitasking. |
| 530 | * This means that each 16-bit task runs until it voluntarily yields |
| 531 | * control, at which point the scheduler gets active and selects the |
| 532 | * next task to run. |
| 533 | * |
| 534 | * In Wine, all processes, even 16-bit ones, are scheduled preemptively |
| 535 | * by the standard scheduler of the underlying OS. As many 16-bit apps |
| 536 | * *rely* on the behaviour of the Windows scheduler, however, we have |
| 537 | * to simulate that behaviour. |
| 538 | * |
| 539 | * This is achieved as follows: every 16-bit task is at time (except |
| 540 | * during task creation and deletion) in one of two states: either it |
| 541 | * is the one currently running, then the global variable hCurrentTask |
| 542 | * contains its task handle, or it is not currently running, then it |
| 543 | * is blocked on a special scheduler event, a global handle to which |
| 544 | * is stored in the task struct. |
| 545 | * |
| 546 | * When the current task yields control, this routine gets called. Its |
| 547 | * purpose is to determine the next task to be active, signal the |
| 548 | * scheduler event of that task, and then put the current task to sleep |
| 549 | * waiting for *its* scheduler event to get signalled again. |
| 550 | * |
| 551 | * This routine can get called in a few other special situations as well: |
| 552 | * |
| 553 | * - On creation of a 16-bit task, the Unix process executing the task |
| 554 | * calls TASK_Reschedule once it has completed its initialization. |
| 555 | * At this point, the task needs to be blocked until its scheduler |
| 556 | * event is signalled the first time (this will be done by the parent |
| 557 | * process to get the task up and running). |
| 558 | * |
| 559 | * - When the task currently running terminates itself, this routine gets |
| 560 | * called and has to schedule another task, *without* blocking the |
| 561 | * terminating task. |
| 562 | * |
| 563 | * - When a 32-bit thread posts an event for a 16-bit task, it might be |
| 564 | * the case that *no* 16-bit task is currently running. In this case |
| 565 | * the task that has now an event pending is to be scheduled. |
| 566 | * |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 567 | */ |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 568 | void TASK_Reschedule(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 569 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 570 | TDB *pOldTask = NULL, *pNewTask = NULL; |
| 571 | HTASK16 hOldTask = 0, hNewTask = 0; |
| 572 | enum { MODE_YIELD, MODE_SLEEP, MODE_WAKEUP } mode; |
| 573 | DWORD lockCount; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 574 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 575 | SYSLEVEL_EnterWin16Lock(); |
| 576 | |
| 577 | /* Check what we need to do */ |
| 578 | hOldTask = GetCurrentTask(); |
| 579 | pOldTask = (TDB *)GlobalLock16( hOldTask ); |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 580 | TRACE( "entered with hCurrentTask %04x by hTask %04x (pid %ld)\n", |
| 581 | hCurrentTask, hOldTask, (long) getpid() ); |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 582 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 583 | if ( pOldTask && THREAD_IsWin16( NtCurrentTeb() ) ) |
Ulrich Weigand | 89fc6fd | 1999-05-24 08:13:02 +0000 | [diff] [blame] | 584 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 585 | /* We are called by an active (non-deleted) 16-bit task */ |
| 586 | |
| 587 | /* If we don't even have a current task, or else the current |
| 588 | task has yielded, we'll need to schedule a new task and |
| 589 | (possibly) put the calling task to sleep. Otherwise, we |
| 590 | only block the caller. */ |
| 591 | |
| 592 | if ( !hCurrentTask || hCurrentTask == hOldTask ) |
| 593 | mode = MODE_YIELD; |
| 594 | else |
| 595 | mode = MODE_SLEEP; |
Ulrich Weigand | 89fc6fd | 1999-05-24 08:13:02 +0000 | [diff] [blame] | 596 | } |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 597 | else |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 598 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 599 | /* We are called by a deleted 16-bit task or a 32-bit thread */ |
| 600 | |
| 601 | /* The only situation where we need to do something is if we |
| 602 | now do not have a current task. Then, we'll need to wake up |
| 603 | some task that has events pending. */ |
| 604 | |
| 605 | if ( !hCurrentTask || hCurrentTask == hOldTask ) |
| 606 | mode = MODE_WAKEUP; |
| 607 | else |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 608 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 609 | /* nothing to do */ |
| 610 | SYSLEVEL_LeaveWin16Lock(); |
| 611 | return; |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 612 | } |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 615 | /* Find a task to yield to: check for DirectedYield() */ |
| 616 | if ( mode == MODE_YIELD && pOldTask && pOldTask->hYieldTo ) |
Alexandre Julliard | b7258be | 1995-09-01 15:57:28 +0000 | [diff] [blame] | 617 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 618 | hNewTask = pOldTask->hYieldTo; |
| 619 | pNewTask = (TDB *)GlobalLock16( hNewTask ); |
| 620 | if( !pNewTask || !pNewTask->nEvents) hNewTask = 0; |
Alexandre Julliard | da0cfb3 | 1996-12-01 17:17:47 +0000 | [diff] [blame] | 621 | pOldTask->hYieldTo = 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 624 | /* Find a task to yield to: check for pending events */ |
| 625 | if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && !hNewTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 626 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 627 | hNewTask = hFirstTask; |
| 628 | while (hNewTask) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 629 | { |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 630 | pNewTask = (TDB *)GlobalLock16( hNewTask ); |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 631 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 632 | TRACE( "\ttask = %04x, events = %i\n", hNewTask, pNewTask->nEvents ); |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 633 | |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 634 | if (pNewTask->nEvents) break; |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 635 | hNewTask = pNewTask->hNext; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 636 | } |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 637 | if (hLockedTask && (hNewTask != hLockedTask)) hNewTask = 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 640 | /* If we are still the task with highest priority, just return ... */ |
| 641 | if ( mode == MODE_YIELD && hNewTask == hCurrentTask ) |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 642 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 643 | TRACE("returning to the current task (%04x)\n", hCurrentTask ); |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 644 | SYSLEVEL_LeaveWin16Lock(); |
| 645 | |
Ulrich Weigand | d1e2239 | 1999-04-15 15:30:33 +0000 | [diff] [blame] | 646 | /* Allow Win32 threads to thunk down even while a Win16 task is |
| 647 | in a tight PeekMessage() or Yield() loop ... */ |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 648 | ReleaseThunkLock( &lockCount ); |
| 649 | RestoreThunkLock( lockCount ); |
Ulrich Weigand | be672fe | 1998-12-24 14:59:53 +0000 | [diff] [blame] | 650 | return; |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 651 | } |
Ulrich Weigand | be672fe | 1998-12-24 14:59:53 +0000 | [diff] [blame] | 652 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 653 | /* If no task to yield to found, suspend 16-bit scheduler ... */ |
| 654 | if ( mode == MODE_YIELD && !hNewTask ) |
| 655 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 656 | TRACE("No currently active task\n"); |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 657 | hCurrentTask = 0; |
| 658 | } |
Ulrich Weigand | 8c2dc8b | 1999-05-22 18:58:36 +0000 | [diff] [blame] | 659 | |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 660 | /* If we found a task to wake up, do it ... */ |
| 661 | if ( (mode == MODE_YIELD || mode == MODE_WAKEUP) && hNewTask ) |
| 662 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 663 | TRACE("Switching to task %04x (%.8s)\n", |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 664 | hNewTask, pNewTask->module_name ); |
| 665 | |
| 666 | pNewTask->priority++; |
| 667 | TASK_UnlinkTask( hNewTask ); |
| 668 | TASK_LinkTask( hNewTask ); |
| 669 | pNewTask->priority--; |
| 670 | |
| 671 | hCurrentTask = hNewTask; |
| 672 | SetEvent( pNewTask->hEvent ); |
| 673 | |
| 674 | /* This is set just in case some app reads it ... */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 675 | pNewTask->ss_sp = pNewTask->teb->cur_stack; |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* If we need to put the current task to sleep, do it ... */ |
| 679 | if ( (mode == MODE_YIELD || mode == MODE_SLEEP) && hOldTask != hCurrentTask ) |
| 680 | { |
| 681 | ResetEvent( pOldTask->hEvent ); |
| 682 | |
| 683 | ReleaseThunkLock( &lockCount ); |
| 684 | SYSLEVEL_CheckNotLevel( 1 ); |
| 685 | WaitForSingleObject( pOldTask->hEvent, INFINITE ); |
| 686 | RestoreThunkLock( lockCount ); |
| 687 | } |
| 688 | |
| 689 | SYSLEVEL_LeaveWin16Lock(); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 692 | /*********************************************************************** |
| 693 | * InitTask (KERNEL.91) |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 694 | * |
| 695 | * Called by the application startup code. |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 696 | */ |
Alexandre Julliard | 617955d | 1999-06-26 18:40:24 +0000 | [diff] [blame] | 697 | void WINAPI InitTask16( CONTEXT86 *context ) |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 698 | { |
| 699 | TDB *pTask; |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 700 | INSTANCEDATA *pinstance; |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 701 | SEGPTR ptr; |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 702 | |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 703 | EAX_reg(context) = 0; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 704 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return; |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 705 | |
| 706 | /* Note: we need to trust that BX/CX contain the stack/heap sizes, |
| 707 | as some apps, notably Visual Basic apps, *modify* the heap/stack |
| 708 | size of the instance data segment before calling InitTask() */ |
| 709 | |
| 710 | /* Initialize the INSTANCEDATA structure */ |
| 711 | pinstance = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN(CURRENT_DS, 0); |
| 712 | pinstance->stackmin = OFFSETOF( pTask->teb->cur_stack ); |
| 713 | pinstance->stackbottom = pinstance->stackmin; /* yup, that's right. Confused me too. */ |
| 714 | pinstance->stacktop = ( pinstance->stackmin > BX_reg(context)? |
| 715 | pinstance->stackmin - BX_reg(context) : 0 ) + 150; |
| 716 | |
| 717 | /* Initialize the local heap */ |
| 718 | if ( CX_reg(context) ) |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 719 | LocalInit16( GlobalHandleToSel16(pTask->hInstance), 0, CX_reg(context) ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 720 | |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 721 | /* Initialize implicitly loaded DLLs */ |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 722 | NE_InitializeDLLs( pTask->hModule ); |
Ulrich Weigand | 979000e | 1999-12-05 02:48:53 +0000 | [diff] [blame] | 723 | NE_DllProcessAttach( pTask->hModule ); |
Alexandre Julliard | 2787be8 | 1995-05-22 18:23:01 +0000 | [diff] [blame] | 724 | |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 725 | /* Registers on return are: |
| 726 | * ax 1 if OK, 0 on error |
| 727 | * cx stack limit in bytes |
| 728 | * dx cmdShow parameter |
| 729 | * si instance handle of the previous instance |
| 730 | * di instance handle of the new task |
| 731 | * es:bx pointer to command-line inside PSP |
| 732 | * |
| 733 | * 0 (=%bp) is pushed on the stack |
| 734 | */ |
Alexandre Julliard | a1089c5 | 1999-08-01 12:51:55 +0000 | [diff] [blame] | 735 | ptr = stack16_push( sizeof(WORD) ); |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 736 | *(WORD *)PTR_SEG_TO_LIN(ptr) = 0; |
Alexandre Julliard | a1089c5 | 1999-08-01 12:51:55 +0000 | [diff] [blame] | 737 | ESP_reg(context) -= 2; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 738 | |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 739 | EAX_reg(context) = 1; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 740 | |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 741 | if (!pTask->pdb.cmdLine[0]) EBX_reg(context) = 0x80; |
| 742 | else |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 743 | { |
Ulrich Weigand | a3afeef | 1999-07-10 10:18:26 +0000 | [diff] [blame] | 744 | LPBYTE p = &pTask->pdb.cmdLine[1]; |
| 745 | while ((*p == ' ') || (*p == '\t')) p++; |
| 746 | EBX_reg(context) = 0x80 + (p - pTask->pdb.cmdLine); |
| 747 | } |
| 748 | ECX_reg(context) = pinstance->stacktop; |
| 749 | EDX_reg(context) = pTask->nCmdShow; |
| 750 | ESI_reg(context) = (DWORD)pTask->hPrevInstance; |
| 751 | EDI_reg(context) = (DWORD)pTask->hInstance; |
| 752 | ES_reg (context) = (WORD)pTask->hPDB; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | |
| 756 | /*********************************************************************** |
| 757 | * WaitEvent (KERNEL.30) |
| 758 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 759 | BOOL16 WINAPI WaitEvent16( HTASK16 hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 760 | { |
| 761 | TDB *pTask; |
| 762 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 763 | if (!hTask) hTask = GetCurrentTask(); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 764 | pTask = (TDB *)GlobalLock16( hTask ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 765 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 766 | if ( !THREAD_IsWin16( NtCurrentTeb() ) ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 767 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 768 | FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 769 | return TRUE; |
| 770 | } |
| 771 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 772 | if (pTask->nEvents > 0) |
| 773 | { |
| 774 | pTask->nEvents--; |
| 775 | return FALSE; |
| 776 | } |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 777 | TASK_Reschedule(); |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 778 | |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 779 | /* When we get back here, we have an event */ |
Alexandre Julliard | 2c69f6d | 1996-09-28 18:11:01 +0000 | [diff] [blame] | 780 | |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 781 | if (pTask->nEvents > 0) pTask->nEvents--; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 782 | return TRUE; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | /*********************************************************************** |
| 787 | * PostEvent (KERNEL.31) |
| 788 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 789 | void WINAPI PostEvent16( HTASK16 hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 790 | { |
| 791 | TDB *pTask; |
| 792 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 793 | if (!hTask) hTask = GetCurrentTask(); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 794 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 795 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 796 | if ( !THREAD_IsWin16( pTask->teb ) ) |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 797 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 798 | FIXME("called for Win32 thread (%04x)!\n", pTask->teb->teb_sel ); |
Ulrich Weigand | 93b23d7 | 1999-03-22 10:35:44 +0000 | [diff] [blame] | 799 | return; |
| 800 | } |
| 801 | |
Stephane Lussier | b50272b | 1999-02-21 18:15:26 +0000 | [diff] [blame] | 802 | pTask->nEvents++; |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 803 | |
| 804 | /* If we are a 32-bit task, we might need to wake up the 16-bit scheduler */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 805 | if ( !THREAD_IsWin16( NtCurrentTeb() ) ) |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 806 | TASK_Reschedule(); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | /*********************************************************************** |
| 811 | * SetPriority (KERNEL.32) |
| 812 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 813 | void WINAPI SetPriority16( HTASK16 hTask, INT16 delta ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 814 | { |
| 815 | TDB *pTask; |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 816 | INT16 newpriority; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 817 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 818 | if (!hTask) hTask = GetCurrentTask(); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 819 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 820 | newpriority = pTask->priority + delta; |
| 821 | if (newpriority < -32) newpriority = -32; |
| 822 | else if (newpriority > 15) newpriority = 15; |
| 823 | |
| 824 | pTask->priority = newpriority + 1; |
| 825 | TASK_UnlinkTask( hTask ); |
| 826 | TASK_LinkTask( hTask ); |
| 827 | pTask->priority--; |
| 828 | } |
| 829 | |
| 830 | |
| 831 | /*********************************************************************** |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 832 | * LockCurrentTask (KERNEL.33) |
| 833 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 834 | HTASK16 WINAPI LockCurrentTask16( BOOL16 bLock ) |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 835 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 836 | if (bLock) hLockedTask = GetCurrentTask(); |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 837 | else hLockedTask = 0; |
| 838 | return hLockedTask; |
| 839 | } |
| 840 | |
| 841 | |
| 842 | /*********************************************************************** |
| 843 | * IsTaskLocked (KERNEL.122) |
| 844 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 845 | HTASK16 WINAPI IsTaskLocked16(void) |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 846 | { |
| 847 | return hLockedTask; |
| 848 | } |
| 849 | |
| 850 | |
| 851 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 852 | * OldYield (KERNEL.117) |
| 853 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 854 | void WINAPI OldYield16(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 855 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 856 | TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 857 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 858 | if ( !THREAD_IsWin16( NtCurrentTeb() ) ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 859 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 860 | FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 861 | return; |
| 862 | } |
| 863 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 864 | if (pCurTask) pCurTask->nEvents++; /* Make sure we get back here */ |
Ulrich Weigand | ed49003 | 1999-06-06 14:47:50 +0000 | [diff] [blame] | 865 | TASK_Reschedule(); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 866 | if (pCurTask) pCurTask->nEvents--; |
| 867 | } |
| 868 | |
Ulrich Weigand | 081daf7 | 1999-07-31 17:27:38 +0000 | [diff] [blame] | 869 | /*********************************************************************** |
| 870 | * WIN32_OldYield16 (KERNEL.447) |
| 871 | */ |
| 872 | void WINAPI WIN32_OldYield16(void) |
| 873 | { |
| 874 | DWORD count; |
| 875 | |
| 876 | ReleaseThunkLock(&count); |
| 877 | RestoreThunkLock(count); |
| 878 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 879 | |
| 880 | /*********************************************************************** |
| 881 | * DirectedYield (KERNEL.150) |
| 882 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 883 | void WINAPI DirectedYield16( HTASK16 hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 884 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 885 | TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() ); |
| 886 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 887 | if ( !THREAD_IsWin16( NtCurrentTeb() ) ) |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 888 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 889 | FIXME("called for Win32 thread (%04x)!\n", NtCurrentTeb()->teb_sel); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 890 | return; |
| 891 | } |
| 892 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 893 | TRACE("%04x: DirectedYield(%04x)\n", pCurTask->hSelf, hTask ); |
Alex Korobka | 92c8178 | 1998-10-18 10:47:03 +0000 | [diff] [blame] | 894 | |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 895 | pCurTask->hYieldTo = hTask; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 896 | OldYield16(); |
Alex Korobka | 92c8178 | 1998-10-18 10:47:03 +0000 | [diff] [blame] | 897 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 898 | TRACE("%04x: back from DirectedYield(%04x)\n", pCurTask->hSelf, hTask ); |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 899 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 900 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 901 | /*********************************************************************** |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 902 | * Yield16 (KERNEL.29) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 903 | */ |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 904 | void WINAPI Yield16(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 905 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 906 | TDB *pCurTask = (TDB *)GlobalLock16( GetCurrentTask() ); |
| 907 | |
Alexandre Julliard | ef702d8 | 1996-05-28 18:54:58 +0000 | [diff] [blame] | 908 | if (pCurTask) pCurTask->hYieldTo = 0; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 909 | if (pCurTask && pCurTask->hQueue) Callout.UserYield16(); |
| 910 | else OldYield16(); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Ulrich Weigand | b9eab15 | 1998-11-26 14:58:11 +0000 | [diff] [blame] | 913 | /*********************************************************************** |
| 914 | * KERNEL_490 (KERNEL.490) |
| 915 | */ |
| 916 | HTASK16 WINAPI KERNEL_490( HTASK16 someTask ) |
| 917 | { |
| 918 | if ( !someTask ) return 0; |
| 919 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 920 | FIXME("(%04x): stub\n", someTask ); |
Ulrich Weigand | b9eab15 | 1998-11-26 14:58:11 +0000 | [diff] [blame] | 921 | return 0; |
| 922 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 923 | |
| 924 | /*********************************************************************** |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 925 | * MakeProcInstance16 (KERNEL.51) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 926 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 927 | FARPROC16 WINAPI MakeProcInstance16( FARPROC16 func, HANDLE16 hInstance ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 928 | { |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 929 | BYTE *thunk,*lfunc; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 930 | SEGPTR thunkaddr; |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 931 | WORD hInstanceSelector; |
| 932 | |
| 933 | hInstanceSelector = GlobalHandleToSel16(hInstance); |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 934 | |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 935 | TRACE("(%08lx, %04x);", (DWORD)func, hInstance); |
| 936 | |
| 937 | if (!HIWORD(func)) { |
| 938 | /* Win95 actually protects via SEH, but this is better for debugging */ |
| 939 | ERR("Ouch ! Called with invalid func 0x%08lx !\n", (DWORD)func); |
| 940 | return (FARPROC16)0; |
Alexandre Julliard | a0d7731 | 1998-09-13 16:32:00 +0000 | [diff] [blame] | 941 | } |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 942 | |
| 943 | if (hInstance) |
Uwe Bonnes | f959be9 | 1999-10-23 19:47:55 +0000 | [diff] [blame] | 944 | { |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 945 | if ( (!(hInstance & 4)) || |
| 946 | ((hInstance != 0xffff) && IS_SELECTOR_FREE(hInstance|7)) ) |
| 947 | { |
| 948 | ERR("Invalid hInstance (%04x) passed to MakeProcInstance !\n", |
| 949 | hInstance); |
| 950 | return 0; |
| 951 | } |
Uwe Bonnes | f959be9 | 1999-10-23 19:47:55 +0000 | [diff] [blame] | 952 | } |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 953 | |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 954 | if ( (CURRENT_DS != hInstanceSelector) |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 955 | && (hInstance != 0) |
| 956 | && (hInstance != 0xffff) ) |
| 957 | { |
| 958 | /* calling MPI with a foreign DSEG is invalid ! */ |
| 959 | ERR("Problem with hInstance? Got %04x, using %04x instead\n", |
| 960 | hInstance,CURRENT_DS); |
| 961 | } |
| 962 | |
| 963 | /* Always use the DSEG that MPI was entered with. |
| 964 | * We used to set hInstance to GetTaskDS16(), but this should be wrong |
| 965 | * as CURRENT_DS provides the DSEG value we need. |
| 966 | * ("calling" DS, *not* "task" DS !) */ |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 967 | hInstanceSelector = CURRENT_DS; |
| 968 | hInstance = GlobalHandle16(hInstanceSelector); |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 969 | |
| 970 | /* no thunking for DLLs */ |
| 971 | if (NE_GetPtr(FarGetOwner16(hInstance))->flags & NE_FFLAGS_LIBMODULE) |
| 972 | return func; |
| 973 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 974 | thunkaddr = TASK_AllocThunk( GetCurrentTask() ); |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 975 | if (!thunkaddr) return (FARPROC16)0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 976 | thunk = PTR_SEG_TO_LIN( thunkaddr ); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 977 | lfunc = PTR_SEG_TO_LIN( func ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 978 | |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 979 | TRACE("(%08lx,%04x): got thunk %08lx\n", |
| 980 | (DWORD)func, hInstance, (DWORD)thunkaddr ); |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 981 | if (((lfunc[0]==0x8c) && (lfunc[1]==0xd8)) || /* movw %ds, %ax */ |
| 982 | ((lfunc[0]==0x1e) && (lfunc[1]==0x58)) /* pushw %ds, popw %ax */ |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 983 | ) { |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 984 | FIXME("This was the (in)famous \"thunk useless\" warning. We thought we have to overwrite with nop;nop;, but this isn't true.\n"); |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 985 | } |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 986 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 987 | *thunk++ = 0xb8; /* movw instance, %ax */ |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 988 | *thunk++ = (BYTE)(hInstanceSelector & 0xff); |
| 989 | *thunk++ = (BYTE)(hInstanceSelector >> 8); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 990 | *thunk++ = 0xea; /* ljmp func */ |
| 991 | *(DWORD *)thunk = (DWORD)func; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 992 | return (FARPROC16)thunkaddr; |
Andreas Mohr | 45b5bcf | 2000-03-07 12:22:35 +0000 | [diff] [blame] | 993 | /* CX reg indicates if thunkaddr != NULL, implement if needed */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | |
| 997 | /*********************************************************************** |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 998 | * FreeProcInstance16 (KERNEL.52) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 999 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1000 | void WINAPI FreeProcInstance16( FARPROC16 func ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1001 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1002 | TRACE("(%08lx)\n", (DWORD)func ); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1003 | TASK_FreeThunk( GetCurrentTask(), (SEGPTR)func ); |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1006 | /********************************************************************** |
| 1007 | * TASK_GetCodeSegment |
| 1008 | * |
| 1009 | * Helper function for GetCodeHandle/GetCodeInfo: Retrieve the module |
| 1010 | * and logical segment number of a given code segment. |
| 1011 | * |
| 1012 | * 'proc' either *is* already a pair of module handle and segment number, |
| 1013 | * in which case there's nothing to do. Otherwise, it is a pointer to |
| 1014 | * a function, and we need to retrieve the code segment. If the pointer |
| 1015 | * happens to point to a thunk, we'll retrieve info about the code segment |
| 1016 | * where the function pointed to by the thunk resides, not the thunk itself. |
| 1017 | * |
| 1018 | * FIXME: if 'proc' is a SNOOP16 return stub, we should retrieve info about |
| 1019 | * the function the snoop code will return to ... |
| 1020 | * |
| 1021 | */ |
| 1022 | static BOOL TASK_GetCodeSegment( FARPROC16 proc, NE_MODULE **ppModule, |
| 1023 | SEGTABLEENTRY **ppSeg, int *pSegNr ) |
| 1024 | { |
| 1025 | NE_MODULE *pModule = NULL; |
| 1026 | SEGTABLEENTRY *pSeg = NULL; |
| 1027 | int segNr; |
| 1028 | |
| 1029 | /* Try pair of module handle / segment number */ |
| 1030 | pModule = (NE_MODULE *) GlobalLock16( HIWORD( proc ) ); |
| 1031 | if ( pModule && pModule->magic == IMAGE_OS2_SIGNATURE ) |
| 1032 | { |
| 1033 | segNr = LOWORD( proc ); |
| 1034 | if ( segNr && segNr <= pModule->seg_count ) |
| 1035 | pSeg = NE_SEG_TABLE( pModule ) + segNr-1; |
| 1036 | } |
| 1037 | |
| 1038 | /* Try thunk or function */ |
| 1039 | else |
| 1040 | { |
| 1041 | BYTE *thunk = (BYTE *)PTR_SEG_TO_LIN( proc ); |
| 1042 | WORD selector; |
| 1043 | |
| 1044 | if ((thunk[0] == 0xb8) && (thunk[3] == 0xea)) |
| 1045 | selector = thunk[6] + (thunk[7] << 8); |
| 1046 | else |
| 1047 | selector = HIWORD( proc ); |
| 1048 | |
| 1049 | pModule = NE_GetPtr( GlobalHandle16( selector ) ); |
| 1050 | pSeg = pModule? NE_SEG_TABLE( pModule ) : NULL; |
| 1051 | |
| 1052 | if ( pModule ) |
| 1053 | for ( segNr = 1; segNr <= pModule->seg_count; segNr++, pSeg++ ) |
| 1054 | if ( GlobalHandleToSel16(pSeg->hSeg) == selector ) |
| 1055 | break; |
| 1056 | |
| 1057 | if ( pModule && segNr > pModule->seg_count ) |
| 1058 | pSeg = NULL; |
| 1059 | } |
| 1060 | |
| 1061 | /* Abort if segment not found */ |
| 1062 | |
| 1063 | if ( !pModule || !pSeg ) |
| 1064 | return FALSE; |
| 1065 | |
| 1066 | /* Return segment data */ |
| 1067 | |
| 1068 | if ( ppModule ) *ppModule = pModule; |
| 1069 | if ( ppSeg ) *ppSeg = pSeg; |
| 1070 | if ( pSegNr ) *pSegNr = segNr; |
| 1071 | |
| 1072 | return TRUE; |
| 1073 | } |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 1074 | |
| 1075 | /********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1076 | * GetCodeHandle (KERNEL.93) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 1077 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1078 | HANDLE16 WINAPI GetCodeHandle16( FARPROC16 proc ) |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 1079 | { |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1080 | SEGTABLEENTRY *pSeg; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1081 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1082 | if ( !TASK_GetCodeSegment( proc, NULL, &pSeg, NULL ) ) |
| 1083 | return (HANDLE16)0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1084 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1085 | return pSeg->hSeg; |
Alexandre Julliard | 5819953 | 1994-04-21 01:20:00 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1088 | /********************************************************************** |
| 1089 | * GetCodeInfo (KERNEL.104) |
| 1090 | */ |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1091 | BOOL16 WINAPI GetCodeInfo16( FARPROC16 proc, SEGINFO *segInfo ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1092 | { |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1093 | NE_MODULE *pModule; |
| 1094 | SEGTABLEENTRY *pSeg; |
| 1095 | int segNr; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1096 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1097 | if ( !TASK_GetCodeSegment( proc, &pModule, &pSeg, &segNr ) ) |
| 1098 | return FALSE; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1099 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1100 | /* Fill in segment information */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1101 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1102 | segInfo->offSegment = pSeg->filepos; |
| 1103 | segInfo->cbSegment = pSeg->size; |
| 1104 | segInfo->flags = pSeg->flags; |
| 1105 | segInfo->cbAlloc = pSeg->minsize; |
| 1106 | segInfo->h = pSeg->hSeg; |
| 1107 | segInfo->alignShift = pModule->alignment; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1108 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1109 | if ( segNr == pModule->dgroup ) |
| 1110 | segInfo->cbAlloc += pModule->heap_size + pModule->stack_size; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1111 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1112 | /* Return module handle in %es */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1113 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1114 | CURRENT_STACK16->es = GlobalHandleToSel16( pModule->self ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1115 | |
Ulrich Weigand | a2d5bb3 | 1999-09-20 15:30:27 +0000 | [diff] [blame] | 1116 | return TRUE; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1119 | |
Alexandre Julliard | e658d82 | 1997-11-30 17:45:40 +0000 | [diff] [blame] | 1120 | /********************************************************************** |
| 1121 | * DefineHandleTable16 (KERNEL.94) |
| 1122 | */ |
| 1123 | BOOL16 WINAPI DefineHandleTable16( WORD wOffset ) |
| 1124 | { |
| 1125 | return TRUE; /* FIXME */ |
| 1126 | } |
| 1127 | |
| 1128 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1129 | /*********************************************************************** |
| 1130 | * SetTaskQueue (KERNEL.34) |
| 1131 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1132 | HQUEUE16 WINAPI SetTaskQueue16( HTASK16 hTask, HQUEUE16 hQueue ) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 1133 | { |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 1134 | HQUEUE16 hPrev; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1135 | TDB *pTask; |
| 1136 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1137 | if (!hTask) hTask = GetCurrentTask(); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1138 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0; |
Alexandre Julliard | c981d0b | 1996-03-31 16:40:13 +0000 | [diff] [blame] | 1139 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1140 | hPrev = pTask->hQueue; |
| 1141 | pTask->hQueue = hQueue; |
Alexandre Julliard | c981d0b | 1996-03-31 16:40:13 +0000 | [diff] [blame] | 1142 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1143 | return hPrev; |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1146 | |
| 1147 | /*********************************************************************** |
| 1148 | * GetTaskQueue (KERNEL.35) |
| 1149 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1150 | HQUEUE16 WINAPI GetTaskQueue16( HTASK16 hTask ) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 1151 | { |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1152 | TDB *pTask; |
| 1153 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1154 | if (!hTask) hTask = GetCurrentTask(); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1155 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return 0; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1156 | return pTask->hQueue; |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1159 | /*********************************************************************** |
| 1160 | * SetThreadQueue (KERNEL.463) |
| 1161 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1162 | HQUEUE16 WINAPI SetThreadQueue16( DWORD thread, HQUEUE16 hQueue ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1163 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1164 | TEB *teb = thread? THREAD_IdToTEB( thread ) : NtCurrentTeb(); |
| 1165 | HQUEUE16 oldQueue = teb? teb->queue : 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1166 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1167 | if ( teb ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1168 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1169 | teb->queue = hQueue; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1170 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1171 | if ( GetTaskQueue16( teb->process->task ) == oldQueue ) |
| 1172 | SetTaskQueue16( teb->process->task, hQueue ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | return oldQueue; |
| 1176 | } |
| 1177 | |
| 1178 | /*********************************************************************** |
| 1179 | * GetThreadQueue (KERNEL.464) |
| 1180 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1181 | HQUEUE16 WINAPI GetThreadQueue16( DWORD thread ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1182 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1183 | TEB *teb = NULL; |
Ulrich Weigand | 901ca36 | 1999-01-20 13:35:34 +0000 | [diff] [blame] | 1184 | if ( !thread ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1185 | teb = NtCurrentTeb(); |
Ulrich Weigand | 901ca36 | 1999-01-20 13:35:34 +0000 | [diff] [blame] | 1186 | else if ( HIWORD(thread) ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1187 | teb = THREAD_IdToTEB( thread ); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1188 | else if ( IsTask16( (HTASK16)thread ) ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1189 | teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb; |
Ulrich Weigand | 901ca36 | 1999-01-20 13:35:34 +0000 | [diff] [blame] | 1190 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1191 | return (HQUEUE16)(teb? teb->queue : 0); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /*********************************************************************** |
| 1195 | * SetFastQueue (KERNEL.624) |
| 1196 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1197 | VOID WINAPI SetFastQueue16( DWORD thread, HANDLE hQueue ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1198 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1199 | TEB *teb = NULL; |
Ulrich Weigand | 901ca36 | 1999-01-20 13:35:34 +0000 | [diff] [blame] | 1200 | if ( !thread ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1201 | teb = NtCurrentTeb(); |
Ulrich Weigand | 901ca36 | 1999-01-20 13:35:34 +0000 | [diff] [blame] | 1202 | else if ( HIWORD(thread) ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1203 | teb = THREAD_IdToTEB( thread ); |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1204 | else if ( IsTask16( (HTASK16)thread ) ) |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1205 | teb = ((TDB *)GlobalLock16( (HANDLE16)thread ))->teb; |
Ulrich Weigand | 901ca36 | 1999-01-20 13:35:34 +0000 | [diff] [blame] | 1206 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1207 | if ( teb ) teb->queue = (HQUEUE16) hQueue; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | /*********************************************************************** |
| 1211 | * GetFastQueue (KERNEL.625) |
| 1212 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1213 | HANDLE WINAPI GetFastQueue16( void ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1214 | { |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1215 | TEB *teb = NtCurrentTeb(); |
| 1216 | if (!teb) return 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1217 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1218 | if (!teb->queue) |
| 1219 | Callout.InitThreadInput16( 0, THREAD_IsWin16(teb)? 4 : 5 ); |
Ulrich Weigand | c75d0e1 | 1998-11-01 17:59:35 +0000 | [diff] [blame] | 1220 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1221 | if (!teb->queue) |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1222 | FIXME("(): should initialize thread-local queue, expect failure!\n" ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1223 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1224 | return (HANDLE)teb->queue; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1225 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1226 | |
| 1227 | /*********************************************************************** |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1228 | * SwitchStackTo (KERNEL.108) |
| 1229 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1230 | void WINAPI SwitchStackTo16( WORD seg, WORD ptr, WORD top ) |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1231 | { |
| 1232 | TDB *pTask; |
| 1233 | STACK16FRAME *oldFrame, *newFrame; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1234 | INSTANCEDATA *pData; |
| 1235 | UINT16 copySize; |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1236 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1237 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1238 | if (!(pData = (INSTANCEDATA *)GlobalLock16( seg ))) return; |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1239 | TRACE("old=%04x:%04x new=%04x:%04x\n", |
| 1240 | SELECTOROF( pTask->teb->cur_stack ), |
| 1241 | OFFSETOF( pTask->teb->cur_stack ), seg, ptr ); |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1242 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1243 | /* Save the old stack */ |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1244 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1245 | oldFrame = THREAD_STACK16( pTask->teb ); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 1246 | /* pop frame + args and push bp */ |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1247 | pData->old_ss_sp = pTask->teb->cur_stack + sizeof(STACK16FRAME) |
Alexandre Julliard | 75796ea | 1998-10-25 08:05:46 +0000 | [diff] [blame] | 1248 | + 2 * sizeof(WORD); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 1249 | *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp) = oldFrame->bp; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1250 | pData->stacktop = top; |
| 1251 | pData->stackmin = ptr; |
| 1252 | pData->stackbottom = ptr; |
| 1253 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1254 | /* Switch to the new stack */ |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1255 | |
Alexandre Julliard | 17216f5 | 1997-10-12 16:30:17 +0000 | [diff] [blame] | 1256 | /* Note: we need to take the 3 arguments into account; otherwise, |
| 1257 | * the stack will underflow upon return from this function. |
| 1258 | */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 1259 | copySize = oldFrame->bp - OFFSETOF(pData->old_ss_sp); |
| 1260 | copySize += 3 * sizeof(WORD) + sizeof(STACK16FRAME); |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1261 | pTask->teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( seg, ptr - copySize ); |
| 1262 | newFrame = THREAD_STACK16( pTask->teb ); |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* Copy the stack frame and the local variables to the new stack */ |
| 1265 | |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 1266 | memmove( newFrame, oldFrame, copySize ); |
| 1267 | newFrame->bp = ptr; |
| 1268 | *(WORD *)PTR_SEG_OFF_TO_LIN( seg, ptr ) = 0; /* clear previous bp */ |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | /*********************************************************************** |
| 1273 | * SwitchStackBack (KERNEL.109) |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1274 | */ |
Alexandre Julliard | 617955d | 1999-06-26 18:40:24 +0000 | [diff] [blame] | 1275 | void WINAPI SwitchStackBack16( CONTEXT86 *context ) |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1276 | { |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1277 | STACK16FRAME *oldFrame, *newFrame; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1278 | INSTANCEDATA *pData; |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1279 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1280 | if (!(pData = (INSTANCEDATA *)GlobalLock16(SELECTOROF(NtCurrentTeb()->cur_stack)))) |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1281 | return; |
| 1282 | if (!pData->old_ss_sp) |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1283 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1284 | WARN("No previous SwitchStackTo\n" ); |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1285 | return; |
| 1286 | } |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1287 | TRACE("restoring stack %04x:%04x\n", |
| 1288 | SELECTOROF(pData->old_ss_sp), OFFSETOF(pData->old_ss_sp) ); |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1289 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1290 | oldFrame = CURRENT_STACK16; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 1291 | |
| 1292 | /* Pop bp from the previous stack */ |
| 1293 | |
| 1294 | BP_reg(context) = *(WORD *)PTR_SEG_TO_LIN(pData->old_ss_sp); |
| 1295 | pData->old_ss_sp += sizeof(WORD); |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1296 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1297 | /* Switch back to the old stack */ |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1298 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1299 | NtCurrentTeb()->cur_stack = pData->old_ss_sp - sizeof(STACK16FRAME); |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1300 | SS_reg(context) = SELECTOROF(pData->old_ss_sp); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 1301 | ESP_reg(context) = OFFSETOF(pData->old_ss_sp) - sizeof(DWORD); /*ret addr*/ |
Alexandre Julliard | d37eb36 | 1997-07-20 16:23:21 +0000 | [diff] [blame] | 1302 | pData->old_ss_sp = 0; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1303 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1304 | /* Build a stack frame for the return */ |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1305 | |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1306 | newFrame = CURRENT_STACK16; |
Ulrich Weigand | 3df3e53 | 1999-08-18 18:24:08 +0000 | [diff] [blame] | 1307 | newFrame->frame32 = oldFrame->frame32; |
| 1308 | newFrame->module_cs = oldFrame->module_cs; |
| 1309 | newFrame->callfrom_ip = oldFrame->callfrom_ip; |
| 1310 | newFrame->entry_ip = oldFrame->entry_ip; |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | /*********************************************************************** |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1315 | * GetTaskQueueDS16 (KERNEL.118) |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1316 | */ |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1317 | void WINAPI GetTaskQueueDS16(void) |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1318 | { |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1319 | CURRENT_STACK16->ds = GlobalHandleToSel16( GetTaskQueue16(0) ); |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1320 | } |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1321 | |
| 1322 | |
| 1323 | /*********************************************************************** |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1324 | * GetTaskQueueES16 (KERNEL.119) |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1325 | */ |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1326 | void WINAPI GetTaskQueueES16(void) |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1327 | { |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1328 | CURRENT_STACK16->es = GlobalHandleToSel16( GetTaskQueue16(0) ); |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1329 | } |
Alexandre Julliard | ade697e | 1995-11-26 13:59:11 +0000 | [diff] [blame] | 1330 | |
| 1331 | |
| 1332 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1333 | * GetCurrentTask (KERNEL.36) |
| 1334 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1335 | HTASK16 WINAPI GetCurrentTask(void) |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 1336 | { |
Alexandre Julliard | 54a39e2 | 1999-05-29 14:27:26 +0000 | [diff] [blame] | 1337 | return PROCESS_Current()->task; |
Alexandre Julliard | 2d93d00 | 1996-05-21 15:01:41 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1340 | DWORD WINAPI WIN16_GetCurrentTask(void) |
Alexandre Julliard | 2d93d00 | 1996-05-21 15:01:41 +0000 | [diff] [blame] | 1341 | { |
| 1342 | /* This is the version used by relay code; the first task is */ |
| 1343 | /* returned in the high word of the result */ |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1344 | return MAKELONG( GetCurrentTask(), hFirstTask ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | /*********************************************************************** |
| 1349 | * GetCurrentPDB (KERNEL.37) |
Andreas Mohr | a00b49f | 1998-12-07 10:48:09 +0000 | [diff] [blame] | 1350 | * |
| 1351 | * UNDOC: returns PSP of KERNEL in high word |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1352 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1353 | DWORD WINAPI GetCurrentPDB16(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1354 | { |
| 1355 | TDB *pTask; |
| 1356 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1357 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
Andreas Mohr | a00b49f | 1998-12-07 10:48:09 +0000 | [diff] [blame] | 1358 | return MAKELONG(pTask->hPDB, 0); /* FIXME */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | /*********************************************************************** |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1363 | * GetCurPID16 (KERNEL.157) |
| 1364 | */ |
| 1365 | DWORD WINAPI GetCurPID16( DWORD unused ) |
| 1366 | { |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | /*********************************************************************** |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 1372 | * GetInstanceData (KERNEL.54) |
| 1373 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1374 | INT16 WINAPI GetInstanceData16( HINSTANCE16 instance, WORD buffer, INT16 len ) |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 1375 | { |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1376 | char *ptr = (char *)GlobalLock16( instance ); |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 1377 | if (!ptr || !len) return 0; |
| 1378 | if ((int)buffer + len >= 0x10000) len = 0x10000 - buffer; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 1379 | memcpy( (char *)GlobalLock16(CURRENT_DS) + buffer, ptr + buffer, len ); |
Alexandre Julliard | a2f2e01 | 1995-06-06 16:40:35 +0000 | [diff] [blame] | 1380 | return len; |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | /*********************************************************************** |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1385 | * GetExeVersion (KERNEL.105) |
| 1386 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1387 | WORD WINAPI GetExeVersion16(void) |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1388 | { |
| 1389 | TDB *pTask; |
| 1390 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1391 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1392 | return pTask->version; |
| 1393 | } |
| 1394 | |
| 1395 | |
| 1396 | /*********************************************************************** |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 1397 | * SetErrorMode16 (KERNEL.107) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1398 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1399 | UINT16 WINAPI SetErrorMode16( UINT16 mode ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1400 | { |
| 1401 | TDB *pTask; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1402 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1403 | pTask->error_mode = mode; |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1404 | return SetErrorMode( mode ); |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | /*********************************************************************** |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame] | 1409 | * GetDOSEnvironment (KERNEL.131) |
| 1410 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1411 | SEGPTR WINAPI GetDOSEnvironment16(void) |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame] | 1412 | { |
| 1413 | TDB *pTask; |
| 1414 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1415 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 1416 | return PTR_SEG_OFF_TO_SEGPTR( pTask->pdb.environment, 0 ); |
Alexandre Julliard | 808cb04 | 1995-08-17 17:11:36 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1421 | * GetNumTasks (KERNEL.152) |
| 1422 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1423 | UINT16 WINAPI GetNumTasks16(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1424 | { |
| 1425 | return nTaskCount; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | /*********************************************************************** |
| 1430 | * GetTaskDS (KERNEL.155) |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1431 | * |
| 1432 | * Note: this function apparently returns a DWORD with LOWORD == HIWORD. |
| 1433 | * I don't think we need to bother with this. |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1434 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1435 | HINSTANCE16 WINAPI GetTaskDS16(void) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1436 | { |
| 1437 | TDB *pTask; |
| 1438 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1439 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 1440 | return GlobalHandleToSel16(pTask->hInstance); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Ulrich Weigand | b9eab15 | 1998-11-26 14:58:11 +0000 | [diff] [blame] | 1443 | /*********************************************************************** |
| 1444 | * GetDummyModuleHandleDS (KERNEL.602) |
| 1445 | */ |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1446 | WORD WINAPI GetDummyModuleHandleDS16(void) |
Ulrich Weigand | b9eab15 | 1998-11-26 14:58:11 +0000 | [diff] [blame] | 1447 | { |
| 1448 | TDB *pTask; |
| 1449 | WORD selector; |
| 1450 | |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1451 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
| 1452 | if (!(pTask->flags & TDBF_WIN32)) return 0; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1453 | selector = GlobalHandleToSel16( pTask->hModule ); |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1454 | CURRENT_DS = selector; |
| 1455 | return selector; |
Ulrich Weigand | b9eab15 | 1998-11-26 14:58:11 +0000 | [diff] [blame] | 1456 | } |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1457 | |
| 1458 | /*********************************************************************** |
| 1459 | * IsTask (KERNEL.320) |
| 1460 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1461 | BOOL16 WINAPI IsTask16( HTASK16 hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1462 | { |
| 1463 | TDB *pTask; |
| 1464 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1465 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return FALSE; |
| 1466 | if (GlobalSize16( hTask ) < sizeof(TDB)) return FALSE; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1467 | return (pTask->magic == TDB_MAGIC); |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | /*********************************************************************** |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1472 | * IsWinOldApTask16 (KERNEL.158) |
| 1473 | */ |
| 1474 | BOOL16 WINAPI IsWinOldApTask16( HTASK16 hTask ) |
| 1475 | { |
| 1476 | /* should return bit 0 of byte 0x48 in PSP */ |
| 1477 | return FALSE; |
| 1478 | } |
| 1479 | |
| 1480 | /*********************************************************************** |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1481 | * SetTaskSignalProc (KERNEL.38) |
| 1482 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1483 | FARPROC16 WINAPI SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc ) |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1484 | { |
| 1485 | TDB *pTask; |
| 1486 | FARPROC16 oldProc; |
| 1487 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1488 | if (!hTask) hTask = GetCurrentTask(); |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1489 | if (!(pTask = (TDB *)GlobalLock16( hTask ))) return NULL; |
Ulrich Weigand | c44ab1f | 1999-09-20 18:48:29 +0000 | [diff] [blame] | 1490 | oldProc = pTask->userhandler; |
| 1491 | pTask->userhandler = proc; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1492 | return oldProc; |
| 1493 | } |
| 1494 | |
Ulrich Weigand | c44ab1f | 1999-09-20 18:48:29 +0000 | [diff] [blame] | 1495 | /*********************************************************************** |
| 1496 | * TASK_CallTaskSignalProc |
| 1497 | */ |
| 1498 | /* ### start build ### */ |
| 1499 | extern WORD CALLBACK TASK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD); |
| 1500 | /* ### stop build ### */ |
| 1501 | void TASK_CallTaskSignalProc( UINT16 uCode, HANDLE16 hTaskOrModule ) |
| 1502 | { |
| 1503 | TDB *pTask = (TDB *)GlobalLock16( GetCurrentTask() ); |
| 1504 | if ( !pTask || !pTask->userhandler ) return; |
| 1505 | |
| 1506 | TASK_CallTo16_word_wwwww( pTask->userhandler, |
| 1507 | hTaskOrModule, uCode, 0, |
| 1508 | pTask->hInstance, pTask->hQueue ); |
| 1509 | } |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1510 | |
| 1511 | /*********************************************************************** |
| 1512 | * SetSigHandler (KERNEL.140) |
| 1513 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1514 | WORD WINAPI SetSigHandler16( FARPROC16 newhandler, FARPROC16* oldhandler, |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1515 | UINT16 *oldmode, UINT16 newmode, UINT16 flag ) |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1516 | { |
Dimitrie O. Paun | dd03cc1 | 1999-12-08 03:56:23 +0000 | [diff] [blame] | 1517 | FIXME("(%p,%p,%p,%d,%d), unimplemented.\n", |
Alexandre Julliard | 54c2711 | 1998-03-29 19:44:57 +0000 | [diff] [blame] | 1518 | newhandler,oldhandler,oldmode,newmode,flag ); |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (flag != 1) return 0; |
| 1521 | if (!newmode) newhandler = NULL; /* Default handler */ |
| 1522 | if (newmode != 4) |
| 1523 | { |
| 1524 | TDB *pTask; |
| 1525 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1526 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return 0; |
Alexandre Julliard | f1aa303 | 1996-08-05 17:42:43 +0000 | [diff] [blame] | 1527 | if (oldmode) *oldmode = pTask->signal_flags; |
| 1528 | pTask->signal_flags = newmode; |
| 1529 | if (oldhandler) *oldhandler = pTask->sighandler; |
| 1530 | pTask->sighandler = newhandler; |
| 1531 | } |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | /*********************************************************************** |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 1537 | * GlobalNotify (KERNEL.154) |
Andreas Mohr | a00b49f | 1998-12-07 10:48:09 +0000 | [diff] [blame] | 1538 | * |
| 1539 | * Note that GlobalNotify does _not_ return the old NotifyProc |
| 1540 | * -- contrary to LocalNotify !! |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 1541 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1542 | VOID WINAPI GlobalNotify16( FARPROC16 proc ) |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 1543 | { |
| 1544 | TDB *pTask; |
| 1545 | |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1546 | if (!(pTask = (TDB *)GlobalLock16( GetCurrentTask() ))) return; |
Alexandre Julliard | bf9130a | 1996-10-13 17:45:47 +0000 | [diff] [blame] | 1547 | pTask->discardhandler = proc; |
| 1548 | } |
| 1549 | |
| 1550 | |
| 1551 | /*********************************************************************** |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1552 | * GetExePtr (KERNEL.133) |
| 1553 | */ |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1554 | static inline HMODULE16 GetExePtrHelper( HANDLE16 handle, HTASK16 *hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1555 | { |
| 1556 | char *ptr; |
Alexandre Julliard | 18f92e7 | 1996-07-17 20:02:21 +0000 | [diff] [blame] | 1557 | HANDLE16 owner; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1558 | |
| 1559 | /* Check for module handle */ |
| 1560 | |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1561 | if (!(ptr = GlobalLock16( handle ))) return 0; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 1562 | if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return handle; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1563 | |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1564 | /* Search for this handle inside all tasks */ |
| 1565 | |
| 1566 | *hTask = hFirstTask; |
| 1567 | while (*hTask) |
| 1568 | { |
| 1569 | TDB *pTask = (TDB *)GlobalLock16( *hTask ); |
| 1570 | if ((*hTask == handle) || |
| 1571 | (pTask->hInstance == handle) || |
| 1572 | (pTask->hQueue == handle) || |
| 1573 | (pTask->hPDB == handle)) return pTask->hModule; |
| 1574 | *hTask = pTask->hNext; |
| 1575 | } |
| 1576 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1577 | /* Check the owner for module handle */ |
| 1578 | |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1579 | owner = FarGetOwner16( handle ); |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1580 | if (!(ptr = GlobalLock16( owner ))) return 0; |
Alexandre Julliard | 23946ad | 1997-06-16 17:43:53 +0000 | [diff] [blame] | 1581 | if (((NE_MODULE *)ptr)->magic == IMAGE_OS2_SIGNATURE) return owner; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1582 | |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1583 | /* Search for the owner inside all tasks */ |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1584 | |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1585 | *hTask = hFirstTask; |
| 1586 | while (*hTask) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1587 | { |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1588 | TDB *pTask = (TDB *)GlobalLock16( *hTask ); |
| 1589 | if ((*hTask == owner) || |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1590 | (pTask->hInstance == owner) || |
| 1591 | (pTask->hQueue == owner) || |
| 1592 | (pTask->hPDB == owner)) return pTask->hModule; |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1593 | *hTask = pTask->hNext; |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1594 | } |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1595 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1596 | return 0; |
| 1597 | } |
| 1598 | |
Ulrich Weigand | cfa977d | 1999-07-12 13:39:10 +0000 | [diff] [blame] | 1599 | HMODULE16 WINAPI WIN16_GetExePtr( HANDLE16 handle ) |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1600 | { |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1601 | HTASK16 hTask = 0; |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1602 | HMODULE16 hModule = GetExePtrHelper( handle, &hTask ); |
Ulrich Weigand | cfa977d | 1999-07-12 13:39:10 +0000 | [diff] [blame] | 1603 | STACK16FRAME *frame = CURRENT_STACK16; |
| 1604 | frame->ecx = hModule; |
| 1605 | if (hTask) frame->es = hTask; |
Alexandre Julliard | 4220b29 | 1999-07-11 17:20:01 +0000 | [diff] [blame] | 1606 | return hModule; |
Alexandre Julliard | ebfc0fe | 1998-06-28 18:40:26 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Ulrich Weigand | cfa977d | 1999-07-12 13:39:10 +0000 | [diff] [blame] | 1609 | HMODULE16 WINAPI GetExePtr( HANDLE16 handle ) |
| 1610 | { |
| 1611 | HTASK16 hTask = 0; |
| 1612 | return GetExePtrHelper( handle, &hTask ); |
| 1613 | } |
| 1614 | |
| 1615 | |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1616 | /*********************************************************************** |
| 1617 | * TaskFirst (TOOLHELP.63) |
| 1618 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1619 | BOOL16 WINAPI TaskFirst16( TASKENTRY *lpte ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1620 | { |
| 1621 | lpte->hNext = hFirstTask; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1622 | return TaskNext16( lpte ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | /*********************************************************************** |
| 1627 | * TaskNext (TOOLHELP.64) |
| 1628 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1629 | BOOL16 WINAPI TaskNext16( TASKENTRY *lpte ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1630 | { |
| 1631 | TDB *pTask; |
| 1632 | INSTANCEDATA *pInstData; |
| 1633 | |
Alexandre Julliard | e8c6a08 | 1999-05-02 14:33:41 +0000 | [diff] [blame] | 1634 | TRACE_(toolhelp)("(%p): task=%04x\n", lpte, lpte->hNext ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1635 | if (!lpte->hNext) return FALSE; |
Alexandre Julliard | 1285c2f | 1996-05-06 16:06:24 +0000 | [diff] [blame] | 1636 | pTask = (TDB *)GlobalLock16( lpte->hNext ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1637 | if (!pTask || pTask->magic != TDB_MAGIC) return FALSE; |
Bernd Herd | 4d05761 | 2000-05-11 00:10:36 +0000 | [diff] [blame] | 1638 | pInstData = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( GlobalHandleToSel16(pTask->hInstance), 0 ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1639 | lpte->hTask = lpte->hNext; |
| 1640 | lpte->hTaskParent = pTask->hParent; |
| 1641 | lpte->hInst = pTask->hInstance; |
| 1642 | lpte->hModule = pTask->hModule; |
Alexandre Julliard | 0a860a0 | 1999-06-22 11:43:42 +0000 | [diff] [blame] | 1643 | lpte->wSS = SELECTOROF( pTask->teb->cur_stack ); |
| 1644 | lpte->wSP = OFFSETOF( pTask->teb->cur_stack ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1645 | lpte->wStackTop = pInstData->stacktop; |
| 1646 | lpte->wStackMinimum = pInstData->stackmin; |
| 1647 | lpte->wStackBottom = pInstData->stackbottom; |
| 1648 | lpte->wcEvents = pTask->nEvents; |
| 1649 | lpte->hQueue = pTask->hQueue; |
Francois Gouget | baa9bf9 | 1999-12-27 05:24:06 +0000 | [diff] [blame] | 1650 | lstrcpynA( lpte->szModule, pTask->module_name, sizeof(lpte->szModule) ); |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1651 | lpte->wPSPOffset = 0x100; /*??*/ |
| 1652 | lpte->hNext = pTask->hNext; |
| 1653 | return TRUE; |
| 1654 | } |
| 1655 | |
| 1656 | |
| 1657 | /*********************************************************************** |
| 1658 | * TaskFindHandle (TOOLHELP.65) |
| 1659 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1660 | BOOL16 WINAPI TaskFindHandle16( TASKENTRY *lpte, HTASK16 hTask ) |
Alexandre Julliard | 594997c | 1995-04-30 10:05:20 +0000 | [diff] [blame] | 1661 | { |
| 1662 | lpte->hNext = hTask; |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1663 | return TaskNext16( lpte ); |
Alexandre Julliard | 3f2abfa | 1994-08-16 15:43:11 +0000 | [diff] [blame] | 1664 | } |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1665 | |
| 1666 | |
| 1667 | /*********************************************************************** |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1668 | * GetAppCompatFlags16 (KERNEL.354) |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1669 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 1670 | DWORD WINAPI GetAppCompatFlags16( HTASK16 hTask ) |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1671 | { |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1672 | return GetAppCompatFlags( hTask ); |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | |
| 1676 | /*********************************************************************** |
Patrik Stridvall | 2d6457c | 2000-03-28 20:22:59 +0000 | [diff] [blame] | 1677 | * GetAppCompatFlags (USER32.206) |
Alexandre Julliard | 2197901 | 1997-03-05 08:22:35 +0000 | [diff] [blame] | 1678 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 1679 | DWORD WINAPI GetAppCompatFlags( HTASK hTask ) |
Alexandre Julliard | 339eefc | 1996-06-23 14:56:20 +0000 | [diff] [blame] | 1680 | { |
| 1681 | TDB *pTask; |
| 1682 | |
| 1683 | if (!hTask) hTask = GetCurrentTask(); |
| 1684 | if (!(pTask=(TDB *)GlobalLock16( (HTASK16)hTask ))) return 0; |
| 1685 | if (GlobalSize16(hTask) < sizeof(TDB)) return 0; |
| 1686 | return pTask->compat_flags; |
| 1687 | } |