sighnd.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {
  2. This file is part of the Free Pascal run time library.
  3. (c) 2008 by Jonas Maebe
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. Signalhandler for Darwin/arm
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY;without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. procedure SignalToRunerror(Sig: cint; info : PSigInfo; SigContext:PSigContext); public name '_FPC_DEFAULTSIGHANDLER'; cdecl;
  13. var
  14. fpuexceptionflags: cardinal;
  15. res : word;
  16. begin
  17. res:=0;
  18. case sig of
  19. SIGFPE :
  20. begin
  21. Case Info^.si_code Of
  22. FPE_FLTDIV : Res:=208; { floating point divide by zero }
  23. FPE_INTDIV : Res:=200; { integer divide by zero }
  24. FPE_FLTOVF : Res:=205; { floating point overflow }
  25. FPE_FLTUND : Res:=206; { floating point underflow }
  26. FPE_FLTRES, { floating point inexact result }
  27. FPE_FLTINV : Res:=207; { invalid floating point operation }
  28. Else
  29. Res:=207; {coprocessor error}
  30. SigContext^.uc_mcontext^.__ns.__fpsr:=SigContext^.uc_mcontext^.__ns.__fpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift);
  31. end;
  32. end;
  33. SIGBUS:
  34. res:=214;
  35. SIGILL:
  36. begin
  37. { right now, macOS generates SIGILL signals for fpu exceptions on AArch64.
  38. Additionally, fpsr is 0 in the context when this happens. Fortunately,
  39. the esr is valid, so we can decode that one. }
  40. if (Info^.si_code=ILL_ILLTRP) and
  41. { Trapped AArch64 floating point exception }
  42. ((SigContext^.uc_mcontext^.__es.__esr and __ESR_EC_Mask)=__ESR_EC_TrappedAArch64_FloatingPoint) then
  43. begin
  44. { the FPU status bits in the ESR are valid }
  45. if (SigContext^.uc_mcontext^.__es.__esr and __ESR_ISS_TFV)<>0 then
  46. begin
  47. fpuexceptionflags:=(SigContext^.uc_mcontext^.__es.__esr shl fpu_exception_mask_to_status_mask_shift) and fpu_exception_mask;
  48. if (fpuexceptionflags and fpu_dze)<>0 then
  49. res:=208
  50. else if (fpuexceptionflags and fpu_ofe)<>0 then
  51. res:=205
  52. else if (fpuexceptionflags and fpu_ufe)<>0 then
  53. res:=206
  54. else if (fpuexceptionflags and fpu_ioe)<>0 then
  55. res:=207
  56. else if (fpuexceptionflags and fpu_ixe)<>0 then
  57. res:=207
  58. else if (fpuexceptionflags and fpu_ide)<>0 then
  59. res:=216
  60. else
  61. { unknown FPU exception }
  62. res:=207
  63. end
  64. else
  65. { unknown FPU exception }
  66. res:=207;
  67. end
  68. else
  69. res:=216;
  70. { for safety, always clear in case we had a SIGILL to prevent potential
  71. infinite trap loops, even if it can cause us to miss some FPU
  72. exceptions in case we process an actual illegal instruction }
  73. SigContext^.uc_mcontext^.__ns.__fpsr:=SigContext^.uc_mcontext^.__ns.__fpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift);
  74. end;
  75. SIGSEGV :
  76. res:=216;
  77. SIGINT:
  78. res:=217;
  79. SIGQUIT:
  80. res:=233;
  81. end;
  82. {$ifdef FPC_USE_SIGPROCMASK}
  83. reenable_signal(sig);
  84. {$endif }
  85. { return to trampoline }
  86. if res <> 0 then
  87. begin
  88. SigContext^.uc_mcontext^.__ss.__r[0] := res;
  89. SigContext^.uc_mcontext^.__ss.__r[1] := SigContext^.uc_mcontext^.__ss.__pc;
  90. SigContext^.uc_mcontext^.__ss.__r[2] := SigContext^.uc_mcontext^.__ss.__fp;
  91. pointer(SigContext^.uc_mcontext^.__ss.__pc) := @HandleErrorAddrFrame;
  92. end;
  93. end;