summaryrefslogtreecommitdiff
path: root/ipc/ipc_marequest.c
blob: 540382af303fcc2e0276bd545481aa84155e3128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Mach Operating System
 * Copyright (c) 1991,1990,1989 Carnegie Mellon University.
 * Copyright (c) 1993,1994 The University of Utah and
 * the Computer Systems Laboratory (CSL).
 * All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF
 * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
 * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
 * THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 *	File:	ipc/ipc_marequest.c
 *	Author:	Rich Draves
 *	Date:	1989
 *
 *	Functions to handle msg-accepted requests.
 */

#include <mach/message.h>
#include <mach/port.h>
#include <kern/lock.h>
#include <kern/mach_param.h>
#include <kern/kalloc.h>
#include <kern/zalloc.h>
#include <ipc/port.h>
#include <ipc/ipc_init.h>
#include <ipc/ipc_space.h>
#include <ipc/ipc_entry.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_right.h>
#include <ipc/ipc_marequest.h>
#include <ipc/ipc_notify.h>

#if	MACH_IPC_DEBUG
#include <mach/kern_return.h>
#include <mach_debug/hash_info.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <vm/vm_user.h>
#endif


zone_t ipc_marequest_zone;
int ipc_marequest_max = IMAR_MAX;

#define	imar_alloc()		((ipc_marequest_t) zalloc(ipc_marequest_zone))
#define	imar_free(imar)		zfree(ipc_marequest_zone, (vm_offset_t) (imar))

typedef unsigned int ipc_marequest_index_t;

ipc_marequest_index_t ipc_marequest_size;
ipc_marequest_index_t ipc_marequest_mask;

#define	IMAR_HASH(space, name)						\
		((((ipc_marequest_index_t)((vm_offset_t)space) >> 4) +	\
		  MACH_PORT_INDEX(name) + MACH_PORT_NGEN(name)) &	\
		 ipc_marequest_mask)

typedef struct ipc_marequest_bucket {
	decl_simple_lock_data(, imarb_lock_data)
	ipc_marequest_t imarb_head;
} *ipc_marequest_bucket_t;

#define	IMARB_NULL	((ipc_marequest_bucket_t) 0)

#define	imarb_lock_init(imarb)	simple_lock_init(&(imarb)->imarb_lock_data)
#define	imarb_lock(imarb)	simple_lock(&(imarb)->imarb_lock_data)
#define	imarb_unlock(imarb)	simple_unlock(&(imarb)->imarb_lock_data)

ipc_marequest_bucket_t ipc_marequest_table;



/*
 *	Routine:	ipc_marequest_init
 *	Purpose:
 *		Initialize the msg-accepted request module.
 */

void
ipc_marequest_init(void)
{
	ipc_marequest_index_t i;

	/* if not configured, initialize ipc_marequest_size */

	if (ipc_marequest_size == 0) {
		ipc_marequest_size = ipc_marequest_max >> 8;
		if (ipc_marequest_size < 16)
			ipc_marequest_size = 16;
	}

	/* make sure it is a power of two */

	ipc_marequest_mask = ipc_marequest_size - 1;
	if ((ipc_marequest_size & ipc_marequest_mask) != 0) {
		unsigned int bit;

		/* round up to closest power of two */

		for (bit = 1;; bit <<= 1) {
			ipc_marequest_mask |= bit;
			ipc_marequest_size = ipc_marequest_mask + 1;

			if ((ipc_marequest_size & ipc_marequest_mask) == 0)
				break;
		}
	}

	/* allocate ipc_marequest_table */

	ipc_marequest_table = (ipc_marequest_bucket_t)
		kalloc((vm_size_t) (ipc_marequest_size *
				    sizeof(struct ipc_marequest_bucket)));
	assert(ipc_marequest_table != IMARB_NULL);

	/* and initialize it */

	for (i = 0; i < ipc_marequest_size; i++) {
		ipc_marequest_bucket_t bucket;

		bucket = &ipc_marequest_table[i];
		imarb_lock_init(bucket);
		bucket->imarb_head = IMAR_NULL;
	}

	ipc_marequest_zone =
		zinit(sizeof(struct ipc_marequest), 0,
		      ipc_marequest_max * sizeof(struct ipc_marequest),
		      sizeof(struct ipc_marequest),
		      IPC_ZONE_TYPE, "ipc msg-accepted requests");
}

