xref: /titanic_51/usr/src/uts/common/os/msg.c (revision e5994f965f428546c8808ba1aec167c12f0612be)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5b2eb1770Sudpa  * Common Development and Distribution License (the "License").
6b2eb1770Sudpa  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*e5994f96Sdv142724  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Inter-Process Communication Message Facility.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * See os/ipc.c for a description of common IPC functionality.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Resource controls
387c478bd9Sstevel@tonic-gate  * -----------------
397c478bd9Sstevel@tonic-gate  *
40824c205fSml93401  * Control:      zone.max-msg-ids (rc_zone_msgmni)
41824c205fSml93401  * Description:  Maximum number of message queue ids allowed a zone.
42824c205fSml93401  *
43824c205fSml93401  *   When msgget() is used to allocate a message queue, one id is
44824c205fSml93401  *   allocated.  If the id allocation doesn't succeed, msgget() fails
45824c205fSml93401  *   and errno is set to ENOSPC.  Upon successful msgctl(, IPC_RMID)
46824c205fSml93401  *   the id is deallocated.
47824c205fSml93401  *
487c478bd9Sstevel@tonic-gate  * Control:      project.max-msg-ids (rc_project_msgmni)
497c478bd9Sstevel@tonic-gate  * Description:  Maximum number of message queue ids allowed a project.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *   When msgget() is used to allocate a message queue, one id is
527c478bd9Sstevel@tonic-gate  *   allocated.  If the id allocation doesn't succeed, msgget() fails
537c478bd9Sstevel@tonic-gate  *   and errno is set to ENOSPC.  Upon successful msgctl(, IPC_RMID)
547c478bd9Sstevel@tonic-gate  *   the id is deallocated.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Control:      process.max-msg-qbytes (rc_process_msgmnb)
577c478bd9Sstevel@tonic-gate  * Description:  Maximum number of bytes of messages on a message queue.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *   When msgget() successfully allocates a message queue, the minimum
607c478bd9Sstevel@tonic-gate  *   enforced value of this limit is used to initialize msg_qbytes.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * Control:      process.max-msg-messages (rc_process_msgtql)
637c478bd9Sstevel@tonic-gate  * Description:  Maximum number of messages on a message queue.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *   When msgget() successfully allocates a message queue, the minimum
667c478bd9Sstevel@tonic-gate  *   enforced value of this limit is used to initialize a per-queue
677c478bd9Sstevel@tonic-gate  *   limit on the number of messages.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/types.h>
717c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
727c478bd9Sstevel@tonic-gate #include <sys/param.h>
737c478bd9Sstevel@tonic-gate #include <sys/cred.h>
747c478bd9Sstevel@tonic-gate #include <sys/user.h>
757c478bd9Sstevel@tonic-gate #include <sys/proc.h>
767c478bd9Sstevel@tonic-gate #include <sys/time.h>
777c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
787c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
797c478bd9Sstevel@tonic-gate #include <sys/msg.h>
807c478bd9Sstevel@tonic-gate #include <sys/msg_impl.h>
817c478bd9Sstevel@tonic-gate #include <sys/list.h>
827c478bd9Sstevel@tonic-gate #include <sys/systm.h>
837c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
847c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
857c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
867c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
877c478bd9Sstevel@tonic-gate #include <sys/errno.h>
887c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
897c478bd9Sstevel@tonic-gate #include <sys/debug.h>
907c478bd9Sstevel@tonic-gate #include <sys/project.h>
917c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
927c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
937c478bd9Sstevel@tonic-gate #include <sys/policy.h>
947c478bd9Sstevel@tonic-gate #include <sys/zone.h>
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #include <c2/audit.h>
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * The following tunables are obsolete.  Though for compatibility we
1007c478bd9Sstevel@tonic-gate  * still read and interpret msginfo_msgmnb, msginfo_msgmni, and
1017c478bd9Sstevel@tonic-gate  * msginfo_msgtql (see os/project.c and os/rctl_proc.c), the preferred
1027c478bd9Sstevel@tonic-gate  * mechanism for administrating the IPC Message facility is through the
1037c478bd9Sstevel@tonic-gate  * resource controls described at the top of this file.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate size_t	msginfo_msgmax = 2048;	/* (obsolete) */
1067c478bd9Sstevel@tonic-gate size_t	msginfo_msgmnb = 4096;	/* (obsolete) */
1077c478bd9Sstevel@tonic-gate int	msginfo_msgmni = 50;	/* (obsolete) */
1087c478bd9Sstevel@tonic-gate int	msginfo_msgtql = 40;	/* (obsolete) */
1097c478bd9Sstevel@tonic-gate int	msginfo_msgssz = 8;	/* (obsolete) */
1107c478bd9Sstevel@tonic-gate int	msginfo_msgmap = 0;	/* (obsolete) */
1117c478bd9Sstevel@tonic-gate ushort_t msginfo_msgseg = 1024;	/* (obsolete) */
1127c478bd9Sstevel@tonic-gate 
113824c205fSml93401 extern rctl_hndl_t rc_zone_msgmni;
1147c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_msgmni;
1157c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_process_msgmnb;
1167c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_process_msgtql;
1177c478bd9Sstevel@tonic-gate static ipc_service_t *msq_svc;
1187c478bd9Sstevel@tonic-gate static zone_key_t msg_zone_key;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static void msg_dtor(kipc_perm_t *);
1217c478bd9Sstevel@tonic-gate static void msg_rmid(kipc_perm_t *);
1227c478bd9Sstevel@tonic-gate static void msg_remove_zone(zoneid_t, void *);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static ssize_t msgsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2,
1287c478bd9Sstevel@tonic-gate 	uintptr_t a4, uintptr_t a5);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static struct sysent ipcmsg_sysent = {
1317c478bd9Sstevel@tonic-gate 	6,
1327c478bd9Sstevel@tonic-gate #ifdef	_LP64
1337c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
1347c478bd9Sstevel@tonic-gate #else
1357c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 	(int (*)())msgsys
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1417c478bd9Sstevel@tonic-gate static ssize32_t msgsys32(int opcode, uint32_t a0, uint32_t a1, uint32_t a2,
1427c478bd9Sstevel@tonic-gate 	uint32_t a4, uint32_t a5);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static struct sysent ipcmsg_sysent32 = {
1457c478bd9Sstevel@tonic-gate 	6,
1467c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1477c478bd9Sstevel@tonic-gate 	(int (*)())msgsys32
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
1527c478bd9Sstevel@tonic-gate 	&mod_syscallops, "System V message facility", &ipcmsg_sysent
1537c478bd9Sstevel@tonic-gate };
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1567c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
1577c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit System V message facility", &ipcmsg_sysent32
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate #endif
1607c478bd9Sstevel@tonic-gate 
1612c5b6df1Sdv142724 /*
1622c5b6df1Sdv142724  *      Big Theory statement for message queue correctness
1632c5b6df1Sdv142724  *
1642c5b6df1Sdv142724  * The msgrcv and msgsnd functions no longer uses cv_broadcast to wake up
1652c5b6df1Sdv142724  * receivers who are waiting for an event.  Using the cv_broadcast method
1662c5b6df1Sdv142724  * resulted in negative scaling when the number of waiting receivers are large
1672c5b6df1Sdv142724  * (the thundering herd problem).  Instead, the receivers waiting to receive a
1682c5b6df1Sdv142724  * message are now linked in a queue-like fashion and awaken one at a time in
1692c5b6df1Sdv142724  * a controlled manner.
1702c5b6df1Sdv142724  *
1712c5b6df1Sdv142724  * Receivers can block on two different classes of waiting list:
1722c5b6df1Sdv142724  *    1) "sendwait" list, which is the more complex list of the two.  The
1732c5b6df1Sdv142724  *	  receiver will be awakened by a sender posting a new message.  There
1742c5b6df1Sdv142724  *	  are two types of "sendwait" list used:
1752c5b6df1Sdv142724  *		a) msg_wait_snd: handles all receivers who are looking for
1762c5b6df1Sdv142724  *		   a message type >= 0, but was unable to locate a match.
1772c5b6df1Sdv142724  *
1782c5b6df1Sdv142724  *		   slot 0: reserved for receivers that have designated they
1792c5b6df1Sdv142724  *			   will take any message type.
1802c5b6df1Sdv142724  *		   rest:   consist of receivers requesting a specific type
1812c5b6df1Sdv142724  *			   but the type was not present.  The entries are
1822c5b6df1Sdv142724  *			   hashed into a bucket in an attempt to keep
1832c5b6df1Sdv142724  *			   any list search relatively short.
1842c5b6df1Sdv142724  * 		b) msg_wait_snd_ngt: handles all receivers that have designated
1852c5b6df1Sdv142724  *		   a negative message type. Unlike msg_wait_snd, the hash bucket
1862c5b6df1Sdv142724  *		   serves a range of negative message types (-1 to -5, -6 to -10
1872c5b6df1Sdv142724  *		   and so forth), where the last bucket is reserved for all the
1882c5b6df1Sdv142724  *		   negative message types that hash outside of MSG_MAX_QNUM - 1.
1892c5b6df1Sdv142724  *		   This is done this way to simplify the operation of locating a
1902c5b6df1Sdv142724  *		   negative message type.
1912c5b6df1Sdv142724  *
1922c5b6df1Sdv142724  *    2) "copyout" list, where the receiver is awakened by another
1932c5b6df1Sdv142724  *	 receiver after a message is copied out.  This is a linked list
1942c5b6df1Sdv142724  *	 of waiters that are awakened one at a time.  Although the solution is
1952c5b6df1Sdv142724  *	 not optimal, the complexity that would be added in for waking
1962c5b6df1Sdv142724  *	 up the right entry far exceeds any potential pay back (too many
1972c5b6df1Sdv142724  *	 correctness and corner case issues).
1982c5b6df1Sdv142724  *
1992c5b6df1Sdv142724  * The lists are doubly linked.  In the case of the "sendwait"
2002c5b6df1Sdv142724  * list, this allows the thread to remove itself from the list without having
2012c5b6df1Sdv142724  * to traverse the list.  In the case of the "copyout" list it simply allows
2022c5b6df1Sdv142724  * us to use common functions with the "sendwait" list.
2032c5b6df1Sdv142724  *
2042c5b6df1Sdv142724  * To make sure receivers are not hung out to dry, we must guarantee:
2052c5b6df1Sdv142724  *    1. If any queued message matches any receiver, then at least one
2062c5b6df1Sdv142724  *       matching receiver must be processing the request.
2072c5b6df1Sdv142724  *    2. Blocking on the copyout queue is only temporary while messages
2082c5b6df1Sdv142724  *	 are being copied out.  The process is guaranted to wakeup
2092c5b6df1Sdv142724  *	 when it gets to front of the queue (copyout is a FIFO).
2102c5b6df1Sdv142724  *
2112c5b6df1Sdv142724  * Rules for blocking and waking up:
2122c5b6df1Sdv142724  *   1. A receiver entering msgrcv must examine all messages for a match
2132c5b6df1Sdv142724  *      before blocking on a sendwait queue.
2142c5b6df1Sdv142724  *   2. If the receiver blocks because the message it chose is already
2152c5b6df1Sdv142724  *	being copied out, then when it wakes up needs to start start
2162c5b6df1Sdv142724  *	checking the messages from the beginning.
2172c5b6df1Sdv142724  *   3) When ever a process returns from msgrcv for any reason, if it
2182c5b6df1Sdv142724  *	had attempted to copy a message or blocked waiting for a copy
2192c5b6df1Sdv142724  *	to complete it needs to wakeup the next receiver blocked on
2202c5b6df1Sdv142724  *	a copy out.
2212c5b6df1Sdv142724  *   4) When a message is sent, the sender selects a process waiting
2222c5b6df1Sdv142724  *	for that type of message.  This selection process rotates between
2232c5b6df1Sdv142724  *	receivers types of 0, negative and positive to prevent starvation of
2242c5b6df1Sdv142724  *	any one particular receiver type.
2252c5b6df1Sdv142724  *   5) The following are the scenarios for processes that are awakened
2262c5b6df1Sdv142724  *	by a msgsnd:
2272c5b6df1Sdv142724  *		a) The process finds the message and is able to copy
2282c5b6df1Sdv142724  *		   it out.  Once complete, the process returns.
2292c5b6df1Sdv142724  *		b) The message that was sent that triggered the wakeup is no
2302c5b6df1Sdv142724  *		   longer available (another process found the message first).
2312c5b6df1Sdv142724  *		   We issue a wakeup on copy queue and then go back to
2322c5b6df1Sdv142724  *		   sleep waiting for another matching message to be sent.
2332c5b6df1Sdv142724  *		c) The message that was supposed to be processed was
2342c5b6df1Sdv142724  *		   already serviced by another process.  However a different
2352c5b6df1Sdv142724  *		   message is present which we can service.  The message
2362c5b6df1Sdv142724  *		   is copied and the process returns.
2372c5b6df1Sdv142724  *		d) The message is found, but some sort of error occurs that
2382c5b6df1Sdv142724  *		   prevents the message from being copied.  The receiver
2392c5b6df1Sdv142724  *		   wakes up the next sender that can service this message
2402c5b6df1Sdv142724  *		   type and returns an error to the caller.
2412c5b6df1Sdv142724  *		e) The message is found, but it is marked as being copied
2422c5b6df1Sdv142724  *		   out.  The receiver then goes to sleep on the copyout
2432c5b6df1Sdv142724  *		   queue where it will be awakened again sometime in the future.
2442c5b6df1Sdv142724  *
2452c5b6df1Sdv142724  *
2462c5b6df1Sdv142724  *   6) Whenever a message is found that matches the message type designated,
2472c5b6df1Sdv142724  * 	but is being copied out we have to block on the copyout queue.
2482c5b6df1Sdv142724  *	After process copying finishes the copy out, it  must wakeup (either
2492c5b6df1Sdv142724  *	directly or indirectly) all receivers who blocked on its copyout,
2502c5b6df1Sdv142724  *	so they are guaranteed a chance to examine the remaining messages.
2512c5b6df1Sdv142724  *	This is implemented via a chain of wakeups: Y wakes X, who wakes Z,
2522c5b6df1Sdv142724  *	and so on.  The chain cannot be broken.  This leads to the following
2532c5b6df1Sdv142724  *	cases:
2542c5b6df1Sdv142724  *		a) A receiver is finished copying the message (or encountered)
2552c5b6df1Sdv142724  *		   an error), the first entry on the copyout queue is woken
2562c5b6df1Sdv142724  *		   up.
2572c5b6df1Sdv142724  *		b) When the receiver is woken up, it attempts to locate
2582c5b6df1Sdv142724  *		   a message type match.
2592c5b6df1Sdv142724  *		c) If a message type is found and
2602c5b6df1Sdv142724  *			-- MSG_RCVCOPY flag is not set, the message is
2612c5b6df1Sdv142724  *			   marked for copying out.  Regardless of the copyout
2622c5b6df1Sdv142724  *			   success the next entry on the copyout queue is
2632c5b6df1Sdv142724  *			   awakened and the operation is completed.
2642c5b6df1Sdv142724  *			-- MSG_RCVCOPY is set, we simply go back to sleep again
2652c5b6df1Sdv142724  *			   on the copyout queue.
2662c5b6df1Sdv142724  *		d) If the message type is not found then we wakeup the next
2672c5b6df1Sdv142724  *		   process on the copyout queue.
2682c5b6df1Sdv142724  */
2692c5b6df1Sdv142724 
270*e5994f96Sdv142724 static uint_t msg_type_hash(long);
2712c5b6df1Sdv142724 static int msgq_check_err(kmsqid_t *qp, int cvres);
2722c5b6df1Sdv142724 static int msg_rcvq_sleep(list_t *, msgq_wakeup_t *, kmutex_t **,
2732c5b6df1Sdv142724     kmsqid_t *);
2742c5b6df1Sdv142724 static int msg_copyout(kmsqid_t *, long, kmutex_t **, size_t *, size_t,
2752c5b6df1Sdv142724     struct msg *, struct ipcmsgbuf *, int);
2762c5b6df1Sdv142724 static void msg_rcvq_wakeup_all(list_t *);
2772c5b6df1Sdv142724 static void msg_wakeup_rdr(kmsqid_t *, msg_select_t **, long);
2782c5b6df1Sdv142724 static msgq_wakeup_t *msg_fnd_any_snd(kmsqid_t *, int, long);
2792c5b6df1Sdv142724 static msgq_wakeup_t *msg_fnd_any_rdr(kmsqid_t *, int, long);
2802c5b6df1Sdv142724 static msgq_wakeup_t *msg_fnd_neg_snd(kmsqid_t *, int, long);
2812c5b6df1Sdv142724 static msgq_wakeup_t *msg_fnd_spc_snd(kmsqid_t *, int, long);
2822c5b6df1Sdv142724 static struct msg *msgrcv_lookup(kmsqid_t *, long);
2832c5b6df1Sdv142724 
2842c5b6df1Sdv142724 msg_select_t msg_fnd_sndr[] = {
2852c5b6df1Sdv142724 	{ msg_fnd_any_snd, &msg_fnd_sndr[1] },
2862c5b6df1Sdv142724 	{ msg_fnd_spc_snd, &msg_fnd_sndr[2] },
2872c5b6df1Sdv142724 	{ msg_fnd_neg_snd, &msg_fnd_sndr[0] }
2882c5b6df1Sdv142724 };
2892c5b6df1Sdv142724 
2902c5b6df1Sdv142724 msg_select_t msg_fnd_rdr[1] = {
2912c5b6df1Sdv142724 	{ msg_fnd_any_rdr, &msg_fnd_rdr[0] },
2922c5b6df1Sdv142724 };
2932c5b6df1Sdv142724 
2947c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2957c478bd9Sstevel@tonic-gate 	MODREV_1,
2967c478bd9Sstevel@tonic-gate 	&modlsys,
2977c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2987c478bd9Sstevel@tonic-gate 	&modlsys32,
2997c478bd9Sstevel@tonic-gate #endif
3007c478bd9Sstevel@tonic-gate 	NULL
3017c478bd9Sstevel@tonic-gate };
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate int
3057c478bd9Sstevel@tonic-gate _init(void)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	int result;
3087c478bd9Sstevel@tonic-gate 
309824c205fSml93401 	msq_svc = ipcs_create("msqids", rc_project_msgmni, rc_zone_msgmni,
310824c205fSml93401 	    sizeof (kmsqid_t), msg_dtor, msg_rmid, AT_IPC_MSG,
311824c205fSml93401 	    offsetof(ipc_rqty_t, ipcq_msgmni));
3127c478bd9Sstevel@tonic-gate 	zone_key_create(&msg_zone_key, NULL, msg_remove_zone, NULL);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if ((result = mod_install(&modlinkage)) == 0)
3157c478bd9Sstevel@tonic-gate 		return (0);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	(void) zone_key_delete(msg_zone_key);
3187c478bd9Sstevel@tonic-gate 	ipcs_destroy(msq_svc);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (result);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate int
3247c478bd9Sstevel@tonic-gate _fini(void)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	return (EBUSY);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate int
3307c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static void
3367c478bd9Sstevel@tonic-gate msg_dtor(kipc_perm_t *perm)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	kmsqid_t *qp = (kmsqid_t *)perm;
339b2eb1770Sudpa 	int		ii;
3407c478bd9Sstevel@tonic-gate 
3412c5b6df1Sdv142724 	for (ii = 0; ii <= MSG_MAX_QNUM; ii++) {
3422c5b6df1Sdv142724 		ASSERT(list_is_empty(&qp->msg_wait_snd[ii]));
3432c5b6df1Sdv142724 		ASSERT(list_is_empty(&qp->msg_wait_snd_ngt[ii]));
3442c5b6df1Sdv142724 		list_destroy(&qp->msg_wait_snd[ii]);
3452c5b6df1Sdv142724 		list_destroy(&qp->msg_wait_snd_ngt[ii]);
3462c5b6df1Sdv142724 	}
3472c5b6df1Sdv142724 	ASSERT(list_is_empty(&qp->msg_cpy_block));
3482c5b6df1Sdv142724 	list_destroy(&qp->msg_cpy_block);
3497c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_snd_cnt == 0);
3507c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_cbytes == 0);
3517c478bd9Sstevel@tonic-gate 	list_destroy(&qp->msg_list);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate #define	msg_hold(mp)	(mp)->msg_copycnt++
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * msg_rele - decrement the reference count on the message.  When count
3597c478bd9Sstevel@tonic-gate  * reaches zero, free message header and contents.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate static void
3627c478bd9Sstevel@tonic-gate msg_rele(struct msg *mp)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	ASSERT(mp->msg_copycnt > 0);
3657c478bd9Sstevel@tonic-gate 	if (mp->msg_copycnt-- == 1) {
3667c478bd9Sstevel@tonic-gate 		if (mp->msg_addr)
3677c478bd9Sstevel@tonic-gate 			kmem_free(mp->msg_addr, mp->msg_size);
3687c478bd9Sstevel@tonic-gate 		kmem_free(mp, sizeof (struct msg));
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * msgunlink - Unlink msg from queue, decrement byte count and wake up anyone
3747c478bd9Sstevel@tonic-gate  * waiting for free bytes on queue.
3757c478bd9Sstevel@tonic-gate  *
3767c478bd9Sstevel@tonic-gate  * Called with queue locked.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate static void
3797c478bd9Sstevel@tonic-gate msgunlink(kmsqid_t *qp, struct msg *mp)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	list_remove(&qp->msg_list, mp);
3827c478bd9Sstevel@tonic-gate 	qp->msg_qnum--;
3837c478bd9Sstevel@tonic-gate 	qp->msg_cbytes -= mp->msg_size;
3847c478bd9Sstevel@tonic-gate 	msg_rele(mp);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* Wake up waiting writers */
3877c478bd9Sstevel@tonic-gate 	if (qp->msg_snd_cnt)
3887c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->msg_snd_cv);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate static void
3927c478bd9Sstevel@tonic-gate msg_rmid(kipc_perm_t *perm)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	kmsqid_t *qp = (kmsqid_t *)perm;
3957c478bd9Sstevel@tonic-gate 	struct msg *mp;
396b2eb1770Sudpa 	int		ii;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	while ((mp = list_head(&qp->msg_list)) != NULL)
4007c478bd9Sstevel@tonic-gate 		msgunlink(qp, mp);
4017c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_cbytes == 0);
4027c478bd9Sstevel@tonic-gate 
4032c5b6df1Sdv142724 	/*
4042c5b6df1Sdv142724 	 * Wake up everyone who is in a wait state of some sort
4052c5b6df1Sdv142724 	 * for this message queue.
4062c5b6df1Sdv142724 	 */
4072c5b6df1Sdv142724 	for (ii = 0; ii <= MSG_MAX_QNUM; ii++) {
4082c5b6df1Sdv142724 		msg_rcvq_wakeup_all(&qp->msg_wait_snd[ii]);
4092c5b6df1Sdv142724 		msg_rcvq_wakeup_all(&qp->msg_wait_snd_ngt[ii]);
410b2eb1770Sudpa 	}
4112c5b6df1Sdv142724 	msg_rcvq_wakeup_all(&qp->msg_cpy_block);
4127c478bd9Sstevel@tonic-gate 	if (qp->msg_snd_cnt)
4137c478bd9Sstevel@tonic-gate 		cv_broadcast(&qp->msg_snd_cv);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * msgctl system call.
4187c478bd9Sstevel@tonic-gate  *
4197c478bd9Sstevel@tonic-gate  * gets q lock (via ipc_lookup), releases before return.
4207c478bd9Sstevel@tonic-gate  * may call users of msg_lock
4217c478bd9Sstevel@tonic-gate  */
4227c478bd9Sstevel@tonic-gate static int
4237c478bd9Sstevel@tonic-gate msgctl(int msgid, int cmd, void *arg)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msqid_ds, ds);		/* SVR4 queue work area */
4267c478bd9Sstevel@tonic-gate 	kmsqid_t		*qp;		/* ptr to associated q */
4272c5b6df1Sdv142724 	int			error;
4287c478bd9Sstevel@tonic-gate 	struct	cred		*cr;
4297c478bd9Sstevel@tonic-gate 	model_t	mdl = get_udatamodel();
4307c478bd9Sstevel@tonic-gate 	struct msqid_ds64	ds64;
4317c478bd9Sstevel@tonic-gate 	kmutex_t		*lock;
4327c478bd9Sstevel@tonic-gate 	proc_t			*pp = curproc;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	STRUCT_INIT(ds, mdl);
4357c478bd9Sstevel@tonic-gate 	cr = CRED();
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Perform pre- or non-lookup actions (e.g. copyins, RMID).
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	switch (cmd) {
4417c478bd9Sstevel@tonic-gate 	case IPC_SET:
4427c478bd9Sstevel@tonic-gate 		if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
4437c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4447c478bd9Sstevel@tonic-gate 		break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	case IPC_SET64:
4477c478bd9Sstevel@tonic-gate 		if (copyin(arg, &ds64, sizeof (struct msqid_ds64)))
4487c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4497c478bd9Sstevel@tonic-gate 		break;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	case IPC_RMID:
4527c478bd9Sstevel@tonic-gate 		if (error = ipc_rmid(msq_svc, msgid, cr))
4537c478bd9Sstevel@tonic-gate 			return (set_errno(error));
4547c478bd9Sstevel@tonic-gate 		return (0);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * get msqid_ds for this msgid
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msgid, (kipc_perm_t **)&qp)) == NULL)
4617c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	switch (cmd) {
4647c478bd9Sstevel@tonic-gate 	case IPC_SET:
4657c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(ds, msg_qbytes) > qp->msg_qbytes &&
4667c478bd9Sstevel@tonic-gate 		    secpolicy_ipc_config(cr) != 0) {
4677c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
4687c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set(msq_svc, cr, &qp->msg_perm,
4717c478bd9Sstevel@tonic-gate 		    &STRUCT_BUF(ds)->msg_perm, mdl)) {
4727c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
4737c478bd9Sstevel@tonic-gate 			return (set_errno(error));
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = STRUCT_FGET(ds, msg_qbytes);
4767c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
4777c478bd9Sstevel@tonic-gate 		break;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	case IPC_STAT:
4807c478bd9Sstevel@tonic-gate 		if (error = ipcperm_access(&qp->msg_perm, MSG_R, cr)) {
4817c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
4827c478bd9Sstevel@tonic-gate 			return (set_errno(error));
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 
4852c5b6df1Sdv142724 		if (qp->msg_rcv_cnt)
4867c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_RWAIT;
4877c478bd9Sstevel@tonic-gate 		if (qp->msg_snd_cnt)
4887c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_WWAIT;
4897c478bd9Sstevel@tonic-gate 		ipcperm_stat(&STRUCT_BUF(ds)->msg_perm, &qp->msg_perm, mdl);
4907c478bd9Sstevel@tonic-gate 		qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT);
4917c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, msg_first, NULL); 	/* kernel addr */
4927c478bd9Sstevel@tonic-gate 		STRUCT_FSETP(ds, msg_last, NULL);
4937c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_cbytes, qp->msg_cbytes);
4947c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_qnum, qp->msg_qnum);
4957c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_qbytes, qp->msg_qbytes);
4967c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_lspid, qp->msg_lspid);
4977c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_lrpid, qp->msg_lrpid);
4987c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_stime, qp->msg_stime);
4997c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_rtime, qp->msg_rtime);
5007c478bd9Sstevel@tonic-gate 		STRUCT_FSET(ds, msg_ctime, qp->msg_ctime);
5017c478bd9Sstevel@tonic-gate 		break;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	case IPC_SET64:
5047c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
5057c478bd9Sstevel@tonic-gate 		if ((ds64.msgx_qbytes > qp->msg_qbytes) &&
5067c478bd9Sstevel@tonic-gate 		    secpolicy_ipc_config(cr) != 0 &&
5077c478bd9Sstevel@tonic-gate 		    rctl_test(rc_process_msgmnb, pp->p_rctls, pp,
5087c478bd9Sstevel@tonic-gate 		    ds64.msgx_qbytes, RCA_SAFE) & RCT_DENY) {
5097c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
5107c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
5117c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5147c478bd9Sstevel@tonic-gate 		if (error = ipcperm_set64(msq_svc, cr, &qp->msg_perm,
5157c478bd9Sstevel@tonic-gate 		    &ds64.msgx_perm)) {
5167c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
5177c478bd9Sstevel@tonic-gate 			return (set_errno(error));
5187c478bd9Sstevel@tonic-gate 		}
5197c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = ds64.msgx_qbytes;
5207c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
5217c478bd9Sstevel@tonic-gate 		break;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
5242c5b6df1Sdv142724 		if (qp->msg_rcv_cnt)
5257c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_RWAIT;
5267c478bd9Sstevel@tonic-gate 		if (qp->msg_snd_cnt)
5277c478bd9Sstevel@tonic-gate 			qp->msg_perm.ipc_mode |= MSG_WWAIT;
5287c478bd9Sstevel@tonic-gate 		ipcperm_stat64(&ds64.msgx_perm, &qp->msg_perm);
5297c478bd9Sstevel@tonic-gate 		qp->msg_perm.ipc_mode &= ~(MSG_RWAIT|MSG_WWAIT);
5307c478bd9Sstevel@tonic-gate 		ds64.msgx_cbytes = qp->msg_cbytes;
5317c478bd9Sstevel@tonic-gate 		ds64.msgx_qnum = qp->msg_qnum;
5327c478bd9Sstevel@tonic-gate 		ds64.msgx_qbytes = qp->msg_qbytes;
5337c478bd9Sstevel@tonic-gate 		ds64.msgx_lspid = qp->msg_lspid;
5347c478bd9Sstevel@tonic-gate 		ds64.msgx_lrpid = qp->msg_lrpid;
5357c478bd9Sstevel@tonic-gate 		ds64.msgx_stime = qp->msg_stime;
5367c478bd9Sstevel@tonic-gate 		ds64.msgx_rtime = qp->msg_rtime;
5377c478bd9Sstevel@tonic-gate 		ds64.msgx_ctime = qp->msg_ctime;
5387c478bd9Sstevel@tonic-gate 		break;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	default:
5417c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
5427c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * Do copyout last (after releasing mutex).
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	switch (cmd) {
5517c478bd9Sstevel@tonic-gate 	case IPC_STAT:
5527c478bd9Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
5537c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
5547c478bd9Sstevel@tonic-gate 		break;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	case IPC_STAT64:
5577c478bd9Sstevel@tonic-gate 		if (copyout(&ds64, arg, sizeof (struct msqid_ds64)))
5587c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	return (0);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Remove all message queues associated with a given zone.  Called by
5677c478bd9Sstevel@tonic-gate  * zone_shutdown when the zone is halted.
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
5707c478bd9Sstevel@tonic-gate static void
5717c478bd9Sstevel@tonic-gate msg_remove_zone(zoneid_t zoneid, void *arg)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	ipc_remove_zone(msq_svc, zoneid);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * msgget system call.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate static int
5807c478bd9Sstevel@tonic-gate msgget(key_t key, int msgflg)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;
5837c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
5847c478bd9Sstevel@tonic-gate 	int		id, error;
585b2eb1770Sudpa 	int		ii;
5867c478bd9Sstevel@tonic-gate 	proc_t		*pp = curproc;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate top:
5897c478bd9Sstevel@tonic-gate 	if (error = ipc_get(msq_svc, key, msgflg, (kipc_perm_t **)&qp, &lock))
5907c478bd9Sstevel@tonic-gate 		return (set_errno(error));
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&qp->msg_perm)) {
5937c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
5947c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		list_create(&qp->msg_list, sizeof (struct msg),
5977c478bd9Sstevel@tonic-gate 		    offsetof(struct msg, msg_node));
5987c478bd9Sstevel@tonic-gate 		qp->msg_qnum = 0;
5997c478bd9Sstevel@tonic-gate 		qp->msg_lspid = qp->msg_lrpid = 0;
6007c478bd9Sstevel@tonic-gate 		qp->msg_stime = qp->msg_rtime = 0;
6017c478bd9Sstevel@tonic-gate 		qp->msg_ctime = gethrestime_sec();
6022c5b6df1Sdv142724 		qp->msg_ngt_cnt = 0;
6032c5b6df1Sdv142724 		qp->msg_neg_copy = 0;
6042c5b6df1Sdv142724 		for (ii = 0; ii <= MSG_MAX_QNUM; ii++) {
6052c5b6df1Sdv142724 			list_create(&qp->msg_wait_snd[ii],
6062c5b6df1Sdv142724 			    sizeof (msgq_wakeup_t),
6072c5b6df1Sdv142724 			    offsetof(msgq_wakeup_t, msgw_list));
6082c5b6df1Sdv142724 			list_create(&qp->msg_wait_snd_ngt[ii],
6092c5b6df1Sdv142724 			    sizeof (msgq_wakeup_t),
6102c5b6df1Sdv142724 			    offsetof(msgq_wakeup_t, msgw_list));
6112c5b6df1Sdv142724 		}
6122c5b6df1Sdv142724 		/*
6132c5b6df1Sdv142724 		 * The proper initialization of msg_lowest_type is to the
6142c5b6df1Sdv142724 		 * highest possible value.  By doing this we guarantee that
6152c5b6df1Sdv142724 		 * when the first send happens, the lowest type will be set
6162c5b6df1Sdv142724 		 * properly.
6172c5b6df1Sdv142724 		 */
618*e5994f96Sdv142724 		qp->msg_lowest_type = LONG_MAX;
6192c5b6df1Sdv142724 		list_create(&qp->msg_cpy_block,
6202c5b6df1Sdv142724 		    sizeof (msgq_wakeup_t),
6212c5b6df1Sdv142724 		    offsetof(msgq_wakeup_t, msgw_list));
6222c5b6df1Sdv142724 		qp->msg_fnd_sndr = &msg_fnd_sndr[0];
6232c5b6df1Sdv142724 		qp->msg_fnd_rdr = &msg_fnd_rdr[0];
6242c5b6df1Sdv142724 		qp->msg_rcv_cnt = 0;
625b2eb1770Sudpa 		qp->msg_snd_cnt = 0;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		if (error = ipc_commit_begin(msq_svc, key, msgflg,
6287c478bd9Sstevel@tonic-gate 		    (kipc_perm_t *)qp)) {
6297c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
6307c478bd9Sstevel@tonic-gate 				goto top;
6317c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 		qp->msg_qbytes = rctl_enforced_value(rc_process_msgmnb,
6347c478bd9Sstevel@tonic-gate 		    pp->p_rctls, pp);
6357c478bd9Sstevel@tonic-gate 		qp->msg_qmax = rctl_enforced_value(rc_process_msgtql,
6367c478bd9Sstevel@tonic-gate 		    pp->p_rctls, pp);
6377c478bd9Sstevel@tonic-gate 		lock = ipc_commit_end(msq_svc, &qp->msg_perm);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	if (audit_active)
6407c478bd9Sstevel@tonic-gate 		audit_ipcget(AT_IPC_MSG, (void *)qp);
6417c478bd9Sstevel@tonic-gate 	id = qp->msg_perm.ipc_id;
6427c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
6437c478bd9Sstevel@tonic-gate 	return (id);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate static ssize_t
6477c478bd9Sstevel@tonic-gate msgrcv(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, long msgtyp, int msgflg)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	struct msg	*smp;	/* ptr to best msg on q */
6507c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;	/* ptr to associated q */
6517c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
6527c478bd9Sstevel@tonic-gate 	size_t		xtsz;	/* transfer byte count */
6532c5b6df1Sdv142724 	int		error = 0;
6547c478bd9Sstevel@tonic-gate 	int		cvres;
655*e5994f96Sdv142724 	uint_t		msg_hash;
6562c5b6df1Sdv142724 	msgq_wakeup_t	msg_entry;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, msg, 1);	/* bump msg send/rcv count */
6597c478bd9Sstevel@tonic-gate 
6602c5b6df1Sdv142724 	msg_hash = msg_type_hash(msgtyp);
6612c5b6df1Sdv142724 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL) {
6627c478bd9Sstevel@tonic-gate 		return ((ssize_t)set_errno(EINVAL));
6632c5b6df1Sdv142724 	}
6647c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
6657c478bd9Sstevel@tonic-gate 
6662c5b6df1Sdv142724 	if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED())) {
6677c478bd9Sstevel@tonic-gate 		goto msgrcv_out;
6682c5b6df1Sdv142724 	}
6697c478bd9Sstevel@tonic-gate 
6702c5b6df1Sdv142724 	/*
6712c5b6df1Sdv142724 	 * Various information (including the condvar_t) required for the
6722c5b6df1Sdv142724 	 * process to sleep is provided by it's stack.
6732c5b6df1Sdv142724 	 */
6742c5b6df1Sdv142724 	msg_entry.msgw_thrd = curthread;
6752c5b6df1Sdv142724 	msg_entry.msgw_snd_wake = 0;
6762c5b6df1Sdv142724 	msg_entry.msgw_type = msgtyp;
6777c478bd9Sstevel@tonic-gate findmsg:
6782c5b6df1Sdv142724 	smp = msgrcv_lookup(qp, msgtyp);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if (smp) {
6817c478bd9Sstevel@tonic-gate 		/*
6822c5b6df1Sdv142724 		 * We found a possible message to copy out.
6837c478bd9Sstevel@tonic-gate 		 */
6847c478bd9Sstevel@tonic-gate 		if ((smp->msg_flags & MSG_RCVCOPY) == 0) {
685*e5994f96Sdv142724 			long t = msg_entry.msgw_snd_wake;
6867c478bd9Sstevel@tonic-gate 			/*
6872c5b6df1Sdv142724 			 * It is available, attempt to copy it.
6887c478bd9Sstevel@tonic-gate 			 */
6892c5b6df1Sdv142724 			error = msg_copyout(qp, msgtyp, &lock, &xtsz, msgsz,
6902c5b6df1Sdv142724 			    smp, msgp, msgflg);
691*e5994f96Sdv142724 
692*e5994f96Sdv142724 			/*
693*e5994f96Sdv142724 			 * It is possible to consume a different message
694*e5994f96Sdv142724 			 * type then what originally awakened for (negative
695*e5994f96Sdv142724 			 * types).  If this happens a check must be done to
696*e5994f96Sdv142724 			 * to determine if another receiver is available
697*e5994f96Sdv142724 			 * for the waking message type,  Failure to do this
698*e5994f96Sdv142724 			 * can result in a message on the queue that can be
699*e5994f96Sdv142724 			 * serviced by a sleeping receiver.
700*e5994f96Sdv142724 			 */
701*e5994f96Sdv142724 			if (!error && t && (smp->msg_type != t))
702*e5994f96Sdv142724 				msg_wakeup_rdr(qp, &qp->msg_fnd_sndr, t);
703*e5994f96Sdv142724 
7042c5b6df1Sdv142724 			/*
7052c5b6df1Sdv142724 			 * Don't forget to wakeup a sleeper that blocked because
7062c5b6df1Sdv142724 			 * we were copying things out.
7072c5b6df1Sdv142724 			 */
7082c5b6df1Sdv142724 			msg_wakeup_rdr(qp, &qp->msg_fnd_rdr, 0);
7092c5b6df1Sdv142724 			goto msgrcv_out;
7102c5b6df1Sdv142724 		}
7112c5b6df1Sdv142724 		/*
7122c5b6df1Sdv142724 		 * The selected message is being copied out, so block.  We do
7132c5b6df1Sdv142724 		 * not need to wake the next person up on the msg_cpy_block list
7142c5b6df1Sdv142724 		 * due to the fact some one is copying out and they will get
7152c5b6df1Sdv142724 		 * things moving again once the copy is completed.
7162c5b6df1Sdv142724 		 */
7172c5b6df1Sdv142724 		cvres = msg_rcvq_sleep(&qp->msg_cpy_block,
7182c5b6df1Sdv142724 		    &msg_entry, &lock, qp);
7192c5b6df1Sdv142724 		error = msgq_check_err(qp, cvres);
7202c5b6df1Sdv142724 		if (error) {
7212c5b6df1Sdv142724 			goto msgrcv_out;
7222c5b6df1Sdv142724 		}
7232c5b6df1Sdv142724 		goto findmsg;
7242c5b6df1Sdv142724 	}
7252c5b6df1Sdv142724 	/*
7262c5b6df1Sdv142724 	 * There isn't a message to copy out that matches the designated
7272c5b6df1Sdv142724 	 * criteria.
7282c5b6df1Sdv142724 	 */
7292c5b6df1Sdv142724 	if (msgflg & IPC_NOWAIT) {
7302c5b6df1Sdv142724 		error = ENOMSG;
7312c5b6df1Sdv142724 		goto msgrcv_out;
7322c5b6df1Sdv142724 	}
7332c5b6df1Sdv142724 	msg_wakeup_rdr(qp,  &qp->msg_fnd_rdr, 0);
7342c5b6df1Sdv142724 
7352c5b6df1Sdv142724 	/*
7362c5b6df1Sdv142724 	 * Wait for new message.  We keep the negative and positive types
7372c5b6df1Sdv142724 	 * separate for performance reasons.
7382c5b6df1Sdv142724 	 */
7392c5b6df1Sdv142724 	msg_entry.msgw_snd_wake = 0;
7402c5b6df1Sdv142724 	if (msgtyp >= 0) {
7412c5b6df1Sdv142724 		cvres = msg_rcvq_sleep(&qp->msg_wait_snd[msg_hash],
7422c5b6df1Sdv142724 		    &msg_entry, &lock, qp);
7432c5b6df1Sdv142724 	} else {
7442c5b6df1Sdv142724 		qp->msg_ngt_cnt++;
7452c5b6df1Sdv142724 		cvres = msg_rcvq_sleep(&qp->msg_wait_snd_ngt[msg_hash],
7462c5b6df1Sdv142724 		    &msg_entry, &lock, qp);
7472c5b6df1Sdv142724 		qp->msg_ngt_cnt--;
7482c5b6df1Sdv142724 	}
7492c5b6df1Sdv142724 
7502c5b6df1Sdv142724 	if (!(error = msgq_check_err(qp, cvres))) {
7512c5b6df1Sdv142724 		goto findmsg;
7522c5b6df1Sdv142724 	}
7532c5b6df1Sdv142724 
7542c5b6df1Sdv142724 msgrcv_out:
7552c5b6df1Sdv142724 	if (error) {
7562c5b6df1Sdv142724 		msg_wakeup_rdr(qp,  &qp->msg_fnd_rdr, 0);
7572c5b6df1Sdv142724 		if (msg_entry.msgw_snd_wake) {
7582c5b6df1Sdv142724 			msg_wakeup_rdr(qp, &qp->msg_fnd_sndr,
7592c5b6df1Sdv142724 			    msg_entry.msgw_snd_wake);
7602c5b6df1Sdv142724 		}
7612c5b6df1Sdv142724 		ipc_rele(msq_svc, (kipc_perm_t *)qp);
7622c5b6df1Sdv142724 		return ((ssize_t)set_errno(error));
7632c5b6df1Sdv142724 	}
7642c5b6df1Sdv142724 	ipc_rele(msq_svc, (kipc_perm_t *)qp);
7652c5b6df1Sdv142724 	return ((ssize_t)xtsz);
7662c5b6df1Sdv142724 }
7672c5b6df1Sdv142724 
7682c5b6df1Sdv142724 static int
7692c5b6df1Sdv142724 msgq_check_err(kmsqid_t *qp, int cvres)
7702c5b6df1Sdv142724 {
7712c5b6df1Sdv142724 	if (IPC_FREE(&qp->msg_perm)) {
7722c5b6df1Sdv142724 		return (EIDRM);
7732c5b6df1Sdv142724 	}
7742c5b6df1Sdv142724 
7752c5b6df1Sdv142724 	if (cvres == 0) {
7762c5b6df1Sdv142724 		return (EINTR);
7772c5b6df1Sdv142724 	}
7782c5b6df1Sdv142724 
7792c5b6df1Sdv142724 	return (0);
7802c5b6df1Sdv142724 }
7812c5b6df1Sdv142724 
7822c5b6df1Sdv142724 static int
7832c5b6df1Sdv142724 msg_copyout(kmsqid_t *qp, long msgtyp, kmutex_t **lock, size_t *xtsz_ret,
7842c5b6df1Sdv142724     size_t msgsz, struct msg *smp, struct ipcmsgbuf *msgp, int msgflg)
7852c5b6df1Sdv142724 {
7862c5b6df1Sdv142724 	size_t		xtsz;
7872c5b6df1Sdv142724 	STRUCT_HANDLE(ipcmsgbuf, umsgp);
7882c5b6df1Sdv142724 	model_t		mdl = get_udatamodel();
7892c5b6df1Sdv142724 	int		copyerror = 0;
7902c5b6df1Sdv142724 
7912c5b6df1Sdv142724 	STRUCT_SET_HANDLE(umsgp, mdl, msgp);
7927c478bd9Sstevel@tonic-gate 	if (msgsz < smp->msg_size) {
7937c478bd9Sstevel@tonic-gate 		if ((msgflg & MSG_NOERROR) == 0) {
7942c5b6df1Sdv142724 			return (E2BIG);
7957c478bd9Sstevel@tonic-gate 		} else {
7967c478bd9Sstevel@tonic-gate 			xtsz = msgsz;
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 	} else {
7997c478bd9Sstevel@tonic-gate 		xtsz = smp->msg_size;
8007c478bd9Sstevel@tonic-gate 	}
8012c5b6df1Sdv142724 	*xtsz_ret = xtsz;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/*
8042c5b6df1Sdv142724 	 * To prevent a DOS attack we mark the message as being
8052c5b6df1Sdv142724 	 * copied out and release mutex.  When the copy is completed
8062c5b6df1Sdv142724 	 * we need to acquire the mutex and make the appropriate updates.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	ASSERT((smp->msg_flags & MSG_RCVCOPY) == 0);
8097c478bd9Sstevel@tonic-gate 	smp->msg_flags |= MSG_RCVCOPY;
8107c478bd9Sstevel@tonic-gate 	msg_hold(smp);
8112c5b6df1Sdv142724 	if (msgtyp < 0) {
8122c5b6df1Sdv142724 		ASSERT(qp->msg_neg_copy == 0);
8132c5b6df1Sdv142724 		qp->msg_neg_copy = 1;
8142c5b6df1Sdv142724 	}
8152c5b6df1Sdv142724 	mutex_exit(*lock);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (mdl == DATAMODEL_NATIVE) {
8187c478bd9Sstevel@tonic-gate 		copyerror = copyout(&smp->msg_type, msgp,
8197c478bd9Sstevel@tonic-gate 		    sizeof (smp->msg_type));
8207c478bd9Sstevel@tonic-gate 	} else {
8217c478bd9Sstevel@tonic-gate 		/*
8227c478bd9Sstevel@tonic-gate 		 * 32-bit callers need an imploded msg type.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		int32_t	msg_type32 = smp->msg_type;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		copyerror = copyout(&msg_type32, msgp,
8277c478bd9Sstevel@tonic-gate 		    sizeof (msg_type32));
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
8302c5b6df1Sdv142724 	if (copyerror == 0 && xtsz) {
8317c478bd9Sstevel@tonic-gate 		copyerror = copyout(smp->msg_addr,
8327c478bd9Sstevel@tonic-gate 		    STRUCT_FADDR(umsgp, mtext), xtsz);
8332c5b6df1Sdv142724 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/*
8362c5b6df1Sdv142724 	 * Reclaim the mutex and make sure the message queue still exists.
8377c478bd9Sstevel@tonic-gate 	 */
8382c5b6df1Sdv142724 
8392c5b6df1Sdv142724 	*lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
8402c5b6df1Sdv142724 	if (msgtyp < 0) {
8412c5b6df1Sdv142724 		qp->msg_neg_copy = 0;
8422c5b6df1Sdv142724 	}
8437c478bd9Sstevel@tonic-gate 	ASSERT(smp->msg_flags & MSG_RCVCOPY);
8447c478bd9Sstevel@tonic-gate 	smp->msg_flags &= ~MSG_RCVCOPY;
8457c478bd9Sstevel@tonic-gate 	msg_rele(smp);
8467c478bd9Sstevel@tonic-gate 	if (IPC_FREE(&qp->msg_perm)) {
8472c5b6df1Sdv142724 		return (EIDRM);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 	if (copyerror) {
8502c5b6df1Sdv142724 		return (EFAULT);
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 	qp->msg_lrpid = ttoproc(curthread)->p_pid;
8537c478bd9Sstevel@tonic-gate 	qp->msg_rtime = gethrestime_sec();
8547c478bd9Sstevel@tonic-gate 	msgunlink(qp, smp);
8552c5b6df1Sdv142724 	return (0);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8582c5b6df1Sdv142724 static struct msg *
8592c5b6df1Sdv142724 msgrcv_lookup(kmsqid_t *qp, long msgtyp)
8602c5b6df1Sdv142724 {
8612c5b6df1Sdv142724 	struct msg 		*smp = NULL;
862*e5994f96Sdv142724 	long			qp_low;
8632c5b6df1Sdv142724 	struct msg		*mp;	/* ptr to msg on q */
864*e5994f96Sdv142724 	long			low_msgtype;
8652c5b6df1Sdv142724 	static struct msg	neg_copy_smp;
8662c5b6df1Sdv142724 
8672c5b6df1Sdv142724 	mp = list_head(&qp->msg_list);
8682c5b6df1Sdv142724 	if (msgtyp == 0) {
8692c5b6df1Sdv142724 		smp = mp;
8702c5b6df1Sdv142724 	} else {
8712c5b6df1Sdv142724 		qp_low = qp->msg_lowest_type;
8722c5b6df1Sdv142724 		if (msgtyp > 0) {
8732c5b6df1Sdv142724 			/*
8742c5b6df1Sdv142724 			 * If our lowest possible message type is larger than
8752c5b6df1Sdv142724 			 * the message type desired, then we know there is
8762c5b6df1Sdv142724 			 * no entry present.
8772c5b6df1Sdv142724 			 */
8782c5b6df1Sdv142724 			if (qp_low > msgtyp) {
8792c5b6df1Sdv142724 				return (NULL);
8802c5b6df1Sdv142724 			}
8812c5b6df1Sdv142724 
8822c5b6df1Sdv142724 			for (; mp; mp = list_next(&qp->msg_list, mp)) {
8832c5b6df1Sdv142724 				if (msgtyp == mp->msg_type) {
8842c5b6df1Sdv142724 					smp = mp;
8852c5b6df1Sdv142724 					break;
8862c5b6df1Sdv142724 				}
8872c5b6df1Sdv142724 			}
8887c478bd9Sstevel@tonic-gate 		} else {
8897c478bd9Sstevel@tonic-gate 			/*
8902c5b6df1Sdv142724 			 * We have kept track of the lowest possible message
8912c5b6df1Sdv142724 			 * type on the send queue.  This allows us to terminate
8922c5b6df1Sdv142724 			 * the search early if we find a message type of that
8932c5b6df1Sdv142724 			 * type.  Note, the lowest type may not be the actual
8942c5b6df1Sdv142724 			 * lowest value in the system, it is only guaranteed
8952c5b6df1Sdv142724 			 * that there isn't a value lower than that.
8967c478bd9Sstevel@tonic-gate 			 */
8972c5b6df1Sdv142724 			low_msgtype = -msgtyp;
898*e5994f96Sdv142724 			if (low_msgtype < qp_low) {
8992c5b6df1Sdv142724 				return (NULL);
9002c5b6df1Sdv142724 			}
9012c5b6df1Sdv142724 			if (qp->msg_neg_copy) {
9022c5b6df1Sdv142724 				neg_copy_smp.msg_flags = MSG_RCVCOPY;
9032c5b6df1Sdv142724 				return (&neg_copy_smp);
9042c5b6df1Sdv142724 			}
9052c5b6df1Sdv142724 			for (; mp; mp = list_next(&qp->msg_list, mp)) {
906*e5994f96Sdv142724 				if (mp->msg_type <= low_msgtype &&
907*e5994f96Sdv142724 				    !(smp && smp->msg_type <= mp->msg_type)) {
9082c5b6df1Sdv142724 					smp = mp;
9092c5b6df1Sdv142724 					low_msgtype = mp->msg_type;
9102c5b6df1Sdv142724 					if (low_msgtype == qp_low) {
9112c5b6df1Sdv142724 						break;
9127c478bd9Sstevel@tonic-gate 					}
9137c478bd9Sstevel@tonic-gate 				}
9147c478bd9Sstevel@tonic-gate 			}
9152c5b6df1Sdv142724 			if (smp) {
9162c5b6df1Sdv142724 				/*
9172c5b6df1Sdv142724 				 * Update the lowest message type.
9182c5b6df1Sdv142724 				 */
9192c5b6df1Sdv142724 				qp->msg_lowest_type = smp->msg_type;
9207c478bd9Sstevel@tonic-gate 			}
9212c5b6df1Sdv142724 		}
9222c5b6df1Sdv142724 	}
9232c5b6df1Sdv142724 	return (smp);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate  * msgids system call.
9287c478bd9Sstevel@tonic-gate  */
9297c478bd9Sstevel@tonic-gate static int
9307c478bd9Sstevel@tonic-gate msgids(int *buf, uint_t nids, uint_t *pnids)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	int error;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (error = ipc_ids(msq_svc, buf, nids, pnids))
9357c478bd9Sstevel@tonic-gate 		return (set_errno(error));
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	return (0);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate #define	RND(x)		roundup((x), sizeof (size_t))
9417c478bd9Sstevel@tonic-gate #define	RND32(x)	roundup((x), sizeof (size32_t))
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate /*
9447c478bd9Sstevel@tonic-gate  * msgsnap system call.
9457c478bd9Sstevel@tonic-gate  */
9467c478bd9Sstevel@tonic-gate static int
9477c478bd9Sstevel@tonic-gate msgsnap(int msqid, caddr_t buf, size_t bufsz, long msgtyp)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	struct msg	*mp;	/* ptr to msg on q */
9507c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;	/* ptr to associated q */
9517c478bd9Sstevel@tonic-gate 	kmutex_t	*lock;
9527c478bd9Sstevel@tonic-gate 	size_t		size;
9537c478bd9Sstevel@tonic-gate 	size_t		nmsg;
9547c478bd9Sstevel@tonic-gate 	struct msg	**snaplist;
9557c478bd9Sstevel@tonic-gate 	int		error, i;
9567c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
9577c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msgsnap_head, head);
9587c478bd9Sstevel@tonic-gate 	STRUCT_DECL(msgsnap_mhead, mhead);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	STRUCT_INIT(head, mdl);
9617c478bd9Sstevel@tonic-gate 	STRUCT_INIT(mhead, mdl);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (bufsz < STRUCT_SIZE(head))
9647c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL)
9677c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_R, CRED())) {
9707c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
9717c478bd9Sstevel@tonic-gate 		return (set_errno(error));
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	/*
9767c478bd9Sstevel@tonic-gate 	 * First compute the required buffer size and
9777c478bd9Sstevel@tonic-gate 	 * the number of messages on the queue.
9787c478bd9Sstevel@tonic-gate 	 */
9797c478bd9Sstevel@tonic-gate 	size = nmsg = 0;
9807c478bd9Sstevel@tonic-gate 	for (mp = list_head(&qp->msg_list); mp;
9817c478bd9Sstevel@tonic-gate 	    mp = list_next(&qp->msg_list, mp)) {
9827c478bd9Sstevel@tonic-gate 		if (msgtyp == 0 ||
9837c478bd9Sstevel@tonic-gate 		    (msgtyp > 0 && msgtyp == mp->msg_type) ||
9847c478bd9Sstevel@tonic-gate 		    (msgtyp < 0 && mp->msg_type <= -msgtyp)) {
9857c478bd9Sstevel@tonic-gate 			nmsg++;
9867c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE)
9877c478bd9Sstevel@tonic-gate 				size += RND(mp->msg_size);
9887c478bd9Sstevel@tonic-gate 			else
9897c478bd9Sstevel@tonic-gate 				size += RND32(mp->msg_size);
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	size += STRUCT_SIZE(head) + nmsg * STRUCT_SIZE(mhead);
9947c478bd9Sstevel@tonic-gate 	if (size > bufsz)
9957c478bd9Sstevel@tonic-gate 		nmsg = 0;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (nmsg > 0) {
9987c478bd9Sstevel@tonic-gate 		/*
9997c478bd9Sstevel@tonic-gate 		 * Mark the messages as being copied.
10007c478bd9Sstevel@tonic-gate 		 */
10017c478bd9Sstevel@tonic-gate 		snaplist = (struct msg **)kmem_alloc(nmsg *
10027c478bd9Sstevel@tonic-gate 		    sizeof (struct msg *), KM_SLEEP);
10037c478bd9Sstevel@tonic-gate 		i = 0;
10047c478bd9Sstevel@tonic-gate 		for (mp = list_head(&qp->msg_list); mp;
10057c478bd9Sstevel@tonic-gate 		    mp = list_next(&qp->msg_list, mp)) {
10067c478bd9Sstevel@tonic-gate 			if (msgtyp == 0 ||
10077c478bd9Sstevel@tonic-gate 			    (msgtyp > 0 && msgtyp == mp->msg_type) ||
10087c478bd9Sstevel@tonic-gate 			    (msgtyp < 0 && mp->msg_type <= -msgtyp)) {
10097c478bd9Sstevel@tonic-gate 				msg_hold(mp);
10107c478bd9Sstevel@tonic-gate 				snaplist[i] = mp;
10117c478bd9Sstevel@tonic-gate 				i++;
10127c478bd9Sstevel@tonic-gate 			}
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	/*
10187c478bd9Sstevel@tonic-gate 	 * Copy out the buffer header.
10197c478bd9Sstevel@tonic-gate 	 */
10207c478bd9Sstevel@tonic-gate 	STRUCT_FSET(head, msgsnap_size, size);
10217c478bd9Sstevel@tonic-gate 	STRUCT_FSET(head, msgsnap_nmsg, nmsg);
10227c478bd9Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(head), buf, STRUCT_SIZE(head)))
10237c478bd9Sstevel@tonic-gate 		error = EFAULT;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	buf += STRUCT_SIZE(head);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * Now copy out the messages one by one.
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	for (i = 0; i < nmsg; i++) {
10317c478bd9Sstevel@tonic-gate 		mp = snaplist[i];
10327c478bd9Sstevel@tonic-gate 		if (error == 0) {
10337c478bd9Sstevel@tonic-gate 			STRUCT_FSET(mhead, msgsnap_mlen, mp->msg_size);
10347c478bd9Sstevel@tonic-gate 			STRUCT_FSET(mhead, msgsnap_mtype, mp->msg_type);
10357c478bd9Sstevel@tonic-gate 			if (copyout(STRUCT_BUF(mhead), buf, STRUCT_SIZE(mhead)))
10367c478bd9Sstevel@tonic-gate 				error = EFAULT;
10377c478bd9Sstevel@tonic-gate 			buf += STRUCT_SIZE(mhead);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 			if (error == 0 &&
10407c478bd9Sstevel@tonic-gate 			    mp->msg_size != 0 &&
10417c478bd9Sstevel@tonic-gate 			    copyout(mp->msg_addr, buf, mp->msg_size))
10427c478bd9Sstevel@tonic-gate 				error = EFAULT;
10437c478bd9Sstevel@tonic-gate 			if (mdl == DATAMODEL_NATIVE)
10447c478bd9Sstevel@tonic-gate 				buf += RND(mp->msg_size);
10457c478bd9Sstevel@tonic-gate 			else
10467c478bd9Sstevel@tonic-gate 				buf += RND32(mp->msg_size);
10477c478bd9Sstevel@tonic-gate 		}
10487c478bd9Sstevel@tonic-gate 		lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
10497c478bd9Sstevel@tonic-gate 		msg_rele(mp);
10507c478bd9Sstevel@tonic-gate 		/* Check for msg q deleted or reallocated */
10517c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm))
10527c478bd9Sstevel@tonic-gate 			error = EIDRM;
10537c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	(void) ipc_lock(msq_svc, qp->msg_perm.ipc_id);
10577c478bd9Sstevel@tonic-gate 	ipc_rele(msq_svc, (kipc_perm_t *)qp);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (nmsg > 0)
10607c478bd9Sstevel@tonic-gate 		kmem_free(snaplist, nmsg * sizeof (struct msg *));
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if (error)
10637c478bd9Sstevel@tonic-gate 		return (set_errno(error));
10647c478bd9Sstevel@tonic-gate 	return (0);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
1067e50383f4Sdv142724 #define	MSG_PREALLOC_LIMIT 8192
1068e50383f4Sdv142724 
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate  * msgsnd system call.
10717c478bd9Sstevel@tonic-gate  */
10727c478bd9Sstevel@tonic-gate static int
10737c478bd9Sstevel@tonic-gate msgsnd(int msqid, struct ipcmsgbuf *msgp, size_t msgsz, int msgflg)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	kmsqid_t	*qp;
1076e50383f4Sdv142724 	kmutex_t	*lock = NULL;
10777c478bd9Sstevel@tonic-gate 	struct msg	*mp = NULL;
10787c478bd9Sstevel@tonic-gate 	long		type;
10797c478bd9Sstevel@tonic-gate 	int		error = 0;
10807c478bd9Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
10817c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(ipcmsgbuf, umsgp);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, msg, 1);	/* bump msg send/rcv count */
10847c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(umsgp, mdl, msgp);
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (mdl == DATAMODEL_NATIVE) {
10877c478bd9Sstevel@tonic-gate 		if (copyin(msgp, &type, sizeof (type)))
10887c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
10897c478bd9Sstevel@tonic-gate 	} else {
10907c478bd9Sstevel@tonic-gate 		int32_t	type32;
10917c478bd9Sstevel@tonic-gate 		if (copyin(msgp, &type32, sizeof (type32)))
10927c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
10937c478bd9Sstevel@tonic-gate 		type = type32;
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (type < 1)
10977c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
10987c478bd9Sstevel@tonic-gate 
1099e50383f4Sdv142724 	/*
1100e50383f4Sdv142724 	 * We want the value here large enough that most of the
1101e50383f4Sdv142724 	 * the message operations will use the "lockless" path,
1102e50383f4Sdv142724 	 * but small enough that a user can not reserve large
1103e50383f4Sdv142724 	 * chunks of kernel memory unless they have a valid
1104e50383f4Sdv142724 	 * reason to.
1105e50383f4Sdv142724 	 */
1106e50383f4Sdv142724 	if (msgsz <= MSG_PREALLOC_LIMIT) {
1107e50383f4Sdv142724 		/*
1108e50383f4Sdv142724 		 * We are small enough that we can afford to do the
1109e50383f4Sdv142724 		 * allocation now.  This saves dropping the lock
1110e50383f4Sdv142724 		 * and then reacquiring the lock.
1111e50383f4Sdv142724 		 */
1112e50383f4Sdv142724 		mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP);
1113e50383f4Sdv142724 		mp->msg_copycnt = 1;
1114e50383f4Sdv142724 		mp->msg_size = msgsz;
1115e50383f4Sdv142724 		if (msgsz) {
1116e50383f4Sdv142724 			mp->msg_addr = kmem_alloc(msgsz, KM_SLEEP);
1117e50383f4Sdv142724 			if (copyin(STRUCT_FADDR(umsgp, mtext),
1118e50383f4Sdv142724 			    mp->msg_addr, msgsz) == -1) {
1119e50383f4Sdv142724 				error = EFAULT;
1120e50383f4Sdv142724 				goto msgsnd_out;
1121e50383f4Sdv142724 			}
1122e50383f4Sdv142724 		}
1123e50383f4Sdv142724 	}
1124e50383f4Sdv142724 
1125e50383f4Sdv142724 	if ((lock = ipc_lookup(msq_svc, msqid, (kipc_perm_t **)&qp)) == NULL) {
1126e50383f4Sdv142724 		error = EINVAL;
1127e50383f4Sdv142724 		goto msgsnd_out;
1128e50383f4Sdv142724 	}
1129e50383f4Sdv142724 
11307c478bd9Sstevel@tonic-gate 	ipc_hold(msq_svc, (kipc_perm_t *)qp);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	if (msgsz > qp->msg_qbytes) {
11337c478bd9Sstevel@tonic-gate 		error = EINVAL;
11347c478bd9Sstevel@tonic-gate 		goto msgsnd_out;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (error = ipcperm_access(&qp->msg_perm, MSG_W, CRED()))
11387c478bd9Sstevel@tonic-gate 		goto msgsnd_out;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate top:
11417c478bd9Sstevel@tonic-gate 	/*
11427c478bd9Sstevel@tonic-gate 	 * Allocate space on q, message header, & buffer space.
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	ASSERT(qp->msg_qnum <= qp->msg_qmax);
11457c478bd9Sstevel@tonic-gate 	while ((msgsz > qp->msg_qbytes - qp->msg_cbytes) ||
11467c478bd9Sstevel@tonic-gate 	    (qp->msg_qnum == qp->msg_qmax)) {
11477c478bd9Sstevel@tonic-gate 		int cvres;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 		if (msgflg & IPC_NOWAIT) {
11507c478bd9Sstevel@tonic-gate 			error = EAGAIN;
11517c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
11527c478bd9Sstevel@tonic-gate 		}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		qp->msg_snd_cnt++;
11557c478bd9Sstevel@tonic-gate 		cvres = cv_wait_sig(&qp->msg_snd_cv, lock);
11567c478bd9Sstevel@tonic-gate 		lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, lock);
11577c478bd9Sstevel@tonic-gate 		qp->msg_snd_cnt--;
11587c478bd9Sstevel@tonic-gate 
11592c5b6df1Sdv142724 		if (error = msgq_check_err(qp, cvres)) {
11607c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
11617c478bd9Sstevel@tonic-gate 		}
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
11657c478bd9Sstevel@tonic-gate 		int failure;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
1168e50383f4Sdv142724 		ASSERT(msgsz > 0);
11697c478bd9Sstevel@tonic-gate 		mp = kmem_zalloc(sizeof (struct msg), KM_SLEEP);
1170e50383f4Sdv142724 		mp->msg_addr = kmem_alloc(msgsz, KM_SLEEP);
11717c478bd9Sstevel@tonic-gate 		mp->msg_size = msgsz;
11727c478bd9Sstevel@tonic-gate 		mp->msg_copycnt = 1;
11737c478bd9Sstevel@tonic-gate 
1174e50383f4Sdv142724 		failure = (copyin(STRUCT_FADDR(umsgp, mtext),
11757c478bd9Sstevel@tonic-gate 		    mp->msg_addr, msgsz) == -1);
11767c478bd9Sstevel@tonic-gate 		lock = ipc_lock(msq_svc, qp->msg_perm.ipc_id);
11777c478bd9Sstevel@tonic-gate 		if (IPC_FREE(&qp->msg_perm)) {
11787c478bd9Sstevel@tonic-gate 			error = EIDRM;
11797c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 		if (failure) {
11827c478bd9Sstevel@tonic-gate 			error = EFAULT;
11837c478bd9Sstevel@tonic-gate 			goto msgsnd_out;
11847c478bd9Sstevel@tonic-gate 		}
11857c478bd9Sstevel@tonic-gate 		goto top;
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/*
11897c478bd9Sstevel@tonic-gate 	 * Everything is available, put msg on q.
11907c478bd9Sstevel@tonic-gate 	 */
11917c478bd9Sstevel@tonic-gate 	qp->msg_qnum++;
11927c478bd9Sstevel@tonic-gate 	qp->msg_cbytes += msgsz;
11937c478bd9Sstevel@tonic-gate 	qp->msg_lspid = curproc->p_pid;
11947c478bd9Sstevel@tonic-gate 	qp->msg_stime = gethrestime_sec();
11957c478bd9Sstevel@tonic-gate 	mp->msg_type = type;
11962c5b6df1Sdv142724 	if (qp->msg_lowest_type > type)
11972c5b6df1Sdv142724 		qp->msg_lowest_type = type;
11987c478bd9Sstevel@tonic-gate 	list_insert_tail(&qp->msg_list, mp);
1199b2eb1770Sudpa 	/*
12002c5b6df1Sdv142724 	 * Get the proper receiver going.
1201b2eb1770Sudpa 	 */
12022c5b6df1Sdv142724 	msg_wakeup_rdr(qp, &qp->msg_fnd_sndr, type);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate msgsnd_out:
1205e50383f4Sdv142724 	if (lock)
12067c478bd9Sstevel@tonic-gate 		ipc_rele(msq_svc, (kipc_perm_t *)qp);	/* drops lock */
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	if (error) {
12097c478bd9Sstevel@tonic-gate 		if (mp)
12107c478bd9Sstevel@tonic-gate 			msg_rele(mp);
12117c478bd9Sstevel@tonic-gate 		return (set_errno(error));
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	return (0);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12172c5b6df1Sdv142724 static void
12182c5b6df1Sdv142724 msg_wakeup_rdr(kmsqid_t *qp, msg_select_t **flist, long type)
12192c5b6df1Sdv142724 {
12202c5b6df1Sdv142724 	msg_select_t	*walker = *flist;
12212c5b6df1Sdv142724 	msgq_wakeup_t	*wakeup;
1222*e5994f96Sdv142724 	uint_t		msg_hash;
12232c5b6df1Sdv142724 
12242c5b6df1Sdv142724 	msg_hash = msg_type_hash(type);
12252c5b6df1Sdv142724 
12262c5b6df1Sdv142724 	do {
12272c5b6df1Sdv142724 		wakeup = walker->selection(qp, msg_hash, type);
12282c5b6df1Sdv142724 		walker = walker->next_selection;
12292c5b6df1Sdv142724 	} while (!wakeup && walker != *flist);
12302c5b6df1Sdv142724 
12312c5b6df1Sdv142724 	*flist = (*flist)->next_selection;
12322c5b6df1Sdv142724 	if (wakeup) {
12332c5b6df1Sdv142724 		if (type) {
12342c5b6df1Sdv142724 			wakeup->msgw_snd_wake = type;
12352c5b6df1Sdv142724 		}
12362c5b6df1Sdv142724 		cv_signal(&wakeup->msgw_wake_cv);
12372c5b6df1Sdv142724 	}
12382c5b6df1Sdv142724 }
12392c5b6df1Sdv142724 
1240*e5994f96Sdv142724 static uint_t
12412c5b6df1Sdv142724 msg_type_hash(long msg_type)
12422c5b6df1Sdv142724 {
12432c5b6df1Sdv142724 	if (msg_type < 0) {
1244*e5994f96Sdv142724 		long	hash = -msg_type / MSG_NEG_INTERVAL;
12452c5b6df1Sdv142724 		/*
12462c5b6df1Sdv142724 		 * Negative message types are hashed over an
12472c5b6df1Sdv142724 		 * interval.  Any message type that hashes
12482c5b6df1Sdv142724 		 * beyond MSG_MAX_QNUM is automatically placed
12492c5b6df1Sdv142724 		 * in the last bucket.
12502c5b6df1Sdv142724 		 */
1251*e5994f96Sdv142724 		if (hash > MSG_MAX_QNUM)
12522c5b6df1Sdv142724 			hash = MSG_MAX_QNUM;
12532c5b6df1Sdv142724 		return (hash);
12542c5b6df1Sdv142724 	}
12552c5b6df1Sdv142724 
12562c5b6df1Sdv142724 	/*
12572c5b6df1Sdv142724 	 * 0 or positive message type.  The first bucket is reserved for
12582c5b6df1Sdv142724 	 * message receivers of type 0, the other buckets we hash into.
12592c5b6df1Sdv142724 	 */
1260*e5994f96Sdv142724 	if (msg_type)
1261*e5994f96Sdv142724 		return (1 + (msg_type % MSG_MAX_QNUM));
12622c5b6df1Sdv142724 	return (0);
12632c5b6df1Sdv142724 }
12642c5b6df1Sdv142724 
12652c5b6df1Sdv142724 /*
12662c5b6df1Sdv142724  * Routines to see if we have a receiver of type 0 either blocked waiting
12672c5b6df1Sdv142724  * for a message.  Simply return the first guy on the list.
12682c5b6df1Sdv142724  */
12692c5b6df1Sdv142724 
12702c5b6df1Sdv142724 static msgq_wakeup_t *
1271*e5994f96Sdv142724 /* ARGSUSED */
12722c5b6df1Sdv142724 msg_fnd_any_snd(kmsqid_t *qp, int msg_hash, long type)
12732c5b6df1Sdv142724 {
1274*e5994f96Sdv142724 	msgq_wakeup_t	*walker;
1275*e5994f96Sdv142724 
1276*e5994f96Sdv142724 	walker = list_head(&qp->msg_wait_snd[0]);
1277*e5994f96Sdv142724 
1278*e5994f96Sdv142724 	if (walker)
1279*e5994f96Sdv142724 		list_remove(&qp->msg_wait_snd[0], walker);
1280*e5994f96Sdv142724 	return (walker);
12812c5b6df1Sdv142724 }
12822c5b6df1Sdv142724 
12832c5b6df1Sdv142724 static msgq_wakeup_t *
1284*e5994f96Sdv142724 /* ARGSUSED */
12852c5b6df1Sdv142724 msg_fnd_any_rdr(kmsqid_t *qp, int msg_hash, long type)
12862c5b6df1Sdv142724 {
1287*e5994f96Sdv142724 	msgq_wakeup_t	*walker;
1288*e5994f96Sdv142724 
1289*e5994f96Sdv142724 	walker = list_head(&qp->msg_cpy_block);
1290*e5994f96Sdv142724 	if (walker)
1291*e5994f96Sdv142724 		list_remove(&qp->msg_cpy_block, walker);
1292*e5994f96Sdv142724 	return (walker);
12932c5b6df1Sdv142724 }
12942c5b6df1Sdv142724 
12952c5b6df1Sdv142724 static msgq_wakeup_t *
12962c5b6df1Sdv142724 msg_fnd_spc_snd(kmsqid_t *qp, int msg_hash, long type)
12972c5b6df1Sdv142724 {
12982c5b6df1Sdv142724 	msgq_wakeup_t	*walker;
12992c5b6df1Sdv142724 
13002c5b6df1Sdv142724 	walker = list_head(&qp->msg_wait_snd[msg_hash]);
13012c5b6df1Sdv142724 
1302*e5994f96Sdv142724 	while (walker && walker->msgw_type != type)
1303*e5994f96Sdv142724 		walker = list_next(&qp->msg_wait_snd[msg_hash], walker);
1304*e5994f96Sdv142724 	if (walker)
1305*e5994f96Sdv142724 		list_remove(&qp->msg_wait_snd[msg_hash], walker);
13062c5b6df1Sdv142724 	return (walker);
13072c5b6df1Sdv142724 }
13082c5b6df1Sdv142724 
1309*e5994f96Sdv142724 /* ARGSUSED */
13102c5b6df1Sdv142724 static msgq_wakeup_t *
13112c5b6df1Sdv142724 msg_fnd_neg_snd(kmsqid_t *qp, int msg_hash, long type)
13122c5b6df1Sdv142724 {
13132c5b6df1Sdv142724 	msgq_wakeup_t	*qptr;
13142c5b6df1Sdv142724 	int		count;
13152c5b6df1Sdv142724 	int		check_index;
13162c5b6df1Sdv142724 	int		neg_index;
13172c5b6df1Sdv142724 	int		nbuckets;
13182c5b6df1Sdv142724 
13192c5b6df1Sdv142724 	if (!qp->msg_ngt_cnt) {
13202c5b6df1Sdv142724 		return (NULL);
13212c5b6df1Sdv142724 	}
13222c5b6df1Sdv142724 	neg_index = msg_type_hash(-type);
13232c5b6df1Sdv142724 
13242c5b6df1Sdv142724 	/*
13252c5b6df1Sdv142724 	 * Check for a match among the negative type queues.  Any buckets
13262c5b6df1Sdv142724 	 * at neg_index or larger can match the type.  Use the last send
13272c5b6df1Sdv142724 	 * time to randomize the starting bucket to prevent starvation.
13282c5b6df1Sdv142724 	 * Search all buckets from neg_index to MSG_MAX_QNUM, starting
13292c5b6df1Sdv142724 	 * from the random starting point, and wrapping around after
13302c5b6df1Sdv142724 	 * MSG_MAX_QNUM.
13312c5b6df1Sdv142724 	 */
13322c5b6df1Sdv142724 
13332c5b6df1Sdv142724 	nbuckets = MSG_MAX_QNUM - neg_index + 1;
13342c5b6df1Sdv142724 	check_index = neg_index + (qp->msg_stime % nbuckets);
13352c5b6df1Sdv142724 
13362c5b6df1Sdv142724 	for (count = nbuckets; count > 0; count--) {
13372c5b6df1Sdv142724 		qptr = list_head(&qp->msg_wait_snd_ngt[check_index]);
13382c5b6df1Sdv142724 		while (qptr) {
13392c5b6df1Sdv142724 			/*
13402c5b6df1Sdv142724 			 * The lowest hash bucket may actually contain
13412c5b6df1Sdv142724 			 * message types that are not valid for this
13422c5b6df1Sdv142724 			 * request.  This can happen due to the fact that
13432c5b6df1Sdv142724 			 * the message buckets actually contain a consecutive
13442c5b6df1Sdv142724 			 * range of types.
13452c5b6df1Sdv142724 			 */
13462c5b6df1Sdv142724 			if (-qptr->msgw_type >= type) {
1347*e5994f96Sdv142724 				list_remove(&qp->msg_wait_snd_ngt[check_index],
1348*e5994f96Sdv142724 				    qptr);
13492c5b6df1Sdv142724 				return (qptr);
13502c5b6df1Sdv142724 			}
1351*e5994f96Sdv142724 			qptr = list_next(&qp->msg_wait_snd_ngt[check_index],
1352*e5994f96Sdv142724 			    qptr);
13532c5b6df1Sdv142724 		}
13542c5b6df1Sdv142724 		if (++check_index > MSG_MAX_QNUM) {
13552c5b6df1Sdv142724 			check_index = neg_index;
13562c5b6df1Sdv142724 		}
13572c5b6df1Sdv142724 	}
13582c5b6df1Sdv142724 	return (NULL);
13592c5b6df1Sdv142724 }
13602c5b6df1Sdv142724 
13612c5b6df1Sdv142724 static int
13622c5b6df1Sdv142724 msg_rcvq_sleep(list_t *queue, msgq_wakeup_t *entry, kmutex_t **lock,
13632c5b6df1Sdv142724     kmsqid_t *qp)
13642c5b6df1Sdv142724 {
13652c5b6df1Sdv142724 	int		cvres;
13662c5b6df1Sdv142724 
13672c5b6df1Sdv142724 	cv_init(&entry->msgw_wake_cv, NULL, 0, NULL);
13682c5b6df1Sdv142724 
13692c5b6df1Sdv142724 	list_insert_tail(queue, entry);
13702c5b6df1Sdv142724 
13712c5b6df1Sdv142724 	qp->msg_rcv_cnt++;
13722c5b6df1Sdv142724 	cvres = cv_wait_sig(&entry->msgw_wake_cv, *lock);
13732c5b6df1Sdv142724 	*lock = ipc_relock(msq_svc, qp->msg_perm.ipc_id, *lock);
13742c5b6df1Sdv142724 	qp->msg_rcv_cnt--;
1375*e5994f96Sdv142724 
1376*e5994f96Sdv142724 	if (list_link_active(&entry->msgw_list)) {
13772c5b6df1Sdv142724 		/*
1378*e5994f96Sdv142724 		 * We woke up unexpectedly, remove ourself.
13792c5b6df1Sdv142724 		 */
13802c5b6df1Sdv142724 		list_remove(queue, entry);
1381*e5994f96Sdv142724 	}
13822c5b6df1Sdv142724 
13832c5b6df1Sdv142724 	return (cvres);
13842c5b6df1Sdv142724 }
13852c5b6df1Sdv142724 
13862c5b6df1Sdv142724 static void
13872c5b6df1Sdv142724 msg_rcvq_wakeup_all(list_t *q_ptr)
13882c5b6df1Sdv142724 {
13892c5b6df1Sdv142724 	msgq_wakeup_t	*q_walk;
13902c5b6df1Sdv142724 
1391*e5994f96Sdv142724 	while (q_walk = list_head(q_ptr)) {
1392*e5994f96Sdv142724 		list_remove(q_ptr, q_walk);
13932c5b6df1Sdv142724 		cv_signal(&q_walk->msgw_wake_cv);
13942c5b6df1Sdv142724 	}
13952c5b6df1Sdv142724 }
13962c5b6df1Sdv142724 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * msgsys - System entry point for msgctl, msgget, msgrcv, and msgsnd
13997c478bd9Sstevel@tonic-gate  * system calls.
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate static ssize_t
14027c478bd9Sstevel@tonic-gate msgsys(int opcode, uintptr_t a1, uintptr_t a2, uintptr_t a3,
14037c478bd9Sstevel@tonic-gate 	uintptr_t a4, uintptr_t a5)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	ssize_t error;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	switch (opcode) {
14087c478bd9Sstevel@tonic-gate 	case MSGGET:
14097c478bd9Sstevel@tonic-gate 		error = msgget((key_t)a1, (int)a2);
14107c478bd9Sstevel@tonic-gate 		break;
14117c478bd9Sstevel@tonic-gate 	case MSGCTL:
14127c478bd9Sstevel@tonic-gate 		error = msgctl((int)a1, (int)a2, (void *)a3);
14137c478bd9Sstevel@tonic-gate 		break;
14147c478bd9Sstevel@tonic-gate 	case MSGRCV:
14157c478bd9Sstevel@tonic-gate 		error = msgrcv((int)a1, (struct ipcmsgbuf *)a2,
14167c478bd9Sstevel@tonic-gate 		    (size_t)a3, (long)a4, (int)a5);
14177c478bd9Sstevel@tonic-gate 		break;
14187c478bd9Sstevel@tonic-gate 	case MSGSND:
14197c478bd9Sstevel@tonic-gate 		error = msgsnd((int)a1, (struct ipcmsgbuf *)a2,
14207c478bd9Sstevel@tonic-gate 		    (size_t)a3, (int)a4);
14217c478bd9Sstevel@tonic-gate 		break;
14227c478bd9Sstevel@tonic-gate 	case MSGIDS:
14237c478bd9Sstevel@tonic-gate 		error = msgids((int *)a1, (uint_t)a2, (uint_t *)a3);
14247c478bd9Sstevel@tonic-gate 		break;
14257c478bd9Sstevel@tonic-gate 	case MSGSNAP:
14267c478bd9Sstevel@tonic-gate 		error = msgsnap((int)a1, (caddr_t)a2, (size_t)a3, (long)a4);
14277c478bd9Sstevel@tonic-gate 		break;
14287c478bd9Sstevel@tonic-gate 	default:
14297c478bd9Sstevel@tonic-gate 		error = set_errno(EINVAL);
14307c478bd9Sstevel@tonic-gate 		break;
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	return (error);
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
14377c478bd9Sstevel@tonic-gate /*
14387c478bd9Sstevel@tonic-gate  * msgsys32 - System entry point for msgctl, msgget, msgrcv, and msgsnd
14397c478bd9Sstevel@tonic-gate  * system calls for 32-bit callers on LP64 kernel.
14407c478bd9Sstevel@tonic-gate  */
14417c478bd9Sstevel@tonic-gate static ssize32_t
14427c478bd9Sstevel@tonic-gate msgsys32(int opcode, uint32_t a1, uint32_t a2, uint32_t a3,
14437c478bd9Sstevel@tonic-gate 	uint32_t a4, uint32_t a5)
14447c478bd9Sstevel@tonic-gate {
14457c478bd9Sstevel@tonic-gate 	ssize_t error;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	switch (opcode) {
14487c478bd9Sstevel@tonic-gate 	case MSGGET:
14497c478bd9Sstevel@tonic-gate 		error = msgget((key_t)a1, (int)a2);
14507c478bd9Sstevel@tonic-gate 		break;
14517c478bd9Sstevel@tonic-gate 	case MSGCTL:
14527c478bd9Sstevel@tonic-gate 		error = msgctl((int)a1, (int)a2, (void *)(uintptr_t)a3);
14537c478bd9Sstevel@tonic-gate 		break;
14547c478bd9Sstevel@tonic-gate 	case MSGRCV:
14557c478bd9Sstevel@tonic-gate 		error = msgrcv((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2,
14567c478bd9Sstevel@tonic-gate 		    (size_t)a3, (long)(int32_t)a4, (int)a5);
14577c478bd9Sstevel@tonic-gate 		break;
14587c478bd9Sstevel@tonic-gate 	case MSGSND:
14597c478bd9Sstevel@tonic-gate 		error = msgsnd((int)a1, (struct ipcmsgbuf *)(uintptr_t)a2,
14607c478bd9Sstevel@tonic-gate 		    (size_t)(int32_t)a3, (int)a4);
14617c478bd9Sstevel@tonic-gate 		break;
14627c478bd9Sstevel@tonic-gate 	case MSGIDS:
14637c478bd9Sstevel@tonic-gate 		error = msgids((int *)(uintptr_t)a1, (uint_t)a2,
14647c478bd9Sstevel@tonic-gate 		    (uint_t *)(uintptr_t)a3);
14657c478bd9Sstevel@tonic-gate 		break;
14667c478bd9Sstevel@tonic-gate 	case MSGSNAP:
14677c478bd9Sstevel@tonic-gate 		error = msgsnap((int)a1, (caddr_t)(uintptr_t)a2, (size_t)a3,
14687c478bd9Sstevel@tonic-gate 		    (long)(int32_t)a4);
14697c478bd9Sstevel@tonic-gate 		break;
14707c478bd9Sstevel@tonic-gate 	default:
14717c478bd9Sstevel@tonic-gate 		error = set_errno(EINVAL);
14727c478bd9Sstevel@tonic-gate 		break;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	return (error);
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate #endif	/* SYSCALL32_IMPL */
1478