* elf/elf.h (R_ARM_TLS_DTPMOD32, R_ARM_TLS_DTPOFF32,
[glibc.git] / hurd / intr-msg.c
blobe75fca81fb66a27c9d95a2963794eaf550431d66
1 /* Replacement for mach_msg used in interruptible Hurd RPCs.
2 Copyright (C) 1995,96,97,98,99,2000,01,02 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <mach.h>
21 #include <mach/mig_errors.h>
22 #include <mach/mig_support.h>
23 #include <hurd/signal.h>
24 #include <assert.h>
26 #include "intr-msg.h"
28 #ifdef NDR_CHAR_ASCII /* OSF Mach flavors have different names. */
29 # define mig_reply_header_t mig_reply_error_t
30 #endif
32 error_t
33 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
34 mach_msg_option_t option,
35 mach_msg_size_t send_size,
36 mach_msg_size_t rcv_size,
37 mach_port_t rcv_name,
38 mach_msg_timeout_t timeout,
39 mach_port_t notify)
41 error_t err;
42 struct hurd_sigstate *ss;
43 const mach_msg_option_t user_option = option;
44 const mach_msg_timeout_t user_timeout = timeout;
46 struct clobber
48 #ifdef NDR_CHAR_ASCII
49 NDR_record_t ndr;
50 #else
51 mach_msg_type_t type;
52 #endif
53 error_t err;
55 union msg
57 mach_msg_header_t header;
58 mig_reply_header_t reply;
59 struct
61 mach_msg_header_t header;
62 #ifdef NDR_CHAR_ASCII
63 NDR_record_t ndr;
64 #else
65 int type;
66 #endif
67 int code;
68 } check;
69 struct
71 mach_msg_header_t header;
72 struct clobber data;
73 } request;
75 union msg *const m = (void *) msg;
76 mach_msg_bits_t msgh_bits;
77 mach_port_t remote_port;
78 mach_msg_id_t msgid;
79 struct clobber save_data;
81 if ((option & (MACH_SEND_MSG|MACH_RCV_MSG)) != (MACH_SEND_MSG|MACH_RCV_MSG)
82 || _hurd_msgport_thread == MACH_PORT_NULL)
84 /* Either this is not an RPC (i.e., only a send or only a receive),
85 so it can't be interruptible; or, the signal thread is not set up
86 yet, so we cannot do the normal signal magic. Do a normal,
87 uninterruptible mach_msg call instead. */
88 return __mach_msg (&m->header, option, send_size, rcv_size, rcv_name,
89 timeout, notify);
92 ss = _hurd_self_sigstate ();
94 /* Save state that gets clobbered by an EINTR reply message.
95 We will need to restore it if we want to retry the RPC. */
96 msgh_bits = m->header.msgh_bits;
97 remote_port = m->header.msgh_remote_port;
98 msgid = m->header.msgh_id;
99 assert (rcv_size >= sizeof m->request);
100 save_data = m->request.data;
102 /* Tell the signal thread that we are doing an interruptible RPC on
103 this port. If we get a signal and should return EINTR, the signal
104 thread will set this variable to MACH_PORT_NULL. The RPC might
105 return EINTR when some other thread gets a signal, in which case we
106 want to restart our call. */
107 ss->intr_port = m->header.msgh_remote_port;
109 /* A signal may arrive here, after intr_port is set, but before the
110 mach_msg system call. The signal handler might do an interruptible
111 RPC, and clobber intr_port; then it would not be set properly when we
112 actually did send the RPC, and a later signal wouldn't interrupt that
113 RPC. So, _hurd_setup_sighandler saves intr_port in the sigcontext,
114 and sigreturn restores it. */
116 message:
118 /* XXX
119 At all points here (once SS->intr_port is set), the signal thread
120 thinks we are "about to enter the syscall", and might mutate our
121 return-value register. This is bogus.
124 if (ss->cancel)
126 /* We have been cancelled. Don't do an RPC at all. */
127 ss->intr_port = MACH_PORT_NULL;
128 ss->cancel = 0;
129 return EINTR;
132 /* Note that the signal trampoline code might modify our OPTION! */
133 err = INTR_MSG_TRAP (msg, option, send_size,
134 rcv_size, rcv_name, timeout, notify);
136 switch (err)
138 case MACH_RCV_TIMED_OUT:
139 if (user_option & MACH_RCV_TIMEOUT)
140 /* The real user RPC timed out. */
141 break;
142 else
143 /* The operation was supposedly interrupted, but still has
144 not returned. Declare it interrupted. */
145 goto interrupted;
147 case MACH_SEND_INTERRUPTED: /* RPC didn't get out. */
148 if (!(option & MACH_SEND_MSG))
150 /* Oh yes, it did! Since we were not doing a message send,
151 this return code cannot have come from the kernel!
152 Instead, it was the signal thread mutating our state to tell
153 us not to enter this RPC. However, we are already in the receive!
154 Since the signal thread thought we weren't in the RPC yet,
155 it didn't do an interrupt_operation.
156 XXX */
157 goto retry_receive;
159 /* FALLTHROUGH */
161 /* These are the other codes that mean a pseudo-receive modified
162 the message buffer and we might need to clean up the port rights. */
163 case MACH_SEND_TIMED_OUT:
164 case MACH_SEND_INVALID_NOTIFY:
165 #ifdef MACH_SEND_NO_NOTIFY
166 case MACH_SEND_NO_NOTIFY:
167 #endif
168 #ifdef MACH_SEND_NOTIFY_IN_PROGRESS
169 case MACH_SEND_NOTIFY_IN_PROGRESS:
170 #endif
171 if (MACH_MSGH_BITS_REMOTE (msg->msgh_bits) == MACH_MSG_TYPE_MOVE_SEND)
173 __mach_port_deallocate (__mach_task_self (), msg->msgh_remote_port);
174 msg->msgh_bits
175 = (MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
176 MACH_MSGH_BITS_LOCAL (msg->msgh_bits))
177 | MACH_MSGH_BITS_OTHER (msg->msgh_bits));
179 if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX)
181 #ifndef MACH_MSG_PORT_DESCRIPTOR
182 /* Check for MOVE_SEND rights in the message. These hold refs
183 that we need to release in case the message is in fact never
184 re-sent later. Since it might in fact be re-sent, we turn
185 these into COPY_SEND's after deallocating the extra user ref;
186 the caller is responsible for still holding a ref to go with
187 the original COPY_SEND right, so the resend copies it again. */
189 mach_msg_type_long_t *ty = (void *) (msg + 1);
190 while ((void *) ty < (void *) msg + msg->msgh_size)
192 mach_msg_type_name_t name;
193 mach_msg_type_size_t size;
194 mach_msg_type_number_t number;
196 inline void clean_ports (mach_port_t *ports, int dealloc)
198 mach_msg_type_number_t i;
199 switch (name)
201 case MACH_MSG_TYPE_MOVE_SEND:
202 for (i = 0; i < number; i++)
203 __mach_port_deallocate (__mach_task_self (), *ports++);
204 (ty->msgtl_header.msgt_longform
205 ? ty->msgtl_name : ty->msgtl_header.msgt_name)
206 = MACH_MSG_TYPE_COPY_SEND;
207 break;
208 case MACH_MSG_TYPE_COPY_SEND:
209 case MACH_MSG_TYPE_MOVE_RECEIVE:
210 break;
211 default:
212 if (MACH_MSG_TYPE_PORT_ANY (name))
213 assert (! "unexpected port type in interruptible RPC");
215 if (dealloc)
216 __vm_deallocate (__mach_task_self (),
217 (vm_address_t) ports,
218 number * sizeof (mach_port_t));
221 if (ty->msgtl_header.msgt_longform)
223 name = ty->msgtl_name;
224 size = ty->msgtl_size;
225 number = ty->msgtl_number;
226 (char *) ty += sizeof (mach_msg_type_long_t);
228 else
230 name = ty->msgtl_header.msgt_name;
231 size = ty->msgtl_header.msgt_size;
232 number = ty->msgtl_header.msgt_number;
233 (char *) ty += sizeof (mach_msg_type_t);
236 if (ty->msgtl_header.msgt_inline)
238 clean_ports ((void *) ty, 0);
239 /* calculate length of data in bytes, rounding up */
240 (char *) ty += (((((number * size) + 7) >> 3)
241 + sizeof (mach_msg_type_t) - 1)
242 &~ (sizeof (mach_msg_type_t) - 1));
244 else
246 clean_ports (*(void **) ty,
247 ty->msgtl_header.msgt_deallocate);
248 ++(void **) ty;
251 #else /* Untyped Mach IPC flavor. */
252 mach_msg_body_t *body = (void *) (msg + 1);
253 mach_msg_descriptor_t *desc = (void *) (body + 1);
254 mach_msg_descriptor_t *desc_end = desc + body->msgh_descriptor_count;
255 for (; desc < desc_end; ++desc)
256 switch (desc->type.type)
258 case MACH_MSG_PORT_DESCRIPTOR:
259 switch (desc->port.disposition)
261 case MACH_MSG_TYPE_MOVE_SEND:
262 __mach_port_deallocate (mach_task_self (),
263 desc->port.name);
264 desc->port.disposition = MACH_MSG_TYPE_COPY_SEND;
265 break;
266 case MACH_MSG_TYPE_COPY_SEND:
267 case MACH_MSG_TYPE_MOVE_RECEIVE:
268 break;
269 default:
270 assert (! "unexpected port type in interruptible RPC");
272 break;
273 case MACH_MSG_OOL_DESCRIPTOR:
274 if (desc->out_of_line.deallocate)
275 __vm_deallocate (__mach_task_self (),
276 (vm_address_t) desc->out_of_line.address,
277 desc->out_of_line.size);
278 break;
279 case MACH_MSG_OOL_PORTS_DESCRIPTOR:
280 switch (desc->ool_ports.disposition)
282 case MACH_MSG_TYPE_MOVE_SEND:
284 mach_msg_size_t i;
285 const mach_port_t *ports = desc->ool_ports.address;
286 for (i = 0; i < desc->ool_ports.count; ++i)
287 __mach_port_deallocate (__mach_task_self (), ports[i]);
288 desc->ool_ports.disposition = MACH_MSG_TYPE_COPY_SEND;
289 break;
291 case MACH_MSG_TYPE_COPY_SEND:
292 case MACH_MSG_TYPE_MOVE_RECEIVE:
293 break;
294 default:
295 assert (! "unexpected port type in interruptible RPC");
297 if (desc->ool_ports.deallocate)
298 __vm_deallocate (__mach_task_self (),
299 (vm_address_t) desc->ool_ports.address,
300 desc->ool_ports.count
301 * sizeof (mach_port_t));
302 break;
303 default:
304 assert (! "unexpected descriptor type in interruptible RPC");
306 #endif
308 break;
310 case EINTR:
311 /* Either the process was stopped and continued,
312 or the server doesn't support interrupt_operation. */
313 if (ss->intr_port != MACH_PORT_NULL)
314 /* If this signal was for us and it should interrupt calls, the
315 signal thread will have cleared SS->intr_port.
316 Since it's not cleared, the signal was for another thread,
317 or SA_RESTART is set. Restart the interrupted call. */
319 /* Make sure we have a valid reply port. The one we were using
320 may have been destroyed by interruption. */
321 m->header.msgh_local_port = rcv_name = __mig_get_reply_port ();
322 m->header.msgh_bits = msgh_bits;
323 option = user_option;
324 timeout = user_timeout;
325 goto message;
327 /* FALLTHROUGH */
329 case MACH_RCV_PORT_DIED:
330 /* Server didn't respond to interrupt_operation,
331 so the signal thread destroyed the reply port. */
332 /* FALLTHROUGH */
334 interrupted:
335 err = EINTR;
337 /* The EINTR return indicates cancellation, so clear the flag. */
338 ss->cancel = 0;
339 break;
341 case MACH_RCV_INTERRUPTED: /* RPC sent; no reply. */
342 option &= ~MACH_SEND_MSG; /* Don't send again. */
343 retry_receive:
344 if (ss->intr_port == MACH_PORT_NULL)
346 /* This signal or cancellation was for us. We need to wait for
347 the reply, but not hang forever. */
348 option |= MACH_RCV_TIMEOUT;
349 /* Never decrease the user's timeout. */
350 if (!(user_option & MACH_RCV_TIMEOUT)
351 || timeout > _hurd_interrupted_rpc_timeout)
352 timeout = _hurd_interrupted_rpc_timeout;
354 else
356 option = user_option;
357 timeout = user_timeout;
359 goto message; /* Retry the receive. */
361 case MACH_MSG_SUCCESS:
363 /* We got a reply. Was it EINTR? */
364 #ifdef MACH_MSG_TYPE_BIT
365 const union
367 mach_msg_type_t t;
368 int i;
369 } check =
370 { t: { MACH_MSG_TYPE_INTEGER_T, sizeof (integer_t) * 8,
371 1, TRUE, FALSE, FALSE, 0 } };
372 #endif
374 if (m->reply.RetCode == EINTR &&
375 m->header.msgh_size == sizeof m->reply &&
376 #ifdef MACH_MSG_TYPE_BIT
377 m->check.type == check.i &&
378 #endif
379 !(m->header.msgh_bits & MACH_MSGH_BITS_COMPLEX))
381 /* It is indeed EINTR. Is the interrupt for us? */
382 if (ss->intr_port != MACH_PORT_NULL)
384 /* Nope; repeat the RPC.
385 XXX Resources moved? */
387 assert (m->header.msgh_id == msgid + 100);
389 /* We know we have a valid reply port, because we just
390 received the EINTR reply on it. Restore it and the
391 other fields in the message header needed for send,
392 since the header now reflects receipt of the reply. */
393 m->header.msgh_local_port = rcv_name;
394 m->header.msgh_remote_port = remote_port;
395 m->header.msgh_id = msgid;
396 m->header.msgh_bits = msgh_bits;
397 /* Restore the two words clobbered by the reply data. */
398 m->request.data = save_data;
400 /* Restore the original mach_msg options.
401 OPTION may have had MACH_RCV_TIMEOUT added,
402 and/or MACH_SEND_MSG removed. */
403 option = user_option;
404 timeout = user_timeout;
406 /* Now we are ready to repeat the original message send. */
407 goto message;
409 else
410 /* The EINTR return indicates cancellation,
411 so clear the flag. */
412 ss->cancel = 0;
415 break;
417 default: /* Quiet -Wswitch-enum. */
418 break;
421 ss->intr_port = MACH_PORT_NULL;
423 return err;