2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: kern/ipc_kobject.c
57 * Functions for letting a port represent a kernel object.
60 #include <mach_debug.h>
61 #include <mach_ipc_test.h>
62 #include <mach_machine_routines.h>
63 #include <norma_task.h>
65 #include <platforms.h>
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>
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>
96 #include <device/device_types.h>
97 #include <device/device_server.h>
99 #include <UserNotification/UNDReplyServer.h>
101 #if MACH_MACHINE_ROUTINES
102 #include <machine/machine_routines.h>
103 #endif /* MACH_MACHINE_ROUTINES */
105 #include <uk_xkern/xk_uproxy_server.h>
106 #endif /* XK_PROXY */
108 #include <kern/ipc_tt.h>
109 #include <kern/ipc_mig.h>
110 #include <kern/ipc_kobject.h>
111 #include <kern/host_notify.h>
112 #include <kern/mk_timer.h>
113 #include <kern/misc_protos.h>
114 #include <ipc/ipc_kmsg.h>
115 #include <ipc/ipc_port.h>
116 #include <kern/counters.h>
118 #include <vm/vm_shared_memory_server.h>
119 #include <vm/vm_protos.h>
122 * Routine: ipc_kobject_notify
124 * Deliver notifications to kobjects that care about them.
128 mach_msg_header_t
*request_header
,
129 mach_msg_header_t
*reply_header
);
133 mig_routine_t routine
;
136 mach_counter_t callcount
;
140 #define MAX_MIG_ENTRIES 1024
141 #define MIG_HASH(x) (x)
144 #define max(a,b) (((a) > (b)) ? (a) : (b))
147 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
148 int mig_table_max_displ
;
149 mach_msg_size_t mig_reply_size
;
154 const 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
,
176 (const struct mig_subsystem
*)&do_uproxy_xk_uproxy_subsystem
,
177 #endif /* XK_PROXY */
178 #if MACH_MACHINE_ROUTINES
179 (const struct mig_subsystem
*)&MACHINE_SUBSYSTEM
,
180 #endif /* MACH_MACHINE_ROUTINES */
182 (const struct mig_subsystem
*)&mcmsg_info_subsystem
,
183 #endif /* MCMSG && iPSC860 */
189 unsigned int i
, n
= sizeof(mig_e
)/sizeof(const struct mig_subsystem
*);
191 mach_msg_id_t j
, pos
, nentry
, range
;
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
);
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");
210 if (howmany
== MAX_MIG_ENTRIES
)
211 panic("the mig dispatch table is too small");
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
;
219 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
221 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
225 printf("mig_table_max_displ = %d\n", mig_table_max_displ
);
230 * Routine: ipc_kobject_server
232 * Handle a message sent to the kernel.
233 * Generates a reply message.
234 * Version for Untyped IPC.
243 mach_msg_size_t reply_size
;
247 mach_msg_format_0_trailer_t
*trailer
;
248 register mig_hash_t
*ptr
;
251 * Find out corresponding mig_hash entry if any
254 register int key
= request
->ikm_header
->msgh_id
;
255 register int i
= MIG_HASH(key
);
256 register int max_iter
= mig_table_max_displ
;
259 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
260 while (key
!= ptr
->num
&& ptr
->num
&& --max_iter
);
262 if (!ptr
->routine
|| key
!= ptr
->num
) {
263 ptr
= (mig_hash_t
*)0;
264 reply_size
= mig_reply_size
;
266 reply_size
= ptr
->size
;
273 /* round up for trailer size */
274 reply_size
+= MAX_TRAILER_SIZE
;
275 reply
= ipc_kmsg_alloc(reply_size
);
277 if (reply
== IKM_NULL
) {
278 printf("ipc_kobject_server: dropping request\n");
279 ipc_kmsg_destroy(request
);
284 * Initialize reply message.
287 #define InP ((mach_msg_header_t *) request->ikm_header)
288 #define OutP ((mig_reply_error_t *) reply->ikm_header)
290 OutP
->NDR
= NDR_record
;
291 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
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;
304 * Find the routine to call, and call it
305 * to perform the kernel function
309 (*ptr
->routine
)(request
->ikm_header
, reply
->ikm_header
);
310 kernel_task
->messages_received
++;
313 if (!ipc_kobject_notify(request
->ikm_header
, reply
->ikm_header
)){
315 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
316 request
->ikm_header
->msgh_id
);
317 #endif /* MACH_IPC_TEST */
318 _MIG_MSGID_INVALID(request
->ikm_header
->msgh_id
);
320 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
324 kernel_task
->messages_received
++;
326 kernel_task
->messages_sent
++;
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.
337 * We set msgh_remote_port to IP_NULL so that the kmsg
338 * destroy routines don't try to destroy the port twice.
340 destp
= (ipc_port_t
*) &request
->ikm_header
->msgh_remote_port
;
341 switch (MACH_MSGH_BITS_REMOTE(request
->ikm_header
->msgh_bits
)) {
342 case MACH_MSG_TYPE_PORT_SEND
:
343 ipc_port_release_send(*destp
);
346 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
347 ipc_port_release_sonce(*destp
);
351 panic("ipc_kobject_server: strange destination rights");
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
;
361 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
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
369 ipc_kmsg_free(request
);
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.
377 request
->ikm_header
->msgh_local_port
= MACH_PORT_NULL
;
378 ipc_kmsg_destroy(request
);
381 if (kr
== MIG_NO_REPLY
) {
383 * The server function will send a reply message
384 * using the reply port right, which it has saved.
387 ipc_kmsg_free(reply
);
390 } else if (!IP_VALID((ipc_port_t
)reply
->ikm_header
->msgh_remote_port
)) {
392 * Can't queue the reply message if the destination
393 * (the reply port) isn't valid.
396 ipc_kmsg_destroy(reply
);
401 trailer
= (mach_msg_format_0_trailer_t
*)
402 ((vm_offset_t
)reply
->ikm_header
+ (int)reply
->ikm_header
->msgh_size
);
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
;
412 * Routine: ipc_kobject_set
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.
418 * Nothing locked. The port must be active if setting
419 * a kobject linkage. Clearing a linkage is OK on an
425 ipc_kobject_t kobject
,
426 ipc_kobject_type_t type
)
429 ipc_kobject_set_atomically(port
, kobject
, type
);
434 ipc_kobject_set_atomically(
436 ipc_kobject_t kobject
,
437 ipc_kobject_type_t type
)
439 assert(type
== IKOT_NONE
|| ip_active(port
));
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
;
448 * Routine: ipc_kobject_destroy
450 * Release any kernel object resources associated
451 * with the port, which is being destroyed.
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.
458 * The port is not locked, but it is dead.
465 switch (ip_kotype(port
)) {
468 mk_timer_port_destroy(port
);
471 case IKOT_NAMED_ENTRY
:
472 mach_destroy_memory_entry(port
);
475 case IKOT_HOST_NOTIFY
:
476 host_notify_port_destroy(port
);
487 mach_msg_header_t
*request_header
,
488 mach_msg_header_t
*reply_header
)
490 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
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
) {
499 * Bring the sequence number and mscount in
500 * line with ipc_port_destroy assertion.
502 port
->ip_mscount
= 0;
503 port
->ip_messages
.imq_seqno
= 0;
504 ipc_port_destroy(port
); /* releases lock */
507 if (ip_kotype(port
) == IKOT_UPL
) {
509 request_header
->msgh_remote_port
,
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
520 case MACH_NOTIFY_PORT_DELETED
:
521 case MACH_NOTIFY_PORT_DESTROYED
:
522 case MACH_NOTIFY_SEND_ONCE
:
523 case MACH_NOTIFY_DEAD_NAME
:
529 switch (ip_kotype(port
)) {
532 case IKOT_IOKIT_OBJECT
:
533 case IKOT_IOKIT_CONNECT
:
534 case IKOT_IOKIT_SPARE
:
536 return iokit_notify(request_header
);
546 #include <mach_kdb.h>
547 #if MACH_COUNTERS && MACH_KDB
549 #include <ddb/db_output.h>
550 #include <ddb/db_sym.h>
552 #define printf kdbprintf
554 extern void kobjserver_stats(void);
555 extern void bucket_stats_print(mig_hash_t
*bucket
);
557 extern void kobjserver_stats_clear(void);
561 kobjserver_stats_clear(void)
564 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
565 mig_buckets
[i
].callcount
= 0;
570 kobjserver_stats(void)
572 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(struct mig_subsystem
);
573 register unsigned int howmany
;
574 register mach_msg_id_t j
, pos
, nentry
, range
;
576 db_printf("Kobject server call counts:\n");
577 for (i
= 0; i
< n
; i
++) {
579 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
581 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
582 if (!mig_e
[i
]->start
|| range
< 0) continue;
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
]);
597 bucket_stats_print(mig_hash_t
*bucket
)
599 if (bucket
->callcount
) {
601 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
602 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
607 #endif /* MACH_COUNTERS && MACH_KDB */