1999-09-14 09:32:05 -04:00
|
|
|
;;; Copyright (c) 1993 by Olin Shivers.
|
|
|
|
;;; Signal handler system
|
|
|
|
|
|
|
|
;;; The principal trickiness here is that we have to interface to Unix signals
|
|
|
|
;;; *through* an intermediate interface, the S48 vm's idea of interrupts.
|
|
|
|
;;; So there is a difference between delivering a signal to the underlying
|
|
|
|
;;; Unix process and delivering it to the program that runs on the VM.
|
|
|
|
;;;
|
|
|
|
;;; One effect is that we have two separate codes for the same thing -- the
|
|
|
|
;;; Unix signal code, and the S48 interrupt value. E.g., SIGNAL/TSTP and
|
|
|
|
;;; INTERRUPT/TSTP.
|
|
|
|
|
|
|
|
;;; These system calls can return EINTR or restart. In order for the S48 vm's
|
|
|
|
;;; interrupt system to detect a signal and invoke the handler, they *must*
|
|
|
|
;;; return EINTR, and this must cause a return from C to Scheme.
|
|
|
|
;;;
|
|
|
|
;;; open close dup2 accept connect
|
|
|
|
;;; read recv recvfrom recvmsg
|
|
|
|
;;; write send sendto sendmsg
|
|
|
|
;;; select
|
|
|
|
;;; wait
|
|
|
|
;;; fcntl* ioctl
|
|
|
|
;;; sigsuspend
|
|
|
|
;;; HP-UX, but I don't use: poll lockf msem_lock msgsnd msgrcv semop
|
|
|
|
;;;
|
|
|
|
;;; * Only during a F_SETLKW
|
|
|
|
;;;
|
|
|
|
;;; From rts/interrupt.scm (package interrupts, interface interrupts-interface)
|
|
|
|
;;; WITH-INTERRUPTS INTERRUPT-HANDLERS SET-ENABLED-INTERRUPTS !
|
|
|
|
;;; ENABLED-INTERRUPTS
|
|
|
|
;;; Must define WITH-INTERRUPTS* and WITH-INTERRUPTS.
|
|
|
|
|
|
|
|
;;; Map a Unix async signal to its S48 interrupt value.
|
|
|
|
;;; -1 => Not defined.
|
2001-09-13 03:59:40 -04:00
|
|
|
(import-lambda-definition %signal->interrupt (sig) "sig2interrupt")
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
(define (signal->interrupt sig)
|
|
|
|
(let ((int (%signal->interrupt sig)))
|
|
|
|
(if (>= int 0) int
|
|
|
|
(error "Unix signal has no Scheme 48 interrupt." sig))))
|
|
|
|
|
|
|
|
|
1999-10-15 14:57:29 -04:00
|
|
|
(define (interrupt-enabled? int mask)
|
2001-01-09 10:52:52 -05:00
|
|
|
(interrupt-in-set? int mask))
|
1999-10-15 14:57:29 -04:00
|
|
|
|
|
|
|
(define (interrupt-enable int mask)
|
2001-01-09 10:52:52 -05:00
|
|
|
(insert-interrupt int mask))
|
1999-10-15 14:57:29 -04:00
|
|
|
|
|
|
|
(define *enabled-interrupts*
|
|
|
|
(let lp ((i 0) (mask 0))
|
1999-11-04 15:28:06 -05:00
|
|
|
(if (= i number-of-interrupts)
|
1999-10-15 14:57:29 -04:00
|
|
|
mask
|
|
|
|
(lp (+ i 1) (interrupt-enable i mask)))))
|
|
|
|
|
|
|
|
(define (enabled-interrupts) *enabled-interrupts*)
|
|
|
|
|
2001-01-09 10:52:52 -05:00
|
|
|
(define *pending-interrupts* 0)
|
|
|
|
|
|
|
|
(define (interrupt-pending? int)
|
|
|
|
(interrupt-in-set? int *pending-interrupts*))
|
|
|
|
|
|
|
|
(define (make-interrupt-pending int)
|
2002-08-05 07:30:33 -04:00
|
|
|
(set! *pending-interrupts* (insert-interrupt int *pending-interrupts*)))
|
2001-01-09 10:52:52 -05:00
|
|
|
|
|
|
|
(define (remove-pending-interrupt int)
|
2002-08-05 07:30:33 -04:00
|
|
|
(set! *pending-interrupts* (remove-interrupt int *pending-interrupts*)))
|
1999-10-15 14:57:29 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; I'm trying to be consistent about the ! suffix -- I don't use it
|
|
|
|
;;; when frobbing process state. This is not a great rule; perhaps I
|
|
|
|
;;; should change it.
|
1999-10-15 14:57:29 -04:00
|
|
|
;;;
|
|
|
|
;;; I think you should...
|
|
|
|
(define (set-enabled-interrupts new-enabled-interrupts)
|
2002-08-05 07:30:33 -04:00
|
|
|
(let ((old-enabled-interrupts *enabled-interrupts*))
|
|
|
|
;;; set it here so the handlers see the correct value
|
|
|
|
(set! *enabled-interrupts* new-enabled-interrupts)
|
|
|
|
(do ((int 0 (+ int 1)))
|
|
|
|
((= int number-of-interrupts) new-enabled-interrupts)
|
|
|
|
(let ((old-state (interrupt-enabled? int old-enabled-interrupts))
|
|
|
|
(new-state (interrupt-enabled? int new-enabled-interrupts)))
|
|
|
|
(if (and (not old-state) new-state (interrupt-pending? int))
|
|
|
|
(begin
|
|
|
|
(remove-pending-interrupt int)
|
|
|
|
(call-interrupt-handler int)))))))
|
1999-10-15 14:57:29 -04:00
|
|
|
|
1999-11-04 15:28:06 -05:00
|
|
|
(define-simple-syntax (with-enabled-interrupts interrupt-set body ...)
|
1999-10-08 14:41:05 -04:00
|
|
|
(begin
|
1999-11-04 15:28:06 -05:00
|
|
|
(with-enabled-interrupts* interrupt-set (lambda () body ...))))
|
1999-09-24 19:54:22 -04:00
|
|
|
|
1999-11-04 15:28:06 -05:00
|
|
|
(define (with-enabled-interrupts* interrupt-set thunk)
|
|
|
|
(let ((before *enabled-interrupts*))
|
|
|
|
(set-enabled-interrupts interrupt-set)
|
|
|
|
(let ((return (thunk)))
|
|
|
|
(set-enabled-interrupts before)
|
|
|
|
return)))
|
1999-09-24 19:54:22 -04:00
|
|
|
|
2002-09-06 07:25:59 -04:00
|
|
|
(define *interrupt-handlers-vector*)
|
|
|
|
|
|
|
|
(define (install-fresh-interrupt-handlers-vector!)
|
|
|
|
(set! *interrupt-handlers-vector* (make-vector number-of-interrupts #t)))
|
1999-10-15 14:57:29 -04:00
|
|
|
|
1999-09-22 13:00:08 -04:00
|
|
|
(define (interrupt-handlers-vector)
|
1999-10-15 14:57:29 -04:00
|
|
|
*interrupt-handlers-vector*)
|
|
|
|
|
|
|
|
(define (interrupt-handler-ref int)
|
1999-11-04 15:28:06 -05:00
|
|
|
(if (or (< int 0) (>= int number-of-interrupts))
|
1999-10-15 14:57:29 -04:00
|
|
|
(error "ill signum in interrupt-handler-ref" int)
|
2001-01-09 10:52:52 -05:00
|
|
|
(vector-ref *interrupt-handlers-vector* int)))
|
1999-10-15 14:57:29 -04:00
|
|
|
|
2001-01-09 10:52:52 -05:00
|
|
|
(define (call-interrupt-handler int)
|
|
|
|
(let ((handler (interrupt-handler-ref int)))
|
|
|
|
(case handler
|
|
|
|
((#t) ((vector-ref default-int-handler-vec int) (enabled-interrupts)))
|
|
|
|
((#f) (if #f #f))
|
|
|
|
(else (handler (enabled-interrupts))))))
|
|
|
|
|
1999-10-15 14:57:29 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; Get/Set signal handlers
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;;; When you set a signal's handler to "default," if the default for that
|
|
|
|
;;; signal is something other than "ignore," we actually install this guy.
|
|
|
|
;;; When he is called by the S48 interrupt system, he'll magically make
|
|
|
|
;;; the default action happen (by calling C code that *really* sets the
|
|
|
|
;;; handler to SIGDFL, and then re-sending the signal). This basically
|
|
|
|
;;; terminates the process, since if the default isn't "ignore," it's always
|
|
|
|
;;; "terminate" of some kind. Doing it this way means the exit code given
|
|
|
|
;;; to our waiting parent proc correctly reflects how we died, and also
|
|
|
|
;;; makes the core dump happen if it should. Details, details.
|
|
|
|
|
2001-09-13 03:59:40 -04:00
|
|
|
(import-lambda-definition %do-default-sigaction (signal) "do_default_sigaction")
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
(define default-int-handler-vec
|
|
|
|
;; Non-Unix-signal interrupts just get their default values from
|
|
|
|
;; the current value of I-H.
|
1999-09-22 13:00:08 -04:00
|
|
|
(let ((v (make-vector 32)))
|
1999-09-14 09:32:05 -04:00
|
|
|
(do ((sig 31 (- sig 1))) ; For each Unix signal
|
|
|
|
((< sig 0)) ; make & install a default
|
|
|
|
(let ((i (%signal->interrupt sig))) ; signal handler.
|
|
|
|
(if (>= i 0) ; Don't mess with non-signal interrupts.
|
|
|
|
(vector-set! v i (if (memv sig signals-ignored-by-default)
|
|
|
|
(lambda (enabled-interrupts) #f)
|
|
|
|
(lambda (enabled-interrupts)
|
|
|
|
(%do-default-sigaction sig)))))))
|
|
|
|
v))
|
|
|
|
|
|
|
|
|
|
|
|
;;; HANDLER is #f (ignore), #t (default), or a procedure taking an integer
|
|
|
|
;;; argument. The interrupt is delivered to a procedure by (1) setting the
|
|
|
|
;;; ENABLED-INTERRUPTS register to 0 (i.e., blocking all interrupts), and (2)
|
|
|
|
;;; applying the procedure to the previous value of the ENABLED-INTERRUPTS
|
|
|
|
;;; register. If the procedure returns normally, the ENABLED-INTERRUPTS
|
|
|
|
;;; register will be restored to its previous value.
|
|
|
|
|
|
|
|
(define (set-interrupt-handler int handler)
|
2001-01-09 10:52:52 -05:00
|
|
|
(if (or (< int 0) (>= int number-of-interrupts))
|
|
|
|
(error "ill signum in set-interrupt-handler!" int)
|
|
|
|
(let ((old-handler (vector-ref *interrupt-handlers-vector* int)))
|
|
|
|
(vector-set! *interrupt-handlers-vector* int handler)
|
|
|
|
old-handler)))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
(define (interrupt-handler int)
|
2001-01-09 10:52:52 -05:00
|
|
|
(interrupt-handler-ref int))
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2001-04-09 04:05:58 -04:00
|
|
|
(define (with-scsh-sighandlers interactive? thunk)
|
2002-09-06 07:25:59 -04:00
|
|
|
(install-fresh-interrupt-handlers-vector!)
|
1999-09-14 09:32:05 -04:00
|
|
|
(do ((sig 32 (- sig 1)))
|
|
|
|
((< sig 0))
|
|
|
|
(let ((i (%signal->interrupt sig)))
|
|
|
|
(if (not (or (= i -1)
|
2001-01-15 08:43:35 -05:00
|
|
|
(= sig signal/alrm))) ; Leave alarm handler alone.
|
1999-10-15 14:57:29 -04:00
|
|
|
(set-interrupt-handler
|
1999-09-22 13:00:08 -04:00
|
|
|
i
|
2001-01-09 10:52:52 -05:00
|
|
|
#t))))
|
2002-09-03 09:23:54 -04:00
|
|
|
(let ((scsh-initial-thread ((structure-ref threads-internal current-thread))))
|
2002-05-03 09:42:36 -04:00
|
|
|
(if (not (eq? (thread-name scsh-initial-thread)
|
|
|
|
'scsh-initial-thread))
|
|
|
|
(error "sighandler did not find scsh-initial-thread, but"
|
|
|
|
scsh-initial-thread))
|
2000-09-17 11:10:42 -04:00
|
|
|
|
|
|
|
;; Note: this will prevent any other system to work, since it pushes
|
|
|
|
;; a new command level !
|
|
|
|
(if interactive?
|
|
|
|
(set-interrupt-handler interrupt/keyboard
|
|
|
|
(lambda stuff
|
|
|
|
((structure-ref threads-internal schedule-event)
|
2002-05-03 09:42:36 -04:00
|
|
|
scsh-initial-thread
|
2000-09-17 11:10:42 -04:00
|
|
|
(enum
|
|
|
|
(structure-ref threads-internal event-type)
|
|
|
|
interrupt)
|
2001-01-09 10:52:52 -05:00
|
|
|
(enum interrupt keyboard))))))
|
2002-06-26 06:02:10 -04:00
|
|
|
(run-as-long-as
|
|
|
|
deliver-interrupts
|
|
|
|
thunk
|
|
|
|
(structure-ref threads-internal spawn-on-root)
|
|
|
|
'deliver-interrupts))
|
2001-01-09 10:52:52 -05:00
|
|
|
|
|
|
|
(define (deliver-interrupts)
|
2001-10-18 05:02:52 -04:00
|
|
|
(let lp ((last ((structure-ref sigevents most-recent-sigevent))))
|
2002-07-18 04:04:34 -04:00
|
|
|
(let* ((event ((structure-ref sigevents next-sigevent-set)
|
|
|
|
last full-interrupt-set))
|
|
|
|
(interrupt ((structure-ref sigevents sigevent-type) event)))
|
|
|
|
(if (interrupt-enabled? interrupt (enabled-interrupts))
|
2002-08-05 07:30:33 -04:00
|
|
|
(call-interrupt-handler interrupt)
|
|
|
|
(make-interrupt-pending interrupt))
|
2001-01-09 10:52:52 -05:00
|
|
|
(lp event))))
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
;;; I am ashamed to say the 33 below is completely bogus.
|
|
|
|
;;; What we want is a value that is 1 + max interrupt value.
|
|
|
|
|
|
|
|
(define int->sig-vec
|
|
|
|
(let ((v (make-vector 33 #f)))
|
|
|
|
(do ((sig 32 (- sig 1)))
|
|
|
|
((< sig 0))
|
|
|
|
(let ((i (%signal->interrupt sig)))
|
|
|
|
(if (not (= i -1)) (vector-set! v i sig))))
|
|
|
|
v))
|
|
|
|
|
|
|
|
(define (int->signal i) (and (<= 0 i 32) (vector-ref int->sig-vec i)))
|