2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 * File: kern/ipc_kobject.c
60 * Functions for letting a port represent a kernel object.
63 #include <mach_debug.h>
64 #include <mach_ipc_test.h>
65 #include <mach_machine_routines.h>
66 #include <norma_task.h>
68 #include <platforms.h>
71 #include <mach/port.h>
72 #include <mach/kern_return.h>
73 #include <mach/message.h>
74 #include <mach/mig_errors.h>
75 #include <mach/notify.h>
77 #include <kern/etap_macros.h>
78 #include <kern/ipc_mig.h>
79 #include <kern/ipc_kobject.h>
80 #include <kern/host_notify.h>
81 #include <kern/mk_timer.h>
82 #include <kern/misc_protos.h>
83 #include <ipc/ipc_kmsg.h>
84 #include <ipc/ipc_port.h>
85 #include <kern/counters.h>
89 * Routine: ipc_kobject_notify
91 * Deliver notifications to kobjects that care about them.
95 mach_msg_header_t
*request_header
,
96 mach_msg_header_t
*reply_header
);
102 mig_routine_t routine
;
105 mach_counter_t callcount
;
109 #define MAX_MIG_ENTRIES 1024
110 #define MIG_HASH(x) (x)
113 #define max(a,b) (((a) > (b)) ? (a) : (b))
116 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
117 int mig_table_max_displ
;
118 mach_msg_size_t mig_reply_size
;
121 #include <mach/mach_port_server.h>
122 #include <mach/mach_host_server.h>
123 #include <mach/host_priv_server.h>
124 #include <mach/host_security_server.h>
125 #include <mach/clock_server.h>
126 #include <mach/clock_priv_server.h>
127 #include <mach/ledger_server.h>
128 #include <mach/lock_set_server.h>
129 #include <default_pager/default_pager_object_server.h>
130 #include <mach/memory_object_server.h>
131 #include <mach/memory_object_control_server.h>
132 #include <mach/memory_object_default_server.h>
133 #include <mach/memory_object_name_server.h>
134 #include <mach/processor_server.h>
135 #include <mach/processor_set_server.h>
136 #include <mach/semaphore_server.h>
137 #include <mach/task_server.h>
138 #include <mach/vm_map_server.h>
139 #include <mach/thread_act_server.h>
140 #include <device/device_server.h>
141 #include <UserNotification/UNDReplyServer.h>
143 #if MACH_MACHINE_ROUTINES
144 #include <machine/machine_routines.h>
145 #endif /* MACH_MACHINE_ROUTINES */
147 #include <uk_xkern/xk_uproxy_server.h>
148 #endif /* XK_PROXY */
151 mig_subsystem_t mig_e
[] = {
152 (mig_subsystem_t
)&mach_port_subsystem
,
153 (mig_subsystem_t
)&mach_host_subsystem
,
154 (mig_subsystem_t
)&host_priv_subsystem
,
155 (mig_subsystem_t
)&host_security_subsystem
,
156 (mig_subsystem_t
)&clock_subsystem
,
157 (mig_subsystem_t
)&clock_priv_subsystem
,
158 (mig_subsystem_t
)&processor_subsystem
,
159 (mig_subsystem_t
)&processor_set_subsystem
,
160 (mig_subsystem_t
)&is_iokit_subsystem
,
161 (mig_subsystem_t
)&memory_object_name_subsystem
,
162 (mig_subsystem_t
)&lock_set_subsystem
,
163 (mig_subsystem_t
)&ledger_subsystem
,
164 (mig_subsystem_t
)&semaphore_subsystem
,
165 (mig_subsystem_t
)&task_subsystem
,
166 (mig_subsystem_t
)&thread_act_subsystem
,
167 (mig_subsystem_t
)&vm_map_subsystem
,
168 (mig_subsystem_t
)&UNDReply_subsystem
,
171 (mig_subsystem_t
)&do_uproxy_xk_uproxy_subsystem
,
172 #endif /* XK_PROXY */
173 #if MACH_MACHINE_ROUTINES
174 (mig_subsystem_t
)&MACHINE_SUBSYSTEM
,
175 #endif /* MACH_MACHINE_ROUTINES */
177 (mig_subsystem_t
)&mcmsg_info_subsystem
,
178 #endif /* MCMSG && iPSC860 */
184 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(mig_subsystem_t
);
185 register unsigned int howmany
;
186 register mach_msg_id_t j
, pos
, nentry
, range
;
188 for (i
= 0; i
< n
; i
++) {
189 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
190 if (!mig_e
[i
]->start
|| range
< 0)
191 panic("the msgh_ids in mig_e[] aren't valid!");
192 mig_reply_size
= max(mig_reply_size
, mig_e
[i
]->maxsize
);
194 for (j
= 0; j
< range
; j
++) {
195 if (mig_e
[i
]->routine
[j
].stub_routine
) {
196 /* Only put real entries in the table */
197 nentry
= j
+ mig_e
[i
]->start
;
198 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
199 mig_buckets
[pos
].num
;
200 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
201 if (mig_buckets
[pos
].num
== nentry
) {
202 printf("message id = %d\n", nentry
);
203 panic("multiple entries with the same msgh_id");
205 if (howmany
== MAX_MIG_ENTRIES
)
206 panic("the mig dispatch table is too small");
209 mig_buckets
[pos
].num
= nentry
;
210 mig_buckets
[pos
].routine
= mig_e
[i
]->routine
[j
].stub_routine
;
211 if (mig_e
[i
]->routine
[j
].max_reply_msg
)
212 mig_buckets
[pos
].size
= mig_e
[i
]->routine
[j
].max_reply_msg
;
214 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
216 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
220 printf("mig_table_max_displ = %d\n", mig_table_max_displ
);
225 * Routine: ipc_kobject_server
227 * Handle a message sent to the kernel.
228 * Generates a reply message.
229 * Version for Untyped IPC.
238 mach_msg_size_t reply_size
;
241 mig_routine_t routine
;
243 mach_msg_format_0_trailer_t
*trailer
;
244 register mig_hash_t
*ptr
;
247 /* Only fetch current thread if ETAP is configured */
248 ETAP_DATA_LOAD(th
, current_thread());
249 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
252 &request
->ikm_header
.msgh_id
,
255 * Find out corresponding mig_hash entry if any
258 register int key
= request
->ikm_header
.msgh_id
;
259 register int i
= MIG_HASH(key
);
260 register int max_iter
= mig_table_max_displ
;
263 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
264 while (key
!= ptr
->num
&& ptr
->num
&& --max_iter
);
266 if (!ptr
->routine
|| key
!= ptr
->num
) {
267 ptr
= (mig_hash_t
*)0;
268 reply_size
= mig_reply_size
;
270 reply_size
= ptr
->size
;
277 /* round up for trailer size */
278 reply_size
+= MAX_TRAILER_SIZE
;
279 reply
= ipc_kmsg_alloc(reply_size
);
281 if (reply
== IKM_NULL
) {
282 printf("ipc_kobject_server: dropping request\n");
283 ipc_kmsg_destroy(request
);
288 * Initialize reply message.
291 #define InP ((mach_msg_header_t *) &request->ikm_header)
292 #define OutP ((mig_reply_error_t *) &reply->ikm_header)
294 OutP
->NDR
= NDR_record
;
295 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
297 OutP
->Head
.msgh_bits
=
298 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP
->msgh_bits
), 0);
299 OutP
->Head
.msgh_remote_port
= InP
->msgh_local_port
;
300 OutP
->Head
.msgh_local_port
= MACH_PORT_NULL
;
301 OutP
->Head
.msgh_id
= InP
->msgh_id
+ 100;
308 * Find the routine to call, and call it
309 * to perform the kernel function
313 (*ptr
->routine
)(&request
->ikm_header
, &reply
->ikm_header
);
314 kernel_task
->messages_received
++;
317 if (!ipc_kobject_notify(&request
->ikm_header
, &reply
->ikm_header
)){
319 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
320 request
->ikm_header
.msgh_id
);
321 #endif /* MACH_IPC_TEST */
322 _MIG_MSGID_INVALID(request
->ikm_header
.msgh_id
);
324 ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
328 kernel_task
->messages_received
++;
330 kernel_task
->messages_sent
++;
334 * Destroy destination. The following code differs from
335 * ipc_object_destroy in that we release the send-once
336 * right instead of generating a send-once notification
337 * (which would bring us here again, creating a loop).
338 * It also differs in that we only expect send or
339 * send-once rights, never receive rights.
341 * We set msgh_remote_port to IP_NULL so that the kmsg
342 * destroy routines don't try to destroy the port twice.
344 destp
= (ipc_port_t
*) &request
->ikm_header
.msgh_remote_port
;
345 switch (MACH_MSGH_BITS_REMOTE(request
->ikm_header
.msgh_bits
)) {
346 case MACH_MSG_TYPE_PORT_SEND
:
347 ipc_port_release_send(*destp
);
350 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
351 ipc_port_release_sonce(*destp
);
355 panic("ipc_kobject_server: strange destination rights");
359 if (!(reply
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
360 ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
!= KERN_SUCCESS
)
361 kr
= ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
;
365 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
367 * The server function is responsible for the contents
368 * of the message. The reply port right is moved
369 * to the reply message, and we have deallocated
370 * the destination port right, so we just need
373 ipc_kmsg_free(request
);
377 * The message contents of the request are intact.
378 * Destroy everthing except the reply port right,
379 * which is needed in the reply message.
381 request
->ikm_header
.msgh_local_port
= MACH_PORT_NULL
;
382 ipc_kmsg_destroy(request
);
385 if (kr
== MIG_NO_REPLY
) {
387 * The server function will send a reply message
388 * using the reply port right, which it has saved.
391 ipc_kmsg_free(reply
);
393 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
396 &request
->ikm_header
.msgh_id
,
400 } else if (!IP_VALID((ipc_port_t
)reply
->ikm_header
.msgh_remote_port
)) {
402 * Can't queue the reply message if the destination
403 * (the reply port) isn't valid.
406 ipc_kmsg_destroy(reply
);
408 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
411 &request
->ikm_header
.msgh_id
,
417 trailer
= (mach_msg_format_0_trailer_t
*)
418 ((vm_offset_t
)&reply
->ikm_header
+ (int)reply
->ikm_header
.msgh_size
);
419 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
420 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
421 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
423 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
426 &request
->ikm_header
.msgh_id
,
433 * Routine: ipc_kobject_set
435 * Make a port represent a kernel object of the given type.
436 * The caller is responsible for handling refs for the
437 * kernel object, if necessary.
439 * Nothing locked. The port must be active if setting
440 * a kobject linkage. Clearing a linkage is OK on an
446 ipc_kobject_t kobject
,
447 ipc_kobject_type_t type
)
450 ipc_kobject_set_atomically(port
, kobject
, type
);
455 ipc_kobject_set_atomically(
457 ipc_kobject_t kobject
,
458 ipc_kobject_type_t type
)
460 assert(type
== IKOT_NONE
|| ip_active(port
));
462 port
->ip_spares
[2] = (port
->ip_bits
& IO_BITS_KOTYPE
);
463 #endif /* MACH_ASSERT */
464 port
->ip_bits
= (port
->ip_bits
&~ IO_BITS_KOTYPE
) | type
;
465 port
->ip_kobject
= kobject
;
469 * Routine: ipc_kobject_destroy
471 * Release any kernel object resources associated
472 * with the port, which is being destroyed.
474 * This should only be needed when resources are
475 * associated with a user's port. In the normal case,
476 * when the kernel is the receiver, the code calling
477 * ipc_port_dealloc_kernel should clean up the resources.
479 * The port is not locked, but it is dead.
486 switch (ip_kotype(port
)) {
489 mk_timer_port_destroy(port
);
492 case IKOT_NAMED_ENTRY
:
493 mach_destroy_memory_entry(port
);
496 case IKOT_HOST_NOTIFY
:
497 host_notify_port_destroy(port
);
506 extern int vnode_pager_workaround
;
510 mach_msg_header_t
*request_header
,
511 mach_msg_header_t
*reply_header
)
513 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
514 mig_subsystem_t paging_subsystem_object
;
515 mach_port_seqno_t seqno
;
517 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
518 switch (request_header
->msgh_id
) {
519 case MACH_NOTIFY_NO_SENDERS
:
520 if(ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
524 * Bring the sequence number and mscount in
525 * line with ipc_port_destroy assertion.
527 port
->ip_mscount
= 0;
528 port
->ip_messages
.imq_seqno
= 0;
529 ipc_port_destroy(port
); /* releases lock */
532 if (ip_kotype(port
) == IKOT_UPL
) {
534 (ipc_port_t
)request_header
->msgh_remote_port
,
535 (mach_port_mscount_t
)
536 ((mach_no_senders_notification_t
*)
537 request_header
)->not_count
);
538 (ipc_port_t
)reply_header
->msgh_remote_port
545 case MACH_NOTIFY_PORT_DELETED
:
546 case MACH_NOTIFY_PORT_DESTROYED
:
547 case MACH_NOTIFY_SEND_ONCE
:
548 case MACH_NOTIFY_DEAD_NAME
:
554 switch (ip_kotype(port
)) {
557 case IKOT_IOKIT_OBJECT
:
558 case IKOT_IOKIT_CONNECT
:
559 case IKOT_IOKIT_SPARE
:
561 extern boolean_t
iokit_notify( mach_msg_header_t
*msg
);
563 return iokit_notify(request_header
);
573 #include <mach_kdb.h>
574 #if MACH_COUNTERS && MACH_KDB
576 #include <ddb/db_output.h>
577 #include <ddb/db_sym.h>
579 #define printf kdbprintf
581 extern void kobjserver_stats(void);
582 extern void bucket_stats_print(mig_hash_t
*bucket
);
584 extern void kobjserver_stats_clear(void);
588 kobjserver_stats_clear(void)
591 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
592 mig_buckets
[i
].callcount
= 0;
597 kobjserver_stats(void)
599 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(mig_subsystem_t
);
600 register unsigned int howmany
;
601 register mach_msg_id_t j
, pos
, nentry
, range
;
603 db_printf("Kobject server call counts:\n");
604 for (i
= 0; i
< n
; i
++) {
606 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
608 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
609 if (!mig_e
[i
]->start
|| range
< 0) continue;
611 for (j
= 0; j
< range
; j
++) {
612 nentry
= j
+ mig_e
[i
]->start
;
613 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
614 mig_buckets
[pos
].num
;
615 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
616 if (mig_buckets
[pos
].num
== nentry
)
617 bucket_stats_print(&mig_buckets
[pos
]);
624 bucket_stats_print(mig_hash_t
*bucket
)
626 if (bucket
->callcount
) {
628 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
629 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
634 #endif /* MACH_COUNTERS && MACH_KDB */