/* IPC status code macros. */
#define IPC_STATUS_REG bx
#define IPC_STATUS_GET(p) ((p)->p_reg.IPC_STATUS_REG)
-#define IPC_STATUS_SET(p, m) ((p)->p_reg.IPC_STATUS_REG = m)
-#define IPC_STATUS_CLEAR(p) IPC_STATUS_SET(p, 0)
-#define IPC_STATUS_ADD(p, m) ((p)->p_reg.IPC_STATUS_REG |= m)
+#define IPC_STATUS_CLEAR(p) ((p)->p_reg.IPC_STATUS_REG = 0)
+
+/*
+ * XXX: the following check is used to set the status code only on RECEIVE.
+ * SENDREC is not currently atomic for user processes. A process can return
+ * from SENDREC in a different context than the original when a Posix signal
+ * handler gets executed. For this reason, it is not safe to manipulate
+ * the context (i.e. registers) when a process is blocked on a SENDREC.
+ * Unfortunately, avoiding setting the status code for SENDREC doesn't solve
+ * the problem entirely because in rare situations it is still necessary to
+ * override retreg dynamically (and possibly in a different context).
+ * A possible reliable solution is to improve our Posix signal handling
+ * implementation and guarantee SENDREC atomicity w.r.t. the process context.
+ */
+#define IPC_STATUS_ADD(p, m) do { \
+ if(!((p)->p_misc_flags & MF_REPLY_PEND)) { \
+ (p)->p_reg.IPC_STATUS_REG |= (m); \
+ } \
+ } while(0)
+#define IPC_STATUS_ADD_CALL(p, call) \
+ IPC_STATUS_ADD(p, IPC_STATUS_CALL_TO(call))
+#define IPC_STATUS_ADD_FLAGS(p, flags) \
+ IPC_STATUS_ADD(p, IPC_STATUS_FLAGS(flags))
#endif /* IPC_H */
break; /* done, or SEND failed */
/* fall through for SENDREC */
case RECEIVE:
- if (call_nr == RECEIVE)
+ if (call_nr == RECEIVE) {
caller_ptr->p_misc_flags &= ~MF_REPLY_PEND;
+ IPC_STATUS_CLEAR(caller_ptr); /* clear IPC status code */
+ }
result = mini_receive(caller_ptr, src_dst_e, m_ptr, 0);
break;
case NOTIFY:
caller_ptr->p_name, caller_ptr->p_endpoint);
}
- /* Clear IPC status code. */
- IPC_STATUS_CLEAR(caller_ptr);
-
/* Now check if the call is known and try to perform the request. The only
* system calls that exist in MINIX are sending and receiving messages.
* - SENDREC: combines SEND and RECEIVE in a single system call
return EFAULT;
} else {
dst_ptr->p_delivermsg = *m_ptr;
- IPC_STATUS_ADD(dst_ptr,
- IPC_STATUS_FLAGS(IPC_FLG_MSG_FROM_KERNEL));
+ IPC_STATUS_ADD_FLAGS(dst_ptr, IPC_FLG_MSG_FROM_KERNEL);
}
dst_ptr->p_delivermsg.m_source = caller_ptr->p_endpoint;
call = (caller_ptr->p_misc_flags & MF_REPLY_PEND ? SENDREC
: (flags & NON_BLOCKING ? SENDNB : SEND));
- IPC_STATUS_ADD(dst_ptr, IPC_STATUS_CALL_TO(call));
+ IPC_STATUS_ADD_CALL(dst_ptr, call);
RTS_UNSET(dst_ptr, RTS_RECEIVING);
} else {
if(flags & NON_BLOCKING) {
caller_ptr->p_delivermsg.m_source = hisep;
caller_ptr->p_misc_flags |= MF_DELIVERMSG;
- IPC_STATUS_ADD(caller_ptr, IPC_STATUS_CALL_TO(NOTIFY));
+ IPC_STATUS_ADD_CALL(caller_ptr, NOTIFY);
return(OK);
}
r= try_async(caller_ptr);
if (r == OK) {
- IPC_STATUS_ADD(caller_ptr, IPC_STATUS_CALL_TO(SENDA));
+ IPC_STATUS_ADD_CALL(caller_ptr, SENDA);
return OK; /* Got a message */
}
}
RTS_UNSET(*xpp, RTS_SENDING);
call = ((*xpp)->p_misc_flags & MF_REPLY_PEND ? SENDREC : SEND);
- IPC_STATUS_ADD(caller_ptr, IPC_STATUS_CALL_TO(call));
+ IPC_STATUS_ADD_CALL(caller_ptr, call);
/*
* if the message is originaly from the kernel on behalf of this
* process, we must send the status flags accordingly
*/
if ((*xpp)->p_misc_flags & MF_SENDING_FROM_KERNEL) {
- IPC_STATUS_ADD(caller_ptr,
- IPC_STATUS_FLAGS(IPC_FLG_MSG_FROM_KERNEL));
+ IPC_STATUS_ADD_FLAGS(caller_ptr, IPC_FLG_MSG_FROM_KERNEL);
/* we can clean the flag now, not need anymore */
(*xpp)->p_misc_flags &= ~MF_SENDING_FROM_KERNEL;
}
dst_ptr->p_delivermsg.m_source = caller_ptr->p_endpoint;
dst_ptr->p_misc_flags |= MF_DELIVERMSG;
- IPC_STATUS_ADD(dst_ptr, IPC_STATUS_CALL_TO(NOTIFY));
+ IPC_STATUS_ADD_CALL(dst_ptr, NOTIFY);
RTS_UNSET(dst_ptr, RTS_RECEIVING);
return(OK);
else {
dst_ptr->p_delivermsg.m_source = caller_ptr->p_endpoint;
dst_ptr->p_misc_flags |= MF_DELIVERMSG;
- IPC_STATUS_ADD(dst_ptr,
- IPC_STATUS_CALL_TO(SENDA));
+ IPC_STATUS_ADD_CALL(dst_ptr, SENDA);
RTS_UNSET(dst_ptr, RTS_RECEIVING);
tabent.result = OK;
}