]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_kobject.c
xnu-344.21.74.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_kobject.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
32 *
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.
38 *
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.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 /*
56 * File: kern/ipc_kobject.c
57 * Author: Rich Draves
58 * Date: 1989
59 *
60 * Functions for letting a port represent a kernel object.
61 */
62
63 #include <mach_debug.h>
64 #include <mach_ipc_test.h>
65 #include <mach_machine_routines.h>
66 #include <norma_task.h>
67 #include <mach_rt.h>
68 #include <platforms.h>
69
70 #include <mach/mig.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>
76
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>
85
86
87 /*
88 * Routine: ipc_kobject_notify
89 * Purpose:
90 * Deliver notifications to kobjects that care about them.
91 */
92 boolean_t
93 ipc_kobject_notify(
94 mach_msg_header_t *request_header,
95 mach_msg_header_t *reply_header);
96
97 #include <mach/ndr.h>
98
99 typedef struct {
100 mach_msg_id_t num;
101 mig_routine_t routine;
102 int size;
103 #if MACH_COUNTERS
104 mach_counter_t callcount;
105 #endif
106 } mig_hash_t;
107
108 #define MAX_MIG_ENTRIES 1024
109 #define MIG_HASH(x) (x)
110
111 #ifndef max
112 #define max(a,b) (((a) > (b)) ? (a) : (b))
113 #endif /* max */
114
115 mig_hash_t mig_buckets[MAX_MIG_ENTRIES];
116 int mig_table_max_displ;
117 mach_msg_size_t mig_reply_size;
118
119
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>
141
142 #if MACH_MACHINE_ROUTINES
143 #include <machine/machine_routines.h>
144 #endif /* MACH_MACHINE_ROUTINES */
145 #if XK_PROXY
146 #include <uk_xkern/xk_uproxy_server.h>
147 #endif /* XK_PROXY */
148
149
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,
168
169 #if XK_PROXY
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 */
175 #if MCMSG && iPSC860
176 (mig_subsystem_t)&mcmsg_info_subsystem,
177 #endif /* MCMSG && iPSC860 */
178 };
179
180 void
181 mig_init(void)
182 {
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;
186
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);
192
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");
203 }
204 if (howmany == MAX_MIG_ENTRIES)
205 panic("the mig dispatch table is too small");
206 }
207
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;
212 else
213 mig_buckets[pos].size = mig_e[i]->maxsize;
214
215 mig_table_max_displ = max(howmany, mig_table_max_displ);
216 }
217 }
218 }
219 printf("mig_table_max_displ = %d\n", mig_table_max_displ);
220 }
221
222
223 /*
224 * Routine: ipc_kobject_server
225 * Purpose:
226 * Handle a message sent to the kernel.
227 * Generates a reply message.
228 * Version for Untyped IPC.
229 * Conditions:
230 * Nothing locked.
231 */
232
233 ipc_kmsg_t
234 ipc_kobject_server(
235 ipc_kmsg_t request)
236 {
237 mach_msg_size_t reply_size;
238 ipc_kmsg_t reply;
239 kern_return_t kr;
240 mig_routine_t routine;
241 ipc_port_t *destp;
242 mach_msg_format_0_trailer_t *trailer;
243 register mig_hash_t *ptr;
244 unsigned int th;
245
246 /* Only fetch current thread if ETAP is configured */
247 ETAP_DATA_LOAD(th, current_thread());
248 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH,
249 EVENT_BEGIN,
250 ((thread_t) th),
251 &request->ikm_header.msgh_id,
252 sizeof(int));
253 /*
254 * Find out corresponding mig_hash entry if any
255 */
256 {
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;
260
261 do
262 ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES];
263 while (key != ptr->num && ptr->num && --max_iter);
264
265 if (!ptr->routine || key != ptr->num) {
266 ptr = (mig_hash_t *)0;
267 reply_size = mig_reply_size;
268 } else {
269 reply_size = ptr->size;
270 #if MACH_COUNTER
271 ptr->callcount++;
272 #endif
273 }
274 }
275
276 /* round up for trailer size */
277 reply_size += MAX_TRAILER_SIZE;
278 reply = ipc_kmsg_alloc(reply_size);
279
280 if (reply == IKM_NULL) {
281 printf("ipc_kobject_server: dropping request\n");
282 ipc_kmsg_destroy(request);
283 return IKM_NULL;
284 }
285
286 /*
287 * Initialize reply message.
288 */
289 {
290 #define InP ((mach_msg_header_t *) &request->ikm_header)
291 #define OutP ((mig_reply_error_t *) &reply->ikm_header)
292
293 OutP->NDR = NDR_record;
294 OutP->Head.msgh_size = sizeof(mig_reply_error_t);
295
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;
301
302 #undef InP
303 #undef OutP
304 }
305
306 /*
307 * Find the routine to call, and call it
308 * to perform the kernel function
309 */
310 {
311 if (ptr) {
312 (*ptr->routine)(&request->ikm_header, &reply->ikm_header);
313 kernel_task->messages_received++;
314 }
315 else {
316 if (!ipc_kobject_notify(&request->ikm_header, &reply->ikm_header)){
317 #if MACH_IPC_TEST
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);
322
323 ((mig_reply_error_t *) &reply->ikm_header)->RetCode
324 = MIG_BAD_ID;
325 }
326 else
327 kernel_task->messages_received++;
328 }
329 kernel_task->messages_sent++;
330 }
331
332 /*
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.
339 *
340 * We set msgh_remote_port to IP_NULL so that the kmsg
341 * destroy routines don't try to destroy the port twice.
342 */
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);
347 break;
348
349 case MACH_MSG_TYPE_PORT_SEND_ONCE:
350 ipc_port_release_sonce(*destp);
351 break;
352
353 default:
354 panic("ipc_kobject_server: strange destination rights");
355 }
356 *destp = IP_NULL;
357
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;
361 else
362 kr = KERN_SUCCESS;
363
364 if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) {
365 /*
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
370 * to free the kmsg.
371 */
372 ipc_kmsg_free(request);
373
374 } else {
375 /*
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.
379 */
380 request->ikm_header.msgh_local_port = MACH_PORT_NULL;
381 ipc_kmsg_destroy(request);
382 }
383
384 if (kr == MIG_NO_REPLY) {
385 /*
386 * The server function will send a reply message
387 * using the reply port right, which it has saved.
388 */
389
390 ipc_kmsg_free(reply);
391
392 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH,
393 EVENT_END,
394 ((thread_t) th),
395 &request->ikm_header.msgh_id,
396 sizeof(int));
397
398 return IKM_NULL;
399 } else if (!IP_VALID((ipc_port_t)reply->ikm_header.msgh_remote_port)) {
400 /*
401 * Can't queue the reply message if the destination
402 * (the reply port) isn't valid.
403 */
404
405 ipc_kmsg_destroy(reply);
406
407 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH,
408 EVENT_END,
409 ((thread_t) th),
410 &request->ikm_header.msgh_id,
411 sizeof(int));
412
413 return IKM_NULL;
414 }
415
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;
421
422 ETAP_PROBE_DATA(ETAP_P_SYSCALL_MACH,
423 EVENT_END,
424 ((thread_t) th),
425 &request->ikm_header.msgh_id,
426 sizeof(int));
427
428 return reply;
429 }
430
431 /*
432 * Routine: ipc_kobject_set
433 * Purpose:
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.
437 * Conditions:
438 * Nothing locked. The port must be active if setting
439 * a kobject linkage. Clearing a linkage is OK on an
440 * inactive port.
441 */
442 void
443 ipc_kobject_set(
444 ipc_port_t port,
445 ipc_kobject_t kobject,
446 ipc_kobject_type_t type)
447 {
448 ip_lock(port);
449 ipc_kobject_set_atomically(port, kobject, type);
450 ip_unlock(port);
451 }
452
453 void
454 ipc_kobject_set_atomically(
455 ipc_port_t port,
456 ipc_kobject_t kobject,
457 ipc_kobject_type_t type)
458 {
459 assert(type == IKOT_NONE || ip_active(port));
460 #if MACH_ASSERT
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;
465 }
466
467 /*
468 * Routine: ipc_kobject_destroy
469 * Purpose:
470 * Release any kernel object resources associated
471 * with the port, which is being destroyed.
472 *
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.
477 * Conditions:
478 * The port is not locked, but it is dead.
479 */
480
481 void
482 ipc_kobject_destroy(
483 ipc_port_t port)
484 {
485 switch (ip_kotype(port)) {
486
487 case IKOT_TIMER:
488 mk_timer_port_destroy(port);
489 break;
490
491 case IKOT_NAMED_ENTRY:
492 mach_destroy_memory_entry(port);
493 break;
494
495 default: /* XXX (bogon) */
496 break;
497 }
498 }
499
500
501 extern int vnode_pager_workaround;
502
503 boolean_t
504 ipc_kobject_notify(
505 mach_msg_header_t *request_header,
506 mach_msg_header_t *reply_header)
507 {
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;
511
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) {
516 ip_lock(port);
517
518 /*
519 * Bring the sequence number and mscount in
520 * line with ipc_port_destroy assertion.
521 */
522 port->ip_mscount = 0;
523 port->ip_messages.imq_seqno = 0;
524 ipc_port_destroy(port); /* releases lock */
525 return TRUE;
526 }
527 if (ip_kotype(port) == IKOT_UPL) {
528 upl_no_senders(
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
534 = MACH_PORT_NULL;
535 return TRUE;
536 }
537
538 break;
539
540 case MACH_NOTIFY_PORT_DELETED:
541 case MACH_NOTIFY_PORT_DESTROYED:
542 case MACH_NOTIFY_SEND_ONCE:
543 case MACH_NOTIFY_DEAD_NAME:
544 break;
545
546 default:
547 return FALSE;
548 }
549 switch (ip_kotype(port)) {
550
551 #ifdef IOKIT
552 case IKOT_IOKIT_OBJECT:
553 case IKOT_IOKIT_CONNECT:
554 case IKOT_IOKIT_SPARE:
555 {
556 extern boolean_t iokit_notify( mach_msg_header_t *msg);
557
558 return iokit_notify(request_header);
559 }
560 #endif
561 default:
562 return FALSE;
563 }
564 }
565
566
567
568 #include <mach_kdb.h>
569 #if MACH_COUNTERS && MACH_KDB
570
571 #include <ddb/db_output.h>
572 #include <ddb/db_sym.h>
573
574 #define printf kdbprintf
575
576 extern void kobjserver_stats(void);
577 extern void bucket_stats_print(mig_hash_t *bucket);
578
579 extern void kobjserver_stats_clear(void);
580
581
582 void
583 kobjserver_stats_clear(void)
584 {
585 int i;
586 for (i = 0; i < MAX_MIG_ENTRIES; i++) {
587 mig_buckets[i].callcount = 0;
588 }
589 }
590
591 void
592 kobjserver_stats(void)
593 {
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;
597
598 db_printf("Kobject server call counts:\n");
599 for (i = 0; i < n; i++) {
600 db_printf(" ");
601 db_printsym((vm_offset_t)mig_e[i], DB_STGY_ANY);
602 db_printf(":\n");
603 range = mig_e[i]->end - mig_e[i]->start;
604 if (!mig_e[i]->start || range < 0) continue;
605
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]);
613 }
614 }
615 }
616 }
617
618 void
619 bucket_stats_print(mig_hash_t *bucket)
620 {
621 if (bucket->callcount) {
622 db_printf(" ");
623 db_printsym((vm_offset_t)bucket->routine, DB_STGY_ANY);
624 db_printf(" (%d):\t%d\n", bucket->num, bucket->callcount);
625 }
626 }
627
628
629 #endif /* MACH_COUNTERS && MACH_KDB */