/*
 *	Routine:	ipc_marequest_create
 *	Purpose:
 *		Create a msg-accepted request, because
 *		a sender is forcing a message with MACH_SEND_NOTIFY.
 *
 *		The "notify" argument should name a receive right
 *		that is used to create the send-once notify port.
 *	Conditions:
 *		Nothing locked; refs held for space and port.
 *	Returns:
 *		MACH_MSG_SUCCESS	Msg-accepted request created.
 *		MACH_SEND_INVALID_NOTIFY	The space is dead.
 *		MACH_SEND_INVALID_NOTIFY	The notify port is bad.
 *		MACH_SEND_NOTIFY_IN_PROGRESS
 *			This space has already forced a message to this port.
 *		MACH_SEND_NO_NOTIFY	Can't allocate a msg-accepted request.
 */

mach_msg_return_t
ipc_marequest_create(space, port, notify, marequestp)
	ipc_space_t space;
	ipc_port_t port;
	mach_port_t notify;
	ipc_marequest_t *marequestp;
{
	mach_port_t name;
	ipc_entry_t entry;
	ipc_port_t soright;
	ipc_marequest_t marequest;
	ipc_marequest_bucket_t bucket;

	marequest = imar_alloc();
	if (marequest == IMAR_NULL)
		return MACH_SEND_NO_NOTIFY;

	/*
	 *	Delay creating the send-once right until
	 *	we know there will be no errors.  Otherwise,
	 *	we would have to worry about disposing of it
	 *	when it turned out it wasn't needed.
	 */

	is_write_lock(space);
	if (!space->is_active) {
		is_write_unlock(space);
		imar_free(marequest);
		return MACH_SEND_INVALID_NOTIFY;
	}

	if (ipc_right_reverse(space, (ipc_object_t) port, &name, &entry)) {
		ipc_entry_bits_t bits;

		/* port is locked and active */
		ip_unlock(port);
		bits = entry->ie_bits;

		assert(port == (ipc_port_t) entry->ie_object);
		assert(bits & MACH_PORT_TYPE_SEND_RECEIVE);

		if (bits & IE_BITS_MAREQUEST) {
			is_write_unlock(space);
			imar_free(marequest);
			return MACH_SEND_NOTIFY_IN_PROGRESS;
		}

		if ((soright = ipc_port_lookup_notify(space, notify))
								== IP_NULL) {
			is_write_unlock(space);
			imar_free(marequest);
			return MACH_SEND_INVALID_NOTIFY;
		}

		entry->ie_bits = bits | IE_BITS_MAREQUEST;

		is_reference(space);
		marequest->imar_space = space;
		marequest->imar_name = name;
		marequest->imar_soright = soright;

		bucket = &ipc_marequest_table[IMAR_HASH(space, name)];
		imarb_lock(bucket);

		marequest->imar_next = bucket->imarb_head;
		bucket->imarb_head = marequest;

		imarb_unlock(bucket);
	} else {
		if ((soright = ipc_port_lookup_notify(space, notify))
								== IP_NULL) {
			is_write_unlock(space);
			imar_free(marequest);
			return MACH_SEND_INVALID_NOTIFY;
		}

		is_reference(space);
		marequest->imar_space = space;
		marequest->imar_name = MACH_PORT_NULL;
		marequest->imar_soright = soright;
	}

	is_write_unlock(space);
	*marequestp = marequest;
	return MACH_MSG_SUCCESS;
}

/*
 *	Routine:	ipc_marequest_cancel
 *	Purpose:
 *		Cancel a msg-accepted request, because
 *		the space's entry is being destroyed.
 *	Conditions:
 *		The space is write-locked and active.
 */

void
ipc_marequest_cancel(space, name)
	ipc_space_t space;
	mach_port_t name;
{
	ipc_marequest_bucket_t bucket;
	ipc_marequest_t marequest, *last;

	assert(space->is_active);

	bucket = &ipc_marequest_table[IMAR_HASH(space, name)];
	imarb_lock(bucket);

	for (last = &bucket->imarb_head;
	     (marequest = *last) != IMAR_NULL;
	     last = &marequest->imar_next)
		if ((marequest->imar_space == space) &&
		    (marequest->imar_name == name))
			break;

	assert(marequest != IMAR_NULL);
	*last = marequest->imar_next;
	imarb_unlock(bucket);

	marequest->imar_name = MACH_PORT_NULL;
}

