* hurd/hurdselect.c (_hurd_select) [MACH_MSG_TRAILER_MINIMUM_SIZE]:
[platform/upstream/glibc.git] / hurd / catch-exc.c
1 /* Copyright (C) 1994,95,96,97,2002 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 #ifdef EXC_MASK_ALL             /* New interface flavor.  */
29                           exception_type_t exception,
30                           exception_data_t code,
31                           mach_msg_type_number_t codeCnt
32 #else                           /* Vanilla Mach 3.0 interface.  */
33                           int exception, int code, int subcode
34 #endif
35                           )
36 {
37   struct hurd_sigstate *ss;
38   int signo;
39   struct hurd_signal_detail d;
40
41   if (task != __mach_task_self ())
42     /* The sender wasn't the kernel.  */
43     return EPERM;
44
45   d.exc = exception;
46 #ifdef EXC_MASK_ALL
47   assert (codeCnt >= 2);
48   d.exc_code = code[0];
49   d.exc_subcode = code[1];
50 #else
51   d.exc_code = code;
52   d.exc_subcode = subcode;
53 #endif
54
55   /* Call the machine-dependent function to translate the Mach exception
56      codes into a signal number and subcode.  */
57   _hurd_exception2signal (&d, &signo);
58
59   /* Find the sigstate structure for the faulting thread.  */
60   __mutex_lock (&_hurd_siglock);
61   for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
62     if (ss->thread == thread)
63       break;
64   __mutex_unlock (&_hurd_siglock);
65   if (ss == NULL)
66     ss = _hurd_thread_sigstate (thread); /* Allocate a fresh one.  */
67
68   if (__spin_lock_locked (&ss->lock))
69     {
70       /* Loser.  The thread faulted with its sigstate lock held.  Its
71          sigstate data is now suspect.  So we reset the parts of it which
72          could cause trouble for the signal thread.  Anything else
73          clobbered therein will just hose this user thread, but it's
74          faulting already.
75
76          This is almost certainly a library bug: unless random memory
77          clobberation caused the sigstate lock to gratuitously appear held,
78          no code should do anything that can fault while holding the
79          sigstate lock.  */
80
81       __spin_unlock (&ss->critical_section_lock);
82       ss->context = NULL;
83       __spin_unlock (&ss->lock);
84     }
85
86   /* Post the signal.  */
87   _hurd_internal_post_signal (ss, signo, &d,
88                               MACH_PORT_NULL, MACH_MSG_TYPE_PORT_SEND,
89                               0);
90
91   return KERN_SUCCESS;
92 }
93
94 #ifdef EXC_MASK_ALL
95 /* XXX New interface flavor has additional RPCs that we could be using
96    instead.  These RPCs roll a thread_get_state/thread_set_state into
97    the message, so the signal thread ought to use these to save some calls.
98  */
99 kern_return_t
100 _S_catch_exception_raise_state (mach_port_t port,
101                                 exception_type_t exception,
102                                 exception_data_t code,
103                                 mach_msg_type_number_t codeCnt,
104                                 int *flavor,
105                                 thread_state_t old_state,
106                                 mach_msg_type_number_t old_stateCnt,
107                                 thread_state_t new_state,
108                                 mach_msg_type_number_t *new_stateCnt)
109 {
110   abort ();
111   return KERN_FAILURE;
112 }
113
114 kern_return_t
115 _S_catch_exception_raise_state_identity (mach_port_t exception_port,
116                                          thread_t thread,
117                                          task_t task,
118                                          exception_type_t exception,
119                                          exception_data_t code,
120                                          mach_msg_type_number_t codeCnt,
121                                          int *flavor,
122                                          thread_state_t old_state,
123                                          mach_msg_type_number_t old_stateCnt,
124                                          thread_state_t new_state,
125                                          mach_msg_type_number_t *new_stateCnt)
126 {
127   abort ();
128   return KERN_FAILURE;
129 }
130 #endif