sighnd.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. member of the Free Pascal development team.
  5. Signal handler is arch dependant due to processor to language
  6. exception conversion.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. const
  14. { Bits in control register }
  15. RoundingMode = $30;
  16. RoundingPrecision = $c0;
  17. { Exception bits common to status and control registers }
  18. InexactDecimal = $100;
  19. InexactOperation = $200;
  20. DivideByZero = $400;
  21. UnderFlow = $800;
  22. OverFlow = $1000;
  23. OperandError = $2000;
  24. SignalingNaN = $4000;
  25. BranchOnUnordered = $8000;
  26. FPU_ES_Mask = $ff00;
  27. { Accrued exception bit only in status register }
  28. AE_Inexact = $8;
  29. AE_DivideByZero = $10;
  30. AE_Underflow = $20;
  31. AE_Overflow = $40;
  32. AE_InvalidOperation = $80;
  33. FPU_AE_Mask = $F80;
  34. reset_fpucw : longint = {InexactOperation or }DivideByZero or
  35. OverFlow or OperandError or
  36. SignalingNaN or BranchOnUnordered;
  37. reset_fpust : longint = 0;
  38. { Bits in psr SigContext field }
  39. PSR_Invalid = $80;
  40. PSR_Denormal = $8;
  41. PSR_DivisionByZero = $10;
  42. PSR_Overflow = $40;
  43. PSR_Underflow = $20;
  44. { m68k is not stack based }
  45. PSR_StackUnderflow = $0;
  46. PSR_StackOverflow = $0;
  47. FPU_Status_Exception_Mask = FPU_ES_Mask or FPU_AE_Mask;
  48. PSR_Exception_Mask =$f8;
  49. FPU_Control_Exception_Mask = FPU_ES_Mask;
  50. Procedure ResetFPU;
  51. var
  52. l_fpucw : longint;
  53. begin
  54. {$if defined(FPU68881) or defined(FPUCOLDFIRE)}
  55. asm
  56. fmove.l fpcr,l_fpucw
  57. end;
  58. { Reset only exception based control bits in fpcr }
  59. l_fpucw := (l_fpucw and not (dword(FPU_Control_Exception_Mask)))
  60. or (reset_fpucw and FPU_Control_Exception_Mask);
  61. asm
  62. fmove.l l_fpucw,fpcr
  63. { Reset fpsr to zero }
  64. fmove.l reset_fpust,fpsr
  65. end;
  66. {$endif}
  67. end;
  68. function GetFPUState(const SigContext : TSigContext) : dword;
  69. begin
  70. GetfpuState:=dword(SigContext.psr);
  71. {$ifdef SYSTEM_DEBUG}
  72. Writeln(stderr,'FpuState = ',GetFpuState);
  73. {$endif SYSTEM_DEBUG}
  74. end;
  75. procedure SignalToRunerror(Sig: longint; Info : pointer; var SigContext: TSigContext); public name '_FPC_DEFAULTSIGHANDLER'; cdecl;
  76. var
  77. res : word;
  78. fpustate : dword;
  79. begin
  80. res:=0;
  81. case sig of
  82. SIGFPE :
  83. begin
  84. { this is not allways necessary but I don't know yet
  85. how to tell if it is or not PM }
  86. res:=200;
  87. fpustate:=GetFPUState(SigContext);
  88. if (FpuState and FPU_Status_Exception_Mask) <> 0 then
  89. begin
  90. { first check the more precise options }
  91. if (FpuState and (DivideByZero or AE_DividebyZero))<>0 then
  92. res:=208
  93. else if (FpuState and (Overflow or AE_Overflow))<>0 then
  94. res:=205
  95. else if (FpuState and (Underflow or AE_Underflow))<>0 then
  96. res:=206
  97. else if (FpuState and PSR_Denormal)<>0 then
  98. res:=206
  99. { else if (FpuState and (PSR_StackOverflow or PRS_StackUnderflow))<>0 then
  100. res:=207, disabled, as there is no fpu stack }
  101. else if (FpuState and (OperandError or SignalingNan or BranchOnUnordered or AE_InvalidOperation))<>0 then
  102. res:=216
  103. else
  104. res:=207; {'Coprocessor Error'}
  105. end;
  106. ResetFPU;
  107. end;
  108. SIGILL,
  109. SIGBUS,
  110. SIGSEGV :
  111. res:=216;
  112. SIGINT:
  113. res:=217;
  114. SIGQUIT:
  115. res:=233;
  116. end;
  117. reenable_signal(sig);
  118. { give runtime error at the position where the signal was raised }
  119. if res<>0 then
  120. begin
  121. { HandleErrorAddrFrame(res,SigContext.sc_pc,SigContext.sc_fp);}
  122. { fp is not saved in context record :( }
  123. HandleError(res);
  124. HandleError(res);
  125. end;
  126. end;