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