2 * Copyright (c) 2000 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>
74 #include <kern/etap_macros.h>
75 #include <kern/ipc_mig.h>
76 #include <kern/ipc_kobject.h>
77 #include <kern/misc_protos.h>
78 #include <kern/mk_timer.h>
79 #include <ipc/ipc_kmsg.h>
80 #include <ipc/ipc_port.h>
81 #include <kern/counters.h>
85 * Routine: ipc_kobject_notify
87 * Deliver notifications to kobjects that care about them.
91 mach_msg_header_t
*request_header
,
92 mach_msg_header_t
*reply_header
);
98 mig_routine_t routine
;
101 mach_counter_t callcount
;
105 #define MAX_MIG_ENTRIES 1024
106 #define MIG_HASH(x) (x)
109 #define max(a,b) (((a) > (b)) ? (a) : (b))
112 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
113 int mig_table_max_displ
;
114 mach_msg_size_t mig_reply_size
;
117 #include <mach/mach_port_server.h>
118 #include <mach/mach_host_server.h>
119 #include <mach/host_priv_server.h>
120 #include <mach/host_security_server.h>
121 #include <mach/clock_server.h>
122 #include <mach/clock_priv_server.h>
123 #include <mach/ledger_server.h>
124 #include <mach/lock_set_server.h>
125 #include <default_pager/default_pager_object_server.h>
126 #include <mach/memory_object_server.h>
127 #include <mach/memory_object_control_server.h>
128 #include <mach/memory_object_default_server.h>
129 #include <mach/memory_object_name_server.h>
130 #include <mach/processor_server.h>
131 #include <mach/processor_set_server.h>
132 #include <mach/semaphore_server.h>
133 #include <mach/task_server.h>
134 #include <mach/vm_map_server.h>
135 #include <mach/thread_act_server.h>
136 #include <device/device_server.h>
137 #include <UserNotification/UNDReplyServer.h>
139 #if MACH_MACHINE_ROUTINES
140 #include <machine/machine_routines.h>
141 #endif /* MACH_MACHINE_ROUTINES */
143 #include <uk_xkern/xk_uproxy_server.h>
144 #endif /* XK_PROXY */
147 mig_subsystem_t mig_e
[] = {
148 (mig_subsystem_t
)&mach_port_subsystem
,
149 (mig_subsystem_t
)&mach_host_subsystem
,
150 (mig_subsystem_t
)&host_priv_subsystem
,
151 (mig_subsystem_t
)&host_security_subsystem
,
152 (mig_subsystem_t
)&clock_subsystem
,
153 (mig_subsystem_t
)&clock_priv_subsystem
,
154 (mig_subsystem_t
)&processor_subsystem
,
155 (mig_subsystem_t
)&processor_set_subsystem
,
156 (mig_subsystem_t
)&is_iokit_subsystem
,
157 (mig_subsystem_t
)&memory_object_name_subsystem
,
158 (mig_subsystem_t
)&lock_set_subsystem
,
159 (mig_subsystem_t
)&ledger_subsystem
,
160 (mig_subsystem_t
)&semaphore_subsystem
,
161 (mig_subsystem_t
)&task_subsystem
,
162 (mig_subsystem_t
)&thread_act_subsystem
,
163 (mig_subsystem_t
)&vm_map_subsystem
,
164 (mig_subsystem_t
)&UNDReply_subsystem
,
167 (mig_subsystem_t
)&do_uproxy_xk_uproxy_subsystem
,
168 #endif /* XK_PROXY */
169 #if MACH_MACHINE_ROUTINES
170 (mig_subsystem_t
)&MACHINE_SUBSYSTEM
,
171 #endif /* MACH_MACHINE_ROUTINES */
173 (mig_subsystem_t
)&mcmsg_info_subsystem
,
174 #endif /* MCMSG && iPSC860 */
180 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(mig_subsystem_t
);
181 register unsigned int howmany
;
182 register mach_msg_id_t j
, pos
, nentry
, range
;
184 for (i
= 0; i
< n
; i
++) {
185 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
186 if (!mig_e
[i
]->start
|| range
< 0)
187 panic("the msgh_ids in mig_e[] aren't valid!");
188 mig_reply_size
= max(mig_reply_size
, mig_e
[i
]->maxsize
);
190 for (j
= 0; j
< range
; j
++) {
191 if (mig_e
[i
]->routine
[j
].stub_routine
) {
192 /* Only put real entries in the table */
193 nentry
= j
+ mig_e
[i
]->start
;
194 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
195 mig_buckets
[pos
].num
;
196 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
197 if (mig_buckets
[pos
].num
== nentry
) {
198 printf("message id = %d\n", nentry
);
199 panic("multiple entries with the same msgh_id");
201 if (howmany
== MAX_MIG_ENTRIES
)
202 panic("the mig dispatch table is too small");
205 mig_buckets
[pos
].num
= nentry
;
206 mig_buckets
[pos
].routine
= mig_e
[i
]->routine
[j
].stub_routine
;
207 if (mig_e
[i
]->routine
[j
].max_reply_msg
)
208 mig_buckets
[pos
].size
= mig_e
[i
]->routine
[j
].max_reply_msg
;
210 mig_buckets
[pos
].size
= mig_e
[i
]->maxsize
;
212 mig_table_max_displ
= max(howmany
, mig_table_max_displ
);
216 printf("mig_table_max_displ = %d\n", mig_table_max_displ
);
221 * Routine: ipc_kobject_server
223 * Handle a message sent to the kernel.
224 * Generates a reply message.
225 * Version for Untyped IPC.
234 mach_msg_size_t reply_size
;
237 mig_routine_t routine
;
239 mach_msg_format_0_trailer_t
*trailer
;
240 register mig_hash_t
*ptr
;
243 /* Only fetch current thread if ETAP is configured */
244 ETAP_DATA_LOAD(th
, current_thread());
245 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
248 &request
->ikm_header
.msgh_id
,
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
);
389 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
392 &request
->ikm_header
.msgh_id
,
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
);
404 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
407 &request
->ikm_header
.msgh_id
,
413 trailer
= (mach_msg_format_0_trailer_t
*)
414 ((vm_offset_t
)&reply
->ikm_header
+ (int)reply
->ikm_header
.msgh_size
);
415 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
416 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
417 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
419 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
422 &request
->ikm_header
.msgh_id
,
429 * Routine: ipc_kobject_set
431 * Make a port represent a kernel object of the given type.
432 * The caller is responsible for handling refs for the
433 * kernel object, if necessary.
435 * Nothing locked. The port must be active if setting
436 * a kobject linkage. Clearing a linkage is OK on an
442 ipc_kobject_t kobject
,
443 ipc_kobject_type_t type
)
446 ipc_kobject_set_atomically(port
, kobject
, type
);
451 ipc_kobject_set_atomically(
453 ipc_kobject_t kobject
,
454 ipc_kobject_type_t type
)
456 assert(type
== IKOT_NONE
|| ip_active(port
));
458 port
->ip_spares
[2] = (port
->ip_bits
& IO_BITS_KOTYPE
);
459 #endif /* MACH_ASSERT */
460 port
->ip_bits
= (port
->ip_bits
&~ IO_BITS_KOTYPE
) | type
;
461 port
->ip_kobject
= kobject
;
465 * Routine: ipc_kobject_destroy
467 * Release any kernel object resources associated
468 * with the port, which is being destroyed.
470 * This should only be needed when resources are
471 * associated with a user's port. In the normal case,
472 * when the kernel is the receiver, the code calling
473 * ipc_port_dealloc_kernel should clean up the resources.
475 * The port is not locked, but it is dead.
482 switch (ip_kotype(port
)) {
485 mk_timer_port_destroy(port
);
488 case IKOT_NAMED_ENTRY
:
489 mach_destroy_memory_entry(port
);
492 default: /* XXX (bogon) */
498 extern int vnode_pager_workaround
;
502 mach_msg_header_t
*request_header
,
503 mach_msg_header_t
*reply_header
)
505 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
506 mig_subsystem_t paging_subsystem_object
;
507 mach_port_seqno_t seqno
;
509 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
510 switch (request_header
->msgh_id
) {
511 case MACH_NOTIFY_NO_SENDERS
:
512 if(ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
516 * Bring the sequence number and mscount in
517 * line with ipc_port_destroy assertion.
519 port
->ip_mscount
= 0;
520 port
->ip_messages
.imq_seqno
= 0;
521 ipc_port_destroy(port
); /* releases lock */
524 if (ip_kotype(port
) == IKOT_UPL
) {
526 (ipc_port_t
)request_header
->msgh_remote_port
,
527 (mach_port_mscount_t
)
528 ((mach_no_senders_notification_t
*)
529 request_header
)->not_count
);
530 (ipc_port_t
)reply_header
->msgh_remote_port
537 case MACH_NOTIFY_PORT_DELETED
:
538 case MACH_NOTIFY_PORT_DESTROYED
:
539 case MACH_NOTIFY_SEND_ONCE
:
540 case MACH_NOTIFY_DEAD_NAME
:
546 switch (ip_kotype(port
)) {
549 case IKOT_IOKIT_OBJECT
:
550 case IKOT_IOKIT_CONNECT
:
551 case IKOT_IOKIT_SPARE
:
553 extern boolean_t
iokit_notify( mach_msg_header_t
*msg
);
555 return iokit_notify(request_header
);
565 #include <mach_kdb.h>
566 #if MACH_COUNTERS && MACH_KDB
568 #include <ddb/db_output.h>
569 #include <ddb/db_sym.h>
571 #define printf kdbprintf
573 extern void kobjserver_stats(void);
574 extern void bucket_stats_print(mig_hash_t
*bucket
);
576 extern void kobjserver_stats_clear(void);
580 kobjserver_stats_clear(void)
583 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
584 mig_buckets
[i
].callcount
= 0;
589 kobjserver_stats(void)
591 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(mig_subsystem_t
);
592 register unsigned int howmany
;
593 register mach_msg_id_t j
, pos
, nentry
, range
;
595 db_printf("Kobject server call counts:\n");
596 for (i
= 0; i
< n
; i
++) {
598 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
600 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
601 if (!mig_e
[i
]->start
|| range
< 0) continue;
603 for (j
= 0; j
< range
; j
++) {
604 nentry
= j
+ mig_e
[i
]->start
;
605 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
606 mig_buckets
[pos
].num
;
607 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
608 if (mig_buckets
[pos
].num
== nentry
)
609 bucket_stats_print(&mig_buckets
[pos
]);
616 bucket_stats_print(mig_hash_t
*bucket
)
618 if (bucket
->callcount
) {
620 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
621 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
626 #endif /* MACH_COUNTERS && MACH_KDB */