sighnd.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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:=216
  57. else
  58. res:=207; {'Coprocessor Error'}
  59. end;
  60. { SSE data? }
  61. if ucontext^.uc_mcontext.fpstate^.magic=0 then
  62. begin
  63. MMState:=ucontext^.uc_mcontext.fpstate^.mxcsr;
  64. if (MMState and MM_ExceptionMask)<>0 then
  65. begin
  66. { first check the more precise options }
  67. if (MMState and MM_DivisionByZero)<>0 then
  68. res:=208
  69. else if (MMState and MM_Invalid)<>0 Then
  70. res:=207
  71. else if (MMState and MM_Overflow)<>0 then
  72. res:=205
  73. else if (MMState and MM_Underflow)<>0 then
  74. res:=206
  75. else if (MMState and MM_Denormal)<>0 then
  76. res:=216
  77. else
  78. res:=207; {'Coprocessor Error'}
  79. end;
  80. end;
  81. end;
  82. end;
  83. if assigned(ucontext^.uc_mcontext.fpstate) then
  84. with ucontext^.uc_mcontext.fpstate^ do
  85. begin
  86. { Reset Status word }
  87. sw:=sw and not(FPU_ExceptionMask);
  88. if magic=0 then
  89. mxcsr:=mxcsr and not(MM_ExceptionMask);
  90. cw:=Default8087CW;
  91. { Reset Tag word to $ffff for all empty }
  92. tag:=$ffff;
  93. end;
  94. end;
  95. SIGBUS:
  96. res:=214;
  97. SIGILL:
  98. if sse_check then
  99. begin
  100. os_supports_sse:=false;
  101. res:=0;
  102. inc(ucontext^.uc_mcontext.eip,3);
  103. end
  104. else
  105. res:=216;
  106. SIGSEGV :
  107. res:=216;
  108. SIGINT:
  109. res:=217;
  110. SIGQUIT:
  111. res:=233;
  112. end;
  113. reenable_signal(sig);
  114. { give runtime error at the position where the signal was raised }
  115. if res<>0 then
  116. begin
  117. ucontext^.uc_mcontext.eax := res;
  118. ucontext^.uc_mcontext.edx := ucontext^.uc_mcontext.eip;
  119. ucontext^.uc_mcontext.ecx := ucontext^.uc_mcontext.ebp;
  120. ucontext^.uc_mcontext.eip := ptruint(@SignalToHandleErrorAddrFrame);
  121. end;
  122. end;