]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/ipc_kobject.c
xnu-792.6.70.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_kobject.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
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.
1c79356b
A
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: kern/ipc_kobject.c
54 * Author: Rich Draves
55 * Date: 1989
56 *
57 * Functions for letting a port represent a kernel object.
58 */
59
60#include <mach_debug.h>
61#include <mach_ipc_test.h>
62#include <mach_machine_routines.h>
63#include <norma_task.h>
64#include <mach_rt.h>
65#include <platforms.h>
66
9bccf70c 67#include <mach/mig.h>
1c79356b
A
68#include <mach/port.h>
69#include <mach/kern_return.h>
70#include <mach/message.h>
71#include <mach/mig_errors.h>
72#include <mach/notify.h>
91447636
A
73#include <mach/ndr.h>
74
75#include <mach/mach_vm_server.h>
76#include <mach/mach_port_server.h>
77#include <mach/mach_host_server.h>
78#include <mach/host_priv_server.h>
79#include <mach/host_security_server.h>
80#include <mach/clock_server.h>
81#include <mach/clock_priv_server.h>
82#include <mach/ledger_server.h>
83#include <mach/lock_set_server.h>
84#include <default_pager/default_pager_object_server.h>
85#include <mach/memory_object_server.h>
86#include <mach/memory_object_control_server.h>
87#include <mach/memory_object_default_server.h>
88#include <mach/memory_object_name_server.h>
89#include <mach/processor_server.h>
90#include <mach/processor_set_server.h>
91#include <mach/semaphore_server.h>
92#include <mach/task_server.h>
93#include <mach/vm_map_server.h>
94#include <mach/thread_act_server.h>
95
96#include <device/device_types.h>
97#include <device/device_server.h>
98
99#include <UserNotification/UNDReplyServer.h>
100
101#if MACH_MACHINE_ROUTINES
102#include <machine/machine_routines.h>
103#endif /* MACH_MACHINE_ROUTINES */
104#if XK_PROXY
105#include <uk_xkern/xk_uproxy_server.h>
106#endif /* XK_PROXY */
9bccf70c 107
91447636 108#include <kern/ipc_tt.h>
1c79356b
A
109#include <kern/ipc_mig.h>
110#include <kern/ipc_kobject.h>
55e303ae 111#include <kern/host_notify.h>
1c79356b 112#include <kern/mk_timer.h>
55e303ae 113#include <kern/misc_protos.h>
1c79356b
A
114#include <ipc/ipc_kmsg.h>
115#include <ipc/ipc_port.h>
116#include <kern/counters.h>
117
91447636
A
118#include <vm/vm_shared_memory_server.h>
119#include <vm/vm_protos.h>
1c79356b 120
1c79356b
A
121/*
122 * Routine: ipc_kobject_notify
123 * Purpose:
124 * Deliver notifications to kobjects that care about them.
125 */
126boolean_t
127ipc_kobject_notify(
128 mach_msg_header_t *request_header,
129 mach_msg_header_t *reply_header);
130
1c79356b
A
131typedef struct {
132 mach_msg_id_t num;
133 mig_routine_t routine;
134 int size;
135#if MACH_COUNTERS
136 mach_counter_t callcount;
137#endif
138} mig_hash_t;
139
140#define MAX_MIG_ENTRIES 1024
141#define MIG_HASH(x) (x)
142
143#ifndef max
144#define max(a,b) (((a) > (b)) ? (a) : (b))
145#endif /* max */
146
147mig_hash_t mig_buckets[MAX_MIG_ENTRIES];
148int mig_table_max_displ;
149mach_msg_size_t mig_reply_size;
150
151
1c79356b
A
152
153
91447636
A
154const struct mig_subsystem *mig_e[] = {
155 (const struct mig_subsystem *)&mach_vm_subsystem,
156 (const struct mig_subsystem *)&mach_port_subsystem,
157 (const struct mig_subsystem *)&mach_host_subsystem,
158 (const struct mig_subsystem *)&host_priv_subsystem,
159 (const struct mig_subsystem *)&host_security_subsystem,
160 (const struct mig_subsystem *)&clock_subsystem,
161 (const struct mig_subsystem *)&clock_priv_subsystem,
162 (const struct mig_subsystem *)&processor_subsystem,
163 (const struct mig_subsystem *)&processor_set_subsystem,
164 (const struct mig_subsystem *)&is_iokit_subsystem,
165 (const struct mig_subsystem *)&memory_object_name_subsystem,
166 (const struct mig_subsystem *)&lock_set_subsystem,
167 (const struct mig_subsystem *)&ledger_subsystem,
168 (const struct mig_subsystem *)&semaphore_subsystem,
169 (const struct mig_subsystem *)&task_subsystem,
170 (const struct mig_subsystem *)&thread_act_subsystem,
171 (const struct mig_subsystem *)&vm_map_subsystem,
172 (const struct mig_subsystem *)&UNDReply_subsystem,
173 (const struct mig_subsystem *)&default_pager_object_subsystem,
1c79356b
A
174
175#if XK_PROXY
91447636 176 (const struct mig_subsystem *)&do_uproxy_xk_uproxy_subsystem,
1c79356b
A
177#endif /* XK_PROXY */
178#if MACH_MACHINE_ROUTINES
91447636 179 (const struct mig_subsystem *)&MACHINE_SUBSYSTEM,
1c79356b
A
180#endif /* MACH_MACHINE_ROUTINES */
181#if MCMSG && iPSC860
91447636 182 (const struct mig_subsystem *)&mcmsg_info_subsystem,
1c79356b
A
183#endif /* MCMSG && iPSC860 */
184};
185
186void
187mig_init(void)
188{
91447636
A
189 unsigned int i, n = sizeof(mig_e)/sizeof(const struct mig_subsystem *);
190 int howmany;
191 mach_msg_id_t j, pos, nentry, range;
1c79356b
A
192
193 for (i = 0; i < n; i++) {
194 range = mig_e[i]->end - mig_e[i]->start;
195 if (!mig_e[i]->start || range < 0)
196 panic("the msgh_ids in mig_e[] aren't valid!");
197 mig_reply_size = max(mig_reply_size, mig_e[i]->maxsize);
198
199 for (j = 0; j < range; j++) {
200 if (mig_e[i]->routine[j].stub_routine) {
201 /* Only put real entries in the table */
202 nentry = j + mig_e[i]->start;
203 for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1;
204 mig_buckets[pos].num;
205 pos = ++pos % MAX_MIG_ENTRIES, howmany++) {
206 if (mig_buckets[pos].num == nentry) {
207 printf("message id = %d\n", nentry);
208 panic("multiple entries with the same msgh_id");
209 }
210 if (howmany == MAX_MIG_ENTRIES)
211 panic("the mig dispatch table is too small");
212 }
213
214 mig_buckets[pos].num = nentry;
215 mig_buckets[pos].routine = mig_e[i]->routine[j].stub_routine;
216 if (mig_e[i]->routine[j].max_reply_msg)
217 mig_buckets[pos].size = mig_e[i]->routine[j].max_reply_msg;
218 else
219 mig_buckets[pos].size = mig_e[i]->maxsize;
220
221 mig_table_max_displ = max(howmany, mig_table_max_displ);
222 }
223 }
224 }
0b4e3aa0 225 printf("mig_table_max_displ = %d\n", mig_table_max_displ);
1c79356b
A
226}
227
228
229/*
230 * Routine: ipc_kobject_server
231 * Purpose:
232 * Handle a message sent to the kernel.
233 * Generates a reply message.
234 * Version for Untyped IPC.
235 * Conditions:
236 * Nothing locked.
237 */
238
239ipc_kmsg_t
240ipc_kobject_server(
241 ipc_kmsg_t request)
242{
243 mach_msg_size_t reply_size;
244 ipc_kmsg_t reply;
245 kern_return_t kr;
1c79356b
A
246 ipc_port_t *destp;
247 mach_msg_format_0_trailer_t *trailer;
248 register mig_hash_t *ptr;
91447636 249
1c79356b 250 /*
91447636
A
251 * Find out corresponding mig_hash entry if any
252 */
1c79356b 253 {
91447636 254 register int key = request->ikm_header->msgh_id;
1c79356b
A
255 register int i = MIG_HASH(key);
256 register int max_iter = mig_table_max_displ;
257
258 do
259 ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES];
260 while (key != ptr->num && ptr->num && --max_iter);
261
262 if (!ptr->routine || key != ptr->num) {
263 ptr = (mig_hash_t *)0;
264 reply_size = mig_reply_size;
265 } else {
266 reply_size = ptr->size;
267#if MACH_COUNTER
268 ptr->callcount++;
269#endif
270 }
271 }
272
273 /* round up for trailer size */
274 reply_size += MAX_TRAILER_SIZE;
275 reply = ipc_kmsg_alloc(reply_size);
276
277 if (reply == IKM_NULL) {
278 printf("ipc_kobject_server: dropping request\n");
279 ipc_kmsg_destroy(request);
280 return IKM_NULL;
281 }
282
283 /*
284 * Initialize reply message.
285 */
286 {
91447636
A
287#define InP ((mach_msg_header_t *) request->ikm_header)
288#define OutP ((mig_reply_error_t *) reply->ikm_header)
1c79356b
A
289
290 OutP->NDR = NDR_record;
291 OutP->Head.msgh_size = sizeof(mig_reply_error_t);
292
293 OutP->Head.msgh_bits =
294 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP->msgh_bits), 0);
295 OutP->Head.msgh_remote_port = InP->msgh_local_port;
296 OutP->Head.msgh_local_port = MACH_PORT_NULL;
297 OutP->Head.msgh_id = InP->msgh_id + 100;
298
299#undef InP
300#undef OutP
301 }
302
303 /*
304 * Find the routine to call, and call it
305 * to perform the kernel function
306 */
307 {
308 if (ptr) {
91447636 309 (*ptr->routine)(request->ikm_header, reply->ikm_header);
1c79356b
A
310 kernel_task->messages_received++;
311 }
312 else {
91447636 313 if (!ipc_kobject_notify(request->ikm_header, reply->ikm_header)){
1c79356b
A
314#if MACH_IPC_TEST
315 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
91447636 316 request->ikm_header->msgh_id);
1c79356b 317#endif /* MACH_IPC_TEST */
91447636 318 _MIG_MSGID_INVALID(request->ikm_header->msgh_id);
1c79356b 319
91447636 320 ((mig_reply_error_t *) reply->ikm_header)->RetCode
1c79356b
A
321 = MIG_BAD_ID;
322 }
323 else
324 kernel_task->messages_received++;
325 }
326 kernel_task->messages_sent++;
327 }
328
329 /*
330 * Destroy destination. The following code differs from
331 * ipc_object_destroy in that we release the send-once
332 * right instead of generating a send-once notification
333 * (which would bring us here again, creating a loop).
334 * It also differs in that we only expect send or
335 * send-once rights, never receive rights.
336 *
337 * We set msgh_remote_port to IP_NULL so that the kmsg
338 * destroy routines don't try to destroy the port twice.
339 */
91447636
A
340 destp = (ipc_port_t *) &request->ikm_header->msgh_remote_port;
341 switch (MACH_MSGH_BITS_REMOTE(request->ikm_header->msgh_bits)) {
1c79356b
A
342 case MACH_MSG_TYPE_PORT_SEND:
343 ipc_port_release_send(*destp);
344 break;
345
346 case MACH_MSG_TYPE_PORT_SEND_ONCE:
347 ipc_port_release_sonce(*destp);
348 break;
349
350 default:
9bccf70c 351 panic("ipc_kobject_server: strange destination rights");
1c79356b
A
352 }
353 *destp = IP_NULL;
354
91447636
A
355 if (!(reply->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
356 ((mig_reply_error_t *) reply->ikm_header)->RetCode != KERN_SUCCESS)
357 kr = ((mig_reply_error_t *) reply->ikm_header)->RetCode;
1c79356b
A
358 else
359 kr = KERN_SUCCESS;
360
361 if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) {
362 /*
363 * The server function is responsible for the contents
364 * of the message. The reply port right is moved
365 * to the reply message, and we have deallocated
366 * the destination port right, so we just need
367 * to free the kmsg.
368 */
369 ipc_kmsg_free(request);
370
371 } else {
372 /*
373 * The message contents of the request are intact.
374 * Destroy everthing except the reply port right,
375 * which is needed in the reply message.
376 */
91447636 377 request->ikm_header->msgh_local_port = MACH_PORT_NULL;
1c79356b
A
378 ipc_kmsg_destroy(request);
379 }
380
381 if (kr == MIG_NO_REPLY) {
382 /*
383 * The server function will send a reply message
384 * using the reply port right, which it has saved.
385 */
386
387 ipc_kmsg_free(reply);
388
1c79356b 389 return IKM_NULL;
91447636 390 } else if (!IP_VALID((ipc_port_t)reply->ikm_header->msgh_remote_port)) {
1c79356b
A
391 /*
392 * Can't queue the reply message if the destination
393 * (the reply port) isn't valid.
394 */
395
396 ipc_kmsg_destroy(reply);
397
1c79356b
A
398 return IKM_NULL;
399 }
400
401 trailer = (mach_msg_format_0_trailer_t *)
91447636
A
402 ((vm_offset_t)reply->ikm_header + (int)reply->ikm_header->msgh_size);
403
1c79356b
A
404 trailer->msgh_sender = KERNEL_SECURITY_TOKEN;
405 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
406 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
407
1c79356b
A
408 return reply;
409}
410
411/*
412 * Routine: ipc_kobject_set
413 * Purpose:
414 * Make a port represent a kernel object of the given type.
415 * The caller is responsible for handling refs for the
416 * kernel object, if necessary.
417 * Conditions:
418 * Nothing locked. The port must be active if setting
419 * a kobject linkage. Clearing a linkage is OK on an
420 * inactive port.
421 */
422void
423ipc_kobject_set(
424 ipc_port_t port,
425 ipc_kobject_t kobject,
426 ipc_kobject_type_t type)
427{
428 ip_lock(port);
429 ipc_kobject_set_atomically(port, kobject, type);
430 ip_unlock(port);
431}
432
433void
434ipc_kobject_set_atomically(
435 ipc_port_t port,
436 ipc_kobject_t kobject,
437 ipc_kobject_type_t type)
438{
439 assert(type == IKOT_NONE || ip_active(port));
440#if MACH_ASSERT
441 port->ip_spares[2] = (port->ip_bits & IO_BITS_KOTYPE);
442#endif /* MACH_ASSERT */
443 port->ip_bits = (port->ip_bits &~ IO_BITS_KOTYPE) | type;
444 port->ip_kobject = kobject;
445}
446
447/*
448 * Routine: ipc_kobject_destroy
449 * Purpose:
450 * Release any kernel object resources associated
451 * with the port, which is being destroyed.
452 *
453 * This should only be needed when resources are
454 * associated with a user's port. In the normal case,
455 * when the kernel is the receiver, the code calling
456 * ipc_port_dealloc_kernel should clean up the resources.
457 * Conditions:
458 * The port is not locked, but it is dead.
459 */
460
461void
462ipc_kobject_destroy(
463 ipc_port_t port)
464{
465 switch (ip_kotype(port)) {
466
467 case IKOT_TIMER:
468 mk_timer_port_destroy(port);
469 break;
470
471 case IKOT_NAMED_ENTRY:
472 mach_destroy_memory_entry(port);
473 break;
474
55e303ae
A
475 case IKOT_HOST_NOTIFY:
476 host_notify_port_destroy(port);
477 break;
478
479 default:
1c79356b
A
480 break;
481 }
482}
483
484
1c79356b
A
485boolean_t
486ipc_kobject_notify(
487 mach_msg_header_t *request_header,
488 mach_msg_header_t *reply_header)
489{
490 ipc_port_t port = (ipc_port_t) request_header->msgh_remote_port;
1c79356b
A
491
492 ((mig_reply_error_t *) reply_header)->RetCode = MIG_NO_REPLY;
493 switch (request_header->msgh_id) {
494 case MACH_NOTIFY_NO_SENDERS:
495 if(ip_kotype(port) == IKOT_NAMED_ENTRY) {
496 ip_lock(port);
497
498 /*
499 * Bring the sequence number and mscount in
500 * line with ipc_port_destroy assertion.
501 */
502 port->ip_mscount = 0;
503 port->ip_messages.imq_seqno = 0;
504 ipc_port_destroy(port); /* releases lock */
505 return TRUE;
506 }
0b4e3aa0
A
507 if (ip_kotype(port) == IKOT_UPL) {
508 upl_no_senders(
91447636 509 request_header->msgh_remote_port,
0b4e3aa0
A
510 (mach_port_mscount_t)
511 ((mach_no_senders_notification_t *)
512 request_header)->not_count);
513 (ipc_port_t)reply_header->msgh_remote_port
514 = MACH_PORT_NULL;
515 return TRUE;
1c79356b 516 }
0b4e3aa0 517
1c79356b 518 break;
0b4e3aa0 519
1c79356b
A
520 case MACH_NOTIFY_PORT_DELETED:
521 case MACH_NOTIFY_PORT_DESTROYED:
522 case MACH_NOTIFY_SEND_ONCE:
523 case MACH_NOTIFY_DEAD_NAME:
524 break;
525
526 default:
527 return FALSE;
528 }
529 switch (ip_kotype(port)) {
530
531#ifdef IOKIT
532 case IKOT_IOKIT_OBJECT:
533 case IKOT_IOKIT_CONNECT:
534 case IKOT_IOKIT_SPARE:
535 {
1c79356b
A
536 return iokit_notify(request_header);
537 }
538#endif
539 default:
540 return FALSE;
541 }
542}
543
544
545
546#include <mach_kdb.h>
547#if MACH_COUNTERS && MACH_KDB
548
549#include <ddb/db_output.h>
550#include <ddb/db_sym.h>
551
552#define printf kdbprintf
553
554extern void kobjserver_stats(void);
555extern void bucket_stats_print(mig_hash_t *bucket);
556
557extern void kobjserver_stats_clear(void);
558
559
560void
561kobjserver_stats_clear(void)
562{
563 int i;
564 for (i = 0; i < MAX_MIG_ENTRIES; i++) {
565 mig_buckets[i].callcount = 0;
566 }
567}
568
569void
570kobjserver_stats(void)
571{
91447636 572 register unsigned int i, n = sizeof(mig_e)/sizeof(struct mig_subsystem);
1c79356b
A
573 register unsigned int howmany;
574 register mach_msg_id_t j, pos, nentry, range;
575
576 db_printf("Kobject server call counts:\n");
577 for (i = 0; i < n; i++) {
578 db_printf(" ");
579 db_printsym((vm_offset_t)mig_e[i], DB_STGY_ANY);
580 db_printf(":\n");
581 range = mig_e[i]->end - mig_e[i]->start;
582 if (!mig_e[i]->start || range < 0) continue;
583
584 for (j = 0; j < range; j++) {
585 nentry = j + mig_e[i]->start;
586 for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1;
587 mig_buckets[pos].num;
588 pos = ++pos % MAX_MIG_ENTRIES, howmany++) {
589 if (mig_buckets[pos].num == nentry)
590 bucket_stats_print(&mig_buckets[pos]);
591 }
592 }
593 }
594}
595
596void
597bucket_stats_print(mig_hash_t *bucket)
598{
599 if (bucket->callcount) {
600 db_printf(" ");
601 db_printsym((vm_offset_t)bucket->routine, DB_STGY_ANY);
602 db_printf(" (%d):\t%d\n", bucket->num, bucket->callcount);
603 }
604}
605
606
607#endif /* MACH_COUNTERS && MACH_KDB */