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
#include "ki.h"
00028
#pragma hdrstop
00029 #define _KXPPC_C_HEADER_
00030
#include "kxppc.h"
00031
00032 BOOLEAN
00033
KiEmulateDcbz (
00034 IN OUT PEXCEPTION_RECORD ExceptionRecord,
00035 IN OUT PKEXCEPTION_FRAME ExceptionFrame,
00036 IN OUT PKTRAP_FRAME TrapFrame
00037 );
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 ULONG
KiEnableAlignmentFaultExceptions =
TRUE;
00054
00055
00056
00057
00058
00059 #define BREAK_INST (TRAP_INSTR | TO_BREAKPOINT)
00060
00061
00062
00063
00064
00065 #define DIVIDE_BREAKPOINT (TRAP_INSTR | TO_DIVIDE_BY_ZERO)
00066 #define UDIVIDE_BREAKPOINT (TRAP_INSTR | TO_UNCONDITIONAL_DIVIDE_BY_ZERO)
00067
00068
00069
00070
00071
00072 #define KERNEL_BREAKPOINT_INSTRUCTION (BREAK_INSTR | DEBUG_STOP_BREAKPOINT)
00073 #define KDDEBUG_BREAKPOINT (BREAK_INSTR | BREAKIN_BREAKPOINT)
00074
00075
00076
00077
00078 ULONG
KiBreakPoints;
00079
00080
VOID
00081 KeContextFromKframes (
00082 IN PKTRAP_FRAME TrapFrame,
00083 IN PKEXCEPTION_FRAME ExceptionFrame,
00084 IN OUT PCONTEXT ContextFrame
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
if ((ContextFrame->ContextFlags &
CONTEXT_CONTROL) ==
CONTEXT_CONTROL) {
00119
00120
00121
00122
00123
00124 ContextFrame->Msr = TrapFrame->Msr;
00125 ContextFrame->Iar = TrapFrame->Iar;
00126 ContextFrame->Lr = TrapFrame->Lr;
00127 ContextFrame->Ctr = TrapFrame->Ctr;
00128 }
00129
00130
00131
00132
00133
00134
if ((ContextFrame->ContextFlags &
CONTEXT_INTEGER) ==
CONTEXT_INTEGER) {
00135
00136
00137
00138
00139
00140 RtlMoveMemory (&ContextFrame->Gpr0, &TrapFrame->Gpr0,
00141 sizeof (ULONG) * 13);
00142
00143
00144
00145
00146
00147 RtlMoveMemory (&ContextFrame->Gpr13, &ExceptionFrame->Gpr13,
00148 sizeof (ULONG) * 19);
00149
00150
00151
00152
00153
00154
00155 ContextFrame->Cr = TrapFrame->Cr;
00156
00157
00158
00159
00160
00161
00162 ContextFrame->Xer = TrapFrame->Xer;
00163 }
00164
00165
00166
00167
00168
00169
if ((ContextFrame->ContextFlags &
CONTEXT_FLOATING_POINT) ==
CONTEXT_FLOATING_POINT) {
00170
00171
00172
00173
00174
00175 RtlMoveMemory(&ContextFrame->Fpr0, &TrapFrame->Fpr0,
00176
sizeof(DOUBLE) * (14));
00177
00178
00179
00180
00181
00182 RtlMoveMemory(&ContextFrame->Fpr14, &ExceptionFrame->Fpr14,
00183
sizeof(DOUBLE) * (18));
00184
00185
00186
00187
00188
00189 ContextFrame->Fpscr = TrapFrame->Fpscr;
00190 }
00191
00192
00193
00194
00195
00196
if ((ContextFrame->ContextFlags & CONTEXT_DEBUG_REGISTERS) ==
00197 CONTEXT_DEBUG_REGISTERS) {
00198
00199 ContextFrame->Dr0 = TrapFrame->Dr0;
00200 ContextFrame->Dr1 = TrapFrame->Dr1;
00201 ContextFrame->Dr2 = TrapFrame->Dr2;
00202 ContextFrame->Dr3 = TrapFrame->Dr3;
00203 ContextFrame->Dr6 = TrapFrame->Dr6;
00204 ContextFrame->Dr6 |=
KiBreakPoints;
00205 ContextFrame->Dr5 = 0;
00206 ContextFrame->Dr4 = 0;
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
if (((TrapFrame->PreviousMode) !=
KernelMode) &&
00217 (
KeGetCurrentThread()->DebugActive)) {
00218
00219 ContextFrame->Dr7 = TrapFrame->Dr7;
00220 }
else {
00221
00222 ContextFrame->Dr7 = 0;
00223 }
00224 }
00225
00226
return;
00227 }
00228
00229
VOID
00230 KeContextToKframes (
00231 IN OUT PKTRAP_FRAME TrapFrame,
00232 IN OUT PKEXCEPTION_FRAME ExceptionFrame,
00233 IN PCONTEXT ContextFrame,
00234 IN ULONG ContextFlags,
00235 IN KPROCESSOR_MODE PreviousMode
00236 )
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 {
00270
00271
00272
00273
00274
00275
if ((ContextFlags &
CONTEXT_CONTROL) ==
CONTEXT_CONTROL) {
00276
00277
00278
00279
00280
00281 TrapFrame->Iar = ContextFrame->Iar;
00282 TrapFrame->Lr = ContextFrame->Lr;
00283 TrapFrame->Ctr = ContextFrame->Ctr;
00284 TrapFrame->Msr = SANITIZE_MSR(ContextFrame->Msr, PreviousMode);
00285 }
00286
00287
00288
00289
00290
00291
if ((ContextFlags &
CONTEXT_INTEGER) ==
CONTEXT_INTEGER) {
00292
00293
00294
00295
00296
00297 RtlMoveMemory(&TrapFrame->Gpr0, &ContextFrame->Gpr0,
00298
sizeof(ULONG) * (13));
00299
00300
00301
00302
00303
00304 RtlMoveMemory(&ExceptionFrame->Gpr13, &ContextFrame->Gpr13,
00305
sizeof(ULONG) * (19));
00306
00307
00308
00309
00310
00311 TrapFrame->Cr = ContextFrame->Cr;
00312 TrapFrame->Xer = ContextFrame->Xer;
00313 }
00314
00315
00316
00317
00318
00319
if ((ContextFlags &
CONTEXT_FLOATING_POINT) ==
CONTEXT_FLOATING_POINT) {
00320
00321
00322
00323
00324
00325 RtlMoveMemory(&TrapFrame->Fpr0, &ContextFrame->Fpr0,
00326
sizeof(DOUBLE) * (14));
00327
00328
00329
00330
00331
00332 RtlMoveMemory(&ExceptionFrame->Fpr14, &ContextFrame->Fpr14,
00333
sizeof(DOUBLE) * (18));
00334
00335
00336
00337
00338
00339 TrapFrame->Fpscr = SANITIZE_FPSCR(ContextFrame->Fpscr, PreviousMode);
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
if ((ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS) {
00350
00351
00352
00353
00354
00355
00356 TrapFrame->Dr0 = SANITIZE_DRADDR(ContextFrame->Dr0, PreviousMode);
00357 TrapFrame->Dr1 = SANITIZE_DRADDR(ContextFrame->Dr1, PreviousMode);
00358 TrapFrame->Dr2 = SANITIZE_DRADDR(ContextFrame->Dr2, PreviousMode);
00359 TrapFrame->Dr3 = SANITIZE_DRADDR(ContextFrame->Dr3, PreviousMode);
00360 TrapFrame->Dr6 = SANITIZE_DR6(ContextFrame->Dr6, PreviousMode);
00361 TrapFrame->Dr7 = SANITIZE_DR7(ContextFrame->Dr7, PreviousMode);
00362
00363
if (PreviousMode !=
KernelMode) {
00364
KeGetPcr()->DebugActive =
KeGetCurrentThread()->DebugActive =
00365 (UCHAR)(TrapFrame->Dr7 & DR7_ACTIVE);
00366 }
00367 }
00368
00369
return;
00370 }
00371
00372
VOID
00373 KiDispatchException (
00374 IN PEXCEPTION_RECORD ExceptionRecord,
00375 IN PKEXCEPTION_FRAME ExceptionFrame,
00376 IN PKTRAP_FRAME TrapFrame,
00377 IN KPROCESSOR_MODE PreviousMode,
00378 IN BOOLEAN FirstChance
00379 )
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
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
00426
00427
00428 {
00429
00430 CONTEXT ContextFrame;
00431 EXCEPTION_RECORD ExceptionRecord1;
00432 LONG Length;
00433 BOOLEAN UserApcPending;
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
if (ExceptionRecord->ExceptionCode == STATUS_DATATYPE_MISALIGNMENT) {
00446
if (FirstChance !=
FALSE) {
00447
00448
00449
00450
00451
00452
00453
if ((
KiEnableAlignmentFaultExceptions ==
FALSE) ||
00454 (
KeGetCurrentThread()->AutoAlignment !=
FALSE) ||
00455 (
KeGetCurrentThread()->ApcState.Process->AutoAlignment !=
FALSE)) {
00456
if (
KiEmulateReference(ExceptionRecord, ExceptionFrame, TrapFrame) !=
FALSE) {
00457
KeGetCurrentPrcb()->KeAlignmentFixupCount += 1;
00458
goto Handled2;
00459 }
00460 }
else {
00461
if (
KiEmulateDcbz(ExceptionRecord, ExceptionFrame, TrapFrame) !=
FALSE) {
00462
KeGetCurrentPrcb()->KeAlignmentFixupCount += 1;
00463
goto Handled2;
00464 }
00465 }
00466 }
00467 }
00468
00469
00470
00471
00472
00473
00474
00475
if (ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) {
00476
00477 ULONG Instr = ExceptionRecord->ExceptionInformation[0];
00478
00479
if ((Instr & 0xffe0ffff) ==
DIVIDE_BREAKPOINT ||
00480 (Instr & 0xffe0ffff) ==
UDIVIDE_BREAKPOINT) {
00481 ExceptionRecord->ExceptionCode = STATUS_INTEGER_DIVIDE_BY_ZERO;
00482 }
else if (Instr ==
KDDEBUG_BREAKPOINT) {
00483 TrapFrame->Iar += 4;
00484 }
00485 }
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
if (ExceptionRecord->ExceptionCode == STATUS_FLOAT_STACK_CHECK) {
00496
00497 PFPSCR Fpscr = (PFPSCR)(&TrapFrame->Fpscr);
00498
00499
if ((Fpscr->XE == 1) && (Fpscr->XX == 1)) {
00500
00501 ExceptionRecord->ExceptionCode = STATUS_FLOAT_INEXACT_RESULT;
00502 Fpscr->XE = 0;
00503
00504 }
00505
else if ((Fpscr->ZE == 1) && (Fpscr->ZX == 1)) {
00506
00507 ExceptionRecord->ExceptionCode = STATUS_FLOAT_DIVIDE_BY_ZERO;
00508 Fpscr->ZE = 0;
00509
00510 }
00511
else if ((Fpscr->UE == 1) && (Fpscr->UX == 1)) {
00512
00513 ExceptionRecord->ExceptionCode = STATUS_FLOAT_UNDERFLOW;
00514 Fpscr->UE = 0;
00515
00516 }
00517
00518
else if ((Fpscr->OE == 1) && (Fpscr->OX == 1)) {
00519
00520 ExceptionRecord->ExceptionCode = STATUS_FLOAT_OVERFLOW;
00521 Fpscr->OE = 0;
00522
00523 }
00524
else {
00525
00526
00527
00528 ExceptionRecord->ExceptionCode = STATUS_FLOAT_INVALID_OPERATION;
00529 Fpscr->VE = 0;
00530 }
00531 }
00532
00533
00534
00535
00536
00537
00538 ContextFrame.ContextFlags =
CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS;
00539
KeContextFromKframes(TrapFrame, ExceptionFrame, &ContextFrame);
00540
KeGetCurrentPrcb()->KeExceptionDispatchCount += 1;
00541
00542
00543
00544
00545
00546
if (PreviousMode ==
KernelMode) {
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
if (FirstChance !=
FALSE) {
00566
00567
00568
00569
00570
00571
00572
00573
if ((
KiDebugRoutine !=
NULL) &&
00574 ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) ||
00575 (ExceptionRecord->ExceptionCode == STATUS_SINGLE_STEP)) &&
00576 (
KdIsThisAKdTrap(ExceptionRecord,
00577 &ContextFrame,
00578
KernelMode) !=
FALSE)) {
00579
00580
if (((
KiDebugRoutine) (TrapFrame,
00581 ExceptionFrame,
00582 ExceptionRecord,
00583 &ContextFrame,
00584
KernelMode,
00585
FALSE)) !=
FALSE) {
00586
00587
goto Handled1;
00588 }
00589 }
00590
00591
00592
00593
00594
00595
if (
RtlDispatchException(ExceptionRecord, &ContextFrame) !=
FALSE) {
00596
goto Handled1;
00597 }
00598 }
00599
00600
00601
00602
00603
00604
if (
KiDebugRoutine !=
NULL) {
00605
if (((
KiDebugRoutine) (TrapFrame,
00606 ExceptionFrame,
00607 ExceptionRecord,
00608 &ContextFrame,
00609 PreviousMode,
00610
TRUE)) !=
FALSE) {
00611
goto Handled1;
00612 }
00613 }
00614
00615
KeBugCheckEx(
KMODE_EXCEPTION_NOT_HANDLED,
00616 ExceptionRecord->ExceptionCode,
00617 (ULONG)ExceptionRecord->ExceptionAddress,
00618 ExceptionRecord->ExceptionInformation[0],
00619 ExceptionRecord->ExceptionInformation[1]);
00620
00621 }
else {
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
if (FirstChance !=
FALSE) {
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
if ((
KiDebugRoutine !=
NULL) &&
00660 ((ExceptionRecord->ExceptionCode == STATUS_BREAKPOINT) ||
00661 (ExceptionRecord->ExceptionCode == STATUS_SINGLE_STEP)) &&
00662 (
KdIsThisAKdTrap(ExceptionRecord,
00663 &ContextFrame,
00664
UserMode) !=
FALSE) &&
00665 ((
PsGetCurrentProcess()->DebugPort ==
NULL) ||
00666 ((
PsGetCurrentProcess()->DebugPort !=
NULL) &&
00667 (ExceptionRecord->ExceptionInformation[0] !=
00668
KERNEL_BREAKPOINT_INSTRUCTION)))) {
00669
00670
if (((
KiDebugRoutine) (TrapFrame,
00671 ExceptionFrame,
00672 ExceptionRecord,
00673 &ContextFrame,
00674
UserMode,
00675
FALSE)) !=
FALSE) {
00676
00677
goto Handled1;
00678 }
00679 }
00680
00681
00682
00683
00684
00685
if (
DbgkForwardException(ExceptionRecord,
TRUE,
FALSE)) {
00686 TrapFrame->Fpscr = SANITIZE_FPSCR(TrapFrame->Fpscr,
UserMode);
00687
goto Handled2;
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
00726
00727
00728
00729
00730
00731
00732
00733
00734 repeat:
00735
try {
00736
00737
00738
00739
00740
00741 ULONG Length = (
sizeof (STACK_FRAME_HEADER) +
sizeof (EXCEPTION_RECORD) +
00742
sizeof (CONTEXT) +
sizeof (ULONG) + STK_SLACK_SPACE + 7) & (~7);
00743
00744 ULONG UserStack = (ContextFrame.Gpr1 & (~7)) - Length;
00745 ULONG ExceptSlot = UserStack +
sizeof (STACK_FRAME_HEADER);
00746 ULONG ContextSlot = ExceptSlot +
sizeof (EXCEPTION_RECORD);
00747 ULONG TocSlot = ContextSlot +
sizeof (CONTEXT);
00748
00749
00750
00751
00752
00753
00754
ProbeForWrite((PCHAR) UserStack, ContextFrame.Gpr1 - UserStack,
sizeof(QUAD));
00755 RtlMoveMemory((PVOID) ExceptSlot, ExceptionRecord,
sizeof (EXCEPTION_RECORD));
00756 RtlMoveMemory((PVOID) ContextSlot, &ContextFrame,
sizeof (CONTEXT));
00757
00758
00759
00760
00761
00762
00763 *((PULONG) TocSlot) = ContextFrame.Gpr2;
00764
00765
00766
00767
00768
00769 *((PULONG) UserStack) = ContextFrame.Gpr1;
00770
00771
00772
00773
00774
00775
00776 TrapFrame->Gpr1 = UserStack;
00777 TrapFrame->Gpr3 = ExceptSlot;
00778 TrapFrame->Gpr4 = ContextSlot;
00779
00780
00781
00782
00783
00784
00785 TrapFrame->Fpscr = SANITIZE_FPSCR(ContextFrame.Fpscr,
UserMode);
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795 {
00796 PULONG FnDesc = (PULONG)
KeUserExceptionDispatcher;
00797 TrapFrame->Iar = FnDesc[0];
00798 TrapFrame->Gpr2 = FnDesc[1];
00799 }
00800
00801
return;
00802
00803
00804
00805
00806
00807
00808 } except (
KiCopyInformation(&ExceptionRecord1,
00809 (GetExceptionInformation())->ExceptionRecord)) {
00810
00811
00812
00813
00814
00815
00816
00817
00818
if (ExceptionRecord1.ExceptionCode == STATUS_STACK_OVERFLOW) {
00819 ExceptionRecord1.ExceptionAddress = ExceptionRecord->ExceptionAddress;
00820 RtlMoveMemory((PVOID)ExceptionRecord,
00821 &ExceptionRecord1,
sizeof(EXCEPTION_RECORD));
00822
goto repeat;
00823 }
00824 }
00825 }
00826
00827
00828
00829
00830
00831 UserApcPending =
KeGetCurrentThread()->ApcState.UserApcPending;
00832
if (
DbgkForwardException(ExceptionRecord,
TRUE,
TRUE)) {
00833 TrapFrame->Fpscr = SANITIZE_FPSCR(TrapFrame->Fpscr,
UserMode);
00834
goto Handled2;
00835
00836 }
else if (
DbgkForwardException(ExceptionRecord,
FALSE,
TRUE)) {
00837
00838
00839
00840
00841
00842
00843
00844
00845
if ((UserApcPending ==
FALSE) &&
00846 (
KeGetCurrentThread()->ApcState.UserApcPending !=
FALSE)) {
00847
00848
00849
00850 }
00851
00852 TrapFrame->Fpscr = SANITIZE_FPSCR(TrapFrame->Fpscr,
UserMode);
00853
goto Handled2;
00854
00855 }
else {
00856 ZwTerminateProcess(NtCurrentProcess(), ExceptionRecord->ExceptionCode);
00857
KeBugCheckEx(
KMODE_EXCEPTION_NOT_HANDLED,
00858 ExceptionRecord->ExceptionCode,
00859 (ULONG)ExceptionRecord->ExceptionAddress,
00860 ExceptionRecord->ExceptionInformation[0],
00861 ExceptionRecord->ExceptionInformation[1]);
00862 }
00863 }
00864
00865
00866
00867
00868
00869
00870 Handled1:
00871
KeContextToKframes(TrapFrame, ExceptionFrame, &ContextFrame,
00872 ContextFrame.ContextFlags, PreviousMode);
00873
00874
00875
00876
00877
00878
00879
00880
00881 Handled2:
00882
return;
00883 }
00884
00885 ULONG
00886 KiCopyInformation (
00887 IN OUT PEXCEPTION_RECORD ExceptionRecord1,
00888 IN PEXCEPTION_RECORD ExceptionRecord2
00889 )
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910 {
00911
00912
00913
00914
00915
00916
00917 RtlMoveMemory((PVOID)ExceptionRecord1,
00918 (PVOID)ExceptionRecord2,
00919
sizeof(EXCEPTION_RECORD));
00920
00921
return EXCEPTION_EXECUTE_HANDLER;
00922 }
00923
00924
NTSTATUS
00925 KeRaiseUserException(
00926 IN NTSTATUS ExceptionCode
00927 )
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948 {
00949 PKTRAP_FRAME TrapFrame;
00950 PULONG FnDesc;
00951
00952
ASSERT(KeGetPreviousMode() ==
UserMode);
00953
00954 TrapFrame =
KeGetCurrentThread()->TrapFrame;
00955 FnDesc = (PULONG)
KeRaiseUserExceptionDispatcher;
00956
00957 TrapFrame->Iar = FnDesc[0];
00958 TrapFrame->Gpr2 = FnDesc[1];
00959
00960
return(ExceptionCode);
00961 }