sighnd.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. {
  2. This file is part of the Free Pascal run time library.
  3. (c) 2000-2003 by Marco van de Voort
  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 FreeBSD/i386
  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); cdecl;
  13. var
  14. p: pbyte;
  15. res : word;
  16. begin
  17. res:=0;
  18. case sig of
  19. SIGFPE :
  20. begin
  21. Case Info^.si_code Of
  22. FPE_INTDIV, { integer divide by zero -NOTIMP on Mac OS X 10.4.7 }
  23. FPE_FLTDIV : Res:=200; { floating point 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. begin
  30. { Assume that if an integer divide was executed, the }
  31. { error was a divide-by-zero (FPE_INTDIV is not }
  32. { implemented as of 10.5.0) }
  33. p:=pbyte(sigcontext^.uc_mcontext^.ts.eip);
  34. if assigned(p) then
  35. begin
  36. { skip some prefix bytes }
  37. while (p^ in [$66,$67]) do
  38. inc(p);
  39. if (p^ in [$f6,$f7]) and
  40. (((p+1)^ and (%110 shl 3)) = (%110 shl 3)) then
  41. Res:=200
  42. else
  43. Res:=207; { coprocessor error }
  44. end
  45. else
  46. Res:=207;
  47. end;
  48. end;
  49. { make sure any fpu operations won't trigger new exceptions in handler }
  50. sysResetFPU;
  51. { Now clear exception flags in the context }
  52. { perform an fnclex: clear exception and busy flags }
  53. sigcontext^.uc_mcontext^.fs.fpu_fsw.flag0:=
  54. sigcontext^.uc_mcontext^.fs.fpu_fsw.flag0 and (not(%11111111) and not(1 shl 15));
  55. { also clear sse exception flags }
  56. sigcontext^.uc_mcontext^.fs.fpu_mxcsr:=
  57. sigcontext^.uc_mcontext^.fs.fpu_mxcsr and not(%111111)
  58. end;
  59. SIGILL,
  60. SIGBUS,
  61. SIGSEGV :
  62. res:=216;
  63. end;
  64. {$ifdef FPC_USE_SIGPROCMASK}
  65. reenable_signal(sig);
  66. {$endif }
  67. if (res <> 0) then
  68. begin
  69. { assume regcall calling convention is the default }
  70. sigcontext^.uc_mcontext^.ts.eax:=res;
  71. sigcontext^.uc_mcontext^.ts.edx:=sigcontext^.uc_mcontext^.ts.eip;
  72. sigcontext^.uc_mcontext^.ts.ecx:=sigcontext^.uc_mcontext^.ts.ebp;
  73. { the ABI expects the stack pointer to be 4 bytes off alignment }
  74. { due to the return address which has been pushed }
  75. dec(sigcontext^.uc_mcontext^.ts.esp,sizeof(pointer));
  76. { return to run time error handler }
  77. sigcontext^.uc_mcontext^.ts.eip:=ptruint(@HandleErrorAddrFrame);
  78. end;
  79. end;