00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "psp.h"
00022
00023 ULONGLONG
00024 PspGetSavedValue (
00025 IN PVOID ContextPointer
00026 )
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 {
00049
00050
00051
00052
00053
00054
00055
if (((ULONG)ContextPointer & 1) != 0) {
00056
return *((PULONGLONG)((ULONG)ContextPointer & ~1));
00057
00058 }
else {
00059
return *((PLONG)ContextPointer);
00060 }
00061 }
00062
00063
VOID
00064 PspSetSavedValue (
00065 IN ULONGLONG ContextValue,
00066 IN PVOID ContextPointer
00067 )
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 {
00092
00093
00094
00095
00096
00097
00098
if (((ULONG)ContextPointer & 1) != 0) {
00099 *((PULONGLONG)((ULONG)ContextPointer & ~1)) = ContextValue;
00100
00101 }
else {
00102 *((PULONG)ContextPointer) = (ULONG)ContextValue;
00103 }
00104 }
00105
00106
VOID
00107 PspGetContext (
00108 IN PKTRAP_FRAME TrapFrame,
00109 IN PKNONVOLATILE_CONTEXT_POINTERS ContextPointers,
00110 IN OUT PCONTEXT ContextRecord
00111 )
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 {
00135
00136 ULONG ContextFlags;
00137 LONG
Index;
00138
00139
00140
00141
00142
00143 ContextFlags = ContextRecord->ContextFlags;
00144
if ((ContextFlags &
CONTEXT_CONTROL) ==
CONTEXT_CONTROL) {
00145
00146
00147
00148
00149
00150 ContextRecord->Fir = TrapFrame->Fir;
00151 ContextRecord->Psr = TrapFrame->Psr;
00152
if ((ContextFlags &
CONTEXT_EXTENDED_INTEGER) !=
CONTEXT_EXTENDED_INTEGER) {
00153 ContextRecord->IntGp = (ULONG)TrapFrame->XIntGp;
00154 ContextRecord->IntSp = (ULONG)TrapFrame->XIntSp;
00155 ContextRecord->IntRa = (ULONG)TrapFrame->XIntRa;
00156
00157 }
else {
00158 ContextRecord->XIntGp = TrapFrame->XIntGp;
00159 ContextRecord->XIntSp = TrapFrame->XIntSp;
00160 ContextRecord->XIntRa = TrapFrame->XIntRa;
00161 }
00162 }
00163
00164
if ((ContextFlags &
CONTEXT_INTEGER) ==
CONTEXT_INTEGER) {
00165
00166
00167
00168
00169
00170
if ((ContextFlags &
CONTEXT_EXTENDED_INTEGER) !=
CONTEXT_EXTENDED_INTEGER) {
00171 ContextRecord->IntZero = 0;
00172 ContextRecord->IntAt = (ULONG)TrapFrame->XIntAt;
00173 ContextRecord->IntV0 = (ULONG)TrapFrame->XIntV0;
00174 ContextRecord->IntV1 = (ULONG)TrapFrame->XIntV1;
00175 ContextRecord->IntA0 = (ULONG)TrapFrame->XIntA0;
00176 ContextRecord->IntA1 = (ULONG)TrapFrame->XIntA1;
00177 ContextRecord->IntA2 = (ULONG)TrapFrame->XIntA2;
00178 ContextRecord->IntA3 = (ULONG)TrapFrame->XIntA3;
00179 ContextRecord->IntT0 = (ULONG)TrapFrame->XIntT0;
00180 ContextRecord->IntT1 = (ULONG)TrapFrame->XIntT1;
00181 ContextRecord->IntT2 = (ULONG)TrapFrame->XIntT2;
00182 ContextRecord->IntT3 = (ULONG)TrapFrame->XIntT3;
00183 ContextRecord->IntT4 = (ULONG)TrapFrame->XIntT4;
00184 ContextRecord->IntT5 = (ULONG)TrapFrame->XIntT5;
00185 ContextRecord->IntT6 = (ULONG)TrapFrame->XIntT6;
00186 ContextRecord->IntT7 = (ULONG)TrapFrame->XIntT7;
00187 ContextRecord->IntT8 = (ULONG)TrapFrame->XIntT8;
00188 ContextRecord->IntT9 = (ULONG)TrapFrame->XIntT9;
00189 ContextRecord->IntK0 = 0;
00190 ContextRecord->IntK1 = 0;
00191 ContextRecord->IntLo = (ULONG)TrapFrame->XIntLo;
00192 ContextRecord->IntHi = (ULONG)TrapFrame->XIntHi;
00193
00194 }
else {
00195 ContextRecord->XIntZero = 0;
00196 ContextRecord->XIntAt = TrapFrame->XIntAt;
00197 ContextRecord->XIntV0 = TrapFrame->XIntV0;
00198 ContextRecord->XIntV1 = TrapFrame->XIntV1;
00199 ContextRecord->XIntA0 = TrapFrame->XIntA0;
00200 ContextRecord->XIntA1 = TrapFrame->XIntA1;
00201 ContextRecord->XIntA2 = TrapFrame->XIntA2;
00202 ContextRecord->XIntA3 = TrapFrame->XIntA3;
00203 ContextRecord->XIntT0 = TrapFrame->XIntT0;
00204 ContextRecord->XIntT1 = TrapFrame->XIntT1;
00205 ContextRecord->XIntT2 = TrapFrame->XIntT2;
00206 ContextRecord->XIntT3 = TrapFrame->XIntT3;
00207 ContextRecord->XIntT4 = TrapFrame->XIntT4;
00208 ContextRecord->XIntT5 = TrapFrame->XIntT5;
00209 ContextRecord->XIntT6 = TrapFrame->XIntT6;
00210 ContextRecord->XIntT7 = TrapFrame->XIntT7;
00211 ContextRecord->XIntT8 = TrapFrame->XIntT8;
00212 ContextRecord->XIntT9 = TrapFrame->XIntT9;
00213 ContextRecord->XIntK0 = 0;
00214 ContextRecord->XIntK1 = 0;
00215 ContextRecord->XIntLo = TrapFrame->XIntLo;
00216 ContextRecord->XIntHi = TrapFrame->XIntHi;
00217 }
00218
00219
00220
00221
00222
00223
if ((ContextFlags &
CONTEXT_EXTENDED_INTEGER) !=
CONTEXT_EXTENDED_INTEGER) {
00224
Index = 7;
00225
do {
00226
if (TrapFrame->SavedFlag == 0) {
00227 (&ContextRecord->IntS0)[
Index] =
00228 (ULONG)
PspGetSavedValue((&ContextPointers->XIntS0)[
Index]);
00229
00230 }
else {
00231 (&ContextRecord->IntS0)[
Index] = (ULONG)(&TrapFrame->XIntS0)[
Index];
00232 }
00233
00234
Index -= 1;
00235 }
while (
Index >= 0);
00236 ContextRecord->IntS8 = (ULONG)TrapFrame->XIntS8;
00237
00238 }
else {
00239
Index = 7;
00240
do {
00241
if (TrapFrame->SavedFlag == 0) {
00242 (&ContextRecord->XIntS0)[
Index] =
00243
PspGetSavedValue((&ContextPointers->XIntS0)[
Index]);
00244
00245 }
else {
00246 (&ContextRecord->XIntS0)[
Index] = (&TrapFrame->XIntS0)[
Index];
00247 }
00248
00249
Index -= 1;
00250 }
while (
Index >= 0);
00251 ContextRecord->XIntS8 = TrapFrame->XIntS8;
00252 }
00253 }
00254
00255
if ((ContextFlags &
CONTEXT_FLOATING_POINT) ==
CONTEXT_FLOATING_POINT) {
00256
00257
00258
00259
00260
00261 RtlMoveMemory(&ContextRecord->FltF0, &TrapFrame->FltF0,
00262
sizeof(ULONG) * (20));
00263
00264
00265
00266
00267
00268 ContextRecord->FltF20 = *ContextPointers->FltF20;
00269 ContextRecord->FltF21 = *ContextPointers->FltF21;
00270 ContextRecord->FltF22 = *ContextPointers->FltF22;
00271 ContextRecord->FltF23 = *ContextPointers->FltF23;
00272 ContextRecord->FltF24 = *ContextPointers->FltF24;
00273 ContextRecord->FltF25 = *ContextPointers->FltF25;
00274 ContextRecord->FltF26 = *ContextPointers->FltF26;
00275 ContextRecord->FltF27 = *ContextPointers->FltF27;
00276 ContextRecord->FltF28 = *ContextPointers->FltF28;
00277 ContextRecord->FltF29 = *ContextPointers->FltF29;
00278 ContextRecord->FltF30 = *ContextPointers->FltF30;
00279 ContextRecord->FltF31 = *ContextPointers->FltF31;
00280
00281
00282
00283
00284
00285 ContextRecord->Fsr = TrapFrame->Fsr;
00286 }
00287
00288
return;
00289 }
00290
00291
VOID
00292 PspSetContext (
00293 IN OUT PKTRAP_FRAME TrapFrame,
00294 IN PKNONVOLATILE_CONTEXT_POINTERS ContextPointers,
00295 IN PCONTEXT ContextRecord,
00296 IN KPROCESSOR_MODE ProcessorMode
00297 )
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 {
00324
00325 ULONG ContextFlags;
00326 LONG
Index;
00327
00328
00329
00330
00331
00332 ContextFlags = ContextRecord->ContextFlags;
00333
if ((ContextFlags &
CONTEXT_CONTROL) ==
CONTEXT_CONTROL) {
00334
00335
00336
00337
00338
00339 TrapFrame->Fir = ContextRecord->Fir;
00340 TrapFrame->Psr = SANITIZE_PSR(ContextRecord->Psr, ProcessorMode);
00341
if ((ContextFlags &
CONTEXT_EXTENDED_INTEGER) !=
CONTEXT_EXTENDED_INTEGER) {
00342 TrapFrame->XIntGp = (LONG)ContextRecord->IntGp;
00343 TrapFrame->XIntSp = (LONG)ContextRecord->IntSp;
00344 TrapFrame->XIntRa = (LONG)ContextRecord->IntRa;
00345
00346 }
else {
00347 TrapFrame->XIntGp = ContextRecord->XIntGp;
00348 TrapFrame->XIntSp = ContextRecord->XIntSp;
00349 TrapFrame->XIntRa = ContextRecord->XIntRa;
00350 }
00351 }
00352
00353
if ((ContextFlags &
CONTEXT_INTEGER) ==
CONTEXT_INTEGER) {
00354
00355
00356
00357
00358
00359
if ((ContextFlags &
CONTEXT_EXTENDED_INTEGER) !=
CONTEXT_EXTENDED_INTEGER) {
00360 TrapFrame->XIntAt = (LONG)ContextRecord->IntAt;
00361 TrapFrame->XIntV0 = (LONG)ContextRecord->IntV0;
00362 TrapFrame->XIntV1 = (LONG)ContextRecord->IntV1;
00363 TrapFrame->XIntA0 = (LONG)ContextRecord->IntA0;
00364 TrapFrame->XIntA1 = (LONG)ContextRecord->IntA1;
00365 TrapFrame->XIntA2 = (LONG)ContextRecord->IntA2;
00366 TrapFrame->XIntA3 = (LONG)ContextRecord->IntA3;
00367 TrapFrame->XIntT0 = (LONG)ContextRecord->IntT0;
00368 TrapFrame->XIntT1 = (LONG)ContextRecord->IntT1;
00369 TrapFrame->XIntT2 = (LONG)ContextRecord->IntT2;
00370 TrapFrame->XIntT3 = (LONG)ContextRecord->IntT3;
00371 TrapFrame->XIntT4 = (LONG)ContextRecord->IntT4;
00372 TrapFrame->XIntT5 = (LONG)ContextRecord->IntT5;
00373 TrapFrame->XIntT6 = (LONG)ContextRecord->IntT6;
00374 TrapFrame->XIntT7 = (LONG)ContextRecord->IntT7;
00375 TrapFrame->XIntT8 = (LONG)ContextRecord->IntT8;
00376 TrapFrame->XIntT9 = (LONG)ContextRecord->IntT9;
00377 TrapFrame->XIntLo = (LONG)ContextRecord->IntLo;
00378 TrapFrame->XIntHi = (LONG)ContextRecord->IntHi;
00379
00380 }
else {
00381 TrapFrame->XIntAt = ContextRecord->XIntAt;
00382 TrapFrame->XIntV0 = ContextRecord->XIntV0;
00383 TrapFrame->XIntV1 = ContextRecord->XIntV1;
00384 TrapFrame->XIntA0 = ContextRecord->XIntA0;
00385 TrapFrame->XIntA1 = ContextRecord->XIntA1;
00386 TrapFrame->XIntA2 = ContextRecord->XIntA2;
00387 TrapFrame->XIntA3 = ContextRecord->XIntA3;
00388 TrapFrame->XIntT0 = ContextRecord->XIntT0;
00389 TrapFrame->XIntT1 = ContextRecord->XIntT1;
00390 TrapFrame->XIntT2 = ContextRecord->XIntT2;
00391 TrapFrame->XIntT3 = ContextRecord->XIntT3;
00392 TrapFrame->XIntT4 = ContextRecord->XIntT4;
00393 TrapFrame->XIntT5 = ContextRecord->XIntT5;
00394 TrapFrame->XIntT6 = ContextRecord->XIntT6;
00395 TrapFrame->XIntT7 = ContextRecord->XIntT7;
00396 TrapFrame->XIntT8 = ContextRecord->XIntT8;
00397 TrapFrame->XIntT9 = ContextRecord->XIntT9;
00398 TrapFrame->XIntLo = ContextRecord->XIntLo;
00399 TrapFrame->XIntHi = ContextRecord->XIntHi;
00400 }
00401
00402
00403
00404
00405
00406
if ((ContextFlags &
CONTEXT_EXTENDED_INTEGER) !=
CONTEXT_EXTENDED_INTEGER) {
00407
Index = 7;
00408
do {
00409
if (TrapFrame->SavedFlag == 0) {
00410
PspSetSavedValue((LONG)(&ContextRecord->IntS0)[
Index],
00411 (&ContextPointers->XIntS0)[
Index]);
00412
00413 }
else {
00414 (&TrapFrame->XIntS0)[
Index] = (LONG)(&ContextRecord->IntS0)[
Index];
00415 }
00416
00417
Index -= 1;
00418 }
while (
Index >= 0);
00419 TrapFrame->XIntS8 = (LONG)ContextRecord->IntS8;
00420
00421 }
else {
00422
Index = 7;
00423
do {
00424
if (TrapFrame->SavedFlag == 0) {
00425
PspSetSavedValue((&ContextRecord->XIntS0)[
Index],
00426 (&ContextPointers->XIntS0)[
Index]);
00427
00428 }
else {
00429 (&TrapFrame->XIntS0)[
Index] = (&ContextRecord->XIntS0)[
Index];
00430 }
00431
00432
Index -= 1;
00433 }
while (
Index >= 0);
00434 TrapFrame->XIntS8 = ContextRecord->XIntS8;
00435 }
00436 }
00437
00438
if ((ContextFlags &
CONTEXT_FLOATING_POINT) ==
CONTEXT_FLOATING_POINT) {
00439
00440
00441
00442
00443
00444 RtlMoveMemory(&TrapFrame->FltF0, &ContextRecord->FltF0,
00445
sizeof(ULONG) * (20));
00446
00447
00448
00449
00450
00451 *ContextPointers->FltF20 = ContextRecord->FltF20;
00452 *ContextPointers->FltF21 = ContextRecord->FltF21;
00453 *ContextPointers->FltF22 = ContextRecord->FltF22;
00454 *ContextPointers->FltF23 = ContextRecord->FltF23;
00455 *ContextPointers->FltF24 = ContextRecord->FltF24;
00456 *ContextPointers->FltF25 = ContextRecord->FltF25;
00457 *ContextPointers->FltF26 = ContextRecord->FltF26;
00458 *ContextPointers->FltF27 = ContextRecord->FltF27;
00459 *ContextPointers->FltF28 = ContextRecord->FltF28;
00460 *ContextPointers->FltF29 = ContextRecord->FltF29;
00461 *ContextPointers->FltF30 = ContextRecord->FltF30;
00462 *ContextPointers->FltF31 = ContextRecord->FltF31;
00463
00464
00465
00466
00467
00468 TrapFrame->Fsr = SANITIZE_FSR(ContextRecord->Fsr, ProcessorMode);
00469 }
00470
00471
return;
00472 }
00473
00474
VOID
00475 PspGetSetContextSpecialApc (
00476 IN
PKAPC Apc,
00477 IN PKNORMAL_ROUTINE *NormalRoutine,
00478 IN PVOID *NormalContext,
00479 IN PVOID *SystemArgument1,
00480 IN PVOID *SystemArgument2
00481 )
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 {
00516
00517
PGETSETCONTEXT ContextBlock;
00518 KNONVOLATILE_CONTEXT_POINTERS ContextPointers;
00519 CONTEXT ContextRecord;
00520 ULONG ControlPc;
00521 ULONG EstablisherFrame;
00522 PRUNTIME_FUNCTION FunctionEntry;
00523 BOOLEAN InFunction;
00524
PETHREAD Thread;
00525 ULONG TrapFrame1;
00526 ULONG TrapFrame2;
00527
00528
00529
00530
00531
00532
00533 ContextBlock = CONTAINING_RECORD(Apc,
GETSETCONTEXT, Apc);
00534 Thread =
PsGetCurrentThread();
00535 TrapFrame1 = (ULONG)Thread->
Tcb.
InitialStack - KTRAP_FRAME_LENGTH;
00536 TrapFrame2 = (ULONG)Thread->
Tcb.
InitialStack - KTRAP_FRAME_LENGTH - KTRAP_FRAME_ARGUMENTS;
00537
00538
00539
00540
00541
00542
00543 RtlCaptureContext(&ContextRecord);
00544 ControlPc = (ULONG)ContextRecord.XIntRa;
00545
00546
00547
00548
00549
00550
00551 ContextPointers.XIntS0 = &ContextRecord.XIntS0;
00552 ContextPointers.XIntS1 = &ContextRecord.XIntS1;
00553 ContextPointers.XIntS2 = &ContextRecord.XIntS2;
00554 ContextPointers.XIntS3 = &ContextRecord.XIntS3;
00555 ContextPointers.XIntS4 = &ContextRecord.XIntS4;
00556 ContextPointers.XIntS5 = &ContextRecord.XIntS5;
00557 ContextPointers.XIntS6 = &ContextRecord.XIntS6;
00558 ContextPointers.XIntS7 = &ContextRecord.XIntS7;
00559
00560 ContextPointers.FltF20 = &ContextRecord.FltF20;
00561 ContextPointers.FltF21 = &ContextRecord.FltF21;
00562 ContextPointers.FltF22 = &ContextRecord.FltF22;
00563 ContextPointers.FltF23 = &ContextRecord.FltF23;
00564 ContextPointers.FltF24 = &ContextRecord.FltF24;
00565 ContextPointers.FltF25 = &ContextRecord.FltF25;
00566 ContextPointers.FltF26 = &ContextRecord.FltF26;
00567 ContextPointers.FltF27 = &ContextRecord.FltF27;
00568 ContextPointers.FltF28 = &ContextRecord.FltF28;
00569 ContextPointers.FltF29 = &ContextRecord.FltF29;
00570 ContextPointers.FltF30 = &ContextRecord.FltF30;
00571 ContextPointers.FltF31 = &ContextRecord.FltF31;
00572
00573
00574
00575
00576
00577
00578
do {
00579
00580
00581
00582
00583
00584
00585 FunctionEntry =
RtlLookupFunctionEntry(ControlPc);
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
if (FunctionEntry !=
NULL) {
00596 ControlPc =
RtlVirtualUnwind(ControlPc | 1,
00597 FunctionEntry,
00598 &ContextRecord,
00599 &InFunction,
00600 &EstablisherFrame,
00601 &ContextPointers);
00602
00603 }
else {
00604 ControlPc = (ULONG)ContextRecord.XIntRa;
00605 }
00606
00607 }
while (((ULONG)ContextRecord.XIntSp != TrapFrame1) &&
00608 (((ULONG)ContextRecord.XIntSp != TrapFrame2) ||
00609 (ControlPc < PCR->SystemServiceDispatchStart) ||
00610 (ControlPc >= PCR->SystemServiceDispatchEnd)));
00611
00612
00613
00614
00615
00616
00617
if (Apc->SystemArgument1 != 0) {
00618
00619
00620
00621
00622
00623
PspSetContext((PKTRAP_FRAME)TrapFrame1,
00624 &ContextPointers,
00625 &ContextBlock->
Context,
00626 ContextBlock->
Mode);
00627
00628 }
else {
00629
00630
00631
00632
00633
00634
PspGetContext((PKTRAP_FRAME)TrapFrame1,
00635 &ContextPointers,
00636 &ContextBlock->
Context);
00637 }
00638
00639
KeSetEvent(&ContextBlock->
OperationComplete, 0,
FALSE);
00640
return;
00641 }