Changeset 8648 for trunk/src/kernel32/wprocess.cpp
- Timestamp:
- Jun 11, 2002, 6:36:54 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kernel32/wprocess.cpp
r8461 r8648 1 /* $Id: wprocess.cpp,v 1.15 2 2002-05-21 12:07:11sandervl Exp $ */1 /* $Id: wprocess.cpp,v 1.153 2002-06-11 16:36:54 sandervl Exp $ */ 2 2 3 3 /* … … 137 137 } 138 138 //****************************************************************************** 139 // Set up the TIB selector and memory for the current thread 140 //****************************************************************************** 141 TEB * WIN32API InitializeTIB(BOOL fMainThread) 142 { 143 TEB *winteb; 144 ULONG hThreadMain; 145 USHORT tibsel; 146 147 //Allocate one dword to store the flat address of our TEB 148 if(fMainThread) { 149 dprintf(("InitializeTIB Process handle %x, id %x", GetCurrentProcess(), GetCurrentProcessId())); 150 151 TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1); 152 if(TIBFlatPtr == 0) { 153 dprintf(("InitializeTIB: local thread memory alloc failed!!")); 154 DebugInt3(); 155 return NULL; 156 } 157 //SvL: This doesn't really create a thread, but only sets up the 158 // handle of thread 0 159 hThreadMain = HMCreateThread(NULL, 0, 0, 0, 0, 0, TRUE); 160 } 139 //Allocate TEB structure for new thread 140 //****************************************************************************** 141 TEB *WIN32API CreateTEB(HANDLE hThread, DWORD dwThreadId) 142 { 143 USHORT tibsel; 144 TEB *winteb; 145 161 146 if(OSLibAllocSel(sizeof(TEB), &tibsel) == FALSE) 162 147 { … … 173 158 } 174 159 memset(winteb, 0, sizeof(TEB)); 175 *TIBFlatPtr = (DWORD)winteb; 160 161 threadListMutex.enter(); 162 TEB *teblast = threadList; 163 if(!teblast) { 164 threadList = winteb; 165 winteb->o.odin.next = NULL; 166 } 167 else { 168 while(teblast->o.odin.next) { 169 teblast = teblast->o.odin.next; 170 } 171 teblast->o.odin.next = winteb; 172 } 173 threadListMutex.leave(); 176 174 177 175 winteb->except = (PVOID)-1; /* 00 Head of exception handling chain */ 178 179 winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */180 winteb->stack_top = (PVOID)(((ULONG)winteb->stack_top + 0xFFF) & ~0xFFF);181 //round to next page (OS/2 doesn't return a nice rounded value)182 winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */183 //round to page boundary (OS/2 doesn't return a nice rounded value)184 winteb->stack_low = (PVOID)((ULONG)winteb->stack_low & ~0xFFF);185 176 winteb->htask16 = (USHORT)OSLibGetPIB(PIB_TASKHNDL); /* 0c Win16 task handle */ 186 177 winteb->stack_sel = getSS(); /* 0e 16-bit stack selector */ … … 190 181 winteb->tls_ptr = &winteb->tls_array[0]; /* 2c Pointer to TLS array */ 191 182 winteb->process = &ProcessPDB; /* 30 owning process (used by NT3.51 applets)*/ 192 193 183 winteb->delta_priority = THREAD_PRIORITY_NORMAL; 194 winteb->process = &ProcessPDB; 184 winteb->process = &ProcessPDB; 185 186 //store selector of new TEB 187 winteb->teb_sel = tibsel; 188 189 winteb->o.odin.hThread = hThread; 190 winteb->o.odin.threadId = dwThreadId; 191 192 return winteb; 193 } 194 //****************************************************************************** 195 // Set up the TIB selector and memory for the main thread 196 //****************************************************************************** 197 TEB *WIN32API InitializeMainThread() 198 { 199 HANDLE hThreadMain; 200 TEB *teb; 201 202 //Allocate one dword to store the flat address of our TEB 203 dprintf(("InitializeMainThread Process handle %x, id %x", GetCurrentProcess(), GetCurrentProcessId())); 204 205 TIBFlatPtr = (DWORD *)OSLibAllocThreadLocalMemory(1); 206 if(TIBFlatPtr == 0) { 207 dprintf(("InitializeTIB: local thread memory alloc failed!!")); 208 DebugInt3(); 209 return NULL; 210 } 211 //SvL: This doesn't really create a thread, but only sets up the 212 // handle of thread 0 213 hThreadMain = HMCreateThread(NULL, 0, 0, 0, 0, 0, TRUE); 214 215 //create and initialize TEB 216 teb = CreateTEB(hThreadMain, GetCurrentThreadId()); 217 if(teb == NULL || InitializeThread(teb, TRUE) == FALSE) { 218 DebugInt3(); 219 return NULL; 220 } 221 222 ProcessTIBSel = teb->teb_sel; 223 224 //todo initialize PDB during process creation 225 //todo: initialize TLS array if required 226 //TLS in executable always TLS index 0? 227 //// ProcessPDB.exit_code = 0x103; /* STILL_ACTIVE */ 228 ProcessPDB.threads = 1; 229 ProcessPDB.running_threads = 1; 230 ProcessPDB.ring0_threads = 1; 231 ProcessPDB.system_heap = GetProcessHeap(); 232 ProcessPDB.parent = 0; 233 ProcessPDB.group = &ProcessPDB; 234 ProcessPDB.priority = 8; /* Normal */ 235 ProcessPDB.heap = ProcessPDB.system_heap; /* will be changed later on */ 236 ProcessPDB.next = NULL; 237 ProcessPDB.winver = 0xffff; /* to be determined */ 238 ProcessPDB.server_pid = (void *)GetCurrentProcessId(); 239 ProcessPDB.tls_bits[0] = 0; //all tls slots are free 240 ProcessPDB.tls_bits[1] = 0; 241 242 GetSystemTime(&ProcessPDB.creationTime) ; 243 244 /* Initialize the critical section */ 245 InitializeCriticalSection( &ProcessPDB.crit_section ); 246 247 // ProcessPDB.startup_info = &StartupInfo; 248 ProcessPDB.unknown10 = (PVOID)&unknownPDBData[0]; 249 StartupInfo.cb = sizeof(StartupInfo); 250 StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); 251 StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); 252 StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); 253 254 return teb; 255 } 256 //****************************************************************************** 257 // Set up the TEB structure of the CURRENT (!) thread 258 //****************************************************************************** 259 BOOL WIN32API InitializeThread(TEB *winteb, BOOL fMainThread) 260 { 261 //store TEB address in thread locale memory for easy retrieval 262 *TIBFlatPtr = (DWORD)winteb; 263 195 264 //// winteb->exit_code = 0x103; /* STILL_ACTIVE */ 196 winteb->teb_sel = tibsel; 265 266 winteb->stack_top = (PVOID)OSLibGetTIB(TIB_STACKTOP); /* 04 Top of thread stack */ 267 winteb->stack_top = (PVOID)(((ULONG)winteb->stack_top + 0xFFF) & ~0xFFF); 268 //round to next page (OS/2 doesn't return a nice rounded value) 269 winteb->stack_low = (PVOID)OSLibGetTIB(TIB_STACKLOW); /* 08 Stack low-water mark */ 270 //round to page boundary (OS/2 doesn't return a nice rounded value) 271 winteb->stack_low = (PVOID)((ULONG)winteb->stack_low & ~0xFFF); 272 197 273 winteb->o.odin.OrgTIBSel = GetFS(); 198 274 winteb->o.odin.pWsockData = NULL; 199 winteb->o.odin.threadId = 0xFFFFFFFF;200 winteb->o.odin.threadId = GetCurrentThreadId();201 275 #ifdef DEBUG 202 276 winteb->o.odin.dbgCallDepth = 0; 203 277 #endif 204 278 winteb->o.odin.pMessageBuffer = NULL; 205 206 if(fMainThread) {207 winteb->o.odin.hThread = hThreadMain;208 // ProcessPDB.startup_info = &StartupInfo;209 ProcessPDB.unknown10 = (PVOID)&unknownPDBData[0];210 StartupInfo.cb = sizeof(StartupInfo);211 StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);212 StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);213 StartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);214 }215 else winteb->o.odin.hThread = GetCurrentThread();216 279 winteb->o.odin.lcid = GetUserDefaultLCID(); 217 218 threadListMutex.enter();219 TEB *teblast = threadList;220 if(!teblast) {221 threadList = winteb;222 }223 else {224 while(teblast->o.odin.next) {225 teblast = teblast->o.odin.next;226 }227 teblast->o.odin.next = winteb;228 }229 winteb->o.odin.next = NULL;230 threadListMutex.leave();231 280 232 281 if(OSLibGetPIB(PIB_TASKTYPE) == TASKTYPE_PM) … … 241 290 *(ULONG *)&RtlAllocateAndInitializeSid = (ULONG)GetProcAddress(hInstNTDll, "RtlAllocateAndInitializeSid"); 242 291 if(RtlAllocateAndInitializeSid == NULL) { 243 292 DebugInt3(); 244 293 } 245 294 } … … 258 307 259 308 if (NULL != RtlAllocateAndInitializeSid) { 260 RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &winteb->o.odin.threadinfo.PrimaryGroup.PrimaryGroup);309 RtlAllocateAndInitializeSid(&sidIdAuth, 1, 0, 0, 0, 0, 0, 0, 0, 0, &winteb->o.odin.threadinfo.PrimaryGroup.PrimaryGroup); 261 310 } 262 311 else DebugInt3(); … … 271 320 winteb->o.odin.threadinfo.TokenType = TokenPrimary; 272 321 273 if(fMainThread) 274 { 275 //todo initialize PDB during process creation 276 //todo: initialize TLS array if required 277 //TLS in executable always TLS index 0? 278 ProcessTIBSel = tibsel; 279 //// ProcessPDB.exit_code = 0x103; /* STILL_ACTIVE */ 280 ProcessPDB.threads = 1; 281 ProcessPDB.running_threads = 1; 282 ProcessPDB.ring0_threads = 1; 283 ProcessPDB.system_heap = GetProcessHeap(); 284 ProcessPDB.parent = 0; 285 ProcessPDB.group = &ProcessPDB; 286 ProcessPDB.priority = 8; /* Normal */ 287 ProcessPDB.heap = ProcessPDB.system_heap; /* will be changed later on */ 288 ProcessPDB.next = NULL; 289 ProcessPDB.winver = 0xffff; /* to be determined */ 290 ProcessPDB.server_pid = (void *)GetCurrentProcessId(); 291 ProcessPDB.tls_bits[0] = 0; //all tls slots are free 292 ProcessPDB.tls_bits[1] = 0; 293 294 GetSystemTime(&ProcessPDB.creationTime) ; 295 296 /* Initialize the critical section */ 297 InitializeCriticalSection( &ProcessPDB.crit_section ); 298 } 299 dprintf(("InitializeTIB setup TEB with selector %x", tibsel)); 322 dprintf(("InitializeTIB setup TEB with selector %x", winteb->teb_sel)); 300 323 dprintf(("InitializeTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain())); 301 return winteb; 324 return TRUE; 325 } 326 //****************************************************************************** 327 // Destroy the TIB selector and memory for the current thread 328 //****************************************************************************** 329 void WIN32API DestroyTEB(TEB *winteb) 330 { 331 SHORT orgtibsel; 332 333 dprintf(("DestroyTIB: FS = %x", GetFS())); 334 dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain())); 335 336 orgtibsel = winteb->o.odin.OrgTIBSel; 337 338 dprintf(("DestroyTIB: OSLibFreeSel %x", winteb->teb_sel)); 339 340 threadListMutex.enter(); 341 TEB *curteb = threadList; 342 if(curteb == winteb) { 343 threadList = winteb->o.odin.next; 344 } 345 else { 346 while(curteb->o.odin.next != winteb) { 347 curteb = curteb->o.odin.next; 348 if(curteb == NULL) { 349 dprintf(("DestroyTIB: couldn't find teb %x", winteb)); 350 DebugInt3(); 351 break; 352 } 353 } 354 if(curteb) { 355 curteb->o.odin.next = winteb->o.odin.next; 356 } 357 } 358 threadListMutex.leave(); 359 360 // free allocated memory for security structures 361 free( winteb->o.odin.threadinfo.pTokenGroups ); 362 363 #ifdef DEBUG 364 if (winteb->o.odin.arrstrCallStack != NULL) 365 free( winteb->o.odin.arrstrCallStack ); 366 #endif 367 368 //Restore our original FS selector 369 SetFS(orgtibsel); 370 371 //And free our own 372 OSLibFreeSel(winteb->teb_sel); 373 374 *TIBFlatPtr = 0; 375 376 dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain())); 377 return; 302 378 } 303 379 //****************************************************************************** … … 309 385 } 310 386 return ProcessTIBSel; 311 }312 //******************************************************************************313 // Destroy the TIB selector and memory for the current thread314 //******************************************************************************315 void WIN32API DestroyTIB()316 {317 SHORT orgtibsel;318 TEB *winteb;319 320 dprintf(("DestroyTIB: FS = %x", GetFS()));321 dprintf(("DestroyTIB: FS:[0] = %x", QueryExceptionChain()));322 323 winteb = (TEB *)*TIBFlatPtr;324 if(winteb) {325 orgtibsel = winteb->o.odin.OrgTIBSel;326 327 dprintf(("DestroyTIB: OSLibFreeSel %x", winteb->teb_sel));328 329 threadListMutex.enter();330 TEB *curteb = threadList;331 if(curteb == winteb) {332 threadList = winteb->o.odin.next;333 }334 else {335 while(curteb->o.odin.next != winteb) {336 curteb = curteb->o.odin.next;337 if(curteb == NULL) {338 dprintf(("DestroyTIB: couldn't find teb %x", winteb));339 DebugInt3();340 break;341 }342 }343 if(curteb) {344 curteb->o.odin.next = winteb->o.odin.next;345 }346 }347 threadListMutex.leave();348 349 // PH 2002-04-11350 // free allocated memory351 free( winteb->o.odin.threadinfo.pTokenGroups );352 353 #ifdef DEBUG354 if (winteb->o.odin.arrstrCallStack != NULL)355 free( winteb->o.odin.arrstrCallStack );356 #endif357 358 //Restore our original FS selector359 SetFS(orgtibsel);360 361 //And free our own362 OSLibFreeSel(winteb->teb_sel);363 364 *TIBFlatPtr = 0;365 }366 else dprintf(("Already destroyed TIB"));367 368 dprintf(("DestroyTIB: FS(%x):[0] = %x", GetFS(), QueryExceptionChain()));369 return;370 387 } 371 388 /******************************************************************************/ … … 403 420 USHORT WIN32API SetWin32TIB(BOOL fForceSwitch) 404 421 { 405 SHORT win32tibsel;406 TEB *winteb;422 SHORT win32tibsel; 423 TEB *winteb; 407 424 408 425 //If we're running an Odin32 OS/2 application (not converted!), then we … … 420 437 } 421 438 else { 422 //we didn't create this thread, so allocate a selector now 423 //NOTE: Possible memory leak (i.e. DART threads in WINMM) 424 winteb = InitializeTIB(); 425 if(winteb == NULL) { 426 DebugInt3(); 427 return GetFS(); 428 } 429 win32tibsel = winteb->teb_sel; 430 431 //Restore our win32 FS selector 432 return SetReturnFS(win32tibsel); 439 return GetFS(); 433 440 } 434 441 // nested calls are OK, OS2ToWinCallback for instance … … 471 478 472 479 #ifdef PROFILE 473 // Note: after this point we do not expect any more Win32-API calls,474 // so this is probably the best time to dump the gathered profiling475 // information476 PerfView_Write();477 ProfilerWrite();478 ProfilerTerminate();480 // Note: after this point we do not expect any more Win32-API calls, 481 // so this is probably the best time to dump the gathered profiling 482 // information 483 PerfView_Write(); 484 ProfilerWrite(); 485 ProfilerTerminate(); 479 486 #endif /* PROFILE */ 480 487 481 482 483 488 //Restore original OS/2 TIB selector 484 DestroyTIB(); 489 teb = GetThreadTEB(); 490 if(teb) DestroyTEB(teb); 485 491 SetExceptionChain((ULONG)-1); 486 492
Note:
See TracChangeset
for help on using the changeset viewer.