sighnd.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. { use a trampoline which pushes the return address for proper unwinding }
  14. Procedure SignalToHandleErrorAddrFrame (Errno : longint;addr : CodePointer; frame : Pointer); nostackframe; assembler;
  15. asm
  16. pushl addr
  17. jmp HandleErrorAddrFrame
  18. end;
  19. procedure SignalToRunerror(sig : longint; SigInfo: PSigInfo; UContext: Pucontext);public name '_FPC_DEFAULTSIGHANDLER';cdecl;
  20. var
  21. res,fpustate,MMState : word;
  22. begin
  23. res:=0;
  24. case sig of
  25. SIGFPE :
  26. begin
  27. { this is not allways necessary but I don't know yet
  28. how to tell if it is or not PM }
  29. res:=200;
  30. {$ifdef SYSTEM_DEBUG}
  31. if assigned(ucontext^.uc_mcontext.fpstate) then
  32. begin
  33. writeln('magic: $',hexstr(ucontext^.uc_mcontext.fpstate^.magic,4));
  34. writeln('magic1: $',hexstr(ucontext^.uc_mcontext.fpstate^.sw_reserved.magic1,8));
  35. end
  36. else
  37. writeln('fpstate=nil');
  38. {$endif SYSTEM_DEBUG}
  39. if SigInfo^.si_code<>FPE_INTDIV then
  40. begin
  41. if assigned(ucontext^.uc_mcontext.fpstate) then
  42. begin
  43. FpuState:=ucontext^.uc_mcontext.fpstate^.sw;
  44. if (FpuState and FPU_ExceptionMask) <> 0 then
  45. begin
  46. { first check the more precise options }
  47. if (FpuState and FPU_DivisionByZero)<>0 then
  48. res:=208
  49. else if (FpuState and (FPU_StackOverflow or FPU_StackUnderflow or FPU_Invalid))<>0 Then
  50. res:=207
  51. else if (FpuState and FPU_Overflow)<>0 then
  52. res:=205
  53. else if (FpuState and FPU_Underflow)<>0 then
  54. res:=206
  55. else if (FpuState and FPU_Denormal)<>0 then
  56. res:=206
  57. else
  58. res:=207; {'Coprocessor Error'}
  59. end
  60. else
  61. begin
  62. { SSE data? }
  63. if ucontext^.uc_mcontext.fpstate^.magic=0 then
  64. begin
  65. MMState:=ucontext^.uc_mcontext.fpstate^.mxcsr;
  66. if (MMState and MM_ExceptionMask)<>0 then
  67. begin
  68. { first check the more precise options }
  69. if (MMState and MM_DivisionByZero)<>0 then
  70. res:=208
  71. else if (MMState and MM_Invalid)<>0 Then
  72. res:=207
  73. else if (MMState and MM_Overflow)<>0 then
  74. res:=205
  75. else if (MMState and MM_Underflow)<>0 then
  76. res:=206
  77. else if (MMState and MM_Denormal)<>0 then
  78. res:=206
  79. else
  80. res:=207; {'Coprocessor Error'}
  81. end;
  82. end;
  83. end;
  84. end;
  85. end;
  86. if assigned(ucontext^.uc_mcontext.fpstate) then
  87. with ucontext^.uc_mcontext.fpstate^ do
  88. begin
  89. { Reset Status word }
  90. sw:=sw and not(FPU_ExceptionMask);
  91. if magic=0 then
  92. mxcsr:=mxcsr and not(MM_ExceptionMask);
  93. cw:=Default8087CW;
  94. { Reset Tag word to $ffff for all empty }
  95. tag:=$ffff;
  96. end;
  97. end;
  98. SIGBUS:
  99. res:=214;
  100. SIGILL:
  101. if sse_check then
  102. begin
  103. os_supports_sse:=false;
  104. res:=0;
  105. inc(ucontext^.uc_mcontext.eip,3);
  106. end
  107. else
  108. res:=216;
  109. SIGSEGV :
  110. res:=216;
  111. SIGINT:
  112. res:=217;
  113. SIGQUIT:
  114. res:=233;
  115. end;
  116. reenable_signal(sig);
  117. { give runtime error at the position where the signal was raised }
  118. if res<>0 then
  119. begin
  120. ucontext^.uc_mcontext.eax := res;
  121. ucontext^.uc_mcontext.edx := ucontext^.uc_mcontext.eip;
  122. ucontext^.uc_mcontext.ecx := ucontext^.uc_mcontext.ebp;
  123. ucontext^.uc_mcontext.eip := ptruint(@SignalToHandleErrorAddrFrame);
  124. end;
  125. end;