]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_kobject.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_kobject.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: kern/ipc_kobject.c
60 * Author: Rich Draves
61 * Date: 1989
62 *
63 * Functions for letting a port represent a kernel object.
64 */
65
66 #include <mach_debug.h>
67 #include <mach_ipc_test.h>
68 #include <mach_machine_routines.h>
69 #include <norma_task.h>
70 #include <mach_rt.h>
71 #include <platforms.h>
72
73 #include <mach/mig.h>
74 #include <mach/port.h>
75 #include <mach/kern_return.h>
76 #include <mach/message.h>
77 #include <mach/mig_errors.h>
78 #include <mach/notify.h>
79 #include <mach/ndr.h>
80
81 #include <mach/mach_vm_server.h>
82 #include <mach/mach_port_server.h>
83 #include <mach/mach_host_server.h>
84 #include <mach/host_priv_server.h>
85 #include <mach/host_security_server.h>
86 #include <mach/clock_server.h>
87 #include <mach/clock_priv_server.h>
88 #include <mach/ledger_server.h>
89 #include <mach/lock_set_server.h>
90 #include <default_pager/default_pager_object_server.h>
91 #include <mach/memory_object_server.h>
92 #include <mach/memory_object_control_server.h>
93 #include <mach/memory_object_default_server.h>
94 #include <mach/memory_object_name_server.h>
95 #include <mach/processor_server.h>
96 #include <mach/processor_set_server.h>
97 #include <mach/semaphore_server.h>
98 #include <mach/task_server.h>
99 #include <mach/vm_map_server.h>
100 #include <mach/thread_act_server.h>
101
102 #include <device/device_types.h>
103 #include <device/device_server.h>
104
105 #include <UserNotification/UNDReplyServer.h>
106
107 #if MACH_MACHINE_ROUTINES
108 #include <machine/machine_routines.h>
109 #endif /* MACH_MACHINE_ROUTINES */
110 #if XK_PROXY
111 #include <uk_xkern/xk_uproxy_server.h>
112 #endif /* XK_PROXY */
113
114 #include <kern/ipc_tt.h>
115 #include <kern/ipc_mig.h>
116 #include <kern/ipc_kobject.h>
117 #include <kern/host_notify.h>
118 #include <kern/mk_timer.h>
119 #include <kern/misc_protos.h>
120 #include <ipc/ipc_kmsg.h>
121 #include <ipc/ipc_port.h>
122 #include <kern/counters.h>
123
124 #include <vm/vm_shared_memory_server.h>
125 #include <vm/vm_protos.h>
126
127 /*
128 * Routine: ipc_kobject_notify
129 * Purpose:
130 * Deliver notifications to kobjects that care about them.
131 */
132 boolean_t
133 ipc_kobject_notify(
134 mach_msg_header_t *request_header,
135 mach_msg_header_t *reply_header);
136
137 typedef struct {
138 mach_msg_id_t num;
139 mig_routine_t routine;
140 int size;
141 #if MACH_COUNTERS
142 mach_counter_t callcount;
143 #endif
144 } mig_hash_t;
145
146 #define MAX_MIG_ENTRIES 1024
147 #define MIG_HASH(x) (x)
148
149 #ifndef max
150 #define max(a,b) (((a) > (b)) ? (a) : (b))
151 #endif /* max */
152
153 mig_hash_t mig_buckets[MAX_MIG_ENTRIES];
154 int mig_table_max_displ;
155 mach_msg_size_t mig_reply_size;
156
157
158
159
160 const struct mig_subsystem *mig_e[] = {
161 (const struct mig_subsystem *)&mach_vm_subsystem,
162 (const struct mig_subsystem *)&mach_port_subsystem,
163 (const struct mig_subsystem *)&mach_host_subsystem,
164 (const struct mig_subsystem *)&host_priv_subsystem,
165 (const struct mig_subsystem *)&host_security_subsystem,
166 (const struct mig_subsystem *)&clock_subsystem,
167 (const struct mig_subsystem *)&clock_priv_subsystem,
168 (const struct mig_subsystem *)&processor_subsystem,
169 (const struct mig_subsystem *)&processor_set_subsystem,
170 (const struct mig_subsystem *)&is_iokit_subsystem,
171 (const struct mig_subsystem *)&memory_object_name_subsystem,
172 (const struct mig_subsystem *)&lock_set_subsystem,
173 (const struct mig_subsystem *)&ledger_subsystem,
174 (const struct mig_subsystem *)&semaphore_subsystem,
175 (const struct mig_subsystem *)&task_subsystem,
176 (const struct mig_subsystem *)&thread_act_subsystem,
177 (const struct mig_subsystem *)&vm_map_subsystem,
178 (const struct mig_subsystem *)&UNDReply_subsystem,
179 (const struct mig_subsystem *)&default_pager_object_subsystem,
180
181 #if XK_PROXY
182 (const struct mig_subsystem *)&do_uproxy_xk_uproxy_subsystem,
183 #endif /* XK_PROXY */
184 #if MACH_MACHINE_ROUTINES
185 (const struct mig_subsystem *)&MACHINE_SUBSYSTEM,
186 #endif /* MACH_MACHINE_ROUTINES */
187 #if MCMSG && iPSC860
188 (const struct mig_subsystem *)&mcmsg_info_subsystem,
189 #endif /* MCMSG && iPSC860 */
190 };
191
192 void
193 mig_init(void)
194 {
195 unsigned int i, n = sizeof(mig_e)/sizeof(const struct mig_subsystem *);
196 int howmany;
197 mach_msg_id_t j, pos, nentry, range;
198
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);
204
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");
215 }
216 if (howmany == MAX_MIG_ENTRIES)
217 panic("the mig dispatch table is too small");
218 }
219
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;
224 else
225 mig_buckets[pos].size = mig_e[i]->maxsize;
226
227 mig_table_max_displ = max(howmany, mig_table_max_displ);
228 }
229 }
230 }
231 printf("mig_table_max_displ = %d\n", mig_table_max_displ);
232 }
233
234
235 /*
236 * Routine: ipc_kobject_server
237 * Purpose:
238 * Handle a message sent to the kernel.
239 * Generates a reply message.
240 * Version for Untyped IPC.
241 * Conditions:
242 * Nothing locked.
243 */
244
245 ipc_kmsg_t
246 ipc_kobject_server(
247 ipc_kmsg_t request)
248 {
249 mach_msg_size_t reply_size;
250 ipc_kmsg_t reply;
251 kern_return_t kr;
252 ipc_port_t *destp;
253 mach_msg_format_0_trailer_t *trailer;
254 register mig_hash_t *ptr;
255
256 /*
257 * Find out corresponding mig_hash entry if any
258 */
259 {
260 register int key = request->ikm_header->msgh_id;
261 register int i = MIG_HASH(key);
262 register int max_iter = mig_table_max_displ;
263
264 do
265 ptr = &mig_buckets[i++ % MAX_MIG_ENTRIES];
266 while (key != ptr->num && ptr->num && --max_iter);
267
268 if (!ptr->routine || key != ptr->num) {
269 ptr = (mig_hash_t *)0;
270 reply_size = mig_reply_size;
271 } else {
272 reply_size = ptr->size;
273 #if MACH_COUNTER
274 ptr->callcount++;
275 #endif
276 }
277 }
278
279 /* round up for trailer size */
280 reply_size += MAX_TRAILER_SIZE;
281 reply = ipc_kmsg_alloc(reply_size);
282
283 if (reply == IKM_NULL) {
284 printf("ipc_kobject_server: dropping request\n");
285 ipc_kmsg_destroy(request);
286 return IKM_NULL;
287 }
288
289 /*
290 * Initialize reply message.
291 */
292 {
293 #define InP ((mach_msg_header_t *) request->ikm_header)
294 #define OutP ((mig_reply_error_t *) reply->ikm_header)
295
296 OutP->NDR = NDR_record;
297 OutP->Head.msgh_size = sizeof(mig_reply_error_t);
298
299 OutP->Head.msgh_bits =
300 MACH_MSGH_BITS(MACH_MSGH_BITS_LOCAL(InP->msgh_bits), 0);
301 OutP->Head.msgh_remote_port = InP->msgh_local_port;
302 OutP->Head.msgh_local_port = MACH_PORT_NULL;
303 OutP->Head.msgh_id = InP->msgh_id + 100;
304
305 #undef InP
306 #undef OutP
307 }
308
309 /*
310 * Find the routine to call, and call it
311 * to perform the kernel function
312 */
313 {
314 if (ptr) {
315 (*ptr->routine)(request->ikm_header, reply->ikm_header);
316 kernel_task->messages_received++;
317 }
318 else {
319 if (!ipc_kobject_notify(request->ikm_header, reply->ikm_header)){
320 #if MACH_IPC_TEST
321 printf("ipc_kobject_server: bogus kernel message, id=%d\n",
322 request->ikm_header->msgh_id);
323 #endif /* MACH_IPC_TEST */
324 _MIG_MSGID_INVALID(request->ikm_header->msgh_id);
325
326 ((mig_reply_error_t *) reply->ikm_header)->RetCode
327 = MIG_BAD_ID;
328 }
329 else
330 kernel_task->messages_received++;
331 }
332 kernel_task->messages_sent++;
333 }
334
335 /*
336 * Destroy destination. The following code differs from
337 * ipc_object_destroy in that we release the send-once
338 * right instead of generating a send-once notification
339 * (which would bring us here again, creating a loop).
340 * It also differs in that we only expect send or
341 * send-once rights, never receive rights.
342 *
343 * We set msgh_remote_port to IP_NULL so that the kmsg
344 * destroy routines don't try to destroy the port twice.
345 */
346 destp = (ipc_port_t *) &request->ikm_header->msgh_remote_port;
347 switch (MACH_MSGH_BITS_REMOTE(request->ikm_header->msgh_bits)) {
348 case MACH_MSG_TYPE_PORT_SEND:
349 ipc_port_release_send(*destp);
350 break;
351
352 case MACH_MSG_TYPE_PORT_SEND_ONCE:
353 ipc_port_release_sonce(*destp);
354 break;
355
356 default:
357 panic("ipc_kobject_server: strange destination rights");
358 }
359 *destp = IP_NULL;
360
361 if (!(reply->ikm_header->msgh_bits & MACH_MSGH_BITS_COMPLEX) &&
362 ((mig_reply_error_t *) reply->ikm_header)->RetCode != KERN_SUCCESS)
363 kr = ((mig_reply_error_t *) reply->ikm_header)->RetCode;
364 else
365 kr = KERN_SUCCESS;
366
367 if ((kr == KERN_SUCCESS) || (kr == MIG_NO_REPLY)) {
368 /*
369 * The server function is responsible for the contents
370 * of the message. The reply port right is moved
371 * to the reply message, and we have deallocated
372 * the destination port right, so we just need
373 * to free the kmsg.
374 */
375 ipc_kmsg_free(request);
376
377 } else {
378 /*
379 * The message contents of the request are intact.
380 * Destroy everthing except the reply port right,
381 * which is needed in the reply message.
382 */
383 request->ikm_header->msgh_local_port = MACH_PORT_NULL;
384 ipc_kmsg_destroy(request);
385 }
386
387 if (kr == MIG_NO_REPLY) {
388 /*
389 * The server function will send a reply message
390 * using the reply port right, which it has saved.
391 */
392
393 ipc_kmsg_free(reply);
394
395 return IKM_NULL;
396 } else if (!IP_VALID((ipc_port_t)reply->ikm_header->msgh_remote_port)) {
397 /*
398 * Can't queue the reply message if the destination
399 * (the reply port) isn't valid.
400 */
401
402 ipc_kmsg_destroy(reply);
403
404 return IKM_NULL;
405 }
406
407 trailer = (mach_msg_format_0_trailer_t *)
408 ((vm_offset_t)reply->ikm_header + (int)reply->ikm_header->msgh_size);
409
410 trailer->msgh_sender = KERNEL_SECURITY_TOKEN;
411 trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
412 trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
413
414 return reply;
415 }
416
417 /*
418 * Routine: ipc_kobject_set
419 * Purpose:
420 * Make a port represent a kernel object of the given type.
421 * The caller is responsible for handling refs for the
422 * kernel object, if necessary.
423 * Conditions:
424 * Nothing locked. The port must be active if setting
425 * a kobject linkage. Clearing a linkage is OK on an
426 * inactive port.
427 */
428 void
429 ipc_kobject_set(
430 ipc_port_t port,
431 ipc_kobject_t kobject,
432 ipc_kobject_type_t type)
433 {
434 ip_lock(port);
435 ipc_kobject_set_atomically(port, kobject, type);
436 ip_unlock(port);
437 }
438
439 void
440 ipc_kobject_set_atomically(
441 ipc_port_t port,
442 ipc_kobject_t kobject,
443 ipc_kobject_type_t type)
444 {
445 assert(type == IKOT_NONE || ip_active(port));
446 #if MACH_ASSERT
447 port->ip_spares[2] = (port->ip_bits & IO_BITS_KOTYPE);
448 #endif /* MACH_ASSERT */
449 port->ip_bits = (port->ip_bits &~ IO_BITS_KOTYPE) | type;
450 port->ip_kobject = kobject;
451 }
452
453 /*
454 * Routine: ipc_kobject_destroy
455 * Purpose:
456 * Release any kernel object resources associated
457 * with the port, which is being destroyed.
458 *
459 * This should only be needed when resources are
460 * associated with a user's port. In the normal case,
461 * when the kernel is the receiver, the code calling
462 * ipc_port_dealloc_kernel should clean up the resources.
463 * Conditions:
464 * The port is not locked, but it is dead.
465 */
466
467 void
468 ipc_kobject_destroy(
469 ipc_port_t port)
470 {
471 switch (ip_kotype(port)) {
472
473 case IKOT_TIMER:
474 mk_timer_port_destroy(port);
475 break;
476
477 case IKOT_NAMED_ENTRY:
478 mach_destroy_memory_entry(port);
479 break;
480
481 case IKOT_HOST_NOTIFY:
482 host_notify_port_destroy(port);
483 break;
484
485 default:
486 break;
487 }
488 }
489
490
491 boolean_t
492 ipc_kobject_notify(
493 mach_msg_header_t *request_header,
494 mach_msg_header_t *reply_header)
495 {
496 ipc_port_t port = (ipc_port_t) request_header->msgh_remote_port;
497
498 ((mig_reply_error_t *) reply_header)->RetCode = MIG_NO_REPLY;
499 switch (request_header->msgh_id) {
500 case MACH_NOTIFY_NO_SENDERS:
501 if(ip_kotype(port) == IKOT_NAMED_ENTRY) {
502 ip_lock(port);
503
504 /*
505 * Bring the sequence number and mscount in
506 * line with ipc_port_destroy assertion.
507 */
508 port->ip_mscount = 0;
509 port->ip_messages.imq_seqno = 0;
510 ipc_port_destroy(port); /* releases lock */
511 return TRUE;
512 }
513 if (ip_kotype(port) == IKOT_UPL) {
514 upl_no_senders(
515 request_header->msgh_remote_port,
516 (mach_port_mscount_t)
517 ((mach_no_senders_notification_t *)
518 request_header)->not_count);
519 (ipc_port_t)reply_header->msgh_remote_port
520 = MACH_PORT_NULL;
521 return TRUE;
522 }
523
524 break;
525
526 case MACH_NOTIFY_PORT_DELETED:
527 case MACH_NOTIFY_PORT_DESTROYED:
528 case MACH_NOTIFY_SEND_ONCE:
529 case MACH_NOTIFY_DEAD_NAME:
530 break;
531
532 default:
533 return FALSE;
534 }
535 switch (ip_kotype(port)) {
536
537 #ifdef IOKIT
538 case IKOT_IOKIT_OBJECT:
539 case IKOT_IOKIT_CONNECT:
540 case IKOT_IOKIT_SPARE:
541 {
542 return iokit_notify(request_header);
543 }
544 #endif
545 default:
546 return FALSE;
547 }
548 }
549
550
551
552 #include <mach_kdb.h>
553 #if MACH_COUNTERS && MACH_KDB
554
555 #include <ddb/db_output.h>
556 #include <ddb/db_sym.h>
557
558 #define printf kdbprintf
559
560 extern void kobjserver_stats(void);
561 extern void bucket_stats_print(mig_hash_t *bucket);
562
563 extern void kobjserver_stats_clear(void);
564
565
566 void
567 kobjserver_stats_clear(void)
568 {
569 int i;
570 for (i = 0; i < MAX_MIG_ENTRIES; i++) {
571 mig_buckets[i].callcount = 0;
572 }
573 }
574
575 void
576 kobjserver_stats(void)
577 {
578 register unsigned int i, n = sizeof(mig_e)/sizeof(struct mig_subsystem);
579 register unsigned int howmany;
580 register mach_msg_id_t j, pos, nentry, range;
581
582 db_printf("Kobject server call counts:\n");
583 for (i = 0; i < n; i++) {
584 db_printf(" ");
585 db_printsym((vm_offset_t)mig_e[i], DB_STGY_ANY);
586 db_printf(":\n");
587 range = mig_e[i]->end - mig_e[i]->start;
588 if (!mig_e[i]->start || range < 0) continue;
589
590 for (j = 0; j < range; j++) {
591 nentry = j + mig_e[i]->start;
592 for (pos = MIG_HASH(nentry) % MAX_MIG_ENTRIES, howmany = 1;
593 mig_buckets[pos].num;
594 pos = ++pos % MAX_MIG_ENTRIES, howmany++) {
595 if (mig_buckets[pos].num == nentry)
596 bucket_stats_print(&mig_buckets[pos]);
597 }
598 }
599 }
600 }
601
602 void
603 bucket_stats_print(mig_hash_t *bucket)
604 {
605 if (bucket->callcount) {
606 db_printf(" ");
607 db_printsym((vm_offset_t)bucket->routine, DB_STGY_ANY);
608 db_printf(" (%d):\t%d\n", bucket->num, bucket->callcount);
609 }
610 }
611
612
613 #endif /* MACH_COUNTERS && MACH_KDB */