2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: kern/ipc_kobject.c
63 * Functions for letting a port represent a kernel object.
66 #include <mach_debug.h>
67 #include <mach_ipc_test.h>
68 #include <mach_machine_routines.h>
69 #include <norma_task.h>
71 #include <platforms.h>
74 #include <mach/port.h>
75 #include <mach/kern_return.h>
76 #include <mach/message.h>
77 #include <mach/mig_errors.h>
78 #include <mach/notify.h>
81 #include <mach/mach_vm_server.h>
82 #include <mach/mach_port_server.h>
83 #include <mach/mach_host_server.h>
84 #include <mach/host_priv_server.h>
85 #include <mach/host_security_server.h>
86 #include <mach/clock_server.h>
87 #include <mach/clock_priv_server.h>
88 #include <mach/ledger_server.h>
89 #include <mach/lock_set_server.h>
90 #include <default_pager/default_pager_object_server.h>
91 #include <mach/memory_object_server.h>
92 #include <mach/memory_object_control_server.h>
93 #include <mach/memory_object_default_server.h>
94 #include <mach/memory_object_name_server.h>
95 #include <mach/processor_server.h>
96 #include <mach/processor_set_server.h>
97 #include <mach/semaphore_server.h>
98 #include <mach/task_server.h>
99 #include <mach/vm_map_server.h>
100 #include <mach/thread_act_server.h>
102 #include <device/device_types.h>
103 #include <device/device_server.h>
105 #include <UserNotification/UNDReplyServer.h>
107 #if MACH_MACHINE_ROUTINES
108 #include <machine/machine_routines.h>
109 #endif /* MACH_MACHINE_ROUTINES */
111 #include <uk_xkern/xk_uproxy_server.h>
112 #endif /* XK_PROXY */
114 #include <kern/ipc_tt.h>
115 #include <kern/ipc_mig.h>
116 #include <kern/ipc_kobject.h>
117 #include <kern/host_notify.h>
118 #include <kern/mk_timer.h>
119 #include <kern/misc_protos.h>
120 #include <ipc/ipc_kmsg.h>
121 #include <ipc/ipc_port.h>
122 #include <kern/counters.h>
124 #include <vm/vm_shared_memory_server.h>
125 #include <vm/vm_protos.h>
128 * Routine: ipc_kobject_notify
130 * Deliver notifications to kobjects that care about them.
134 mach_msg_header_t
*request_header
,
135 mach_msg_header_t
*reply_header
);
139 mig_routine_t routine
;
142 mach_counter_t callcount
;
146 #define MAX_MIG_ENTRIES 1024
147 #define MIG_HASH(x) (x)
150 #define max(a,b) (((a) > (b)) ? (a) : (b))
153 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
154 int mig_table_max_displ
;
155 mach_msg_size_t mig_reply_size
;
160 const struct mig_subsystem
*mig_e
[] = {
161 (const struct mig_subsystem
*)&mach_vm_subsystem
,
162 (const struct mig_subsystem
*)&mach_port_subsystem
,
163 (const struct mig_subsystem
*)&mach_host_subsystem
,
164 (const struct mig_subsystem
*)&host_priv_subsystem
,
165 (const struct mig_subsystem
*)&host_security_subsystem
,
166 (const struct mig_subsystem
*)&clock_subsystem
,
167 (const struct mig_subsystem
*)&clock_priv_subsystem
,
168 (const struct mig_subsystem
*)&processor_subsystem
,
169 (const struct mig_subsystem
*)&processor_set_subsystem
,
170 (const struct mig_subsystem
*)&is_iokit_subsystem
,
171 (const struct mig_subsystem
*)&memory_object_name_subsystem
,
172 (const struct mig_subsystem
*)&lock_set_subsystem
,
173 (const struct mig_subsystem
*)&ledger_subsystem
,
174 (const struct mig_subsystem
*)&semaphore_subsystem
,
175 (const struct mig_subsystem
*)&task_subsystem
,
176 (const struct mig_subsystem
*)&thread_act_subsystem
,
177 (const struct mig_subsystem
*)&vm_map_subsystem
,
178 (const struct mig_subsystem
*)&UNDReply_subsystem
,
179 (const struct mig_subsystem
*)&default_pager_object_subsystem
,
182 (const struct mig_subsystem
*)&do_uproxy_xk_uproxy_subsystem
,
183 #endif /* XK_PROXY */
184 #if MACH_MACHINE_ROUTINES
185 (const struct mig_subsystem
*)&MACHINE_SUBSYSTEM
,
186 #endif /* MACH_MACHINE_ROUTINES */
188 (const struct mig_subsystem
*)&mcmsg_info_subsystem
,
189 #endif /* MCMSG && iPSC860 */
195 unsigned int i
, n
= sizeof(mig_e
)/sizeof(const struct mig_subsystem
*);
197 mach_msg_id_t j
, pos
, nentry
, range
;
199 for (i
= 0; i
< n
; i
++) {
200 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
201 if (!mig_e
[i
]->start
|| range
< 0)
202 panic("the msgh_ids in mig_e[] aren't valid!");
203 mig_reply_size
= max(mig_reply_size
, mig_e
[i
]->maxsize
);
205 for (j
= 0; j
< range
; j
++) {
206 if (mig_e
[i
]->routine
[j
].stub_routine
) {
207 /* Only put real entries in the table */
208 nentry
= j
+ mig_e
[i
]->start
;
209 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
210 mig_buckets
[pos
].num
;
211 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
212 if (mig_buckets
[pos
].num
== nentry
) {
213 printf("message id = %d\n", nentry
);
214 panic("multiple entries with the same msgh_id");
216 if (howmany
== MAX_MIG_ENTRIES
)
217 panic("the mig dispatch table is too small");
220 mig_buckets
[pos
].num
= nentry
;
221 mig_buckets
[pos
].routine
= mig_e
[i
]->routine
[j
].stub_routine
;
222 if (mig_e
[i
]->routine
[j
].max_reply_msg
)
223 mig_buckets
[pos
].size
= mig_e
[i
]->routine
[j
].max_reply_msg
;
225 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
227 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
231 printf("mig_table_max_displ = %d\n", mig_table_max_displ
);
236 * Routine: ipc_kobject_server
238 * Handle a message sent to the kernel.
239 * Generates a reply message.
240 * Version for Untyped IPC.
249 mach_msg_size_t reply_size
;
253 mach_msg_format_0_trailer_t
*trailer
;
254 register mig_hash_t
*ptr
;
257 * Find out corresponding mig_hash entry if any
260 register int key
= request
->ikm_header
->msgh_id
;
261 register int i
= MIG_HASH(key
);
262 register int max_iter
= mig_table_max_displ
;
265 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
266 while (key
!= ptr
->num
&& ptr
->num
&& --max_iter
);
268 if (!ptr
->routine
|| key
!= ptr
->num
) {
269 ptr
= (mig_hash_t
*)0;
270 reply_size
= mig_reply_size
;
272 reply_size
= ptr
->size
;
279 /* round up for trailer size */
280 reply_size
+= MAX_TRAILER_SIZE
;
281 reply
= ipc_kmsg_alloc(reply_size
);
283 if (reply
== IKM_NULL
) {
284 printf("ipc_kobject_server: dropping request\n");
285 ipc_kmsg_destroy(request
);
290 * Initialize reply message.
293 #define InP ((mach_msg_header_t *) request->ikm_header)
294 #define OutP ((mig_reply_error_t *) reply->ikm_header)
296 OutP
->NDR
= NDR_record
;
297 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
299 OutP
->Head
.msgh_bits
=
300 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP
->msgh_bits
), 0);
301 OutP
->Head
.msgh_remote_port
= InP
->msgh_local_port
;
302 OutP
->Head
.msgh_local_port
= MACH_PORT_NULL
;
303 OutP
->Head
.msgh_id
= InP
->msgh_id
+ 100;
310 * Find the routine to call, and call it
311 * to perform the kernel function
315 (*ptr
->routine
)(request
->ikm_header
, reply
->ikm_header
);
316 kernel_task
->messages_received
++;
319 if (!ipc_kobject_notify(request
->ikm_header
, reply
->ikm_header
)){
321 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
322 request
->ikm_header
->msgh_id
);
323 #endif /* MACH_IPC_TEST */
324 _MIG_MSGID_INVALID(request
->ikm_header
->msgh_id
);
326 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
330 kernel_task
->messages_received
++;
332 kernel_task
->messages_sent
++;
336 * Destroy destination. The following code differs from
337 * ipc_object_destroy in that we release the send-once
338 * right instead of generating a send-once notification
339 * (which would bring us here again, creating a loop).
340 * It also differs in that we only expect send or
341 * send-once rights, never receive rights.
343 * We set msgh_remote_port to IP_NULL so that the kmsg
344 * destroy routines don't try to destroy the port twice.
346 destp
= (ipc_port_t
*) &request
->ikm_header
->msgh_remote_port
;
347 switch (MACH_MSGH_BITS_REMOTE(request
->ikm_header
->msgh_bits
)) {
348 case MACH_MSG_TYPE_PORT_SEND
:
349 ipc_port_release_send(*destp
);
352 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
353 ipc_port_release_sonce(*destp
);
357 panic("ipc_kobject_server: strange destination rights");
361 if (!(reply
->ikm_header
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
362 ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
!= KERN_SUCCESS
)
363 kr
= ((mig_reply_error_t
*) reply
->ikm_header
)->RetCode
;
367 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
369 * The server function is responsible for the contents
370 * of the message. The reply port right is moved
371 * to the reply message, and we have deallocated
372 * the destination port right, so we just need
375 ipc_kmsg_free(request
);
379 * The message contents of the request are intact.
380 * Destroy everthing except the reply port right,
381 * which is needed in the reply message.
383 request
->ikm_header
->msgh_local_port
= MACH_PORT_NULL
;
384 ipc_kmsg_destroy(request
);
387 if (kr
== MIG_NO_REPLY
) {
389 * The server function will send a reply message
390 * using the reply port right, which it has saved.
393 ipc_kmsg_free(reply
);
396 } else if (!IP_VALID((ipc_port_t
)reply
->ikm_header
->msgh_remote_port
)) {
398 * Can't queue the reply message if the destination
399 * (the reply port) isn't valid.
402 ipc_kmsg_destroy(reply
);
407 trailer
= (mach_msg_format_0_trailer_t
*)
408 ((vm_offset_t
)reply
->ikm_header
+ (int)reply
->ikm_header
->msgh_size
);
410 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
411 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
412 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
418 * Routine: ipc_kobject_set
420 * Make a port represent a kernel object of the given type.
421 * The caller is responsible for handling refs for the
422 * kernel object, if necessary.
424 * Nothing locked. The port must be active if setting
425 * a kobject linkage. Clearing a linkage is OK on an
431 ipc_kobject_t kobject
,
432 ipc_kobject_type_t type
)
435 ipc_kobject_set_atomically(port
, kobject
, type
);
440 ipc_kobject_set_atomically(
442 ipc_kobject_t kobject
,
443 ipc_kobject_type_t type
)
445 assert(type
== IKOT_NONE
|| ip_active(port
));
447 port
->ip_spares
[2] = (port
->ip_bits
& IO_BITS_KOTYPE
);
448 #endif /* MACH_ASSERT */
449 port
->ip_bits
= (port
->ip_bits
&~ IO_BITS_KOTYPE
) | type
;
450 port
->ip_kobject
= kobject
;
454 * Routine: ipc_kobject_destroy
456 * Release any kernel object resources associated
457 * with the port, which is being destroyed.
459 * This should only be needed when resources are
460 * associated with a user's port. In the normal case,
461 * when the kernel is the receiver, the code calling
462 * ipc_port_dealloc_kernel should clean up the resources.
464 * The port is not locked, but it is dead.
471 switch (ip_kotype(port
)) {
474 mk_timer_port_destroy(port
);
477 case IKOT_NAMED_ENTRY
:
478 mach_destroy_memory_entry(port
);
481 case IKOT_HOST_NOTIFY
:
482 host_notify_port_destroy(port
);
493 mach_msg_header_t
*request_header
,
494 mach_msg_header_t
*reply_header
)
496 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
498 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
499 switch (request_header
->msgh_id
) {
500 case MACH_NOTIFY_NO_SENDERS
:
501 if(ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
505 * Bring the sequence number and mscount in
506 * line with ipc_port_destroy assertion.
508 port
->ip_mscount
= 0;
509 port
->ip_messages
.imq_seqno
= 0;
510 ipc_port_destroy(port
); /* releases lock */
513 if (ip_kotype(port
) == IKOT_UPL
) {
515 request_header
->msgh_remote_port
,
516 (mach_port_mscount_t
)
517 ((mach_no_senders_notification_t
*)
518 request_header
)->not_count
);
519 (ipc_port_t
)reply_header
->msgh_remote_port
526 case MACH_NOTIFY_PORT_DELETED
:
527 case MACH_NOTIFY_PORT_DESTROYED
:
528 case MACH_NOTIFY_SEND_ONCE
:
529 case MACH_NOTIFY_DEAD_NAME
:
535 switch (ip_kotype(port
)) {
538 case IKOT_IOKIT_OBJECT
:
539 case IKOT_IOKIT_CONNECT
:
540 case IKOT_IOKIT_SPARE
:
542 return iokit_notify(request_header
);
552 #include <mach_kdb.h>
553 #if MACH_COUNTERS && MACH_KDB
555 #include <ddb/db_output.h>
556 #include <ddb/db_sym.h>
558 #define printf kdbprintf
560 extern void kobjserver_stats(void);
561 extern void bucket_stats_print(mig_hash_t
*bucket
);
563 extern void kobjserver_stats_clear(void);
567 kobjserver_stats_clear(void)
570 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
571 mig_buckets
[i
].callcount
= 0;
576 kobjserver_stats(void)
578 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(struct mig_subsystem
);
579 register unsigned int howmany
;
580 register mach_msg_id_t j
, pos
, nentry
, range
;
582 db_printf("Kobject server call counts:\n");
583 for (i
= 0; i
< n
; i
++) {
585 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
587 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
588 if (!mig_e
[i
]->start
|| range
< 0) continue;
590 for (j
= 0; j
< range
; j
++) {
591 nentry
= j
+ mig_e
[i
]->start
;
592 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
593 mig_buckets
[pos
].num
;
594 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
595 if (mig_buckets
[pos
].num
== nentry
)
596 bucket_stats_print(&mig_buckets
[pos
]);
603 bucket_stats_print(mig_hash_t
*bucket
)
605 if (bucket
->callcount
) {
607 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
608 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
613 #endif /* MACH_COUNTERS && MACH_KDB */