| 1 | /* |
| 2 | * Copyright (c) 2000-2012 Apple 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 | * 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 | */ |
| 63 | /* |
| 64 | */ |
| 65 | /* |
| 66 | * File: ipc/ipc_init.c |
| 67 | * Author: Rich Draves |
| 68 | * Date: 1989 |
| 69 | * |
| 70 | * Functions to initialize the IPC system. |
| 71 | */ |
| 72 | |
| 73 | #include <mach_debug.h> |
| 74 | |
| 75 | #include <mach/port.h> |
| 76 | #include <mach/message.h> |
| 77 | #include <mach/kern_return.h> |
| 78 | |
| 79 | #include <kern/kern_types.h> |
| 80 | #include <kern/kalloc.h> |
| 81 | #include <kern/simple_lock.h> |
| 82 | #include <kern/mach_param.h> |
| 83 | #include <kern/ipc_host.h> |
| 84 | #include <kern/ipc_mig.h> |
| 85 | #include <kern/host_notify.h> |
| 86 | #include <kern/mk_timer.h> |
| 87 | #include <kern/misc_protos.h> |
| 88 | #include <kern/sync_lock.h> |
| 89 | #include <kern/sync_sema.h> |
| 90 | #include <kern/ux_handler.h> |
| 91 | #include <vm/vm_map.h> |
| 92 | #include <vm/vm_kern.h> |
| 93 | |
| 94 | #include <ipc/ipc_entry.h> |
| 95 | #include <ipc/ipc_space.h> |
| 96 | #include <ipc/ipc_object.h> |
| 97 | #include <ipc/ipc_port.h> |
| 98 | #include <ipc/ipc_pset.h> |
| 99 | #include <ipc/ipc_notify.h> |
| 100 | #include <ipc/ipc_kmsg.h> |
| 101 | #include <ipc/ipc_hash.h> |
| 102 | #include <ipc/ipc_init.h> |
| 103 | #include <ipc/ipc_table.h> |
| 104 | #include <ipc/ipc_voucher.h> |
| 105 | #include <ipc/ipc_importance.h> |
| 106 | |
| 107 | #include <mach/machine/ndr_def.h> /* NDR_record */ |
| 108 | |
| 109 | vm_map_t ipc_kernel_map; |
| 110 | vm_size_t ipc_kernel_map_size = 1024 * 1024; |
| 111 | |
| 112 | /* values to limit physical copy out-of-line memory descriptors */ |
| 113 | vm_map_t ipc_kernel_copy_map; |
| 114 | #define IPC_KERNEL_COPY_MAP_SIZE (8 * 1024 * 1024) |
| 115 | vm_size_t ipc_kernel_copy_map_size = IPC_KERNEL_COPY_MAP_SIZE; |
| 116 | vm_size_t ipc_kmsg_max_vm_space = ((IPC_KERNEL_COPY_MAP_SIZE * 7) / 8); |
| 117 | |
| 118 | /* |
| 119 | * values to limit inline message body handling |
| 120 | * avoid copyin/out limits - even after accounting for maximum descriptor expansion. |
| 121 | */ |
| 122 | #define IPC_KMSG_MAX_SPACE (64 * 1024 * 1024) /* keep in sync with COPYSIZELIMIT_PANIC */ |
| 123 | vm_size_t ipc_kmsg_max_body_space = ((IPC_KMSG_MAX_SPACE * 3)/4 - MAX_TRAILER_SIZE); |
| 124 | |
| 125 | int ipc_space_max; |
| 126 | int ipc_port_max; |
| 127 | int ipc_pset_max; |
| 128 | |
| 129 | |
| 130 | lck_grp_t ipc_lck_grp; |
| 131 | lck_attr_t ipc_lck_attr; |
| 132 | |
| 133 | static lck_grp_attr_t ipc_lck_grp_attr; |
| 134 | |
| 135 | /* |
| 136 | * Routine: ipc_bootstrap |
| 137 | * Purpose: |
| 138 | * Initialization needed before the kernel task |
| 139 | * can be created. |
| 140 | */ |
| 141 | |
| 142 | void |
| 143 | ipc_bootstrap(void) |
| 144 | { |
| 145 | kern_return_t kr; |
| 146 | |
| 147 | lck_grp_attr_setdefault(&ipc_lck_grp_attr); |
| 148 | lck_grp_init(&ipc_lck_grp, "ipc" , &ipc_lck_grp_attr); |
| 149 | lck_attr_setdefault(&ipc_lck_attr); |
| 150 | |
| 151 | ipc_port_multiple_lock_init(); |
| 152 | |
| 153 | ipc_port_timestamp_data = 0; |
| 154 | |
| 155 | /* all IPC zones should be exhaustible */ |
| 156 | |
| 157 | ipc_space_zone = zinit(sizeof(struct ipc_space), |
| 158 | ipc_space_max * sizeof(struct ipc_space), |
| 159 | sizeof(struct ipc_space), |
| 160 | "ipc spaces" ); |
| 161 | zone_change(ipc_space_zone, Z_NOENCRYPT, TRUE); |
| 162 | |
| 163 | /* |
| 164 | * populate all port(set) zones |
| 165 | */ |
| 166 | ipc_object_zones[IOT_PORT] = |
| 167 | zinit(sizeof(struct ipc_port), |
| 168 | ipc_port_max * sizeof(struct ipc_port), |
| 169 | sizeof(struct ipc_port), |
| 170 | "ipc ports" ); |
| 171 | /* cant charge callers for port allocations (references passed) */ |
| 172 | zone_change(ipc_object_zones[IOT_PORT], Z_CALLERACCT, FALSE); |
| 173 | zone_change(ipc_object_zones[IOT_PORT], Z_NOENCRYPT, TRUE); |
| 174 | |
| 175 | ipc_object_zones[IOT_PORT_SET] = |
| 176 | zinit(sizeof(struct ipc_pset), |
| 177 | ipc_pset_max * sizeof(struct ipc_pset), |
| 178 | sizeof(struct ipc_pset), |
| 179 | "ipc port sets" ); |
| 180 | zone_change(ipc_object_zones[IOT_PORT_SET], Z_NOENCRYPT, TRUE); |
| 181 | |
| 182 | /* |
| 183 | * Create the basic ipc_kmsg_t zone (the one we also cache) |
| 184 | * elements at the processor-level to avoid the locking. |
| 185 | */ |
| 186 | ipc_kmsg_zone = zinit(IKM_SAVED_KMSG_SIZE, |
| 187 | ipc_port_max * MACH_PORT_QLIMIT_DEFAULT * |
| 188 | IKM_SAVED_KMSG_SIZE, |
| 189 | IKM_SAVED_KMSG_SIZE, |
| 190 | "ipc kmsgs" ); |
| 191 | zone_change(ipc_kmsg_zone, Z_CALLERACCT, FALSE); |
| 192 | zone_change(ipc_kmsg_zone, Z_CACHING_ENABLED, TRUE); |
| 193 | |
| 194 | /* create special spaces */ |
| 195 | |
| 196 | kr = ipc_space_create_special(&ipc_space_kernel); |
| 197 | assert(kr == KERN_SUCCESS); |
| 198 | |
| 199 | |
| 200 | kr = ipc_space_create_special(&ipc_space_reply); |
| 201 | assert(kr == KERN_SUCCESS); |
| 202 | |
| 203 | /* initialize modules with hidden data structures */ |
| 204 | |
| 205 | #if MACH_ASSERT |
| 206 | ipc_port_debug_init(); |
| 207 | #endif |
| 208 | mig_init(); |
| 209 | ipc_table_init(); |
| 210 | ipc_voucher_init(); |
| 211 | |
| 212 | #if IMPORTANCE_INHERITANCE |
| 213 | ipc_importance_init(); |
| 214 | #endif |
| 215 | |
| 216 | semaphore_init(); |
| 217 | mk_timer_init(); |
| 218 | host_notify_init(); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * XXX tunable, belongs in mach.message.h |
| 223 | */ |
| 224 | #define MSG_OOL_SIZE_SMALL_MAX (2*PAGE_SIZE) |
| 225 | vm_size_t msg_ool_size_small; |
| 226 | |
| 227 | /* |
| 228 | * Routine: ipc_init |
| 229 | * Purpose: |
| 230 | * Final initialization of the IPC system. |
| 231 | */ |
| 232 | |
| 233 | void |
| 234 | ipc_init(void) |
| 235 | { |
| 236 | kern_return_t retval; |
| 237 | vm_offset_t min; |
| 238 | |
| 239 | retval = kmem_suballoc(kernel_map, &min, ipc_kernel_map_size, |
| 240 | TRUE, |
| 241 | (VM_FLAGS_ANYWHERE), |
| 242 | VM_MAP_KERNEL_FLAGS_NONE, |
| 243 | VM_KERN_MEMORY_IPC, |
| 244 | &ipc_kernel_map); |
| 245 | |
| 246 | if (retval != KERN_SUCCESS) |
| 247 | panic("ipc_init: kmem_suballoc of ipc_kernel_map failed" ); |
| 248 | |
| 249 | retval = kmem_suballoc(kernel_map, &min, ipc_kernel_copy_map_size, |
| 250 | TRUE, |
| 251 | (VM_FLAGS_ANYWHERE), |
| 252 | VM_MAP_KERNEL_FLAGS_NONE, |
| 253 | VM_KERN_MEMORY_IPC, |
| 254 | &ipc_kernel_copy_map); |
| 255 | |
| 256 | if (retval != KERN_SUCCESS) |
| 257 | panic("ipc_init: kmem_suballoc of ipc_kernel_copy_map failed" ); |
| 258 | |
| 259 | ipc_kernel_copy_map->no_zero_fill = TRUE; |
| 260 | ipc_kernel_copy_map->wait_for_space = TRUE; |
| 261 | |
| 262 | /* |
| 263 | * As an optimization, 'small' out of line data regions using a |
| 264 | * physical copy strategy are copied into kalloc'ed buffers. |
| 265 | * The value of 'small' is determined here. Requests kalloc() |
| 266 | * with sizes greater or equal to kalloc_max_prerounded may fail. |
| 267 | */ |
| 268 | if (kalloc_max_prerounded <= MSG_OOL_SIZE_SMALL_MAX) { |
| 269 | msg_ool_size_small = kalloc_max_prerounded; |
| 270 | } |
| 271 | else { |
| 272 | msg_ool_size_small = MSG_OOL_SIZE_SMALL_MAX; |
| 273 | } |
| 274 | /* account for overhead to avoid spilling over a page */ |
| 275 | msg_ool_size_small -= cpy_kdata_hdr_sz; |
| 276 | |
| 277 | ipc_host_init(); |
| 278 | ux_handler_init(); |
| 279 | |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /* |
| 284 | * Routine: ipc_thread_call_init |
| 285 | * Purpose: |
| 286 | * Initialize IPC logic that needs thread call support |
| 287 | */ |
| 288 | |
| 289 | void |
| 290 | ipc_thread_call_init(void) |
| 291 | { |
| 292 | #if IMPORTANCE_INHERITANCE |
| 293 | ipc_importance_thread_call_init(); |
| 294 | #endif |
| 295 | } |
| 296 | |
| 297 | |