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 "ki.h"
00027
#pragma hdrstop
00028 #define HEADER_FILE
00029
#include "kxmips.h"
00030
00031
00032
00033
00034
00035 #define KDDEBUG_BREAKPOINT ((SPEC_OP << 26) | (BREAKIN_BREAKPOINT << 16) | BREAK_OP)
00036 #define DIVIDE_BREAKPOINT ((SPEC_OP << 26) | (DIVIDE_BY_ZERO_BREAKPOINT << 16) | BREAK_OP)
00037 #define MULTIPLY_BREAKPOINT ((SPEC_OP << 26) | (MULTIPLY_OVERFLOW_BREAKPOINT << 16) | BREAK_OP)
00038 #define OVERFLOW_BREAKPOINT ((SPEC_OP << 26) | (DIVIDE_OVERFLOW_BREAKPOINT << 16) | BREAK_OP)
00039
00040
00041
00042
00043
00044 #define KERNEL_BREAKPOINT_INSTRUCTION 0x16000d
00045
00046
VOID
00047 KeContextFromKframes (
00048 IN PKTRAP_FRAME TrapFrame,
00049 IN PKEXCEPTION_FRAME ExceptionFrame,
00050 IN OUT PCONTEXT ContextFrame
00051 )
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 {
00079
00080 ULONG ContextFlags;
00081
00082
00083
00084
00085
00086 ContextFlags = ContextFrame->ContextFlags;
00087
if ((ContextFlags &
CONTEXT_CONTROL) ==
CONTEXT_CONTROL) {
00088
00089
00090
00091
00092
00093 ContextFrame->XIntGp = TrapFrame->XIntGp;
00094 ContextFrame->XIntSp = TrapFrame->XIntSp;
00095 ContextFrame->Fir = TrapFrame->Fir;
00096 ContextFrame->Psr = TrapFrame->Psr;
00097 ContextFrame->XIntRa = TrapFrame->XIntRa;
00098 }
00099
00100
00101
00102
00103
00104
if ((ContextFlags &
CONTEXT_INTEGER) ==
CONTEXT_INTEGER) {
00105
00106
00107
00108
00109
00110 ContextFrame->XIntZero = 0;
00111 ContextFrame->XIntAt = TrapFrame->XIntAt;
00112 ContextFrame->XIntV0 = TrapFrame->XIntV0;
00113 ContextFrame->XIntV1 = TrapFrame->XIntV1;
00114 ContextFrame->XIntA0 = TrapFrame->XIntA0;
00115 ContextFrame->XIntA1 = TrapFrame->XIntA1;
00116 ContextFrame->XIntA2 = TrapFrame->XIntA2;
00117 ContextFrame->XIntA3 = TrapFrame->XIntA3;
00118 ContextFrame->XIntT0 = TrapFrame->XIntT0;
00119 ContextFrame->XIntT1 = TrapFrame->XIntT1;
00120 ContextFrame->XIntT2 = TrapFrame->XIntT2;
00121 ContextFrame->XIntT3 = TrapFrame->XIntT3;
00122 ContextFrame->XIntT4 = TrapFrame->XIntT4;
00123 ContextFrame->XIntT5 = TrapFrame->XIntT5;
00124 ContextFrame->XIntT6 = TrapFrame->XIntT6;
00125 ContextFrame->XIntT7 = TrapFrame->XIntT7;
00126 ContextFrame->XIntT8 = TrapFrame->XIntT8;
00127 ContextFrame->XIntT9 = TrapFrame->XIntT9;
00128 ContextFrame->XIntK0 = 0;
00129 ContextFrame->XIntK1 = 0;
00130 ContextFrame->XIntLo = TrapFrame->XIntLo;
00131 ContextFrame->XIntHi = TrapFrame->XIntHi;
00132
00133
00134
00135
00136
00137 ContextFrame->XIntS0 = TrapFrame->XIntS0;
00138 ContextFrame->XIntS1 = TrapFrame->XIntS1;
00139 ContextFrame->XIntS2 = TrapFrame->XIntS2;
00140 ContextFrame->XIntS3 = TrapFrame->XIntS3;
00141 ContextFrame->XIntS4 = TrapFrame->XIntS4;
00142 ContextFrame->XIntS5 = TrapFrame->XIntS5;
00143 ContextFrame->XIntS6 = TrapFrame->XIntS6;
00144 ContextFrame->XIntS7 = TrapFrame->XIntS7;
00145 ContextFrame->XIntS8 = TrapFrame->XIntS8;
00146 }
00147
00148
00149
00150
00151
00152
if ((ContextFlags &
CONTEXT_FLOATING_POINT) ==
CONTEXT_FLOATING_POINT) {
00153
00154
00155
00156
00157
00158 RtlMoveMemory(&ContextFrame->FltF0, &TrapFrame->FltF0,
00159
sizeof(ULONG) * (20));
00160
00161
00162
00163
00164
00165 RtlMoveMemory(&ContextFrame->FltF20, &ExceptionFrame->FltF20,
00166
sizeof(ULONG) * (12));
00167
00168
00169
00170
00171
00172 ContextFrame->Fsr = TrapFrame->Fsr;
00173 }
00174
00175
return;
00176 }
00177
00178
VOID
00179 KeContextToKframes (
00180 IN OUT PKTRAP_FRAME TrapFrame,
00181 IN OUT PKEXCEPTION_FRAME ExceptionFrame,
00182 IN PCONTEXT ContextFrame,
00183 IN ULONG ContextFlags,
00184 IN KPROCESSOR_MODE PreviousMode
00185 )
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 {
00219
00220
00221
00222
00223
00224
if ((ContextFlags &
CONTEXT_CONTROL) ==
CONTEXT_CONTROL) {
00225
00226
00227
00228
00229
00230 TrapFrame->XIntGp = ContextFrame->XIntGp;
00231 TrapFrame->XIntSp = ContextFrame->XIntSp;
00232 TrapFrame->Fir = ContextFrame->Fir;
00233 TrapFrame->Psr = SANITIZE_PSR(ContextFrame->Psr, PreviousMode);
00234 TrapFrame->XIntRa = ContextFrame->XIntRa;
00235 }
00236
00237
00238
00239
00240
00241
if ((ContextFlags &
CONTEXT_INTEGER) ==
CONTEXT_INTEGER) {
00242
00243
00244
00245
00246
00247 TrapFrame->XIntAt = ContextFrame->XIntAt;
00248 TrapFrame->XIntV0 = ContextFrame->XIntV0;
00249 TrapFrame->XIntV1 = ContextFrame->XIntV1;
00250 TrapFrame->XIntA0 = ContextFrame->XIntA0;
00251 TrapFrame->XIntA1 = ContextFrame->XIntA1;
00252 TrapFrame->XIntA2 = ContextFrame->XIntA2;
00253 TrapFrame->XIntA3 = ContextFrame->XIntA3;
00254 TrapFrame->XIntT0 = ContextFrame->XIntT0;
00255 TrapFrame->XIntT1 = ContextFrame->XIntT1;
00256 TrapFrame->XIntT2 = ContextFrame->XIntT2;
00257 TrapFrame->XIntT3 = ContextFrame->XIntT3;
00258 TrapFrame->XIntT4 = ContextFrame->XIntT4;
00259 TrapFrame->XIntT5 = ContextFrame->XIntT5;
00260 TrapFrame->XIntT6 = ContextFrame->XIntT6;
00261 TrapFrame->XIntT7 = ContextFrame->XIntT7;
00262 TrapFrame->XIntT8 = ContextFrame->XIntT8;
00263 TrapFrame->XIntT9 = ContextFrame->XIntT9;
00264 TrapFrame->XIntLo = ContextFrame->XIntLo;
00265 TrapFrame->XIntHi = ContextFrame->XIntHi;
00266
00267
00268
00269
00270
00271 TrapFrame->XIntS0 = ContextFrame->XIntS0;
00272 TrapFrame->XIntS1 = ContextFrame->XIntS1;
00273 TrapFrame->XIntS2 = ContextFrame->XIntS2;
00274 TrapFrame->XIntS3 = ContextFrame->XIntS3;
00275 TrapFrame->XIntS4 = ContextFrame->XIntS4;
00276 TrapFrame->XIntS5 = ContextFrame->XIntS5;
00277 TrapFrame->XIntS6 = ContextFrame->XIntS6;
00278 TrapFrame->XIntS7 = ContextFrame->XIntS7;
00279 TrapFrame->XIntS8 = ContextFrame->XIntS8;
00280 }
00281
00282
00283
00284
00285
00286
if ((ContextFlags &
CONTEXT_FLOATING_POINT) ==
CONTEXT_FLOATING_POINT) {
00287
00288
00289
00290
00291
00292 RtlMoveMemory(&TrapFrame->FltF0, &ContextFrame->FltF0,
00293
sizeof(ULONG) * (20));
00294
00295
00296
00297
00298
00299 RtlMoveMemory(&ExceptionFrame->FltF20, &ContextFrame->FltF20,
00300
sizeof(ULONG) * (12));
00301
00302
00303
00304
00305
00306 TrapFrame->Fsr = SANITIZE_FSR(ContextFrame->Fsr, PreviousMode);
00307 }
00308
00309
return;
00310 }
00311
00312
VOID
00313 KiDispatchException (
00314 IN PEXCEPTION_RECORD ExceptionRecord,
00315 IN PKEXCEPTION_FRAME ExceptionFrame,
00316 IN PKTRAP_FRAME TrapFrame,
00317 IN KPROCESSOR_MODE PreviousMode,
00318 IN BOOLEAN FirstChance
00319 )
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 {
00369
00370 CONTEXT ContextFrame;
00371 PULONG Destination;
00372 EXCEPTION_RECORD ExceptionRecord1;
00373 ULONG
Index;
00374 LONG Length;
00375 PULONGLONG Source;
00376 BOOLEAN UserApcPending;
00377 ULONG UserStack1;
00378 ULONG UserStack2;
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
if ((ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION) &&
00392 (((ULONG)ExceptionRecord->ExceptionAddress & 0xffc) == 0xffc) &&
00393 (PreviousMode !=
KernelMode) &&
00394 (
KiEmulateReference(ExceptionRecord, ExceptionFrame, TrapFrame) !=
FALSE)) {
00395
KeGetCurrentPrcb()->KeAlignmentFixupCount += 1;
00396
goto Handled2;
00397 }
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
if (ExceptionRecord->ExceptionCode == (
DATA_BUS_ERROR | 0xdfff0000)) {
00411
00412
00413
00414
00415
00416
00417
00418
00419
if ((ExceptionRecord->ExceptionInformation[1] < 0x80000000) &&
00420 (((ULONG)ExceptionRecord->ExceptionAddress & 0xffc) == 0xffc) &&
00421 (PreviousMode !=
KernelMode)) {
00422
if (
KiEmulateReference(ExceptionRecord, ExceptionFrame, TrapFrame) !=
FALSE) {
00423
KeGetCurrentPrcb()->KeAlignmentFixupCount += 1;
00424
goto Handled2;
00425 }
00426 }
00427
00428
KiDataBusError(ExceptionRecord, ExceptionFrame, TrapFrame);
00429
goto Handled2;
00430 }
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
if (ExceptionRecord->ExceptionCode == (
INSTRUCTION_BUS_ERROR | 0xdfff0000)) {
00444
KiInstructionBusError(ExceptionRecord, ExceptionFrame, TrapFrame);
00445
goto Handled2;
00446 }
00447
00448
00449
00450
00451
00452
00453
00454
if ((ExceptionRecord->ExceptionCode == STATUS_DATATYPE_MISALIGNMENT) &&
00455 (FirstChance !=
FALSE) &&
00456 ((
KeGetCurrentThread()->AutoAlignment !=
FALSE) ||
00457 (
KeGetCurrentThread()->ApcState.Process->AutoAlignment !=
FALSE) ||
00458 (((ExceptionRecord->ExceptionInformation[1] & 0x7fff0000) == 0x7fff0000) &&
00459 (PreviousMode !=
KernelMode))) &&
00460 (
KiEmulateReference(ExceptionRecord, ExceptionFrame, TrapFrame) !=
FALSE)) {
00461
KeGetCurrentPrcb()->KeAlignmentFixupCount += 1;
00462
goto Handled2;
00463 }
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
if ((ExceptionRecord->ExceptionCode == STATUS_FLOAT_STACK_CHECK) &&
00477 (
KiEmulateFloating(ExceptionRecord, ExceptionFrame, TrapFrame) !=
FALSE)) {
00478 TrapFrame->Fsr = SANITIZE_FSR(TrapFrame->Fsr, PreviousMode);
00479
goto Handled2;
00480 }
00481
00482
00483
00484
00485
00486
00487
00488
if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) {
00489
if (ExceptionRecord->ExceptionInformation[0] ==
DIVIDE_BREAKPOINT) {
00490 ExceptionRecord->ExceptionCode = STATUS_INTEGER_DIVIDE_BY_ZERO;
00491
00492 }
else if ((ExceptionRecord->ExceptionInformation[0] ==
MULTIPLY_BREAKPOINT) ||
00493 (ExceptionRecord->ExceptionInformation[0] ==
OVERFLOW_BREAKPOINT)) {
00494 ExceptionRecord->ExceptionCode = STATUS_INTEGER_OVERFLOW;
00495
00496 }
else if (ExceptionRecord->ExceptionInformation[0] ==
KDDEBUG_BREAKPOINT) {
00497 TrapFrame->Fir += 4;
00498 }
00499 }
00500
00501
00502
00503
00504
00505
00506 ContextFrame.ContextFlags =
CONTEXT_FULL;
00507
KeContextFromKframes(TrapFrame, ExceptionFrame, &ContextFrame);
00508
KeGetCurrentPrcb()->KeExceptionDispatchCount += 1;
00509
00510
00511
00512
00513
00514
if (PreviousMode ==
KernelMode) {
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
if (FirstChance !=
FALSE) {
00534
00535
00536
00537
00538
00539
00540
00541
if ((
KiDebugRoutine !=
NULL) &&
00542 (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
00543 (
KdIsThisAKdTrap(ExceptionRecord,
00544 &ContextFrame,
00545
KernelMode) !=
FALSE)) {
00546
00547
if (((
KiDebugRoutine) (TrapFrame,
00548 ExceptionFrame,
00549 ExceptionRecord,
00550 &ContextFrame,
00551
KernelMode,
00552
FALSE)) !=
FALSE) {
00553
00554
goto Handled1;
00555 }
00556 }
00557
00558
00559
00560
00561
00562
if (
RtlDispatchException(ExceptionRecord, &ContextFrame) !=
FALSE) {
00563
goto Handled1;
00564 }
00565 }
00566
00567
00568
00569
00570
00571
if (
KiDebugRoutine !=
NULL) {
00572
if (((
KiDebugRoutine) (TrapFrame,
00573 ExceptionFrame,
00574 ExceptionRecord,
00575 &ContextFrame,
00576 PreviousMode,
00577
TRUE)) !=
FALSE) {
00578
goto Handled1;
00579 }
00580 }
00581
00582
KeBugCheckEx(
KMODE_EXCEPTION_NOT_HANDLED,
00583 ExceptionRecord->ExceptionCode,
00584 (ULONG)ExceptionRecord->ExceptionAddress,
00585 ExceptionRecord->ExceptionInformation[0],
00586 ExceptionRecord->ExceptionInformation[1]);
00587
00588 }
else {
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
if (FirstChance !=
FALSE) {
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
if ((
KiDebugRoutine !=
NULL) &&
00627 (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) &&
00628 (
KdIsThisAKdTrap(ExceptionRecord,
00629 &ContextFrame,
00630
UserMode) !=
FALSE) &&
00631 ((
PsGetCurrentProcess()->DebugPort ==
NULL) ||
00632 ((
PsGetCurrentProcess()->DebugPort !=
NULL) &&
00633 (ExceptionRecord->ExceptionInformation[0] !=
00634
KERNEL_BREAKPOINT_INSTRUCTION)))) {
00635
00636
if (((
KiDebugRoutine) (TrapFrame,
00637 ExceptionFrame,
00638 ExceptionRecord,
00639 &ContextFrame,
00640
UserMode,
00641
FALSE)) !=
FALSE) {
00642
00643
goto Handled1;
00644 }
00645 }
00646
00647
00648
00649
00650
00651
if (
DbgkForwardException(ExceptionRecord,
TRUE,
FALSE)) {
00652 TrapFrame->Fsr = SANITIZE_FSR(TrapFrame->Fsr,
UserMode);
00653
goto Handled2;
00654 }
00655
00656
00657
00658
00659
00660
00661
00662 repeat:
00663
try {
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 Destination = &ContextFrame.IntZero;
00676 Source = &ContextFrame.XIntZero;
00677
for (
Index = 0;
Index < 32;
Index += 1) {
00678 *Destination++ = (ULONG)*Source++;
00679 }
00680
00681
00682
00683
00684
00685
00686 Length = (
sizeof(EXCEPTION_RECORD) + 7) & (~7);
00687 UserStack1 = (ULONG)(ContextFrame.XIntSp & (~7)) - Length;
00688
00689
00690
00691
00692
00693
00694
ProbeForWrite((PCHAR)UserStack1, Length,
sizeof(QUAD));
00695 RtlMoveMemory((PVOID)UserStack1, ExceptionRecord, Length);
00696
00697
00698
00699
00700
00701
00702 Length =
sizeof(CONTEXT);
00703 UserStack2 = UserStack1 - Length;
00704
00705
00706
00707
00708
00709
00710
ProbeForWrite((PCHAR)UserStack2, Length,
sizeof(QUAD));
00711 RtlMoveMemory((PVOID)UserStack2, &ContextFrame,
sizeof(CONTEXT));
00712
00713
00714
00715
00716
00717
00718 TrapFrame->XIntSp = (LONG)UserStack2;
00719 TrapFrame->XIntS8 = (LONG)UserStack2;
00720 TrapFrame->XIntS0 = (LONG)UserStack1;
00721 TrapFrame->XIntS1 = (LONG)UserStack2;
00722
00723
00724
00725
00726
00727
00728 TrapFrame->Fsr = SANITIZE_FSR(ContextFrame.Fsr,
UserMode);
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738 TrapFrame->Fir =
KeUserExceptionDispatcher;
00739
return;
00740
00741
00742
00743
00744
00745
00746 } except (
KiCopyInformation(&ExceptionRecord1,
00747 (GetExceptionInformation())->ExceptionRecord)) {
00748
00749
00750
00751
00752
00753
00754
00755
00756
if (ExceptionRecord1.ExceptionCode == STATUS_STACK_OVERFLOW) {
00757 ExceptionRecord1.ExceptionAddress = ExceptionRecord->ExceptionAddress;
00758 RtlMoveMemory((PVOID)ExceptionRecord,
00759 &ExceptionRecord1,
sizeof(EXCEPTION_RECORD));
00760
goto repeat;
00761 }
00762 }
00763 }
00764
00765
00766
00767
00768
00769 UserApcPending =
KeGetCurrentThread()->ApcState.UserApcPending;
00770
if (
DbgkForwardException(ExceptionRecord,
TRUE,
TRUE)) {
00771 TrapFrame->Fsr = SANITIZE_FSR(TrapFrame->Fsr,
UserMode);
00772
goto Handled2;
00773
00774 }
else if (
DbgkForwardException(ExceptionRecord,
FALSE,
TRUE)) {
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
if ((UserApcPending ==
FALSE) &&
00785 (
KeGetCurrentThread()->ApcState.UserApcPending !=
FALSE)) {
00786 TrapFrame->Fir = (ULONG)USPCR;
00787 }
00788
00789 TrapFrame->Fsr = SANITIZE_FSR(TrapFrame->Fsr,
UserMode);
00790
goto Handled2;
00791
00792 }
else {
00793 ZwTerminateProcess(NtCurrentProcess(), ExceptionRecord->ExceptionCode);
00794
KeBugCheckEx(
KMODE_EXCEPTION_NOT_HANDLED,
00795 ExceptionRecord->ExceptionCode,
00796 (ULONG)ExceptionRecord->ExceptionAddress,
00797 ExceptionRecord->ExceptionInformation[0],
00798 ExceptionRecord->ExceptionInformation[1]);
00799 }
00800 }
00801
00802
00803
00804
00805
00806
00807 Handled1:
00808
KeContextToKframes(TrapFrame, ExceptionFrame, &ContextFrame,
00809 ContextFrame.ContextFlags, PreviousMode);
00810
00811
00812
00813
00814
00815
00816
00817
00818 Handled2:
00819
return;
00820 }
00821
00822 ULONG
00823 KiCopyInformation (
00824 IN OUT PEXCEPTION_RECORD ExceptionRecord1,
00825 IN PEXCEPTION_RECORD ExceptionRecord2
00826 )
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847 {
00848
00849
00850
00851
00852
00853
00854 RtlMoveMemory((PVOID)ExceptionRecord1,
00855 (PVOID)ExceptionRecord2,
00856
sizeof(EXCEPTION_RECORD));
00857
00858
return EXCEPTION_EXECUTE_HANDLER;
00859 }
00860
00861
00862
NTSTATUS
00863 KeRaiseUserException(
00864 IN NTSTATUS ExceptionCode
00865 )
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887 {
00888
00889 PKTRAP_FRAME TrapFrame;
00890
00891
ASSERT(KeGetPreviousMode() ==
UserMode);
00892
00893 TrapFrame =
KeGetCurrentThread()->TrapFrame;
00894 TrapFrame->Fir =
KeRaiseUserExceptionDispatcher;
00895
return ExceptionCode;
00896 }