1/*
2 * Copyright (c) 2000-2020 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/port.h>
74#include <mach/message.h>
75#include <mach/kern_return.h>
76
77#include <kern/kern_types.h>
78#include <kern/arcade.h>
79#include <kern/kalloc.h>
80#include <kern/simple_lock.h>
81#include <kern/mach_param.h>
82#include <kern/ipc_host.h>
83#include <kern/ipc_kobject.h>
84#include <kern/ipc_mig.h>
85#include <kern/host_notify.h>
86#include <kern/misc_protos.h>
87#include <kern/sync_sema.h>
88#include <kern/ux_handler.h>
89#include <vm/vm_map.h>
90#include <vm/vm_kern.h>
91
92#include <ipc/ipc_entry.h>
93#include <ipc/ipc_space.h>
94#include <ipc/ipc_object.h>
95#include <ipc/ipc_port.h>
96#include <ipc/ipc_pset.h>
97#include <ipc/ipc_notify.h>
98#include <ipc/ipc_kmsg.h>
99#include <ipc/ipc_hash.h>
100#include <ipc/ipc_init.h>
101#include <ipc/ipc_voucher.h>
102#include <ipc/ipc_eventlink.h>
103
104#include <mach/machine/ndr_def.h> /* NDR_record */
105
106SECURITY_READ_ONLY_LATE(vm_map_t) ipc_kernel_map;
107
108/* values to limit physical copy out-of-line memory descriptors */
109SECURITY_READ_ONLY_LATE(vm_map_t) ipc_kernel_copy_map;
110#define IPC_KERNEL_COPY_MAP_SIZE (8 * 1024 * 1024)
111const vm_size_t ipc_kmsg_max_vm_space = ((IPC_KERNEL_COPY_MAP_SIZE * 7) / 8);
112
113#define IPC_KERNEL_MAP_SIZE (CONFIG_IPC_KERNEL_MAP_SIZE << 20)
114
115/* Note: Consider Developer Mode when changing the default. */
116#if XNU_TARGET_OS_OSX
117#define IPC_CONTROL_PORT_OPTIONS_DEFAULT (ICP_OPTIONS_IMMOVABLE_1P_HARD | ICP_OPTIONS_PINNED_1P_HARD)
118#else
119#define IPC_CONTROL_PORT_OPTIONS_DEFAULT (ICP_OPTIONS_IMMOVABLE_ALL_HARD | \
120 ICP_OPTIONS_PINNED_1P_HARD | \
121 ICP_OPTIONS_PINNED_3P_SOFT)
122#endif
123
124TUNABLE(ipc_control_port_options_t, ipc_control_port_options,
125 "ipc_control_port_options", IPC_CONTROL_PORT_OPTIONS_DEFAULT);
126
127LCK_GRP_DECLARE(ipc_lck_grp, "ipc");
128LCK_ATTR_DECLARE(ipc_lck_attr, 0, 0);
129
130/*
131 * As an optimization, 'small' out of line data regions using a
132 * physical copy strategy are copied into kalloc'ed buffers.
133 * The value of 'small' is determined here. Requests kalloc()
134 * with sizes greater than msg_ool_size_small may fail.
135 */
136const vm_size_t msg_ool_size_small = KHEAP_MAX_SIZE;
137__startup_data
138static struct mach_vm_range ipc_kernel_range;
139__startup_data
140static struct mach_vm_range ipc_kernel_copy_range;
141KMEM_RANGE_REGISTER_STATIC(ipc_kernel_map, &ipc_kernel_range,
142 IPC_KERNEL_MAP_SIZE);
143KMEM_RANGE_REGISTER_STATIC(ipc_kernel_copy_map, &ipc_kernel_copy_range,
144 IPC_KERNEL_COPY_MAP_SIZE);
145
146/*
147 * Routine: ipc_init
148 * Purpose:
149 * Final initialization
150 */
151__startup_func
152static void
153ipc_init(void)
154{
155 kern_return_t kr;
156
157 /* create special spaces */
158
159 kr = ipc_space_create_special(spacep: &ipc_space_kernel);
160 assert(kr == KERN_SUCCESS);
161
162 kr = ipc_space_create_special(spacep: &ipc_space_reply);
163 assert(kr == KERN_SUCCESS);
164
165 /* initialize modules with hidden data structures */
166
167#if CONFIG_ARCADE
168 arcade_init();
169#endif
170
171 bool pinned_control_port_enabled_1p = !!(ipc_control_port_options & ICP_OPTIONS_1P_PINNED);
172 bool immovable_control_port_enabled_1p = !!(ipc_control_port_options & ICP_OPTIONS_1P_IMMOVABLE);
173
174 bool pinned_control_port_enabled_3p = !!(ipc_control_port_options & ICP_OPTIONS_3P_PINNED);
175 bool immovable_control_port_enabled_3p = !!(ipc_control_port_options & ICP_OPTIONS_3P_IMMOVABLE);
176
177 if (pinned_control_port_enabled_1p && !immovable_control_port_enabled_1p) {
178 kprintf(fmt: "Invalid ipc_control_port_options boot-arg: pinned control port cannot be enabled without immovability enforcement. Ignoring 1p pinning boot-arg.");
179 ipc_control_port_options &= ~ICP_OPTIONS_1P_PINNED;
180 }
181
182 if (pinned_control_port_enabled_3p && !immovable_control_port_enabled_3p) {
183 kprintf(fmt: "Invalid ipc_control_port_options boot-arg: pinned control port cannot be enabled without immovability enforcement. Ignoring 3p pinning boot-arg.");
184 ipc_control_port_options &= ~ICP_OPTIONS_3P_PINNED;
185 }
186
187 ipc_kernel_map = kmem_suballoc(parent: kernel_map, addr: &ipc_kernel_range.min_address,
188 IPC_KERNEL_MAP_SIZE, vmc_options: VM_MAP_CREATE_PAGEABLE,
189 VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, flags: KMS_PERMANENT | KMS_NOFAIL,
190 VM_KERN_MEMORY_IPC).kmr_submap;
191
192 ipc_kernel_copy_map = kmem_suballoc(parent: kernel_map, addr: &ipc_kernel_copy_range.min_address,
193 IPC_KERNEL_COPY_MAP_SIZE,
194 vmc_options: VM_MAP_CREATE_PAGEABLE | VM_MAP_CREATE_DISABLE_HOLELIST,
195 VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, flags: KMS_PERMANENT | KMS_NOFAIL,
196 VM_KERN_MEMORY_IPC).kmr_submap;
197
198 ipc_kernel_copy_map->no_zero_fill = TRUE;
199 ipc_kernel_copy_map->wait_for_space = TRUE;
200
201 ipc_host_init();
202 ux_handler_init();
203}
204STARTUP(MACH_IPC, STARTUP_RANK_LAST, ipc_init);
205