00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
#include "ki.h"
00031
00032
00033
00034
00035
00036
00037 #define ASSERT_PROCESS(E) { \
00038
ASSERT((E)->Header.Type == ProcessObject); \
00039
}
00040
00041 #define ASSERT_THREAD(E) { \
00042
ASSERT((E)->Header.Type == ThreadObject); \
00043
}
00044
00045
00046
00047
00048
#undef ALIGN_UP
00049
#undef ALIGN_DOWN
00050 #define ALIGN_DOWN(address,amt) ((ULONG)(address) & ~(( amt ) - 1))
00051 #define ALIGN_UP(address,amt) (ALIGN_DOWN( (address + (amt) - 1), (amt) ))
00052
00053
00054
00055
00056
00057
00058
VOID
00059
KepSetAlignmentSpecialApc(
00060 IN
PKAPC Apc,
00061 IN PKNORMAL_ROUTINE *NormalRoutine,
00062 IN PVOID *NormalContext,
00063 IN PVOID *SystemArgument1,
00064 IN PVOID *SystemArgument2
00065 );
00066
00067 extern BOOLEAN
KeI386XMMIPresent;
00068
00069
00070
VOID
00071 KiInitializeContextThread (
00072 IN
PKTHREAD Thread,
00073 IN PKSYSTEM_ROUTINE SystemRoutine,
00074 IN PKSTART_ROUTINE StartRoutine OPTIONAL,
00075 IN PVOID StartContext OPTIONAL,
00076 IN PCONTEXT ContextFrame OPTIONAL
00077 )
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 {
00119 PFX_SAVE_AREA NpxFrame;
00120 PKSWITCHFRAME SwitchFrame;
00121 PKTRAP_FRAME TrFrame;
00122 PULONG PSystemRoutine;
00123 PULONG PStartRoutine;
00124 PULONG PStartContext;
00125 PULONG PUserContextFlag;
00126 ULONG ContextFlags;
00127 CONTEXT
Context2;
00128 PCONTEXT ContextFrame2 =
NULL;
00129 PFXSAVE_FORMAT PFxSaveArea;
00130
00131
00132
00133
00134
00135
00136
if (ARGUMENT_PRESENT(ContextFrame)) {
00137
00138 RtlMoveMemory(&
Context2, ContextFrame,
sizeof(CONTEXT));
00139 ContextFrame2 = &
Context2;
00140 ContextFlags =
CONTEXT_CONTROL;
00141
00142
00143
00144
00145
00146 NpxFrame = (PFX_SAVE_AREA)(((ULONG)(Thread->InitialStack) -
00147
sizeof(FX_SAVE_AREA)));
00148
00149
00150
00151
00152
00153
if (
KeI386XMMIPresent ==
TRUE) {
00154 PFxSaveArea = (PFXSAVE_FORMAT)ContextFrame2->ExtendedRegisters;
00155
00156 PFxSaveArea->ControlWord = 0x27f;
00157 PFxSaveArea->StatusWord = 0;
00158 PFxSaveArea->TagWord = 0;
00159 PFxSaveArea->ErrorOffset = 0;
00160 PFxSaveArea->ErrorSelector = 0;
00161 PFxSaveArea->DataOffset = 0;
00162 PFxSaveArea->DataSelector = 0;
00163 PFxSaveArea->MXCsr = 0x1f80;
00164 }
else {
00165 ContextFrame2->FloatSave.ControlWord = 0x27f;
00166 ContextFrame2->FloatSave.StatusWord = 0;
00167 ContextFrame2->FloatSave.TagWord = 0xffff;
00168 ContextFrame2->FloatSave.ErrorOffset = 0;
00169 ContextFrame2->FloatSave.ErrorSelector = 0;
00170 ContextFrame2->FloatSave.DataOffset = 0;
00171 ContextFrame2->FloatSave.DataSelector = 0;
00172 }
00173
00174
00175
if (
KeI386NpxPresent) {
00176 ContextFrame2->FloatSave.Cr0NpxState = 0;
00177 NpxFrame->Cr0NpxState = 0;
00178 NpxFrame->NpxSavedCpu = 0;
00179
if (
KeI386XMMIPresent ==
TRUE) {
00180 ContextFlags |= CONTEXT_EXTENDED_REGISTERS;
00181 }
else {
00182 ContextFlags |=
CONTEXT_FLOATING_POINT;
00183 }
00184
00185
00186
00187
00188
00189 Thread->NpxState = NPX_STATE_NOT_LOADED;
00190 Thread->NpxIrql =
PASSIVE_LEVEL;
00191
00192 }
else {
00193 NpxFrame->Cr0NpxState = CR0_EM;
00194
00195
00196
00197
00198
00199
00200
00201 Thread->NpxState = NPX_STATE_NOT_LOADED & ~CR0_MP;
00202 }
00203
00204
00205
00206
00207
00208
00209 ContextFrame2->Dr0 = 0;
00210 ContextFrame2->Dr1 = 0;
00211 ContextFrame2->Dr2 = 0;
00212 ContextFrame2->Dr3 = 0;
00213 ContextFrame2->Dr6 = 0;
00214 ContextFrame2->Dr7 = 0;
00215 ContextFrame2->ContextFlags &= ~(CONTEXT_DEBUG_REGISTERS);
00216
#if 0
00217
00218
00219
00220
00221
00222
if (Thread->AutoAlignment ==
FALSE) {
00223 ContextFrame2->EFlags |= EFLAGS_ALIGN_CHECK;
00224 }
00225
#endif
00226
00227
00228
00229 TrFrame = (PKTRAP_FRAME)(((ULONG)NpxFrame - KTRAP_FRAME_LENGTH));
00230
00231
00232
00233
00234
00235 PUserContextFlag = (PULONG)TrFrame - 1;
00236 PStartContext = PUserContextFlag - 1;
00237 PStartRoutine = PStartContext - 1;
00238 PSystemRoutine = PStartRoutine - 1;
00239
00240 SwitchFrame = (PKSWITCHFRAME)((PUCHAR)PSystemRoutine -
00241
sizeof(KSWITCHFRAME));
00242
00243
00244
00245
00246
00247
00248
KeContextToKframes(TrFrame,
NULL, ContextFrame2,
00249 ContextFrame2->ContextFlags | ContextFlags,
00250
UserMode);
00251
00252 TrFrame->HardwareSegSs |= RPL_MASK;
00253 TrFrame->SegDs |= RPL_MASK;
00254 TrFrame->SegEs |= RPL_MASK;
00255
00256
#if DBG
00257
TrFrame->DbgArgMark = 0xBADB0D00;
00258
#endif
00259
00260
00261
00262
00263
00264 *PUserContextFlag = 1;
00265
00266
00267
00268
00269
00270
00271 TrFrame->ExceptionList = EXCEPTION_CHAIN_END;
00272
00273
00274
00275
00276
00277 TrFrame->PreviousPreviousMode =
UserMode;
00278
00279
00280
00281
00282
00283 Thread->PreviousMode =
UserMode;
00284
00285
00286 }
else {
00287
00288
00289
00290
00291
00292
00293
00294 NpxFrame = (PFX_SAVE_AREA)(((ULONG)(Thread->InitialStack) -
00295
sizeof(FX_SAVE_AREA)));
00296
00297
00298
00299
00300 RtlZeroMemory((PVOID)NpxFrame,
sizeof(FX_SAVE_AREA));
00301
00302
if (
KeI386FxsrPresent ==
TRUE) {
00303 NpxFrame->U.FxArea.ControlWord = 0x27f;
00304 NpxFrame->U.FxArea.MXCsr = 0x1f80;
00305 }
else {
00306 NpxFrame->U.FnArea.ControlWord = 0x27f;
00307 NpxFrame->U.FnArea.TagWord = 0xffff;
00308 }
00309
00310
00311
00312
00313
00314 Thread->NpxState = NPX_STATE_NOT_LOADED;
00315
00316
00317
00318
00319
00320
00321
00322
00323 PUserContextFlag = (PULONG)((ULONG)NpxFrame) - 1;
00324
00325 PStartContext = PUserContextFlag - 1;
00326 PStartRoutine = PStartContext - 1;
00327 PSystemRoutine = PStartRoutine - 1;
00328
00329 SwitchFrame = (PKSWITCHFRAME)((PUCHAR)PSystemRoutine -
00330
sizeof(KSWITCHFRAME));
00331
00332
00333
00334
00335
00336
00337 *PUserContextFlag = 0;
00338
00339
00340
00341
00342
00343
00344 Thread->PreviousMode =
KernelMode;
00345 }
00346
00347
00348
00349
00350
00351
00352 *PStartContext = (ULONG)StartContext;
00353 *PStartRoutine = (ULONG)StartRoutine;
00354 *PSystemRoutine = (ULONG)SystemRoutine;
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 SwitchFrame->RetAddr = (ULONG)
KiThreadStartup;
00365
00366 SwitchFrame->Eflags = EFLAGS_INTERRUPT_MASK;
00367
00368
#if 0
00369
00370
00371
00372
00373
00374
if (Thread->AutoAlignment ==
FALSE) {
00375 SwitchFrame->Eflags |= EFLAGS_ALIGN_CHECK;
00376 }
00377
#endif
00378
00379 SwitchFrame->ExceptionList = (ULONG)(EXCEPTION_CHAIN_END);
00380
00381
00382
00383
00384
00385
00386
00387
00388 Thread->KernelStack = (PVOID)SwitchFrame;
00389
return;
00390 }
00391
00392 BOOLEAN
00393 KeSetAutoAlignmentProcess (
00394 IN
PKPROCESS Process,
00395 IN BOOLEAN Enable
00396 )
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 {
00424
00425 KIRQL OldIrql;
00426 BOOLEAN Previous;
00427
00428
ASSERT_PROCESS(Process);
00429
00430
00431
00432
00433
00434
KiLockDispatcherDatabase(&OldIrql);
00435
00436
00437
00438
00439
00440
00441 Previous = Process->AutoAlignment;
00442 Process->AutoAlignment = Enable;
00443
00444
00445
00446
00447
00448
00449
KiUnlockDispatcherDatabase(OldIrql);
00450
return Previous;
00451 }
00452
00453 BOOLEAN
00454 KeSetAutoAlignmentThread (
00455 IN
PKTHREAD Thread,
00456 IN BOOLEAN Enable
00457 )
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484 {
00485
00486 BOOLEAN Previous;
00487
PKAPC Apc;
00488
PKEVENT Event;
00489 KIRQL OldIrql;
00490
00491
ASSERT_THREAD(Thread);
00492
00493
00494
00495
00496
00497
KiLockDispatcherDatabase(&OldIrql);
00498
00499
00500
00501
00502
00503
00504 Previous = Thread->AutoAlignment;
00505 Thread->AutoAlignment = Enable;
00506
00507
00508
00509
00510
00511
KiUnlockDispatcherDatabase(OldIrql);
00512
00513
#if 0
00514
Apc =
ExAllocatePool(
NonPagedPoolMustSucceed,
sizeof(
KAPC));
00515
Event =
ExAllocatePool(
NonPagedPoolMustSucceed,
sizeof(
KEVENT));
00516
00517
KeInitializeEvent(
Event, NotificationEvent,
FALSE);
00518
00519
if ( Thread ==
KeGetCurrentThread() ) {
00520
00521 Apc->
SystemArgument1 = Thread;
00522 Apc->
SystemArgument2 =
Event;
00523
00524
KeRaiseIrql(
APC_LEVEL, &Irql);
00525
KepSetAlignmentSpecialApc( Apc,
NULL,
NULL,
00526 &Apc->
SystemArgument1,
00527 &Apc->
SystemArgument2 );
00528
KeLowerIrql(Irql);
00529 }
else {
00530
KeInitializeApc( Apc,
00531 Thread,
00532
CurrentApcEnvironment,
00533
KepSetAlignmentSpecialApc,
00534
NULL,
00535
NULL,
00536
KernelMode,
00537
NULL );
00538
00539
if (!
KeInsertQueueApc( Apc,
00540 Thread,
00541
Event,
00542 2 ) ) {
00543
00544
00545
00546
00547
00548
#if DBG
00549
DbgPrint(
"KeSetAutoAlignmentThread: unable to change thread's context\n");
00550
#endif
00551
Thread->AutoAlignment = Previous;
00552 }
00553
00554
KeWaitForSingleObject(
Event,
00555
Executive,
00556
KernelMode,
00557
FALSE,
00558
NULL );
00559 }
00560
00561
ExFreePool(Apc);
00562
ExFreePool(
Event);
00563
#endif
00564
00565
return(Previous);
00566 }
00567
00568
#if 0
00569
00570
VOID
00571
KepSetAlignmentSpecialApc(
00572 IN
PKAPC Apc,
00573 IN PKNORMAL_ROUTINE *NormalRoutine,
00574 IN PVOID *NormalContext,
00575 IN PVOID *SystemArgument1,
00576 IN PVOID *SystemArgument2
00577 )
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 {
00608
PKTHREAD Thread;
00609
PKEVENT Event;
00610 PKTRAP_FRAME TrapFrame;
00611 CONTEXT ContextFrame;
00612
00613 Thread = *(
PKTHREAD *)SystemArgument1;
00614
Event = *(
PKEVENT *)SystemArgument2;
00615
00616
ASSERT( Thread ==
KeGetCurrentThread() );
00617
00618
00619
00620
00621 TrapFrame = (PKTRAP_FRAME)((PUCHAR)Thread->
InitialStack -
00622
ALIGN_UP(
sizeof(KTRAP_FRAME),KTRAP_FRAME_ALIGN) -
00623
sizeof(FX_SAVE_AREA));
00624
00625 ContextFrame.ContextFlags =
CONTEXT_CONTROL;
00626
00627
KeContextFromKframes( TrapFrame,
00628 NULL,
00629 &ContextFrame );
00630
00631
00632
00633
00634
00635
00636
00637
00638
if (Thread->
AutoAlignment) {
00639 ContextFrame.EFlags &= (~EFLAGS_ALIGN_CHECK);
00640 }
else {
00641 ContextFrame.EFlags |= EFLAGS_ALIGN_CHECK;
00642 }
00643
00644
00645
00646
00647
00648
00649
KeContextToKframes( TrapFrame,
00650 NULL,
00651 &ContextFrame,
00652 CONTEXT_CONTROL,
00653 KeGetPreviousMode() );
00654
00655
KeSetEvent(Event,0,FALSE);
00656 }
00657
#endif