Commit d7850f57 authored by Raymond Toy's avatar Raymond Toy
Browse files

Restore the FPU state before exiting.

Put an unwind-protect around the error calls.  The cleanup form
restores the floating-point modes from the sigcontext so that the mode
is restored.  This is needed, I think, because we throw so that the
signal handler doesn't return so the sigcontext isn't restored.  If we
don't restore the fpu state, it's set to the default processor state.
We want the default state when calling error.

In this way, things like (* 1d300 1d300) signals an overflow, and when
we throw to top-level, the floating-point modes are restored to their
original values they had before.
parent d9763e90
Loading
Loading
Loading
Loading
+56 −49
Changes for src/code/float-trap.lisp: 56 added lines, 49 removed lines.
Original line number Diff line number Diff line
@@ -312,7 +312,6 @@
;;;
;;;    Signal the appropriate condition when we get a floating-point error.
;;;
#+nil
(defvar *debug-sigfpe-handler* nil)
(defun sigfpe-handler (signal code scp)
  (declare (ignore signal)
@@ -321,60 +320,19 @@
		 (alien:sap-alien scp (* unix:sigcontext))))
	 (traps (logand (ldb float-exceptions-byte modes)
			(ldb float-traps-byte modes))))
    #+(and darwin ppc)
    (let* ((new-modes modes)
	   (new-exceptions (logandc2 (ldb float-exceptions-byte new-modes)
				     traps)))
      ;; (format t "sigfpe: modes   = #B~32,'0b~%" modes)
      ;; (format t "sigfpe: new-exc = #B~32,'0b~%" new-exceptions)
      (setf (ldb float-exceptions-byte new-modes) new-exceptions)
      ;; Clear out all exceptions and save them to the context.
      ;;
      ;; XXX: Should we just clear out the bits for the traps that are
      ;; enabled?  If we did that then the accrued exceptions would be
      ;; correct.
      (setf (ldb float-sticky-bits new-modes) 0)
      ;; Clear out the various sticky invalid operation bits too.
      ;;
      ;; XXX: Should we only do that if the invalid trap is enabled?
      (setf (ldb float-invalid-op-1-byte new-modes) 0)
      (setf (ldb float-invalid-op-2-byte new-modes) 0)
      ;; Clear the FP exception summary bit too.
      (setf (ldb float-exceptions-summary-byte new-modes) 0)
      ;; (format t "sigfpe: new modes   = #B~32,'0b~%" new-modes)
      (setf (floating-point-modes) new-modes)
      (setf (sigcontext-floating-point-modes
	     (alien:sap-alien scp (* unix:sigcontext)))
	    new-modes))

    #+nil
    (let* ((new-modes modes)
	   (new-exceptions (logandc2 (ldb float-exceptions-byte new-modes)
				     traps)))
      ;; Clear out the status for any enabled traps.  With SSE2, if
      ;; the current exception is enabled, the next FP instruction
      ;; will cause the exception to be signaled again.  Hence, we
      ;; need to clear out the exceptions that we are handling here.
      (setf (ldb float-exceptions-byte new-modes) new-exceptions)
      ;; XXX: This seems not right.  Shouldn't we be setting the modes
      ;; in the sigcontext instead?  This however seems to do what we
      ;; want.

      (when *debug-sigfpe-handler*
	(format *debug-io* "sigcontext modes: #x~4x (~A)~%"
		modes (decode-floating-point-modes modes))
	(format *debug-io* "current modes:    #x~4x (~A)~%"
		(vm:floating-point-modes) (get-floating-point-modes))
	(format *debug-io* "new  modes: #x~x (~A)~%"
		new-modes (decode-floating-point-modes new-modes)))
      #+nil
      (setf (vm:floating-point-modes) new-modes))
    
    (multiple-value-bind (fop operands)
	(let ((sym (find-symbol "GET-FP-OPERANDS" "VM")))
	  (if (fboundp sym)
	      (funcall sym (alien:sap-alien scp (* unix:sigcontext)) modes)
	      (values nil nil)))
      ;; Don't let throws get away without resetting the
      ;; floating-point modes back to the original values which we get
      ;; from the sigcontext.  Because we can throw, we never return
      ;; from the signal handler so the sigcontext is never restored.
      ;; This means we need to restore the fpu state ourselves.
      (unwind-protect
	   (cond ((not (zerop (logand float-divide-by-zero-trap-bit traps)))
		  (error 'division-by-zero
			 :operation fop
@@ -425,7 +383,56 @@
			     :operation fop
			     :operands operands)
		      (error _"SIGFPE with no exceptions currently enabled? (si-code = ~D)"
			code)))))))
			     code))))
	;; Cleanup
	#+(and darwin ppc)
	(let* ((new-modes modes)
	       (new-exceptions (logandc2 (ldb float-exceptions-byte new-modes)
					 traps)))
	  ;; (format t "sigfpe: modes   = #B~32,'0b~%" modes)
	  ;; (format t "sigfpe: new-exc = #B~32,'0b~%" new-exceptions)
	  (setf (ldb float-exceptions-byte new-modes) new-exceptions)
	  ;; Clear out all exceptions and save them to the context.
	  ;;
	  ;; XXX: Should we just clear out the bits for the traps that are
	  ;; enabled?  If we did that then the accrued exceptions would be
	  ;; correct.
	  (setf (ldb float-sticky-bits new-modes) 0)
	  ;; Clear out the various sticky invalid operation bits too.
	  ;;
	  ;; XXX: Should we only do that if the invalid trap is enabled?
	  (setf (ldb float-invalid-op-1-byte new-modes) 0)
	  (setf (ldb float-invalid-op-2-byte new-modes) 0)
	  ;; Clear the FP exception summary bit too.
	  (setf (ldb float-exceptions-summary-byte new-modes) 0)
	  ;; (format t "sigfpe: new modes   = #B~32,'0b~%" new-modes)
	  (setf (floating-point-modes) new-modes)
	  #+nil
	  (setf (sigcontext-floating-point-modes
		 (alien:sap-alien scp (* unix:sigcontext)))
		new-modes))

	#+sse2
	(let* ((new-modes modes)
	       (new-exceptions (logandc2 (ldb float-exceptions-byte new-modes)
					 traps)))
	  ;; Clear out the status for any enabled traps.  With SSE2, if
	  ;; the current exception is enabled, the next FP instruction
	  ;; will cause the exception to be signaled again.  Hence, we
	  ;; need to clear out the exceptions that we are handling here.
	  (setf (ldb float-exceptions-byte new-modes) new-exceptions)
	  ;; XXX: This seems not right.  Shouldn't we be setting the modes
	  ;; in the sigcontext instead?  This however seems to do what we
	  ;; want.

	  (when *debug-sigfpe-handler*
	    (format *debug-io* "sigcontext modes: #x~4x (~A)~%"
		    modes (decode-floating-point-modes modes))
	    (format *debug-io* "current modes:    #x~4x (~A)~%"
		    (vm:floating-point-modes) (get-floating-point-modes))
	    (format *debug-io* "new  modes: #x~x (~A)~%"
		    new-modes (decode-floating-point-modes new-modes)))
	  (setf (vm:floating-point-modes) new-modes))))))

(macrolet
    ((with-float-traps (name merge-traps docstring)