/*
 *	Routine:	ipc_marequest_rename
 *	Purpose:
 *		Rename a msg-accepted request, because the entry
 *		in the space is being renamed.
 *	Conditions:
 *		The space is write-locked and active.
 */

void
ipc_marequest_rename(space, old, new)
	ipc_space_t space;
	mach_port_t old, new;
{
	ipc_marequest_bucket_t bucket;
	ipc_marequest_t marequest, *last;

	assert(space->is_active);

	bucket = &ipc_marequest_table[IMAR_HASH(space, old)];
	imarb_lock(bucket);

	for (last = &bucket->imarb_head;
	     (marequest = *last) != IMAR_NULL;
	     last = &marequest->imar_next)
		if ((marequest->imar_space == space) &&
		    (marequest->imar_name == old))
			break;

	assert(marequest != IMAR_NULL);
	*last = marequest->imar_next;
	imarb_unlock(bucket);

	marequest->imar_name = new;

	bucket = &ipc_marequest_table[IMAR_HASH(space, new)];
	imarb_lock(bucket);

	marequest->imar_next = bucket->imarb_head;
	bucket->imarb_head = marequest;

	imarb_unlock(bucket);
}

/*
 *	Routine:	ipc_marequest_destroy
 *	Purpose:
 *		Destroy a msg-accepted request, because
 *		the kernel message is being received/destroyed.
 *	Conditions:
 *		Nothing locked.
 */

void
ipc_marequest_destroy(marequest)
	ipc_marequest_t marequest;
{
	ipc_space_t space = marequest->imar_space;
	mach_port_t name;
	ipc_port_t soright;

	is_write_lock(space);

	name = marequest->imar_name;
	soright = marequest->imar_soright;

	if (name != MACH_PORT_NULL) {
		ipc_marequest_bucket_t bucket;
		ipc_marequest_t this, *last;

		bucket = &ipc_marequest_table[IMAR_HASH(space, name)];
		imarb_lock(bucket);

		for (last = &bucket->imarb_head;
		     (this = *last) != IMAR_NULL;
		     last = &this->imar_next)
			if ((this->imar_space == space) &&
			    (this->imar_name == name))
				break;

		assert(this == marequest);
		*last = this->imar_next;
		imarb_unlock(bucket);

		if (space->is_active) {
			ipc_entry_t entry;

			entry = ipc_entry_lookup(space, name);
			assert(entry != IE_NULL);
			assert(entry->ie_bits & IE_BITS_MAREQUEST);
			assert(entry->ie_bits & MACH_PORT_TYPE_SEND_RECEIVE);

			entry->ie_bits &= ~IE_BITS_MAREQUEST;

		} else
			name = MACH_PORT_NULL;
	}

	is_write_unlock(space);
	is_release(space);

	imar_free(marequest);

	assert(soright != IP_NULL);
	ipc_notify_msg_accepted(soright, name);
}

#if	MACH_IPC_DEBUG


/*
 *	Routine:	ipc_marequest_info
 *	Purpose:
 *		Return information about the marequest hash table.
 *		Fills the buffer with as much information as possible
 *		and returns the desired size of the buffer.
 *	Conditions:
 *		Nothing locked.  The caller should provide
 *		possibly-pageable memory.
 */

unsigned int
ipc_marequest_info(maxp, info, count)
	unsigned int *maxp;
	hash_info_bucket_t *info;
	unsigned int count;
{
	ipc_marequest_index_t i;

	if (ipc_marequest_size < count)
		count = ipc_marequest_size;

	for (i = 0; i < count; i++) {
		ipc_marequest_bucket_t bucket = &ipc_marequest_table[i];
		unsigned int bucket_count = 0;
		ipc_marequest_t marequest;

		imarb_lock(bucket);
		for (marequest = bucket->imarb_head;
		     marequest != IMAR_NULL;
		     marequest = marequest->imar_next)
			bucket_count++;
		imarb_unlock(bucket);

		/* don't touch pageable memory while holding locks */
		info[i].hib_count = bucket_count;
	}

	*maxp = ipc_marequest_max;
	return ipc_marequest_size;
}

#endif	/* MACH_IPC_DEBUG */