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