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/misc_protos.h>
81 #include <kern/mk_timer.h>
82 #include <ipc/ipc_kmsg.h>
83 #include <ipc/ipc_port.h>
84 #include <kern/counters.h>
88 * Routine: ipc_kobject_notify
90 * Deliver notifications to kobjects that care about them.
94 mach_msg_header_t
*request_header
,
95 mach_msg_header_t
*reply_header
);
101 mig_routine_t routine
;
104 mach_counter_t callcount
;
108 #define MAX_MIG_ENTRIES 1024
109 #define MIG_HASH(x) (x)
112 #define max(a,b) (((a) > (b)) ? (a) : (b))
115 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
116 int mig_table_max_displ
;
117 mach_msg_size_t mig_reply_size
;
120 #include <mach/mach_port_server.h>
121 #include <mach/mach_host_server.h>
122 #include <mach/host_priv_server.h>
123 #include <mach/host_security_server.h>
124 #include <mach/clock_server.h>
125 #include <mach/clock_priv_server.h>
126 #include <mach/ledger_server.h>
127 #include <mach/lock_set_server.h>
128 #include <default_pager/default_pager_object_server.h>
129 #include <mach/memory_object_server.h>
130 #include <mach/memory_object_control_server.h>
131 #include <mach/memory_object_default_server.h>
132 #include <mach/memory_object_name_server.h>
133 #include <mach/processor_server.h>
134 #include <mach/processor_set_server.h>
135 #include <mach/semaphore_server.h>
136 #include <mach/task_server.h>
137 #include <mach/vm_map_server.h>
138 #include <mach/thread_act_server.h>
139 #include <device/device_server.h>
140 #include <UserNotification/UNDReplyServer.h>
142 #if MACH_MACHINE_ROUTINES
143 #include <machine/machine_routines.h>
144 #endif /* MACH_MACHINE_ROUTINES */
146 #include <uk_xkern/xk_uproxy_server.h>
147 #endif /* XK_PROXY */
150 mig_subsystem_t mig_e
[] = {
151 (mig_subsystem_t
)&mach_port_subsystem
,
152 (mig_subsystem_t
)&mach_host_subsystem
,
153 (mig_subsystem_t
)&host_priv_subsystem
,
154 (mig_subsystem_t
)&host_security_subsystem
,
155 (mig_subsystem_t
)&clock_subsystem
,
156 (mig_subsystem_t
)&clock_priv_subsystem
,
157 (mig_subsystem_t
)&processor_subsystem
,
158 (mig_subsystem_t
)&processor_set_subsystem
,
159 (mig_subsystem_t
)&is_iokit_subsystem
,
160 (mig_subsystem_t
)&memory_object_name_subsystem
,
161 (mig_subsystem_t
)&lock_set_subsystem
,
162 (mig_subsystem_t
)&ledger_subsystem
,
163 (mig_subsystem_t
)&semaphore_subsystem
,
164 (mig_subsystem_t
)&task_subsystem
,
165 (mig_subsystem_t
)&thread_act_subsystem
,
166 (mig_subsystem_t
)&vm_map_subsystem
,
167 (mig_subsystem_t
)&UNDReply_subsystem
,
170 (mig_subsystem_t
)&do_uproxy_xk_uproxy_subsystem
,
171 #endif /* XK_PROXY */
172 #if MACH_MACHINE_ROUTINES
173 (mig_subsystem_t
)&MACHINE_SUBSYSTEM
,
174 #endif /* MACH_MACHINE_ROUTINES */
176 (mig_subsystem_t
)&mcmsg_info_subsystem
,
177 #endif /* MCMSG && iPSC860 */
183 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(mig_subsystem_t
);
184 register unsigned int howmany
;
185 register mach_msg_id_t j
, pos
, nentry
, range
;
187 for (i
= 0; i
< n
; i
++) {
188 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
189 if (!mig_e
[i
]->start
|| range
< 0)
190 panic("the msgh_ids in mig_e[] aren't valid!");
191 mig_reply_size
= max(mig_reply_size
, mig_e
[i
]->maxsize
);
193 for (j
= 0; j
< range
; j
++) {
194 if (mig_e
[i
]->routine
[j
].stub_routine
) {
195 /* Only put real entries in the table */
196 nentry
= j
+ mig_e
[i
]->start
;
197 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
198 mig_buckets
[pos
].num
;
199 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
200 if (mig_buckets
[pos
].num
== nentry
) {
201 printf("message id = %d\n", nentry
);
202 panic("multiple entries with the same msgh_id");
204 if (howmany
== MAX_MIG_ENTRIES
)
205 panic("the mig dispatch table is too small");
208 mig_buckets
[pos
].num
= nentry
;
209 mig_buckets
[pos
].routine
= mig_e
[i
]->routine
[j
].stub_routine
;
210 if (mig_e
[i
]->routine
[j
].max_reply_msg
)
211 mig_buckets
[pos
].size
= mig_e
[i
]->routine
[j
].max_reply_msg
;
213 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
215 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
219 printf("mig_table_max_displ = %d\n", mig_table_max_displ
);
224 * Routine: ipc_kobject_server
226 * Handle a message sent to the kernel.
227 * Generates a reply message.
228 * Version for Untyped IPC.
237 mach_msg_size_t reply_size
;
240 mig_routine_t routine
;
242 mach_msg_format_0_trailer_t
*trailer
;
243 register mig_hash_t
*ptr
;
246 /* Only fetch current thread if ETAP is configured */
247 ETAP_DATA_LOAD(th
, current_thread());
248 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
251 &request
->ikm_header
.msgh_id
,
254 * Find out corresponding mig_hash entry if any
257 register int key
= request
->ikm_header
.msgh_id
;
258 register int i
= MIG_HASH(key
);
259 register int max_iter
= mig_table_max_displ
;
262 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
263 while (key
!= ptr
->num
&& ptr
->num
&& --max_iter
);
265 if (!ptr
->routine
|| key
!= ptr
->num
) {
266 ptr
= (mig_hash_t
*)0;
267 reply_size
= mig_reply_size
;
269 reply_size
= ptr
->size
;
276 /* round up for trailer size */
277 reply_size
+= MAX_TRAILER_SIZE
;
278 reply
= ipc_kmsg_alloc(reply_size
);
280 if (reply
== IKM_NULL
) {
281 printf("ipc_kobject_server: dropping request\n");
282 ipc_kmsg_destroy(request
);
287 * Initialize reply message.
290 #define InP ((mach_msg_header_t *) &request->ikm_header)
291 #define OutP ((mig_reply_error_t *) &reply->ikm_header)
293 OutP
->NDR
= NDR_record
;
294 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
296 OutP
->Head
.msgh_bits
=
297 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP
->msgh_bits
), 0);
298 OutP
->Head
.msgh_remote_port
= InP
->msgh_local_port
;
299 OutP
->Head
.msgh_local_port
= MACH_PORT_NULL
;
300 OutP
->Head
.msgh_id
= InP
->msgh_id
+ 100;
307 * Find the routine to call, and call it
308 * to perform the kernel function
312 (*ptr
->routine
)(&request
->ikm_header
, &reply
->ikm_header
);
313 kernel_task
->messages_received
++;
316 if (!ipc_kobject_notify(&request
->ikm_header
, &reply
->ikm_header
)){
318 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
319 request
->ikm_header
.msgh_id
);
320 #endif /* MACH_IPC_TEST */
321 _MIG_MSGID_INVALID(request
->ikm_header
.msgh_id
);
323 ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
327 kernel_task
->messages_received
++;
329 kernel_task
->messages_sent
++;
333 * Destroy destination. The following code differs from
334 * ipc_object_destroy in that we release the send-once
335 * right instead of generating a send-once notification
336 * (which would bring us here again, creating a loop).
337 * It also differs in that we only expect send or
338 * send-once rights, never receive rights.
340 * We set msgh_remote_port to IP_NULL so that the kmsg
341 * destroy routines don't try to destroy the port twice.
343 destp
= (ipc_port_t
*) &request
->ikm_header
.msgh_remote_port
;
344 switch (MACH_MSGH_BITS_REMOTE(request
->ikm_header
.msgh_bits
)) {
345 case MACH_MSG_TYPE_PORT_SEND
:
346 ipc_port_release_send(*destp
);
349 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
350 ipc_port_release_sonce(*destp
);
354 panic("ipc_kobject_server: strange destination rights");
358 if (!(reply
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
359 ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
!= KERN_SUCCESS
)
360 kr
= ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
;
364 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
366 * The server function is responsible for the contents
367 * of the message. The reply port right is moved
368 * to the reply message, and we have deallocated
369 * the destination port right, so we just need
372 ipc_kmsg_free(request
);
376 * The message contents of the request are intact.
377 * Destroy everthing except the reply port right,
378 * which is needed in the reply message.
380 request
->ikm_header
.msgh_local_port
= MACH_PORT_NULL
;
381 ipc_kmsg_destroy(request
);
384 if (kr
== MIG_NO_REPLY
) {
386 * The server function will send a reply message
387 * using the reply port right, which it has saved.
390 ipc_kmsg_free(reply
);
392 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
395 &request
->ikm_header
.msgh_id
,
399 } else if (!IP_VALID((ipc_port_t
)reply
->ikm_header
.msgh_remote_port
)) {
401 * Can't queue the reply message if the destination
402 * (the reply port) isn't valid.
405 ipc_kmsg_destroy(reply
);
407 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
410 &request
->ikm_header
.msgh_id
,
416 trailer
= (mach_msg_format_0_trailer_t
*)
417 ((vm_offset_t
)&reply
->ikm_header
+ (int)reply
->ikm_header
.msgh_size
);
418 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
419 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
420 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
422 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
425 &request
->ikm_header
.msgh_id
,
432 * Routine: ipc_kobject_set
434 * Make a port represent a kernel object of the given type.
435 * The caller is responsible for handling refs for the
436 * kernel object, if necessary.
438 * Nothing locked. The port must be active if setting
439 * a kobject linkage. Clearing a linkage is OK on an
445 ipc_kobject_t kobject
,
446 ipc_kobject_type_t type
)
449 ipc_kobject_set_atomically(port
, kobject
, type
);
454 ipc_kobject_set_atomically(
456 ipc_kobject_t kobject
,
457 ipc_kobject_type_t type
)
459 assert(type
== IKOT_NONE
|| ip_active(port
));
461 port
->ip_spares
[2] = (port
->ip_bits
& IO_BITS_KOTYPE
);
462 #endif /* MACH_ASSERT */
463 port
->ip_bits
= (port
->ip_bits
&~ IO_BITS_KOTYPE
) | type
;
464 port
->ip_kobject
= kobject
;
468 * Routine: ipc_kobject_destroy
470 * Release any kernel object resources associated
471 * with the port, which is being destroyed.
473 * This should only be needed when resources are
474 * associated with a user's port. In the normal case,
475 * when the kernel is the receiver, the code calling
476 * ipc_port_dealloc_kernel should clean up the resources.
478 * The port is not locked, but it is dead.
485 switch (ip_kotype(port
)) {
488 mk_timer_port_destroy(port
);
491 case IKOT_NAMED_ENTRY
:
492 mach_destroy_memory_entry(port
);
495 default: /* XXX (bogon) */
501 extern int vnode_pager_workaround
;
505 mach_msg_header_t
*request_header
,
506 mach_msg_header_t
*reply_header
)
508 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
509 mig_subsystem_t paging_subsystem_object
;
510 mach_port_seqno_t seqno
;
512 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
513 switch (request_header
->msgh_id
) {
514 case MACH_NOTIFY_NO_SENDERS
:
515 if(ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
519 * Bring the sequence number and mscount in
520 * line with ipc_port_destroy assertion.
522 port
->ip_mscount
= 0;
523 port
->ip_messages
.imq_seqno
= 0;
524 ipc_port_destroy(port
); /* releases lock */
527 if (ip_kotype(port
) == IKOT_UPL
) {
529 (ipc_port_t
)request_header
->msgh_remote_port
,
530 (mach_port_mscount_t
)
531 ((mach_no_senders_notification_t
*)
532 request_header
)->not_count
);
533 (ipc_port_t
)reply_header
->msgh_remote_port
540 case MACH_NOTIFY_PORT_DELETED
:
541 case MACH_NOTIFY_PORT_DESTROYED
:
542 case MACH_NOTIFY_SEND_ONCE
:
543 case MACH_NOTIFY_DEAD_NAME
:
549 switch (ip_kotype(port
)) {
552 case IKOT_IOKIT_OBJECT
:
553 case IKOT_IOKIT_CONNECT
:
554 case IKOT_IOKIT_SPARE
:
556 extern boolean_t
iokit_notify( mach_msg_header_t
*msg
);
558 return iokit_notify(request_header
);
568 #include <mach_kdb.h>
569 #if MACH_COUNTERS && MACH_KDB
571 #include <ddb/db_output.h>
572 #include <ddb/db_sym.h>
574 #define printf kdbprintf
576 extern void kobjserver_stats(void);
577 extern void bucket_stats_print(mig_hash_t
*bucket
);
579 extern void kobjserver_stats_clear(void);
583 kobjserver_stats_clear(void)
586 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
587 mig_buckets
[i
].callcount
= 0;
592 kobjserver_stats(void)
594 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(mig_subsystem_t
);
595 register unsigned int howmany
;
596 register mach_msg_id_t j
, pos
, nentry
, range
;
598 db_printf("Kobject server call counts:\n");
599 for (i
= 0; i
< n
; i
++) {
601 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
603 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
604 if (!mig_e
[i
]->start
|| range
< 0) continue;
606 for (j
= 0; j
< range
; j
++) {
607 nentry
= j
+ mig_e
[i
]->start
;
608 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
609 mig_buckets
[pos
].num
;
610 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
611 if (mig_buckets
[pos
].num
== nentry
)
612 bucket_stats_print(&mig_buckets
[pos
]);
619 bucket_stats_print(mig_hash_t
*bucket
)
621 if (bucket
->callcount
) {
623 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
624 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
629 #endif /* MACH_COUNTERS && MACH_KDB */