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>
73 #include <kern/etap_macros.h>
74 #include <kern/ipc_mig.h>
75 #include <kern/ipc_kobject.h>
76 #include <kern/misc_protos.h>
77 #include <kern/mk_timer.h>
78 #include <ipc/ipc_object.h>
79 #include <ipc/ipc_kmsg.h>
80 #include <ipc/ipc_port.h>
81 #include <kern/counters.h>
85 def_pager_hash_insert(
86 ipc_port_t name_port
);
87 void pager_mux_hash_insert(
90 void pager_mux_hash_delete(
92 rpc_subsystem_t
pager_mux_hash_lookup(
97 * Routine: ipc_kobject_notify
99 * Deliver notifications to kobjects that care about them.
103 mach_msg_header_t
*request_header
,
104 mach_msg_header_t
*reply_header
);
106 #include <mach/ndr.h>
110 mig_routine_t routine
;
113 mach_counter_t callcount
;
117 #define MAX_MIG_ENTRIES 1024
118 #define MIG_HASH(x) (x)
121 #define max(a,b) (((a) > (b)) ? (a) : (b))
124 mig_hash_t mig_buckets
[MAX_MIG_ENTRIES
];
125 int mig_table_max_displ
;
126 mach_msg_size_t mig_reply_size
;
129 #include <mach/mach_port_server.h>
130 #include <mach/mach_host_server.h>
131 #include <mach/host_priv_server.h>
132 #include <mach/host_security_server.h>
133 #include <mach/clock_server.h>
134 #include <mach/clock_priv_server.h>
135 #include <mach/ledger_server.h>
136 #include <mach/lock_set_server.h>
137 #include <default_pager/default_pager_object_server.h>
138 #include <mach/memory_object_server.h>
139 #include <mach/memory_object_control_server.h>
140 #include <mach/memory_object_default_server.h>
141 #include <mach/memory_object_name_server.h>
142 #include <mach/processor_server.h>
143 #include <mach/processor_set_server.h>
144 #include <mach/semaphore_server.h>
145 #include <mach/task_server.h>
146 #include <mach/vm_map_server.h>
147 #include <mach/thread_act_server.h>
148 #include <device/device_server.h>
149 #include <UserNotification/UNDReplyServer.h>
151 #if MACH_MACHINE_ROUTINES
152 #include <machine/machine_routines.h>
153 #endif /* MACH_MACHINE_ROUTINES */
155 #include <uk_xkern/xk_uproxy_server.h>
156 #endif /* XK_PROXY */
159 rpc_subsystem_t mig_e
[] = {
160 (rpc_subsystem_t
)&mach_port_subsystem
,
161 (rpc_subsystem_t
)&mach_host_subsystem
,
162 (rpc_subsystem_t
)&host_priv_subsystem
,
163 (rpc_subsystem_t
)&host_security_subsystem
,
164 (rpc_subsystem_t
)&clock_subsystem
,
165 (rpc_subsystem_t
)&clock_priv_subsystem
,
166 (rpc_subsystem_t
)&processor_subsystem
,
167 (rpc_subsystem_t
)&processor_set_subsystem
,
168 (rpc_subsystem_t
)&is_iokit_subsystem
,
169 (rpc_subsystem_t
)&dp_memory_object_subsystem
,
170 (rpc_subsystem_t
)&dp_memory_object_default_subsystem
,
171 (rpc_subsystem_t
)&memory_object_name_subsystem
,
172 (rpc_subsystem_t
)&default_pager_object_subsystem
,
173 (rpc_subsystem_t
)&lock_set_subsystem
,
174 (rpc_subsystem_t
)&ledger_subsystem
,
175 (rpc_subsystem_t
)&semaphore_subsystem
,
176 (rpc_subsystem_t
)&task_subsystem
,
177 (rpc_subsystem_t
)&thread_act_subsystem
,
178 (rpc_subsystem_t
)&vm_map_subsystem
,
179 (rpc_subsystem_t
)&UNDReply_subsystem
,
182 (rpc_subsystem_t
)&do_uproxy_xk_uproxy_subsystem
,
183 #endif /* XK_PROXY */
184 #if MACH_MACHINE_ROUTINES
185 (rpc_subsystem_t
)&MACHINE_SUBSYSTEM
,
186 #endif /* MACH_MACHINE_ROUTINES */
188 (rpc_subsystem_t
)&mcmsg_info_subsystem
,
189 #endif /* MCMSG && iPSC860 */
195 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(rpc_subsystem_t
);
196 register unsigned int howmany
;
197 register 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
);
234 #define PAGER_MUX_HASH_COUNT 127
237 struct pager_mux_entry
{
240 rpc_subsystem_t pager_object
;
242 typedef struct pager_mux_entry
*pager_mux_entry_t
;
244 queue_head_t pager_mux_hashtable
[PAGER_MUX_HASH_COUNT
];
245 zone_t pager_mux_hash_zone
;
247 decl_mutex_data(,pager_mux_hash_lock
)
249 #define pager_mux_hash(name_port) \
250 (((natural_t)(name_port) & 0xffffff) % PAGER_MUX_HASH_COUNT)
254 pager_mux_hash_lookup(
255 ipc_port_t name_port
)
257 register queue_t bucket
;
258 register pager_mux_entry_t entry
;
259 register rpc_subsystem_t pager_object
;
261 bucket
= &pager_mux_hashtable
[pager_mux_hash(name_port
)];
263 mutex_lock(&pager_mux_hash_lock
);
264 for (entry
= (pager_mux_entry_t
)queue_first(bucket
);
265 !queue_end(bucket
, &entry
->links
);
266 entry
= (pager_mux_entry_t
)queue_next(&entry
->links
)) {
267 if (entry
->name
== name_port
) {
268 pager_object
= entry
->pager_object
;
269 /* don't need to reference the object, it can't disappear */
270 /* pager_mux_reference(pager_object); */
271 mutex_unlock(&pager_mux_hash_lock
);
272 return (pager_object
);
275 mutex_unlock(&pager_mux_hash_lock
);
276 return (rpc_subsystem_t
)0;
281 pager_mux_hash_init(void)
284 register vm_size_t size
;
286 size
= sizeof(struct pager_mux_entry
);
287 pager_mux_hash_zone
= zinit(
291 "pager mux port hash");
292 for (i
= 0; i
< PAGER_MUX_HASH_COUNT
; i
++)
293 queue_init(&pager_mux_hashtable
[i
]);
294 mutex_init(&pager_mux_hash_lock
, ETAP_IO_DEV_PAGEH
);
299 pager_mux_hash_insert(
300 ipc_port_t name_port
,
301 rpc_subsystem_t pager_object
)
303 register pager_mux_entry_t new_entry
;
305 new_entry
= (pager_mux_entry_t
) zalloc(pager_mux_hash_zone
);
306 new_entry
->links
.prev
= (queue_entry_t
) 0;
307 new_entry
->links
.next
= (queue_entry_t
) 0;
308 new_entry
->name
= name_port
;
309 new_entry
->pager_object
= pager_object
;
311 mutex_lock(&pager_mux_hash_lock
);
312 queue_enter((&pager_mux_hashtable
[pager_mux_hash(name_port
)]),
313 new_entry
, pager_mux_entry_t
, links
);
314 mutex_unlock(&pager_mux_hash_lock
);
318 pager_mux_hash_delete(
319 ipc_port_t name_port
)
321 register queue_t bucket
;
322 register pager_mux_entry_t entry
;
324 bucket
= &pager_mux_hashtable
[pager_mux_hash(name_port
)];
326 mutex_lock(&pager_mux_hash_lock
);
327 for (entry
= (pager_mux_entry_t
)queue_first(bucket
);
328 !queue_end(bucket
, &entry
->links
);
329 entry
= (pager_mux_entry_t
)queue_next(&entry
->links
)) {
330 if (entry
->name
== name_port
) {
331 queue_remove(bucket
, entry
, pager_mux_entry_t
, links
);
332 zfree(pager_mux_hash_zone
, (vm_offset_t
)entry
);
336 mutex_unlock(&pager_mux_hash_lock
);
341 * Routine: ipc_kobject_server
343 * Handle a message sent to the kernel.
344 * Generates a reply message.
345 * Version for Untyped IPC.
354 mach_msg_size_t reply_size
;
357 mig_routine_t routine
;
359 mach_msg_format_0_trailer_t
*trailer
;
360 register mig_hash_t
*ptr
;
363 /* Only fetch current thread if ETAP is configured */
364 ETAP_DATA_LOAD(th
, current_thread());
365 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
368 &request
->ikm_header
.msgh_id
,
371 * Find out corresponding mig_hash entry if any
374 register int key
= request
->ikm_header
.msgh_id
;
375 register int i
= MIG_HASH(key
);
376 register int max_iter
= mig_table_max_displ
;
379 ptr
= &mig_buckets
[i
++ % MAX_MIG_ENTRIES
];
380 while (key
!= ptr
->num
&& ptr
->num
&& --max_iter
);
382 if (!ptr
->routine
|| key
!= ptr
->num
) {
383 ptr
= (mig_hash_t
*)0;
384 reply_size
= mig_reply_size
;
386 reply_size
= ptr
->size
;
393 /* round up for trailer size */
394 reply_size
+= MAX_TRAILER_SIZE
;
395 reply
= ipc_kmsg_alloc(reply_size
);
397 if (reply
== IKM_NULL
) {
398 printf("ipc_kobject_server: dropping request\n");
399 ipc_kmsg_destroy(request
);
404 * Initialize reply message.
407 #define InP ((mach_msg_header_t *) &request->ikm_header)
408 #define OutP ((mig_reply_error_t *) &reply->ikm_header)
410 OutP
->NDR
= NDR_record
;
411 OutP
->Head
.msgh_size
= sizeof(mig_reply_error_t
);
413 OutP
->Head
.msgh_bits
=
414 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP
->msgh_bits
), 0);
415 OutP
->Head
.msgh_remote_port
= InP
->msgh_local_port
;
416 OutP
->Head
.msgh_local_port
= MACH_PORT_NULL
;
417 OutP
->Head
.msgh_id
= InP
->msgh_id
+ 100;
424 * Find the routine to call, and call it
425 * to perform the kernel function
429 (*ptr
->routine
)(&request
->ikm_header
, &reply
->ikm_header
);
430 kernel_task
->messages_received
++;
433 if (!ipc_kobject_notify(&request
->ikm_header
, &reply
->ikm_header
)){
435 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
436 request
->ikm_header
.msgh_id
);
437 #endif /* MACH_IPC_TEST */
438 _MIG_MSGID_INVALID(request
->ikm_header
.msgh_id
);
440 ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
444 kernel_task
->messages_received
++;
446 kernel_task
->messages_sent
++;
450 * Destroy destination. The following code differs from
451 * ipc_object_destroy in that we release the send-once
452 * right instead of generating a send-once notification
453 * (which would bring us here again, creating a loop).
454 * It also differs in that we only expect send or
455 * send-once rights, never receive rights.
457 * We set msgh_remote_port to IP_NULL so that the kmsg
458 * destroy routines don't try to destroy the port twice.
460 destp
= (ipc_port_t
*) &request
->ikm_header
.msgh_remote_port
;
461 switch (MACH_MSGH_BITS_REMOTE(request
->ikm_header
.msgh_bits
)) {
462 case MACH_MSG_TYPE_PORT_SEND
:
463 ipc_port_release_send(*destp
);
466 case MACH_MSG_TYPE_PORT_SEND_ONCE
:
467 ipc_port_release_sonce(*destp
);
471 panic("ipc_object_destroy: strange destination rights");
475 if (!(reply
->ikm_header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
476 ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
!= KERN_SUCCESS
)
477 kr
= ((mig_reply_error_t
*) &reply
->ikm_header
)->RetCode
;
481 if ((kr
== KERN_SUCCESS
) || (kr
== MIG_NO_REPLY
)) {
483 * The server function is responsible for the contents
484 * of the message. The reply port right is moved
485 * to the reply message, and we have deallocated
486 * the destination port right, so we just need
489 ipc_kmsg_free(request
);
493 * The message contents of the request are intact.
494 * Destroy everthing except the reply port right,
495 * which is needed in the reply message.
497 request
->ikm_header
.msgh_local_port
= MACH_PORT_NULL
;
498 ipc_kmsg_destroy(request
);
501 if (kr
== MIG_NO_REPLY
) {
503 * The server function will send a reply message
504 * using the reply port right, which it has saved.
507 ipc_kmsg_free(reply
);
509 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
512 &request
->ikm_header
.msgh_id
,
516 } else if (!IP_VALID((ipc_port_t
)reply
->ikm_header
.msgh_remote_port
)) {
518 * Can't queue the reply message if the destination
519 * (the reply port) isn't valid.
522 ipc_kmsg_destroy(reply
);
524 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
527 &request
->ikm_header
.msgh_id
,
533 trailer
= (mach_msg_format_0_trailer_t
*)
534 ((vm_offset_t
)&reply
->ikm_header
+ (int)reply
->ikm_header
.msgh_size
);
535 trailer
->msgh_sender
= KERNEL_SECURITY_TOKEN
;
536 trailer
->msgh_trailer_type
= MACH_MSG_TRAILER_FORMAT_0
;
537 trailer
->msgh_trailer_size
= MACH_MSG_TRAILER_MINIMUM_SIZE
;
539 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH
,
542 &request
->ikm_header
.msgh_id
,
549 * Routine: ipc_kobject_set
551 * Make a port represent a kernel object of the given type.
552 * The caller is responsible for handling refs for the
553 * kernel object, if necessary.
555 * Nothing locked. The port must be active if setting
556 * a kobject linkage. Clearing a linkage is OK on an
562 ipc_kobject_t kobject
,
563 ipc_kobject_type_t type
)
566 ipc_kobject_set_atomically(port
, kobject
, type
);
571 ipc_kobject_set_atomically(
573 ipc_kobject_t kobject
,
574 ipc_kobject_type_t type
)
576 assert(type
== IKOT_NONE
|| ip_active(port
));
578 port
->ip_spares
[2] = (port
->ip_bits
& IO_BITS_KOTYPE
);
579 #endif /* MACH_ASSERT */
580 port
->ip_bits
= (port
->ip_bits
&~ IO_BITS_KOTYPE
) | type
;
581 port
->ip_kobject
= kobject
;
585 * Routine: ipc_kobject_destroy
587 * Release any kernel object resources associated
588 * with the port, which is being destroyed.
590 * This should only be needed when resources are
591 * associated with a user's port. In the normal case,
592 * when the kernel is the receiver, the code calling
593 * ipc_port_dealloc_kernel should clean up the resources.
595 * The port is not locked, but it is dead.
602 switch (ip_kotype(port
)) {
605 mk_timer_port_destroy(port
);
608 case IKOT_NAMED_ENTRY
:
609 mach_destroy_memory_entry(port
);
613 mach_destroy_upl(port
);
616 default: /* XXX (bogon) */
617 vm_object_destroy(port
);
623 extern int vnode_pager_workaround
;
627 mach_msg_header_t
*request_header
,
628 mach_msg_header_t
*reply_header
)
630 ipc_port_t port
= (ipc_port_t
) request_header
->msgh_remote_port
;
631 rpc_subsystem_t paging_subsystem_object
;
632 mach_port_seqno_t seqno
;
634 ((mig_reply_error_t
*) reply_header
)->RetCode
= MIG_NO_REPLY
;
635 switch (request_header
->msgh_id
) {
636 case MACH_NOTIFY_NO_SENDERS
:
637 if(ip_kotype(port
) == IKOT_NAMED_ENTRY
) {
641 * Bring the sequence number and mscount in
642 * line with ipc_port_destroy assertion.
644 port
->ip_mscount
= 0;
645 port
->ip_messages
.imq_seqno
= 0;
646 ipc_port_destroy(port
); /* releases lock */
649 paging_subsystem_object
= pager_mux_hash_lookup(
650 (ipc_port_t
)request_header
->msgh_remote_port
);
651 if(paging_subsystem_object
== (rpc_subsystem_t
)
652 &dp_memory_object_subsystem
) {
653 default_pager_no_senders(
654 (ipc_port_t
)request_header
->msgh_remote_port
,
656 (mach_port_mscount_t
)
657 ((mach_no_senders_notification_t
*)
658 request_header
)->not_count
);
659 (ipc_port_t
)reply_header
->msgh_remote_port
663 if(paging_subsystem_object
== (rpc_subsystem_t
)
664 &vnode_pager_workaround
) {
665 vnode_pager_no_senders(
666 (ipc_port_t
)request_header
->msgh_remote_port
,
667 (mach_port_mscount_t
)
668 ((mach_no_senders_notification_t
*)
669 request_header
)->not_count
);
670 (ipc_port_t
)reply_header
->msgh_remote_port
675 case MACH_NOTIFY_PORT_DELETED
:
676 case MACH_NOTIFY_PORT_DESTROYED
:
677 case MACH_NOTIFY_SEND_ONCE
:
678 case MACH_NOTIFY_DEAD_NAME
:
684 switch (ip_kotype(port
)) {
687 case IKOT_IOKIT_OBJECT
:
688 case IKOT_IOKIT_CONNECT
:
689 case IKOT_IOKIT_SPARE
:
691 extern boolean_t
iokit_notify( mach_msg_header_t
*msg
);
693 return iokit_notify(request_header
);
703 #include <mach_kdb.h>
704 #if MACH_COUNTERS && MACH_KDB
706 #include <ddb/db_output.h>
707 #include <ddb/db_sym.h>
709 #define printf kdbprintf
711 extern void kobjserver_stats(void);
712 extern void bucket_stats_print(mig_hash_t
*bucket
);
714 extern void kobjserver_stats_clear(void);
718 kobjserver_stats_clear(void)
721 for (i
= 0; i
< MAX_MIG_ENTRIES
; i
++) {
722 mig_buckets
[i
].callcount
= 0;
727 kobjserver_stats(void)
729 register unsigned int i
, n
= sizeof(mig_e
)/sizeof(rpc_subsystem_t
);
730 register unsigned int howmany
;
731 register mach_msg_id_t j
, pos
, nentry
, range
;
733 db_printf("Kobject server call counts:\n");
734 for (i
= 0; i
< n
; i
++) {
736 db_printsym((vm_offset_t
)mig_e
[i
], DB_STGY_ANY
);
738 range
= mig_e
[i
]->end
- mig_e
[i
]->start
;
739 if (!mig_e
[i
]->start
|| range
< 0) continue;
741 for (j
= 0; j
< range
; j
++) {
742 nentry
= j
+ mig_e
[i
]->start
;
743 for (pos
= MIG_HASH(nentry
) % MAX_MIG_ENTRIES
, howmany
= 1;
744 mig_buckets
[pos
].num
;
745 pos
= ++pos
% MAX_MIG_ENTRIES
, howmany
++) {
746 if (mig_buckets
[pos
].num
== nentry
)
747 bucket_stats_print(&mig_buckets
[pos
]);
754 bucket_stats_print(mig_hash_t
*bucket
)
756 if (bucket
->callcount
) {
758 db_printsym((vm_offset_t
)bucket
->routine
, DB_STGY_ANY
);
759 db_printf(" (%d):\t%d\n", bucket
->num
, bucket
->callcount
);
764 #endif /* MACH_COUNTERS && MACH_KDB */