x86hnd.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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); public name '_FPC_DEFAULTSIGHANDLER'; 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. {$ifdef cpu64}
  34. p:=pbyte(sigcontext^.uc_mcontext^.ts.rip);
  35. {$else cpu64}
  36. p:=pbyte(sigcontext^.uc_mcontext^.ts.eip);
  37. {$endif cpu64}
  38. { skip some prefix bytes }
  39. while (p^ in [{$ifdef cpu64}$40..$4f,{$endif}$66,$67]) do
  40. inc(p);
  41. if (p^ in [$f6,$f7]) and
  42. (((p+1)^ and (%110 shl 3)) = (%110 shl 3)) then
  43. Res:=200
  44. else
  45. Res:=207; { coprocessor error }
  46. end;
  47. end;
  48. { make sure any fpu operations won't trigger new exceptions in handler }
  49. sysResetFPU;
  50. { Now clear exception flags in the context }
  51. { perform an fnclex: clear exception and busy flags }
  52. sigcontext^.uc_mcontext^.fs.fpu_fsw.flag0:=
  53. sigcontext^.uc_mcontext^.fs.fpu_fsw.flag0 and (not(%11111111) and not(1 shl 15));
  54. { also clear sse exception flags }
  55. sigcontext^.uc_mcontext^.fs.fpu_mxcsr:=
  56. sigcontext^.uc_mcontext^.fs.fpu_mxcsr and not(%111111)
  57. end;
  58. SIGILL,
  59. SIGBUS,
  60. SIGSEGV :
  61. res:=216;
  62. SIGINT:
  63. res:=217;
  64. SIGQUIT:
  65. res:=233;
  66. end;
  67. {$ifdef FPC_USE_SIGPROCMASK}
  68. reenable_signal(sig);
  69. {$endif }
  70. if (res <> 0) then
  71. begin
  72. {$ifdef cpu64}
  73. sigcontext^.uc_mcontext^.ts.rdi:=res;
  74. sigcontext^.uc_mcontext^.ts.rsi:=sigcontext^.uc_mcontext^.ts.rip;
  75. sigcontext^.uc_mcontext^.ts.rdx:=sigcontext^.uc_mcontext^.ts.rbp;
  76. { the ABI expects the stack pointer to be 8 bytes off alignment
  77. due to the return address which has been pushed -- but take into account
  78. that rsp may already unaligned in case of a leaf routine }
  79. if (sigcontext^.uc_mcontext^.ts.rsp and 15)=0 then
  80. dec(sigcontext^.uc_mcontext^.ts.rsp,sizeof(pointer));
  81. { return to run time error handler }
  82. sigcontext^.uc_mcontext^.ts.rip:=ptruint(@HandleErrorAddrFrame);
  83. {$else cpu64}
  84. { assume regcall calling convention is the default }
  85. sigcontext^.uc_mcontext^.ts.eax:=res;
  86. sigcontext^.uc_mcontext^.ts.edx:=sigcontext^.uc_mcontext^.ts.eip;
  87. sigcontext^.uc_mcontext^.ts.ecx:=sigcontext^.uc_mcontext^.ts.ebp;
  88. { the ABI expects the stack pointer to be 4 bytes off alignment }
  89. { due to the return address which has been pushed -- but take into account
  90. that esp may already unaligned in case of a leaf routine }
  91. if (sigcontext^.uc_mcontext^.ts.esp and 15)=0 then
  92. dec(sigcontext^.uc_mcontext^.ts.esp,sizeof(pointer));
  93. { return to run time error handler }
  94. sigcontext^.uc_mcontext^.ts.eip:=ptruint(@HandleErrorAddrFrame);
  95. {$endif cpu64}
  96. end;
  97. end;