2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: kern/ipc_kobject.c
65 * Functions for letting a port represent a kernel object.
68 #include <mach_debug.h>
69 #include <mach_ipc_test.h>
70 #include <mach_machine_routines.h>
71 #include <norma_task.h>
73 #include <platforms.h>
76 #include <mach/port.h>
77 #include <mach/kern_return.h>
78 #include <mach/message.h>
79 #include <mach/mig_errors.h>
80 #include <mach/notify.h>
83 #include <mach/mach_vm_server.h>
84 #include <mach/mach_port_server.h>
85 #include <mach/mach_host_server.h>
86 #include <mach/host_priv_server.h>
87 #include <mach/host_security_server.h>
88 #include <mach/clock_server.h>
89 #include <mach/clock_priv_server.h>
90 #include <mach/ledger_server.h>
91 #include <mach/lock_set_server.h>
92 #include <default_pager/default_pager_object_server.h>
93 #include <mach/memory_object_server.h>
94 #include <mach/memory_object_control_server.h>
95 #include <mach/memory_object_default_server.h>
96 #include <mach/memory_object_name_server.h>
97 #include <mach/processor_server.h>
98 #include <mach/processor_set_server.h>
99 #include <mach/semaphore_server.h>
100 #include <mach/task_server.h>
101 #include <mach/vm_map_server.h>
102 #include <mach/thread_act_server.h>
104 #include <device/device_types.h>
105 #include <device/device_server.h>
107 #include <UserNotification/UNDReplyServer.h>
109 #if MACH_MACHINE_ROUTINES
110 #include <machine/machine_routines.h>
111 #endif /* MACH_MACHINE_ROUTINES */
113 #include <uk_xkern/xk_uproxy_server.h>
114 #endif /* XK_PROXY */
116 #include <kern/ipc_tt.h>
117 #include <kern/ipc_mig.h>
118 #include <kern/ipc_kobject.h>
119 #include <kern/host_notify.h>
120 #include <kern/mk_timer.h>
121 #include <kern/misc_protos.h>
122 #include <ipc/ipc_kmsg.h>
123 #include <ipc/ipc_port.h>
124 #include <kern/counters.h>
126 #include <vm/vm_shared_memory_server.h>
127 #include <vm/vm_protos.h>
130 * Routine: ipc_kobject_notify
132 * Deliver notifications to kobjects that care about them.
136 mach_msg_header_t
*request_header
,
137 mach_msg_header_t
*reply_header
);
141 mig_routine_t routine
;
144 mach_counter_t callcount
;
148 #define MAX_MIG_ENTRIES 1024
149 #define MIG_HASH(x) (x)
152 #define max(a,b) (((a) > (b)) ? (a) : (b))
155 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
156 int mig_table_max_displ
;
157 mach_msg_size_t mig_reply_size
;
162 const struct mig_subsystem
*mig_e
[] = {
163 (const struct mig_subsystem
*)&mach_vm_subsystem
,
164 (const struct mig_subsystem
*)&mach_port_subsystem
,
165 (const struct mig_subsystem
*)&mach_host_subsystem
,
166 (const struct mig_subsystem
*)&host_priv_subsystem
,
167 (const struct mig_subsystem
*)&host_security_subsystem
,
168 (const struct mig_subsystem
*)&clock_subsystem
,
169 (const struct mig_subsystem
*)&clock_priv_subsystem
,
170 (const struct mig_subsystem
*)&processor_subsystem
,
171 (const struct mig_subsystem
*)&processor_set_subsystem
,
172 (const struct mig_subsystem
*)&is_iokit_subsystem
,
173 (const struct mig_subsystem
*)&memory_object_name_subsystem
,
174 (const struct mig_subsystem
*)&lock_set_subsystem
,
175 (const struct mig_subsystem
*)&ledger_subsystem
,
176 (const struct mig_subsystem
*)&semaphore_subsystem
,
177 (const struct mig_subsystem
*)&task_subsystem
,
178 (const struct mig_subsystem
*)&thread_act_subsystem
,
179 (const struct mig_subsystem
*)&vm_map_subsystem
,
180 (const struct mig_subsystem
*)&UNDReply_subsystem
,
181 (const struct mig_subsystem
*)&default_pager_object_subsystem
,
184 (const struct mig_subsystem
*)&do_uproxy_xk_uproxy_subsystem
,
185 #endif /* XK_PROXY */
186 #if MACH_MACHINE_ROUTINES
187 (const struct mig_subsystem
*)&MACHINE_SUBSYSTEM
,
188 #endif /* MACH_MACHINE_ROUTINES */
190 (const struct mig_subsystem
*)&mcmsg_info_subsystem
,
191 #endif /* MCMSG && iPSC860 */
197 unsigned int i
, n
= sizeof(mig_e
)/sizeof(const struct mig_subsystem
*);
199 mach_msg_id_t j
, pos
, nentry
, range
;
201 for (i
= 0; i
< n
; i
++) {
202 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
203 if (!mig_e
[i
]->start
|| range
< 0)
204 panic("the msgh_ids in mig_e[] aren't valid!");
205 mig_reply_size
= max(mig_reply_size
, mig_e
[i
]->maxsize
);
207 for (j
= 0; j
< range
; j
++) {
208 if (mig_e
[i
]->routine
[j
].stub_routine
) {
209 /* Only put real entries in the table */
210 nentry
= j
+ mig_e
[i
]->start
;
211 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
212 mig_buckets
[pos
].num
;
213 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
214 if (mig_buckets
[pos
].num
== nentry
) {
215 printf("message id = %d\n", nentry
);
216 panic("multiple entries with the same msgh_id");
218 if (howmany
== MAX_MIG_ENTRIES
)
219 panic("the mig dispatch table is too small");
222 mig_buckets
[pos
].num
= nentry
;
223 mig_buckets
[pos
].routine
= mig_e
[i
]->routine
[j
].stub_routine
;
224 if (mig_e
[i
]->routine
[j
].max_reply_msg
)
225 mig_buckets
[pos
].size
= mig_e
[i
]->routine
[j
].max_reply_msg
;
227 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
229 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
233 printf("mig_table_max_displ = %d\n", mig_table_max_displ
);
238 * Routine: ipc_kobject_server
240 * Handle a message sent to the kernel.
241 * Generates a reply message.
242 * Version for Untyped IPC.
251 mach_msg_size_t reply_size
;
255 mach_msg_format_0_trailer_t
*trailer
;
256 register mig_hash_t
*ptr
;
259 * Find out corresponding mig_hash entry if any
262 register int key
= request
->ikm_header
->msgh_id
;
263 register int i
= MIG_HASH(key
);
264 register int max_iter
= mig_table_max_displ
;
267 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
268 while (key
!= ptr
->num
&& ptr
->num
&& --max_iter
);
270 if (!ptr
->routine
|| key
!= ptr
->num
) {
271 ptr
= (mig_hash_t
*)0;
272 reply_size
= mig_reply_size
;
274 reply_size
= ptr
->size
;
281 /* round up for trailer size */
282 reply_size
+= MAX_TRAILER_SIZE
;
283 reply
= ipc_kmsg_alloc(reply_size
);
285 if (reply
== IKM_NULL
) {
286 printf("ipc_kobject_server: dropping request\n");
287 ipc_kmsg_destroy(request
);
292 * Initialize reply message.
295 #define InP ((mach_msg_header_t *) request->ikm_header)
296 #define OutP ((mig_reply_error_t *) reply->ikm_header)
298 OutP
->NDR
= NDR_record
;
299 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
301 OutP
->Head
.msgh_bits
=
302 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP
->msgh_bits
), 0);
303 OutP
->Head
.msgh_remote_port
= InP
->msgh_local_port
;
304 OutP
->Head
.msgh_local_port
= MACH_PORT_NULL
;
305 OutP
->Head
.msgh_id
= InP
->msgh_id
+ 100;
312 * Find the routine to call, and call it
313 * to perform the kernel function
317 (*ptr
->routine
)(request
->ikm_header
, reply
->ikm_header
);
318 kernel_task
->messages_received
++;
321 if (!ipc_kobject_notify(request
->ikm_header
, reply
->ikm_header
)){
323 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
324 request
->ikm_header
->msgh_id
);
325 #endif /* MACH_IPC_TEST */
326 _MIG_MSGID_INVALID(request
->ikm_header
->msgh_id
);
328 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
332 kernel_task
->messages_received
++;
334 kernel_task
->messages_sent
++;
338 * Destroy destination. The following code differs from
339 * ipc_object_destroy in that we release the send-once
340 * right instead of generating a send-once notification
341 * (which would bring us here again, creating a loop).
342 * It also differs in that we only expect send or
343 * send-once rights, never receive rights.
345 * We set msgh_remote_port to IP_NULL so that the kmsg
346 * destroy routines don't try to destroy the port twice.
348 destp
= (ipc_port_t
*) &request
->ikm_header
->msgh_remote_port
;
349 switch (MACH_MSGH_BITS_REMOTE(request
->ikm_header
->msgh_bits
)) {
350 case MACH_MSG_TYPE_PORT_SEND
:
351 ipc_port_release_send(*destp
);
354 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
355 ipc_port_release_sonce(*destp
);
359 panic("ipc_kobject_server: strange destination rights");
363 if (!(reply
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
364 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
!= KERN_SUCCESS
)
365 kr
= ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
;
369 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
371 * The server function is responsible for the contents
372 * of the message. The reply port right is moved
373 * to the reply message, and we have deallocated
374 * the destination port right, so we just need
377 ipc_kmsg_free(request
);
381 * The message contents of the request are intact.
382 * Destroy everthing except the reply port right,
383 * which is needed in the reply message.
385 request
->ikm_header
->msgh_local_port
= MACH_PORT_NULL
;
386 ipc_kmsg_destroy(request
);
389 if (kr
== MIG_NO_REPLY
) {
391 * The server function will send a reply message
392 * using the reply port right, which it has saved.
395 ipc_kmsg_free(reply
);
398 } else if (!IP_VALID((ipc_port_t
)reply
->ikm_header
->msgh_remote_port
)) {
400 * Can't queue the reply message if the destination
401 * (the reply port) isn't valid.
404 ipc_kmsg_destroy(reply
);
409 trailer
= (mach_msg_format_0_trailer_t
*)
410 ((vm_offset_t
)reply
->ikm_header
+ (int)reply
->ikm_header
->msgh_size
);
412 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
413 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
414 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
420 * Routine: ipc_kobject_set
422 * Make a port represent a kernel object of the given type.
423 * The caller is responsible for handling refs for the
424 * kernel object, if necessary.
426 * Nothing locked. The port must be active if setting
427 * a kobject linkage. Clearing a linkage is OK on an
433 ipc_kobject_t kobject
,
434 ipc_kobject_type_t type
)
437 ipc_kobject_set_atomically(port
, kobject
, type
);
442 ipc_kobject_set_atomically(
444 ipc_kobject_t kobject
,
445 ipc_kobject_type_t type
)
447 assert(type
== IKOT_NONE
|| ip_active(port
));
449 port
->ip_spares
[2] = (port
->ip_bits
& IO_BITS_KOTYPE
);
450 #endif /* MACH_ASSERT */
451 port
->ip_bits
= (port
->ip_bits
&~ IO_BITS_KOTYPE
) | type
;
452 port
->ip_kobject
= kobject
;
456 * Routine: ipc_kobject_destroy
458 * Release any kernel object resources associated
459 * with the port, which is being destroyed.
461 * This should only be needed when resources are
462 * associated with a user's port. In the normal case,
463 * when the kernel is the receiver, the code calling
464 * ipc_port_dealloc_kernel should clean up the resources.
466 * The port is not locked, but it is dead.
473 switch (ip_kotype(port
)) {
476 mk_timer_port_destroy(port
);
479 case IKOT_NAMED_ENTRY
:
480 mach_destroy_memory_entry(port
);
483 case IKOT_HOST_NOTIFY
:
484 host_notify_port_destroy(port
);
495 mach_msg_header_t
*request_header
,
496 mach_msg_header_t
*reply_header
)
498 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
500 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
501 switch (request_header
->msgh_id
) {
502 case MACH_NOTIFY_NO_SENDERS
:
503 if(ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
507 * Bring the sequence number and mscount in
508 * line with ipc_port_destroy assertion.
510 port
->ip_mscount
= 0;
511 port
->ip_messages
.imq_seqno
= 0;
512 ipc_port_destroy(port
); /* releases lock */
515 if (ip_kotype(port
) == IKOT_UPL
) {
517 request_header
->msgh_remote_port
,
518 (mach_port_mscount_t
)
519 ((mach_no_senders_notification_t
*)
520 request_header
)->not_count
);
521 (ipc_port_t
)reply_header
->msgh_remote_port
528 case MACH_NOTIFY_PORT_DELETED
:
529 case MACH_NOTIFY_PORT_DESTROYED
:
530 case MACH_NOTIFY_SEND_ONCE
:
531 case MACH_NOTIFY_DEAD_NAME
:
537 switch (ip_kotype(port
)) {
540 case IKOT_IOKIT_OBJECT
:
541 case IKOT_IOKIT_CONNECT
:
542 case IKOT_IOKIT_SPARE
:
544 return iokit_notify(request_header
);
554 #include <mach_kdb.h>
555 #if MACH_COUNTERS && MACH_KDB
557 #include <ddb/db_output.h>
558 #include <ddb/db_sym.h>
560 #define printf kdbprintf
562 extern void kobjserver_stats(void);
563 extern void bucket_stats_print(mig_hash_t
*bucket
);
565 extern void kobjserver_stats_clear(void);
569 kobjserver_stats_clear(void)
572 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
573 mig_buckets
[i
].callcount
= 0;
578 kobjserver_stats(void)
580 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(struct mig_subsystem
);
581 register unsigned int howmany
;
582 register mach_msg_id_t j
, pos
, nentry
, range
;
584 db_printf("Kobject server call counts:\n");
585 for (i
= 0; i
< n
; i
++) {
587 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
589 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
590 if (!mig_e
[i
]->start
|| range
< 0) continue;
592 for (j
= 0; j
< range
; j
++) {
593 nentry
= j
+ mig_e
[i
]->start
;
594 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
595 mig_buckets
[pos
].num
;
596 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
597 if (mig_buckets
[pos
].num
== nentry
)
598 bucket_stats_print(&mig_buckets
[pos
]);
605 bucket_stats_print(mig_hash_t
*bucket
)
607 if (bucket
->callcount
) {
609 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
610 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
615 #endif /* MACH_COUNTERS && MACH_KDB */