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
#pragma hdrstop
00032
#include "alphaops.h"
00033
00034
00035
00036
00037
00038 BOOLEAN
00039
KiLocateTriggerPc (
00040 IN OUT PEXCEPTION_RECORD ExceptionRecord,
00041 IN OUT PKTRAP_FRAME TrapFrame
00042 );
00043
00044
00045
00046
00047
00048
#if DBG
00049
00050
extern ULONG RtlDebugFlags;
00051
#define DBGPRINT ((RtlDebugFlags & 0x4) != 0) && DbgPrint
00052
00053
#else
00054
00055 #define DBGPRINT 0 && DbgPrint
00056
00057
#endif
00058
00059
00060
00061
00062
00063
00064
00065 #define NON_IEEE(ExceptionRecord, Reason) \
00066
(ExceptionRecord)->NumberParameters = 4; \
00067
(ExceptionRecord)->ExceptionInformation[3] = (Reason);
00068
00069 #define TRIGGER_FLOATING_REGISTER_MASK_CLEAR 1
00070 #define TRIGGER_INTEGER_REGISTER_MASK_SET 2
00071 #define TRIGGER_NO_SOFTWARE_COMPLETION 3
00072 #define TRIGGER_INVALID_INSTRUCTION_FOUND 4
00073 #define TRIGGER_INSTRUCTION_FETCH_ERROR 5
00074 #define TRIGGER_INSTRUCTION_NOT_FOUND 6
00075 #define TRIGGER_SOURCE_IS_DESTINATION 7
00076 #define TRIGGER_WRONG_INSTRUCTION 8
00077
00078 BOOLEAN
00079 KiFloatingException (
00080 IN OUT PEXCEPTION_RECORD ExceptionRecord,
00081 IN OUT PKEXCEPTION_FRAME ExceptionFrame,
00082 IN OUT PKTRAP_FRAME TrapFrame,
00083 IN BOOLEAN ImpreciseTrap,
00084 IN OUT PULONG SoftFpcrCopy
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
00120 BOOLEAN
Status;
00121 PSW_FPCR SoftwareFpcr;
00122 PTEB Teb;
00123
00124
try {
00125
00126
00127
00128
00129
00130 Teb = NtCurrentTeb();
00131 *SoftFpcrCopy = Teb->FpSoftwareStatusRegister;
00132 SoftwareFpcr = (PSW_FPCR)SoftFpcrCopy;
00133
DBGPRINT(
"KiFloatingException: SoftFpcr = %.8lx\n", *SoftFpcrCopy);
00134
00135
#if DBG
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
if (SoftwareFpcr->NoSoftwareEmulation != 0) {
00146
DBGPRINT(
"KiFloatingException: NoSoftwareEmulation\n");
00147
return FALSE;
00148 }
00149
#endif
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
if (ImpreciseTrap !=
FALSE) {
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
if (SoftwareFpcr->ArithmeticTrapIgnore != 0) {
00173
return TRUE;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
if (
KiLocateTriggerPc(ExceptionRecord, TrapFrame) ==
FALSE) {
00188
KiSetFloatingStatus(ExceptionRecord);
00189
return FALSE;
00190 }
00191
Status =
KiEmulateFloating(ExceptionRecord,
00192 ExceptionFrame,
00193 TrapFrame,
00194 SoftwareFpcr);
00195
00196 }
else {
00197
00198
00199
00200
00201
00202
00203
00204
00205
Status =
KiEmulateFloating(ExceptionRecord,
00206 ExceptionFrame,
00207 TrapFrame,
00208 SoftwareFpcr);
00209
00210
00211
00212
00213
00214
00215
00216
if ((
Status ==
FALSE) &&
00217 (SoftwareFpcr->ArithmeticTrapIgnore != 0) &&
00218 (ExceptionRecord->ExceptionCode != STATUS_ILLEGAL_INSTRUCTION)) {
00219
Status =
TRUE;
00220 }
00221 }
00222
00223
00224
00225
00226
00227 Teb->FpSoftwareStatusRegister = *SoftFpcrCopy;
00228
DBGPRINT(
"KiFloatingException: SoftFpcr = %.8lx\n", *SoftFpcrCopy);
00229
00230 } except (
EXCEPTION_EXECUTE_HANDLER) {
00231
00232
00233
00234
00235
00236 ExceptionRecord->ExceptionCode = GetExceptionCode();
00237
return FALSE;
00238 }
00239
00240
return Status;
00241 }
00242
00243 BOOLEAN
00244 KiLocateTriggerPc (
00245 IN OUT PEXCEPTION_RECORD ExceptionRecord,
00246 IN OUT PKTRAP_FRAME TrapFrame
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
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 {
00305
00306 PEXC_SUM ExceptionSummary;
00307 ULONG Fa;
00308 ULONG Fb;
00309 ULONG Fc;
00310 ULONG FloatRegisterTrashMask;
00311 ULONG FloatRegisterWriteMask;
00312 ALPHA_INSTRUCTION Instruction;
00313 ULONG IntegerRegisterWriteMask;
00314 ULONG Opcode;
00315 ULONG_PTR TrapShadowLowLimit;
00316 ULONG_PTR TriggerPc;
00317
KPROCESSOR_MODE PreviousMode;
00318
00319
00320
00321
00322
00323
00324
00325 FloatRegisterWriteMask = (ULONG)ExceptionRecord->ExceptionInformation[0];
00326 IntegerRegisterWriteMask = (ULONG)ExceptionRecord->ExceptionInformation[1];
00327 ExceptionSummary = (PEXC_SUM)&(ExceptionRecord->ExceptionInformation[2]);
00328
DBGPRINT(
"KiLocateTriggerPc: WriteMask %.8lx.%.8lx, ExceptionSummary %.8lx\n",
00329 FloatRegisterWriteMask, IntegerRegisterWriteMask,
00330 *(PULONG)ExceptionSummary);
00331
00332
00333
00334
00335
00336 PreviousMode = (
KPROCESSOR_MODE)(((
PSR *)(&TrapFrame->Psr))->MODE);
00337
00338
if (FloatRegisterWriteMask == 0) {
00339
00340
00341
00342
00343
00344
00345
00346
DBGPRINT(
"KiLocateTriggerPc: FloatRegisterWriteMask clear\n");
00347
NON_IEEE(ExceptionRecord,
TRIGGER_FLOATING_REGISTER_MASK_CLEAR);
00348
return FALSE;
00349 }
00350
if (IntegerRegisterWriteMask != 0) {
00351
00352
00353
00354
00355
00356
00357
DBGPRINT(
"KiLocateTriggerPc: IntegerRegisterMask set.\n");
00358
NON_IEEE(ExceptionRecord,
TRIGGER_INTEGER_REGISTER_MASK_SET);
00359
return FALSE;
00360 }
00361
if (ExceptionSummary->SoftwareCompletion == 0) {
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
DBGPRINT(
"KiLocateTriggerPc: SoftwareCompletion clear\n");
00373
NON_IEEE(ExceptionRecord,
TRIGGER_NO_SOFTWARE_COMPLETION);
00374
return FALSE;
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 FloatRegisterTrashMask = 0;
00387 TriggerPc = (ULONG_PTR)TrapFrame->Fir;
00388 TrapShadowLowLimit = TriggerPc - (500 *
sizeof(ULONG));
00389
00390
try {
00391
do {
00392 TriggerPc -= 4;
00393
if (TriggerPc < TrapShadowLowLimit) {
00394
00395
00396
00397
00398
00399
00400
DBGPRINT(
"KiLocateTriggerPc: Trap shadow too long\n");
00401
NON_IEEE(ExceptionRecord,
TRIGGER_INSTRUCTION_NOT_FOUND);
00402
return FALSE;
00403 }
00404
00405
if (PreviousMode !=
KernelMode) {
00406 Instruction.Long =
ProbeAndReadUlong((PULONG)TriggerPc);
00407 }
else {
00408 Instruction.Long = *((PULONG)TriggerPc);
00409 }
00410
00411
00412
00413
00414
00415
00416 Opcode = Instruction.Memory.Opcode;
00417
if (Opcode == JMP_OP) {
00418
00419
00420
00421
00422
00423
00424
DBGPRINT(
"KiLocateTriggerPc: Jump within Trap Shadow\n");
00425
NON_IEEE(ExceptionRecord,
TRIGGER_INVALID_INSTRUCTION_FOUND);
00426
return FALSE;
00427
00428 }
else if ((Opcode >= BR_OP) && (Opcode <= BGT_OP)) {
00429
00430
00431
00432
00433
00434
00435
00436
DBGPRINT(
"KiLocateTriggerPc: Branch within Trap Shadow\n");
00437
NON_IEEE(ExceptionRecord,
TRIGGER_INVALID_INSTRUCTION_FOUND);
00438
return FALSE;
00439
00440 }
else if ((Instruction.Memory.Opcode == MEMSPC_OP) &&
00441 ((Instruction.Memory.MemDisp == TRAPB_FUNC) ||
00442 (Instruction.Memory.MemDisp == EXCB_FUNC))) {
00443
00444
00445
00446
00447
00448
00449
DBGPRINT(
"KiLocateTriggerPc: Trapb within Trap Shadow\n");
00450
NON_IEEE(ExceptionRecord,
TRIGGER_INVALID_INSTRUCTION_FOUND);
00451
return FALSE;
00452
00453 }
else if (Opcode == CALLPAL_OP) {
00454
00455
00456
00457
00458
00459
DBGPRINT(
"KiLocateTriggerPc: Call PAL within Trap Shadow\n");
00460
NON_IEEE(ExceptionRecord,
TRIGGER_INVALID_INSTRUCTION_FOUND);
00461
return FALSE;
00462
00463 }
else if ((Opcode == IEEEFP_OP) || (Opcode == FPOP_OP)) {
00464
00465
00466
00467
00468
00469
00470
00471 Fc = Instruction.FpOp.Fc;
00472
if (Fc != FZERO_REG) {
00473 FloatRegisterTrashMask |= (1 << Fc);
00474 }
00475 FloatRegisterWriteMask &= ~(1 << Fc);
00476 }
00477
00478 }
while (FloatRegisterWriteMask != 0);
00479
00480
00481
00482
00483
00484
00485
00486
if ((Instruction.FpOp.Function & FP_TRAP_ENABLE_S) == 0) {
00487
DBGPRINT(
"KiLocateTriggerPc: Trigger instruction missing /S\n");
00488
NON_IEEE(ExceptionRecord,
TRIGGER_WRONG_INSTRUCTION);
00489
return FALSE;
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 Fa = Instruction.FpOp.Fa;
00502 Fb = Instruction.FpOp.Fb;
00503
if ((FloatRegisterTrashMask & ((1 << Fa) | (1 << Fb))) != 0) {
00504
DBGPRINT(
"KiLocateTriggerPc: Source is destination\n");
00505
NON_IEEE(ExceptionRecord,
TRIGGER_SOURCE_IS_DESTINATION);
00506
return FALSE;
00507 }
00508
00509 } except (
EXCEPTION_EXECUTE_HANDLER) {
00510
00511
00512
00513
00514
00515
00516
DBGPRINT(
"KiLocateTriggerPc: Instruction fetch error\n");
00517
NON_IEEE(ExceptionRecord,
TRIGGER_INSTRUCTION_FETCH_ERROR);
00518
return FALSE;
00519 }
00520
00521
00522
00523
00524
00525
00526
00527
DBGPRINT(
"KiLocateTriggerPc: Exception PC = %p, Trigger PC = %p\n",
00528 ExceptionRecord->ExceptionAddress, TriggerPc);
00529 ExceptionRecord->ExceptionAddress = (PVOID)TriggerPc;
00530 TrapFrame->Fir = (ULONGLONG)(LONG_PTR)(TriggerPc + 4);
00531
return TRUE;
00532 }
00533
00534
VOID
00535 KiSetFloatingStatus (
00536 IN OUT PEXCEPTION_RECORD ExceptionRecord
00537 )
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 {
00557
00558 PEXC_SUM ExceptionSummary;
00559
00560
00561
00562
00563
00564
00565
00566
DBGPRINT(
"KiSetFloatingStatus: ExceptionSummary = %.8lx\n",
00567 ExceptionRecord->ExceptionInformation[2]);
00568
00569 ExceptionSummary = (PEXC_SUM)(&ExceptionRecord->ExceptionInformation[2]);
00570
if (ExceptionSummary->InvalidOperation != 0) {
00571 ExceptionRecord->ExceptionCode = STATUS_FLOAT_INVALID_OPERATION;
00572
00573 }
else if (ExceptionSummary->DivisionByZero != 0) {
00574 ExceptionRecord->ExceptionCode = STATUS_FLOAT_DIVIDE_BY_ZERO;
00575
00576 }
else if (ExceptionSummary->Overflow != 0) {
00577 ExceptionRecord->ExceptionCode = STATUS_FLOAT_OVERFLOW;
00578
00579 }
else if (ExceptionSummary->Underflow != 0) {
00580 ExceptionRecord->ExceptionCode = STATUS_FLOAT_UNDERFLOW;
00581
00582 }
else if (ExceptionSummary->InexactResult != 0) {
00583 ExceptionRecord->ExceptionCode = STATUS_FLOAT_INEXACT_RESULT;
00584
00585 }
else if (ExceptionSummary->IntegerOverflow != 0) {
00586 ExceptionRecord->ExceptionCode = STATUS_INTEGER_OVERFLOW;
00587
00588 }
else {
00589 ExceptionRecord->ExceptionCode = STATUS_FLOAT_STACK_CHECK;
00590 }
00591 }