]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_pset.c
xnu-201.42.3.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_pset.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52/*
53 * File: ipc/ipc_pset.c
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Functions to manipulate IPC port sets.
58 */
59
60#include <mach/port.h>
61#include <mach/kern_return.h>
62#include <mach/message.h>
63#include <ipc/ipc_mqueue.h>
64#include <ipc/ipc_object.h>
65#include <ipc/ipc_pset.h>
66#include <ipc/ipc_right.h>
67#include <ipc/ipc_space.h>
68#include <ipc/ipc_port.h>
69#include <ipc/ipc_print.h>
70#include <kern/thread_pool.h>
71
72/*
73 * Routine: ipc_pset_alloc
74 * Purpose:
75 * Allocate a port set.
76 * Conditions:
77 * Nothing locked. If successful, the port set is returned
78 * locked. (The caller doesn't have a reference.)
79 * Returns:
80 * KERN_SUCCESS The port set is allocated.
81 * KERN_INVALID_TASK The space is dead.
82 * KERN_NO_SPACE No room for an entry in the space.
83 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
84 */
85
86kern_return_t
87ipc_pset_alloc(
88 ipc_space_t space,
89 mach_port_name_t *namep,
90 ipc_pset_t *psetp)
91{
92 ipc_pset_t pset;
93 mach_port_name_t name;
94 kern_return_t kr;
95
96 kr = ipc_object_alloc(space, IOT_PORT_SET,
97 MACH_PORT_TYPE_PORT_SET, 0,
98 &name, (ipc_object_t *) &pset);
99 if (kr != KERN_SUCCESS)
100 return kr;
101 /* pset is locked */
102
103 pset->ips_local_name = name;
104 pset->ips_pset_self = pset;
105 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
106 thread_pool_init(&pset->ips_thread_pool);
107
108 *namep = name;
109 *psetp = pset;
110 return KERN_SUCCESS;
111}
112
113/*
114 * Routine: ipc_pset_alloc_name
115 * Purpose:
116 * Allocate a port set, with a specific name.
117 * Conditions:
118 * Nothing locked. If successful, the port set is returned
119 * locked. (The caller doesn't have a reference.)
120 * Returns:
121 * KERN_SUCCESS The port set is allocated.
122 * KERN_INVALID_TASK The space is dead.
123 * KERN_NAME_EXISTS The name already denotes a right.
124 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
125 */
126
127kern_return_t
128ipc_pset_alloc_name(
129 ipc_space_t space,
130 mach_port_name_t name,
131 ipc_pset_t *psetp)
132{
133 ipc_pset_t pset;
134 kern_return_t kr;
135
136
137 kr = ipc_object_alloc_name(space, IOT_PORT_SET,
138 MACH_PORT_TYPE_PORT_SET, 0,
139 name, (ipc_object_t *) &pset);
140 if (kr != KERN_SUCCESS)
141 return kr;
142 /* pset is locked */
143
144 pset->ips_local_name = name;
145 pset->ips_pset_self = pset;
146 ipc_mqueue_init(&pset->ips_messages, TRUE /* set */);
147 thread_pool_init(&pset->ips_thread_pool);
148
149 *psetp = pset;
150 return KERN_SUCCESS;
151}
152
153/*
154 * Routine: ipc_pset_member
155 * Purpose:
156 * Checks to see if a port is a member of a pset
157 * Conditions:
158 * Both port and port set are locked.
159 * The port must be active.
160 */
161boolean_t
162ipc_pset_member(
163 ipc_pset_t pset,
164 ipc_port_t port)
165{
166 assert(ip_active(port));
167
168 return (ipc_mqueue_member(&port->ip_messages, &pset->ips_messages));
169}
170
171
172/*
173 * Routine: ipc_pset_add
174 * Purpose:
175 * Puts a port into a port set.
176 * The port set gains a reference.
177 * Conditions:
178 * Both port and port set are locked and active.
179 * The owner of the port set is also receiver for the port.
180 */
181
182kern_return_t
183ipc_pset_add(
184 ipc_pset_t pset,
185 ipc_port_t port)
186{
187 assert(ips_active(pset));
188 assert(ip_active(port));
189
190 if (ipc_pset_member(pset, port))
191 return KERN_ALREADY_IN_SET;
192
193 ips_reference(pset);
194 port->ip_pset_count++;
195
196 ipc_mqueue_add(&port->ip_messages, &pset->ips_messages);
197
198 return KERN_SUCCESS;
199}
200
201
202
203/*
204 * Routine: ipc_pset_remove
205 * Purpose:
206 * Removes a port from a port set.
207 * The port set loses a reference.
208 * Conditions:
209 * Both port and port set are locked.
210 * The port must be active.
211 */
212
213kern_return_t
214ipc_pset_remove(
215 ipc_pset_t pset,
216 ipc_port_t port)
217{
218 mach_msg_return_t mr;
219
220 assert(ip_active(port));
221
222 if (port->ip_pset_count == 0)
223 return KERN_NOT_IN_SET;
224
225 mr = ipc_mqueue_remove(&port->ip_messages, &pset->ips_messages);
226
227 if (mr == MACH_MSG_SUCCESS) {
228 port->ip_pset_count--;
229 ips_release(pset);
230 }
231 return mr;
232}
233
234/*
235 * Routine: ipc_pset_remove_all
236 * Purpose:
237 * Removes a port from all it's port sets.
238 * Each port set loses a reference.
239 * Conditions:
240 * port is locked and active.
241 */
242
243kern_return_t
244ipc_pset_remove_all(
245 ipc_port_t port)
246{
247 ipc_pset_mqueue_t pset_mqueue;
248 ipc_pset_t pset;
249
250 assert(ip_active(port));
251
252 if (port->ip_pset_count == 0)
253 return KERN_NOT_IN_SET;
254
255 /*
256 * Remove each port set's mqueue from the port's (one at a time).
257 */
258 while (port->ip_pset_count > 0) {
259 ipc_mqueue_remove_one(&port->ip_messages,
260 (ipc_mqueue_t)&pset_mqueue);
261 assert(pset_mqueue != (ipc_pset_mqueue_t)0);
262 port->ip_pset_count--;
263
264 pset = pset_mqueue->ipsm_pset;
265 ipc_pset_release(pset); /* locks and unlocks pset */
266
267 }
268
269 assert(port->ip_pset_count == 0);
270 return KERN_SUCCESS;
271}
272
273
274/*
275 * Routine: ipc_pset_destroy
276 * Purpose:
277 * Destroys a port_set.
278 *
279 * Doesn't remove members from the port set;
280 * that happens lazily.
281 * Conditions:
282 * The port_set is locked and alive.
283 * The caller has a reference, which is consumed.
284 * Afterwards, the port_set is unlocked and dead.
285 */
286
287void
288ipc_pset_destroy(
289 ipc_pset_t pset)
290{
291 spl_t s;
292
293 assert(ips_active(pset));
294
295 pset->ips_object.io_bits &= ~IO_BITS_ACTIVE;
296
297 s = splsched();
298 imq_lock(&pset->ips_messages);
299 ipc_mqueue_changed(&pset->ips_messages);
300 imq_unlock(&pset->ips_messages);
301 splx(s);
302
303 /* XXXX Perhaps ought to verify ips_thread_pool is empty */
304
305 ips_release(pset); /* consume the ref our caller gave us */
306 ips_check_unlock(pset);
307}
308
309#include <mach_kdb.h>
310#if MACH_KDB
311
312#include <ddb/db_output.h>
313
314#define printf kdbprintf
315
316int
317ipc_list_count(
318 struct ipc_kmsg *base)
319{
320 register int count = 0;
321
322 if (base) {
323 struct ipc_kmsg *kmsg = base;
324
325 ++count;
326 while (kmsg && kmsg->ikm_next != base
327 && kmsg->ikm_next != IKM_BOGUS){
328 kmsg = kmsg->ikm_next;
329 ++count;
330 }
331 }
332 return(count);
333}
334
335/*
336 * Routine: ipc_pset_print
337 * Purpose:
338 * Pretty-print a port set for kdb.
339 */
340
341void
342ipc_pset_print(
343 ipc_pset_t pset)
344{
345 extern int db_indent;
346
347 printf("pset 0x%x\n", pset);
348
349 db_indent += 2;
350
351 ipc_object_print(&pset->ips_object);
352 iprintf("local_name = 0x%x\n", pset->ips_local_name);
353 iprintf("%d kmsgs => 0x%x",
354 ipc_list_count(pset->ips_messages.imq_messages.ikmq_base),
355 pset->ips_messages.imq_messages.ikmq_base);
356 printf(",rcvrs queue= 0x%x\n", &pset->ips_messages.imq_wait_queue);
357
358 db_indent -=2;
359}
360
361#endif /* MACH_KDB */