Update to LGPL v2.1.
[jlayton/glibc.git] / hurd / catch-exc.c
1 /* Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <mach/exc_server.h>
20 #include <hurd/signal.h>
21
22 /* Called by the microkernel when a thread gets an exception.  */
23
24 kern_return_t
25 _S_catch_exception_raise (mach_port_t port,
26                           thread_t thread,
27                           task_t task,
28                           int exception,
29                           int code,
30                           int subcode)
31 {
32   struct hurd_sigstate *ss;
33   int signo;
34   struct hurd_signal_detail d;
35
36   if (task != __mach_task_self ())
37     /* The sender wasn't the kernel.  */
38     return EPERM;
39
40   d.exc = exception;
41   d.exc_code = code;
42   d.exc_subcode = subcode;
43
44   /* Call the machine-dependent function to translate the Mach exception
45      codes into a signal number and subcode.  */
46   _hurd_exception2signal (&d, &signo);
47
48   /* Find the sigstate structure for the faulting thread.  */
49   __mutex_lock (&_hurd_siglock);
50   for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
51     if (ss->thread == thread)
52       break;
53   __mutex_unlock (&_hurd_siglock);
54   if (ss == NULL)
55     ss = _hurd_thread_sigstate (thread); /* Allocate a fresh one.  */
56
57   if (__spin_lock_locked (&ss->lock))
58     {
59       /* Loser.  The thread faulted with its sigstate lock held.  Its
60          sigstate data is now suspect.  So we reset the parts of it which
61          could cause trouble for the signal thread.  Anything else
62          clobbered therein will just hose this user thread, but it's
63          faulting already.
64
65          This is almost certainly a library bug: unless random memory
66          clobberation caused the sigstate lock to gratuitously appear held,
67          no code should do anything that can fault while holding the
68          sigstate lock.  */
69
70       __spin_unlock (&ss->critical_section_lock);
71       ss->context = NULL;
72       __spin_unlock (&ss->lock);
73     }
74
75   /* Post the signal.  */
76   _hurd_internal_post_signal (ss, signo, &d,
77                               MACH_PORT_NULL, MACH_MSG_TYPE_PORT_SEND,
78                               0);
79
80   return KERN_SUCCESS;
81 }