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
#include "ntrtlp.h"
00027
00028
00029
00030
00031
00032
00033
00034 #define RAISE_EXCEPTION(Status, ExceptionRecordt) { \
00035
EXCEPTION_RECORD ExceptionRecordn; \
00036
\
00037
ExceptionRecordn.ExceptionCode = Status; \
00038
ExceptionRecordn.ExceptionFlags = EXCEPTION_NONCONTINUABLE; \
00039
ExceptionRecordn.ExceptionRecord = ExceptionRecordt; \
00040
ExceptionRecordn.NumberParameters = 0; \
00041
RtlRaiseException(&ExceptionRecordn); \
00042
}
00043
00044
00045
00046
00047
00048 #define RA 0x1f // integer register 31
00049 #define SP 0x1d // integer register 29
00050 #define ZERO 0x0 // integer register 0
00051
00052
00053
00054
00055
00056 #define SAVED_FLOATING_MASK 0xfff00000 // saved floating registers
00057 #define SAVED_INTEGER_MASK 0xf3ffff02 // saved integer registers
00058
00059
00060
00061
00062
00063
VOID
00064
RtlpRestoreContext (
00065 IN PCONTEXT Context,
00066 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL
00067 );
00068
00069
VOID
00070
RtlpRaiseException (
00071 IN PEXCEPTION_RECORD ExceptionRecord
00072 );
00073
00074
VOID
00075
RtlpRaiseStatus (
00076 IN NTSTATUS Status
00077 );
00078
00079 ULONG
00080
RtlpVirtualUnwind (
00081 IN ULONG ControlPc,
00082 IN PRUNTIME_FUNCTION FunctionEntry,
00083 IN PCONTEXT ContextRecord,
00084 OUT PBOOLEAN InFunction,
00085 OUT PULONG EstablisherFrame,
00086 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
00087 );
00088
00089 ULONG
00090
RtlpVirtualUnwind32 (
00091 IN ULONG ControlPc,
00092 IN PRUNTIME_FUNCTION FunctionEntry,
00093 IN OUT PCONTEXT ContextRecord,
00094 OUT PBOOLEAN InFunction,
00095 OUT PULONG EstablisherFrame,
00096 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
00097 );
00098
00099
00100 BOOLEAN
00101 RtlDispatchException (
00102 IN PEXCEPTION_RECORD ExceptionRecord,
00103 IN PCONTEXT ContextRecord
00104 )
00105
00106
00107
00108
00109
00110
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
00137
00138 {
00139
00140 CONTEXT ContextRecord1;
00141 ULONG ControlPc;
00142
DISPATCHER_CONTEXT DispatcherContext;
00143 EXCEPTION_DISPOSITION Disposition;
00144 ULONG EstablisherFrame;
00145 ULONG ExceptionFlags;
00146 PRUNTIME_FUNCTION FunctionEntry;
00147 ULONG
Index;
00148 BOOLEAN InFunction;
00149 ULONG HighLimit;
00150 ULONG LowLimit;
00151 ULONG NestedFrame;
00152 ULONG NextPc;
00153
00154
00155
00156
00157
00158
00159
00160
RtlpGetStackLimits(&LowLimit, &HighLimit);
00161 RtlMoveMemory(&ContextRecord1, ContextRecord,
sizeof(CONTEXT));
00162 ControlPc = ContextRecord1.Fir;
00163 ExceptionFlags = ExceptionRecord->ExceptionFlags &
EXCEPTION_NONCONTINUABLE;
00164 NestedFrame = 0;
00165
00166
00167
00168
00169
00170
00171
00172
do {
00173
00174
00175
00176
00177
00178
00179 FunctionEntry =
RtlLookupFunctionEntry(ControlPc);
00180
00181
00182
00183
00184
00185
00186
00187
00188
if (FunctionEntry !=
NULL) {
00189 NextPc =
RtlVirtualUnwind(ControlPc | 1,
00190 FunctionEntry,
00191 &ContextRecord1,
00192 &InFunction,
00193 &EstablisherFrame,
00194
NULL);
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
if ((EstablisherFrame < LowLimit) || (EstablisherFrame > HighLimit) ||
00205 ((EstablisherFrame & 0x7) != 0)) {
00206 ExceptionFlags |=
EXCEPTION_STACK_INVALID;
00207
break;
00208
00209 }
else if ((FunctionEntry->ExceptionHandler !=
NULL) && InFunction) {
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 DispatcherContext.ControlPc = ControlPc;
00220 DispatcherContext.FunctionEntry = FunctionEntry;
00221 DispatcherContext.EstablisherFrame = EstablisherFrame;
00222 DispatcherContext.ContextRecord = ContextRecord;
00223 ExceptionRecord->ExceptionFlags = ExceptionFlags;
00224
00225
00226
00227
00228
00229
if (
NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
00230
Index =
RtlpLogExceptionHandler(ExceptionRecord,
00231 ContextRecord,
00232 ControlPc,
00233 FunctionEntry,
00234
sizeof(RUNTIME_FUNCTION));
00235 }
00236
00237 Disposition =
00238
RtlpExecuteHandlerForException(ExceptionRecord,
00239 EstablisherFrame,
00240 ContextRecord,
00241 &DispatcherContext,
00242 FunctionEntry->ExceptionHandler);
00243
00244
if (
NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
00245
RtlpLogLastExceptionDisposition(
Index, Disposition);
00246 }
00247
00248 ExceptionFlags |=
00249 (ExceptionRecord->ExceptionFlags &
EXCEPTION_NONCONTINUABLE);
00250
00251
00252
00253
00254
00255
00256
00257
00258
if (NestedFrame == EstablisherFrame) {
00259 ExceptionFlags &= (~
EXCEPTION_NESTED_CALL);
00260 NestedFrame = 0;
00261 }
00262
00263
00264
00265
00266
00267
switch (Disposition) {
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
case ExceptionContinueExecution :
00278
if ((ExceptionFlags &
EXCEPTION_NONCONTINUABLE) != 0) {
00279
RAISE_EXCEPTION(STATUS_NONCONTINUABLE_EXCEPTION, ExceptionRecord);
00280
00281 }
else {
00282
return TRUE;
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
case ExceptionContinueSearch :
00292
break;
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
case ExceptionNestedException :
00303 ExceptionFlags |=
EXCEPTION_NESTED_CALL;
00304
if (DispatcherContext.EstablisherFrame > NestedFrame) {
00305 NestedFrame = DispatcherContext.EstablisherFrame;
00306 }
00307
00308
break;
00309
00310
00311
00312
00313
00314
00315
00316
default :
00317
RAISE_EXCEPTION(STATUS_INVALID_DISPOSITION, ExceptionRecord);
00318 }
00319 }
00320
00321 }
else {
00322
00323
00324
00325
00326
00327 NextPc = (ULONG)(ContextRecord1.XIntRa - 4);
00328
00329
00330
00331
00332
00333
00334
if (NextPc == ControlPc) {
00335
break;
00336 }
00337 }
00338
00339
00340
00341
00342
00343 ControlPc = NextPc;
00344 }
while ((ULONG)ContextRecord1.XIntSp < HighLimit);
00345
00346
00347
00348
00349
00350 ExceptionRecord->ExceptionFlags = ExceptionFlags;
00351
return FALSE;
00352 }
00353
00354 PRUNTIME_FUNCTION
00355 RtlLookupFunctionEntry (
00356 IN ULONG ControlPc
00357 )
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 {
00380
00381 PRUNTIME_FUNCTION FunctionEntry;
00382 PRUNTIME_FUNCTION FunctionTable;
00383 ULONG SizeOfExceptionTable;
00384 LONG High;
00385 PVOID ImageBase;
00386 LONG Low;
00387 LONG Middle;
00388
USHORT i;
00389
00390
00391
00392
00393
00394 ImageBase =
RtlPcToFileHeader((PVOID)ControlPc, &ImageBase);
00395
00396
00397
00398
00399
00400
00401
if (ImageBase !=
NULL) {
00402 FunctionTable = (PRUNTIME_FUNCTION)
RtlImageDirectoryEntryToData(
00403 ImageBase,
TRUE, IMAGE_DIRECTORY_ENTRY_EXCEPTION,
00404 &SizeOfExceptionTable);
00405
00406
00407
00408
00409
00410
00411
if (FunctionTable !=
NULL) {
00412
00413
00414
00415
00416
00417 Low = 0;
00418 High = (SizeOfExceptionTable /
sizeof(RUNTIME_FUNCTION)) - 1;
00419
00420
00421
00422
00423
00424
00425
while (High >= Low) {
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 Middle = (Low + High) >> 1;
00436 FunctionEntry = &FunctionTable[Middle];
00437
if (ControlPc < FunctionEntry->BeginAddress) {
00438 High = Middle - 1;
00439
00440 }
else if (ControlPc >= FunctionEntry->EndAddress) {
00441 Low = Middle + 1;
00442
00443 }
else {
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
if ((FunctionEntry->PrologEndAddress < FunctionEntry->BeginAddress) ||
00457 (FunctionEntry->PrologEndAddress > FunctionEntry->EndAddress)) {
00458 FunctionEntry = (PRUNTIME_FUNCTION)FunctionEntry->PrologEndAddress;
00459 }
00460
00461
return FunctionEntry;
00462 }
00463 }
00464 }
00465 }
00466
00467
00468
00469
00470
00471
return NULL;
00472 }
00473
00474
VOID
00475 RtlRaiseException (
00476 IN PEXCEPTION_RECORD ExceptionRecord
00477 )
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 {
00502
00503
RtlpRaiseException(ExceptionRecord);
00504
return;
00505 }
00506
00507
VOID
00508
RtlpRaiseException (
00509 IN PEXCEPTION_RECORD ExceptionRecord
00510 )
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 {
00530
00531 ULONG ControlPc;
00532 CONTEXT ContextRecord;
00533 ULONG EstablisherFrame;
00534 PRUNTIME_FUNCTION FunctionEntry;
00535 BOOLEAN InFunction;
00536 ULONG NextPc;
00537
NTSTATUS Status;
00538
00539
00540
00541
00542
00543
00544
00545 RtlCaptureContext(&ContextRecord);
00546 ControlPc = (ULONG)(ContextRecord.XIntRa - 4);
00547 FunctionEntry =
RtlLookupFunctionEntry(ControlPc);
00548 NextPc =
RtlVirtualUnwind(ControlPc | 1,
00549 FunctionEntry,
00550 &ContextRecord,
00551 &InFunction,
00552 &EstablisherFrame,
00553 NULL);
00554
00555 ContextRecord.Fir = NextPc + 4;
00556 ExceptionRecord->ExceptionAddress = (PVOID)ContextRecord.Fir;
00557
Status = ZwRaiseException(ExceptionRecord, &ContextRecord, TRUE);
00558
00559
00560
00561
00562
00563
00564
00565
RtlRaiseStatus(Status);
00566
return;
00567 }
00568
00569
VOID
00570 RtlRaiseStatus (
00571 IN NTSTATUS Status
00572 )
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597 {
00598
00599
RtlpRaiseStatus(
Status);
00600
return;
00601 }
00602
00603
VOID
00604
RtlpRaiseStatus (
00605 IN NTSTATUS Status
00606 )
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 {
00627
00628 ULONG ControlPc;
00629 CONTEXT ContextRecord;
00630 ULONG EstablisherFrame;
00631 EXCEPTION_RECORD ExceptionRecord;
00632 PRUNTIME_FUNCTION FunctionEntry;
00633 BOOLEAN InFunction;
00634 ULONG NextPc;
00635
00636
00637
00638
00639
00640 ExceptionRecord.ExceptionCode =
Status;
00641 ExceptionRecord.ExceptionRecord = (PEXCEPTION_RECORD)
NULL;
00642 ExceptionRecord.NumberParameters = 0;
00643 ExceptionRecord.ExceptionFlags =
EXCEPTION_NONCONTINUABLE;
00644
00645
00646
00647
00648
00649
00650
00651 RtlCaptureContext(&ContextRecord);
00652 ControlPc = (ULONG)(ContextRecord.XIntRa - 4);
00653 FunctionEntry =
RtlLookupFunctionEntry(ControlPc);
00654 NextPc =
RtlVirtualUnwind(ControlPc | 1,
00655 FunctionEntry,
00656 &ContextRecord,
00657 &InFunction,
00658 &EstablisherFrame,
00659 NULL);
00660
00661 ContextRecord.Fir = NextPc + 4;
00662 ExceptionRecord.ExceptionAddress = (PVOID)ContextRecord.Fir;
00663
Status = ZwRaiseException(&ExceptionRecord, &ContextRecord, TRUE);
00664
00665
00666
00667
00668
00669
00670
00671
RtlRaiseStatus(Status);
00672
return;
00673 }
00674
00675
VOID
00676 RtlUnwind (
00677 IN PVOID TargetFrame OPTIONAL,
00678 IN PVOID TargetIp OPTIONAL,
00679 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
00680 IN PVOID ReturnValue
00681 )
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 {
00724
00725 CONTEXT ContextRecord;
00726
00727
00728
00729
00730
00731
00732
RtlUnwind2(TargetFrame,
00733 TargetIp,
00734 ExceptionRecord,
00735 ReturnValue,
00736 &ContextRecord);
00737
00738
return;
00739 }
00740
00741
VOID
00742 RtlUnwind2 (
00743 IN PVOID TargetFrame OPTIONAL,
00744 IN PVOID TargetIp OPTIONAL,
00745 IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
00746 IN PVOID ReturnValue,
00747 IN PCONTEXT ContextRecord
00748 )
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791 {
00792
00793 ULONG ControlPc;
00794
DISPATCHER_CONTEXT DispatcherContext;
00795 EXCEPTION_DISPOSITION Disposition;
00796 ULONG EstablisherFrame;
00797 ULONG ExceptionFlags;
00798 EXCEPTION_RECORD ExceptionRecord1;
00799 PRUNTIME_FUNCTION FunctionEntry;
00800 BOOLEAN InFunction;
00801 ULONG HighLimit;
00802 ULONG LowLimit;
00803 ULONG NextPc;
00804
00805
00806
00807
00808
00809
00810
00811
RtlpGetStackLimits(&LowLimit, &HighLimit);
00812 RtlCaptureContext(ContextRecord);
00813 ControlPc = (ULONG)(ContextRecord->XIntRa - 4);
00814 FunctionEntry =
RtlLookupFunctionEntry(ControlPc);
00815 NextPc =
RtlVirtualUnwind(ControlPc | 1,
00816 FunctionEntry,
00817 ContextRecord,
00818 &InFunction,
00819 &EstablisherFrame,
00820
NULL);
00821
00822 ControlPc = NextPc;
00823 ContextRecord->Fir = (ULONG)TargetIp;
00824
00825
00826
00827
00828
00829
00830
if (ARGUMENT_PRESENT(ExceptionRecord) ==
FALSE) {
00831 ExceptionRecord = &ExceptionRecord1;
00832 ExceptionRecord1.ExceptionCode = STATUS_UNWIND;
00833 ExceptionRecord1.ExceptionRecord =
NULL;
00834 ExceptionRecord1.ExceptionAddress = (PVOID)ControlPc;
00835 ExceptionRecord1.NumberParameters = 0;
00836 }
00837
00838
00839
00840
00841
00842
00843 ExceptionFlags =
EXCEPTION_UNWINDING;
00844
if (ARGUMENT_PRESENT(TargetFrame) ==
FALSE) {
00845 ExceptionRecord->ExceptionFlags |=
EXCEPTION_EXIT_UNWIND;
00846 }
00847
00848
00849
00850
00851
00852
00853
do {
00854
00855
00856
00857
00858
00859
00860 FunctionEntry =
RtlLookupFunctionEntry(ControlPc);
00861
00862
00863
00864
00865
00866
00867
00868
if (FunctionEntry !=
NULL) {
00869 NextPc =
RtlpVirtualUnwind(ControlPc,
00870 FunctionEntry,
00871 ContextRecord,
00872 &InFunction,
00873 &EstablisherFrame,
00874
NULL);
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
if ((EstablisherFrame < LowLimit) || (EstablisherFrame > HighLimit) ||
00886 ((ARGUMENT_PRESENT(TargetFrame) !=
FALSE) &&
00887 ((ULONG)TargetFrame < EstablisherFrame)) ||
00888 ((EstablisherFrame & 0x7) != 0)) {
00889
RAISE_EXCEPTION(STATUS_BAD_STACK, ExceptionRecord);
00890
00891 }
else if ((FunctionEntry->ExceptionHandler !=
NULL) && InFunction) {
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909 DispatcherContext.ControlPc = ControlPc;
00910 DispatcherContext.FunctionEntry = FunctionEntry;
00911 DispatcherContext.EstablisherFrame = EstablisherFrame;
00912 DispatcherContext.ContextRecord = ContextRecord;
00913
00914
00915
00916
00917
00918
do {
00919
00920
00921
00922
00923
00924
00925
if ((ULONG)TargetFrame == EstablisherFrame) {
00926 ExceptionFlags |=
EXCEPTION_TARGET_UNWIND;
00927 }
00928
00929 ExceptionRecord->ExceptionFlags = ExceptionFlags;
00930
00931
00932
00933
00934
00935
00936 ContextRecord->XIntV0 = (LONG)ReturnValue;
00937 Disposition =
00938
RtlpExecuteHandlerForUnwind(ExceptionRecord,
00939 EstablisherFrame,
00940 ContextRecord,
00941 &DispatcherContext,
00942 FunctionEntry->ExceptionHandler);
00943
00944
00945
00946
00947
00948 ExceptionFlags &= ~(
EXCEPTION_COLLIDED_UNWIND |
00949
EXCEPTION_TARGET_UNWIND);
00950
00951
00952
00953
00954
00955
switch (Disposition) {
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
case ExceptionContinueSearch :
00967
if (EstablisherFrame != (ULONG)TargetFrame) {
00968 NextPc =
RtlVirtualUnwind(ControlPc | 1,
00969 FunctionEntry,
00970 ContextRecord,
00971 &InFunction,
00972 &EstablisherFrame,
00973
NULL);
00974 }
00975
00976
break;
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
case ExceptionCollidedUnwind :
00988 ControlPc = DispatcherContext.ControlPc;
00989 FunctionEntry = DispatcherContext.FunctionEntry;
00990 ContextRecord = DispatcherContext.ContextRecord;
00991 ContextRecord->Fir = (ULONG)TargetIp;
00992 ExceptionFlags |=
EXCEPTION_COLLIDED_UNWIND;
00993 EstablisherFrame = DispatcherContext.EstablisherFrame;
00994
break;
00995
00996
00997
00998
00999
01000
01001
01002
default :
01003
RAISE_EXCEPTION(STATUS_INVALID_DISPOSITION, ExceptionRecord);
01004 }
01005
01006 }
while ((ExceptionFlags &
EXCEPTION_COLLIDED_UNWIND) != 0);
01007
01008 }
else {
01009
01010
01011
01012
01013
01014
01015
if (EstablisherFrame != (ULONG)TargetFrame) {
01016 NextPc =
RtlVirtualUnwind(ControlPc | 1,
01017 FunctionEntry,
01018 ContextRecord,
01019 &InFunction,
01020 &EstablisherFrame,
01021
NULL);
01022 }
01023 }
01024
01025 }
else {
01026
01027
01028
01029
01030
01031 NextPc = (ULONG)(ContextRecord->XIntRa - 4);
01032
01033
01034
01035
01036
01037
01038
if (NextPc == ControlPc) {
01039
RtlRaiseStatus(STATUS_BAD_FUNCTION_TABLE);
01040 }
01041 }
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051 ControlPc = NextPc;
01052
01053 }
while ((EstablisherFrame < HighLimit) &&
01054 (EstablisherFrame != (ULONG)TargetFrame));
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
if (EstablisherFrame == (ULONG)TargetFrame) {
01065 ContextRecord->XIntV0 = (LONG)ReturnValue;
01066
RtlpRestoreContext(ContextRecord, ExceptionRecord);
01067
01068 }
else {
01069 ZwRaiseException(ExceptionRecord, ContextRecord,
FALSE);
01070 }
01071 }
01072
01073 ULONG
01074 RtlVirtualUnwind (
01075 IN ULONG ControlPc,
01076 IN PRUNTIME_FUNCTION FunctionEntry,
01077 IN OUT PCONTEXT ContextRecord,
01078 OUT PBOOLEAN InFunction,
01079 OUT PULONG EstablisherFrame,
01080 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
01081 )
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139 {
01140
01141 ULONG Address;
01142 ULONG DecrementOffset;
01143 ULONG DecrementRegister;
01144 PULONG FloatingRegister;
01145 ULONG Function;
01146 MIPS_INSTRUCTION Instruction;
01147 PULONGLONG IntegerRegister;
01148 ULONG NextPc;
01149 LONG
Offset;
01150 ULONG Opcode;
01151 ULONG Rd;
01152 BOOLEAN RestoredRa;
01153 BOOLEAN RestoredSp;
01154 ULONG Rs;
01155 ULONG Rt;
01156
01157
01158
01159
01160
01161
01162
01163
if ((ControlPc & 1) == 0) {
01164
return RtlpVirtualUnwind32(ControlPc,
01165 FunctionEntry,
01166 ContextRecord,
01167 InFunction,
01168 EstablisherFrame,
01169 ContextPointers);
01170
01171 }
else {
01172
01173
01174
01175
01176
01177 FloatingRegister = &ContextRecord->FltF0;
01178 IntegerRegister = &ContextRecord->XIntZero;
01179
01180
01181
01182
01183
01184
01185
01186
01187 ControlPc &= ~1;
01188
if (*((PULONG)ControlPc) == JUMP_RA) {
01189 *InFunction =
FALSE;
01190 Instruction.Long = *((PULONG)ControlPc + 1);
01191 Opcode = Instruction.i_format.Opcode;
01192
Offset = Instruction.i_format.Simmediate;
01193 Rd = Instruction.r_format.Rd;
01194 Rs = Instruction.i_format.Rs;
01195 Rt = Instruction.i_format.Rt;
01196 Function = Instruction.r_format.Function;
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
if ((Opcode == ADDIU_OP) && (Rt ==
SP) && (Rs ==
SP)) {
01208 IntegerRegister[
SP] +=
Offset;
01209
01210 }
else if ((Opcode == SPEC_OP) && (Function == ADDU_OP) &&
01211 (Rd ==
SP) && (Rs ==
SP)) {
01212 IntegerRegister[
SP] += IntegerRegister[Rt];
01213 }
01214
01215 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01216
return (ULONG)ContextRecord->XIntRa;
01217 }
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
if ((ControlPc < FunctionEntry->BeginAddress) ||
01228 (ControlPc >= FunctionEntry->PrologEndAddress)) {
01229 *InFunction =
TRUE;
01230 ControlPc = FunctionEntry->PrologEndAddress;
01231
01232 }
else {
01233 *InFunction =
FALSE;
01234 }
01235
01236
01237
01238
01239
01240
01241 DecrementRegister = 0;
01242 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01243 NextPc = (ULONG)(ContextRecord->XIntRa - 4);
01244 RestoredRa =
FALSE;
01245 RestoredSp =
FALSE;
01246
while (ControlPc > FunctionEntry->BeginAddress) {
01247
01248
01249
01250
01251
01252
01253 ControlPc -= 4;
01254 Instruction.Long = *((PULONG)ControlPc);
01255 Opcode = Instruction.i_format.Opcode;
01256
Offset = Instruction.i_format.Simmediate;
01257 Rd = Instruction.r_format.Rd;
01258 Rs = Instruction.i_format.Rs;
01259 Rt = Instruction.i_format.Rt;
01260 Address = (ULONG)(
Offset + IntegerRegister[Rs]);
01261
if (Opcode == SW_OP) {
01262
01263
01264
01265
01266
01267
01268
01269
01270
if (Rs ==
SP) {
01271 IntegerRegister[Rt] = *((PLONG)Address);
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
if (Rt ==
RA) {
01284
if (RestoredRa ==
FALSE) {
01285 NextPc = (ULONG)(ContextRecord->XIntRa - 4);
01286 RestoredRa =
TRUE;
01287
01288 }
else {
01289 NextPc += 4;
01290 }
01291
01292 }
else if (Rt ==
SP) {
01293
if (RestoredSp ==
FALSE) {
01294 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01295 RestoredSp =
TRUE;
01296 }
01297 }
01298
01299
01300
01301
01302
01303
01304
01305
if (ARGUMENT_PRESENT(ContextPointers)) {
01306 ContextPointers->XIntegerContext[Rt] = (PULONGLONG)Address;
01307 }
01308 }
01309
01310 }
else if (Opcode == SD_OP) {
01311
01312
01313
01314
01315
01316
01317
01318
01319
if (Rs ==
SP) {
01320 IntegerRegister[Rt] = *((PULONGLONG)Address);
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
if (Rt ==
RA) {
01333
if (RestoredRa ==
FALSE) {
01334 NextPc = (ULONG)(ContextRecord->XIntRa - 4);
01335 RestoredRa =
TRUE;
01336
01337 }
else {
01338 NextPc += 4;
01339 }
01340
01341 }
else if (Rt ==
SP) {
01342
if (RestoredSp ==
FALSE) {
01343 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01344 RestoredSp =
TRUE;
01345 }
01346 }
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
if (ARGUMENT_PRESENT(ContextPointers)) {
01358 ContextPointers->XIntegerContext[Rt] = (PLONGLONG)((ULONG)Address | 1);
01359 }
01360 }
01361
01362 }
else if (Opcode == SWC1_OP) {
01363
01364
01365
01366
01367
01368
01369
01370
01371
if (Rs ==
SP) {
01372 FloatingRegister[Rt] = *((PULONG)Address);
01373
01374
01375
01376
01377
01378
01379
01380
if (ARGUMENT_PRESENT(ContextPointers)) {
01381 ContextPointers->FloatingContext[Rt] = (PULONG)Address;
01382 }
01383 }
01384
01385 }
else if (Opcode == SDC1_OP) {
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
if (Rs ==
SP) {
01396 FloatingRegister[Rt] = *((PULONG)Address);
01397 FloatingRegister[Rt + 1] = *((PULONG)(Address + 4));
01398
01399
01400
01401
01402
01403
01404
01405
if (ARGUMENT_PRESENT(ContextPointers)) {
01406 ContextPointers->FloatingContext[Rt] = (PULONG)Address;
01407 ContextPointers->FloatingContext[Rt + 1] = (PULONG)(Address + 4);
01408 }
01409 }
01410
01411 }
else if (Opcode == ADDIU_OP) {
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
if ((Rs ==
SP) && (Rt ==
SP)) {
01425 IntegerRegister[
SP] -=
Offset;
01426
if (RestoredSp ==
FALSE) {
01427 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01428 RestoredSp =
TRUE;
01429 }
01430
01431 }
else if ((Rt == DecrementRegister) && (Rs ==
ZERO)) {
01432 IntegerRegister[
SP] +=
Offset;
01433
if (RestoredSp ==
FALSE) {
01434 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01435 RestoredSp =
TRUE;
01436 }
01437 }
01438
01439 }
else if (Opcode == ORI_OP) {
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
if ((Rs == DecrementRegister) && (Rt == DecrementRegister)) {
01451 DecrementOffset = (
Offset & 0xffff);
01452
01453 }
else if ((Rt == DecrementRegister) && (Rs ==
ZERO)) {
01454 IntegerRegister[
SP] += (
Offset & 0xffff);
01455
if (RestoredSp ==
FALSE) {
01456 *EstablisherFrame = (ULONG)ContextRecord->XIntSp;
01457 RestoredSp =
TRUE;
01458 }
01459 }
01460
01461 }
else if (Opcode == SPEC_OP) {
01462
01463
01464
01465
01466
01467
01468
01469
01470 Function = Instruction.r_format.Function;
01471
if ((Function == ADDU_OP) || (Function == OR_OP)) {
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
if (Rt ==
ZERO) {
01483 IntegerRegister[Rs] = IntegerRegister[Rd];
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
if (Rs ==
RA) {
01494
if (RestoredRa ==
FALSE) {
01495 NextPc = (ULONG)(ContextRecord->XIntRa - 4);
01496 RestoredRa =
TRUE;
01497
01498 }
else {
01499 NextPc += 4;
01500 }
01501 }
01502
01503 }
else if (Rs ==
ZERO) {
01504 IntegerRegister[Rt] = IntegerRegister[Rd];
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
if (Rt ==
RA) {
01515
if (RestoredRa ==
FALSE) {
01516 NextPc = (ULONG)(ContextRecord->XIntRa - 4);
01517 RestoredRa =
TRUE;
01518
01519 }
else {
01520 NextPc += 4;
01521 }
01522 }
01523 }
01524
01525 }
else if (Function == SUBU_OP) {
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
if ((Rd ==
SP) && (Rs ==
SP)) {
01537 DecrementRegister = Rt;
01538 }
01539 }
01540
01541 }
else if (Opcode == LUI_OP) {
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
if (Rt == DecrementRegister) {
01552 DecrementRegister = 0;
01553 IntegerRegister[
SP] += (LONG)(DecrementOffset + (
Offset << 16));
01554
if (RestoredSp ==
FALSE) {
01555 *EstablisherFrame = (ULONG)(ContextRecord->XIntSp);
01556 RestoredSp =
TRUE;
01557 }
01558 }
01559 }
01560 }
01561
01562
01563
01564
01565
01566 ContextRecord->XIntZero = 0;
01567
return NextPc;
01568 }
01569 }
01570
01571 ULONG
01572 RtlpVirtualUnwind32 (
01573 IN ULONG ControlPc,
01574 IN PRUNTIME_FUNCTION FunctionEntry,
01575 IN OUT PCONTEXT ContextRecord,
01576 OUT PBOOLEAN InFunction,
01577 OUT PULONG EstablisherFrame,
01578 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
01579 )
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632 {
01633
01634 ULONG Address;
01635 ULONG DecrementOffset;
01636 ULONG DecrementRegister;
01637 PULONG FloatingRegister;
01638 ULONG Function;
01639 MIPS_INSTRUCTION Instruction;
01640 PULONG IntegerRegister;
01641 ULONG NextPc;
01642 LONG
Offset;
01643 ULONG Opcode;
01644 ULONG Rd;
01645 BOOLEAN RestoredRa;
01646 BOOLEAN RestoredSp;
01647 ULONG Rs;
01648 ULONG Rt;
01649
01650
01651
01652
01653
01654 FloatingRegister = &ContextRecord->FltF0;
01655 IntegerRegister = &ContextRecord->IntZero;
01656
01657
01658
01659
01660
01661
01662
01663
01664
if (*((PULONG)ControlPc) == JUMP_RA) {
01665 *InFunction =
FALSE;
01666 Instruction.Long = *((PULONG)ControlPc + 1);
01667 Opcode = Instruction.i_format.Opcode;
01668
Offset = Instruction.i_format.Simmediate;
01669 Rd = Instruction.r_format.Rd;
01670 Rs = Instruction.i_format.Rs;
01671 Rt = Instruction.i_format.Rt;
01672 Function = Instruction.r_format.Function;
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
if ((Opcode == ADDIU_OP) && (Rt ==
SP) && (Rs ==
SP)) {
01683 IntegerRegister[
SP] +=
Offset;
01684
01685 }
else if ((Opcode == SPEC_OP) && (Function == ADDU_OP) &&
01686 (Rd ==
SP) && (Rs ==
SP)) {
01687 IntegerRegister[
SP] += IntegerRegister[Rt];
01688 }
01689
01690 *EstablisherFrame = ContextRecord->IntSp;
01691
return ContextRecord->IntRa;
01692 }
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
if ((ControlPc < FunctionEntry->BeginAddress) ||
01703 (ControlPc >= FunctionEntry->PrologEndAddress)) {
01704 *InFunction =
TRUE;
01705 ControlPc = FunctionEntry->PrologEndAddress;
01706
01707 }
else {
01708 *InFunction =
FALSE;
01709 }
01710
01711
01712
01713
01714
01715
01716 DecrementRegister = 0;
01717 *EstablisherFrame = ContextRecord->IntSp;
01718 NextPc = ContextRecord->IntRa - 4;
01719 RestoredRa =
FALSE;
01720 RestoredSp =
FALSE;
01721
while (ControlPc > FunctionEntry->BeginAddress) {
01722
01723
01724
01725
01726
01727
01728 ControlPc -= 4;
01729 Instruction.Long = *((PULONG)ControlPc);
01730 Opcode = Instruction.i_format.Opcode;
01731
Offset = Instruction.i_format.Simmediate;
01732 Rd = Instruction.r_format.Rd;
01733 Rs = Instruction.i_format.Rs;
01734 Rt = Instruction.i_format.Rt;
01735 Address =
Offset + IntegerRegister[Rs];
01736
if (Opcode == SW_OP) {
01737
01738
01739
01740
01741
01742
01743
01744
01745
if (Rs ==
SP) {
01746 IntegerRegister[Rt] = *((PULONG)Address);
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758
if (Rt ==
RA) {
01759
if (RestoredRa ==
FALSE) {
01760 NextPc = ContextRecord->IntRa - 4;
01761 RestoredRa =
TRUE;
01762
01763 }
else {
01764 NextPc += 4;
01765 }
01766
01767 }
else if (Rt ==
SP) {
01768
if (RestoredSp ==
FALSE) {
01769 *EstablisherFrame = ContextRecord->IntSp;
01770 RestoredSp =
TRUE;
01771 }
01772 }
01773
01774
01775
01776
01777
01778
01779
01780
if (ARGUMENT_PRESENT(ContextPointers)) {
01781 ContextPointers->XIntegerContext[Rt] = (PULONGLONG)Address;
01782 }
01783 }
01784
01785 }
else if (Opcode == SWC1_OP) {
01786
01787
01788
01789
01790
01791
01792
01793
01794
if (Rs ==
SP) {
01795 FloatingRegister[Rt] = *((PULONG)Address);
01796
01797
01798
01799
01800
01801
01802
01803
if (ARGUMENT_PRESENT(ContextPointers)) {
01804 ContextPointers->FloatingContext[Rt] = (PULONG)Address;
01805 }
01806 }
01807
01808 }
else if (Opcode == SDC1_OP) {
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
if (Rs ==
SP) {
01819 FloatingRegister[Rt] = *((PULONG)Address);
01820 FloatingRegister[Rt + 1] = *((PULONG)(Address + 4));
01821
01822
01823
01824
01825
01826
01827
01828
if (ARGUMENT_PRESENT(ContextPointers)) {
01829 ContextPointers->FloatingContext[Rt] = (PULONG)Address;
01830 ContextPointers->FloatingContext[Rt + 1] = (PULONG)(Address + 4);
01831 }
01832 }
01833
01834 }
else if (Opcode == ADDIU_OP) {
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
if ((Rs ==
SP) && (Rt ==
SP)) {
01848 IntegerRegister[
SP] -=
Offset;
01849
if (RestoredSp ==
FALSE) {
01850 *EstablisherFrame = ContextRecord->IntSp;
01851 RestoredSp =
TRUE;
01852 }
01853
01854 }
else if ((Rt == DecrementRegister) && (Rs ==
ZERO)) {
01855 IntegerRegister[
SP] +=
Offset;
01856
if (RestoredSp ==
FALSE) {
01857 *EstablisherFrame = ContextRecord->IntSp;
01858 RestoredSp =
TRUE;
01859 }
01860 }
01861
01862 }
else if (Opcode == ORI_OP) {
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
if ((Rs == DecrementRegister) && (Rt == DecrementRegister)) {
01874 DecrementOffset = (
Offset & 0xffff);
01875
01876 }
else if ((Rt == DecrementRegister) && (Rs ==
ZERO)) {
01877 IntegerRegister[
SP] += (
Offset & 0xffff);
01878
if (RestoredSp ==
FALSE) {
01879 *EstablisherFrame = ContextRecord->IntSp;
01880 RestoredSp =
TRUE;
01881 }
01882 }
01883
01884 }
else if (Opcode == SPEC_OP) {
01885
01886
01887
01888
01889
01890
01891
01892
01893 Function = Instruction.r_format.Function;
01894
if ((Function == ADDU_OP) || (Function == OR_OP)) {
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
if (Rt ==
ZERO) {
01906 IntegerRegister[Rs] = IntegerRegister[Rd];
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
if (Rs ==
RA) {
01917
if (RestoredRa ==
FALSE) {
01918 NextPc = ContextRecord->IntRa - 4;
01919 RestoredRa =
TRUE;
01920
01921 }
else {
01922 NextPc += 4;
01923 }
01924 }
01925
01926 }
else if (Rs ==
ZERO) {
01927 IntegerRegister[Rt] = IntegerRegister[Rd];
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
if (Rt ==
RA) {
01938
if (RestoredRa ==
FALSE) {
01939 NextPc = ContextRecord->IntRa - 4;
01940 RestoredRa =
TRUE;
01941
01942 }
else {
01943 NextPc += 4;
01944 }
01945 }
01946 }
01947
01948 }
else if (Function == SUBU_OP) {
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
if ((Rd ==
SP) && (Rs ==
SP)) {
01960 DecrementRegister = Rt;
01961 }
01962 }
01963
01964 }
else if (Opcode == LUI_OP) {
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
if (Rt == DecrementRegister) {
01975 DecrementRegister = 0;
01976 IntegerRegister[
SP] += (DecrementOffset + (
Offset << 16));
01977
if (RestoredSp ==
FALSE) {
01978 *EstablisherFrame = ContextRecord->IntSp;
01979 RestoredSp =
TRUE;
01980 }
01981 }
01982 }
01983 }
01984
01985
01986
01987
01988
01989 ContextRecord->IntZero = 0;
01990
return NextPc;
01991 }
01992
01993 ULONG
01994 RtlpVirtualUnwind (
01995 IN ULONG ControlPc,
01996 IN PRUNTIME_FUNCTION FunctionEntry,
01997 IN PCONTEXT ContextRecord,
01998 OUT PBOOLEAN InFunction,
01999 OUT PULONG EstablisherFrame,
02000 IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
02001 )
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055 {
02056
02057 CONTEXT LocalContext;
02058
02059
02060
02061
02062
02063
02064
02065 RtlMoveMemory((PVOID)&LocalContext, ContextRecord,
sizeof(CONTEXT));
02066
return RtlVirtualUnwind(ControlPc | 1,
02067 FunctionEntry,
02068 &LocalContext,
02069 InFunction,
02070 EstablisherFrame,
02071 ContextPointers);
02072 }